Skip to content

Commit 6dd3137

Browse files
committed
hal: wch: update the HAL to pull in CH32V00x support
Update the HAL to cnlohr/ch32fun@c234dad This pulls in CH32V00x support which will fix the AFIO remapping issue. Signed-off-by: Michael Hope <michaelh@juju.nz>
1 parent 941f518 commit 6dd3137

File tree

3 files changed

+511
-579
lines changed

3 files changed

+511
-579
lines changed

ch32fun/ch32fun.h

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@
128128
#define FUNCONF_DEBUG_HARDFAULT 1
129129
#endif
130130

131+
#if !defined( FUNCONF_INIT_ANALOG )
132+
#define FUNCONF_INIT_ANALOG 1
133+
#endif
134+
131135
#if defined( CH32X03x ) && FUNCONF_USE_PLL
132136
#error No PLL on the X03x
133137
#endif
@@ -160,20 +164,23 @@
160164
#endif
161165
#elif defined(CH32V30x)
162166
#define HSE_VALUE (8000000)
167+
#elif defined(CH57x) || defined(CH58x) || defined(CH59x)
168+
#define HSE_VALUE (32000000)
163169
#endif
164170
#endif
165171

172+
// Value of the Internal oscillator in Hz, default.
166173
#ifndef HSI_VALUE
167-
#if defined(CH32V003)
168-
#define HSI_VALUE (24000000) // Value of the Internal oscillator in Hz, default.
174+
#if defined(CH32V003) || defined(CH32V00x)
175+
#define HSI_VALUE (24000000)
169176
#elif defined(CH32X03x)
170-
#define HSI_VALUE (48000000)
177+
#define HSI_VALUE (48000000)
171178
#elif defined(CH32V10x)
172-
#define HSI_VALUE (8000000)
179+
#define HSI_VALUE (8000000)
173180
#elif defined(CH32V20x)
174-
#define HSI_VALUE (8000000)
181+
#define HSI_VALUE (8000000)
175182
#elif defined(CH32V30x)
176-
#define HSI_VALUE (8000000)
183+
#define HSI_VALUE (8000000)
177184
#endif
178185
#endif
179186

@@ -202,7 +209,12 @@
202209
#endif
203210

204211
#ifndef FUNCONF_SYSTEM_CORE_CLOCK
205-
#if defined(FUNCONF_USE_HSI) && FUNCONF_USE_HSI
212+
#if defined(CH57x) || defined(CH58x) || defined(CH59x) // no PLL multiplier, but a divider from the 480 MHz clock
213+
#define FUNCONF_SYSTEM_CORE_CLOCK 60000000 // default in ch32fun.c using CLK_SOURCE_PLL_60MHz
214+
#if defined(CLK_SOURCE_CH5XX)
215+
#error Must define FUNCONF_SYSTEM_CORE_CLOCK too if CLK_SOURCE_CH5XX is defined
216+
#endif
217+
#elif defined(FUNCONF_USE_HSI) && FUNCONF_USE_HSI
206218
#define FUNCONF_SYSTEM_CORE_CLOCK ((HSI_VALUE)*(FUNCONF_PLL_MULTIPLIER))
207219
#elif defined(FUNCONF_USE_HSE) && FUNCONF_USE_HSE
208220
#define FUNCONF_SYSTEM_CORE_CLOCK ((HSE_VALUE)*(FUNCONF_PLL_MULTIPLIER))
@@ -338,6 +350,8 @@ typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;
338350

339351
#ifdef CH32V003
340352
#include "ch32v003hw.h"
353+
#elif defined( CH32V002 ) || defined( CH32V00x )
354+
#include "ch32x00xhw.h"
341355
#elif defined( CH32X03x )
342356
#include "ch32x03xhw.h"
343357
#elif defined( CH32X03x )
@@ -348,6 +362,12 @@ typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;
348362
#include "ch32v20xhw.h"
349363
#elif defined( CH32V30x )
350364
#include "ch32v30xhw.h"
365+
#elif defined( CH57x )
366+
#include "ch57xhw.h"
367+
#elif defined( CH58x )
368+
#include "ch58xhw.h"
369+
#elif defined( CH59x )
370+
#include "ch59xhw.h"
351371
#endif
352372

353373
#if defined(__riscv) || defined(__riscv__) || defined( CH32V003FUN_BASE )
@@ -817,11 +837,55 @@ extern "C" {
817837
// and take two cycles, so you typically would use 0, 2, 4, etc.
818838
#define ADD_N_NOPS( n ) asm volatile( ".rept " #n "\nc.nop\n.endr" );
819839

840+
#define FUN_HIGH 0x1
841+
#define FUN_LOW 0x0
842+
#if defined(CH57x) || defined(CH58x) || defined(CH59x)
843+
#if defined( PB ) && defined( R32_PB_PIN )
844+
#define OFFSET_FOR_GPIOB(pin) (((pin & PB) >> 31) * (&R32_PB_PIN - &R32_PA_PIN)) // 0 if GPIOA, 0x20 if GPIOB
845+
#else
846+
#define PB 0
847+
#define OFFSET_FOR_GPIOB(pin) 0
848+
#endif
849+
#define GPIO_ResetBits(pin) (*(&R32_PA_CLR + OFFSET_FOR_GPIOB(pin)) |= (pin & ~PB))
850+
#define GPIO_SetBits(pin) (*(&R32_PA_OUT + OFFSET_FOR_GPIOB(pin)) |= (pin & ~PB))
851+
#define GPIO_InverseBits(pin) (*(&R32_PA_OUT + OFFSET_FOR_GPIOB(pin)) ^= (pin & ~PB))
852+
#define GPIO_ReadPortPin(pin) (*(&R32_PA_PIN + OFFSET_FOR_GPIOB(pin)) & (pin & ~PB))
853+
#define funDigitalRead(pin) GPIO_ReadPortPin(pin)
854+
#define funDigitalWrite( pin, value ) { if((value)==FUN_HIGH){GPIO_SetBits(pin);} else if((value)==FUN_LOW){GPIO_ResetBits(pin);} }
855+
#define funGpioInitAll() // funGpioInitAll() does not do anything on ch5xx, put here for consistency
856+
857+
RV_STATIC_INLINE void funPinMode(u32 pin, GPIOModeTypeDef mode)
858+
{
859+
switch(mode) {
860+
case GPIO_ModeIN_Floating:
861+
*(&R32_PA_PD_DRV + OFFSET_FOR_GPIOB(pin)) &= ~(pin & ~PB);
862+
*(&R32_PA_PU + OFFSET_FOR_GPIOB(pin)) &= ~(pin & ~PB);
863+
*(&R32_PA_DIR + OFFSET_FOR_GPIOB(pin)) &= ~(pin & ~PB);
864+
break;
865+
case GPIO_ModeIN_PU:
866+
*(&R32_PA_PD_DRV + OFFSET_FOR_GPIOB(pin)) &= ~(pin & ~PB);
867+
*(&R32_PA_PU + OFFSET_FOR_GPIOB(pin)) |= (pin & ~PB);
868+
*(&R32_PA_DIR + OFFSET_FOR_GPIOB(pin)) &= ~(pin & ~PB);
869+
break;
870+
case GPIO_ModeIN_PD:
871+
*(&R32_PA_PD_DRV + OFFSET_FOR_GPIOB(pin)) |= (pin & ~PB);
872+
*(&R32_PA_PU + OFFSET_FOR_GPIOB(pin)) &= ~(pin & ~PB);
873+
*(&R32_PA_DIR + OFFSET_FOR_GPIOB(pin)) &= ~(pin & ~PB);
874+
break;
875+
case GPIO_ModeOut_PP_5mA:
876+
*(&R32_PA_PD_DRV + OFFSET_FOR_GPIOB(pin)) &= ~(pin & ~PB);
877+
*(&R32_PA_DIR + OFFSET_FOR_GPIOB(pin)) |= (pin & ~PB);
878+
break;
879+
case GPIO_ModeOut_PP_20mA:
880+
*(&R32_PA_PD_DRV + OFFSET_FOR_GPIOB(pin)) |= (pin & ~PB);
881+
*(&R32_PA_DIR + OFFSET_FOR_GPIOB(pin)) |= (pin & ~PB);
882+
break;
883+
}
884+
}
885+
#else
820886
// Arduino-like GPIO Functionality
821887
#define GpioOf( pin ) ((GPIO_TypeDef *)(GPIOA_BASE + 0x400 * ((pin)>>4)))
822888

823-
#define FUN_HIGH 0x1
824-
#define FUN_LOW 0x0
825889
#define FUN_OUTPUT (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)
826890
#define FUN_INPUT (GPIO_CNF_IN_FLOATING)
827891

@@ -846,6 +910,7 @@ extern "C" {
846910
#define funGpioInitC() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC ); }
847911
#define funGpioInitD() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD ); }
848912
#define funDigitalRead( pin ) ((int)((GpioOf(pin)->INDR >> ((pin)&0xf)) & 1))
913+
#endif
849914

850915

851916
#define ANALOG_0 0
@@ -902,7 +967,11 @@ static inline void Delay_Tiny( int n ) {
902967
#endif //defined(__riscv) || defined(__riscv__) || defined( CH32V003FUN_BASE )
903968

904969
// Tricky: We need to make sure main and SystemInit() are preserved.
970+
#ifdef MINICHLINK
971+
int main( int argc, char ** argv) __attribute__((used));
972+
#else
905973
int main() __attribute__((used));
974+
#endif
906975
void SystemInit(void);
907976

908977
#ifdef FUNCONF_UART_PRINTF_BAUD

0 commit comments

Comments
 (0)