Code for the 2018 BeerGuess project

Dependencies:   FreescaleIAP MMA8451Q MPL3115A2 mbed tsi_sensor

Committer:
martlefebvre94
Date:
Tue Nov 13 16:23:18 2018 +0000
Revision:
1:521869dcf4f1
Parent:
0:642dcee532b6
Removal of ADC22

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