STM32L476G-Discovery board drivers V1.0.0

Dependents:   DiscoLogger DISCO_L476VG_GlassLCD DISCO_L476VG_MicrophoneRecorder DISCO_L476VG_UART ... more

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