Skip to content

Commit 7b9b4f8

Browse files
committed
Trigger a host CPU interrupt on I2C transaction completion
1 parent b6ec5ad commit 7b9b4f8

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
@@ -228,6 +236,9 @@ typedef struct i2c_state_struct {
228236
// Bit index of the IO pins in GPIO bank 0/1
229237
uint8_t scl_bit;
230238
uint8_t sda_bit;
239+
240+
// Event to trigger to inform the ARM of completion
241+
uint8_t intr_event;
231242
} i2c_state_struct;
232243
i2c_state_struct i2c_states[PBDRV_RPROC_EV3_PRU1_NUM_I2C_BUSES];
233244

@@ -471,6 +482,7 @@ static void handle_i2c(volatile pbdrv_rproc_ev3_pru1_i2c_command_t *cmd, i2c_sta
471482
0,
472483
PBDRV_RPROC_EV3_PRU1_I2C_STAT_DONE | PBDRV_RPROC_EV3_PRU1_I2C_STAT_OK
473484
);
485+
__R31 = i2c->intr_event;
474486
}
475487
}
476488
break;
@@ -575,6 +587,7 @@ static void handle_i2c(volatile pbdrv_rproc_ev3_pru1_i2c_command_t *cmd, i2c_sta
575587
PBDRV_RPROC_EV3_PRU1_I2C_STAT_DONE | PBDRV_RPROC_EV3_PRU1_I2C_STAT_NAK
576588
);
577589
state = I2C_STATE_IDLE;
590+
__R31 = i2c->intr_event;
578591
}
579592

580593
if (0) {
@@ -588,6 +601,7 @@ static void handle_i2c(volatile pbdrv_rproc_ev3_pru1_i2c_command_t *cmd, i2c_sta
588601
PBDRV_RPROC_EV3_PRU1_I2C_STAT_DONE | PBDRV_RPROC_EV3_PRU1_I2C_STAT_TIMEOUT
589602
);
590603
state = I2C_STATE_IDLE;
604+
__R31 = i2c->intr_event;
591605
}
592606
}
593607

@@ -604,12 +618,16 @@ void main() {
604618
// We initialize this in code to avoid having to copy a data ram binary
605619
i2c_states[0].scl_bit = SENSOR_PORT_1_PIN_SCL;
606620
i2c_states[0].sda_bit = SENSOR_PORT_1_PIN_SDA;
621+
i2c_states[0].intr_event = SENSOR_PORT_1_IRQ_EVT;
607622
i2c_states[1].scl_bit = SENSOR_PORT_2_PIN_SCL;
608623
i2c_states[1].sda_bit = SENSOR_PORT_2_PIN_SDA;
624+
i2c_states[1].intr_event = SENSOR_PORT_2_IRQ_EVT;
609625
i2c_states[2].scl_bit = SENSOR_PORT_3_PIN_SCL;
610626
i2c_states[2].sda_bit = SENSOR_PORT_3_PIN_SDA;
627+
i2c_states[2].intr_event = SENSOR_PORT_3_IRQ_EVT;
611628
i2c_states[3].scl_bit = SENSOR_PORT_4_PIN_SCL;
612629
i2c_states[3].sda_bit = SENSOR_PORT_4_PIN_SDA;
630+
i2c_states[3].intr_event = SENSOR_PORT_4_IRQ_EVT;
613631

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

0 commit comments

Comments
 (0)