ch32v003funでprintfが手軽に使用できるようにライブラリを修正しました。
ch32v003fun.cの1730行あたりの関数SetupUARTに追加します。
- void SetupUART( int uartBRR )
- {
- #ifdef CH32V003
- // Enable GPIOD and UART.
- RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO;
- #ifdef USE_UARTPRINTF_PD6
- // Push-Pull, 10MHz Output, GPIO D6, with AutoFunction
- GPIOD->CFGLR &= ~( 0xf << ( 4 * 6 ) );
- GPIOD->CFGLR |= ( GPIO_Speed_10MHz | GPIO_CNF_OUT_PP_AF ) << ( 4 * 6 );
- // AFIO->PCFR1 |= 0x200000; // 再マップ
- AFIO->PCFR1 &= ~( 1 << 2 | 1 << 21 ); // 再マップ
- AFIO->PCFR1 |= 1 << 21;
- #else
- // 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 );
- // 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 );
- #endif
- #elif defined( CH32X03x )
- RCC->APB2PCENR |= RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1;
- // Push-Pull, 10MHz Output, GPIO A9, with AutoFunction
- GPIOB->CFGHR &= ~( 0xf << ( 4 * 2 ) );
- GPIOB->CFGHR |= ( GPIO_Speed_10MHz | GPIO_CNF_OUT_PP_AF ) << ( 4 * 2 );
- #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;
- USART1->CTLR3 = USART_HardwareFlowControl_None;
- USART1->BRR = uartBRR;
- USART1->CTLR1 |= CTLR1_UE_Set;
- }
funconfig.hを次のように書き換えます。
- #ifndef _FUNCONFIG_H
- #define _FUNCONFIG_H
- #define CH32V003 1
- #define FUNCONF_USE_UARTPRINTF 1
- #define USE_UARTPRINTF_PD6 1
- // #define FUNCONF_USE_DEBUGPRINTF 1
- #endif