/* * peripherals.c * * Created on: Feb 23, 2014 * Author: michael */ #include "main.h" #include "peripherals.h" #include "globalVariables.h" #include "stm32f30x_it.h" #include //These variables are counted down to 0 by the timer interrupt (every 1 ms) __IO uint32_t timerTimeSinceLastKeepAlive = 0; __IO uint32_t timerTimeSinceLastFrameSent = 0; __IO uint32_t timerTimeSinceLastAccMagReadout = 0; __IO uint32_t timerDelayCountdown = 0; uint16_t currentGyroScale; //Either 250, 500 or 2000, set by initGyro(), used by calibration routine //The scaling factors are used to convert arbitary digits from the 3 sensors to physical units. //In particular to [10^-4 rad/s], [10^-4 m/s^2] and [10^-4 Gauss] //As the factors depend on the current sensor scale, the values are set by the initSensor functions. float gyroScalingFactors[3]; //X,Y,Z float accScalingFactors[3]; //X,Y,Z float magScalingFactors[3]; //X,Z,Y //To correct the gyro offset error, these values are subtracted from the result float gyroBiasCalValue250[3] = { 0.0, 0.0, 0.0 }; float gyroBiasCalValue500[3] = { 0.0, 0.0, 0.0 }; float gyroBiasCalValue2000[3] = { 0.0, 0.0, 0.0 }; float *gyroBiasCalValue; //Will always point to the relevant one of the three arrays ( set by initGyro() ) //------------------------------------------------------------------------------------------- // Timer interrupt, called every 1 ms, keeps track of some counting variables //------------------------------------------------------------------------------------------- void SysTick_Handler(void) { timerTimeSinceLastKeepAlive++; timerTimeSinceLastFrameSent++; timerTimeSinceLastAccMagReadout++; if (timerDelayCountdown > 0){ timerDelayCountdown--; } } //do nothing for _delay_ ms void delayms( uint32_t delay ){ timerDelayCountdown = delay; while( timerDelayCountdown ); } // Little convenience function to set all 3 scale factors (X,Y,Z) to the same value void _setAll3ScalingFactors( float *scaleFactorsToSet, float scaleValue ){ uint8_t i; for(i=0; i<=2; i++){ scaleFactorsToSet[i] = scaleValue; } } //------------------------------------------------------------------------------------------- // * @brief Configure the L3GD20 Mems gyroscope sensor and set scaling values // * @param gyroScale set by this in the SDK: GyroRangeRamp[] = { 250, 500, 1000, 2000 }; //------------------------------------------------------------------------------------------- void initSensorGyro(uint16_t gyroScale) { uint8_t tempValue; L3GD20_InitTypeDef L3GD20_InitStructure; currentGyroScale = gyroScale; /* Configure Mems L3GD20 */ L3GD20_InitStructure.Power_Mode = L3GD20_MODE_ACTIVE; L3GD20_InitStructure.Output_DataRate = L3GD20_OUTPUT_DATARATE_4;//760 Hz L3GD20_InitStructure.Axes_Enable = L3GD20_AXES_ENABLE; L3GD20_InitStructure.Band_Width = L3GD20_BANDWIDTH_4; //Max bandwidth L3GD20_InitStructure.BlockData_Update = L3GD20_BlockDataUpdate_Single; L3GD20_InitStructure.Endianness = L3GD20_BLE_LSB; switch (gyroScale) { case 250: L3GD20_InitStructure.Full_Scale = L3GD20_FULLSCALE_250; _setAll3ScalingFactors( gyroScalingFactors, GYRO_SCALE_FACTOR_250 ); gyroBiasCalValue = gyroBiasCalValue250; break; case 500: case 1000: //1000 deg/s fullscale is not supported by ST chip, use 500 deg/s instead L3GD20_InitStructure.Full_Scale = L3GD20_FULLSCALE_500; _setAll3ScalingFactors( gyroScalingFactors, GYRO_SCALE_FACTOR_500 ); gyroBiasCalValue = gyroBiasCalValue500; break; case 2000: default: L3GD20_InitStructure.Full_Scale = L3GD20_FULLSCALE_2000; _setAll3ScalingFactors( gyroScalingFactors, GYRO_SCALE_FACTOR_2000 ); gyroBiasCalValue = gyroBiasCalValue2000; } L3GD20_Init(&L3GD20_InitStructure); //Writes to CTRL_REG1 and CTRL_REG4 tempValue = 0x01; //No INT1, No boot status on INT1 pin, INT1 inactive, push-pull, No DRDY on INT2 pin, No INT2 FIFO watermakr, No INT2 FIFO overrun, INT2 FIFO empty = on L3GD20_Write(&tempValue, L3GD20_CTRL_REG3_ADDR, 1); tempValue = 0x40; //No reboot, Enable FIFO, Disable HPF, INT1 and OUTPUT directly connected to ADC without filters L3GD20_Write(&tempValue, L3GD20_CTRL_REG5_ADDR, 1); tempValue = 0x40; //FIFO stream mode, Watermrk level = 0 L3GD20_Write(&tempValue, L3GD20_FIFO_CTRL_REG_ADDR, 1); } //Measure average gyro reading over several seconds, while the sensor is not moved. This is the average bias error, which //can be subtracted from the raw gyro reading for correction //This routine measures the bias error for all three axes and all three gyroscope range settings. It saves the 9 values in a global array. void doGyroBiasCalibration(void){ float avgBuffers[3]; int32_t readValues[3], counter; const uint16_t scales[3]={250, 500, 2000}; //These gyro range settings are calibrated separately uint8_t i, scalesToCalibrate; for( scalesToCalibrate=0; scalesToCalibrate<=2; scalesToCalibrate++ ){ //For each range setting LED_out(0x01< pwmCount ) // check if it is time to set a specific portPin tmpOut |= portPinMask; // the larger the BRIGHTNESS value, the sooner the pin will be set to one portPinMask<<=1; } if( ++pwmCount >= 64 ){ pwmCount = 0; } LED_out( tmpOut ); } //Display a byte on the 8 LEDs, where it goes around circles void LED_out( uint8_t dat ){ GPIOE->ODR = (GPIOE->ODR&0x0F) | (dat<<8); } //Switch off all 8 LEDs void LEDs_Off(void) { uint8_t i; for (i = LED3; i <= LED10; i++) { STM_EVAL_LEDOff((Led_TypeDef)i); } } //If button not pressed: returns 0 //If button is pressed it blocks for 1 s to measure time length //If button is pressed < 1 s: returns 1 //If button is pressed > 1 s: returns 2 uint8_t getShortLongButtonPress(void){ if ( STM_EVAL_PBGetState( BUTTON_USER ) ){ //Check if button is pressed delayms( 50 ); //Debounce if ( !STM_EVAL_PBGetState( BUTTON_USER ) ){ return 0; //Just a fluke, ignore } timerDelayCountdown = 1000; //Set countdwon timer to 1 s to find long button press while( STM_EVAL_PBGetState( BUTTON_USER ) && (timerDelayCountdown>0) ){ ; } // Wait max 2s. for button release if( timerDelayCountdown>0 ){ //Short button press return 1; } else { //Long button press return 2; } } return 0; }