stm32f4_discovery.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4_discovery.c
  4. * @author MCD Application Team
  5. * @brief This file provides set of firmware functions to manage Leds and
  6. * push-button available on STM32F4-Discovery Kit from STMicroelectronics.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2017 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32f4_discovery.h"
  21. /** @defgroup BSP BSP
  22. * @{
  23. */
  24. /** @defgroup STM32F4_DISCOVERY STM32F4 DISCOVERY
  25. * @{
  26. */
  27. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL STM32F4 DISCOVERY LOW LEVEL
  28. * @brief This file provides set of firmware functions to manage Leds and push-button
  29. * available on STM32F4-Discovery Kit from STMicroelectronics.
  30. * @{
  31. */
  32. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_TypesDefinitions STM32F4 DISCOVERY LOW LEVEL Private TypesDefinitions
  33. * @{
  34. */
  35. /**
  36. * @}
  37. */
  38. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Defines STM32F4 DISCOVERY LOW LEVEL Private Defines
  39. * @{
  40. */
  41. /**
  42. * @brief STM32F4 DISCO BSP Driver version number V2.1.4
  43. */
  44. #define __STM32F4_DISCO_BSP_VERSION_MAIN (0x02) /*!< [31:24] main version */
  45. #define __STM32F4_DISCO_BSP_VERSION_SUB1 (0x01) /*!< [23:16] sub1 version */
  46. #define __STM32F4_DISCO_BSP_VERSION_SUB2 (0x04) /*!< [15:8] sub2 version */
  47. #define __STM32F4_DISCO_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */
  48. #define __STM32F4_DISCO_BSP_VERSION ((__STM32F4_DISCO_BSP_VERSION_MAIN << 24)\
  49. |(__STM32F4_DISCO_BSP_VERSION_SUB1 << 16)\
  50. |(__STM32F4_DISCO_BSP_VERSION_SUB2 << 8 )\
  51. |(__STM32F4_DISCO_BSP_VERSION_RC))
  52. /**
  53. * @}
  54. */
  55. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Macros STM32F4 DISCOVERY LOW LEVEL Private Macros
  56. * @{
  57. */
  58. /**
  59. * @}
  60. */
  61. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Variables STM32F4 DISCOVERY LOW LEVEL Private Variables
  62. * @{
  63. */
  64. GPIO_TypeDef* GPIO_PORT[LEDn] = {LED4_GPIO_PORT,
  65. LED3_GPIO_PORT,
  66. LED5_GPIO_PORT,
  67. LED6_GPIO_PORT};
  68. const uint16_t GPIO_PIN[LEDn] = {LED4_PIN,
  69. LED3_PIN,
  70. LED5_PIN,
  71. LED6_PIN};
  72. GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {KEY_BUTTON_GPIO_PORT};
  73. const uint16_t BUTTON_PIN[BUTTONn] = {KEY_BUTTON_PIN};
  74. const uint8_t BUTTON_IRQn[BUTTONn] = {KEY_BUTTON_EXTI_IRQn};
  75. #if 0
  76. uint32_t I2cxTimeout = I2Cx_TIMEOUT_MAX; /*<! Value of Timeout when I2C communication fails */
  77. uint32_t SpixTimeout = SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */
  78. static SPI_HandleTypeDef SpiHandle;
  79. static I2C_HandleTypeDef I2cHandle;
  80. /**
  81. * @}
  82. */
  83. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_FunctionPrototypes STM32F4 DISCOVERY LOW LEVEL Private FunctionPrototypes
  84. * @{
  85. */
  86. /**
  87. * @}
  88. */
  89. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Functions STM32F4 DISCOVERY LOW LEVEL Private Functions
  90. * @{
  91. */
  92. static void I2Cx_Init(void);
  93. static void I2Cx_WriteData(uint8_t Addr, uint8_t Reg, uint8_t Value);
  94. static uint8_t I2Cx_ReadData(uint8_t Addr, uint8_t Reg);
  95. static void I2Cx_MspInit(void);
  96. static void I2Cx_Error(uint8_t Addr);
  97. static void SPIx_Init(void);
  98. static void SPIx_MspInit(void);
  99. static uint8_t SPIx_WriteRead(uint8_t Byte);
  100. static void SPIx_Error(void);
  101. /* Link functions for Accelerometer peripheral */
  102. void ACCELERO_IO_Init(void);
  103. void ACCELERO_IO_ITConfig(void);
  104. void ACCELERO_IO_Write(uint8_t *pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite);
  105. void ACCELERO_IO_Read(uint8_t *pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead);
  106. /* Link functions for Audio peripheral */
  107. void AUDIO_IO_Init(void);
  108. void AUDIO_IO_DeInit(void);
  109. void AUDIO_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
  110. uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg);
  111. /**
  112. * @}
  113. */
  114. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_LED_Functions STM32F4 DISCOVERY LOW LEVEL LED Functions
  115. * @{
  116. */
  117. #endif
  118. /**
  119. * @brief This method returns the STM32F4 DISCO BSP Driver revision
  120. * @retval version : 0xXYZR (8bits for each decimal, R for RC)
  121. */
  122. uint32_t BSP_GetVersion(void)
  123. {
  124. return __STM32F4_DISCO_BSP_VERSION;
  125. }
  126. /**
  127. * @brief Configures LED GPIO.
  128. * @param Led: Specifies the Led to be configured.
  129. * This parameter can be one of following parameters:
  130. * @arg LED4
  131. * @arg LED3
  132. * @arg LED5
  133. * @arg LED6
  134. */
  135. void BSP_LED_Init(Led_TypeDef Led)
  136. {
  137. GPIO_InitTypeDef GPIO_InitStruct;
  138. /* Enable the GPIO_LED Clock */
  139. LEDx_GPIO_CLK_ENABLE(Led);
  140. /* Configure the GPIO_LED pin */
  141. GPIO_InitStruct.Pin = GPIO_PIN[Led];
  142. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  143. GPIO_InitStruct.Pull = GPIO_PULLUP;
  144. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  145. HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
  146. HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
  147. }
  148. /**
  149. * @brief Turns selected LED On.
  150. * @param Led: Specifies the Led to be set on.
  151. * This parameter can be one of following parameters:
  152. * @arg LED4
  153. * @arg LED3
  154. * @arg LED5
  155. * @arg LED6
  156. */
  157. void BSP_LED_On(Led_TypeDef Led)
  158. {
  159. HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
  160. }
  161. /**
  162. * @brief Turns selected LED Off.
  163. * @param Led: Specifies the Led to be set off.
  164. * This parameter can be one of following parameters:
  165. * @arg LED4
  166. * @arg LED3
  167. * @arg LED5
  168. * @arg LED6
  169. */
  170. void BSP_LED_Off(Led_TypeDef Led)
  171. {
  172. HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
  173. }
  174. /**
  175. * @brief Toggles the selected LED.
  176. * @param Led: Specifies the Led to be toggled.
  177. * This parameter can be one of following parameters:
  178. * @arg LED4
  179. * @arg LED3
  180. * @arg LED5
  181. * @arg LED6
  182. */
  183. void BSP_LED_Toggle(Led_TypeDef Led)
  184. {
  185. HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
  186. }
  187. /**
  188. * @}
  189. */
  190. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_BUTTON_Functions STM32F4 DISCOVERY LOW LEVEL BUTTON Functions
  191. * @{
  192. */
  193. /**
  194. * @brief Configures Button GPIO and EXTI Line.
  195. * @param Button: Specifies the Button to be configured.
  196. * This parameter should be: BUTTON_KEY
  197. * @param Mode: Specifies Button mode.
  198. * This parameter can be one of following parameters:
  199. * @arg BUTTON_MODE_GPIO: Button will be used as simple IO
  200. * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
  201. * generation capability
  202. */
  203. void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef Mode)
  204. {
  205. GPIO_InitTypeDef GPIO_InitStruct;
  206. /* Enable the BUTTON Clock */
  207. BUTTONx_GPIO_CLK_ENABLE(Button);
  208. if (Mode == BUTTON_MODE_GPIO)
  209. {
  210. /* Configure Button pin as input */
  211. GPIO_InitStruct.Pin = BUTTON_PIN[Button];
  212. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  213. GPIO_InitStruct.Pull = GPIO_NOPULL;
  214. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  215. HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
  216. }
  217. if (Mode == BUTTON_MODE_EXTI)
  218. {
  219. /* Configure Button pin as input with External interrupt */
  220. GPIO_InitStruct.Pin = BUTTON_PIN[Button];
  221. GPIO_InitStruct.Pull = GPIO_NOPULL;
  222. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  223. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  224. HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
  225. /* Enable and set Button EXTI Interrupt to the lowest priority */
  226. HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0);
  227. HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
  228. }
  229. }
  230. /**
  231. * @brief Returns the selected Button state.
  232. * @param Button: Specifies the Button to be checked.
  233. * This parameter should be: BUTTON_KEY
  234. * @retval The Button GPIO pin value.
  235. */
  236. uint32_t BSP_PB_GetState(Button_TypeDef Button)
  237. {
  238. return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
  239. }
  240. #if 0
  241. /**
  242. * @}
  243. */
  244. /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_BUS_Functions STM32F4 DISCOVERY LOW LEVEL BUS Functions
  245. * @{
  246. */
  247. /*******************************************************************************
  248. BUS OPERATIONS
  249. *******************************************************************************/
  250. /******************************* SPI Routines *********************************/
  251. /**
  252. * @brief SPIx Bus initialization
  253. */
  254. static void SPIx_Init(void)
  255. {
  256. if(HAL_SPI_GetState(&SpiHandle) == HAL_SPI_STATE_RESET)
  257. {
  258. /* SPI configuration -----------------------------------------------------*/
  259. SpiHandle.Instance = DISCOVERY_SPIx;
  260. SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  261. SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
  262. SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
  263. SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW;
  264. SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
  265. SpiHandle.Init.CRCPolynomial = 7;
  266. SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
  267. SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
  268. SpiHandle.Init.NSS = SPI_NSS_SOFT;
  269. SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED;
  270. SpiHandle.Init.Mode = SPI_MODE_MASTER;
  271. SPIx_MspInit();
  272. HAL_SPI_Init(&SpiHandle);
  273. }
  274. }
  275. /**
  276. * @brief Sends a Byte through the SPI interface and return the Byte received
  277. * from the SPI bus.
  278. * @param Byte: Byte send.
  279. * @retval The received byte value
  280. */
  281. static uint8_t SPIx_WriteRead(uint8_t Byte)
  282. {
  283. uint8_t receivedbyte = 0;
  284. /* Send a Byte through the SPI peripheral */
  285. /* Read byte from the SPI bus */
  286. if(HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t*) &Byte, (uint8_t*) &receivedbyte, 1, SpixTimeout) != HAL_OK)
  287. {
  288. SPIx_Error();
  289. }
  290. return receivedbyte;
  291. }
  292. /**
  293. * @brief SPIx error treatment function.
  294. */
  295. static void SPIx_Error(void)
  296. {
  297. /* De-initialize the SPI communication bus */
  298. HAL_SPI_DeInit(&SpiHandle);
  299. /* Re-Initialize the SPI communication bus */
  300. SPIx_Init();
  301. }
  302. /**
  303. * @brief SPI MSP Init.
  304. */
  305. static void SPIx_MspInit(void)
  306. {
  307. GPIO_InitTypeDef GPIO_InitStructure;
  308. /* Enable the SPI peripheral */
  309. DISCOVERY_SPIx_CLK_ENABLE();
  310. /* Enable SCK, MOSI and MISO GPIO clocks */
  311. DISCOVERY_SPIx_GPIO_CLK_ENABLE();
  312. /* SPI SCK, MOSI, MISO pin configuration */
  313. GPIO_InitStructure.Pin = (DISCOVERY_SPIx_SCK_PIN | DISCOVERY_SPIx_MISO_PIN | DISCOVERY_SPIx_MOSI_PIN);
  314. GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
  315. GPIO_InitStructure.Pull = GPIO_PULLDOWN;
  316. GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM;
  317. GPIO_InitStructure.Alternate = DISCOVERY_SPIx_AF;
  318. HAL_GPIO_Init(DISCOVERY_SPIx_GPIO_PORT, &GPIO_InitStructure);
  319. }
  320. /******************************* I2C Routines**********************************/
  321. /**
  322. * @brief Configures I2C interface.
  323. */
  324. static void I2Cx_Init(void)
  325. {
  326. if(HAL_I2C_GetState(&I2cHandle) == HAL_I2C_STATE_RESET)
  327. {
  328. /* DISCOVERY_I2Cx peripheral configuration */
  329. I2cHandle.Init.ClockSpeed = BSP_I2C_SPEED;
  330. I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
  331. I2cHandle.Init.OwnAddress1 = 0x33;
  332. I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  333. I2cHandle.Instance = DISCOVERY_I2Cx;
  334. /* Init the I2C */
  335. I2Cx_MspInit();
  336. HAL_I2C_Init(&I2cHandle);
  337. }
  338. }
  339. /**
  340. * @brief Write a value in a register of the device through BUS.
  341. * @param Addr: Device address on BUS Bus.
  342. * @param Reg: The target register address to write
  343. * @param Value: The target register value to be written
  344. * @retval HAL status
  345. */
  346. static void I2Cx_WriteData(uint8_t Addr, uint8_t Reg, uint8_t Value)
  347. {
  348. HAL_StatusTypeDef status = HAL_OK;
  349. status = HAL_I2C_Mem_Write(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1, I2cxTimeout);
  350. /* Check the communication status */
  351. if(status != HAL_OK)
  352. {
  353. /* Execute user timeout callback */
  354. I2Cx_Error(Addr);
  355. }
  356. }
  357. /**
  358. * @brief Read a register of the device through BUS
  359. * @param Addr: Device address on BUS
  360. * @param Reg: The target register address to read
  361. * @retval HAL status
  362. */
  363. static uint8_t I2Cx_ReadData(uint8_t Addr, uint8_t Reg)
  364. {
  365. HAL_StatusTypeDef status = HAL_OK;
  366. uint8_t value = 0;
  367. status = HAL_I2C_Mem_Read(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &value, 1,I2cxTimeout);
  368. /* Check the communication status */
  369. if(status != HAL_OK)
  370. {
  371. /* Execute user timeout callback */
  372. I2Cx_Error(Addr);
  373. }
  374. return value;
  375. }
  376. /**
  377. * @brief Manages error callback by re-initializing I2C.
  378. * @param Addr: I2C Address
  379. */
  380. static void I2Cx_Error(uint8_t Addr)
  381. {
  382. /* De-initialize the I2C communication bus */
  383. HAL_I2C_DeInit(&I2cHandle);
  384. /* Re-Initialize the I2C communication bus */
  385. I2Cx_Init();
  386. }
  387. /**
  388. * @brief I2C MSP Initialization
  389. */
  390. static void I2Cx_MspInit(void)
  391. {
  392. GPIO_InitTypeDef GPIO_InitStruct;
  393. /* Enable I2C GPIO clocks */
  394. DISCOVERY_I2Cx_SCL_SDA_GPIO_CLK_ENABLE();
  395. /* DISCOVERY_I2Cx SCL and SDA pins configuration ---------------------------*/
  396. GPIO_InitStruct.Pin = DISCOVERY_I2Cx_SCL_PIN | DISCOVERY_I2Cx_SDA_PIN;
  397. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  398. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  399. GPIO_InitStruct.Pull = GPIO_NOPULL;
  400. GPIO_InitStruct.Alternate = DISCOVERY_I2Cx_SCL_SDA_AF;
  401. HAL_GPIO_Init(DISCOVERY_I2Cx_SCL_SDA_GPIO_PORT, &GPIO_InitStruct);
  402. /* Enable the DISCOVERY_I2Cx peripheral clock */
  403. DISCOVERY_I2Cx_CLK_ENABLE();
  404. /* Force the I2C peripheral clock reset */
  405. DISCOVERY_I2Cx_FORCE_RESET();
  406. /* Release the I2C peripheral clock reset */
  407. DISCOVERY_I2Cx_RELEASE_RESET();
  408. /* Enable and set I2Cx Interrupt to the highest priority */
  409. HAL_NVIC_SetPriority(DISCOVERY_I2Cx_EV_IRQn, 0, 0);
  410. HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_EV_IRQn);
  411. /* Enable and set I2Cx Interrupt to the highest priority */
  412. HAL_NVIC_SetPriority(DISCOVERY_I2Cx_ER_IRQn, 0, 0);
  413. HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_ER_IRQn);
  414. }
  415. /*******************************************************************************
  416. LINK OPERATIONS
  417. *******************************************************************************/
  418. /***************************** LINK ACCELEROMETER *****************************/
  419. /**
  420. * @brief Configures the Accelerometer SPI interface.
  421. */
  422. void ACCELERO_IO_Init(void)
  423. {
  424. GPIO_InitTypeDef GPIO_InitStructure;
  425. /* Configure the Accelerometer Control pins --------------------------------*/
  426. /* Enable CS GPIO clock and configure GPIO pin for Accelerometer Chip select */
  427. ACCELERO_CS_GPIO_CLK_ENABLE();
  428. /* Configure GPIO PIN for LIS Chip select */
  429. GPIO_InitStructure.Pin = ACCELERO_CS_PIN;
  430. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  431. GPIO_InitStructure.Pull = GPIO_NOPULL;
  432. GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM;
  433. HAL_GPIO_Init(ACCELERO_CS_GPIO_PORT, &GPIO_InitStructure);
  434. /* Deselect: Chip Select high */
  435. ACCELERO_CS_HIGH();
  436. SPIx_Init();
  437. }
  438. /**
  439. * @brief Configures the Accelerometer INT2.
  440. * EXTI0 is already used by user button so INT1 is not configured here.
  441. */
  442. void ACCELERO_IO_ITConfig(void)
  443. {
  444. GPIO_InitTypeDef GPIO_InitStructure;
  445. /* Enable INT2 GPIO clock and configure GPIO PINs to detect Interrupts */
  446. ACCELERO_INT_GPIO_CLK_ENABLE();
  447. /* Configure GPIO PINs to detect Interrupts */
  448. GPIO_InitStructure.Pin = ACCELERO_INT2_PIN;
  449. GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
  450. GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
  451. GPIO_InitStructure.Pull = GPIO_NOPULL;
  452. HAL_GPIO_Init(ACCELERO_INT_GPIO_PORT, &GPIO_InitStructure);
  453. /* Enable and set Accelerometer INT2 to the lowest priority */
  454. HAL_NVIC_SetPriority((IRQn_Type)ACCELERO_INT2_EXTI_IRQn, 0x0F, 0);
  455. HAL_NVIC_EnableIRQ((IRQn_Type)ACCELERO_INT2_EXTI_IRQn);
  456. }
  457. /**
  458. * @brief Writes one byte to the Accelerometer.
  459. * @param pBuffer: pointer to the buffer containing the data to be written to the Accelerometer.
  460. * @param WriteAddr: Accelerometer's internal address to write to.
  461. * @param NumByteToWrite: Number of bytes to write.
  462. */
  463. void ACCELERO_IO_Write(uint8_t *pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite)
  464. {
  465. /* Configure the MS bit:
  466. - When 0, the address will remain unchanged in multiple read/write commands.
  467. - When 1, the address will be auto incremented in multiple read/write commands.
  468. */
  469. if(NumByteToWrite > 0x01)
  470. {
  471. WriteAddr |= (uint8_t)MULTIPLEBYTE_CMD;
  472. }
  473. /* Set chip select Low at the start of the transmission */
  474. ACCELERO_CS_LOW();
  475. /* Send the Address of the indexed register */
  476. SPIx_WriteRead(WriteAddr);
  477. /* Send the data that will be written into the device (MSB First) */
  478. while(NumByteToWrite >= 0x01)
  479. {
  480. SPIx_WriteRead(*pBuffer);
  481. NumByteToWrite--;
  482. pBuffer++;
  483. }
  484. /* Set chip select High at the end of the transmission */
  485. ACCELERO_CS_HIGH();
  486. }
  487. /**
  488. * @brief Reads a block of data from the Accelerometer.
  489. * @param pBuffer: pointer to the buffer that receives the data read from the Accelerometer.
  490. * @param ReadAddr: Accelerometer's internal address to read from.
  491. * @param NumByteToRead: number of bytes to read from the Accelerometer.
  492. */
  493. void ACCELERO_IO_Read(uint8_t *pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead)
  494. {
  495. if(NumByteToRead > 0x01)
  496. {
  497. ReadAddr |= (uint8_t)(READWRITE_CMD | MULTIPLEBYTE_CMD);
  498. }
  499. else
  500. {
  501. ReadAddr |= (uint8_t)READWRITE_CMD;
  502. }
  503. /* Set chip select Low at the start of the transmission */
  504. ACCELERO_CS_LOW();
  505. /* Send the Address of the indexed register */
  506. SPIx_WriteRead(ReadAddr);
  507. /* Receive the data that will be read from the device (MSB First) */
  508. while(NumByteToRead > 0x00)
  509. {
  510. /* Send dummy byte (0x00) to generate the SPI clock to ACCELEROMETER (Slave device) */
  511. *pBuffer = SPIx_WriteRead(DUMMY_BYTE);
  512. NumByteToRead--;
  513. pBuffer++;
  514. }
  515. /* Set chip select High at the end of the transmission */
  516. ACCELERO_CS_HIGH();
  517. }
  518. /********************************* LINK AUDIO *********************************/
  519. /**
  520. * @brief Initializes Audio low level.
  521. */
  522. void AUDIO_IO_Init(void)
  523. {
  524. GPIO_InitTypeDef GPIO_InitStruct;
  525. /* Enable Reset GPIO Clock */
  526. AUDIO_RESET_GPIO_CLK_ENABLE();
  527. /* Audio reset pin configuration */
  528. GPIO_InitStruct.Pin = AUDIO_RESET_PIN;
  529. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  530. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  531. GPIO_InitStruct.Pull = GPIO_NOPULL;
  532. HAL_GPIO_Init(AUDIO_RESET_GPIO, &GPIO_InitStruct);
  533. I2Cx_Init();
  534. /* Power Down the codec */
  535. HAL_GPIO_WritePin(AUDIO_RESET_GPIO, AUDIO_RESET_PIN, GPIO_PIN_RESET);
  536. /* Wait for a delay to insure registers erasing */
  537. HAL_Delay(5);
  538. /* Power on the codec */
  539. HAL_GPIO_WritePin(AUDIO_RESET_GPIO, AUDIO_RESET_PIN, GPIO_PIN_SET);
  540. /* Wait for a delay to insure registers erasing */
  541. HAL_Delay(5);
  542. }
  543. /**
  544. * @brief DeInitializes Audio low level.
  545. */
  546. void AUDIO_IO_DeInit(void)
  547. {
  548. }
  549. /**
  550. * @brief Writes a single data.
  551. * @param Addr: I2C address
  552. * @param Reg: Reg address
  553. * @param Value: Data to be written
  554. */
  555. void AUDIO_IO_Write (uint8_t Addr, uint8_t Reg, uint8_t Value)
  556. {
  557. I2Cx_WriteData(Addr, Reg, Value);
  558. }
  559. /**
  560. * @brief Reads a single data.
  561. * @param Addr: I2C address
  562. * @param Reg: Reg address
  563. * @retval Data to be read
  564. */
  565. uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg)
  566. {
  567. return I2Cx_ReadData(Addr, Reg);
  568. }
  569. #endif
  570. /**
  571. * @}
  572. */
  573. /**
  574. * @}
  575. */
  576. /**
  577. * @}
  578. */
  579. /**
  580. * @}
  581. */