Skip to content

Instantly share code, notes, and snippets.

@tanakamasayuki
Created April 12, 2024 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanakamasayuki/56251790ad3c06a9837895054fd2f15e to your computer and use it in GitHub Desktop.
Save tanakamasayuki/56251790ad3c06a9837895054fd2f15e to your computer and use it in GitHub Desktop.
const size_t usart1_buff_size = 256;
char usart1_buff[usart1_buff_size];
uint16_t usart1_buff_write = 0;
uint16_t usart1_buff_read = 0;
#ifdef __cplusplus
extern "C" {
#endif
void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void USART1_IRQHandler(void) {
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
usart1_buff[usart1_buff_write] = USART_ReceiveData(USART1);
usart1_buff_write++;
usart1_buff_write %= usart1_buff_size;
}
#ifdef __cplusplus
}
#endif
int usart1_available() {
return (usart1_buff_write != usart1_buff_read);
}
char usart1_get() {
uint8_t c;
if (usart1_buff_write != usart1_buff_read) {
c = usart1_buff[usart1_buff_read];
usart1_buff_read++;
usart1_buff_read %= usart1_buff_size;
return c;
}
return 0;
}
void setup() {
Serial.begin(115200);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_SetPriority(USART1_IRQn, UART_IRQ_PRIO);
NVIC_EnableIRQ(USART1_IRQn);
}
void loop() {
while (usart1_available()) {
Serial.print(usart1_get());
}
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment