TUKS MCU Introductory course / TUKS-COURSE-TIMER
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_gyroscope.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 functions needed to manage the L3GD20
elmot 1:d0dfbce63a89 8 * MEMS accelerometer available on STM32L476G-Discovery board.
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_gyroscope.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 /** @addtogroup STM32L476G_DISCOVERY
elmot 1:d0dfbce63a89 47 * @{
elmot 1:d0dfbce63a89 48 */
elmot 1:d0dfbce63a89 49
elmot 1:d0dfbce63a89 50 /** @defgroup STM32L476G_DISCOVERY_GYROSCOPE STM32L476G-DISCOVERY GYROSCOPE
elmot 1:d0dfbce63a89 51 * @{
elmot 1:d0dfbce63a89 52 */
elmot 1:d0dfbce63a89 53
elmot 1:d0dfbce63a89 54 /* Private typedef -----------------------------------------------------------*/
elmot 1:d0dfbce63a89 55 /** @defgroup STM32L476G_DISCOVERY_GYROSCOPE_Private_Types Private Types
elmot 1:d0dfbce63a89 56 * @{
elmot 1:d0dfbce63a89 57 */
elmot 1:d0dfbce63a89 58 /**
elmot 1:d0dfbce63a89 59 * @}
elmot 1:d0dfbce63a89 60 */
elmot 1:d0dfbce63a89 61
elmot 1:d0dfbce63a89 62 /* Private defines ------------------------------------------------------------*/
elmot 1:d0dfbce63a89 63 /** @defgroup STM32L476G_DISCOVERY_GYROSCOPE_Private_Constants Private Constants
elmot 1:d0dfbce63a89 64 * @{
elmot 1:d0dfbce63a89 65 */
elmot 1:d0dfbce63a89 66 /**
elmot 1:d0dfbce63a89 67 * @}
elmot 1:d0dfbce63a89 68 */
elmot 1:d0dfbce63a89 69
elmot 1:d0dfbce63a89 70 /* Private macros ------------------------------------------------------------*/
elmot 1:d0dfbce63a89 71 /** @defgroup STM32L476G_DISCOVERY_GYROSCOPE_Private_Macros Private Macros
elmot 1:d0dfbce63a89 72 * @{
elmot 1:d0dfbce63a89 73 */
elmot 1:d0dfbce63a89 74 /**
elmot 1:d0dfbce63a89 75 * @}
elmot 1:d0dfbce63a89 76 */
elmot 1:d0dfbce63a89 77
elmot 1:d0dfbce63a89 78 /* Private variables ---------------------------------------------------------*/
elmot 1:d0dfbce63a89 79 /** @defgroup STM32L476G_DISCOVERY_GYROSCOPE_Private_Variables Private Variables
elmot 1:d0dfbce63a89 80 * @{
elmot 1:d0dfbce63a89 81 */
elmot 1:d0dfbce63a89 82 static GYRO_DrvTypeDef *GyroscopeDrv;
elmot 1:d0dfbce63a89 83
elmot 1:d0dfbce63a89 84 /**
elmot 1:d0dfbce63a89 85 * @}
elmot 1:d0dfbce63a89 86 */
elmot 1:d0dfbce63a89 87
elmot 1:d0dfbce63a89 88 /* Private function prototypes -----------------------------------------------*/
elmot 1:d0dfbce63a89 89 /** @defgroup STM32L476G_DISCOVERY_GYROSCOPE_Private_FunctionPrototypes Private Functions
elmot 1:d0dfbce63a89 90 * @{
elmot 1:d0dfbce63a89 91 */
elmot 1:d0dfbce63a89 92 /**
elmot 1:d0dfbce63a89 93 * @}
elmot 1:d0dfbce63a89 94 */
elmot 1:d0dfbce63a89 95
elmot 1:d0dfbce63a89 96 /* Exported functions --------------------------------------------------------*/
elmot 1:d0dfbce63a89 97 /** @addtogroup STM32L476G_DISCOVERY_GYROSCOPE_Exported_Functions
elmot 1:d0dfbce63a89 98 * @{
elmot 1:d0dfbce63a89 99 */
elmot 1:d0dfbce63a89 100
elmot 1:d0dfbce63a89 101 /**
elmot 1:d0dfbce63a89 102 * @brief Initialize Gyroscope.
elmot 1:d0dfbce63a89 103 * @retval GYRO_OK or GYRO_ERROR
elmot 1:d0dfbce63a89 104 */
elmot 1:d0dfbce63a89 105 uint8_t BSP_GYRO_Init(void)
elmot 1:d0dfbce63a89 106 {
elmot 1:d0dfbce63a89 107 uint8_t ret = GYRO_ERROR;
elmot 1:d0dfbce63a89 108 uint16_t ctrl = 0x0000;
elmot 1:d0dfbce63a89 109 GYRO_InitTypeDef L3GD20_InitStructure;
elmot 1:d0dfbce63a89 110 GYRO_FilterConfigTypeDef L3GD20_FilterStructure={0,0};
elmot 1:d0dfbce63a89 111
elmot 1:d0dfbce63a89 112 if((L3gd20Drv.ReadID() == I_AM_L3GD20) || (L3gd20Drv.ReadID() == I_AM_L3GD20_TR))
elmot 1:d0dfbce63a89 113 {
elmot 1:d0dfbce63a89 114 /* Initialize the gyroscope driver structure */
elmot 1:d0dfbce63a89 115 GyroscopeDrv = &L3gd20Drv;
elmot 1:d0dfbce63a89 116
elmot 1:d0dfbce63a89 117 /* Configure Mems : data rate, power mode, full scale and axes */
elmot 1:d0dfbce63a89 118 L3GD20_InitStructure.Power_Mode = L3GD20_MODE_ACTIVE;
elmot 1:d0dfbce63a89 119 L3GD20_InitStructure.Output_DataRate = L3GD20_OUTPUT_DATARATE_1;
elmot 1:d0dfbce63a89 120 L3GD20_InitStructure.Axes_Enable = L3GD20_AXES_ENABLE;
elmot 1:d0dfbce63a89 121 L3GD20_InitStructure.Band_Width = L3GD20_BANDWIDTH_4;
elmot 1:d0dfbce63a89 122 L3GD20_InitStructure.BlockData_Update = L3GD20_BlockDataUpdate_Continous;
elmot 1:d0dfbce63a89 123 L3GD20_InitStructure.Endianness = L3GD20_BLE_LSB;
elmot 1:d0dfbce63a89 124 L3GD20_InitStructure.Full_Scale = L3GD20_FULLSCALE_500;
elmot 1:d0dfbce63a89 125
elmot 1:d0dfbce63a89 126 /* Configure MEMS: data rate, power mode, full scale and axes */
elmot 1:d0dfbce63a89 127 ctrl = (uint16_t) (L3GD20_InitStructure.Power_Mode | L3GD20_InitStructure.Output_DataRate | \
elmot 1:d0dfbce63a89 128 L3GD20_InitStructure.Axes_Enable | L3GD20_InitStructure.Band_Width);
elmot 1:d0dfbce63a89 129
elmot 1:d0dfbce63a89 130 ctrl |= (uint16_t) ((L3GD20_InitStructure.BlockData_Update | L3GD20_InitStructure.Endianness | \
elmot 1:d0dfbce63a89 131 L3GD20_InitStructure.Full_Scale) << 8);
elmot 1:d0dfbce63a89 132
elmot 1:d0dfbce63a89 133 /* Initialize component */
elmot 1:d0dfbce63a89 134 GyroscopeDrv->Init(ctrl);
elmot 1:d0dfbce63a89 135
elmot 1:d0dfbce63a89 136 L3GD20_FilterStructure.HighPassFilter_Mode_Selection =L3GD20_HPM_NORMAL_MODE_RES;
elmot 1:d0dfbce63a89 137 L3GD20_FilterStructure.HighPassFilter_CutOff_Frequency = L3GD20_HPFCF_0;
elmot 1:d0dfbce63a89 138
elmot 1:d0dfbce63a89 139 ctrl = (uint8_t) ((L3GD20_FilterStructure.HighPassFilter_Mode_Selection |\
elmot 1:d0dfbce63a89 140 L3GD20_FilterStructure.HighPassFilter_CutOff_Frequency));
elmot 1:d0dfbce63a89 141
elmot 1:d0dfbce63a89 142 /* Configure component filter */
elmot 1:d0dfbce63a89 143 GyroscopeDrv->FilterConfig(ctrl) ;
elmot 1:d0dfbce63a89 144
elmot 1:d0dfbce63a89 145 /* Enable component filter */
elmot 1:d0dfbce63a89 146 GyroscopeDrv->FilterCmd(L3GD20_HIGHPASSFILTER_ENABLE);
elmot 1:d0dfbce63a89 147
elmot 1:d0dfbce63a89 148 ret = GYRO_OK;
elmot 1:d0dfbce63a89 149 }
elmot 1:d0dfbce63a89 150 else
elmot 1:d0dfbce63a89 151 {
elmot 1:d0dfbce63a89 152 ret = GYRO_ERROR;
elmot 1:d0dfbce63a89 153 }
elmot 1:d0dfbce63a89 154
elmot 1:d0dfbce63a89 155 return ret;
elmot 1:d0dfbce63a89 156 }
elmot 1:d0dfbce63a89 157
elmot 1:d0dfbce63a89 158
elmot 1:d0dfbce63a89 159 /**
elmot 1:d0dfbce63a89 160 * @brief DeInitialize Gyroscope.
elmot 1:d0dfbce63a89 161 * @retval None
elmot 1:d0dfbce63a89 162 */
elmot 1:d0dfbce63a89 163 void BSP_GYRO_DeInit(void)
elmot 1:d0dfbce63a89 164 {
elmot 1:d0dfbce63a89 165 GYRO_IO_DeInit();
elmot 1:d0dfbce63a89 166 }
elmot 1:d0dfbce63a89 167
elmot 1:d0dfbce63a89 168
elmot 1:d0dfbce63a89 169 /**
elmot 1:d0dfbce63a89 170 * @brief Put Gyroscope in low power mode.
elmot 1:d0dfbce63a89 171 * @retval None
elmot 1:d0dfbce63a89 172 */
elmot 1:d0dfbce63a89 173 void BSP_GYRO_LowPower(void)
elmot 1:d0dfbce63a89 174 {
elmot 1:d0dfbce63a89 175 uint16_t ctrl = 0x0000;
elmot 1:d0dfbce63a89 176 GYRO_InitTypeDef L3GD20_InitStructure;
elmot 1:d0dfbce63a89 177
elmot 1:d0dfbce63a89 178 /* configure only Power_Mode field */
elmot 1:d0dfbce63a89 179 L3GD20_InitStructure.Power_Mode = L3GD20_MODE_POWERDOWN;
elmot 1:d0dfbce63a89 180
elmot 1:d0dfbce63a89 181 ctrl = (uint16_t) (L3GD20_InitStructure.Power_Mode);
elmot 1:d0dfbce63a89 182
elmot 1:d0dfbce63a89 183 /* Set component in low-power mode */
elmot 1:d0dfbce63a89 184 GyroscopeDrv->LowPower(ctrl);
elmot 1:d0dfbce63a89 185
elmot 1:d0dfbce63a89 186
elmot 1:d0dfbce63a89 187 }
elmot 1:d0dfbce63a89 188
elmot 1:d0dfbce63a89 189 /**
elmot 1:d0dfbce63a89 190 * @brief Read ID of Gyroscope component.
elmot 1:d0dfbce63a89 191 * @retval ID
elmot 1:d0dfbce63a89 192 */
elmot 1:d0dfbce63a89 193 uint8_t BSP_GYRO_ReadID(void)
elmot 1:d0dfbce63a89 194 {
elmot 1:d0dfbce63a89 195 uint8_t id = 0x00;
elmot 1:d0dfbce63a89 196
elmot 1:d0dfbce63a89 197 if(GyroscopeDrv->ReadID != NULL)
elmot 1:d0dfbce63a89 198 {
elmot 1:d0dfbce63a89 199 id = GyroscopeDrv->ReadID();
elmot 1:d0dfbce63a89 200 }
elmot 1:d0dfbce63a89 201 return id;
elmot 1:d0dfbce63a89 202 }
elmot 1:d0dfbce63a89 203
elmot 1:d0dfbce63a89 204 /**
elmot 1:d0dfbce63a89 205 * @brief Reboot memory content of Gyroscope.
elmot 1:d0dfbce63a89 206 * @retval None
elmot 1:d0dfbce63a89 207 */
elmot 1:d0dfbce63a89 208 void BSP_GYRO_Reset(void)
elmot 1:d0dfbce63a89 209 {
elmot 1:d0dfbce63a89 210 if(GyroscopeDrv->Reset != NULL)
elmot 1:d0dfbce63a89 211 {
elmot 1:d0dfbce63a89 212 GyroscopeDrv->Reset();
elmot 1:d0dfbce63a89 213 }
elmot 1:d0dfbce63a89 214 }
elmot 1:d0dfbce63a89 215
elmot 1:d0dfbce63a89 216 /**
elmot 1:d0dfbce63a89 217 * @brief Configure Gyroscope interrupts (INT1 or INT2).
elmot 1:d0dfbce63a89 218 * @param pIntConfig: pointer to a GYRO_InterruptConfigTypeDef
elmot 1:d0dfbce63a89 219 * structure that contains the configuration setting for the L3GD20 Interrupt.
elmot 1:d0dfbce63a89 220 * @retval None
elmot 1:d0dfbce63a89 221 */
elmot 1:d0dfbce63a89 222 void BSP_GYRO_ITConfig(GYRO_InterruptConfigTypeDef *pIntConfig)
elmot 1:d0dfbce63a89 223 {
elmot 1:d0dfbce63a89 224 uint16_t interruptconfig = 0x0000;
elmot 1:d0dfbce63a89 225
elmot 1:d0dfbce63a89 226 if(GyroscopeDrv->ConfigIT != NULL)
elmot 1:d0dfbce63a89 227 {
elmot 1:d0dfbce63a89 228 /* Configure latch Interrupt request and axe interrupts */
elmot 1:d0dfbce63a89 229 interruptconfig |= ((uint8_t)(pIntConfig->Latch_Request| \
elmot 1:d0dfbce63a89 230 pIntConfig->Interrupt_Axes) << 8);
elmot 1:d0dfbce63a89 231
elmot 1:d0dfbce63a89 232 interruptconfig |= (uint8_t)(pIntConfig->Interrupt_ActiveEdge);
elmot 1:d0dfbce63a89 233
elmot 1:d0dfbce63a89 234 GyroscopeDrv->ConfigIT(interruptconfig);
elmot 1:d0dfbce63a89 235 }
elmot 1:d0dfbce63a89 236 }
elmot 1:d0dfbce63a89 237
elmot 1:d0dfbce63a89 238 /**
elmot 1:d0dfbce63a89 239 * @brief Enable Gyroscope interrupts (INT1 or INT2).
elmot 1:d0dfbce63a89 240 * @param IntPin: Interrupt pin
elmot 1:d0dfbce63a89 241 * This parameter can be:
elmot 1:d0dfbce63a89 242 * @arg L3GD20_INT1
elmot 1:d0dfbce63a89 243 * @arg L3GD20_INT2
elmot 1:d0dfbce63a89 244 * @retval None
elmot 1:d0dfbce63a89 245 */
elmot 1:d0dfbce63a89 246 void BSP_GYRO_EnableIT(uint8_t IntPin)
elmot 1:d0dfbce63a89 247 {
elmot 1:d0dfbce63a89 248 if(GyroscopeDrv->EnableIT != NULL)
elmot 1:d0dfbce63a89 249 {
elmot 1:d0dfbce63a89 250 GyroscopeDrv->EnableIT(IntPin);
elmot 1:d0dfbce63a89 251 }
elmot 1:d0dfbce63a89 252 }
elmot 1:d0dfbce63a89 253
elmot 1:d0dfbce63a89 254 /**
elmot 1:d0dfbce63a89 255 * @brief Disable Gyroscope interrupts (INT1 or INT2).
elmot 1:d0dfbce63a89 256 * @param IntPin: Interrupt pin
elmot 1:d0dfbce63a89 257 * This parameter can be:
elmot 1:d0dfbce63a89 258 * @arg L3GD20_INT1
elmot 1:d0dfbce63a89 259 * @arg L3GD20_INT2
elmot 1:d0dfbce63a89 260 * @retval None
elmot 1:d0dfbce63a89 261 */
elmot 1:d0dfbce63a89 262 void BSP_GYRO_DisableIT(uint8_t IntPin)
elmot 1:d0dfbce63a89 263 {
elmot 1:d0dfbce63a89 264 if(GyroscopeDrv->DisableIT != NULL)
elmot 1:d0dfbce63a89 265 {
elmot 1:d0dfbce63a89 266 GyroscopeDrv->DisableIT(IntPin);
elmot 1:d0dfbce63a89 267 }
elmot 1:d0dfbce63a89 268 }
elmot 1:d0dfbce63a89 269
elmot 1:d0dfbce63a89 270 /**
elmot 1:d0dfbce63a89 271 * @brief Get XYZ angular acceleration from the Gyroscope.
elmot 1:d0dfbce63a89 272 * @param pfData: pointer on floating array
elmot 1:d0dfbce63a89 273 * @retval None
elmot 1:d0dfbce63a89 274 */
elmot 1:d0dfbce63a89 275 void BSP_GYRO_GetXYZ(float* pfData)
elmot 1:d0dfbce63a89 276 {
elmot 1:d0dfbce63a89 277 if(GyroscopeDrv->GetXYZ!= NULL)
elmot 1:d0dfbce63a89 278 {
elmot 1:d0dfbce63a89 279 GyroscopeDrv->GetXYZ(pfData);
elmot 1:d0dfbce63a89 280 }
elmot 1:d0dfbce63a89 281 }
elmot 1:d0dfbce63a89 282
elmot 1:d0dfbce63a89 283 /**
elmot 1:d0dfbce63a89 284 * @}
elmot 1:d0dfbce63a89 285 */
elmot 1:d0dfbce63a89 286
elmot 1:d0dfbce63a89 287 /**
elmot 1:d0dfbce63a89 288 * @}
elmot 1:d0dfbce63a89 289 */
elmot 1:d0dfbce63a89 290
elmot 1:d0dfbce63a89 291 /**
elmot 1:d0dfbce63a89 292 * @}
elmot 1:d0dfbce63a89 293 */
elmot 1:d0dfbce63a89 294
elmot 1:d0dfbce63a89 295 /**
elmot 1:d0dfbce63a89 296 * @}
elmot 1:d0dfbce63a89 297 */
elmot 1:d0dfbce63a89 298
elmot 1:d0dfbce63a89 299 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/