Skip to content

Commit

Permalink
* Found a slightly more robus reset mechanism for programming
Browse files Browse the repository at this point in the history
* Output UART PRINTF on a default Port A9
  • Loading branch information
cnlohr committed Feb 13, 2024
1 parent 5801635 commit b75429b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
10 changes: 9 additions & 1 deletion ch32v003fun/ch32v003fun.c
Original file line number Diff line number Diff line change
Expand Up @@ -1211,13 +1211,21 @@ void handle_reset( void )
#if defined( FUNCONF_USE_UARTPRINTF ) && FUNCONF_USE_UARTPRINTF
void SetupUART( int uartBRR )
{
#ifdef CH32V003
// Enable GPIOD and UART.
RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_USART1;

// Push-Pull, 10MHz Output, GPIO D5, with AutoFunction
GPIOD->CFGLR &= ~(0xf<<(4*5));
GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP_AF)<<(4*5);

#else
RCC->APB2PCENR |= RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1;

// Push-Pull, 10MHz Output, GPIO A9, with AutoFunction
GPIOA->CFGHR &= ~(0xf<<(4*1));
GPIOA->CFGHR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP_AF)<<(4*1);
#endif

// 115200, 8n1. Note if you don't specify a mode, UART remains off even when UE_Set.
USART1->CTLR1 = USART_WordLength_8b | USART_Parity_No | USART_Mode_Tx;
USART1->CTLR2 = USART_StopBits_1;
Expand Down
6 changes: 6 additions & 0 deletions ch32v003fun/ch32v003fun.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
poll_input, handle_debug_input may be used with semihsoting.

Note: Semihosting appears to currently only work on the CH32V003 (may be improved in future)

For UART printf, on:
CH32V003, Port D5, 115200 8n1
CH32V203, Port A9, 115200 8n1

Modifications can be made to SetupUart, or your own version as desired.
*/


Expand Down
3 changes: 2 additions & 1 deletion minichlink/pgm-wch-linke.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,17 +337,18 @@ static int LESetupInterface( void * d )
return -1;

wch_link_multicommands( (libusb_device_handle *)dev, 1, 4, "\x81\x0d\x01\x13" ); // Try forcing reset line low.
wch_link_command( (libusb_device_handle *)dev, "\x81\x0d\x01\xff", 4, 0, 0, 0); //Exit programming

if( already_tried_reset > 3 )
{
MCF.DelayUS( iss, 5000 );
wch_link_command( dev, "\x81\x0d\x01\x03", 4, (int*)&transferred, rbuff, 1024 ); // Reply: Ignored, 820d050900300500
}
else
{
MCF.DelayUS( iss, 5000 );
}

wch_link_multicommands( (libusb_device_handle *)dev, 3, 4, "\x81\x0b\x01\x01", 4, "\x81\x0d\x01\x02", 4, "\x81\x0d\x01\xff" );
wch_link_multicommands( (libusb_device_handle *)dev, 1, 4, "\x81\x0d\x01\x14" ); // Release reset line.
wch_link_multicommands( (libusb_device_handle *)dev, 3, 4, "\x81\x0b\x01\x01", 4, "\x81\x0d\x01\x02", 4, "\x81\x0d\x01\xff" );
already_tried_reset++;
Expand Down

0 comments on commit b75429b

Please sign in to comment.