Skip to content

Commit 67590b8

Browse files
committed
Make x00x more intercompatible with v003, this makes it so x00x projects can be built without v003 shims.
1 parent a7adc57 commit 67590b8

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

ch32fun/ch32fun.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ void InterruptVectorDefault( void )
984984
#endif
985985
}
986986

987-
#if defined( CH32V003 ) || defined( CH32X03x )
987+
#if defined( CH32V003 ) || defined( CH32X03x ) || defined(CH32V00x)
988988

989989
void handle_reset( void )
990990
{
@@ -1467,7 +1467,7 @@ WEAK int putchar(int c)
14671467

14681468
void DelaySysTick( uint32_t n )
14691469
{
1470-
#ifdef CH32V003
1470+
#if defined(CH32V003) || defined(CH32V00x)
14711471
uint32_t targend = SysTick->CNT + n;
14721472
while( ((int32_t)( SysTick->CNT - targend )) < 0 );
14731473
#elif defined(CH32V20x) || defined(CH32V30x)
@@ -1500,15 +1500,15 @@ void SystemInit( void )
15001500
#endif
15011501

15021502
#if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL
1503-
#if defined(CH32V003)
1503+
#if defined(CH32V003) || defined(CH32V00x)
15041504
#define BASE_CFGR0 RCC_HPRE_DIV1 | RCC_PLLSRC_HSI_Mul2 // HCLK = SYSCLK = APB1 And, enable PLL
15051505
#elif defined(CH32V20x_D8W)
15061506
#define BASE_CFGR0 RCC_HPRE_DIV1 | RCC_PPRE2_DIV1 | RCC_PPRE1_DIV1 | PLL_MULTIPLICATION
15071507
#else
15081508
#define BASE_CFGR0 RCC_HPRE_DIV1 | RCC_PPRE2_DIV1 | RCC_PPRE1_DIV2 | PLL_MULTIPLICATION
15091509
#endif
15101510
#else
1511-
#if defined(CH32V003) || defined(CH32X03x)
1511+
#if defined(CH32V003) || defined(CH32X03x) || defined(CH32V00x)
15121512
#define BASE_CFGR0 RCC_HPRE_DIV1 // HCLK = SYSCLK = APB1 And, no pll.
15131513
#else
15141514
#define BASE_CFGR0 RCC_HPRE_DIV1 | RCC_PPRE2_DIV1 | RCC_PPRE1_DIV1
@@ -1553,7 +1553,7 @@ void SystemInit( void )
15531553

15541554
#elif defined(FUNCONF_USE_HSE) && FUNCONF_USE_HSE
15551555

1556-
#if defined(CH32V003)
1556+
#if defined(CH32V003) || defined(CH32V00x)
15571557
RCC->CTLR = BASE_CTLR | RCC_HSION | RCC_HSEON ; // Keep HSI on while turning on HSE
15581558
#else
15591559
RCC->CTLR = RCC_HSEON; // Only turn on HSE.
@@ -1562,7 +1562,7 @@ void SystemInit( void )
15621562
// Values lifted from the EVT. There is little to no documentation on what this does.
15631563
while(!(RCC->CTLR&RCC_HSERDY)) {};
15641564

1565-
#if defined(CH32V003)
1565+
#if defined(CH32V003) || defined(CH32V00x)
15661566
RCC->CFGR0 = RCC_PLLSRC_HSE_Mul2 | RCC_SW_HSE;
15671567
#else
15681568
RCC->CFGR0 = BASE_CFGR0 | RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE;
@@ -1631,12 +1631,12 @@ void funAnalogInit( void )
16311631
ADC1->CTLR2 |= ADC_ADON | ADC_EXTSEL; // turn on ADC and set rule group to sw trig
16321632

16331633
// Reset calibration
1634-
ADC1->CTLR2 |= ADC_RSTCAL;
1635-
while(ADC1->CTLR2 & ADC_RSTCAL);
1634+
ADC1->CTLR2 |= CTLR2_RSTCAL_Set;
1635+
while(ADC1->CTLR2 & CTLR2_RSTCAL_Set);
16361636

16371637
// Calibrate
1638-
ADC1->CTLR2 |= ADC_CAL;
1639-
while(ADC1->CTLR2 & ADC_CAL);
1638+
ADC1->CTLR2 |= CTLR2_CAL_Set;
1639+
while(ADC1->CTLR2 & CTLR2_CAL_Set);
16401640

16411641
}
16421642

ch32fun/ch32fun.h

+2
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;
338338

339339
#ifdef CH32V003
340340
#include "ch32v003hw.h"
341+
#elif defined( CH32V002 ) || defined( CH32V00x )
342+
#include "ch32x00xhw.h"
341343
#elif defined( CH32X03x )
342344
#include "ch32x03xhw.h"
343345
#elif defined( CH32X03x )

ch32fun/ch32x00xhw.h

+42-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* This file supports the CH32V00X and CH32M00X family of chips
22
*/
33

4-
#ifndef TODO_HARDWARE_H
5-
#define TODO_HARDWARE_H
4+
#ifndef TODO_CH32X00X_H
5+
#define TODO_CH32X00X_H
66

77
#include "ch32fun.h"
88

@@ -117,13 +117,19 @@ typedef struct
117117
#define HSE_Value HSE_VALUE
118118
#define HSEStartUp_TimeOut HSE_STARTUP_TIMEOUT
119119

120+
// Datasheet recommends HSE of 24M
121+
#ifndef HSE_VALUE
122+
#define HSE_VALUE 24000000
123+
#endif
124+
120125
#ifndef __ASSEMBLER__
121126
/* Analog to Digital Converter */
122127
typedef struct
123128
{
124129
__IO uint32_t STATR;
125130
__IO uint32_t CTLR1;
126131
__IO uint32_t CTLR2;
132+
__IO uint32_t SAMPTR1;
127133
__IO uint32_t SAMPTR2;
128134
__IO uint32_t IOFR1;
129135
__IO uint32_t IOFR2;
@@ -235,6 +241,21 @@ typedef enum
235241
GPIO_CFGLR_OUT_OD = 0b0101,
236242
GPIO_CFGLR_OUT_AF_PP = 0b1001,
237243
GPIO_CFGLR_OUT_AF_OD = 0b1101,
244+
245+
246+
// For intercompatibility with 003 legacy code.
247+
GPIO_CFGLR_OUT_10Mhz_PP = 0b0001,
248+
GPIO_CFGLR_OUT_2Mhz_PP = 0b0001,
249+
GPIO_CFGLR_OUT_50Mhz_PP = 0b0001,
250+
GPIO_CFGLR_OUT_10Mhz_OD = 0b0101,
251+
GPIO_CFGLR_OUT_2Mhz_OD = 0b0101,
252+
GPIO_CFGLR_OUT_50Mhz_OD = 0b0101,
253+
GPIO_CFGLR_OUT_10Mhz_AF_PP = 0b1001,
254+
GPIO_CFGLR_OUT_2Mhz_AF_PP = 0b1001,
255+
GPIO_CFGLR_OUT_50Mhz_AF_PP = 0b1001,
256+
GPIO_CFGLR_OUT_10Mhz_AF_OD = 0b1101,
257+
GPIO_CFGLR_OUT_2Mhz_AF_OD = 0b1101,
258+
GPIO_CFGLR_OUT_50Mhz_AF_OD = 0b1101,
238259
} GPIO_CFGLR_PIN_MODE_Typedef;
239260

240261
/* This was correct in the 003, but the 00X have 0b0010 as a reserved bit in this field. The above enum never sets this bit as directed by the RM. But it's included in the following structure as a 4 bit field becaus changing it to two disjoint bit fields would make life too painful. */
@@ -403,11 +424,23 @@ typedef struct
403424
__IO uint32_t CTLR;
404425
__IO uint32_t CFGR0;
405426
__IO uint32_t INTR;
406-
__IO uint32_t PB2PRSTR;
427+
union
428+
{
429+
__IO uint32_t PB2PRSTR;
430+
__IO uint32_t APB2PRSTR; // For 003 compatibility
431+
};
407432
__IO uint32_t PB1PRSTR;
408433
__IO uint32_t HBPCENR;
409-
__IO uint32_t PB2PCENR;
410-
__IO uint32_t PB1PCENR;
434+
union
435+
{
436+
__IO uint32_t PB2PCENR;
437+
__IO uint32_t APB2PCENR; // For 003 compatibility
438+
};
439+
union
440+
{
441+
__IO uint32_t PB1PCENR;
442+
__IO uint32_t APB1PCENR; // For 003 compatibility
443+
};
411444
__IO uint32_t RESERVED0;
412445
__IO uint32_t RSTSCKR;
413446
} RCC_TypeDef;
@@ -1893,6 +1926,10 @@ typedef struct
18931926

18941927
#define RCC_PLLSRC ((uint32_t)0x00010000) /* PLL entry clock source */
18951928

1929+
//For compatibility with the v003
1930+
#define RCC_PLLSRC_HSI_Mul2 ((uint32_t)0x00000000) /* HSI clock*2 selected as PLL entry clock source */
1931+
#define RCC_PLLSRC_HSE_Mul2 ((uint32_t)0x00010000) /* HSE clock*2 selected as PLL entry clock source */
1932+
18961933
#define RCC_CFGR0_MCO ((uint32_t)0x07000000) /* MCO[2:0] bits (Microcontroller Clock Output) */
18971934
#define RCC_MCO_0 ((uint32_t)0x01000000) /* Bit 0 */
18981935
#define RCC_MCO_1 ((uint32_t)0x02000000) /* Bit 1 */

0 commit comments

Comments
 (0)