Subversion Repositories LedShow

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/**
2
  ******************************************************************************
3
  * @file    stm32f1xx_hal_spi_ex.c
4
  * @author  MCD Application Team
5
  * @brief   Extended SPI HAL module driver.
6
  *    
7
  *          This file provides firmware functions to manage the following
8
  *          functionalities SPI extension peripheral:
9
  *           + Extended Peripheral Control functions
10
  *  
11
  ******************************************************************************
12
  * @attention
13
  *
14
  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
15
  *
16
  * Redistribution and use in source and binary forms, with or without modification,
17
  * are permitted provided that the following conditions are met:
18
  *   1. Redistributions of source code must retain the above copyright notice,
19
  *      this list of conditions and the following disclaimer.
20
  *   2. Redistributions in binary form must reproduce the above copyright notice,
21
  *      this list of conditions and the following disclaimer in the documentation
22
  *      and/or other materials provided with the distribution.
23
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
24
  *      may be used to endorse or promote products derived from this software
25
  *      without specific prior written permission.
26
  *
27
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
  *
38
  ******************************************************************************
39
  */
40
 
41
/* Includes ------------------------------------------------------------------*/
42
#include "stm32f1xx_hal.h"
43
 
44
/** @addtogroup STM32F1xx_HAL_Driver
45
  * @{
46
  */
47
 
48
/** @addtogroup SPI
49
  * @{
50
  */
51
#ifdef HAL_SPI_MODULE_ENABLED
52
 
53
/** @defgroup SPI_Private_Variables SPI Private Variables
54
  * @{
55
  */
56
#if (USE_SPI_CRC != 0U)
57
/* Variable used to determine if device is impacted by implementation of workaround
58
   related to wrong CRC errors detection on SPI2. Conditions in which this workaround has to be applied, are:
59
    - STM32F101CDE/STM32F103CDE
60
    - Revision ID : Z
61
    - SPI2
62
    - In receive only mode, with CRC calculation enabled, at the end of the CRC reception,
63
      the software needs to check the CRCERR flag. If it is found set, read back the SPI_RXCRC:
64
        + If the value is 0, the complete data transfer is successful.
65
        + Otherwise, one or more errors have been detected during the data transfer by CPU or DMA.
66
      If CRCERR is found reset, the complete data transfer is considered successful.
67
*/
68
uint8_t uCRCErrorWorkaroundCheck = 0U;
69
#endif /* USE_SPI_CRC */
70
/**
71
  * @}
72
  */
73
 
74
 
75
/* Private typedef -----------------------------------------------------------*/
76
/* Private define ------------------------------------------------------------*/
77
/* Private macro -------------------------------------------------------------*/
78
/* Private variables ---------------------------------------------------------*/
79
/* Private function prototypes -----------------------------------------------*/
80
/* Private functions ---------------------------------------------------------*/
81
 
82
/** @addtogroup SPI_Exported_Functions
83
  * @{
84
  */
85
 
86
/** @addtogroup SPI_Exported_Functions_Group1
87
  *
88
  * @{
89
  */
90
 
91
/**
92
  * @brief  Initializes the SPI according to the specified parameters
93
  *         in the SPI_InitTypeDef and create the associated handle.
94
  * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
95
  *                the configuration information for SPI module.
96
  * @retval HAL status
97
  */
98
HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
99
{
100
  /* Check the SPI handle allocation */
101
  if(hspi == NULL)
102
  {
103
    return HAL_ERROR;
104
  }
105
 
106
  /* Check the parameters */
107
  assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
108
  assert_param(IS_SPI_MODE(hspi->Init.Mode));
109
  assert_param(IS_SPI_DIRECTION(hspi->Init.Direction));
110
  assert_param(IS_SPI_DATASIZE(hspi->Init.DataSize));
111
  assert_param(IS_SPI_CPOL(hspi->Init.CLKPolarity));
112
  assert_param(IS_SPI_CPHA(hspi->Init.CLKPhase));
113
  assert_param(IS_SPI_NSS(hspi->Init.NSS));
114
  assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler));
115
  assert_param(IS_SPI_FIRST_BIT(hspi->Init.FirstBit));
116
 
117
#if (USE_SPI_CRC != 0U)
118
  assert_param(IS_SPI_CRC_CALCULATION(hspi->Init.CRCCalculation));
119
  if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
120
  {
121
    assert_param(IS_SPI_CRC_POLYNOMIAL(hspi->Init.CRCPolynomial));
122
  }
123
#else
124
  hspi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
125
#endif /* USE_SPI_CRC */
126
 
127
  if(hspi->State == HAL_SPI_STATE_RESET)
128
  {
129
    /* Init the low level hardware : GPIO, CLOCK, NVIC... */
130
    HAL_SPI_MspInit(hspi);
131
  }
132
 
133
  hspi->State = HAL_SPI_STATE_BUSY;
134
 
135
  /* Disble the selected SPI peripheral */
136
  __HAL_SPI_DISABLE(hspi);
137
 
138
  /*----------------------- SPIx CR1 & CR2 Configuration ---------------------*/
139
  /* Configure : SPI Mode, Communication Mode, Data size, Clock polarity and phase, NSS management,
140
  Communication speed, First bit and CRC calculation state */
141
  WRITE_REG(hspi->Instance->CR1, (hspi->Init.Mode | hspi->Init.Direction | hspi->Init.DataSize |
142
                                  hspi->Init.CLKPolarity | hspi->Init.CLKPhase | (hspi->Init.NSS & SPI_CR1_SSM) |
143
                                  hspi->Init.BaudRatePrescaler | hspi->Init.FirstBit  | hspi->Init.CRCCalculation) );
144
 
145
  /* Configure : NSS management */
146
  WRITE_REG(hspi->Instance->CR2, (((hspi->Init.NSS >> 16U) & SPI_CR2_SSOE) | hspi->Init.TIMode));
147
 
148
  /*---------------------------- SPIx CRCPOLY Configuration ------------------*/
149
  /* Configure : CRC Polynomial */
150
  WRITE_REG(hspi->Instance->CRCPR, hspi->Init.CRCPolynomial);
151
 
152
#if defined(SPI_I2SCFGR_I2SMOD)
153
  /* Activate the SPI mode (Make sure that I2SMOD bit in I2SCFGR register is reset) */
154
  CLEAR_BIT(hspi->Instance->I2SCFGR, SPI_I2SCFGR_I2SMOD);
155
#endif /* SPI_I2SCFGR_I2SMOD */
156
 
157
#if (USE_SPI_CRC != 0U)
158
#if defined (STM32F101xE) || defined (STM32F103xE)
159
  /* Check RevisionID value for identifying if Device is Rev Z (0x0001) in order to enable workaround for
160
     CRC errors wrongly detected */
161
  /* Pb is that ES_STM32F10xxCDE also identify an issue in Debug registers access while not in Debug mode.
162
     Revision ID information is only available in Debug mode, so Workaround could not be implemented
163
     to distinguish Rev Z devices (issue present) from more recent version (issue fixed).
164
     So, in case of Revison Z F101 or F103 devices, below variable should be assigned to 1 */
165
  uCRCErrorWorkaroundCheck = 0U;
166
#else
167
  uCRCErrorWorkaroundCheck = 0U;
168
#endif /* STM32F101xE || STM32F103xE */
169
#endif /* USE_SPI_CRC */
170
 
171
  hspi->ErrorCode = HAL_SPI_ERROR_NONE;
172
  hspi->State = HAL_SPI_STATE_READY;
173
 
174
  return HAL_OK;
175
}
176
 
177
/**
178
  * @}
179
  */
180
 
181
/**
182
  * @}
183
  */
184
 
185
/** @addtogroup SPI_Private_Functions
186
  * @{
187
  */
188
#if (USE_SPI_CRC != 0U)
189
/**
190
  * @brief  Checks if encountered CRC error could be corresponding to wrongly detected errors
191
  *         according to SPI instance, Device type, and revision ID.
192
  * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
193
  *               the configuration information for SPI module.
194
  * @retval CRC error validity (SPI_INVALID_CRC_ERROR or SPI_VALID_CRC_ERROR).  
195
*/
196
uint8_t SPI_ISCRCErrorValid(SPI_HandleTypeDef *hspi)
197
{
198
#if defined(STM32F101xE) || defined(STM32F103xE)
199
  /* Check how to handle this CRC error (workaround to be applied or not) */
200
  /* If CRC errors could be wrongly detected (issue 2.15.2 in STM32F10xxC/D/E silicon limitations ES (DocID14732 Rev 13) */
201
  if((uCRCErrorWorkaroundCheck != 0U) && (hspi->Instance == SPI2))
202
  {
203
    if(hspi->Instance->RXCRCR == 0U)
204
    {
205
      return (SPI_INVALID_CRC_ERROR);
206
    }
207
  }
208
  return (SPI_VALID_CRC_ERROR);
209
#else
210
  /* Prevent unused argument(s) compilation warning */
211
  UNUSED(hspi);
212
 
213
  return (SPI_VALID_CRC_ERROR);
214
#endif
215
}
216
#endif /* USE_SPI_CRC */
217
 
218
/**
219
  * @}
220
  */
221
 
222
#endif /* HAL_SPI_MODULE_ENABLED */
223
/**
224
  * @}
225
  */
226
 
227
/**
228
  * @}
229
  */
230
 
231
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/