Skip to content

Instantly share code, notes, and snippets.

@tanakamasayuki
Created April 12, 2024 03:13
Show Gist options
  • 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);
}
@maxint-rd
Copy link

Excellent code! I'm currently using this to to see how I can integrate using interrupts in the CH32 Arduino core.
I'm testing it on the CH32V003 to improve my clumsy polling implementation.

@tanakamasayuki
Copy link
Author

https://github.com/ch32-riscv-ug/arduino_core_ch32_riscv_arduino

I am making a new Arduino Core.
It is not yet complete.
One of its features is that the EVT works as is.

@maxint-rd
Copy link

Impressive! That's quite a bit of work you've taken upon yourself.
I'm currently using the WCH Arduino core for my CH32V003 projects. Using the Arduino API allows me to reuse many libraries.
After getting features such as working I2C and Serial working, I now often run into the limited flash space, partly caused by HAL overhead. Your core promises a smaller footprint. Good to see potential alternatives coming up...

For the WCH core I've contributed features such as I2C slave and Eeprom emulation. Your code helped me with interrupt driven Serial RX. Thank you! (I'll probably submit my PR for those changes sometime soon.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment