Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2 | Rev 9 | ||
---|---|---|---|
Line 27... | Line 27... | ||
27 | (+++) Configure these SPI pins as alternate function push-pull |
27 | (+++) Configure these SPI pins as alternate function push-pull |
28 | (##) NVIC configuration if you need to use interrupt process |
28 | (##) NVIC configuration if you need to use interrupt process |
29 | (+++) Configure the SPIx interrupt priority |
29 | (+++) Configure the SPIx interrupt priority |
30 | (+++) Enable the NVIC SPI IRQ handle |
30 | (+++) Enable the NVIC SPI IRQ handle |
31 | (##) DMA Configuration if you need to use DMA process |
31 | (##) DMA Configuration if you need to use DMA process |
32 | (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive Channel |
32 | (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive Stream/Channel |
33 | (+++) Enable the DMAx clock |
33 | (+++) Enable the DMAx clock |
34 | (+++) Configure the DMA handle parameters |
34 | (+++) Configure the DMA handle parameters |
35 | (+++) Configure the DMA Tx or Rx Channel |
35 | (+++) Configure the DMA Tx or Rx Stream/Channel |
36 | (+++) Associate the initilalized hdma_tx(or _rx) handle to the hspi DMA Tx (or Rx) handle |
36 | (+++) Associate the initialized hdma_tx(or _rx) handle to the hspi DMA Tx or Rx handle |
37 | (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx or Rx Channel |
37 | (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx or Rx Stream/Channel |
38 | 38 | ||
39 | (#) Program the Mode, BidirectionalMode , Data size, Baudrate Prescaler, NSS |
39 | (#) Program the Mode, BidirectionalMode , Data size, Baudrate Prescaler, NSS |
40 | management, Clock polarity and phase, FirstBit and CRC configuration in the hspi Init structure. |
40 | management, Clock polarity and phase, FirstBit and CRC configuration in the hspi Init structure. |
41 | 41 | ||
42 | (#) Initialize the SPI registers by calling the HAL_SPI_Init() API: |
42 | (#) Initialize the SPI registers by calling the HAL_SPI_Init() API: |
Line 50... | Line 50... | ||
50 | (#) The CRC feature is not managed when the DMA circular mode is enabled |
50 | (#) The CRC feature is not managed when the DMA circular mode is enabled |
51 | (#) When the SPI DMA Pause/Stop features are used, we must use the following APIs |
51 | (#) When the SPI DMA Pause/Stop features are used, we must use the following APIs |
52 | the HAL_SPI_DMAPause()/ HAL_SPI_DMAStop() only under the SPI callbacks |
52 | the HAL_SPI_DMAPause()/ HAL_SPI_DMAStop() only under the SPI callbacks |
53 | [..] |
53 | [..] |
54 | Master Receive mode restriction: |
54 | Master Receive mode restriction: |
55 | (#) In Master unidirectional receive-only mode (MSTR =1, BIDIMODE=0, RXONLY=0) or |
55 | (#) In Master unidirectional receive-only mode (MSTR =1, BIDIMODE=0, RXONLY=1) or |
56 | bidirectional receive mode (MSTR=1, BIDIMODE=1, BIDIOE=0), to ensure that the SPI |
56 | bidirectional receive mode (MSTR=1, BIDIMODE=1, BIDIOE=0), to ensure that the SPI |
57 | does not initiate a new transfer the following procedure has to be respected: |
57 | does not initiate a new transfer the following procedure has to be respected: |
58 | (##) HAL_SPI_DeInit() |
58 | (##) HAL_SPI_DeInit() |
59 | (##) HAL_SPI_Init() |
59 | (##) HAL_SPI_Init() |
- | 60 | [..] |
|
- | 61 | Callback registration: |
|
60 | 62 | ||
- | 63 | (#) The compilation flag USE_HAL_SPI_REGISTER_CALLBACKS when set to 1U |
|
- | 64 | allows the user to configure dynamically the driver callbacks. |
|
- | 65 | Use Functions HAL_SPI_RegisterCallback() to register an interrupt callback. |
|
- | 66 | ||
- | 67 | Function HAL_SPI_RegisterCallback() allows to register following callbacks: |
|
- | 68 | (++) TxCpltCallback : SPI Tx Completed callback |
|
- | 69 | (++) RxCpltCallback : SPI Rx Completed callback |
|
- | 70 | (++) TxRxCpltCallback : SPI TxRx Completed callback |
|
- | 71 | (++) TxHalfCpltCallback : SPI Tx Half Completed callback |
|
- | 72 | (++) RxHalfCpltCallback : SPI Rx Half Completed callback |
|
- | 73 | (++) TxRxHalfCpltCallback : SPI TxRx Half Completed callback |
|
- | 74 | (++) ErrorCallback : SPI Error callback |
|
- | 75 | (++) AbortCpltCallback : SPI Abort callback |
|
- | 76 | (++) MspInitCallback : SPI Msp Init callback |
|
- | 77 | (++) MspDeInitCallback : SPI Msp DeInit callback |
|
- | 78 | This function takes as parameters the HAL peripheral handle, the Callback ID |
|
- | 79 | and a pointer to the user callback function. |
|
- | 80 | ||
- | 81 | ||
- | 82 | (#) Use function HAL_SPI_UnRegisterCallback to reset a callback to the default |
|
- | 83 | weak function. |
|
- | 84 | HAL_SPI_UnRegisterCallback takes as parameters the HAL peripheral handle, |
|
- | 85 | and the Callback ID. |
|
- | 86 | This function allows to reset following callbacks: |
|
- | 87 | (++) TxCpltCallback : SPI Tx Completed callback |
|
- | 88 | (++) RxCpltCallback : SPI Rx Completed callback |
|
- | 89 | (++) TxRxCpltCallback : SPI TxRx Completed callback |
|
- | 90 | (++) TxHalfCpltCallback : SPI Tx Half Completed callback |
|
- | 91 | (++) RxHalfCpltCallback : SPI Rx Half Completed callback |
|
- | 92 | (++) TxRxHalfCpltCallback : SPI TxRx Half Completed callback |
|
- | 93 | (++) ErrorCallback : SPI Error callback |
|
- | 94 | (++) AbortCpltCallback : SPI Abort callback |
|
- | 95 | (++) MspInitCallback : SPI Msp Init callback |
|
- | 96 | (++) MspDeInitCallback : SPI Msp DeInit callback |
|
- | 97 | ||
- | 98 | [..] |
|
- | 99 | By default, after the HAL_SPI_Init() and when the state is HAL_SPI_STATE_RESET |
|
- | 100 | all callbacks are set to the corresponding weak functions: |
|
- | 101 | examples HAL_SPI_MasterTxCpltCallback(), HAL_SPI_MasterRxCpltCallback(). |
|
- | 102 | Exception done for MspInit and MspDeInit functions that are |
|
- | 103 | reset to the legacy weak functions in the HAL_SPI_Init()/ HAL_SPI_DeInit() only when |
|
- | 104 | these callbacks are null (not registered beforehand). |
|
- | 105 | If MspInit or MspDeInit are not null, the HAL_SPI_Init()/ HAL_SPI_DeInit() |
|
- | 106 | keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state. |
|
- | 107 | ||
- | 108 | [..] |
|
- | 109 | Callbacks can be registered/unregistered in HAL_SPI_STATE_READY state only. |
|
- | 110 | Exception done MspInit/MspDeInit functions that can be registered/unregistered |
|
- | 111 | in HAL_SPI_STATE_READY or HAL_SPI_STATE_RESET state, |
|
- | 112 | thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit. |
|
- | 113 | Then, the user first registers the MspInit/MspDeInit user callbacks |
|
- | 114 | using HAL_SPI_RegisterCallback() before calling HAL_SPI_DeInit() |
|
- | 115 | or HAL_SPI_Init() function. |
|
- | 116 | ||
61 | @endverbatim |
117 | [..] |
- | 118 | When the compilation define USE_HAL_PPP_REGISTER_CALLBACKS is set to 0 or |
|
- | 119 | not defined, the callback registering feature is not available |
|
- | 120 | and weak (surcharged) callbacks are used. |
|
62 | 121 | ||
63 | Using the HAL it is not possible to reach all supported SPI frequency with the differents SPI Modes, |
- | |
64 | the following tables resume the max SPI frequency reached with data size 8bits/16bits, |
- | |
65 | according to frequency used on APBx Peripheral Clock (fPCLK) used by the SPI instance : |
- | |
66 | |
- | |
67 | DataSize = SPI_DATASIZE_8BIT: |
- | |
68 | +--------------------------------------------------------------------------------------------------+ |
- | |
69 | | | | 2Lines Fullduplex | 2Lines RxOnly | 1Line | |
- | |
70 | | Process | Tranfert mode |-----------------------|-----------------------|-----------------------| |
- | |
71 | | | | Master | Slave | Master | Slave | Master | Slave | |
- | |
72 | |==================================================================================================| |
- | |
73 | | T | Polling | fPCLK/2 | fPCLK/16 | NA | NA | NA | NA | |
- | |
74 | | X |----------------|-----------|-----------|-----------|-----------|-----------|-----------| |
- | |
75 | | / | Interrupt | fPCLK/8 | fPCLK/32 | NA | NA | NA | NA | |
- | |
76 | | R |----------------|-----------|-----------|-----------|-----------|-----------|-----------| |
- | |
77 | | X | DMA | fPCLK/2 | fPCLK/4 | NA | NA | NA | NA | |
- | |
78 | |=========|================|===========|===========|===========|===========|===========|===========| |
- | |
79 | | | Polling | fPCLK/4 | fPCLK/8 | fPCLK/8 | fPCLK/16 | fPCLK/64 | fPCLK/2 | |
- | |
80 | | |----------------|-----------|-----------|-----------|-----------|-----------|-----------| |
- | |
81 | | R | Interrupt | fPCLK/8 | fPCLK/16 | fPCLK/32 | fPCLK/16 | fPCLK/64 | fPCLK/4 | |
- | |
82 | | X |----------------|-----------|-----------|-----------|-----------|-----------|-----------| |
- | |
83 | | | DMA | fPCLK/2 | fPCLK/16 | fPCLK/8 | fPCLK/16 | fPCLK/64 | fPCLK/2 | |
- | |
84 | |=========|================|===========|===========|===========|===========|===========|===========| |
- | |
85 | | | Polling | fPCLK/2 | fPCLK/2 | NA | NA | fPCLK/2 | fPCLK/32 | |
- | |
86 | | |----------------|-----------|-----------|-----------|-----------|-----------|-----------| |
- | |
87 | | T | Interrupt | fPCLK/8 | fPCLK/16 | NA | NA | fPCLK/2 | fPCLK/64 | |
- | |
88 | | X |----------------|-----------|-----------|-----------|-----------|-----------|-----------| |
- | |
89 | | | DMA | fPCLK/2 | fPCLK/4 | NA | NA | fPCLK/2 | fPCLK/32 | |
- | |
90 | +--------------------------------------------------------------------------------------------------+ |
- | |
91 | - | ||
92 | DataSize = SPI_DATASIZE_16BIT: |
- | |
93 | +--------------------------------------------------------------------------------------------------+ |
- | |
94 | | | | 2Lines Fullduplex | 2Lines RxOnly | 1Line | |
- | |
95 | | Process | Tranfert mode |-----------------------|-----------------------|-----------------------| |
- | |
96 | | | | Master | Slave | Master | Slave | Master | Slave | |
- | |
97 | |==================================================================================================| |
- | |
98 | | T | Polling | fPCLK/4 | fPCLK/4 | NA | NA | NA | NA | |
- | |
99 | | X |----------------|-----------|-----------|-----------|-----------|-----------|-----------| |
- | |
100 | | / | Interrupt | fPCLK/8 | fPCLK/16 | NA | NA | NA | NA | |
- | |
101 | | R |----------------|-----------|-----------|-----------|-----------|-----------|-----------| |
- | |
102 | | X | DMA | fPCLK/2 | fPCLK/4 | NA | NA | NA | NA | |
- | |
103 | |=========|================|===========|===========|===========|===========|===========|===========| |
- | |
104 | | | Polling | fPCLK/4 | fPCLK/8 | fPCLK/4 | fPCLK/8 | fPCLK/64 | fPCLK/2 | |
- | |
105 | | |----------------|-----------|-----------|-----------|-----------|-----------|-----------| |
- | |
106 | | R | Interrupt | fPCLK/8 | fPCLK/8 | fPCLK/128 | fPCLK/8 | fPCLK/128 | fPCLK/4 | |
- | |
107 | | X |----------------|-----------|-----------|-----------|-----------|-----------|-----------| |
- | |
108 | | | DMA | fPCLK/2 | fPCLK/2 | fPCLK/128 | fPCLK/16 | fPCLK/64 | fPCLK/2 | |
- | |
109 | |=========|================|===========|===========|===========|===========|===========|===========| |
- | |
110 | | | Polling | fPCLK/2 | fPCLK/4 | NA | NA | fPCLK/4 | fPCLK/8 | |
- | |
111 | | |----------------|-----------|-----------|-----------|-----------|-----------|-----------| |
- | |
112 | | T | Interrupt | fPCLK/4 | fPCLK/8 | NA | NA | fPCLK/4 | fPCLK/4 | |
- | |
113 | | X |----------------|-----------|-----------|-----------|-----------|-----------|-----------| |
- | |
114 | | | DMA | fPCLK/2 | fPCLK/2 | NA | NA | fPCLK/4 | fPCLK/8 | |
- | |
115 | +--------------------------------------------------------------------------------------------------+ |
- | |
116 | [..] |
122 | [..] |
- | 123 | Using the HAL it is not possible to reach all supported SPI frequency with the different SPI Modes, |
|
- | 124 | the following table resume the max SPI frequency reached with data size 8bits/16bits, |
|
- | 125 | according to frequency of the APBx Peripheral Clock (fPCLK) used by the SPI instance. |
|
- | 126 | ||
- | 127 | @endverbatim |
|
- | 128 | ||
- | 129 | Additional table : |
|
- | 130 | ||
- | 131 | DataSize = SPI_DATASIZE_8BIT: |
|
- | 132 | +----------------------------------------------------------------------------------------------+ |
|
- | 133 | | | | 2Lines Fullduplex | 2Lines RxOnly | 1Line | |
|
- | 134 | | Process | Transfer mode |---------------------|----------------------|----------------------| |
|
- | 135 | | | | Master | Slave | Master | Slave | Master | Slave | |
|
- | 136 | |==============================================================================================| |
|
- | 137 | | T | Polling | Fpclk/2 | Fpclk/2 | NA | NA | NA | NA | |
|
- | 138 | | X |----------------|----------|----------|-----------|----------|-----------|----------| |
|
- | 139 | | / | Interrupt | Fpclk/4 | Fpclk/8 | NA | NA | NA | NA | |
|
- | 140 | | R |----------------|----------|----------|-----------|----------|-----------|----------| |
|
- | 141 | | X | DMA | Fpclk/2 | Fpclk/2 | NA | NA | NA | NA | |
|
- | 142 | |=========|================|==========|==========|===========|==========|===========|==========| |
|
- | 143 | | | Polling | Fpclk/2 | Fpclk/2 | Fpclk/64 | Fpclk/2 | Fpclk/64 | Fpclk/2 | |
|
- | 144 | | |----------------|----------|----------|-----------|----------|-----------|----------| |
|
- | 145 | | R | Interrupt | Fpclk/8 | Fpclk/8 | Fpclk/64 | Fpclk/2 | Fpclk/64 | Fpclk/2 | |
|
- | 146 | | X |----------------|----------|----------|-----------|----------|-----------|----------| |
|
- | 147 | | | DMA | Fpclk/2 | Fpclk/2 | Fpclk/64 | Fpclk/2 | Fpclk/128 | Fpclk/2 | |
|
- | 148 | |=========|================|==========|==========|===========|==========|===========|==========| |
|
- | 149 | | | Polling | Fpclk/2 | Fpclk/4 | NA | NA | Fpclk/2 | Fpclk/64 | |
|
- | 150 | | |----------------|----------|----------|-----------|----------|-----------|----------| |
|
- | 151 | | T | Interrupt | Fpclk/2 | Fpclk/4 | NA | NA | Fpclk/2 | Fpclk/64 | |
|
- | 152 | | X |----------------|----------|----------|-----------|----------|-----------|----------| |
|
- | 153 | | | DMA | Fpclk/2 | Fpclk/2 | NA | NA | Fpclk/2 | Fpclk/128| |
|
- | 154 | +----------------------------------------------------------------------------------------------+ |
|
- | 155 | ||
- | 156 | DataSize = SPI_DATASIZE_16BIT: |
|
- | 157 | +----------------------------------------------------------------------------------------------+ |
|
- | 158 | | | | 2Lines Fullduplex | 2Lines RxOnly | 1Line | |
|
- | 159 | | Process | Transfer mode |---------------------|----------------------|----------------------| |
|
- | 160 | | | | Master | Slave | Master | Slave | Master | Slave | |
|
- | 161 | |==============================================================================================| |
|
- | 162 | | T | Polling | Fpclk/2 | Fpclk/2 | NA | NA | NA | NA | |
|
- | 163 | | X |----------------|----------|----------|-----------|----------|-----------|----------| |
|
- | 164 | | / | Interrupt | Fpclk/4 | Fpclk/4 | NA | NA | NA | NA | |
|
- | 165 | | R |----------------|----------|----------|-----------|----------|-----------|----------| |
|
- | 166 | | X | DMA | Fpclk/2 | Fpclk/2 | NA | NA | NA | NA | |
|
- | 167 | |=========|================|==========|==========|===========|==========|===========|==========| |
|
- | 168 | | | Polling | Fpclk/2 | Fpclk/2 | Fpclk/64 | Fpclk/2 | Fpclk/32 | Fpclk/2 | |
|
- | 169 | | |----------------|----------|----------|-----------|----------|-----------|----------| |
|
- | 170 | | R | Interrupt | Fpclk/4 | Fpclk/4 | Fpclk/64 | Fpclk/2 | Fpclk/64 | Fpclk/2 | |
|
- | 171 | | X |----------------|----------|----------|-----------|----------|-----------|----------| |
|
- | 172 | | | DMA | Fpclk/2 | Fpclk/2 | Fpclk/64 | Fpclk/2 | Fpclk/128 | Fpclk/2 | |
|
- | 173 | |=========|================|==========|==========|===========|==========|===========|==========| |
|
- | 174 | | | Polling | Fpclk/2 | Fpclk/2 | NA | NA | Fpclk/2 | Fpclk/32 | |
|
- | 175 | | |----------------|----------|----------|-----------|----------|-----------|----------| |
|
- | 176 | | T | Interrupt | Fpclk/2 | Fpclk/2 | NA | NA | Fpclk/2 | Fpclk/64 | |
|
- | 177 | | X |----------------|----------|----------|-----------|----------|-----------|----------| |
|
- | 178 | | | DMA | Fpclk/2 | Fpclk/2 | NA | NA | Fpclk/2 | Fpclk/128| |
|
- | 179 | +----------------------------------------------------------------------------------------------+ |
|
117 | (@) The max SPI frequency depend on SPI data size (8bits, 16bits), |
180 | @note The max SPI frequency depend on SPI data size (8bits, 16bits), |
118 | SPI mode(2 Lines fullduplex, 2 lines RxOnly, 1 line TX/RX) and Process mode (Polling, IT, DMA). |
181 | SPI mode(2 Lines fullduplex, 2 lines RxOnly, 1 line TX/RX) and Process mode (Polling, IT, DMA). |
119 | (@) |
182 | @note |
120 | (+@) TX/RX processes are HAL_SPI_TransmitReceive(), HAL_SPI_TransmitReceive_IT() and HAL_SPI_TransmitReceive_DMA() |
183 | (#) TX/RX processes are HAL_SPI_TransmitReceive(), HAL_SPI_TransmitReceive_IT() and HAL_SPI_TransmitReceive_DMA() |
121 | (+@) RX processes are HAL_SPI_Receive(), HAL_SPI_Receive_IT() and HAL_SPI_Receive_DMA() |
184 | (#) RX processes are HAL_SPI_Receive(), HAL_SPI_Receive_IT() and HAL_SPI_Receive_DMA() |
122 | (+@) TX processes are HAL_SPI_Transmit(), HAL_SPI_Transmit_IT() and HAL_SPI_Transmit_DMA() |
185 | (#) TX processes are HAL_SPI_Transmit(), HAL_SPI_Transmit_IT() and HAL_SPI_Transmit_DMA() |
- | 186 | ||
123 | ****************************************************************************** |
187 | ****************************************************************************** |
124 | * @attention |
188 | * @attention |
125 | * |
189 | * |
126 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
190 | * <h2><center>© Copyright (c) 2016 STMicroelectronics. |
127 | * |
- | |
128 | * Redistribution and use in source and binary forms, with or without modification, |
- | |
129 | * are permitted provided that the following conditions are met: |
- | |
130 | * 1. Redistributions of source code must retain the above copyright notice, |
- | |
131 | * this list of conditions and the following disclaimer. |
- | |
132 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
- | |
133 | * this list of conditions and the following disclaimer in the documentation |
- | |
134 | * and/or other materials provided with the distribution. |
- | |
135 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
- | |
136 | * may be used to endorse or promote products derived from this software |
- | |
137 | * without specific prior written permission. |
191 | * All rights reserved.</center></h2> |
138 | * |
192 | * |
139 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
193 | * This software component is licensed by ST under BSD 3-Clause license, |
140 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
- | |
141 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
- | |
142 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
- | |
143 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
- | |
144 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
194 | * the "License"; You may not use this file except in compliance with the |
145 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
195 | * License. You may obtain a copy of the License at: |
146 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
- | |
147 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
- | |
148 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
196 | * opensource.org/licenses/BSD-3-Clause |
149 | * |
197 | * |
150 | ****************************************************************************** |
198 | ****************************************************************************** |
151 | */ |
199 | */ |
152 | 200 | ||
153 | /* Includes ------------------------------------------------------------------*/ |
201 | /* Includes ------------------------------------------------------------------*/ |
154 | #include "stm32f1xx_hal.h" |
202 | #include "stm32f1xx_hal.h" |
155 | 203 | ||
156 | /** @addtogroup STM32F1xx_HAL_Driver |
204 | /** @addtogroup STM32F1xx_HAL_Driver |
157 | * @{ |
205 | * @{ |
158 | */ |
206 | */ |
- | 207 | ||
159 | /** @defgroup SPI SPI |
208 | /** @defgroup SPI SPI |
160 | * @brief SPI HAL module driver |
209 | * @brief SPI HAL module driver |
161 | * @{ |
210 | * @{ |
162 | */ |
211 | */ |
163 | #ifdef HAL_SPI_MODULE_ENABLED |
212 | #ifdef HAL_SPI_MODULE_ENABLED |
164 | 213 | ||
165 | /* Private typedef -----------------------------------------------------------*/ |
214 | /* Private typedef -----------------------------------------------------------*/ |
166 | /* Private defines -----------------------------------------------------------*/ |
215 | /* Private defines -----------------------------------------------------------*/ |
- | 216 | #if (USE_SPI_CRC != 0U) && defined(SPI_CRC_ERROR_WORKAROUND_FEATURE) |
|
- | 217 | /* CRC WORKAOUND FEATURE: Variable used to determine if device is impacted by implementation |
|
- | 218 | * of workaround related to wrong CRC errors detection on SPI2. Conditions in which this workaround |
|
- | 219 | * has to be applied, are: |
|
- | 220 | * - STM32F101CDE/STM32F103CDE |
|
- | 221 | * - Revision ID : Z |
|
- | 222 | * - SPI2 |
|
- | 223 | * - In receive only mode, with CRC calculation enabled, at the end of the CRC reception, |
|
- | 224 | * the software needs to check the CRCERR flag. If it is found set, read back the SPI_RXCRC: |
|
- | 225 | * + If the value is 0, the complete data transfer is successful. |
|
- | 226 | * + Otherwise, one or more errors have been detected during the data transfer by CPU or DMA. |
|
- | 227 | * If CRCERR is found reset, the complete data transfer is considered successful. |
|
- | 228 | * |
|
- | 229 | * Check RevisionID value for identifying if Device is Rev Z (0x0001) in order to enable workaround for |
|
- | 230 | * CRC errors wrongly detected |
|
- | 231 | */ |
|
- | 232 | /* Pb is that ES_STM32F10xxCDE also identify an issue in Debug registers access while not in Debug mode |
|
- | 233 | * Revision ID information is only available in Debug mode, so Workaround could not be implemented |
|
- | 234 | * to distinguish Rev Z devices (issue present) from more recent version (issue fixed). |
|
- | 235 | * So, in case of Revison Z F101 or F103 devices, below define should be assigned to 1. |
|
- | 236 | */ |
|
- | 237 | #define USE_SPI_CRC_ERROR_WORKAROUND 0U |
|
- | 238 | #endif |
|
167 | /** @defgroup SPI_Private_Constants SPI Private Constants |
239 | /** @defgroup SPI_Private_Constants SPI Private Constants |
168 | * @{ |
240 | * @{ |
169 | */ |
241 | */ |
170 | #define SPI_DEFAULT_TIMEOUT 100U |
242 | #define SPI_DEFAULT_TIMEOUT 100U |
171 | /** |
243 | /** |
Line 173... | Line 245... | ||
173 | */ |
245 | */ |
174 | 246 | ||
175 | /* Private macros ------------------------------------------------------------*/ |
247 | /* Private macros ------------------------------------------------------------*/ |
176 | /* Private variables ---------------------------------------------------------*/ |
248 | /* Private variables ---------------------------------------------------------*/ |
177 | /* Private function prototypes -----------------------------------------------*/ |
249 | /* Private function prototypes -----------------------------------------------*/ |
178 | /** @addtogroup SPI_Private_Functions |
250 | /** @defgroup SPI_Private_Functions SPI Private Functions |
179 | * @{ |
251 | * @{ |
180 | */ |
252 | */ |
181 | static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma); |
253 | static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma); |
182 | static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma); |
254 | static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma); |
183 | static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma); |
255 | static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma); |
Line 186... | Line 258... | ||
186 | static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma); |
258 | static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma); |
187 | static void SPI_DMAError(DMA_HandleTypeDef *hdma); |
259 | static void SPI_DMAError(DMA_HandleTypeDef *hdma); |
188 | static void SPI_DMAAbortOnError(DMA_HandleTypeDef *hdma); |
260 | static void SPI_DMAAbortOnError(DMA_HandleTypeDef *hdma); |
189 | static void SPI_DMATxAbortCallback(DMA_HandleTypeDef *hdma); |
261 | static void SPI_DMATxAbortCallback(DMA_HandleTypeDef *hdma); |
190 | static void SPI_DMARxAbortCallback(DMA_HandleTypeDef *hdma); |
262 | static void SPI_DMARxAbortCallback(DMA_HandleTypeDef *hdma); |
191 | static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, uint32_t State, uint32_t Timeout, uint32_t Tickstart); |
263 | static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus State, |
- | 264 | uint32_t Timeout, uint32_t Tickstart); |
|
192 | static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi); |
265 | static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi); |
193 | static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi); |
266 | static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi); |
194 | static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi); |
267 | static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi); |
195 | static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi); |
268 | static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi); |
196 | static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi); |
269 | static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi); |
Line 206... | Line 279... | ||
206 | static void SPI_AbortRx_ISR(SPI_HandleTypeDef *hspi); |
279 | static void SPI_AbortRx_ISR(SPI_HandleTypeDef *hspi); |
207 | static void SPI_AbortTx_ISR(SPI_HandleTypeDef *hspi); |
280 | static void SPI_AbortTx_ISR(SPI_HandleTypeDef *hspi); |
208 | static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi); |
281 | static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi); |
209 | static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi); |
282 | static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi); |
210 | static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi); |
283 | static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi); |
211 | static HAL_StatusTypeDef SPI_CheckFlag_BSY(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart); |
284 | static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart); |
- | 285 | static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart); |
|
212 | /** |
286 | /** |
213 | * @} |
287 | * @} |
214 | */ |
288 | */ |
215 | 289 | ||
216 | /* Exported functions --------------------------------------------------------*/ |
290 | /* Exported functions --------------------------------------------------------*/ |
217 | /** @defgroup SPI_Exported_Functions SPI Exported Functions |
291 | /** @defgroup SPI_Exported_Functions SPI Exported Functions |
218 | * @{ |
292 | * @{ |
219 | */ |
293 | */ |
220 | 294 | ||
221 | /** @defgroup SPI_Exported_Functions_Group1 Initialization and de-initialization functions |
295 | /** @defgroup SPI_Exported_Functions_Group1 Initialization and de-initialization functions |
222 | * @brief Initialization and Configuration functions |
296 | * @brief Initialization and Configuration functions |
223 | * |
297 | * |
224 | @verbatim |
298 | @verbatim |
225 | =============================================================================== |
299 | =============================================================================== |
226 | ##### Initialization and de-initialization functions ##### |
300 | ##### Initialization and de-initialization functions ##### |
227 | =============================================================================== |
301 | =============================================================================== |
228 | [..] This subsection provides a set of functions allowing to initialize and |
302 | [..] This subsection provides a set of functions allowing to initialize and |
Line 252... | Line 326... | ||
252 | */ |
326 | */ |
253 | 327 | ||
254 | /** |
328 | /** |
255 | * @brief Initialize the SPI according to the specified parameters |
329 | * @brief Initialize the SPI according to the specified parameters |
256 | * in the SPI_InitTypeDef and initialize the associated handle. |
330 | * in the SPI_InitTypeDef and initialize the associated handle. |
257 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
331 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
258 | * the configuration information for SPI module. |
332 | * the configuration information for SPI module. |
259 | * @retval HAL status |
333 | * @retval HAL status |
260 | */ |
334 | */ |
261 | __weak HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi) |
335 | HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi) |
262 | { |
336 | { |
263 | /* Check the SPI handle allocation */ |
337 | /* Check the SPI handle allocation */ |
264 | if(hspi == NULL) |
338 | if (hspi == NULL) |
265 | { |
339 | { |
266 | return HAL_ERROR; |
340 | return HAL_ERROR; |
267 | } |
341 | } |
268 | 342 | ||
269 | /* Check the parameters */ |
343 | /* Check the parameters */ |
270 | assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance)); |
344 | assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance)); |
271 | assert_param(IS_SPI_MODE(hspi->Init.Mode)); |
345 | assert_param(IS_SPI_MODE(hspi->Init.Mode)); |
272 | assert_param(IS_SPI_DIRECTION(hspi->Init.Direction)); |
346 | assert_param(IS_SPI_DIRECTION(hspi->Init.Direction)); |
273 | assert_param(IS_SPI_DATASIZE(hspi->Init.DataSize)); |
347 | assert_param(IS_SPI_DATASIZE(hspi->Init.DataSize)); |
274 | assert_param(IS_SPI_CPOL(hspi->Init.CLKPolarity)); |
- | |
275 | assert_param(IS_SPI_CPHA(hspi->Init.CLKPhase)); |
- | |
276 | assert_param(IS_SPI_NSS(hspi->Init.NSS)); |
348 | assert_param(IS_SPI_NSS(hspi->Init.NSS)); |
277 | assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler)); |
349 | assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler)); |
278 | assert_param(IS_SPI_FIRST_BIT(hspi->Init.FirstBit)); |
350 | assert_param(IS_SPI_FIRST_BIT(hspi->Init.FirstBit)); |
- | 351 | /* TI mode is not supported on this device. |
|
- | 352 | TIMode parameter is mandatory equal to SPI_TIMODE_DISABLE */ |
|
- | 353 | assert_param(IS_SPI_TIMODE(hspi->Init.TIMode)); |
|
- | 354 | if (hspi->Init.TIMode == SPI_TIMODE_DISABLE) |
|
- | 355 | { |
|
- | 356 | assert_param(IS_SPI_CPOL(hspi->Init.CLKPolarity)); |
|
- | 357 | assert_param(IS_SPI_CPHA(hspi->Init.CLKPhase)); |
|
- | 358 | ||
- | 359 | if (hspi->Init.Mode == SPI_MODE_MASTER) |
|
- | 360 | { |
|
- | 361 | assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler)); |
|
- | 362 | } |
|
- | 363 | else |
|
- | 364 | { |
|
- | 365 | /* Baudrate prescaler not use in Motoraola Slave mode. force to default value */ |
|
- | 366 | hspi->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; |
|
- | 367 | } |
|
- | 368 | } |
|
- | 369 | else |
|
- | 370 | { |
|
- | 371 | assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler)); |
|
279 | 372 | ||
- | 373 | /* Force polarity and phase to TI protocaol requirements */ |
|
- | 374 | hspi->Init.CLKPolarity = SPI_POLARITY_LOW; |
|
- | 375 | hspi->Init.CLKPhase = SPI_PHASE_1EDGE; |
|
- | 376 | } |
|
280 | #if (USE_SPI_CRC != 0U) |
377 | #if (USE_SPI_CRC != 0U) |
281 | assert_param(IS_SPI_CRC_CALCULATION(hspi->Init.CRCCalculation)); |
378 | assert_param(IS_SPI_CRC_CALCULATION(hspi->Init.CRCCalculation)); |
282 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
379 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
283 | { |
380 | { |
284 | assert_param(IS_SPI_CRC_POLYNOMIAL(hspi->Init.CRCPolynomial)); |
381 | assert_param(IS_SPI_CRC_POLYNOMIAL(hspi->Init.CRCPolynomial)); |
285 | } |
382 | } |
286 | #else |
383 | #else |
287 | hspi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
384 | hspi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
288 | #endif /* USE_SPI_CRC */ |
385 | #endif /* USE_SPI_CRC */ |
289 | 386 | ||
290 | if(hspi->State == HAL_SPI_STATE_RESET) |
387 | if (hspi->State == HAL_SPI_STATE_RESET) |
291 | { |
388 | { |
292 | /* Allocate lock resource and initialize it */ |
389 | /* Allocate lock resource and initialize it */ |
293 | hspi->Lock = HAL_UNLOCKED; |
390 | hspi->Lock = HAL_UNLOCKED; |
294 | 391 | ||
- | 392 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 393 | /* Init the SPI Callback settings */ |
|
- | 394 | hspi->TxCpltCallback = HAL_SPI_TxCpltCallback; /* Legacy weak TxCpltCallback */ |
|
- | 395 | hspi->RxCpltCallback = HAL_SPI_RxCpltCallback; /* Legacy weak RxCpltCallback */ |
|
- | 396 | hspi->TxRxCpltCallback = HAL_SPI_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */ |
|
- | 397 | hspi->TxHalfCpltCallback = HAL_SPI_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */ |
|
- | 398 | hspi->RxHalfCpltCallback = HAL_SPI_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */ |
|
- | 399 | hspi->TxRxHalfCpltCallback = HAL_SPI_TxRxHalfCpltCallback; /* Legacy weak TxRxHalfCpltCallback */ |
|
- | 400 | hspi->ErrorCallback = HAL_SPI_ErrorCallback; /* Legacy weak ErrorCallback */ |
|
- | 401 | hspi->AbortCpltCallback = HAL_SPI_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ |
|
- | 402 | ||
- | 403 | if (hspi->MspInitCallback == NULL) |
|
- | 404 | { |
|
- | 405 | hspi->MspInitCallback = HAL_SPI_MspInit; /* Legacy weak MspInit */ |
|
- | 406 | } |
|
- | 407 | ||
- | 408 | /* Init the low level hardware : GPIO, CLOCK, NVIC... */ |
|
- | 409 | hspi->MspInitCallback(hspi); |
|
- | 410 | #else |
|
295 | /* Init the low level hardware : GPIO, CLOCK, NVIC... */ |
411 | /* Init the low level hardware : GPIO, CLOCK, NVIC... */ |
296 | HAL_SPI_MspInit(hspi); |
412 | HAL_SPI_MspInit(hspi); |
- | 413 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
297 | } |
414 | } |
298 | 415 | ||
299 | hspi->State = HAL_SPI_STATE_BUSY; |
416 | hspi->State = HAL_SPI_STATE_BUSY; |
300 | 417 | ||
301 | /* Disable the selected SPI peripheral */ |
418 | /* Disable the selected SPI peripheral */ |
302 | __HAL_SPI_DISABLE(hspi); |
419 | __HAL_SPI_DISABLE(hspi); |
303 | 420 | ||
304 | /*----------------------- SPIx CR1 & CR2 Configuration ---------------------*/ |
421 | /*----------------------- SPIx CR1 & CR2 Configuration ---------------------*/ |
305 | /* Configure : SPI Mode, Communication Mode, Data size, Clock polarity and phase, NSS management, |
422 | /* Configure : SPI Mode, Communication Mode, Data size, Clock polarity and phase, NSS management, |
306 | Communication speed, First bit and CRC calculation state */ |
423 | Communication speed, First bit and CRC calculation state */ |
307 | WRITE_REG(hspi->Instance->CR1, (hspi->Init.Mode | hspi->Init.Direction | hspi->Init.DataSize | |
424 | WRITE_REG(hspi->Instance->CR1, ((hspi->Init.Mode & (SPI_CR1_MSTR | SPI_CR1_SSI)) | |
- | 425 | (hspi->Init.Direction & (SPI_CR1_RXONLY | SPI_CR1_BIDIMODE)) | |
|
- | 426 | (hspi->Init.DataSize & SPI_CR1_DFF) | |
|
308 | hspi->Init.CLKPolarity | hspi->Init.CLKPhase | (hspi->Init.NSS & SPI_CR1_SSM) | |
427 | (hspi->Init.CLKPolarity & SPI_CR1_CPOL) | |
- | 428 | (hspi->Init.CLKPhase & SPI_CR1_CPHA) | |
|
- | 429 | (hspi->Init.NSS & SPI_CR1_SSM) | |
|
309 | hspi->Init.BaudRatePrescaler | hspi->Init.FirstBit | hspi->Init.CRCCalculation) ); |
430 | (hspi->Init.BaudRatePrescaler & SPI_CR1_BR_Msk) | |
- | 431 | (hspi->Init.FirstBit & SPI_CR1_LSBFIRST) | |
|
- | 432 | (hspi->Init.CRCCalculation & SPI_CR1_CRCEN))); |
|
310 | 433 | ||
311 | /* Configure : NSS management */ |
434 | /* Configure : NSS management */ |
312 | WRITE_REG(hspi->Instance->CR2, (((hspi->Init.NSS >> 16U) & SPI_CR2_SSOE) | hspi->Init.TIMode)); |
435 | WRITE_REG(hspi->Instance->CR2, ((hspi->Init.NSS >> 16U) & SPI_CR2_SSOE)); |
313 | 436 | ||
314 | #if (USE_SPI_CRC != 0U) |
437 | #if (USE_SPI_CRC != 0U) |
315 | /*---------------------------- SPIx CRCPOLY Configuration ------------------*/ |
438 | /*---------------------------- SPIx CRCPOLY Configuration ------------------*/ |
316 | /* Configure : CRC Polynomial */ |
439 | /* Configure : CRC Polynomial */ |
317 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
440 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
318 | { |
441 | { |
319 | WRITE_REG(hspi->Instance->CRCPR, hspi->Init.CRCPolynomial); |
442 | WRITE_REG(hspi->Instance->CRCPR, (hspi->Init.CRCPolynomial & SPI_CRCPR_CRCPOLY_Msk)); |
320 | } |
443 | } |
321 | #endif /* USE_SPI_CRC */ |
444 | #endif /* USE_SPI_CRC */ |
322 | 445 | ||
323 | #if defined(SPI_I2SCFGR_I2SMOD) |
446 | #if defined(SPI_I2SCFGR_I2SMOD) |
324 | /* Activate the SPI mode (Make sure that I2SMOD bit in I2SCFGR register is reset) */ |
447 | /* Activate the SPI mode (Make sure that I2SMOD bit in I2SCFGR register is reset) */ |
Line 330... | Line 453... | ||
330 | 453 | ||
331 | return HAL_OK; |
454 | return HAL_OK; |
332 | } |
455 | } |
333 | 456 | ||
334 | /** |
457 | /** |
335 | * @brief De Initialize the SPI peripheral. |
458 | * @brief De-Initialize the SPI peripheral. |
336 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
459 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
337 | * the configuration information for SPI module. |
460 | * the configuration information for SPI module. |
338 | * @retval HAL status |
461 | * @retval HAL status |
339 | */ |
462 | */ |
340 | HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi) |
463 | HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi) |
341 | { |
464 | { |
342 | /* Check the SPI handle allocation */ |
465 | /* Check the SPI handle allocation */ |
343 | if(hspi == NULL) |
466 | if (hspi == NULL) |
344 | { |
467 | { |
345 | return HAL_ERROR; |
468 | return HAL_ERROR; |
346 | } |
469 | } |
347 | 470 | ||
348 | /* Check SPI Instance parameter */ |
471 | /* Check SPI Instance parameter */ |
Line 351... | Line 474... | ||
351 | hspi->State = HAL_SPI_STATE_BUSY; |
474 | hspi->State = HAL_SPI_STATE_BUSY; |
352 | 475 | ||
353 | /* Disable the SPI Peripheral Clock */ |
476 | /* Disable the SPI Peripheral Clock */ |
354 | __HAL_SPI_DISABLE(hspi); |
477 | __HAL_SPI_DISABLE(hspi); |
355 | 478 | ||
- | 479 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 480 | if (hspi->MspDeInitCallback == NULL) |
|
- | 481 | { |
|
- | 482 | hspi->MspDeInitCallback = HAL_SPI_MspDeInit; /* Legacy weak MspDeInit */ |
|
- | 483 | } |
|
- | 484 | ||
- | 485 | /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */ |
|
- | 486 | hspi->MspDeInitCallback(hspi); |
|
- | 487 | #else |
|
356 | /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */ |
488 | /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */ |
357 | HAL_SPI_MspDeInit(hspi); |
489 | HAL_SPI_MspDeInit(hspi); |
- | 490 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
358 | 491 | ||
359 | hspi->ErrorCode = HAL_SPI_ERROR_NONE; |
492 | hspi->ErrorCode = HAL_SPI_ERROR_NONE; |
360 | hspi->State = HAL_SPI_STATE_RESET; |
493 | hspi->State = HAL_SPI_STATE_RESET; |
361 | 494 | ||
362 | /* Release Lock */ |
495 | /* Release Lock */ |
Line 365... | Line 498... | ||
365 | return HAL_OK; |
498 | return HAL_OK; |
366 | } |
499 | } |
367 | 500 | ||
368 | /** |
501 | /** |
369 | * @brief Initialize the SPI MSP. |
502 | * @brief Initialize the SPI MSP. |
370 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
503 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
371 | * the configuration information for SPI module. |
504 | * the configuration information for SPI module. |
372 | * @retval None |
505 | * @retval None |
373 | */ |
506 | */ |
374 | __weak void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi) |
507 | __weak void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi) |
375 | { |
508 | { |
376 | /* Prevent unused argument(s) compilation warning */ |
509 | /* Prevent unused argument(s) compilation warning */ |
377 | UNUSED(hspi); |
510 | UNUSED(hspi); |
- | 511 | ||
378 | /* NOTE : This function should not be modified, when the callback is needed, |
512 | /* NOTE : This function should not be modified, when the callback is needed, |
379 | the HAL_SPI_MspInit should be implemented in the user file |
513 | the HAL_SPI_MspInit should be implemented in the user file |
380 | */ |
514 | */ |
381 | } |
515 | } |
382 | 516 | ||
383 | /** |
517 | /** |
384 | * @brief De-Initialize the SPI MSP. |
518 | * @brief De-Initialize the SPI MSP. |
385 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
519 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
386 | * the configuration information for SPI module. |
520 | * the configuration information for SPI module. |
387 | * @retval None |
521 | * @retval None |
388 | */ |
522 | */ |
389 | __weak void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi) |
523 | __weak void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi) |
390 | { |
524 | { |
391 | /* Prevent unused argument(s) compilation warning */ |
525 | /* Prevent unused argument(s) compilation warning */ |
392 | UNUSED(hspi); |
526 | UNUSED(hspi); |
- | 527 | ||
393 | /* NOTE : This function should not be modified, when the callback is needed, |
528 | /* NOTE : This function should not be modified, when the callback is needed, |
394 | the HAL_SPI_MspDeInit should be implemented in the user file |
529 | the HAL_SPI_MspDeInit should be implemented in the user file |
- | 530 | */ |
|
- | 531 | } |
|
- | 532 | ||
- | 533 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 534 | /** |
|
- | 535 | * @brief Register a User SPI Callback |
|
- | 536 | * To be used instead of the weak predefined callback |
|
- | 537 | * @param hspi Pointer to a SPI_HandleTypeDef structure that contains |
|
- | 538 | * the configuration information for the specified SPI. |
|
- | 539 | * @param CallbackID ID of the callback to be registered |
|
- | 540 | * @param pCallback pointer to the Callback function |
|
- | 541 | * @retval HAL status |
|
395 | */ |
542 | */ |
- | 543 | HAL_StatusTypeDef HAL_SPI_RegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID, |
|
- | 544 | pSPI_CallbackTypeDef pCallback) |
|
- | 545 | { |
|
- | 546 | HAL_StatusTypeDef status = HAL_OK; |
|
- | 547 | ||
- | 548 | if (pCallback == NULL) |
|
- | 549 | { |
|
- | 550 | /* Update the error code */ |
|
- | 551 | hspi->ErrorCode |= HAL_SPI_ERROR_INVALID_CALLBACK; |
|
- | 552 | ||
- | 553 | return HAL_ERROR; |
|
- | 554 | } |
|
- | 555 | /* Process locked */ |
|
- | 556 | __HAL_LOCK(hspi); |
|
- | 557 | ||
- | 558 | if (HAL_SPI_STATE_READY == hspi->State) |
|
- | 559 | { |
|
- | 560 | switch (CallbackID) |
|
- | 561 | { |
|
- | 562 | case HAL_SPI_TX_COMPLETE_CB_ID : |
|
- | 563 | hspi->TxCpltCallback = pCallback; |
|
- | 564 | break; |
|
- | 565 | ||
- | 566 | case HAL_SPI_RX_COMPLETE_CB_ID : |
|
- | 567 | hspi->RxCpltCallback = pCallback; |
|
- | 568 | break; |
|
- | 569 | ||
- | 570 | case HAL_SPI_TX_RX_COMPLETE_CB_ID : |
|
- | 571 | hspi->TxRxCpltCallback = pCallback; |
|
- | 572 | break; |
|
- | 573 | ||
- | 574 | case HAL_SPI_TX_HALF_COMPLETE_CB_ID : |
|
- | 575 | hspi->TxHalfCpltCallback = pCallback; |
|
- | 576 | break; |
|
- | 577 | ||
- | 578 | case HAL_SPI_RX_HALF_COMPLETE_CB_ID : |
|
- | 579 | hspi->RxHalfCpltCallback = pCallback; |
|
- | 580 | break; |
|
- | 581 | ||
- | 582 | case HAL_SPI_TX_RX_HALF_COMPLETE_CB_ID : |
|
- | 583 | hspi->TxRxHalfCpltCallback = pCallback; |
|
- | 584 | break; |
|
- | 585 | ||
- | 586 | case HAL_SPI_ERROR_CB_ID : |
|
- | 587 | hspi->ErrorCallback = pCallback; |
|
- | 588 | break; |
|
- | 589 | ||
- | 590 | case HAL_SPI_ABORT_CB_ID : |
|
- | 591 | hspi->AbortCpltCallback = pCallback; |
|
- | 592 | break; |
|
- | 593 | ||
- | 594 | case HAL_SPI_MSPINIT_CB_ID : |
|
- | 595 | hspi->MspInitCallback = pCallback; |
|
- | 596 | break; |
|
- | 597 | ||
- | 598 | case HAL_SPI_MSPDEINIT_CB_ID : |
|
- | 599 | hspi->MspDeInitCallback = pCallback; |
|
- | 600 | break; |
|
- | 601 | ||
- | 602 | default : |
|
- | 603 | /* Update the error code */ |
|
- | 604 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK); |
|
- | 605 | ||
- | 606 | /* Return error status */ |
|
- | 607 | status = HAL_ERROR; |
|
- | 608 | break; |
|
- | 609 | } |
|
- | 610 | } |
|
- | 611 | else if (HAL_SPI_STATE_RESET == hspi->State) |
|
- | 612 | { |
|
- | 613 | switch (CallbackID) |
|
- | 614 | { |
|
- | 615 | case HAL_SPI_MSPINIT_CB_ID : |
|
- | 616 | hspi->MspInitCallback = pCallback; |
|
- | 617 | break; |
|
- | 618 | ||
- | 619 | case HAL_SPI_MSPDEINIT_CB_ID : |
|
- | 620 | hspi->MspDeInitCallback = pCallback; |
|
- | 621 | break; |
|
- | 622 | ||
- | 623 | default : |
|
- | 624 | /* Update the error code */ |
|
- | 625 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK); |
|
- | 626 | ||
- | 627 | /* Return error status */ |
|
- | 628 | status = HAL_ERROR; |
|
- | 629 | break; |
|
- | 630 | } |
|
- | 631 | } |
|
- | 632 | else |
|
- | 633 | { |
|
- | 634 | /* Update the error code */ |
|
- | 635 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK); |
|
- | 636 | ||
- | 637 | /* Return error status */ |
|
- | 638 | status = HAL_ERROR; |
|
- | 639 | } |
|
- | 640 | ||
- | 641 | /* Release Lock */ |
|
- | 642 | __HAL_UNLOCK(hspi); |
|
- | 643 | return status; |
|
396 | } |
644 | } |
397 | 645 | ||
398 | /** |
646 | /** |
- | 647 | * @brief Unregister an SPI Callback |
|
- | 648 | * SPI callback is redirected to the weak predefined callback |
|
- | 649 | * @param hspi Pointer to a SPI_HandleTypeDef structure that contains |
|
- | 650 | * the configuration information for the specified SPI. |
|
- | 651 | * @param CallbackID ID of the callback to be unregistered |
|
- | 652 | * @retval HAL status |
|
- | 653 | */ |
|
- | 654 | HAL_StatusTypeDef HAL_SPI_UnRegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID) |
|
- | 655 | { |
|
- | 656 | HAL_StatusTypeDef status = HAL_OK; |
|
- | 657 | ||
- | 658 | /* Process locked */ |
|
- | 659 | __HAL_LOCK(hspi); |
|
- | 660 | ||
- | 661 | if (HAL_SPI_STATE_READY == hspi->State) |
|
- | 662 | { |
|
- | 663 | switch (CallbackID) |
|
- | 664 | { |
|
- | 665 | case HAL_SPI_TX_COMPLETE_CB_ID : |
|
- | 666 | hspi->TxCpltCallback = HAL_SPI_TxCpltCallback; /* Legacy weak TxCpltCallback */ |
|
- | 667 | break; |
|
- | 668 | ||
- | 669 | case HAL_SPI_RX_COMPLETE_CB_ID : |
|
- | 670 | hspi->RxCpltCallback = HAL_SPI_RxCpltCallback; /* Legacy weak RxCpltCallback */ |
|
- | 671 | break; |
|
- | 672 | ||
- | 673 | case HAL_SPI_TX_RX_COMPLETE_CB_ID : |
|
- | 674 | hspi->TxRxCpltCallback = HAL_SPI_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */ |
|
- | 675 | break; |
|
- | 676 | ||
- | 677 | case HAL_SPI_TX_HALF_COMPLETE_CB_ID : |
|
- | 678 | hspi->TxHalfCpltCallback = HAL_SPI_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */ |
|
- | 679 | break; |
|
- | 680 | ||
- | 681 | case HAL_SPI_RX_HALF_COMPLETE_CB_ID : |
|
- | 682 | hspi->RxHalfCpltCallback = HAL_SPI_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */ |
|
- | 683 | break; |
|
- | 684 | ||
- | 685 | case HAL_SPI_TX_RX_HALF_COMPLETE_CB_ID : |
|
- | 686 | hspi->TxRxHalfCpltCallback = HAL_SPI_TxRxHalfCpltCallback; /* Legacy weak TxRxHalfCpltCallback */ |
|
- | 687 | break; |
|
- | 688 | ||
- | 689 | case HAL_SPI_ERROR_CB_ID : |
|
- | 690 | hspi->ErrorCallback = HAL_SPI_ErrorCallback; /* Legacy weak ErrorCallback */ |
|
- | 691 | break; |
|
- | 692 | ||
- | 693 | case HAL_SPI_ABORT_CB_ID : |
|
- | 694 | hspi->AbortCpltCallback = HAL_SPI_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ |
|
- | 695 | break; |
|
- | 696 | ||
- | 697 | case HAL_SPI_MSPINIT_CB_ID : |
|
- | 698 | hspi->MspInitCallback = HAL_SPI_MspInit; /* Legacy weak MspInit */ |
|
- | 699 | break; |
|
- | 700 | ||
- | 701 | case HAL_SPI_MSPDEINIT_CB_ID : |
|
- | 702 | hspi->MspDeInitCallback = HAL_SPI_MspDeInit; /* Legacy weak MspDeInit */ |
|
- | 703 | break; |
|
- | 704 | ||
- | 705 | default : |
|
- | 706 | /* Update the error code */ |
|
- | 707 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK); |
|
- | 708 | ||
- | 709 | /* Return error status */ |
|
- | 710 | status = HAL_ERROR; |
|
- | 711 | break; |
|
- | 712 | } |
|
- | 713 | } |
|
- | 714 | else if (HAL_SPI_STATE_RESET == hspi->State) |
|
- | 715 | { |
|
- | 716 | switch (CallbackID) |
|
- | 717 | { |
|
- | 718 | case HAL_SPI_MSPINIT_CB_ID : |
|
- | 719 | hspi->MspInitCallback = HAL_SPI_MspInit; /* Legacy weak MspInit */ |
|
- | 720 | break; |
|
- | 721 | ||
- | 722 | case HAL_SPI_MSPDEINIT_CB_ID : |
|
- | 723 | hspi->MspDeInitCallback = HAL_SPI_MspDeInit; /* Legacy weak MspDeInit */ |
|
- | 724 | break; |
|
- | 725 | ||
- | 726 | default : |
|
- | 727 | /* Update the error code */ |
|
- | 728 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK); |
|
- | 729 | ||
- | 730 | /* Return error status */ |
|
- | 731 | status = HAL_ERROR; |
|
- | 732 | break; |
|
- | 733 | } |
|
- | 734 | } |
|
- | 735 | else |
|
- | 736 | { |
|
- | 737 | /* Update the error code */ |
|
- | 738 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK); |
|
- | 739 | ||
- | 740 | /* Return error status */ |
|
- | 741 | status = HAL_ERROR; |
|
- | 742 | } |
|
- | 743 | ||
- | 744 | /* Release Lock */ |
|
- | 745 | __HAL_UNLOCK(hspi); |
|
- | 746 | return status; |
|
- | 747 | } |
|
- | 748 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
- | 749 | /** |
|
399 | * @} |
750 | * @} |
400 | */ |
751 | */ |
401 | 752 | ||
402 | /** @defgroup SPI_Exported_Functions_Group2 IO operation functions |
753 | /** @defgroup SPI_Exported_Functions_Group2 IO operation functions |
403 | * @brief Data transfers functions |
754 | * @brief Data transfers functions |
404 | * |
755 | * |
405 | @verbatim |
756 | @verbatim |
406 | ============================================================================== |
757 | ============================================================================== |
407 | ##### IO operation functions ##### |
758 | ##### IO operation functions ##### |
408 | =============================================================================== |
759 | =============================================================================== |
409 | [..] |
760 | [..] |
Line 432... | Line 783... | ||
432 | * @{ |
783 | * @{ |
433 | */ |
784 | */ |
434 | 785 | ||
435 | /** |
786 | /** |
436 | * @brief Transmit an amount of data in blocking mode. |
787 | * @brief Transmit an amount of data in blocking mode. |
437 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
788 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
438 | * the configuration information for SPI module. |
789 | * the configuration information for SPI module. |
439 | * @param pData: pointer to data buffer |
790 | * @param pData pointer to data buffer |
440 | * @param Size: amount of data to be sent |
791 | * @param Size amount of data to be sent |
441 | * @param Timeout: Timeout duration |
792 | * @param Timeout Timeout duration |
442 | * @retval HAL status |
793 | * @retval HAL status |
443 | */ |
794 | */ |
444 | HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
795 | HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
445 | { |
796 | { |
446 | uint32_t tickstart = 0U; |
797 | uint32_t tickstart; |
447 | HAL_StatusTypeDef errorcode = HAL_OK; |
798 | HAL_StatusTypeDef errorcode = HAL_OK; |
- | 799 | uint16_t initial_TxXferCount; |
|
448 | 800 | ||
449 | /* Check Direction parameter */ |
801 | /* Check Direction parameter */ |
450 | assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction)); |
802 | assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction)); |
451 | 803 | ||
452 | /* Process Locked */ |
804 | /* Process Locked */ |
453 | __HAL_LOCK(hspi); |
805 | __HAL_LOCK(hspi); |
454 | 806 | ||
455 | /* Init tickstart for timeout management*/ |
807 | /* Init tickstart for timeout management*/ |
456 | tickstart = HAL_GetTick(); |
808 | tickstart = HAL_GetTick(); |
- | 809 | initial_TxXferCount = Size; |
|
457 | 810 | ||
458 | if(hspi->State != HAL_SPI_STATE_READY) |
811 | if (hspi->State != HAL_SPI_STATE_READY) |
459 | { |
812 | { |
460 | errorcode = HAL_BUSY; |
813 | errorcode = HAL_BUSY; |
461 | goto error; |
814 | goto error; |
462 | } |
815 | } |
463 | 816 | ||
464 | if((pData == NULL ) || (Size == 0U)) |
817 | if ((pData == NULL) || (Size == 0U)) |
465 | { |
818 | { |
466 | errorcode = HAL_ERROR; |
819 | errorcode = HAL_ERROR; |
467 | goto error; |
820 | goto error; |
468 | } |
821 | } |
469 | 822 | ||
Line 480... | Line 833... | ||
480 | hspi->RxXferCount = 0U; |
833 | hspi->RxXferCount = 0U; |
481 | hspi->TxISR = NULL; |
834 | hspi->TxISR = NULL; |
482 | hspi->RxISR = NULL; |
835 | hspi->RxISR = NULL; |
483 | 836 | ||
484 | /* Configure communication direction : 1Line */ |
837 | /* Configure communication direction : 1Line */ |
485 | if(hspi->Init.Direction == SPI_DIRECTION_1LINE) |
838 | if (hspi->Init.Direction == SPI_DIRECTION_1LINE) |
486 | { |
839 | { |
- | 840 | /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */ |
|
- | 841 | __HAL_SPI_DISABLE(hspi); |
|
487 | SPI_1LINE_TX(hspi); |
842 | SPI_1LINE_TX(hspi); |
488 | } |
843 | } |
489 | 844 | ||
490 | #if (USE_SPI_CRC != 0U) |
845 | #if (USE_SPI_CRC != 0U) |
491 | /* Reset CRC Calculation */ |
846 | /* Reset CRC Calculation */ |
492 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
847 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
493 | { |
848 | { |
494 | SPI_RESET_CRC(hspi); |
849 | SPI_RESET_CRC(hspi); |
495 | } |
850 | } |
496 | #endif /* USE_SPI_CRC */ |
851 | #endif /* USE_SPI_CRC */ |
497 | 852 | ||
498 | /* Check if the SPI is already enabled */ |
853 | /* Check if the SPI is already enabled */ |
499 | if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) |
854 | if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) |
500 | { |
855 | { |
501 | /* Enable SPI peripheral */ |
856 | /* Enable SPI peripheral */ |
502 | __HAL_SPI_ENABLE(hspi); |
857 | __HAL_SPI_ENABLE(hspi); |
503 | } |
858 | } |
504 | 859 | ||
505 | /* Transmit data in 16 Bit mode */ |
860 | /* Transmit data in 16 Bit mode */ |
506 | if(hspi->Init.DataSize == SPI_DATASIZE_16BIT) |
861 | if (hspi->Init.DataSize == SPI_DATASIZE_16BIT) |
507 | { |
862 | { |
508 | if((hspi->Init.Mode == SPI_MODE_SLAVE) || (hspi->TxXferCount == 0x01)) |
863 | if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U)) |
509 | { |
864 | { |
510 | hspi->Instance->DR = *((uint16_t *)pData); |
865 | hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr); |
511 | pData += sizeof(uint16_t); |
866 | hspi->pTxBuffPtr += sizeof(uint16_t); |
512 | hspi->TxXferCount--; |
867 | hspi->TxXferCount--; |
513 | } |
868 | } |
514 | /* Transmit data in 16 Bit mode */ |
869 | /* Transmit data in 16 Bit mode */ |
515 | while (hspi->TxXferCount > 0U) |
870 | while (hspi->TxXferCount > 0U) |
516 | { |
871 | { |
517 | /* Wait until TXE flag is set to send data */ |
872 | /* Wait until TXE flag is set to send data */ |
518 | if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) |
873 | if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) |
519 | { |
874 | { |
520 | hspi->Instance->DR = *((uint16_t *)pData); |
875 | hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr); |
521 | pData += sizeof(uint16_t); |
876 | hspi->pTxBuffPtr += sizeof(uint16_t); |
522 | hspi->TxXferCount--; |
877 | hspi->TxXferCount--; |
523 | } |
878 | } |
524 | else |
879 | else |
525 | { |
880 | { |
526 | /* Timeout management */ |
881 | /* Timeout management */ |
527 | if((Timeout == 0U) || ((Timeout != HAL_MAX_DELAY) && ((HAL_GetTick()-tickstart) >= Timeout))) |
882 | if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U)) |
528 | { |
883 | { |
529 | errorcode = HAL_TIMEOUT; |
884 | errorcode = HAL_TIMEOUT; |
530 | goto error; |
885 | goto error; |
531 | } |
886 | } |
532 | } |
887 | } |
533 | } |
888 | } |
534 | } |
889 | } |
535 | /* Transmit data in 8 Bit mode */ |
890 | /* Transmit data in 8 Bit mode */ |
536 | else |
891 | else |
537 | { |
892 | { |
538 | if((hspi->Init.Mode == SPI_MODE_SLAVE)|| (hspi->TxXferCount == 0x01)) |
893 | if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U)) |
539 | { |
894 | { |
540 | *((__IO uint8_t*)&hspi->Instance->DR) = (*pData); |
895 | *((__IO uint8_t *)&hspi->Instance->DR) = (*hspi->pTxBuffPtr); |
541 | pData += sizeof(uint8_t); |
896 | hspi->pTxBuffPtr += sizeof(uint8_t); |
542 | hspi->TxXferCount--; |
897 | hspi->TxXferCount--; |
543 | } |
898 | } |
544 | while (hspi->TxXferCount > 0U) |
899 | while (hspi->TxXferCount > 0U) |
545 | { |
900 | { |
546 | /* Wait until TXE flag is set to send data */ |
901 | /* Wait until TXE flag is set to send data */ |
547 | if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) |
902 | if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) |
548 | { |
903 | { |
549 | *((__IO uint8_t*)&hspi->Instance->DR) = (*pData); |
904 | *((__IO uint8_t *)&hspi->Instance->DR) = (*hspi->pTxBuffPtr); |
550 | pData += sizeof(uint8_t); |
905 | hspi->pTxBuffPtr += sizeof(uint8_t); |
551 | hspi->TxXferCount--; |
906 | hspi->TxXferCount--; |
552 | } |
907 | } |
553 | else |
908 | else |
554 | { |
909 | { |
555 | /* Timeout management */ |
910 | /* Timeout management */ |
556 | if((Timeout == 0U) || ((Timeout != HAL_MAX_DELAY) && ((HAL_GetTick()-tickstart) >= Timeout))) |
911 | if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U)) |
557 | { |
912 | { |
558 | errorcode = HAL_TIMEOUT; |
913 | errorcode = HAL_TIMEOUT; |
559 | goto error; |
914 | goto error; |
560 | } |
915 | } |
561 | } |
916 | } |
562 | } |
917 | } |
563 | } |
918 | } |
564 | - | ||
- | 919 | #if (USE_SPI_CRC != 0U) |
|
565 | /* Wait until TXE flag */ |
920 | /* Enable CRC Transmission */ |
566 | if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_TXE, SET, Timeout, tickstart) != HAL_OK) |
921 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
567 | { |
922 | { |
568 | errorcode = HAL_TIMEOUT; |
923 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
569 | goto error; |
- | |
570 | } |
924 | } |
- | 925 | #endif /* USE_SPI_CRC */ |
|
571 | 926 | ||
572 | /* Check Busy flag */ |
927 | /* Check the end of the transaction */ |
573 | if(SPI_CheckFlag_BSY(hspi, Timeout, tickstart) != HAL_OK) |
928 | if (SPI_EndRxTxTransaction(hspi, Timeout, tickstart) != HAL_OK) |
574 | { |
929 | { |
575 | errorcode = HAL_ERROR; |
- | |
576 | hspi->ErrorCode = HAL_SPI_ERROR_FLAG; |
930 | hspi->ErrorCode = HAL_SPI_ERROR_FLAG; |
577 | goto error; |
- | |
578 | } |
931 | } |
579 | 932 | ||
580 | /* Clear overrun flag in 2 Lines communication mode because received is not read */ |
933 | /* Clear overrun flag in 2 Lines communication mode because received is not read */ |
581 | if(hspi->Init.Direction == SPI_DIRECTION_2LINES) |
934 | if (hspi->Init.Direction == SPI_DIRECTION_2LINES) |
582 | { |
935 | { |
583 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
936 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
584 | } |
937 | } |
585 | #if (USE_SPI_CRC != 0U) |
- | |
586 | /* Enable CRC Transmission */ |
- | |
587 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
- | |
588 | { |
- | |
589 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
- | |
590 | } |
- | |
591 | #endif /* USE_SPI_CRC */ |
- | |
592 | 938 | ||
593 | if(hspi->ErrorCode != HAL_SPI_ERROR_NONE) |
939 | if (hspi->ErrorCode != HAL_SPI_ERROR_NONE) |
594 | { |
940 | { |
595 | errorcode = HAL_ERROR; |
941 | errorcode = HAL_ERROR; |
596 | } |
942 | } |
597 | 943 | ||
598 | error: |
944 | error: |
Line 602... | Line 948... | ||
602 | return errorcode; |
948 | return errorcode; |
603 | } |
949 | } |
604 | 950 | ||
605 | /** |
951 | /** |
606 | * @brief Receive an amount of data in blocking mode. |
952 | * @brief Receive an amount of data in blocking mode. |
607 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
953 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
608 | * the configuration information for SPI module. |
954 | * the configuration information for SPI module. |
609 | * @param pData: pointer to data buffer |
955 | * @param pData pointer to data buffer |
610 | * @param Size: amount of data to be received |
956 | * @param Size amount of data to be received |
611 | * @param Timeout: Timeout duration |
957 | * @param Timeout Timeout duration |
612 | * @retval HAL status |
958 | * @retval HAL status |
613 | */ |
959 | */ |
614 | HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
960 | HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
615 | { |
961 | { |
616 | #if (USE_SPI_CRC != 0U) |
- | |
617 | __IO uint16_t tmpreg = 0U; |
- | |
618 | #endif /* USE_SPI_CRC */ |
- | |
619 | uint32_t tickstart = 0U; |
962 | uint32_t tickstart; |
620 | HAL_StatusTypeDef errorcode = HAL_OK; |
963 | HAL_StatusTypeDef errorcode = HAL_OK; |
621 | 964 | ||
622 | if((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES)) |
965 | if ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES)) |
623 | { |
966 | { |
624 | hspi->State = HAL_SPI_STATE_BUSY_RX; |
967 | hspi->State = HAL_SPI_STATE_BUSY_RX; |
625 | /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */ |
968 | /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */ |
626 | return HAL_SPI_TransmitReceive(hspi,pData,pData,Size,Timeout); |
969 | return HAL_SPI_TransmitReceive(hspi, pData, pData, Size, Timeout); |
627 | } |
970 | } |
628 | 971 | ||
629 | /* Process Locked */ |
972 | /* Process Locked */ |
630 | __HAL_LOCK(hspi); |
973 | __HAL_LOCK(hspi); |
631 | 974 | ||
632 | /* Init tickstart for timeout management*/ |
975 | /* Init tickstart for timeout management*/ |
633 | tickstart = HAL_GetTick(); |
976 | tickstart = HAL_GetTick(); |
634 | 977 | ||
635 | if(hspi->State != HAL_SPI_STATE_READY) |
978 | if (hspi->State != HAL_SPI_STATE_READY) |
636 | { |
979 | { |
637 | errorcode = HAL_BUSY; |
980 | errorcode = HAL_BUSY; |
638 | goto error; |
981 | goto error; |
639 | } |
982 | } |
640 | 983 | ||
641 | if((pData == NULL ) || (Size == 0U)) |
984 | if ((pData == NULL) || (Size == 0U)) |
642 | { |
985 | { |
643 | errorcode = HAL_ERROR; |
986 | errorcode = HAL_ERROR; |
644 | goto error; |
987 | goto error; |
645 | } |
988 | } |
646 | 989 | ||
Line 658... | Line 1001... | ||
658 | hspi->RxISR = NULL; |
1001 | hspi->RxISR = NULL; |
659 | hspi->TxISR = NULL; |
1002 | hspi->TxISR = NULL; |
660 | 1003 | ||
661 | #if (USE_SPI_CRC != 0U) |
1004 | #if (USE_SPI_CRC != 0U) |
662 | /* Reset CRC Calculation */ |
1005 | /* Reset CRC Calculation */ |
663 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
1006 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
664 | { |
1007 | { |
665 | SPI_RESET_CRC(hspi); |
1008 | SPI_RESET_CRC(hspi); |
666 | /* this is done to handle the CRCNEXT before the latest data */ |
1009 | /* this is done to handle the CRCNEXT before the latest data */ |
667 | hspi->RxXferCount--; |
1010 | hspi->RxXferCount--; |
668 | } |
1011 | } |
669 | #endif /* USE_SPI_CRC */ |
1012 | #endif /* USE_SPI_CRC */ |
670 | 1013 | ||
671 | /* Configure communication direction: 1Line */ |
1014 | /* Configure communication direction: 1Line */ |
672 | if(hspi->Init.Direction == SPI_DIRECTION_1LINE) |
1015 | if (hspi->Init.Direction == SPI_DIRECTION_1LINE) |
673 | { |
1016 | { |
- | 1017 | /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */ |
|
- | 1018 | __HAL_SPI_DISABLE(hspi); |
|
674 | SPI_1LINE_RX(hspi); |
1019 | SPI_1LINE_RX(hspi); |
675 | } |
1020 | } |
676 | 1021 | ||
677 | /* Check if the SPI is already enabled */ |
1022 | /* Check if the SPI is already enabled */ |
678 | if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) |
1023 | if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) |
679 | { |
1024 | { |
680 | /* Enable SPI peripheral */ |
1025 | /* Enable SPI peripheral */ |
681 | __HAL_SPI_ENABLE(hspi); |
1026 | __HAL_SPI_ENABLE(hspi); |
682 | } |
1027 | } |
683 | 1028 | ||
684 | /* Receive data in 8 Bit mode */ |
1029 | /* Receive data in 8 Bit mode */ |
685 | if(hspi->Init.DataSize == SPI_DATASIZE_8BIT) |
1030 | if (hspi->Init.DataSize == SPI_DATASIZE_8BIT) |
686 | { |
1031 | { |
687 | /* Transfer loop */ |
1032 | /* Transfer loop */ |
688 | while(hspi->RxXferCount > 0U) |
1033 | while (hspi->RxXferCount > 0U) |
689 | { |
1034 | { |
690 | /* Check the RXNE flag */ |
1035 | /* Check the RXNE flag */ |
691 | if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) |
1036 | if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) |
692 | { |
1037 | { |
693 | /* read the received data */ |
1038 | /* read the received data */ |
694 | (* (uint8_t *)pData)= *(__IO uint8_t *)&hspi->Instance->DR; |
1039 | (* (uint8_t *)hspi->pRxBuffPtr) = *(__IO uint8_t *)&hspi->Instance->DR; |
695 | pData += sizeof(uint8_t); |
1040 | hspi->pRxBuffPtr += sizeof(uint8_t); |
696 | hspi->RxXferCount--; |
1041 | hspi->RxXferCount--; |
697 | } |
1042 | } |
698 | else |
1043 | else |
699 | { |
1044 | { |
700 | /* Timeout management */ |
1045 | /* Timeout management */ |
701 | if((Timeout == 0U) || ((Timeout != HAL_MAX_DELAY) && ((HAL_GetTick()-tickstart) >= Timeout))) |
1046 | if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U)) |
702 | { |
1047 | { |
703 | errorcode = HAL_TIMEOUT; |
1048 | errorcode = HAL_TIMEOUT; |
704 | goto error; |
1049 | goto error; |
705 | } |
1050 | } |
706 | } |
1051 | } |
707 | } |
1052 | } |
708 | } |
1053 | } |
709 | else |
1054 | else |
710 | { |
1055 | { |
711 | /* Transfer loop */ |
1056 | /* Transfer loop */ |
712 | while(hspi->RxXferCount > 0U) |
1057 | while (hspi->RxXferCount > 0U) |
713 | { |
1058 | { |
714 | /* Check the RXNE flag */ |
1059 | /* Check the RXNE flag */ |
715 | if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) |
1060 | if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) |
716 | { |
1061 | { |
717 | *((uint16_t*)pData) = hspi->Instance->DR; |
1062 | *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR; |
718 | pData += sizeof(uint16_t); |
1063 | hspi->pRxBuffPtr += sizeof(uint16_t); |
719 | hspi->RxXferCount--; |
1064 | hspi->RxXferCount--; |
720 | } |
1065 | } |
721 | else |
1066 | else |
722 | { |
1067 | { |
723 | /* Timeout management */ |
1068 | /* Timeout management */ |
724 | if((Timeout == 0U) || ((Timeout != HAL_MAX_DELAY) && ((HAL_GetTick()-tickstart) >= Timeout))) |
1069 | if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U)) |
725 | { |
1070 | { |
726 | errorcode = HAL_TIMEOUT; |
1071 | errorcode = HAL_TIMEOUT; |
727 | goto error; |
1072 | goto error; |
728 | } |
1073 | } |
729 | } |
1074 | } |
730 | } |
1075 | } |
731 | } |
1076 | } |
732 | 1077 | ||
733 | #if (USE_SPI_CRC != 0U) |
1078 | #if (USE_SPI_CRC != 0U) |
734 | /* Handle the CRC Transmission */ |
1079 | /* Handle the CRC Transmission */ |
735 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
1080 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
736 | { |
1081 | { |
737 | /* freeze the CRC before the latest data */ |
1082 | /* freeze the CRC before the latest data */ |
738 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
1083 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
739 | 1084 | ||
- | 1085 | /* Check if CRCNEXT is well reseted by hardware */ |
|
- | 1086 | if (READ_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT)) |
|
- | 1087 | { |
|
- | 1088 | /* Workaround to force CRCNEXT bit to zero in case of CRCNEXT is not reset automatically by hardware */ |
|
- | 1089 | CLEAR_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
|
- | 1090 | } |
|
740 | /* Read the latest data */ |
1091 | /* Read the latest data */ |
741 | if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK) |
1092 | if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK) |
742 | { |
1093 | { |
743 | /* the latest data has not been received */ |
1094 | /* the latest data has not been received */ |
744 | errorcode = HAL_TIMEOUT; |
1095 | errorcode = HAL_TIMEOUT; |
745 | goto error; |
1096 | goto error; |
746 | } |
1097 | } |
747 | 1098 | ||
748 | /* Receive last data in 16 Bit mode */ |
1099 | /* Receive last data in 16 Bit mode */ |
749 | if(hspi->Init.DataSize == SPI_DATASIZE_16BIT) |
1100 | if (hspi->Init.DataSize == SPI_DATASIZE_16BIT) |
750 | { |
1101 | { |
751 | *((uint16_t*)pData) = hspi->Instance->DR; |
1102 | *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR; |
752 | } |
1103 | } |
753 | /* Receive last data in 8 Bit mode */ |
1104 | /* Receive last data in 8 Bit mode */ |
754 | else |
1105 | else |
755 | { |
1106 | { |
756 | (*(uint8_t *)pData) = *(__IO uint8_t *)&hspi->Instance->DR; |
1107 | (*(uint8_t *)hspi->pRxBuffPtr) = *(__IO uint8_t *)&hspi->Instance->DR; |
757 | } |
1108 | } |
758 | 1109 | ||
759 | /* Wait the CRC data */ |
1110 | /* Wait the CRC data */ |
760 | if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK) |
1111 | if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK) |
761 | { |
1112 | { |
762 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
1113 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
763 | errorcode = HAL_TIMEOUT; |
1114 | errorcode = HAL_TIMEOUT; |
764 | goto error; |
1115 | goto error; |
765 | } |
1116 | } |
766 | 1117 | ||
767 | /* Read CRC to Flush DR and RXNE flag */ |
1118 | /* Read CRC to Flush DR and RXNE flag */ |
768 | tmpreg = hspi->Instance->DR; |
1119 | READ_REG(hspi->Instance->DR); |
769 | /* To avoid GCC warning */ |
- | |
770 | UNUSED(tmpreg); |
- | |
771 | } |
1120 | } |
772 | #endif /* USE_SPI_CRC */ |
1121 | #endif /* USE_SPI_CRC */ |
773 | 1122 | ||
774 | /* Check the end of the transaction */ |
1123 | /* Check the end of the transaction */ |
775 | if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY))) |
1124 | if (SPI_EndRxTransaction(hspi, Timeout, tickstart) != HAL_OK) |
776 | { |
1125 | { |
777 | /* Disable SPI peripheral */ |
- | |
778 | __HAL_SPI_DISABLE(hspi); |
1126 | hspi->ErrorCode = HAL_SPI_ERROR_FLAG; |
779 | } |
1127 | } |
780 | 1128 | ||
781 | #if (USE_SPI_CRC != 0U) |
1129 | #if (USE_SPI_CRC != 0U) |
782 | /* Check if CRC error occurred */ |
1130 | /* Check if CRC error occurred */ |
783 | if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET) |
1131 | if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET) |
- | 1132 | { |
|
- | 1133 | /* Check if CRC error is valid or not (workaround to be applied or not) */ |
|
- | 1134 | if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR) |
|
784 | { |
1135 | { |
785 | /* Check if CRC error is valid or not (workaround to be applied or not) */ |
- | |
786 | if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR) |
- | |
787 | { |
- | |
788 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
1136 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
789 | 1137 | ||
790 | /* Reset CRC Calculation */ |
1138 | /* Reset CRC Calculation */ |
791 | SPI_RESET_CRC(hspi); |
1139 | SPI_RESET_CRC(hspi); |
792 | } |
- | |
793 | else |
- | |
794 | { |
- | |
795 | __HAL_SPI_CLEAR_CRCERRFLAG(hspi); |
- | |
796 | } |
- | |
797 | } |
1140 | } |
- | 1141 | else |
|
- | 1142 | { |
|
- | 1143 | __HAL_SPI_CLEAR_CRCERRFLAG(hspi); |
|
- | 1144 | } |
|
- | 1145 | } |
|
798 | #endif /* USE_SPI_CRC */ |
1146 | #endif /* USE_SPI_CRC */ |
799 | 1147 | ||
800 | if(hspi->ErrorCode != HAL_SPI_ERROR_NONE) |
1148 | if (hspi->ErrorCode != HAL_SPI_ERROR_NONE) |
801 | { |
1149 | { |
802 | errorcode = HAL_ERROR; |
1150 | errorcode = HAL_ERROR; |
803 | } |
1151 | } |
804 | 1152 | ||
805 | error : |
1153 | error : |
Line 808... | Line 1156... | ||
808 | return errorcode; |
1156 | return errorcode; |
809 | } |
1157 | } |
810 | 1158 | ||
811 | /** |
1159 | /** |
812 | * @brief Transmit and Receive an amount of data in blocking mode. |
1160 | * @brief Transmit and Receive an amount of data in blocking mode. |
813 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
1161 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
814 | * the configuration information for SPI module. |
1162 | * the configuration information for SPI module. |
815 | * @param pTxData: pointer to transmission data buffer |
1163 | * @param pTxData pointer to transmission data buffer |
816 | * @param pRxData: pointer to reception data buffer |
1164 | * @param pRxData pointer to reception data buffer |
817 | * @param Size: amount of data to be sent and received |
1165 | * @param Size amount of data to be sent and received |
818 | * @param Timeout: Timeout duration |
1166 | * @param Timeout Timeout duration |
819 | * @retval HAL status |
1167 | * @retval HAL status |
820 | */ |
1168 | */ |
821 | HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout) |
1169 | HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, |
- | 1170 | uint32_t Timeout) |
|
822 | { |
1171 | { |
823 | uint32_t tmp = 0U, tmp1 = 0U; |
1172 | uint16_t initial_TxXferCount; |
824 | #if (USE_SPI_CRC != 0U) |
1173 | uint32_t tmp_mode; |
825 | __IO uint16_t tmpreg1 = 0U; |
1174 | HAL_SPI_StateTypeDef tmp_state; |
826 | #endif /* USE_SPI_CRC */ |
- | |
827 | uint32_t tickstart = 0U; |
1175 | uint32_t tickstart; |
- | 1176 | ||
828 | /* Variable used to alternate Rx and Tx during transfer */ |
1177 | /* Variable used to alternate Rx and Tx during transfer */ |
829 | uint32_t txallowed = 1U; |
1178 | uint32_t txallowed = 1U; |
830 | HAL_StatusTypeDef errorcode = HAL_OK; |
1179 | HAL_StatusTypeDef errorcode = HAL_OK; |
831 | 1180 | ||
832 | /* Check Direction parameter */ |
1181 | /* Check Direction parameter */ |
833 | assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction)); |
1182 | assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction)); |
834 | 1183 | ||
835 | /* Process Locked */ |
1184 | /* Process Locked */ |
836 | __HAL_LOCK(hspi); |
1185 | __HAL_LOCK(hspi); |
837 | 1186 | ||
838 | /* Init tickstart for timeout management*/ |
1187 | /* Init tickstart for timeout management*/ |
839 | tickstart = HAL_GetTick(); |
1188 | tickstart = HAL_GetTick(); |
840 | 1189 | ||
- | 1190 | /* Init temporary variables */ |
|
841 | tmp = hspi->State; |
1191 | tmp_state = hspi->State; |
842 | tmp1 = hspi->Init.Mode; |
1192 | tmp_mode = hspi->Init.Mode; |
- | 1193 | initial_TxXferCount = Size; |
|
843 | 1194 | ||
844 | if(!((tmp == HAL_SPI_STATE_READY) || \ |
1195 | if (!((tmp_state == HAL_SPI_STATE_READY) || \ |
845 | ((tmp1 == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp == HAL_SPI_STATE_BUSY_RX)))) |
1196 | ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp_state == HAL_SPI_STATE_BUSY_RX)))) |
846 | { |
1197 | { |
847 | errorcode = HAL_BUSY; |
1198 | errorcode = HAL_BUSY; |
848 | goto error; |
1199 | goto error; |
849 | } |
1200 | } |
850 | 1201 | ||
851 | if((pTxData == NULL) || (pRxData == NULL) || (Size == 0U)) |
1202 | if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U)) |
852 | { |
1203 | { |
853 | errorcode = HAL_ERROR; |
1204 | errorcode = HAL_ERROR; |
854 | goto error; |
1205 | goto error; |
855 | } |
1206 | } |
856 | 1207 | ||
857 | /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */ |
1208 | /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */ |
858 | if(hspi->State == HAL_SPI_STATE_READY) |
1209 | if (hspi->State != HAL_SPI_STATE_BUSY_RX) |
859 | { |
1210 | { |
860 | hspi->State = HAL_SPI_STATE_BUSY_TX_RX; |
1211 | hspi->State = HAL_SPI_STATE_BUSY_TX_RX; |
861 | } |
1212 | } |
862 | 1213 | ||
863 | /* Set the transaction information */ |
1214 | /* Set the transaction information */ |
Line 873... | Line 1224... | ||
873 | hspi->RxISR = NULL; |
1224 | hspi->RxISR = NULL; |
874 | hspi->TxISR = NULL; |
1225 | hspi->TxISR = NULL; |
875 | 1226 | ||
876 | #if (USE_SPI_CRC != 0U) |
1227 | #if (USE_SPI_CRC != 0U) |
877 | /* Reset CRC Calculation */ |
1228 | /* Reset CRC Calculation */ |
878 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
1229 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
879 | { |
1230 | { |
880 | SPI_RESET_CRC(hspi); |
1231 | SPI_RESET_CRC(hspi); |
881 | } |
1232 | } |
882 | #endif /* USE_SPI_CRC */ |
1233 | #endif /* USE_SPI_CRC */ |
883 | 1234 | ||
884 | /* Check if the SPI is already enabled */ |
1235 | /* Check if the SPI is already enabled */ |
885 | if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE) |
1236 | if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) |
886 | { |
1237 | { |
887 | /* Enable SPI peripheral */ |
1238 | /* Enable SPI peripheral */ |
888 | __HAL_SPI_ENABLE(hspi); |
1239 | __HAL_SPI_ENABLE(hspi); |
889 | } |
1240 | } |
890 | 1241 | ||
891 | /* Transmit and Receive data in 16 Bit mode */ |
1242 | /* Transmit and Receive data in 16 Bit mode */ |
892 | if(hspi->Init.DataSize == SPI_DATASIZE_16BIT) |
1243 | if (hspi->Init.DataSize == SPI_DATASIZE_16BIT) |
893 | { |
1244 | { |
894 | if((hspi->Init.Mode == SPI_MODE_SLAVE) || (hspi->TxXferCount == 0x01U)) |
1245 | if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U)) |
895 | { |
1246 | { |
896 | hspi->Instance->DR = *((uint16_t *)pTxData); |
1247 | hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr); |
897 | pTxData += sizeof(uint16_t); |
1248 | hspi->pTxBuffPtr += sizeof(uint16_t); |
898 | hspi->TxXferCount--; |
1249 | hspi->TxXferCount--; |
899 | } |
1250 | } |
900 | while ((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U)) |
1251 | while ((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U)) |
901 | { |
1252 | { |
902 | /* Check TXE flag */ |
1253 | /* Check TXE flag */ |
903 | if(txallowed && (hspi->TxXferCount > 0U) && (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE))) |
1254 | if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) && (hspi->TxXferCount > 0U) && (txallowed == 1U)) |
904 | { |
1255 | { |
905 | hspi->Instance->DR = *((uint16_t *)pTxData); |
1256 | hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr); |
906 | pTxData += sizeof(uint16_t); |
1257 | hspi->pTxBuffPtr += sizeof(uint16_t); |
907 | hspi->TxXferCount--; |
1258 | hspi->TxXferCount--; |
908 | /* Next Data is a reception (Rx). Tx not allowed */ |
1259 | /* Next Data is a reception (Rx). Tx not allowed */ |
909 | txallowed = 0U; |
1260 | txallowed = 0U; |
910 | 1261 | ||
911 | #if (USE_SPI_CRC != 0U) |
1262 | #if (USE_SPI_CRC != 0U) |
912 | /* Enable CRC Transmission */ |
1263 | /* Enable CRC Transmission */ |
913 | if((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)) |
1264 | if ((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)) |
914 | { |
1265 | { |
915 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
1266 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
916 | } |
1267 | } |
917 | #endif /* USE_SPI_CRC */ |
1268 | #endif /* USE_SPI_CRC */ |
918 | } |
1269 | } |
919 | 1270 | ||
920 | /* Check RXNE flag */ |
1271 | /* Check RXNE flag */ |
921 | if((hspi->RxXferCount > 0U) && (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE))) |
1272 | if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) && (hspi->RxXferCount > 0U)) |
922 | { |
1273 | { |
923 | *((uint16_t *)pRxData) = hspi->Instance->DR; |
1274 | *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR; |
924 | pRxData += sizeof(uint16_t); |
1275 | hspi->pRxBuffPtr += sizeof(uint16_t); |
925 | hspi->RxXferCount--; |
1276 | hspi->RxXferCount--; |
926 | /* Next Data is a Transmission (Tx). Tx is allowed */ |
1277 | /* Next Data is a Transmission (Tx). Tx is allowed */ |
927 | txallowed = 1U; |
1278 | txallowed = 1U; |
928 | } |
1279 | } |
929 | if((Timeout != HAL_MAX_DELAY) && ((HAL_GetTick()-tickstart) >= Timeout)) |
1280 | if (((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) |
930 | { |
1281 | { |
931 | errorcode = HAL_TIMEOUT; |
1282 | errorcode = HAL_TIMEOUT; |
932 | goto error; |
1283 | goto error; |
933 | } |
1284 | } |
934 | } |
1285 | } |
935 | } |
1286 | } |
936 | /* Transmit and Receive data in 8 Bit mode */ |
1287 | /* Transmit and Receive data in 8 Bit mode */ |
937 | else |
1288 | else |
938 | { |
1289 | { |
939 | if((hspi->Init.Mode == SPI_MODE_SLAVE) || (hspi->TxXferCount == 0x01U)) |
1290 | if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U)) |
940 | { |
1291 | { |
941 | *((__IO uint8_t*)&hspi->Instance->DR) = (*pTxData); |
1292 | *((__IO uint8_t *)&hspi->Instance->DR) = (*hspi->pTxBuffPtr); |
942 | pTxData += sizeof(uint8_t); |
1293 | hspi->pTxBuffPtr += sizeof(uint8_t); |
943 | hspi->TxXferCount--; |
1294 | hspi->TxXferCount--; |
944 | } |
1295 | } |
945 | while((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U)) |
1296 | while ((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U)) |
946 | { |
1297 | { |
947 | /* check TXE flag */ |
1298 | /* Check TXE flag */ |
948 | if(txallowed && (hspi->TxXferCount > 0U) && (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE))) |
1299 | if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) && (hspi->TxXferCount > 0U) && (txallowed == 1U)) |
949 | { |
1300 | { |
950 | *(__IO uint8_t *)&hspi->Instance->DR = (*pTxData++); |
1301 | *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr); |
- | 1302 | hspi->pTxBuffPtr++; |
|
951 | hspi->TxXferCount--; |
1303 | hspi->TxXferCount--; |
952 | /* Next Data is a reception (Rx). Tx not allowed */ |
1304 | /* Next Data is a reception (Rx). Tx not allowed */ |
953 | txallowed = 0U; |
1305 | txallowed = 0U; |
954 | 1306 | ||
955 | #if (USE_SPI_CRC != 0U) |
1307 | #if (USE_SPI_CRC != 0U) |
956 | /* Enable CRC Transmission */ |
1308 | /* Enable CRC Transmission */ |
957 | if((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)) |
1309 | if ((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)) |
958 | { |
1310 | { |
959 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
1311 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
960 | } |
1312 | } |
961 | #endif /* USE_SPI_CRC */ |
1313 | #endif /* USE_SPI_CRC */ |
962 | } |
1314 | } |
963 | 1315 | ||
964 | /* Wait until RXNE flag is reset */ |
1316 | /* Wait until RXNE flag is reset */ |
965 | if((hspi->RxXferCount > 0U) && (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE))) |
1317 | if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) && (hspi->RxXferCount > 0U)) |
966 | { |
1318 | { |
967 | (*(uint8_t *)pRxData++) = hspi->Instance->DR; |
1319 | (*(uint8_t *)hspi->pRxBuffPtr) = hspi->Instance->DR; |
- | 1320 | hspi->pRxBuffPtr++; |
|
968 | hspi->RxXferCount--; |
1321 | hspi->RxXferCount--; |
969 | /* Next Data is a Transmission (Tx). Tx is allowed */ |
1322 | /* Next Data is a Transmission (Tx). Tx is allowed */ |
970 | txallowed = 1U; |
1323 | txallowed = 1U; |
971 | } |
1324 | } |
972 | if((Timeout != HAL_MAX_DELAY) && ((HAL_GetTick()-tickstart) >= Timeout)) |
1325 | if ((((HAL_GetTick() - tickstart) >= Timeout) && ((Timeout != HAL_MAX_DELAY))) || (Timeout == 0U)) |
973 | { |
1326 | { |
974 | errorcode = HAL_TIMEOUT; |
1327 | errorcode = HAL_TIMEOUT; |
975 | goto error; |
1328 | goto error; |
976 | } |
1329 | } |
977 | } |
1330 | } |
978 | } |
1331 | } |
979 | 1332 | ||
980 | #if (USE_SPI_CRC != 0U) |
1333 | #if (USE_SPI_CRC != 0U) |
981 | /* Read CRC from DR to close CRC calculation process */ |
1334 | /* Read CRC from DR to close CRC calculation process */ |
982 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
1335 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
983 | { |
1336 | { |
984 | /* Wait until TXE flag */ |
1337 | /* Wait until TXE flag */ |
985 | if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK) |
1338 | if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK) |
986 | { |
1339 | { |
987 | /* Error on the CRC reception */ |
1340 | /* Error on the CRC reception */ |
988 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
1341 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
989 | errorcode = HAL_TIMEOUT; |
1342 | errorcode = HAL_TIMEOUT; |
990 | goto error; |
1343 | goto error; |
991 | } |
1344 | } |
992 | /* Read CRC */ |
1345 | /* Read CRC */ |
993 | tmpreg1 = hspi->Instance->DR; |
1346 | READ_REG(hspi->Instance->DR); |
994 | /* To avoid GCC warning */ |
- | |
995 | UNUSED(tmpreg1); |
- | |
996 | } |
1347 | } |
997 | 1348 | ||
998 | /* Check if CRC error occurred */ |
1349 | /* Check if CRC error occurred */ |
999 | if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET) |
1350 | if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET) |
1000 | { |
1351 | { |
1001 | /* Check if CRC error is valid or not (workaround to be applied or not) */ |
1352 | /* Check if CRC error is valid or not (workaround to be applied or not) */ |
1002 | if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR) |
1353 | if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR) |
1003 | { |
1354 | { |
1004 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
1355 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
1005 | 1356 | ||
1006 | /* Reset CRC Calculation */ |
1357 | /* Reset CRC Calculation */ |
1007 | SPI_RESET_CRC(hspi); |
1358 | SPI_RESET_CRC(hspi); |
1008 | 1359 | ||
1009 | errorcode = HAL_ERROR; |
1360 | errorcode = HAL_ERROR; |
1010 | } |
1361 | } |
1011 | else |
1362 | else |
1012 | { |
1363 | { |
1013 | __HAL_SPI_CLEAR_CRCERRFLAG(hspi); |
1364 | __HAL_SPI_CLEAR_CRCERRFLAG(hspi); |
1014 | } |
1365 | } |
1015 | } |
1366 | } |
1016 | #endif /* USE_SPI_CRC */ |
1367 | #endif /* USE_SPI_CRC */ |
1017 | 1368 | ||
1018 | /* Wait until TXE flag */ |
- | |
1019 | if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_TXE, SET, Timeout, tickstart) != HAL_OK) |
- | |
1020 | { |
- | |
1021 | errorcode = HAL_TIMEOUT; |
- | |
1022 | goto error; |
- | |
1023 | } |
- | |
1024 | - | ||
1025 | /* Check Busy flag */ |
1369 | /* Check the end of the transaction */ |
1026 | if(SPI_CheckFlag_BSY(hspi, Timeout, tickstart) != HAL_OK) |
1370 | if (SPI_EndRxTxTransaction(hspi, Timeout, tickstart) != HAL_OK) |
1027 | { |
1371 | { |
1028 | errorcode = HAL_ERROR; |
1372 | errorcode = HAL_ERROR; |
1029 | hspi->ErrorCode = HAL_SPI_ERROR_FLAG; |
1373 | hspi->ErrorCode = HAL_SPI_ERROR_FLAG; |
1030 | goto error; |
1374 | goto error; |
1031 | } |
1375 | } |
1032 | 1376 | ||
1033 | /* Clear overrun flag in 2 Lines communication mode because received is not read */ |
1377 | /* Clear overrun flag in 2 Lines communication mode because received is not read */ |
1034 | if(hspi->Init.Direction == SPI_DIRECTION_2LINES) |
1378 | if (hspi->Init.Direction == SPI_DIRECTION_2LINES) |
1035 | { |
1379 | { |
1036 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
1380 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
1037 | } |
1381 | } |
1038 | 1382 | ||
1039 | error : |
1383 | error : |
1040 | hspi->State = HAL_SPI_STATE_READY; |
1384 | hspi->State = HAL_SPI_STATE_READY; |
1041 | __HAL_UNLOCK(hspi); |
1385 | __HAL_UNLOCK(hspi); |
1042 | return errorcode; |
1386 | return errorcode; |
1043 | } |
1387 | } |
1044 | 1388 | ||
1045 | /** |
1389 | /** |
1046 | * @brief Transmit an amount of data in non-blocking mode with Interrupt. |
1390 | * @brief Transmit an amount of data in non-blocking mode with Interrupt. |
1047 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
1391 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
1048 | * the configuration information for SPI module. |
1392 | * the configuration information for SPI module. |
1049 | * @param pData: pointer to data buffer |
1393 | * @param pData pointer to data buffer |
1050 | * @param Size: amount of data to be sent |
1394 | * @param Size amount of data to be sent |
1051 | * @retval HAL status |
1395 | * @retval HAL status |
1052 | */ |
1396 | */ |
1053 | HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size) |
1397 | HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size) |
1054 | { |
1398 | { |
1055 | HAL_StatusTypeDef errorcode = HAL_OK; |
1399 | HAL_StatusTypeDef errorcode = HAL_OK; |
Line 1058... | Line 1402... | ||
1058 | assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction)); |
1402 | assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction)); |
1059 | 1403 | ||
1060 | /* Process Locked */ |
1404 | /* Process Locked */ |
1061 | __HAL_LOCK(hspi); |
1405 | __HAL_LOCK(hspi); |
1062 | 1406 | ||
1063 | if((pData == NULL) || (Size == 0U)) |
1407 | if ((pData == NULL) || (Size == 0U)) |
1064 | { |
1408 | { |
1065 | errorcode = HAL_ERROR; |
1409 | errorcode = HAL_ERROR; |
1066 | goto error; |
1410 | goto error; |
1067 | } |
1411 | } |
1068 | 1412 | ||
1069 | if(hspi->State != HAL_SPI_STATE_READY) |
1413 | if (hspi->State != HAL_SPI_STATE_READY) |
1070 | { |
1414 | { |
1071 | errorcode = HAL_BUSY; |
1415 | errorcode = HAL_BUSY; |
1072 | goto error; |
1416 | goto error; |
1073 | } |
1417 | } |
1074 | 1418 | ||
Line 1084... | Line 1428... | ||
1084 | hspi->RxXferSize = 0U; |
1428 | hspi->RxXferSize = 0U; |
1085 | hspi->RxXferCount = 0U; |
1429 | hspi->RxXferCount = 0U; |
1086 | hspi->RxISR = NULL; |
1430 | hspi->RxISR = NULL; |
1087 | 1431 | ||
1088 | /* Set the function for IT treatment */ |
1432 | /* Set the function for IT treatment */ |
1089 | if(hspi->Init.DataSize > SPI_DATASIZE_8BIT ) |
1433 | if (hspi->Init.DataSize > SPI_DATASIZE_8BIT) |
1090 | { |
1434 | { |
1091 | hspi->TxISR = SPI_TxISR_16BIT; |
1435 | hspi->TxISR = SPI_TxISR_16BIT; |
1092 | } |
1436 | } |
1093 | else |
1437 | else |
1094 | { |
1438 | { |
1095 | hspi->TxISR = SPI_TxISR_8BIT; |
1439 | hspi->TxISR = SPI_TxISR_8BIT; |
1096 | } |
1440 | } |
1097 | 1441 | ||
1098 | /* Configure communication direction : 1Line */ |
1442 | /* Configure communication direction : 1Line */ |
1099 | if(hspi->Init.Direction == SPI_DIRECTION_1LINE) |
1443 | if (hspi->Init.Direction == SPI_DIRECTION_1LINE) |
1100 | { |
1444 | { |
- | 1445 | /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */ |
|
- | 1446 | __HAL_SPI_DISABLE(hspi); |
|
1101 | SPI_1LINE_TX(hspi); |
1447 | SPI_1LINE_TX(hspi); |
1102 | } |
1448 | } |
1103 | 1449 | ||
1104 | #if (USE_SPI_CRC != 0U) |
1450 | #if (USE_SPI_CRC != 0U) |
1105 | /* Reset CRC Calculation */ |
1451 | /* Reset CRC Calculation */ |
1106 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
1452 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
1107 | { |
1453 | { |
1108 | SPI_RESET_CRC(hspi); |
1454 | SPI_RESET_CRC(hspi); |
1109 | } |
1455 | } |
1110 | #endif /* USE_SPI_CRC */ |
1456 | #endif /* USE_SPI_CRC */ |
1111 | 1457 | ||
1112 | if (hspi->Init.Direction == SPI_DIRECTION_2LINES) |
- | |
1113 | { |
- | |
1114 | /* Enable TXE interrupt */ |
- | |
1115 | __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE)); |
- | |
1116 | } |
- | |
1117 | else |
- | |
1118 | { |
- | |
1119 | /* Enable TXE and ERR interrupt */ |
1458 | /* Enable TXE and ERR interrupt */ |
1120 | __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_ERR)); |
1459 | __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_ERR)); |
1121 | } |
1460 | |
1122 | 1461 | ||
1123 | /* Check if the SPI is already enabled */ |
1462 | /* Check if the SPI is already enabled */ |
1124 | if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE) |
1463 | if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) |
1125 | { |
1464 | { |
1126 | /* Enable SPI peripheral */ |
1465 | /* Enable SPI peripheral */ |
1127 | __HAL_SPI_ENABLE(hspi); |
1466 | __HAL_SPI_ENABLE(hspi); |
1128 | } |
1467 | } |
1129 | 1468 | ||
Line 1132... | Line 1471... | ||
1132 | return errorcode; |
1471 | return errorcode; |
1133 | } |
1472 | } |
1134 | 1473 | ||
1135 | /** |
1474 | /** |
1136 | * @brief Receive an amount of data in non-blocking mode with Interrupt. |
1475 | * @brief Receive an amount of data in non-blocking mode with Interrupt. |
1137 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
1476 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
1138 | * the configuration information for SPI module. |
1477 | * the configuration information for SPI module. |
1139 | * @param pData: pointer to data buffer |
1478 | * @param pData pointer to data buffer |
1140 | * @param Size: amount of data to be sent |
1479 | * @param Size amount of data to be sent |
1141 | * @retval HAL status |
1480 | * @retval HAL status |
1142 | */ |
1481 | */ |
1143 | HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size) |
1482 | HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size) |
1144 | { |
1483 | { |
1145 | HAL_StatusTypeDef errorcode = HAL_OK; |
1484 | HAL_StatusTypeDef errorcode = HAL_OK; |
1146 | 1485 | ||
1147 | if((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER)) |
1486 | if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER)) |
1148 | { |
1487 | { |
1149 | hspi->State = HAL_SPI_STATE_BUSY_RX; |
1488 | hspi->State = HAL_SPI_STATE_BUSY_RX; |
1150 | /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */ |
1489 | /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */ |
1151 | return HAL_SPI_TransmitReceive_IT(hspi, pData, pData, Size); |
1490 | return HAL_SPI_TransmitReceive_IT(hspi, pData, pData, Size); |
1152 | } |
1491 | } |
1153 | 1492 | ||
1154 | /* Process Locked */ |
1493 | /* Process Locked */ |
1155 | __HAL_LOCK(hspi); |
1494 | __HAL_LOCK(hspi); |
1156 | 1495 | ||
1157 | if(hspi->State != HAL_SPI_STATE_READY) |
1496 | if (hspi->State != HAL_SPI_STATE_READY) |
1158 | { |
1497 | { |
1159 | errorcode = HAL_BUSY; |
1498 | errorcode = HAL_BUSY; |
1160 | goto error; |
1499 | goto error; |
1161 | } |
1500 | } |
1162 | 1501 | ||
1163 | if((pData == NULL) || (Size == 0U)) |
1502 | if ((pData == NULL) || (Size == 0U)) |
1164 | { |
1503 | { |
1165 | errorcode = HAL_ERROR; |
1504 | errorcode = HAL_ERROR; |
1166 | goto error; |
1505 | goto error; |
1167 | } |
1506 | } |
1168 | 1507 | ||
Line 1178... | Line 1517... | ||
1178 | hspi->TxXferSize = 0U; |
1517 | hspi->TxXferSize = 0U; |
1179 | hspi->TxXferCount = 0U; |
1518 | hspi->TxXferCount = 0U; |
1180 | hspi->TxISR = NULL; |
1519 | hspi->TxISR = NULL; |
1181 | 1520 | ||
1182 | /* Set the function for IT treatment */ |
1521 | /* Set the function for IT treatment */ |
1183 | if(hspi->Init.DataSize > SPI_DATASIZE_8BIT ) |
1522 | if (hspi->Init.DataSize > SPI_DATASIZE_8BIT) |
1184 | { |
1523 | { |
1185 | hspi->RxISR = SPI_RxISR_16BIT; |
1524 | hspi->RxISR = SPI_RxISR_16BIT; |
1186 | } |
1525 | } |
1187 | else |
1526 | else |
1188 | { |
1527 | { |
1189 | hspi->RxISR = SPI_RxISR_8BIT; |
1528 | hspi->RxISR = SPI_RxISR_8BIT; |
1190 | } |
1529 | } |
1191 | 1530 | ||
1192 | /* Configure communication direction : 1Line */ |
1531 | /* Configure communication direction : 1Line */ |
1193 | if(hspi->Init.Direction == SPI_DIRECTION_1LINE) |
1532 | if (hspi->Init.Direction == SPI_DIRECTION_1LINE) |
1194 | { |
1533 | { |
- | 1534 | /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */ |
|
- | 1535 | __HAL_SPI_DISABLE(hspi); |
|
1195 | SPI_1LINE_RX(hspi); |
1536 | SPI_1LINE_RX(hspi); |
1196 | } |
1537 | } |
1197 | 1538 | ||
1198 | #if (USE_SPI_CRC != 0U) |
1539 | #if (USE_SPI_CRC != 0U) |
1199 | /* Reset CRC Calculation */ |
1540 | /* Reset CRC Calculation */ |
1200 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
1541 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
1201 | { |
1542 | { |
1202 | SPI_RESET_CRC(hspi); |
1543 | SPI_RESET_CRC(hspi); |
1203 | } |
1544 | } |
1204 | #endif /* USE_SPI_CRC */ |
1545 | #endif /* USE_SPI_CRC */ |
1205 | 1546 | ||
Line 1209... | Line 1550... | ||
1209 | /* Note : The SPI must be enabled after unlocking current process |
1550 | /* Note : The SPI must be enabled after unlocking current process |
1210 | to avoid the risk of SPI interrupt handle execution before current |
1551 | to avoid the risk of SPI interrupt handle execution before current |
1211 | process unlock */ |
1552 | process unlock */ |
1212 | 1553 | ||
1213 | /* Check if the SPI is already enabled */ |
1554 | /* Check if the SPI is already enabled */ |
1214 | if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE) |
1555 | if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) |
1215 | { |
1556 | { |
1216 | /* Enable SPI peripheral */ |
1557 | /* Enable SPI peripheral */ |
1217 | __HAL_SPI_ENABLE(hspi); |
1558 | __HAL_SPI_ENABLE(hspi); |
1218 | } |
1559 | } |
1219 | 1560 | ||
Line 1223... | Line 1564... | ||
1223 | return errorcode; |
1564 | return errorcode; |
1224 | } |
1565 | } |
1225 | 1566 | ||
1226 | /** |
1567 | /** |
1227 | * @brief Transmit and Receive an amount of data in non-blocking mode with Interrupt. |
1568 | * @brief Transmit and Receive an amount of data in non-blocking mode with Interrupt. |
1228 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
1569 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
1229 | * the configuration information for SPI module. |
1570 | * the configuration information for SPI module. |
1230 | * @param pTxData: pointer to transmission data buffer |
1571 | * @param pTxData pointer to transmission data buffer |
1231 | * @param pRxData: pointer to reception data buffer |
1572 | * @param pRxData pointer to reception data buffer |
1232 | * @param Size: amount of data to be sent and received |
1573 | * @param Size amount of data to be sent and received |
1233 | * @retval HAL status |
1574 | * @retval HAL status |
1234 | */ |
1575 | */ |
1235 | HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size) |
1576 | HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size) |
1236 | { |
1577 | { |
1237 | uint32_t tmp = 0U, tmp1 = 0U; |
1578 | uint32_t tmp_mode; |
- | 1579 | HAL_SPI_StateTypeDef tmp_state; |
|
1238 | HAL_StatusTypeDef errorcode = HAL_OK; |
1580 | HAL_StatusTypeDef errorcode = HAL_OK; |
1239 | 1581 | ||
1240 | /* Check Direction parameter */ |
1582 | /* Check Direction parameter */ |
1241 | assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction)); |
1583 | assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction)); |
1242 | 1584 | ||
1243 | /* Process locked */ |
1585 | /* Process locked */ |
1244 | __HAL_LOCK(hspi); |
1586 | __HAL_LOCK(hspi); |
1245 | 1587 | ||
- | 1588 | /* Init temporary variables */ |
|
1246 | tmp = hspi->State; |
1589 | tmp_state = hspi->State; |
1247 | tmp1 = hspi->Init.Mode; |
1590 | tmp_mode = hspi->Init.Mode; |
1248 | 1591 | ||
1249 | if(!((tmp == HAL_SPI_STATE_READY) || \ |
1592 | if (!((tmp_state == HAL_SPI_STATE_READY) || \ |
1250 | ((tmp1 == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp == HAL_SPI_STATE_BUSY_RX)))) |
1593 | ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp_state == HAL_SPI_STATE_BUSY_RX)))) |
1251 | { |
1594 | { |
1252 | errorcode = HAL_BUSY; |
1595 | errorcode = HAL_BUSY; |
1253 | goto error; |
1596 | goto error; |
1254 | } |
1597 | } |
1255 | 1598 | ||
1256 | if((pTxData == NULL ) || (pRxData == NULL ) || (Size == 0U)) |
1599 | if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U)) |
1257 | { |
1600 | { |
1258 | errorcode = HAL_ERROR; |
1601 | errorcode = HAL_ERROR; |
1259 | goto error; |
1602 | goto error; |
1260 | } |
1603 | } |
1261 | 1604 | ||
1262 | /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */ |
1605 | /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */ |
1263 | if(hspi->State == HAL_SPI_STATE_READY) |
1606 | if (hspi->State != HAL_SPI_STATE_BUSY_RX) |
1264 | { |
1607 | { |
1265 | hspi->State = HAL_SPI_STATE_BUSY_TX_RX; |
1608 | hspi->State = HAL_SPI_STATE_BUSY_TX_RX; |
1266 | } |
1609 | } |
1267 | 1610 | ||
1268 | /* Set the transaction information */ |
1611 | /* Set the transaction information */ |
Line 1273... | Line 1616... | ||
1273 | hspi->pRxBuffPtr = (uint8_t *)pRxData; |
1616 | hspi->pRxBuffPtr = (uint8_t *)pRxData; |
1274 | hspi->RxXferSize = Size; |
1617 | hspi->RxXferSize = Size; |
1275 | hspi->RxXferCount = Size; |
1618 | hspi->RxXferCount = Size; |
1276 | 1619 | ||
1277 | /* Set the function for IT treatment */ |
1620 | /* Set the function for IT treatment */ |
1278 | if(hspi->Init.DataSize > SPI_DATASIZE_8BIT ) |
1621 | if (hspi->Init.DataSize > SPI_DATASIZE_8BIT) |
1279 | { |
1622 | { |
1280 | hspi->RxISR = SPI_2linesRxISR_16BIT; |
1623 | hspi->RxISR = SPI_2linesRxISR_16BIT; |
1281 | hspi->TxISR = SPI_2linesTxISR_16BIT; |
1624 | hspi->TxISR = SPI_2linesTxISR_16BIT; |
1282 | } |
1625 | } |
1283 | else |
1626 | else |
Line 1286... | Line 1629... | ||
1286 | hspi->TxISR = SPI_2linesTxISR_8BIT; |
1629 | hspi->TxISR = SPI_2linesTxISR_8BIT; |
1287 | } |
1630 | } |
1288 | 1631 | ||
1289 | #if (USE_SPI_CRC != 0U) |
1632 | #if (USE_SPI_CRC != 0U) |
1290 | /* Reset CRC Calculation */ |
1633 | /* Reset CRC Calculation */ |
1291 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
1634 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
1292 | { |
1635 | { |
1293 | SPI_RESET_CRC(hspi); |
1636 | SPI_RESET_CRC(hspi); |
1294 | } |
1637 | } |
1295 | #endif /* USE_SPI_CRC */ |
1638 | #endif /* USE_SPI_CRC */ |
1296 | 1639 | ||
1297 | /* Enable TXE, RXNE and ERR interrupt */ |
1640 | /* Enable TXE, RXNE and ERR interrupt */ |
1298 | __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR)); |
1641 | __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR)); |
1299 | 1642 | ||
1300 | /* Check if the SPI is already enabled */ |
1643 | /* Check if the SPI is already enabled */ |
1301 | if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE) |
1644 | if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) |
1302 | { |
1645 | { |
1303 | /* Enable SPI peripheral */ |
1646 | /* Enable SPI peripheral */ |
1304 | __HAL_SPI_ENABLE(hspi); |
1647 | __HAL_SPI_ENABLE(hspi); |
1305 | } |
1648 | } |
1306 | 1649 | ||
Line 1310... | Line 1653... | ||
1310 | return errorcode; |
1653 | return errorcode; |
1311 | } |
1654 | } |
1312 | 1655 | ||
1313 | /** |
1656 | /** |
1314 | * @brief Transmit an amount of data in non-blocking mode with DMA. |
1657 | * @brief Transmit an amount of data in non-blocking mode with DMA. |
1315 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
1658 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
1316 | * the configuration information for SPI module. |
1659 | * the configuration information for SPI module. |
1317 | * @param pData: pointer to data buffer |
1660 | * @param pData pointer to data buffer |
1318 | * @param Size: amount of data to be sent |
1661 | * @param Size amount of data to be sent |
1319 | * @retval HAL status |
1662 | * @retval HAL status |
1320 | */ |
1663 | */ |
1321 | HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size) |
1664 | HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size) |
1322 | { |
1665 | { |
1323 | HAL_StatusTypeDef errorcode = HAL_OK; |
1666 | HAL_StatusTypeDef errorcode = HAL_OK; |
1324 | 1667 | ||
- | 1668 | /* Check tx dma handle */ |
|
- | 1669 | assert_param(IS_SPI_DMA_HANDLE(hspi->hdmatx)); |
|
- | 1670 | ||
1325 | /* Check Direction parameter */ |
1671 | /* Check Direction parameter */ |
1326 | assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction)); |
1672 | assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction)); |
1327 | 1673 | ||
1328 | /* Process Locked */ |
1674 | /* Process Locked */ |
1329 | __HAL_LOCK(hspi); |
1675 | __HAL_LOCK(hspi); |
1330 | 1676 | ||
1331 | if(hspi->State != HAL_SPI_STATE_READY) |
1677 | if (hspi->State != HAL_SPI_STATE_READY) |
1332 | { |
1678 | { |
1333 | errorcode = HAL_BUSY; |
1679 | errorcode = HAL_BUSY; |
1334 | goto error; |
1680 | goto error; |
1335 | } |
1681 | } |
1336 | 1682 | ||
1337 | if((pData == NULL) || (Size == 0U)) |
1683 | if ((pData == NULL) || (Size == 0U)) |
1338 | { |
1684 | { |
1339 | errorcode = HAL_ERROR; |
1685 | errorcode = HAL_ERROR; |
1340 | goto error; |
1686 | goto error; |
1341 | } |
1687 | } |
1342 | 1688 | ||
Line 1353... | Line 1699... | ||
1353 | hspi->RxISR = NULL; |
1699 | hspi->RxISR = NULL; |
1354 | hspi->RxXferSize = 0U; |
1700 | hspi->RxXferSize = 0U; |
1355 | hspi->RxXferCount = 0U; |
1701 | hspi->RxXferCount = 0U; |
1356 | 1702 | ||
1357 | /* Configure communication direction : 1Line */ |
1703 | /* Configure communication direction : 1Line */ |
1358 | if(hspi->Init.Direction == SPI_DIRECTION_1LINE) |
1704 | if (hspi->Init.Direction == SPI_DIRECTION_1LINE) |
1359 | { |
1705 | { |
- | 1706 | /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */ |
|
- | 1707 | __HAL_SPI_DISABLE(hspi); |
|
1360 | SPI_1LINE_TX(hspi); |
1708 | SPI_1LINE_TX(hspi); |
1361 | } |
1709 | } |
1362 | 1710 | ||
1363 | #if (USE_SPI_CRC != 0U) |
1711 | #if (USE_SPI_CRC != 0U) |
1364 | /* Reset CRC Calculation */ |
1712 | /* Reset CRC Calculation */ |
1365 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
1713 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
1366 | { |
1714 | { |
1367 | SPI_RESET_CRC(hspi); |
1715 | SPI_RESET_CRC(hspi); |
1368 | } |
1716 | } |
1369 | #endif /* USE_SPI_CRC */ |
1717 | #endif /* USE_SPI_CRC */ |
1370 | 1718 | ||
Line 1378... | Line 1726... | ||
1378 | hspi->hdmatx->XferErrorCallback = SPI_DMAError; |
1726 | hspi->hdmatx->XferErrorCallback = SPI_DMAError; |
1379 | 1727 | ||
1380 | /* Set the DMA AbortCpltCallback */ |
1728 | /* Set the DMA AbortCpltCallback */ |
1381 | hspi->hdmatx->XferAbortCallback = NULL; |
1729 | hspi->hdmatx->XferAbortCallback = NULL; |
1382 | 1730 | ||
1383 | /* Enable the Tx DMA Stream */ |
1731 | /* Enable the Tx DMA Stream/Channel */ |
1384 | HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, hspi->TxXferCount); |
1732 | if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, |
- | 1733 | hspi->TxXferCount)) |
|
- | 1734 | { |
|
- | 1735 | /* Update SPI error code */ |
|
- | 1736 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA); |
|
- | 1737 | errorcode = HAL_ERROR; |
|
- | 1738 | ||
- | 1739 | hspi->State = HAL_SPI_STATE_READY; |
|
- | 1740 | goto error; |
|
- | 1741 | } |
|
1385 | 1742 | ||
1386 | /* Check if the SPI is already enabled */ |
1743 | /* Check if the SPI is already enabled */ |
1387 | if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE) |
1744 | if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) |
1388 | { |
1745 | { |
1389 | /* Enable SPI peripheral */ |
1746 | /* Enable SPI peripheral */ |
1390 | __HAL_SPI_ENABLE(hspi); |
1747 | __HAL_SPI_ENABLE(hspi); |
1391 | } |
1748 | } |
1392 | 1749 | ||
1393 | /* Enable the SPI Error Interrupt Bit */ |
1750 | /* Enable the SPI Error Interrupt Bit */ |
1394 | SET_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE); |
1751 | __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_ERR)); |
1395 | 1752 | ||
1396 | /* Enable Tx DMA Request */ |
1753 | /* Enable Tx DMA Request */ |
1397 | SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN); |
1754 | SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN); |
1398 | 1755 | ||
1399 | error : |
1756 | error : |
Line 1402... | Line 1759... | ||
1402 | return errorcode; |
1759 | return errorcode; |
1403 | } |
1760 | } |
1404 | 1761 | ||
1405 | /** |
1762 | /** |
1406 | * @brief Receive an amount of data in non-blocking mode with DMA. |
1763 | * @brief Receive an amount of data in non-blocking mode with DMA. |
- | 1764 | * @note In case of MASTER mode and SPI_DIRECTION_2LINES direction, hdmatx shall be defined. |
|
1407 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
1765 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
1408 | * the configuration information for SPI module. |
1766 | * the configuration information for SPI module. |
1409 | * @param pData: pointer to data buffer |
1767 | * @param pData pointer to data buffer |
1410 | * @note When the CRC feature is enabled the pData Length must be Size + 1. |
1768 | * @note When the CRC feature is enabled the pData Length must be Size + 1. |
1411 | * @param Size: amount of data to be sent |
1769 | * @param Size amount of data to be sent |
1412 | * @retval HAL status |
1770 | * @retval HAL status |
1413 | */ |
1771 | */ |
1414 | HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size) |
1772 | HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size) |
1415 | { |
1773 | { |
1416 | HAL_StatusTypeDef errorcode = HAL_OK; |
1774 | HAL_StatusTypeDef errorcode = HAL_OK; |
1417 | 1775 | ||
- | 1776 | /* Check rx dma handle */ |
|
- | 1777 | assert_param(IS_SPI_DMA_HANDLE(hspi->hdmarx)); |
|
- | 1778 | ||
1418 | if((hspi->Init.Direction == SPI_DIRECTION_2LINES)&&(hspi->Init.Mode == SPI_MODE_MASTER)) |
1779 | if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER)) |
1419 | { |
1780 | { |
1420 | hspi->State = HAL_SPI_STATE_BUSY_RX; |
1781 | hspi->State = HAL_SPI_STATE_BUSY_RX; |
- | 1782 | ||
- | 1783 | /* Check tx dma handle */ |
|
- | 1784 | assert_param(IS_SPI_DMA_HANDLE(hspi->hdmatx)); |
|
- | 1785 | ||
1421 | /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */ |
1786 | /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */ |
1422 | return HAL_SPI_TransmitReceive_DMA(hspi, pData, pData, Size); |
1787 | return HAL_SPI_TransmitReceive_DMA(hspi, pData, pData, Size); |
1423 | } |
1788 | } |
1424 | 1789 | ||
1425 | /* Process Locked */ |
1790 | /* Process Locked */ |
1426 | __HAL_LOCK(hspi); |
1791 | __HAL_LOCK(hspi); |
1427 | 1792 | ||
1428 | if(hspi->State != HAL_SPI_STATE_READY) |
1793 | if (hspi->State != HAL_SPI_STATE_READY) |
1429 | { |
1794 | { |
1430 | errorcode = HAL_BUSY; |
1795 | errorcode = HAL_BUSY; |
1431 | goto error; |
1796 | goto error; |
1432 | } |
1797 | } |
1433 | 1798 | ||
1434 | if((pData == NULL) || (Size == 0U)) |
1799 | if ((pData == NULL) || (Size == 0U)) |
1435 | { |
1800 | { |
1436 | errorcode = HAL_ERROR; |
1801 | errorcode = HAL_ERROR; |
1437 | goto error; |
1802 | goto error; |
1438 | } |
1803 | } |
1439 | 1804 | ||
Line 1449... | Line 1814... | ||
1449 | hspi->TxISR = NULL; |
1814 | hspi->TxISR = NULL; |
1450 | hspi->TxXferSize = 0U; |
1815 | hspi->TxXferSize = 0U; |
1451 | hspi->TxXferCount = 0U; |
1816 | hspi->TxXferCount = 0U; |
1452 | 1817 | ||
1453 | /* Configure communication direction : 1Line */ |
1818 | /* Configure communication direction : 1Line */ |
1454 | if(hspi->Init.Direction == SPI_DIRECTION_1LINE) |
1819 | if (hspi->Init.Direction == SPI_DIRECTION_1LINE) |
1455 | { |
1820 | { |
- | 1821 | /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */ |
|
- | 1822 | __HAL_SPI_DISABLE(hspi); |
|
1456 | SPI_1LINE_RX(hspi); |
1823 | SPI_1LINE_RX(hspi); |
1457 | } |
1824 | } |
1458 | 1825 | ||
1459 | #if (USE_SPI_CRC != 0U) |
1826 | #if (USE_SPI_CRC != 0U) |
1460 | /* Reset CRC Calculation */ |
1827 | /* Reset CRC Calculation */ |
1461 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
1828 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
1462 | { |
1829 | { |
1463 | SPI_RESET_CRC(hspi); |
1830 | SPI_RESET_CRC(hspi); |
1464 | } |
1831 | } |
1465 | #endif /* USE_SPI_CRC */ |
1832 | #endif /* USE_SPI_CRC */ |
1466 | 1833 | ||
Line 1471... | Line 1838... | ||
1471 | hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt; |
1838 | hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt; |
1472 | 1839 | ||
1473 | /* Set the DMA error callback */ |
1840 | /* Set the DMA error callback */ |
1474 | hspi->hdmarx->XferErrorCallback = SPI_DMAError; |
1841 | hspi->hdmarx->XferErrorCallback = SPI_DMAError; |
1475 | 1842 | ||
1476 | /* Set the DMA AbortCpltCallback */ |
1843 | /* Set the DMA AbortCpltCallback */ |
1477 | hspi->hdmarx->XferAbortCallback = NULL; |
1844 | hspi->hdmarx->XferAbortCallback = NULL; |
1478 | 1845 | ||
1479 | /* Enable the Rx DMA Stream */ |
1846 | /* Enable the Rx DMA Stream/Channel */ |
1480 | HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr, hspi->RxXferCount); |
1847 | if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr, |
- | 1848 | hspi->RxXferCount)) |
|
- | 1849 | { |
|
- | 1850 | /* Update SPI error code */ |
|
- | 1851 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA); |
|
- | 1852 | errorcode = HAL_ERROR; |
|
- | 1853 | ||
- | 1854 | hspi->State = HAL_SPI_STATE_READY; |
|
- | 1855 | goto error; |
|
- | 1856 | } |
|
1481 | 1857 | ||
1482 | /* Check if the SPI is already enabled */ |
1858 | /* Check if the SPI is already enabled */ |
1483 | if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE) |
1859 | if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) |
1484 | { |
1860 | { |
1485 | /* Enable SPI peripheral */ |
1861 | /* Enable SPI peripheral */ |
1486 | __HAL_SPI_ENABLE(hspi); |
1862 | __HAL_SPI_ENABLE(hspi); |
1487 | } |
1863 | } |
1488 | 1864 | ||
1489 | /* Enable the SPI Error Interrupt Bit */ |
1865 | /* Enable the SPI Error Interrupt Bit */ |
1490 | SET_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE); |
1866 | __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_ERR)); |
1491 | 1867 | ||
1492 | /* Enable Rx DMA Request */ |
1868 | /* Enable Rx DMA Request */ |
1493 | SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN); |
1869 | SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN); |
1494 | 1870 | ||
1495 | error: |
1871 | error: |
Line 1498... | Line 1874... | ||
1498 | return errorcode; |
1874 | return errorcode; |
1499 | } |
1875 | } |
1500 | 1876 | ||
1501 | /** |
1877 | /** |
1502 | * @brief Transmit and Receive an amount of data in non-blocking mode with DMA. |
1878 | * @brief Transmit and Receive an amount of data in non-blocking mode with DMA. |
1503 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
1879 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
1504 | * the configuration information for SPI module. |
1880 | * the configuration information for SPI module. |
1505 | * @param pTxData: pointer to transmission data buffer |
1881 | * @param pTxData pointer to transmission data buffer |
1506 | * @param pRxData: pointer to reception data buffer |
1882 | * @param pRxData pointer to reception data buffer |
1507 | * @note When the CRC feature is enabled the pRxData Length must be Size + 1 |
1883 | * @note When the CRC feature is enabled the pRxData Length must be Size + 1 |
1508 | * @param Size: amount of data to be sent |
1884 | * @param Size amount of data to be sent |
1509 | * @retval HAL status |
1885 | * @retval HAL status |
1510 | */ |
1886 | */ |
1511 | HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size) |
1887 | HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, |
- | 1888 | uint16_t Size) |
|
1512 | { |
1889 | { |
1513 | uint32_t tmp = 0U, tmp1 = 0U; |
1890 | uint32_t tmp_mode; |
- | 1891 | HAL_SPI_StateTypeDef tmp_state; |
|
1514 | HAL_StatusTypeDef errorcode = HAL_OK; |
1892 | HAL_StatusTypeDef errorcode = HAL_OK; |
1515 | 1893 | ||
- | 1894 | /* Check rx & tx dma handles */ |
|
- | 1895 | assert_param(IS_SPI_DMA_HANDLE(hspi->hdmarx)); |
|
- | 1896 | assert_param(IS_SPI_DMA_HANDLE(hspi->hdmatx)); |
|
- | 1897 | ||
1516 | /* Check Direction parameter */ |
1898 | /* Check Direction parameter */ |
1517 | assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction)); |
1899 | assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction)); |
1518 | 1900 | ||
1519 | /* Process locked */ |
1901 | /* Process locked */ |
1520 | __HAL_LOCK(hspi); |
1902 | __HAL_LOCK(hspi); |
1521 | 1903 | ||
- | 1904 | /* Init temporary variables */ |
|
1522 | tmp = hspi->State; |
1905 | tmp_state = hspi->State; |
1523 | tmp1 = hspi->Init.Mode; |
1906 | tmp_mode = hspi->Init.Mode; |
- | 1907 | ||
1524 | if(!((tmp == HAL_SPI_STATE_READY) || |
1908 | if (!((tmp_state == HAL_SPI_STATE_READY) || |
1525 | ((tmp1 == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp == HAL_SPI_STATE_BUSY_RX)))) |
1909 | ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp_state == HAL_SPI_STATE_BUSY_RX)))) |
1526 | { |
1910 | { |
1527 | errorcode = HAL_BUSY; |
1911 | errorcode = HAL_BUSY; |
1528 | goto error; |
1912 | goto error; |
1529 | } |
1913 | } |
1530 | 1914 | ||
1531 | if((pTxData == NULL ) || (pRxData == NULL ) || (Size == 0U)) |
1915 | if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U)) |
1532 | { |
1916 | { |
1533 | errorcode = HAL_ERROR; |
1917 | errorcode = HAL_ERROR; |
1534 | goto error; |
1918 | goto error; |
1535 | } |
1919 | } |
1536 | 1920 | ||
1537 | /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */ |
1921 | /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */ |
1538 | if(hspi->State == HAL_SPI_STATE_READY) |
1922 | if (hspi->State != HAL_SPI_STATE_BUSY_RX) |
1539 | { |
1923 | { |
1540 | hspi->State = HAL_SPI_STATE_BUSY_TX_RX; |
1924 | hspi->State = HAL_SPI_STATE_BUSY_TX_RX; |
1541 | } |
1925 | } |
1542 | 1926 | ||
1543 | /* Set the transaction information */ |
1927 | /* Set the transaction information */ |
1544 | hspi->ErrorCode = HAL_SPI_ERROR_NONE; |
1928 | hspi->ErrorCode = HAL_SPI_ERROR_NONE; |
1545 | hspi->pTxBuffPtr = (uint8_t*)pTxData; |
1929 | hspi->pTxBuffPtr = (uint8_t *)pTxData; |
1546 | hspi->TxXferSize = Size; |
1930 | hspi->TxXferSize = Size; |
1547 | hspi->TxXferCount = Size; |
1931 | hspi->TxXferCount = Size; |
1548 | hspi->pRxBuffPtr = (uint8_t*)pRxData; |
1932 | hspi->pRxBuffPtr = (uint8_t *)pRxData; |
1549 | hspi->RxXferSize = Size; |
1933 | hspi->RxXferSize = Size; |
1550 | hspi->RxXferCount = Size; |
1934 | hspi->RxXferCount = Size; |
1551 | 1935 | ||
1552 | /* Init field not used in handle to zero */ |
1936 | /* Init field not used in handle to zero */ |
1553 | hspi->RxISR = NULL; |
1937 | hspi->RxISR = NULL; |
1554 | hspi->TxISR = NULL; |
1938 | hspi->TxISR = NULL; |
1555 | 1939 | ||
1556 | #if (USE_SPI_CRC != 0U) |
1940 | #if (USE_SPI_CRC != 0U) |
1557 | /* Reset CRC Calculation */ |
1941 | /* Reset CRC Calculation */ |
1558 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
1942 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
1559 | { |
1943 | { |
1560 | SPI_RESET_CRC(hspi); |
1944 | SPI_RESET_CRC(hspi); |
1561 | } |
1945 | } |
1562 | #endif /* USE_SPI_CRC */ |
1946 | #endif /* USE_SPI_CRC */ |
1563 | 1947 | ||
1564 | /* Check if we are in Rx only or in Rx/Tx Mode and configure the DMA transfer complete callback */ |
1948 | /* Check if we are in Rx only or in Rx/Tx Mode and configure the DMA transfer complete callback */ |
1565 | if(hspi->State == HAL_SPI_STATE_BUSY_RX) |
1949 | if (hspi->State == HAL_SPI_STATE_BUSY_RX) |
1566 | { |
1950 | { |
1567 | /* Set the SPI Rx DMA Half transfer complete callback */ |
1951 | /* Set the SPI Rx DMA Half transfer complete callback */ |
1568 | hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt; |
1952 | hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt; |
1569 | hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt; |
1953 | hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt; |
1570 | } |
1954 | } |
Line 1579... | Line 1963... | ||
1579 | hspi->hdmarx->XferErrorCallback = SPI_DMAError; |
1963 | hspi->hdmarx->XferErrorCallback = SPI_DMAError; |
1580 | 1964 | ||
1581 | /* Set the DMA AbortCpltCallback */ |
1965 | /* Set the DMA AbortCpltCallback */ |
1582 | hspi->hdmarx->XferAbortCallback = NULL; |
1966 | hspi->hdmarx->XferAbortCallback = NULL; |
1583 | 1967 | ||
1584 | /* Enable the Rx DMA Stream */ |
1968 | /* Enable the Rx DMA Stream/Channel */ |
1585 | HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr, hspi->RxXferCount); |
1969 | if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr, |
- | 1970 | hspi->RxXferCount)) |
|
- | 1971 | { |
|
- | 1972 | /* Update SPI error code */ |
|
- | 1973 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA); |
|
- | 1974 | errorcode = HAL_ERROR; |
|
- | 1975 | ||
- | 1976 | hspi->State = HAL_SPI_STATE_READY; |
|
- | 1977 | goto error; |
|
- | 1978 | } |
|
1586 | 1979 | ||
1587 | /* Enable Rx DMA Request */ |
1980 | /* Enable Rx DMA Request */ |
1588 | SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN); |
1981 | SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN); |
1589 | 1982 | ||
1590 | /* Set the SPI Tx DMA transfer complete callback as NULL because the communication closing |
1983 | /* Set the SPI Tx DMA transfer complete callback as NULL because the communication closing |
Line 1592... | Line 1985... | ||
1592 | hspi->hdmatx->XferHalfCpltCallback = NULL; |
1985 | hspi->hdmatx->XferHalfCpltCallback = NULL; |
1593 | hspi->hdmatx->XferCpltCallback = NULL; |
1986 | hspi->hdmatx->XferCpltCallback = NULL; |
1594 | hspi->hdmatx->XferErrorCallback = NULL; |
1987 | hspi->hdmatx->XferErrorCallback = NULL; |
1595 | hspi->hdmatx->XferAbortCallback = NULL; |
1988 | hspi->hdmatx->XferAbortCallback = NULL; |
1596 | 1989 | ||
1597 | /* Enable the Tx DMA Stream */ |
1990 | /* Enable the Tx DMA Stream/Channel */ |
1598 | HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, hspi->TxXferCount); |
1991 | if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, |
- | 1992 | hspi->TxXferCount)) |
|
- | 1993 | { |
|
- | 1994 | /* Update SPI error code */ |
|
- | 1995 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA); |
|
- | 1996 | errorcode = HAL_ERROR; |
|
- | 1997 | ||
- | 1998 | hspi->State = HAL_SPI_STATE_READY; |
|
- | 1999 | goto error; |
|
- | 2000 | } |
|
1599 | 2001 | ||
1600 | /* Check if the SPI is already enabled */ |
2002 | /* Check if the SPI is already enabled */ |
1601 | if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE) |
2003 | if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) |
1602 | { |
2004 | { |
1603 | /* Enable SPI peripheral */ |
2005 | /* Enable SPI peripheral */ |
1604 | __HAL_SPI_ENABLE(hspi); |
2006 | __HAL_SPI_ENABLE(hspi); |
1605 | } |
2007 | } |
1606 | /* Enable the SPI Error Interrupt Bit */ |
2008 | /* Enable the SPI Error Interrupt Bit */ |
1607 | SET_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE); |
2009 | __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_ERR)); |
1608 | 2010 | ||
1609 | /* Enable Tx DMA Request */ |
2011 | /* Enable Tx DMA Request */ |
1610 | SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN); |
2012 | SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN); |
1611 | 2013 | ||
1612 | error : |
2014 | error : |
Line 1624... | Line 2026... | ||
1624 | * - Disable SPI Interrupts (depending of transfer direction) |
2026 | * - Disable SPI Interrupts (depending of transfer direction) |
1625 | * - Disable the DMA transfer in the peripheral register (if enabled) |
2027 | * - Disable the DMA transfer in the peripheral register (if enabled) |
1626 | * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) |
2028 | * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) |
1627 | * - Set handle State to READY |
2029 | * - Set handle State to READY |
1628 | * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. |
2030 | * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. |
1629 | * @note Once transfer is aborted, the __HAL_SPI_CLEAR_OVRFLAG() macro must be called in user application |
- | |
1630 | * before starting new SPI receive process. |
- | |
1631 | * @retval HAL status |
2031 | * @retval HAL status |
1632 | */ |
2032 | */ |
1633 | HAL_StatusTypeDef HAL_SPI_Abort(SPI_HandleTypeDef *hspi) |
2033 | HAL_StatusTypeDef HAL_SPI_Abort(SPI_HandleTypeDef *hspi) |
1634 | { |
2034 | { |
- | 2035 | HAL_StatusTypeDef errorcode; |
|
- | 2036 | __IO uint32_t count; |
|
- | 2037 | __IO uint32_t resetcount; |
|
- | 2038 | ||
- | 2039 | /* Initialized local variable */ |
|
- | 2040 | errorcode = HAL_OK; |
|
1635 | __IO uint32_t count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U); |
2041 | resetcount = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U); |
- | 2042 | count = resetcount; |
|
- | 2043 | ||
- | 2044 | /* Clear ERRIE interrupt to avoid error interrupts generation during Abort procedure */ |
|
- | 2045 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE); |
|
1636 | 2046 | ||
1637 | /* Disable TXEIE, RXNEIE and ERRIE(mode fault event, overrun error, TI frame error) interrupts */ |
2047 | /* Disable TXEIE, RXNEIE and ERRIE(mode fault event, overrun error, TI frame error) interrupts */ |
1638 | if(HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXEIE)) |
2048 | if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXEIE)) |
1639 | { |
2049 | { |
1640 | hspi->TxISR = SPI_AbortTx_ISR; |
2050 | hspi->TxISR = SPI_AbortTx_ISR; |
- | 2051 | /* Wait HAL_SPI_STATE_ABORT state */ |
|
- | 2052 | do |
|
- | 2053 | { |
|
- | 2054 | if (count == 0U) |
|
- | 2055 | { |
|
- | 2056 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT); |
|
- | 2057 | break; |
|
- | 2058 | } |
|
- | 2059 | count--; |
|
- | 2060 | } while (hspi->State != HAL_SPI_STATE_ABORT); |
|
- | 2061 | /* Reset Timeout Counter */ |
|
- | 2062 | count = resetcount; |
|
1641 | } |
2063 | } |
1642 | 2064 | ||
1643 | if(HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE)) |
2065 | if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE)) |
1644 | { |
2066 | { |
1645 | hspi->RxISR = SPI_AbortRx_ISR; |
2067 | hspi->RxISR = SPI_AbortRx_ISR; |
- | 2068 | /* Wait HAL_SPI_STATE_ABORT state */ |
|
- | 2069 | do |
|
- | 2070 | { |
|
- | 2071 | if (count == 0U) |
|
- | 2072 | { |
|
- | 2073 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT); |
|
- | 2074 | break; |
|
- | 2075 | } |
|
- | 2076 | count--; |
|
- | 2077 | } while (hspi->State != HAL_SPI_STATE_ABORT); |
|
- | 2078 | /* Reset Timeout Counter */ |
|
- | 2079 | count = resetcount; |
|
1646 | } |
2080 | } |
1647 | 2081 | ||
1648 | /* Clear ERRIE interrupts in case of DMA Mode */ |
- | |
1649 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE); |
- | |
1650 | - | ||
1651 | /* Disable the SPI DMA Tx or SPI DMA Rx request if enabled */ |
2082 | /* Disable the SPI DMA Tx request if enabled */ |
1652 | if ((HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN)) || (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))) |
2083 | if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN)) |
1653 | { |
2084 | { |
1654 | /* Abort the SPI DMA Tx channel : use blocking DMA Abort API (no callback) */ |
2085 | /* Abort the SPI DMA Tx Stream/Channel : use blocking DMA Abort API (no callback) */ |
1655 | if(hspi->hdmatx != NULL) |
2086 | if (hspi->hdmatx != NULL) |
1656 | { |
2087 | { |
1657 | /* Set the SPI DMA Abort callback : |
2088 | /* Set the SPI DMA Abort callback : |
1658 | will lead to call HAL_SPI_AbortCpltCallback() at end of DMA abort procedure */ |
2089 | will lead to call HAL_SPI_AbortCpltCallback() at end of DMA abort procedure */ |
1659 | hspi->hdmatx->XferAbortCallback = NULL; |
2090 | hspi->hdmatx->XferAbortCallback = NULL; |
1660 | 2091 | ||
1661 | /* Abort DMA Tx Handle linked to SPI Peripheral */ |
2092 | /* Abort DMA Tx Handle linked to SPI Peripheral */ |
1662 | HAL_DMA_Abort(hspi->hdmatx); |
2093 | if (HAL_DMA_Abort(hspi->hdmatx) != HAL_OK) |
- | 2094 | { |
|
- | 2095 | hspi->ErrorCode = HAL_SPI_ERROR_ABORT; |
|
- | 2096 | } |
|
1663 | 2097 | ||
1664 | /* Disable Tx DMA Request */ |
2098 | /* Disable Tx DMA Request */ |
1665 | CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXDMAEN)); |
2099 | CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXDMAEN)); |
1666 | 2100 | ||
1667 | /* Wait until TXE flag is set */ |
2101 | /* Wait until TXE flag is set */ |
1668 | do |
2102 | do |
1669 | { |
2103 | { |
1670 | if(count-- == 0U) |
2104 | if (count == 0U) |
1671 | { |
2105 | { |
1672 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
2106 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT); |
1673 | break; |
2107 | break; |
1674 | } |
2108 | } |
1675 | } |
2109 | count--; |
1676 | while((hspi->Instance->SR & SPI_FLAG_TXE) == RESET); |
2110 | } while ((hspi->Instance->SR & SPI_FLAG_TXE) == RESET); |
1677 | } |
2111 | } |
- | 2112 | } |
|
- | 2113 | ||
- | 2114 | /* Disable the SPI DMA Rx request if enabled */ |
|
- | 2115 | if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN)) |
|
- | 2116 | { |
|
1678 | /* Abort the SPI DMA Rx channel : use blocking DMA Abort API (no callback) */ |
2117 | /* Abort the SPI DMA Rx Stream/Channel : use blocking DMA Abort API (no callback) */ |
1679 | if(hspi->hdmarx != NULL) |
2118 | if (hspi->hdmarx != NULL) |
1680 | { |
2119 | { |
1681 | /* Set the SPI DMA Abort callback : |
2120 | /* Set the SPI DMA Abort callback : |
1682 | will lead to call HAL_SPI_AbortCpltCallback() at end of DMA abort procedure */ |
2121 | will lead to call HAL_SPI_AbortCpltCallback() at end of DMA abort procedure */ |
1683 | hspi->hdmarx->XferAbortCallback = NULL; |
2122 | hspi->hdmarx->XferAbortCallback = NULL; |
1684 | 2123 | ||
1685 | /* Abort DMA Rx Handle linked to SPI Peripheral */ |
2124 | /* Abort DMA Rx Handle linked to SPI Peripheral */ |
1686 | HAL_DMA_Abort(hspi->hdmarx); |
2125 | if (HAL_DMA_Abort(hspi->hdmarx) != HAL_OK) |
- | 2126 | { |
|
- | 2127 | hspi->ErrorCode = HAL_SPI_ERROR_ABORT; |
|
- | 2128 | } |
|
1687 | 2129 | ||
1688 | /* Disable peripheral */ |
2130 | /* Disable peripheral */ |
1689 | __HAL_SPI_DISABLE(hspi); |
2131 | __HAL_SPI_DISABLE(hspi); |
1690 | 2132 | ||
1691 | /* Disable Rx DMA Request */ |
2133 | /* Disable Rx DMA Request */ |
1692 | CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_RXDMAEN)); |
2134 | CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_RXDMAEN)); |
1693 | - | ||
1694 | } |
2135 | } |
1695 | } |
2136 | } |
1696 | /* Reset Tx and Rx transfer counters */ |
2137 | /* Reset Tx and Rx transfer counters */ |
1697 | hspi->RxXferCount = 0U; |
2138 | hspi->RxXferCount = 0U; |
1698 | hspi->TxXferCount = 0U; |
2139 | hspi->TxXferCount = 0U; |
1699 | 2140 | ||
- | 2141 | /* Check error during Abort procedure */ |
|
- | 2142 | if (hspi->ErrorCode == HAL_SPI_ERROR_ABORT) |
|
- | 2143 | { |
|
- | 2144 | /* return HAL_Error in case of error during Abort procedure */ |
|
- | 2145 | errorcode = HAL_ERROR; |
|
- | 2146 | } |
|
- | 2147 | else |
|
- | 2148 | { |
|
1700 | /* Reset errorCode */ |
2149 | /* Reset errorCode */ |
1701 | hspi->ErrorCode = HAL_SPI_ERROR_NONE; |
2150 | hspi->ErrorCode = HAL_SPI_ERROR_NONE; |
- | 2151 | } |
|
1702 | 2152 | ||
1703 | /* Clear the Error flags in the SR register */ |
2153 | /* Clear the Error flags in the SR register */ |
1704 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
2154 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
1705 | 2155 | ||
1706 | /* Restore hspi->state to ready */ |
2156 | /* Restore hspi->state to ready */ |
1707 | hspi->State = HAL_SPI_STATE_READY; |
2157 | hspi->State = HAL_SPI_STATE_READY; |
1708 | 2158 | ||
1709 | return HAL_OK; |
2159 | return errorcode; |
1710 | } |
2160 | } |
1711 | 2161 | ||
1712 | /** |
2162 | /** |
1713 | * @brief Abort ongoing transfer (Interrupt mode). |
2163 | * @brief Abort ongoing transfer (Interrupt mode). |
1714 | * @param hspi SPI handle. |
2164 | * @param hspi SPI handle. |
Line 1720... | Line 2170... | ||
1720 | * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) |
2170 | * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) |
1721 | * - Set handle State to READY |
2171 | * - Set handle State to READY |
1722 | * - At abort completion, call user abort complete callback |
2172 | * - At abort completion, call user abort complete callback |
1723 | * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be |
2173 | * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be |
1724 | * considered as completed only when user abort complete callback is executed (not when exiting function). |
2174 | * considered as completed only when user abort complete callback is executed (not when exiting function). |
1725 | * @note Once transfer is aborted, the __HAL_SPI_CLEAR_OVRFLAG() macro must be called in user application |
- | |
1726 | * before starting new SPI receive process. |
- | |
1727 | * @retval HAL status |
2175 | * @retval HAL status |
1728 | */ |
2176 | */ |
1729 | HAL_StatusTypeDef HAL_SPI_Abort_IT(SPI_HandleTypeDef *hspi) |
2177 | HAL_StatusTypeDef HAL_SPI_Abort_IT(SPI_HandleTypeDef *hspi) |
1730 | { |
2178 | { |
- | 2179 | HAL_StatusTypeDef errorcode; |
|
1731 | uint32_t abortcplt; |
2180 | uint32_t abortcplt ; |
- | 2181 | __IO uint32_t count; |
|
- | 2182 | __IO uint32_t resetcount; |
|
- | 2183 | ||
- | 2184 | /* Initialized local variable */ |
|
- | 2185 | errorcode = HAL_OK; |
|
- | 2186 | abortcplt = 1U; |
|
- | 2187 | resetcount = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U); |
|
- | 2188 | count = resetcount; |
|
- | 2189 | ||
- | 2190 | /* Clear ERRIE interrupt to avoid error interrupts generation during Abort procedure */ |
|
- | 2191 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE); |
|
1732 | 2192 | ||
1733 | /* Change Rx and Tx Irq Handler to Disable TXEIE, RXNEIE and ERRIE interrupts */ |
2193 | /* Change Rx and Tx Irq Handler to Disable TXEIE, RXNEIE and ERRIE interrupts */ |
1734 | if(HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXEIE)) |
2194 | if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXEIE)) |
1735 | { |
2195 | { |
1736 | hspi->TxISR = SPI_AbortTx_ISR; |
2196 | hspi->TxISR = SPI_AbortTx_ISR; |
- | 2197 | /* Wait HAL_SPI_STATE_ABORT state */ |
|
- | 2198 | do |
|
- | 2199 | { |
|
- | 2200 | if (count == 0U) |
|
- | 2201 | { |
|
- | 2202 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT); |
|
- | 2203 | break; |
|
- | 2204 | } |
|
- | 2205 | count--; |
|
- | 2206 | } while (hspi->State != HAL_SPI_STATE_ABORT); |
|
- | 2207 | /* Reset Timeout Counter */ |
|
- | 2208 | count = resetcount; |
|
1737 | } |
2209 | } |
1738 | 2210 | ||
1739 | if(HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE)) |
2211 | if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE)) |
1740 | { |
2212 | { |
1741 | hspi->RxISR = SPI_AbortRx_ISR; |
2213 | hspi->RxISR = SPI_AbortRx_ISR; |
- | 2214 | /* Wait HAL_SPI_STATE_ABORT state */ |
|
- | 2215 | do |
|
- | 2216 | { |
|
- | 2217 | if (count == 0U) |
|
- | 2218 | { |
|
- | 2219 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT); |
|
- | 2220 | break; |
|
- | 2221 | } |
|
- | 2222 | count--; |
|
- | 2223 | } while (hspi->State != HAL_SPI_STATE_ABORT); |
|
- | 2224 | /* Reset Timeout Counter */ |
|
- | 2225 | count = resetcount; |
|
1742 | } |
2226 | } |
1743 | 2227 | ||
1744 | /* Clear ERRIE interrupts in case of DMA Mode */ |
- | |
1745 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE); |
- | |
1746 | - | ||
1747 | abortcplt = 1U; |
- | |
1748 | - | ||
1749 | /* If DMA Tx and/or DMA Rx Handles are associated to SPI Handle, DMA Abort complete callbacks should be initialised |
2228 | /* If DMA Tx and/or DMA Rx Handles are associated to SPI Handle, DMA Abort complete callbacks should be initialised |
1750 | before any call to DMA Abort functions */ |
2229 | before any call to DMA Abort functions */ |
1751 | /* DMA Tx Handle is valid */ |
2230 | /* DMA Tx Handle is valid */ |
1752 | if(hspi->hdmatx != NULL) |
2231 | if (hspi->hdmatx != NULL) |
1753 | { |
2232 | { |
1754 | /* Set DMA Abort Complete callback if UART DMA Tx request if enabled. |
2233 | /* Set DMA Abort Complete callback if UART DMA Tx request if enabled. |
1755 | Otherwise, set it to NULL */ |
2234 | Otherwise, set it to NULL */ |
1756 | if(HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN)) |
2235 | if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN)) |
1757 | { |
2236 | { |
1758 | hspi->hdmatx->XferAbortCallback = SPI_DMATxAbortCallback; |
2237 | hspi->hdmatx->XferAbortCallback = SPI_DMATxAbortCallback; |
1759 | } |
2238 | } |
1760 | else |
2239 | else |
1761 | { |
2240 | { |
1762 | hspi->hdmatx->XferAbortCallback = NULL; |
2241 | hspi->hdmatx->XferAbortCallback = NULL; |
1763 | } |
2242 | } |
1764 | } |
2243 | } |
1765 | /* DMA Rx Handle is valid */ |
2244 | /* DMA Rx Handle is valid */ |
1766 | if(hspi->hdmarx != NULL) |
2245 | if (hspi->hdmarx != NULL) |
1767 | { |
2246 | { |
1768 | /* Set DMA Abort Complete callback if UART DMA Rx request if enabled. |
2247 | /* Set DMA Abort Complete callback if UART DMA Rx request if enabled. |
1769 | Otherwise, set it to NULL */ |
2248 | Otherwise, set it to NULL */ |
1770 | if(HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN)) |
2249 | if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN)) |
1771 | { |
2250 | { |
1772 | hspi->hdmarx->XferAbortCallback = SPI_DMARxAbortCallback; |
2251 | hspi->hdmarx->XferAbortCallback = SPI_DMARxAbortCallback; |
1773 | } |
2252 | } |
1774 | else |
2253 | else |
1775 | { |
2254 | { |
1776 | hspi->hdmarx->XferAbortCallback = NULL; |
2255 | hspi->hdmarx->XferAbortCallback = NULL; |
1777 | } |
2256 | } |
1778 | } |
2257 | } |
1779 | 2258 | ||
1780 | /* Disable the SPI DMA Tx or the SPI Rx request if enabled */ |
2259 | /* Disable the SPI DMA Tx request if enabled */ |
1781 | if((HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN)) && (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))) |
- | |
1782 | { |
- | |
1783 | /* Abort the SPI DMA Tx channel */ |
- | |
1784 | if(hspi->hdmatx != NULL) |
- | |
1785 | { |
- | |
1786 | /* Abort DMA Tx Handle linked to SPI Peripheral */ |
- | |
1787 | if(HAL_DMA_Abort_IT(hspi->hdmatx) != HAL_OK) |
- | |
1788 | { |
- | |
1789 | hspi->hdmatx->XferAbortCallback = NULL; |
- | |
1790 | } |
- | |
1791 | else |
- | |
1792 | { |
- | |
1793 | abortcplt = 0U; |
- | |
1794 | } |
- | |
1795 | } |
- | |
1796 | /* Abort the SPI DMA Rx channel */ |
- | |
1797 | if(hspi->hdmarx != NULL) |
- | |
1798 | { |
- | |
1799 | /* Abort DMA Rx Handle linked to SPI Peripheral */ |
- | |
1800 | if(HAL_DMA_Abort_IT(hspi->hdmarx)!= HAL_OK) |
- | |
1801 | { |
- | |
1802 | hspi->hdmarx->XferAbortCallback = NULL; |
- | |
1803 | abortcplt = 1U; |
- | |
1804 | } |
- | |
1805 | else |
- | |
1806 | { |
- | |
1807 | abortcplt = 0U; |
- | |
1808 | } |
- | |
1809 | } |
- | |
1810 | } |
- | |
1811 | - | ||
1812 | /* Disable the SPI DMA Tx or the SPI Rx request if enabled */ |
- | |
1813 | if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN)) |
2260 | if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN)) |
1814 | { |
2261 | { |
1815 | /* Abort the SPI DMA Tx channel */ |
2262 | /* Abort the SPI DMA Tx Stream/Channel */ |
1816 | if(hspi->hdmatx != NULL) |
2263 | if (hspi->hdmatx != NULL) |
1817 | { |
2264 | { |
1818 | /* Abort DMA Tx Handle linked to SPI Peripheral */ |
2265 | /* Abort DMA Tx Handle linked to SPI Peripheral */ |
1819 | if(HAL_DMA_Abort_IT(hspi->hdmatx) != HAL_OK) |
2266 | if (HAL_DMA_Abort_IT(hspi->hdmatx) != HAL_OK) |
1820 | { |
2267 | { |
1821 | hspi->hdmatx->XferAbortCallback = NULL; |
2268 | hspi->hdmatx->XferAbortCallback = NULL; |
- | 2269 | hspi->ErrorCode = HAL_SPI_ERROR_ABORT; |
|
1822 | } |
2270 | } |
1823 | else |
2271 | else |
1824 | { |
2272 | { |
1825 | abortcplt = 0U; |
2273 | abortcplt = 0U; |
1826 | } |
2274 | } |
1827 | } |
2275 | } |
1828 | } |
2276 | } |
1829 | /* Disable the SPI DMA Tx or the SPI Rx request if enabled */ |
2277 | /* Disable the SPI DMA Rx request if enabled */ |
1830 | if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN)) |
2278 | if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN)) |
1831 | { |
2279 | { |
1832 | /* Abort the SPI DMA Rx channel */ |
2280 | /* Abort the SPI DMA Rx Stream/Channel */ |
1833 | if(hspi->hdmarx != NULL) |
2281 | if (hspi->hdmarx != NULL) |
1834 | { |
2282 | { |
1835 | /* Abort DMA Rx Handle linked to SPI Peripheral */ |
2283 | /* Abort DMA Rx Handle linked to SPI Peripheral */ |
1836 | if(HAL_DMA_Abort_IT(hspi->hdmarx)!= HAL_OK) |
2284 | if (HAL_DMA_Abort_IT(hspi->hdmarx) != HAL_OK) |
1837 | { |
2285 | { |
1838 | hspi->hdmarx->XferAbortCallback = NULL; |
2286 | hspi->hdmarx->XferAbortCallback = NULL; |
- | 2287 | hspi->ErrorCode = HAL_SPI_ERROR_ABORT; |
|
1839 | } |
2288 | } |
1840 | else |
2289 | else |
1841 | { |
2290 | { |
1842 | abortcplt = 0U; |
2291 | abortcplt = 0U; |
1843 | } |
2292 | } |
1844 | } |
2293 | } |
1845 | } |
2294 | } |
1846 | 2295 | ||
1847 | if(abortcplt == 1U) |
2296 | if (abortcplt == 1U) |
1848 | { |
2297 | { |
1849 | /* Reset Tx and Rx transfer counters */ |
2298 | /* Reset Tx and Rx transfer counters */ |
1850 | hspi->RxXferCount = 0U; |
2299 | hspi->RxXferCount = 0U; |
1851 | hspi->TxXferCount = 0U; |
2300 | hspi->TxXferCount = 0U; |
1852 | 2301 | ||
- | 2302 | /* Check error during Abort procedure */ |
|
- | 2303 | if (hspi->ErrorCode == HAL_SPI_ERROR_ABORT) |
|
- | 2304 | { |
|
- | 2305 | /* return HAL_Error in case of error during Abort procedure */ |
|
- | 2306 | errorcode = HAL_ERROR; |
|
- | 2307 | } |
|
- | 2308 | else |
|
- | 2309 | { |
|
1853 | /* Reset errorCode */ |
2310 | /* Reset errorCode */ |
1854 | hspi->ErrorCode = HAL_SPI_ERROR_NONE; |
2311 | hspi->ErrorCode = HAL_SPI_ERROR_NONE; |
- | 2312 | } |
|
1855 | 2313 | ||
1856 | /* Clear the Error flags in the SR register */ |
2314 | /* Clear the Error flags in the SR register */ |
1857 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
2315 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
1858 | 2316 | ||
1859 | /* Restore hspi->State to Ready */ |
2317 | /* Restore hspi->State to Ready */ |
1860 | hspi->State = HAL_SPI_STATE_READY; |
2318 | hspi->State = HAL_SPI_STATE_READY; |
1861 | 2319 | ||
1862 | /* As no DMA to be aborted, call directly user Abort complete callback */ |
2320 | /* As no DMA to be aborted, call directly user Abort complete callback */ |
- | 2321 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 2322 | hspi->AbortCpltCallback(hspi); |
|
- | 2323 | #else |
|
1863 | HAL_SPI_AbortCpltCallback(hspi); |
2324 | HAL_SPI_AbortCpltCallback(hspi); |
- | 2325 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
1864 | } |
2326 | } |
- | 2327 | ||
1865 | return HAL_OK; |
2328 | return errorcode; |
1866 | } |
2329 | } |
1867 | 2330 | ||
1868 | /** |
2331 | /** |
1869 | * @brief Pause the DMA Transfer. |
2332 | * @brief Pause the DMA Transfer. |
1870 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
2333 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
1871 | * the configuration information for the specified SPI module. |
2334 | * the configuration information for the specified SPI module. |
1872 | * @retval HAL status |
2335 | * @retval HAL status |
1873 | */ |
2336 | */ |
1874 | HAL_StatusTypeDef HAL_SPI_DMAPause(SPI_HandleTypeDef *hspi) |
2337 | HAL_StatusTypeDef HAL_SPI_DMAPause(SPI_HandleTypeDef *hspi) |
1875 | { |
2338 | { |
Line 1885... | Line 2348... | ||
1885 | return HAL_OK; |
2348 | return HAL_OK; |
1886 | } |
2349 | } |
1887 | 2350 | ||
1888 | /** |
2351 | /** |
1889 | * @brief Resume the DMA Transfer. |
2352 | * @brief Resume the DMA Transfer. |
1890 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
2353 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
1891 | * the configuration information for the specified SPI module. |
2354 | * the configuration information for the specified SPI module. |
1892 | * @retval HAL status |
2355 | * @retval HAL status |
1893 | */ |
2356 | */ |
1894 | HAL_StatusTypeDef HAL_SPI_DMAResume(SPI_HandleTypeDef *hspi) |
2357 | HAL_StatusTypeDef HAL_SPI_DMAResume(SPI_HandleTypeDef *hspi) |
1895 | { |
2358 | { |
Line 1904... | Line 2367... | ||
1904 | 2367 | ||
1905 | return HAL_OK; |
2368 | return HAL_OK; |
1906 | } |
2369 | } |
1907 | 2370 | ||
1908 | /** |
2371 | /** |
1909 | * @brief Stop the DMA Transfer. |
2372 | * @brief Stop the DMA Transfer. |
1910 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
2373 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
1911 | * the configuration information for the specified SPI module. |
2374 | * the configuration information for the specified SPI module. |
1912 | * @retval HAL status |
2375 | * @retval HAL status |
1913 | */ |
2376 | */ |
1914 | HAL_StatusTypeDef HAL_SPI_DMAStop(SPI_HandleTypeDef *hspi) |
2377 | HAL_StatusTypeDef HAL_SPI_DMAStop(SPI_HandleTypeDef *hspi) |
1915 | { |
2378 | { |
- | 2379 | HAL_StatusTypeDef errorcode = HAL_OK; |
|
1916 | /* The Lock is not implemented on this API to allow the user application |
2380 | /* The Lock is not implemented on this API to allow the user application |
1917 | to call the HAL SPI API under callbacks HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback(): |
2381 | to call the HAL SPI API under callbacks HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback(): |
1918 | when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated |
2382 | when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated |
1919 | and the correspond call back is executed HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback() |
2383 | and the correspond call back is executed HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback() |
1920 | */ |
2384 | */ |
1921 | 2385 | ||
1922 | /* Abort the SPI DMA tx Stream */ |
2386 | /* Abort the SPI DMA tx Stream/Channel */ |
1923 | if(hspi->hdmatx != NULL) |
2387 | if (hspi->hdmatx != NULL) |
1924 | { |
2388 | { |
1925 | HAL_DMA_Abort(hspi->hdmatx); |
2389 | if (HAL_OK != HAL_DMA_Abort(hspi->hdmatx)) |
- | 2390 | { |
|
- | 2391 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA); |
|
- | 2392 | errorcode = HAL_ERROR; |
|
- | 2393 | } |
|
1926 | } |
2394 | } |
1927 | /* Abort the SPI DMA rx Stream */ |
2395 | /* Abort the SPI DMA rx Stream/Channel */ |
1928 | if(hspi->hdmarx != NULL) |
2396 | if (hspi->hdmarx != NULL) |
1929 | { |
2397 | { |
1930 | HAL_DMA_Abort(hspi->hdmarx); |
2398 | if (HAL_OK != HAL_DMA_Abort(hspi->hdmarx)) |
- | 2399 | { |
|
- | 2400 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA); |
|
- | 2401 | errorcode = HAL_ERROR; |
|
- | 2402 | } |
|
1931 | } |
2403 | } |
1932 | 2404 | ||
1933 | /* Disable the SPI DMA Tx & Rx requests */ |
2405 | /* Disable the SPI DMA Tx & Rx requests */ |
1934 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN); |
2406 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN); |
1935 | hspi->State = HAL_SPI_STATE_READY; |
2407 | hspi->State = HAL_SPI_STATE_READY; |
1936 | return HAL_OK; |
2408 | return errorcode; |
1937 | } |
2409 | } |
1938 | 2410 | ||
1939 | /** |
2411 | /** |
1940 | * @brief Handle SPI interrupt request. |
2412 | * @brief Handle SPI interrupt request. |
1941 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
2413 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
1942 | * the configuration information for the specified SPI module. |
2414 | * the configuration information for the specified SPI module. |
1943 | * @retval None |
2415 | * @retval None |
1944 | */ |
2416 | */ |
1945 | void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi) |
2417 | void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi) |
1946 | { |
2418 | { |
1947 | uint32_t itsource = hspi->Instance->CR2; |
2419 | uint32_t itsource = hspi->Instance->CR2; |
1948 | uint32_t itflag = hspi->Instance->SR; |
2420 | uint32_t itflag = hspi->Instance->SR; |
1949 | 2421 | ||
1950 | /* SPI in mode Receiver ----------------------------------------------------*/ |
2422 | /* SPI in mode Receiver ----------------------------------------------------*/ |
1951 | if(((itflag & SPI_FLAG_OVR) == RESET) && |
2423 | if ((SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) == RESET) && |
1952 | ((itflag & SPI_FLAG_RXNE) != RESET) && ((itsource & SPI_IT_RXNE) != RESET)) |
2424 | (SPI_CHECK_FLAG(itflag, SPI_FLAG_RXNE) != RESET) && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_RXNE) != RESET)) |
1953 | { |
2425 | { |
1954 | hspi->RxISR(hspi); |
2426 | hspi->RxISR(hspi); |
1955 | return; |
2427 | return; |
1956 | } |
2428 | } |
1957 | 2429 | ||
1958 | /* SPI in mode Transmitter -------------------------------------------------*/ |
2430 | /* SPI in mode Transmitter -------------------------------------------------*/ |
1959 | if(((itflag & SPI_FLAG_TXE) != RESET) && ((itsource & SPI_IT_TXE) != RESET)) |
2431 | if ((SPI_CHECK_FLAG(itflag, SPI_FLAG_TXE) != RESET) && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_TXE) != RESET)) |
1960 | { |
2432 | { |
1961 | hspi->TxISR(hspi); |
2433 | hspi->TxISR(hspi); |
1962 | return; |
2434 | return; |
1963 | } |
2435 | } |
1964 | 2436 | ||
1965 | /* SPI in Error Treatment --------------------------------------------------*/ |
2437 | /* SPI in Error Treatment --------------------------------------------------*/ |
1966 | if(((itflag & (SPI_FLAG_MODF | SPI_FLAG_OVR)) != RESET) && ((itsource & SPI_IT_ERR) != RESET)) |
2438 | if (((SPI_CHECK_FLAG(itflag, SPI_FLAG_MODF) != RESET) || (SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) != RESET)) |
- | 2439 | && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_ERR) != RESET)) |
|
1967 | { |
2440 | { |
1968 | /* SPI Overrun error interrupt occurred ----------------------------------*/ |
2441 | /* SPI Overrun error interrupt occurred ----------------------------------*/ |
1969 | if((itflag & SPI_FLAG_OVR) != RESET) |
2442 | if (SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) != RESET) |
1970 | { |
2443 | { |
1971 | if(hspi->State != HAL_SPI_STATE_BUSY_TX) |
2444 | if (hspi->State != HAL_SPI_STATE_BUSY_TX) |
1972 | { |
2445 | { |
1973 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_OVR); |
2446 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_OVR); |
1974 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
2447 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
1975 | } |
2448 | } |
1976 | else |
2449 | else |
Line 1979... | Line 2452... | ||
1979 | return; |
2452 | return; |
1980 | } |
2453 | } |
1981 | } |
2454 | } |
1982 | 2455 | ||
1983 | /* SPI Mode Fault error interrupt occurred -------------------------------*/ |
2456 | /* SPI Mode Fault error interrupt occurred -------------------------------*/ |
1984 | if((itflag & SPI_FLAG_MODF) != RESET) |
2457 | if (SPI_CHECK_FLAG(itflag, SPI_FLAG_MODF) != RESET) |
1985 | { |
2458 | { |
1986 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_MODF); |
2459 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_MODF); |
1987 | __HAL_SPI_CLEAR_MODFFLAG(hspi); |
2460 | __HAL_SPI_CLEAR_MODFFLAG(hspi); |
1988 | } |
2461 | } |
1989 | 2462 | ||
- | 2463 | /* SPI Frame error interrupt occurred ------------------------------------*/ |
|
- | 2464 | ||
1990 | if(hspi->ErrorCode != HAL_SPI_ERROR_NONE) |
2465 | if (hspi->ErrorCode != HAL_SPI_ERROR_NONE) |
1991 | { |
2466 | { |
1992 | /* Disable all interrupts */ |
2467 | /* Disable all interrupts */ |
1993 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE | SPI_IT_TXE | SPI_IT_ERR); |
2468 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE | SPI_IT_TXE | SPI_IT_ERR); |
1994 | 2469 | ||
1995 | hspi->State = HAL_SPI_STATE_READY; |
2470 | hspi->State = HAL_SPI_STATE_READY; |
1996 | /* Disable the SPI DMA requests if enabled */ |
2471 | /* Disable the SPI DMA requests if enabled */ |
1997 | if ((HAL_IS_BIT_SET(itsource, SPI_CR2_TXDMAEN))||(HAL_IS_BIT_SET(itsource, SPI_CR2_RXDMAEN))) |
2472 | if ((HAL_IS_BIT_SET(itsource, SPI_CR2_TXDMAEN)) || (HAL_IS_BIT_SET(itsource, SPI_CR2_RXDMAEN))) |
1998 | { |
2473 | { |
1999 | CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN)); |
2474 | CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN)); |
2000 | 2475 | ||
2001 | /* Abort the SPI DMA Rx channel */ |
2476 | /* Abort the SPI DMA Rx channel */ |
2002 | if(hspi->hdmarx != NULL) |
2477 | if (hspi->hdmarx != NULL) |
2003 | { |
2478 | { |
2004 | /* Set the SPI DMA Abort callback : |
2479 | /* Set the SPI DMA Abort callback : |
2005 | will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */ |
2480 | will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */ |
2006 | hspi->hdmarx->XferAbortCallback = SPI_DMAAbortOnError; |
2481 | hspi->hdmarx->XferAbortCallback = SPI_DMAAbortOnError; |
2007 | HAL_DMA_Abort_IT(hspi->hdmarx); |
2482 | if (HAL_OK != HAL_DMA_Abort_IT(hspi->hdmarx)) |
- | 2483 | { |
|
- | 2484 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT); |
|
- | 2485 | } |
|
2008 | } |
2486 | } |
2009 | /* Abort the SPI DMA Tx channel */ |
2487 | /* Abort the SPI DMA Tx channel */ |
2010 | if(hspi->hdmatx != NULL) |
2488 | if (hspi->hdmatx != NULL) |
2011 | { |
2489 | { |
2012 | /* Set the SPI DMA Abort callback : |
2490 | /* Set the SPI DMA Abort callback : |
2013 | will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */ |
2491 | will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */ |
2014 | hspi->hdmatx->XferAbortCallback = SPI_DMAAbortOnError; |
2492 | hspi->hdmatx->XferAbortCallback = SPI_DMAAbortOnError; |
2015 | HAL_DMA_Abort_IT(hspi->hdmatx); |
2493 | if (HAL_OK != HAL_DMA_Abort_IT(hspi->hdmatx)) |
- | 2494 | { |
|
- | 2495 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT); |
|
- | 2496 | } |
|
2016 | } |
2497 | } |
2017 | } |
2498 | } |
2018 | else |
2499 | else |
2019 | { |
2500 | { |
2020 | /* Call user error callback */ |
2501 | /* Call user error callback */ |
- | 2502 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 2503 | hspi->ErrorCallback(hspi); |
|
- | 2504 | #else |
|
2021 | HAL_SPI_ErrorCallback(hspi); |
2505 | HAL_SPI_ErrorCallback(hspi); |
- | 2506 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
2022 | } |
2507 | } |
2023 | } |
2508 | } |
2024 | return; |
2509 | return; |
2025 | } |
2510 | } |
2026 | } |
2511 | } |
2027 | 2512 | ||
2028 | /** |
2513 | /** |
2029 | * @brief Tx Transfer completed callback. |
2514 | * @brief Tx Transfer completed callback. |
2030 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
2515 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2031 | * the configuration information for SPI module. |
2516 | * the configuration information for SPI module. |
2032 | * @retval None |
2517 | * @retval None |
2033 | */ |
2518 | */ |
2034 | __weak void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) |
2519 | __weak void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) |
2035 | { |
2520 | { |
2036 | /* Prevent unused argument(s) compilation warning */ |
2521 | /* Prevent unused argument(s) compilation warning */ |
2037 | UNUSED(hspi); |
2522 | UNUSED(hspi); |
- | 2523 | ||
2038 | /* NOTE : This function should not be modified, when the callback is needed, |
2524 | /* NOTE : This function should not be modified, when the callback is needed, |
2039 | the HAL_SPI_TxCpltCallback should be implemented in the user file |
2525 | the HAL_SPI_TxCpltCallback should be implemented in the user file |
2040 | */ |
2526 | */ |
2041 | } |
2527 | } |
2042 | 2528 | ||
2043 | /** |
2529 | /** |
2044 | * @brief Rx Transfer completed callback. |
2530 | * @brief Rx Transfer completed callback. |
2045 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
2531 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2046 | * the configuration information for SPI module. |
2532 | * the configuration information for SPI module. |
2047 | * @retval None |
2533 | * @retval None |
2048 | */ |
2534 | */ |
2049 | __weak void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) |
2535 | __weak void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) |
2050 | { |
2536 | { |
2051 | /* Prevent unused argument(s) compilation warning */ |
2537 | /* Prevent unused argument(s) compilation warning */ |
2052 | UNUSED(hspi); |
2538 | UNUSED(hspi); |
- | 2539 | ||
2053 | /* NOTE : This function should not be modified, when the callback is needed, |
2540 | /* NOTE : This function should not be modified, when the callback is needed, |
2054 | the HAL_SPI_RxCpltCallback should be implemented in the user file |
2541 | the HAL_SPI_RxCpltCallback should be implemented in the user file |
2055 | */ |
2542 | */ |
2056 | } |
2543 | } |
2057 | 2544 | ||
2058 | /** |
2545 | /** |
2059 | * @brief Tx and Rx Transfer completed callback. |
2546 | * @brief Tx and Rx Transfer completed callback. |
2060 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
2547 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2061 | * the configuration information for SPI module. |
2548 | * the configuration information for SPI module. |
2062 | * @retval None |
2549 | * @retval None |
2063 | */ |
2550 | */ |
2064 | __weak void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) |
2551 | __weak void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) |
2065 | { |
2552 | { |
2066 | /* Prevent unused argument(s) compilation warning */ |
2553 | /* Prevent unused argument(s) compilation warning */ |
2067 | UNUSED(hspi); |
2554 | UNUSED(hspi); |
- | 2555 | ||
2068 | /* NOTE : This function should not be modified, when the callback is needed, |
2556 | /* NOTE : This function should not be modified, when the callback is needed, |
2069 | the HAL_SPI_TxRxCpltCallback should be implemented in the user file |
2557 | the HAL_SPI_TxRxCpltCallback should be implemented in the user file |
2070 | */ |
2558 | */ |
2071 | } |
2559 | } |
2072 | 2560 | ||
2073 | /** |
2561 | /** |
2074 | * @brief Tx Half Transfer completed callback. |
2562 | * @brief Tx Half Transfer completed callback. |
2075 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
2563 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2076 | * the configuration information for SPI module. |
2564 | * the configuration information for SPI module. |
2077 | * @retval None |
2565 | * @retval None |
2078 | */ |
2566 | */ |
2079 | __weak void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi) |
2567 | __weak void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi) |
2080 | { |
2568 | { |
2081 | /* Prevent unused argument(s) compilation warning */ |
2569 | /* Prevent unused argument(s) compilation warning */ |
2082 | UNUSED(hspi); |
2570 | UNUSED(hspi); |
- | 2571 | ||
2083 | /* NOTE : This function should not be modified, when the callback is needed, |
2572 | /* NOTE : This function should not be modified, when the callback is needed, |
2084 | the HAL_SPI_TxHalfCpltCallback should be implemented in the user file |
2573 | the HAL_SPI_TxHalfCpltCallback should be implemented in the user file |
2085 | */ |
2574 | */ |
2086 | } |
2575 | } |
2087 | 2576 | ||
2088 | /** |
2577 | /** |
2089 | * @brief Rx Half Transfer completed callback. |
2578 | * @brief Rx Half Transfer completed callback. |
2090 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
2579 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2091 | * the configuration information for SPI module. |
2580 | * the configuration information for SPI module. |
2092 | * @retval None |
2581 | * @retval None |
2093 | */ |
2582 | */ |
2094 | __weak void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi) |
2583 | __weak void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi) |
2095 | { |
2584 | { |
2096 | /* Prevent unused argument(s) compilation warning */ |
2585 | /* Prevent unused argument(s) compilation warning */ |
2097 | UNUSED(hspi); |
2586 | UNUSED(hspi); |
- | 2587 | ||
2098 | /* NOTE : This function should not be modified, when the callback is needed, |
2588 | /* NOTE : This function should not be modified, when the callback is needed, |
2099 | the HAL_SPI_RxHalfCpltCallback() should be implemented in the user file |
2589 | the HAL_SPI_RxHalfCpltCallback() should be implemented in the user file |
2100 | */ |
2590 | */ |
2101 | } |
2591 | } |
2102 | 2592 | ||
2103 | /** |
2593 | /** |
2104 | * @brief Tx and Rx Half Transfer callback. |
2594 | * @brief Tx and Rx Half Transfer callback. |
2105 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
2595 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2106 | * the configuration information for SPI module. |
2596 | * the configuration information for SPI module. |
2107 | * @retval None |
2597 | * @retval None |
2108 | */ |
2598 | */ |
2109 | __weak void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi) |
2599 | __weak void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi) |
2110 | { |
2600 | { |
2111 | /* Prevent unused argument(s) compilation warning */ |
2601 | /* Prevent unused argument(s) compilation warning */ |
2112 | UNUSED(hspi); |
2602 | UNUSED(hspi); |
- | 2603 | ||
2113 | /* NOTE : This function should not be modified, when the callback is needed, |
2604 | /* NOTE : This function should not be modified, when the callback is needed, |
2114 | the HAL_SPI_TxRxHalfCpltCallback() should be implemented in the user file |
2605 | the HAL_SPI_TxRxHalfCpltCallback() should be implemented in the user file |
2115 | */ |
2606 | */ |
2116 | } |
2607 | } |
2117 | 2608 | ||
2118 | /** |
2609 | /** |
2119 | * @brief SPI error callback. |
2610 | * @brief SPI error callback. |
2120 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
2611 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2121 | * the configuration information for SPI module. |
2612 | * the configuration information for SPI module. |
2122 | * @retval None |
2613 | * @retval None |
2123 | */ |
2614 | */ |
2124 | __weak void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi) |
2615 | __weak void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi) |
2125 | { |
2616 | { |
2126 | /* Prevent unused argument(s) compilation warning */ |
2617 | /* Prevent unused argument(s) compilation warning */ |
2127 | UNUSED(hspi); |
2618 | UNUSED(hspi); |
- | 2619 | ||
2128 | /* NOTE : This function should not be modified, when the callback is needed, |
2620 | /* NOTE : This function should not be modified, when the callback is needed, |
2129 | the HAL_SPI_ErrorCallback should be implemented in the user file |
2621 | the HAL_SPI_ErrorCallback should be implemented in the user file |
2130 | */ |
2622 | */ |
2131 | /* NOTE : The ErrorCode parameter in the hspi handle is updated by the SPI processes |
2623 | /* NOTE : The ErrorCode parameter in the hspi handle is updated by the SPI processes |
2132 | and user can use HAL_SPI_GetError() API to check the latest error occurred |
2624 | and user can use HAL_SPI_GetError() API to check the latest error occurred |
2133 | */ |
2625 | */ |
2134 | } |
2626 | } |
2135 | 2627 | ||
2136 | /** |
2628 | /** |
2137 | * @brief SPI Abort Complete callback. |
2629 | * @brief SPI Abort Complete callback. |
2138 | * @param hspi SPI handle. |
2630 | * @param hspi SPI handle. |
Line 2167... | Line 2659... | ||
2167 | * @{ |
2659 | * @{ |
2168 | */ |
2660 | */ |
2169 | 2661 | ||
2170 | /** |
2662 | /** |
2171 | * @brief Return the SPI handle state. |
2663 | * @brief Return the SPI handle state. |
2172 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
2664 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2173 | * the configuration information for SPI module. |
2665 | * the configuration information for SPI module. |
2174 | * @retval SPI state |
2666 | * @retval SPI state |
2175 | */ |
2667 | */ |
2176 | HAL_SPI_StateTypeDef HAL_SPI_GetState(SPI_HandleTypeDef *hspi) |
2668 | HAL_SPI_StateTypeDef HAL_SPI_GetState(SPI_HandleTypeDef *hspi) |
2177 | { |
2669 | { |
Line 2179... | Line 2671... | ||
2179 | return hspi->State; |
2671 | return hspi->State; |
2180 | } |
2672 | } |
2181 | 2673 | ||
2182 | /** |
2674 | /** |
2183 | * @brief Return the SPI error code. |
2675 | * @brief Return the SPI error code. |
2184 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
2676 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2185 | * the configuration information for SPI module. |
2677 | * the configuration information for SPI module. |
2186 | * @retval SPI error code in bitmap format |
2678 | * @retval SPI error code in bitmap format |
2187 | */ |
2679 | */ |
2188 | uint32_t HAL_SPI_GetError(SPI_HandleTypeDef *hspi) |
2680 | uint32_t HAL_SPI_GetError(SPI_HandleTypeDef *hspi) |
2189 | { |
2681 | { |
Line 2203... | Line 2695... | ||
2203 | * @brief Private functions |
2695 | * @brief Private functions |
2204 | * @{ |
2696 | * @{ |
2205 | */ |
2697 | */ |
2206 | 2698 | ||
2207 | /** |
2699 | /** |
2208 | * @brief DMA SPI transmit process complete callback. |
2700 | * @brief DMA SPI transmit process complete callback. |
2209 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
2701 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
2210 | * the configuration information for the specified DMA module. |
2702 | * the configuration information for the specified DMA module. |
2211 | * @retval None |
2703 | * @retval None |
2212 | */ |
2704 | */ |
2213 | static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma) |
2705 | static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma) |
2214 | { |
2706 | { |
2215 | SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
2707 | SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */ |
2216 | uint32_t tickstart = 0U; |
2708 | uint32_t tickstart; |
2217 | 2709 | ||
2218 | /* Init tickstart for timeout managment*/ |
2710 | /* Init tickstart for timeout management*/ |
2219 | tickstart = HAL_GetTick(); |
2711 | tickstart = HAL_GetTick(); |
2220 | 2712 | ||
2221 | /* DMA Normal Mode */ |
2713 | /* DMA Normal Mode */ |
2222 | if((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC) |
2714 | if ((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC) |
2223 | { |
2715 | { |
- | 2716 | /* Disable ERR interrupt */ |
|
- | 2717 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR); |
|
- | 2718 | ||
2224 | /* Disable Tx DMA Request */ |
2719 | /* Disable Tx DMA Request */ |
2225 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN); |
2720 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN); |
2226 | 2721 | ||
2227 | /* Check the end of the transaction */ |
2722 | /* Check the end of the transaction */ |
2228 | if(SPI_CheckFlag_BSY(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK) |
2723 | if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK) |
2229 | { |
2724 | { |
2230 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
2725 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
2231 | } |
2726 | } |
2232 | 2727 | ||
2233 | /* Clear overrun flag in 2 Lines communication mode because received data is not read */ |
2728 | /* Clear overrun flag in 2 Lines communication mode because received data is not read */ |
2234 | if(hspi->Init.Direction == SPI_DIRECTION_2LINES) |
2729 | if (hspi->Init.Direction == SPI_DIRECTION_2LINES) |
2235 | { |
2730 | { |
2236 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
2731 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
2237 | } |
2732 | } |
2238 | 2733 | ||
2239 | hspi->TxXferCount = 0U; |
2734 | hspi->TxXferCount = 0U; |
2240 | hspi->State = HAL_SPI_STATE_READY; |
2735 | hspi->State = HAL_SPI_STATE_READY; |
2241 | 2736 | ||
2242 | if(hspi->ErrorCode != HAL_SPI_ERROR_NONE) |
2737 | if (hspi->ErrorCode != HAL_SPI_ERROR_NONE) |
2243 | { |
2738 | { |
- | 2739 | /* Call user error callback */ |
|
- | 2740 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 2741 | hspi->ErrorCallback(hspi); |
|
- | 2742 | #else |
|
2244 | HAL_SPI_ErrorCallback(hspi); |
2743 | HAL_SPI_ErrorCallback(hspi); |
- | 2744 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
2245 | return; |
2745 | return; |
2246 | } |
2746 | } |
2247 | } |
2747 | } |
- | 2748 | /* Call user Tx complete callback */ |
|
- | 2749 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 2750 | hspi->TxCpltCallback(hspi); |
|
- | 2751 | #else |
|
2248 | HAL_SPI_TxCpltCallback(hspi); |
2752 | HAL_SPI_TxCpltCallback(hspi); |
- | 2753 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
2249 | } |
2754 | } |
2250 | 2755 | ||
2251 | /** |
2756 | /** |
2252 | * @brief DMA SPI receive process complete callback. |
2757 | * @brief DMA SPI receive process complete callback. |
2253 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
2758 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
2254 | * the configuration information for the specified DMA module. |
2759 | * the configuration information for the specified DMA module. |
2255 | * @retval None |
2760 | * @retval None |
2256 | */ |
2761 | */ |
2257 | static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma) |
2762 | static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma) |
2258 | { |
2763 | { |
2259 | SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
2764 | SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */ |
2260 | #if (USE_SPI_CRC != 0U) |
- | |
2261 | uint32_t tickstart = 0U; |
2765 | uint32_t tickstart; |
2262 | __IO uint16_t tmpreg = 0U; |
- | |
2263 | 2766 | ||
2264 | /* Init tickstart for timeout management*/ |
2767 | /* Init tickstart for timeout management*/ |
2265 | tickstart = HAL_GetTick(); |
2768 | tickstart = HAL_GetTick(); |
2266 | #endif /* USE_SPI_CRC */ |
- | |
2267 | 2769 | ||
- | 2770 | /* DMA Normal Mode */ |
|
2268 | if((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC) |
2771 | if ((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC) |
2269 | { |
2772 | { |
- | 2773 | /* Disable ERR interrupt */ |
|
- | 2774 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR); |
|
- | 2775 | ||
2270 | #if (USE_SPI_CRC != 0U) |
2776 | #if (USE_SPI_CRC != 0U) |
2271 | /* CRC handling */ |
2777 | /* CRC handling */ |
2272 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
2778 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
2273 | { |
2779 | { |
2274 | /* Wait until RXNE flag */ |
2780 | /* Wait until RXNE flag */ |
2275 | if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK) |
2781 | if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK) |
2276 | { |
2782 | { |
2277 | /* Error on the CRC reception */ |
2783 | /* Error on the CRC reception */ |
2278 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
2784 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
2279 | } |
2785 | } |
2280 | /* Read CRC */ |
2786 | /* Read CRC */ |
2281 | tmpreg = hspi->Instance->DR; |
2787 | READ_REG(hspi->Instance->DR); |
2282 | /* To avoid GCC warning */ |
- | |
2283 | UNUSED(tmpreg); |
- | |
2284 | } |
2788 | } |
2285 | #endif /* USE_SPI_CRC */ |
2789 | #endif /* USE_SPI_CRC */ |
2286 | 2790 | ||
- | 2791 | /* Check if we are in Master RX 2 line mode */ |
|
- | 2792 | if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER)) |
|
- | 2793 | { |
|
2287 | /* Disable Rx/Tx DMA Request (done by default to handle the case master rx direction 2 lines) */ |
2794 | /* Disable Rx/Tx DMA Request (done by default to handle the case master rx direction 2 lines) */ |
2288 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN); |
2795 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN); |
- | 2796 | } |
|
- | 2797 | else |
|
- | 2798 | { |
|
- | 2799 | /* Normal case */ |
|
- | 2800 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN); |
|
- | 2801 | } |
|
2289 | 2802 | ||
2290 | /* Check the end of the transaction */ |
2803 | /* Check the end of the transaction */ |
2291 | if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY))) |
2804 | if (SPI_EndRxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK) |
2292 | { |
2805 | { |
2293 | /* Disable SPI peripheral */ |
- | |
2294 | __HAL_SPI_DISABLE(hspi); |
2806 | hspi->ErrorCode = HAL_SPI_ERROR_FLAG; |
2295 | } |
2807 | } |
2296 | 2808 | ||
2297 | hspi->RxXferCount = 0U; |
2809 | hspi->RxXferCount = 0U; |
2298 | hspi->State = HAL_SPI_STATE_READY; |
2810 | hspi->State = HAL_SPI_STATE_READY; |
2299 | 2811 | ||
2300 | #if (USE_SPI_CRC != 0U) |
2812 | #if (USE_SPI_CRC != 0U) |
2301 | /* Check if CRC error occurred */ |
2813 | /* Check if CRC error occurred */ |
2302 | if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET) |
2814 | if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET) |
2303 | { |
2815 | { |
2304 | /* Check if CRC error is valid or not (workaround to be applied or not) */ |
2816 | /* Check if CRC error is valid or not (workaround to be applied or not) */ |
2305 | if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR) |
2817 | if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR) |
2306 | { |
2818 | { |
2307 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
2819 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
2308 | 2820 | ||
2309 | /* Reset CRC Calculation */ |
2821 | /* Reset CRC Calculation */ |
2310 | SPI_RESET_CRC(hspi); |
2822 | SPI_RESET_CRC(hspi); |
2311 | } |
2823 | } |
2312 | else |
2824 | else |
2313 | { |
2825 | { |
2314 | __HAL_SPI_CLEAR_CRCERRFLAG(hspi); |
2826 | __HAL_SPI_CLEAR_CRCERRFLAG(hspi); |
2315 | } |
2827 | } |
2316 | } |
2828 | } |
2317 | #endif /* USE_SPI_CRC */ |
2829 | #endif /* USE_SPI_CRC */ |
2318 | 2830 | ||
2319 | if(hspi->ErrorCode != HAL_SPI_ERROR_NONE) |
2831 | if (hspi->ErrorCode != HAL_SPI_ERROR_NONE) |
2320 | { |
2832 | { |
- | 2833 | /* Call user error callback */ |
|
- | 2834 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 2835 | hspi->ErrorCallback(hspi); |
|
- | 2836 | #else |
|
2321 | HAL_SPI_ErrorCallback(hspi); |
2837 | HAL_SPI_ErrorCallback(hspi); |
- | 2838 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
2322 | return; |
2839 | return; |
2323 | } |
2840 | } |
2324 | } |
2841 | } |
- | 2842 | /* Call user Rx complete callback */ |
|
- | 2843 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 2844 | hspi->RxCpltCallback(hspi); |
|
- | 2845 | #else |
|
2325 | HAL_SPI_RxCpltCallback(hspi); |
2846 | HAL_SPI_RxCpltCallback(hspi); |
- | 2847 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
2326 | } |
2848 | } |
2327 | 2849 | ||
2328 | /** |
2850 | /** |
2329 | * @brief DMA SPI transmit receive process complete callback. |
2851 | * @brief DMA SPI transmit receive process complete callback. |
2330 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
2852 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
2331 | * the configuration information for the specified DMA module. |
2853 | * the configuration information for the specified DMA module. |
2332 | * @retval None |
2854 | * @retval None |
2333 | */ |
2855 | */ |
2334 | static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma) |
2856 | static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma) |
2335 | { |
2857 | { |
2336 | SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
2858 | SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */ |
2337 | uint32_t tickstart = 0U; |
2859 | uint32_t tickstart; |
2338 | #if (USE_SPI_CRC != 0U) |
- | |
2339 | __IO int16_t tmpreg = 0U; |
- | |
2340 | #endif /* USE_SPI_CRC */ |
- | |
- | 2860 | ||
2341 | /* Init tickstart for timeout management*/ |
2861 | /* Init tickstart for timeout management*/ |
2342 | tickstart = HAL_GetTick(); |
2862 | tickstart = HAL_GetTick(); |
2343 | 2863 | ||
- | 2864 | /* DMA Normal Mode */ |
|
2344 | if((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC) |
2865 | if ((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC) |
2345 | { |
2866 | { |
- | 2867 | /* Disable ERR interrupt */ |
|
- | 2868 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR); |
|
- | 2869 | ||
2346 | #if (USE_SPI_CRC != 0U) |
2870 | #if (USE_SPI_CRC != 0U) |
2347 | /* CRC handling */ |
2871 | /* CRC handling */ |
2348 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
2872 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
2349 | { |
2873 | { |
2350 | /* Wait the CRC data */ |
2874 | /* Wait the CRC data */ |
2351 | if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK) |
2875 | if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK) |
2352 | { |
2876 | { |
2353 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
2877 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
2354 | } |
2878 | } |
2355 | /* Read CRC to Flush DR and RXNE flag */ |
2879 | /* Read CRC to Flush DR and RXNE flag */ |
2356 | tmpreg = hspi->Instance->DR; |
2880 | READ_REG(hspi->Instance->DR); |
2357 | /* To avoid GCC warning */ |
- | |
2358 | UNUSED(tmpreg); |
- | |
2359 | } |
2881 | } |
2360 | #endif /* USE_SPI_CRC */ |
2882 | #endif /* USE_SPI_CRC */ |
- | 2883 | ||
2361 | /* Check the end of the transaction */ |
2884 | /* Check the end of the transaction */ |
2362 | if(SPI_CheckFlag_BSY(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK) |
2885 | if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK) |
2363 | { |
2886 | { |
2364 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
2887 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
2365 | } |
2888 | } |
2366 | 2889 | ||
2367 | /* Disable Rx/Tx DMA Request */ |
2890 | /* Disable Rx/Tx DMA Request */ |
Line 2371... | Line 2894... | ||
2371 | hspi->RxXferCount = 0U; |
2894 | hspi->RxXferCount = 0U; |
2372 | hspi->State = HAL_SPI_STATE_READY; |
2895 | hspi->State = HAL_SPI_STATE_READY; |
2373 | 2896 | ||
2374 | #if (USE_SPI_CRC != 0U) |
2897 | #if (USE_SPI_CRC != 0U) |
2375 | /* Check if CRC error occurred */ |
2898 | /* Check if CRC error occurred */ |
2376 | if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET) |
2899 | if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET) |
2377 | { |
2900 | { |
2378 | /* Check if CRC error is valid or not (workaround to be applied or not) */ |
2901 | /* Check if CRC error is valid or not (workaround to be applied or not) */ |
2379 | if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR) |
2902 | if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR) |
2380 | { |
2903 | { |
2381 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
2904 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
2382 | 2905 | ||
2383 | /* Reset CRC Calculation */ |
2906 | /* Reset CRC Calculation */ |
2384 | SPI_RESET_CRC(hspi); |
2907 | SPI_RESET_CRC(hspi); |
2385 | } |
2908 | } |
2386 | else |
2909 | else |
2387 | { |
2910 | { |
2388 | __HAL_SPI_CLEAR_CRCERRFLAG(hspi); |
2911 | __HAL_SPI_CLEAR_CRCERRFLAG(hspi); |
2389 | } |
2912 | } |
2390 | } |
2913 | } |
2391 | #endif /* USE_SPI_CRC */ |
2914 | #endif /* USE_SPI_CRC */ |
2392 | 2915 | ||
2393 | if(hspi->ErrorCode != HAL_SPI_ERROR_NONE) |
2916 | if (hspi->ErrorCode != HAL_SPI_ERROR_NONE) |
2394 | { |
2917 | { |
- | 2918 | /* Call user error callback */ |
|
- | 2919 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 2920 | hspi->ErrorCallback(hspi); |
|
- | 2921 | #else |
|
2395 | HAL_SPI_ErrorCallback(hspi); |
2922 | HAL_SPI_ErrorCallback(hspi); |
- | 2923 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
2396 | return; |
2924 | return; |
2397 | } |
2925 | } |
2398 | } |
2926 | } |
- | 2927 | /* Call user TxRx complete callback */ |
|
- | 2928 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 2929 | hspi->TxRxCpltCallback(hspi); |
|
- | 2930 | #else |
|
2399 | HAL_SPI_TxRxCpltCallback(hspi); |
2931 | HAL_SPI_TxRxCpltCallback(hspi); |
- | 2932 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
2400 | } |
2933 | } |
2401 | 2934 | ||
2402 | /** |
2935 | /** |
2403 | * @brief DMA SPI half transmit process complete callback. |
2936 | * @brief DMA SPI half transmit process complete callback. |
2404 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
2937 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
2405 | * the configuration information for the specified DMA module. |
2938 | * the configuration information for the specified DMA module. |
2406 | * @retval None |
2939 | * @retval None |
2407 | */ |
2940 | */ |
2408 | static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma) |
2941 | static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma) |
2409 | { |
2942 | { |
2410 | SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
2943 | SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */ |
2411 | 2944 | ||
- | 2945 | /* Call user Tx half complete callback */ |
|
- | 2946 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 2947 | hspi->TxHalfCpltCallback(hspi); |
|
- | 2948 | #else |
|
2412 | HAL_SPI_TxHalfCpltCallback(hspi); |
2949 | HAL_SPI_TxHalfCpltCallback(hspi); |
- | 2950 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
2413 | } |
2951 | } |
2414 | 2952 | ||
2415 | /** |
2953 | /** |
2416 | * @brief DMA SPI half receive process complete callback |
2954 | * @brief DMA SPI half receive process complete callback |
2417 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
2955 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
2418 | * the configuration information for the specified DMA module. |
2956 | * the configuration information for the specified DMA module. |
2419 | * @retval None |
2957 | * @retval None |
2420 | */ |
2958 | */ |
2421 | static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma) |
2959 | static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma) |
2422 | { |
2960 | { |
2423 | SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
2961 | SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */ |
2424 | 2962 | ||
- | 2963 | /* Call user Rx half complete callback */ |
|
- | 2964 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 2965 | hspi->RxHalfCpltCallback(hspi); |
|
- | 2966 | #else |
|
2425 | HAL_SPI_RxHalfCpltCallback(hspi); |
2967 | HAL_SPI_RxHalfCpltCallback(hspi); |
- | 2968 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
2426 | } |
2969 | } |
2427 | 2970 | ||
2428 | /** |
2971 | /** |
2429 | * @brief DMA SPI half transmit receive process complete callback. |
2972 | * @brief DMA SPI half transmit receive process complete callback. |
2430 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
2973 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
2431 | * the configuration information for the specified DMA module. |
2974 | * the configuration information for the specified DMA module. |
2432 | * @retval None |
2975 | * @retval None |
2433 | */ |
2976 | */ |
2434 | static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma) |
2977 | static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma) |
2435 | { |
2978 | { |
2436 | SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
2979 | SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */ |
2437 | 2980 | ||
- | 2981 | /* Call user TxRx half complete callback */ |
|
- | 2982 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 2983 | hspi->TxRxHalfCpltCallback(hspi); |
|
- | 2984 | #else |
|
2438 | HAL_SPI_TxRxHalfCpltCallback(hspi); |
2985 | HAL_SPI_TxRxHalfCpltCallback(hspi); |
- | 2986 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
2439 | } |
2987 | } |
2440 | 2988 | ||
2441 | /** |
2989 | /** |
2442 | * @brief DMA SPI communication error callback. |
2990 | * @brief DMA SPI communication error callback. |
2443 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
2991 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
2444 | * the configuration information for the specified DMA module. |
2992 | * the configuration information for the specified DMA module. |
2445 | * @retval None |
2993 | * @retval None |
2446 | */ |
2994 | */ |
2447 | static void SPI_DMAError(DMA_HandleTypeDef *hdma) |
2995 | static void SPI_DMAError(DMA_HandleTypeDef *hdma) |
2448 | { |
2996 | { |
2449 | SPI_HandleTypeDef* hspi = (SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
2997 | SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */ |
2450 | 2998 | ||
2451 | /* Stop the disable DMA transfer on SPI side */ |
2999 | /* Stop the disable DMA transfer on SPI side */ |
2452 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN); |
3000 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN); |
2453 | 3001 | ||
2454 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA); |
3002 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA); |
2455 | hspi->State = HAL_SPI_STATE_READY; |
3003 | hspi->State = HAL_SPI_STATE_READY; |
- | 3004 | /* Call user error callback */ |
|
- | 3005 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 3006 | hspi->ErrorCallback(hspi); |
|
- | 3007 | #else |
|
2456 | HAL_SPI_ErrorCallback(hspi); |
3008 | HAL_SPI_ErrorCallback(hspi); |
- | 3009 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
2457 | } |
3010 | } |
2458 | 3011 | ||
2459 | /** |
3012 | /** |
2460 | * @brief DMA SPI communication abort callback, when initiated by HAL services on Error |
3013 | * @brief DMA SPI communication abort callback, when initiated by HAL services on Error |
2461 | * (To be called at end of DMA Abort procedure following error occurrence). |
3014 | * (To be called at end of DMA Abort procedure following error occurrence). |
2462 | * @param hdma DMA handle. |
3015 | * @param hdma DMA handle. |
2463 | * @retval None |
3016 | * @retval None |
2464 | */ |
3017 | */ |
2465 | static void SPI_DMAAbortOnError(DMA_HandleTypeDef *hdma) |
3018 | static void SPI_DMAAbortOnError(DMA_HandleTypeDef *hdma) |
2466 | { |
3019 | { |
2467 | SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
3020 | SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */ |
2468 | hspi->RxXferCount = 0U; |
3021 | hspi->RxXferCount = 0U; |
2469 | hspi->TxXferCount = 0U; |
3022 | hspi->TxXferCount = 0U; |
2470 | 3023 | ||
- | 3024 | /* Call user error callback */ |
|
- | 3025 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 3026 | hspi->ErrorCallback(hspi); |
|
- | 3027 | #else |
|
2471 | HAL_SPI_ErrorCallback(hspi); |
3028 | HAL_SPI_ErrorCallback(hspi); |
- | 3029 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
2472 | } |
3030 | } |
2473 | 3031 | ||
2474 | /** |
3032 | /** |
2475 | * @brief DMA SPI Tx communication abort callback, when initiated by user |
3033 | * @brief DMA SPI Tx communication abort callback, when initiated by user |
2476 | * (To be called at end of DMA Tx Abort procedure following user abort request). |
3034 | * (To be called at end of DMA Tx Abort procedure following user abort request). |
Line 2479... | Line 3037... | ||
2479 | * @param hdma DMA handle. |
3037 | * @param hdma DMA handle. |
2480 | * @retval None |
3038 | * @retval None |
2481 | */ |
3039 | */ |
2482 | static void SPI_DMATxAbortCallback(DMA_HandleTypeDef *hdma) |
3040 | static void SPI_DMATxAbortCallback(DMA_HandleTypeDef *hdma) |
2483 | { |
3041 | { |
2484 | __IO uint32_t count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U); |
- | |
2485 | SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
3042 | SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */ |
- | 3043 | __IO uint32_t count; |
|
2486 | 3044 | ||
2487 | hspi->hdmatx->XferAbortCallback = NULL; |
3045 | hspi->hdmatx->XferAbortCallback = NULL; |
- | 3046 | count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U); |
|
2488 | 3047 | ||
2489 | /* Disable Tx DMA Request */ |
3048 | /* Disable Tx DMA Request */ |
2490 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN ); |
3049 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN); |
2491 | 3050 | ||
2492 | /* Wait until TXE flag is set */ |
3051 | /* Wait until TXE flag is set */ |
2493 | do |
3052 | do |
2494 | { |
3053 | { |
2495 | if(count-- == 0U) |
3054 | if (count == 0U) |
2496 | { |
3055 | { |
2497 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
3056 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT); |
2498 | break; |
3057 | break; |
2499 | } |
3058 | } |
2500 | } |
3059 | count--; |
2501 | while((hspi->Instance->SR & SPI_FLAG_TXE) == RESET); |
3060 | } while ((hspi->Instance->SR & SPI_FLAG_TXE) == RESET); |
2502 | 3061 | ||
2503 | /* Check if an Abort process is still ongoing */ |
3062 | /* Check if an Abort process is still ongoing */ |
2504 | if(hspi->hdmarx != NULL) |
3063 | if (hspi->hdmarx != NULL) |
2505 | { |
3064 | { |
2506 | if(hspi->hdmarx->XferAbortCallback != NULL) |
3065 | if (hspi->hdmarx->XferAbortCallback != NULL) |
2507 | { |
3066 | { |
2508 | return; |
3067 | return; |
2509 | } |
3068 | } |
2510 | } |
3069 | } |
2511 | 3070 | ||
2512 | /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ |
3071 | /* No Abort process still ongoing : All DMA Stream/Channel are aborted, call user Abort Complete callback */ |
2513 | hspi->RxXferCount = 0U; |
3072 | hspi->RxXferCount = 0U; |
2514 | hspi->TxXferCount = 0U; |
3073 | hspi->TxXferCount = 0U; |
2515 | 3074 | ||
- | 3075 | /* Check no error during Abort procedure */ |
|
- | 3076 | if (hspi->ErrorCode != HAL_SPI_ERROR_ABORT) |
|
- | 3077 | { |
|
2516 | /* Reset errorCode */ |
3078 | /* Reset errorCode */ |
2517 | hspi->ErrorCode = HAL_SPI_ERROR_NONE; |
3079 | hspi->ErrorCode = HAL_SPI_ERROR_NONE; |
- | 3080 | } |
|
- | 3081 | ||
- | 3082 | /* Clear the Error flags in the SR register */ |
|
- | 3083 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
|
2518 | 3084 | ||
2519 | /* Restore hspi->State to Ready */ |
3085 | /* Restore hspi->State to Ready */ |
2520 | hspi->State = HAL_SPI_STATE_READY; |
3086 | hspi->State = HAL_SPI_STATE_READY; |
2521 | 3087 | ||
2522 | /* Call user Abort complete callback */ |
3088 | /* Call user Abort complete callback */ |
- | 3089 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 3090 | hspi->AbortCpltCallback(hspi); |
|
- | 3091 | #else |
|
2523 | HAL_SPI_AbortCpltCallback(hspi); |
3092 | HAL_SPI_AbortCpltCallback(hspi); |
- | 3093 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
2524 | } |
3094 | } |
2525 | 3095 | ||
2526 | /** |
3096 | /** |
2527 | * @brief DMA SPI Rx communication abort callback, when initiated by user |
3097 | * @brief DMA SPI Rx communication abort callback, when initiated by user |
2528 | * (To be called at end of DMA Rx Abort procedure following user abort request). |
3098 | * (To be called at end of DMA Rx Abort procedure following user abort request). |
Line 2531... | Line 3101... | ||
2531 | * @param hdma DMA handle. |
3101 | * @param hdma DMA handle. |
2532 | * @retval None |
3102 | * @retval None |
2533 | */ |
3103 | */ |
2534 | static void SPI_DMARxAbortCallback(DMA_HandleTypeDef *hdma) |
3104 | static void SPI_DMARxAbortCallback(DMA_HandleTypeDef *hdma) |
2535 | { |
3105 | { |
2536 | SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
3106 | SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */ |
2537 | 3107 | ||
2538 | /* Disable SPI Peripheral */ |
3108 | /* Disable SPI Peripheral */ |
2539 | __HAL_SPI_DISABLE(hspi); |
3109 | __HAL_SPI_DISABLE(hspi); |
2540 | 3110 | ||
2541 | hspi->hdmarx->XferAbortCallback = NULL; |
3111 | hspi->hdmarx->XferAbortCallback = NULL; |
2542 | 3112 | ||
2543 | /* Disable Rx DMA Request */ |
3113 | /* Disable Rx DMA Request */ |
2544 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN); |
3114 | CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN); |
2545 | 3115 | ||
- | 3116 | /* Check Busy flag */ |
|
- | 3117 | if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK) |
|
- | 3118 | { |
|
- | 3119 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT); |
|
- | 3120 | } |
|
- | 3121 | ||
2546 | /* Check if an Abort process is still ongoing */ |
3122 | /* Check if an Abort process is still ongoing */ |
2547 | if(hspi->hdmatx != NULL) |
3123 | if (hspi->hdmatx != NULL) |
2548 | { |
3124 | { |
2549 | if(hspi->hdmatx->XferAbortCallback != NULL) |
3125 | if (hspi->hdmatx->XferAbortCallback != NULL) |
2550 | { |
3126 | { |
2551 | return; |
3127 | return; |
2552 | } |
3128 | } |
2553 | } |
3129 | } |
2554 | 3130 | ||
2555 | /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ |
3131 | /* No Abort process still ongoing : All DMA Stream/Channel are aborted, call user Abort Complete callback */ |
2556 | hspi->RxXferCount = 0U; |
3132 | hspi->RxXferCount = 0U; |
2557 | hspi->TxXferCount = 0U; |
3133 | hspi->TxXferCount = 0U; |
2558 | 3134 | ||
- | 3135 | /* Check no error during Abort procedure */ |
|
- | 3136 | if (hspi->ErrorCode != HAL_SPI_ERROR_ABORT) |
|
- | 3137 | { |
|
2559 | /* Reset errorCode */ |
3138 | /* Reset errorCode */ |
2560 | hspi->ErrorCode = HAL_SPI_ERROR_NONE; |
3139 | hspi->ErrorCode = HAL_SPI_ERROR_NONE; |
- | 3140 | } |
|
2561 | 3141 | ||
2562 | /* Clear the Error flags in the SR register */ |
3142 | /* Clear the Error flags in the SR register */ |
2563 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
3143 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
2564 | 3144 | ||
2565 | /* Restore hspi->State to Ready */ |
3145 | /* Restore hspi->State to Ready */ |
2566 | hspi->State = HAL_SPI_STATE_READY; |
3146 | hspi->State = HAL_SPI_STATE_READY; |
2567 | 3147 | ||
2568 | /* Call user Abort complete callback */ |
3148 | /* Call user Abort complete callback */ |
- | 3149 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 3150 | hspi->AbortCpltCallback(hspi); |
|
- | 3151 | #else |
|
2569 | HAL_SPI_AbortCpltCallback(hspi); |
3152 | HAL_SPI_AbortCpltCallback(hspi); |
- | 3153 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
2570 | } |
3154 | } |
2571 | 3155 | ||
2572 | /** |
3156 | /** |
2573 | * @brief Rx 8-bit handler for Transmit and Receive in Interrupt mode. |
3157 | * @brief Rx 8-bit handler for Transmit and Receive in Interrupt mode. |
2574 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3158 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2575 | * the configuration information for SPI module. |
3159 | * the configuration information for SPI module. |
2576 | * @retval None |
3160 | * @retval None |
2577 | */ |
3161 | */ |
2578 | static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi) |
3162 | static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi) |
2579 | { |
3163 | { |
2580 | /* Receive data in 8bit mode */ |
3164 | /* Receive data in 8bit mode */ |
2581 | *hspi->pRxBuffPtr++ = *((__IO uint8_t *)&hspi->Instance->DR); |
3165 | *hspi->pRxBuffPtr = *((__IO uint8_t *)&hspi->Instance->DR); |
- | 3166 | hspi->pRxBuffPtr++; |
|
2582 | hspi->RxXferCount--; |
3167 | hspi->RxXferCount--; |
2583 | 3168 | ||
2584 | /* check end of the reception */ |
3169 | /* Check end of the reception */ |
2585 | if(hspi->RxXferCount == 0U) |
3170 | if (hspi->RxXferCount == 0U) |
2586 | { |
3171 | { |
2587 | #if (USE_SPI_CRC != 0U) |
3172 | #if (USE_SPI_CRC != 0U) |
2588 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
3173 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
2589 | { |
3174 | { |
2590 | hspi->RxISR = SPI_2linesRxISR_8BITCRC; |
3175 | hspi->RxISR = SPI_2linesRxISR_8BITCRC; |
2591 | return; |
3176 | return; |
2592 | } |
3177 | } |
2593 | #endif /* USE_SPI_CRC */ |
3178 | #endif /* USE_SPI_CRC */ |
2594 | 3179 | ||
2595 | /* Disable RXNE interrupt */ |
3180 | /* Disable RXNE and ERR interrupt */ |
2596 | __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR)); |
3181 | __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR)); |
2597 | 3182 | ||
2598 | if(hspi->TxXferCount == 0U) |
3183 | if (hspi->TxXferCount == 0U) |
2599 | { |
3184 | { |
2600 | SPI_CloseRxTx_ISR(hspi); |
3185 | SPI_CloseRxTx_ISR(hspi); |
2601 | } |
3186 | } |
2602 | } |
3187 | } |
2603 | } |
3188 | } |
2604 | 3189 | ||
2605 | #if (USE_SPI_CRC != 0U) |
3190 | #if (USE_SPI_CRC != 0U) |
2606 | /** |
3191 | /** |
2607 | * @brief Rx 8-bit handler for Transmit and Receive in Interrupt mode. |
3192 | * @brief Rx 8-bit handler for Transmit and Receive in Interrupt mode. |
2608 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3193 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2609 | * the configuration information for SPI module. |
3194 | * the configuration information for SPI module. |
2610 | * @retval None |
3195 | * @retval None |
2611 | */ |
3196 | */ |
2612 | static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi) |
3197 | static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi) |
2613 | { |
3198 | { |
- | 3199 | /* Read 8bit CRC to flush Data Register */ |
|
2614 | __IO uint8_t tmpreg = 0U; |
3200 | READ_REG(*(__IO uint8_t *)&hspi->Instance->DR); |
2615 | 3201 | ||
2616 | /* Read data register to flush CRC */ |
- | |
2617 | tmpreg = *((__IO uint8_t *)&hspi->Instance->DR); |
- | |
2618 | - | ||
2619 | /* To avoid GCC warning */ |
- | |
2620 | - | ||
2621 | UNUSED(tmpreg); |
- | |
2622 | - | ||
2623 | /* Disable RXNE interrupt */ |
3202 | /* Disable RXNE and ERR interrupt */ |
2624 | __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR)); |
3203 | __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR)); |
2625 | 3204 | ||
2626 | if(hspi->TxXferCount == 0U) |
3205 | if (hspi->TxXferCount == 0U) |
2627 | { |
3206 | { |
2628 | SPI_CloseRxTx_ISR(hspi); |
3207 | SPI_CloseRxTx_ISR(hspi); |
2629 | } |
3208 | } |
2630 | } |
3209 | } |
2631 | #endif /* USE_SPI_CRC */ |
3210 | #endif /* USE_SPI_CRC */ |
2632 | 3211 | ||
2633 | /** |
3212 | /** |
2634 | * @brief Tx 8-bit handler for Transmit and Receive in Interrupt mode. |
3213 | * @brief Tx 8-bit handler for Transmit and Receive in Interrupt mode. |
2635 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3214 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2636 | * the configuration information for SPI module. |
3215 | * the configuration information for SPI module. |
2637 | * @retval None |
3216 | * @retval None |
2638 | */ |
3217 | */ |
2639 | static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi) |
3218 | static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi) |
2640 | { |
3219 | { |
2641 | *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr++); |
3220 | *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr); |
- | 3221 | hspi->pTxBuffPtr++; |
|
2642 | hspi->TxXferCount--; |
3222 | hspi->TxXferCount--; |
2643 | 3223 | ||
2644 | /* check the end of the transmission */ |
3224 | /* Check the end of the transmission */ |
2645 | if(hspi->TxXferCount == 0U) |
3225 | if (hspi->TxXferCount == 0U) |
2646 | { |
3226 | { |
2647 | #if (USE_SPI_CRC != 0U) |
3227 | #if (USE_SPI_CRC != 0U) |
2648 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
3228 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
2649 | { |
3229 | { |
- | 3230 | /* Set CRC Next Bit to send CRC */ |
|
2650 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
3231 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
- | 3232 | /* Disable TXE interrupt */ |
|
2651 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE); |
3233 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE); |
2652 | return; |
3234 | return; |
2653 | } |
3235 | } |
2654 | #endif /* USE_SPI_CRC */ |
3236 | #endif /* USE_SPI_CRC */ |
2655 | 3237 | ||
2656 | /* Disable TXE interrupt */ |
3238 | /* Disable TXE interrupt */ |
2657 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE); |
3239 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE); |
2658 | 3240 | ||
2659 | if(hspi->RxXferCount == 0U) |
3241 | if (hspi->RxXferCount == 0U) |
2660 | { |
3242 | { |
2661 | SPI_CloseRxTx_ISR(hspi); |
3243 | SPI_CloseRxTx_ISR(hspi); |
2662 | } |
3244 | } |
2663 | } |
3245 | } |
2664 | } |
3246 | } |
2665 | 3247 | ||
2666 | /** |
3248 | /** |
2667 | * @brief Rx 16-bit handler for Transmit and Receive in Interrupt mode. |
3249 | * @brief Rx 16-bit handler for Transmit and Receive in Interrupt mode. |
2668 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3250 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2669 | * the configuration information for SPI module. |
3251 | * the configuration information for SPI module. |
2670 | * @retval None |
3252 | * @retval None |
2671 | */ |
3253 | */ |
2672 | static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi) |
3254 | static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi) |
2673 | { |
3255 | { |
2674 | /* Receive data in 16 Bit mode */ |
3256 | /* Receive data in 16 Bit mode */ |
2675 | *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR; |
3257 | *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)(hspi->Instance->DR); |
2676 | hspi->pRxBuffPtr += sizeof(uint16_t); |
3258 | hspi->pRxBuffPtr += sizeof(uint16_t); |
2677 | hspi->RxXferCount--; |
3259 | hspi->RxXferCount--; |
2678 | 3260 | ||
2679 | if(hspi->RxXferCount == 0U) |
3261 | if (hspi->RxXferCount == 0U) |
2680 | { |
3262 | { |
2681 | #if (USE_SPI_CRC != 0U) |
3263 | #if (USE_SPI_CRC != 0U) |
2682 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
3264 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
2683 | { |
3265 | { |
2684 | hspi->RxISR = SPI_2linesRxISR_16BITCRC; |
3266 | hspi->RxISR = SPI_2linesRxISR_16BITCRC; |
2685 | return; |
3267 | return; |
2686 | } |
3268 | } |
2687 | #endif /* USE_SPI_CRC */ |
3269 | #endif /* USE_SPI_CRC */ |
2688 | 3270 | ||
2689 | /* Disable RXNE interrupt */ |
3271 | /* Disable RXNE interrupt */ |
2690 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE); |
3272 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE); |
2691 | 3273 | ||
2692 | if(hspi->TxXferCount == 0U) |
3274 | if (hspi->TxXferCount == 0U) |
2693 | { |
3275 | { |
2694 | SPI_CloseRxTx_ISR(hspi); |
3276 | SPI_CloseRxTx_ISR(hspi); |
2695 | } |
3277 | } |
2696 | } |
3278 | } |
2697 | } |
3279 | } |
2698 | 3280 | ||
2699 | #if (USE_SPI_CRC != 0U) |
3281 | #if (USE_SPI_CRC != 0U) |
2700 | /** |
3282 | /** |
2701 | * @brief Manage the CRC 16-bit receive for Transmit and Receive in Interrupt mode. |
3283 | * @brief Manage the CRC 16-bit receive for Transmit and Receive in Interrupt mode. |
2702 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3284 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2703 | * the configuration information for SPI module. |
3285 | * the configuration information for SPI module. |
2704 | * @retval None |
3286 | * @retval None |
2705 | */ |
3287 | */ |
2706 | static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi) |
3288 | static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi) |
2707 | { |
3289 | { |
2708 | /* Receive data in 16 Bit mode */ |
- | |
2709 | __IO uint16_t tmpreg = 0U; |
- | |
2710 | - | ||
2711 | /* Read data register to flush CRC */ |
3290 | /* Read 16bit CRC to flush Data Register */ |
2712 | tmpreg = hspi->Instance->DR; |
3291 | READ_REG(hspi->Instance->DR); |
2713 | - | ||
2714 | /* To avoid GCC warning */ |
- | |
2715 | UNUSED(tmpreg); |
- | |
2716 | 3292 | ||
2717 | /* Disable RXNE interrupt */ |
3293 | /* Disable RXNE interrupt */ |
2718 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE); |
3294 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE); |
2719 | 3295 | ||
2720 | SPI_CloseRxTx_ISR(hspi); |
3296 | SPI_CloseRxTx_ISR(hspi); |
2721 | } |
3297 | } |
2722 | #endif /* USE_SPI_CRC */ |
3298 | #endif /* USE_SPI_CRC */ |
2723 | 3299 | ||
2724 | /** |
3300 | /** |
2725 | * @brief Tx 16-bit handler for Transmit and Receive in Interrupt mode. |
3301 | * @brief Tx 16-bit handler for Transmit and Receive in Interrupt mode. |
2726 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3302 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2727 | * the configuration information for SPI module. |
3303 | * the configuration information for SPI module. |
2728 | * @retval None |
3304 | * @retval None |
2729 | */ |
3305 | */ |
2730 | static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi) |
3306 | static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi) |
2731 | { |
3307 | { |
Line 2733... | Line 3309... | ||
2733 | hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr); |
3309 | hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr); |
2734 | hspi->pTxBuffPtr += sizeof(uint16_t); |
3310 | hspi->pTxBuffPtr += sizeof(uint16_t); |
2735 | hspi->TxXferCount--; |
3311 | hspi->TxXferCount--; |
2736 | 3312 | ||
2737 | /* Enable CRC Transmission */ |
3313 | /* Enable CRC Transmission */ |
2738 | if(hspi->TxXferCount == 0U) |
3314 | if (hspi->TxXferCount == 0U) |
2739 | { |
3315 | { |
2740 | #if (USE_SPI_CRC != 0U) |
3316 | #if (USE_SPI_CRC != 0U) |
2741 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
3317 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
2742 | { |
3318 | { |
- | 3319 | /* Set CRC Next Bit to send CRC */ |
|
2743 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
3320 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
- | 3321 | /* Disable TXE interrupt */ |
|
2744 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE); |
3322 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE); |
2745 | return; |
3323 | return; |
2746 | } |
3324 | } |
2747 | #endif /* USE_SPI_CRC */ |
3325 | #endif /* USE_SPI_CRC */ |
2748 | 3326 | ||
2749 | /* Disable TXE interrupt */ |
3327 | /* Disable TXE interrupt */ |
2750 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE); |
3328 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE); |
2751 | 3329 | ||
2752 | if(hspi->RxXferCount == 0U) |
3330 | if (hspi->RxXferCount == 0U) |
2753 | { |
3331 | { |
2754 | SPI_CloseRxTx_ISR(hspi); |
3332 | SPI_CloseRxTx_ISR(hspi); |
2755 | } |
3333 | } |
2756 | } |
3334 | } |
2757 | } |
3335 | } |
2758 | 3336 | ||
2759 | #if (USE_SPI_CRC != 0U) |
3337 | #if (USE_SPI_CRC != 0U) |
2760 | /** |
3338 | /** |
2761 | * @brief Manage the CRC 8-bit receive in Interrupt context. |
3339 | * @brief Manage the CRC 8-bit receive in Interrupt context. |
2762 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3340 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2763 | * the configuration information for SPI module. |
3341 | * the configuration information for SPI module. |
2764 | * @retval None |
3342 | * @retval None |
2765 | */ |
3343 | */ |
2766 | static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi) |
3344 | static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi) |
2767 | { |
3345 | { |
2768 | __IO uint8_t tmpreg = 0U; |
- | |
2769 | - | ||
2770 | /* Read data register to flush CRC */ |
3346 | /* Read 8bit CRC to flush Data Register */ |
2771 | tmpreg = *((__IO uint8_t*)&hspi->Instance->DR); |
3347 | READ_REG(*(__IO uint8_t *)&hspi->Instance->DR); |
2772 | - | ||
2773 | /* To avoid GCC warning */ |
- | |
2774 | UNUSED(tmpreg); |
- | |
2775 | 3348 | ||
2776 | SPI_CloseRx_ISR(hspi); |
3349 | SPI_CloseRx_ISR(hspi); |
2777 | } |
3350 | } |
2778 | #endif /* USE_SPI_CRC */ |
3351 | #endif /* USE_SPI_CRC */ |
2779 | 3352 | ||
2780 | /** |
3353 | /** |
2781 | * @brief Manage the receive 8-bit in Interrupt context. |
3354 | * @brief Manage the receive 8-bit in Interrupt context. |
2782 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3355 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2783 | * the configuration information for SPI module. |
3356 | * the configuration information for SPI module. |
2784 | * @retval None |
3357 | * @retval None |
2785 | */ |
3358 | */ |
2786 | static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi) |
3359 | static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi) |
2787 | { |
3360 | { |
2788 | *hspi->pRxBuffPtr++ = (*(__IO uint8_t *)&hspi->Instance->DR); |
3361 | *hspi->pRxBuffPtr = (*(__IO uint8_t *)&hspi->Instance->DR); |
- | 3362 | hspi->pRxBuffPtr++; |
|
2789 | hspi->RxXferCount--; |
3363 | hspi->RxXferCount--; |
2790 | 3364 | ||
2791 | #if (USE_SPI_CRC != 0U) |
3365 | #if (USE_SPI_CRC != 0U) |
2792 | /* Enable CRC Transmission */ |
3366 | /* Enable CRC Transmission */ |
2793 | if((hspi->RxXferCount == 1U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)) |
3367 | if ((hspi->RxXferCount == 1U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)) |
2794 | { |
3368 | { |
2795 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
3369 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
2796 | } |
3370 | } |
- | 3371 | /* Check if CRCNEXT is well reseted by hardware */ |
|
- | 3372 | if (READ_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT)) |
|
- | 3373 | { |
|
- | 3374 | /* Workaround to force CRCNEXT bit to zero in case of CRCNEXT is not reset automatically by hardware */ |
|
- | 3375 | CLEAR_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
|
- | 3376 | } |
|
- | 3377 | ||
2797 | #endif /* USE_SPI_CRC */ |
3378 | #endif /* USE_SPI_CRC */ |
2798 | 3379 | ||
2799 | if(hspi->RxXferCount == 0U) |
3380 | if (hspi->RxXferCount == 0U) |
2800 | { |
3381 | { |
2801 | #if (USE_SPI_CRC != 0U) |
3382 | #if (USE_SPI_CRC != 0U) |
2802 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
3383 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
2803 | { |
3384 | { |
2804 | hspi->RxISR = SPI_RxISR_8BITCRC; |
3385 | hspi->RxISR = SPI_RxISR_8BITCRC; |
2805 | return; |
3386 | return; |
2806 | } |
3387 | } |
2807 | #endif /* USE_SPI_CRC */ |
3388 | #endif /* USE_SPI_CRC */ |
Line 2810... | Line 3391... | ||
2810 | } |
3391 | } |
2811 | 3392 | ||
2812 | #if (USE_SPI_CRC != 0U) |
3393 | #if (USE_SPI_CRC != 0U) |
2813 | /** |
3394 | /** |
2814 | * @brief Manage the CRC 16-bit receive in Interrupt context. |
3395 | * @brief Manage the CRC 16-bit receive in Interrupt context. |
2815 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3396 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2816 | * the configuration information for SPI module. |
3397 | * the configuration information for SPI module. |
2817 | * @retval None |
3398 | * @retval None |
2818 | */ |
3399 | */ |
2819 | static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi) |
3400 | static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi) |
2820 | { |
3401 | { |
2821 | __IO uint16_t tmpreg = 0U; |
- | |
2822 | - | ||
2823 | /* Read data register to flush CRC */ |
3402 | /* Read 16bit CRC to flush Data Register */ |
2824 | tmpreg = hspi->Instance->DR; |
3403 | READ_REG(hspi->Instance->DR); |
2825 | - | ||
2826 | /* To avoid GCC warning */ |
- | |
2827 | UNUSED(tmpreg); |
- | |
2828 | 3404 | ||
2829 | /* Disable RXNE and ERR interrupt */ |
3405 | /* Disable RXNE and ERR interrupt */ |
2830 | __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR)); |
3406 | __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR)); |
2831 | 3407 | ||
2832 | SPI_CloseRx_ISR(hspi); |
3408 | SPI_CloseRx_ISR(hspi); |
2833 | } |
3409 | } |
2834 | #endif /* USE_SPI_CRC */ |
3410 | #endif /* USE_SPI_CRC */ |
2835 | 3411 | ||
2836 | /** |
3412 | /** |
2837 | * @brief Manage the 16-bit receive in Interrupt context. |
3413 | * @brief Manage the 16-bit receive in Interrupt context. |
2838 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3414 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2839 | * the configuration information for SPI module. |
3415 | * the configuration information for SPI module. |
2840 | * @retval None |
3416 | * @retval None |
2841 | */ |
3417 | */ |
2842 | static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi) |
3418 | static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi) |
2843 | { |
3419 | { |
2844 | *((uint16_t *)hspi->pRxBuffPtr) = hspi->Instance->DR; |
3420 | *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)(hspi->Instance->DR); |
2845 | hspi->pRxBuffPtr += sizeof(uint16_t); |
3421 | hspi->pRxBuffPtr += sizeof(uint16_t); |
2846 | hspi->RxXferCount--; |
3422 | hspi->RxXferCount--; |
2847 | 3423 | ||
2848 | #if (USE_SPI_CRC != 0U) |
3424 | #if (USE_SPI_CRC != 0U) |
2849 | /* Enable CRC Transmission */ |
3425 | /* Enable CRC Transmission */ |
2850 | if((hspi->RxXferCount == 1U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)) |
3426 | if ((hspi->RxXferCount == 1U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)) |
2851 | { |
3427 | { |
2852 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
3428 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
2853 | } |
3429 | } |
- | 3430 | /* Check if CRCNEXT is well reseted by hardware */ |
|
- | 3431 | if (READ_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT)) |
|
- | 3432 | { |
|
- | 3433 | /* Workaround to force CRCNEXT bit to zero in case of CRCNEXT is not reset automatically by hardware */ |
|
- | 3434 | CLEAR_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
|
- | 3435 | } |
|
- | 3436 | ||
2854 | #endif /* USE_SPI_CRC */ |
3437 | #endif /* USE_SPI_CRC */ |
2855 | 3438 | ||
2856 | if(hspi->RxXferCount == 0U) |
3439 | if (hspi->RxXferCount == 0U) |
2857 | { |
3440 | { |
2858 | #if (USE_SPI_CRC != 0U) |
3441 | #if (USE_SPI_CRC != 0U) |
2859 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
3442 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
2860 | { |
3443 | { |
2861 | hspi->RxISR = SPI_RxISR_16BITCRC; |
3444 | hspi->RxISR = SPI_RxISR_16BITCRC; |
2862 | return; |
3445 | return; |
2863 | } |
3446 | } |
2864 | #endif /* USE_SPI_CRC */ |
3447 | #endif /* USE_SPI_CRC */ |
Line 2866... | Line 3449... | ||
2866 | } |
3449 | } |
2867 | } |
3450 | } |
2868 | 3451 | ||
2869 | /** |
3452 | /** |
2870 | * @brief Handle the data 8-bit transmit in Interrupt mode. |
3453 | * @brief Handle the data 8-bit transmit in Interrupt mode. |
2871 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3454 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2872 | * the configuration information for SPI module. |
3455 | * the configuration information for SPI module. |
2873 | * @retval None |
3456 | * @retval None |
2874 | */ |
3457 | */ |
2875 | static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi) |
3458 | static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi) |
2876 | { |
3459 | { |
2877 | *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr++); |
3460 | *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr); |
- | 3461 | hspi->pTxBuffPtr++; |
|
2878 | hspi->TxXferCount--; |
3462 | hspi->TxXferCount--; |
2879 | 3463 | ||
2880 | if(hspi->TxXferCount == 0U) |
3464 | if (hspi->TxXferCount == 0U) |
2881 | { |
3465 | { |
2882 | #if (USE_SPI_CRC != 0U) |
3466 | #if (USE_SPI_CRC != 0U) |
2883 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
3467 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
2884 | { |
3468 | { |
2885 | /* Enable CRC Transmission */ |
3469 | /* Enable CRC Transmission */ |
2886 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
3470 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
2887 | } |
3471 | } |
2888 | #endif /* USE_SPI_CRC */ |
3472 | #endif /* USE_SPI_CRC */ |
Line 2890... | Line 3474... | ||
2890 | } |
3474 | } |
2891 | } |
3475 | } |
2892 | 3476 | ||
2893 | /** |
3477 | /** |
2894 | * @brief Handle the data 16-bit transmit in Interrupt mode. |
3478 | * @brief Handle the data 16-bit transmit in Interrupt mode. |
2895 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3479 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2896 | * the configuration information for SPI module. |
3480 | * the configuration information for SPI module. |
2897 | * @retval None |
3481 | * @retval None |
2898 | */ |
3482 | */ |
2899 | static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi) |
3483 | static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi) |
2900 | { |
3484 | { |
2901 | /* Transmit data in 16 Bit mode */ |
3485 | /* Transmit data in 16 Bit mode */ |
2902 | hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr); |
3486 | hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr); |
2903 | hspi->pTxBuffPtr += sizeof(uint16_t); |
3487 | hspi->pTxBuffPtr += sizeof(uint16_t); |
2904 | hspi->TxXferCount--; |
3488 | hspi->TxXferCount--; |
2905 | 3489 | ||
2906 | if(hspi->TxXferCount == 0U) |
3490 | if (hspi->TxXferCount == 0U) |
2907 | { |
3491 | { |
2908 | #if (USE_SPI_CRC != 0U) |
3492 | #if (USE_SPI_CRC != 0U) |
2909 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
3493 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
2910 | { |
3494 | { |
2911 | /* Enable CRC Transmission */ |
3495 | /* Enable CRC Transmission */ |
2912 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
3496 | SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT); |
2913 | } |
3497 | } |
2914 | #endif /* USE_SPI_CRC */ |
3498 | #endif /* USE_SPI_CRC */ |
2915 | SPI_CloseTx_ISR(hspi); |
3499 | SPI_CloseTx_ISR(hspi); |
2916 | } |
3500 | } |
2917 | } |
3501 | } |
2918 | 3502 | ||
2919 | /** |
3503 | /** |
2920 | * @brief Handle SPI Communication Timeout. |
3504 | * @brief Handle SPI Communication Timeout. |
2921 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3505 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2922 | * the configuration information for SPI module. |
3506 | * the configuration information for SPI module. |
2923 | * @param Flag: SPI flag to check |
3507 | * @param Flag SPI flag to check |
2924 | * @param State: flag state to check |
3508 | * @param State flag state to check |
2925 | * @param Timeout: Timeout duration |
3509 | * @param Timeout Timeout duration |
2926 | * @param Tickstart: tick start value |
3510 | * @param Tickstart tick start value |
2927 | * @retval HAL status |
3511 | * @retval HAL status |
2928 | */ |
3512 | */ |
2929 | static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, uint32_t State, uint32_t Timeout, uint32_t Tickstart) |
3513 | static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus State, |
- | 3514 | uint32_t Timeout, uint32_t Tickstart) |
|
2930 | { |
3515 | { |
- | 3516 | __IO uint32_t count; |
|
- | 3517 | uint32_t tmp_timeout; |
|
- | 3518 | uint32_t tmp_tickstart; |
|
- | 3519 | ||
- | 3520 | /* Adjust Timeout value in case of end of transfer */ |
|
- | 3521 | tmp_timeout = Timeout - (HAL_GetTick() - Tickstart); |
|
- | 3522 | tmp_tickstart = HAL_GetTick(); |
|
- | 3523 | ||
- | 3524 | /* Calculate Timeout based on a software loop to avoid blocking issue if Systick is disabled */ |
|
- | 3525 | count = tmp_timeout * ((SystemCoreClock * 32U) >> 20U); |
|
- | 3526 | ||
2931 | while((((hspi->Instance->SR & Flag) == (Flag)) ? SET : RESET) != State) |
3527 | while ((__HAL_SPI_GET_FLAG(hspi, Flag) ? SET : RESET) != State) |
2932 | { |
3528 | { |
2933 | if(Timeout != HAL_MAX_DELAY) |
3529 | if (Timeout != HAL_MAX_DELAY) |
2934 | { |
3530 | { |
2935 | if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) >= Timeout)) |
3531 | if (((HAL_GetTick() - tmp_tickstart) >= tmp_timeout) || (tmp_timeout == 0U)) |
2936 | { |
3532 | { |
2937 | /* Disable the SPI and reset the CRC: the CRC value should be cleared |
3533 | /* Disable the SPI and reset the CRC: the CRC value should be cleared |
2938 | on both master and slave sides in order to resynchronize the master |
3534 | on both master and slave sides in order to resynchronize the master |
2939 | and slave for their respective CRC calculation */ |
3535 | and slave for their respective CRC calculation */ |
2940 | 3536 | ||
2941 | /* Disable TXE, RXNE and ERR interrupts for the interrupt process */ |
3537 | /* Disable TXE, RXNE and ERR interrupts for the interrupt process */ |
2942 | __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR)); |
3538 | __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR)); |
2943 | 3539 | ||
2944 | if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY))) |
3540 | if ((hspi->Init.Mode == SPI_MODE_MASTER) && ((hspi->Init.Direction == SPI_DIRECTION_1LINE) |
- | 3541 | || (hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY))) |
|
2945 | { |
3542 | { |
2946 | /* Disable SPI peripheral */ |
3543 | /* Disable SPI peripheral */ |
2947 | __HAL_SPI_DISABLE(hspi); |
3544 | __HAL_SPI_DISABLE(hspi); |
2948 | } |
3545 | } |
2949 | 3546 | ||
2950 | /* Reset CRC Calculation */ |
3547 | /* Reset CRC Calculation */ |
2951 | if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
3548 | if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) |
2952 | { |
3549 | { |
2953 | SPI_RESET_CRC(hspi); |
3550 | SPI_RESET_CRC(hspi); |
2954 | } |
3551 | } |
2955 | 3552 | ||
2956 | hspi->State= HAL_SPI_STATE_READY; |
3553 | hspi->State = HAL_SPI_STATE_READY; |
2957 | 3554 | ||
2958 | /* Process Unlocked */ |
3555 | /* Process Unlocked */ |
2959 | __HAL_UNLOCK(hspi); |
3556 | __HAL_UNLOCK(hspi); |
2960 | 3557 | ||
2961 | return HAL_TIMEOUT; |
3558 | return HAL_TIMEOUT; |
2962 | } |
3559 | } |
- | 3560 | /* If Systick is disabled or not incremented, deactivate timeout to go in disable loop procedure */ |
|
- | 3561 | if(count == 0U) |
|
- | 3562 | { |
|
- | 3563 | tmp_timeout = 0U; |
|
- | 3564 | } |
|
- | 3565 | count--; |
|
2963 | } |
3566 | } |
2964 | } |
3567 | } |
2965 | 3568 | ||
2966 | return HAL_OK; |
3569 | return HAL_OK; |
2967 | } |
3570 | } |
- | 3571 | ||
2968 | /** |
3572 | /** |
2969 | * @brief Handle to check BSY flag before start a new transaction. |
3573 | * @brief Handle the check of the RX transaction complete. |
2970 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3574 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2971 | * the configuration information for SPI module. |
3575 | * the configuration information for SPI module. |
2972 | * @param Timeout: Timeout duration |
3576 | * @param Timeout Timeout duration |
2973 | * @param Tickstart: tick start value |
3577 | * @param Tickstart tick start value |
2974 | * @retval HAL status |
3578 | * @retval HAL status |
2975 | */ |
3579 | */ |
2976 | static HAL_StatusTypeDef SPI_CheckFlag_BSY(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart) |
3580 | static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart) |
- | 3581 | { |
|
- | 3582 | if ((hspi->Init.Mode == SPI_MODE_MASTER) && ((hspi->Init.Direction == SPI_DIRECTION_1LINE) |
|
- | 3583 | || (hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY))) |
|
- | 3584 | { |
|
- | 3585 | /* Disable SPI peripheral */ |
|
- | 3586 | __HAL_SPI_DISABLE(hspi); |
|
- | 3587 | } |
|
- | 3588 | ||
- | 3589 | if ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)) |
|
- | 3590 | { |
|
- | 3591 | /* Wait the RXNE reset */ |
|
- | 3592 | if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout, Tickstart) != HAL_OK) |
|
- | 3593 | { |
|
- | 3594 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
|
- | 3595 | return HAL_TIMEOUT; |
|
- | 3596 | } |
|
- | 3597 | } |
|
- | 3598 | else |
|
- | 3599 | { |
|
- | 3600 | /* Control the BSY flag */ |
|
- | 3601 | if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout, Tickstart) != HAL_OK) |
|
- | 3602 | { |
|
- | 3603 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
|
- | 3604 | return HAL_TIMEOUT; |
|
- | 3605 | } |
|
- | 3606 | } |
|
- | 3607 | return HAL_OK; |
|
- | 3608 | } |
|
- | 3609 | ||
- | 3610 | /** |
|
- | 3611 | * @brief Handle the check of the RXTX or TX transaction complete. |
|
- | 3612 | * @param hspi SPI handle |
|
- | 3613 | * @param Timeout Timeout duration |
|
- | 3614 | * @param Tickstart tick start value |
|
- | 3615 | * @retval HAL status |
|
- | 3616 | */ |
|
- | 3617 | static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart) |
|
2977 | { |
3618 | { |
2978 | /* Control the BSY flag */ |
3619 | /* Control the BSY flag */ |
2979 | if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout, Tickstart) != HAL_OK) |
3620 | if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout, Tickstart) != HAL_OK) |
2980 | { |
3621 | { |
2981 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
3622 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
2982 | return HAL_TIMEOUT; |
3623 | return HAL_TIMEOUT; |
2983 | } |
3624 | } |
2984 | return HAL_OK; |
3625 | return HAL_OK; |
2985 | } |
3626 | } |
2986 | 3627 | ||
2987 | /** |
3628 | /** |
2988 | * @brief Handle the end of the RXTX transaction. |
3629 | * @brief Handle the end of the RXTX transaction. |
2989 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3630 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
2990 | * the configuration information for SPI module. |
3631 | * the configuration information for SPI module. |
2991 | * @retval None |
3632 | * @retval None |
2992 | */ |
3633 | */ |
2993 | static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi) |
3634 | static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi) |
2994 | { |
3635 | { |
2995 | uint32_t tickstart = 0U; |
3636 | uint32_t tickstart; |
2996 | __IO uint32_t count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U); |
3637 | __IO uint32_t count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U); |
- | 3638 | ||
2997 | /* Init tickstart for timeout managment*/ |
3639 | /* Init tickstart for timeout management */ |
2998 | tickstart = HAL_GetTick(); |
3640 | tickstart = HAL_GetTick(); |
2999 | 3641 | ||
3000 | /* Disable ERR interrupt */ |
3642 | /* Disable ERR interrupt */ |
3001 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR); |
3643 | __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR); |
3002 | 3644 | ||
3003 | /* Wait until TXE flag is set */ |
3645 | /* Wait until TXE flag is set */ |
3004 | do |
3646 | do |
3005 | { |
3647 | { |
3006 | if(count-- == 0U) |
3648 | if (count == 0U) |
3007 | { |
3649 | { |
3008 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
3650 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
3009 | break; |
3651 | break; |
3010 | } |
3652 | } |
3011 | } |
3653 | count--; |
3012 | while((hspi->Instance->SR & SPI_FLAG_TXE) == RESET); |
3654 | } while ((hspi->Instance->SR & SPI_FLAG_TXE) == RESET); |
3013 | 3655 | ||
3014 | /* Check the end of the transaction */ |
3656 | /* Check the end of the transaction */ |
3015 | if(SPI_CheckFlag_BSY(hspi, SPI_DEFAULT_TIMEOUT, tickstart)!=HAL_OK) |
3657 | if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK) |
3016 | { |
3658 | { |
3017 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
3659 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
3018 | } |
3660 | } |
3019 | 3661 | ||
3020 | /* Clear overrun flag in 2 Lines communication mode because received is not read */ |
3662 | /* Clear overrun flag in 2 Lines communication mode because received is not read */ |
3021 | if(hspi->Init.Direction == SPI_DIRECTION_2LINES) |
3663 | if (hspi->Init.Direction == SPI_DIRECTION_2LINES) |
3022 | { |
3664 | { |
3023 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
3665 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
3024 | } |
3666 | } |
3025 | 3667 | ||
3026 | #if (USE_SPI_CRC != 0U) |
3668 | #if (USE_SPI_CRC != 0U) |
3027 | /* Check if CRC error occurred */ |
3669 | /* Check if CRC error occurred */ |
3028 | if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET) |
3670 | if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET) |
3029 | { |
3671 | { |
3030 | /* Check if CRC error is valid or not (workaround to be applied or not) */ |
3672 | /* Check if CRC error is valid or not (workaround to be applied or not) */ |
3031 | if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR) |
3673 | if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR) |
3032 | { |
3674 | { |
3033 | hspi->State = HAL_SPI_STATE_READY; |
3675 | hspi->State = HAL_SPI_STATE_READY; |
3034 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
3676 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
3035 | 3677 | ||
3036 | /* Reset CRC Calculation */ |
3678 | /* Reset CRC Calculation */ |
3037 | SPI_RESET_CRC(hspi); |
3679 | SPI_RESET_CRC(hspi); |
3038 | 3680 | ||
- | 3681 | /* Call user error callback */ |
|
- | 3682 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 3683 | hspi->ErrorCallback(hspi); |
|
- | 3684 | #else |
|
3039 | HAL_SPI_ErrorCallback(hspi); |
3685 | HAL_SPI_ErrorCallback(hspi); |
- | 3686 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
3040 | } |
3687 | } |
3041 | else |
3688 | else |
3042 | { |
3689 | { |
3043 | __HAL_SPI_CLEAR_CRCERRFLAG(hspi); |
3690 | __HAL_SPI_CLEAR_CRCERRFLAG(hspi); |
3044 | } |
3691 | } |
3045 | } |
3692 | } |
3046 | else |
3693 | else |
3047 | { |
3694 | { |
3048 | #endif /* USE_SPI_CRC */ |
3695 | #endif /* USE_SPI_CRC */ |
3049 | if(hspi->ErrorCode == HAL_SPI_ERROR_NONE) |
3696 | if (hspi->ErrorCode == HAL_SPI_ERROR_NONE) |
3050 | { |
3697 | { |
3051 | if(hspi->State == HAL_SPI_STATE_BUSY_RX) |
3698 | if (hspi->State == HAL_SPI_STATE_BUSY_RX) |
3052 | { |
3699 | { |
3053 | hspi->State = HAL_SPI_STATE_READY; |
3700 | hspi->State = HAL_SPI_STATE_READY; |
- | 3701 | /* Call user Rx complete callback */ |
|
- | 3702 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 3703 | hspi->RxCpltCallback(hspi); |
|
- | 3704 | #else |
|
3054 | HAL_SPI_RxCpltCallback(hspi); |
3705 | HAL_SPI_RxCpltCallback(hspi); |
- | 3706 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
3055 | } |
3707 | } |
3056 | else |
3708 | else |
3057 | { |
3709 | { |
3058 | hspi->State = HAL_SPI_STATE_READY; |
3710 | hspi->State = HAL_SPI_STATE_READY; |
- | 3711 | /* Call user TxRx complete callback */ |
|
- | 3712 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 3713 | hspi->TxRxCpltCallback(hspi); |
|
- | 3714 | #else |
|
3059 | HAL_SPI_TxRxCpltCallback(hspi); |
3715 | HAL_SPI_TxRxCpltCallback(hspi); |
- | 3716 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
3060 | } |
3717 | } |
3061 | } |
3718 | } |
3062 | else |
3719 | else |
3063 | { |
3720 | { |
3064 | hspi->State = HAL_SPI_STATE_READY; |
3721 | hspi->State = HAL_SPI_STATE_READY; |
- | 3722 | /* Call user error callback */ |
|
- | 3723 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 3724 | hspi->ErrorCallback(hspi); |
|
- | 3725 | #else |
|
3065 | HAL_SPI_ErrorCallback(hspi); |
3726 | HAL_SPI_ErrorCallback(hspi); |
- | 3727 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
3066 | } |
3728 | } |
3067 | #if (USE_SPI_CRC != 0U) |
3729 | #if (USE_SPI_CRC != 0U) |
3068 | } |
3730 | } |
3069 | #endif /* USE_SPI_CRC */ |
3731 | #endif /* USE_SPI_CRC */ |
3070 | } |
3732 | } |
3071 | 3733 | ||
3072 | /** |
3734 | /** |
3073 | * @brief Handle the end of the RX transaction. |
3735 | * @brief Handle the end of the RX transaction. |
3074 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3736 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
3075 | * the configuration information for SPI module. |
3737 | * the configuration information for SPI module. |
3076 | * @retval None |
3738 | * @retval None |
3077 | */ |
3739 | */ |
3078 | static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi) |
3740 | static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi) |
3079 | { |
3741 | { |
3080 | /* Disable RXNE and ERR interrupt */ |
3742 | /* Disable RXNE and ERR interrupt */ |
3081 | __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR)); |
3743 | __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR)); |
3082 | 3744 | ||
3083 | /* Check the end of the transaction */ |
3745 | /* Check the end of the transaction */ |
3084 | if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY))) |
3746 | if (SPI_EndRxTransaction(hspi, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK) |
3085 | { |
3747 | { |
3086 | /* Disable SPI peripheral */ |
- | |
3087 | __HAL_SPI_DISABLE(hspi); |
3748 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
3088 | } |
3749 | } |
3089 | 3750 | ||
3090 | /* Clear overrun flag in 2 Lines communication mode because received is not read */ |
3751 | /* Clear overrun flag in 2 Lines communication mode because received is not read */ |
3091 | if(hspi->Init.Direction == SPI_DIRECTION_2LINES) |
3752 | if (hspi->Init.Direction == SPI_DIRECTION_2LINES) |
3092 | { |
3753 | { |
3093 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
3754 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
3094 | } |
3755 | } |
3095 | hspi->State = HAL_SPI_STATE_READY; |
3756 | hspi->State = HAL_SPI_STATE_READY; |
3096 | 3757 | ||
3097 | #if (USE_SPI_CRC != 0U) |
3758 | #if (USE_SPI_CRC != 0U) |
3098 | /* Check if CRC error occurred */ |
3759 | /* Check if CRC error occurred */ |
3099 | if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET) |
3760 | if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET) |
- | 3761 | { |
|
- | 3762 | /* Check if CRC error is valid or not (workaround to be applied or not) */ |
|
- | 3763 | if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR) |
|
3100 | { |
3764 | { |
3101 | /* Check if CRC error is valid or not (workaround to be applied or not) */ |
- | |
3102 | if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR) |
- | |
3103 | { |
- | |
3104 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
3765 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC); |
3105 | 3766 | ||
3106 | /* Reset CRC Calculation */ |
3767 | /* Reset CRC Calculation */ |
3107 | SPI_RESET_CRC(hspi); |
3768 | SPI_RESET_CRC(hspi); |
3108 | 3769 | ||
3109 | HAL_SPI_ErrorCallback(hspi); |
3770 | /* Call user error callback */ |
3110 | } |
3771 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
3111 | else |
3772 | hspi->ErrorCallback(hspi); |
3112 | { |
3773 | #else |
3113 | __HAL_SPI_CLEAR_CRCERRFLAG(hspi); |
3774 | HAL_SPI_ErrorCallback(hspi); |
3114 | } |
3775 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
3115 | } |
3776 | } |
3116 | else |
3777 | else |
3117 | { |
3778 | { |
- | 3779 | __HAL_SPI_CLEAR_CRCERRFLAG(hspi); |
|
- | 3780 | } |
|
- | 3781 | } |
|
- | 3782 | else |
|
- | 3783 | { |
|
3118 | #endif /* USE_SPI_CRC */ |
3784 | #endif /* USE_SPI_CRC */ |
3119 | if(hspi->ErrorCode == HAL_SPI_ERROR_NONE) |
3785 | if (hspi->ErrorCode == HAL_SPI_ERROR_NONE) |
3120 | { |
3786 | { |
- | 3787 | /* Call user Rx complete callback */ |
|
- | 3788 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 3789 | hspi->RxCpltCallback(hspi); |
|
- | 3790 | #else |
|
3121 | HAL_SPI_RxCpltCallback(hspi); |
3791 | HAL_SPI_RxCpltCallback(hspi); |
- | 3792 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
3122 | } |
3793 | } |
3123 | else |
3794 | else |
3124 | { |
3795 | { |
- | 3796 | /* Call user error callback */ |
|
- | 3797 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
3125 | HAL_SPI_ErrorCallback(hspi); |
3798 | hspi->ErrorCallback(hspi); |
3126 | } |
3799 | #else |
- | 3800 | HAL_SPI_ErrorCallback(hspi); |
|
3127 | #if (USE_SPI_CRC != 0U) |
3801 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
3128 | } |
3802 | } |
- | 3803 | #if (USE_SPI_CRC != 0U) |
|
- | 3804 | } |
|
3129 | #endif /* USE_SPI_CRC */ |
3805 | #endif /* USE_SPI_CRC */ |
3130 | } |
3806 | } |
3131 | 3807 | ||
3132 | /** |
3808 | /** |
3133 | * @brief Handle the end of the TX transaction. |
3809 | * @brief Handle the end of the TX transaction. |
3134 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3810 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
3135 | * the configuration information for SPI module. |
3811 | * the configuration information for SPI module. |
3136 | * @retval None |
3812 | * @retval None |
3137 | */ |
3813 | */ |
3138 | static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi) |
3814 | static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi) |
3139 | { |
3815 | { |
3140 | uint32_t tickstart = 0U; |
3816 | uint32_t tickstart; |
3141 | __IO uint32_t count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U); |
3817 | __IO uint32_t count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U); |
3142 | 3818 | ||
3143 | /* Init tickstart for timeout management*/ |
3819 | /* Init tickstart for timeout management*/ |
3144 | tickstart = HAL_GetTick(); |
3820 | tickstart = HAL_GetTick(); |
3145 | 3821 | ||
3146 | /* Wait until TXE flag is set */ |
3822 | /* Wait until TXE flag is set */ |
3147 | do |
3823 | do |
3148 | { |
3824 | { |
3149 | if(count-- == 0U) |
3825 | if (count == 0U) |
3150 | { |
3826 | { |
3151 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
3827 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
3152 | break; |
3828 | break; |
3153 | } |
3829 | } |
3154 | } |
3830 | count--; |
3155 | while((hspi->Instance->SR & SPI_FLAG_TXE) == RESET); |
3831 | } while ((hspi->Instance->SR & SPI_FLAG_TXE) == RESET); |
3156 | 3832 | ||
3157 | /* Disable TXE and ERR interrupt */ |
3833 | /* Disable TXE and ERR interrupt */ |
3158 | __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_ERR)); |
3834 | __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_ERR)); |
3159 | 3835 | ||
3160 | /* Check Busy flag */ |
3836 | /* Check the end of the transaction */ |
3161 | if(SPI_CheckFlag_BSY(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK) |
3837 | if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK) |
3162 | { |
3838 | { |
3163 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
3839 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
3164 | } |
3840 | } |
3165 | 3841 | ||
3166 | /* Clear overrun flag in 2 Lines communication mode because received is not read */ |
3842 | /* Clear overrun flag in 2 Lines communication mode because received is not read */ |
3167 | if(hspi->Init.Direction == SPI_DIRECTION_2LINES) |
3843 | if (hspi->Init.Direction == SPI_DIRECTION_2LINES) |
3168 | { |
3844 | { |
3169 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
3845 | __HAL_SPI_CLEAR_OVRFLAG(hspi); |
3170 | } |
3846 | } |
3171 | 3847 | ||
3172 | hspi->State = HAL_SPI_STATE_READY; |
3848 | hspi->State = HAL_SPI_STATE_READY; |
3173 | if(hspi->ErrorCode != HAL_SPI_ERROR_NONE) |
3849 | if (hspi->ErrorCode != HAL_SPI_ERROR_NONE) |
3174 | { |
3850 | { |
- | 3851 | /* Call user error callback */ |
|
- | 3852 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 3853 | hspi->ErrorCallback(hspi); |
|
- | 3854 | #else |
|
3175 | HAL_SPI_ErrorCallback(hspi); |
3855 | HAL_SPI_ErrorCallback(hspi); |
- | 3856 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
3176 | } |
3857 | } |
3177 | else |
3858 | else |
3178 | { |
3859 | { |
- | 3860 | /* Call user Rx complete callback */ |
|
- | 3861 | #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U) |
|
- | 3862 | hspi->TxCpltCallback(hspi); |
|
- | 3863 | #else |
|
3179 | HAL_SPI_TxCpltCallback(hspi); |
3864 | HAL_SPI_TxCpltCallback(hspi); |
- | 3865 | #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */ |
|
3180 | } |
3866 | } |
3181 | } |
3867 | } |
3182 | 3868 | ||
3183 | /** |
3869 | /** |
3184 | * @} |
- | |
3185 | */ |
- | |
3186 | - | ||
3187 | /** |
- | |
3188 | * @brief Handle abort a Tx or Rx transaction. |
3870 | * @brief Handle abort a Rx transaction. |
3189 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3871 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
3190 | * the configuration information for SPI module. |
3872 | * the configuration information for SPI module. |
3191 | * @retval None |
3873 | * @retval None |
3192 | */ |
3874 | */ |
3193 | static void SPI_AbortRx_ISR(SPI_HandleTypeDef *hspi) |
3875 | static void SPI_AbortRx_ISR(SPI_HandleTypeDef *hspi) |
3194 | { |
3876 | { |
3195 | __IO uint32_t tmpreg = 0U; |
- | |
3196 | __IO uint32_t count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U); |
3877 | __IO uint32_t count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U); |
3197 | 3878 | ||
3198 | /* Wait until TXE flag is set */ |
3879 | /* Wait until TXE flag is set */ |
3199 | do |
3880 | do |
3200 | { |
3881 | { |
3201 | if(count-- == 0U) |
3882 | if (count == 0U) |
3202 | { |
3883 | { |
3203 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG); |
3884 | SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT); |
3204 | break; |
3885 | break; |
3205 | } |
3886 | } |
3206 | } |
3887 | count--; |
3207 | while((hspi->Instance->SR & SPI_FLAG_TXE) == RESET); |
3888 | } while ((hspi->Instance->SR & SPI_FLAG_TXE) == RESET); |
3208 | 3889 | ||
3209 | /* Disable SPI Peripheral */ |
3890 | /* Disable SPI Peripheral */ |
3210 | __HAL_SPI_DISABLE(hspi); |
3891 | __HAL_SPI_DISABLE(hspi); |
3211 | 3892 | ||
3212 | /* Disable TXEIE, RXNEIE and ERRIE(mode fault event, overrun error, TI frame error) interrupts */ |
3893 | /* Disable TXEIE, RXNEIE and ERRIE(mode fault event, overrun error, TI frame error) interrupts */ |
3213 | CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXEIE | SPI_CR2_RXNEIE | SPI_CR2_ERRIE)); |
3894 | CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXEIE | SPI_CR2_RXNEIE | SPI_CR2_ERRIE)); |
3214 | 3895 | ||
3215 | /* Flush DR Register */ |
3896 | /* Read CRC to flush Data Register */ |
3216 | tmpreg = (*(__IO uint32_t *)&hspi->Instance->DR); |
3897 | READ_REG(hspi->Instance->DR); |
3217 | 3898 | ||
3218 | /* To avoid GCC warning */ |
3899 | hspi->State = HAL_SPI_STATE_ABORT; |
3219 | UNUSED(tmpreg); |
- | |
3220 | } |
3900 | } |
3221 | 3901 | ||
3222 | /** |
3902 | /** |
3223 | * @brief Handle abort a Tx or Rx transaction. |
3903 | * @brief Handle abort a Tx or Rx/Tx transaction. |
3224 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3904 | * @param hspi pointer to a SPI_HandleTypeDef structure that contains |
3225 | * the configuration information for SPI module. |
3905 | * the configuration information for SPI module. |
3226 | * @retval None |
3906 | * @retval None |
3227 | */ |
3907 | */ |
3228 | static void SPI_AbortTx_ISR(SPI_HandleTypeDef *hspi) |
3908 | static void SPI_AbortTx_ISR(SPI_HandleTypeDef *hspi) |
3229 | { |
3909 | { |
3230 | /* Disable TXEIE, RXNEIE and ERRIE(mode fault event, overrun error, TI frame error) interrupts */ |
3910 | /* Disable TXEIE interrupt */ |
3231 | CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXEIE | SPI_CR2_RXNEIE | SPI_CR2_ERRIE)); |
3911 | CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXEIE)); |
3232 | 3912 | ||
3233 | /* Disable SPI Peripheral */ |
3913 | /* Disable SPI Peripheral */ |
3234 | __HAL_SPI_DISABLE(hspi); |
3914 | __HAL_SPI_DISABLE(hspi); |
- | 3915 | ||
- | 3916 | hspi->State = HAL_SPI_STATE_ABORT; |
|
3235 | } |
3917 | } |
3236 | 3918 | ||
- | 3919 | #if (USE_SPI_CRC != 0U) |
|
3237 | /** |
3920 | /** |
3238 | * @brief Checks if encountered CRC error could be corresponding to wrongly detected errors |
3921 | * @brief Checks if encountered CRC error could be corresponding to wrongly detected errors |
3239 | * according to SPI instance, Device type, and revision ID. |
3922 | * according to SPI instance, Device type, and revision ID. |
3240 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3923 | * @param hspi: pointer to a SPI_HandleTypeDef structure that contains |
3241 | * the configuration information for SPI module. |
3924 | * the configuration information for SPI module. |
3242 | * @retval CRC error validity (SPI_INVALID_CRC_ERROR or SPI_VALID_CRC_ERROR). |
3925 | * @retval CRC error validity (SPI_INVALID_CRC_ERROR or SPI_VALID_CRC_ERROR). |
3243 | */ |
3926 | */ |
3244 | __weak uint8_t SPI_ISCRCErrorValid(SPI_HandleTypeDef *hspi) |
3927 | uint8_t SPI_ISCRCErrorValid(SPI_HandleTypeDef *hspi) |
3245 | { |
3928 | { |
- | 3929 | #if defined(SPI_CRC_ERROR_WORKAROUND_FEATURE) && (USE_SPI_CRC_ERROR_WORKAROUND != 0U) |
|
- | 3930 | /* Check how to handle this CRC error (workaround to be applied or not) */ |
|
- | 3931 | /* If CRC errors could be wrongly detected (issue 2.15.2 in STM32F10xxC/D/E silicon limitations ES (DocID14732 Rev 13) */ |
|
- | 3932 | if (hspi->Instance == SPI2) |
|
- | 3933 | { |
|
- | 3934 | if (hspi->Instance->RXCRCR == 0U) |
|
- | 3935 | { |
|
- | 3936 | return (SPI_INVALID_CRC_ERROR); |
|
- | 3937 | } |
|
- | 3938 | } |
|
- | 3939 | #endif |
|
3246 | /* Prevent unused argument(s) compilation warning */ |
3940 | /* Prevent unused argument(s) compilation warning */ |
3247 | UNUSED(hspi); |
3941 | UNUSED(hspi); |
3248 | 3942 | ||
3249 | return (SPI_VALID_CRC_ERROR); |
3943 | return (SPI_VALID_CRC_ERROR); |
3250 | } |
3944 | } |
- | 3945 | #endif /* USE_SPI_CRC */ |
|
3251 | /** |
3946 | /** |
3252 | * @} |
3947 | * @} |
3253 | */ |
3948 | */ |
- | 3949 | ||
3254 | #endif /* HAL_SPI_MODULE_ENABLED */ |
3950 | #endif /* HAL_SPI_MODULE_ENABLED */ |
3255 | 3951 | ||
3256 | /** |
3952 | /** |
3257 | * @} |
3953 | * @} |
3258 | */ |
3954 | */ |