128
128
#define FUNCONF_DEBUG_HARDFAULT 1
129
129
#endif
130
130
131
+ #if !defined( FUNCONF_INIT_ANALOG )
132
+ #define FUNCONF_INIT_ANALOG 1
133
+ #endif
134
+
131
135
#if defined( CH32X03x ) && FUNCONF_USE_PLL
132
136
#error No PLL on the X03x
133
137
#endif
160
164
#endif
161
165
#elif defined(CH32V30x )
162
166
#define HSE_VALUE (8000000)
167
+ #elif defined(CH57x ) || defined(CH58x ) || defined(CH59x )
168
+ #define HSE_VALUE (32000000)
163
169
#endif
164
170
#endif
165
171
172
+ // Value of the Internal oscillator in Hz, default.
166
173
#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)
169
176
#elif defined(CH32X03x )
170
- #define HSI_VALUE (48000000)
177
+ #define HSI_VALUE (48000000)
171
178
#elif defined(CH32V10x )
172
- #define HSI_VALUE (8000000)
179
+ #define HSI_VALUE (8000000)
173
180
#elif defined(CH32V20x )
174
- #define HSI_VALUE (8000000)
181
+ #define HSI_VALUE (8000000)
175
182
#elif defined(CH32V30x )
176
- #define HSI_VALUE (8000000)
183
+ #define HSI_VALUE (8000000)
177
184
#endif
178
185
#endif
179
186
202
209
#endif
203
210
204
211
#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
206
218
#define FUNCONF_SYSTEM_CORE_CLOCK ((HSI_VALUE)*(FUNCONF_PLL_MULTIPLIER))
207
219
#elif defined(FUNCONF_USE_HSE ) && FUNCONF_USE_HSE
208
220
#define FUNCONF_SYSTEM_CORE_CLOCK ((HSE_VALUE)*(FUNCONF_PLL_MULTIPLIER))
@@ -338,6 +350,8 @@ typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;
338
350
339
351
#ifdef CH32V003
340
352
#include "ch32v003hw.h"
353
+ #elif defined( CH32V002 ) || defined( CH32V00x )
354
+ #include "ch32x00xhw.h"
341
355
#elif defined( CH32X03x )
342
356
#include "ch32x03xhw.h"
343
357
#elif defined( CH32X03x )
@@ -348,6 +362,12 @@ typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;
348
362
#include "ch32v20xhw.h"
349
363
#elif defined( CH32V30x )
350
364
#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"
351
371
#endif
352
372
353
373
#if defined(__riscv ) || defined(__riscv__ ) || defined( CH32V003FUN_BASE )
@@ -817,11 +837,55 @@ extern "C" {
817
837
// and take two cycles, so you typically would use 0, 2, 4, etc.
818
838
#define ADD_N_NOPS ( n ) asm volatile( ".rept " #n "\nc.nop\n.endr" );
819
839
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
820
886
// Arduino-like GPIO Functionality
821
887
#define GpioOf ( pin ) ((GPIO_TypeDef *)(GPIOA_BASE + 0x400 * ((pin)>>4)))
822
888
823
- #define FUN_HIGH 0x1
824
- #define FUN_LOW 0x0
825
889
#define FUN_OUTPUT (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)
826
890
#define FUN_INPUT (GPIO_CNF_IN_FLOATING)
827
891
@@ -846,6 +910,7 @@ extern "C" {
846
910
#define funGpioInitC () { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC ); }
847
911
#define funGpioInitD () { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD ); }
848
912
#define funDigitalRead ( pin ) ((int)((GpioOf(pin)->INDR >> ((pin)&0xf)) & 1))
913
+ #endif
849
914
850
915
851
916
#define ANALOG_0 0
@@ -902,7 +967,11 @@ static inline void Delay_Tiny( int n ) {
902
967
#endif //defined(__riscv) || defined(__riscv__) || defined( CH32V003FUN_BASE )
903
968
904
969
// 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
905
973
int main () __attribute__((used ));
974
+ #endif
906
975
void SystemInit (void );
907
976
908
977
#ifdef FUNCONF_UART_PRINTF_BAUD
0 commit comments