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

CH32X035 Working #305

Merged
merged 32 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1b382a9
Working on making the x035 work.
cnlohr Apr 19, 2024
094dc33
Working on 035, need to regression test on 003 and 203
cnlohr Apr 19, 2024
cd36df3
Fix bootup code for 035
cnlohr Apr 19, 2024
526d9c5
Get systick up and running + debug printf + input.
cnlohr Apr 19, 2024
df3cdc8
Fixed up register addresses
cnlohr Apr 19, 2024
5f0474c
Update 035 work, getting close to USB
cnlohr Apr 19, 2024
76c99a6
Progress but still no go
cnlohr Apr 20, 2024
63e8327
Well, it enumerates.
cnlohr Apr 20, 2024
9294351
Oops forgot to commit file
cnlohr Apr 20, 2024
9732a81
Tweak setupp ins
cnlohr Apr 21, 2024
5c2b435
Merge branch 'ch32x035_work' of https://github.com/cnlohr/ch32v003fun…
cnlohr Apr 21, 2024
aeef7b7
One of the endpoints works, but not the other?
cnlohr Apr 21, 2024
6d49c00
Working device.
cnlohr Apr 21, 2024
15f6149
Stage 1 of cleanup complete.
cnlohr Apr 21, 2024
1dca24b
Clean up some control flow.
cnlohr Apr 21, 2024
85a1f95
Working with DMA
cnlohr Apr 22, 2024
6466e3c
Things are working, but albeit with a bit of a cursed stint.
cnlohr Apr 22, 2024
371d677
Hide the cursed junk behind other stuff.
cnlohr Apr 22, 2024
64866b9
Bump! Almost working hid reports.
cnlohr Apr 22, 2024
623eda3
I am just chasing my tail
cnlohr Apr 22, 2024
c612d10
Cleanup forgot to commit last night
cnlohr Apr 23, 2024
124fec8
Fix compile
cnlohr Apr 23, 2024
36e4c3b
Try to make CI happy
cnlohr Apr 23, 2024
30820c1
Build test straps
cnlohr Apr 23, 2024
53a25da
Fix CI
cnlohr Apr 23, 2024
7ab7a55
Add hidapi
cnlohr Apr 24, 2024
1763213
Add webusb demo
cnlohr Apr 24, 2024
9d7ee13
Update app
cnlohr Apr 24, 2024
5b0a0f4
Update with PD defines
cnlohr Apr 24, 2024
a1fe5b0
Fix 203 blink demo
cnlohr Apr 27, 2024
45c1d38
Update overall, tested and working on 203
cnlohr Apr 27, 2024
098fb62
Fix compile
cnlohr Apr 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Install Dependencies
run: sudo apt-get update && sudo apt-get install -y build-essential make libnewlib-dev gcc-riscv64-unknown-elf libusb-1.0-0-dev
run: sudo apt-get update && sudo apt-get install -y build-essential make libnewlib-dev gcc-riscv64-unknown-elf libusb-1.0-0-dev libudev-dev
- name: Build ${{ matrix.example }}
run: cd ${{ matrix.example }} && make V=1 -j3 $(basename ${{ matrix.example }}.elf) && riscv64-unknown-elf-size $(basename ${{ matrix.example }}.elf)
# Build using PlatformIO
Expand Down
148 changes: 118 additions & 30 deletions ch32v003fun/ch32v003fun.c
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ void NMI_Handler( void ) __attribute__((section(".text.vector_handler"))) _
void NMI_Handler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
#endif
void HardFault_Handler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
#if defined(CH32V20x) || defined(CH32V30x)
#if defined(CH32V20x) || defined(CH32V30x) || defined(CH32X03x)
void Ecall_M_Mode_Handler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void Ecall_U_Mode_Handler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void Break_Point_Handler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
Expand All @@ -732,7 +732,7 @@ void RTC_IRQHandler( void ) __attribute__((section(".text.vector_handler")))
#endif // defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x)
void FLASH_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void RCC_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
#ifdef CH32V003
#if defined(CH32V003) || defined(CH32X03x)
void EXTI7_0_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void AWU_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
#elif defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x)
Expand All @@ -749,7 +749,7 @@ void DMA1_Channel4_IRQHandler( void ) __attribute__((section(".text.vector_ha
void DMA1_Channel5_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void DMA1_Channel6_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void DMA1_Channel7_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
#ifdef CH32V003
#if defined( CH32V003 ) || defined(CH32X03x)
void ADC1_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
#elif defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x)
void ADC1_2_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
Expand All @@ -774,7 +774,7 @@ void TIM4_IRQHandler( void ) __attribute__((section(".text.vector_handler"))
#endif // defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x)
void I2C1_EV_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void I2C1_ER_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
#ifdef CH32V003
#if defined( CH32V003 ) || defined( CH32X03x )
void USART1_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void SPI1_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
#elif defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x)
Expand Down Expand Up @@ -840,7 +840,26 @@ void DMA2_Channel10_IRQHandler( void ) __attribute__((section(".text.vector_han
void DMA2_Channel11_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
#endif

#ifdef CH32V003
#if defined( CH32X03x)
void USART2_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void EXTI15_8_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void EXTI25_16_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void USART3_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void USART4_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void DMA1_Channel8_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void USBFS_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void USBFS_WakeUp_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void PIOC_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void OPA_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void USBPD_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void USBPD_WKUP_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void TIM2_CC_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void TIM2_TRG_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void TIM2_BRK_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void TIM3_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
#endif

#if defined( CH32V003 ) || defined( CH32X03x )

void InterruptVector() __attribute__((naked)) __attribute((section(".init"))) __attribute((weak,alias("InterruptVectorDefault")));
void InterruptVectorDefault() __attribute__((naked)) __attribute((section(".init")));
Expand All @@ -852,18 +871,46 @@ void InterruptVectorDefault()
.option push;\n\
.option norvc;\n\
j handle_reset\n" );
#if 0 // What is this for? I don't see any reason to have it.
#ifdef CH32X03x
asm volatile( "\n\
.word 0x00000013 \n\
.word 0x00000013 \n\
.word 0x00000013 \n\
.word 0x00000013 \n\
.word 0x00000013 \n\
.word 0x00000013 \n\
.word 0x00000013 \n\
.word 0x00000013 \n\
.word 0x00000013 \n\
.word 0x00000013 \n\
.word 0x00000013 \n\
.word 0x00000013 \n\
.word 0x00100073" );
#endif
#endif

#if !defined(FUNCONF_TINYVECTOR) || !FUNCONF_TINYVECTOR
asm volatile( "\n\
.word 0\n\
.word NMI_Handler /* NMI Handler */ \n\
.word HardFault_Handler /* Hard Fault Handler */ \n\
.word 0\n"
#if defined(CH32X03x)
" .word Ecall_M_Mode_Handler /* Ecall M Mode */ \n\
.word 0 \n\
.word 0 \n\
.word Ecall_U_Mode_Handler /* Ecall U Mode */ \n\
.word Break_Point_Handler /* Break Point */ \n\
"
#else
" .word 0\n\
.word 0\n\
.word 0\n\
.word 0\n\
.word 0\n\
.word 0\n\
.word 0\n\
.word 0\n\
.word 0\n"
#endif
" .word 0\n\
.word 0\n\
.word SysTick_Handler /* SysTick Handler */ \n\
.word 0\n\
Expand Down Expand Up @@ -892,8 +939,26 @@ void InterruptVectorDefault()
.word TIM1_UP_IRQHandler /* TIM1 Update */ \n\
.word TIM1_TRG_COM_IRQHandler /* TIM1 Trigger and Commutation */ \n\
.word TIM1_CC_IRQHandler /* TIM1 Capture Compare */ \n\
.word TIM2_IRQHandler /* TIM2 */ \n\
");
.word TIM2_IRQHandler /* TIM2 */ \n"
#if defined( CH32X03x )
" .word USART2_IRQHandler /* UART2 Interrupt */ \n\
.word EXTI15_8_IRQHandler /* External Line[8:15] Interrupt */ \n\
.word EXTI25_16_IRQHandler /* External Line[25:16] Interrupt */ \n\
.word USART3_IRQHandler /* UART2 Interrupt */ \n\
.word USART4_IRQHandler /* UART2 Interrupt */ \n\
.word DMA1_Channel8_IRQHandler /* DMA1 Channel 8 global Interrupt */ \n\
.word USBFS_IRQHandler /* USB Full-Speed Interrupt */ \n\
.word USBFS_WakeUp_IRQHandler /* USB Full-Speed Wake-Up Interrupt */ \n\
.word PIOC_IRQHandler /* Programmable IO Controller Interrupt */ \n\
.word OPA_IRQHandler /* Op Amp Interrupt */ \n\
.word USBPD_IRQHandler /* USB Power Delivery Interrupt */ \n\
.word USBPD_WKUP_IRQHandler /* USB Power Delivery Wake-Up Interrupt */ \n\
.word TIM2_CC_IRQHandler /* Timer 2 Compare Global Interrupt */ \n\
.word TIM2_TRG_IRQHandler /* Timer 2 Trigger Global Interrupt */ \n\
.word TIM2_BRK_IRQHandler /* Timer 2 Brk Global Interrupt */ \n\
.word TIM3_IRQHandler /* Timer 3 Global Interrupt */"
#endif
);
#endif
asm volatile( ".option pop;\n");
}
Expand All @@ -910,9 +975,17 @@ void handle_reset()
".option arch, +zicsr\n"
#endif
// Setup the interrupt vector, processor status and INTSYSCR.

#if FUNCONF_ENABLE_HPE // Enabled nested and hardware (HPE) stack, since it's really good on the x035.
" li t0, 0x88\n\
csrs mstatus, t0\n"
" li t0, 0x0b\n\
csrw 0x804, t0\n"
#else
" li a0, 0x80\n\
csrw mstatus, a0\n\
li a3, 0x3\n\
csrw mstatus, a0\n"
#endif
" li a3, 0x3\n\
la a0, InterruptVector\n\
or a0, a0, a3\n\
csrw mtvec, a0\n"
Expand Down Expand Up @@ -950,7 +1023,6 @@ asm volatile(
#endif
);


#if defined( FUNCONF_SYSTICK_USE_HCLK ) && FUNCONF_SYSTICK_USE_HCLK
SysTick->CTLR = 5;
#else
Expand Down Expand Up @@ -1137,7 +1209,8 @@ void InterruptVectorDefault()
.word DMA2_Channel10_IRQHandler /* DMA2 Channel 10 */ \n\
.word DMA2_Channel11_IRQHandler /* DMA2 Channel 11 */ \n"
#endif
#endif

#endif // !defined(FUNCONF_TINYVECTOR) || !FUNCONF_TINYVECTOR
" .option rvc; \n");

}
Expand Down Expand Up @@ -1190,14 +1263,19 @@ void handle_reset( void )
asm volatile(
" li t0, 0x1f\n\
csrw 0xbc0, t0\n"
#if defined(CH32V20x)
// Enabled nested and hardware stack

//XXX TODO: CHECKME - TEST ON 203!!!
#if FUNCONF_ENABLE_HPE // Enabled nested and hardware (HPE) stack, since it's really good on the x035.
" li t0, 0x88\n\
csrs mstatus, t0\n"
#elif defined(CH32V30x)
// Enable nested and hardware stack
" li t0, 0x0b\n\
csrw 0x804, t0\n"
#else
" li a0, 0x80\n\
csrw mstatus, a0\n"
#endif

#if defined(CH32V30x)
// Enable floating point and interrupt
" li t0, 0x688\n\
csrs mstatus, t0\n"
Expand Down Expand Up @@ -1232,6 +1310,12 @@ void SetupUART( int uartBRR )
// 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);
#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;

Expand Down Expand Up @@ -1403,9 +1487,11 @@ void DelaySysTick( uint32_t n )
#elif defined(CH32V20x) || defined(CH32V30x)
uint64_t targend = SysTick->CNT + n;
while( ((int64_t)( SysTick->CNT - targend )) < 0 );
#elif defined(CH32V10x)
#elif defined(CH32V10x) || defined(CH32X03x)
uint32_t targend = SysTick->CNTL + n;
while( ((int32_t)( SysTick->CNTL - targend )) < 0 );
#else
#error DelaySysTick not defined.
#endif
}

Expand All @@ -1430,7 +1516,7 @@ void SystemInit()
#define BASE_CFGR0 RCC_HPRE_DIV1 | RCC_PPRE2_DIV1 | RCC_PPRE1_DIV2 | PLL_MULTIPLICATION
#endif
#else
#if defined(CH32V003)
#if defined(CH32V003) || defined(CH32X03x)
#define BASE_CFGR0 RCC_HPRE_DIV1 // HCLK = SYSCLK = APB1 And, no pll.
#else
#define BASE_CFGR0 RCC_HPRE_DIV1 | RCC_PPRE2_DIV1 | RCC_PPRE1_DIV1
Expand All @@ -1441,6 +1527,17 @@ void SystemInit()
#define BASE_CTLR (((FUNCONF_HSITRIM) << 3) | RCC_HSION | HSEBYP | RCC_CSS)
//#define BASE_CTLR (((FUNCONF_HSITRIM) << 3) | HSEBYP | RCC_CSS) // disable HSI in HSE modes

// CH32V003 flash latency
#if defined(CH32X03x)
FLASH->ACTLR = FLASH_ACTLR_LATENCY_2; // +2 Cycle Latency (Recommended per TRM)
#elif defined(CH32V003)
#if FUNCONF_SYSTEM_CORE_CLOCK > 25000000
FLASH->ACTLR = FLASH_ACTLR_LATENCY_1; // +1 Cycle Latency
#else
FLASH->ACTLR = FLASH_ACTLR_LATENCY_0; // +0 Cycle Latency
#endif
#endif

#if defined(FUNCONF_USE_HSI) && FUNCONF_USE_HSI
#if defined(CH32V30x) || defined(CH32V20x) || defined(CH32V10x)
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
Expand Down Expand Up @@ -1483,15 +1580,6 @@ void SystemInit()
FLASH->ACTLR |= FLASH_ACTLR_PRFTBE;
#endif

// CH32V003 flash latency
#if defined(CH32V003)
#if FUNCONF_SYSTEM_CORE_CLOCK > 25000000
FLASH->ACTLR = FLASH_ACTLR_LATENCY_1; // +1 Cycle Latency
#else
FLASH->ACTLR = FLASH_ACTLR_LATENCY_0; // +0 Cycle Latency
#endif
#endif

// CH32V10x flash latency
#if defined(CH32V10x)
#if defined(FUNCONF_USE_HSE) && FUNCONF_USE_HSE
Expand Down
Loading
Loading