Combination of Circular Buffer and Bit Field
Bit Fields in C – GeeksforGeeks
STM32 Approach for USART
/* USER CODE BEGIN PV */
typedef struct {
char buff[32];
uint16_t writeI : 5;
uint16_t readI : 5;
}rec_buff;
rec_buff c_rec_buff = {.writeI = 0, .readI = 0};
/* USER CODE END PV */
/* USER CODE BEGIN PFP */
void cbuff_put(rec_buff *cbuf, uint8_t data);
uint8_t cbuff_get(rec_buff *cbuf);
/* USER CODE END PFP */
/* USER CODE BEGIN 3 */
uint8_t value = cbuff_get(&c_rec_buff);
// What to do with this value? ...
}
/* USER CODE END 3 */
/* USER CODE BEGIN 4 */
void cbuff_put(rec_buff *cbuf, uint8_t data)
{
cbuf->buff[cbuf->writeI++] = data;
}
uint8_t cbuff_get(rec_buff *cbuf)
{
while(cbuf->readI == cbuf->writeI);
return cbuf->buff[cbuf->readI++];
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
uint8_t rx = huart->Instance->DR;
cbuff_put(&c_rec_buff, rx);
// what to do next?
}
/* USER CODE END 4 */