Skip to content

Commit 95dbb71

Browse files
committed
Trigger a host CPU interrupt on I2C transaction completion
1 parent c16d2c2 commit 95dbb71

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,13 @@ 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+
#define SENSOR_PORT_1_IRQ_EVT 42
54+
#define SENSOR_PORT_2_IRQ_EVT 44
55+
#define SENSOR_PORT_3_IRQ_EVT 46
56+
#define SENSOR_PORT_4_IRQ_EVT 48
57+
5158
// This timeout specifies the *total permissible delay* of an I2C transaction.
5259
// This is the permissible sum total of all clock stretching as well as
5360
// the initial wait for the bus to become free. Time spent actually transferring
@@ -163,6 +170,10 @@ typedef struct i2c_state_struct {
163170
// Bit index of the IO pins in GPIO bank 0/1
164171
uint8_t scl_bit;
165172
uint8_t sda_bit;
173+
174+
// Event to trigger to inform the ARM of completion
175+
// (has bit5 already set)
176+
uint8_t intr_event;
166177
} i2c_state_struct;
167178
i2c_state_struct i2c_states[PBDRV_RPROC_EV3_PRU1_NUM_I2C_CHANNELS];
168179

@@ -382,6 +393,7 @@ static void handle_i2c(volatile pbdrv_rproc_ev3_pru1_i2c_command_t *cmd, i2c_sta
382393
0,
383394
PBDRV_RPROC_EV3_PRU1_I2C_STAT_DONE | PBDRV_RPROC_EV3_PRU1_I2C_STAT_OK
384395
);
396+
__R31 = i2c->intr_event;
385397
}
386398
}
387399
break;
@@ -492,6 +504,7 @@ static void handle_i2c(volatile pbdrv_rproc_ev3_pru1_i2c_command_t *cmd, i2c_sta
492504
PBDRV_RPROC_EV3_PRU1_I2C_STAT_DONE | PBDRV_RPROC_EV3_PRU1_I2C_STAT_NAK
493505
);
494506
state = I2C_STATE_IDLE;
507+
__R31 = i2c->intr_event;
495508
}
496509

497510
if (0) {
@@ -505,6 +518,7 @@ static void handle_i2c(volatile pbdrv_rproc_ev3_pru1_i2c_command_t *cmd, i2c_sta
505518
PBDRV_RPROC_EV3_PRU1_I2C_STAT_DONE | PBDRV_RPROC_EV3_PRU1_I2C_STAT_TIMEOUT
506519
);
507520
state = I2C_STATE_IDLE;
521+
__R31 = i2c->intr_event;
508522
}
509523
}
510524

@@ -521,12 +535,16 @@ void main() {
521535
// We initialize this in code to avoid having to copy a data ram binary
522536
i2c_states[0].scl_bit = SENSOR_PORT_1_PIN_SCL;
523537
i2c_states[0].sda_bit = SENSOR_PORT_1_PIN_SDA;
538+
i2c_states[0].intr_event = SENSOR_PORT_1_IRQ_EVT;
524539
i2c_states[1].scl_bit = SENSOR_PORT_2_PIN_SCL;
525540
i2c_states[1].sda_bit = SENSOR_PORT_2_PIN_SDA;
541+
i2c_states[1].intr_event = SENSOR_PORT_2_IRQ_EVT;
526542
i2c_states[2].scl_bit = SENSOR_PORT_3_PIN_SCL;
527543
i2c_states[2].sda_bit = SENSOR_PORT_3_PIN_SDA;
544+
i2c_states[2].intr_event = SENSOR_PORT_3_IRQ_EVT;
528545
i2c_states[3].scl_bit = SENSOR_PORT_4_PIN_SCL;
529546
i2c_states[3].sda_bit = SENSOR_PORT_4_PIN_SDA;
547+
i2c_states[3].intr_event = SENSOR_PORT_4_IRQ_EVT;
530548

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

0 commit comments

Comments
 (0)