stm32l0xx_nucleo.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_nucleo.c
  4. * @author MCD Application Team
  5. * @brief This file provides set of firmware functions to manage:
  6. * - LEDs and push-button available on STM32L0XX-Nucleo Kit
  7. * from STMicroelectronics
  8. * - LCD, joystick and microSD available on Adafruit 1.8" TFT LCD
  9. * shield (reference ID 802)
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * Copyright (c) 2016 STMicroelectronics.
  14. * All rights reserved.
  15. *
  16. * This software is licensed under terms that can be found in the LICENSE file
  17. * in the root directory of this software component.
  18. * If no LICENSE file comes with this software, it is provided AS-IS.
  19. *
  20. ******************************************************************************
  21. */
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "stm32l0xx_nucleo.h"
  24. /** @addtogroup BSP
  25. * @{
  26. */
  27. /** @addtogroup STM32L0XX_NUCLEO
  28. * @{
  29. */
  30. /** @addtogroup STM32L0XX_NUCLEO_LOW_LEVEL
  31. * @brief This file provides set of firmware functions to manage Leds and push-button
  32. * available on STM32L0XX-Nucleo Kit from STMicroelectronics.
  33. * @{
  34. */
  35. /** @defgroup STM32L0XX_NUCLEO_LOW_LEVEL_Private_TypesDefinitions Private Types Definitions
  36. * @{
  37. */
  38. /**
  39. * @}
  40. */
  41. /** @defgroup STM32L0XX_NUCLEO_LOW_LEVEL_Private_Defines Private Defines
  42. * @{
  43. */
  44. /**
  45. * @brief STM32L0XX NUCLEO BSP Driver version number
  46. */
  47. #define __STM32L0XX_NUCLEO_BSP_VERSION_MAIN (0x02) /*!< [31:24] main version */
  48. #define __STM32L0XX_NUCLEO_BSP_VERSION_SUB1 (0x01) /*!< [23:16] sub1 version */
  49. #define __STM32L0XX_NUCLEO_BSP_VERSION_SUB2 (0x04) /*!< [15:8] sub2 version */
  50. #define __STM32L0XX_NUCLEO_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */
  51. #define __STM32L0XX_NUCLEO_BSP_VERSION ((__STM32L0XX_NUCLEO_BSP_VERSION_MAIN << 24)\
  52. |(__STM32L0XX_NUCLEO_BSP_VERSION_SUB1 << 16)\
  53. |(__STM32L0XX_NUCLEO_BSP_VERSION_SUB2 << 8 )\
  54. |(__STM32L0XX_NUCLEO_BSP_VERSION_RC))
  55. /**
  56. * @brief LINK SD Card
  57. */
  58. #define SD_DUMMY_BYTE 0xFF
  59. #define SD_NO_RESPONSE_EXPECTED 0x80
  60. /**
  61. * @}
  62. */
  63. /** @defgroup STM32L0XX_NUCLEO_LOW_LEVEL_Private_Variables Private Variables
  64. * @{
  65. */
  66. GPIO_TypeDef* LED_PORT[LEDn] = {LED2_GPIO_PORT};
  67. const uint16_t LED_PIN[LEDn] = {LED2_PIN};
  68. GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT };
  69. const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN };
  70. const uint8_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn };
  71. /**
  72. * @brief BUS variables
  73. */
  74. #ifdef HAL_SPI_MODULE_ENABLED
  75. uint32_t SpixTimeout = NUCLEO_SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */
  76. static SPI_HandleTypeDef hnucleo_Spi;
  77. #endif /* HAL_SPI_MODULE_ENABLED */
  78. #ifdef HAL_ADC_MODULE_ENABLED
  79. static ADC_HandleTypeDef hnucleo_Adc;
  80. /* ADC channel configuration structure declaration */
  81. static ADC_ChannelConfTypeDef sConfig;
  82. #endif /* HAL_ADC_MODULE_ENABLED */
  83. /**
  84. * @}
  85. */
  86. /** @defgroup STM32L0XX_NUCLEO_LOW_LEVEL_Private_FunctionPrototypes Private Function Prototypes
  87. * @{
  88. */
  89. #ifdef HAL_SPI_MODULE_ENABLED
  90. static void SPIx_Init(void);
  91. static void SPIx_Write(uint8_t Value);
  92. static void SPIx_WriteData(uint8_t *DataIn, uint16_t DataLength);
  93. static void SPIx_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLegnth);
  94. static void SPIx_Error (void);
  95. static void SPIx_MspInit(void);
  96. /* SD IO functions */
  97. void SD_IO_Init(void);
  98. void SD_IO_CSState(uint8_t state);
  99. void SD_IO_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength);
  100. void SD_IO_ReadData(uint8_t *DataOut, uint16_t DataLength);
  101. void SD_IO_WriteData(const uint8_t *Data, uint16_t DataLength);
  102. uint8_t SD_IO_WriteByte(uint8_t Data);
  103. uint8_t SD_IO_ReadByte(void);
  104. /* LCD IO functions */
  105. void LCD_IO_Init(void);
  106. void LCD_IO_WriteData(uint8_t Data);
  107. void LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size);
  108. void LCD_IO_WriteReg(uint8_t LCDReg);
  109. void LCD_Delay(uint32_t delay);
  110. #endif /* HAL_SPI_MODULE_ENABLED */
  111. #ifdef HAL_ADC_MODULE_ENABLED
  112. static HAL_StatusTypeDef ADCx_Init(void);
  113. static void ADCx_DeInit(void);
  114. static void ADCx_MspInit(ADC_HandleTypeDef *hadc);
  115. static void ADCx_MspDeInit(ADC_HandleTypeDef *hadc);
  116. #endif /* HAL_ADC_MODULE_ENABLED */
  117. /**
  118. * @}
  119. */
  120. /** @defgroup STM32L0XX_NUCLEO_LOW_LEVEL_Private_Functions Private Functions
  121. * @{
  122. */
  123. /**
  124. * @brief This method returns the STM32L0XX NUCLEO BSP Driver revision
  125. * @retval version : 0xXYZR (8bits for each decimal, R for RC)
  126. */
  127. uint32_t BSP_GetVersion(void)
  128. {
  129. return __STM32L0XX_NUCLEO_BSP_VERSION;
  130. }
  131. /**
  132. * @brief Configures LED GPIO.
  133. * @param Led: Led to be configured.
  134. * This parameter can be one of the following values:
  135. * @arg LED2
  136. * @retval None
  137. */
  138. void BSP_LED_Init(Led_TypeDef Led)
  139. {
  140. GPIO_InitTypeDef gpioinitstruct;
  141. /* Enable the GPIO_LED Clock */
  142. LEDx_GPIO_CLK_ENABLE(Led);
  143. /* Configure the GPIO_LED pin */
  144. gpioinitstruct.Pin = LED_PIN[Led];
  145. gpioinitstruct.Mode = GPIO_MODE_OUTPUT_PP;
  146. gpioinitstruct.Pull = GPIO_NOPULL;
  147. gpioinitstruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  148. HAL_GPIO_Init(LED_PORT[Led], &gpioinitstruct);
  149. /* Reset PIN to switch off the LED */
  150. HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET);
  151. }
  152. /**
  153. * @brief DeInit LEDs.
  154. * @param Led: LED to be de-init.
  155. * This parameter can be one of the following values:
  156. * @arg LED2
  157. * @note Led DeInit does not disable the GPIO clock nor disable the Mfx
  158. * @retval None
  159. */
  160. void BSP_LED_DeInit(Led_TypeDef Led)
  161. {
  162. GPIO_InitTypeDef gpio_init_structure;
  163. /* Turn off LED */
  164. HAL_GPIO_WritePin(LED_PORT[Led],LED_PIN[Led], GPIO_PIN_RESET);
  165. /* DeInit the GPIO_LED pin */
  166. gpio_init_structure.Pin = LED_PIN[Led];
  167. HAL_GPIO_DeInit(LED_PORT[Led], gpio_init_structure.Pin);
  168. }
  169. /**
  170. * @brief Turns selected LED On.
  171. * @param Led: Specifies the Led to be set on.
  172. * This parameter can be one of following parameters:
  173. * @arg LED2
  174. * @retval None
  175. */
  176. void BSP_LED_On(Led_TypeDef Led)
  177. {
  178. HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_SET);
  179. }
  180. /**
  181. * @brief Turns selected LED Off.
  182. * @param Led: Specifies the Led to be set off.
  183. * This parameter can be one of following parameters:
  184. * @arg LED2
  185. * @retval None
  186. */
  187. void BSP_LED_Off(Led_TypeDef Led)
  188. {
  189. HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET);
  190. }
  191. /**
  192. * @brief Toggles the selected LED.
  193. * @param Led: Specifies the Led to be toggled.
  194. * This parameter can be one of following parameters:
  195. * @arg LED2
  196. * @retval None
  197. */
  198. void BSP_LED_Toggle(Led_TypeDef Led)
  199. {
  200. HAL_GPIO_TogglePin(LED_PORT[Led], LED_PIN[Led]);
  201. }
  202. /**
  203. * @brief Configures Button GPIO and EXTI Line.
  204. * @param Button: Specifies the Button to be configured.
  205. * This parameter should be: BUTTON_USER
  206. * @param ButtonMode: Specifies Button mode.
  207. * This parameter can be one of following parameters:
  208. * @arg BUTTON_MODE_GPIO: Button will be used as simple IO
  209. * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
  210. * generation capability
  211. * @retval None
  212. */
  213. void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
  214. {
  215. GPIO_InitTypeDef gpioinitstruct;
  216. /* Enable the BUTTON Clock */
  217. BUTTONx_GPIO_CLK_ENABLE(Button);
  218. gpioinitstruct.Pin = BUTTON_PIN[Button];
  219. gpioinitstruct.Pull = GPIO_NOPULL;
  220. gpioinitstruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  221. if(ButtonMode == BUTTON_MODE_GPIO)
  222. {
  223. /* Configure Button pin as input */
  224. gpioinitstruct.Mode = GPIO_MODE_INPUT;
  225. HAL_GPIO_Init(BUTTON_PORT[Button], &gpioinitstruct);
  226. }
  227. if(ButtonMode == BUTTON_MODE_EXTI)
  228. {
  229. /* Configure Button pin as input with External interrupt */
  230. gpioinitstruct.Mode = GPIO_MODE_IT_FALLING;
  231. HAL_GPIO_Init(BUTTON_PORT[Button], &gpioinitstruct);
  232. /* Enable and set Button EXTI Interrupt to the lowest priority */
  233. HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0);
  234. HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
  235. }
  236. }
  237. /**
  238. * @brief Push Button DeInit.
  239. * @param Button: Button to be configured
  240. * This parameter should be: BUTTON_USER
  241. * @note PB DeInit does not disable the GPIO clock
  242. * @retval None
  243. */
  244. void BSP_PB_DeInit(Button_TypeDef Button)
  245. {
  246. GPIO_InitTypeDef gpio_init_structure;
  247. gpio_init_structure.Pin = BUTTON_PIN[Button];
  248. HAL_NVIC_DisableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
  249. HAL_GPIO_DeInit(BUTTON_PORT[Button], gpio_init_structure.Pin);
  250. }
  251. /**
  252. * @brief Returns the selected Button state.
  253. * @param Button: Specifies the Button to be checked.
  254. * This parameter should be: BUTTON_USER
  255. * @retval Button state.
  256. */
  257. uint32_t BSP_PB_GetState(Button_TypeDef Button)
  258. {
  259. return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
  260. }
  261. #ifdef HAL_SPI_MODULE_ENABLED
  262. /******************************************************************************
  263. BUS OPERATIONS
  264. *******************************************************************************/
  265. /**
  266. * @brief Initialize SPI MSP.
  267. * @retval None
  268. */
  269. static void SPIx_MspInit(void)
  270. {
  271. GPIO_InitTypeDef gpioinitstruct = {0};
  272. /*** Configure the GPIOs ***/
  273. /* Enable GPIO clock */
  274. NUCLEO_SPIx_SCK_GPIO_CLK_ENABLE();
  275. NUCLEO_SPIx_MISO_MOSI_GPIO_CLK_ENABLE();
  276. /* Configure SPI SCK */
  277. gpioinitstruct.Pin = NUCLEO_SPIx_SCK_PIN;
  278. gpioinitstruct.Mode = GPIO_MODE_AF_PP;
  279. gpioinitstruct.Pull = GPIO_PULLUP;
  280. gpioinitstruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  281. gpioinitstruct.Alternate = NUCLEO_SPIx_SCK_AF;
  282. HAL_GPIO_Init(NUCLEO_SPIx_SCK_GPIO_PORT, &gpioinitstruct);
  283. /* Configure SPI MISO and MOSI */
  284. gpioinitstruct.Pin = NUCLEO_SPIx_MOSI_PIN;
  285. gpioinitstruct.Alternate = NUCLEO_SPIx_MISO_MOSI_AF;
  286. gpioinitstruct.Pull = GPIO_PULLDOWN;
  287. HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &gpioinitstruct);
  288. gpioinitstruct.Pin = NUCLEO_SPIx_MISO_PIN;
  289. HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &gpioinitstruct);
  290. /*** Configure the SPI peripheral ***/
  291. /* Enable SPI clock */
  292. NUCLEO_SPIx_CLK_ENABLE();
  293. }
  294. /**
  295. * @brief Initialize SPI HAL.
  296. * @retval None
  297. */
  298. static void SPIx_Init(void)
  299. {
  300. if(HAL_SPI_GetState(&hnucleo_Spi) == HAL_SPI_STATE_RESET)
  301. {
  302. /* SPI Config */
  303. hnucleo_Spi.Instance = NUCLEO_SPIx;
  304. /* SPI baudrate is set to 8 MHz maximum (PCLK2/SPI_BaudRatePrescaler = 32/4 = 8 MHz)
  305. to verify these constraints:
  306. - ST7735 LCD SPI interface max baudrate is 15MHz for write and 6.66MHz for read
  307. Since the provided driver doesn't use read capability from LCD, only constraint
  308. on write baudrate is considered.
  309. - SD card SPI interface max baudrate is 25MHz for write/read
  310. - PCLK2 max frequency is 32 MHz
  311. */
  312. hnucleo_Spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  313. hnucleo_Spi.Init.Direction = SPI_DIRECTION_2LINES;
  314. hnucleo_Spi.Init.CLKPhase = SPI_PHASE_1EDGE;
  315. hnucleo_Spi.Init.CLKPolarity = SPI_POLARITY_LOW;
  316. hnucleo_Spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  317. hnucleo_Spi.Init.CRCPolynomial = 7;
  318. hnucleo_Spi.Init.DataSize = SPI_DATASIZE_8BIT;
  319. hnucleo_Spi.Init.FirstBit = SPI_FIRSTBIT_MSB;
  320. hnucleo_Spi.Init.NSS = SPI_NSS_SOFT;
  321. hnucleo_Spi.Init.TIMode = SPI_TIMODE_DISABLE;
  322. hnucleo_Spi.Init.Mode = SPI_MODE_MASTER;
  323. SPIx_MspInit();
  324. HAL_SPI_Init(&hnucleo_Spi);
  325. }
  326. }
  327. /**
  328. * @brief SPI Write a byte to device
  329. * @param DataIn: value to be written
  330. * @param DataOut: read value
  331. * @param DataLength: number of bytes to write
  332. * @retval None
  333. */
  334. static void SPIx_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength)
  335. {
  336. HAL_StatusTypeDef status = HAL_OK;
  337. status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t*) DataIn, DataOut, DataLength, SpixTimeout);
  338. /* Check the communication status */
  339. if(status != HAL_OK)
  340. {
  341. /* Execute user timeout callback */
  342. SPIx_Error();
  343. }
  344. }
  345. /**
  346. * @brief SPI Write an amount of data to device
  347. * @param DataIn: value to be written
  348. * @param DataLength: number of bytes to write
  349. * @retval None
  350. */
  351. static void SPIx_WriteData(uint8_t *DataIn, uint16_t DataLength)
  352. {
  353. HAL_StatusTypeDef status = HAL_OK;
  354. status = HAL_SPI_Transmit(&hnucleo_Spi, DataIn, DataLength, SpixTimeout);
  355. /* Check the communication status */
  356. if(status != HAL_OK)
  357. {
  358. /* Execute user timeout callback */
  359. SPIx_Error();
  360. }
  361. }
  362. /**
  363. * @brief SPI Write a byte to device
  364. * @param Value: value to be written
  365. * @retval None
  366. */
  367. static void SPIx_Write(uint8_t Value)
  368. {
  369. HAL_StatusTypeDef status = HAL_OK;
  370. uint8_t data;
  371. status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t*) &Value, &data, 1, SpixTimeout);
  372. /* Check the communication status */
  373. if(status != HAL_OK)
  374. {
  375. /* Execute user timeout callback */
  376. SPIx_Error();
  377. }
  378. }
  379. /**
  380. * @brief SPI error treatment function
  381. * @retval None
  382. */
  383. static void SPIx_Error (void)
  384. {
  385. /* De-initialize the SPI communication BUS */
  386. HAL_SPI_DeInit(&hnucleo_Spi);
  387. /* Re-Initiaize the SPI communication BUS */
  388. SPIx_Init();
  389. }
  390. /******************************************************************************
  391. LINK OPERATIONS
  392. *******************************************************************************/
  393. /********************************* LINK SD ************************************/
  394. /**
  395. * @brief Initialize the SD Card and put it into StandBy State (Ready for
  396. * data transfer).
  397. * @retval None
  398. */
  399. void SD_IO_Init(void)
  400. {
  401. GPIO_InitTypeDef gpioinitstruct = {0};
  402. uint8_t counter = 0;
  403. /* SD_CS_GPIO Periph clock enable */
  404. SD_CS_GPIO_CLK_ENABLE();
  405. /* Configure SD_CS_PIN pin: SD Card CS pin */
  406. gpioinitstruct.Pin = SD_CS_PIN;
  407. gpioinitstruct.Mode = GPIO_MODE_OUTPUT_PP;
  408. gpioinitstruct.Pull = GPIO_PULLUP;
  409. gpioinitstruct.Speed = GPIO_SPEED_FREQ_HIGH;
  410. HAL_GPIO_Init(SD_CS_GPIO_PORT, &gpioinitstruct);
  411. /* Configure LCD_CS_PIN pin: LCD Card CS pin */
  412. gpioinitstruct.Pin = LCD_CS_PIN;
  413. gpioinitstruct.Mode = GPIO_MODE_OUTPUT_PP;
  414. gpioinitstruct.Pull = GPIO_NOPULL;
  415. gpioinitstruct.Speed = GPIO_SPEED_FREQ_HIGH;
  416. HAL_GPIO_Init(SD_CS_GPIO_PORT, &gpioinitstruct);
  417. LCD_CS_HIGH();
  418. /*------------Put SD in SPI mode--------------*/
  419. /* SD SPI Config */
  420. SPIx_Init();
  421. /* SD chip select high */
  422. SD_CS_HIGH();
  423. /* Send dummy byte 0xFF, 10 times with CS high */
  424. /* Rise CS and MOSI for 80 clocks cycles */
  425. for (counter = 0; counter <= 9; counter++)
  426. {
  427. /* Send dummy byte 0xFF */
  428. SD_IO_WriteByte(SD_DUMMY_BYTE);
  429. }
  430. }
  431. /**
  432. * @brief Set the SD_CS pin.
  433. * @param val: pin value.
  434. * @retval None
  435. */
  436. void SD_IO_CSState(uint8_t val)
  437. {
  438. if(val == 1)
  439. {
  440. SD_CS_HIGH();
  441. }
  442. else
  443. {
  444. SD_CS_LOW();
  445. }
  446. }
  447. /**
  448. * @brief Write byte(s) on the SD
  449. * @param DataIn: Pointer to data buffer to write
  450. * @param DataOut: Pointer to data buffer for read data
  451. * @param DataLength: number of bytes to write
  452. * @retval None
  453. */
  454. void SD_IO_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength)
  455. {
  456. /* Send the byte */
  457. SPIx_WriteReadData(DataIn, DataOut, DataLength);
  458. }
  459. /**
  460. * @brief Write a byte on the SD.
  461. * @param Data: byte to send.
  462. * @retval Data written
  463. */
  464. uint8_t SD_IO_WriteByte(uint8_t Data)
  465. {
  466. uint8_t tmp;
  467. /* Send the byte */
  468. SPIx_WriteReadData(&Data,&tmp,1);
  469. return tmp;
  470. }
  471. /**
  472. * @brief Write an amount of data on the SD.
  473. * @param DataOut: byte to send.
  474. * @param DataLength: number of bytes to write
  475. * @retval none
  476. */
  477. void SD_IO_ReadData(uint8_t *DataOut, uint16_t DataLength)
  478. {
  479. /* Send the byte */
  480. SD_IO_WriteReadData(DataOut, DataOut, DataLength);
  481. }
  482. /**
  483. * @brief Write an amount of data on the SD.
  484. * @param Data: byte to send.
  485. * @param DataLength: number of bytes to write
  486. * @retval none
  487. */
  488. void SD_IO_WriteData(const uint8_t *Data, uint16_t DataLength)
  489. {
  490. /* Send the byte */
  491. SPIx_WriteData((uint8_t *)Data, DataLength);
  492. }
  493. /********************************* LINK LCD ***********************************/
  494. /**
  495. * @brief Initialize the LCD
  496. * @retval None
  497. */
  498. void LCD_IO_Init(void)
  499. {
  500. GPIO_InitTypeDef gpioinitstruct = {0};
  501. /* LCD_CS_GPIO and LCD_DC_GPIO Periph clock enable */
  502. LCD_CS_GPIO_CLK_ENABLE();
  503. LCD_DC_GPIO_CLK_ENABLE();
  504. /* Configure LCD_CS_PIN pin: LCD Card CS pin */
  505. gpioinitstruct.Pin = LCD_CS_PIN;
  506. gpioinitstruct.Mode = GPIO_MODE_OUTPUT_PP;
  507. gpioinitstruct.Pull = GPIO_NOPULL;
  508. gpioinitstruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  509. HAL_GPIO_Init(SD_CS_GPIO_PORT, &gpioinitstruct);
  510. /* Configure LCD_DC_PIN pin: LCD Card DC pin */
  511. gpioinitstruct.Pin = LCD_DC_PIN;
  512. HAL_GPIO_Init(LCD_DC_GPIO_PORT, &gpioinitstruct);
  513. /* LCD chip select high */
  514. LCD_CS_HIGH();
  515. /* LCD SPI Config */
  516. SPIx_Init();
  517. }
  518. /**
  519. * @brief Write command to select the LCD register.
  520. * @param LCDReg: Address of the selected register.
  521. * @retval None
  522. */
  523. void LCD_IO_WriteReg(uint8_t LCDReg)
  524. {
  525. /* Reset LCD control line CS */
  526. LCD_CS_LOW();
  527. /* Set LCD data/command line DC to Low */
  528. LCD_DC_LOW();
  529. /* Send Command */
  530. SPIx_Write(LCDReg);
  531. /* Deselect : Chip Select high */
  532. LCD_CS_HIGH();
  533. }
  534. /**
  535. * @brief Writes data to select the LCD register.
  536. * This function must be used after st7735_WriteReg() function
  537. * @param Data: data to write to the selected register.
  538. * @retval None
  539. */
  540. void LCD_IO_WriteData(uint8_t Data)
  541. {
  542. /* Reset LCD control line CS */
  543. LCD_CS_LOW();
  544. /* Set LCD data/command line DC to High */
  545. LCD_DC_HIGH();
  546. /* Send Data */
  547. SPIx_Write(Data);
  548. /* Deselect : Chip Select high */
  549. LCD_CS_HIGH();
  550. }
  551. /**
  552. * @brief Write register value.
  553. * @param pData Pointer on the register value
  554. * @param Size Size of byte to transmit to the register
  555. * @retval None
  556. */
  557. void LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size)
  558. {
  559. uint32_t counter = 0;
  560. __IO uint32_t data = 0;
  561. /* Reset LCD control line CS */
  562. LCD_CS_LOW();
  563. /* Set LCD data/command line DC to High */
  564. LCD_DC_HIGH();
  565. if (Size == 1)
  566. {
  567. /* Only 1 byte to be sent to LCD - general interface can be used */
  568. /* Send Data */
  569. SPIx_Write(*pData);
  570. }
  571. else
  572. {
  573. /* Several data should be sent in a raw */
  574. /* Direct SPI accesses for optimization */
  575. for (counter = Size; counter != 0; counter--)
  576. {
  577. while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE)
  578. {
  579. }
  580. /* Need to invert bytes for LCD*/
  581. *((__IO uint8_t*)&hnucleo_Spi.Instance->DR) = *(pData+1);
  582. while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE)
  583. {
  584. }
  585. *((__IO uint8_t*)&hnucleo_Spi.Instance->DR) = *pData;
  586. counter--;
  587. pData += 2;
  588. }
  589. /* Wait until the bus is ready before releasing Chip select */
  590. while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_BSY) != RESET)
  591. {
  592. }
  593. }
  594. /* Empty the Rx fifo */
  595. data = *(&hnucleo_Spi.Instance->DR);
  596. UNUSED(data); /* Remove GNU warning */
  597. /* Deselect : Chip Select high */
  598. LCD_CS_HIGH();
  599. }
  600. /**
  601. * @brief Wait for loop in ms.
  602. * @param Delay in ms.
  603. * @retval None
  604. */
  605. void LCD_Delay(uint32_t Delay)
  606. {
  607. HAL_Delay(Delay);
  608. }
  609. #endif /* HAL_SPI_MODULE_ENABLED */
  610. /******************************* LINK JOYSTICK ********************************/
  611. #ifdef HAL_ADC_MODULE_ENABLED
  612. /**
  613. * @brief Initialize ADC MSP.
  614. * @retval None
  615. */
  616. static void ADCx_MspInit(ADC_HandleTypeDef *hadc)
  617. {
  618. GPIO_InitTypeDef gpioinitstruct = {0};
  619. /*** Configure the GPIOs ***/
  620. /* Enable GPIO clock */
  621. NUCLEO_ADCx_GPIO_CLK_ENABLE();
  622. /* Configure ADC1 Channel8 as analog input */
  623. gpioinitstruct.Pin = NUCLEO_ADCx_GPIO_PIN ;
  624. gpioinitstruct.Mode = GPIO_MODE_ANALOG;
  625. gpioinitstruct.Pull = GPIO_NOPULL;
  626. HAL_GPIO_Init(NUCLEO_ADCx_GPIO_PORT, &gpioinitstruct);
  627. /*** Configure the ADC peripheral ***/
  628. /* Enable ADC clock */
  629. NUCLEO_ADCx_CLK_ENABLE();
  630. }
  631. /**
  632. * @brief DeInitializes ADC MSP.
  633. * @param hadc: ADC peripheral
  634. * @note ADC DeInit does not disable the GPIO clock
  635. * @retval None
  636. */
  637. static void ADCx_MspDeInit(ADC_HandleTypeDef *hadc)
  638. {
  639. GPIO_InitTypeDef gpioinitstruct;
  640. /*** DeInit the ADC peripheral ***/
  641. /* Disable ADC clock */
  642. NUCLEO_ADCx_CLK_DISABLE();
  643. /* Configure the selected ADC Channel as analog input */
  644. gpioinitstruct.Pin = NUCLEO_ADCx_GPIO_PIN ;
  645. HAL_GPIO_DeInit(NUCLEO_ADCx_GPIO_PORT, gpioinitstruct.Pin);
  646. /* Disable GPIO clock has to be done by the application*/
  647. /* NUCLEO_ADCx_GPIO_CLK_DISABLE(); */
  648. }
  649. /**
  650. * @brief Initializes ADC HAL.
  651. * @retval None
  652. */
  653. static HAL_StatusTypeDef ADCx_Init(void)
  654. {
  655. /* Set ADC instance */
  656. hnucleo_Adc.Instance = NUCLEO_ADCx;
  657. if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_RESET)
  658. {
  659. /* ADC Config */
  660. hnucleo_Adc.Instance = NUCLEO_ADCx;
  661. hnucleo_Adc.Init.OversamplingMode = DISABLE;
  662. hnucleo_Adc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; /* (must not exceed 16MHz) */
  663. hnucleo_Adc.Init.LowPowerAutoPowerOff = DISABLE;
  664. hnucleo_Adc.Init.LowPowerFrequencyMode = ENABLE;
  665. hnucleo_Adc.Init.LowPowerAutoWait = ENABLE;
  666. hnucleo_Adc.Init.Resolution = ADC_RESOLUTION_12B;
  667. hnucleo_Adc.Init.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  668. hnucleo_Adc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
  669. hnucleo_Adc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  670. hnucleo_Adc.Init.ContinuousConvMode = DISABLE;
  671. hnucleo_Adc.Init.DiscontinuousConvMode = DISABLE;
  672. hnucleo_Adc.Init.ExternalTrigConv = ADC_SOFTWARE_START; /* Trig of conversion start done manually by software, without external event */
  673. hnucleo_Adc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because trig by software start */
  674. hnucleo_Adc.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  675. hnucleo_Adc.Init.DMAContinuousRequests = DISABLE;
  676. /* Initialize MSP related to ADC */
  677. ADCx_MspInit(&hnucleo_Adc);
  678. /* Initialize ADC */
  679. if (HAL_ADC_Init(&hnucleo_Adc) != HAL_OK)
  680. {
  681. return HAL_ERROR;
  682. }
  683. if (HAL_ADCEx_Calibration_Start(&hnucleo_Adc,ADC_SINGLE_ENDED) != HAL_OK)
  684. {
  685. return HAL_ERROR;
  686. }
  687. }
  688. return HAL_OK;
  689. }
  690. /**
  691. * @brief Initializes ADC HAL.
  692. * @retval None
  693. */
  694. static void ADCx_DeInit(void)
  695. {
  696. hnucleo_Adc.Instance = NUCLEO_ADCx;
  697. HAL_ADC_DeInit(&hnucleo_Adc);
  698. ADCx_MspDeInit(&hnucleo_Adc);
  699. }
  700. /******************************* LINK JOYSTICK ********************************/
  701. /**
  702. * @brief Configures joystick available on adafruit 1.8" TFT shield
  703. * managed through ADC to detect motion.
  704. * @retval Joystickstatus (0=> success, 1=> fail)
  705. */
  706. uint8_t BSP_JOY_Init(void)
  707. {
  708. if (ADCx_Init() != HAL_OK)
  709. {
  710. return (uint8_t) HAL_ERROR;
  711. }
  712. /* Select Channel 8 to be converted */
  713. sConfig.Channel = ADC_CHANNEL_8;
  714. sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  715. /* Return Joystick initialization status */
  716. return (uint8_t) HAL_ADC_ConfigChannel(&hnucleo_Adc, &sConfig);
  717. }
  718. /**
  719. * @brief DeInit joystick GPIOs.
  720. * @note JOY DeInit does not disable the Mfx, just set the Mfx pins in Off mode
  721. * @retval None.
  722. */
  723. void BSP_JOY_DeInit(void)
  724. {
  725. ADCx_DeInit();
  726. }
  727. /**
  728. * @brief Returns the Joystick key pressed.
  729. * @note To know which Joystick key is pressed we need to detect the voltage
  730. * level on each key output
  731. * - None : 3.3 V / 4095
  732. * - SEL : 1.055 V / 1308
  733. * - DOWN : 0.71 V / 88
  734. * - LEFT : 3.0 V / 3720
  735. * - RIGHT : 0.595 V / 737
  736. * - UP : 1.65 V / 2046
  737. * @retval JOYState_TypeDef: Code of the Joystick key pressed.
  738. */
  739. JOYState_TypeDef BSP_JOY_GetState(void)
  740. {
  741. JOYState_TypeDef state = JOY_NONE;
  742. uint16_t keyconvertedvalue = 0;
  743. /* Start the conversion process */
  744. HAL_ADC_Start(&hnucleo_Adc);
  745. /* Wait for the end of conversion */
  746. if (HAL_ADC_PollForConversion(&hnucleo_Adc, 10) != HAL_TIMEOUT)
  747. {
  748. /* Get the converted value of regular channel */
  749. keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc);
  750. }
  751. if((keyconvertedvalue > 2010) && (keyconvertedvalue < 2090))
  752. {
  753. state = JOY_UP;
  754. }
  755. else if((keyconvertedvalue > 680) && (keyconvertedvalue < 780))
  756. {
  757. state = JOY_RIGHT;
  758. }
  759. else if((keyconvertedvalue > 1270) && (keyconvertedvalue < 1350))
  760. {
  761. state = JOY_SEL;
  762. }
  763. else if((keyconvertedvalue > 50) && (keyconvertedvalue < 130))
  764. {
  765. state = JOY_DOWN;
  766. }
  767. else if((keyconvertedvalue > 3570) && (keyconvertedvalue < 3800))
  768. {
  769. state = JOY_LEFT;
  770. }
  771. else
  772. {
  773. state = JOY_NONE;
  774. }
  775. /* Return the code of the Joystick key pressed */
  776. return state;
  777. }
  778. #endif /* HAL_ADC_MODULE_ENABLED */
  779. /**
  780. * @}
  781. */
  782. /**
  783. * @}
  784. */
  785. /**
  786. * @}
  787. */
  788. /**
  789. * @}
  790. */