123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- /**
- ******************************************************************************
- * @file stm32746g_discovery.c
- * @author MCD Application Team
- * @version V2.0.0
- * @date 30-December-2016
- * @brief This file provides a set of firmware functions to manage LEDs,
- * push-buttons and COM ports available on STM32746G-Discovery
- * board(MB1191) from STMicroelectronics.
- ******************************************************************************
- * @attention
- *
- * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32746g_discovery.h"
- /** @addtogroup BSP
- * @{
- */
- /** @addtogroup STM32746G_DISCOVERY
- * @{
- */
- /** @defgroup STM32746G_DISCOVERY_LOW_LEVEL STM32746G_DISCOVERY_LOW_LEVEL
- * @{
- */
- /** @defgroup STM32746G_DISCOVERY_LOW_LEVEL_Private_TypesDefinitions STM32746G_DISCOVERY_LOW_LEVEL Private Types Definitions
- * @{
- */
- /**
- * @}
- */
- /** @defgroup STM32746G_DISCOVERY_LOW_LEVEL_Private_Defines STM32746G_DISCOVERY_LOW_LEVEL Private Defines
- * @{
- */
- /**
- * @brief STM32746G DISCOVERY BSP Driver version number V2.0.0
- */
- #define __STM32746G_DISCO_BSP_VERSION_MAIN (0x02) /*!< [31:24] main version */
- #define __STM32746G_DISCO_BSP_VERSION_SUB1 (0x00) /*!< [23:16] sub1 version */
- #define __STM32746G_DISCO_BSP_VERSION_SUB2 (0x00) /*!< [15:8] sub2 version */
- #define __STM32746G_DISCO_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */
- #define __STM32746G_DISCO_BSP_VERSION ((__STM32746G_DISCO_BSP_VERSION_MAIN << 24)\
- |(__STM32746G_DISCO_BSP_VERSION_SUB1 << 16)\
- |(__STM32746G_DISCO_BSP_VERSION_SUB2 << 8 )\
- |(__STM32746G_DISCO_BSP_VERSION_RC))
- /**
- * @}
- */
- /** @defgroup STM32746G_DISCOVERY_LOW_LEVEL_Private_Macros STM32746G_DISCOVERY_LOW_LEVEL Private Macros
- * @{
- */
- /**
- * @}
- */
- /** @defgroup STM32746G_DISCOVERY_LOW_LEVEL_Private_Variables STM32746G_DISCOVERY_LOW_LEVEL Private Variables
- * @{
- */
- static I2C_HandleTypeDef hI2cAudioHandler = {0};
- static I2C_HandleTypeDef hI2cExtHandler = {0};
- /**
- * @}
- */
- /** @defgroup STM32746G_DISCOVERY_LOW_LEVEL_Private_FunctionPrototypes STM32746G_DISCOVERY_LOW_LEVEL Private Function Prototypes
- * @{
- */
- static void I2Cx_MspInit(I2C_HandleTypeDef *i2c_handler);
- static void I2Cx_Init(I2C_HandleTypeDef *i2c_handler);
- static HAL_StatusTypeDef I2Cx_ReadMultiple(I2C_HandleTypeDef *i2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length);
- static HAL_StatusTypeDef I2Cx_WriteMultiple(I2C_HandleTypeDef *i2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length);
- static HAL_StatusTypeDef I2Cx_IsDeviceReady(I2C_HandleTypeDef *i2c_handler, uint16_t DevAddress, uint32_t Trials);
- static void I2Cx_Error(I2C_HandleTypeDef *i2c_handler, uint8_t Addr);
- /* AUDIO IO functions */
- void AUDIO_IO_Init(void);
- void AUDIO_IO_DeInit(void);
- void AUDIO_IO_Write(uint8_t Addr, uint16_t Reg, uint16_t Value);
- uint16_t AUDIO_IO_Read(uint8_t Addr, uint16_t Reg);
- void AUDIO_IO_Delay(uint32_t Delay);
- /* TOUCHSCREEN IO functions */
- void TS_IO_Init(void);
- void TS_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
- uint8_t TS_IO_Read(uint8_t Addr, uint8_t Reg);
- void TS_IO_Delay(uint32_t Delay);
- /**
- * @}
- */
- /** @defgroup STM32746G_DISCOVERY_LOW_LEVEL_Exported_Functions STM32746G_DISCOVERY_LOW_LEVELSTM32746G_DISCOVERY_LOW_LEVEL Exported Functions
- * @{
- */
- /**
- * @brief This method returns the STM32746G DISCOVERY BSP Driver revision
- * @retval version: 0xXYZR (8bits for each decimal, R for RC)
- */
- /*******************************************************************************
- BUS OPERATIONS
- *******************************************************************************/
- /******************************* I2C Routines *********************************/
- /**
- * @brief Initializes I2C MSP.
- * @param i2c_handler : I2C handler
- * @retval None
- */
- static void I2Cx_MspInit(I2C_HandleTypeDef *i2c_handler)
- {
- GPIO_InitTypeDef gpio_init_structure;
-
- if (i2c_handler == (I2C_HandleTypeDef*)(&hI2cAudioHandler))
- {
- /* AUDIO and LCD I2C MSP init */
- /*** Configure the GPIOs ***/
- /* Enable GPIO clock */
- DISCOVERY_AUDIO_I2Cx_SCL_SDA_GPIO_CLK_ENABLE();
- /* Configure I2C Tx as alternate function */
- gpio_init_structure.Pin = DISCOVERY_AUDIO_I2Cx_SCL_PIN;
- gpio_init_structure.Mode = GPIO_MODE_AF_OD;
- gpio_init_structure.Pull = GPIO_NOPULL;
- gpio_init_structure.Speed = GPIO_SPEED_FAST;
- gpio_init_structure.Alternate = DISCOVERY_AUDIO_I2Cx_SCL_SDA_AF;
- HAL_GPIO_Init(DISCOVERY_AUDIO_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
- /* Configure I2C Rx as alternate function */
- gpio_init_structure.Pin = DISCOVERY_AUDIO_I2Cx_SDA_PIN;
- HAL_GPIO_Init(DISCOVERY_AUDIO_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
- /*** Configure the I2C peripheral ***/
- /* Enable I2C clock */
- DISCOVERY_AUDIO_I2Cx_CLK_ENABLE();
- /* Force the I2C peripheral clock reset */
- DISCOVERY_AUDIO_I2Cx_FORCE_RESET();
- /* Release the I2C peripheral clock reset */
- DISCOVERY_AUDIO_I2Cx_RELEASE_RESET();
- /* Enable and set I2Cx Interrupt to a lower priority */
- HAL_NVIC_SetPriority(DISCOVERY_AUDIO_I2Cx_EV_IRQn, 0x0F, 0);
- HAL_NVIC_EnableIRQ(DISCOVERY_AUDIO_I2Cx_EV_IRQn);
- /* Enable and set I2Cx Interrupt to a lower priority */
- HAL_NVIC_SetPriority(DISCOVERY_AUDIO_I2Cx_ER_IRQn, 0x0F, 0);
- HAL_NVIC_EnableIRQ(DISCOVERY_AUDIO_I2Cx_ER_IRQn);
- }
- else
- {
- /* External, camera and Arduino connector I2C MSP init */
- /*** Configure the GPIOs ***/
- /* Enable GPIO clock */
- DISCOVERY_EXT_I2Cx_SCL_SDA_GPIO_CLK_ENABLE();
- /* Configure I2C Tx as alternate function */
- gpio_init_structure.Pin = DISCOVERY_EXT_I2Cx_SCL_PIN;
- gpio_init_structure.Mode = GPIO_MODE_AF_OD;
- gpio_init_structure.Pull = GPIO_NOPULL;
- gpio_init_structure.Speed = GPIO_SPEED_FAST;
- gpio_init_structure.Alternate = DISCOVERY_EXT_I2Cx_SCL_SDA_AF;
- HAL_GPIO_Init(DISCOVERY_EXT_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
- /* Configure I2C Rx as alternate function */
- gpio_init_structure.Pin = DISCOVERY_EXT_I2Cx_SDA_PIN;
- HAL_GPIO_Init(DISCOVERY_EXT_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
- /*** Configure the I2C peripheral ***/
- /* Enable I2C clock */
- DISCOVERY_EXT_I2Cx_CLK_ENABLE();
- /* Force the I2C peripheral clock reset */
- DISCOVERY_EXT_I2Cx_FORCE_RESET();
- /* Release the I2C peripheral clock reset */
- DISCOVERY_EXT_I2Cx_RELEASE_RESET();
- /* Enable and set I2Cx Interrupt to a lower priority */
- HAL_NVIC_SetPriority(DISCOVERY_EXT_I2Cx_EV_IRQn, 0x0F, 0);
- HAL_NVIC_EnableIRQ(DISCOVERY_EXT_I2Cx_EV_IRQn);
- /* Enable and set I2Cx Interrupt to a lower priority */
- HAL_NVIC_SetPriority(DISCOVERY_EXT_I2Cx_ER_IRQn, 0x0F, 0);
- HAL_NVIC_EnableIRQ(DISCOVERY_EXT_I2Cx_ER_IRQn);
- }
- }
- /**
- * @brief Initializes I2C HAL.
- * @param i2c_handler : I2C handler
- * @retval None
- */
- static void I2Cx_Init(I2C_HandleTypeDef *i2c_handler)
- {
- if(HAL_I2C_GetState(i2c_handler) == HAL_I2C_STATE_RESET)
- {
- if (i2c_handler == (I2C_HandleTypeDef*)(&hI2cAudioHandler))
- {
- /* Audio and LCD I2C configuration */
- i2c_handler->Instance = DISCOVERY_AUDIO_I2Cx;
- }
- else
- {
- /* External, camera and Arduino connector I2C configuration */
- i2c_handler->Instance = DISCOVERY_EXT_I2Cx;
- }
- i2c_handler->Init.Timing = DISCOVERY_I2Cx_TIMING;
- i2c_handler->Init.OwnAddress1 = 0;
- i2c_handler->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
- i2c_handler->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
- i2c_handler->Init.OwnAddress2 = 0;
- i2c_handler->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
- i2c_handler->Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
- /* Init the I2C */
- I2Cx_MspInit(i2c_handler);
- HAL_I2C_Init(i2c_handler);
- }
- }
- /**
- * @brief Reads multiple data.
- * @param i2c_handler : I2C handler
- * @param Addr: I2C address
- * @param Reg: Reg address
- * @param MemAddress: Memory address
- * @param Buffer: Pointer to data buffer
- * @param Length: Length of the data
- * @retval Number of read data
- */
- static HAL_StatusTypeDef I2Cx_ReadMultiple(I2C_HandleTypeDef *i2c_handler,
- uint8_t Addr,
- uint16_t Reg,
- uint16_t MemAddress,
- uint8_t *Buffer,
- uint16_t Length)
- {
- HAL_StatusTypeDef status = HAL_OK;
- status = HAL_I2C_Mem_Read(i2c_handler, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, 1000);
- /* Check the communication status */
- if(status != HAL_OK)
- {
- /* I2C error occurred */
- I2Cx_Error(i2c_handler, Addr);
- }
- return status;
- }
- /**
- * @brief Writes a value in a register of the device through BUS in using DMA mode.
- * @param i2c_handler : I2C handler
- * @param Addr: Device address on BUS Bus.
- * @param Reg: The target register address to write
- * @param MemAddress: Memory address
- * @param Buffer: The target register value to be written
- * @param Length: buffer size to be written
- * @retval HAL status
- */
- static HAL_StatusTypeDef I2Cx_WriteMultiple(I2C_HandleTypeDef *i2c_handler,
- uint8_t Addr,
- uint16_t Reg,
- uint16_t MemAddress,
- uint8_t *Buffer,
- uint16_t Length)
- {
- HAL_StatusTypeDef status = HAL_OK;
-
- status = HAL_I2C_Mem_Write(i2c_handler, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, 1000);
-
- /* Check the communication status */
- if(status != HAL_OK)
- {
- /* Re-Initiaize the I2C Bus */
- I2Cx_Error(i2c_handler, Addr);
- }
- return status;
- }
- /**
- * @brief Checks if target device is ready for communication.
- * @note This function is used with Memory devices
- * @param i2c_handler : I2C handler
- * @param DevAddress: Target device address
- * @param Trials: Number of trials
- * @retval HAL status
- */
- static HAL_StatusTypeDef I2Cx_IsDeviceReady(I2C_HandleTypeDef *i2c_handler, uint16_t DevAddress, uint32_t Trials)
- {
- return (HAL_I2C_IsDeviceReady(i2c_handler, DevAddress, Trials, 1000));
- }
- /**
- * @brief Manages error callback by re-initializing I2C.
- * @param i2c_handler : I2C handler
- * @param Addr: I2C Address
- * @retval None
- */
- static void I2Cx_Error(I2C_HandleTypeDef *i2c_handler, uint8_t Addr)
- {
- /* De-initialize the I2C communication bus */
- HAL_I2C_DeInit(i2c_handler);
-
- /* Re-Initialize the I2C communication bus */
- I2Cx_Init(i2c_handler);
- }
- /*******************************************************************************
- LINK OPERATIONS
- *******************************************************************************/
- /********************************* LINK AUDIO *********************************/
- /**
- * @brief Initializes Audio low level.
- * @retval None
- */
- void AUDIO_IO_Init(void)
- {
- I2Cx_Init(&hI2cAudioHandler);
- }
- /**
- * @brief Deinitializes Audio low level.
- * @retval None
- */
- void AUDIO_IO_DeInit(void)
- {
- }
- /**
- * @brief Writes a single data.
- * @param Addr: I2C address
- * @param Reg: Reg address
- * @param Value: Data to be written
- * @retval None
- */
- void AUDIO_IO_Write(uint8_t Addr, uint16_t Reg, uint16_t Value)
- {
- uint16_t tmp = Value;
-
- Value = ((uint16_t)(tmp >> 8) & 0x00FF);
-
- Value |= ((uint16_t)(tmp << 8)& 0xFF00);
-
- I2Cx_WriteMultiple(&hI2cAudioHandler, Addr, Reg, I2C_MEMADD_SIZE_16BIT,(uint8_t*)&Value, 2);
- }
- /**
- * @brief Reads a single data.
- * @param Addr: I2C address
- * @param Reg: Reg address
- * @retval Data to be read
- */
- uint16_t AUDIO_IO_Read(uint8_t Addr, uint16_t Reg)
- {
- uint16_t read_value = 0, tmp = 0;
-
- I2Cx_ReadMultiple(&hI2cAudioHandler, Addr, Reg, I2C_MEMADD_SIZE_16BIT, (uint8_t*)&read_value, 2);
-
- tmp = ((uint16_t)(read_value >> 8) & 0x00FF);
-
- tmp |= ((uint16_t)(read_value << 8)& 0xFF00);
-
- read_value = tmp;
-
- return read_value;
- }
- /**
- * @brief AUDIO Codec delay
- * @param Delay: Delay in ms
- * @retval None
- */
- void AUDIO_IO_Delay(uint32_t Delay)
- {
- HAL_Delay(Delay);
- }
- /********************************* I2C nunchuck controller **************************/
- void DJ_IO_Init(void)
- {
- I2Cx_Init(&hI2cExtHandler);
- }
- void DJ_IO_Write(uint8_t Addr, uint32_t Reg, uint8_t Value)
- {
- I2Cx_WriteMultiple(&hI2cExtHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT,(uint8_t*)&Value, 1);
- }
- static uint8_t djhero_buf[6];
- uint8_t *DJ_IO_Read(uint8_t Addr, uint8_t Reg)
- {
- uint8_t read_value = 0;
- I2Cx_ReadMultiple(&hI2cExtHandler, Addr, Reg, I2C_MEMADD_SIZE_8BIT, djhero_buf, 6);
- return djhero_buf;
- }
- void DJ_IO_Delay(uint32_t Delay)
- {
- HAL_Delay(Delay);
- }
- /********************************* LINK TOUCHSCREEN *********************************/
- /**
- * @brief Initializes Touchscreen low level.
- * @retval None
- */
- void TS_IO_Init(void)
- {
- I2Cx_Init(&hI2cAudioHandler);
- }
- /**
- * @brief Writes a single data.
- * @param Addr: I2C address
- * @param Reg: Reg address
- * @param Value: Data to be written
- * @retval None
- */
- void TS_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
- {
- I2Cx_WriteMultiple(&hI2cAudioHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT,(uint8_t*)&Value, 1);
- }
- /**
- * @brief Reads a single data.
- * @param Addr: I2C address
- * @param Reg: Reg address
- * @retval Data to be read
- */
- uint8_t TS_IO_Read(uint8_t Addr, uint8_t Reg)
- {
- uint8_t read_value = 0;
- I2Cx_ReadMultiple(&hI2cAudioHandler, Addr, Reg, I2C_MEMADD_SIZE_8BIT, (uint8_t*)&read_value, 1);
- return read_value;
- }
- /**
- * @brief TS delay
- * @param Delay: Delay in ms
- * @retval None
- */
- void TS_IO_Delay(uint32_t Delay)
- {
- HAL_Delay(Delay);
- }
- /**
- * @}
- */
- /**
- * @}
- */
- /**
- * @}
- */
- /**
- * @}
- */
-
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|