store accel_z and sallen key signal

Dependencies:   mbed tsi_sensor FreescaleIAP MMA8451Q MPL3115A2

Committer:
tuscasp
Date:
Fri Nov 30 18:57:32 2018 +0000
Revision:
4:64f3383f2c43
Parent:
3:93bc37d442df
Child:
5:03e5a9dedec4
working code, no real time;

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