LELEC_2811 Accelerometer application Based on the FRDM-KL25Z board Use the MMA8451Q sensor

Dependencies:   FreescaleIAP MMA8451Q mbed

Committer:
martlefebvre94
Date:
Sat Sep 22 23:17:02 2018 +0000
Revision:
0:17f544fcad6f
Child:
1:19cb7d77efe1
First version of the code to interface the MMA8451Q accelerometer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
martlefebvre94 0:17f544fcad6f 1 /* LELEC_2811 Accelerometer Project
martlefebvre94 0:17f544fcad6f 2 UCL 2014 - P. Gérard
martlefebvre94 0:17f544fcad6f 3 */
martlefebvre94 0:17f544fcad6f 4 #include "mbed.h"
martlefebvre94 0:17f544fcad6f 5 #include "FreescaleIAP.h" // Library for Flash Access
martlefebvre94 0:17f544fcad6f 6 #include "MMA8451Q.h" // Accelerometer
martlefebvre94 0:17f544fcad6f 7
martlefebvre94 0:17f544fcad6f 8 #define MMA8451_I2C_ADDRESS (0x1d<<1)
martlefebvre94 0:17f544fcad6f 9
martlefebvre94 0:17f544fcad6f 10 #define NO_JUMPER 0
martlefebvre94 0:17f544fcad6f 11 #define JUMPER_PRESENT 1
martlefebvre94 0:17f544fcad6f 12
martlefebvre94 0:17f544fcad6f 13 #define LEVEL_0 0
martlefebvre94 0:17f544fcad6f 14 #define LEVEL_1 1
martlefebvre94 0:17f544fcad6f 15
martlefebvre94 0:17f544fcad6f 16 #define LED_ON 0
martlefebvre94 0:17f544fcad6f 17 #define LED_OFF 1
martlefebvre94 0:17f544fcad6f 18
martlefebvre94 0:17f544fcad6f 19 #define DISABLE_STATE 0
martlefebvre94 0:17f544fcad6f 20 #define ENABLE_STATE 1
martlefebvre94 0:17f544fcad6f 21
martlefebvre94 0:17f544fcad6f 22 #define REG_OUT_X_MSB 0x01
martlefebvre94 0:17f544fcad6f 23 #define REG_OUT_Y_MSB 0x03
martlefebvre94 0:17f544fcad6f 24 #define REG_OUT_Z_MSB 0x05
martlefebvre94 0:17f544fcad6f 25
martlefebvre94 0:17f544fcad6f 26 #define FLASH_NO_ACQ_DONE 0
martlefebvre94 0:17f544fcad6f 27 #define FLASH_ACQ_DONE 1
martlefebvre94 0:17f544fcad6f 28 #define ERASE_FLASH_ERROR -1
martlefebvre94 0:17f544fcad6f 29 #define WRITE_FLASH_ERROR -2
martlefebvre94 0:17f544fcad6f 30
martlefebvre94 0:17f544fcad6f 31 #define SECTOR_SIZE 1024
martlefebvre94 0:17f544fcad6f 32 #define RESERVED_SECTOR 32
martlefebvre94 0:17f544fcad6f 33
martlefebvre94 0:17f544fcad6f 34 #define ACQ_TIMER_PERIOD 0.01 // Time between 2 acquisitions (here 10 mSec)
martlefebvre94 0:17f544fcad6f 35
martlefebvre94 0:17f544fcad6f 36 typedef struct{
martlefebvre94 0:17f544fcad6f 37 int16_t X;
martlefebvre94 0:17f544fcad6f 38 int16_t Y;
martlefebvre94 0:17f544fcad6f 39 int16_t Z;
martlefebvre94 0:17f544fcad6f 40 } Accel_Data;
martlefebvre94 0:17f544fcad6f 41
martlefebvre94 0:17f544fcad6f 42 // --- Setup I2C for MMA8451
martlefebvre94 0:17f544fcad6f 43 MMA8451Q my8451(PTE25, PTE24, MMA8451_I2C_ADDRESS);
martlefebvre94 0:17f544fcad6f 44
martlefebvre94 0:17f544fcad6f 45 // --- Set Serial Port
martlefebvre94 0:17f544fcad6f 46 Serial Host_Comm(USBTX, USBRX); // tx, rxSerial pc(USBTX, USBRX); // tx, rx
martlefebvre94 0:17f544fcad6f 47
martlefebvre94 0:17f544fcad6f 48 Ticker myTick_Acq; // Periodical timer for Acquisition
martlefebvre94 0:17f544fcad6f 49
martlefebvre94 0:17f544fcad6f 50 DigitalOut Led_Red(LED1); // Define I/O for Leds
martlefebvre94 0:17f544fcad6f 51 DigitalOut Led_Green(LED2);
martlefebvre94 0:17f544fcad6f 52 DigitalOut Led_Blue(LED3);
martlefebvre94 0:17f544fcad6f 53
martlefebvre94 0:17f544fcad6f 54 DigitalOut Accel_Enable(PTA13);
martlefebvre94 0:17f544fcad6f 55
martlefebvre94 0:17f544fcad6f 56 DigitalOut Start_Pulse_Out(PTC9); // Used to enter/exit Acquisition mode
martlefebvre94 0:17f544fcad6f 57 DigitalIn Start_Pulse_In(PTC11); // ShortPin J1_15 and J1_16 to enter in Acq_Mode
martlefebvre94 0:17f544fcad6f 58
martlefebvre94 0:17f544fcad6f 59
martlefebvre94 0:17f544fcad6f 60 // Globale variable
martlefebvre94 0:17f544fcad6f 61 volatile bool bTimer; // 1 means a Timer tick is done
martlefebvre94 0:17f544fcad6f 62
martlefebvre94 0:17f544fcad6f 63 int Flash_Base_Address = RESERVED_SECTOR * SECTOR_SIZE ; // Store Flash Base Adresse with 32K reserved for Application Code
martlefebvre94 0:17f544fcad6f 64 int Nb_Sector;
martlefebvre94 0:17f544fcad6f 65 uint32_t KL25_Flash_Size;
martlefebvre94 0:17f544fcad6f 66
martlefebvre94 0:17f544fcad6f 67 // Function Declaration
martlefebvre94 0:17f544fcad6f 68
martlefebvre94 0:17f544fcad6f 69 void Clear_Led(void);
martlefebvre94 0:17f544fcad6f 70 int Acquisition_Flash(void);
martlefebvre94 0:17f544fcad6f 71 int Read_Data_Logging(void);
martlefebvre94 0:17f544fcad6f 72 bool Check_Jumper(void);
martlefebvre94 0:17f544fcad6f 73 void myTimer_Acq_Task(void);
martlefebvre94 0:17f544fcad6f 74 void Acquisition_Task(void);
martlefebvre94 0:17f544fcad6f 75 void Read_Task(void);
martlefebvre94 0:17f544fcad6f 76
martlefebvre94 0:17f544fcad6f 77 extern IAPCode verify_erased(int address, unsigned int length);
martlefebvre94 0:17f544fcad6f 78
martlefebvre94 0:17f544fcad6f 79 int main() {
martlefebvre94 0:17f544fcad6f 80
martlefebvre94 0:17f544fcad6f 81 uint8_t Count;
martlefebvre94 0:17f544fcad6f 82
martlefebvre94 0:17f544fcad6f 83 Start_Pulse_In.mode(PullNone); // Input Pin is programmed as floating
martlefebvre94 0:17f544fcad6f 84 Accel_Enable = DISABLE_STATE; // Turn Accel Enable to disabled state
martlefebvre94 0:17f544fcad6f 85
martlefebvre94 0:17f544fcad6f 86 // --- Baud rate setting
martlefebvre94 0:17f544fcad6f 87 Host_Comm.baud(115200);
martlefebvre94 0:17f544fcad6f 88
martlefebvre94 0:17f544fcad6f 89 Clear_Led();
martlefebvre94 0:17f544fcad6f 90
martlefebvre94 0:17f544fcad6f 91 KL25_Flash_Size = flash_size(); // Get Size of KL25 Embedded Flash
martlefebvre94 0:17f544fcad6f 92 Nb_Sector = (KL25_Flash_Size / SECTOR_SIZE) - RESERVED_SECTOR; // Reserve Max 32K for App Code
martlefebvre94 0:17f544fcad6f 93
martlefebvre94 0:17f544fcad6f 94 myTick_Acq.attach(&myTimer_Acq_Task, ACQ_TIMER_PERIOD);
martlefebvre94 0:17f544fcad6f 95
martlefebvre94 0:17f544fcad6f 96 Host_Comm.printf("\n\rLELEC2811 Accelerometer Logger V1.0 UCL 2014\n\r");
martlefebvre94 0:17f544fcad6f 97
martlefebvre94 0:17f544fcad6f 98 for (;;)
martlefebvre94 0:17f544fcad6f 99 {
martlefebvre94 0:17f544fcad6f 100 if (Check_Jumper() == JUMPER_PRESENT)
martlefebvre94 0:17f544fcad6f 101 {
martlefebvre94 0:17f544fcad6f 102 Clear_Led();
martlefebvre94 0:17f544fcad6f 103
martlefebvre94 0:17f544fcad6f 104 Count = 5;
martlefebvre94 0:17f544fcad6f 105 while (Count !=0)
martlefebvre94 0:17f544fcad6f 106 {
martlefebvre94 0:17f544fcad6f 107 if (Check_Jumper() == JUMPER_PRESENT)
martlefebvre94 0:17f544fcad6f 108 {
martlefebvre94 0:17f544fcad6f 109 Led_Blue = LED_ON; // Blink to alert user "Enter in Acquisition"
martlefebvre94 0:17f544fcad6f 110 wait_ms(900);
martlefebvre94 0:17f544fcad6f 111 Led_Blue = LED_OFF;
martlefebvre94 0:17f544fcad6f 112 wait_ms(100);
martlefebvre94 0:17f544fcad6f 113 Count --;
martlefebvre94 0:17f544fcad6f 114 if (Count == 0)
martlefebvre94 0:17f544fcad6f 115 {
martlefebvre94 0:17f544fcad6f 116 Acquisition_Task();
martlefebvre94 0:17f544fcad6f 117 }
martlefebvre94 0:17f544fcad6f 118 }
martlefebvre94 0:17f544fcad6f 119 else
martlefebvre94 0:17f544fcad6f 120 {
martlefebvre94 0:17f544fcad6f 121 Count = 0;
martlefebvre94 0:17f544fcad6f 122 }
martlefebvre94 0:17f544fcad6f 123 }
martlefebvre94 0:17f544fcad6f 124 }
martlefebvre94 0:17f544fcad6f 125 else
martlefebvre94 0:17f544fcad6f 126 {
martlefebvre94 0:17f544fcad6f 127 Read_Task();
martlefebvre94 0:17f544fcad6f 128 }
martlefebvre94 0:17f544fcad6f 129 }
martlefebvre94 0:17f544fcad6f 130 }
martlefebvre94 0:17f544fcad6f 131
martlefebvre94 0:17f544fcad6f 132 void Read_Task()
martlefebvre94 0:17f544fcad6f 133 {
martlefebvre94 0:17f544fcad6f 134 char host_cmd;
martlefebvre94 0:17f544fcad6f 135 IAPCode Flash_State;
martlefebvre94 0:17f544fcad6f 136 bool bAcq_Done;
martlefebvre94 0:17f544fcad6f 137
martlefebvre94 0:17f544fcad6f 138 Flash_State = verify_erased(Flash_Base_Address, KL25_Flash_Size - (RESERVED_SECTOR * SECTOR_SIZE));
martlefebvre94 0:17f544fcad6f 139 if (Flash_State == 0) // Virgin Flash ?
martlefebvre94 0:17f544fcad6f 140 {
martlefebvre94 0:17f544fcad6f 141 bAcq_Done = 0;
martlefebvre94 0:17f544fcad6f 142 }
martlefebvre94 0:17f544fcad6f 143 else
martlefebvre94 0:17f544fcad6f 144 {
martlefebvre94 0:17f544fcad6f 145 bAcq_Done = 1;
martlefebvre94 0:17f544fcad6f 146 }
martlefebvre94 0:17f544fcad6f 147
martlefebvre94 0:17f544fcad6f 148 Clear_Led();
martlefebvre94 0:17f544fcad6f 149 wait_ms(500);
martlefebvre94 0:17f544fcad6f 150
martlefebvre94 0:17f544fcad6f 151 if (bAcq_Done == 1)
martlefebvre94 0:17f544fcad6f 152 {
martlefebvre94 0:17f544fcad6f 153 Led_Green = LED_ON;
martlefebvre94 0:17f544fcad6f 154 Host_Comm.putc('1');
martlefebvre94 0:17f544fcad6f 155 }
martlefebvre94 0:17f544fcad6f 156 else
martlefebvre94 0:17f544fcad6f 157 {
martlefebvre94 0:17f544fcad6f 158 Led_Red = LED_ON;
martlefebvre94 0:17f544fcad6f 159 Host_Comm.putc('0');
martlefebvre94 0:17f544fcad6f 160 }
martlefebvre94 0:17f544fcad6f 161
martlefebvre94 0:17f544fcad6f 162 if(Host_Comm.readable()) // Did we receive a char from Host ?
martlefebvre94 0:17f544fcad6f 163 {
martlefebvre94 0:17f544fcad6f 164 host_cmd = Host_Comm.getc(); // Get it
martlefebvre94 0:17f544fcad6f 165
martlefebvre94 0:17f544fcad6f 166 if ((host_cmd == 'R') || (host_cmd == 'r')) // Read Flash Command ?
martlefebvre94 0:17f544fcad6f 167 {
martlefebvre94 0:17f544fcad6f 168 Read_Data_Logging(); // Read and send acquisition data
martlefebvre94 0:17f544fcad6f 169 }
martlefebvre94 0:17f544fcad6f 170 }
martlefebvre94 0:17f544fcad6f 171 wait_ms(50);
martlefebvre94 0:17f544fcad6f 172 }
martlefebvre94 0:17f544fcad6f 173
martlefebvre94 0:17f544fcad6f 174 void Acquisition_Task()
martlefebvre94 0:17f544fcad6f 175 {
martlefebvre94 0:17f544fcad6f 176 int Acq_Status;
martlefebvre94 0:17f544fcad6f 177
martlefebvre94 0:17f544fcad6f 178 Clear_Led();
martlefebvre94 0:17f544fcad6f 179
martlefebvre94 0:17f544fcad6f 180 Acq_Status = Acquisition_Flash();
martlefebvre94 0:17f544fcad6f 181
martlefebvre94 0:17f544fcad6f 182 Clear_Led();
martlefebvre94 0:17f544fcad6f 183
martlefebvre94 0:17f544fcad6f 184 while (Check_Jumper() == JUMPER_PRESENT)
martlefebvre94 0:17f544fcad6f 185 {
martlefebvre94 0:17f544fcad6f 186 if (Acq_Status != FLASH_ACQ_DONE)
martlefebvre94 0:17f544fcad6f 187 {
martlefebvre94 0:17f544fcad6f 188 Led_Red = !Led_Red;
martlefebvre94 0:17f544fcad6f 189 }
martlefebvre94 0:17f544fcad6f 190 else
martlefebvre94 0:17f544fcad6f 191 {
martlefebvre94 0:17f544fcad6f 192 Led_Green = !Led_Green;
martlefebvre94 0:17f544fcad6f 193 }
martlefebvre94 0:17f544fcad6f 194 wait_ms(100);
martlefebvre94 0:17f544fcad6f 195 }
martlefebvre94 0:17f544fcad6f 196 }
martlefebvre94 0:17f544fcad6f 197
martlefebvre94 0:17f544fcad6f 198 void Clear_Led(void)
martlefebvre94 0:17f544fcad6f 199 {
martlefebvre94 0:17f544fcad6f 200 Led_Red = LED_OFF;
martlefebvre94 0:17f544fcad6f 201 Led_Green = LED_OFF;
martlefebvre94 0:17f544fcad6f 202 Led_Blue = LED_OFF ; // Bug on board : Turning On Blue Led decrease consumption...
martlefebvre94 0:17f544fcad6f 203 }
martlefebvre94 0:17f544fcad6f 204
martlefebvre94 0:17f544fcad6f 205 bool Check_Jumper() // If J1_15 and J1_16 connected together -> return JUMPER_PRESENT
martlefebvre94 0:17f544fcad6f 206 {
martlefebvre94 0:17f544fcad6f 207 uint8_t i;
martlefebvre94 0:17f544fcad6f 208
martlefebvre94 0:17f544fcad6f 209 for (i = 0 ; i < 2 ; i ++)
martlefebvre94 0:17f544fcad6f 210 {
martlefebvre94 0:17f544fcad6f 211 Start_Pulse_Out = LEVEL_1;
martlefebvre94 0:17f544fcad6f 212 wait_ms(1);
martlefebvre94 0:17f544fcad6f 213 if (Start_Pulse_In != LEVEL_1)
martlefebvre94 0:17f544fcad6f 214 {
martlefebvre94 0:17f544fcad6f 215 return NO_JUMPER;
martlefebvre94 0:17f544fcad6f 216 }
martlefebvre94 0:17f544fcad6f 217
martlefebvre94 0:17f544fcad6f 218 Start_Pulse_Out = LEVEL_0;
martlefebvre94 0:17f544fcad6f 219 wait_ms(1);
martlefebvre94 0:17f544fcad6f 220 if (Start_Pulse_In != LEVEL_0)
martlefebvre94 0:17f544fcad6f 221 {
martlefebvre94 0:17f544fcad6f 222 return NO_JUMPER;
martlefebvre94 0:17f544fcad6f 223 }
martlefebvre94 0:17f544fcad6f 224 }
martlefebvre94 0:17f544fcad6f 225 return JUMPER_PRESENT;
martlefebvre94 0:17f544fcad6f 226 }
martlefebvre94 0:17f544fcad6f 227
martlefebvre94 0:17f544fcad6f 228 int Acquisition_Flash(void)
martlefebvre94 0:17f544fcad6f 229 {
martlefebvre94 0:17f544fcad6f 230 int Status;
martlefebvre94 0:17f544fcad6f 231 int Flash_Ptr ;
martlefebvre94 0:17f544fcad6f 232 uint8_t Data_Ptr;
martlefebvre94 0:17f544fcad6f 233 Accel_Data myData[2];
martlefebvre94 0:17f544fcad6f 234 int Led_Counter;
martlefebvre94 0:17f544fcad6f 235
martlefebvre94 0:17f544fcad6f 236
martlefebvre94 0:17f544fcad6f 237 for (Flash_Ptr = Flash_Base_Address ; Flash_Ptr < KL25_Flash_Size ; Flash_Ptr += 0x400)
martlefebvre94 0:17f544fcad6f 238 {
martlefebvre94 0:17f544fcad6f 239 Status = erase_sector(Flash_Ptr); // Erase sector
martlefebvre94 0:17f544fcad6f 240
martlefebvre94 0:17f544fcad6f 241 if (Status !=0)
martlefebvre94 0:17f544fcad6f 242 {
martlefebvre94 0:17f544fcad6f 243 return ERASE_FLASH_ERROR;
martlefebvre94 0:17f544fcad6f 244 }
martlefebvre94 0:17f544fcad6f 245 }
martlefebvre94 0:17f544fcad6f 246
martlefebvre94 0:17f544fcad6f 247 Flash_Ptr = Flash_Base_Address;
martlefebvre94 0:17f544fcad6f 248
martlefebvre94 0:17f544fcad6f 249 Led_Blue = LED_ON;
martlefebvre94 0:17f544fcad6f 250
martlefebvre94 0:17f544fcad6f 251 Led_Counter = 0;
martlefebvre94 0:17f544fcad6f 252 Data_Ptr = 0;
martlefebvre94 0:17f544fcad6f 253 while (Flash_Ptr < KL25_Flash_Size ) // Acq Loop
martlefebvre94 0:17f544fcad6f 254 {
martlefebvre94 0:17f544fcad6f 255 while (bTimer == 0) // Wait Acq Tick Timer
martlefebvre94 0:17f544fcad6f 256 {
martlefebvre94 0:17f544fcad6f 257
martlefebvre94 0:17f544fcad6f 258 }
martlefebvre94 0:17f544fcad6f 259 bTimer = 0;
martlefebvre94 0:17f544fcad6f 260
martlefebvre94 0:17f544fcad6f 261 Accel_Enable = ENABLE_STATE; // Rising Edge -> Start Accel Measure
martlefebvre94 0:17f544fcad6f 262
martlefebvre94 0:17f544fcad6f 263 if ((float) Led_Counter * ACQ_TIMER_PERIOD == 1.0) // Blink at 1 Hz
martlefebvre94 0:17f544fcad6f 264 {
martlefebvre94 0:17f544fcad6f 265 Led_Counter = 0;
martlefebvre94 0:17f544fcad6f 266 Led_Blue = !Led_Blue;
martlefebvre94 0:17f544fcad6f 267 }
martlefebvre94 0:17f544fcad6f 268
martlefebvre94 0:17f544fcad6f 269 myData[Data_Ptr].X = my8451.getAccAxis(REG_OUT_X_MSB);
martlefebvre94 0:17f544fcad6f 270 myData[Data_Ptr].Y = my8451.getAccAxis(REG_OUT_Y_MSB);
martlefebvre94 0:17f544fcad6f 271 myData[Data_Ptr].Z = my8451.getAccAxis(REG_OUT_Z_MSB);
martlefebvre94 0:17f544fcad6f 272
martlefebvre94 0:17f544fcad6f 273 Led_Counter++;
martlefebvre94 0:17f544fcad6f 274
martlefebvre94 0:17f544fcad6f 275 Accel_Enable = DISABLE_STATE;
martlefebvre94 0:17f544fcad6f 276
martlefebvre94 0:17f544fcad6f 277 //Host_Comm.printf("\n\r%x\tX = %f", Flash_Ptr, float(myData[Data_Ptr].X)*4.0/4096.0 );
martlefebvre94 0:17f544fcad6f 278 //Host_Comm.printf("\tY = %f", float(myData[Data_Ptr].Y)*4.0/4096.0 );
martlefebvre94 0:17f544fcad6f 279 //Host_Comm.printf("\tZ = %f", float(myData[Data_Ptr].Z)*4.0/4096.0 );
martlefebvre94 0:17f544fcad6f 280
martlefebvre94 0:17f544fcad6f 281 Data_Ptr ++;
martlefebvre94 0:17f544fcad6f 282
martlefebvre94 0:17f544fcad6f 283 if (Data_Ptr == 2)// Save 2 acquistions -> 2 * 3 * 2 bytes = 12 bytes
martlefebvre94 0:17f544fcad6f 284 {
martlefebvre94 0:17f544fcad6f 285 Data_Ptr = 0;
martlefebvre94 0:17f544fcad6f 286 Status = program_flash(Flash_Ptr, (char *) &myData[0].X, 4); // Write 4 bytes in the Flash
martlefebvre94 0:17f544fcad6f 287 if (Status !=0)
martlefebvre94 0:17f544fcad6f 288 {
martlefebvre94 0:17f544fcad6f 289 return WRITE_FLASH_ERROR;
martlefebvre94 0:17f544fcad6f 290 }
martlefebvre94 0:17f544fcad6f 291
martlefebvre94 0:17f544fcad6f 292 Flash_Ptr += 4;
martlefebvre94 0:17f544fcad6f 293 Status = program_flash(Flash_Ptr, (char *) &myData[0].Z, 4); // Write 4 bytes in the Flash
martlefebvre94 0:17f544fcad6f 294 if (Status !=0)
martlefebvre94 0:17f544fcad6f 295 {
martlefebvre94 0:17f544fcad6f 296 return WRITE_FLASH_ERROR;
martlefebvre94 0:17f544fcad6f 297 }
martlefebvre94 0:17f544fcad6f 298
martlefebvre94 0:17f544fcad6f 299 Flash_Ptr += 4;
martlefebvre94 0:17f544fcad6f 300 Status = program_flash(Flash_Ptr, (char *) &myData[0].Y, 4); // Bug corrected 23/11/2016
martlefebvre94 0:17f544fcad6f 301 if (Status !=0)
martlefebvre94 0:17f544fcad6f 302 {
martlefebvre94 0:17f544fcad6f 303 return WRITE_FLASH_ERROR;
martlefebvre94 0:17f544fcad6f 304 }
martlefebvre94 0:17f544fcad6f 305
martlefebvre94 0:17f544fcad6f 306 Flash_Ptr += 4;
martlefebvre94 0:17f544fcad6f 307
martlefebvre94 0:17f544fcad6f 308 if ((Flash_Ptr & 0x3FC) == 0x3FC)
martlefebvre94 0:17f544fcad6f 309 {
martlefebvre94 0:17f544fcad6f 310 Flash_Ptr += 4; //170 * 6 = 1020 ---> skip 4 last bytes of each sector of 1024 bytes
martlefebvre94 0:17f544fcad6f 311 }
martlefebvre94 0:17f544fcad6f 312 }
martlefebvre94 0:17f544fcad6f 313 if (Check_Jumper() != JUMPER_PRESENT) // If Jumper remoded -> Stop Acquisition
martlefebvre94 0:17f544fcad6f 314 {
martlefebvre94 0:17f544fcad6f 315 return FLASH_ACQ_DONE ;
martlefebvre94 0:17f544fcad6f 316 }
martlefebvre94 0:17f544fcad6f 317 }
martlefebvre94 0:17f544fcad6f 318 return FLASH_ACQ_DONE ;
martlefebvre94 0:17f544fcad6f 319 }
martlefebvre94 0:17f544fcad6f 320
martlefebvre94 0:17f544fcad6f 321 int Read_Data_Logging()
martlefebvre94 0:17f544fcad6f 322 {
martlefebvre94 0:17f544fcad6f 323 int16_t *data = (int16_t *) Flash_Base_Address; // uint16 pointer of data stored in Flash
martlefebvre94 0:17f544fcad6f 324 int Flash_Ptr;
martlefebvre94 0:17f544fcad6f 325 char cmd;
martlefebvre94 0:17f544fcad6f 326 int Record_Counter;
martlefebvre94 0:17f544fcad6f 327 float X_Val, Y_Val, Z_Val;
martlefebvre94 0:17f544fcad6f 328 int16_t Raw_X, Raw_Y, Raw_Z;
martlefebvre94 0:17f544fcad6f 329 int Max_Record;
martlefebvre94 0:17f544fcad6f 330
martlefebvre94 0:17f544fcad6f 331 Clear_Led();
martlefebvre94 0:17f544fcad6f 332
martlefebvre94 0:17f544fcad6f 333 Max_Record = Nb_Sector * (SECTOR_SIZE / sizeof(Accel_Data));
martlefebvre94 0:17f544fcad6f 334 Record_Counter = 0;
martlefebvre94 0:17f544fcad6f 335 Flash_Ptr = 0;
martlefebvre94 0:17f544fcad6f 336
martlefebvre94 0:17f544fcad6f 337 //Host_Comm.printf("\n\rBegin of Data");
martlefebvre94 0:17f544fcad6f 338
martlefebvre94 0:17f544fcad6f 339 while (Record_Counter < Max_Record)
martlefebvre94 0:17f544fcad6f 340 {
martlefebvre94 0:17f544fcad6f 341 Led_Green = !Led_Green;
martlefebvre94 0:17f544fcad6f 342 Led_Blue = !Led_Green;
martlefebvre94 0:17f544fcad6f 343
martlefebvre94 0:17f544fcad6f 344 if(Host_Comm.readable())
martlefebvre94 0:17f544fcad6f 345 {
martlefebvre94 0:17f544fcad6f 346 cmd = Host_Comm.getc();
martlefebvre94 0:17f544fcad6f 347 if ((cmd == 'S') || (cmd == 's')) // Receiving 'S' or 's' means stop Read Flash
martlefebvre94 0:17f544fcad6f 348 {
martlefebvre94 0:17f544fcad6f 349 Clear_Led();
martlefebvre94 0:17f544fcad6f 350 return 0;
martlefebvre94 0:17f544fcad6f 351 }
martlefebvre94 0:17f544fcad6f 352 }
martlefebvre94 0:17f544fcad6f 353
martlefebvre94 0:17f544fcad6f 354 Record_Counter ++;
martlefebvre94 0:17f544fcad6f 355
martlefebvre94 0:17f544fcad6f 356 Raw_X = data[Flash_Ptr++];
martlefebvre94 0:17f544fcad6f 357 Raw_Y = data[Flash_Ptr++];
martlefebvre94 0:17f544fcad6f 358 Raw_Z = data[Flash_Ptr++];
martlefebvre94 0:17f544fcad6f 359
martlefebvre94 0:17f544fcad6f 360 if ((Raw_X == -1) && (Raw_Y == -1) && (Raw_Z == -1)) // Valid data ? (!= 0xFFFFFFFF from empty Flash sector)
martlefebvre94 0:17f544fcad6f 361 {
martlefebvre94 0:17f544fcad6f 362 }
martlefebvre94 0:17f544fcad6f 363 else
martlefebvre94 0:17f544fcad6f 364 {
martlefebvre94 0:17f544fcad6f 365 X_Val = float(Raw_X) * 4.0/4096.0;
martlefebvre94 0:17f544fcad6f 366 Y_Val = float(Raw_Y) * 4.0/4096.0;
martlefebvre94 0:17f544fcad6f 367 Z_Val = float(Raw_Z) * 4.0/4096.0;
martlefebvre94 0:17f544fcad6f 368
martlefebvre94 0:17f544fcad6f 369 Host_Comm.printf("\n\r%d\tX=%.4f\tY=%.4f\tZ=%.4f", Record_Counter, X_Val, Y_Val, Z_Val);
martlefebvre94 0:17f544fcad6f 370 }
martlefebvre94 0:17f544fcad6f 371
martlefebvre94 0:17f544fcad6f 372 if ((Flash_Ptr & 0x1FE) == 0x1FE)
martlefebvre94 0:17f544fcad6f 373 {
martlefebvre94 0:17f544fcad6f 374 Flash_Ptr +=2; // skip the last bytes at the end of a sector
martlefebvre94 0:17f544fcad6f 375 }
martlefebvre94 0:17f544fcad6f 376 }
martlefebvre94 0:17f544fcad6f 377 //Host_Comm.printf("\n\rEnd of Data");
martlefebvre94 0:17f544fcad6f 378 Clear_Led();
martlefebvre94 0:17f544fcad6f 379 return 0;
martlefebvre94 0:17f544fcad6f 380 }
martlefebvre94 0:17f544fcad6f 381
martlefebvre94 0:17f544fcad6f 382 /* Interrupt Task */
martlefebvre94 0:17f544fcad6f 383 void myTimer_Acq_Task()
martlefebvre94 0:17f544fcad6f 384 {
martlefebvre94 0:17f544fcad6f 385 bTimer = 1;
martlefebvre94 0:17f544fcad6f 386 }