Working version. ADXL355 data saved to microSD.

Committer:
DUR
Date:
Fri Sep 29 12:53:50 2017 +0000
Revision:
0:e2579e8d9b13
Working version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DUR 0:e2579e8d9b13 1 #include "mbed.h"
DUR 0:e2579e8d9b13 2 #include "rtos.h"
DUR 0:e2579e8d9b13 3 #include "SDBlockDevice.h"
DUR 0:e2579e8d9b13 4 #include "FATFileSystem.h"
DUR 0:e2579e8d9b13 5 #include "utility.h"
DUR 0:e2579e8d9b13 6
DUR 0:e2579e8d9b13 7
DUR 0:e2579e8d9b13 8 /*
DUR 0:e2579e8d9b13 9 FLAG SCRITTURA SU SD CARD IN CORSO
DUR 0:e2579e8d9b13 10 */
DUR 0:e2579e8d9b13 11 bool volatile start_writing;
DUR 0:e2579e8d9b13 12 /*
DUR 0:e2579e8d9b13 13 PULSANTE
DUR 0:e2579e8d9b13 14 */
DUR 0:e2579e8d9b13 15 bool volatile button_int_f;/* Flag pulsante pigiato */
DUR 0:e2579e8d9b13 16 InterruptIn user_button(USER_KEY_PIN);
DUR 0:e2579e8d9b13 17 /*
DUR 0:e2579e8d9b13 18 OGGETTO FILE
DUR 0:e2579e8d9b13 19 */
DUR 0:e2579e8d9b13 20 FILE *fp = NULL;
DUR 0:e2579e8d9b13 21 uint32_t file_index;//Indice che conteggia in maniera progressiva i record sul file
DUR 0:e2579e8d9b13 22 /*
DUR 0:e2579e8d9b13 23 SD Card
DUR 0:e2579e8d9b13 24 */
DUR 0:e2579e8d9b13 25 SDBlockDevice sd(SDCARD_MOSI, SDCARD_MISO, SDCARD_SCK, SDCARD_CS, 8000000);
DUR 0:e2579e8d9b13 26 FATFileSystem fs("fs");
DUR 0:e2579e8d9b13 27 /*
DUR 0:e2579e8d9b13 28 Bus SPI
DUR 0:e2579e8d9b13 29 */
DUR 0:e2579e8d9b13 30 SPI spiHandler(SPI_MOSI, SPI_MISO, SPI_SCK);
DUR 0:e2579e8d9b13 31 DigitalOut spiADXL355cs(ADXL355_CS);// Pin di CS ADXL355
DUR 0:e2579e8d9b13 32 /*
DUR 0:e2579e8d9b13 33 ADXL355
DUR 0:e2579e8d9b13 34 */
DUR 0:e2579e8d9b13 35 adxl355_handler adxl355_config =//Handler accelerometro
DUR 0:e2579e8d9b13 36 {
DUR 0:e2579e8d9b13 37 .measure_range = adxl355_2g,
DUR 0:e2579e8d9b13 38 .out_data_rate = adxl355_250hz,
DUR 0:e2579e8d9b13 39 .drdy_pin = ADXL355_DRDY,
DUR 0:e2579e8d9b13 40 .int1_pin = NULL,
DUR 0:e2579e8d9b13 41 .int2_pin = NULL,
DUR 0:e2579e8d9b13 42 .int_config = {}
DUR 0:e2579e8d9b13 43 };
DUR 0:e2579e8d9b13 44 InterruptIn adxl355_int((PinName)adxl355_config.drdy_pin);// Interrupt su pin accelerometro
DUR 0:e2579e8d9b13 45 uint32_t adxl355_int_timestamp;//Timestamp registrato nella routine di interrupt
DUR 0:e2579e8d9b13 46 uint8_t adxl355_int_index;//Numero di interrupt consecutivi
DUR 0:e2579e8d9b13 47 uint16_t adxl355_newData_error = 0;//Errore metodo
DUR 0:e2579e8d9b13 48 //uint8_t adxl355_newData_sample = 0;//Conteggio dei sample acquisiti
DUR 0:e2579e8d9b13 49 uint32_t adxl355_newData_timestamp;//Timestamp rilevato al 5° sample
DUR 0:e2579e8d9b13 50 /* Dati locali temporanei ADXL355 */
DUR 0:e2579e8d9b13 51 //uint32_t adxl355_rawX_tmp = 0;
DUR 0:e2579e8d9b13 52 //uint32_t adxl355_rawY_tmp = 0;
DUR 0:e2579e8d9b13 53 //uint32_t adxl355_rawZ_tmp = 0;
DUR 0:e2579e8d9b13 54 //uint32_t adxl355_rawT_tmp = 0;
DUR 0:e2579e8d9b13 55 /*
DUR 0:e2579e8d9b13 56 DEBUG PIN
DUR 0:e2579e8d9b13 57 */
DUR 0:e2579e8d9b13 58 DigitalOut BLUpin(DBG_PIN_2);
DUR 0:e2579e8d9b13 59 DigitalOut YELLOWpin(DBG_PIN_4);
DUR 0:e2579e8d9b13 60 DigitalOut GREENpin(DBG_PIN);
DUR 0:e2579e8d9b13 61 DigitalOut VIOLApin(DBG_PIN_3);
DUR 0:e2579e8d9b13 62 DigitalOut led_pin(STATUS_LED_PIN);
DUR 0:e2579e8d9b13 63 /*
DUR 0:e2579e8d9b13 64 DEFINIZIONE CODA DATI
DUR 0:e2579e8d9b13 65 */
DUR 0:e2579e8d9b13 66 MemoryPool<record_t, RECORD_NUMBER*RECORD_SIZE> mpool;
DUR 0:e2579e8d9b13 67 Queue<record_t, RECORD_NUMBER*RECORD_SIZE> queue;
DUR 0:e2579e8d9b13 68 /*
DUR 0:e2579e8d9b13 69 SCRITTURA SU SD CARD
DUR 0:e2579e8d9b13 70 */
DUR 0:e2579e8d9b13 71 void writeOnSDcard(void)
DUR 0:e2579e8d9b13 72 {
DUR 0:e2579e8d9b13 73 while(1)
DUR 0:e2579e8d9b13 74 {
DUR 0:e2579e8d9b13 75 Thread::wait(10);
DUR 0:e2579e8d9b13 76 osEvent evt = queue.get();
DUR 0:e2579e8d9b13 77 if((evt.status == osEventMessage) && (start_writing == true))
DUR 0:e2579e8d9b13 78 {
DUR 0:e2579e8d9b13 79 record_t *message = (record_t*)evt.value.p;
DUR 0:e2579e8d9b13 80 BLUpin = 1;
DUR 0:e2579e8d9b13 81 fprintf(fp, "%06d;%04x;%08d;%05x;%05x;%05x;%03x;%06x;%06x;%06x;%06x;%06x;%06x \r\n",
DUR 0:e2579e8d9b13 82 message->index, //#
DUR 0:e2579e8d9b13 83 message->error, //Error
DUR 0:e2579e8d9b13 84 message->timestamp, //Timestamp
DUR 0:e2579e8d9b13 85 message->adxl355_rawX, //ACC_DIG_3A_X
DUR 0:e2579e8d9b13 86 message->adxl355_rawY, //ACC_DIG_3A_Y
DUR 0:e2579e8d9b13 87 message->adxl355_rawZ, //ACC_DIG_3A_Z
DUR 0:e2579e8d9b13 88 message->adxl355_rawT, //ACC_DIG_3A_TEMP
DUR 0:e2579e8d9b13 89 message->adxl354_rawX, //ACC_AN_3A_X
DUR 0:e2579e8d9b13 90 message->adxl354_rawY, //ACC_AN_3A_Y
DUR 0:e2579e8d9b13 91 message->adxl354_rawZ, //ACC_AN_3A_Z
DUR 0:e2579e8d9b13 92 message->adxl354_rawT, //ACC_AN_3A_TEMP
DUR 0:e2579e8d9b13 93 message->colibrys_rawVal, //ACC_AN_1A_VAL
DUR 0:e2579e8d9b13 94 message->colibrys_rawTEMPval //ACC_AN_1A_TEMP
DUR 0:e2579e8d9b13 95 );
DUR 0:e2579e8d9b13 96 BLUpin = 0;
DUR 0:e2579e8d9b13 97 mpool.free(message);
DUR 0:e2579e8d9b13 98 }else{}
DUR 0:e2579e8d9b13 99
DUR 0:e2579e8d9b13 100 if((button_int_f == true)&&(start_writing == true))
DUR 0:e2579e8d9b13 101 {
DUR 0:e2579e8d9b13 102 fprintf(fp,"DATA END \r\n");
DUR 0:e2579e8d9b13 103 fclose(fp); // DO THIS AT THE END!!!
DUR 0:e2579e8d9b13 104 sd.deinit();
DUR 0:e2579e8d9b13 105 fs.unmount();
DUR 0:e2579e8d9b13 106 start_writing = false;
DUR 0:e2579e8d9b13 107
DUR 0:e2579e8d9b13 108 Thread::wait(100);
DUR 0:e2579e8d9b13 109 if(adxl355_start_acquisition(adxl355_none) == 0)
DUR 0:e2579e8d9b13 110 {
DUR 0:e2579e8d9b13 111 led_pin = 0;
DUR 0:e2579e8d9b13 112 YELLOWpin = 1;
DUR 0:e2579e8d9b13 113 }else{}
DUR 0:e2579e8d9b13 114
DUR 0:e2579e8d9b13 115 }else{}
DUR 0:e2579e8d9b13 116 }
DUR 0:e2579e8d9b13 117 }
DUR 0:e2579e8d9b13 118 /*
DUR 0:e2579e8d9b13 119 THREAD SCRITTURA SU SD CARD
DUR 0:e2579e8d9b13 120 */
DUR 0:e2579e8d9b13 121 Thread writeSDthread(writeOnSDcard);
DUR 0:e2579e8d9b13 122 /*
DUR 0:e2579e8d9b13 123 NUOVI DATI ADXL355
DUR 0:e2579e8d9b13 124 */
DUR 0:e2579e8d9b13 125 void adxl355_newData(void)
DUR 0:e2579e8d9b13 126 {
DUR 0:e2579e8d9b13 127 while (true)
DUR 0:e2579e8d9b13 128 { // Signal flags that are reported as event are automatically cleared.
DUR 0:e2579e8d9b13 129 Thread::signal_wait(ADXL355_SIGNAL);
DUR 0:e2579e8d9b13 130 GREENpin = 1;
DUR 0:e2579e8d9b13 131 if(adxl355_get_data(&adxl355_config) != 0)
DUR 0:e2579e8d9b13 132 {
DUR 0:e2579e8d9b13 133 adxl355_newData_error |= 0xFF00;
DUR 0:e2579e8d9b13 134 }else{}
DUR 0:e2579e8d9b13 135
DUR 0:e2579e8d9b13 136 file_index++;
DUR 0:e2579e8d9b13 137 adxl355_newData_timestamp = HAL_GetTick();
DUR 0:e2579e8d9b13 138 adxl355_newData_error |= (uint16_t)(adxl355_newData_timestamp - adxl355_int_timestamp);
DUR 0:e2579e8d9b13 139
DUR 0:e2579e8d9b13 140 record_t *message = mpool.alloc();
DUR 0:e2579e8d9b13 141 message->index = file_index;
DUR 0:e2579e8d9b13 142 message->error = adxl355_newData_error;
DUR 0:e2579e8d9b13 143 message->timestamp = adxl355_newData_timestamp;
DUR 0:e2579e8d9b13 144
DUR 0:e2579e8d9b13 145 message->adxl355_rawX = adxl355_raw_x_acc(&adxl355_config);
DUR 0:e2579e8d9b13 146 message->adxl355_rawY = adxl355_raw_y_acc(&adxl355_config);
DUR 0:e2579e8d9b13 147 message->adxl355_rawZ = adxl355_raw_z_acc(&adxl355_config);
DUR 0:e2579e8d9b13 148 message->adxl355_rawT = adxl355_raw_temp(&adxl355_config);
DUR 0:e2579e8d9b13 149
DUR 0:e2579e8d9b13 150 message->adxl354_rawX = 3;
DUR 0:e2579e8d9b13 151 message->adxl354_rawY = 3;
DUR 0:e2579e8d9b13 152 message->adxl354_rawZ = 3;
DUR 0:e2579e8d9b13 153 message->adxl354_rawT = 3;
DUR 0:e2579e8d9b13 154
DUR 0:e2579e8d9b13 155 message->colibrys_rawVal = 4;
DUR 0:e2579e8d9b13 156 message->colibrys_rawTEMPval = 4;
DUR 0:e2579e8d9b13 157
DUR 0:e2579e8d9b13 158 queue.put(message);
DUR 0:e2579e8d9b13 159
DUR 0:e2579e8d9b13 160 adxl355_newData_error = 0;
DUR 0:e2579e8d9b13 161
DUR 0:e2579e8d9b13 162 GREENpin = 0;
DUR 0:e2579e8d9b13 163 }
DUR 0:e2579e8d9b13 164 }
DUR 0:e2579e8d9b13 165 /*
DUR 0:e2579e8d9b13 166 THREAD NUOVI DATI ADXL355
DUR 0:e2579e8d9b13 167 */
DUR 0:e2579e8d9b13 168 Thread adxl355Thread(adxl355_newData);
DUR 0:e2579e8d9b13 169 /*
DUR 0:e2579e8d9b13 170 FUNZIONE DI GESTIONE DELL'INTERRUPT ADXL355
DUR 0:e2579e8d9b13 171 */
DUR 0:e2579e8d9b13 172 void adxl355_int_handler(void)
DUR 0:e2579e8d9b13 173 {
DUR 0:e2579e8d9b13 174 VIOLApin = 1;
DUR 0:e2579e8d9b13 175 adxl355_int_index++;
DUR 0:e2579e8d9b13 176 if(adxl355_int_index == 5)
DUR 0:e2579e8d9b13 177 {
DUR 0:e2579e8d9b13 178 adxl355_int_timestamp = HAL_GetTick();
DUR 0:e2579e8d9b13 179 adxl355_int_index = 0;
DUR 0:e2579e8d9b13 180 adxl355Thread.signal_set(ADXL355_SIGNAL);
DUR 0:e2579e8d9b13 181 }else{};
DUR 0:e2579e8d9b13 182 // adxl355Thread.signal_set(ADXL355_SIGNAL);
DUR 0:e2579e8d9b13 183 VIOLApin = 0;
DUR 0:e2579e8d9b13 184 }
DUR 0:e2579e8d9b13 185 /*
DUR 0:e2579e8d9b13 186 FUNZIONE DI GESTIONE INTERRUPT PULSANTE
DUR 0:e2579e8d9b13 187 */
DUR 0:e2579e8d9b13 188 void pressed(void)
DUR 0:e2579e8d9b13 189 {
DUR 0:e2579e8d9b13 190 button_int_f = true;
DUR 0:e2579e8d9b13 191 }
DUR 0:e2579e8d9b13 192
DUR 0:e2579e8d9b13 193
DUR 0:e2579e8d9b13 194
DUR 0:e2579e8d9b13 195
DUR 0:e2579e8d9b13 196 int main()
DUR 0:e2579e8d9b13 197 {
DUR 0:e2579e8d9b13 198 uint16_t u16error;
DUR 0:e2579e8d9b13 199
DUR 0:e2579e8d9b13 200 start_writing = false;
DUR 0:e2579e8d9b13 201 button_int_f = false;
DUR 0:e2579e8d9b13 202 file_index = 0;
DUR 0:e2579e8d9b13 203 adxl355_int_timestamp = 0;
DUR 0:e2579e8d9b13 204 adxl355_int_index = 0;
DUR 0:e2579e8d9b13 205
DUR 0:e2579e8d9b13 206 led_pin = 0;
DUR 0:e2579e8d9b13 207
DUR 0:e2579e8d9b13 208 BLUpin = 0;
DUR 0:e2579e8d9b13 209 GREENpin = 0;
DUR 0:e2579e8d9b13 210 VIOLApin = 0;
DUR 0:e2579e8d9b13 211
DUR 0:e2579e8d9b13 212 /* Impulso che segnala l'avvio del codice */
DUR 0:e2579e8d9b13 213 YELLOWpin = 0;
DUR 0:e2579e8d9b13 214 YELLOWpin = 1;
DUR 0:e2579e8d9b13 215 YELLOWpin = 0;
DUR 0:e2579e8d9b13 216
DUR 0:e2579e8d9b13 217 /* Setting SPI */
DUR 0:e2579e8d9b13 218 spiHandler.frequency(SPI_FREQUENCY_HZ);
DUR 0:e2579e8d9b13 219 spiHandler.format(8, 0);//POL = 0; PHA = 0
DUR 0:e2579e8d9b13 220
DUR 0:e2579e8d9b13 221 /* Setting interrupt su pin accelerometro */
DUR 0:e2579e8d9b13 222 adxl355_int.rise(&adxl355_int_handler);
DUR 0:e2579e8d9b13 223 /* Setting interrupt pulsante */
DUR 0:e2579e8d9b13 224 user_button.fall(&pressed);
DUR 0:e2579e8d9b13 225
DUR 0:e2579e8d9b13 226 /* Init ADXL355 */
DUR 0:e2579e8d9b13 227 u16error = adxl355_init(&adxl355_config);
DUR 0:e2579e8d9b13 228 if(u16error != 0)
DUR 0:e2579e8d9b13 229 {
DUR 0:e2579e8d9b13 230 //VIOLApin = !VIOLApin;
DUR 0:e2579e8d9b13 231 }else{}
DUR 0:e2579e8d9b13 232 /* Init SD CARD reader */
DUR 0:e2579e8d9b13 233 sd.init();
DUR 0:e2579e8d9b13 234 fs.mount(&sd);
DUR 0:e2579e8d9b13 235 fp = fopen("/fs/mydata.csv", "w");
DUR 0:e2579e8d9b13 236 if(fp == NULL)
DUR 0:e2579e8d9b13 237 {
DUR 0:e2579e8d9b13 238 start_writing = false;
DUR 0:e2579e8d9b13 239 led_pin = 0;
DUR 0:e2579e8d9b13 240 }
DUR 0:e2579e8d9b13 241 else
DUR 0:e2579e8d9b13 242 {
DUR 0:e2579e8d9b13 243 start_writing = true;
DUR 0:e2579e8d9b13 244 fprintf(fp,"#;ERROR;TIMESTAMP;ACC_DIG_3A_X;ACC_DIG_3A_Y;ACC_DIG_3A_Z;ACC_DIG_3A_TEMP;ACC_AN_3A_X;ACC_AN_3A_Y;ACC_AN_3A_Z;ACC_AN_3A_TEMP;ACC_AN_1A_VAL;ACC_AN_1A_TEMP\r\n");
DUR 0:e2579e8d9b13 245 u16error = adxl355_start_acquisition(adxl355_both);
DUR 0:e2579e8d9b13 246 if(u16error != 0)
DUR 0:e2579e8d9b13 247 {
DUR 0:e2579e8d9b13 248 led_pin = 0;
DUR 0:e2579e8d9b13 249 fclose(fp);
DUR 0:e2579e8d9b13 250 sd.deinit();
DUR 0:e2579e8d9b13 251 fs.unmount();
DUR 0:e2579e8d9b13 252 start_writing = false;
DUR 0:e2579e8d9b13 253 }else
DUR 0:e2579e8d9b13 254 {
DUR 0:e2579e8d9b13 255 led_pin = 1;
DUR 0:e2579e8d9b13 256 }
DUR 0:e2579e8d9b13 257 }
DUR 0:e2579e8d9b13 258
DUR 0:e2579e8d9b13 259 while(1)
DUR 0:e2579e8d9b13 260 {
DUR 0:e2579e8d9b13 261 Thread::wait(1000);
DUR 0:e2579e8d9b13 262 }
DUR 0:e2579e8d9b13 263
DUR 0:e2579e8d9b13 264 }