example of i2c slave device supporting block transfers
Dependencies: lib_i2c_slave_block
Tested on nucleo board as slave. Use with i2c_master_block_example on raspberry pi as master.
Master I2C write will print on serial terminal 115200. User button will raise an irq; the RPi test program will service the interrupt.
See lib_i2c_slave_block for explanation of code.
Diff: main.cpp
- Revision:
- 2:d7e05c75f240
- Parent:
- 0:3ccfaf358115
- Child:
- 3:0288d257446a
--- a/main.cpp Fri Jan 18 16:01:28 2019 -0800 +++ b/main.cpp Mon Jan 21 18:09:05 2019 -0800 @@ -14,6 +14,7 @@ DigitalIn button(USER_BUTTON); +uint8_t test_buf[32]; void fill_tx_buf(uint8_t cmd) { @@ -30,6 +31,9 @@ i2c.tx_buf[i] = i + cnt; cnt++; break; + case CMD_BUFFER: + memcpy(i2c.tx_buf, test_buf, sizeof(test_buf)); + break; case CMD_IRQ: for (i = 0; i < cmd_to_length[CMD_IRQ]; i++) i2c.tx_buf[i] = irq.buf[i]; @@ -43,6 +47,12 @@ } } +bool cmd_allowed(uint8_t) +{ + /* return false if slave cannot take this cmd now */ + return true; +} + void service_i2c_write(uint8_t cmd, uint8_t len, const uint8_t* req) { uint8_t s8; @@ -57,8 +67,11 @@ pc.printf("%02x ", req[s8]); pc.printf("\r\n"); break; + case CMD_BUFFER: + memcpy(test_buf, req, sizeof(test_buf)); + break; default: - pc.printf("??%02x??|\r\n", cmd); + pc.printf("??%02x??\r\n", cmd); break; } // ..switch (cmd) }