1 | /**
|
---|
2 | ******************************************************************************
|
---|
3 | * @file stm32f4xx_nucleo.c
|
---|
4 | * @author MCD Application Team
|
---|
5 | * @version V1.2.2
|
---|
6 | * @date 14-August-2015
|
---|
7 | * @brief This file provides set of firmware functions to manage:
|
---|
8 | * - LEDs and push-button available on STM32F4XX-Nucleo Kit
|
---|
9 | * from STMicroelectronics
|
---|
10 | * - LCD, joystick and microSD available on Adafruit 1.8" TFT LCD
|
---|
11 | * shield (reference ID 802)
|
---|
12 | ******************************************************************************
|
---|
13 | * @attention
|
---|
14 | *
|
---|
15 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
|
---|
16 | *
|
---|
17 | * Redistribution and use in source and binary forms, with or without modification,
|
---|
18 | * are permitted provided that the following conditions are met:
|
---|
19 | * 1. Redistributions of source code must retain the above copyright notice,
|
---|
20 | * this list of conditions and the following disclaimer.
|
---|
21 | * 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
22 | * this list of conditions and the following disclaimer in the documentation
|
---|
23 | * and/or other materials provided with the distribution.
|
---|
24 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
|
---|
25 | * may be used to endorse or promote products derived from this software
|
---|
26 | * without specific prior written permission.
|
---|
27 | *
|
---|
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
---|
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
31 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
---|
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
---|
34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
---|
35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
---|
36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
38 | *
|
---|
39 | ******************************************************************************
|
---|
40 | */
|
---|
41 |
|
---|
42 | /* Includes ------------------------------------------------------------------*/
|
---|
43 | #include "stm32f4xx_nucleo.h"
|
---|
44 |
|
---|
45 | /** @addtogroup BSP
|
---|
46 | * @{
|
---|
47 | */
|
---|
48 |
|
---|
49 | /** @addtogroup STM32F4XX_NUCLEO
|
---|
50 | * @{
|
---|
51 | */
|
---|
52 |
|
---|
53 | /** @addtogroup STM32F4XX_NUCLEO_LOW_LEVEL
|
---|
54 | * @brief This file provides set of firmware functions to manage Leds and push-button
|
---|
55 | * available on STM32F4xx-Nucleo Kit from STMicroelectronics.
|
---|
56 | * @{
|
---|
57 | */
|
---|
58 |
|
---|
59 | /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_TypesDefinitions
|
---|
60 | * @{
|
---|
61 | */
|
---|
62 | /**
|
---|
63 | * @}
|
---|
64 | */
|
---|
65 |
|
---|
66 |
|
---|
67 | /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_Defines
|
---|
68 | * @{
|
---|
69 | */
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * @brief STM32F4xx NUCLEO BSP Driver version number V1.2.2
|
---|
73 | */
|
---|
74 | #define __STM32F4xx_NUCLEO_BSP_VERSION_MAIN (0x01) /*!< [31:24] main version */
|
---|
75 | #define __STM32F4xx_NUCLEO_BSP_VERSION_SUB1 (0x02) /*!< [23:16] sub1 version */
|
---|
76 | #define __STM32F4xx_NUCLEO_BSP_VERSION_SUB2 (0x02) /*!< [15:8] sub2 version */
|
---|
77 | #define __STM32F4xx_NUCLEO_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */
|
---|
78 | #define __STM32F4xx_NUCLEO_BSP_VERSION ((__STM32F4xx_NUCLEO_BSP_VERSION_MAIN << 24)\
|
---|
79 | |(__STM32F4xx_NUCLEO_BSP_VERSION_SUB1 << 16)\
|
---|
80 | |(__STM32F4xx_NUCLEO_BSP_VERSION_SUB2 << 8 )\
|
---|
81 | |(__STM32F4xx_NUCLEO_BSP_VERSION_RC))
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * @brief LINK SD Card
|
---|
85 | */
|
---|
86 | #define SD_DUMMY_BYTE 0xFF
|
---|
87 | #define SD_NO_RESPONSE_EXPECTED 0x80
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * @}
|
---|
91 | */
|
---|
92 |
|
---|
93 | /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_Macros
|
---|
94 | * @{
|
---|
95 | */
|
---|
96 | /**
|
---|
97 | * @}
|
---|
98 | */
|
---|
99 |
|
---|
100 | /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_Variables
|
---|
101 | * @{
|
---|
102 | */
|
---|
103 | GPIO_TypeDef* GPIO_PORT[LEDn] = {LED2_GPIO_PORT};
|
---|
104 |
|
---|
105 | const uint16_t GPIO_PIN[LEDn] = {LED2_PIN};
|
---|
106 |
|
---|
107 | GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {KEY_BUTTON_GPIO_PORT};
|
---|
108 | const uint16_t BUTTON_PIN[BUTTONn] = {KEY_BUTTON_PIN};
|
---|
109 | const uint8_t BUTTON_IRQn[BUTTONn] = {KEY_BUTTON_EXTI_IRQn};
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * @brief BUS variables
|
---|
113 | */
|
---|
114 | #if 0
|
---|
115 | uint32_t SpixTimeout = NUCLEO_SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */
|
---|
116 | static SPI_HandleTypeDef hnucleo_Spi;
|
---|
117 | static ADC_HandleTypeDef hnucleo_Adc;
|
---|
118 |
|
---|
119 | /* ADC channel configuration structure declaration */
|
---|
120 | static ADC_ChannelConfTypeDef sConfig;
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * @}
|
---|
124 | */
|
---|
125 |
|
---|
126 | /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_FunctionPrototypes
|
---|
127 | * @{
|
---|
128 | */
|
---|
129 | static void SPIx_Init(void);
|
---|
130 | static void SPIx_Write(uint8_t Value);
|
---|
131 | static uint32_t SPIx_Read(void);
|
---|
132 | static void SPIx_Error(void);
|
---|
133 | static void SPIx_MspInit(SPI_HandleTypeDef *hspi);
|
---|
134 |
|
---|
135 | static void ADCx_Init(void);
|
---|
136 | static void ADCx_MspInit(ADC_HandleTypeDef *hadc);
|
---|
137 |
|
---|
138 | /* SD IO functions */
|
---|
139 | void SD_IO_Init(void);
|
---|
140 | HAL_StatusTypeDef SD_IO_WriteCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Response);
|
---|
141 | HAL_StatusTypeDef SD_IO_WaitResponse(uint8_t Response);
|
---|
142 | void SD_IO_WriteDummy(void);
|
---|
143 | void SD_IO_WriteByte(uint8_t Data);
|
---|
144 | uint8_t SD_IO_ReadByte(void);
|
---|
145 |
|
---|
146 | /* LCD IO functions */
|
---|
147 | void LCD_IO_Init(void);
|
---|
148 | void LCD_IO_WriteData(uint8_t Data);
|
---|
149 | void LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size);
|
---|
150 | void LCD_IO_WriteReg(uint8_t LCDReg);
|
---|
151 | void LCD_Delay(uint32_t delay);
|
---|
152 | #endif
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * @}
|
---|
156 | */
|
---|
157 |
|
---|
158 | /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_Functions
|
---|
159 | * @{
|
---|
160 | */
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * @brief This method returns the STM32F4xx NUCLEO BSP Driver revision
|
---|
164 | * @param None
|
---|
165 | * @retval version: 0xXYZR (8bits for each decimal, R for RC)
|
---|
166 | */
|
---|
167 | uint32_t BSP_GetVersion(void)
|
---|
168 | {
|
---|
169 | return __STM32F4xx_NUCLEO_BSP_VERSION;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * @brief Configures LED GPIO.
|
---|
174 | * @param Led: Specifies the Led to be configured.
|
---|
175 | * This parameter can be one of following parameters:
|
---|
176 | * @arg LED2
|
---|
177 | * @retval None
|
---|
178 | */
|
---|
179 | void BSP_LED_Init(Led_TypeDef Led)
|
---|
180 | {
|
---|
181 | GPIO_InitTypeDef GPIO_InitStruct;
|
---|
182 |
|
---|
183 | /* Enable the GPIO_LED Clock */
|
---|
184 | LEDx_GPIO_CLK_ENABLE(Led);
|
---|
185 |
|
---|
186 | /* Configure the GPIO_LED pin */
|
---|
187 | GPIO_InitStruct.Pin = GPIO_PIN[Led];
|
---|
188 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
---|
189 | GPIO_InitStruct.Pull = GPIO_PULLUP;
|
---|
190 | GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
|
---|
191 |
|
---|
192 | HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
|
---|
193 |
|
---|
194 | HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * @brief Turns selected LED On.
|
---|
199 | * @param Led: Specifies the Led to be set on.
|
---|
200 | * This parameter can be one of following parameters:
|
---|
201 | * @arg LED2
|
---|
202 | * @retval None
|
---|
203 | */
|
---|
204 | void BSP_LED_On(Led_TypeDef Led)
|
---|
205 | {
|
---|
206 | HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * @brief Turns selected LED Off.
|
---|
211 | * @param Led: Specifies the Led to be set off.
|
---|
212 | * This parameter can be one of following parameters:
|
---|
213 | * @arg LED2
|
---|
214 | * @retval None
|
---|
215 | */
|
---|
216 | void BSP_LED_Off(Led_TypeDef Led)
|
---|
217 | {
|
---|
218 | HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
|
---|
219 | }
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * @brief Toggles the selected LED.
|
---|
223 | * @param Led: Specifies the Led to be toggled.
|
---|
224 | * This parameter can be one of following parameters:
|
---|
225 | * @arg LED2
|
---|
226 | * @retval None
|
---|
227 | */
|
---|
228 | void BSP_LED_Toggle(Led_TypeDef Led)
|
---|
229 | {
|
---|
230 | HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * @brief Configures Button GPIO and EXTI Line.
|
---|
235 | * @param Button: Specifies the Button to be configured.
|
---|
236 | * This parameter should be: BUTTON_KEY
|
---|
237 | * @param ButtonMode: Specifies Button mode.
|
---|
238 | * This parameter can be one of following parameters:
|
---|
239 | * @arg BUTTON_MODE_GPIO: Button will be used as simple IO
|
---|
240 | * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
|
---|
241 | * generation capability
|
---|
242 | * @retval None
|
---|
243 | */
|
---|
244 | void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
|
---|
245 | {
|
---|
246 | GPIO_InitTypeDef GPIO_InitStruct;
|
---|
247 |
|
---|
248 | /* Enable the BUTTON Clock */
|
---|
249 | BUTTONx_GPIO_CLK_ENABLE(Button);
|
---|
250 |
|
---|
251 | if(ButtonMode == BUTTON_MODE_GPIO)
|
---|
252 | {
|
---|
253 | /* Configure Button pin as input */
|
---|
254 | GPIO_InitStruct.Pin = BUTTON_PIN[Button];
|
---|
255 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
---|
256 | GPIO_InitStruct.Pull = GPIO_PULLDOWN;
|
---|
257 | GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
|
---|
258 | HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
|
---|
259 | }
|
---|
260 |
|
---|
261 | if(ButtonMode == BUTTON_MODE_EXTI)
|
---|
262 | {
|
---|
263 | /* Configure Button pin as input with External interrupt */
|
---|
264 | GPIO_InitStruct.Pin = BUTTON_PIN[Button];
|
---|
265 | GPIO_InitStruct.Pull = GPIO_NOPULL;
|
---|
266 | GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
|
---|
267 | HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
|
---|
268 |
|
---|
269 | /* Enable and set Button EXTI Interrupt to the lowest priority */
|
---|
270 | // HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0x00);
|
---|
271 | // HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * @brief Returns the selected Button state.
|
---|
277 | * @param Button: Specifies the Button to be checked.
|
---|
278 | * This parameter should be: BUTTON_KEY
|
---|
279 | * @retval The Button GPIO pin value.
|
---|
280 | */
|
---|
281 | uint32_t BSP_PB_GetState(Button_TypeDef Button)
|
---|
282 | {
|
---|
283 | return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
|
---|
284 | }
|
---|
285 | #if 0
|
---|
286 | /******************************************************************************
|
---|
287 | BUS OPERATIONS
|
---|
288 | *******************************************************************************/
|
---|
289 | /**
|
---|
290 | * @brief Initializes SPI MSP.
|
---|
291 | * @param None
|
---|
292 | * @retval None
|
---|
293 | */
|
---|
294 | static void SPIx_MspInit(SPI_HandleTypeDef *hspi)
|
---|
295 | {
|
---|
296 | GPIO_InitTypeDef GPIO_InitStruct;
|
---|
297 |
|
---|
298 | /*** Configure the GPIOs ***/
|
---|
299 | /* Enable GPIO clock */
|
---|
300 | NUCLEO_SPIx_SCK_GPIO_CLK_ENABLE();
|
---|
301 | NUCLEO_SPIx_MISO_MOSI_GPIO_CLK_ENABLE();
|
---|
302 |
|
---|
303 | /* Configure SPI SCK */
|
---|
304 | GPIO_InitStruct.Pin = NUCLEO_SPIx_SCK_PIN;
|
---|
305 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
---|
306 | GPIO_InitStruct.Pull = GPIO_PULLUP;
|
---|
307 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
|
---|
308 | GPIO_InitStruct.Alternate = NUCLEO_SPIx_SCK_AF;
|
---|
309 | HAL_GPIO_Init(NUCLEO_SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);
|
---|
310 |
|
---|
311 | /* Configure SPI MISO and MOSI */
|
---|
312 | GPIO_InitStruct.Pin = NUCLEO_SPIx_MOSI_PIN;
|
---|
313 | GPIO_InitStruct.Alternate = NUCLEO_SPIx_MISO_MOSI_AF;
|
---|
314 | GPIO_InitStruct.Pull = GPIO_PULLDOWN;
|
---|
315 | HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &GPIO_InitStruct);
|
---|
316 |
|
---|
317 | GPIO_InitStruct.Pin = NUCLEO_SPIx_MISO_PIN;
|
---|
318 | HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &GPIO_InitStruct);
|
---|
319 |
|
---|
320 | /*** Configure the SPI peripheral ***/
|
---|
321 | /* Enable SPI clock */
|
---|
322 | NUCLEO_SPIx_CLK_ENABLE();
|
---|
323 | }
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * @brief Initializes SPI HAL.
|
---|
327 | * @param None
|
---|
328 | * @retval None
|
---|
329 | */
|
---|
330 | static void SPIx_Init(void)
|
---|
331 | {
|
---|
332 | if(HAL_SPI_GetState(&hnucleo_Spi) == HAL_SPI_STATE_RESET)
|
---|
333 | {
|
---|
334 | /* SPI Config */
|
---|
335 | hnucleo_Spi.Instance = NUCLEO_SPIx;
|
---|
336 | /* SPI baudrate is set to 12,5 MHz maximum (PCLK2/SPI_BaudRatePrescaler = 100/8 = 12,5 MHz)
|
---|
337 | to verify these constraints:
|
---|
338 | - ST7735 LCD SPI interface max baudrate is 15MHz for write and 6.66MHz for read
|
---|
339 | Since the provided driver doesn't use read capability from LCD, only constraint
|
---|
340 | on write baudrate is considered.
|
---|
341 | - SD card SPI interface max baudrate is 25MHz for write/read
|
---|
342 | - PCLK2 max frequency is 100 MHz
|
---|
343 | */
|
---|
344 | hnucleo_Spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
|
---|
345 | hnucleo_Spi.Init.Direction = SPI_DIRECTION_2LINES;
|
---|
346 | hnucleo_Spi.Init.CLKPhase = SPI_PHASE_2EDGE;
|
---|
347 | hnucleo_Spi.Init.CLKPolarity = SPI_POLARITY_HIGH;
|
---|
348 | hnucleo_Spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
|
---|
349 | hnucleo_Spi.Init.CRCPolynomial = 7;
|
---|
350 | hnucleo_Spi.Init.DataSize = SPI_DATASIZE_8BIT;
|
---|
351 | hnucleo_Spi.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
---|
352 | hnucleo_Spi.Init.NSS = SPI_NSS_SOFT;
|
---|
353 | hnucleo_Spi.Init.TIMode = SPI_TIMODE_DISABLED;
|
---|
354 | hnucleo_Spi.Init.Mode = SPI_MODE_MASTER;
|
---|
355 |
|
---|
356 | SPIx_MspInit(&hnucleo_Spi);
|
---|
357 | HAL_SPI_Init(&hnucleo_Spi);
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 | /**
|
---|
362 | * @brief SPI Read 4 bytes from device.
|
---|
363 | * @param None
|
---|
364 | * @retval Read data
|
---|
365 | */
|
---|
366 | static uint32_t SPIx_Read(void)
|
---|
367 | {
|
---|
368 | HAL_StatusTypeDef status = HAL_OK;
|
---|
369 | uint32_t readvalue = 0;
|
---|
370 | uint32_t writevalue = 0xFFFFFFFF;
|
---|
371 |
|
---|
372 | status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t*) &writevalue, (uint8_t*) &readvalue, 1, SpixTimeout);
|
---|
373 |
|
---|
374 | /* Check the communication status */
|
---|
375 | if(status != HAL_OK)
|
---|
376 | {
|
---|
377 | /* Execute user timeout callback */
|
---|
378 | SPIx_Error();
|
---|
379 | }
|
---|
380 |
|
---|
381 | return readvalue;
|
---|
382 | }
|
---|
383 |
|
---|
384 | /**
|
---|
385 | * @brief SPI Write a byte to device.
|
---|
386 | * @param Value: value to be written
|
---|
387 | * @retval None
|
---|
388 | */
|
---|
389 | static void SPIx_Write(uint8_t Value)
|
---|
390 | {
|
---|
391 | HAL_StatusTypeDef status = HAL_OK;
|
---|
392 |
|
---|
393 | status = HAL_SPI_Transmit(&hnucleo_Spi, (uint8_t*) &Value, 1, SpixTimeout);
|
---|
394 |
|
---|
395 | /* Check the communication status */
|
---|
396 | if(status != HAL_OK)
|
---|
397 | {
|
---|
398 | /* Execute user timeout callback */
|
---|
399 | SPIx_Error();
|
---|
400 | }
|
---|
401 | }
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * @brief SPI error treatment function.
|
---|
405 | * @param None
|
---|
406 | * @retval None
|
---|
407 | */
|
---|
408 | static void SPIx_Error (void)
|
---|
409 | {
|
---|
410 | /* De-initialize the SPI communication BUS */
|
---|
411 | HAL_SPI_DeInit(&hnucleo_Spi);
|
---|
412 |
|
---|
413 | /* Re-Initiaize the SPI communication BUS */
|
---|
414 | SPIx_Init();
|
---|
415 | }
|
---|
416 |
|
---|
417 | /******************************************************************************
|
---|
418 | LINK OPERATIONS
|
---|
419 | *******************************************************************************/
|
---|
420 |
|
---|
421 | /********************************* LINK SD ************************************/
|
---|
422 | /**
|
---|
423 | * @brief Initializes the SD Card and put it into StandBy State (Ready for
|
---|
424 | * data transfer).
|
---|
425 | * @param None
|
---|
426 | * @retval None
|
---|
427 | */
|
---|
428 | void SD_IO_Init(void)
|
---|
429 | {
|
---|
430 | GPIO_InitTypeDef GPIO_InitStruct;
|
---|
431 | uint8_t counter;
|
---|
432 |
|
---|
433 | /* SD_CS_GPIO Periph clock enable */
|
---|
434 | SD_CS_GPIO_CLK_ENABLE();
|
---|
435 |
|
---|
436 | /* Configure SD_CS_PIN pin: SD Card CS pin */
|
---|
437 | GPIO_InitStruct.Pin = SD_CS_PIN;
|
---|
438 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
---|
439 | GPIO_InitStruct.Pull = GPIO_PULLUP;
|
---|
440 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
|
---|
441 | HAL_GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStruct);
|
---|
442 |
|
---|
443 | /*------------Put SD in SPI mode--------------*/
|
---|
444 | /* SD SPI Config */
|
---|
445 | SPIx_Init();
|
---|
446 |
|
---|
447 | /* SD chip select high */
|
---|
448 | SD_CS_HIGH();
|
---|
449 |
|
---|
450 | /* Send dummy byte 0xFF, 10 times with CS high */
|
---|
451 | /* Rise CS and MOSI for 80 clocks cycles */
|
---|
452 | for (counter = 0; counter <= 9; counter++)
|
---|
453 | {
|
---|
454 | /* Send dummy byte 0xFF */
|
---|
455 | SD_IO_WriteByte(SD_DUMMY_BYTE);
|
---|
456 | }
|
---|
457 | }
|
---|
458 |
|
---|
459 | /**
|
---|
460 | * @brief Writes a byte on the SD.
|
---|
461 | * @param Data: byte to send.
|
---|
462 | * @retval None
|
---|
463 | */
|
---|
464 | void SD_IO_WriteByte(uint8_t Data)
|
---|
465 | {
|
---|
466 | /* Send the byte */
|
---|
467 | SPIx_Write(Data);
|
---|
468 | }
|
---|
469 |
|
---|
470 | /**
|
---|
471 | * @brief Reads a byte from the SD.
|
---|
472 | * @param None
|
---|
473 | * @retval The received byte.
|
---|
474 | */
|
---|
475 | uint8_t SD_IO_ReadByte(void)
|
---|
476 | {
|
---|
477 | uint8_t data = 0;
|
---|
478 |
|
---|
479 | /* Get the received data */
|
---|
480 | data = SPIx_Read();
|
---|
481 |
|
---|
482 | /* Return the shifted data */
|
---|
483 | return data;
|
---|
484 | }
|
---|
485 |
|
---|
486 | /**
|
---|
487 | * @brief Sends 5 bytes command to the SD card and get response.
|
---|
488 | * @param Cmd: The user expected command to send to SD card.
|
---|
489 | * @param Arg: The command argument
|
---|
490 | * @param Crc: The CRC
|
---|
491 | * @param Response: Expected response from the SD card
|
---|
492 | * @retval HAL_StatusTypeDef HAL Status
|
---|
493 | */
|
---|
494 | HAL_StatusTypeDef SD_IO_WriteCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Response)
|
---|
495 | {
|
---|
496 | uint32_t counter = 0x00;
|
---|
497 | uint8_t frame[6];
|
---|
498 |
|
---|
499 | /* Prepare Frame to send */
|
---|
500 | frame[0] = (Cmd | 0x40); /* Construct byte 1 */
|
---|
501 | frame[1] = (uint8_t)(Arg >> 24); /* Construct byte 2 */
|
---|
502 | frame[2] = (uint8_t)(Arg >> 16); /* Construct byte 3 */
|
---|
503 | frame[3] = (uint8_t)(Arg >> 8); /* Construct byte 4 */
|
---|
504 | frame[4] = (uint8_t)(Arg); /* Construct byte 5 */
|
---|
505 | frame[5] = (Crc); /* Construct byte 6 */
|
---|
506 |
|
---|
507 | /* SD chip select low */
|
---|
508 | SD_CS_LOW();
|
---|
509 |
|
---|
510 | /* Send Frame */
|
---|
511 | for (counter = 0; counter < 6; counter++)
|
---|
512 | {
|
---|
513 | SD_IO_WriteByte(frame[counter]); /* Send the Cmd bytes */
|
---|
514 | }
|
---|
515 |
|
---|
516 | if(Response != SD_NO_RESPONSE_EXPECTED)
|
---|
517 | {
|
---|
518 | return SD_IO_WaitResponse(Response);
|
---|
519 | }
|
---|
520 |
|
---|
521 | return HAL_OK;
|
---|
522 | }
|
---|
523 |
|
---|
524 | /**
|
---|
525 | * @brief Waits response from the SD card
|
---|
526 | * @param Response: Expected response from the SD card
|
---|
527 | * @retval HAL_StatusTypeDef HAL Status
|
---|
528 | */
|
---|
529 | HAL_StatusTypeDef SD_IO_WaitResponse(uint8_t Response)
|
---|
530 | {
|
---|
531 | uint32_t timeout = 0xFFFF;
|
---|
532 |
|
---|
533 | /* Check if response is got or a timeout is happen */
|
---|
534 | while ((SD_IO_ReadByte() != Response) && timeout)
|
---|
535 | {
|
---|
536 | timeout--;
|
---|
537 | }
|
---|
538 |
|
---|
539 | if (timeout == 0)
|
---|
540 | {
|
---|
541 | /* After time out */
|
---|
542 | return HAL_TIMEOUT;
|
---|
543 | }
|
---|
544 | else
|
---|
545 | {
|
---|
546 | /* Right response got */
|
---|
547 | return HAL_OK;
|
---|
548 | }
|
---|
549 | }
|
---|
550 |
|
---|
551 | /**
|
---|
552 | * @brief Sends dummy byte with CS High.
|
---|
553 | * @param None
|
---|
554 | * @retval None
|
---|
555 | */
|
---|
556 | void SD_IO_WriteDummy(void)
|
---|
557 | {
|
---|
558 | /* SD chip select high */
|
---|
559 | SD_CS_HIGH();
|
---|
560 |
|
---|
561 | /* Send Dummy byte 0xFF */
|
---|
562 | SD_IO_WriteByte(SD_DUMMY_BYTE);
|
---|
563 | }
|
---|
564 |
|
---|
565 | /********************************* LINK LCD ***********************************/
|
---|
566 | /**
|
---|
567 | * @brief Initializes the LCD.
|
---|
568 | * @param None
|
---|
569 | * @retval None
|
---|
570 | */
|
---|
571 | void LCD_IO_Init(void)
|
---|
572 | {
|
---|
573 | GPIO_InitTypeDef GPIO_InitStruct;
|
---|
574 |
|
---|
575 | /* LCD_CS_GPIO and LCD_DC_GPIO Periph clock enable */
|
---|
576 | LCD_CS_GPIO_CLK_ENABLE();
|
---|
577 | LCD_DC_GPIO_CLK_ENABLE();
|
---|
578 |
|
---|
579 | /* Configure LCD_CS_PIN pin: LCD Card CS pin */
|
---|
580 | GPIO_InitStruct.Pin = LCD_CS_PIN;
|
---|
581 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
---|
582 | GPIO_InitStruct.Pull = GPIO_NOPULL;
|
---|
583 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
|
---|
584 | HAL_GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStruct);
|
---|
585 |
|
---|
586 | /* Configure LCD_DC_PIN pin: LCD Card DC pin */
|
---|
587 | GPIO_InitStruct.Pin = LCD_DC_PIN;
|
---|
588 | HAL_GPIO_Init(LCD_DC_GPIO_PORT, &GPIO_InitStruct);
|
---|
589 |
|
---|
590 | /* LCD chip select high */
|
---|
591 | LCD_CS_HIGH();
|
---|
592 |
|
---|
593 | /* LCD SPI Config */
|
---|
594 | SPIx_Init();
|
---|
595 | }
|
---|
596 |
|
---|
597 | /**
|
---|
598 | * @brief Writes command to select the LCD register.
|
---|
599 | * @param LCDReg: Address of the selected register.
|
---|
600 | * @retval None
|
---|
601 | */
|
---|
602 | void LCD_IO_WriteReg(uint8_t LCDReg)
|
---|
603 | {
|
---|
604 | /* Reset LCD control line CS */
|
---|
605 | LCD_CS_LOW();
|
---|
606 |
|
---|
607 | /* Set LCD data/command line DC to Low */
|
---|
608 | LCD_DC_LOW();
|
---|
609 |
|
---|
610 | /* Send Command */
|
---|
611 | SPIx_Write(LCDReg);
|
---|
612 |
|
---|
613 | /* Deselect : Chip Select high */
|
---|
614 | LCD_CS_HIGH();
|
---|
615 | }
|
---|
616 |
|
---|
617 | /**
|
---|
618 | * @brief Writes data to select the LCD register.
|
---|
619 | * This function must be used after st7735_WriteReg() function
|
---|
620 | * @param Data: data to write to the selected register.
|
---|
621 | * @retval None
|
---|
622 | */
|
---|
623 | void LCD_IO_WriteData(uint8_t Data)
|
---|
624 | {
|
---|
625 | /* Reset LCD control line CS */
|
---|
626 | LCD_CS_LOW();
|
---|
627 |
|
---|
628 | /* Set LCD data/command line DC to High */
|
---|
629 | LCD_DC_HIGH();
|
---|
630 |
|
---|
631 | /* Send Data */
|
---|
632 | SPIx_Write(Data);
|
---|
633 |
|
---|
634 | /* Deselect : Chip Select high */
|
---|
635 | LCD_CS_HIGH();
|
---|
636 | }
|
---|
637 |
|
---|
638 | /**
|
---|
639 | * @brief Writes register value.
|
---|
640 | * @param pData: Pointer on the register value
|
---|
641 | * @param Size: Size of byte to transmit to the register
|
---|
642 | * @retval None
|
---|
643 | */
|
---|
644 | void LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size)
|
---|
645 | {
|
---|
646 | uint32_t counter = 0;
|
---|
647 |
|
---|
648 | /* Reset LCD control line CS */
|
---|
649 | LCD_CS_LOW();
|
---|
650 |
|
---|
651 | /* Set LCD data/command line DC to High */
|
---|
652 | LCD_DC_HIGH();
|
---|
653 |
|
---|
654 | if (Size == 1)
|
---|
655 | {
|
---|
656 | /* Only 1 byte to be sent to LCD - general interface can be used */
|
---|
657 | /* Send Data */
|
---|
658 | SPIx_Write(*pData);
|
---|
659 | }
|
---|
660 | else
|
---|
661 | {
|
---|
662 | /* Several data should be sent in a raw */
|
---|
663 | /* Direct SPI accesses for optimization */
|
---|
664 | for (counter = Size; counter != 0; counter--)
|
---|
665 | {
|
---|
666 | while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE)
|
---|
667 | {
|
---|
668 | }
|
---|
669 | /* Need to invert bytes for LCD*/
|
---|
670 | *((__IO uint8_t*)&hnucleo_Spi.Instance->DR) = *(pData+1);
|
---|
671 |
|
---|
672 | while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE)
|
---|
673 | {
|
---|
674 | }
|
---|
675 | *((__IO uint8_t*)&hnucleo_Spi.Instance->DR) = *pData;
|
---|
676 | counter--;
|
---|
677 | pData += 2;
|
---|
678 | }
|
---|
679 |
|
---|
680 | /* Wait until the bus is ready before releasing Chip select */
|
---|
681 | while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_BSY) != RESET)
|
---|
682 | {
|
---|
683 | }
|
---|
684 | }
|
---|
685 |
|
---|
686 | /* Deselect : Chip Select high */
|
---|
687 | LCD_CS_HIGH();
|
---|
688 | }
|
---|
689 |
|
---|
690 | /**
|
---|
691 | * @brief Wait for loop in ms.
|
---|
692 | * @param Delay in ms.
|
---|
693 | * @retval None
|
---|
694 | */
|
---|
695 | void LCD_Delay(uint32_t Delay)
|
---|
696 | {
|
---|
697 | HAL_Delay(Delay);
|
---|
698 | }
|
---|
699 |
|
---|
700 | /******************************* LINK JOYSTICK ********************************/
|
---|
701 | /**
|
---|
702 | * @brief Initializes ADC MSP.
|
---|
703 | * @param None
|
---|
704 | * @retval None
|
---|
705 | */
|
---|
706 | static void ADCx_MspInit(ADC_HandleTypeDef *hadc)
|
---|
707 | {
|
---|
708 | GPIO_InitTypeDef GPIO_InitStruct;
|
---|
709 |
|
---|
710 | /*** Configure the GPIOs ***/
|
---|
711 | /* Enable GPIO clock */
|
---|
712 | NUCLEO_ADCx_GPIO_CLK_ENABLE();
|
---|
713 |
|
---|
714 | /* Configure the selected ADC Channel as analog input */
|
---|
715 | GPIO_InitStruct.Pin = NUCLEO_ADCx_GPIO_PIN ;
|
---|
716 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
---|
717 | GPIO_InitStruct.Pull = GPIO_NOPULL;
|
---|
718 | HAL_GPIO_Init(NUCLEO_ADCx_GPIO_PORT, &GPIO_InitStruct);
|
---|
719 |
|
---|
720 | /*** Configure the ADC peripheral ***/
|
---|
721 | /* Enable ADC clock */
|
---|
722 | NUCLEO_ADCx_CLK_ENABLE();
|
---|
723 | }
|
---|
724 |
|
---|
725 | /**
|
---|
726 | * @brief Initializes ADC HAL.
|
---|
727 | * @param None
|
---|
728 | * @retval None
|
---|
729 | */
|
---|
730 | static void ADCx_Init(void)
|
---|
731 | {
|
---|
732 | if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_RESET)
|
---|
733 | {
|
---|
734 | /* ADC Config */
|
---|
735 | hnucleo_Adc.Instance = NUCLEO_ADCx;
|
---|
736 | hnucleo_Adc.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4; /* (must not exceed 36MHz) */
|
---|
737 | hnucleo_Adc.Init.Resolution = ADC_RESOLUTION12b;
|
---|
738 | hnucleo_Adc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
---|
739 | hnucleo_Adc.Init.ContinuousConvMode = DISABLE;
|
---|
740 | hnucleo_Adc.Init.DiscontinuousConvMode = DISABLE;
|
---|
741 | hnucleo_Adc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
---|
742 | hnucleo_Adc.Init.EOCSelection = EOC_SINGLE_CONV;
|
---|
743 | hnucleo_Adc.Init.NbrOfConversion = 1;
|
---|
744 | hnucleo_Adc.Init.DMAContinuousRequests = DISABLE;
|
---|
745 |
|
---|
746 | ADCx_MspInit(&hnucleo_Adc);
|
---|
747 | HAL_ADC_Init(&hnucleo_Adc);
|
---|
748 | }
|
---|
749 | }
|
---|
750 |
|
---|
751 | /**
|
---|
752 | * @brief Configures joystick available on adafruit 1.8" TFT shield
|
---|
753 | * managed through ADC to detect motion.
|
---|
754 | * @param None
|
---|
755 | * @retval Joystickstatus (0=> success, 1=> fail)
|
---|
756 | */
|
---|
757 | uint8_t BSP_JOY_Init(void)
|
---|
758 | {
|
---|
759 | uint8_t status = 1;
|
---|
760 |
|
---|
761 | ADCx_Init();
|
---|
762 |
|
---|
763 | /* Select the ADC Channel to be converted */
|
---|
764 | sConfig.Channel = NUCLEO_ADCx_CHANNEL;
|
---|
765 | sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
|
---|
766 | sConfig.Rank = 1;
|
---|
767 | status = HAL_ADC_ConfigChannel(&hnucleo_Adc, &sConfig);
|
---|
768 |
|
---|
769 | /* Return Joystick initialization status */
|
---|
770 | return status;
|
---|
771 | }
|
---|
772 |
|
---|
773 | /**
|
---|
774 | * @brief Returns the Joystick key pressed.
|
---|
775 | * @note To know which Joystick key is pressed we need to detect the voltage
|
---|
776 | * level on each key output
|
---|
777 | * - None : 3.3 V / 4095
|
---|
778 | * - SEL : 1.055 V / 1308
|
---|
779 | * - DOWN : 0.71 V / 88
|
---|
780 | * - LEFT : 3.0 V / 3720
|
---|
781 | * - RIGHT : 0.595 V / 737
|
---|
782 | * - UP : 1.65 V / 2046
|
---|
783 | * @retval JOYState_TypeDef: Code of the Joystick key pressed.
|
---|
784 | */
|
---|
785 | JOYState_TypeDef BSP_JOY_GetState(void)
|
---|
786 | {
|
---|
787 | JOYState_TypeDef state;
|
---|
788 | uint16_t keyconvertedvalue = 0;
|
---|
789 |
|
---|
790 | /* Start the conversion process */
|
---|
791 | HAL_ADC_Start(&hnucleo_Adc);
|
---|
792 |
|
---|
793 | /* Wait for the end of conversion */
|
---|
794 | HAL_ADC_PollForConversion(&hnucleo_Adc, 10);
|
---|
795 |
|
---|
796 | /* Check if the continuous conversion of regular channel is finished */
|
---|
797 | if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_EOC_REG)
|
---|
798 | {
|
---|
799 | /* Get the converted value of regular channel */
|
---|
800 | keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc);
|
---|
801 | }
|
---|
802 |
|
---|
803 | if((keyconvertedvalue > 2010) && (keyconvertedvalue < 2090))
|
---|
804 | {
|
---|
805 | state = JOY_UP;
|
---|
806 | }
|
---|
807 | else if((keyconvertedvalue > 680) && (keyconvertedvalue < 780))
|
---|
808 | {
|
---|
809 | state = JOY_RIGHT;
|
---|
810 | }
|
---|
811 | else if((keyconvertedvalue > 1270) && (keyconvertedvalue < 1350))
|
---|
812 | {
|
---|
813 | state = JOY_SEL;
|
---|
814 | }
|
---|
815 | else if((keyconvertedvalue > 50) && (keyconvertedvalue < 130))
|
---|
816 | {
|
---|
817 | state = JOY_DOWN;
|
---|
818 | }
|
---|
819 | else if((keyconvertedvalue > 3680) && (keyconvertedvalue < 3760))
|
---|
820 | {
|
---|
821 | state = JOY_LEFT;
|
---|
822 | }
|
---|
823 | else
|
---|
824 | {
|
---|
825 | state = JOY_NONE;
|
---|
826 | }
|
---|
827 |
|
---|
828 | /* Loop while a key is pressed */
|
---|
829 | if(state != JOY_NONE)
|
---|
830 | {
|
---|
831 | keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc);
|
---|
832 | }
|
---|
833 | /* Return the code of the Joystick key pressed */
|
---|
834 | return state;
|
---|
835 | }
|
---|
836 |
|
---|
837 | /**
|
---|
838 | * @}
|
---|
839 | */
|
---|
840 |
|
---|
841 | /**
|
---|
842 | * @}
|
---|
843 | */
|
---|
844 |
|
---|
845 | /**
|
---|
846 | * @}
|
---|
847 | */
|
---|
848 |
|
---|
849 | /**
|
---|
850 | * @}
|
---|
851 | */
|
---|
852 |
|
---|
853 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
---|
854 | #endif
|
---|