feat: Replies to pings (ignoring crc)
This commit is contained in:
@@ -7,94 +7,19 @@
|
||||
#include "stm32f4xx_ll_i2c.h"
|
||||
|
||||
#include "incp_handler.h"
|
||||
#include "msg_buf.h"
|
||||
|
||||
#define I2C_BUF_SIZE 512
|
||||
uint8_t i2c_buf[I2C_BUF_SIZE];
|
||||
#include "my_i2c.h"
|
||||
|
||||
#define MSG_BUF_CAPACITY (2*1024)
|
||||
uint8_t i2c_msg_store[MSG_BUF_CAPACITY];
|
||||
msg_buf_t i2c_msgs;
|
||||
|
||||
void I2C1_EV_IRQHandler()
|
||||
{
|
||||
if (!LL_I2C_IsActiveFlag_ADDR(I2C1))
|
||||
Error_Handler();
|
||||
|
||||
//Skip checking matched addr (should only be one)
|
||||
//Skip checking if receiving or transmitting (should always be false)
|
||||
LL_I2C_ClearFlag_ADDR(I2C1);
|
||||
|
||||
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));
|
||||
|
||||
if (LL_I2C_IsActiveFlag_STOP(I2C1))
|
||||
{
|
||||
LL_I2C_ClearFlag_STOP(I2C1);
|
||||
break;
|
||||
}
|
||||
else //Data received
|
||||
{
|
||||
if (head >= I2C_BUF_SIZE) //Overflow for sure
|
||||
Error_Handler();
|
||||
|
||||
uint8_t data = LL_I2C_ReceiveData8(I2C1);
|
||||
i2c_buf[head++] = data;
|
||||
}
|
||||
}
|
||||
|
||||
//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)
|
||||
{
|
||||
while (LL_I2C_IsActiveFlag_BUSY(I2C1));
|
||||
|
||||
//Transmit start
|
||||
LL_I2C_GenerateStartCondition(I2C1);
|
||||
while (!LL_I2C_IsActiveFlag_SB(I2C1));
|
||||
|
||||
//Transmit address + write
|
||||
LL_I2C_TransmitData8(I2C1, addr << 1);
|
||||
while (!LL_I2C_IsActiveFlag_ADDR(I2C1));
|
||||
LL_I2C_ClearFlag_ADDR(I2C1);
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
while (!LL_I2C_IsActiveFlag_TXE(I2C1));
|
||||
|
||||
if (LL_I2C_IsActiveFlag_AF(I2C1))
|
||||
{
|
||||
//REVIEW: Anything else neeeded?
|
||||
LL_I2C_Disable(I2C1);
|
||||
LL_I2C_Enable(I2C1);
|
||||
return false;
|
||||
}
|
||||
|
||||
LL_I2C_TransmitData8(I2C1, buf[i]);
|
||||
}
|
||||
|
||||
LL_I2C_GenerateStopCondition(I2C1);
|
||||
while (LL_I2C_IsActiveFlag_STOP(I2C1));
|
||||
|
||||
LL_I2C_Disable(I2C1);
|
||||
LL_I2C_Enable(I2C1);
|
||||
return true;
|
||||
}
|
||||
|
||||
int my_main()
|
||||
{
|
||||
LL_I2C_EnableIT_EVT(I2C1);
|
||||
|
||||
msg_buf_init(&i2c_msgs, i2c_msg_store, sizeof(i2c_msg_store));
|
||||
|
||||
while (1)
|
||||
{
|
||||
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
|
||||
for (int i = 0; i < 500; ++i)
|
||||
{
|
||||
HAL_Delay(1);
|
||||
|
||||
Reference in New Issue
Block a user