AudioRecord

Dependencies:   STM32L4xx_HAL_Driver CMSIS_DSP_401

Committer:
EricLew
Date:
Mon Nov 23 19:37:13 2015 +0000
Revision:
0:d4e5ad7ad71c
commit;

Who changed what in which revision?

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