stm32l4xx_nucleo.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_nucleo.c
  4. * @author MCD Application Team
  5. * @version V2.1.0
  6. * @date 16-June-2017
  7. * @brief This file provides set of firmware functions to manage:
  8. * - LEDs and push-button available on STM32L4XX-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>&copy; 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. /* Includes ------------------------------------------------------------------*/
  42. #include "stm32l4xx_nucleo.h"
  43. /** @addtogroup BSP
  44. * @{
  45. */
  46. /** @defgroup STM32L4XX_NUCLEO STM32L476RG-Nucleo
  47. * @brief This file provides set of firmware functions to manage Leds and push-button
  48. * available on STM32L4XX-Nucleo Kit from STMicroelectronics.
  49. * It provides also LCD, joystick and uSD functions to communicate with
  50. * Adafruit 1.8" TFT LCD shield (reference ID 802)
  51. * @{
  52. */
  53. /** @defgroup STM32L4XX_NUCLEO_Private_Defines Private Defines
  54. * @{
  55. */
  56. /**
  57. * @brief STM32L4xx NUCLEO BSP Driver version V2.1.0
  58. */
  59. #define __STM32L4XX_NUCLEO_BSP_VERSION_MAIN (0x02) /*!< [31:24] main version */
  60. #define __STM32L4XX_NUCLEO_BSP_VERSION_SUB1 (0x01) /*!< [23:16] sub1 version */
  61. #define __STM32L4XX_NUCLEO_BSP_VERSION_SUB2 (0x00) /*!< [15:8] sub2 version */
  62. #define __STM32L4XX_NUCLEO_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */
  63. #define __STM32L4XX_NUCLEO_BSP_VERSION ((__STM32L4XX_NUCLEO_BSP_VERSION_MAIN << 24)\
  64. |(__STM32L4XX_NUCLEO_BSP_VERSION_SUB1 << 16)\
  65. |(__STM32L4XX_NUCLEO_BSP_VERSION_SUB2 << 8 )\
  66. |(__STM32L4XX_NUCLEO_BSP_VERSION_RC))
  67. /**
  68. * @brief LINK SD Card
  69. */
  70. #define SD_DUMMY_BYTE 0xFF
  71. #define SD_NO_RESPONSE_EXPECTED 0x80
  72. /**
  73. * @}
  74. */
  75. #ifdef USE_STM32L4XX_NUCLEO_64_SMPS
  76. /**
  77. * @brief SMPS
  78. */
  79. #ifdef USE_ADP5301ACBZ /* ADP5301ACBZ */
  80. /* ######################################################################## */
  81. /* #define PORT_SMPS GPIOA */
  82. /* #define PIN_SMPS_ENABLE GPIO_PIN_4 */
  83. /* #define PIN_SMPS_POWERGOOD GPIO_PIN_6 */
  84. /* #define PIN_SMPS_SWITCH_ENABLE GPIO_PIN_7 */
  85. /* IN CASE OF SMPS VOLTAGE RANGE SELECTION */
  86. /* #define PIN_SMPS_V1 GPIO_PIN_5 */
  87. /* ######################################################################## */
  88. #endif /* ADP5301ACBZ */
  89. /**
  90. * @}
  91. */
  92. #endif /* USE_STM32L4XX_NUCLEO_64_SMPS */
  93. /** @defgroup STM32L4XX_NUCLEO_Private_Variables Exported Variables
  94. * @{
  95. */
  96. #ifdef USE_STM32L4XX_NUCLEO_64_SMPS
  97. GPIO_TypeDef* GPIO_PORT[LEDn] = {LED4_GPIO_PORT};
  98. const uint16_t GPIO_PIN[LEDn] = {LED4_PIN};
  99. #else
  100. GPIO_TypeDef* GPIO_PORT[LEDn] = {LED2_GPIO_PORT};
  101. const uint16_t GPIO_PIN[LEDn] = {LED2_PIN};
  102. #endif
  103. GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT};
  104. const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN};
  105. const uint16_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn};
  106. /**
  107. * @brief BUS variables
  108. */
  109. #ifdef HAL_SPI_MODULE_ENABLED
  110. uint32_t hnucleo_SpixTimeout = NUCLEO_SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */
  111. static SPI_HandleTypeDef hnucleo_Spi;
  112. #endif /* HAL_SPI_MODULE_ENABLED */
  113. #ifdef HAL_ADC_MODULE_ENABLED
  114. static ADC_HandleTypeDef hnucleo_Adc;
  115. /* ADC channel configuration structure declaration */
  116. static ADC_ChannelConfTypeDef hnucleo_AdcChannelConfig;
  117. #endif /* HAL_ADC_MODULE_ENABLED */
  118. /**
  119. * @}
  120. */
  121. /** @defgroup STM32L4XX_NUCLEO_Private_Functions Private Functions
  122. * @{
  123. */
  124. #ifdef HAL_SPI_MODULE_ENABLED
  125. static void SPIx_Init(void);
  126. static void SPIx_Write(uint8_t Value);
  127. static void SPIx_Error (void);
  128. static void SPIx_MspInit(void);
  129. #endif /* HAL_SPI_MODULE_ENABLED */
  130. #ifdef HAL_ADC_MODULE_ENABLED
  131. static HAL_StatusTypeDef ADCx_Init(void);
  132. static void ADCx_MspInit(ADC_HandleTypeDef *hadc);
  133. #endif /* HAL_ADC_MODULE_ENABLED */
  134. #ifdef HAL_SPI_MODULE_ENABLED
  135. /* SD IO functions */
  136. void SD_IO_Init(void);
  137. void SD_IO_CSState(uint8_t state);
  138. void SD_IO_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength);
  139. uint8_t SD_IO_WriteByte(uint8_t Data);
  140. /* LCD IO functions */
  141. void LCD_IO_Init(void);
  142. void LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size);
  143. void LCD_IO_WriteReg(uint8_t LCDReg);
  144. void LCD_Delay(uint32_t delay);
  145. #endif /* HAL_SPI_MODULE_ENABLED */
  146. /**
  147. * @}
  148. */
  149. /** @defgroup STM32L4XX_NUCLEO_Exported_Functions Exported Functions
  150. * @{
  151. */
  152. /**
  153. * @brief This method returns the STM32L4XX NUCLEO BSP Driver revision
  154. * @retval version : 0xXYZR (8bits for each decimal, R for RC)
  155. */
  156. uint32_t BSP_GetVersion(void)
  157. {
  158. return __STM32L4XX_NUCLEO_BSP_VERSION;
  159. }
  160. /** @defgroup STM32L4XX_NUCLEO_LED_Functions LED Functions
  161. * @{
  162. */
  163. /**
  164. * @brief Configures LED GPIO.
  165. * @param Led: LED to be configured.
  166. * This parameter can be one of the following values:
  167. * @arg LED2 or LED4 on Nucleo-64 with external SMPS
  168. * @retval None
  169. */
  170. void BSP_LED_Init(Led_TypeDef Led)
  171. {
  172. GPIO_InitTypeDef gpioinitstruct = {0};
  173. /* Enable the GPIO_LED Clock */
  174. LEDx_GPIO_CLK_ENABLE(Led);
  175. /* Configure the GPIO_LED pin */
  176. gpioinitstruct.Pin = GPIO_PIN[Led];
  177. gpioinitstruct.Mode = GPIO_MODE_OUTPUT_PP;
  178. gpioinitstruct.Pull = GPIO_NOPULL;
  179. gpioinitstruct.Speed = GPIO_SPEED_FREQ_HIGH;
  180. HAL_GPIO_Init(GPIO_PORT[Led], &gpioinitstruct);
  181. }
  182. /**
  183. * @brief Turns selected LED On.
  184. * @param Led: Specifies the Led to be set on.
  185. * This parameter can be one of following parameters:
  186. * @arg LED2 or LED4 on Nucleo-64 with external SMPS
  187. * @retval None
  188. */
  189. void BSP_LED_On(Led_TypeDef Led)
  190. {
  191. HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
  192. }
  193. /**
  194. * @brief Turns selected LED Off.
  195. * @param Led: Specifies the Led to be set off.
  196. * This parameter can be one of following parameters:
  197. * @arg LED2 or LED4 on Nucleo-64 with external SMPS
  198. * @retval None
  199. */
  200. void BSP_LED_Off(Led_TypeDef Led)
  201. {
  202. HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
  203. }
  204. /**
  205. * @brief Toggles the selected LED.
  206. * @param Led: Specifies the Led to be toggled.
  207. * This parameter can be one of following parameters:
  208. * @arg LED2 or LED4 on Nucleo-64 with external SMPS
  209. * @retval None
  210. */
  211. void BSP_LED_Toggle(Led_TypeDef Led)
  212. {
  213. HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
  214. }
  215. /**
  216. * @}
  217. */
  218. /** @defgroup STM32L4XX_NUCLEO_BUTTON_Functions BUTTON Functions
  219. * @{
  220. */
  221. /**
  222. * @brief Configures Button GPIO and EXTI Line.
  223. * @param Button: Specifies the Button to be configured.
  224. * This parameter should be: BUTTON_USER
  225. * @param ButtonMode: Specifies Button mode.
  226. * This parameter can be one of following parameters:
  227. * @arg BUTTON_MODE_GPIO: Button will be used as simple IO
  228. * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
  229. * generation capability
  230. * @retval None
  231. */
  232. void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
  233. {
  234. GPIO_InitTypeDef gpioinitstruct = {0};
  235. /* Enable the BUTTON Clock */
  236. BUTTONx_GPIO_CLK_ENABLE(Button);
  237. if (ButtonMode == BUTTON_MODE_GPIO)
  238. {
  239. /* Configure Button pin as input */
  240. gpioinitstruct.Pin = BUTTON_PIN[Button];
  241. gpioinitstruct.Mode = GPIO_MODE_INPUT;
  242. gpioinitstruct.Pull = GPIO_NOPULL;
  243. gpioinitstruct.Speed = GPIO_SPEED_FREQ_HIGH;
  244. HAL_GPIO_Init(BUTTON_PORT[Button], &gpioinitstruct);
  245. }
  246. else if(ButtonMode == BUTTON_MODE_EXTI)
  247. {
  248. /* Configure Button pin as input with External interrupt */
  249. gpioinitstruct.Pin = BUTTON_PIN[Button];
  250. gpioinitstruct.Mode = GPIO_MODE_IT_FALLING;
  251. gpioinitstruct.Pull = GPIO_NOPULL;
  252. gpioinitstruct.Speed = GPIO_SPEED_FREQ_HIGH;
  253. HAL_GPIO_Init(BUTTON_PORT[Button], &gpioinitstruct);
  254. /* Enable and set Button EXTI Interrupt to the lowest priority */
  255. HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0);
  256. HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
  257. }
  258. }
  259. /**
  260. * @brief Returns the selected Button state.
  261. * @param Button: Specifies the Button to be checked.
  262. * This parameter should be: BUTTON_USER
  263. * @retval Button state.
  264. */
  265. uint32_t BSP_PB_GetState(Button_TypeDef Button)
  266. {
  267. return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
  268. }
  269. #ifdef USE_STM32L4XX_NUCLEO_64_SMPS
  270. #ifdef USE_ADP5301ACBZ /* ADP5301ACBZ */
  271. /******************************************************************************
  272. SMPS OPERATIONS
  273. *******************************************************************************/
  274. /**
  275. * @brief DeInitialize the external SMPS component
  276. * @param None
  277. * @note Low power consumption GPIO settings
  278. * @retval SMPS status
  279. */
  280. uint32_t BSP_SMPS_DeInit(void)
  281. {
  282. GPIO_InitTypeDef GPIO_InitStruct;
  283. PWR_AND_CLK_SMPS();
  284. /* -------------------------------------------------------------------------------------- */
  285. /* Added for Deinit if No PIN_SMPS_ENABLE & PIN_SMPS_SWITCH_ENABLE are not disabled before */
  286. /* Disable SMPS SWITCH */
  287. HAL_GPIO_WritePin(PORT_SMPS, PIN_SMPS_SWITCH_ENABLE, GPIO_PIN_RESET);
  288. HAL_Delay(1);
  289. /* Disable SMPS */
  290. HAL_GPIO_WritePin(PORT_SMPS, PIN_SMPS_ENABLE, GPIO_PIN_RESET);
  291. /* -------------------------------------------------------------------------------------- */
  292. /* Set all GPIO in output push/pull pulldown state to reduce power consumption */
  293. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  294. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  295. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  296. /* Consider all SMPS pins but V1, not used on ADP5301ACBZ */
  297. GPIO_InitStruct.Pin = PIN_SMPS_ENABLE | PIN_SMPS_SWITCH_ENABLE | PIN_SMPS_POWERGOOD;
  298. HAL_GPIO_Init(PORT_SMPS, &GPIO_InitStruct);
  299. return SMPS_OK;
  300. }
  301. /**
  302. * @brief Initialize the external SMPS component
  303. * @param VoltageRange: Select operating SMPS supply
  304. * @arg DCDC_AND_BOARD_DEPENDENT
  305. * @note VoltageRange is not used with all boards. When not
  306. * used, resort to PWR_REGULATOR_VOLTAGE_SCALE2 by default.
  307. * @retval SMPS status
  308. */
  309. uint32_t BSP_SMPS_Init(uint32_t VoltageRange)
  310. {
  311. PWR_AND_CLK_SMPS();
  312. GPIO_InitTypeDef GPIO_InitStruct;
  313. /* Reconfigure PWR_PUCRx/PDCRx registers only when not coming */
  314. /* back from Standby or Shutdown states. */
  315. /* Consider as well non-SMPS related pins. */
  316. if(!(READ_BIT(PWR->PWR_PU_REG, PWR_GPIO_ENABLE)))
  317. {
  318. HAL_PWREx_EnableGPIOPullDown (PWR_GPIO_SMPS, PWR_GPIO_SWITCH_ENABLE);
  319. HAL_PWREx_EnableGPIOPullDown (PWR_GPIO_SMPS, PWR_GPIO_ENABLE);
  320. /* HW limitation: Level shifter consumes because of dangling, so pull PA2 up
  321. (LPUART1_TX), PA13 (SWD/TMS) and PB3 (SWO) */
  322. HAL_PWREx_EnableGPIOPullUp(PWR_GPIO_A,GPIO_PIN_2); /* LPUART1_TX */
  323. HAL_PWREx_EnableGPIOPullUp(PWR_GPIO_A,GPIO_PIN_13); /* SWD/TMS */
  324. HAL_PWREx_EnableGPIOPullUp(PWR_GPIO_B,GPIO_PIN_3); /* SWO */
  325. /* Don't set PWR_CR3 APC bit at this time as it increases power
  326. consumption in non-Standby/Shutdown modes. It will have to be
  327. set with HAL_PWREx_EnablePullUpPullDownConfig() API upon
  328. Standby or Shutdown modes entering */
  329. }
  330. /* ------------------------------------------------------------------------ */
  331. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  332. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  333. GPIO_InitStruct.Pull = GPIO_PULLUP;
  334. GPIO_InitStruct.Pin = PIN_SMPS_POWERGOOD;
  335. HAL_GPIO_Init(PORT_SMPS, &GPIO_InitStruct);
  336. /* ------------------------------------------------------------------------ */
  337. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  338. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  339. GPIO_InitStruct.Pull = GPIO_NOPULL;
  340. GPIO_InitStruct.Pin = PIN_SMPS_ENABLE | PIN_SMPS_SWITCH_ENABLE;
  341. HAL_GPIO_Init(PORT_SMPS, &GPIO_InitStruct);
  342. /* --------- SMPS VOLTAGE RANGE SELECTION ----------------------------------*/
  343. /* ######################################################################## */
  344. /* - > Not applicable to ADP5301ACBZ on MB1319 */
  345. /* ######################################################################## */
  346. /* - > Applicable to ST1PS02D1QTR */
  347. /* Control to be added */
  348. /* ST1PS02D1QTR on MB1312 */
  349. /* if (VoltageRange == ST1PS02D1QTR_VOUT_1_25) */
  350. /* HAL_GPIO_WritePin(PORT_SMPS, PIN_SMPS_V1, GPIO_PIN_SET); */
  351. /* 1.25V */
  352. /* D0/D1/D2 = H/L/L */
  353. /* else */
  354. /* */
  355. /* ST1PS02D1QTR on MB1312 */
  356. /* ST1PS02D1QTR_VOUT_1_05 */
  357. /* 1.05V */
  358. /* D0/D1/D2 = L/L/L */
  359. /* HAL_GPIO_WritePin(PORT_SMPS, PIN_SMPS_V1, GPIO_PIN_RESET); */
  360. /* ######################################################################## */
  361. return SMPS_OK;
  362. }
  363. /**
  364. * @brief Enable the external SMPS component
  365. * @param Delay: delay in ms after enable
  366. * @param Power_Good_Check: Enable Power good check
  367. * @note Power_Good_Check is not used with all external
  368. * SMPS components
  369. * @retval SMPS status
  370. * @arg SMPS_OK: SMPS ENABLE OK
  371. * @arg SMPS_KO: POWER GOOD CHECK FAILS
  372. */
  373. uint32_t BSP_SMPS_Enable (uint32_t Delay, uint32_t Power_Good_Check)
  374. {
  375. PWR_AND_CLK_SMPS();
  376. HAL_GPIO_WritePin(PORT_SMPS, PIN_SMPS_ENABLE, GPIO_PIN_SET);
  377. /* Delay upon request */
  378. if (Delay != 0)
  379. {
  380. HAL_Delay(Delay);
  381. }
  382. /* CHECK POWER GOOD or NOT */
  383. if (Power_Good_Check != 0)
  384. {
  385. if (GPIO_PIN_RESET == (HAL_GPIO_ReadPin(PORT_SMPS, PIN_SMPS_POWERGOOD)))
  386. {
  387. /* POWER GOOD KO */
  388. return SMPS_KO;
  389. }
  390. }
  391. /* SMPS ENABLE */
  392. return SMPS_OK;
  393. }
  394. /**
  395. * @brief Disable the external SMPS component
  396. * @param NONE
  397. * @note SMPS SWITCH should be disabled first !
  398. * @retval SMPS status
  399. * @arg SMPS_OK: SMPS DISABLE OK - DONE
  400. * @arg SMPS_KO: POWER GOOD CHECK FAILS
  401. *
  402. */
  403. uint32_t BSP_SMPS_Disable (void)
  404. {
  405. PWR_AND_CLK_SMPS();
  406. /* Check if SMPS SWITCH is disabled */
  407. if (HAL_GPIO_ReadPin(PORT_SMPS, PIN_SMPS_SWITCH_ENABLE) != GPIO_PIN_RESET)
  408. {
  409. /* ERROR AS SWITCH SHOULD BE DISABLE */
  410. return SMPS_KO;
  411. }
  412. /* Disable SMPS */
  413. HAL_GPIO_WritePin(PORT_SMPS, PIN_SMPS_ENABLE, GPIO_PIN_RESET);
  414. /* SMPS DISABLE */
  415. return SMPS_OK;
  416. }
  417. /**
  418. * @brief Enable the external SMPS SWITCH component
  419. * @param Delay: delay in ms before SMPS SWITCH ENABLE
  420. * @param Power_Good_Check: Enable Power good check
  421. * @note Power_Good_Check is not used with all boards
  422. * @retval SMPS status
  423. * @arg SMPS_OK: SMPS ENABLE OK
  424. * @arg SMPS_KO: POWER GOOD CHECK FAILS
  425. */
  426. uint32_t BSP_SMPS_Supply_Enable (uint32_t Delay, uint32_t Power_Good_Check)
  427. {
  428. PWR_AND_CLK_SMPS();
  429. if (Delay != 0)
  430. {
  431. HAL_Delay(Delay);
  432. }
  433. /* CHECK POWER GOOD or NOT */
  434. if (Power_Good_Check != 0)
  435. {
  436. if (GPIO_PIN_RESET == (HAL_GPIO_ReadPin(PORT_SMPS, PIN_SMPS_POWERGOOD)))
  437. {
  438. /* POWER GOOD KO */
  439. return SMPS_KO;
  440. }
  441. }
  442. /* SMPS SWITCH ENABLE */
  443. HAL_GPIO_WritePin(PORT_SMPS, PIN_SMPS_SWITCH_ENABLE, GPIO_PIN_SET);
  444. return SMPS_OK;
  445. }
  446. /**
  447. * @brief Disable the external SMPS SWITCH component
  448. * @param None
  449. * @retval SMPS status
  450. * @arg SMPS_OK: SMPS SWITCH DISABLE OK
  451. */
  452. uint32_t BSP_SMPS_Supply_Disable (void)
  453. {
  454. PWR_AND_CLK_SMPS();
  455. /* SMPS SWITCH DISABLE */
  456. HAL_GPIO_WritePin(PORT_SMPS, PIN_SMPS_SWITCH_ENABLE, GPIO_PIN_RESET);
  457. return SMPS_OK;
  458. }
  459. #endif /* ADP5301ACBZ */
  460. #endif /* USE_STM32L4XX_NUCLEO_64_SMPS */
  461. #ifdef HAL_ADC_MODULE_ENABLED
  462. /**
  463. * @brief Configures joystick available on adafruit 1.8" TFT shield
  464. * managed through ADC to detect motion.
  465. * @retval Joystickstatus (0=> success, 1=> fail)
  466. */
  467. uint8_t BSP_JOY_Init(void)
  468. {
  469. if (ADCx_Init() != HAL_OK)
  470. {
  471. return (uint8_t) HAL_ERROR;
  472. }
  473. /* Select Channel 15 to be converted */
  474. hnucleo_AdcChannelConfig.Channel = ADC_CHANNEL_15;
  475. hnucleo_AdcChannelConfig.SamplingTime = ADC_SAMPLETIME_24CYCLES_5;
  476. hnucleo_AdcChannelConfig.Rank = 1;
  477. hnucleo_AdcChannelConfig.SingleDiff = ADC_SINGLE_ENDED;
  478. hnucleo_AdcChannelConfig.OffsetNumber = ADC_OFFSET_NONE;
  479. /* Return Joystick initialization status */
  480. return (uint8_t) HAL_ADC_ConfigChannel(&hnucleo_Adc, &hnucleo_AdcChannelConfig);
  481. }
  482. /**
  483. * @brief Returns the Joystick key pressed.
  484. * @note To know which Joystick key is pressed we need to detect the voltage
  485. * level on each key output
  486. * - None : 3.3 V / 4095
  487. * - SEL : 1.055 V / 1308
  488. * - DOWN : 0.71 V / 88
  489. * - LEFT : 3.0 V / 3720
  490. * - RIGHT : 0.595 V / 737
  491. * - UP : 1.65 V / 2046
  492. * @retval JOYState_TypeDef: Code of the Joystick key pressed.
  493. */
  494. JOYState_TypeDef BSP_JOY_GetState(void)
  495. {
  496. JOYState_TypeDef state = JOY_NONE;
  497. uint16_t keyconvertedvalue = 0;
  498. /* Start the conversion process */
  499. HAL_ADC_Start(&hnucleo_Adc);
  500. /* Wait for the end of conversion */
  501. HAL_ADC_PollForConversion(&hnucleo_Adc, 10);
  502. /* Check if the continous conversion of regular channel is finished */
  503. if(HAL_ADC_GetState(&hnucleo_Adc) & HAL_ADC_STATE_REG_EOC)
  504. {
  505. /* Get the converted value of regular channel */
  506. keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc);
  507. }
  508. if((keyconvertedvalue > 1980) && (keyconvertedvalue < 2120))
  509. {
  510. state = JOY_UP;
  511. }
  512. else if((keyconvertedvalue > 630) && (keyconvertedvalue < 830))
  513. {
  514. state = JOY_RIGHT;
  515. }
  516. else if((keyconvertedvalue > 1210) && (keyconvertedvalue < 1410))
  517. {
  518. state = JOY_SEL;
  519. }
  520. else if((keyconvertedvalue > 20) && (keyconvertedvalue < 160))
  521. {
  522. state = JOY_DOWN;
  523. }
  524. else if((keyconvertedvalue > 3620) && (keyconvertedvalue < 3820))
  525. {
  526. state = JOY_LEFT;
  527. }
  528. else
  529. {
  530. state = JOY_NONE;
  531. }
  532. /* Return the code of the Joystick key pressed*/
  533. return state;
  534. }
  535. #endif /* HAL_ADC_MODULE_ENABLED */
  536. /**
  537. * @}
  538. */
  539. /**
  540. * @}
  541. */
  542. /** @addtogroup STM32L4XX_NUCLEO_Private_Functions
  543. * @{
  544. */
  545. #ifdef HAL_SPI_MODULE_ENABLED
  546. /******************************************************************************
  547. BUS OPERATIONS
  548. *******************************************************************************/
  549. /**
  550. * @brief Initialize SPI MSP.
  551. * @retval None
  552. */
  553. static void SPIx_MspInit(void)
  554. {
  555. GPIO_InitTypeDef gpioinitstruct = {0};
  556. /*** Configure the GPIOs ***/
  557. /* Enable GPIO clock */
  558. NUCLEO_SPIx_SCK_GPIO_CLK_ENABLE();
  559. NUCLEO_SPIx_MISO_MOSI_GPIO_CLK_ENABLE();
  560. /* Configure SPI SCK */
  561. gpioinitstruct.Pin = NUCLEO_SPIx_SCK_PIN;
  562. gpioinitstruct.Mode = GPIO_MODE_AF_PP;
  563. gpioinitstruct.Pull = GPIO_PULLUP;
  564. gpioinitstruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  565. gpioinitstruct.Alternate = NUCLEO_SPIx_SCK_AF;
  566. HAL_GPIO_Init(NUCLEO_SPIx_SCK_GPIO_PORT, &gpioinitstruct);
  567. /* Configure SPI MISO and MOSI */
  568. gpioinitstruct.Pin = NUCLEO_SPIx_MOSI_PIN;
  569. gpioinitstruct.Alternate = NUCLEO_SPIx_MISO_MOSI_AF;
  570. gpioinitstruct.Pull = GPIO_PULLDOWN;
  571. HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &gpioinitstruct);
  572. gpioinitstruct.Pin = NUCLEO_SPIx_MISO_PIN;
  573. HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &gpioinitstruct);
  574. /*** Configure the SPI peripheral ***/
  575. /* Enable SPI clock */
  576. NUCLEO_SPIx_CLK_ENABLE();
  577. }
  578. /**
  579. * @brief Initialize SPI HAL.
  580. * @retval None
  581. */
  582. static void SPIx_Init(void)
  583. {
  584. if(HAL_SPI_GetState(&hnucleo_Spi) == HAL_SPI_STATE_RESET)
  585. {
  586. /* SPI Config */
  587. hnucleo_Spi.Instance = NUCLEO_SPIx;
  588. /* SPI baudrate is set to 8 MHz maximum (PCLK2/SPI_BaudRatePrescaler = 32/4 = 8 MHz)
  589. to verify these constraints:
  590. - ST7735 LCD SPI interface max baudrate is 15MHz for write and 6.66MHz for read
  591. Since the provided driver doesn't use read capability from LCD, only constraint
  592. on write baudrate is considered.
  593. - SD card SPI interface max baudrate is 25MHz for write/read
  594. - PCLK2 max frequency is 32 MHz
  595. */
  596. hnucleo_Spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  597. hnucleo_Spi.Init.Direction = SPI_DIRECTION_2LINES;
  598. hnucleo_Spi.Init.CLKPhase = SPI_PHASE_2EDGE;
  599. hnucleo_Spi.Init.CLKPolarity = SPI_POLARITY_HIGH;
  600. hnucleo_Spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  601. hnucleo_Spi.Init.CRCPolynomial = 7;
  602. hnucleo_Spi.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  603. hnucleo_Spi.Init.DataSize = SPI_DATASIZE_8BIT;
  604. hnucleo_Spi.Init.FirstBit = SPI_FIRSTBIT_MSB;
  605. hnucleo_Spi.Init.NSS = SPI_NSS_SOFT;
  606. hnucleo_Spi.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  607. hnucleo_Spi.Init.TIMode = SPI_TIMODE_DISABLE;
  608. hnucleo_Spi.Init.Mode = SPI_MODE_MASTER;
  609. SPIx_MspInit();
  610. HAL_SPI_Init(&hnucleo_Spi);
  611. }
  612. }
  613. /**
  614. * @brief SPI Write byte(s) to device
  615. * @param DataIn: Pointer to data buffer to write
  616. * @param DataOut: Pointer to data buffer for read data
  617. * @param DataLength: number of bytes to write
  618. * @retval None
  619. */
  620. static void SPIx_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength)
  621. {
  622. HAL_StatusTypeDef status = HAL_OK;
  623. status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t*) DataIn, DataOut, DataLength, hnucleo_SpixTimeout);
  624. /* Check the communication status */
  625. if(status != HAL_OK)
  626. {
  627. /* Execute user timeout callback */
  628. SPIx_Error();
  629. }
  630. }
  631. /**
  632. * @brief SPI Write a byte to device
  633. * @param Value: value to be written
  634. * @retval None
  635. */
  636. static void SPIx_Write(uint8_t Value)
  637. {
  638. HAL_StatusTypeDef status = HAL_OK;
  639. uint8_t data;
  640. status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t*) &Value, &data, 1, hnucleo_SpixTimeout);
  641. /* Check the communication status */
  642. if(status != HAL_OK)
  643. {
  644. /* Execute user timeout callback */
  645. SPIx_Error();
  646. }
  647. }
  648. /**
  649. * @brief SPI error treatment function
  650. * @retval None
  651. */
  652. static void SPIx_Error (void)
  653. {
  654. /* De-initialize the SPI communication BUS */
  655. HAL_SPI_DeInit(&hnucleo_Spi);
  656. /* Re-Initiaize the SPI communication BUS */
  657. SPIx_Init();
  658. }
  659. /******************************************************************************
  660. LINK OPERATIONS
  661. *******************************************************************************/
  662. /********************************* LINK SD ************************************/
  663. /**
  664. * @brief Initialize the SD Card and put it into StandBy State (Ready for
  665. * data transfer).
  666. * @retval None
  667. */
  668. void SD_IO_Init(void)
  669. {
  670. GPIO_InitTypeDef gpioinitstruct = {0};
  671. uint8_t counter = 0;
  672. /* SD_CS_GPIO Periph clock enable */
  673. SD_CS_GPIO_CLK_ENABLE();
  674. /* Configure SD_CS_PIN pin: SD Card CS pin */
  675. gpioinitstruct.Pin = SD_CS_PIN;
  676. gpioinitstruct.Mode = GPIO_MODE_OUTPUT_PP;
  677. gpioinitstruct.Pull = GPIO_PULLUP;
  678. gpioinitstruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  679. HAL_GPIO_Init(SD_CS_GPIO_PORT, &gpioinitstruct);
  680. /* Configure LCD_CS_PIN pin: LCD Card CS pin */
  681. gpioinitstruct.Pin = LCD_CS_PIN;
  682. gpioinitstruct.Mode = GPIO_MODE_OUTPUT_PP;
  683. gpioinitstruct.Pull = GPIO_NOPULL;
  684. gpioinitstruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  685. HAL_GPIO_Init(SD_CS_GPIO_PORT, &gpioinitstruct);
  686. LCD_CS_HIGH();
  687. /*------------Put SD in SPI mode--------------*/
  688. /* SD SPI Config */
  689. SPIx_Init();
  690. /* SD chip select high */
  691. SD_CS_HIGH();
  692. /* Send dummy byte 0xFF, 10 times with CS high */
  693. /* Rise CS and MOSI for 80 clocks cycles */
  694. for (counter = 0; counter <= 9; counter++)
  695. {
  696. /* Send dummy byte 0xFF */
  697. SD_IO_WriteByte(SD_DUMMY_BYTE);
  698. }
  699. }
  700. /**
  701. * @brief Set SD interface Chip Select state
  702. * @param val: 0 (low) or 1 (high) state
  703. * @retval None
  704. */
  705. void SD_IO_CSState(uint8_t val)
  706. {
  707. if(val == 1)
  708. {
  709. SD_CS_HIGH();
  710. }
  711. else
  712. {
  713. SD_CS_LOW();
  714. }
  715. }
  716. /**
  717. * @brief Write byte(s) on the SD
  718. * @param DataIn: Pointer to data buffer to write
  719. * @param DataOut: Pointer to data buffer for read data
  720. * @param DataLength: number of bytes to write
  721. * @retval None
  722. */
  723. void SD_IO_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength)
  724. {
  725. /* Send the byte */
  726. SPIx_WriteReadData(DataIn, DataOut, DataLength);
  727. }
  728. /**
  729. * @brief Write a byte on the SD.
  730. * @param Data: byte to send.
  731. * @retval Data written
  732. */
  733. uint8_t SD_IO_WriteByte(uint8_t Data)
  734. {
  735. uint8_t tmp;
  736. /* Send the byte */
  737. SPIx_WriteReadData(&Data,&tmp,1);
  738. return tmp;
  739. }
  740. /********************************* LINK LCD ***********************************/
  741. /**
  742. * @brief Initialize the LCD
  743. * @retval None
  744. */
  745. void LCD_IO_Init(void)
  746. {
  747. GPIO_InitTypeDef gpioinitstruct = {0};
  748. /* LCD_CS_GPIO and LCD_DC_GPIO Periph clock enable */
  749. LCD_CS_GPIO_CLK_ENABLE();
  750. LCD_DC_GPIO_CLK_ENABLE();
  751. /* Configure LCD_CS_PIN pin: LCD Card CS pin */
  752. gpioinitstruct.Pin = LCD_CS_PIN;
  753. gpioinitstruct.Mode = GPIO_MODE_OUTPUT_PP;
  754. gpioinitstruct.Pull = GPIO_NOPULL;
  755. gpioinitstruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  756. HAL_GPIO_Init(SD_CS_GPIO_PORT, &gpioinitstruct);
  757. /* Configure LCD_DC_PIN pin: LCD Card DC pin */
  758. gpioinitstruct.Pin = LCD_DC_PIN;
  759. HAL_GPIO_Init(LCD_DC_GPIO_PORT, &gpioinitstruct);
  760. /* LCD chip select high */
  761. LCD_CS_HIGH();
  762. /* LCD SPI Config */
  763. SPIx_Init();
  764. }
  765. /**
  766. * @brief Write command to select the LCD register.
  767. * @param LCDReg: Address of the selected register.
  768. * @retval None
  769. */
  770. void LCD_IO_WriteReg(uint8_t LCDReg)
  771. {
  772. /* Reset LCD control line CS */
  773. LCD_CS_LOW();
  774. /* Set LCD data/command line DC to Low */
  775. LCD_DC_LOW();
  776. /* Send Command */
  777. SPIx_Write(LCDReg);
  778. /* Deselect : Chip Select high */
  779. LCD_CS_HIGH();
  780. }
  781. /**
  782. * @brief Write register value.
  783. * @param pData Pointer on the register value
  784. * @param Size Size of byte to transmit to the register
  785. * @retval None
  786. */
  787. void LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size)
  788. {
  789. uint32_t counter = 0;
  790. __IO uint32_t data = 0;
  791. /* Reset LCD control line CS */
  792. LCD_CS_LOW();
  793. /* Set LCD data/command line DC to High */
  794. LCD_DC_HIGH();
  795. if (Size == 1)
  796. {
  797. /* Only 1 byte to be sent to LCD - general interface can be used */
  798. /* Send Data */
  799. SPIx_Write(*pData);
  800. }
  801. else
  802. {
  803. /* Several data should be sent in a raw */
  804. /* Direct SPI accesses for optimization */
  805. for (counter = Size; counter != 0; counter--)
  806. {
  807. while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE)
  808. {
  809. }
  810. /* Need to invert bytes for LCD*/
  811. *((__IO uint8_t*)&hnucleo_Spi.Instance->DR) = *(pData+1);
  812. while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE)
  813. {
  814. }
  815. *((__IO uint8_t*)&hnucleo_Spi.Instance->DR) = *pData;
  816. counter--;
  817. pData += 2;
  818. }
  819. /* Wait until the bus is ready before releasing Chip select */
  820. while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_BSY) != RESET)
  821. {
  822. }
  823. }
  824. /* Empty the Rx fifo */
  825. data = *(&hnucleo_Spi.Instance->DR);
  826. UNUSED(data); /* Remove GNU warning */
  827. /* Deselect : Chip Select high */
  828. LCD_CS_HIGH();
  829. }
  830. /**
  831. * @brief Wait for loop in ms.
  832. * @param Delay in ms.
  833. * @retval None
  834. */
  835. void LCD_Delay(uint32_t Delay)
  836. {
  837. HAL_Delay(Delay);
  838. }
  839. #endif /* HAL_SPI_MODULE_ENABLED */
  840. #ifdef HAL_ADC_MODULE_ENABLED
  841. /******************************* LINK JOYSTICK ********************************/
  842. /**
  843. * @brief Initialize ADC MSP.
  844. * @retval None
  845. */
  846. static void ADCx_MspInit(ADC_HandleTypeDef *hadc)
  847. {
  848. GPIO_InitTypeDef gpioinitstruct = {0};
  849. RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct;
  850. /*** Configure the GPIOs ***/
  851. /* Enable GPIO clock */
  852. NUCLEO_ADCx_GPIO_CLK_ENABLE();
  853. /* Configure ADC1 Channel8 as analog input */
  854. gpioinitstruct.Pin = NUCLEO_ADCx_GPIO_PIN ;
  855. gpioinitstruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
  856. gpioinitstruct.Pull = GPIO_NOPULL;
  857. gpioinitstruct.Speed = GPIO_SPEED_FREQ_HIGH;
  858. HAL_GPIO_Init(NUCLEO_ADCx_GPIO_PORT, &gpioinitstruct);
  859. /*** Configure the ADC peripheral ***/
  860. /* Enable ADC clock */
  861. NUCLEO_ADCx_CLK_ENABLE();
  862. /* Configure SYSCLK as source clock for ADC */
  863. RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  864. RCC_PeriphCLKInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_SYSCLK;
  865. HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct);
  866. }
  867. /**
  868. * @brief Initializes ADC HAL.
  869. * @retval None
  870. */
  871. static HAL_StatusTypeDef ADCx_Init(void)
  872. {
  873. if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_RESET)
  874. {
  875. /* ADC Config */
  876. hnucleo_Adc.Instance = NUCLEO_ADCx;
  877. hnucleo_Adc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV8; /* (must not exceed 16MHz) */
  878. hnucleo_Adc.Init.Resolution = ADC_RESOLUTION_12B;
  879. hnucleo_Adc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  880. hnucleo_Adc.Init.ScanConvMode = DISABLE;
  881. hnucleo_Adc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  882. hnucleo_Adc.Init.LowPowerAutoWait = ENABLE;
  883. hnucleo_Adc.Init.ContinuousConvMode = DISABLE;
  884. hnucleo_Adc.Init.NbrOfConversion = 1;
  885. hnucleo_Adc.Init.DiscontinuousConvMode = DISABLE;
  886. hnucleo_Adc.Init.NbrOfDiscConversion = 1;
  887. hnucleo_Adc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  888. hnucleo_Adc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  889. hnucleo_Adc.Init.DMAContinuousRequests = DISABLE;
  890. hnucleo_Adc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  891. hnucleo_Adc.Init.OversamplingMode = DISABLE;
  892. ADCx_MspInit(&hnucleo_Adc);
  893. if (HAL_ADC_Init(&hnucleo_Adc) != HAL_OK)
  894. {
  895. return HAL_ERROR;
  896. }
  897. if (HAL_ADCEx_Calibration_Start(&hnucleo_Adc,ADC_SINGLE_ENDED) != HAL_OK)
  898. {
  899. return HAL_ERROR;
  900. }
  901. }
  902. return HAL_OK;
  903. }
  904. #endif /* HAL_ADC_MODULE_ENABLED */
  905. /**
  906. * @}
  907. */
  908. /**
  909. * @}
  910. */
  911. /**
  912. * @}
  913. */
  914. /**
  915. * @}
  916. */
  917. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/