STM32F072B-Discovery BSP User Manual
|
stm32f072b_discovery.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f072b_discovery.c 00004 * @author MCD Application Team 00005 * @brief This file provides set of firmware functions to manage Leds, 00006 * push-button available on STM32F072B-Discovery Kit from STMicroelectronics. 00007 ****************************************************************************** 00008 * @attention 00009 * 00010 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> 00011 * 00012 * Redistribution and use in source and binary forms, with or without modification, 00013 * are permitted provided that the following conditions are met: 00014 * 1. Redistributions of source code must retain the above copyright notice, 00015 * this list of conditions and the following disclaimer. 00016 * 2. Redistributions in binary form must reproduce the above copyright notice, 00017 * this list of conditions and the following disclaimer in the documentation 00018 * and/or other materials provided with the distribution. 00019 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00020 * may be used to endorse or promote products derived from this software 00021 * without specific prior written permission. 00022 * 00023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00024 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00025 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00026 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00027 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00028 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00029 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00031 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00032 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 * 00034 ****************************************************************************** 00035 */ 00036 00037 /* Includes ------------------------------------------------------------------*/ 00038 #include "stm32f072b_discovery.h" 00039 00040 /** @addtogroup BSP 00041 * @{ 00042 */ 00043 00044 /** @addtogroup STM32F072B_DISCOVERY 00045 * @{ 00046 */ 00047 00048 /** @addtogroup STM32F072B_DISCOVERY_Common 00049 * @brief This file provides firmware functions to manage Leds, push-buttons, 00050 * and temperature sensor (LM75) available on 00051 * STM32F072B-DISCO evaluation board from STMicroelectronics. 00052 * @{ 00053 */ 00054 00055 /** @defgroup STM32F072B_DISCOVERY_Private_Constants Private Constants 00056 * @{ 00057 */ 00058 00059 /** 00060 * @brief STM32F072B DISCO BSP Driver version number V2.1.6 00061 */ 00062 #define __STM32F072B_DISCO_BSP_VERSION_MAIN (0x02) /*!< [31:24] main version */ 00063 #define __STM32F072B_DISCO_BSP_VERSION_SUB1 (0x01) /*!< [23:16] sub1 version */ 00064 #define __STM32F072B_DISCO_BSP_VERSION_SUB2 (0x06) /*!< [15:8] sub2 version */ 00065 #define __STM32F072B_DISCO_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */ 00066 #define __STM32F072B_DISCO_BSP_VERSION ((__STM32F072B_DISCO_BSP_VERSION_MAIN << 24)\ 00067 |(__STM32F072B_DISCO_BSP_VERSION_SUB1 << 16)\ 00068 |(__STM32F072B_DISCO_BSP_VERSION_SUB2 << 8 )\ 00069 |(__STM32F072B_DISCO_BSP_VERSION_RC)) 00070 00071 /** 00072 * @} 00073 */ 00074 00075 /** @defgroup STM32F072B_DISCOVERY_Private_Variables Private Variables 00076 * @{ 00077 */ 00078 GPIO_TypeDef* LED_PORT[LEDn] = {LED3_GPIO_PORT, 00079 LED4_GPIO_PORT, 00080 LED5_GPIO_PORT, 00081 LED6_GPIO_PORT}; 00082 00083 const uint16_t LED_PIN[LEDn] = {LED3_PIN, 00084 LED4_PIN, 00085 LED5_PIN, 00086 LED6_PIN}; 00087 00088 GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT}; 00089 const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN}; 00090 const uint8_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn}; 00091 00092 /** 00093 * @brief BUS variables 00094 */ 00095 #if defined(HAL_I2C_MODULE_ENABLED) 00096 I2C_HandleTypeDef I2cHandle; 00097 #endif 00098 00099 #if defined(HAL_SPI_MODULE_ENABLED) 00100 uint32_t SpixTimeout = SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */ 00101 static SPI_HandleTypeDef SpiHandle; 00102 #endif 00103 00104 /** 00105 * @} 00106 */ 00107 00108 00109 /** @defgroup STM32F072B_DISCOVERY_BUS_Operations_Functions Bus Operations functions 00110 * @{ 00111 */ 00112 #if defined(HAL_I2C_MODULE_ENABLED) 00113 /* I2Cx bus function */ 00114 static void I2Cx_Init(void); 00115 static uint32_t I2Cx_WriteData(uint8_t Addr, uint16_t Reg, uint8_t Value); 00116 static uint32_t I2Cx_WriteBuffer(uint8_t Addr, uint16_t Reg, uint8_t *pBuffer, uint16_t Length); 00117 static uint32_t I2Cx_ReadData(uint8_t Addr, uint16_t Reg, uint8_t *Value); 00118 static uint32_t I2Cx_ReadBuffer(uint8_t Addr, uint16_t Reg, uint8_t *pBuffer, uint16_t Length); 00119 static void I2Cx_Error(uint8_t Addr); 00120 #endif /* HAL_I2C_MODULE_ENABLED */ 00121 00122 #if defined(HAL_SPI_MODULE_ENABLED) 00123 /* SPIx bus function */ 00124 static void SPIx_Init(void); 00125 static uint8_t SPIx_WriteRead(uint8_t byte); 00126 static void SPIx_Error (void); 00127 static void SPIx_MspInit(SPI_HandleTypeDef *hspi); 00128 00129 /** 00130 * @} 00131 */ 00132 00133 /** @defgroup STM32F072B_DISCOVERY_LINK_Operations_Functions Link Operations functions 00134 * @{ 00135 */ 00136 00137 /* Link function for GYRO peripheral */ 00138 void GYRO_IO_Init(void); 00139 void GYRO_IO_Write(uint8_t* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite); 00140 void GYRO_IO_Read(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead); 00141 #endif 00142 00143 #if defined(HAL_I2C_MODULE_ENABLED) 00144 /* Link function for I2C EEPROM peripheral */ 00145 void EEPROM_IO_Init(void); 00146 uint32_t EEPROM_IO_WriteData(uint16_t DevAddress, uint16_t MemAddress, uint32_t pBuffer, uint32_t BufferSize); 00147 uint32_t EEPROM_IO_ReadData(uint16_t DevAddress, uint16_t MemAddress, uint32_t pBuffer, uint32_t BufferSize); 00148 HAL_StatusTypeDef EEPROM_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials); 00149 #endif /* HAL_I2C_MODULE_ENABLED */ 00150 00151 /** 00152 * @} 00153 */ 00154 00155 /** @addtogroup STM32F072B_DISCOVERY_Exported_Functions 00156 * @{ 00157 */ 00158 00159 /** 00160 * @brief This method returns the STM32F072B DISCO BSP Driver revision 00161 * @retval version : 0xXYZR (8bits for each decimal, R for RC) 00162 */ 00163 uint32_t BSP_GetVersion(void) 00164 { 00165 return __STM32F072B_DISCO_BSP_VERSION; 00166 } 00167 00168 /** 00169 * @brief Configures LED GPIO. 00170 * @param Led Specifies the Led to be configured. 00171 * This parameter can be one of following parameters: 00172 * @arg LED3 00173 * @arg LED4 00174 * @arg LED5 00175 * @arg LED6 00176 * @retval None 00177 */ 00178 void BSP_LED_Init(Led_TypeDef Led) 00179 { 00180 GPIO_InitTypeDef GPIO_InitStruct; 00181 00182 /* Enable the GPIO_LED Clock */ 00183 LEDx_GPIO_CLK_ENABLE(Led); 00184 00185 /* Configure the GPIO_LED pin */ 00186 GPIO_InitStruct.Pin = LED_PIN[Led]; 00187 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 00188 GPIO_InitStruct.Pull = GPIO_PULLUP; /* GPIO_PuPd_DOWN */ 00189 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 00190 00191 HAL_GPIO_Init(LED_PORT[Led], &GPIO_InitStruct); 00192 00193 HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET); 00194 } 00195 00196 /** 00197 * @brief Turns selected LED On. 00198 * @param Led Specifies the Led to be set on. 00199 * This parameter can be one of following parameters: 00200 * @arg LED3 00201 * @arg LED4 00202 * @arg LED5 00203 * @arg LED6 00204 * @retval None 00205 */ 00206 void BSP_LED_On(Led_TypeDef Led) 00207 { 00208 HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_SET); 00209 } 00210 00211 /** 00212 * @brief Turns selected LED Off. 00213 * @param Led Specifies the Led to be set off. 00214 * This parameter can be one of following parameters: 00215 * @arg LED3 00216 * @arg LED4 00217 * @arg LED5 00218 * @arg LED6 00219 * @retval None 00220 */ 00221 void BSP_LED_Off(Led_TypeDef Led) 00222 { 00223 HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET); 00224 } 00225 00226 /** 00227 * @brief Toggles the selected LED. 00228 * @param Led Specifies the Led to be toggled. 00229 * This parameter can be one of following parameters: 00230 * @arg LED3 00231 * @arg LED4 00232 * @arg LED5 00233 * @arg LED6 00234 * @retval None 00235 */ 00236 void BSP_LED_Toggle(Led_TypeDef Led) 00237 { 00238 HAL_GPIO_TogglePin(LED_PORT[Led], LED_PIN[Led]); 00239 } 00240 00241 /** 00242 * @brief Configures Button GPIO and EXTI Line. 00243 * @param Button Specifies the Button to be configured. 00244 * This parameter should be: BUTTON_USER 00245 * @param Mode Specifies Button mode. 00246 * This parameter can be one of following parameters: 00247 * @arg BUTTON_MODE_GPIO: Button will be used as simple IO 00248 * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt 00249 * generation capability 00250 * @retval None 00251 */ 00252 void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef Mode) 00253 { 00254 GPIO_InitTypeDef GPIO_InitStruct; 00255 00256 /* Enable the BUTTON Clock */ 00257 BUTTONx_GPIO_CLK_ENABLE(Button); 00258 __HAL_RCC_SYSCFG_CLK_ENABLE(); 00259 00260 GPIO_InitStruct.Pin = BUTTON_PIN[Button]; 00261 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 00262 00263 if(Mode == BUTTON_MODE_GPIO) 00264 { 00265 /* Configure Button pin as input */ 00266 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 00267 GPIO_InitStruct.Pull = GPIO_PULLDOWN; 00268 HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct); 00269 } 00270 00271 if(Mode == BUTTON_MODE_EXTI) 00272 { 00273 /* Configure Button pin as input with External interrupt */ 00274 GPIO_InitStruct.Pull = GPIO_NOPULL; 00275 GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; 00276 HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct); 00277 00278 /* Enable and set Button EXTI Interrupt to the lowest priority */ 00279 HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x03, 0x00); 00280 HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button])); 00281 } 00282 } 00283 00284 /** 00285 * @brief Returns the selected Push Button state. 00286 * @param Button Specifies the Button to be checked. 00287 * This parameter should be: BUTTON_USER 00288 * @retval The Button GPIO pin value. 00289 */ 00290 uint32_t BSP_PB_GetState(Button_TypeDef Button) 00291 { 00292 return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]); 00293 } 00294 00295 /** 00296 * @} 00297 */ 00298 00299 /** @addtogroup STM32F072B_DISCOVERY_BUS_Operations_Functions 00300 * @{ 00301 */ 00302 00303 /****************************************************************************** 00304 BUS OPERATIONS 00305 *******************************************************************************/ 00306 #if defined(HAL_I2C_MODULE_ENABLED) 00307 /******************************* I2C Routines**********************************/ 00308 00309 /** 00310 * @brief Discovery I2Cx MSP Initialization 00311 * @param hi2c I2C handle 00312 * @retval None 00313 */ 00314 static void I2Cx_MspInit(I2C_HandleTypeDef *hi2c) 00315 { 00316 GPIO_InitTypeDef GPIO_InitStruct; 00317 static DMA_HandleTypeDef hdma_tx; 00318 static DMA_HandleTypeDef hdma_rx; 00319 00320 /* Enable GPIO clock */ 00321 DISCOVERY_I2Cx_GPIO_CLK_ENABLE(); 00322 00323 /* Configure I2C Tx and Rx as alternate function */ 00324 GPIO_InitStruct.Pin = (DISCOVERY_I2Cx_SCL_PIN | DISCOVERY_I2Cx_SDA_PIN); 00325 GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; 00326 GPIO_InitStruct.Pull = GPIO_NOPULL; 00327 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 00328 GPIO_InitStruct.Alternate = DISCOVERY_I2Cx_AF; 00329 HAL_GPIO_Init(DISCOVERY_I2Cx_GPIO_PORT, &GPIO_InitStruct); 00330 00331 /* Enable the DMA clock */ 00332 DISCOVERY_EEPROM_DMA_CLK_ENABLE(); 00333 00334 /* Configure the DMA channel for the EEPROM peripheral TX direction */ 00335 /* Configure the DMA channel */ 00336 hdma_tx.Instance = DISCOVERY_EEPROM_DMA_CHANNEL_TX; 00337 /* Set the parameters to be configured */ 00338 hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; 00339 hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE; 00340 hdma_tx.Init.MemInc = DMA_MINC_ENABLE; 00341 hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; 00342 hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; 00343 hdma_tx.Init.Mode = DMA_NORMAL; 00344 hdma_tx.Init.Priority = DMA_PRIORITY_VERY_HIGH; 00345 00346 /* Associate the initilalized hdma_rx handle to the the husart handle*/ 00347 __HAL_LINKDMA(hi2c, hdmatx, hdma_tx); 00348 00349 /* Configure the DMA Stream */ 00350 HAL_DMA_Init(&hdma_tx); 00351 00352 /* Configure and enable DMA TX Channel interrupt */ 00353 HAL_NVIC_SetPriority((IRQn_Type)(DISCOVERY_EEPROM_DMA_TX_IRQn), DISCOVERY_EEPROM_DMA_PREPRIO, DISCOVERY_EEPROM_DMA_SUBPRIO); 00354 HAL_NVIC_EnableIRQ((IRQn_Type)(DISCOVERY_EEPROM_DMA_TX_IRQn)); 00355 00356 /* Configure the DMA channel for the EEPROM peripheral RX direction */ 00357 /* Configure the DMA channel */ 00358 hdma_rx.Instance = DISCOVERY_EEPROM_DMA_CHANNEL_RX; 00359 /* Set the parameters to be configured */ 00360 hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; 00361 hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE; 00362 hdma_rx.Init.MemInc = DMA_MINC_ENABLE; 00363 hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; 00364 hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; 00365 hdma_rx.Init.Mode = DMA_NORMAL; 00366 hdma_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH; 00367 00368 /* Associate the initilalized hdma_rx handle to the the husart handle*/ 00369 __HAL_LINKDMA(hi2c, hdmarx, hdma_rx); 00370 00371 /* Configure the DMA Stream */ 00372 HAL_DMA_Init(&hdma_rx); 00373 00374 /* Configure and enable DMA RX Channel interrupt */ 00375 HAL_NVIC_SetPriority((IRQn_Type)(DISCOVERY_EEPROM_DMA_RX_IRQn), DISCOVERY_EEPROM_DMA_PREPRIO, DISCOVERY_EEPROM_DMA_SUBPRIO); 00376 HAL_NVIC_EnableIRQ((IRQn_Type)(DISCOVERY_EEPROM_DMA_RX_IRQn)); 00377 00378 /* Enable I2C clock */ 00379 DISCOVERY_I2Cx_CLK_ENABLE(); 00380 } 00381 00382 /** 00383 * @brief Discovery I2Cx Bus initialization 00384 * @retval None 00385 */ 00386 static void I2Cx_Init(void) 00387 { 00388 if(HAL_I2C_GetState(&I2cHandle) == HAL_I2C_STATE_RESET) 00389 { 00390 I2cHandle.Instance = DISCOVERY_I2Cx; 00391 I2cHandle.Init.Timing = DISCOVERY_I2Cx_TIMING; 00392 I2cHandle.Init.OwnAddress1 = 0; 00393 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; 00394 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; 00395 I2cHandle.Init.OwnAddress2 = 0; 00396 I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK; 00397 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; 00398 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; 00399 00400 /* Init the I2C */ 00401 I2Cx_MspInit(&I2cHandle); 00402 HAL_I2C_Init(&I2cHandle); 00403 } 00404 } 00405 00406 /** 00407 * @brief Write a value in a register of the device through BUS. 00408 * @param Addr Device address on BUS Bus. 00409 * @param Reg The target register address to write 00410 * @param Value The target register value to be written 00411 * @retval None 00412 */ 00413 static uint32_t I2Cx_WriteData(uint8_t Addr, uint16_t Reg, uint8_t Value) 00414 { 00415 HAL_StatusTypeDef status = HAL_OK; 00416 00417 status = HAL_I2C_Mem_Write(&I2cHandle, Addr, Reg, I2C_MEMADD_SIZE_16BIT, &Value, 1, I2Cx_TIMEOUT_MAX); 00418 00419 /* Check the communication status */ 00420 if(status != HAL_OK) 00421 { 00422 /* Execute user timeout callback */ 00423 I2Cx_Error(Addr); 00424 return HAL_ERROR; 00425 } 00426 return HAL_OK; 00427 } 00428 00429 /** 00430 * @brief Write a value in a register of the device through BUS. 00431 * @param Addr Device address on BUS Bus. 00432 * @param Reg The target register address to write 00433 * @param pBuffer The target register value to be written 00434 * @param Length buffer size to be written 00435 * @retval None 00436 */ 00437 static uint32_t I2Cx_WriteBuffer(uint8_t Addr, uint16_t Reg, uint8_t *pBuffer, uint16_t Length) 00438 { 00439 HAL_StatusTypeDef status = HAL_OK; 00440 00441 status = HAL_I2C_Mem_Write_DMA(&I2cHandle, Addr, Reg, I2C_MEMADD_SIZE_16BIT, pBuffer, Length); 00442 00443 /* Check the communication status */ 00444 if(status != HAL_OK) 00445 { 00446 /* Execute user timeout callback */ 00447 I2Cx_Error(Addr); 00448 return HAL_ERROR; 00449 } 00450 return HAL_OK; 00451 } 00452 00453 /** 00454 * @brief Read a register of the device through BUS 00455 * @param Addr Device address on BUS 00456 * @param Reg The target register address to read 00457 * @retval Value: read register value 00458 */ 00459 static uint32_t I2Cx_ReadData(uint8_t Addr, uint16_t Reg, uint8_t *Value) 00460 { 00461 HAL_StatusTypeDef status = HAL_OK; 00462 00463 status = HAL_I2C_Mem_Read(&I2cHandle, Addr, Reg, I2C_MEMADD_SIZE_16BIT, Value, 1, I2Cx_TIMEOUT_MAX); 00464 00465 /* Check the communication status */ 00466 if(status != HAL_OK) 00467 { 00468 /* Execute user timeout callback */ 00469 I2Cx_Error(Addr); 00470 return HAL_ERROR; 00471 } 00472 return HAL_OK; 00473 } 00474 00475 /** 00476 * @brief Reads multiple data on the BUS. 00477 * @param Addr I2C Address 00478 * @param Reg Reg Address 00479 * @param pBuffer pointer to read data buffer 00480 * @param Length length of the data 00481 * @retval 0 if no problems to read multiple data 00482 */ 00483 static uint32_t I2Cx_ReadBuffer(uint8_t Addr, uint16_t Reg, uint8_t *pBuffer, uint16_t Length) 00484 { 00485 HAL_StatusTypeDef status = HAL_OK; 00486 00487 status = HAL_I2C_Mem_Read_DMA(&I2cHandle, Addr, Reg, I2C_MEMADD_SIZE_16BIT, pBuffer, Length); 00488 00489 /* Check the communication status */ 00490 if(status != HAL_OK) 00491 { 00492 /* Execute user timeout callback */ 00493 I2Cx_Error(Addr); 00494 return HAL_ERROR; 00495 } 00496 return HAL_OK; 00497 } 00498 00499 /** 00500 * @brief Discovery I2Cx error treatment function 00501 * @retval None 00502 */ 00503 static void I2Cx_Error (uint8_t Addr) 00504 { 00505 /* De-initialize the I2C comunication BUS */ 00506 HAL_I2C_DeInit(&I2cHandle); 00507 00508 /* Re- Initiaize the I2C comunication BUS */ 00509 I2Cx_Init(); 00510 } 00511 #endif /* HAL_I2C_MODULE_ENABLED */ 00512 00513 #if defined(HAL_SPI_MODULE_ENABLED) 00514 /******************************* SPI Routines**********************************/ 00515 /** 00516 * @brief SPI1 Bus initialization 00517 * @retval None 00518 */ 00519 static void SPIx_Init(void) 00520 { 00521 if(HAL_SPI_GetState(&SpiHandle) == HAL_SPI_STATE_RESET) 00522 { 00523 /* SPI Config */ 00524 SpiHandle.Instance = DISCOVERY_SPIx; 00525 /* SPI baudrate is set to 5.6 MHz (PCLK2/SPI_BaudRatePrescaler = 90/16 = 5.625 MHz) 00526 to verify these constraints: 00527 l3gd20 SPI interface max baudrate is 10MHz for write/read 00528 PCLK2 frequency is set to 90 MHz 00529 */ 00530 SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; 00531 SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; 00532 SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE; 00533 SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW; 00534 SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; 00535 SpiHandle.Init.CRCPolynomial = 7; 00536 SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT; 00537 SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; 00538 SpiHandle.Init.NSS = SPI_NSS_SOFT; 00539 SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE; 00540 SpiHandle.Init.Mode = SPI_MODE_MASTER; 00541 00542 SPIx_MspInit(&SpiHandle); 00543 HAL_SPI_Init(&SpiHandle); 00544 } 00545 } 00546 00547 /** 00548 * @brief Sends a Byte through the SPI interface and return the Byte received 00549 * from the SPI bus. 00550 * @param Byte Byte send. 00551 * @retval The received byte value 00552 */ 00553 static uint8_t SPIx_WriteRead(uint8_t Byte) 00554 { 00555 00556 uint8_t receivedbyte = 0; 00557 00558 /* Send a Byte through the SPI peripheral */ 00559 /* Read byte from the SPI bus */ 00560 if(HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t*) &Byte, (uint8_t*) &receivedbyte, 1, SpixTimeout) != HAL_OK) 00561 { 00562 SPIx_Error(); 00563 } 00564 00565 return receivedbyte; 00566 } 00567 00568 00569 /** 00570 * @brief SPI1 error treatment function 00571 * @retval None 00572 */ 00573 static void SPIx_Error (void) 00574 { 00575 /* De-initialize the SPI comunication BUS */ 00576 HAL_SPI_DeInit(&SpiHandle); 00577 00578 /* Re- Initiaize the SPI comunication BUS */ 00579 SPIx_Init(); 00580 } 00581 00582 00583 /** 00584 * @brief SPI MSP Init 00585 * @param hspi SPI handle 00586 * @retval None 00587 */ 00588 static void SPIx_MspInit(SPI_HandleTypeDef *hspi) 00589 { 00590 GPIO_InitTypeDef GPIO_InitStructure; 00591 00592 /* Enable SPI2 clock */ 00593 DISCOVERY_SPIx_CLOCK_ENABLE(); 00594 00595 /* enable SPI2 gpio clock */ 00596 DISCOVERY_SPIx_GPIO_CLK_ENABLE(); 00597 00598 /* configure SPI5 SCK, MOSI and MISO */ 00599 GPIO_InitStructure.Pin = (DISCOVERY_SPIx_SCK_PIN | DISCOVERY_SPIx_MOSI_PIN | DISCOVERY_SPIx_MISO_PIN); 00600 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; 00601 GPIO_InitStructure.Pull = GPIO_NOPULL; /*GPIO_PULLDOWN*/ 00602 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH; 00603 GPIO_InitStructure.Alternate = DISCOVERY_SPIx_AF; 00604 HAL_GPIO_Init(DISCOVERY_SPIx_GPIO_PORT, &GPIO_InitStructure); 00605 } 00606 00607 /** 00608 * @} 00609 */ 00610 00611 /** @addtogroup STM32F072B_DISCOVERY_LINK_Operations_Functions 00612 * @{ 00613 */ 00614 00615 /****************************************************************************** 00616 LINK OPERATIONS 00617 *******************************************************************************/ 00618 00619 /********************************* LINK GYRO *****************************/ 00620 /** 00621 * @brief Configures the GYRO SPI interface. 00622 * @retval None 00623 */ 00624 void GYRO_IO_Init(void) 00625 { 00626 GPIO_InitTypeDef GPIO_InitStructure; 00627 00628 /* Configure the Gyroscope Control pins ------------------------------------------*/ 00629 /* Enable CS GPIO clock and Configure GPIO PIN for Gyroscope Chip select */ 00630 GYRO_CS_GPIO_CLK_ENABLE(); 00631 GPIO_InitStructure.Pin = GYRO_CS_PIN; 00632 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; 00633 GPIO_InitStructure.Pull = GPIO_NOPULL; 00634 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH; 00635 HAL_GPIO_Init(GYRO_CS_GPIO_PORT, &GPIO_InitStructure); 00636 00637 /* Deselect : Chip Select high */ 00638 GYRO_CS_HIGH(); 00639 00640 /* Enable INT1, INT2 GPIO clock and Configure GPIO PINs to detect Interrupts */ 00641 GYRO_INT_GPIO_CLK_ENABLE(); 00642 GPIO_InitStructure.Pin = GYRO_INT1_PIN | GYRO_INT2_PIN; 00643 GPIO_InitStructure.Mode = GPIO_MODE_INPUT; 00644 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH; 00645 GPIO_InitStructure.Pull= GPIO_NOPULL; 00646 HAL_GPIO_Init(GYRO_INT_GPIO_PORT, &GPIO_InitStructure); 00647 00648 SPIx_Init(); 00649 } 00650 00651 /** 00652 * @brief Writes one byte to the GYRO. 00653 * @param pBuffer pointer to the buffer containing the data to be written to the GYRO. 00654 * @param WriteAddr GYRO's internal address to write to. 00655 * @param NumByteToWrite Number of bytes to write. 00656 * @retval None 00657 */ 00658 void GYRO_IO_Write(uint8_t* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite) 00659 { 00660 /* Configure the MS bit: 00661 - When 0, the address will remain unchanged in multiple read/write commands. 00662 - When 1, the address will be auto incremented in multiple read/write commands. 00663 */ 00664 if(NumByteToWrite > 0x01) 00665 { 00666 WriteAddr |= (uint8_t)MULTIPLEBYTE_CMD; 00667 } 00668 /* Set chip select Low at the start of the transmission */ 00669 GYRO_CS_LOW(); 00670 00671 /* Send the Address of the indexed register */ 00672 SPIx_WriteRead(WriteAddr); 00673 00674 /* Send the data that will be written into the device (MSB First) */ 00675 while(NumByteToWrite >= 0x01) 00676 { 00677 SPIx_WriteRead(*pBuffer); 00678 NumByteToWrite--; 00679 pBuffer++; 00680 } 00681 00682 /* Set chip select High at the end of the transmission */ 00683 GYRO_CS_HIGH(); 00684 } 00685 00686 /** 00687 * @brief Reads a block of data from the GYROSCOPE. 00688 * @param pBuffer pointer to the buffer that receives the data read from the GYROSCOPE. 00689 * @param ReadAddr GYROSCOPE's internal address to read from. 00690 * @param NumByteToRead number of bytes to read from the GYROSCOPE. 00691 * @retval None 00692 */ 00693 void GYRO_IO_Read(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead) 00694 { 00695 if(NumByteToRead > 0x01) 00696 { 00697 ReadAddr |= (uint8_t)(READWRITE_CMD | MULTIPLEBYTE_CMD); 00698 } 00699 else 00700 { 00701 ReadAddr |= (uint8_t)READWRITE_CMD; 00702 } 00703 /* Set chip select Low at the start of the transmission */ 00704 GYRO_CS_LOW(); 00705 00706 /* Send the Address of the indexed register */ 00707 SPIx_WriteRead(ReadAddr); 00708 00709 /* Receive the data that will be read from the device (MSB First) */ 00710 while(NumByteToRead > 0x00) 00711 { 00712 /* Send dummy byte (0x00) to generate the SPI clock to GYROSCOPE (Slave device) */ 00713 *pBuffer = SPIx_WriteRead(DUMMY_BYTE); 00714 NumByteToRead--; 00715 pBuffer++; 00716 } 00717 00718 /* Set chip select High at the end of the transmission */ 00719 GYRO_CS_HIGH(); 00720 } 00721 #endif /* HAL_SPI_MODULE_ENABLED */ 00722 00723 #if defined(HAL_I2C_MODULE_ENABLED) 00724 /********************************* LINK I2C EEPROM *****************************/ 00725 /** 00726 * @brief Initializes peripherals used by the I2C EEPROM driver. 00727 * @retval None 00728 */ 00729 void EEPROM_IO_Init(void) 00730 { 00731 I2Cx_Init(); 00732 } 00733 00734 /** 00735 * @brief Write data to I2C EEPROM driver in using DMA channel 00736 * @param DevAddress Target device address 00737 * @param MemAddress Internal memory address 00738 * @param pBuffer Pointer to data buffer 00739 * @param BufferSize Amount of data to be sent 00740 * @retval HAL status 00741 */ 00742 uint32_t EEPROM_IO_WriteData(uint16_t DevAddress, uint16_t MemAddress, uint32_t pBuffer, uint32_t BufferSize) 00743 { 00744 if (BufferSize == 1) 00745 { 00746 return (I2Cx_WriteData(DevAddress, MemAddress, *((uint8_t*)pBuffer))); 00747 } 00748 else 00749 { 00750 return (I2Cx_WriteBuffer(DevAddress, MemAddress, (uint8_t*)pBuffer, (uint32_t)BufferSize)); 00751 } 00752 } 00753 00754 /** 00755 * @brief Read data from I2C EEPROM driver in using DMA channel 00756 * @param DevAddress Target device address 00757 * @param MemAddress Internal memory address 00758 * @param pBuffer Pointer to data buffer 00759 * @param BufferSize Amount of data to be read 00760 * @retval HAL status 00761 */ 00762 uint32_t EEPROM_IO_ReadData(uint16_t DevAddress, uint16_t MemAddress, uint32_t pBuffer, uint32_t BufferSize) 00763 { 00764 if (BufferSize == 1) 00765 { 00766 return (I2Cx_ReadData(DevAddress, MemAddress, (uint8_t*)pBuffer)); 00767 } 00768 else 00769 { 00770 return (I2Cx_ReadBuffer(DevAddress, MemAddress, (uint8_t*)pBuffer, (uint32_t)BufferSize)); 00771 } 00772 } 00773 00774 /** 00775 * @brief Checks if target device is ready for communication. 00776 * @note This function is used with Memory devices 00777 * @param DevAddress Target device address 00778 * @param Trials Number of trials 00779 * @retval HAL status 00780 */ 00781 HAL_StatusTypeDef EEPROM_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials) 00782 { 00783 HAL_Delay(5); 00784 return (HAL_I2C_IsDeviceReady(&I2cHandle, DevAddress, Trials, I2Cx_TIMEOUT_MAX)); 00785 } 00786 #endif /* HAL_I2C_MODULE_ENABLED */ 00787 00788 /** 00789 * @} 00790 */ 00791 00792 /** 00793 * @} 00794 */ 00795 00796 /** 00797 * @} 00798 */ 00799 00800 /** 00801 * @} 00802 */ 00803 00804 /** 00805 * @} 00806 */ 00807 00808 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Wed Jul 5 2017 09:43:19 for STM32F072B-Discovery BSP User Manual by
