store accel_z and sallen key signal

Dependencies:   mbed tsi_sensor FreescaleIAP MMA8451Q MPL3115A2

Committer:
martlefebvre94
Date:
Mon Nov 12 22:13:57 2018 +0000
Revision:
0:642dcee532b6
Child:
1:3a57bceb88f8
Code for the 2018 project

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