Skip to content

Commit f7b12a3

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 940621c commit f7b12a3

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

lib/pbio/drv/i2c/i2c_ev3.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <pbdrv/i2c.h>
1919
#include "i2c_ev3.h"
2020

21+
#include "../rproc/rproc_ev3.h"
22+
2123
#define DEBUG 1
2224
#if DEBUG
2325
#include <stdio.h>
@@ -38,10 +40,10 @@ struct _pbdrv_i2c_dev_t {
3840
//
3941
};
4042

41-
static pbdrv_i2c_dev_t i2c_devs[PBDRV_CONFIG_I2C_EV3_NUM_DEV];
43+
static pbdrv_i2c_dev_t i2c_devs[PBDRV_RPROC_EV3_PRU1_NUM_I2C_BUSES];
4244

4345
pbio_error_t pbdrv_i2c_get_instance(uint8_t id, pbdrv_i2c_dev_t **i2c_dev) {
44-
if (id >= PBDRV_CONFIG_I2C_EV3_NUM_DEV) {
46+
if (id >= PBDRV_RPROC_EV3_PRU1_NUM_I2C_BUSES) {
4547
return PBIO_ERROR_INVALID_ARG;
4648
}
4749
pbdrv_i2c_dev_t *dev = &i2c_devs[id];
@@ -59,7 +61,7 @@ pbio_error_t pbdrv_i2c_placeholder_operation(pbdrv_i2c_dev_t *i2c_dev, const cha
5961
}
6062

6163
void pbdrv_i2c_init(void) {
62-
for (int i = 0; i < PBDRV_CONFIG_I2C_EV3_NUM_DEV; i++) {
64+
for (int i = 0; i < PBDRV_RPROC_EV3_PRU1_NUM_I2C_BUSES; i++) {
6365
const pbdrv_i2c_ev3_platform_data_t *pdata = &pbdrv_i2c_ev3_platform_data[i];
6466
pbdrv_i2c_dev_t *i2c = &i2c_devs[i];
6567
i2c->pdata = pdata;

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_BUSES 4
15+
16+
#define PBDRV_RPROC_EV3_PRU1_I2C_CLK_SPEED_HZ 10000
17+
18+
// I2C command bits, valid when bit7 == 0
19+
// Start an I2C transaction
20+
#define PBDRV_RPROC_EV3_PRU1_I2C_CMD_START (1 << 0)
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 (1 << 1)
24+
25+
// I2C status bits, valid when bit7 == 1
26+
// Indicates transaction is complete
27+
#define PBDRV_RPROC_EV3_PRU1_I2C_STAT_DONE (1 << 7)
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 or 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+
uintptr_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_BUSES];
2868
} pbdrv_rproc_ev3_pru1_shared_ram_t;
2969

3070
#endif // _INTERNAL_PBDRV_RPROC_EV3_PRU1_H_

lib/pbio/platform/ev3/pbdrvconfig.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
#define PBDRV_CONFIG_I2C (1)
4343
#define PBDRV_CONFIG_I2C_EV3 (1)
44-
#define PBDRV_CONFIG_I2C_EV3_NUM_DEV (4)
4544

4645
#define PBDRV_CONFIG_BUTTON (1)
4746
#define PBDRV_CONFIG_BUTTON_GPIO (1)

0 commit comments

Comments
 (0)