diff --git a/fxls8974cf.c b/fxls8974cf.c index 8dae2da..78e93bf 100644 --- a/fxls8974cf.c +++ b/fxls8974cf.c @@ -5,14 +5,19 @@ void fxls_init(void) { // SENS_CONFIG1: Enable Active Mode for I2C communication - uint8_t sens_config1 = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_SENS_CONFIG1); + uint8_t sens_config1; + bool success_sens_config1 = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_SENS_CONFIG1, &sens_config1); + sens_config1 |= SENS_CONFIG1_MASK; i2c_write_reg8(FXLS_I2C_ADDR, FXLS_SENS_CONFIG1, sens_config1); - - uint8_t sys_mode = i2c_read_reg8(FXLS_I2C_ADDR, 0x14); + + uint8_t sys_mode; + bool success_sys_mode = i2c_read_reg8(FXLS_I2C_ADDR, 0x14, &sys_mode); // SENS_CONFIG2: Configure wake and sleep modes, endian format, and read mode - uint8_t sens_config2 = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_SENS_CONFIG2); + uint8_t sens_config2; + bool success_sens_config2 = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_SENS_CONFIG2, &sens_config2); + sens_config2 &= ~(0x01 << 3); // Clear LE_BE bit sens_config2 &= ~(0x01 << 1); // Clear AINC_TEMP bit sens_config2 &= ~(0x01); // Clear F_READ bit @@ -21,58 +26,72 @@ void fxls_init(void) { i2c_write_reg8(FXLS_I2C_ADDR, FXLS_SENS_CONFIG2, sens_config2); // // BUF_CONFIG1: Disable buffer mode - uint8_t buf_config1 = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_BUF_CONFIG1); + uint8_t buf_config1; + bool success_buf_config1 = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_BUF_CONFIG1, &buf_config1); + buf_config1 &= ~0x60; // Clear BUF_MODE1 and BUF_MODE0 bits i2c_write_reg8(FXLS_I2C_ADDR, FXLS_BUF_CONFIG1, buf_config1); // // SENS_CONFIG3: Set ODR to 100Hz: set bits 7:4 to 0101 - uint8_t sens_config3 = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_SENS_CONFIG3); + uint8_t sens_config3; + bool success_sens_config3 = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_SENS_CONFIG3, &sens_config3); sens_config3 &= 0x0F; sens_config3 |= 0b01010000; i2c_write_reg8(FXLS_I2C_ADDR, FXLS_SENS_CONFIG3, sens_config3); // // Set the DRDY_EN bit (bit 7) in the INT_EN register to enable the data-ready interrupt. - uint8_t int_en = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_INT_EN); + uint8_t int_en; + bool success_int_en= i2c_read_reg8(FXLS_I2C_ADDR, FXLS_INT_EN, &int_en); int_en |= 0x80; i2c_write_reg8(FXLS_I2C_ADDR, FXLS_INT_EN, int_en); // // Set the DRDY_INT2 bit (bit 7) in the INT_PIN_SEL register to route the data-ready interrupt // to the INT2 pin. - uint8_t int_pin_sel = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_INT_PIN_SEL); + uint8_t int_pin_sel; + bool success_int_pin_sel= i2c_read_reg8(FXLS_I2C_ADDR, FXLS_INT_PIN_SEL, &int_pin_sel); int_pin_sel |= 0x80; i2c_write_reg8(FXLS_I2C_ADDR, FXLS_INT_PIN_SEL, int_pin_sel); // // Set the INT_POL bit (bit 0) in the SENS_CONFIG4 register to configure the polarity of the // interrupt (default active high). - uint8_t sens_config4 = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_SENS_CONFIG4); + uint8_t sens_config4; + bool success_sens_config4= i2c_read_reg8(FXLS_I2C_ADDR, FXLS_SENS_CONFIG4, &sens_config4); sens_config4 |= INT_POL_MASK; // Set INT_POL bit to 1 (active high interrupt on INT2 pin) i2c_write_reg8(FXLS_I2C_ADDR, FXLS_SENS_CONFIG4, sens_config4); } uint8_t fxls_get_prod_rev(void) { - return i2c_read_reg8(FXLS_I2C_ADDR, FXLS_PROD_REV); + uint8_t prod_rev; + bool success_prod_rev = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_PROD_REV, &prod_rev); + return prod_rev; } uint8_t fxls_get_whoami(void) { - return i2c_read_reg8(FXLS_I2C_ADDR, FXLS_WHO_AM_I); + uint8_t whoami; + bool success_whoami = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_WHO_AM_I, &whoami); + return whoami; } // Do I need this if ISR handles interrupts from INT2? int data_ready() { - uint8_t status = i2c_read_reg8(FXLS_I2C_ADDR, INT_STATUS_REG); + uint8_t status; + bool success_status = i2c_read_reg8(FXLS_I2C_ADDR, INT_STATUS_REG, &status); return (status & SRC_DRDY_MASK) ? 1 : 0; } + void fxls_read_accel_data(int16_t *x, int16_t *y, int16_t *z) { uint8_t x_lsb, x_msb, y_lsb, y_msb, z_lsb, z_msb; + // Read each byte of accelerometer data individually - x_lsb = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_REG_OUT_X_LSB); - x_msb = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_REG_OUT_X_MSB); - y_lsb = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_REG_OUT_Y_LSB); - y_msb = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_REG_OUT_Y_MSB); - z_lsb = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_REG_OUT_Z_LSB); - z_msb = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_REG_OUT_Z_MSB); + bool success_x_lsb = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_REG_OUT_X_LSB, &x_lsb); + bool success_x_msb = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_REG_OUT_X_MSB, &x_msb); + bool success_y_lsb = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_REG_OUT_Y_LSB, &y_lsb); + bool success_y_msb = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_REG_OUT_Y_MSB, &y_msb); + bool success_z_lsb = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_REG_OUT_Z_LSB, &z_lsb); + bool success_z_msb = i2c_read_reg8(FXLS_I2C_ADDR, FXLS_REG_OUT_Z_MSB, &z_msb); + // Combine the LSB and MSB for each axis *x = (((int16_t)x_msb << 8) | x_lsb); diff --git a/main.c b/main.c index a5f0f24..f9771aa 100644 --- a/main.c +++ b/main.c @@ -10,6 +10,7 @@ #include "mcc_generated_files/system/system.h" #include "slf3s.h" + #define STATUS_TIME_DIFF_ms 500 // 2 Hz #define FLOW_TIME_DIFF_ms 100 // 10 Hz #define ACCEL_TIME_DIFF_ms 10 // 100 Hz @@ -139,8 +140,10 @@ int main(void) { slf3s_init(); while (1) { + volatile uint8_t whoiam = fxls_get_whoami() ; can_msg_t msg; CLRWDT(); + if ((millis() - last_status_millis) > STATUS_TIME_DIFF_ms) { last_status_millis = millis(); @@ -175,6 +178,7 @@ int main(void) { file_write_buf_ptr += len; } + if ((millis() - last_flow_millis) > FLOW_TIME_DIFF_ms) { last_flow_millis = millis(); @@ -200,6 +204,8 @@ int main(void) { } txb_heartbeat(); + + if (file_write_buf_ptr > 1400) { if (f_open(&Fil, GLOBAL_FILENAME, FA_OPEN_APPEND | FA_WRITE) == FR_OK) { diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 2314878..81ad1ae 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -97,10 +97,10 @@ PIC18F26K83 - snap + PICkit3PlatformTool XC8 - 2.46 - 2 + 2.50 + 4 @@ -213,13 +213,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -244,30 +297,37 @@ + + + - + + value="${programoptions.preservedataflash.ranges}"/> + + + + @@ -327,7 +387,7 @@ + value="/Users/riyapatel/CODE/rocketry/electrical/cansw_vibration/debug/default/default_ptg"/> diff --git a/rocketlib b/rocketlib index 3848077..7eebde5 160000 --- a/rocketlib +++ b/rocketlib @@ -1 +1 @@ -Subproject commit 3848077123eea3cf7a381c70f5a1194c70977236 +Subproject commit 7eebde5fc5401af044877774db48d2047d0df21b diff --git a/slf3s.c b/slf3s.c index 7db523a..2df8577 100644 --- a/slf3s.c +++ b/slf3s.c @@ -12,15 +12,14 @@ static inline void i2c_write_cmd_16(uint8_t addr, uint16_t cmd) { uint8_t data[2]; data[0] = (cmd >> 8) & 0xff; data[1] = cmd & 0xff; - i2c_write(addr, data, 2); + bool success_data = i2c_write_data(addr, data, 2); } void slf3s_init(void) { uint8_t data[18]; i2c_write_cmd_16(SLF3S_I2C_ADDR, CMD_ID_1); i2c_write_cmd_16(SLF3S_I2C_ADDR, CMD_ID_2); - i2c_read(SLF3S_I2C_ADDR, data, 18); - + bool success_data = i2c_read_data(SLF3S_I2C_ADDR, data, 18); // Water measurements: LQDS_I2C_ADDR = 0x8, CMD_START_CONT = 0x3608 i2c_write_cmd_16(SLF3S_I2C_ADDR, CMD_START_CONT); } @@ -30,7 +29,8 @@ void read_flow_sensor_data(uint16_t *flow, uint16_t *temperature) { uint8_t crc; uint8_t data[9]; // 3x3 bytes of data - i2c_read(SLF3S_I2C_ADDR, data, 9); + bool success_data = i2c_read_data(SLF3S_I2C_ADDR, data, 9); + *flow = (((uint16_t)data[0] << 8) | data[1]); *temperature = (((uint16_t)data[3] << 8) | data[4]);