Add data receiving capabilities using a buffer with overflow protection

This commit is contained in:
2025-10-04 15:12:25 +02:00
parent 40a5ad5a35
commit 6c48ebb65f
3 changed files with 77 additions and 6 deletions
+58
View File
@@ -0,0 +1,58 @@
#include <stdint.h>
#include <avr/io.h>
#include "config.h"
#define BUFFER_SIZE 8
static inline void twiReset(void) {
// Enable TWI; enable ACK; clear interrupt flag; enable twi external interrupt
TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWINT) | (1 << TWIE);
}
void twiInit(void) {
TWAR = TWI_ADDR; //address definition found in config.h
twiReset();
}
static inline void twiSendAck(void) {
TWCR = (1 << TWEA) | (1 << TWINT);
}
static inline void twiSendNack(void) {
TWCR = (TWCR & ~(1 << TWEA)) | (1 << TWINT);
}
static void twiRespond(int8_t response[]) {
// Respond to TWI requests
}
static void twiExecute(int8_t buffer[]) {
// Perform actions based on received message
// if needed:
// twiRespond();
}
void twiAlert(void) {
static int8_t twiBuffer[BUFFER_SIZE] = {0};
static uint8_t bufferIndex = 0;
switch (TWSR & 0xF8) {
case 0x60: // SLA+W received
twiSendAck();
break;
case 0x80: // data received
if(bufferIndex < BUFFER_SIZE) {
twiBuffer[bufferIndex++] = TWDR;
twiSendAck();
} else twiSendNack();
break;
case 0xA0: // stop condition
twiExecute(twiBuffer);
twiReset();
break;
default:
twiSendNack();
break;
}
}