Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem mbed
main.cpp
00001 #include "mbed.h" 00002 #include "SDFileSystem.h" 00003 00004 //FXOS8700CQ 00005 #define FXOS_ADDRESS_W 0x3C 00006 #define FXOS_ADDRESS_R 0x3D 00007 #define FXOS_STATUS 0x00 00008 #define FXOS_OUT_X_MSB 0x01 00009 #define FXOS_F_SETUP 0x09 00010 #define FXOS_TRIG_CFG 0x0A 00011 #define FXOS_SYS_MOD 0x0B 00012 #define FXOS_INT_SOURCE 0x0C 00013 #define FXOS_WHO_AM_I 0x0D 00014 #define FXOS_ID 0xC7 00015 #define FXOS_XYZ_DATA_CFG 0x0E 00016 #define FXOS_TRANSIENT_CFG 0x1D 00017 #define FXOS_TRANSIENT_SRC 0x1E 00018 #define FXOS_ASLP_COUNT 0x29 00019 #define FXOS_CTRL_REG_1 0x2A 00020 #define FXOS_CTRL_REG_2 0x2B 00021 #define FXOS_CTRL_REG_3 0x2C 00022 #define FXOS_CTRL_REG_4 0x2D 00023 #define FXOS_CTRL_REG_5 0x2E 00024 #define FXOS_OFF_X 0x2F 00025 #define FXOS_OFF_Y 0x30 00026 #define FXOS_OFF_Z 0x31 00027 #define FXOS_TEMP 0x51 00028 #define FXOS_M_CTRL_REG_1 0x5B 00029 #define FXOS_M_CTRL_REG_2 0x5C 00030 #define FXOS_M_CTRL_REG_3 0x5D 00031 00032 //FXAS21002C 00033 #define FXAS_ADDRESS_W 0x40 00034 #define FXAS_ADDRESS_R 0x41 00035 #define FXAS_STATUS 0x00 00036 #define FXAS_OUT_X_MSB 0x01 00037 #define FXAS_F_SETUP 0x09 00038 #define FXAS_F_EVENT 0x0A 00039 #define FXAS_INT_SRC_FLAG 0x0B 00040 #define FXAS_WHO_AM_I 0x0C 00041 #define FXAS_ID 0xD7 00042 #define FXAS_CTRL_REG_0 0x0D 00043 #define FXAS_RT_CFG 0x0E 00044 #define FXAS_TEMP 0x12 00045 #define FXAS_CTRL_REG_1 0x13 00046 #define FXAS_CTRL_REG_2 0x14 00047 #define FXAS_CTRL_REG_3 0x15 00048 00049 //Global variables 00050 int FXAS_FIFO_FULL_FLAG = 0; 00051 int FXOS_FIFO_FULL_FLAG = 0; 00052 char fxos_data_1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 00053 char fxas_data_1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 00054 00055 //Setup UART to PC 00056 Serial pc(USBTX, USBRX); 00057 00058 //Setup I2C 00059 I2C sensors(PTE0, PTE1); 00060 00061 //FXOS8700C Control Pins 00062 InterruptIn fxos_int1_8700(PTD4); //INT1-8700 00063 InterruptIn fxos_int2_8700(PTA4); //INT2-8700 00064 00065 //FXAS21002C Control Pins 00066 InterruptIn fxas_int1_21002(PTA5); //INT1-21002 00067 InterruptIn fxas_int2_21002(PTA13); //INT2-21002 00068 00069 //SD File System 00070 SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); 00071 00072 ////LED Setup 00073 DigitalOut led_1(LED1, 1); //red 00074 DigitalOut led_2(LED2, 1); //green 00075 //DigitalOut led_3(LED3, 1); //blue <- NB!! Wont play nice with SDFileSystem 00076 00077 //****************************************************************// 00078 //Functions 00079 int i2c_write(char address, char device_register, char data){ 00080 sensors.start(); 00081 sensors.write((char)address); 00082 sensors.write((char)device_register); 00083 sensors.write((char)data); 00084 sensors.stop(); 00085 return 0; 00086 } 00087 00088 int i2c_write(char address, char device_register, int stop){ 00089 sensors.start(); 00090 sensors.write((char)address); 00091 sensors.write((char)device_register); 00092 if(stop == 1){ 00093 sensors.stop(); 00094 } 00095 return 0; 00096 } 00097 00098 char i2c_read(char address, char device_register){ 00099 char data; 00100 00101 sensors.start(); 00102 sensors.write(address); 00103 sensors.write(device_register); 00104 sensors.start(); 00105 sensors.write(address | 0x01); 00106 data = (char)sensors.read(0); 00107 sensors.stop(); 00108 return data; 00109 } 00110 00111 int i2c_write_verify(char address, char device_register, char data){ 00112 char return_data; 00113 int return_val; 00114 00115 sensors.start(); 00116 sensors.write((char)address); 00117 sensors.write((char)device_register); 00118 sensors.write((char)data); 00119 sensors.stop(); 00120 00121 sensors.start(); 00122 sensors.write(address); 00123 sensors.write(device_register); 00124 sensors.start(); 00125 sensors.write(address | 0x01); 00126 return_data = sensors.read(0); 00127 sensors.stop(); 00128 00129 if (return_data == data){ 00130 return_val = 0; 00131 } 00132 else{ 00133 return_val = -1; 00134 } 00135 return return_val; 00136 } 00137 00138 int fxas_init() { 00139 int return_val = 0; 00140 int sensor_id = 0; 00141 00142 sensor_id = i2c_read(FXAS_ADDRESS_W, FXAS_WHO_AM_I); 00143 // pc.printf("Expected ID from FXAS21002C is 0xD7\n\r"); 00144 // pc.printf("Returned value: %X\n\n\r", sensor_id); 00145 00146 if (sensor_id == FXAS_ID){ 00147 return_val = 0; 00148 return_val += i2c_write_verify(FXAS_ADDRESS_W, FXAS_CTRL_REG_1, 0x00); 00149 return_val += i2c_write_verify(FXAS_ADDRESS_W, FXAS_F_SETUP, 0x00); 00150 return_val += i2c_write_verify(FXAS_ADDRESS_W, FXAS_F_SETUP, 0x57); 00151 return_val += i2c_write_verify(FXAS_ADDRESS_W, FXAS_CTRL_REG_0, 0x03); 00152 00153 return_val += i2c_write_verify(FXAS_ADDRESS_W, FXAS_CTRL_REG_2, 0xC2); 00154 return_val += i2c_write_verify(FXAS_ADDRESS_W, FXAS_CTRL_REG_3, 0x08); 00155 return_val += i2c_write_verify(FXAS_ADDRESS_W, FXAS_CTRL_REG_1, 0x13); 00156 } 00157 else{ 00158 return_val = -1; 00159 } 00160 return return_val; 00161 } 00162 00163 int fxos_init() { 00164 int return_val = 0; 00165 int sensor_id = 0; 00166 00167 sensor_id = i2c_read(FXOS_ADDRESS_W, FXOS_WHO_AM_I); 00168 // pc.printf("Expected ID from FXOS8700CQ is 0xC7\n\r"); 00169 // pc.printf("Returned value: %X\n\n\r", sensor_id); 00170 00171 if (sensor_id == FXOS_ID){ 00172 return_val = 0; 00173 return_val += i2c_write_verify(FXOS_ADDRESS_W, FXOS_CTRL_REG_1, 0x00); 00174 return_val += i2c_write_verify(FXOS_ADDRESS_W, FXOS_F_SETUP, 0x00); 00175 return_val += i2c_write_verify(FXOS_ADDRESS_W, FXOS_F_SETUP, 0x57); 00176 return_val += i2c_write_verify(FXOS_ADDRESS_W, FXOS_ASLP_COUNT, 0xFF); 00177 return_val += i2c_write_verify(FXOS_ADDRESS_W, FXOS_XYZ_DATA_CFG, 0x02); 00178 return_val += i2c_write_verify(FXOS_ADDRESS_W, FXOS_CTRL_REG_2, 0x00); 00179 return_val += i2c_write_verify(FXOS_ADDRESS_W, FXOS_CTRL_REG_3, 0x02); 00180 return_val += i2c_write_verify(FXOS_ADDRESS_W, FXOS_CTRL_REG_4, 0x41); //Enable FIFO & DRDY Interrupts 00181 return_val += i2c_write_verify(FXOS_ADDRESS_W, FXOS_CTRL_REG_5, 0x40); //FIFO -> Int_1, DRDY -> Imt_2 00182 return_val += i2c_write_verify(FXOS_ADDRESS_W, FXOS_M_CTRL_REG_1, 0x00); 00183 return_val += i2c_write_verify(FXOS_ADDRESS_W, FXOS_CTRL_REG_1, 0x21); 00184 } 00185 else{ 00186 return_val = -1; 00187 } 00188 return return_val; 00189 } 00190 00191 //Interrupt for FXAS21002 INT_1 00192 void read_fxas_data(){ 00193 // //Read data from FXAS21002 sensor 00194 if(FXAS_FIFO_FULL_FLAG != 2){ 00195 FXAS_FIFO_FULL_FLAG = 1; 00196 } 00197 } 00198 00199 //Interrupt for FXOS8700S INT_1 00200 void read_fxos_data(){ 00201 //Read data from FXOS8700C sensor 00202 if(FXOS_FIFO_FULL_FLAG != 2){ 00203 FXOS_FIFO_FULL_FLAG = 1; 00204 } 00205 } 00206 00207 int main() { 00208 int initilised = 0; 00209 int index = 0; 00210 int test_index = 0; 00211 int fxos_data_flag_1 = 0; 00212 int fxas_data_flag_1 = 0; 00213 00214 //Configure interrupts 00215 fxas_int1_21002.rise(&read_fxas_data);//FIFO -> Int_1 00216 // fxas_int2_21002.rise(&read_fxas_data);//DRDY -> Int_2 00217 fxos_int1_8700.rise(&read_fxos_data);//FIFO -> Int_1 00218 // fxos_int2_8700.rise(&read_fxos_data);//DRDY -> Int_2 00219 00220 sensors.frequency(450000); 00221 pc.baud(115200); 00222 00223 mkdir("/sd/sensor_data", 0777); 00224 FILE *fp = fopen("/sd/sensor_data/data.txt", "w"); 00225 if(fp == NULL) { 00226 error("Could not open file for write\n"); 00227 } 00228 fprintf(fp, "Gyro X,\tGyro Y,\tGyro Z,\t\tAcc X,\tACC Y,\tACC Z\n\r\n\r\n\r\n\r"); 00229 fclose(fp); 00230 00231 //Initlilise the FXOS8700CQ Accelorometer & the FXAS21002C Gyroscope 00232 initilised = fxas_init() & fxos_init(); 00233 // pc.printf("Returned value: %d\n\n\r", initilised); 00234 00235 while(1) { 00236 if (FXOS_FIFO_FULL_FLAG == 1){//0.976mg * (fxos_data >> 2) = total g force (8g scale) 00237 FXOS_FIFO_FULL_FLAG = 0; 00238 i2c_write(FXOS_ADDRESS_W, FXOS_STATUS, 0); 00239 sensors.read(FXOS_ADDRESS_R, fxos_data_1, 139, false);//Reads the status register on every pass 0x06 -> 0x00 00240 fxos_data_flag_1 = 1; 00241 } 00242 00243 if (FXAS_FIFO_FULL_FLAG == 1){//7.8125mdps * fxas_data = total degrees per second (250dps scale) 00244 FXAS_FIFO_FULL_FLAG = 0; 00245 i2c_write(FXAS_ADDRESS_W, FXAS_STATUS, 0); 00246 sensors.read(FXAS_ADDRESS_R, fxas_data_1, 139, false);//Reads the status register on the first if set to otherwise 0x06 -> 0x01 00247 fxas_data_flag_1 = 1; 00248 } 00249 00250 00251 if ((fxos_data_flag_1 == 1) && (fxas_data_flag_1 == 1)){ 00252 //Store data on SD card 00253 if (test_index == 0){// <--------- Just till a proper file closing system can be implemented 00254 fp = fopen("/sd/sensor_data/data.txt", "a"); 00255 if(fp == NULL) { 00256 error("Could not open file for write\n"); 00257 } 00258 } 00259 00260 for (index = 1; index < 139; index += 6){ 00261 //move data to SD card 00262 fprintf(fp, "%d\t%d\t%d\t\t", ((int16_t)((fxos_data_1[(index + 0)] << 8) | fxos_data_1[(index + 1)]) >> 2), 00263 ((int16_t)((fxos_data_1[(index + 2)] << 8) | fxos_data_1[(index + 3)]) >> 2), 00264 ((int16_t)((fxos_data_1[(index + 4)] << 8) | fxos_data_1[(index + 5)]) >> 2)); 00265 00266 fprintf(fp, "%d\t%d\t%d\n\r\n\r", ((int16_t)((fxas_data_1[(index + 0)] << 8) | fxas_data_1[(index + 1)])), 00267 ((int16_t)((fxas_data_1[(index + 2)] << 8) | fxas_data_1[(index + 3)])), 00268 ((int16_t)((fxas_data_1[(index + 4)] << 8) | fxas_data_1[(index + 5)]))); 00269 } 00270 00271 fxos_data_flag_1 = 0; 00272 fxas_data_flag_1 = 0; 00273 test_index++; 00274 if (test_index == 30){// <--------- Just till a proper file closing system can be implemented 00275 fclose(fp); 00276 test_index = 0; 00277 } 00278 for (int i = 0; i < 210; i++){ 00279 fxos_data_1[i] = 0; 00280 fxas_data_1[i] = 0; 00281 } 00282 } 00283 } 00284 }
Generated on Fri Jul 15 2022 00:45:13 by
1.7.2