BSP Drivers
Dependencies: CMSIS_STM32L4xx CMSIS_DSP_401 STM32L4xx_HAL_Driver
Disco/stm32l476g_discovery.c@4:a1219ef3537f, 2015-11-22 (annotated)
- Committer:
- EricLew
- Date:
- Sun Nov 22 21:15:34 2015 +0000
- Revision:
- 4:a1219ef3537f
- Parent:
- 0:ad9dfc0179dc
11/22/2015
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
EricLew | 0:ad9dfc0179dc | 1 | /** |
EricLew | 0:ad9dfc0179dc | 2 | ****************************************************************************** |
EricLew | 0:ad9dfc0179dc | 3 | * @file stm32l476g_discovery.c |
EricLew | 0:ad9dfc0179dc | 4 | * @author MCD Application Team |
EricLew | 0:ad9dfc0179dc | 5 | * @version V1.0.1 |
EricLew | 0:ad9dfc0179dc | 6 | * @date 16-September-2015 |
EricLew | 0:ad9dfc0179dc | 7 | * @brief This file provides a set of firmware functions to manage Leds, |
EricLew | 0:ad9dfc0179dc | 8 | * push-button and joystick of STM32L476G-Discovery board (MB1184) |
EricLew | 0:ad9dfc0179dc | 9 | ****************************************************************************** |
EricLew | 0:ad9dfc0179dc | 10 | * @attention |
EricLew | 0:ad9dfc0179dc | 11 | * |
EricLew | 0:ad9dfc0179dc | 12 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
EricLew | 0:ad9dfc0179dc | 13 | * |
EricLew | 0:ad9dfc0179dc | 14 | * Redistribution and use in source and binary forms, with or without modification, |
EricLew | 0:ad9dfc0179dc | 15 | * are permitted provided that the following conditions are met: |
EricLew | 0:ad9dfc0179dc | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
EricLew | 0:ad9dfc0179dc | 17 | * this list of conditions and the following disclaimer. |
EricLew | 0:ad9dfc0179dc | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
EricLew | 0:ad9dfc0179dc | 19 | * this list of conditions and the following disclaimer in the documentation |
EricLew | 0:ad9dfc0179dc | 20 | * and/or other materials provided with the distribution. |
EricLew | 0:ad9dfc0179dc | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
EricLew | 0:ad9dfc0179dc | 22 | * may be used to endorse or promote products derived from this software |
EricLew | 0:ad9dfc0179dc | 23 | * without specific prior written permission. |
EricLew | 0:ad9dfc0179dc | 24 | * |
EricLew | 0:ad9dfc0179dc | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
EricLew | 0:ad9dfc0179dc | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
EricLew | 0:ad9dfc0179dc | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
EricLew | 0:ad9dfc0179dc | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
EricLew | 0:ad9dfc0179dc | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
EricLew | 0:ad9dfc0179dc | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
EricLew | 0:ad9dfc0179dc | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
EricLew | 0:ad9dfc0179dc | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
EricLew | 0:ad9dfc0179dc | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
EricLew | 0:ad9dfc0179dc | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
EricLew | 0:ad9dfc0179dc | 35 | * |
EricLew | 0:ad9dfc0179dc | 36 | ****************************************************************************** |
EricLew | 0:ad9dfc0179dc | 37 | */ |
EricLew | 0:ad9dfc0179dc | 38 | |
EricLew | 0:ad9dfc0179dc | 39 | /* Includes ------------------------------------------------------------------*/ |
EricLew | 0:ad9dfc0179dc | 40 | #include "stm32l476g_discovery.h" |
EricLew | 0:ad9dfc0179dc | 41 | |
EricLew | 0:ad9dfc0179dc | 42 | /** @addtogroup BSP |
EricLew | 0:ad9dfc0179dc | 43 | * @{ |
EricLew | 0:ad9dfc0179dc | 44 | */ |
EricLew | 0:ad9dfc0179dc | 45 | |
EricLew | 0:ad9dfc0179dc | 46 | /** @defgroup STM32L476G_DISCOVERY STM32L476G-DISCOVERY |
EricLew | 0:ad9dfc0179dc | 47 | * @{ |
EricLew | 0:ad9dfc0179dc | 48 | */ |
EricLew | 0:ad9dfc0179dc | 49 | |
EricLew | 0:ad9dfc0179dc | 50 | /** @defgroup STM32L476G_DISCOVERY_Common STM32L476G-DISCOVERY Common |
EricLew | 0:ad9dfc0179dc | 51 | * @{ |
EricLew | 0:ad9dfc0179dc | 52 | */ |
EricLew | 0:ad9dfc0179dc | 53 | |
EricLew | 0:ad9dfc0179dc | 54 | /** @defgroup STM32L476G_DISCOVERY_Private_TypesDefinitions Private Types Definitions |
EricLew | 0:ad9dfc0179dc | 55 | * @brief This file provides firmware functions to manage Leds, push-buttons, |
EricLew | 0:ad9dfc0179dc | 56 | * COM ports, SD card on SPI and temperature sensor (TS751) available on |
EricLew | 0:ad9dfc0179dc | 57 | * STM32L476G-DISCOVERY discoveryuation board from STMicroelectronics. |
EricLew | 0:ad9dfc0179dc | 58 | * @{ |
EricLew | 0:ad9dfc0179dc | 59 | */ |
EricLew | 0:ad9dfc0179dc | 60 | |
EricLew | 0:ad9dfc0179dc | 61 | /** |
EricLew | 0:ad9dfc0179dc | 62 | * @} |
EricLew | 0:ad9dfc0179dc | 63 | */ |
EricLew | 0:ad9dfc0179dc | 64 | |
EricLew | 0:ad9dfc0179dc | 65 | /** @defgroup STM32L476G_DISCOVERY_Private_Defines Private Defines |
EricLew | 0:ad9dfc0179dc | 66 | * @{ |
EricLew | 0:ad9dfc0179dc | 67 | */ |
EricLew | 0:ad9dfc0179dc | 68 | |
EricLew | 0:ad9dfc0179dc | 69 | /** |
EricLew | 0:ad9dfc0179dc | 70 | * @brief STM32L476G DISCOVERY BSP Driver version number V1.0.1 |
EricLew | 0:ad9dfc0179dc | 71 | */ |
EricLew | 0:ad9dfc0179dc | 72 | #define __STM32L476G_DISCOVERY_BSP_VERSION_MAIN (0x01) /*!< [31:24] main version */ |
EricLew | 0:ad9dfc0179dc | 73 | #define __STM32L476G_DISCOVERY_BSP_VERSION_SUB1 (0x00) /*!< [23:16] sub1 version */ |
EricLew | 0:ad9dfc0179dc | 74 | #define __STM32L476G_DISCOVERY_BSP_VERSION_SUB2 (0x01) /*!< [15:8] sub2 version */ |
EricLew | 0:ad9dfc0179dc | 75 | #define __STM32L476G_DISCOVERY_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */ |
EricLew | 0:ad9dfc0179dc | 76 | #define __STM32L476G_DISCOVERY_BSP_VERSION ((__STM32L476G_DISCOVERY_BSP_VERSION_MAIN << 24)\ |
EricLew | 0:ad9dfc0179dc | 77 | |(__STM32L476G_DISCOVERY_BSP_VERSION_SUB1 << 16)\ |
EricLew | 0:ad9dfc0179dc | 78 | |(__STM32L476G_DISCOVERY_BSP_VERSION_SUB2 << 8 )\ |
EricLew | 0:ad9dfc0179dc | 79 | |(__STM32L476G_DISCOVERY_BSP_VERSION_RC)) |
EricLew | 0:ad9dfc0179dc | 80 | /** |
EricLew | 0:ad9dfc0179dc | 81 | * @} |
EricLew | 0:ad9dfc0179dc | 82 | */ |
EricLew | 0:ad9dfc0179dc | 83 | |
EricLew | 0:ad9dfc0179dc | 84 | |
EricLew | 0:ad9dfc0179dc | 85 | /** @defgroup STM32L476G_DISCOVERY_Private_Macros Private Macros |
EricLew | 0:ad9dfc0179dc | 86 | * @{ |
EricLew | 0:ad9dfc0179dc | 87 | */ |
EricLew | 0:ad9dfc0179dc | 88 | |
EricLew | 0:ad9dfc0179dc | 89 | /** |
EricLew | 0:ad9dfc0179dc | 90 | * @} |
EricLew | 0:ad9dfc0179dc | 91 | */ |
EricLew | 0:ad9dfc0179dc | 92 | |
EricLew | 0:ad9dfc0179dc | 93 | |
EricLew | 0:ad9dfc0179dc | 94 | /** @defgroup STM32L476G_DISCOVERY_Exported_Variables Exported Variables |
EricLew | 0:ad9dfc0179dc | 95 | * @{ |
EricLew | 0:ad9dfc0179dc | 96 | */ |
EricLew | 0:ad9dfc0179dc | 97 | |
EricLew | 0:ad9dfc0179dc | 98 | /** |
EricLew | 0:ad9dfc0179dc | 99 | * @brief LED variables |
EricLew | 0:ad9dfc0179dc | 100 | */ |
EricLew | 0:ad9dfc0179dc | 101 | #if defined (USE_STM32L476G_DISCO_REVC) || defined (USE_STM32L476G_DISCO_REVB) |
EricLew | 0:ad9dfc0179dc | 102 | GPIO_TypeDef* LED_PORT[LEDn] = {LED4_GPIO_PORT, |
EricLew | 0:ad9dfc0179dc | 103 | LED5_GPIO_PORT}; |
EricLew | 0:ad9dfc0179dc | 104 | |
EricLew | 0:ad9dfc0179dc | 105 | const uint16_t LED_PIN[LEDn] = {LED4_PIN, |
EricLew | 0:ad9dfc0179dc | 106 | LED5_PIN}; |
EricLew | 0:ad9dfc0179dc | 107 | #elif defined (USE_STM32L476G_DISCO_REVA) |
EricLew | 0:ad9dfc0179dc | 108 | GPIO_TypeDef* LED_PORT[LEDn] = {LED3_GPIO_PORT, |
EricLew | 0:ad9dfc0179dc | 109 | LED4_GPIO_PORT}; |
EricLew | 0:ad9dfc0179dc | 110 | |
EricLew | 0:ad9dfc0179dc | 111 | const uint16_t LED_PIN[LEDn] = {LED3_PIN, |
EricLew | 0:ad9dfc0179dc | 112 | LED4_PIN}; |
EricLew | 0:ad9dfc0179dc | 113 | #endif |
EricLew | 0:ad9dfc0179dc | 114 | |
EricLew | 0:ad9dfc0179dc | 115 | |
EricLew | 0:ad9dfc0179dc | 116 | /** |
EricLew | 0:ad9dfc0179dc | 117 | * @brief JOYSTICK variables |
EricLew | 0:ad9dfc0179dc | 118 | */ |
EricLew | 0:ad9dfc0179dc | 119 | GPIO_TypeDef* JOY_PORT[JOYn] = {SEL_JOY_GPIO_PORT, |
EricLew | 0:ad9dfc0179dc | 120 | DOWN_JOY_GPIO_PORT, |
EricLew | 0:ad9dfc0179dc | 121 | LEFT_JOY_GPIO_PORT, |
EricLew | 0:ad9dfc0179dc | 122 | RIGHT_JOY_GPIO_PORT, |
EricLew | 0:ad9dfc0179dc | 123 | UP_JOY_GPIO_PORT}; |
EricLew | 0:ad9dfc0179dc | 124 | |
EricLew | 0:ad9dfc0179dc | 125 | const uint16_t JOY_PIN[JOYn] = {SEL_JOY_PIN, |
EricLew | 0:ad9dfc0179dc | 126 | LEFT_JOY_PIN, |
EricLew | 0:ad9dfc0179dc | 127 | RIGHT_JOY_PIN, |
EricLew | 0:ad9dfc0179dc | 128 | DOWN_JOY_PIN, |
EricLew | 0:ad9dfc0179dc | 129 | UP_JOY_PIN}; |
EricLew | 0:ad9dfc0179dc | 130 | |
EricLew | 0:ad9dfc0179dc | 131 | const uint8_t JOY_IRQn[JOYn] = {SEL_JOY_EXTI_IRQn, |
EricLew | 0:ad9dfc0179dc | 132 | LEFT_JOY_EXTI_IRQn, |
EricLew | 0:ad9dfc0179dc | 133 | RIGHT_JOY_EXTI_IRQn, |
EricLew | 0:ad9dfc0179dc | 134 | DOWN_JOY_EXTI_IRQn, |
EricLew | 0:ad9dfc0179dc | 135 | UP_JOY_EXTI_IRQn}; |
EricLew | 0:ad9dfc0179dc | 136 | |
EricLew | 0:ad9dfc0179dc | 137 | /** |
EricLew | 0:ad9dfc0179dc | 138 | * @brief BUS variables |
EricLew | 0:ad9dfc0179dc | 139 | */ |
EricLew | 0:ad9dfc0179dc | 140 | #if defined(HAL_I2C_MODULE_ENABLED) |
EricLew | 0:ad9dfc0179dc | 141 | uint32_t I2c1Timeout = DISCOVERY_I2C2_TIMEOUT_MAX; /*<! Value of Timeout when I2C1 communication fails */ |
EricLew | 0:ad9dfc0179dc | 142 | uint32_t I2c2Timeout = DISCOVERY_I2C2_TIMEOUT_MAX; /*<! Value of Timeout when I2C2 communication fails */ |
EricLew | 0:ad9dfc0179dc | 143 | static I2C_HandleTypeDef I2c1Handle; |
EricLew | 0:ad9dfc0179dc | 144 | static I2C_HandleTypeDef I2c2Handle; |
EricLew | 0:ad9dfc0179dc | 145 | #endif /* HAL_I2C_MODULE_ENABLED */ |
EricLew | 0:ad9dfc0179dc | 146 | |
EricLew | 0:ad9dfc0179dc | 147 | #if defined(HAL_SPI_MODULE_ENABLED) |
EricLew | 0:ad9dfc0179dc | 148 | |
EricLew | 0:ad9dfc0179dc | 149 | /* LL definition */ |
EricLew | 0:ad9dfc0179dc | 150 | #define __SPI_DIRECTION_2LINES(__HANDLE__) do{\ |
EricLew | 0:ad9dfc0179dc | 151 | CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_RXONLY | SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE);\ |
EricLew | 0:ad9dfc0179dc | 152 | }while(0); |
EricLew | 0:ad9dfc0179dc | 153 | |
EricLew | 0:ad9dfc0179dc | 154 | #define __SPI_DIRECTION_2LINES_RXONLY(__HANDLE__) do{\ |
EricLew | 0:ad9dfc0179dc | 155 | CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_RXONLY | SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE);\ |
EricLew | 0:ad9dfc0179dc | 156 | SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_RXONLY);\ |
EricLew | 0:ad9dfc0179dc | 157 | }while(0); |
EricLew | 0:ad9dfc0179dc | 158 | |
EricLew | 0:ad9dfc0179dc | 159 | #define __SPI_DIRECTION_1LINE_TX(__HANDLE__) do{\ |
EricLew | 0:ad9dfc0179dc | 160 | CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_RXONLY | SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE);\ |
EricLew | 0:ad9dfc0179dc | 161 | SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE);\ |
EricLew | 0:ad9dfc0179dc | 162 | }while(0); |
EricLew | 0:ad9dfc0179dc | 163 | |
EricLew | 0:ad9dfc0179dc | 164 | #define __SPI_DIRECTION_1LINE_RX(__HANDLE__) do {\ |
EricLew | 0:ad9dfc0179dc | 165 | CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_RXONLY | SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE);\ |
EricLew | 0:ad9dfc0179dc | 166 | SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_BIDIMODE);\ |
EricLew | 0:ad9dfc0179dc | 167 | } while(0); |
EricLew | 0:ad9dfc0179dc | 168 | |
EricLew | 0:ad9dfc0179dc | 169 | |
EricLew | 0:ad9dfc0179dc | 170 | uint32_t SpixTimeout = SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */ |
EricLew | 0:ad9dfc0179dc | 171 | static SPI_HandleTypeDef SpiHandle; |
EricLew | 0:ad9dfc0179dc | 172 | #endif /* HAL_SPI_MODULE_ENABLED */ |
EricLew | 0:ad9dfc0179dc | 173 | |
EricLew | 0:ad9dfc0179dc | 174 | /** |
EricLew | 0:ad9dfc0179dc | 175 | * @} |
EricLew | 0:ad9dfc0179dc | 176 | */ |
EricLew | 0:ad9dfc0179dc | 177 | |
EricLew | 0:ad9dfc0179dc | 178 | /** @defgroup STM32L476G_DISCOVERY_Private_FunctionPrototypes Private Functions |
EricLew | 0:ad9dfc0179dc | 179 | * @{ |
EricLew | 0:ad9dfc0179dc | 180 | */ |
EricLew | 0:ad9dfc0179dc | 181 | /**************************** Bus functions ************************************/ |
EricLew | 0:ad9dfc0179dc | 182 | /* I2C2 bus function */ |
EricLew | 0:ad9dfc0179dc | 183 | #if defined(HAL_I2C_MODULE_ENABLED) |
EricLew | 0:ad9dfc0179dc | 184 | static void I2C2_Init(void); |
EricLew | 0:ad9dfc0179dc | 185 | static void I2C2_MspInit(I2C_HandleTypeDef *hi2c); |
EricLew | 0:ad9dfc0179dc | 186 | static void I2C2_DeInit(void); |
EricLew | 0:ad9dfc0179dc | 187 | static void I2C2_MspDeInit(I2C_HandleTypeDef *hi2c); |
EricLew | 0:ad9dfc0179dc | 188 | static void I2C2_WriteData(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t Value); |
EricLew | 0:ad9dfc0179dc | 189 | static HAL_StatusTypeDef I2C2_WriteBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length); |
EricLew | 0:ad9dfc0179dc | 190 | static uint8_t I2C2_ReadData(uint16_t Addr, uint16_t Reg, uint16_t RegSize); |
EricLew | 0:ad9dfc0179dc | 191 | static HAL_StatusTypeDef I2C2_ReadBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length); |
EricLew | 0:ad9dfc0179dc | 192 | static void I2C2_Error (void); |
EricLew | 0:ad9dfc0179dc | 193 | |
EricLew | 0:ad9dfc0179dc | 194 | static void I2C1_Init(void); |
EricLew | 0:ad9dfc0179dc | 195 | static void I2C1_MspInit(I2C_HandleTypeDef *hi2c); |
EricLew | 0:ad9dfc0179dc | 196 | static void I2C1_DeInit(void); |
EricLew | 0:ad9dfc0179dc | 197 | static void I2C1_MspDeInit(I2C_HandleTypeDef *hi2c); |
EricLew | 0:ad9dfc0179dc | 198 | static HAL_StatusTypeDef I2C1_WriteBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length); |
EricLew | 0:ad9dfc0179dc | 199 | static HAL_StatusTypeDef I2C1_ReadBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length); |
EricLew | 0:ad9dfc0179dc | 200 | static void I2C1_Error (void); |
EricLew | 0:ad9dfc0179dc | 201 | #endif/* HAL_I2C_MODULE_ENABLED */ |
EricLew | 0:ad9dfc0179dc | 202 | |
EricLew | 0:ad9dfc0179dc | 203 | /* SPIx bus function */ |
EricLew | 0:ad9dfc0179dc | 204 | #if defined(HAL_SPI_MODULE_ENABLED) |
EricLew | 0:ad9dfc0179dc | 205 | static void SPIx_Init(void); |
EricLew | 0:ad9dfc0179dc | 206 | static void SPIx_MspInit(SPI_HandleTypeDef *hspi); |
EricLew | 0:ad9dfc0179dc | 207 | static void SPIx_DeInit(void); |
EricLew | 0:ad9dfc0179dc | 208 | static void SPIx_MspDeInit(void); |
EricLew | 0:ad9dfc0179dc | 209 | static uint8_t SPIx_WriteRead(uint8_t Byte); |
EricLew | 0:ad9dfc0179dc | 210 | static void SPIx_Write(uint8_t byte); |
EricLew | 0:ad9dfc0179dc | 211 | static uint8_t SPIx_Read(void); |
EricLew | 0:ad9dfc0179dc | 212 | #endif |
EricLew | 0:ad9dfc0179dc | 213 | |
EricLew | 0:ad9dfc0179dc | 214 | /**************************** Link functions ***********************************/ |
EricLew | 0:ad9dfc0179dc | 215 | #if defined(HAL_I2C_MODULE_ENABLED) |
EricLew | 0:ad9dfc0179dc | 216 | /* Link functions for EEPROM peripheral over I2C */ |
EricLew | 0:ad9dfc0179dc | 217 | void EEPROM_I2C_IO_Init(void); |
EricLew | 0:ad9dfc0179dc | 218 | HAL_StatusTypeDef EEPROM_I2C_IO_WriteData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize); |
EricLew | 0:ad9dfc0179dc | 219 | HAL_StatusTypeDef EEPROM_I2C_IO_ReadData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize); |
EricLew | 0:ad9dfc0179dc | 220 | HAL_StatusTypeDef EEPROM_I2C_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials); |
EricLew | 0:ad9dfc0179dc | 221 | |
EricLew | 0:ad9dfc0179dc | 222 | /* Link functions for Audio Codec peripheral */ |
EricLew | 0:ad9dfc0179dc | 223 | void AUDIO_IO_Init(void); |
EricLew | 0:ad9dfc0179dc | 224 | void AUDIO_IO_DeInit(void); |
EricLew | 0:ad9dfc0179dc | 225 | void AUDIO_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value); |
EricLew | 0:ad9dfc0179dc | 226 | uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg); |
EricLew | 0:ad9dfc0179dc | 227 | void AUDIO_IO_Delay(uint32_t delay); |
EricLew | 0:ad9dfc0179dc | 228 | #endif/* HAL_I2C_MODULE_ENABLED */ |
EricLew | 0:ad9dfc0179dc | 229 | |
EricLew | 0:ad9dfc0179dc | 230 | #if defined(HAL_SPI_MODULE_ENABLED) |
EricLew | 0:ad9dfc0179dc | 231 | /* Link function for COMPASS / ACCELERO peripheral */ |
EricLew | 0:ad9dfc0179dc | 232 | void ACCELERO_IO_Init(void); |
EricLew | 0:ad9dfc0179dc | 233 | void ACCELERO_IO_DeInit(void); |
EricLew | 0:ad9dfc0179dc | 234 | void ACCELERO_IO_ITConfig(void); |
EricLew | 0:ad9dfc0179dc | 235 | void ACCELERO_IO_Write(uint8_t RegisterAddr, uint8_t Value); |
EricLew | 0:ad9dfc0179dc | 236 | uint8_t ACCELERO_IO_Read(uint8_t RegisterAddr); |
EricLew | 0:ad9dfc0179dc | 237 | |
EricLew | 0:ad9dfc0179dc | 238 | void MAGNETO_IO_Init(void); |
EricLew | 0:ad9dfc0179dc | 239 | void MAGNETO_IO_DeInit(void); |
EricLew | 0:ad9dfc0179dc | 240 | void MAGNETO_IO_ITConfig(void); |
EricLew | 0:ad9dfc0179dc | 241 | void MAGNETO_IO_Write(uint8_t RegisterAddr, uint8_t Value); |
EricLew | 0:ad9dfc0179dc | 242 | uint8_t MAGNETO_IO_Read(uint8_t RegisterAddr); |
EricLew | 0:ad9dfc0179dc | 243 | |
EricLew | 0:ad9dfc0179dc | 244 | |
EricLew | 0:ad9dfc0179dc | 245 | /* Link functions for GYRO peripheral */ |
EricLew | 0:ad9dfc0179dc | 246 | void GYRO_IO_Init(void); |
EricLew | 0:ad9dfc0179dc | 247 | void GYRO_IO_DeInit(void); |
EricLew | 0:ad9dfc0179dc | 248 | void GYRO_IO_Write(uint8_t* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite); |
EricLew | 0:ad9dfc0179dc | 249 | void GYRO_IO_Read(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead); |
EricLew | 0:ad9dfc0179dc | 250 | |
EricLew | 0:ad9dfc0179dc | 251 | #endif |
EricLew | 0:ad9dfc0179dc | 252 | |
EricLew | 0:ad9dfc0179dc | 253 | #if defined(HAL_I2C_MODULE_ENABLED) |
EricLew | 0:ad9dfc0179dc | 254 | /* Link functions IOExpander */ |
EricLew | 0:ad9dfc0179dc | 255 | void IOE_Init(void); |
EricLew | 0:ad9dfc0179dc | 256 | void IOE_ITConfig(void); |
EricLew | 0:ad9dfc0179dc | 257 | void IOE_Delay(uint32_t Delay); |
EricLew | 0:ad9dfc0179dc | 258 | void IOE_Write(uint8_t Addr, uint8_t Reg, uint8_t Value); |
EricLew | 0:ad9dfc0179dc | 259 | uint8_t IOE_Read(uint8_t Addr, uint8_t Reg); |
EricLew | 0:ad9dfc0179dc | 260 | uint16_t IOE_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length); |
EricLew | 0:ad9dfc0179dc | 261 | |
EricLew | 0:ad9dfc0179dc | 262 | /* Link functions for IDD measurment */ |
EricLew | 0:ad9dfc0179dc | 263 | void MFX_IO_Init(void); |
EricLew | 0:ad9dfc0179dc | 264 | void MFX_IO_DeInit(void); |
EricLew | 0:ad9dfc0179dc | 265 | void MFX_IO_ITConfig (void); |
EricLew | 0:ad9dfc0179dc | 266 | void MFX_IO_EnableWakeupPin(void); |
EricLew | 0:ad9dfc0179dc | 267 | void MFX_IO_Wakeup(void); |
EricLew | 0:ad9dfc0179dc | 268 | void MFX_IO_Delay(uint32_t delay); |
EricLew | 0:ad9dfc0179dc | 269 | void MFX_IO_Write(uint16_t addr, uint8_t reg, uint8_t value); |
EricLew | 0:ad9dfc0179dc | 270 | uint8_t MFX_IO_Read(uint16_t addr, uint8_t reg); |
EricLew | 0:ad9dfc0179dc | 271 | void MFX_IO_WriteMultiple(uint16_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length); |
EricLew | 0:ad9dfc0179dc | 272 | uint16_t MFX_IO_ReadMultiple(uint16_t addr, uint8_t reg, uint8_t *buffer, uint16_t length); |
EricLew | 0:ad9dfc0179dc | 273 | #endif/* HAL_I2C_MODULE_ENABLED */ |
EricLew | 0:ad9dfc0179dc | 274 | /** |
EricLew | 0:ad9dfc0179dc | 275 | * @} |
EricLew | 0:ad9dfc0179dc | 276 | */ |
EricLew | 0:ad9dfc0179dc | 277 | |
EricLew | 0:ad9dfc0179dc | 278 | /** @defgroup STM32L476G_DISCOVERY_Exported_Functions Exported Functions |
EricLew | 0:ad9dfc0179dc | 279 | * @{ |
EricLew | 0:ad9dfc0179dc | 280 | */ |
EricLew | 0:ad9dfc0179dc | 281 | |
EricLew | 0:ad9dfc0179dc | 282 | /** |
EricLew | 0:ad9dfc0179dc | 283 | * @brief This method returns the STM32L476 DISCOVERY BSP Driver revision |
EricLew | 0:ad9dfc0179dc | 284 | * @retval version : 0xXYZR (8bits for each decimal, R for RC) |
EricLew | 0:ad9dfc0179dc | 285 | */ |
EricLew | 0:ad9dfc0179dc | 286 | uint32_t BSP_GetVersion(void) |
EricLew | 0:ad9dfc0179dc | 287 | { |
EricLew | 0:ad9dfc0179dc | 288 | return __STM32L476G_DISCOVERY_BSP_VERSION; |
EricLew | 0:ad9dfc0179dc | 289 | } |
EricLew | 0:ad9dfc0179dc | 290 | |
EricLew | 0:ad9dfc0179dc | 291 | /** |
EricLew | 0:ad9dfc0179dc | 292 | * @brief This method returns the STM32L476 DISCOVERY supply mode |
EricLew | 0:ad9dfc0179dc | 293 | * @retval Code of current supply mode |
EricLew | 0:ad9dfc0179dc | 294 | * This code can be one of following: |
EricLew | 0:ad9dfc0179dc | 295 | * @arg SUPPLY_MODE_EXTERNAL |
EricLew | 0:ad9dfc0179dc | 296 | * @arg SUPPLY_MODE_BATTERY |
EricLew | 0:ad9dfc0179dc | 297 | */ |
EricLew | 0:ad9dfc0179dc | 298 | SupplyMode_TypeDef BSP_SupplyModeDetection(void) |
EricLew | 0:ad9dfc0179dc | 299 | { |
EricLew | 0:ad9dfc0179dc | 300 | SupplyMode_TypeDef supplymode = SUPPLY_MODE_ERROR; |
EricLew | 0:ad9dfc0179dc | 301 | GPIO_InitTypeDef GPIO_InitStruct; |
EricLew | 0:ad9dfc0179dc | 302 | |
EricLew | 0:ad9dfc0179dc | 303 | BATTERY_DETECTION_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 304 | |
EricLew | 0:ad9dfc0179dc | 305 | /* COMP GPIO pin configuration */ |
EricLew | 0:ad9dfc0179dc | 306 | GPIO_InitStruct.Pin = BATTERY_DETECTION_PIN; |
EricLew | 0:ad9dfc0179dc | 307 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
EricLew | 0:ad9dfc0179dc | 308 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
EricLew | 0:ad9dfc0179dc | 309 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 0:ad9dfc0179dc | 310 | HAL_GPIO_Init(BATTERY_DETECTION_GPIO_PORT, &GPIO_InitStruct); |
EricLew | 0:ad9dfc0179dc | 311 | |
EricLew | 0:ad9dfc0179dc | 312 | HAL_Delay(400); |
EricLew | 0:ad9dfc0179dc | 313 | if(HAL_GPIO_ReadPin(BATTERY_DETECTION_GPIO_PORT, GPIO_InitStruct.Pin) != GPIO_PIN_RESET) |
EricLew | 0:ad9dfc0179dc | 314 | { |
EricLew | 0:ad9dfc0179dc | 315 | supplymode = SUPPLY_MODE_EXTERNAL; |
EricLew | 0:ad9dfc0179dc | 316 | } |
EricLew | 0:ad9dfc0179dc | 317 | else |
EricLew | 0:ad9dfc0179dc | 318 | { |
EricLew | 0:ad9dfc0179dc | 319 | supplymode = SUPPLY_MODE_BATTERY; |
EricLew | 0:ad9dfc0179dc | 320 | } |
EricLew | 0:ad9dfc0179dc | 321 | |
EricLew | 0:ad9dfc0179dc | 322 | HAL_GPIO_DeInit(BATTERY_DETECTION_GPIO_PORT, GPIO_InitStruct.Pin); |
EricLew | 0:ad9dfc0179dc | 323 | |
EricLew | 0:ad9dfc0179dc | 324 | return supplymode; |
EricLew | 0:ad9dfc0179dc | 325 | } |
EricLew | 0:ad9dfc0179dc | 326 | |
EricLew | 0:ad9dfc0179dc | 327 | #if defined (USE_STM32L476G_DISCO_REVC) || defined (USE_STM32L476G_DISCO_REVB) |
EricLew | 0:ad9dfc0179dc | 328 | /** |
EricLew | 0:ad9dfc0179dc | 329 | * @brief Configures LED GPIOs. |
EricLew | 0:ad9dfc0179dc | 330 | * @param Led: Specifies the Led to be configured. |
EricLew | 0:ad9dfc0179dc | 331 | * This parameter can be one of following parameters: |
EricLew | 0:ad9dfc0179dc | 332 | * @arg LED4 |
EricLew | 0:ad9dfc0179dc | 333 | * @arg LED5 |
EricLew | 0:ad9dfc0179dc | 334 | * @retval None |
EricLew | 0:ad9dfc0179dc | 335 | */ |
EricLew | 0:ad9dfc0179dc | 336 | #elif defined (USE_STM32L476G_DISCO_REVA) |
EricLew | 0:ad9dfc0179dc | 337 | /** |
EricLew | 0:ad9dfc0179dc | 338 | * @brief Configures LED GPIOs. |
EricLew | 0:ad9dfc0179dc | 339 | * @param Led: Specifies the Led to be configured. |
EricLew | 0:ad9dfc0179dc | 340 | * This parameter can be one of following parameters: |
EricLew | 0:ad9dfc0179dc | 341 | * @arg LED3 |
EricLew | 0:ad9dfc0179dc | 342 | * @arg LED4 |
EricLew | 0:ad9dfc0179dc | 343 | * @retval None |
EricLew | 0:ad9dfc0179dc | 344 | */ |
EricLew | 0:ad9dfc0179dc | 345 | #endif |
EricLew | 0:ad9dfc0179dc | 346 | void BSP_LED_Init(Led_TypeDef Led) |
EricLew | 0:ad9dfc0179dc | 347 | { |
EricLew | 0:ad9dfc0179dc | 348 | GPIO_InitTypeDef GPIO_InitStructure; |
EricLew | 0:ad9dfc0179dc | 349 | |
EricLew | 0:ad9dfc0179dc | 350 | /* Enable the GPIO_LED clock */ |
EricLew | 0:ad9dfc0179dc | 351 | LEDx_GPIO_CLK_ENABLE(Led); |
EricLew | 0:ad9dfc0179dc | 352 | |
EricLew | 0:ad9dfc0179dc | 353 | /* Configure the GPIO_LED pin */ |
EricLew | 0:ad9dfc0179dc | 354 | GPIO_InitStructure.Pin = LED_PIN[Led]; |
EricLew | 0:ad9dfc0179dc | 355 | GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
EricLew | 0:ad9dfc0179dc | 356 | GPIO_InitStructure.Pull = GPIO_NOPULL; |
EricLew | 0:ad9dfc0179dc | 357 | GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 0:ad9dfc0179dc | 358 | |
EricLew | 0:ad9dfc0179dc | 359 | HAL_GPIO_Init(LED_PORT[Led], &GPIO_InitStructure); |
EricLew | 0:ad9dfc0179dc | 360 | |
EricLew | 0:ad9dfc0179dc | 361 | HAL_GPIO_WritePin(LED_PORT[Led], GPIO_InitStructure.Pin, GPIO_PIN_RESET); |
EricLew | 0:ad9dfc0179dc | 362 | } |
EricLew | 0:ad9dfc0179dc | 363 | |
EricLew | 0:ad9dfc0179dc | 364 | #if defined (USE_STM32L476G_DISCO_REVC) || defined (USE_STM32L476G_DISCO_REVB) |
EricLew | 0:ad9dfc0179dc | 365 | /** |
EricLew | 0:ad9dfc0179dc | 366 | * @brief Unconfigures LED GPIOs. |
EricLew | 0:ad9dfc0179dc | 367 | * @param Led: Specifies the Led to be unconfigured. |
EricLew | 0:ad9dfc0179dc | 368 | * This parameter can be one of following parameters: |
EricLew | 0:ad9dfc0179dc | 369 | * @arg LED4 |
EricLew | 0:ad9dfc0179dc | 370 | * @arg LED5 |
EricLew | 0:ad9dfc0179dc | 371 | * @retval None |
EricLew | 0:ad9dfc0179dc | 372 | */ |
EricLew | 0:ad9dfc0179dc | 373 | #elif defined (USE_STM32L476G_DISCO_REVA) |
EricLew | 0:ad9dfc0179dc | 374 | /** |
EricLew | 0:ad9dfc0179dc | 375 | * @brief Unconfigures LED GPIOs. |
EricLew | 0:ad9dfc0179dc | 376 | * @param Led: Specifies the Led to be unconfigured. |
EricLew | 0:ad9dfc0179dc | 377 | * This parameter can be one of following parameters: |
EricLew | 0:ad9dfc0179dc | 378 | * @arg LED3 |
EricLew | 0:ad9dfc0179dc | 379 | * @arg LED4 |
EricLew | 0:ad9dfc0179dc | 380 | * @retval None |
EricLew | 0:ad9dfc0179dc | 381 | */ |
EricLew | 0:ad9dfc0179dc | 382 | #endif |
EricLew | 0:ad9dfc0179dc | 383 | void BSP_LED_DeInit(Led_TypeDef Led) |
EricLew | 0:ad9dfc0179dc | 384 | { |
EricLew | 0:ad9dfc0179dc | 385 | /* Enable the GPIO_LED clock */ |
EricLew | 0:ad9dfc0179dc | 386 | LEDx_GPIO_CLK_ENABLE(Led); |
EricLew | 0:ad9dfc0179dc | 387 | |
EricLew | 0:ad9dfc0179dc | 388 | HAL_GPIO_DeInit(LED_PORT[Led], LED_PIN[Led]); |
EricLew | 0:ad9dfc0179dc | 389 | } |
EricLew | 0:ad9dfc0179dc | 390 | |
EricLew | 0:ad9dfc0179dc | 391 | #if defined (USE_STM32L476G_DISCO_REVC) || defined (USE_STM32L476G_DISCO_REVB) |
EricLew | 0:ad9dfc0179dc | 392 | /** |
EricLew | 0:ad9dfc0179dc | 393 | * @brief Turns selected LED On. |
EricLew | 0:ad9dfc0179dc | 394 | * @param Led: Specifies the Led to be set on. |
EricLew | 0:ad9dfc0179dc | 395 | * This parameter can be one of following parameters: |
EricLew | 0:ad9dfc0179dc | 396 | * @arg LED4 |
EricLew | 0:ad9dfc0179dc | 397 | * @arg LED5 |
EricLew | 0:ad9dfc0179dc | 398 | * @retval None |
EricLew | 0:ad9dfc0179dc | 399 | */ |
EricLew | 0:ad9dfc0179dc | 400 | #elif defined (USE_STM32L476G_DISCO_REVA) |
EricLew | 0:ad9dfc0179dc | 401 | /** |
EricLew | 0:ad9dfc0179dc | 402 | * @brief Turns selected LED On. |
EricLew | 0:ad9dfc0179dc | 403 | * @param Led: Specifies the Led to be set on. |
EricLew | 0:ad9dfc0179dc | 404 | * This parameter can be one of following parameters: |
EricLew | 0:ad9dfc0179dc | 405 | * @arg LED3 |
EricLew | 0:ad9dfc0179dc | 406 | * @arg LED4 |
EricLew | 0:ad9dfc0179dc | 407 | * @retval None |
EricLew | 0:ad9dfc0179dc | 408 | */ |
EricLew | 0:ad9dfc0179dc | 409 | #endif |
EricLew | 0:ad9dfc0179dc | 410 | void BSP_LED_On(Led_TypeDef Led) |
EricLew | 0:ad9dfc0179dc | 411 | { |
EricLew | 0:ad9dfc0179dc | 412 | HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_SET); |
EricLew | 0:ad9dfc0179dc | 413 | } |
EricLew | 0:ad9dfc0179dc | 414 | |
EricLew | 0:ad9dfc0179dc | 415 | #if defined (USE_STM32L476G_DISCO_REVC) || defined (USE_STM32L476G_DISCO_REVB) |
EricLew | 0:ad9dfc0179dc | 416 | /** |
EricLew | 0:ad9dfc0179dc | 417 | * @brief Turns selected LED Off. |
EricLew | 0:ad9dfc0179dc | 418 | * @param Led: Specifies the Led to be set off. |
EricLew | 0:ad9dfc0179dc | 419 | * This parameter can be one of following parameters: |
EricLew | 0:ad9dfc0179dc | 420 | * @arg LED4 |
EricLew | 0:ad9dfc0179dc | 421 | * @arg LED5 |
EricLew | 0:ad9dfc0179dc | 422 | * @retval None |
EricLew | 0:ad9dfc0179dc | 423 | */ |
EricLew | 0:ad9dfc0179dc | 424 | #elif defined (USE_STM32L476G_DISCO_REVA) |
EricLew | 0:ad9dfc0179dc | 425 | /** |
EricLew | 0:ad9dfc0179dc | 426 | * @brief Turns selected LED Off. |
EricLew | 0:ad9dfc0179dc | 427 | * @param Led: Specifies the Led to be set off. |
EricLew | 0:ad9dfc0179dc | 428 | * This parameter can be one of following parameters: |
EricLew | 0:ad9dfc0179dc | 429 | * @arg LED3 |
EricLew | 0:ad9dfc0179dc | 430 | * @arg LED4 |
EricLew | 0:ad9dfc0179dc | 431 | * @retval None |
EricLew | 0:ad9dfc0179dc | 432 | */ |
EricLew | 0:ad9dfc0179dc | 433 | #endif |
EricLew | 0:ad9dfc0179dc | 434 | void BSP_LED_Off(Led_TypeDef Led) |
EricLew | 0:ad9dfc0179dc | 435 | { |
EricLew | 0:ad9dfc0179dc | 436 | HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET); |
EricLew | 0:ad9dfc0179dc | 437 | } |
EricLew | 0:ad9dfc0179dc | 438 | |
EricLew | 0:ad9dfc0179dc | 439 | #if defined (USE_STM32L476G_DISCO_REVC) || defined (USE_STM32L476G_DISCO_REVB) |
EricLew | 0:ad9dfc0179dc | 440 | /** |
EricLew | 0:ad9dfc0179dc | 441 | * @brief Toggles the selected LED. |
EricLew | 0:ad9dfc0179dc | 442 | * @param Led: Specifies the Led to be toggled. |
EricLew | 0:ad9dfc0179dc | 443 | * This parameter can be one of following parameters: |
EricLew | 0:ad9dfc0179dc | 444 | * @arg LED4 |
EricLew | 0:ad9dfc0179dc | 445 | * @arg LED5 |
EricLew | 0:ad9dfc0179dc | 446 | * @retval None |
EricLew | 0:ad9dfc0179dc | 447 | */ |
EricLew | 0:ad9dfc0179dc | 448 | #elif defined (USE_STM32L476G_DISCO_REVA) |
EricLew | 0:ad9dfc0179dc | 449 | /** |
EricLew | 0:ad9dfc0179dc | 450 | * @brief Toggles the selected LED. |
EricLew | 0:ad9dfc0179dc | 451 | * @param Led: Specifies the Led to be toggled. |
EricLew | 0:ad9dfc0179dc | 452 | * This parameter can be one of following parameters: |
EricLew | 0:ad9dfc0179dc | 453 | * @arg LED3 |
EricLew | 0:ad9dfc0179dc | 454 | * @arg LED4 |
EricLew | 0:ad9dfc0179dc | 455 | * @retval None |
EricLew | 0:ad9dfc0179dc | 456 | */ |
EricLew | 0:ad9dfc0179dc | 457 | #endif |
EricLew | 0:ad9dfc0179dc | 458 | void BSP_LED_Toggle(Led_TypeDef Led) |
EricLew | 0:ad9dfc0179dc | 459 | { |
EricLew | 0:ad9dfc0179dc | 460 | HAL_GPIO_TogglePin(LED_PORT[Led], LED_PIN[Led]); |
EricLew | 0:ad9dfc0179dc | 461 | } |
EricLew | 0:ad9dfc0179dc | 462 | |
EricLew | 0:ad9dfc0179dc | 463 | /** |
EricLew | 0:ad9dfc0179dc | 464 | * @brief Configures all buttons of the joystick in GPIO or EXTI modes. |
EricLew | 0:ad9dfc0179dc | 465 | * @param Joy_Mode: Joystick mode. |
EricLew | 0:ad9dfc0179dc | 466 | * This parameter can be one of the following values: |
EricLew | 0:ad9dfc0179dc | 467 | * @arg JOY_MODE_GPIO: Joystick pins will be used as simple IOs |
EricLew | 0:ad9dfc0179dc | 468 | * @arg JOY_MODE_EXTI: Joystick pins will be connected to EXTI line |
EricLew | 0:ad9dfc0179dc | 469 | * with interrupt generation capability |
EricLew | 0:ad9dfc0179dc | 470 | * @retval HAL_OK: if all initializations are OK. Other value if error. |
EricLew | 0:ad9dfc0179dc | 471 | */ |
EricLew | 0:ad9dfc0179dc | 472 | uint8_t BSP_JOY_Init(JOYMode_TypeDef Joy_Mode) |
EricLew | 0:ad9dfc0179dc | 473 | { |
EricLew | 0:ad9dfc0179dc | 474 | JOYState_TypeDef joykey; |
EricLew | 0:ad9dfc0179dc | 475 | GPIO_InitTypeDef GPIO_InitStruct; |
EricLew | 0:ad9dfc0179dc | 476 | |
EricLew | 0:ad9dfc0179dc | 477 | /* Initialized the Joystick. */ |
EricLew | 0:ad9dfc0179dc | 478 | for(joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++) |
EricLew | 0:ad9dfc0179dc | 479 | { |
EricLew | 0:ad9dfc0179dc | 480 | /* Enable the JOY clock */ |
EricLew | 0:ad9dfc0179dc | 481 | JOYx_GPIO_CLK_ENABLE(joykey); |
EricLew | 0:ad9dfc0179dc | 482 | |
EricLew | 0:ad9dfc0179dc | 483 | GPIO_InitStruct.Pin = JOY_PIN[joykey]; |
EricLew | 0:ad9dfc0179dc | 484 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
EricLew | 0:ad9dfc0179dc | 485 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 0:ad9dfc0179dc | 486 | |
EricLew | 0:ad9dfc0179dc | 487 | if (Joy_Mode == JOY_MODE_GPIO) |
EricLew | 0:ad9dfc0179dc | 488 | { |
EricLew | 0:ad9dfc0179dc | 489 | /* Configure Joy pin as input */ |
EricLew | 0:ad9dfc0179dc | 490 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
EricLew | 0:ad9dfc0179dc | 491 | HAL_GPIO_Init(JOY_PORT[joykey], &GPIO_InitStruct); |
EricLew | 0:ad9dfc0179dc | 492 | } |
EricLew | 0:ad9dfc0179dc | 493 | else if (Joy_Mode == JOY_MODE_EXTI) |
EricLew | 0:ad9dfc0179dc | 494 | { |
EricLew | 0:ad9dfc0179dc | 495 | /* Configure Joy pin as input with External interrupt */ |
EricLew | 0:ad9dfc0179dc | 496 | GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; |
EricLew | 0:ad9dfc0179dc | 497 | HAL_GPIO_Init(JOY_PORT[joykey], &GPIO_InitStruct); |
EricLew | 0:ad9dfc0179dc | 498 | |
EricLew | 0:ad9dfc0179dc | 499 | /* Enable and set Joy EXTI Interrupt to the lowest priority */ |
EricLew | 0:ad9dfc0179dc | 500 | HAL_NVIC_SetPriority((IRQn_Type)(JOY_IRQn[joykey]), 0x0F, 0x00); |
EricLew | 0:ad9dfc0179dc | 501 | HAL_NVIC_EnableIRQ((IRQn_Type)(JOY_IRQn[joykey])); |
EricLew | 0:ad9dfc0179dc | 502 | } |
EricLew | 0:ad9dfc0179dc | 503 | } |
EricLew | 0:ad9dfc0179dc | 504 | |
EricLew | 0:ad9dfc0179dc | 505 | return HAL_OK; |
EricLew | 0:ad9dfc0179dc | 506 | } |
EricLew | 0:ad9dfc0179dc | 507 | |
EricLew | 0:ad9dfc0179dc | 508 | /** |
EricLew | 0:ad9dfc0179dc | 509 | * @brief Unonfigures all GPIOs used as buttons of the joystick. |
EricLew | 0:ad9dfc0179dc | 510 | * @retval None. |
EricLew | 0:ad9dfc0179dc | 511 | */ |
EricLew | 0:ad9dfc0179dc | 512 | void BSP_JOY_DeInit(void) |
EricLew | 0:ad9dfc0179dc | 513 | { |
EricLew | 0:ad9dfc0179dc | 514 | JOYState_TypeDef joykey; |
EricLew | 0:ad9dfc0179dc | 515 | |
EricLew | 0:ad9dfc0179dc | 516 | /* Initialized the Joystick. */ |
EricLew | 0:ad9dfc0179dc | 517 | for(joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++) |
EricLew | 0:ad9dfc0179dc | 518 | { |
EricLew | 0:ad9dfc0179dc | 519 | /* Enable the JOY clock */ |
EricLew | 0:ad9dfc0179dc | 520 | JOYx_GPIO_CLK_ENABLE(joykey); |
EricLew | 0:ad9dfc0179dc | 521 | |
EricLew | 0:ad9dfc0179dc | 522 | HAL_GPIO_DeInit(JOY_PORT[joykey], JOY_PIN[joykey]); |
EricLew | 0:ad9dfc0179dc | 523 | } |
EricLew | 0:ad9dfc0179dc | 524 | } |
EricLew | 0:ad9dfc0179dc | 525 | |
EricLew | 0:ad9dfc0179dc | 526 | /** |
EricLew | 0:ad9dfc0179dc | 527 | * @brief Returns the current joystick status. |
EricLew | 0:ad9dfc0179dc | 528 | * @retval Code of the joystick key pressed |
EricLew | 0:ad9dfc0179dc | 529 | * This code can be one of the following values: |
EricLew | 0:ad9dfc0179dc | 530 | * @arg JOY_NONE |
EricLew | 0:ad9dfc0179dc | 531 | * @arg JOY_SEL |
EricLew | 0:ad9dfc0179dc | 532 | * @arg JOY_DOWN |
EricLew | 0:ad9dfc0179dc | 533 | * @arg JOY_LEFT |
EricLew | 0:ad9dfc0179dc | 534 | * @arg JOY_RIGHT |
EricLew | 0:ad9dfc0179dc | 535 | * @arg JOY_UP |
EricLew | 0:ad9dfc0179dc | 536 | */ |
EricLew | 0:ad9dfc0179dc | 537 | JOYState_TypeDef BSP_JOY_GetState(void) |
EricLew | 0:ad9dfc0179dc | 538 | { |
EricLew | 0:ad9dfc0179dc | 539 | JOYState_TypeDef joykey; |
EricLew | 0:ad9dfc0179dc | 540 | |
EricLew | 0:ad9dfc0179dc | 541 | for (joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++) |
EricLew | 0:ad9dfc0179dc | 542 | { |
EricLew | 0:ad9dfc0179dc | 543 | if (HAL_GPIO_ReadPin(JOY_PORT[joykey], JOY_PIN[joykey]) == GPIO_PIN_SET) |
EricLew | 0:ad9dfc0179dc | 544 | { |
EricLew | 0:ad9dfc0179dc | 545 | /* Return Code Joystick key pressed */ |
EricLew | 0:ad9dfc0179dc | 546 | return joykey; |
EricLew | 0:ad9dfc0179dc | 547 | } |
EricLew | 0:ad9dfc0179dc | 548 | } |
EricLew | 0:ad9dfc0179dc | 549 | |
EricLew | 0:ad9dfc0179dc | 550 | /* No Joystick key pressed */ |
EricLew | 0:ad9dfc0179dc | 551 | return JOY_NONE; |
EricLew | 0:ad9dfc0179dc | 552 | } |
EricLew | 0:ad9dfc0179dc | 553 | |
EricLew | 0:ad9dfc0179dc | 554 | /** |
EricLew | 0:ad9dfc0179dc | 555 | * @} |
EricLew | 0:ad9dfc0179dc | 556 | */ |
EricLew | 0:ad9dfc0179dc | 557 | |
EricLew | 0:ad9dfc0179dc | 558 | /** @defgroup STM32L476G_DISCOVERY_BusOperations_Functions Bus Operations Functions |
EricLew | 0:ad9dfc0179dc | 559 | * @{ |
EricLew | 0:ad9dfc0179dc | 560 | */ |
EricLew | 0:ad9dfc0179dc | 561 | |
EricLew | 0:ad9dfc0179dc | 562 | /******************************************************************************* |
EricLew | 0:ad9dfc0179dc | 563 | BUS OPERATIONS |
EricLew | 0:ad9dfc0179dc | 564 | *******************************************************************************/ |
EricLew | 0:ad9dfc0179dc | 565 | #if defined(HAL_SPI_MODULE_ENABLED) |
EricLew | 0:ad9dfc0179dc | 566 | /******************************* SPI Routines**********************************/ |
EricLew | 0:ad9dfc0179dc | 567 | /** |
EricLew | 0:ad9dfc0179dc | 568 | * @brief SPIx Bus initialization |
EricLew | 0:ad9dfc0179dc | 569 | * @retval None |
EricLew | 0:ad9dfc0179dc | 570 | */ |
EricLew | 0:ad9dfc0179dc | 571 | static void SPIx_Init(void) |
EricLew | 0:ad9dfc0179dc | 572 | { |
EricLew | 0:ad9dfc0179dc | 573 | if(HAL_SPI_GetState(&SpiHandle) == HAL_SPI_STATE_RESET) |
EricLew | 0:ad9dfc0179dc | 574 | { |
EricLew | 0:ad9dfc0179dc | 575 | /* SPI Config */ |
EricLew | 0:ad9dfc0179dc | 576 | SpiHandle.Instance = DISCOVERY_SPIx; |
EricLew | 0:ad9dfc0179dc | 577 | /* SPI baudrate is set to 10 MHz (PCLK1/SPI_BaudRatePrescaler = 80/8 = 10 MHz) |
EricLew | 0:ad9dfc0179dc | 578 | to verify these constraints: |
EricLew | 0:ad9dfc0179dc | 579 | lsm303c SPI interface max baudrate is 10MHz for write/read |
EricLew | 0:ad9dfc0179dc | 580 | PCLK1 max frequency is set to 80 MHz |
EricLew | 0:ad9dfc0179dc | 581 | */ |
EricLew | 0:ad9dfc0179dc | 582 | SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; |
EricLew | 0:ad9dfc0179dc | 583 | SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; |
EricLew | 0:ad9dfc0179dc | 584 | SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE; |
EricLew | 0:ad9dfc0179dc | 585 | SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW; |
EricLew | 0:ad9dfc0179dc | 586 | SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
EricLew | 0:ad9dfc0179dc | 587 | SpiHandle.Init.CRCPolynomial = 7; |
EricLew | 0:ad9dfc0179dc | 588 | SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT; |
EricLew | 0:ad9dfc0179dc | 589 | SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; |
EricLew | 0:ad9dfc0179dc | 590 | SpiHandle.Init.NSS = SPI_NSS_SOFT; |
EricLew | 0:ad9dfc0179dc | 591 | SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE; |
EricLew | 0:ad9dfc0179dc | 592 | SpiHandle.Init.Mode = SPI_MODE_MASTER; |
EricLew | 0:ad9dfc0179dc | 593 | |
EricLew | 0:ad9dfc0179dc | 594 | SPIx_MspInit(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 595 | HAL_SPI_Init(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 596 | } |
EricLew | 0:ad9dfc0179dc | 597 | } |
EricLew | 0:ad9dfc0179dc | 598 | |
EricLew | 0:ad9dfc0179dc | 599 | /** |
EricLew | 0:ad9dfc0179dc | 600 | * @brief SPI MSP Init |
EricLew | 0:ad9dfc0179dc | 601 | * @param hspi: SPI handle |
EricLew | 0:ad9dfc0179dc | 602 | * @retval None |
EricLew | 0:ad9dfc0179dc | 603 | */ |
EricLew | 0:ad9dfc0179dc | 604 | static void SPIx_MspInit(SPI_HandleTypeDef *hspi) |
EricLew | 0:ad9dfc0179dc | 605 | { |
EricLew | 0:ad9dfc0179dc | 606 | GPIO_InitTypeDef GPIO_InitStructure; |
EricLew | 0:ad9dfc0179dc | 607 | |
EricLew | 0:ad9dfc0179dc | 608 | /* Enable SPIx clock */ |
EricLew | 0:ad9dfc0179dc | 609 | DISCOVERY_SPIx_CLOCK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 610 | |
EricLew | 0:ad9dfc0179dc | 611 | /* enable SPIx gpio clock */ |
EricLew | 0:ad9dfc0179dc | 612 | DISCOVERY_SPIx_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 613 | |
EricLew | 0:ad9dfc0179dc | 614 | /* configure SPIx SCK, MOSI and MISO */ |
EricLew | 0:ad9dfc0179dc | 615 | GPIO_InitStructure.Pin = (DISCOVERY_SPIx_SCK_PIN | DISCOVERY_SPIx_MOSI_PIN | DISCOVERY_SPIx_MISO_PIN); |
EricLew | 0:ad9dfc0179dc | 616 | GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; |
EricLew | 0:ad9dfc0179dc | 617 | GPIO_InitStructure.Pull = GPIO_NOPULL; // GPIO_PULLDOWN; |
EricLew | 0:ad9dfc0179dc | 618 | GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH; |
EricLew | 0:ad9dfc0179dc | 619 | GPIO_InitStructure.Alternate = DISCOVERY_SPIx_AF; |
EricLew | 0:ad9dfc0179dc | 620 | HAL_GPIO_Init(DISCOVERY_SPIx_GPIO_PORT, &GPIO_InitStructure); |
EricLew | 0:ad9dfc0179dc | 621 | } |
EricLew | 0:ad9dfc0179dc | 622 | |
EricLew | 0:ad9dfc0179dc | 623 | /** |
EricLew | 0:ad9dfc0179dc | 624 | * @brief SPIx Bus Deinitialization |
EricLew | 0:ad9dfc0179dc | 625 | * @retval None |
EricLew | 0:ad9dfc0179dc | 626 | */ |
EricLew | 0:ad9dfc0179dc | 627 | void SPIx_DeInit(void) |
EricLew | 0:ad9dfc0179dc | 628 | { |
EricLew | 0:ad9dfc0179dc | 629 | if(HAL_SPI_GetState(&SpiHandle) != HAL_SPI_STATE_RESET) |
EricLew | 0:ad9dfc0179dc | 630 | { |
EricLew | 0:ad9dfc0179dc | 631 | /* SPI Deinit */ |
EricLew | 0:ad9dfc0179dc | 632 | HAL_SPI_DeInit(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 633 | SPIx_MspDeInit(); |
EricLew | 0:ad9dfc0179dc | 634 | } |
EricLew | 0:ad9dfc0179dc | 635 | } |
EricLew | 0:ad9dfc0179dc | 636 | |
EricLew | 0:ad9dfc0179dc | 637 | /** |
EricLew | 0:ad9dfc0179dc | 638 | * @brief SPI MSP DeInit |
EricLew | 0:ad9dfc0179dc | 639 | * @retval None |
EricLew | 0:ad9dfc0179dc | 640 | */ |
EricLew | 0:ad9dfc0179dc | 641 | static void SPIx_MspDeInit(void) |
EricLew | 0:ad9dfc0179dc | 642 | { |
EricLew | 0:ad9dfc0179dc | 643 | /* enable SPIx gpio clock */ |
EricLew | 0:ad9dfc0179dc | 644 | DISCOVERY_SPIx_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 645 | |
EricLew | 0:ad9dfc0179dc | 646 | /* Unconfigure SPIx SCK, MOSI and MISO */ |
EricLew | 0:ad9dfc0179dc | 647 | HAL_GPIO_DeInit(DISCOVERY_SPIx_GPIO_PORT, (DISCOVERY_SPIx_SCK_PIN | DISCOVERY_SPIx_MOSI_PIN | DISCOVERY_SPIx_MISO_PIN)); |
EricLew | 0:ad9dfc0179dc | 648 | |
EricLew | 0:ad9dfc0179dc | 649 | DISCOVERY_SPIx_GPIO_FORCE_RESET(); |
EricLew | 0:ad9dfc0179dc | 650 | DISCOVERY_SPIx_GPIO_RELEASE_RESET(); |
EricLew | 0:ad9dfc0179dc | 651 | |
EricLew | 0:ad9dfc0179dc | 652 | /* Disable SPIx clock */ |
EricLew | 0:ad9dfc0179dc | 653 | DISCOVERY_SPIx_CLOCK_DISABLE(); |
EricLew | 0:ad9dfc0179dc | 654 | } |
EricLew | 0:ad9dfc0179dc | 655 | |
EricLew | 0:ad9dfc0179dc | 656 | /** |
EricLew | 0:ad9dfc0179dc | 657 | * @brief Sends a Byte through the SPI interface and return the Byte received |
EricLew | 0:ad9dfc0179dc | 658 | * from the SPI bus. |
EricLew | 0:ad9dfc0179dc | 659 | * @param Byte : Byte send. |
EricLew | 0:ad9dfc0179dc | 660 | * @retval none. |
EricLew | 0:ad9dfc0179dc | 661 | */ |
EricLew | 0:ad9dfc0179dc | 662 | static uint8_t SPIx_WriteRead(uint8_t Byte) |
EricLew | 0:ad9dfc0179dc | 663 | { |
EricLew | 0:ad9dfc0179dc | 664 | uint8_t receivedbyte; |
EricLew | 0:ad9dfc0179dc | 665 | |
EricLew | 0:ad9dfc0179dc | 666 | /* Enable the SPI */ |
EricLew | 0:ad9dfc0179dc | 667 | __HAL_SPI_ENABLE(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 668 | /* check TXE flag */ |
EricLew | 0:ad9dfc0179dc | 669 | while((SpiHandle.Instance->SR & SPI_FLAG_TXE) != SPI_FLAG_TXE); |
EricLew | 0:ad9dfc0179dc | 670 | |
EricLew | 0:ad9dfc0179dc | 671 | /* Write the data */ |
EricLew | 0:ad9dfc0179dc | 672 | *((__IO uint8_t*)&SpiHandle.Instance->DR) = Byte; |
EricLew | 0:ad9dfc0179dc | 673 | |
EricLew | 0:ad9dfc0179dc | 674 | while((SpiHandle.Instance->SR & SPI_FLAG_RXNE) != SPI_FLAG_RXNE); |
EricLew | 0:ad9dfc0179dc | 675 | receivedbyte = *((__IO uint8_t*)&SpiHandle.Instance->DR); |
EricLew | 0:ad9dfc0179dc | 676 | |
EricLew | 0:ad9dfc0179dc | 677 | /* Wait BSY flag */ |
EricLew | 0:ad9dfc0179dc | 678 | while((SpiHandle.Instance->SR & SPI_FLAG_FTLVL) != SPI_FTLVL_EMPTY); |
EricLew | 0:ad9dfc0179dc | 679 | while((SpiHandle.Instance->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY); |
EricLew | 0:ad9dfc0179dc | 680 | |
EricLew | 0:ad9dfc0179dc | 681 | /* disable the SPI */ |
EricLew | 0:ad9dfc0179dc | 682 | __HAL_SPI_DISABLE(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 683 | |
EricLew | 0:ad9dfc0179dc | 684 | return receivedbyte; |
EricLew | 0:ad9dfc0179dc | 685 | } |
EricLew | 0:ad9dfc0179dc | 686 | |
EricLew | 0:ad9dfc0179dc | 687 | /** |
EricLew | 0:ad9dfc0179dc | 688 | * @brief Sends a Byte through the SPI interface. |
EricLew | 0:ad9dfc0179dc | 689 | * @param Byte : Byte to send. |
EricLew | 0:ad9dfc0179dc | 690 | * @retval none. |
EricLew | 0:ad9dfc0179dc | 691 | */ |
EricLew | 0:ad9dfc0179dc | 692 | static void SPIx_Write(uint8_t Byte) |
EricLew | 0:ad9dfc0179dc | 693 | { |
EricLew | 0:ad9dfc0179dc | 694 | /* Enable the SPI */ |
EricLew | 0:ad9dfc0179dc | 695 | __HAL_SPI_ENABLE(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 696 | /* check TXE flag */ |
EricLew | 0:ad9dfc0179dc | 697 | while((SpiHandle.Instance->SR & SPI_FLAG_TXE) != SPI_FLAG_TXE); |
EricLew | 0:ad9dfc0179dc | 698 | |
EricLew | 0:ad9dfc0179dc | 699 | /* Write the data */ |
EricLew | 0:ad9dfc0179dc | 700 | *((__IO uint8_t*)&SpiHandle.Instance->DR) = Byte; |
EricLew | 0:ad9dfc0179dc | 701 | |
EricLew | 0:ad9dfc0179dc | 702 | /* Wait BSY flag */ |
EricLew | 0:ad9dfc0179dc | 703 | while((SpiHandle.Instance->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY); |
EricLew | 0:ad9dfc0179dc | 704 | |
EricLew | 0:ad9dfc0179dc | 705 | /* disable the SPI */ |
EricLew | 0:ad9dfc0179dc | 706 | __HAL_SPI_DISABLE(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 707 | } |
EricLew | 0:ad9dfc0179dc | 708 | |
EricLew | 0:ad9dfc0179dc | 709 | #if defined(__ICCARM__) |
EricLew | 0:ad9dfc0179dc | 710 | #pragma optimize=none |
EricLew | 0:ad9dfc0179dc | 711 | #endif |
EricLew | 0:ad9dfc0179dc | 712 | /** |
EricLew | 0:ad9dfc0179dc | 713 | * @brief Receives a Byte from the SPI bus. |
EricLew | 0:ad9dfc0179dc | 714 | * @retval The received byte value |
EricLew | 0:ad9dfc0179dc | 715 | */ |
EricLew | 0:ad9dfc0179dc | 716 | static uint8_t SPIx_Read(void) |
EricLew | 0:ad9dfc0179dc | 717 | { |
EricLew | 0:ad9dfc0179dc | 718 | uint8_t receivedbyte; |
EricLew | 0:ad9dfc0179dc | 719 | |
EricLew | 0:ad9dfc0179dc | 720 | __HAL_SPI_ENABLE(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 721 | __DSB(); |
EricLew | 0:ad9dfc0179dc | 722 | __DSB(); |
EricLew | 0:ad9dfc0179dc | 723 | __DSB(); |
EricLew | 0:ad9dfc0179dc | 724 | __DSB(); |
EricLew | 0:ad9dfc0179dc | 725 | __DSB(); |
EricLew | 0:ad9dfc0179dc | 726 | __DSB(); |
EricLew | 0:ad9dfc0179dc | 727 | __DSB(); |
EricLew | 0:ad9dfc0179dc | 728 | __DSB(); |
EricLew | 0:ad9dfc0179dc | 729 | __HAL_SPI_DISABLE(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 730 | |
EricLew | 0:ad9dfc0179dc | 731 | while((SpiHandle.Instance->SR & SPI_FLAG_RXNE) != SPI_FLAG_RXNE); |
EricLew | 0:ad9dfc0179dc | 732 | /* read the received data */ |
EricLew | 0:ad9dfc0179dc | 733 | receivedbyte = *(__IO uint8_t *)&SpiHandle.Instance->DR; |
EricLew | 0:ad9dfc0179dc | 734 | |
EricLew | 0:ad9dfc0179dc | 735 | /* Wait for the BSY flag reset */ |
EricLew | 0:ad9dfc0179dc | 736 | while((SpiHandle.Instance->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY); |
EricLew | 0:ad9dfc0179dc | 737 | |
EricLew | 0:ad9dfc0179dc | 738 | |
EricLew | 0:ad9dfc0179dc | 739 | return receivedbyte; |
EricLew | 0:ad9dfc0179dc | 740 | } |
EricLew | 0:ad9dfc0179dc | 741 | #endif /* HAL_SPI_MODULE_ENABLED */ |
EricLew | 0:ad9dfc0179dc | 742 | |
EricLew | 0:ad9dfc0179dc | 743 | |
EricLew | 0:ad9dfc0179dc | 744 | #if defined(HAL_I2C_MODULE_ENABLED) |
EricLew | 0:ad9dfc0179dc | 745 | /******************************* I2C Routines**********************************/ |
EricLew | 0:ad9dfc0179dc | 746 | /** |
EricLew | 0:ad9dfc0179dc | 747 | * @brief Discovery I2C1 Bus initialization |
EricLew | 0:ad9dfc0179dc | 748 | * @retval None |
EricLew | 0:ad9dfc0179dc | 749 | */ |
EricLew | 0:ad9dfc0179dc | 750 | static void I2C1_Init(void) |
EricLew | 0:ad9dfc0179dc | 751 | { |
EricLew | 0:ad9dfc0179dc | 752 | if(HAL_I2C_GetState(&I2c1Handle) == HAL_I2C_STATE_RESET) |
EricLew | 0:ad9dfc0179dc | 753 | { |
EricLew | 0:ad9dfc0179dc | 754 | I2c1Handle.Instance = DISCOVERY_I2C1; |
EricLew | 0:ad9dfc0179dc | 755 | I2c1Handle.Init.Timing = DISCOVERY_I2C1_TIMING; |
EricLew | 0:ad9dfc0179dc | 756 | I2c1Handle.Init.OwnAddress1 = 0; |
EricLew | 0:ad9dfc0179dc | 757 | I2c1Handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
EricLew | 0:ad9dfc0179dc | 758 | I2c1Handle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; |
EricLew | 0:ad9dfc0179dc | 759 | I2c1Handle.Init.OwnAddress2 = 0; |
EricLew | 0:ad9dfc0179dc | 760 | I2c1Handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; |
EricLew | 0:ad9dfc0179dc | 761 | I2c1Handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; |
EricLew | 0:ad9dfc0179dc | 762 | |
EricLew | 0:ad9dfc0179dc | 763 | /* Init the I2C */ |
EricLew | 0:ad9dfc0179dc | 764 | I2C1_MspInit(&I2c1Handle); |
EricLew | 0:ad9dfc0179dc | 765 | HAL_I2C_Init(&I2c1Handle); |
EricLew | 0:ad9dfc0179dc | 766 | } |
EricLew | 0:ad9dfc0179dc | 767 | } |
EricLew | 0:ad9dfc0179dc | 768 | |
EricLew | 0:ad9dfc0179dc | 769 | /** |
EricLew | 0:ad9dfc0179dc | 770 | * @brief Discovery I2C1 MSP Initialization |
EricLew | 0:ad9dfc0179dc | 771 | * @param hi2c: I2C handle |
EricLew | 0:ad9dfc0179dc | 772 | * @retval None |
EricLew | 0:ad9dfc0179dc | 773 | */ |
EricLew | 0:ad9dfc0179dc | 774 | static void I2C1_MspInit(I2C_HandleTypeDef *hi2c) |
EricLew | 0:ad9dfc0179dc | 775 | { |
EricLew | 0:ad9dfc0179dc | 776 | GPIO_InitTypeDef GPIO_InitStructure; |
EricLew | 0:ad9dfc0179dc | 777 | RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct; |
EricLew | 0:ad9dfc0179dc | 778 | |
EricLew | 0:ad9dfc0179dc | 779 | /* IOSV bit MUST be set to access GPIO port G[2:15] */ |
EricLew | 0:ad9dfc0179dc | 780 | __HAL_RCC_PWR_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 781 | HAL_PWREx_EnableVddIO2(); |
EricLew | 0:ad9dfc0179dc | 782 | |
EricLew | 0:ad9dfc0179dc | 783 | if (hi2c->Instance == DISCOVERY_I2C1) |
EricLew | 0:ad9dfc0179dc | 784 | { |
EricLew | 0:ad9dfc0179dc | 785 | /*##-1- Configure the Discovery I2C clock source. The clock is derived from the SYSCLK #*/ |
EricLew | 0:ad9dfc0179dc | 786 | RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C1; |
EricLew | 0:ad9dfc0179dc | 787 | RCC_PeriphCLKInitStruct.I2c1ClockSelection = RCC_I2C1CLKSOURCE_SYSCLK; |
EricLew | 0:ad9dfc0179dc | 788 | HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct); |
EricLew | 0:ad9dfc0179dc | 789 | |
EricLew | 0:ad9dfc0179dc | 790 | /*##-2- Configure the GPIOs ################################################*/ |
EricLew | 0:ad9dfc0179dc | 791 | /* Enable GPIO clock */ |
EricLew | 0:ad9dfc0179dc | 792 | DISCOVERY_I2C1_SDA_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 793 | DISCOVERY_I2C1_SCL_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 794 | |
EricLew | 0:ad9dfc0179dc | 795 | /* Configure I2C Rx/Tx as alternate function */ |
EricLew | 0:ad9dfc0179dc | 796 | GPIO_InitStructure.Pin = DISCOVERY_I2C1_SCL_PIN | DISCOVERY_I2C1_SDA_PIN; |
EricLew | 0:ad9dfc0179dc | 797 | GPIO_InitStructure.Mode = GPIO_MODE_AF_OD; |
EricLew | 0:ad9dfc0179dc | 798 | GPIO_InitStructure.Pull = GPIO_PULLUP; |
EricLew | 0:ad9dfc0179dc | 799 | GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 0:ad9dfc0179dc | 800 | GPIO_InitStructure.Alternate = DISCOVERY_I2C1_SCL_SDA_AF; |
EricLew | 0:ad9dfc0179dc | 801 | HAL_GPIO_Init(DISCOVERY_I2C1_SCL_GPIO_PORT, &GPIO_InitStructure); |
EricLew | 0:ad9dfc0179dc | 802 | |
EricLew | 0:ad9dfc0179dc | 803 | /*##-3- Configure the Discovery I2C1 peripheral #######################################*/ |
EricLew | 0:ad9dfc0179dc | 804 | /* Enable Discovery I2C1 clock */ |
EricLew | 0:ad9dfc0179dc | 805 | DISCOVERY_I2C1_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 806 | |
EricLew | 0:ad9dfc0179dc | 807 | /* Force and release the I2C Peripheral Clock Reset */ |
EricLew | 0:ad9dfc0179dc | 808 | DISCOVERY_I2C1_FORCE_RESET(); |
EricLew | 0:ad9dfc0179dc | 809 | DISCOVERY_I2C1_RELEASE_RESET(); |
EricLew | 0:ad9dfc0179dc | 810 | |
EricLew | 0:ad9dfc0179dc | 811 | /* Enable and set Discovery I2C1 Interrupt to the highest priority */ |
EricLew | 0:ad9dfc0179dc | 812 | HAL_NVIC_SetPriority(DISCOVERY_I2C1_EV_IRQn, 0x00, 0); |
EricLew | 0:ad9dfc0179dc | 813 | HAL_NVIC_EnableIRQ(DISCOVERY_I2C1_EV_IRQn); |
EricLew | 0:ad9dfc0179dc | 814 | |
EricLew | 0:ad9dfc0179dc | 815 | /* Enable and set Discovery I2C1 Interrupt to the highest priority */ |
EricLew | 0:ad9dfc0179dc | 816 | HAL_NVIC_SetPriority(DISCOVERY_I2C1_ER_IRQn, 0x00, 0); |
EricLew | 0:ad9dfc0179dc | 817 | HAL_NVIC_EnableIRQ(DISCOVERY_I2C1_ER_IRQn); |
EricLew | 0:ad9dfc0179dc | 818 | } |
EricLew | 0:ad9dfc0179dc | 819 | } |
EricLew | 0:ad9dfc0179dc | 820 | |
EricLew | 0:ad9dfc0179dc | 821 | /** |
EricLew | 0:ad9dfc0179dc | 822 | * @brief Discovery I2C1 Bus Deitialization |
EricLew | 0:ad9dfc0179dc | 823 | * @retval None |
EricLew | 0:ad9dfc0179dc | 824 | */ |
EricLew | 0:ad9dfc0179dc | 825 | static void I2C1_DeInit(void) |
EricLew | 0:ad9dfc0179dc | 826 | { |
EricLew | 0:ad9dfc0179dc | 827 | if(HAL_I2C_GetState(&I2c1Handle) != HAL_I2C_STATE_RESET) |
EricLew | 0:ad9dfc0179dc | 828 | { |
EricLew | 0:ad9dfc0179dc | 829 | /* Deinit the I2C */ |
EricLew | 0:ad9dfc0179dc | 830 | HAL_I2C_DeInit(&I2c1Handle); |
EricLew | 0:ad9dfc0179dc | 831 | I2C1_MspDeInit(&I2c1Handle); |
EricLew | 0:ad9dfc0179dc | 832 | } |
EricLew | 0:ad9dfc0179dc | 833 | } |
EricLew | 0:ad9dfc0179dc | 834 | |
EricLew | 0:ad9dfc0179dc | 835 | /** |
EricLew | 0:ad9dfc0179dc | 836 | * @brief Discovery I2C1 MSP Deinitialization |
EricLew | 0:ad9dfc0179dc | 837 | * @param hi2c: I2C handle |
EricLew | 0:ad9dfc0179dc | 838 | * @retval None |
EricLew | 0:ad9dfc0179dc | 839 | */ |
EricLew | 0:ad9dfc0179dc | 840 | static void I2C1_MspDeInit(I2C_HandleTypeDef *hi2c) |
EricLew | 0:ad9dfc0179dc | 841 | { |
EricLew | 0:ad9dfc0179dc | 842 | if(hi2c->Instance == DISCOVERY_I2C1) |
EricLew | 0:ad9dfc0179dc | 843 | { |
EricLew | 0:ad9dfc0179dc | 844 | /*##-1- Unconfigure the GPIOs ################################################*/ |
EricLew | 0:ad9dfc0179dc | 845 | /* Enable GPIO clock */ |
EricLew | 0:ad9dfc0179dc | 846 | DISCOVERY_I2C1_SDA_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 847 | DISCOVERY_I2C1_SCL_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 848 | |
EricLew | 0:ad9dfc0179dc | 849 | /* Deinit Rx/Tx pins */ |
EricLew | 0:ad9dfc0179dc | 850 | HAL_GPIO_DeInit(DISCOVERY_I2C1_SCL_GPIO_PORT, (DISCOVERY_I2C1_SCL_PIN | DISCOVERY_I2C1_SDA_PIN)); |
EricLew | 0:ad9dfc0179dc | 851 | |
EricLew | 0:ad9dfc0179dc | 852 | /*##-2- Unconfigure the Discovery I2C1 peripheral ############################*/ |
EricLew | 0:ad9dfc0179dc | 853 | /* Force & Release the I2C Peripheral Clock Reset */ |
EricLew | 0:ad9dfc0179dc | 854 | DISCOVERY_I2C1_FORCE_RESET(); |
EricLew | 0:ad9dfc0179dc | 855 | DISCOVERY_I2C1_RELEASE_RESET(); |
EricLew | 0:ad9dfc0179dc | 856 | |
EricLew | 0:ad9dfc0179dc | 857 | /* Disable Discovery I2C1 clock */ |
EricLew | 0:ad9dfc0179dc | 858 | DISCOVERY_I2C1_CLK_DISABLE(); |
EricLew | 0:ad9dfc0179dc | 859 | |
EricLew | 0:ad9dfc0179dc | 860 | /* Disable Discovery I2C1 interrupts */ |
EricLew | 0:ad9dfc0179dc | 861 | HAL_NVIC_DisableIRQ(DISCOVERY_I2C1_EV_IRQn); |
EricLew | 0:ad9dfc0179dc | 862 | HAL_NVIC_DisableIRQ(DISCOVERY_I2C1_ER_IRQn); |
EricLew | 0:ad9dfc0179dc | 863 | |
EricLew | 0:ad9dfc0179dc | 864 | __HAL_RCC_PWR_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 865 | HAL_PWREx_DisableVddIO2(); |
EricLew | 0:ad9dfc0179dc | 866 | } |
EricLew | 0:ad9dfc0179dc | 867 | } |
EricLew | 0:ad9dfc0179dc | 868 | |
EricLew | 0:ad9dfc0179dc | 869 | /** |
EricLew | 0:ad9dfc0179dc | 870 | * @brief Write a value in a register of the device through BUS. |
EricLew | 0:ad9dfc0179dc | 871 | * @param Addr: Device address on BUS Bus. |
EricLew | 0:ad9dfc0179dc | 872 | * @param Reg: The target register address to write |
EricLew | 0:ad9dfc0179dc | 873 | * @param RegSize: The target register size (can be 8BIT or 16BIT) |
EricLew | 0:ad9dfc0179dc | 874 | * @param pBuffer: The target register value to be written |
EricLew | 0:ad9dfc0179dc | 875 | * @param Length: buffer size to be written |
EricLew | 0:ad9dfc0179dc | 876 | * @retval None |
EricLew | 0:ad9dfc0179dc | 877 | */ |
EricLew | 0:ad9dfc0179dc | 878 | static HAL_StatusTypeDef I2C1_WriteBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length) |
EricLew | 0:ad9dfc0179dc | 879 | { |
EricLew | 0:ad9dfc0179dc | 880 | HAL_StatusTypeDef status = HAL_OK; |
EricLew | 0:ad9dfc0179dc | 881 | |
EricLew | 0:ad9dfc0179dc | 882 | status = HAL_I2C_Mem_Write(&I2c1Handle, Addr, (uint16_t)Reg, RegSize, pBuffer, Length, I2c1Timeout); |
EricLew | 0:ad9dfc0179dc | 883 | |
EricLew | 0:ad9dfc0179dc | 884 | /* Check the communication status */ |
EricLew | 0:ad9dfc0179dc | 885 | if(status != HAL_OK) |
EricLew | 0:ad9dfc0179dc | 886 | { |
EricLew | 0:ad9dfc0179dc | 887 | /* Re-Initiaize the BUS */ |
EricLew | 0:ad9dfc0179dc | 888 | I2C1_Error(); |
EricLew | 0:ad9dfc0179dc | 889 | } |
EricLew | 0:ad9dfc0179dc | 890 | return status; |
EricLew | 0:ad9dfc0179dc | 891 | } |
EricLew | 0:ad9dfc0179dc | 892 | |
EricLew | 0:ad9dfc0179dc | 893 | /** |
EricLew | 0:ad9dfc0179dc | 894 | * @brief Reads multiple data on the BUS. |
EricLew | 0:ad9dfc0179dc | 895 | * @param Addr: I2C Address |
EricLew | 0:ad9dfc0179dc | 896 | * @param Reg: Reg Address |
EricLew | 0:ad9dfc0179dc | 897 | * @param RegSize : The target register size (can be 8BIT or 16BIT) |
EricLew | 0:ad9dfc0179dc | 898 | * @param pBuffer: pointer to read data buffer |
EricLew | 0:ad9dfc0179dc | 899 | * @param Length: length of the data |
EricLew | 0:ad9dfc0179dc | 900 | * @retval 0 if no problems to read multiple data |
EricLew | 0:ad9dfc0179dc | 901 | */ |
EricLew | 0:ad9dfc0179dc | 902 | static HAL_StatusTypeDef I2C1_ReadBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length) |
EricLew | 0:ad9dfc0179dc | 903 | { |
EricLew | 0:ad9dfc0179dc | 904 | HAL_StatusTypeDef status = HAL_OK; |
EricLew | 0:ad9dfc0179dc | 905 | |
EricLew | 0:ad9dfc0179dc | 906 | status = HAL_I2C_Mem_Read(&I2c1Handle, Addr, (uint16_t)Reg, RegSize, pBuffer, Length, I2c1Timeout); |
EricLew | 0:ad9dfc0179dc | 907 | |
EricLew | 0:ad9dfc0179dc | 908 | /* Check the communication status */ |
EricLew | 0:ad9dfc0179dc | 909 | if(status != HAL_OK) |
EricLew | 0:ad9dfc0179dc | 910 | { |
EricLew | 0:ad9dfc0179dc | 911 | /* Re-Initiaize the BUS */ |
EricLew | 0:ad9dfc0179dc | 912 | I2C1_Error(); |
EricLew | 0:ad9dfc0179dc | 913 | } |
EricLew | 0:ad9dfc0179dc | 914 | return status; |
EricLew | 0:ad9dfc0179dc | 915 | } |
EricLew | 0:ad9dfc0179dc | 916 | |
EricLew | 0:ad9dfc0179dc | 917 | /** |
EricLew | 0:ad9dfc0179dc | 918 | * @brief Discovery I2C1 error treatment function |
EricLew | 0:ad9dfc0179dc | 919 | * @retval None |
EricLew | 0:ad9dfc0179dc | 920 | */ |
EricLew | 0:ad9dfc0179dc | 921 | static void I2C1_Error (void) |
EricLew | 0:ad9dfc0179dc | 922 | { |
EricLew | 0:ad9dfc0179dc | 923 | /* De-initialize the I2C communication BUS */ |
EricLew | 0:ad9dfc0179dc | 924 | HAL_I2C_DeInit(&I2c1Handle); |
EricLew | 0:ad9dfc0179dc | 925 | |
EricLew | 0:ad9dfc0179dc | 926 | /* Re- Initiaize the I2C communication BUS */ |
EricLew | 0:ad9dfc0179dc | 927 | I2C1_Init(); |
EricLew | 0:ad9dfc0179dc | 928 | } |
EricLew | 0:ad9dfc0179dc | 929 | |
EricLew | 0:ad9dfc0179dc | 930 | /** |
EricLew | 0:ad9dfc0179dc | 931 | * @brief Discovery I2C2 Bus initialization |
EricLew | 0:ad9dfc0179dc | 932 | * @retval None |
EricLew | 0:ad9dfc0179dc | 933 | */ |
EricLew | 0:ad9dfc0179dc | 934 | static void I2C2_Init(void) |
EricLew | 0:ad9dfc0179dc | 935 | { |
EricLew | 0:ad9dfc0179dc | 936 | if(HAL_I2C_GetState(&I2c2Handle) == HAL_I2C_STATE_RESET) |
EricLew | 0:ad9dfc0179dc | 937 | { |
EricLew | 0:ad9dfc0179dc | 938 | I2c2Handle.Instance = DISCOVERY_I2C2; |
EricLew | 0:ad9dfc0179dc | 939 | I2c2Handle.Init.Timing = DISCOVERY_I2C2_TIMING; |
EricLew | 0:ad9dfc0179dc | 940 | I2c2Handle.Init.OwnAddress1 = 0; |
EricLew | 0:ad9dfc0179dc | 941 | I2c2Handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
EricLew | 0:ad9dfc0179dc | 942 | I2c2Handle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; |
EricLew | 0:ad9dfc0179dc | 943 | I2c2Handle.Init.OwnAddress2 = 0; |
EricLew | 0:ad9dfc0179dc | 944 | I2c2Handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; |
EricLew | 0:ad9dfc0179dc | 945 | I2c2Handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; |
EricLew | 0:ad9dfc0179dc | 946 | |
EricLew | 0:ad9dfc0179dc | 947 | /* Init the I2C */ |
EricLew | 0:ad9dfc0179dc | 948 | I2C2_MspInit(&I2c2Handle); |
EricLew | 0:ad9dfc0179dc | 949 | HAL_I2C_Init(&I2c2Handle); |
EricLew | 0:ad9dfc0179dc | 950 | } |
EricLew | 0:ad9dfc0179dc | 951 | } |
EricLew | 0:ad9dfc0179dc | 952 | |
EricLew | 0:ad9dfc0179dc | 953 | /** |
EricLew | 0:ad9dfc0179dc | 954 | * @brief Discovery I2C2 MSP Initialization |
EricLew | 0:ad9dfc0179dc | 955 | * @param hi2c: I2C2 handle |
EricLew | 0:ad9dfc0179dc | 956 | * @retval None |
EricLew | 0:ad9dfc0179dc | 957 | */ |
EricLew | 0:ad9dfc0179dc | 958 | static void I2C2_MspInit(I2C_HandleTypeDef *hi2c) |
EricLew | 0:ad9dfc0179dc | 959 | { |
EricLew | 0:ad9dfc0179dc | 960 | GPIO_InitTypeDef GPIO_InitStructure; |
EricLew | 0:ad9dfc0179dc | 961 | RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct; |
EricLew | 0:ad9dfc0179dc | 962 | |
EricLew | 0:ad9dfc0179dc | 963 | if (hi2c->Instance == DISCOVERY_I2C2) |
EricLew | 0:ad9dfc0179dc | 964 | { |
EricLew | 0:ad9dfc0179dc | 965 | /*##-1- Configure the Discovery I2C2 clock source. The clock is derived from the SYSCLK #*/ |
EricLew | 0:ad9dfc0179dc | 966 | RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C2; |
EricLew | 0:ad9dfc0179dc | 967 | RCC_PeriphCLKInitStruct.I2c2ClockSelection = RCC_I2C2CLKSOURCE_SYSCLK; |
EricLew | 0:ad9dfc0179dc | 968 | HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct); |
EricLew | 0:ad9dfc0179dc | 969 | |
EricLew | 0:ad9dfc0179dc | 970 | /*##-2- Configure the GPIOs ################################################*/ |
EricLew | 0:ad9dfc0179dc | 971 | /* Enable GPIO clock */ |
EricLew | 0:ad9dfc0179dc | 972 | DISCOVERY_I2C2_SDA_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 973 | DISCOVERY_I2C2_SCL_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 974 | |
EricLew | 0:ad9dfc0179dc | 975 | /* Configure I2C Rx/Tx as alternate function */ |
EricLew | 0:ad9dfc0179dc | 976 | GPIO_InitStructure.Pin = DISCOVERY_I2C2_SCL_PIN | DISCOVERY_I2C2_SDA_PIN; |
EricLew | 0:ad9dfc0179dc | 977 | GPIO_InitStructure.Mode = GPIO_MODE_AF_OD; |
EricLew | 0:ad9dfc0179dc | 978 | GPIO_InitStructure.Pull = GPIO_PULLUP; |
EricLew | 0:ad9dfc0179dc | 979 | GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 0:ad9dfc0179dc | 980 | GPIO_InitStructure.Alternate = DISCOVERY_I2C2_SCL_SDA_AF; |
EricLew | 0:ad9dfc0179dc | 981 | HAL_GPIO_Init(DISCOVERY_I2C2_SCL_GPIO_PORT, &GPIO_InitStructure); |
EricLew | 0:ad9dfc0179dc | 982 | |
EricLew | 0:ad9dfc0179dc | 983 | /*##-3- Configure the Discovery I2C2 peripheral #############################*/ |
EricLew | 0:ad9dfc0179dc | 984 | /* Enable Discovery_I2C2 clock */ |
EricLew | 0:ad9dfc0179dc | 985 | DISCOVERY_I2C2_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 986 | |
EricLew | 0:ad9dfc0179dc | 987 | /* Force and release the I2C Peripheral Clock Reset */ |
EricLew | 0:ad9dfc0179dc | 988 | DISCOVERY_I2C2_FORCE_RESET(); |
EricLew | 0:ad9dfc0179dc | 989 | DISCOVERY_I2C2_RELEASE_RESET(); |
EricLew | 0:ad9dfc0179dc | 990 | |
EricLew | 0:ad9dfc0179dc | 991 | /* Enable and set Discovery I2C2 Interrupt to the highest priority */ |
EricLew | 0:ad9dfc0179dc | 992 | HAL_NVIC_SetPriority(DISCOVERY_I2C2_EV_IRQn, 0x00, 0); |
EricLew | 0:ad9dfc0179dc | 993 | HAL_NVIC_EnableIRQ(DISCOVERY_I2C2_EV_IRQn); |
EricLew | 0:ad9dfc0179dc | 994 | |
EricLew | 0:ad9dfc0179dc | 995 | /* Enable and set Discovery I2C2 Interrupt to the highest priority */ |
EricLew | 0:ad9dfc0179dc | 996 | HAL_NVIC_SetPriority(DISCOVERY_I2C2_ER_IRQn, 0x00, 0); |
EricLew | 0:ad9dfc0179dc | 997 | HAL_NVIC_EnableIRQ(DISCOVERY_I2C2_ER_IRQn); |
EricLew | 0:ad9dfc0179dc | 998 | } |
EricLew | 0:ad9dfc0179dc | 999 | } |
EricLew | 0:ad9dfc0179dc | 1000 | |
EricLew | 0:ad9dfc0179dc | 1001 | /** |
EricLew | 0:ad9dfc0179dc | 1002 | * @brief Discovery I2C2 Bus Deinitialization |
EricLew | 0:ad9dfc0179dc | 1003 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1004 | */ |
EricLew | 0:ad9dfc0179dc | 1005 | static void I2C2_DeInit(void) |
EricLew | 0:ad9dfc0179dc | 1006 | { |
EricLew | 0:ad9dfc0179dc | 1007 | if(HAL_I2C_GetState(&I2c2Handle) != HAL_I2C_STATE_RESET) |
EricLew | 0:ad9dfc0179dc | 1008 | { |
EricLew | 0:ad9dfc0179dc | 1009 | /* DeInit the I2C */ |
EricLew | 0:ad9dfc0179dc | 1010 | HAL_I2C_DeInit(&I2c2Handle); |
EricLew | 0:ad9dfc0179dc | 1011 | I2C2_MspDeInit(&I2c2Handle); |
EricLew | 0:ad9dfc0179dc | 1012 | } |
EricLew | 0:ad9dfc0179dc | 1013 | } |
EricLew | 0:ad9dfc0179dc | 1014 | |
EricLew | 0:ad9dfc0179dc | 1015 | /** |
EricLew | 0:ad9dfc0179dc | 1016 | * @brief Discovery I2C2 MSP DeInitialization |
EricLew | 0:ad9dfc0179dc | 1017 | * @param hi2c: I2C2 handle |
EricLew | 0:ad9dfc0179dc | 1018 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1019 | */ |
EricLew | 0:ad9dfc0179dc | 1020 | static void I2C2_MspDeInit(I2C_HandleTypeDef *hi2c) |
EricLew | 0:ad9dfc0179dc | 1021 | { |
EricLew | 0:ad9dfc0179dc | 1022 | if (hi2c->Instance == DISCOVERY_I2C2) |
EricLew | 0:ad9dfc0179dc | 1023 | { |
EricLew | 0:ad9dfc0179dc | 1024 | /*##-1- Unconfigure the GPIOs ################################################*/ |
EricLew | 0:ad9dfc0179dc | 1025 | /* Enable GPIO clock */ |
EricLew | 0:ad9dfc0179dc | 1026 | DISCOVERY_I2C2_SDA_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1027 | DISCOVERY_I2C2_SCL_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1028 | |
EricLew | 0:ad9dfc0179dc | 1029 | /* Configure I2C Rx/Tx as alternate function */ |
EricLew | 0:ad9dfc0179dc | 1030 | HAL_GPIO_DeInit(DISCOVERY_I2C2_SCL_GPIO_PORT, (DISCOVERY_I2C2_SCL_PIN | DISCOVERY_I2C2_SDA_PIN)); |
EricLew | 0:ad9dfc0179dc | 1031 | |
EricLew | 0:ad9dfc0179dc | 1032 | /*##-2- Unconfigure the Discovery I2C2 peripheral ############################*/ |
EricLew | 0:ad9dfc0179dc | 1033 | /* Force and release I2C Peripheral */ |
EricLew | 0:ad9dfc0179dc | 1034 | DISCOVERY_I2C2_FORCE_RESET(); |
EricLew | 0:ad9dfc0179dc | 1035 | DISCOVERY_I2C2_RELEASE_RESET(); |
EricLew | 0:ad9dfc0179dc | 1036 | |
EricLew | 0:ad9dfc0179dc | 1037 | /* Disable Discovery I2C2 clock */ |
EricLew | 0:ad9dfc0179dc | 1038 | DISCOVERY_I2C2_CLK_DISABLE(); |
EricLew | 0:ad9dfc0179dc | 1039 | |
EricLew | 0:ad9dfc0179dc | 1040 | /* Disable Discovery I2C2 interrupts */ |
EricLew | 0:ad9dfc0179dc | 1041 | HAL_NVIC_DisableIRQ(DISCOVERY_I2C2_EV_IRQn); |
EricLew | 0:ad9dfc0179dc | 1042 | HAL_NVIC_DisableIRQ(DISCOVERY_I2C2_ER_IRQn); |
EricLew | 0:ad9dfc0179dc | 1043 | } |
EricLew | 0:ad9dfc0179dc | 1044 | } |
EricLew | 0:ad9dfc0179dc | 1045 | |
EricLew | 0:ad9dfc0179dc | 1046 | /** |
EricLew | 0:ad9dfc0179dc | 1047 | * @brief Write a value in a register of the device through BUS. |
EricLew | 0:ad9dfc0179dc | 1048 | * @param Addr: Device address on BUS Bus. |
EricLew | 0:ad9dfc0179dc | 1049 | * @param Reg: The target register address to write |
EricLew | 0:ad9dfc0179dc | 1050 | * @param RegSize: The target register size (can be 8BIT or 16BIT) |
EricLew | 0:ad9dfc0179dc | 1051 | * @param Value: The target register value to be written |
EricLew | 0:ad9dfc0179dc | 1052 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1053 | */ |
EricLew | 0:ad9dfc0179dc | 1054 | static void I2C2_WriteData(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t Value) |
EricLew | 0:ad9dfc0179dc | 1055 | { |
EricLew | 0:ad9dfc0179dc | 1056 | HAL_StatusTypeDef status = HAL_OK; |
EricLew | 0:ad9dfc0179dc | 1057 | |
EricLew | 0:ad9dfc0179dc | 1058 | status = HAL_I2C_Mem_Write(&I2c2Handle, Addr, (uint16_t)Reg, RegSize, &Value, 1, I2c2Timeout); |
EricLew | 0:ad9dfc0179dc | 1059 | |
EricLew | 0:ad9dfc0179dc | 1060 | /* Check the communication status */ |
EricLew | 0:ad9dfc0179dc | 1061 | if(status != HAL_OK) |
EricLew | 0:ad9dfc0179dc | 1062 | { |
EricLew | 0:ad9dfc0179dc | 1063 | /* Re-Initiaize the BUS */ |
EricLew | 0:ad9dfc0179dc | 1064 | I2C2_Error(); |
EricLew | 0:ad9dfc0179dc | 1065 | } |
EricLew | 0:ad9dfc0179dc | 1066 | } |
EricLew | 0:ad9dfc0179dc | 1067 | |
EricLew | 0:ad9dfc0179dc | 1068 | /** |
EricLew | 0:ad9dfc0179dc | 1069 | * @brief Write a value in a register of the device through BUS. |
EricLew | 0:ad9dfc0179dc | 1070 | * @param Addr: Device address on BUS Bus. |
EricLew | 0:ad9dfc0179dc | 1071 | * @param Reg: The target register address to write |
EricLew | 0:ad9dfc0179dc | 1072 | * @param RegSize: The target register size (can be 8BIT or 16BIT) |
EricLew | 0:ad9dfc0179dc | 1073 | * @param pBuffer: The target register value to be written |
EricLew | 0:ad9dfc0179dc | 1074 | * @param Length: buffer size to be written |
EricLew | 0:ad9dfc0179dc | 1075 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1076 | */ |
EricLew | 0:ad9dfc0179dc | 1077 | static HAL_StatusTypeDef I2C2_WriteBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length) |
EricLew | 0:ad9dfc0179dc | 1078 | { |
EricLew | 0:ad9dfc0179dc | 1079 | HAL_StatusTypeDef status = HAL_OK; |
EricLew | 0:ad9dfc0179dc | 1080 | |
EricLew | 0:ad9dfc0179dc | 1081 | status = HAL_I2C_Mem_Write(&I2c2Handle, Addr, (uint16_t)Reg, RegSize, pBuffer, Length, I2c2Timeout); |
EricLew | 0:ad9dfc0179dc | 1082 | |
EricLew | 0:ad9dfc0179dc | 1083 | /* Check the communication status */ |
EricLew | 0:ad9dfc0179dc | 1084 | if(status != HAL_OK) |
EricLew | 0:ad9dfc0179dc | 1085 | { |
EricLew | 0:ad9dfc0179dc | 1086 | /* Re-Initiaize the BUS */ |
EricLew | 0:ad9dfc0179dc | 1087 | I2C2_Error(); |
EricLew | 0:ad9dfc0179dc | 1088 | } |
EricLew | 0:ad9dfc0179dc | 1089 | |
EricLew | 0:ad9dfc0179dc | 1090 | return status; |
EricLew | 0:ad9dfc0179dc | 1091 | } |
EricLew | 0:ad9dfc0179dc | 1092 | |
EricLew | 0:ad9dfc0179dc | 1093 | /** |
EricLew | 0:ad9dfc0179dc | 1094 | * @brief Read a register of the device through BUS |
EricLew | 0:ad9dfc0179dc | 1095 | * @param Addr: Device address on BUS |
EricLew | 0:ad9dfc0179dc | 1096 | * @param Reg: The target register address to read |
EricLew | 0:ad9dfc0179dc | 1097 | * @param RegSize: The target register size (can be 8BIT or 16BIT) |
EricLew | 0:ad9dfc0179dc | 1098 | * @retval read register value |
EricLew | 0:ad9dfc0179dc | 1099 | */ |
EricLew | 0:ad9dfc0179dc | 1100 | static uint8_t I2C2_ReadData(uint16_t Addr, uint16_t Reg, uint16_t RegSize) |
EricLew | 0:ad9dfc0179dc | 1101 | { |
EricLew | 0:ad9dfc0179dc | 1102 | HAL_StatusTypeDef status = HAL_OK; |
EricLew | 0:ad9dfc0179dc | 1103 | uint8_t value = 0x0; |
EricLew | 0:ad9dfc0179dc | 1104 | |
EricLew | 0:ad9dfc0179dc | 1105 | status = HAL_I2C_Mem_Read(&I2c2Handle, Addr, Reg, RegSize, &value, 1, I2c2Timeout); |
EricLew | 0:ad9dfc0179dc | 1106 | |
EricLew | 0:ad9dfc0179dc | 1107 | /* Check the communication status */ |
EricLew | 0:ad9dfc0179dc | 1108 | if(status != HAL_OK) |
EricLew | 0:ad9dfc0179dc | 1109 | { |
EricLew | 0:ad9dfc0179dc | 1110 | /* Re-Initiaize the BUS */ |
EricLew | 0:ad9dfc0179dc | 1111 | I2C2_Error(); |
EricLew | 0:ad9dfc0179dc | 1112 | } |
EricLew | 0:ad9dfc0179dc | 1113 | |
EricLew | 0:ad9dfc0179dc | 1114 | return value; |
EricLew | 0:ad9dfc0179dc | 1115 | } |
EricLew | 0:ad9dfc0179dc | 1116 | |
EricLew | 0:ad9dfc0179dc | 1117 | /** |
EricLew | 0:ad9dfc0179dc | 1118 | * @brief Reads multiple data on the BUS. |
EricLew | 0:ad9dfc0179dc | 1119 | * @param Addr: I2C Address |
EricLew | 0:ad9dfc0179dc | 1120 | * @param Reg: Reg Address |
EricLew | 0:ad9dfc0179dc | 1121 | * @param RegSize : The target register size (can be 8BIT or 16BIT) |
EricLew | 0:ad9dfc0179dc | 1122 | * @param pBuffer: pointer to read data buffer |
EricLew | 0:ad9dfc0179dc | 1123 | * @param Length: length of the data |
EricLew | 0:ad9dfc0179dc | 1124 | * @retval 0 if no problems to read multiple data |
EricLew | 0:ad9dfc0179dc | 1125 | */ |
EricLew | 0:ad9dfc0179dc | 1126 | static HAL_StatusTypeDef I2C2_ReadBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length) |
EricLew | 0:ad9dfc0179dc | 1127 | { |
EricLew | 0:ad9dfc0179dc | 1128 | HAL_StatusTypeDef status = HAL_OK; |
EricLew | 0:ad9dfc0179dc | 1129 | |
EricLew | 0:ad9dfc0179dc | 1130 | status = HAL_I2C_Mem_Read(&I2c2Handle, Addr, (uint16_t)Reg, RegSize, pBuffer, Length, I2c2Timeout); |
EricLew | 0:ad9dfc0179dc | 1131 | |
EricLew | 0:ad9dfc0179dc | 1132 | /* Check the communication status */ |
EricLew | 0:ad9dfc0179dc | 1133 | if(status != HAL_OK) |
EricLew | 0:ad9dfc0179dc | 1134 | { |
EricLew | 0:ad9dfc0179dc | 1135 | /* Re-Initiaize the BUS */ |
EricLew | 0:ad9dfc0179dc | 1136 | I2C2_Error(); |
EricLew | 0:ad9dfc0179dc | 1137 | } |
EricLew | 0:ad9dfc0179dc | 1138 | |
EricLew | 0:ad9dfc0179dc | 1139 | return status; |
EricLew | 0:ad9dfc0179dc | 1140 | } |
EricLew | 0:ad9dfc0179dc | 1141 | |
EricLew | 0:ad9dfc0179dc | 1142 | /** |
EricLew | 0:ad9dfc0179dc | 1143 | * @brief Discovery I2C2 error treatment function |
EricLew | 0:ad9dfc0179dc | 1144 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1145 | */ |
EricLew | 0:ad9dfc0179dc | 1146 | static void I2C2_Error (void) |
EricLew | 0:ad9dfc0179dc | 1147 | { |
EricLew | 0:ad9dfc0179dc | 1148 | /* De-initialize the I2C communication BUS */ |
EricLew | 0:ad9dfc0179dc | 1149 | HAL_I2C_DeInit(&I2c2Handle); |
EricLew | 0:ad9dfc0179dc | 1150 | |
EricLew | 0:ad9dfc0179dc | 1151 | /* Re- Initiaize the I2C communication BUS */ |
EricLew | 0:ad9dfc0179dc | 1152 | I2C2_Init(); |
EricLew | 0:ad9dfc0179dc | 1153 | } |
EricLew | 0:ad9dfc0179dc | 1154 | #endif /*HAL_I2C_MODULE_ENABLED*/ |
EricLew | 0:ad9dfc0179dc | 1155 | |
EricLew | 0:ad9dfc0179dc | 1156 | |
EricLew | 0:ad9dfc0179dc | 1157 | /******************************************************************************* |
EricLew | 0:ad9dfc0179dc | 1158 | LINK OPERATIONS |
EricLew | 0:ad9dfc0179dc | 1159 | *******************************************************************************/ |
EricLew | 0:ad9dfc0179dc | 1160 | #if defined(HAL_SPI_MODULE_ENABLED) |
EricLew | 0:ad9dfc0179dc | 1161 | /*********************** LINK ACCELEROMETER ***********************************/ |
EricLew | 0:ad9dfc0179dc | 1162 | /** |
EricLew | 0:ad9dfc0179dc | 1163 | * @brief Configures COMPASS/ACCELEROMETER io interface. |
EricLew | 0:ad9dfc0179dc | 1164 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1165 | */ |
EricLew | 0:ad9dfc0179dc | 1166 | void ACCELERO_IO_Init(void) |
EricLew | 0:ad9dfc0179dc | 1167 | { |
EricLew | 0:ad9dfc0179dc | 1168 | GPIO_InitTypeDef GPIO_InitStructure; |
EricLew | 0:ad9dfc0179dc | 1169 | |
EricLew | 0:ad9dfc0179dc | 1170 | /* Enable CS GPIO clock and Configure GPIO PIN for Gyroscope Chip select */ |
EricLew | 0:ad9dfc0179dc | 1171 | ACCELERO_CS_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1172 | GPIO_InitStructure.Pin = ACCELERO_CS_PIN; |
EricLew | 0:ad9dfc0179dc | 1173 | GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
EricLew | 0:ad9dfc0179dc | 1174 | GPIO_InitStructure.Pull = GPIO_NOPULL; |
EricLew | 0:ad9dfc0179dc | 1175 | GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 0:ad9dfc0179dc | 1176 | HAL_GPIO_Init(ACCELERO_CS_GPIO_PORT, &GPIO_InitStructure); |
EricLew | 0:ad9dfc0179dc | 1177 | |
EricLew | 0:ad9dfc0179dc | 1178 | /* Deselect : Chip Select high */ |
EricLew | 0:ad9dfc0179dc | 1179 | ACCELERO_CS_HIGH(); |
EricLew | 0:ad9dfc0179dc | 1180 | |
EricLew | 0:ad9dfc0179dc | 1181 | SPIx_Init(); |
EricLew | 0:ad9dfc0179dc | 1182 | } |
EricLew | 0:ad9dfc0179dc | 1183 | |
EricLew | 0:ad9dfc0179dc | 1184 | /** |
EricLew | 0:ad9dfc0179dc | 1185 | * @brief De-Configures COMPASS/ACCELEROMETER io interface. |
EricLew | 0:ad9dfc0179dc | 1186 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1187 | */ |
EricLew | 0:ad9dfc0179dc | 1188 | void ACCELERO_IO_DeInit(void) |
EricLew | 0:ad9dfc0179dc | 1189 | { |
EricLew | 0:ad9dfc0179dc | 1190 | GPIO_InitTypeDef GPIO_InitStructure; |
EricLew | 0:ad9dfc0179dc | 1191 | |
EricLew | 0:ad9dfc0179dc | 1192 | /* Enable CS GPIO clock and Configure GPIO PIN for Gyroscope Chip select */ |
EricLew | 0:ad9dfc0179dc | 1193 | ACCELERO_CS_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1194 | GPIO_InitStructure.Pin = ACCELERO_CS_PIN; |
EricLew | 0:ad9dfc0179dc | 1195 | GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
EricLew | 0:ad9dfc0179dc | 1196 | GPIO_InitStructure.Pull = GPIO_NOPULL; |
EricLew | 0:ad9dfc0179dc | 1197 | GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 0:ad9dfc0179dc | 1198 | HAL_GPIO_Init(ACCELERO_CS_GPIO_PORT, &GPIO_InitStructure); |
EricLew | 0:ad9dfc0179dc | 1199 | |
EricLew | 0:ad9dfc0179dc | 1200 | /* Deselect : Chip Select high */ |
EricLew | 0:ad9dfc0179dc | 1201 | ACCELERO_CS_HIGH(); |
EricLew | 0:ad9dfc0179dc | 1202 | |
EricLew | 0:ad9dfc0179dc | 1203 | /* Uninitialize SPI bus */ |
EricLew | 0:ad9dfc0179dc | 1204 | SPIx_DeInit(); |
EricLew | 0:ad9dfc0179dc | 1205 | } |
EricLew | 0:ad9dfc0179dc | 1206 | |
EricLew | 0:ad9dfc0179dc | 1207 | /** |
EricLew | 0:ad9dfc0179dc | 1208 | * @brief Configures COMPASS / ACCELERO click IT |
EricLew | 0:ad9dfc0179dc | 1209 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1210 | */ |
EricLew | 0:ad9dfc0179dc | 1211 | void ACCELERO_IO_ITConfig(void) |
EricLew | 0:ad9dfc0179dc | 1212 | { |
EricLew | 0:ad9dfc0179dc | 1213 | } |
EricLew | 0:ad9dfc0179dc | 1214 | |
EricLew | 0:ad9dfc0179dc | 1215 | /** |
EricLew | 0:ad9dfc0179dc | 1216 | * @brief Writes one byte to the COMPASS / ACCELEROMETER. |
EricLew | 0:ad9dfc0179dc | 1217 | * @param RegisterAddr specifies the COMPASS / ACCELEROMETER register to be written. |
EricLew | 0:ad9dfc0179dc | 1218 | * @param Value : Data to be written |
EricLew | 0:ad9dfc0179dc | 1219 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1220 | */ |
EricLew | 0:ad9dfc0179dc | 1221 | void ACCELERO_IO_Write(uint8_t RegisterAddr, uint8_t Value) |
EricLew | 0:ad9dfc0179dc | 1222 | { |
EricLew | 0:ad9dfc0179dc | 1223 | ACCELERO_CS_LOW(); |
EricLew | 0:ad9dfc0179dc | 1224 | __SPI_DIRECTION_1LINE_TX(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 1225 | /* call SPI Read data bus function */ |
EricLew | 0:ad9dfc0179dc | 1226 | SPIx_Write(RegisterAddr); |
EricLew | 0:ad9dfc0179dc | 1227 | SPIx_Write(Value); |
EricLew | 0:ad9dfc0179dc | 1228 | ACCELERO_CS_HIGH(); |
EricLew | 0:ad9dfc0179dc | 1229 | } |
EricLew | 0:ad9dfc0179dc | 1230 | |
EricLew | 0:ad9dfc0179dc | 1231 | /** |
EricLew | 0:ad9dfc0179dc | 1232 | * @brief Reads a block of data from the COMPASS / ACCELEROMETER. |
EricLew | 0:ad9dfc0179dc | 1233 | * @param RegisterAddr : specifies the COMPASS / ACCELEROMETER internal address register to read from |
EricLew | 0:ad9dfc0179dc | 1234 | * @retval ACCELEROMETER register value |
EricLew | 0:ad9dfc0179dc | 1235 | */ |
EricLew | 0:ad9dfc0179dc | 1236 | uint8_t ACCELERO_IO_Read(uint8_t RegisterAddr) |
EricLew | 0:ad9dfc0179dc | 1237 | { |
EricLew | 0:ad9dfc0179dc | 1238 | RegisterAddr = RegisterAddr | ((uint8_t)0x80); |
EricLew | 0:ad9dfc0179dc | 1239 | ACCELERO_CS_LOW(); |
EricLew | 0:ad9dfc0179dc | 1240 | __SPI_DIRECTION_1LINE_TX(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 1241 | SPIx_Write(RegisterAddr); |
EricLew | 0:ad9dfc0179dc | 1242 | __SPI_DIRECTION_1LINE_RX(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 1243 | uint8_t val = SPIx_Read(); |
EricLew | 0:ad9dfc0179dc | 1244 | ACCELERO_CS_HIGH(); |
EricLew | 0:ad9dfc0179dc | 1245 | return val; |
EricLew | 0:ad9dfc0179dc | 1246 | } |
EricLew | 0:ad9dfc0179dc | 1247 | |
EricLew | 0:ad9dfc0179dc | 1248 | /********************************* LINK MAGNETO *******************************/ |
EricLew | 0:ad9dfc0179dc | 1249 | /** |
EricLew | 0:ad9dfc0179dc | 1250 | * @brief Configures COMPASS/MAGNETO SPI interface. |
EricLew | 0:ad9dfc0179dc | 1251 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1252 | */ |
EricLew | 0:ad9dfc0179dc | 1253 | void MAGNETO_IO_Init(void) |
EricLew | 0:ad9dfc0179dc | 1254 | { |
EricLew | 0:ad9dfc0179dc | 1255 | GPIO_InitTypeDef GPIO_InitStructure; |
EricLew | 0:ad9dfc0179dc | 1256 | |
EricLew | 0:ad9dfc0179dc | 1257 | /* Enable CS GPIO clock and Configure GPIO PIN for Gyroscope Chip select */ |
EricLew | 0:ad9dfc0179dc | 1258 | MAGNETO_CS_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1259 | GPIO_InitStructure.Pin = MAGNETO_CS_PIN; |
EricLew | 0:ad9dfc0179dc | 1260 | GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
EricLew | 0:ad9dfc0179dc | 1261 | GPIO_InitStructure.Pull = GPIO_NOPULL; |
EricLew | 0:ad9dfc0179dc | 1262 | GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 0:ad9dfc0179dc | 1263 | HAL_GPIO_Init(MAGNETO_CS_GPIO_PORT, &GPIO_InitStructure); |
EricLew | 0:ad9dfc0179dc | 1264 | |
EricLew | 0:ad9dfc0179dc | 1265 | /* Deselect : Chip Select high */ |
EricLew | 0:ad9dfc0179dc | 1266 | MAGNETO_CS_HIGH(); |
EricLew | 0:ad9dfc0179dc | 1267 | |
EricLew | 0:ad9dfc0179dc | 1268 | SPIx_Init(); |
EricLew | 0:ad9dfc0179dc | 1269 | } |
EricLew | 0:ad9dfc0179dc | 1270 | |
EricLew | 0:ad9dfc0179dc | 1271 | /** |
EricLew | 0:ad9dfc0179dc | 1272 | * @brief de-Configures COMPASS/MAGNETO SPI interface. |
EricLew | 0:ad9dfc0179dc | 1273 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1274 | */ |
EricLew | 0:ad9dfc0179dc | 1275 | void MAGNETO_IO_DeInit(void) |
EricLew | 0:ad9dfc0179dc | 1276 | { |
EricLew | 0:ad9dfc0179dc | 1277 | GPIO_InitTypeDef GPIO_InitStructure; |
EricLew | 0:ad9dfc0179dc | 1278 | |
EricLew | 0:ad9dfc0179dc | 1279 | /* Enable CS GPIO clock and Configure GPIO PIN for Gyroscope Chip select */ |
EricLew | 0:ad9dfc0179dc | 1280 | MAGNETO_CS_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1281 | GPIO_InitStructure.Pin = MAGNETO_CS_PIN; |
EricLew | 0:ad9dfc0179dc | 1282 | GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
EricLew | 0:ad9dfc0179dc | 1283 | GPIO_InitStructure.Pull = GPIO_NOPULL; |
EricLew | 0:ad9dfc0179dc | 1284 | GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 0:ad9dfc0179dc | 1285 | HAL_GPIO_Init(MAGNETO_CS_GPIO_PORT, &GPIO_InitStructure); |
EricLew | 0:ad9dfc0179dc | 1286 | |
EricLew | 0:ad9dfc0179dc | 1287 | /* Deselect : Chip Select high */ |
EricLew | 0:ad9dfc0179dc | 1288 | MAGNETO_CS_HIGH(); |
EricLew | 0:ad9dfc0179dc | 1289 | |
EricLew | 0:ad9dfc0179dc | 1290 | HAL_GPIO_DeInit(MAGNETO_CS_GPIO_PORT, MAGNETO_INT1_PIN|MAGNETO_DRDY_PIN); |
EricLew | 0:ad9dfc0179dc | 1291 | |
EricLew | 0:ad9dfc0179dc | 1292 | |
EricLew | 0:ad9dfc0179dc | 1293 | /* Uninitialize SPI bus */ |
EricLew | 0:ad9dfc0179dc | 1294 | SPIx_DeInit(); |
EricLew | 0:ad9dfc0179dc | 1295 | } |
EricLew | 0:ad9dfc0179dc | 1296 | |
EricLew | 0:ad9dfc0179dc | 1297 | /** |
EricLew | 0:ad9dfc0179dc | 1298 | * @brief Writes one byte to the COMPASS/MAGNETO. |
EricLew | 0:ad9dfc0179dc | 1299 | * @param RegisterAddr specifies the COMPASS/MAGNETO register to be written. |
EricLew | 0:ad9dfc0179dc | 1300 | * @param Value : Data to be written |
EricLew | 0:ad9dfc0179dc | 1301 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1302 | */ |
EricLew | 0:ad9dfc0179dc | 1303 | void MAGNETO_IO_Write(uint8_t RegisterAddr, uint8_t Value) |
EricLew | 0:ad9dfc0179dc | 1304 | { |
EricLew | 0:ad9dfc0179dc | 1305 | MAGNETO_CS_LOW(); |
EricLew | 0:ad9dfc0179dc | 1306 | __SPI_DIRECTION_1LINE_TX(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 1307 | /* call SPI Read data bus function */ |
EricLew | 0:ad9dfc0179dc | 1308 | SPIx_Write(RegisterAddr); |
EricLew | 0:ad9dfc0179dc | 1309 | SPIx_Write(Value); |
EricLew | 0:ad9dfc0179dc | 1310 | MAGNETO_CS_HIGH(); |
EricLew | 0:ad9dfc0179dc | 1311 | } |
EricLew | 0:ad9dfc0179dc | 1312 | |
EricLew | 0:ad9dfc0179dc | 1313 | /** |
EricLew | 0:ad9dfc0179dc | 1314 | * @brief Reads a block of data from the COMPASS/MAGNETO. |
EricLew | 0:ad9dfc0179dc | 1315 | * @param RegisterAddr : specifies the COMPASS/MAGNETO internal address register to read from |
EricLew | 0:ad9dfc0179dc | 1316 | * @retval ACCELEROMETER register value |
EricLew | 0:ad9dfc0179dc | 1317 | */ |
EricLew | 0:ad9dfc0179dc | 1318 | uint8_t MAGNETO_IO_Read(uint8_t RegisterAddr) |
EricLew | 0:ad9dfc0179dc | 1319 | { |
EricLew | 0:ad9dfc0179dc | 1320 | MAGNETO_CS_LOW(); |
EricLew | 0:ad9dfc0179dc | 1321 | __SPI_DIRECTION_1LINE_TX(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 1322 | SPIx_Write(RegisterAddr | 0x80); |
EricLew | 0:ad9dfc0179dc | 1323 | __SPI_DIRECTION_1LINE_RX(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 1324 | uint8_t val = SPIx_Read(); |
EricLew | 0:ad9dfc0179dc | 1325 | MAGNETO_CS_HIGH(); |
EricLew | 0:ad9dfc0179dc | 1326 | return val; |
EricLew | 0:ad9dfc0179dc | 1327 | } |
EricLew | 0:ad9dfc0179dc | 1328 | |
EricLew | 0:ad9dfc0179dc | 1329 | /********************************* LINK GYRO *****************************/ |
EricLew | 0:ad9dfc0179dc | 1330 | /** |
EricLew | 0:ad9dfc0179dc | 1331 | * @brief Configures the GYRO SPI interface. |
EricLew | 0:ad9dfc0179dc | 1332 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1333 | */ |
EricLew | 0:ad9dfc0179dc | 1334 | void GYRO_IO_Init(void) |
EricLew | 0:ad9dfc0179dc | 1335 | { |
EricLew | 0:ad9dfc0179dc | 1336 | GPIO_InitTypeDef GPIO_InitStructure; |
EricLew | 0:ad9dfc0179dc | 1337 | |
EricLew | 0:ad9dfc0179dc | 1338 | |
EricLew | 0:ad9dfc0179dc | 1339 | /* Case GYRO not used in the demonstration software except being set in |
EricLew | 0:ad9dfc0179dc | 1340 | low power mode. |
EricLew | 0:ad9dfc0179dc | 1341 | To avoid access conflicts with accelerometer and magnetometer, |
EricLew | 0:ad9dfc0179dc | 1342 | initialize XL_CS and MAG_CS pins then deselect these I/O */ |
EricLew | 0:ad9dfc0179dc | 1343 | ACCELERO_CS_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1344 | GPIO_InitStructure.Pin = ACCELERO_CS_PIN; |
EricLew | 0:ad9dfc0179dc | 1345 | GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
EricLew | 0:ad9dfc0179dc | 1346 | GPIO_InitStructure.Pull = GPIO_NOPULL; |
EricLew | 0:ad9dfc0179dc | 1347 | GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 0:ad9dfc0179dc | 1348 | HAL_GPIO_Init(ACCELERO_CS_GPIO_PORT, &GPIO_InitStructure); |
EricLew | 0:ad9dfc0179dc | 1349 | |
EricLew | 0:ad9dfc0179dc | 1350 | /* Deselect : Chip Select high */ |
EricLew | 0:ad9dfc0179dc | 1351 | ACCELERO_CS_HIGH(); |
EricLew | 0:ad9dfc0179dc | 1352 | |
EricLew | 0:ad9dfc0179dc | 1353 | /* Enable CS GPIO clock and Configure GPIO PIN for Gyroscope Chip select */ |
EricLew | 0:ad9dfc0179dc | 1354 | MAGNETO_CS_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1355 | GPIO_InitStructure.Pin = MAGNETO_CS_PIN; |
EricLew | 0:ad9dfc0179dc | 1356 | GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
EricLew | 0:ad9dfc0179dc | 1357 | GPIO_InitStructure.Pull = GPIO_NOPULL; |
EricLew | 0:ad9dfc0179dc | 1358 | GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 0:ad9dfc0179dc | 1359 | HAL_GPIO_Init(MAGNETO_CS_GPIO_PORT, &GPIO_InitStructure); |
EricLew | 0:ad9dfc0179dc | 1360 | |
EricLew | 0:ad9dfc0179dc | 1361 | /* Deselect : Chip Select high */ |
EricLew | 0:ad9dfc0179dc | 1362 | MAGNETO_CS_HIGH(); |
EricLew | 0:ad9dfc0179dc | 1363 | |
EricLew | 0:ad9dfc0179dc | 1364 | |
EricLew | 0:ad9dfc0179dc | 1365 | /* Configure the Gyroscope Control pins ---------------------------------*/ |
EricLew | 0:ad9dfc0179dc | 1366 | /* Enable CS GPIO clock and Configure GPIO PIN for Gyroscope Chip select */ |
EricLew | 0:ad9dfc0179dc | 1367 | GYRO_CS_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1368 | GPIO_InitStructure.Pin = GYRO_CS_PIN; |
EricLew | 0:ad9dfc0179dc | 1369 | GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
EricLew | 0:ad9dfc0179dc | 1370 | GPIO_InitStructure.Pull = GPIO_NOPULL; |
EricLew | 0:ad9dfc0179dc | 1371 | GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 0:ad9dfc0179dc | 1372 | HAL_GPIO_Init(GYRO_CS_GPIO_PORT, &GPIO_InitStructure); |
EricLew | 0:ad9dfc0179dc | 1373 | |
EricLew | 0:ad9dfc0179dc | 1374 | /* Deselect : Chip Select high */ |
EricLew | 0:ad9dfc0179dc | 1375 | GYRO_CS_HIGH(); |
EricLew | 0:ad9dfc0179dc | 1376 | |
EricLew | 0:ad9dfc0179dc | 1377 | /* Enable INT1, INT2 GPIO clock and Configure GPIO PINs to detect Interrupts */ |
EricLew | 0:ad9dfc0179dc | 1378 | GYRO_INT1_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1379 | GPIO_InitStructure.Pin = GYRO_INT1_PIN; |
EricLew | 0:ad9dfc0179dc | 1380 | GPIO_InitStructure.Mode = GPIO_MODE_INPUT; |
EricLew | 0:ad9dfc0179dc | 1381 | GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 0:ad9dfc0179dc | 1382 | GPIO_InitStructure.Pull= GPIO_NOPULL; |
EricLew | 0:ad9dfc0179dc | 1383 | HAL_GPIO_Init(GYRO_INT1_GPIO_PORT, &GPIO_InitStructure); |
EricLew | 0:ad9dfc0179dc | 1384 | |
EricLew | 0:ad9dfc0179dc | 1385 | GYRO_INT2_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1386 | GPIO_InitStructure.Pin = GYRO_INT2_PIN; |
EricLew | 0:ad9dfc0179dc | 1387 | HAL_GPIO_Init(GYRO_INT2_GPIO_PORT, &GPIO_InitStructure); |
EricLew | 0:ad9dfc0179dc | 1388 | |
EricLew | 0:ad9dfc0179dc | 1389 | SPIx_Init(); |
EricLew | 0:ad9dfc0179dc | 1390 | |
EricLew | 0:ad9dfc0179dc | 1391 | } |
EricLew | 0:ad9dfc0179dc | 1392 | |
EricLew | 0:ad9dfc0179dc | 1393 | |
EricLew | 0:ad9dfc0179dc | 1394 | /** |
EricLew | 0:ad9dfc0179dc | 1395 | * @brief de-Configures GYRO SPI interface. |
EricLew | 0:ad9dfc0179dc | 1396 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1397 | */ |
EricLew | 0:ad9dfc0179dc | 1398 | void GYRO_IO_DeInit(void) |
EricLew | 0:ad9dfc0179dc | 1399 | { |
EricLew | 0:ad9dfc0179dc | 1400 | GPIO_InitTypeDef GPIO_InitStructure; |
EricLew | 0:ad9dfc0179dc | 1401 | /* Enable CS GPIO clock */ |
EricLew | 0:ad9dfc0179dc | 1402 | GYRO_CS_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1403 | |
EricLew | 0:ad9dfc0179dc | 1404 | GPIO_InitStructure.Pin = GYRO_CS_PIN; |
EricLew | 0:ad9dfc0179dc | 1405 | GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
EricLew | 0:ad9dfc0179dc | 1406 | GPIO_InitStructure.Pull = GPIO_NOPULL; |
EricLew | 0:ad9dfc0179dc | 1407 | GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 0:ad9dfc0179dc | 1408 | HAL_GPIO_Init(GYRO_CS_GPIO_PORT, &GPIO_InitStructure); |
EricLew | 0:ad9dfc0179dc | 1409 | |
EricLew | 0:ad9dfc0179dc | 1410 | /* Deselect : Chip Select high */ |
EricLew | 0:ad9dfc0179dc | 1411 | GYRO_CS_HIGH(); |
EricLew | 0:ad9dfc0179dc | 1412 | |
EricLew | 0:ad9dfc0179dc | 1413 | GYRO_INT1_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1414 | GYRO_INT2_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1415 | |
EricLew | 0:ad9dfc0179dc | 1416 | /* Uninitialize the INT1/INT2 Pins */ |
EricLew | 0:ad9dfc0179dc | 1417 | HAL_GPIO_DeInit(GYRO_INT1_GPIO_PORT, GYRO_INT1_PIN); |
EricLew | 0:ad9dfc0179dc | 1418 | HAL_GPIO_DeInit(GYRO_INT2_GPIO_PORT, GYRO_INT2_PIN); |
EricLew | 0:ad9dfc0179dc | 1419 | |
EricLew | 0:ad9dfc0179dc | 1420 | /* Uninitialize SPI bus */ |
EricLew | 0:ad9dfc0179dc | 1421 | SPIx_DeInit(); |
EricLew | 0:ad9dfc0179dc | 1422 | } |
EricLew | 0:ad9dfc0179dc | 1423 | |
EricLew | 0:ad9dfc0179dc | 1424 | /** |
EricLew | 0:ad9dfc0179dc | 1425 | * @brief Writes one byte to the GYRO. |
EricLew | 0:ad9dfc0179dc | 1426 | * @param pBuffer : pointer to the buffer containing the data to be written to the GYRO. |
EricLew | 0:ad9dfc0179dc | 1427 | * @param WriteAddr : GYRO's internal address to write to. |
EricLew | 0:ad9dfc0179dc | 1428 | * @param NumByteToWrite: Number of bytes to write. |
EricLew | 0:ad9dfc0179dc | 1429 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1430 | */ |
EricLew | 0:ad9dfc0179dc | 1431 | void GYRO_IO_Write(uint8_t* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite) |
EricLew | 0:ad9dfc0179dc | 1432 | { |
EricLew | 0:ad9dfc0179dc | 1433 | /* Configure the MS bit: |
EricLew | 0:ad9dfc0179dc | 1434 | - When 0, the address will remain unchanged in multiple read/write commands. |
EricLew | 0:ad9dfc0179dc | 1435 | - When 1, the address will be auto incremented in multiple read/write commands. |
EricLew | 0:ad9dfc0179dc | 1436 | */ |
EricLew | 0:ad9dfc0179dc | 1437 | if(NumByteToWrite > 0x01) |
EricLew | 0:ad9dfc0179dc | 1438 | { |
EricLew | 0:ad9dfc0179dc | 1439 | WriteAddr |= (uint8_t)MULTIPLEBYTE_CMD; |
EricLew | 0:ad9dfc0179dc | 1440 | } |
EricLew | 0:ad9dfc0179dc | 1441 | /* Set chip select Low at the start of the transmission */ |
EricLew | 0:ad9dfc0179dc | 1442 | GYRO_CS_LOW(); |
EricLew | 0:ad9dfc0179dc | 1443 | __SPI_DIRECTION_2LINES(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 1444 | |
EricLew | 0:ad9dfc0179dc | 1445 | /* Send the Address of the indexed register */ |
EricLew | 0:ad9dfc0179dc | 1446 | SPIx_WriteRead(WriteAddr); |
EricLew | 0:ad9dfc0179dc | 1447 | |
EricLew | 0:ad9dfc0179dc | 1448 | /* Send the data that will be written into the device (MSB First) */ |
EricLew | 0:ad9dfc0179dc | 1449 | while(NumByteToWrite >= 0x01) |
EricLew | 0:ad9dfc0179dc | 1450 | { |
EricLew | 0:ad9dfc0179dc | 1451 | SPIx_WriteRead(*pBuffer); |
EricLew | 0:ad9dfc0179dc | 1452 | NumByteToWrite--; |
EricLew | 0:ad9dfc0179dc | 1453 | pBuffer++; |
EricLew | 0:ad9dfc0179dc | 1454 | } |
EricLew | 0:ad9dfc0179dc | 1455 | |
EricLew | 0:ad9dfc0179dc | 1456 | /* Set chip select High at the end of the transmission */ |
EricLew | 0:ad9dfc0179dc | 1457 | GYRO_CS_HIGH(); |
EricLew | 0:ad9dfc0179dc | 1458 | } |
EricLew | 0:ad9dfc0179dc | 1459 | |
EricLew | 0:ad9dfc0179dc | 1460 | /** |
EricLew | 0:ad9dfc0179dc | 1461 | * @brief Reads a block of data from the GYROSCOPE. |
EricLew | 0:ad9dfc0179dc | 1462 | * @param pBuffer : pointer to the buffer that receives the data read from the GYROSCOPE. |
EricLew | 0:ad9dfc0179dc | 1463 | * @param ReadAddr : GYROSCOPE's internal address to read from. |
EricLew | 0:ad9dfc0179dc | 1464 | * @param NumByteToRead : number of bytes to read from the GYROSCOPE. |
EricLew | 0:ad9dfc0179dc | 1465 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1466 | */ |
EricLew | 0:ad9dfc0179dc | 1467 | void GYRO_IO_Read(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead) |
EricLew | 0:ad9dfc0179dc | 1468 | { |
EricLew | 0:ad9dfc0179dc | 1469 | if(NumByteToRead > 0x01) |
EricLew | 0:ad9dfc0179dc | 1470 | { |
EricLew | 0:ad9dfc0179dc | 1471 | ReadAddr |= (uint8_t)(READWRITE_CMD | MULTIPLEBYTE_CMD); |
EricLew | 0:ad9dfc0179dc | 1472 | } |
EricLew | 0:ad9dfc0179dc | 1473 | else |
EricLew | 0:ad9dfc0179dc | 1474 | { |
EricLew | 0:ad9dfc0179dc | 1475 | ReadAddr |= (uint8_t)READWRITE_CMD; |
EricLew | 0:ad9dfc0179dc | 1476 | } |
EricLew | 0:ad9dfc0179dc | 1477 | /* Set chip select Low at the start of the transmission */ |
EricLew | 0:ad9dfc0179dc | 1478 | GYRO_CS_LOW(); |
EricLew | 0:ad9dfc0179dc | 1479 | __SPI_DIRECTION_2LINES(&SpiHandle); |
EricLew | 0:ad9dfc0179dc | 1480 | /* Send the Address of the indexed register */ |
EricLew | 0:ad9dfc0179dc | 1481 | SPIx_WriteRead(ReadAddr); |
EricLew | 0:ad9dfc0179dc | 1482 | |
EricLew | 0:ad9dfc0179dc | 1483 | /* Receive the data that will be read from the device (MSB First) */ |
EricLew | 0:ad9dfc0179dc | 1484 | while(NumByteToRead > 0x00) |
EricLew | 0:ad9dfc0179dc | 1485 | { |
EricLew | 0:ad9dfc0179dc | 1486 | /* Send dummy byte (0x00) to generate the SPI clock to GYROSCOPE (Slave device) */ |
EricLew | 0:ad9dfc0179dc | 1487 | *pBuffer = SPIx_WriteRead(0x00); |
EricLew | 0:ad9dfc0179dc | 1488 | NumByteToRead--; |
EricLew | 0:ad9dfc0179dc | 1489 | pBuffer++; |
EricLew | 0:ad9dfc0179dc | 1490 | } |
EricLew | 0:ad9dfc0179dc | 1491 | |
EricLew | 0:ad9dfc0179dc | 1492 | /* Set chip select High at the end of the transmission */ |
EricLew | 0:ad9dfc0179dc | 1493 | GYRO_CS_HIGH(); |
EricLew | 0:ad9dfc0179dc | 1494 | } |
EricLew | 0:ad9dfc0179dc | 1495 | #endif /* HAL_SPI_MODULE_ENABLED */ |
EricLew | 0:ad9dfc0179dc | 1496 | |
EricLew | 0:ad9dfc0179dc | 1497 | #if defined(HAL_I2C_MODULE_ENABLED) |
EricLew | 0:ad9dfc0179dc | 1498 | /********************************* LINK MFX ***********************************/ |
EricLew | 0:ad9dfc0179dc | 1499 | /** |
EricLew | 0:ad9dfc0179dc | 1500 | * @brief Initializes MFX low level. |
EricLew | 0:ad9dfc0179dc | 1501 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1502 | */ |
EricLew | 0:ad9dfc0179dc | 1503 | void MFX_IO_Init(void) |
EricLew | 0:ad9dfc0179dc | 1504 | { |
EricLew | 0:ad9dfc0179dc | 1505 | /* I2C2 init */ |
EricLew | 0:ad9dfc0179dc | 1506 | I2C2_Init(); |
EricLew | 0:ad9dfc0179dc | 1507 | } |
EricLew | 0:ad9dfc0179dc | 1508 | /** |
EricLew | 0:ad9dfc0179dc | 1509 | * @brief Deinitializes MFX low level. |
EricLew | 0:ad9dfc0179dc | 1510 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1511 | */ |
EricLew | 0:ad9dfc0179dc | 1512 | void MFX_IO_DeInit(void) |
EricLew | 0:ad9dfc0179dc | 1513 | { |
EricLew | 0:ad9dfc0179dc | 1514 | GPIO_InitTypeDef GPIO_InitStruct; |
EricLew | 0:ad9dfc0179dc | 1515 | |
EricLew | 0:ad9dfc0179dc | 1516 | /* Enable wakeup gpio clock */ |
EricLew | 0:ad9dfc0179dc | 1517 | IDD_WAKEUP_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1518 | |
EricLew | 0:ad9dfc0179dc | 1519 | /* MFX wakeup pin configuration */ |
EricLew | 0:ad9dfc0179dc | 1520 | GPIO_InitStruct.Pin = IDD_WAKEUP_PIN; |
EricLew | 0:ad9dfc0179dc | 1521 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
EricLew | 0:ad9dfc0179dc | 1522 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
EricLew | 0:ad9dfc0179dc | 1523 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
EricLew | 0:ad9dfc0179dc | 1524 | HAL_GPIO_Init(IDD_WAKEUP_GPIO_PORT, &GPIO_InitStruct); |
EricLew | 0:ad9dfc0179dc | 1525 | |
EricLew | 0:ad9dfc0179dc | 1526 | /* DeInit interrupt pin : disable IRQ before to avoid spurious interrupt */ |
EricLew | 0:ad9dfc0179dc | 1527 | HAL_NVIC_DisableIRQ((IRQn_Type)(IDD_INT_EXTI_IRQn)); |
EricLew | 0:ad9dfc0179dc | 1528 | IDD_INT_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1529 | HAL_GPIO_DeInit(IDD_INT_GPIO_PORT, IDD_INT_PIN); |
EricLew | 0:ad9dfc0179dc | 1530 | |
EricLew | 0:ad9dfc0179dc | 1531 | /* I2C2 Deinit */ |
EricLew | 0:ad9dfc0179dc | 1532 | I2C2_DeInit(); |
EricLew | 0:ad9dfc0179dc | 1533 | } |
EricLew | 0:ad9dfc0179dc | 1534 | |
EricLew | 0:ad9dfc0179dc | 1535 | /** |
EricLew | 0:ad9dfc0179dc | 1536 | * @brief Configures MFX low level interrupt. |
EricLew | 0:ad9dfc0179dc | 1537 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1538 | */ |
EricLew | 0:ad9dfc0179dc | 1539 | void MFX_IO_ITConfig(void) |
EricLew | 0:ad9dfc0179dc | 1540 | { |
EricLew | 0:ad9dfc0179dc | 1541 | GPIO_InitTypeDef GPIO_InitStruct; |
EricLew | 0:ad9dfc0179dc | 1542 | |
EricLew | 0:ad9dfc0179dc | 1543 | /* Enable the GPIO clock */ |
EricLew | 0:ad9dfc0179dc | 1544 | IDD_INT_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1545 | |
EricLew | 0:ad9dfc0179dc | 1546 | /* MFX_OUT_IRQ (normally used for EXTI_WKUP) */ |
EricLew | 0:ad9dfc0179dc | 1547 | GPIO_InitStruct.Pin = IDD_INT_PIN; |
EricLew | 0:ad9dfc0179dc | 1548 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
EricLew | 0:ad9dfc0179dc | 1549 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 0:ad9dfc0179dc | 1550 | GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; |
EricLew | 0:ad9dfc0179dc | 1551 | HAL_GPIO_Init(IDD_INT_GPIO_PORT, &GPIO_InitStruct); |
EricLew | 0:ad9dfc0179dc | 1552 | |
EricLew | 0:ad9dfc0179dc | 1553 | /* Enable and set GPIO EXTI Interrupt to the lowest priority */ |
EricLew | 0:ad9dfc0179dc | 1554 | HAL_NVIC_SetPriority((IRQn_Type)(IDD_INT_EXTI_IRQn), 0x0F, 0x0F); |
EricLew | 0:ad9dfc0179dc | 1555 | HAL_NVIC_EnableIRQ((IRQn_Type)(IDD_INT_EXTI_IRQn)); |
EricLew | 0:ad9dfc0179dc | 1556 | } |
EricLew | 0:ad9dfc0179dc | 1557 | |
EricLew | 0:ad9dfc0179dc | 1558 | /** |
EricLew | 0:ad9dfc0179dc | 1559 | * @brief Configures MFX wke up pin. |
EricLew | 0:ad9dfc0179dc | 1560 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1561 | */ |
EricLew | 0:ad9dfc0179dc | 1562 | void MFX_IO_EnableWakeupPin(void) |
EricLew | 0:ad9dfc0179dc | 1563 | { |
EricLew | 0:ad9dfc0179dc | 1564 | GPIO_InitTypeDef GPIO_InitStruct; |
EricLew | 0:ad9dfc0179dc | 1565 | |
EricLew | 0:ad9dfc0179dc | 1566 | /* Enable wakeup gpio clock */ |
EricLew | 0:ad9dfc0179dc | 1567 | IDD_WAKEUP_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1568 | |
EricLew | 0:ad9dfc0179dc | 1569 | /* MFX wakeup pin configuration */ |
EricLew | 0:ad9dfc0179dc | 1570 | GPIO_InitStruct.Pin = IDD_WAKEUP_PIN; |
EricLew | 0:ad9dfc0179dc | 1571 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
EricLew | 0:ad9dfc0179dc | 1572 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
EricLew | 0:ad9dfc0179dc | 1573 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
EricLew | 0:ad9dfc0179dc | 1574 | HAL_GPIO_Init(IDD_WAKEUP_GPIO_PORT, &GPIO_InitStruct); |
EricLew | 0:ad9dfc0179dc | 1575 | } |
EricLew | 0:ad9dfc0179dc | 1576 | |
EricLew | 0:ad9dfc0179dc | 1577 | /** |
EricLew | 0:ad9dfc0179dc | 1578 | * @brief Wakeup MFX. |
EricLew | 0:ad9dfc0179dc | 1579 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1580 | */ |
EricLew | 0:ad9dfc0179dc | 1581 | void MFX_IO_Wakeup(void) |
EricLew | 0:ad9dfc0179dc | 1582 | { |
EricLew | 0:ad9dfc0179dc | 1583 | /* Set Wakeup pin to high to wakeup Idd measurement component from standby mode */ |
EricLew | 0:ad9dfc0179dc | 1584 | HAL_GPIO_WritePin(IDD_WAKEUP_GPIO_PORT, IDD_WAKEUP_PIN, GPIO_PIN_SET); |
EricLew | 0:ad9dfc0179dc | 1585 | |
EricLew | 0:ad9dfc0179dc | 1586 | /* Wait */ |
EricLew | 0:ad9dfc0179dc | 1587 | HAL_Delay(1); |
EricLew | 0:ad9dfc0179dc | 1588 | |
EricLew | 0:ad9dfc0179dc | 1589 | /* Set gpio pin basck to low */ |
EricLew | 0:ad9dfc0179dc | 1590 | HAL_GPIO_WritePin(IDD_WAKEUP_GPIO_PORT, IDD_WAKEUP_PIN, GPIO_PIN_RESET); |
EricLew | 0:ad9dfc0179dc | 1591 | } |
EricLew | 0:ad9dfc0179dc | 1592 | |
EricLew | 0:ad9dfc0179dc | 1593 | /** |
EricLew | 0:ad9dfc0179dc | 1594 | * @brief MFX writes single data. |
EricLew | 0:ad9dfc0179dc | 1595 | * @param Addr: I2C address |
EricLew | 0:ad9dfc0179dc | 1596 | * @param Reg: Register address |
EricLew | 0:ad9dfc0179dc | 1597 | * @param Value: Data to be written |
EricLew | 0:ad9dfc0179dc | 1598 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1599 | */ |
EricLew | 0:ad9dfc0179dc | 1600 | void MFX_IO_Write(uint16_t Addr, uint8_t Reg, uint8_t Value) |
EricLew | 0:ad9dfc0179dc | 1601 | { |
EricLew | 0:ad9dfc0179dc | 1602 | I2C2_WriteData(Addr, Reg, I2C_MEMADD_SIZE_8BIT, Value); |
EricLew | 0:ad9dfc0179dc | 1603 | } |
EricLew | 0:ad9dfc0179dc | 1604 | |
EricLew | 0:ad9dfc0179dc | 1605 | /** |
EricLew | 0:ad9dfc0179dc | 1606 | * @brief MFX reads single data. |
EricLew | 0:ad9dfc0179dc | 1607 | * @param Addr: I2C address |
EricLew | 0:ad9dfc0179dc | 1608 | * @param Reg: Register address |
EricLew | 0:ad9dfc0179dc | 1609 | * @retval Read data |
EricLew | 0:ad9dfc0179dc | 1610 | */ |
EricLew | 0:ad9dfc0179dc | 1611 | uint8_t MFX_IO_Read(uint16_t Addr, uint8_t Reg) |
EricLew | 0:ad9dfc0179dc | 1612 | { |
EricLew | 0:ad9dfc0179dc | 1613 | return I2C2_ReadData(Addr, Reg, I2C_MEMADD_SIZE_8BIT); |
EricLew | 0:ad9dfc0179dc | 1614 | } |
EricLew | 0:ad9dfc0179dc | 1615 | |
EricLew | 0:ad9dfc0179dc | 1616 | /** |
EricLew | 0:ad9dfc0179dc | 1617 | * @brief MFX reads multiple data. |
EricLew | 0:ad9dfc0179dc | 1618 | * @param Addr: I2C address |
EricLew | 0:ad9dfc0179dc | 1619 | * @param Reg: Register address |
EricLew | 0:ad9dfc0179dc | 1620 | * @param Buffer: Pointer to data buffer |
EricLew | 0:ad9dfc0179dc | 1621 | * @param Length: Length of the data |
EricLew | 0:ad9dfc0179dc | 1622 | * @retval Number of read data |
EricLew | 0:ad9dfc0179dc | 1623 | */ |
EricLew | 0:ad9dfc0179dc | 1624 | uint16_t MFX_IO_ReadMultiple(uint16_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length) |
EricLew | 0:ad9dfc0179dc | 1625 | { |
EricLew | 0:ad9dfc0179dc | 1626 | return I2C2_ReadBuffer(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length); |
EricLew | 0:ad9dfc0179dc | 1627 | } |
EricLew | 0:ad9dfc0179dc | 1628 | |
EricLew | 0:ad9dfc0179dc | 1629 | /** |
EricLew | 0:ad9dfc0179dc | 1630 | * @brief MFX writes multiple data. |
EricLew | 0:ad9dfc0179dc | 1631 | * @param Addr: I2C address |
EricLew | 0:ad9dfc0179dc | 1632 | * @param Reg: Register address |
EricLew | 0:ad9dfc0179dc | 1633 | * @param Buffer: Pointer to data buffer |
EricLew | 0:ad9dfc0179dc | 1634 | * @param Length: Length of the data |
EricLew | 0:ad9dfc0179dc | 1635 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1636 | */ |
EricLew | 0:ad9dfc0179dc | 1637 | void MFX_IO_WriteMultiple(uint16_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length) |
EricLew | 0:ad9dfc0179dc | 1638 | { |
EricLew | 0:ad9dfc0179dc | 1639 | I2C2_WriteBuffer(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length); |
EricLew | 0:ad9dfc0179dc | 1640 | } |
EricLew | 0:ad9dfc0179dc | 1641 | |
EricLew | 0:ad9dfc0179dc | 1642 | /** |
EricLew | 0:ad9dfc0179dc | 1643 | * @brief MFX delay |
EricLew | 0:ad9dfc0179dc | 1644 | * @param Delay: Delay in ms |
EricLew | 0:ad9dfc0179dc | 1645 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1646 | */ |
EricLew | 0:ad9dfc0179dc | 1647 | void MFX_IO_Delay(uint32_t Delay) |
EricLew | 0:ad9dfc0179dc | 1648 | { |
EricLew | 0:ad9dfc0179dc | 1649 | HAL_Delay(Delay); |
EricLew | 0:ad9dfc0179dc | 1650 | } |
EricLew | 0:ad9dfc0179dc | 1651 | |
EricLew | 0:ad9dfc0179dc | 1652 | |
EricLew | 0:ad9dfc0179dc | 1653 | /********************************* LINK AUDIO *********************************/ |
EricLew | 0:ad9dfc0179dc | 1654 | /** |
EricLew | 0:ad9dfc0179dc | 1655 | * @brief Initializes Audio low level. |
EricLew | 0:ad9dfc0179dc | 1656 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1657 | */ |
EricLew | 0:ad9dfc0179dc | 1658 | void AUDIO_IO_Init(void) |
EricLew | 0:ad9dfc0179dc | 1659 | { |
EricLew | 0:ad9dfc0179dc | 1660 | GPIO_InitTypeDef GPIO_InitStruct; |
EricLew | 0:ad9dfc0179dc | 1661 | |
EricLew | 0:ad9dfc0179dc | 1662 | /* Enable Reset GPIO Clock */ |
EricLew | 0:ad9dfc0179dc | 1663 | AUDIO_RESET_GPIO_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1664 | |
EricLew | 0:ad9dfc0179dc | 1665 | /* Audio reset pin configuration */ |
EricLew | 0:ad9dfc0179dc | 1666 | GPIO_InitStruct.Pin = AUDIO_RESET_PIN; |
EricLew | 0:ad9dfc0179dc | 1667 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
EricLew | 0:ad9dfc0179dc | 1668 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
EricLew | 0:ad9dfc0179dc | 1669 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
EricLew | 0:ad9dfc0179dc | 1670 | HAL_GPIO_Init(AUDIO_RESET_GPIO, &GPIO_InitStruct); |
EricLew | 0:ad9dfc0179dc | 1671 | |
EricLew | 0:ad9dfc0179dc | 1672 | /* I2C bus init */ |
EricLew | 0:ad9dfc0179dc | 1673 | I2C1_Init(); |
EricLew | 0:ad9dfc0179dc | 1674 | |
EricLew | 0:ad9dfc0179dc | 1675 | /* Power Down the codec */ |
EricLew | 0:ad9dfc0179dc | 1676 | CODEC_AUDIO_POWER_OFF(); |
EricLew | 0:ad9dfc0179dc | 1677 | |
EricLew | 0:ad9dfc0179dc | 1678 | /* wait for a delay to insure registers erasing */ |
EricLew | 0:ad9dfc0179dc | 1679 | HAL_Delay(5); |
EricLew | 0:ad9dfc0179dc | 1680 | |
EricLew | 0:ad9dfc0179dc | 1681 | /* Power on the codec */ |
EricLew | 0:ad9dfc0179dc | 1682 | CODEC_AUDIO_POWER_ON(); |
EricLew | 0:ad9dfc0179dc | 1683 | |
EricLew | 0:ad9dfc0179dc | 1684 | /* wait for a delay to insure registers erasing */ |
EricLew | 0:ad9dfc0179dc | 1685 | HAL_Delay(5); |
EricLew | 0:ad9dfc0179dc | 1686 | } |
EricLew | 0:ad9dfc0179dc | 1687 | |
EricLew | 0:ad9dfc0179dc | 1688 | /** |
EricLew | 0:ad9dfc0179dc | 1689 | * @brief Deinitializes Audio low level. |
EricLew | 0:ad9dfc0179dc | 1690 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1691 | */ |
EricLew | 0:ad9dfc0179dc | 1692 | void AUDIO_IO_DeInit(void) /* TO DO */ |
EricLew | 0:ad9dfc0179dc | 1693 | { |
EricLew | 0:ad9dfc0179dc | 1694 | GPIO_InitTypeDef GPIO_InitStruct; |
EricLew | 0:ad9dfc0179dc | 1695 | |
EricLew | 0:ad9dfc0179dc | 1696 | /***********************************************************************/ |
EricLew | 0:ad9dfc0179dc | 1697 | /* In case of battery-supplied powered, there is no audio codec-based |
EricLew | 0:ad9dfc0179dc | 1698 | features available. Set audio codec I/O default setting */ |
EricLew | 0:ad9dfc0179dc | 1699 | /***********************************************************************/ |
EricLew | 0:ad9dfc0179dc | 1700 | __HAL_RCC_GPIOE_CLK_ENABLE(); |
EricLew | 0:ad9dfc0179dc | 1701 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP ; |
EricLew | 0:ad9dfc0179dc | 1702 | GPIO_InitStruct.Pin = (GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6); |
EricLew | 0:ad9dfc0179dc | 1703 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
EricLew | 0:ad9dfc0179dc | 1704 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
EricLew | 0:ad9dfc0179dc | 1705 | HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); |
EricLew | 0:ad9dfc0179dc | 1706 | HAL_GPIO_WritePin(GPIOE, GPIO_PIN_2, GPIO_PIN_RESET); |
EricLew | 0:ad9dfc0179dc | 1707 | HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET); |
EricLew | 0:ad9dfc0179dc | 1708 | HAL_GPIO_WritePin(GPIOE, GPIO_PIN_4, GPIO_PIN_RESET); |
EricLew | 0:ad9dfc0179dc | 1709 | HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_RESET); |
EricLew | 0:ad9dfc0179dc | 1710 | HAL_GPIO_WritePin(GPIOE, GPIO_PIN_6, GPIO_PIN_RESET); |
EricLew | 0:ad9dfc0179dc | 1711 | |
EricLew | 0:ad9dfc0179dc | 1712 | /* I2C bus Deinit */ |
EricLew | 0:ad9dfc0179dc | 1713 | I2C1_DeInit(); |
EricLew | 0:ad9dfc0179dc | 1714 | } |
EricLew | 0:ad9dfc0179dc | 1715 | |
EricLew | 0:ad9dfc0179dc | 1716 | /** |
EricLew | 0:ad9dfc0179dc | 1717 | * @brief Writes a single data. |
EricLew | 0:ad9dfc0179dc | 1718 | * @param Addr: I2C address |
EricLew | 0:ad9dfc0179dc | 1719 | * @param Reg: Reg address |
EricLew | 0:ad9dfc0179dc | 1720 | * @param Value: Data to be written |
EricLew | 0:ad9dfc0179dc | 1721 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1722 | */ |
EricLew | 0:ad9dfc0179dc | 1723 | void AUDIO_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value) |
EricLew | 0:ad9dfc0179dc | 1724 | { |
EricLew | 0:ad9dfc0179dc | 1725 | I2C1_WriteBuffer(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1); |
EricLew | 0:ad9dfc0179dc | 1726 | } |
EricLew | 0:ad9dfc0179dc | 1727 | |
EricLew | 0:ad9dfc0179dc | 1728 | /** |
EricLew | 0:ad9dfc0179dc | 1729 | * @brief Reads a single data. |
EricLew | 0:ad9dfc0179dc | 1730 | * @param Addr: I2C address |
EricLew | 0:ad9dfc0179dc | 1731 | * @param Reg: Reg address |
EricLew | 0:ad9dfc0179dc | 1732 | * @retval Data to be read |
EricLew | 0:ad9dfc0179dc | 1733 | */ |
EricLew | 0:ad9dfc0179dc | 1734 | uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg) |
EricLew | 0:ad9dfc0179dc | 1735 | { |
EricLew | 0:ad9dfc0179dc | 1736 | uint8_t Read_Value = 0; |
EricLew | 0:ad9dfc0179dc | 1737 | |
EricLew | 0:ad9dfc0179dc | 1738 | I2C1_ReadBuffer((uint16_t) Addr, (uint16_t) Reg, I2C_MEMADD_SIZE_8BIT, &Read_Value, 1); |
EricLew | 0:ad9dfc0179dc | 1739 | |
EricLew | 0:ad9dfc0179dc | 1740 | return Read_Value; |
EricLew | 0:ad9dfc0179dc | 1741 | } |
EricLew | 0:ad9dfc0179dc | 1742 | |
EricLew | 0:ad9dfc0179dc | 1743 | /** |
EricLew | 0:ad9dfc0179dc | 1744 | * @brief AUDIO Codec delay |
EricLew | 0:ad9dfc0179dc | 1745 | * @param Delay: Delay in ms |
EricLew | 0:ad9dfc0179dc | 1746 | * @retval None |
EricLew | 0:ad9dfc0179dc | 1747 | */ |
EricLew | 0:ad9dfc0179dc | 1748 | void AUDIO_IO_Delay(uint32_t Delay) |
EricLew | 0:ad9dfc0179dc | 1749 | { |
EricLew | 0:ad9dfc0179dc | 1750 | HAL_Delay(Delay); |
EricLew | 0:ad9dfc0179dc | 1751 | } |
EricLew | 0:ad9dfc0179dc | 1752 | #endif /* HAL_I2C_MODULE_ENABLED */ |
EricLew | 0:ad9dfc0179dc | 1753 | |
EricLew | 0:ad9dfc0179dc | 1754 | /** |
EricLew | 0:ad9dfc0179dc | 1755 | * @} |
EricLew | 0:ad9dfc0179dc | 1756 | */ |
EricLew | 0:ad9dfc0179dc | 1757 | |
EricLew | 0:ad9dfc0179dc | 1758 | /** |
EricLew | 0:ad9dfc0179dc | 1759 | * @} |
EricLew | 0:ad9dfc0179dc | 1760 | */ |
EricLew | 0:ad9dfc0179dc | 1761 | |
EricLew | 0:ad9dfc0179dc | 1762 | /** |
EricLew | 0:ad9dfc0179dc | 1763 | * @} |
EricLew | 0:ad9dfc0179dc | 1764 | */ |
EricLew | 0:ad9dfc0179dc | 1765 | |
EricLew | 0:ad9dfc0179dc | 1766 | /** |
EricLew | 0:ad9dfc0179dc | 1767 | * @} |
EricLew | 0:ad9dfc0179dc | 1768 | */ |
EricLew | 0:ad9dfc0179dc | 1769 | |
EricLew | 0:ad9dfc0179dc | 1770 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
EricLew | 0:ad9dfc0179dc | 1771 |