diff --git a/prosjekt.X/i2c.c b/prosjekt.X/i2c.c index 7b0eddc..75cdb5f 100644 --- a/prosjekt.X/i2c.c +++ b/prosjekt.X/i2c.c @@ -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 diff --git a/prosjekt.X/i2c.h b/prosjekt.X/i2c.h index 0e8f64f..6b4214c 100644 --- a/prosjekt.X/i2c.h +++ b/prosjekt.X/i2c.h @@ -18,6 +18,12 @@ extern "C" { #include "uart.h" #include +// 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); diff --git a/prosjekt.X/uart.h b/prosjekt.X/uart.h index 7284552..9ca2621 100644 --- a/prosjekt.X/uart.h +++ b/prosjekt.X/uart.h @@ -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);