TUKS MCU Introductory course / TUKS-COURSE-2-LED
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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