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