store accel_z and sallen key signal

Dependencies:   mbed tsi_sensor FreescaleIAP MMA8451Q MPL3115A2

Committer:
tuscasp
Date:
Wed Nov 28 00:31:25 2018 +0000
Revision:
2:1bd31ca8a126
Parent:
1:3a57bceb88f8
Child:
3:93bc37d442df
working code, with one unexplained bug of writing some constant values to the memory at the end of the data acquisition.

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