Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes for debug i2c driver [WIP] #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 37 additions & 18 deletions fxls8974cf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();

Expand All @@ -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) {
Expand Down
72 changes: 66 additions & 6 deletions nbproject/configurations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@
<targetDevice>PIC18F26K83</targetDevice>
<targetHeader></targetHeader>
<targetPluginBoard></targetPluginBoard>
<platformTool>snap</platformTool>
<platformTool>PICkit3PlatformTool</platformTool>
<languageToolchain>XC8</languageToolchain>
<languageToolchainVersion>2.46</languageToolchainVersion>
<platform>2</platform>
<languageToolchainVersion>2.50</languageToolchainVersion>
<platform>4</platform>
</toolsSet>
<packs>
<pack name="PIC18F-K_DFP" vendor="Microchip" version="1.13.292"/>
Expand Down Expand Up @@ -213,13 +213,64 @@
<property key="program-the-device-with-default-config-words" value="false"/>
<property key="remove-unused-sections" value="true"/>
</HI-TECH-LINK>
<PICkit3PlatformTool>
<property key="AutoSelectMemRanges" value="auto"/>
<property key="Freeze Peripherals" value="true"/>
<property key="SecureSegment.SegmentProgramming" value="FullChipProgramming"/>
<property key="ToolFirmwareFilePath"
value="Press to browse for a specific firmware version"/>
<property key="ToolFirmwareOption.UseLatestFirmware" value="true"/>
<property key="debugoptions.debug-startup" value="Use system settings"/>
<property key="debugoptions.reset-behaviour" value="Use system settings"/>
<property key="debugoptions.useswbreakpoints" value="false"/>
<property key="event.recorder.debugger.behavior" value="Running"/>
<property key="event.recorder.enabled" value="false"/>
<property key="event.recorder.scvd.files" value=""/>
<property key="firmware.download.all" value="false"/>
<property key="hwtoolclock.frcindebug" value="false"/>
<property key="lastid" value=""/>
<property key="memories.aux" value="false"/>
<property key="memories.bootflash" value="true"/>
<property key="memories.configurationmemory" value="true"/>
<property key="memories.configurationmemory2" value="true"/>
<property key="memories.dataflash" value="true"/>
<property key="memories.eeprom" value="true"/>
<property key="memories.flashdata" value="true"/>
<property key="memories.id" value="true"/>
<property key="memories.instruction.ram" value="true"/>
<property key="memories.instruction.ram.ranges"
value="${memories.instruction.ram.ranges}"/>
<property key="memories.programmemory" value="true"/>
<property key="memories.programmemory.ranges" value="0-ffff"/>
<property key="poweroptions.powerenable" value="false"/>
<property key="programmertogo.imagename" value=""/>
<property key="programoptions.donoteraseauxmem" value="false"/>
<property key="programoptions.eraseb4program" value="true"/>
<property key="programoptions.pgmspeed" value="2"/>
<property key="programoptions.preservedataflash" value="false"/>
<property key="programoptions.preservedataflash.ranges"
value="${programoptions.preservedataflash.ranges}"/>
<property key="programoptions.preserveeeprom" value="false"/>
<property key="programoptions.preserveeeprom.ranges" value="310000-3103ff"/>
<property key="programoptions.preserveprogram.ranges" value=""/>
<property key="programoptions.preserveprogramrange" value="false"/>
<property key="programoptions.preserveuserid" value="false"/>
<property key="programoptions.programcalmem" value="false"/>
<property key="programoptions.programuserotp" value="false"/>
<property key="programoptions.testmodeentrymethod" value="VDDFirst"/>
<property key="programoptions.usehighvoltageonmclr" value="false"/>
<property key="programoptions.uselvpprogramming" value="false"/>
<property key="voltagevalue" value="5.0"/>
</PICkit3PlatformTool>
<Tool>
<property key="AutoSelectMemRanges" value="auto"/>
<property key="Freeze Peripherals" value="true"/>
<property key="SecureSegment.SegmentProgramming" value="FullChipProgramming"/>
<property key="ToolFirmwareFilePath"
value="Press to browse for a specific firmware version"/>
<property key="ToolFirmwareOption.UpdateOptions"
value="ToolFirmwareOption.UseLatest"/>
<property key="ToolFirmwareOption.UseLatestFirmware" value="true"/>
<property key="ToolFirmwareToolPack"
value="Press to select which tool pack to use"/>
<property key="communication.interface"
Expand All @@ -233,7 +284,9 @@
<property key="event.recorder.debugger.behavior" value="Running"/>
<property key="event.recorder.enabled" value="false"/>
<property key="event.recorder.scvd.files" value=""/>
<property key="firmware.download.all" value="false"/>
<property key="freeze.timers" value="false"/>
<property key="hwtoolclock.frcindebug" value="false"/>
<property key="lastid" value=""/>
<property key="memories.aux" value="false"/>
<property key="memories.bootflash" value="true"/>
Expand All @@ -244,30 +297,37 @@
<property key="memories.exclude.configurationmemory" value="true"/>
<property key="memories.flashdata" value="true"/>
<property key="memories.id" value="true"/>
<property key="memories.instruction.ram" value="true"/>
<property key="memories.instruction.ram.ranges"
value="${memories.instruction.ram.ranges}"/>
<property key="memories.programmemory" value="true"/>
<property key="memories.programmemory.ranges" value="0-ffff"/>
<property key="poweroptions.powerenable" value="false"/>
<property key="programmerToGoFilePath"
value="/home/jason/rocketry/cansw_vibration/debug/default/default_ptg"/>
<property key="programmertogo.imagename" value=""/>
<property key="programoptions.donoteraseauxmem" value="false"/>
<property key="programoptions.eraseb4program" value="true"/>
<property key="programoptions.pgmentry.voltage" value="low"/>
<property key="programoptions.pgmspeed" value="Med"/>
<property key="programoptions.pgmspeed" value="2"/>
<property key="programoptions.preservedataflash" value="false"/>
<property key="programoptions.preservedataflash.ranges"
value="${memories.dataflash.default}"/>
value="${programoptions.preservedataflash.ranges}"/>
<property key="programoptions.preserveeeprom" value="false"/>
<property key="programoptions.preserveeeprom.ranges" value="310000-3103ff"/>
<property key="programoptions.preserveprogram.ranges" value=""/>
<property key="programoptions.preserveprogramrange" value="false"/>
<property key="programoptions.preserveuserid" value="false"/>
<property key="programoptions.programcalmem" value="false"/>
<property key="programoptions.programuserotp" value="false"/>
<property key="programoptions.testmodeentrymethod" value="VDDFirst"/>
<property key="programoptions.usehighvoltageonmclr" value="false"/>
<property key="programoptions.uselvpprogramming" value="false"/>
<property key="toolpack.updateoptions"
value="toolpack.updateoptions.uselatestoolpack"/>
<property key="toolpack.updateoptions.packversion"
value="Press to select which tool pack to use"/>
<property key="voltagevalue" value="5.0"/>
</Tool>
<XC8-CO>
<property key="coverage-enable" value=""/>
Expand Down Expand Up @@ -327,7 +387,7 @@
<property key="memories.programmemory" value="true"/>
<property key="memories.programmemory.ranges" value="0-ffff"/>
<property key="programmerToGoFilePath"
value="/home/jason/rocketry/cansw_vibration/debug/default/default_ptg"/>
value="/Users/riyapatel/CODE/rocketry/electrical/cansw_vibration/debug/default/default_ptg"/>
<property key="programoptions.donoteraseauxmem" value="false"/>
<property key="programoptions.eraseb4program" value="true"/>
<property key="programoptions.pgmentry.voltage" value="low"/>
Expand Down
8 changes: 4 additions & 4 deletions slf3s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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]);
Expand Down
Loading