Store and hold I2C data

I2C data is now stored until stop is sent, then printed.
This commit is contained in:
Sebastian H. Gabrielli 2024-03-20 13:24:55 +01:00
parent 4e0b810346
commit 8e3607cbda
3 changed files with 27 additions and 5 deletions

View File

@ -1,6 +1,9 @@
#include "i2c.h"
// Heavily inspired by: https://github.com/microchip-pic-avr-examples/avr128db48-bare-metal-twi-mplab/blob/master/twi-client.X/peripherals/TWI/TWI_client.c
// Basic I2C handling structure is heavily inspired by: https://github.com/microchip-pic-avr-examples/avr128db48-bare-metal-twi-mplab/blob/master/twi-client.X/peripherals/TWI/TWI_client.c
volatile uint8_t i2c_recv[I2C_RECV_BUF_SIZE]; // Arbitrary length array to hold the received data, longer than max expected command
volatile uint8_t i2c_recv_len;
void init_i2c(void) {
// Pin setup
@ -27,12 +30,22 @@ void init_i2c(void) {
| PIN2_bm // Respond to all TWI addresses
| TWI_ENABLE_bm // Enable acting as a slave
;
// Reset the received stuff
i2c_reset_recv();
}
// TODO: Figure out which interrupt does what
// Reset received counter and clear receive buffer
void i2c_reset_recv() {
i2c_recv_len = 0;
for (int i = 0; i < I2C_RECV_BUF_SIZE; i++) {
i2c_recv[i] = 0;
}
}
void i2c_write_handler(uint8_t data) {
printf("%u\n", data);
i2c_recv[i2c_recv_len] = data;
i2c_recv_len++;
}
void i2c_read_handler() {
@ -40,7 +53,10 @@ void i2c_read_handler() {
}
void i2c_stop_handler() {
printf("Stop, I guess\n");
// Reset counter and clear receive buffer
i2c_recv[i2c_recv_len] = '\0';
printf("%s\n", i2c_recv);
i2c_reset_recv();
}
// Address received

View File

@ -18,6 +18,12 @@ extern "C" {
#include "uart.h"
#include <util/twi.h>
// Received data info
#define I2C_RECV_BUF_SIZE 64
// Reset recv to initial state
void i2c_reset_recv();
// Initialize the I2C bus
void init_i2c(void);

View File

@ -20,7 +20,7 @@ extern "C" {
#endif
#define USART3_BAUD_RATE(BAUD_RATE) ((float)(F_CPU * 64 / (16 * (float)BAUD_RATE)) + 0.5)
// Initialize the USART3 controller
void init_uart(uint16_t baud);