store accel_z and sallen key signal

Dependencies:   mbed tsi_sensor FreescaleIAP MMA8451Q MPL3115A2

Committer:
tuscasp
Date:
Wed Dec 05 23:13:47 2018 +0000
Revision:
5:03e5a9dedec4
Parent:
4:64f3383f2c43
save acce_z and sallen key signal

Who changed what in which revision?

UserRevisionLine numberNew contents of line
martlefebvre94 0:642dcee532b6 1 /*******************************************************/
martlefebvre94 0:642dcee532b6 2 /**** LELEC_2811 Multiple sensors logger ***/
martlefebvre94 0:642dcee532b6 3 /**** P. Gerard UCL 23/11/2016 ***/
martlefebvre94 0:642dcee532b6 4 /**** M. Lefebvre UCL 09/11/2018 ***/
martlefebvre94 0:642dcee532b6 5 /*******************************************************/
martlefebvre94 0:642dcee532b6 6
martlefebvre94 0:642dcee532b6 7 #include "mbed.h"
martlefebvre94 0:642dcee532b6 8 #include "FreescaleIAP.h" // Library for Flash access
martlefebvre94 0:642dcee532b6 9 #include "MMA8451Q.h" // Accelerometer
martlefebvre94 0:642dcee532b6 10 #include "MPL3115A2.h" // Library for altimeter/temperature access
martlefebvre94 0:642dcee532b6 11
martlefebvre94 0:642dcee532b6 12 #define KL25Z_VDD 2.7 // !!! Value of supply voltage VDD: To be measured on board KL25Z pin 3V3 (calibration)
martlefebvre94 0:642dcee532b6 13
martlefebvre94 0:642dcee532b6 14 #define MMA8451_I2C_ADDRESS (0x1d<<1)
martlefebvre94 0:642dcee532b6 15 #define FSR 0x02 // 0x00 for 2G, 0x01 for 4G, 0x02 for 8G
martlefebvre94 0:642dcee532b6 16 #define REG_OUT_X_MSB 0x01
martlefebvre94 0:642dcee532b6 17 #define REG_OUT_Y_MSB 0x03
martlefebvre94 0:642dcee532b6 18 #define REG_OUT_Z_MSB 0x05
martlefebvre94 0:642dcee532b6 19
martlefebvre94 0:642dcee532b6 20 #define MPL3115A2_I2C_ADDRESS (0x60<<1)
martlefebvre94 0:642dcee532b6 21 #define REG_ALTIMETER_MSB 0x01
martlefebvre94 0:642dcee532b6 22
martlefebvre94 0:642dcee532b6 23 #define DISABLE_STATE 0
martlefebvre94 0:642dcee532b6 24 #define ENABLE_STATE 1
martlefebvre94 0:642dcee532b6 25
martlefebvre94 0:642dcee532b6 26 #define NO_JUMPER 0
martlefebvre94 0:642dcee532b6 27 #define JUMPER_PRESENT 1
martlefebvre94 0:642dcee532b6 28
martlefebvre94 0:642dcee532b6 29 #define LEVEL_0 0
martlefebvre94 0:642dcee532b6 30 #define LEVEL_1 1
martlefebvre94 0:642dcee532b6 31
martlefebvre94 0:642dcee532b6 32 #define LED_ON 0
martlefebvre94 0:642dcee532b6 33 #define LED_OFF 1
martlefebvre94 0:642dcee532b6 34
martlefebvre94 0:642dcee532b6 35 #define FLASH_NO_ACQ_DONE 0
martlefebvre94 0:642dcee532b6 36 #define FLASH_ACQ_DONE 1
martlefebvre94 0:642dcee532b6 37 #define ERASE_FLASH_ERROR -1
martlefebvre94 0:642dcee532b6 38 #define WRITE_FLASH_ERROR -2
martlefebvre94 0:642dcee532b6 39
martlefebvre94 0:642dcee532b6 40 #define SECTOR_SIZE 1024
martlefebvre94 0:642dcee532b6 41 #define RESERVED_SECTOR 32
martlefebvre94 0:642dcee532b6 42
tuscasp 5:03e5a9dedec4 43 #define ACQ_TIMER_PERIOD 0.05 // Time between 2 acquisitions in seconds
tuscasp 5:03e5a9dedec4 44 // f_sampling = 1 / ACQ_TIMER_PERIOD = 20Hz
martlefebvre94 0:642dcee532b6 45
tuscasp 5:03e5a9dedec4 46 #define N_PER_AVERAGE 20 // f_storage = f_sampling / N_PER_AVERAGE = 2Hz
tuscasp 5:03e5a9dedec4 47
tuscasp 3:93bc37d442df 48 #define BOARD_STRAIGHT 0
tuscasp 3:93bc37d442df 49 #define BOARD_UPSIDE_DOWN 1
tuscasp 2:1bd31ca8a126 50 #define ACCEL_MINIMUM_FOR_STORAGE 0
tuscasp 2:1bd31ca8a126 51
tuscasp 3:93bc37d442df 52 // Structure of sensors Data !!!!! SIZE MUST BE A MULTIPLE OF 4 bytes !!!!!
martlefebvre94 0:642dcee532b6 53 typedef struct{
martlefebvre94 0:642dcee532b6 54 int16_t Accel_X; // 2 bytes
martlefebvre94 0:642dcee532b6 55 int16_t Accel_Y; // 2 bytes
martlefebvre94 0:642dcee532b6 56 int16_t Accel_Z; // 2 bytes
martlefebvre94 0:642dcee532b6 57 float Temperature; // 4 bytes
martlefebvre94 0:642dcee532b6 58 unsigned short Analog_PTE20; // 2 bytes
martlefebvre94 0:642dcee532b6 59 unsigned short Analog_PTE21; // 2 bytes
tuscasp 3:93bc37d442df 60 unsigned short Empty; // 2 bytes
martlefebvre94 0:642dcee532b6 61 } Sensor_Data; // TOTAL = 16 bytes
martlefebvre94 0:642dcee532b6 62
tuscasp 3:93bc37d442df 63 typedef struct{
tuscasp 2:1bd31ca8a126 64 unsigned short Accel_Z;
tuscasp 3:93bc37d442df 65 unsigned short Sallen_Key;
tuscasp 5:03e5a9dedec4 66 // unsigned short Interface;
tuscasp 5:03e5a9dedec4 67 // unsigned short Useless;
tuscasp 2:1bd31ca8a126 68 } Stored_Data;
tuscasp 2:1bd31ca8a126 69
tuscasp 3:93bc37d442df 70 int Number_Stored_Points;
tuscasp 2:1bd31ca8a126 71
martlefebvre94 0:642dcee532b6 72 // --- Setup I2C for MMA8451 accelerometer
martlefebvre94 0:642dcee532b6 73 // --- The last argument is the full scale range (FSR). 0x00 for 2G, 0x01 for 4G, 0x02 for 8G
martlefebvre94 0:642dcee532b6 74 MMA8451Q my8451(PTE25, PTE24, MMA8451_I2C_ADDRESS, FSR);
martlefebvre94 0:642dcee532b6 75 DigitalOut Accel_Enable(PTA13);
martlefebvre94 0:642dcee532b6 76
martlefebvre94 0:642dcee532b6 77 // --- Set temperature sensor
martlefebvre94 0:642dcee532b6 78 MPL3115A2 myMPL3115(PTE0, PTE1, MPL3115A2_I2C_ADDRESS);
martlefebvre94 0:642dcee532b6 79
martlefebvre94 0:642dcee532b6 80 // --- Set serial port (for communication with PC)
martlefebvre94 0:642dcee532b6 81 Serial Host_Comm(USBTX, USBRX);
martlefebvre94 0:642dcee532b6 82
martlefebvre94 0:642dcee532b6 83 // Analog inputs (ADCs)
martlefebvre94 0:642dcee532b6 84 AnalogIn myPTE20(PTE20);
martlefebvre94 0:642dcee532b6 85 AnalogIn myPTE21(PTE21);
martlefebvre94 0:642dcee532b6 86
martlefebvre94 0:642dcee532b6 87 // Analog output (DAC)
martlefebvre94 0:642dcee532b6 88 AnalogOut myDAC(PTE30);
martlefebvre94 0:642dcee532b6 89
martlefebvre94 0:642dcee532b6 90 // Digital outputs
martlefebvre94 0:642dcee532b6 91 DigitalOut Led_Red(LED1); // Define I/O for LEDs
martlefebvre94 0:642dcee532b6 92 DigitalOut Led_Green(LED2);
martlefebvre94 0:642dcee532b6 93 DigitalOut Led_Blue(LED3);
martlefebvre94 0:642dcee532b6 94 DigitalOut Start_Pulse_Out(PTC9); // Used to enter/exit acquisition mode
martlefebvre94 0:642dcee532b6 95 DigitalIn Start_Pulse_In(PTC11); // Short pins J1_15 and J1_16 to enter in Acq_Mode
martlefebvre94 0:642dcee532b6 96
martlefebvre94 0:642dcee532b6 97 // Global variables
martlefebvre94 0:642dcee532b6 98 volatile bool bTimer; // 1 means a timer tick is done
martlefebvre94 0:642dcee532b6 99 Ticker myTick_Acq; // Periodical timer for acquisition
martlefebvre94 0:642dcee532b6 100
martlefebvre94 0:642dcee532b6 101 int Flash_Base_Address = RESERVED_SECTOR * SECTOR_SIZE; // Store Flash base address with 32K reserved for application code
martlefebvre94 0:642dcee532b6 102 int Nb_Sector;
martlefebvre94 0:642dcee532b6 103 uint32_t KL25_Flash_Size;
martlefebvre94 0:642dcee532b6 104
martlefebvre94 0:642dcee532b6 105 // Functions declaration
martlefebvre94 0:642dcee532b6 106 void Clear_Led(void);
martlefebvre94 0:642dcee532b6 107 int Acquisition_Flash(void);
martlefebvre94 0:642dcee532b6 108 int Read_Data_Logging(void);
martlefebvre94 0:642dcee532b6 109 bool Check_Jumper(void);
martlefebvre94 0:642dcee532b6 110 void myTimer_Acq_Task(void);
martlefebvre94 0:642dcee532b6 111 void Acquisition_Task(void);
martlefebvre94 0:642dcee532b6 112 void Read_Task(void);
martlefebvre94 0:642dcee532b6 113 extern IAPCode verify_erased(int address, unsigned int length);
martlefebvre94 0:642dcee532b6 114
martlefebvre94 0:642dcee532b6 115 int main() {
tuscasp 3:93bc37d442df 116
tuscasp 2:1bd31ca8a126 117 uint8_t Count; // Count defining the number of time the LED blinks before data acquisition
martlefebvre94 0:642dcee532b6 118
martlefebvre94 0:642dcee532b6 119 // Set DAC output voltage
martlefebvre94 0:642dcee532b6 120 float vdac;
martlefebvre94 0:642dcee532b6 121 uint16_t dac_value; // Local variable in 16 bits
martlefebvre94 0:642dcee532b6 122
tuscasp 2:1bd31ca8a126 123 vdac = 0; // Voltage output value
martlefebvre94 0:642dcee532b6 124 dac_value = (uint16_t) ((vdac * 65536) / KL25Z_VDD); // Transform desired voltage to fraction of 0xFFFF
martlefebvre94 0:642dcee532b6 125 myDAC.write_u16(dac_value); // DAC value in range 0x0000 - 0xFFFF (see mbed's AnalogOut classe ref.)
martlefebvre94 0:642dcee532b6 126
martlefebvre94 0:642dcee532b6 127 Start_Pulse_In.mode(PullNone); // Input pin is programmed as floating
martlefebvre94 0:642dcee532b6 128 Accel_Enable = DISABLE_STATE; // Turn Accel. Enable I/O to disabled state
martlefebvre94 0:642dcee532b6 129
martlefebvre94 0:642dcee532b6 130 // --- Baud rate setting
martlefebvre94 0:642dcee532b6 131 Host_Comm.baud(115200);
martlefebvre94 0:642dcee532b6 132 Clear_Led();
martlefebvre94 0:642dcee532b6 133
martlefebvre94 0:642dcee532b6 134 KL25_Flash_Size = flash_size(); // Get size of KL25 embedded Flash
martlefebvre94 0:642dcee532b6 135 Nb_Sector = (KL25_Flash_Size / SECTOR_SIZE) - RESERVED_SECTOR; // Reserve max 32K for app code
martlefebvre94 0:642dcee532b6 136 myTick_Acq.attach(&myTimer_Acq_Task, ACQ_TIMER_PERIOD); // Initialize timer interrupt
martlefebvre94 0:642dcee532b6 137
tuscasp 3:93bc37d442df 138 Host_Comm.printf("\n\rLELEC2811 Multiple sensors logger V2.0 UCL 2018\n\r");
martlefebvre94 0:642dcee532b6 139
martlefebvre94 0:642dcee532b6 140 if ((sizeof(Sensor_Data) % 4) != 0)
martlefebvre94 0:642dcee532b6 141 {
martlefebvre94 0:642dcee532b6 142 Host_Comm.printf("\n\rERROR! Acquisition Data Structure size is NOT a multiple of 4!!! (%d)", sizeof(Sensor_Data));
martlefebvre94 0:642dcee532b6 143 for(;;);
martlefebvre94 0:642dcee532b6 144 }
martlefebvre94 0:642dcee532b6 145
martlefebvre94 0:642dcee532b6 146 myMPL3115.Oversample_Ratio(OVERSAMPLE_RATIO_4);
martlefebvre94 0:642dcee532b6 147 myMPL3115.Barometric_Mode(); // Configure MPL3115 (used for temperature and pressure measurement)
martlefebvre94 0:642dcee532b6 148
martlefebvre94 0:642dcee532b6 149 for (;;)
martlefebvre94 0:642dcee532b6 150 {
martlefebvre94 0:642dcee532b6 151 if (Check_Jumper() == JUMPER_PRESENT)
martlefebvre94 0:642dcee532b6 152 {
martlefebvre94 0:642dcee532b6 153 Clear_Led();
martlefebvre94 0:642dcee532b6 154
martlefebvre94 0:642dcee532b6 155 Count = 5;
martlefebvre94 0:642dcee532b6 156 while (Count !=0)
martlefebvre94 0:642dcee532b6 157 {
martlefebvre94 0:642dcee532b6 158 if (Check_Jumper() == JUMPER_PRESENT)
martlefebvre94 0:642dcee532b6 159 {
martlefebvre94 0:642dcee532b6 160 Led_Blue = LED_ON; // Blink to alert user "Enter in Acquisition" after 5 seconds
martlefebvre94 0:642dcee532b6 161 wait_ms(900);
martlefebvre94 0:642dcee532b6 162 Led_Blue = LED_OFF;
martlefebvre94 0:642dcee532b6 163 wait_ms(100);
martlefebvre94 0:642dcee532b6 164 Count --;
martlefebvre94 0:642dcee532b6 165 if (Count == 0)
martlefebvre94 0:642dcee532b6 166 {
martlefebvre94 0:642dcee532b6 167 Acquisition_Task();
martlefebvre94 0:642dcee532b6 168 }
martlefebvre94 0:642dcee532b6 169 }
martlefebvre94 0:642dcee532b6 170 else
martlefebvre94 0:642dcee532b6 171 {
martlefebvre94 0:642dcee532b6 172 Count = 0;
martlefebvre94 0:642dcee532b6 173 }
martlefebvre94 0:642dcee532b6 174 }
martlefebvre94 0:642dcee532b6 175 }
martlefebvre94 0:642dcee532b6 176 else
martlefebvre94 0:642dcee532b6 177 {
martlefebvre94 0:642dcee532b6 178 Read_Task();
martlefebvre94 0:642dcee532b6 179 }
martlefebvre94 0:642dcee532b6 180 }
martlefebvre94 0:642dcee532b6 181 }
tuscasp 3:93bc37d442df 182
martlefebvre94 0:642dcee532b6 183 void Read_Task()
martlefebvre94 0:642dcee532b6 184 {
martlefebvre94 0:642dcee532b6 185 char host_cmd;
martlefebvre94 0:642dcee532b6 186 IAPCode Flash_State;
martlefebvre94 0:642dcee532b6 187 bool bAcq_Done;
martlefebvre94 0:642dcee532b6 188
martlefebvre94 0:642dcee532b6 189 Flash_State = verify_erased(Flash_Base_Address, KL25_Flash_Size - (RESERVED_SECTOR * SECTOR_SIZE));
martlefebvre94 0:642dcee532b6 190 if (Flash_State == 0) // Virgin Flash ?
martlefebvre94 0:642dcee532b6 191 {
martlefebvre94 0:642dcee532b6 192 bAcq_Done = 0;
martlefebvre94 0:642dcee532b6 193 }
martlefebvre94 0:642dcee532b6 194 else
martlefebvre94 0:642dcee532b6 195 {
martlefebvre94 0:642dcee532b6 196 bAcq_Done = 1;
martlefebvre94 0:642dcee532b6 197 }
martlefebvre94 0:642dcee532b6 198
martlefebvre94 0:642dcee532b6 199 Clear_Led();
martlefebvre94 0:642dcee532b6 200 wait_ms(500);
martlefebvre94 0:642dcee532b6 201
martlefebvre94 0:642dcee532b6 202 if (bAcq_Done == 1)
martlefebvre94 0:642dcee532b6 203 {
martlefebvre94 0:642dcee532b6 204 Led_Green = LED_ON;
martlefebvre94 0:642dcee532b6 205 Host_Comm.putc('1');
martlefebvre94 0:642dcee532b6 206 }
martlefebvre94 0:642dcee532b6 207 else
martlefebvre94 0:642dcee532b6 208 {
martlefebvre94 0:642dcee532b6 209 Led_Red = LED_ON;
martlefebvre94 0:642dcee532b6 210 Host_Comm.putc('0');
martlefebvre94 0:642dcee532b6 211 }
martlefebvre94 0:642dcee532b6 212
martlefebvre94 0:642dcee532b6 213 if(Host_Comm.readable()) // Did we receive a char from Host ?
martlefebvre94 0:642dcee532b6 214 {
martlefebvre94 0:642dcee532b6 215 host_cmd = Host_Comm.getc(); // Get it
martlefebvre94 0:642dcee532b6 216
martlefebvre94 0:642dcee532b6 217 if ((host_cmd == 'R') || (host_cmd == 'r')) // Read Flash Command ?
martlefebvre94 0:642dcee532b6 218 {
martlefebvre94 0:642dcee532b6 219 Read_Data_Logging(); // Read and send acquisition data
martlefebvre94 0:642dcee532b6 220 }
martlefebvre94 0:642dcee532b6 221 }
martlefebvre94 0:642dcee532b6 222 wait_ms(50);
martlefebvre94 0:642dcee532b6 223 }
martlefebvre94 0:642dcee532b6 224
martlefebvre94 0:642dcee532b6 225 void Acquisition_Task()
martlefebvre94 0:642dcee532b6 226 {
martlefebvre94 0:642dcee532b6 227 int Acq_Status;
martlefebvre94 0:642dcee532b6 228
martlefebvre94 0:642dcee532b6 229 Clear_Led();
martlefebvre94 0:642dcee532b6 230
martlefebvre94 0:642dcee532b6 231 Acq_Status = Acquisition_Flash();
martlefebvre94 0:642dcee532b6 232
martlefebvre94 0:642dcee532b6 233 Clear_Led();
martlefebvre94 0:642dcee532b6 234
martlefebvre94 0:642dcee532b6 235 while (Check_Jumper() == JUMPER_PRESENT)
martlefebvre94 0:642dcee532b6 236 {
martlefebvre94 0:642dcee532b6 237 if (Acq_Status != FLASH_ACQ_DONE)
martlefebvre94 0:642dcee532b6 238 {
martlefebvre94 0:642dcee532b6 239 Led_Red = !Led_Red;
martlefebvre94 0:642dcee532b6 240 }
martlefebvre94 0:642dcee532b6 241 else
martlefebvre94 0:642dcee532b6 242 {
martlefebvre94 0:642dcee532b6 243 Led_Green = !Led_Green;
martlefebvre94 0:642dcee532b6 244 }
martlefebvre94 0:642dcee532b6 245 wait_ms(100);
martlefebvre94 0:642dcee532b6 246 }
martlefebvre94 0:642dcee532b6 247 }
martlefebvre94 0:642dcee532b6 248
martlefebvre94 0:642dcee532b6 249 void Clear_Led(void)
martlefebvre94 0:642dcee532b6 250 {
martlefebvre94 0:642dcee532b6 251 Led_Red = LED_OFF;
martlefebvre94 0:642dcee532b6 252 Led_Green = LED_OFF;
martlefebvre94 0:642dcee532b6 253 Led_Blue = LED_OFF; // Bug on board : Turning on the blue LED decreases consumption...
martlefebvre94 0:642dcee532b6 254 }
martlefebvre94 0:642dcee532b6 255
martlefebvre94 0:642dcee532b6 256 bool Check_Jumper() // If J1_15 and J1_16 connected together -> return JUMPER_PRESENT
martlefebvre94 0:642dcee532b6 257 {
martlefebvre94 0:642dcee532b6 258 uint8_t i;
martlefebvre94 0:642dcee532b6 259
martlefebvre94 0:642dcee532b6 260 for (i = 0 ; i < 2 ; i ++)
martlefebvre94 0:642dcee532b6 261 {
martlefebvre94 0:642dcee532b6 262 Start_Pulse_Out = LEVEL_1;
martlefebvre94 0:642dcee532b6 263 wait_ms(1);
martlefebvre94 0:642dcee532b6 264 if (Start_Pulse_In != LEVEL_1)
martlefebvre94 0:642dcee532b6 265 {
martlefebvre94 0:642dcee532b6 266 return NO_JUMPER;
martlefebvre94 0:642dcee532b6 267 }
martlefebvre94 0:642dcee532b6 268
martlefebvre94 0:642dcee532b6 269 Start_Pulse_Out = LEVEL_0;
martlefebvre94 0:642dcee532b6 270 wait_ms(1);
martlefebvre94 0:642dcee532b6 271 if (Start_Pulse_In != LEVEL_0)
martlefebvre94 0:642dcee532b6 272 {
martlefebvre94 0:642dcee532b6 273 return NO_JUMPER;
martlefebvre94 0:642dcee532b6 274 }
martlefebvre94 0:642dcee532b6 275 }
martlefebvre94 0:642dcee532b6 276 return JUMPER_PRESENT;
martlefebvre94 0:642dcee532b6 277 }
martlefebvre94 0:642dcee532b6 278
martlefebvre94 0:642dcee532b6 279 int Acquisition_Flash(void)
martlefebvre94 0:642dcee532b6 280 {
martlefebvre94 0:642dcee532b6 281 int Status;
martlefebvre94 0:642dcee532b6 282 int Flash_Ptr ;
martlefebvre94 0:642dcee532b6 283 int Led_Counter;
tuscasp 3:93bc37d442df 284 Sensor_Data myData;
tuscasp 2:1bd31ca8a126 285 int Board_Position;
tuscasp 3:93bc37d442df 286 int Count_Measurements;
tuscasp 2:1bd31ca8a126 287 Stored_Data myStoredData;
tuscasp 3:93bc37d442df 288
tuscasp 5:03e5a9dedec4 289 double sallen_key = 0;
martlefebvre94 0:642dcee532b6 290
martlefebvre94 0:642dcee532b6 291 /*** Erase all Flash Page **/
martlefebvre94 0:642dcee532b6 292 for (Flash_Ptr = Flash_Base_Address ; Flash_Ptr < KL25_Flash_Size ; Flash_Ptr += 0x400)
martlefebvre94 0:642dcee532b6 293 {
martlefebvre94 0:642dcee532b6 294 Status = erase_sector(Flash_Ptr); // Erase sector
martlefebvre94 0:642dcee532b6 295
martlefebvre94 0:642dcee532b6 296 if (Status !=0)
martlefebvre94 0:642dcee532b6 297 {
martlefebvre94 0:642dcee532b6 298 return ERASE_FLASH_ERROR;
martlefebvre94 0:642dcee532b6 299 }
martlefebvre94 0:642dcee532b6 300 }
martlefebvre94 0:642dcee532b6 301
martlefebvre94 0:642dcee532b6 302 Flash_Ptr = Flash_Base_Address; // Begin of Storage Area in Flash
martlefebvre94 0:642dcee532b6 303
martlefebvre94 0:642dcee532b6 304 Led_Blue = LED_ON;
martlefebvre94 0:642dcee532b6 305 Led_Counter = 0;
tuscasp 3:93bc37d442df 306
tuscasp 3:93bc37d442df 307 /*** Reset new variables **/
tuscasp 3:93bc37d442df 308
tuscasp 3:93bc37d442df 309 myStoredData.Accel_Z= 0;
tuscasp 5:03e5a9dedec4 310 // myStoredData.Interface = 0;
tuscasp 3:93bc37d442df 311 myStoredData.Sallen_Key = 0;
martlefebvre94 0:642dcee532b6 312
tuscasp 3:93bc37d442df 313 Board_Position = BOARD_STRAIGHT;
tuscasp 3:93bc37d442df 314
tuscasp 3:93bc37d442df 315 Number_Stored_Points = 0;
tuscasp 3:93bc37d442df 316
tuscasp 3:93bc37d442df 317
tuscasp 3:93bc37d442df 318 /***** Begin of Loop Acquisition - Write in Flash ***/
tuscasp 3:93bc37d442df 319
tuscasp 3:93bc37d442df 320 while (Flash_Ptr < (KL25_Flash_Size - sizeof(Sensor_Data)) ) // Acq Loop
martlefebvre94 0:642dcee532b6 321 {
martlefebvre94 0:642dcee532b6 322 while (bTimer == 0) // Wait Acq Tick Timer Done
martlefebvre94 0:642dcee532b6 323 {
martlefebvre94 0:642dcee532b6 324
martlefebvre94 0:642dcee532b6 325 }
martlefebvre94 0:642dcee532b6 326 bTimer = 0;
martlefebvre94 0:642dcee532b6 327
martlefebvre94 0:642dcee532b6 328 if ((float) Led_Counter * ACQ_TIMER_PERIOD == 1.0) // Blink at 1Hz
martlefebvre94 0:642dcee532b6 329 {
martlefebvre94 0:642dcee532b6 330 Led_Counter = 0;
martlefebvre94 0:642dcee532b6 331 Led_Blue = !Led_Blue;
martlefebvre94 0:642dcee532b6 332 }
martlefebvre94 0:642dcee532b6 333
tuscasp 3:93bc37d442df 334 Led_Counter++;
tuscasp 3:93bc37d442df 335
martlefebvre94 0:642dcee532b6 336 // Get accelerometer data
martlefebvre94 0:642dcee532b6 337 Accel_Enable = ENABLE_STATE; // Rising edge -> Start accelerometer measurement
martlefebvre94 0:642dcee532b6 338
tuscasp 3:93bc37d442df 339 // myData.Accel_X = my8451.getAccAxis(REG_OUT_X_MSB);
tuscasp 3:93bc37d442df 340 // myData.Accel_Y = my8451.getAccAxis(REG_OUT_Y_MSB);
tuscasp 3:93bc37d442df 341 myData.Accel_Z = my8451.getAccAxis(REG_OUT_Z_MSB);
martlefebvre94 0:642dcee532b6 342
martlefebvre94 0:642dcee532b6 343 Accel_Enable = DISABLE_STATE;
martlefebvre94 0:642dcee532b6 344
tuscasp 3:93bc37d442df 345
tuscasp 3:93bc37d442df 346 /*** verify if it is upside down ***/
tuscasp 3:93bc37d442df 347 if (myData.Accel_Z < ACCEL_MINIMUM_FOR_STORAGE)
tuscasp 2:1bd31ca8a126 348 Board_Position = BOARD_UPSIDE_DOWN;
tuscasp 2:1bd31ca8a126 349
tuscasp 3:93bc37d442df 350 if (Board_Position == BOARD_UPSIDE_DOWN){
tuscasp 3:93bc37d442df 351
tuscasp 3:93bc37d442df 352 Count_Measurements ++;
tuscasp 2:1bd31ca8a126 353
tuscasp 3:93bc37d442df 354 // Get ADC values
tuscasp 3:93bc37d442df 355 myData.Analog_PTE20 = myPTE20.read_u16();
tuscasp 5:03e5a9dedec4 356 // myData.Analog_PTE21 = myPTE21.read_u16();
tuscasp 3:93bc37d442df 357
tuscasp 3:93bc37d442df 358 // add data to stored variable
tuscasp 3:93bc37d442df 359 myStoredData.Accel_Z = myData.Accel_Z;
tuscasp 3:93bc37d442df 360
tuscasp 5:03e5a9dedec4 361 sallen_key += myData.Analog_PTE20;
tuscasp 5:03e5a9dedec4 362 // myStoredData.Sallen_Key += myData.Analog_PTE20;
tuscasp 5:03e5a9dedec4 363 // myStoredData.Interface += myData.Analog_PTE21;
tuscasp 2:1bd31ca8a126 364
tuscasp 3:93bc37d442df 365 Host_Comm.printf("\n\r%d %d", Count_Measurements, myStoredData.Accel_Z);
tuscasp 3:93bc37d442df 366
tuscasp 3:93bc37d442df 367 // after N_PER_AVERAGE measurements, take average and store
tuscasp 3:93bc37d442df 368 if (Count_Measurements >= N_PER_AVERAGE){
tuscasp 3:93bc37d442df 369
tuscasp 3:93bc37d442df 370 Count_Measurements = 0;
tuscasp 3:93bc37d442df 371
tuscasp 5:03e5a9dedec4 372 sallen_key = sallen_key/N_PER_AVERAGE;
tuscasp 5:03e5a9dedec4 373 myStoredData.Sallen_Key = (short) sallen_key;
tuscasp 5:03e5a9dedec4 374 // myStoredData.Sallen_Key = myStoredData.Sallen_Key / N_PER_AVERAGE;
tuscasp 5:03e5a9dedec4 375 // myStoredData.Interface = myStoredData.Interface / N_PER_AVERAGE;
tuscasp 5:03e5a9dedec4 376
tuscasp 5:03e5a9dedec4 377 // resets the value at the average
tuscasp 5:03e5a9dedec4 378 sallen_key = 0;
martlefebvre94 0:642dcee532b6 379
tuscasp 2:1bd31ca8a126 380 /*** Save Data in Flash ***/
tuscasp 3:93bc37d442df 381 Number_Stored_Points ++;
tuscasp 2:1bd31ca8a126 382 Status = program_flash(Flash_Ptr, (char *) &myStoredData, sizeof(Stored_Data)); // Write in the Flash
tuscasp 2:1bd31ca8a126 383 if (Status != 0)
tuscasp 2:1bd31ca8a126 384 {
tuscasp 2:1bd31ca8a126 385 Host_Comm.printf("\n\rFlash_Write Error = %d", Status);
tuscasp 2:1bd31ca8a126 386 return WRITE_FLASH_ERROR;
tuscasp 2:1bd31ca8a126 387 }
tuscasp 2:1bd31ca8a126 388 Flash_Ptr += sizeof(Stored_Data);
tuscasp 3:93bc37d442df 389
tuscasp 3:93bc37d442df 390 // verifies if system comes back straight orientation
tuscasp 3:93bc37d442df 391 if (myData.Accel_Z > ACCEL_MINIMUM_FOR_STORAGE)
tuscasp 3:93bc37d442df 392 Board_Position = BOARD_STRAIGHT;
tuscasp 3:93bc37d442df 393
tuscasp 2:1bd31ca8a126 394 if (Check_Jumper() != JUMPER_PRESENT) // If jumper removed -> Stop acquisition
tuscasp 2:1bd31ca8a126 395 {
tuscasp 2:1bd31ca8a126 396 return FLASH_ACQ_DONE ;
tuscasp 2:1bd31ca8a126 397 }
tuscasp 2:1bd31ca8a126 398 }
tuscasp 2:1bd31ca8a126 399
tuscasp 2:1bd31ca8a126 400 }
martlefebvre94 0:642dcee532b6 401
tuscasp 3:93bc37d442df 402 else {
tuscasp 3:93bc37d442df 403 if (Check_Jumper() != JUMPER_PRESENT) // If jumper removed -> Stop acquisition
tuscasp 3:93bc37d442df 404 return FLASH_ACQ_DONE ;
tuscasp 3:93bc37d442df 405 }
tuscasp 3:93bc37d442df 406
martlefebvre94 0:642dcee532b6 407 }
martlefebvre94 0:642dcee532b6 408
martlefebvre94 0:642dcee532b6 409 return FLASH_ACQ_DONE ;
martlefebvre94 0:642dcee532b6 410 }
martlefebvre94 0:642dcee532b6 411
martlefebvre94 0:642dcee532b6 412 int Read_Data_Logging()
martlefebvre94 0:642dcee532b6 413 {
tuscasp 3:93bc37d442df 414 Stored_Data * data = (Stored_Data * )Flash_Base_Address; // Sensor_Data pointer of data stored in Flash
martlefebvre94 0:642dcee532b6 415 int Flash_Record_Ptr;
martlefebvre94 0:642dcee532b6 416 char cmd;
martlefebvre94 0:642dcee532b6 417 int Record_Counter;
martlefebvre94 0:642dcee532b6 418 int Max_Record;
tuscasp 2:1bd31ca8a126 419 Stored_Data myRead_Data; // Data Structure used to retrieve saved value from Flash
martlefebvre94 0:642dcee532b6 420
tuscasp 3:93bc37d442df 421 float Ethanol_Sallen_Key;
tuscasp 5:03e5a9dedec4 422 // float Ethanol_Interface;
martlefebvre94 0:642dcee532b6 423
martlefebvre94 0:642dcee532b6 424 Clear_Led();
martlefebvre94 0:642dcee532b6 425
tuscasp 3:93bc37d442df 426 Max_Record = (Nb_Sector * SECTOR_SIZE) / sizeof(Sensor_Data);
martlefebvre94 0:642dcee532b6 427 Record_Counter = 0;
martlefebvre94 0:642dcee532b6 428 Flash_Record_Ptr = 0;
martlefebvre94 0:642dcee532b6 429
tuscasp 5:03e5a9dedec4 430 Host_Comm.printf("\n\r# AccZ Eth_SK");
martlefebvre94 0:642dcee532b6 431
tuscasp 3:93bc37d442df 432 while (Record_Counter < Number_Stored_Points && Record_Counter < Max_Record)
martlefebvre94 0:642dcee532b6 433 {
tuscasp 5:03e5a9dedec4 434 Led_Green = !Led_Green;
martlefebvre94 0:642dcee532b6 435 Led_Blue = !Led_Green;
martlefebvre94 0:642dcee532b6 436
martlefebvre94 0:642dcee532b6 437 if(Host_Comm.readable())
martlefebvre94 0:642dcee532b6 438 {
martlefebvre94 0:642dcee532b6 439 cmd = Host_Comm.getc();
martlefebvre94 0:642dcee532b6 440 if ((cmd == 'S') || (cmd == 's')) // Receiving 'S' or 's' means stop Read Flash
martlefebvre94 0:642dcee532b6 441 {
martlefebvre94 0:642dcee532b6 442 Clear_Led();
martlefebvre94 0:642dcee532b6 443 return 0;
martlefebvre94 0:642dcee532b6 444 }
martlefebvre94 0:642dcee532b6 445 }
martlefebvre94 0:642dcee532b6 446
martlefebvre94 0:642dcee532b6 447 myRead_Data = data[Flash_Record_Ptr];
martlefebvre94 0:642dcee532b6 448
martlefebvre94 0:642dcee532b6 449 Flash_Record_Ptr ++;
martlefebvre94 0:642dcee532b6 450
tuscasp 2:1bd31ca8a126 451 if (myRead_Data.Accel_Z == -1) // Valid data ? (!= 0xFFFFFFFF from empty Flash sector)
martlefebvre94 0:642dcee532b6 452 {
martlefebvre94 0:642dcee532b6 453 }
martlefebvre94 0:642dcee532b6 454 else
martlefebvre94 0:642dcee532b6 455 {
tuscasp 3:93bc37d442df 456 Ethanol_Sallen_Key = ((float) myRead_Data.Sallen_Key / 0XFFFF) * KL25Z_VDD; // Convert to voltage
tuscasp 5:03e5a9dedec4 457 // Ethanol_Interface = ((float) myRead_Data.Interface / 0XFFFF) * KL25Z_VDD;
martlefebvre94 0:642dcee532b6 458
martlefebvre94 0:642dcee532b6 459 Host_Comm.printf("\n\r%d ", Record_Counter);
tuscasp 2:1bd31ca8a126 460 Host_Comm.printf("%d ", myRead_Data.Accel_Z);
tuscasp 5:03e5a9dedec4 461 Host_Comm.printf("%1.3f", Ethanol_Sallen_Key);
martlefebvre94 0:642dcee532b6 462 }
martlefebvre94 0:642dcee532b6 463
martlefebvre94 0:642dcee532b6 464 Record_Counter ++;
martlefebvre94 0:642dcee532b6 465 }
martlefebvre94 0:642dcee532b6 466 Clear_Led();
martlefebvre94 0:642dcee532b6 467 return 0;
martlefebvre94 0:642dcee532b6 468 }
martlefebvre94 0:642dcee532b6 469
martlefebvre94 0:642dcee532b6 470 /* Interrupt Task */
martlefebvre94 0:642dcee532b6 471 void myTimer_Acq_Task()
martlefebvre94 0:642dcee532b6 472 {
martlefebvre94 0:642dcee532b6 473 bTimer = 1;
tuscasp 4:64f3383f2c43 474 }