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