Skip to content

Commit 9b7ddda

Browse files
committed
pbio/drv/i2c/i2c_ev3.c: Set up transaction buffers
These buffers are sized for a maximum-length transaction and are DMA-aligned. This API currently does not / will not support zero-copy operation, and user buffers will be copied to/from these DMA buffers.
1 parent 5572df1 commit 9b7ddda

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lib/pbio/drv/i2c/i2c_ev3.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <pbio/os.h>
2222
#include <pbio/util.h>
2323

24+
#include <pbdrv/cache.h>
2425
#include <pbdrv/i2c.h>
2526

2627
#include "../drv/rproc/rproc.h"
@@ -38,7 +39,14 @@
3839
#define DBG_ERR(expr)
3940
#endif
4041

42+
// Max 255 bytes write, 255 bytes read
43+
// Rounded up to a nice power of 2 and multiple of cache lines
44+
#define PRU_I2C_MAX_BYTES_PER_TXN 512
45+
46+
static uint8_t pbdrv_i2c_buffers[PRU_I2C_MAX_BYTES_PER_TXN * PBDRV_CONFIG_I2C_EV3_NUM_DEV] PBDRV_DMA_BUF;
47+
4148
struct _pbdrv_i2c_dev_t {
49+
uint8_t *buffer;
4250
volatile bool busy;
4351
bool init;
4452
uint8_t pru_i2c_idx;
@@ -105,6 +113,12 @@ pbio_error_t ev3_i2c_init_process_thread(pbio_os_state_t *state, void *context)
105113
// Need rproc to be initialized, because it sets up the PRU INTC
106114
PBIO_OS_AWAIT_UNTIL(state, pbdrv_rproc_is_ready());
107115

116+
// Set up the buffer pointers
117+
for (int i = 0; i < PBDRV_CONFIG_I2C_EV3_NUM_DEV; i++) {
118+
pbdrv_i2c_dev_t *i2c = &i2c_devs[i];
119+
pbdrv_rproc_ev3_pru1_shared_ram.i2c[i].buffer = i2c->buffer;
120+
}
121+
108122
// REVISIT: These event numbers get set up by the SUART library.
109123
// We should separate them cleanly in the future.
110124
IntRegister(SYS_INT_EVTOUT4, pbdrv_i2c_irq_0);
@@ -136,6 +150,7 @@ void pbdrv_i2c_init(void) {
136150
for (int i = 0; i < PBDRV_CONFIG_I2C_EV3_NUM_DEV; i++) {
137151
pbdrv_i2c_dev_t *i2c = &i2c_devs[i];
138152
i2c->pru_i2c_idx = i;
153+
i2c->buffer = &pbdrv_i2c_buffers[i * PRU_I2C_MAX_BYTES_PER_TXN];
139154
i2c->init = true;
140155
}
141156

0 commit comments

Comments
 (0)