Additional font macro

Dependents:   EspSerial

Committer:
SomeRandomBloke
Date:
Fri Feb 05 09:20:30 2016 +0000
Revision:
1:a6fd0931a9e9
Parent:
0:e1d164542663
Added additional font macro

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 0:e1d164542663 1 /**
bcostm 0:e1d164542663 2 ******************************************************************************
bcostm 0:e1d164542663 3 * @file stm32f429i_discovery.c
bcostm 0:e1d164542663 4 * @author MCD Application Team
bcostm 0:e1d164542663 5 * @version V2.1.2
bcostm 0:e1d164542663 6 * @date 02-March-2015
bcostm 0:e1d164542663 7 * @brief This file provides set of firmware functions to manage Leds and
bcostm 0:e1d164542663 8 * push-button available on STM32F429I-Discovery Kit from STMicroelectronics.
bcostm 0:e1d164542663 9 ******************************************************************************
bcostm 0:e1d164542663 10 * @attention
bcostm 0:e1d164542663 11 *
bcostm 0:e1d164542663 12 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
bcostm 0:e1d164542663 13 *
bcostm 0:e1d164542663 14 * Redistribution and use in source and binary forms, with or without modification,
bcostm 0:e1d164542663 15 * are permitted provided that the following conditions are met:
bcostm 0:e1d164542663 16 * 1. Redistributions of source code must retain the above copyright notice,
bcostm 0:e1d164542663 17 * this list of conditions and the following disclaimer.
bcostm 0:e1d164542663 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
bcostm 0:e1d164542663 19 * this list of conditions and the following disclaimer in the documentation
bcostm 0:e1d164542663 20 * and/or other materials provided with the distribution.
bcostm 0:e1d164542663 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
bcostm 0:e1d164542663 22 * may be used to endorse or promote products derived from this software
bcostm 0:e1d164542663 23 * without specific prior written permission.
bcostm 0:e1d164542663 24 *
bcostm 0:e1d164542663 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
bcostm 0:e1d164542663 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
bcostm 0:e1d164542663 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
bcostm 0:e1d164542663 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
bcostm 0:e1d164542663 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
bcostm 0:e1d164542663 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
bcostm 0:e1d164542663 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
bcostm 0:e1d164542663 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
bcostm 0:e1d164542663 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
bcostm 0:e1d164542663 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bcostm 0:e1d164542663 35 *
bcostm 0:e1d164542663 36 ******************************************************************************
bcostm 0:e1d164542663 37 */
bcostm 0:e1d164542663 38
bcostm 0:e1d164542663 39 /* Includes ------------------------------------------------------------------*/
bcostm 0:e1d164542663 40 #include "stm32f429i_discovery.h"
bcostm 0:e1d164542663 41
bcostm 0:e1d164542663 42 // mbed
bcostm 0:e1d164542663 43 void wait_ms(int ms);
bcostm 0:e1d164542663 44
bcostm 0:e1d164542663 45 /** @addtogroup BSP
bcostm 0:e1d164542663 46 * @{
bcostm 0:e1d164542663 47 */
bcostm 0:e1d164542663 48
bcostm 0:e1d164542663 49 /** @addtogroup STM32F429I_DISCOVERY
bcostm 0:e1d164542663 50 * @{
bcostm 0:e1d164542663 51 */
bcostm 0:e1d164542663 52
bcostm 0:e1d164542663 53 /** @defgroup STM32F429I_DISCOVERY_LOW_LEVEL
bcostm 0:e1d164542663 54 * @brief This file provides set of firmware functions to manage Leds and push-button
bcostm 0:e1d164542663 55 * available on STM32F429I-Discovery Kit from STMicroelectronics.
bcostm 0:e1d164542663 56 * @{
bcostm 0:e1d164542663 57 */
bcostm 0:e1d164542663 58
bcostm 0:e1d164542663 59 /** @defgroup STM32F429I_DISCOVERY_LOW_LEVEL_Private_TypesDefinitions
bcostm 0:e1d164542663 60 * @{
bcostm 0:e1d164542663 61 */
bcostm 0:e1d164542663 62 /**
bcostm 0:e1d164542663 63 * @}
bcostm 0:e1d164542663 64 */
bcostm 0:e1d164542663 65
bcostm 0:e1d164542663 66 /** @defgroup STM32F429I_DISCOVERY_LOW_LEVEL_Private_Defines
bcostm 0:e1d164542663 67 * @{
bcostm 0:e1d164542663 68 */
bcostm 0:e1d164542663 69
bcostm 0:e1d164542663 70 /**
bcostm 0:e1d164542663 71 * @brief STM32F429I DISCO BSP Driver version number V2.1.0
bcostm 0:e1d164542663 72 */
bcostm 0:e1d164542663 73 #define __STM32F429I_DISCO_BSP_VERSION_MAIN (0x02) /*!< [31:24] main version */
bcostm 0:e1d164542663 74 #define __STM32F429I_DISCO_BSP_VERSION_SUB1 (0x01) /*!< [23:16] sub1 version */
bcostm 0:e1d164542663 75 #define __STM32F429I_DISCO_BSP_VERSION_SUB2 (0x02) /*!< [15:8] sub2 version */
bcostm 0:e1d164542663 76 #define __STM32F429I_DISCO_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */
bcostm 0:e1d164542663 77 #define __STM32F429I_DISCO_BSP_VERSION ((__STM32F429I_DISCO_BSP_VERSION_MAIN << 24)\
bcostm 0:e1d164542663 78 |(__STM32F429I_DISCO_BSP_VERSION_SUB1 << 16)\
bcostm 0:e1d164542663 79 |(__STM32F429I_DISCO_BSP_VERSION_SUB2 << 8 )\
bcostm 0:e1d164542663 80 |(__STM32F429I_DISCO_BSP_VERSION_RC))
bcostm 0:e1d164542663 81 /**
bcostm 0:e1d164542663 82 * @}
bcostm 0:e1d164542663 83 */
bcostm 0:e1d164542663 84
bcostm 0:e1d164542663 85 /** @defgroup STM32F429I_DISCOVERY_LOW_LEVEL_Private_Macros
bcostm 0:e1d164542663 86 * @{
bcostm 0:e1d164542663 87 */
bcostm 0:e1d164542663 88 /**
bcostm 0:e1d164542663 89 * @}
bcostm 0:e1d164542663 90 */
bcostm 0:e1d164542663 91
bcostm 0:e1d164542663 92 /** @defgroup STM32F429I_DISCOVERY_LOW_LEVEL_Private_Variables
bcostm 0:e1d164542663 93 * @{
bcostm 0:e1d164542663 94 */
bcostm 0:e1d164542663 95 GPIO_TypeDef* GPIO_PORT[LEDn] = {LED3_GPIO_PORT,
bcostm 0:e1d164542663 96 LED4_GPIO_PORT};
bcostm 0:e1d164542663 97
bcostm 0:e1d164542663 98 const uint16_t GPIO_PIN[LEDn] = {LED3_PIN,
bcostm 0:e1d164542663 99 LED4_PIN};
bcostm 0:e1d164542663 100
bcostm 0:e1d164542663 101 GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {KEY_BUTTON_GPIO_PORT};
bcostm 0:e1d164542663 102 const uint16_t BUTTON_PIN[BUTTONn] = {KEY_BUTTON_PIN};
bcostm 0:e1d164542663 103 const uint8_t BUTTON_IRQn[BUTTONn] = {KEY_BUTTON_EXTI_IRQn};
bcostm 0:e1d164542663 104
bcostm 0:e1d164542663 105 uint32_t I2cxTimeout = I2Cx_TIMEOUT_MAX; /*<! Value of Timeout when I2C communication fails */
bcostm 0:e1d164542663 106 uint32_t SpixTimeout = SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */
bcostm 0:e1d164542663 107
bcostm 0:e1d164542663 108 static I2C_HandleTypeDef I2cHandle;
bcostm 0:e1d164542663 109 static SPI_HandleTypeDef SpiHandle;
bcostm 0:e1d164542663 110 static uint8_t Is_LCD_IO_Initialized = 0;
bcostm 0:e1d164542663 111
bcostm 0:e1d164542663 112 /**
bcostm 0:e1d164542663 113 * @}
bcostm 0:e1d164542663 114 */
bcostm 0:e1d164542663 115
bcostm 0:e1d164542663 116 /** @defgroup STM32F429I_DISCOVERY_LOW_LEVEL_Private_FunctionPrototypes
bcostm 0:e1d164542663 117 * @{
bcostm 0:e1d164542663 118 */
bcostm 0:e1d164542663 119 /* I2Cx bus function */
bcostm 0:e1d164542663 120 static void I2Cx_Init(void);
bcostm 0:e1d164542663 121 static void I2Cx_ITConfig(void);
bcostm 0:e1d164542663 122 static void I2Cx_WriteData(uint8_t Addr, uint8_t Reg, uint8_t Value);
bcostm 0:e1d164542663 123 static void I2Cx_WriteBuffer(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Length);
bcostm 0:e1d164542663 124 static uint8_t I2Cx_ReadData(uint8_t Addr, uint8_t Reg);
bcostm 0:e1d164542663 125 static uint8_t I2Cx_ReadBuffer(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Length);
bcostm 0:e1d164542663 126 static void I2Cx_Error(void);
bcostm 0:e1d164542663 127 static void I2Cx_MspInit(I2C_HandleTypeDef *hi2c);
bcostm 0:e1d164542663 128 #ifdef EE_M24LR64
bcostm 0:e1d164542663 129 static HAL_StatusTypeDef I2Cx_WriteBufferDMA(uint8_t Addr, uint16_t Reg, uint8_t *pBuffer, uint16_t Length);
bcostm 0:e1d164542663 130 static HAL_StatusTypeDef I2Cx_ReadBufferDMA(uint8_t Addr, uint16_t Reg, uint8_t *pBuffer, uint16_t Length);
bcostm 0:e1d164542663 131 static HAL_StatusTypeDef I2Cx_IsDeviceReady(uint16_t DevAddress, uint32_t Trials);
bcostm 0:e1d164542663 132 #endif /* EE_M24LR64 */
bcostm 0:e1d164542663 133
bcostm 0:e1d164542663 134 /* SPIx bus function */
bcostm 0:e1d164542663 135 static void SPIx_Init(void);
bcostm 0:e1d164542663 136 static void SPIx_Write(uint16_t Value);
bcostm 0:e1d164542663 137 static uint32_t SPIx_Read(uint8_t ReadSize);
bcostm 0:e1d164542663 138 static uint8_t SPIx_WriteRead(uint8_t Byte);
bcostm 0:e1d164542663 139 static void SPIx_Error(void);
bcostm 0:e1d164542663 140 static void SPIx_MspInit(SPI_HandleTypeDef *hspi);
bcostm 0:e1d164542663 141
bcostm 0:e1d164542663 142 /* Link function for LCD peripheral */
bcostm 0:e1d164542663 143 void LCD_IO_Init(void);
bcostm 0:e1d164542663 144 void LCD_IO_WriteData(uint16_t RegValue);
bcostm 0:e1d164542663 145 void LCD_IO_WriteReg(uint8_t Reg);
bcostm 0:e1d164542663 146 uint32_t LCD_IO_ReadData(uint16_t RegValue, uint8_t ReadSize);
bcostm 0:e1d164542663 147 void LCD_Delay(uint32_t delay);
bcostm 0:e1d164542663 148
bcostm 0:e1d164542663 149 /* IOExpander IO functions */
bcostm 0:e1d164542663 150 void IOE_Init(void);
bcostm 0:e1d164542663 151 void IOE_ITConfig(void);
bcostm 0:e1d164542663 152 void IOE_Delay(uint32_t Delay);
bcostm 0:e1d164542663 153 void IOE_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
bcostm 0:e1d164542663 154 uint8_t IOE_Read(uint8_t Addr, uint8_t Reg);
bcostm 0:e1d164542663 155 uint16_t IOE_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Length);
bcostm 0:e1d164542663 156 void IOE_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Length);
bcostm 0:e1d164542663 157
bcostm 0:e1d164542663 158 /* Link function for GYRO peripheral */
bcostm 0:e1d164542663 159 void GYRO_IO_Init(void);
bcostm 0:e1d164542663 160 void GYRO_IO_Write(uint8_t* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite);
bcostm 0:e1d164542663 161 void GYRO_IO_Read(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead);
bcostm 0:e1d164542663 162
bcostm 0:e1d164542663 163 #ifdef EE_M24LR64
bcostm 0:e1d164542663 164 /* Link function for I2C EEPROM peripheral */
bcostm 0:e1d164542663 165 void EEPROM_IO_Init(void);
bcostm 0:e1d164542663 166 HAL_StatusTypeDef EEPROM_IO_WriteData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize);
bcostm 0:e1d164542663 167 HAL_StatusTypeDef EEPROM_IO_ReadData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize);
bcostm 0:e1d164542663 168 HAL_StatusTypeDef EEPROM_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials);
bcostm 0:e1d164542663 169 #endif /* EE_M24LR64 */
bcostm 0:e1d164542663 170
bcostm 0:e1d164542663 171 /**
bcostm 0:e1d164542663 172 * @}
bcostm 0:e1d164542663 173 */
bcostm 0:e1d164542663 174
bcostm 0:e1d164542663 175 /** @defgroup STM32F429I_DISCOVERY_LOW_LEVEL_Private_Functions
bcostm 0:e1d164542663 176 * @{
bcostm 0:e1d164542663 177 */
bcostm 0:e1d164542663 178
bcostm 0:e1d164542663 179 /**
bcostm 0:e1d164542663 180 * @brief This method returns the STM32F429I DISCO BSP Driver revision
bcostm 0:e1d164542663 181 * @param None
bcostm 0:e1d164542663 182 * @retval version: 0xXYZR (8bits for each decimal, R for RC)
bcostm 0:e1d164542663 183 */
bcostm 0:e1d164542663 184 uint32_t BSP_GetVersion(void)
bcostm 0:e1d164542663 185 {
bcostm 0:e1d164542663 186 return __STM32F429I_DISCO_BSP_VERSION;
bcostm 0:e1d164542663 187 }
bcostm 0:e1d164542663 188
bcostm 0:e1d164542663 189 /**
bcostm 0:e1d164542663 190 * @brief Configures LED GPIO.
bcostm 0:e1d164542663 191 * @param Led: Specifies the Led to be configured.
bcostm 0:e1d164542663 192 * This parameter can be one of following parameters:
bcostm 0:e1d164542663 193 * @arg LED3
bcostm 0:e1d164542663 194 * @arg LED4
bcostm 0:e1d164542663 195 * @retval None
bcostm 0:e1d164542663 196 */
bcostm 0:e1d164542663 197 void BSP_LED_Init(Led_TypeDef Led)
bcostm 0:e1d164542663 198 {
bcostm 0:e1d164542663 199 GPIO_InitTypeDef GPIO_InitStruct;
bcostm 0:e1d164542663 200
bcostm 0:e1d164542663 201 /* Enable the GPIO_LED Clock */
bcostm 0:e1d164542663 202 LEDx_GPIO_CLK_ENABLE(Led);
bcostm 0:e1d164542663 203
bcostm 0:e1d164542663 204 /* Configure the GPIO_LED pin */
bcostm 0:e1d164542663 205 GPIO_InitStruct.Pin = GPIO_PIN[Led];
bcostm 0:e1d164542663 206 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
bcostm 0:e1d164542663 207 GPIO_InitStruct.Pull = GPIO_PULLUP;
bcostm 0:e1d164542663 208 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
bcostm 0:e1d164542663 209
bcostm 0:e1d164542663 210 HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
bcostm 0:e1d164542663 211
bcostm 0:e1d164542663 212 HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
bcostm 0:e1d164542663 213 }
bcostm 0:e1d164542663 214
bcostm 0:e1d164542663 215 /**
bcostm 0:e1d164542663 216 * @brief Turns selected LED On.
bcostm 0:e1d164542663 217 * @param Led: Specifies the Led to be set on.
bcostm 0:e1d164542663 218 * This parameter can be one of following parameters:
bcostm 0:e1d164542663 219 * @arg LED3
bcostm 0:e1d164542663 220 * @arg LED4
bcostm 0:e1d164542663 221 * @retval None
bcostm 0:e1d164542663 222 */
bcostm 0:e1d164542663 223 void BSP_LED_On(Led_TypeDef Led)
bcostm 0:e1d164542663 224 {
bcostm 0:e1d164542663 225 HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
bcostm 0:e1d164542663 226 }
bcostm 0:e1d164542663 227
bcostm 0:e1d164542663 228 /**
bcostm 0:e1d164542663 229 * @brief Turns selected LED Off.
bcostm 0:e1d164542663 230 * @param Led: Specifies the Led to be set off.
bcostm 0:e1d164542663 231 * This parameter can be one of following parameters:
bcostm 0:e1d164542663 232 * @arg LED3
bcostm 0:e1d164542663 233 * @arg LED4
bcostm 0:e1d164542663 234 * @retval None
bcostm 0:e1d164542663 235 */
bcostm 0:e1d164542663 236 void BSP_LED_Off(Led_TypeDef Led)
bcostm 0:e1d164542663 237 {
bcostm 0:e1d164542663 238 HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
bcostm 0:e1d164542663 239 }
bcostm 0:e1d164542663 240
bcostm 0:e1d164542663 241 /**
bcostm 0:e1d164542663 242 * @brief Toggles the selected LED.
bcostm 0:e1d164542663 243 * @param Led: Specifies the Led to be toggled.
bcostm 0:e1d164542663 244 * This parameter can be one of following parameters:
bcostm 0:e1d164542663 245 * @arg LED3
bcostm 0:e1d164542663 246 * @arg LED4
bcostm 0:e1d164542663 247 * @retval None
bcostm 0:e1d164542663 248 */
bcostm 0:e1d164542663 249 void BSP_LED_Toggle(Led_TypeDef Led)
bcostm 0:e1d164542663 250 {
bcostm 0:e1d164542663 251 HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
bcostm 0:e1d164542663 252 }
bcostm 0:e1d164542663 253
bcostm 0:e1d164542663 254 /**
bcostm 0:e1d164542663 255 * @brief Configures Button GPIO and EXTI Line.
bcostm 0:e1d164542663 256 * @param Button: Specifies the Button to be configured.
bcostm 0:e1d164542663 257 * This parameter should be: BUTTON_KEY
bcostm 0:e1d164542663 258 * @param Button_Mode: Specifies Button mode.
bcostm 0:e1d164542663 259 * This parameter can be one of following parameters:
bcostm 0:e1d164542663 260 * @arg BUTTON_MODE_GPIO: Button will be used as simple IO
bcostm 0:e1d164542663 261 * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
bcostm 0:e1d164542663 262 * generation capability
bcostm 0:e1d164542663 263 * @retval None
bcostm 0:e1d164542663 264 */
bcostm 0:e1d164542663 265 void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
bcostm 0:e1d164542663 266 {
bcostm 0:e1d164542663 267 GPIO_InitTypeDef GPIO_InitStruct;
bcostm 0:e1d164542663 268
bcostm 0:e1d164542663 269 /* Enable the BUTTON Clock */
bcostm 0:e1d164542663 270 BUTTONx_GPIO_CLK_ENABLE(Button);
bcostm 0:e1d164542663 271
bcostm 0:e1d164542663 272 if (ButtonMode == BUTTON_MODE_GPIO)
bcostm 0:e1d164542663 273 {
bcostm 0:e1d164542663 274 /* Configure Button pin as input */
bcostm 0:e1d164542663 275 GPIO_InitStruct.Pin = BUTTON_PIN[Button];
bcostm 0:e1d164542663 276 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
bcostm 0:e1d164542663 277 GPIO_InitStruct.Pull = GPIO_PULLDOWN;
bcostm 0:e1d164542663 278 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
bcostm 0:e1d164542663 279 HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
bcostm 0:e1d164542663 280 }
bcostm 0:e1d164542663 281
bcostm 0:e1d164542663 282 if (ButtonMode == BUTTON_MODE_EXTI)
bcostm 0:e1d164542663 283 {
bcostm 0:e1d164542663 284 /* Configure Button pin as input with External interrupt */
bcostm 0:e1d164542663 285 GPIO_InitStruct.Pin = BUTTON_PIN[Button];
bcostm 0:e1d164542663 286 GPIO_InitStruct.Pull = GPIO_NOPULL;
bcostm 0:e1d164542663 287 GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
bcostm 0:e1d164542663 288 HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
bcostm 0:e1d164542663 289
bcostm 0:e1d164542663 290 /* Enable and set Button EXTI Interrupt to the lowest priority */
bcostm 0:e1d164542663 291 HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0x00);
bcostm 0:e1d164542663 292 HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
bcostm 0:e1d164542663 293 }
bcostm 0:e1d164542663 294 }
bcostm 0:e1d164542663 295
bcostm 0:e1d164542663 296 /**
bcostm 0:e1d164542663 297 * @brief Returns the selected Button state.
bcostm 0:e1d164542663 298 * @param Button: Specifies the Button to be checked.
bcostm 0:e1d164542663 299 * This parameter should be: BUTTON_KEY
bcostm 0:e1d164542663 300 * @retval The Button GPIO pin value.
bcostm 0:e1d164542663 301 */
bcostm 0:e1d164542663 302 uint32_t BSP_PB_GetState(Button_TypeDef Button)
bcostm 0:e1d164542663 303 {
bcostm 0:e1d164542663 304 return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
bcostm 0:e1d164542663 305 }
bcostm 0:e1d164542663 306
bcostm 0:e1d164542663 307 /*******************************************************************************
bcostm 0:e1d164542663 308 BUS OPERATIONS
bcostm 0:e1d164542663 309 *******************************************************************************/
bcostm 0:e1d164542663 310
bcostm 0:e1d164542663 311 /******************************* I2C Routines *********************************/
bcostm 0:e1d164542663 312
bcostm 0:e1d164542663 313 /**
bcostm 0:e1d164542663 314 * @brief I2Cx MSP Initialization
bcostm 0:e1d164542663 315 * @param hi2c: I2C handle
bcostm 0:e1d164542663 316 * @retval None
bcostm 0:e1d164542663 317 */
bcostm 0:e1d164542663 318 static void I2Cx_MspInit(I2C_HandleTypeDef *hi2c)
bcostm 0:e1d164542663 319 {
bcostm 0:e1d164542663 320 GPIO_InitTypeDef GPIO_InitStruct;
bcostm 0:e1d164542663 321 #ifdef EE_M24LR64
bcostm 0:e1d164542663 322 static DMA_HandleTypeDef hdma_tx;
bcostm 0:e1d164542663 323 static DMA_HandleTypeDef hdma_rx;
bcostm 0:e1d164542663 324
bcostm 0:e1d164542663 325 I2C_HandleTypeDef* pI2cHandle;
bcostm 0:e1d164542663 326 pI2cHandle = &I2cHandle;
bcostm 0:e1d164542663 327 #endif /* EE_M24LR64 */
bcostm 0:e1d164542663 328
bcostm 0:e1d164542663 329 if (hi2c->Instance == DISCOVERY_I2Cx)
bcostm 0:e1d164542663 330 {
bcostm 0:e1d164542663 331 /* Configure the GPIOs ---------------------------------------------------*/
bcostm 0:e1d164542663 332 /* Enable GPIO clock */
bcostm 0:e1d164542663 333 DISCOVERY_I2Cx_SDA_GPIO_CLK_ENABLE();
bcostm 0:e1d164542663 334 DISCOVERY_I2Cx_SCL_GPIO_CLK_ENABLE();
bcostm 0:e1d164542663 335
bcostm 0:e1d164542663 336 /* Configure I2C Tx as alternate function */
bcostm 0:e1d164542663 337 GPIO_InitStruct.Pin = DISCOVERY_I2Cx_SCL_PIN;
bcostm 0:e1d164542663 338 GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
bcostm 0:e1d164542663 339 GPIO_InitStruct.Pull = GPIO_NOPULL;
bcostm 0:e1d164542663 340 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
bcostm 0:e1d164542663 341 GPIO_InitStruct.Alternate = DISCOVERY_I2Cx_SCL_SDA_AF;
bcostm 0:e1d164542663 342 HAL_GPIO_Init(DISCOVERY_I2Cx_SCL_GPIO_PORT, &GPIO_InitStruct);
bcostm 0:e1d164542663 343
bcostm 0:e1d164542663 344 /* Configure I2C Rx as alternate function */
bcostm 0:e1d164542663 345 GPIO_InitStruct.Pin = DISCOVERY_I2Cx_SDA_PIN;
bcostm 0:e1d164542663 346 HAL_GPIO_Init(DISCOVERY_I2Cx_SDA_GPIO_PORT, &GPIO_InitStruct);
bcostm 0:e1d164542663 347
bcostm 0:e1d164542663 348
bcostm 0:e1d164542663 349 /* Configure the Discovery I2Cx peripheral -------------------------------*/
bcostm 0:e1d164542663 350 /* Enable I2C3 clock */
bcostm 0:e1d164542663 351 DISCOVERY_I2Cx_CLOCK_ENABLE();
bcostm 0:e1d164542663 352
bcostm 0:e1d164542663 353 /* Force the I2C Peripheral Clock Reset */
bcostm 0:e1d164542663 354 DISCOVERY_I2Cx_FORCE_RESET();
bcostm 0:e1d164542663 355
bcostm 0:e1d164542663 356 /* Release the I2C Peripheral Clock Reset */
bcostm 0:e1d164542663 357 DISCOVERY_I2Cx_RELEASE_RESET();
bcostm 0:e1d164542663 358
bcostm 0:e1d164542663 359 /* Enable and set Discovery I2Cx Interrupt to the highest priority */
bcostm 0:e1d164542663 360 HAL_NVIC_SetPriority(DISCOVERY_I2Cx_EV_IRQn, 0x00, 0);
bcostm 0:e1d164542663 361 HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_EV_IRQn);
bcostm 0:e1d164542663 362
bcostm 0:e1d164542663 363 /* Enable and set Discovery I2Cx Interrupt to the highest priority */
bcostm 0:e1d164542663 364 HAL_NVIC_SetPriority(DISCOVERY_I2Cx_ER_IRQn, 0x00, 0);
bcostm 0:e1d164542663 365 HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_ER_IRQn);
bcostm 0:e1d164542663 366
bcostm 0:e1d164542663 367 #ifdef EE_M24LR64
bcostm 0:e1d164542663 368 /* I2C DMA TX and RX channels configuration */
bcostm 0:e1d164542663 369 /* Enable the DMA clock */
bcostm 0:e1d164542663 370 EEPROM_I2C_DMA_CLK_ENABLE();
bcostm 0:e1d164542663 371
bcostm 0:e1d164542663 372 /* Configure the DMA stream for the EE I2C peripheral TX direction */
bcostm 0:e1d164542663 373 /* Configure the DMA Stream */
bcostm 0:e1d164542663 374 hdma_tx.Instance = EEPROM_I2C_DMA_STREAM_TX;
bcostm 0:e1d164542663 375 /* Set the parameters to be configured */
bcostm 0:e1d164542663 376 hdma_tx.Init.Channel = EEPROM_I2C_DMA_CHANNEL;
bcostm 0:e1d164542663 377 hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
bcostm 0:e1d164542663 378 hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
bcostm 0:e1d164542663 379 hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
bcostm 0:e1d164542663 380 hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
bcostm 0:e1d164542663 381 hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
bcostm 0:e1d164542663 382 hdma_tx.Init.Mode = DMA_NORMAL;
bcostm 0:e1d164542663 383 hdma_tx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
bcostm 0:e1d164542663 384 hdma_tx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
bcostm 0:e1d164542663 385 hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
bcostm 0:e1d164542663 386 hdma_tx.Init.MemBurst = DMA_MBURST_SINGLE;
bcostm 0:e1d164542663 387 hdma_tx.Init.PeriphBurst = DMA_PBURST_SINGLE;
bcostm 0:e1d164542663 388
bcostm 0:e1d164542663 389 /* Associate the initilalized hdma_tx handle to the the pI2cHandle handle */
bcostm 0:e1d164542663 390 __HAL_LINKDMA(pI2cHandle, hdmatx, hdma_tx);
bcostm 0:e1d164542663 391
bcostm 0:e1d164542663 392 /* Configure the DMA Stream */
bcostm 0:e1d164542663 393 HAL_DMA_Init(&hdma_tx);
bcostm 0:e1d164542663 394
bcostm 0:e1d164542663 395 /* Configure and enable I2C DMA TX Channel interrupt */
bcostm 0:e1d164542663 396 HAL_NVIC_SetPriority((IRQn_Type)(EEPROM_I2C_DMA_TX_IRQn), EEPROM_I2C_DMA_PREPRIO, 0);
bcostm 0:e1d164542663 397 HAL_NVIC_EnableIRQ((IRQn_Type)(EEPROM_I2C_DMA_TX_IRQn));
bcostm 0:e1d164542663 398
bcostm 0:e1d164542663 399 /* Configure the DMA stream for the EE I2C peripheral TX direction */
bcostm 0:e1d164542663 400 /* Configure the DMA Stream */
bcostm 0:e1d164542663 401 hdma_rx.Instance = EEPROM_I2C_DMA_STREAM_RX;
bcostm 0:e1d164542663 402 /* Set the parameters to be configured */
bcostm 0:e1d164542663 403 hdma_rx.Init.Channel = EEPROM_I2C_DMA_CHANNEL;
bcostm 0:e1d164542663 404 hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
bcostm 0:e1d164542663 405 hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
bcostm 0:e1d164542663 406 hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
bcostm 0:e1d164542663 407 hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
bcostm 0:e1d164542663 408 hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
bcostm 0:e1d164542663 409 hdma_rx.Init.Mode = DMA_NORMAL;
bcostm 0:e1d164542663 410 hdma_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
bcostm 0:e1d164542663 411 hdma_rx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
bcostm 0:e1d164542663 412 hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
bcostm 0:e1d164542663 413 hdma_rx.Init.MemBurst = DMA_MBURST_SINGLE;
bcostm 0:e1d164542663 414 hdma_rx.Init.PeriphBurst = DMA_PBURST_SINGLE;
bcostm 0:e1d164542663 415
bcostm 0:e1d164542663 416 /* Associate the initilalized hdma_rx handle to the the pI2cHandle handle*/
bcostm 0:e1d164542663 417 __HAL_LINKDMA(pI2cHandle, hdmarx, hdma_rx);
bcostm 0:e1d164542663 418
bcostm 0:e1d164542663 419 /* Configure the DMA Stream */
bcostm 0:e1d164542663 420 HAL_DMA_Init(&hdma_rx);
bcostm 0:e1d164542663 421
bcostm 0:e1d164542663 422 /* Configure and enable I2C DMA RX Channel interrupt */
bcostm 0:e1d164542663 423 HAL_NVIC_SetPriority((IRQn_Type)(EEPROM_I2C_DMA_RX_IRQn), EEPROM_I2C_DMA_PREPRIO, 0);
bcostm 0:e1d164542663 424 HAL_NVIC_EnableIRQ((IRQn_Type)(EEPROM_I2C_DMA_RX_IRQn));
bcostm 0:e1d164542663 425 #endif /* EE_M24LR64 */
bcostm 0:e1d164542663 426 }
bcostm 0:e1d164542663 427 }
bcostm 0:e1d164542663 428
bcostm 0:e1d164542663 429 /**
bcostm 0:e1d164542663 430 * @brief I2Cx Bus initialization.
bcostm 0:e1d164542663 431 * @param None
bcostm 0:e1d164542663 432 * @retval None
bcostm 0:e1d164542663 433 */
bcostm 0:e1d164542663 434 static void I2Cx_Init(void)
bcostm 0:e1d164542663 435 {
bcostm 0:e1d164542663 436 if(HAL_I2C_GetState(&I2cHandle) == HAL_I2C_STATE_RESET)
bcostm 0:e1d164542663 437 {
bcostm 0:e1d164542663 438 I2cHandle.Instance = DISCOVERY_I2Cx;
bcostm 0:e1d164542663 439 I2cHandle.Init.ClockSpeed = BSP_I2C_SPEED;
bcostm 0:e1d164542663 440 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
bcostm 0:e1d164542663 441 I2cHandle.Init.OwnAddress1 = 0;
bcostm 0:e1d164542663 442 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
bcostm 0:e1d164542663 443 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
bcostm 0:e1d164542663 444 I2cHandle.Init.OwnAddress2 = 0;
bcostm 0:e1d164542663 445 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
bcostm 0:e1d164542663 446 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
bcostm 0:e1d164542663 447
bcostm 0:e1d164542663 448 /* Init the I2C */
bcostm 0:e1d164542663 449 I2Cx_MspInit(&I2cHandle);
bcostm 0:e1d164542663 450 HAL_I2C_Init(&I2cHandle);
bcostm 0:e1d164542663 451 }
bcostm 0:e1d164542663 452 }
bcostm 0:e1d164542663 453
bcostm 0:e1d164542663 454 /**
bcostm 0:e1d164542663 455 * @brief Configures Interruption pin for I2C communication.
bcostm 0:e1d164542663 456 * @param None
bcostm 0:e1d164542663 457 * @retval None
bcostm 0:e1d164542663 458 */
bcostm 0:e1d164542663 459 static void I2Cx_ITConfig(void)
bcostm 0:e1d164542663 460 {
bcostm 0:e1d164542663 461 GPIO_InitTypeDef GPIO_InitStruct;
bcostm 0:e1d164542663 462
bcostm 0:e1d164542663 463 /* Enable the GPIO EXTI Clock */
bcostm 0:e1d164542663 464 STMPE811_INT_CLK_ENABLE();
bcostm 0:e1d164542663 465
bcostm 0:e1d164542663 466 GPIO_InitStruct.Pin = STMPE811_INT_PIN;
bcostm 0:e1d164542663 467 GPIO_InitStruct.Pull = GPIO_PULLUP;
bcostm 0:e1d164542663 468 GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
bcostm 0:e1d164542663 469 GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
bcostm 0:e1d164542663 470 HAL_GPIO_Init(STMPE811_INT_GPIO_PORT, &GPIO_InitStruct);
bcostm 0:e1d164542663 471
bcostm 0:e1d164542663 472 /* Enable and set GPIO EXTI Interrupt to the highest priority */
bcostm 0:e1d164542663 473 HAL_NVIC_SetPriority((IRQn_Type)(STMPE811_INT_EXTI), 0x00, 0x00);
bcostm 0:e1d164542663 474 HAL_NVIC_EnableIRQ((IRQn_Type)(STMPE811_INT_EXTI));
bcostm 0:e1d164542663 475 }
bcostm 0:e1d164542663 476
bcostm 0:e1d164542663 477 /**
bcostm 0:e1d164542663 478 * @brief Writes a value in a register of the device through BUS.
bcostm 0:e1d164542663 479 * @param Addr: Device address on BUS Bus.
bcostm 0:e1d164542663 480 * @param Reg: The target register address to write
bcostm 0:e1d164542663 481 * @param Value: The target register value to be written
bcostm 0:e1d164542663 482 * @retval None
bcostm 0:e1d164542663 483 */
bcostm 0:e1d164542663 484 static void I2Cx_WriteData(uint8_t Addr, uint8_t Reg, uint8_t Value)
bcostm 0:e1d164542663 485 {
bcostm 0:e1d164542663 486 HAL_StatusTypeDef status = HAL_OK;
bcostm 0:e1d164542663 487
bcostm 0:e1d164542663 488 status = HAL_I2C_Mem_Write(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1, I2cxTimeout);
bcostm 0:e1d164542663 489
bcostm 0:e1d164542663 490 /* Check the communication status */
bcostm 0:e1d164542663 491 if(status != HAL_OK)
bcostm 0:e1d164542663 492 {
bcostm 0:e1d164542663 493 /* Re-Initialize the BUS */
bcostm 0:e1d164542663 494 I2Cx_Error();
bcostm 0:e1d164542663 495 }
bcostm 0:e1d164542663 496 }
bcostm 0:e1d164542663 497
bcostm 0:e1d164542663 498 /**
bcostm 0:e1d164542663 499 * @brief Writes a value in a register of the device through BUS.
bcostm 0:e1d164542663 500 * @param Addr: Device address on BUS Bus.
bcostm 0:e1d164542663 501 * @param Reg: The target register address to write
bcostm 0:e1d164542663 502 * @param pBuffer: The target register value to be written
bcostm 0:e1d164542663 503 * @param Length: buffer size to be written
bcostm 0:e1d164542663 504 * @retval None
bcostm 0:e1d164542663 505 */
bcostm 0:e1d164542663 506 static void I2Cx_WriteBuffer(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Length)
bcostm 0:e1d164542663 507 {
bcostm 0:e1d164542663 508 HAL_StatusTypeDef status = HAL_OK;
bcostm 0:e1d164542663 509
bcostm 0:e1d164542663 510 status = HAL_I2C_Mem_Write(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, pBuffer, Length, I2cxTimeout);
bcostm 0:e1d164542663 511
bcostm 0:e1d164542663 512 /* Check the communication status */
bcostm 0:e1d164542663 513 if(status != HAL_OK)
bcostm 0:e1d164542663 514 {
bcostm 0:e1d164542663 515 /* Re-Initialize the BUS */
bcostm 0:e1d164542663 516 I2Cx_Error();
bcostm 0:e1d164542663 517 }
bcostm 0:e1d164542663 518 }
bcostm 0:e1d164542663 519
bcostm 0:e1d164542663 520 /**
bcostm 0:e1d164542663 521 * @brief Reads a register of the device through BUS.
bcostm 0:e1d164542663 522 * @param Addr: Device address on BUS Bus.
bcostm 0:e1d164542663 523 * @param Reg: The target register address to write
bcostm 0:e1d164542663 524 * @retval Data read at register address
bcostm 0:e1d164542663 525 */
bcostm 0:e1d164542663 526 static uint8_t I2Cx_ReadData(uint8_t Addr, uint8_t Reg)
bcostm 0:e1d164542663 527 {
bcostm 0:e1d164542663 528 HAL_StatusTypeDef status = HAL_OK;
bcostm 0:e1d164542663 529 uint8_t value = 0;
bcostm 0:e1d164542663 530
bcostm 0:e1d164542663 531 status = HAL_I2C_Mem_Read(&I2cHandle, Addr, Reg, I2C_MEMADD_SIZE_8BIT, &value, 1, I2cxTimeout);
bcostm 0:e1d164542663 532
bcostm 0:e1d164542663 533 /* Check the communication status */
bcostm 0:e1d164542663 534 if(status != HAL_OK)
bcostm 0:e1d164542663 535 {
bcostm 0:e1d164542663 536 /* Re-Initialize the BUS */
bcostm 0:e1d164542663 537 I2Cx_Error();
bcostm 0:e1d164542663 538
bcostm 0:e1d164542663 539 }
bcostm 0:e1d164542663 540 return value;
bcostm 0:e1d164542663 541 }
bcostm 0:e1d164542663 542
bcostm 0:e1d164542663 543 /**
bcostm 0:e1d164542663 544 * @brief Reads multiple data on the BUS.
bcostm 0:e1d164542663 545 * @param Addr: I2C Address
bcostm 0:e1d164542663 546 * @param Reg: Reg Address
bcostm 0:e1d164542663 547 * @param pBuffer: pointer to read data buffer
bcostm 0:e1d164542663 548 * @param Length: length of the data
bcostm 0:e1d164542663 549 * @retval 0 if no problems to read multiple data
bcostm 0:e1d164542663 550 */
bcostm 0:e1d164542663 551 static uint8_t I2Cx_ReadBuffer(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Length)
bcostm 0:e1d164542663 552 {
bcostm 0:e1d164542663 553 HAL_StatusTypeDef status = HAL_OK;
bcostm 0:e1d164542663 554
bcostm 0:e1d164542663 555 status = HAL_I2C_Mem_Read(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, pBuffer, Length, I2cxTimeout);
bcostm 0:e1d164542663 556
bcostm 0:e1d164542663 557 /* Check the communication status */
bcostm 0:e1d164542663 558 if(status == HAL_OK)
bcostm 0:e1d164542663 559 {
bcostm 0:e1d164542663 560 return 0;
bcostm 0:e1d164542663 561 }
bcostm 0:e1d164542663 562 else
bcostm 0:e1d164542663 563 {
bcostm 0:e1d164542663 564 /* Re-Initialize the BUS */
bcostm 0:e1d164542663 565 I2Cx_Error();
bcostm 0:e1d164542663 566
bcostm 0:e1d164542663 567 return 1;
bcostm 0:e1d164542663 568 }
bcostm 0:e1d164542663 569 }
bcostm 0:e1d164542663 570
bcostm 0:e1d164542663 571 #ifdef EE_M24LR64
bcostm 0:e1d164542663 572 /**
bcostm 0:e1d164542663 573 * @brief Writes a value in a register of the device through BUS in using DMA mode.
bcostm 0:e1d164542663 574 * @param Addr: Device address on BUS Bus.
bcostm 0:e1d164542663 575 * @param Reg: The target register address to write
bcostm 0:e1d164542663 576 * @param pBuffer: The target register value to be written
bcostm 0:e1d164542663 577 * @param Length: buffer size to be written
bcostm 0:e1d164542663 578 * @retval HAL status
bcostm 0:e1d164542663 579 */
bcostm 0:e1d164542663 580 static HAL_StatusTypeDef I2Cx_WriteBufferDMA(uint8_t Addr, uint16_t Reg, uint8_t *pBuffer, uint16_t Length)
bcostm 0:e1d164542663 581 {
bcostm 0:e1d164542663 582 HAL_StatusTypeDef status = HAL_OK;
bcostm 0:e1d164542663 583
bcostm 0:e1d164542663 584 status = HAL_I2C_Mem_Write_DMA(&I2cHandle, Addr, Reg, I2C_MEMADD_SIZE_16BIT, pBuffer, Length);
bcostm 0:e1d164542663 585
bcostm 0:e1d164542663 586 /* Check the communication status */
bcostm 0:e1d164542663 587 if(status != HAL_OK)
bcostm 0:e1d164542663 588 {
bcostm 0:e1d164542663 589 /* Re-Initialize the BUS */
bcostm 0:e1d164542663 590 I2Cx_Error();
bcostm 0:e1d164542663 591 }
bcostm 0:e1d164542663 592
bcostm 0:e1d164542663 593 return status;
bcostm 0:e1d164542663 594 }
bcostm 0:e1d164542663 595
bcostm 0:e1d164542663 596 /**
bcostm 0:e1d164542663 597 * @brief Reads multiple data on the BUS in using DMA mode.
bcostm 0:e1d164542663 598 * @param Addr: I2C Address
bcostm 0:e1d164542663 599 * @param Reg: Reg Address
bcostm 0:e1d164542663 600 * @param pBuffer: pointer to read data buffer
bcostm 0:e1d164542663 601 * @param Length: length of the data
bcostm 0:e1d164542663 602 * @retval HAL status
bcostm 0:e1d164542663 603 */
bcostm 0:e1d164542663 604 static HAL_StatusTypeDef I2Cx_ReadBufferDMA(uint8_t Addr, uint16_t Reg, uint8_t *pBuffer, uint16_t Length)
bcostm 0:e1d164542663 605 {
bcostm 0:e1d164542663 606 HAL_StatusTypeDef status = HAL_OK;
bcostm 0:e1d164542663 607
bcostm 0:e1d164542663 608 status = HAL_I2C_Mem_Read_DMA(&I2cHandle, Addr, Reg, I2C_MEMADD_SIZE_16BIT, pBuffer, Length);
bcostm 0:e1d164542663 609
bcostm 0:e1d164542663 610 /* Check the communication status */
bcostm 0:e1d164542663 611 if(status != HAL_OK)
bcostm 0:e1d164542663 612 {
bcostm 0:e1d164542663 613 /* Re-Initialize the BUS */
bcostm 0:e1d164542663 614 I2Cx_Error();
bcostm 0:e1d164542663 615 }
bcostm 0:e1d164542663 616
bcostm 0:e1d164542663 617 return status;
bcostm 0:e1d164542663 618 }
bcostm 0:e1d164542663 619
bcostm 0:e1d164542663 620 /**
bcostm 0:e1d164542663 621 * @brief Checks if target device is ready for communication.
bcostm 0:e1d164542663 622 * @note This function is used with Memory devices
bcostm 0:e1d164542663 623 * @param DevAddress: Target device address
bcostm 0:e1d164542663 624 * @param Trials: Number of trials
bcostm 0:e1d164542663 625 * @retval HAL status
bcostm 0:e1d164542663 626 */
bcostm 0:e1d164542663 627 static HAL_StatusTypeDef I2Cx_IsDeviceReady(uint16_t DevAddress, uint32_t Trials)
bcostm 0:e1d164542663 628 {
bcostm 0:e1d164542663 629 return (HAL_I2C_IsDeviceReady(&I2cHandle, DevAddress, Trials, I2cxTimeout));
bcostm 0:e1d164542663 630 }
bcostm 0:e1d164542663 631 #endif /* EE_M24LR64 */
bcostm 0:e1d164542663 632
bcostm 0:e1d164542663 633 /**
bcostm 0:e1d164542663 634 * @brief I2Cx error treatment function
bcostm 0:e1d164542663 635 * @param None
bcostm 0:e1d164542663 636 * @retval None
bcostm 0:e1d164542663 637 */
bcostm 0:e1d164542663 638 static void I2Cx_Error(void)
bcostm 0:e1d164542663 639 {
bcostm 0:e1d164542663 640 /* De-initialize the SPI communication BUS */
bcostm 0:e1d164542663 641 HAL_I2C_DeInit(&I2cHandle);
bcostm 0:e1d164542663 642
bcostm 0:e1d164542663 643 /* Re-Initialize the SPI communication BUS */
bcostm 0:e1d164542663 644 I2Cx_Init();
bcostm 0:e1d164542663 645 }
bcostm 0:e1d164542663 646
bcostm 0:e1d164542663 647 /******************************* SPI Routines *********************************/
bcostm 0:e1d164542663 648
bcostm 0:e1d164542663 649 /**
bcostm 0:e1d164542663 650 * @brief SPIx Bus initialization
bcostm 0:e1d164542663 651 * @param None
bcostm 0:e1d164542663 652 * @retval None
bcostm 0:e1d164542663 653 */
bcostm 0:e1d164542663 654 static void SPIx_Init(void)
bcostm 0:e1d164542663 655 {
bcostm 0:e1d164542663 656 if(HAL_SPI_GetState(&SpiHandle) == HAL_SPI_STATE_RESET)
bcostm 0:e1d164542663 657 {
bcostm 0:e1d164542663 658 /* SPI configuration -----------------------------------------------------*/
bcostm 0:e1d164542663 659 SpiHandle.Instance = DISCOVERY_SPIx;
bcostm 0:e1d164542663 660 /* SPI baudrate is set to 5.6 MHz (PCLK2/SPI_BaudRatePrescaler = 90/16 = 5.625 MHz)
bcostm 0:e1d164542663 661 to verify these constraints:
bcostm 0:e1d164542663 662 - ILI9341 LCD SPI interface max baudrate is 10MHz for write and 6.66MHz for read
bcostm 0:e1d164542663 663 - l3gd20 SPI interface max baudrate is 10MHz for write/read
bcostm 0:e1d164542663 664 - PCLK2 frequency is set to 90 MHz
bcostm 0:e1d164542663 665 */
bcostm 0:e1d164542663 666 SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
bcostm 0:e1d164542663 667
bcostm 0:e1d164542663 668 /* On STM32F429I-Discovery, LCD ID cannot be read then keep a common configuration */
bcostm 0:e1d164542663 669 /* for LCD and GYRO (SPI_DIRECTION_2LINES) */
bcostm 0:e1d164542663 670 /* Note: To read a register a LCD, SPI_DIRECTION_1LINE should be set */
bcostm 0:e1d164542663 671 SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
bcostm 0:e1d164542663 672 SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
bcostm 0:e1d164542663 673 SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW;
bcostm 0:e1d164542663 674 SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
bcostm 0:e1d164542663 675 SpiHandle.Init.CRCPolynomial = 7;
bcostm 0:e1d164542663 676 SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
bcostm 0:e1d164542663 677 SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
bcostm 0:e1d164542663 678 SpiHandle.Init.NSS = SPI_NSS_SOFT;
bcostm 0:e1d164542663 679 SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED;
bcostm 0:e1d164542663 680 SpiHandle.Init.Mode = SPI_MODE_MASTER;
bcostm 0:e1d164542663 681
bcostm 0:e1d164542663 682 SPIx_MspInit(&SpiHandle);
bcostm 0:e1d164542663 683 HAL_SPI_Init(&SpiHandle);
bcostm 0:e1d164542663 684 }
bcostm 0:e1d164542663 685 }
bcostm 0:e1d164542663 686
bcostm 0:e1d164542663 687 /**
bcostm 0:e1d164542663 688 * @brief Reads 4 bytes from device.
bcostm 0:e1d164542663 689 * @param ReadSize: Number of bytes to read (max 4 bytes)
bcostm 0:e1d164542663 690 * @retval Value read on the SPI
bcostm 0:e1d164542663 691 */
bcostm 0:e1d164542663 692 static uint32_t SPIx_Read(uint8_t ReadSize)
bcostm 0:e1d164542663 693 {
bcostm 0:e1d164542663 694 HAL_StatusTypeDef status = HAL_OK;
bcostm 0:e1d164542663 695 uint32_t readvalue;
bcostm 0:e1d164542663 696
bcostm 0:e1d164542663 697 status = HAL_SPI_Receive(&SpiHandle, (uint8_t*) &readvalue, ReadSize, SpixTimeout);
bcostm 0:e1d164542663 698
bcostm 0:e1d164542663 699 /* Check the communication status */
bcostm 0:e1d164542663 700 if(status != HAL_OK)
bcostm 0:e1d164542663 701 {
bcostm 0:e1d164542663 702 /* Re-Initialize the BUS */
bcostm 0:e1d164542663 703 SPIx_Error();
bcostm 0:e1d164542663 704 }
bcostm 0:e1d164542663 705
bcostm 0:e1d164542663 706 return readvalue;
bcostm 0:e1d164542663 707 }
bcostm 0:e1d164542663 708
bcostm 0:e1d164542663 709 /**
bcostm 0:e1d164542663 710 * @brief Writes a byte to device.
bcostm 0:e1d164542663 711 * @param Value: value to be written
bcostm 0:e1d164542663 712 * @retval None
bcostm 0:e1d164542663 713 */
bcostm 0:e1d164542663 714 static void SPIx_Write(uint16_t Value)
bcostm 0:e1d164542663 715 {
bcostm 0:e1d164542663 716 HAL_StatusTypeDef status = HAL_OK;
bcostm 0:e1d164542663 717
bcostm 0:e1d164542663 718 status = HAL_SPI_Transmit(&SpiHandle, (uint8_t*) &Value, 1, SpixTimeout);
bcostm 0:e1d164542663 719
bcostm 0:e1d164542663 720 /* Check the communication status */
bcostm 0:e1d164542663 721 if(status != HAL_OK)
bcostm 0:e1d164542663 722 {
bcostm 0:e1d164542663 723 /* Re-Initialize the BUS */
bcostm 0:e1d164542663 724 SPIx_Error();
bcostm 0:e1d164542663 725 }
bcostm 0:e1d164542663 726 }
bcostm 0:e1d164542663 727
bcostm 0:e1d164542663 728 /**
bcostm 0:e1d164542663 729 * @brief Sends a Byte through the SPI interface and return the Byte received
bcostm 0:e1d164542663 730 * from the SPI bus.
bcostm 0:e1d164542663 731 * @param Byte: Byte send.
bcostm 0:e1d164542663 732 * @retval The received byte value
bcostm 0:e1d164542663 733 */
bcostm 0:e1d164542663 734 static uint8_t SPIx_WriteRead(uint8_t Byte)
bcostm 0:e1d164542663 735 {
bcostm 0:e1d164542663 736 uint8_t receivedbyte = 0;
bcostm 0:e1d164542663 737
bcostm 0:e1d164542663 738 /* Send a Byte through the SPI peripheral */
bcostm 0:e1d164542663 739 /* Read byte from the SPI bus */
bcostm 0:e1d164542663 740 if(HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t*) &Byte, (uint8_t*) &receivedbyte, 1, SpixTimeout) != HAL_OK)
bcostm 0:e1d164542663 741 {
bcostm 0:e1d164542663 742 SPIx_Error();
bcostm 0:e1d164542663 743 }
bcostm 0:e1d164542663 744
bcostm 0:e1d164542663 745 return receivedbyte;
bcostm 0:e1d164542663 746 }
bcostm 0:e1d164542663 747
bcostm 0:e1d164542663 748 /**
bcostm 0:e1d164542663 749 * @brief SPIx error treatment function.
bcostm 0:e1d164542663 750 * @param None
bcostm 0:e1d164542663 751 * @retval None
bcostm 0:e1d164542663 752 */
bcostm 0:e1d164542663 753 static void SPIx_Error(void)
bcostm 0:e1d164542663 754 {
bcostm 0:e1d164542663 755 /* De-initialize the SPI communication BUS */
bcostm 0:e1d164542663 756 HAL_SPI_DeInit(&SpiHandle);
bcostm 0:e1d164542663 757
bcostm 0:e1d164542663 758 /* Re- Initialize the SPI communication BUS */
bcostm 0:e1d164542663 759 SPIx_Init();
bcostm 0:e1d164542663 760 }
bcostm 0:e1d164542663 761
bcostm 0:e1d164542663 762 /**
bcostm 0:e1d164542663 763 * @brief SPI MSP Init.
bcostm 0:e1d164542663 764 * @param hspi: SPI handle
bcostm 0:e1d164542663 765 * @retval None
bcostm 0:e1d164542663 766 */
bcostm 0:e1d164542663 767 static void SPIx_MspInit(SPI_HandleTypeDef *hspi)
bcostm 0:e1d164542663 768 {
bcostm 0:e1d164542663 769 GPIO_InitTypeDef GPIO_InitStructure;
bcostm 0:e1d164542663 770
bcostm 0:e1d164542663 771 /* Enable SPIx clock */
bcostm 0:e1d164542663 772 DISCOVERY_SPIx_CLK_ENABLE();
bcostm 0:e1d164542663 773
bcostm 0:e1d164542663 774 /* Enable DISCOVERY_SPI GPIO clock */
bcostm 0:e1d164542663 775 DISCOVERY_SPIx_GPIO_CLK_ENABLE();
bcostm 0:e1d164542663 776
bcostm 0:e1d164542663 777 /* configure SPI SCK, MOSI and MISO */
bcostm 0:e1d164542663 778 GPIO_InitStructure.Pin = (DISCOVERY_SPIx_SCK_PIN | DISCOVERY_SPIx_MOSI_PIN | DISCOVERY_SPIx_MISO_PIN);
bcostm 0:e1d164542663 779 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
bcostm 0:e1d164542663 780 GPIO_InitStructure.Pull = GPIO_PULLDOWN;
bcostm 0:e1d164542663 781 GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM;
bcostm 0:e1d164542663 782 GPIO_InitStructure.Alternate = DISCOVERY_SPIx_AF;
bcostm 0:e1d164542663 783 HAL_GPIO_Init(DISCOVERY_SPIx_GPIO_PORT, &GPIO_InitStructure);
bcostm 0:e1d164542663 784 }
bcostm 0:e1d164542663 785
bcostm 0:e1d164542663 786 /********************************* LINK LCD ***********************************/
bcostm 0:e1d164542663 787
bcostm 0:e1d164542663 788 /**
bcostm 0:e1d164542663 789 * @brief Configures the LCD_SPI interface.
bcostm 0:e1d164542663 790 * @param None
bcostm 0:e1d164542663 791 * @retval None
bcostm 0:e1d164542663 792 */
bcostm 0:e1d164542663 793 void LCD_IO_Init(void)
bcostm 0:e1d164542663 794 {
bcostm 0:e1d164542663 795 GPIO_InitTypeDef GPIO_InitStructure;
bcostm 0:e1d164542663 796
bcostm 0:e1d164542663 797 if(Is_LCD_IO_Initialized == 0)
bcostm 0:e1d164542663 798 {
bcostm 0:e1d164542663 799 Is_LCD_IO_Initialized = 1;
bcostm 0:e1d164542663 800
bcostm 0:e1d164542663 801 /* Configure NCS in Output Push-Pull mode */
bcostm 0:e1d164542663 802 LCD_WRX_GPIO_CLK_ENABLE();
bcostm 0:e1d164542663 803 GPIO_InitStructure.Pin = LCD_WRX_PIN;
bcostm 0:e1d164542663 804 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
bcostm 0:e1d164542663 805 GPIO_InitStructure.Pull = GPIO_NOPULL;
bcostm 0:e1d164542663 806 GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
bcostm 0:e1d164542663 807 HAL_GPIO_Init(LCD_WRX_GPIO_PORT, &GPIO_InitStructure);
bcostm 0:e1d164542663 808
bcostm 0:e1d164542663 809 LCD_RDX_GPIO_CLK_ENABLE();
bcostm 0:e1d164542663 810 GPIO_InitStructure.Pin = LCD_RDX_PIN;
bcostm 0:e1d164542663 811 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
bcostm 0:e1d164542663 812 GPIO_InitStructure.Pull = GPIO_NOPULL;
bcostm 0:e1d164542663 813 GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
bcostm 0:e1d164542663 814 HAL_GPIO_Init(LCD_RDX_GPIO_PORT, &GPIO_InitStructure);
bcostm 0:e1d164542663 815
bcostm 0:e1d164542663 816 /* Configure the LCD Control pins ----------------------------------------*/
bcostm 0:e1d164542663 817 LCD_NCS_GPIO_CLK_ENABLE();
bcostm 0:e1d164542663 818
bcostm 0:e1d164542663 819 /* Configure NCS in Output Push-Pull mode */
bcostm 0:e1d164542663 820 GPIO_InitStructure.Pin = LCD_NCS_PIN;
bcostm 0:e1d164542663 821 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
bcostm 0:e1d164542663 822 GPIO_InitStructure.Pull = GPIO_NOPULL;
bcostm 0:e1d164542663 823 GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
bcostm 0:e1d164542663 824 HAL_GPIO_Init(LCD_NCS_GPIO_PORT, &GPIO_InitStructure);
bcostm 0:e1d164542663 825
bcostm 0:e1d164542663 826 /* Set or Reset the control line */
bcostm 0:e1d164542663 827 LCD_CS_LOW();
bcostm 0:e1d164542663 828 LCD_CS_HIGH();
bcostm 0:e1d164542663 829
bcostm 0:e1d164542663 830 SPIx_Init();
bcostm 0:e1d164542663 831 }
bcostm 0:e1d164542663 832 }
bcostm 0:e1d164542663 833
bcostm 0:e1d164542663 834 /**
bcostm 0:e1d164542663 835 * @brief Writes register value.
bcostm 0:e1d164542663 836 * @param None
bcostm 0:e1d164542663 837 * @retval None
bcostm 0:e1d164542663 838 */
bcostm 0:e1d164542663 839 void LCD_IO_WriteData(uint16_t RegValue)
bcostm 0:e1d164542663 840 {
bcostm 0:e1d164542663 841 /* Set WRX to send data */
bcostm 0:e1d164542663 842 LCD_WRX_HIGH();
bcostm 0:e1d164542663 843
bcostm 0:e1d164542663 844 /* Reset LCD control line(/CS) and Send data */
bcostm 0:e1d164542663 845 LCD_CS_LOW();
bcostm 0:e1d164542663 846 SPIx_Write(RegValue);
bcostm 0:e1d164542663 847
bcostm 0:e1d164542663 848 /* Deselect: Chip Select high */
bcostm 0:e1d164542663 849 LCD_CS_HIGH();
bcostm 0:e1d164542663 850 }
bcostm 0:e1d164542663 851
bcostm 0:e1d164542663 852 /**
bcostm 0:e1d164542663 853 * @brief Writes register address.
bcostm 0:e1d164542663 854 * @param None
bcostm 0:e1d164542663 855 * @retval None
bcostm 0:e1d164542663 856 */
bcostm 0:e1d164542663 857 void LCD_IO_WriteReg(uint8_t Reg)
bcostm 0:e1d164542663 858 {
bcostm 0:e1d164542663 859 /* Reset WRX to send command */
bcostm 0:e1d164542663 860 LCD_WRX_LOW();
bcostm 0:e1d164542663 861
bcostm 0:e1d164542663 862 /* Reset LCD control line(/CS) and Send command */
bcostm 0:e1d164542663 863 LCD_CS_LOW();
bcostm 0:e1d164542663 864 SPIx_Write(Reg);
bcostm 0:e1d164542663 865
bcostm 0:e1d164542663 866 /* Deselect: Chip Select high */
bcostm 0:e1d164542663 867 LCD_CS_HIGH();
bcostm 0:e1d164542663 868 }
bcostm 0:e1d164542663 869
bcostm 0:e1d164542663 870 /**
bcostm 0:e1d164542663 871 * @brief Reads register value.
bcostm 0:e1d164542663 872 * @param RegValue Address of the register to read
bcostm 0:e1d164542663 873 * @param ReadSize Number of bytes to read
bcostm 0:e1d164542663 874 * @retval Content of the register value
bcostm 0:e1d164542663 875 */
bcostm 0:e1d164542663 876 uint32_t LCD_IO_ReadData(uint16_t RegValue, uint8_t ReadSize)
bcostm 0:e1d164542663 877 {
bcostm 0:e1d164542663 878 uint32_t readvalue = 0;
bcostm 0:e1d164542663 879
bcostm 0:e1d164542663 880 /* Select: Chip Select low */
bcostm 0:e1d164542663 881 LCD_CS_LOW();
bcostm 0:e1d164542663 882
bcostm 0:e1d164542663 883 /* Reset WRX to send command */
bcostm 0:e1d164542663 884 LCD_WRX_LOW();
bcostm 0:e1d164542663 885
bcostm 0:e1d164542663 886 SPIx_Write(RegValue);
bcostm 0:e1d164542663 887
bcostm 0:e1d164542663 888 readvalue = SPIx_Read(ReadSize);
bcostm 0:e1d164542663 889
bcostm 0:e1d164542663 890 /* Set WRX to send data */
bcostm 0:e1d164542663 891 LCD_WRX_HIGH();
bcostm 0:e1d164542663 892
bcostm 0:e1d164542663 893 /* Deselect: Chip Select high */
bcostm 0:e1d164542663 894 LCD_CS_HIGH();
bcostm 0:e1d164542663 895
bcostm 0:e1d164542663 896 return readvalue;
bcostm 0:e1d164542663 897 }
bcostm 0:e1d164542663 898
bcostm 0:e1d164542663 899 /**
bcostm 0:e1d164542663 900 * @brief Wait for loop in ms.
bcostm 0:e1d164542663 901 * @param Delay in ms.
bcostm 0:e1d164542663 902 * @retval None
bcostm 0:e1d164542663 903 */
bcostm 0:e1d164542663 904 void LCD_Delay(uint32_t Delay)
bcostm 0:e1d164542663 905 {
bcostm 0:e1d164542663 906 //HAL_Delay(Delay);
bcostm 0:e1d164542663 907 wait_ms(Delay);
bcostm 0:e1d164542663 908 }
bcostm 0:e1d164542663 909
bcostm 0:e1d164542663 910 /*******************************************************************************
bcostm 0:e1d164542663 911 LINK OPERATIONS
bcostm 0:e1d164542663 912 *******************************************************************************/
bcostm 0:e1d164542663 913
bcostm 0:e1d164542663 914 /********************************* LINK IOE ***********************************/
bcostm 0:e1d164542663 915
bcostm 0:e1d164542663 916 /**
bcostm 0:e1d164542663 917 * @brief IOE Low Level Initialization.
bcostm 0:e1d164542663 918 * @param None
bcostm 0:e1d164542663 919 * @retval None
bcostm 0:e1d164542663 920 */
bcostm 0:e1d164542663 921 void IOE_Init(void)
bcostm 0:e1d164542663 922 {
bcostm 0:e1d164542663 923 I2Cx_Init();
bcostm 0:e1d164542663 924 }
bcostm 0:e1d164542663 925
bcostm 0:e1d164542663 926 /**
bcostm 0:e1d164542663 927 * @brief IOE Low Level Interrupt configuration.
bcostm 0:e1d164542663 928 * @param None
bcostm 0:e1d164542663 929 * @retval None
bcostm 0:e1d164542663 930 */
bcostm 0:e1d164542663 931 void IOE_ITConfig(void)
bcostm 0:e1d164542663 932 {
bcostm 0:e1d164542663 933 I2Cx_ITConfig();
bcostm 0:e1d164542663 934 }
bcostm 0:e1d164542663 935
bcostm 0:e1d164542663 936 /**
bcostm 0:e1d164542663 937 * @brief IOE Writes single data operation.
bcostm 0:e1d164542663 938 * @param Addr: I2C Address
bcostm 0:e1d164542663 939 * @param Reg: Reg Address
bcostm 0:e1d164542663 940 * @param Value: Data to be written
bcostm 0:e1d164542663 941 * @retval None
bcostm 0:e1d164542663 942 */
bcostm 0:e1d164542663 943 void IOE_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
bcostm 0:e1d164542663 944 {
bcostm 0:e1d164542663 945 I2Cx_WriteData(Addr, Reg, Value);
bcostm 0:e1d164542663 946 }
bcostm 0:e1d164542663 947
bcostm 0:e1d164542663 948 /**
bcostm 0:e1d164542663 949 * @brief IOE Reads single data.
bcostm 0:e1d164542663 950 * @param Addr: I2C Address
bcostm 0:e1d164542663 951 * @param Reg: Reg Address
bcostm 0:e1d164542663 952 * @retval The read data
bcostm 0:e1d164542663 953 */
bcostm 0:e1d164542663 954 uint8_t IOE_Read(uint8_t Addr, uint8_t Reg)
bcostm 0:e1d164542663 955 {
bcostm 0:e1d164542663 956 return I2Cx_ReadData(Addr, Reg);
bcostm 0:e1d164542663 957 }
bcostm 0:e1d164542663 958
bcostm 0:e1d164542663 959 /**
bcostm 0:e1d164542663 960 * @brief IOE Writes multiple data.
bcostm 0:e1d164542663 961 * @param Addr: I2C Address
bcostm 0:e1d164542663 962 * @param Reg: Reg Address
bcostm 0:e1d164542663 963 * @param pBuffer: pointer to data buffer
bcostm 0:e1d164542663 964 * @param Length: length of the data
bcostm 0:e1d164542663 965 * @retval None
bcostm 0:e1d164542663 966 */
bcostm 0:e1d164542663 967 void IOE_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Length)
bcostm 0:e1d164542663 968 {
bcostm 0:e1d164542663 969 I2Cx_WriteBuffer(Addr, Reg, pBuffer, Length);
bcostm 0:e1d164542663 970 }
bcostm 0:e1d164542663 971
bcostm 0:e1d164542663 972 /**
bcostm 0:e1d164542663 973 * @brief IOE Reads multiple data.
bcostm 0:e1d164542663 974 * @param Addr: I2C Address
bcostm 0:e1d164542663 975 * @param Reg: Reg Address
bcostm 0:e1d164542663 976 * @param pBuffer: pointer to data buffer
bcostm 0:e1d164542663 977 * @param Length: length of the data
bcostm 0:e1d164542663 978 * @retval 0 if no problems to read multiple data
bcostm 0:e1d164542663 979 */
bcostm 0:e1d164542663 980 uint16_t IOE_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Length)
bcostm 0:e1d164542663 981 {
bcostm 0:e1d164542663 982 return I2Cx_ReadBuffer(Addr, Reg, pBuffer, Length);
bcostm 0:e1d164542663 983 }
bcostm 0:e1d164542663 984
bcostm 0:e1d164542663 985 /**
bcostm 0:e1d164542663 986 * @brief IOE Delay.
bcostm 0:e1d164542663 987 * @param Delay in ms
bcostm 0:e1d164542663 988 * @retval None
bcostm 0:e1d164542663 989 */
bcostm 0:e1d164542663 990 void IOE_Delay(uint32_t Delay)
bcostm 0:e1d164542663 991 {
bcostm 0:e1d164542663 992 //HAL_Delay(Delay);
bcostm 0:e1d164542663 993 wait_ms(Delay);
bcostm 0:e1d164542663 994 }
bcostm 0:e1d164542663 995
bcostm 0:e1d164542663 996 /********************************* LINK GYROSCOPE *****************************/
bcostm 0:e1d164542663 997
bcostm 0:e1d164542663 998 /**
bcostm 0:e1d164542663 999 * @brief Configures the Gyroscope SPI interface.
bcostm 0:e1d164542663 1000 * @param None
bcostm 0:e1d164542663 1001 * @retval None
bcostm 0:e1d164542663 1002 */
bcostm 0:e1d164542663 1003 void GYRO_IO_Init(void)
bcostm 0:e1d164542663 1004 {
bcostm 0:e1d164542663 1005 GPIO_InitTypeDef GPIO_InitStructure;
bcostm 0:e1d164542663 1006
bcostm 0:e1d164542663 1007 /* Configure the Gyroscope Control pins ------------------------------------*/
bcostm 0:e1d164542663 1008 /* Enable CS GPIO clock and Configure GPIO PIN for Gyroscope Chip select */
bcostm 0:e1d164542663 1009 GYRO_CS_GPIO_CLK_ENABLE();
bcostm 0:e1d164542663 1010 GPIO_InitStructure.Pin = GYRO_CS_PIN;
bcostm 0:e1d164542663 1011 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
bcostm 0:e1d164542663 1012 GPIO_InitStructure.Pull = GPIO_NOPULL;
bcostm 0:e1d164542663 1013 GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM;
bcostm 0:e1d164542663 1014 HAL_GPIO_Init(GYRO_CS_GPIO_PORT, &GPIO_InitStructure);
bcostm 0:e1d164542663 1015
bcostm 0:e1d164542663 1016 /* Deselect: Chip Select high */
bcostm 0:e1d164542663 1017 GYRO_CS_HIGH();
bcostm 0:e1d164542663 1018
bcostm 0:e1d164542663 1019 /* Enable INT1, INT2 GPIO clock and Configure GPIO PINs to detect Interrupts */
bcostm 0:e1d164542663 1020 GYRO_INT_GPIO_CLK_ENABLE();
bcostm 0:e1d164542663 1021 GPIO_InitStructure.Pin = GYRO_INT1_PIN | GYRO_INT2_PIN;
bcostm 0:e1d164542663 1022 GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
bcostm 0:e1d164542663 1023 GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
bcostm 0:e1d164542663 1024 GPIO_InitStructure.Pull= GPIO_NOPULL;
bcostm 0:e1d164542663 1025 HAL_GPIO_Init(GYRO_INT_GPIO_PORT, &GPIO_InitStructure);
bcostm 0:e1d164542663 1026
bcostm 0:e1d164542663 1027 SPIx_Init();
bcostm 0:e1d164542663 1028 }
bcostm 0:e1d164542663 1029
bcostm 0:e1d164542663 1030 /**
bcostm 0:e1d164542663 1031 * @brief Writes one byte to the Gyroscope.
bcostm 0:e1d164542663 1032 * @param pBuffer: Pointer to the buffer containing the data to be written to the Gyroscope.
bcostm 0:e1d164542663 1033 * @param WriteAdd: Gyroscope's internal address to write to.
bcostm 0:e1d164542663 1034 * @param NumByteToWrite: Number of bytes to write.
bcostm 0:e1d164542663 1035 * @retval None
bcostm 0:e1d164542663 1036 */
bcostm 0:e1d164542663 1037 void GYRO_IO_Write(uint8_t* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite)
bcostm 0:e1d164542663 1038 {
bcostm 0:e1d164542663 1039 /* Configure the MS bit:
bcostm 0:e1d164542663 1040 - When 0, the address will remain unchanged in multiple read/write commands.
bcostm 0:e1d164542663 1041 - When 1, the address will be auto incremented in multiple read/write commands.
bcostm 0:e1d164542663 1042 */
bcostm 0:e1d164542663 1043 if(NumByteToWrite > 0x01)
bcostm 0:e1d164542663 1044 {
bcostm 0:e1d164542663 1045 WriteAddr |= (uint8_t)MULTIPLEBYTE_CMD;
bcostm 0:e1d164542663 1046 }
bcostm 0:e1d164542663 1047 /* Set chip select Low at the start of the transmission */
bcostm 0:e1d164542663 1048 GYRO_CS_LOW();
bcostm 0:e1d164542663 1049
bcostm 0:e1d164542663 1050 /* Send the Address of the indexed register */
bcostm 0:e1d164542663 1051 SPIx_WriteRead(WriteAddr);
bcostm 0:e1d164542663 1052
bcostm 0:e1d164542663 1053 /* Send the data that will be written into the device (MSB First) */
bcostm 0:e1d164542663 1054 while(NumByteToWrite >= 0x01)
bcostm 0:e1d164542663 1055 {
bcostm 0:e1d164542663 1056 SPIx_WriteRead(*pBuffer);
bcostm 0:e1d164542663 1057 NumByteToWrite--;
bcostm 0:e1d164542663 1058 pBuffer++;
bcostm 0:e1d164542663 1059 }
bcostm 0:e1d164542663 1060
bcostm 0:e1d164542663 1061 /* Set chip select High at the end of the transmission */
bcostm 0:e1d164542663 1062 GYRO_CS_HIGH();
bcostm 0:e1d164542663 1063 }
bcostm 0:e1d164542663 1064
bcostm 0:e1d164542663 1065 /**
bcostm 0:e1d164542663 1066 * @brief Reads a block of data from the Gyroscope.
bcostm 0:e1d164542663 1067 * @param pBuffer: Pointer to the buffer that receives the data read from the Gyroscope.
bcostm 0:e1d164542663 1068 * @param ReadAddr: Gyroscope's internal address to read from.
bcostm 0:e1d164542663 1069 * @param NumByteToRead: Number of bytes to read from the Gyroscope.
bcostm 0:e1d164542663 1070 * @retval None
bcostm 0:e1d164542663 1071 */
bcostm 0:e1d164542663 1072 void GYRO_IO_Read(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead)
bcostm 0:e1d164542663 1073 {
bcostm 0:e1d164542663 1074 if(NumByteToRead > 0x01)
bcostm 0:e1d164542663 1075 {
bcostm 0:e1d164542663 1076 ReadAddr |= (uint8_t)(READWRITE_CMD | MULTIPLEBYTE_CMD);
bcostm 0:e1d164542663 1077 }
bcostm 0:e1d164542663 1078 else
bcostm 0:e1d164542663 1079 {
bcostm 0:e1d164542663 1080 ReadAddr |= (uint8_t)READWRITE_CMD;
bcostm 0:e1d164542663 1081 }
bcostm 0:e1d164542663 1082 /* Set chip select Low at the start of the transmission */
bcostm 0:e1d164542663 1083 GYRO_CS_LOW();
bcostm 0:e1d164542663 1084
bcostm 0:e1d164542663 1085 /* Send the Address of the indexed register */
bcostm 0:e1d164542663 1086 SPIx_WriteRead(ReadAddr);
bcostm 0:e1d164542663 1087
bcostm 0:e1d164542663 1088 /* Receive the data that will be read from the device (MSB First) */
bcostm 0:e1d164542663 1089 while(NumByteToRead > 0x00)
bcostm 0:e1d164542663 1090 {
bcostm 0:e1d164542663 1091 /* Send dummy byte (0x00) to generate the SPI clock to Gyroscope (Slave device) */
bcostm 0:e1d164542663 1092 *pBuffer = SPIx_WriteRead(DUMMY_BYTE);
bcostm 0:e1d164542663 1093 NumByteToRead--;
bcostm 0:e1d164542663 1094 pBuffer++;
bcostm 0:e1d164542663 1095 }
bcostm 0:e1d164542663 1096
bcostm 0:e1d164542663 1097 /* Set chip select High at the end of the transmission */
bcostm 0:e1d164542663 1098 GYRO_CS_HIGH();
bcostm 0:e1d164542663 1099 }
bcostm 0:e1d164542663 1100
bcostm 0:e1d164542663 1101
bcostm 0:e1d164542663 1102 #ifdef EE_M24LR64
bcostm 0:e1d164542663 1103
bcostm 0:e1d164542663 1104 /******************************** LINK I2C EEPROM *****************************/
bcostm 0:e1d164542663 1105
bcostm 0:e1d164542663 1106 /**
bcostm 0:e1d164542663 1107 * @brief Initializes peripherals used by the I2C EEPROM driver.
bcostm 0:e1d164542663 1108 * @param None
bcostm 0:e1d164542663 1109 * @retval None
bcostm 0:e1d164542663 1110 */
bcostm 0:e1d164542663 1111 void EEPROM_IO_Init(void)
bcostm 0:e1d164542663 1112 {
bcostm 0:e1d164542663 1113 I2Cx_Init();
bcostm 0:e1d164542663 1114 }
bcostm 0:e1d164542663 1115
bcostm 0:e1d164542663 1116 /**
bcostm 0:e1d164542663 1117 * @brief Writes data to I2C EEPROM driver in using DMA channel.
bcostm 0:e1d164542663 1118 * @param DevAddress: Target device address
bcostm 0:e1d164542663 1119 * @param MemAddress: Internal memory address
bcostm 0:e1d164542663 1120 * @param pBuffer: Pointer to data buffer
bcostm 0:e1d164542663 1121 * @param BufferSize: Amount of data to be sent
bcostm 0:e1d164542663 1122 * @retval HAL status
bcostm 0:e1d164542663 1123 */
bcostm 0:e1d164542663 1124 HAL_StatusTypeDef EEPROM_IO_WriteData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize)
bcostm 0:e1d164542663 1125 {
bcostm 0:e1d164542663 1126 return (I2Cx_WriteBufferDMA(DevAddress, MemAddress, pBuffer, BufferSize));
bcostm 0:e1d164542663 1127 }
bcostm 0:e1d164542663 1128
bcostm 0:e1d164542663 1129 /**
bcostm 0:e1d164542663 1130 * @brief Reads data from I2C EEPROM driver in using DMA channel.
bcostm 0:e1d164542663 1131 * @param DevAddress: Target device address
bcostm 0:e1d164542663 1132 * @param MemAddress: Internal memory address
bcostm 0:e1d164542663 1133 * @param pBuffer: Pointer to data buffer
bcostm 0:e1d164542663 1134 * @param BufferSize: Amount of data to be read
bcostm 0:e1d164542663 1135 * @retval HAL status
bcostm 0:e1d164542663 1136 */
bcostm 0:e1d164542663 1137 HAL_StatusTypeDef EEPROM_IO_ReadData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize)
bcostm 0:e1d164542663 1138 {
bcostm 0:e1d164542663 1139 return (I2Cx_ReadBufferDMA(DevAddress, MemAddress, pBuffer, BufferSize));
bcostm 0:e1d164542663 1140 }
bcostm 0:e1d164542663 1141
bcostm 0:e1d164542663 1142 /**
bcostm 0:e1d164542663 1143 * @brief Checks if target device is ready for communication.
bcostm 0:e1d164542663 1144 * @note This function is used with Memory devices
bcostm 0:e1d164542663 1145 * @param DevAddress: Target device address
bcostm 0:e1d164542663 1146 * @param Trials: Number of trials
bcostm 0:e1d164542663 1147 * @retval HAL status
bcostm 0:e1d164542663 1148 */
bcostm 0:e1d164542663 1149 HAL_StatusTypeDef EEPROM_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials)
bcostm 0:e1d164542663 1150 {
bcostm 0:e1d164542663 1151 return (I2Cx_IsDeviceReady(DevAddress, Trials));
bcostm 0:e1d164542663 1152 }
bcostm 0:e1d164542663 1153 #endif /* EE_M24LR64 */
bcostm 0:e1d164542663 1154
bcostm 0:e1d164542663 1155 /**
bcostm 0:e1d164542663 1156 * @}
bcostm 0:e1d164542663 1157 */
bcostm 0:e1d164542663 1158
bcostm 0:e1d164542663 1159 /**
bcostm 0:e1d164542663 1160 * @}
bcostm 0:e1d164542663 1161 */
bcostm 0:e1d164542663 1162
bcostm 0:e1d164542663 1163 /**
bcostm 0:e1d164542663 1164 * @}
bcostm 0:e1d164542663 1165 */
bcostm 0:e1d164542663 1166
bcostm 0:e1d164542663 1167 /**
bcostm 0:e1d164542663 1168 * @}
bcostm 0:e1d164542663 1169 */
bcostm 0:e1d164542663 1170
bcostm 0:e1d164542663 1171 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/