ST / BSP_DISCO_L476VG

Dependents:   DiscoLogger DISCO_L476VG_GlassLCD DISCO_L476VG_MicrophoneRecorder DISCO_L476VG_UART ... more

Committer:
bcostm
Date:
Wed Jan 17 14:57:52 2018 +0100
Revision:
3:4c7d003a8259
Parent:
1:917af0ca86df
Child:
5:4943b15cce9f
Update BSP files with CubeL4 V1.11.0

Who changed what in which revision?

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