stm32746g_discovery.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /**
  2. ******************************************************************************
  3. * @file stm32746g_discovery.c
  4. * @author MCD Application Team
  5. * @version V2.0.0
  6. * @date 30-December-2016
  7. * @brief This file provides a set of firmware functions to manage LEDs,
  8. * push-buttons and COM ports available on STM32746G-Discovery
  9. * board(MB1191) from STMicroelectronics.
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * <h2><center>&copy; COPYRIGHT(c) 2016 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 "stm32746g_discovery.h"
  41. /** @addtogroup BSP
  42. * @{
  43. */
  44. /** @addtogroup STM32746G_DISCOVERY
  45. * @{
  46. */
  47. /** @defgroup STM32746G_DISCOVERY_LOW_LEVEL STM32746G_DISCOVERY_LOW_LEVEL
  48. * @{
  49. */
  50. /** @defgroup STM32746G_DISCOVERY_LOW_LEVEL_Private_TypesDefinitions STM32746G_DISCOVERY_LOW_LEVEL Private Types Definitions
  51. * @{
  52. */
  53. /**
  54. * @}
  55. */
  56. /** @defgroup STM32746G_DISCOVERY_LOW_LEVEL_Private_Defines STM32746G_DISCOVERY_LOW_LEVEL Private Defines
  57. * @{
  58. */
  59. /**
  60. * @brief STM32746G DISCOVERY BSP Driver version number V2.0.0
  61. */
  62. #define __STM32746G_DISCO_BSP_VERSION_MAIN (0x02) /*!< [31:24] main version */
  63. #define __STM32746G_DISCO_BSP_VERSION_SUB1 (0x00) /*!< [23:16] sub1 version */
  64. #define __STM32746G_DISCO_BSP_VERSION_SUB2 (0x00) /*!< [15:8] sub2 version */
  65. #define __STM32746G_DISCO_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */
  66. #define __STM32746G_DISCO_BSP_VERSION ((__STM32746G_DISCO_BSP_VERSION_MAIN << 24)\
  67. |(__STM32746G_DISCO_BSP_VERSION_SUB1 << 16)\
  68. |(__STM32746G_DISCO_BSP_VERSION_SUB2 << 8 )\
  69. |(__STM32746G_DISCO_BSP_VERSION_RC))
  70. /**
  71. * @}
  72. */
  73. /** @defgroup STM32746G_DISCOVERY_LOW_LEVEL_Private_Macros STM32746G_DISCOVERY_LOW_LEVEL Private Macros
  74. * @{
  75. */
  76. /**
  77. * @}
  78. */
  79. /** @defgroup STM32746G_DISCOVERY_LOW_LEVEL_Private_Variables STM32746G_DISCOVERY_LOW_LEVEL Private Variables
  80. * @{
  81. */
  82. static I2C_HandleTypeDef hI2cAudioHandler = {0};
  83. static I2C_HandleTypeDef hI2cExtHandler = {0};
  84. /**
  85. * @}
  86. */
  87. /** @defgroup STM32746G_DISCOVERY_LOW_LEVEL_Private_FunctionPrototypes STM32746G_DISCOVERY_LOW_LEVEL Private Function Prototypes
  88. * @{
  89. */
  90. static void I2Cx_MspInit(I2C_HandleTypeDef *i2c_handler);
  91. static void I2Cx_Init(I2C_HandleTypeDef *i2c_handler);
  92. static HAL_StatusTypeDef I2Cx_ReadMultiple(I2C_HandleTypeDef *i2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length);
  93. static HAL_StatusTypeDef I2Cx_WriteMultiple(I2C_HandleTypeDef *i2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length);
  94. static HAL_StatusTypeDef I2Cx_IsDeviceReady(I2C_HandleTypeDef *i2c_handler, uint16_t DevAddress, uint32_t Trials);
  95. static void I2Cx_Error(I2C_HandleTypeDef *i2c_handler, uint8_t Addr);
  96. /* AUDIO IO functions */
  97. void AUDIO_IO_Init(void);
  98. void AUDIO_IO_DeInit(void);
  99. void AUDIO_IO_Write(uint8_t Addr, uint16_t Reg, uint16_t Value);
  100. uint16_t AUDIO_IO_Read(uint8_t Addr, uint16_t Reg);
  101. void AUDIO_IO_Delay(uint32_t Delay);
  102. /* TOUCHSCREEN IO functions */
  103. void TS_IO_Init(void);
  104. void TS_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
  105. uint8_t TS_IO_Read(uint8_t Addr, uint8_t Reg);
  106. void TS_IO_Delay(uint32_t Delay);
  107. /**
  108. * @}
  109. */
  110. /** @defgroup STM32746G_DISCOVERY_LOW_LEVEL_Exported_Functions STM32746G_DISCOVERY_LOW_LEVELSTM32746G_DISCOVERY_LOW_LEVEL Exported Functions
  111. * @{
  112. */
  113. /**
  114. * @brief This method returns the STM32746G DISCOVERY BSP Driver revision
  115. * @retval version: 0xXYZR (8bits for each decimal, R for RC)
  116. */
  117. /*******************************************************************************
  118. BUS OPERATIONS
  119. *******************************************************************************/
  120. /******************************* I2C Routines *********************************/
  121. /**
  122. * @brief Initializes I2C MSP.
  123. * @param i2c_handler : I2C handler
  124. * @retval None
  125. */
  126. static void I2Cx_MspInit(I2C_HandleTypeDef *i2c_handler)
  127. {
  128. GPIO_InitTypeDef gpio_init_structure;
  129. if (i2c_handler == (I2C_HandleTypeDef*)(&hI2cAudioHandler))
  130. {
  131. /* AUDIO and LCD I2C MSP init */
  132. /*** Configure the GPIOs ***/
  133. /* Enable GPIO clock */
  134. DISCOVERY_AUDIO_I2Cx_SCL_SDA_GPIO_CLK_ENABLE();
  135. /* Configure I2C Tx as alternate function */
  136. gpio_init_structure.Pin = DISCOVERY_AUDIO_I2Cx_SCL_PIN;
  137. gpio_init_structure.Mode = GPIO_MODE_AF_OD;
  138. gpio_init_structure.Pull = GPIO_NOPULL;
  139. gpio_init_structure.Speed = GPIO_SPEED_FAST;
  140. gpio_init_structure.Alternate = DISCOVERY_AUDIO_I2Cx_SCL_SDA_AF;
  141. HAL_GPIO_Init(DISCOVERY_AUDIO_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
  142. /* Configure I2C Rx as alternate function */
  143. gpio_init_structure.Pin = DISCOVERY_AUDIO_I2Cx_SDA_PIN;
  144. HAL_GPIO_Init(DISCOVERY_AUDIO_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
  145. /*** Configure the I2C peripheral ***/
  146. /* Enable I2C clock */
  147. DISCOVERY_AUDIO_I2Cx_CLK_ENABLE();
  148. /* Force the I2C peripheral clock reset */
  149. DISCOVERY_AUDIO_I2Cx_FORCE_RESET();
  150. /* Release the I2C peripheral clock reset */
  151. DISCOVERY_AUDIO_I2Cx_RELEASE_RESET();
  152. /* Enable and set I2Cx Interrupt to a lower priority */
  153. HAL_NVIC_SetPriority(DISCOVERY_AUDIO_I2Cx_EV_IRQn, 0x0F, 0);
  154. HAL_NVIC_EnableIRQ(DISCOVERY_AUDIO_I2Cx_EV_IRQn);
  155. /* Enable and set I2Cx Interrupt to a lower priority */
  156. HAL_NVIC_SetPriority(DISCOVERY_AUDIO_I2Cx_ER_IRQn, 0x0F, 0);
  157. HAL_NVIC_EnableIRQ(DISCOVERY_AUDIO_I2Cx_ER_IRQn);
  158. }
  159. else
  160. {
  161. /* External, camera and Arduino connector I2C MSP init */
  162. /*** Configure the GPIOs ***/
  163. /* Enable GPIO clock */
  164. DISCOVERY_EXT_I2Cx_SCL_SDA_GPIO_CLK_ENABLE();
  165. /* Configure I2C Tx as alternate function */
  166. gpio_init_structure.Pin = DISCOVERY_EXT_I2Cx_SCL_PIN;
  167. gpio_init_structure.Mode = GPIO_MODE_AF_OD;
  168. gpio_init_structure.Pull = GPIO_NOPULL;
  169. gpio_init_structure.Speed = GPIO_SPEED_FAST;
  170. gpio_init_structure.Alternate = DISCOVERY_EXT_I2Cx_SCL_SDA_AF;
  171. HAL_GPIO_Init(DISCOVERY_EXT_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
  172. /* Configure I2C Rx as alternate function */
  173. gpio_init_structure.Pin = DISCOVERY_EXT_I2Cx_SDA_PIN;
  174. HAL_GPIO_Init(DISCOVERY_EXT_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
  175. /*** Configure the I2C peripheral ***/
  176. /* Enable I2C clock */
  177. DISCOVERY_EXT_I2Cx_CLK_ENABLE();
  178. /* Force the I2C peripheral clock reset */
  179. DISCOVERY_EXT_I2Cx_FORCE_RESET();
  180. /* Release the I2C peripheral clock reset */
  181. DISCOVERY_EXT_I2Cx_RELEASE_RESET();
  182. /* Enable and set I2Cx Interrupt to a lower priority */
  183. HAL_NVIC_SetPriority(DISCOVERY_EXT_I2Cx_EV_IRQn, 0x0F, 0);
  184. HAL_NVIC_EnableIRQ(DISCOVERY_EXT_I2Cx_EV_IRQn);
  185. /* Enable and set I2Cx Interrupt to a lower priority */
  186. HAL_NVIC_SetPriority(DISCOVERY_EXT_I2Cx_ER_IRQn, 0x0F, 0);
  187. HAL_NVIC_EnableIRQ(DISCOVERY_EXT_I2Cx_ER_IRQn);
  188. }
  189. }
  190. /**
  191. * @brief Initializes I2C HAL.
  192. * @param i2c_handler : I2C handler
  193. * @retval None
  194. */
  195. static void I2Cx_Init(I2C_HandleTypeDef *i2c_handler)
  196. {
  197. if(HAL_I2C_GetState(i2c_handler) == HAL_I2C_STATE_RESET)
  198. {
  199. if (i2c_handler == (I2C_HandleTypeDef*)(&hI2cAudioHandler))
  200. {
  201. /* Audio and LCD I2C configuration */
  202. i2c_handler->Instance = DISCOVERY_AUDIO_I2Cx;
  203. }
  204. else
  205. {
  206. /* External, camera and Arduino connector I2C configuration */
  207. i2c_handler->Instance = DISCOVERY_EXT_I2Cx;
  208. }
  209. i2c_handler->Init.Timing = DISCOVERY_I2Cx_TIMING;
  210. i2c_handler->Init.OwnAddress1 = 0;
  211. i2c_handler->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  212. i2c_handler->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  213. i2c_handler->Init.OwnAddress2 = 0;
  214. i2c_handler->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  215. i2c_handler->Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  216. /* Init the I2C */
  217. I2Cx_MspInit(i2c_handler);
  218. HAL_I2C_Init(i2c_handler);
  219. }
  220. }
  221. /**
  222. * @brief Reads multiple data.
  223. * @param i2c_handler : I2C handler
  224. * @param Addr: I2C address
  225. * @param Reg: Reg address
  226. * @param MemAddress: Memory address
  227. * @param Buffer: Pointer to data buffer
  228. * @param Length: Length of the data
  229. * @retval Number of read data
  230. */
  231. static HAL_StatusTypeDef I2Cx_ReadMultiple(I2C_HandleTypeDef *i2c_handler,
  232. uint8_t Addr,
  233. uint16_t Reg,
  234. uint16_t MemAddress,
  235. uint8_t *Buffer,
  236. uint16_t Length)
  237. {
  238. HAL_StatusTypeDef status = HAL_OK;
  239. status = HAL_I2C_Mem_Read(i2c_handler, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, 1000);
  240. /* Check the communication status */
  241. if(status != HAL_OK)
  242. {
  243. /* I2C error occurred */
  244. I2Cx_Error(i2c_handler, Addr);
  245. }
  246. return status;
  247. }
  248. /**
  249. * @brief Writes a value in a register of the device through BUS in using DMA mode.
  250. * @param i2c_handler : I2C handler
  251. * @param Addr: Device address on BUS Bus.
  252. * @param Reg: The target register address to write
  253. * @param MemAddress: Memory address
  254. * @param Buffer: The target register value to be written
  255. * @param Length: buffer size to be written
  256. * @retval HAL status
  257. */
  258. static HAL_StatusTypeDef I2Cx_WriteMultiple(I2C_HandleTypeDef *i2c_handler,
  259. uint8_t Addr,
  260. uint16_t Reg,
  261. uint16_t MemAddress,
  262. uint8_t *Buffer,
  263. uint16_t Length)
  264. {
  265. HAL_StatusTypeDef status = HAL_OK;
  266. status = HAL_I2C_Mem_Write(i2c_handler, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, 1000);
  267. /* Check the communication status */
  268. if(status != HAL_OK)
  269. {
  270. /* Re-Initiaize the I2C Bus */
  271. I2Cx_Error(i2c_handler, Addr);
  272. }
  273. return status;
  274. }
  275. /**
  276. * @brief Checks if target device is ready for communication.
  277. * @note This function is used with Memory devices
  278. * @param i2c_handler : I2C handler
  279. * @param DevAddress: Target device address
  280. * @param Trials: Number of trials
  281. * @retval HAL status
  282. */
  283. static HAL_StatusTypeDef I2Cx_IsDeviceReady(I2C_HandleTypeDef *i2c_handler, uint16_t DevAddress, uint32_t Trials)
  284. {
  285. return (HAL_I2C_IsDeviceReady(i2c_handler, DevAddress, Trials, 1000));
  286. }
  287. /**
  288. * @brief Manages error callback by re-initializing I2C.
  289. * @param i2c_handler : I2C handler
  290. * @param Addr: I2C Address
  291. * @retval None
  292. */
  293. static void I2Cx_Error(I2C_HandleTypeDef *i2c_handler, uint8_t Addr)
  294. {
  295. /* De-initialize the I2C communication bus */
  296. HAL_I2C_DeInit(i2c_handler);
  297. /* Re-Initialize the I2C communication bus */
  298. I2Cx_Init(i2c_handler);
  299. }
  300. /*******************************************************************************
  301. LINK OPERATIONS
  302. *******************************************************************************/
  303. /********************************* LINK AUDIO *********************************/
  304. /**
  305. * @brief Initializes Audio low level.
  306. * @retval None
  307. */
  308. void AUDIO_IO_Init(void)
  309. {
  310. I2Cx_Init(&hI2cAudioHandler);
  311. }
  312. /**
  313. * @brief Deinitializes Audio low level.
  314. * @retval None
  315. */
  316. void AUDIO_IO_DeInit(void)
  317. {
  318. }
  319. /**
  320. * @brief Writes a single data.
  321. * @param Addr: I2C address
  322. * @param Reg: Reg address
  323. * @param Value: Data to be written
  324. * @retval None
  325. */
  326. void AUDIO_IO_Write(uint8_t Addr, uint16_t Reg, uint16_t Value)
  327. {
  328. uint16_t tmp = Value;
  329. Value = ((uint16_t)(tmp >> 8) & 0x00FF);
  330. Value |= ((uint16_t)(tmp << 8)& 0xFF00);
  331. I2Cx_WriteMultiple(&hI2cAudioHandler, Addr, Reg, I2C_MEMADD_SIZE_16BIT,(uint8_t*)&Value, 2);
  332. }
  333. /**
  334. * @brief Reads a single data.
  335. * @param Addr: I2C address
  336. * @param Reg: Reg address
  337. * @retval Data to be read
  338. */
  339. uint16_t AUDIO_IO_Read(uint8_t Addr, uint16_t Reg)
  340. {
  341. uint16_t read_value = 0, tmp = 0;
  342. I2Cx_ReadMultiple(&hI2cAudioHandler, Addr, Reg, I2C_MEMADD_SIZE_16BIT, (uint8_t*)&read_value, 2);
  343. tmp = ((uint16_t)(read_value >> 8) & 0x00FF);
  344. tmp |= ((uint16_t)(read_value << 8)& 0xFF00);
  345. read_value = tmp;
  346. return read_value;
  347. }
  348. /**
  349. * @brief AUDIO Codec delay
  350. * @param Delay: Delay in ms
  351. * @retval None
  352. */
  353. void AUDIO_IO_Delay(uint32_t Delay)
  354. {
  355. HAL_Delay(Delay);
  356. }
  357. /********************************* I2C nunchuck controller **************************/
  358. void DJ_IO_Init(void)
  359. {
  360. I2Cx_Init(&hI2cExtHandler);
  361. }
  362. void DJ_IO_Write(uint8_t Addr, uint32_t Reg, uint8_t Value)
  363. {
  364. I2Cx_WriteMultiple(&hI2cExtHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT,(uint8_t*)&Value, 1);
  365. }
  366. static uint8_t djhero_buf[6];
  367. uint8_t *DJ_IO_Read(uint8_t Addr, uint8_t Reg)
  368. {
  369. uint8_t read_value = 0;
  370. I2Cx_ReadMultiple(&hI2cExtHandler, Addr, Reg, I2C_MEMADD_SIZE_8BIT, djhero_buf, 6);
  371. return djhero_buf;
  372. }
  373. void DJ_IO_Delay(uint32_t Delay)
  374. {
  375. HAL_Delay(Delay);
  376. }
  377. /********************************* LINK TOUCHSCREEN *********************************/
  378. /**
  379. * @brief Initializes Touchscreen low level.
  380. * @retval None
  381. */
  382. void TS_IO_Init(void)
  383. {
  384. I2Cx_Init(&hI2cAudioHandler);
  385. }
  386. /**
  387. * @brief Writes a single data.
  388. * @param Addr: I2C address
  389. * @param Reg: Reg address
  390. * @param Value: Data to be written
  391. * @retval None
  392. */
  393. void TS_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
  394. {
  395. I2Cx_WriteMultiple(&hI2cAudioHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT,(uint8_t*)&Value, 1);
  396. }
  397. /**
  398. * @brief Reads a single data.
  399. * @param Addr: I2C address
  400. * @param Reg: Reg address
  401. * @retval Data to be read
  402. */
  403. uint8_t TS_IO_Read(uint8_t Addr, uint8_t Reg)
  404. {
  405. uint8_t read_value = 0;
  406. I2Cx_ReadMultiple(&hI2cAudioHandler, Addr, Reg, I2C_MEMADD_SIZE_8BIT, (uint8_t*)&read_value, 1);
  407. return read_value;
  408. }
  409. /**
  410. * @brief TS delay
  411. * @param Delay: Delay in ms
  412. * @retval None
  413. */
  414. void TS_IO_Delay(uint32_t Delay)
  415. {
  416. HAL_Delay(Delay);
  417. }
  418. /**
  419. * @}
  420. */
  421. /**
  422. * @}
  423. */
  424. /**
  425. * @}
  426. */
  427. /**
  428. * @}
  429. */
  430. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/