feat: Framework to handle INCP messages

This commit is contained in:
2026-03-31 14:04:12 +01:00
parent 5a7d3a9864
commit f7c2b65ebc
9 changed files with 271 additions and 53 deletions

View File

@@ -6,14 +6,15 @@
#include "stm32f4xx_hal_gpio.h"
#include "stm32f4xx_ll_i2c.h"
#include "gen_wrapper.h"
#include "incp_handler.h"
#include "msg_buf.h"
#include "circular_buffer.h"
#define I2C_BUF_SIZE 512
uint8_t i2c_buf[I2C_BUF_SIZE];
#define CIRC_BUF_SIZE (2*1024)
CIRCULAR_BUFFER_ALL(circ_buff, uint8_t, CIRC_BUF_SIZE)
static volatile circ_buff_t i2c_buf;
#define MSG_BUF_CAPACITY (2*1024)
uint8_t i2c_msg_store[MSG_BUF_CAPACITY];
msg_buf_t i2c_msgs;
void I2C1_EV_IRQHandler()
{
@@ -24,8 +25,10 @@ void I2C1_EV_IRQHandler()
//Skip checking if receiving or transmitting (should always be false)
LL_I2C_ClearFlag_ADDR(I2C1);
//Allow buffer to grow until full
while (i2c_buf.count < _CB_COUNT(&i2c_buf))
int head = 0;
//Allow buffer to grow until full (and inclusive)
while (head <= I2C_BUF_SIZE)
{
//Wait for data or stop flag
while (!LL_I2C_IsActiveFlag_STOP(I2C1) && !LL_I2C_IsActiveFlag_RXNE(I2C1));
@@ -37,14 +40,16 @@ void I2C1_EV_IRQHandler()
}
else //Data received
{
if (head >= I2C_BUF_SIZE) //Overflow for sure
Error_Handler();
uint8_t data = LL_I2C_ReceiveData8(I2C1);
circ_buff_append_range(&i2c_buf, &data, 1);
i2c_buf[head++] = data;
}
}
//Overflow or stop condition reached
if (i2c_buf.count >= _CB_COUNT(&i2c_buf))
Error_Handler();
//Stop condition reached, save msg
msg_buf_push(&i2c_msgs, i2c_buf, head);
}
bool I2C1_transmit_master(uint8_t addr, uint8_t *buf, uint8_t count)
@@ -85,10 +90,16 @@ bool I2C1_transmit_master(uint8_t addr, uint8_t *buf, uint8_t count)
int my_main()
{
msg_buf_init(&i2c_msgs, i2c_msg_store, sizeof(i2c_msg_store));
while (1)
{
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
HAL_Delay(500);
for (int i = 0; i < 500; ++i)
{
HAL_Delay(1);
handle_incp(&i2c_msgs);
}
}
return 0;