Created
April 12, 2024 03:13
-
-
Save tanakamasayuki/56251790ad3c06a9837895054fd2f15e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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