Skip to content

Commit 1fcb1af

Browse files
committed
pbio/drv/rproc/rproc_ev3: Add I2C interface for PRU1
This defines the communications structure and sets up the resources for PRU1's software I2C controller.
1 parent 2b8edc7 commit 1fcb1af

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

lib/pbio/drv/rproc/rproc_ev3.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ void pbdrv_rproc_init(void) {
4545
TimerPeriodSet(SOC_TMR_0_REGS, TMR_TIMER34, 256 * 256 - 1);
4646
TimerEnable(SOC_TMR_0_REGS, TMR_TIMER34, TMR_ENABLE_CONT);
4747

48+
// Enable Timer2 "12" half for 20 kHz = 2 * 10 kHz
49+
// This is used by the PRU to time I2C bits
50+
TimerConfigure(SOC_TMR_2_REGS, TMR_CFG_32BIT_UNCH_CLK_BOTH_INT);
51+
TimerPeriodSet(SOC_TMR_2_REGS, TMR_TIMER12, SOC_SYSCLK_2_FREQ / (2 * PBDRV_RPROC_EV3_PRU1_I2C_CLK_SPEED_HZ) - 1);
52+
TimerEnable(SOC_TMR_2_REGS, TMR_TIMER12, TMR_ENABLE_CONT);
53+
4854
// Clear shared command memory
4955
memset((void *)&pbdrv_rproc_ev3_pru1_shared_ram, 0, sizeof(pbdrv_rproc_ev3_pru1_shared_ram));
5056

lib/pbio/drv/rproc/rproc_ev3_pru1.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,45 @@
1111
// between the two codebases and must be kept in sync.
1212

1313
#define PBDRV_RPROC_EV3_PRU1_NUM_PWM_CHANNELS 4
14+
#define PBDRV_RPROC_EV3_PRU1_NUM_I2C_CHANNELS 4
15+
16+
#define PBDRV_RPROC_EV3_PRU1_I2C_CLK_SPEED_HZ 10000
17+
18+
// I2C command bits
19+
// Start an I2C transaction
20+
#define PBDRV_RPROC_EV3_PRU1_I2C_CMD_START 0x01
21+
// Generate a stop, clock pulse, and start instead of a repeated start
22+
// Used for the NXT ultrasonic sensor.
23+
#define PBDRV_RPROC_EV3_PRU1_I2C_CMD_NXT_QUIRK 0x02
24+
25+
// I2C status bits
26+
// Indicates transaction is complete
27+
#define PBDRV_RPROC_EV3_PRU1_I2C_STAT_DONE 0x80
28+
// Mask for the status code
29+
#define PBDRV_RPROC_EV3_PRU1_I2C_STAT_MASK 0x7f
30+
31+
// I2C transaction status codes
32+
enum {
33+
PBDRV_RPROC_EV3_PRU1_I2C_STAT_OK,
34+
PBDRV_RPROC_EV3_PRU1_I2C_STAT_TIMEOUT,
35+
PBDRV_RPROC_EV3_PRU1_I2C_STAT_NAK,
36+
};
37+
38+
#define PBDRV_RPROC_EV3_PRU1_I2C_PACK_FLAGS(daddr, rlen, wlen, flags) \
39+
((((daddr) & 0xff) << 24) | \
40+
(((rlen) & 0xff) << 16) | \
41+
(((wlen) & 0xff) << 8) | \
42+
((flags) & 0xff))
43+
44+
typedef struct {
45+
// bit[7:0] status and flags
46+
// bit[15:8] write length
47+
// bit[23:16] read length
48+
// bit[31:24] device address (unshifted)
49+
uint32_t flags;
50+
// Physical address of a transaction buffer
51+
uint8_t *buffer;
52+
} pbdrv_rproc_ev3_pru1_i2c_command_t;
1453

1554
typedef struct {
1655
union {
@@ -25,6 +64,7 @@ typedef struct {
2564
// of them and route ARM accesses through the PRU.
2665
uint32_t gpio_bank_01_dir_set;
2766
uint32_t gpio_bank_01_dir_clr;
67+
pbdrv_rproc_ev3_pru1_i2c_command_t i2c[PBDRV_RPROC_EV3_PRU1_NUM_I2C_CHANNELS];
2868
} pbdrv_rproc_ev3_pru1_shared_ram_t;
2969

3070
#endif // _INTERNAL_PBDRV_RPROC_EV3_PRU1_H_

0 commit comments

Comments
 (0)