rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
hal_spi_lld.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2014-2015 Fabio Utzig
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17/**
18 * @file SPIv1/hal_spi_lld.h
19 * @brief KINETIS SPI subsystem low level driver header.
20 * @author andreika <prometheus.pcb@gmail.com>
21 *
22 * @addtogroup SPI
23 * @{
24 */
25
26#ifndef HAL_SPI_LLD_H_
27#define HAL_SPI_LLD_H_
28
29#if HAL_USE_SPI || defined(__DOXYGEN__)
30
31#include "fsl_common.h"
32#include "fsl_lpspi.h"
33
34/*===========================================================================*/
35/* Driver constants. */
36/*===========================================================================*/
37
38/**
39 * @brief Circular mode support flag.
40 */
41#define SPI_SUPPORTS_CIRCULAR TRUE
42
43/*===========================================================================*/
44/* Driver pre-compile time settings. */
45/*===========================================================================*/
46
47/**
48 * @name Configuration options
49 * @{
50 */
51/**
52 * @brief SPI0 driver enable switch.
53 * @details If set to @p TRUE the support for SPI0 is included.
54 * @note The default is @p FALSE.
55 */
56#if !defined(KINETIS_SPI_USE_SPI0) || defined(__DOXYGEN__)
57#define KINETIS_SPI_USE_SPI0 FALSE
58#endif
59
60/**
61 * @brief SPI0 interrupt priority level setting.
62 */
63#if !defined(KINETIS_SPI_SPI0_IRQ_PRIORITY) || defined(__DOXYGEN__)
64#define KINETIS_SPI_SPI0_IRQ_PRIORITY 10
65#endif
66
67/**
68 * @brief SPI1 driver enable switch.
69 * @details If set to @p TRUE the support for SPI0 is included.
70 * @note The default is @p FALSE.
71 */
72#if !defined(KINETIS_SPI_USE_SPI1) || defined(__DOXYGEN__)
73#define KINETIS_SPI_USE_SPI1 FALSE
74#endif
75
76/**
77 * @brief SPI1 interrupt priority level setting.
78 */
79#if !defined(KINETIS_SPI_SPI1_IRQ_PRIORITY) || defined(__DOXYGEN__)
80#define KINETIS_SPI_SPI1_IRQ_PRIORITY 10
81#endif
82
83/*===========================================================================*/
84/* Derived constants and error checks. */
85/*===========================================================================*/
86
87#if KINETIS_SPI_USE_SPI0 && !KINETIS_HAS_SPI0
88#error "SPI0 not present in the selected device"
89#endif
90
91#if KINETIS_SPI_USE_SPI1 && !KINETIS_HAS_SPI1
92#error "SPI1 not present in the selected device"
93#endif
94
95#if KINETIS_SPI_USE_SPI0 && KINETIS_SPI_USE_SPI1
96#error "Only one SPI peripheral can be enabled"
97#endif
98
99#if !(KINETIS_SPI_USE_SPI0 || KINETIS_SPI_USE_SPI1)
100#error "SPI driver activated but no SPI peripheral assigned"
101#endif
102
103#if KINETIS_SPI_USE_SPI0 && \
104 !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_SPI_SPI0_IRQ_PRIORITY)
105#error "Invalid IRQ priority assigned to SPI0"
106#endif
107
108#if KINETIS_SPI_USE_SPI1 && \
109 !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_SPI_SPI1_IRQ_PRIORITY)
110#error "Invalid IRQ priority assigned to SPI1"
111#endif
112
113/*===========================================================================*/
114/* Driver data structures and types. */
115/*===========================================================================*/
116
117/**
118 * @brief SPI notification callback type.
119 *
120 * @param[in] spip pointer to the @p SPIDriver object triggering the
121 * callback
122 */
123typedef void (*spicallback_t)(SPIDriver *spip);
124
125/**
126 * @brief Low level fields of the SPI configuration structure.
127 */
128#define spi_lld_config_fields \
129 /* The chip select port. */ \
130 ioportid_t ssport; \
131 /* The chip select pad number. */ \
132 uint_fast8_t sspad; \
133 /* SPI CR1 register initialization data.*/ \
134 uint16_t cr1; \
135 /* SPI CR2 register initialization data.*/ \
136 uint16_t cr2
137
138/**
139 * @brief Low level fields of the SPI driver structure.
140 */
141#define spi_lld_driver_fields \
142 /* Pointer to the SPIx registers block. */ \
143 LPSPI_Type *spi; \
144 /* Master or Slave. */ \
145 bool isMaster; \
146 /* Stores the data for SPI master transfer. */ \
147 lpspi_transfer_t handleXfer; \
148 /* Transfer flags (including kLPSPI_MasterPcs*) for this SPI config. */ \
149 int32_t flags; \
150 /* IRQ callback handle for SPI master non-blocking transfer. */ \
151 lpspi_master_handle_t masterHandle; \
152 /* IRQ callback handle for SPI slave non-blocking transfer. */ \
153 lpspi_slave_handle_t slaveHandle
154
155/*===========================================================================*/
156/* Driver macros. */
157/*===========================================================================*/
158
159/* TAR settings for n bits at SYSCLK / 2 */
160#define KINETIS_SPI_TAR_SYSCLK_DIV_2(n)\
161 SPIx_CTARn_FMSZ((n) - 1) | \
162 SPIx_CTARn_CPOL | \
163 SPIx_CTARn_CPHA | \
164 SPIx_CTARn_DBR | \
165 SPIx_CTARn_PBR(0) | \
166 SPIx_CTARn_BR(0) | \
167 SPIx_CTARn_CSSCK(0) | \
168 SPIx_CTARn_ASC(0) | \
169 SPIx_CTARn_DT(0)
170
171/* TAR settings for n bits at SYSCLK / 4096 for debugging */
172#define KINETIS_SPI_TAR_SYSCLK_DIV_4096(n) \
173 SPIx_CTARn_FMSZ(((n) - 1)) | \
174 SPIx_CTARn_CPOL | \
175 SPIx_CTARn_CPHA | \
176 SPIx_CTARn_PBR(0) | \
177 SPIx_CTARn_BR(0xB) | \
178 SPIx_CTARn_CSSCK(0xB) | \
179 SPIx_CTARn_ASC(0x7) | \
180 SPIx_CTARn_DT(0xB)
181
182#define KINETIS_SPI_TAR_8BIT_FAST KINETIS_SPI_TAR_SYSCLK_DIV_2(8)
183#define KINETIS_SPI_TAR_8BIT_SLOW KINETIS_SPI_TAR_SYSCLK_DIV_4096(8)
184
185#define KINETIS_SPI_TAR0_DEFAULT KINETIS_SPI_TAR_SYSCLK_DIV_2(8)
186#define KINETIS_SPI_TAR1_DEFAULT KINETIS_SPI_TAR_SYSCLK_DIV_2(8)
187
188/*===========================================================================*/
189/* External declarations. */
190/*===========================================================================*/
191
192#if KINETIS_SPI_USE_SPI0 && !defined(__DOXYGEN__)
193extern SPIDriver SPID1;
194#endif
195
196#if KINETIS_SPI_USE_SPI1 && !defined(__DOXYGEN__)
197extern SPIDriver SPID2;
198#endif
199
200#ifdef __cplusplus
201extern "C" {
202#endif
203 void spi_lld_init(void);
204 void spi_lld_start(SPIDriver *spip);
205 void spi_lld_stop(SPIDriver *spip);
206 void spi_lld_select(SPIDriver *spip);
207 void spi_lld_unselect(SPIDriver *spip);
208 void spi_lld_ignore(SPIDriver *spip, size_t n);
209 void spi_lld_exchange(SPIDriver *spip, size_t n,
210 const void *txbuf, void *rxbuf);
211 void spi_lld_send(SPIDriver *spip, size_t n, const void *txbuf);
212 void spi_lld_receive(SPIDriver *spip, size_t n, void *rxbuf);
213#if (SPI_SUPPORTS_CIRCULAR == TRUE) || defined(__DOXYGEN__)
214 void spi_lld_abort(SPIDriver *spip);
215#endif
216 uint16_t spi_lld_polled_exchange(SPIDriver *spip, uint16_t frame);
217#ifdef __cplusplus
218}
219#endif
220
221#endif /* HAL_USE_SPI */
222
223#endif /* HAL_SPI_LLD_H_ */
224
225/** @} */
void spi_lld_ignore(SPIDriver *spip, size_t n)
Ignores data on the SPI bus.
SPIDriver SPID1
SPI0 driver identifier.
Definition hal_spi_lld.c:42
void spi_lld_abort(SPIDriver *spip)
Aborts the ongoing SPI operation, if any.
void spi_lld_select(SPIDriver *spip)
Asserts the slave select signal and prepares for transfers.
void spi_lld_receive(SPIDriver *spip, size_t n, void *rxbuf)
Receives data from the SPI bus.
void spi_lld_init(void)
Low level SPI driver initialization.
void spi_lld_exchange(SPIDriver *spip, size_t n, const void *txbuf, void *rxbuf)
Exchanges data on the SPI bus.
void spi_lld_stop(SPIDriver *spip)
Deactivates the SPI peripheral.
void spi_lld_send(SPIDriver *spip, size_t n, const void *txbuf)
Sends data over the SPI bus.
uint16_t spi_lld_polled_exchange(SPIDriver *spip, uint16_t frame)
Exchanges one frame using a polled wait.
void(* spicallback_t)(SPIDriver *spip)
SPI notification callback type.
void spi_lld_start(SPIDriver *spip)
Configures and activates the SPI peripheral.
SPIDriver SPID2
SPI1 driver identifier.
Definition hal_spi_lld.c:47
void spi_lld_unselect(SPIDriver *spip)
Deasserts the slave select signal.