Skip to content

Commit 475631a

Browse files
committed
Trigger a host CPU interrupt on I2C transaction completion
1 parent 19d9999 commit 475631a

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

am1808.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
// 30 = Shared RAM 0x80nnnn00
6666
// 31 = mDDR/DDR2 Data 0xC0nnnn00
6767

68+
extern volatile __regio_symbol unsigned int __R30;
69+
extern volatile __regio_symbol unsigned int __R31;
70+
6871
// Timer0 peripheral (only what we need)
6972
static volatile uint32_t * const TIMER0_TIM34 = (volatile uint32_t *)0x01C20014;
7073

main.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ static inline void update_pwm(uint8_t val, uint8_t time_now, uint32_t gpio_bit)
4848
#define SENSOR_PORT_4_PIN_SCL 1
4949
#define SENSOR_PORT_4_PIN_SDA (15 + 16)
5050

51+
// I2C event definition. This currently is set up by the SUART code
52+
// and is thus defined to coexist with it.
53+
// bit5 is required to trigger the interrupt upon a write to R31.
54+
#define SENSOR_PORT_1_IRQ_EVT ((1 << 5) | (42 - 32))
55+
#define SENSOR_PORT_2_IRQ_EVT ((1 << 5) | (44 - 32))
56+
#define SENSOR_PORT_3_IRQ_EVT ((1 << 5) | (46 - 32))
57+
#define SENSOR_PORT_4_IRQ_EVT ((1 << 5) | (48 - 32))
58+
5159
// This timeout specifies the *total permissible delay* of an I2C transaction.
5260
// This is the permissible sum total of all clock stretching as well as
5361
// the initial wait for the bus to become free. Time spent actually transferring
@@ -222,6 +230,9 @@ typedef struct i2c_state_struct {
222230
// Bit index of the IO pins in GPIO bank 0/1
223231
uint8_t scl_bit;
224232
uint8_t sda_bit;
233+
234+
// Event to trigger to inform the ARM of completion
235+
uint8_t intr_event;
225236
} i2c_state_struct;
226237
i2c_state_struct i2c_states[PBDRV_RPROC_EV3_PRU1_NUM_I2C_BUSES];
227238

@@ -468,6 +479,7 @@ static void handle_i2c(volatile pbdrv_rproc_ev3_pru1_i2c_command_t *cmd, i2c_sta
468479
0,
469480
PBDRV_RPROC_EV3_PRU1_I2C_STAT_DONE | PBDRV_RPROC_EV3_PRU1_I2C_STAT_OK
470481
);
482+
__R31 = i2c->intr_event;
471483
}
472484
}
473485
break;
@@ -572,6 +584,7 @@ static void handle_i2c(volatile pbdrv_rproc_ev3_pru1_i2c_command_t *cmd, i2c_sta
572584
PBDRV_RPROC_EV3_PRU1_I2C_STAT_DONE | PBDRV_RPROC_EV3_PRU1_I2C_STAT_NAK
573585
);
574586
state = I2C_STATE_IDLE;
587+
__R31 = i2c->intr_event;
575588
}
576589

577590
if (0) {
@@ -585,6 +598,7 @@ static void handle_i2c(volatile pbdrv_rproc_ev3_pru1_i2c_command_t *cmd, i2c_sta
585598
PBDRV_RPROC_EV3_PRU1_I2C_STAT_DONE | PBDRV_RPROC_EV3_PRU1_I2C_STAT_TIMEOUT
586599
);
587600
state = I2C_STATE_IDLE;
601+
__R31 = i2c->intr_event;
588602
}
589603
}
590604

@@ -601,12 +615,16 @@ void main() {
601615
// We initialize this in code to avoid having to copy a data ram binary
602616
i2c_states[0].scl_bit = SENSOR_PORT_1_PIN_SCL;
603617
i2c_states[0].sda_bit = SENSOR_PORT_1_PIN_SDA;
618+
i2c_states[0].intr_event = SENSOR_PORT_1_IRQ_EVT;
604619
i2c_states[1].scl_bit = SENSOR_PORT_2_PIN_SCL;
605620
i2c_states[1].sda_bit = SENSOR_PORT_2_PIN_SDA;
621+
i2c_states[1].intr_event = SENSOR_PORT_2_IRQ_EVT;
606622
i2c_states[2].scl_bit = SENSOR_PORT_3_PIN_SCL;
607623
i2c_states[2].sda_bit = SENSOR_PORT_3_PIN_SDA;
624+
i2c_states[2].intr_event = SENSOR_PORT_3_IRQ_EVT;
608625
i2c_states[3].scl_bit = SENSOR_PORT_4_PIN_SCL;
609626
i2c_states[3].sda_bit = SENSOR_PORT_4_PIN_SDA;
627+
i2c_states[3].intr_event = SENSOR_PORT_4_IRQ_EVT;
610628

611629
while (1) {
612630
// 24 MHz / 256 ==> 93.75 kHz tick rate for this counter

0 commit comments

Comments
 (0)