From ee4a12c9c54065691c6f48c87566cc3026d6a8e2 Mon Sep 17 00:00:00 2001 From: true Date: Sat, 26 Oct 2024 00:13:53 -0700 Subject: [PATCH] finally fixed the last known bit of i2c corruption needed to implement clock stretching on start/stop routines too. program tick timer on sub MCU doesn't really need to be highest priority so made i2c higher priority. --- nametag8_CH592/user/comm/soft_i2c_master.c | 14 +++++++------- nametag8_V003/user/i2c_slave.c | 3 +-- nametag8_V003/user/main.c | 1 + 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/nametag8_CH592/user/comm/soft_i2c_master.c b/nametag8_CH592/user/comm/soft_i2c_master.c index befa36b..af8e2bf 100644 --- a/nametag8_CH592/user/comm/soft_i2c_master.c +++ b/nametag8_CH592/user/comm/soft_i2c_master.c @@ -20,6 +20,9 @@ * CYCLES_TO_HI = 2, CYCLES_TO_LOW = 0: ~650KHz * CYCLES_TO_HI = 1, bit_delay_lo = __nop: ~900KHz, but sub MCU fails to respond * + * speed settings chosen are what the lowest speed device (the sub MCU) will + * handle without faults. + * */ #include @@ -27,7 +30,7 @@ -#define CYCLES_TO_HI 3 +#define CYCLES_TO_HI 2 #define CYCLES_TO_LO 0 //#define CYCLES_RD 2 // cycles spent in read routine @@ -101,6 +104,7 @@ void i2cm_start() { SDA_IN_HI(); bit_delay_hi(); SCL_IN_HI(); bit_delay_hi(); + while (!SCL_GET()); // clock stretch SDA_OUTLO(); bit_delay_lo(); SCL_OUTLO(); bit_delay_lo(); } @@ -111,8 +115,7 @@ void i2cm_restart() SDA_IN_HI(); bit_delay_hi(); SCL_IN_HI(); while (!SCL_GET()); // clock stretch - bit_delay_hi(); // wait a little bit - bit_delay_hi(); // sub MCU can corrupt data if we don't wait + bit_delay_hi(); // attempt to fix corruption; unnecessary? i2cm_start(); } @@ -121,11 +124,8 @@ void i2cm_stop() { SDA_OUTLO(); bit_delay_lo(); SCL_IN_HI(); bit_delay_hi(); + while (!SCL_GET()); // clock stretch SDA_IN_HI(); bit_delay_hi(); - - // wait some extra time - bit_delay_hi(); - bit_delay_hi(); bit_delay_hi(); bit_delay_hi(); bit_delay_hi(); diff --git a/nametag8_V003/user/i2c_slave.c b/nametag8_V003/user/i2c_slave.c index 0427e95..b719cc3 100644 --- a/nametag8_V003/user/i2c_slave.c +++ b/nametag8_V003/user/i2c_slave.c @@ -170,7 +170,7 @@ void i2cs_init(uint8_t address, uint8_t read_only_mode) // enable interrupts I2C1->CTLR2 |= I2C_CTLR2_ITBUFEN | I2C_CTLR2_ITEVTEN | I2C_CTLR2_ITERREN; - NVIC_SetPriority(I2C1_EV_IRQn, 1 << 6); + NVIC_SetPriority(I2C1_EV_IRQn, 0 << 6); NVIC_EnableIRQ(I2C1_EV_IRQn); // Event interrupt NVIC_SetPriority(I2C1_ER_IRQn, 3 << 6); NVIC_EnableIRQ(I2C1_ER_IRQn); // Error interrupt @@ -192,7 +192,6 @@ void i2cs_poll() } -//__attribute__((section(".ramfunc"))) __attribute__((interrupt("WCH-Interrupt-fast"))) void I2C1_EV_IRQHandler(void) { diff --git a/nametag8_V003/user/main.c b/nametag8_V003/user/main.c index 577eb21..13aabc6 100644 --- a/nametag8_V003/user/main.c +++ b/nametag8_V003/user/main.c @@ -101,6 +101,7 @@ void systick_init(void) SysTick->CTLR = 0xF; // start counter in /1 mode, enable interrupts, auto-reset counter SysTick->SR = 0; // clear count comparison flag + NVIC_SetPriority(SysTicK_IRQn, 1 << 6); NVIC_EnableIRQ(SysTicK_IRQn); // enable interrupt }