Skip to content

HardwareSerial: use correct UART HW for TX #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

danielkucera
Copy link

@danielkucera danielkucera commented Feb 14, 2025

Fixes #159

Copy link
Contributor

@maxint-rd maxint-rd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to compile for CH32X033, but failed. Simple errors though:

  • HardwareSerial.cpp - HardwareSerial::write - len should be size
  • uart.c - uart_putc: USART_SendData returns void, so ending should probably be
  USART_SendData(uart_handlers[obj->index]->Instance, c);
  return 0; // 0 means no error

After making these changes it compiled.
(Currently still figuring out how to properly use UART2 as Serial and get PA3 working as RX on CH32X033F8P6)

@danielkucera
Copy link
Author

@maxint-rd , thanks for the notes, I have no idea why my compiler just took that.

What is the issue you are still having? I ended up implementing the RX as interrupt for concurrency reasons.

  NVIC_EnableIRQ( USART1_IRQn );
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
...
 void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  void USART1_IRQHandler(void){
    __disable_irq();

    if(USART1->STATR & USART_FLAG_RXNE){
      buf[p] = USART1->DATAR & (uint16_t)0x01FF;
...

@maxint-rd
Copy link
Contributor

maxint-rd commented Apr 18, 2025

Hi @danielkucera - Thanks for your reply. I have things working now.

In PR #145 I implemented interrupt driven RX for the CH32V003. That PR only supported Serial1. The CH32X033 has no free pins for regular RX/TX on UART1 (I also want to use SCL/SDA), so I wanted to use UART2 for serial. (RX2/TX2 on PA4/PA3). For that I 've just successfully tested adding a UART2 interrupt handler.
(My interrupt handler code is comparable to yours, but slightly different, since I've reused some old core code that had a cyclic buffer implemented with head and tail pointers. Those are used to properly implement available() and peek().)

In PR #171 I published my addition of board files for the CH32X033. In variant_CH32X033F8P.h I now made SERIAL_UART_INSTANCE 2 the preferred serial port and got it working for both TX and interrupt driven RX. (Hooray!)

BTW. Since my X033's are those with a 0 in their number they don't have hardware I2C (or VREF). In my tests I luckily could use the SoftwareI2C library, so the chips are getting more useful by the hour :-)
I still would like to be able to use USB for serial and also use multiple serial ports simultaneously (see issue #159), but I guess that's something for future fun...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CH32x033F8P6 How to use multiple serial ports
2 participants