You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32f4xx_nucleo.c 24KB

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