Skip to content
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

Baud rate setting missed (fixed) #4

Open
git2fa opened this issue Oct 3, 2024 · 0 comments
Open

Baud rate setting missed (fixed) #4

git2fa opened this issue Oct 3, 2024 · 0 comments

Comments

@git2fa
Copy link

git2fa commented Oct 3, 2024

Thank you for this nice libraries - they are very helpful!

The ch32v003_uart.h is missing a function to set the baud rate and a simple function to transmit strings without the use of printf.

Here is the simple function to be added to output strings:

/**
 * @brief print 0-terminated string
 */
void uart_string (const char *s) {
	while (*s) {
		UART_putc(*s);
		s ++;
	}
}

Now you can simple output with
uart_string("Test");
or with

	char spuffer[17];

	ultoa(variable, spuffer, 10);
	uart_string(spuffer);

In the same way it was nice to input directly strings terminated with \n:

static volatile uint8_t uart_rxHead, uart_rxTail, RXDComplete;

#if defined (UART_MODE_RX)
	if (status & UART_IT_RXC_ENABLE)  // byte received, the same bit as in CTRL1
	{
		data = USART1->DATAR;
		tmp8 = (uart_rxHead + 1) & UART_RX_BF_MASK;
		if (tmp8 == uart_rxTail)	lastErr |= UART_ERR_RXBUF_OVERFLOW;
		uart_rxHead = tmp8;
		if(data != '\n' && data != '\r') {
			uart_rx_bf[tmp8] = data;
		} else {
			uart_rx_bf[tmp8] = 0;
			RXDComplete = 1;
		}
	}
	lastRXerrorFlag = lastErr & 0xFF00;
#endif

Just ask for the flag with if (RXDComplete) and reset it to 0 after fetching and computing the string.

I have a strange sporadic error transmitting data through the UART to an chinese PL2303 USB-converter.
Sometimes the second byte of a transmitted string was wrong. So I thought that 115200 baud is to much, but the same error occurs at 57600 baud. There is no idea what is causing it.

But there was the need to set another baud rate and so the register values have to be calculated.

BAUD

This is done with this Calc-file, so other crystal frequencies can be used and there are some more sheets with calculators, that will comfortable decode some other registers.

CH32V003 Register ods

(ods cannot be uploaded here, so please save the file and delete the ending .jpg)

The baud rate can be set directly after the init of the UART:

	UART_init();
	USART1->BRR = 0x341;	// 57600 Baud

That's it - much 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

No branches or pull requests

1 participant