From b5d2eac413966795467a19ba824eb4a5091a573d Mon Sep 17 00:00:00 2001 From: IOsetting Date: Sun, 9 Jan 2022 19:26:52 +0800 Subject: [PATCH] opt: rename uart1 config methods --- demo/adc/adc_interrupt_10bit.c | 2 +- demo/adc/adc_interrupt_2ch.c | 2 +- demo/adc/adc_poll_10bit.c | 2 +- demo/adc/adc_poll_8bit.c | 2 +- demo/mem/mem_read_chipid.c | 2 +- demo/pca/pca_pwm_10bit_2ch.c | 2 +- demo/pca/pca_pwm_8bit_2ch.c | 2 +- demo/pwm/pwm_2ch_complementary.c | 2 +- demo/pwm/pwm_2ch_timer2_interrupt.c | 2 +- demo/spi/nrf24l01/nrf24l01_stc8h1k.c | 2 +- demo/tim/timer0_print_cpuid.c | 2 +- demo/tim/timer0_timer_1t.c | 2 +- demo/tim/timer2_timer_12t.c | 2 +- demo/tim/timer2_timer_1t.c | 2 +- demo/uart/uart1_timer1_tx.c | 2 +- demo/uart/uart1_timer2_interrupt_tx_rx.c | 2 +- demo/uart/uart1_timer2_tx.c | 2 +- include/fw_exti.h | 19 ++-- include/fw_sys.h | 109 +++++++++++++++++++++++ include/fw_uart.h | 4 +- src/fw_uart.c | 4 +- 21 files changed, 144 insertions(+), 26 deletions(-) diff --git a/demo/adc/adc_interrupt_10bit.c b/demo/adc/adc_interrupt_10bit.c index 215c8c0..464e64e 100644 --- a/demo/adc/adc_interrupt_10bit.c +++ b/demo/adc/adc_interrupt_10bit.c @@ -44,7 +44,7 @@ void main(void) { SYS_SetClock(); // For debug print - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); // Set ADC1(GPIO P1.1) HIP GPIO_P1_SetMode(GPIO_Pin_1, GPIO_Mode_Input_HIP); // Channel: ADC1 diff --git a/demo/adc/adc_interrupt_2ch.c b/demo/adc/adc_interrupt_2ch.c index 8b9e92e..0e932d1 100644 --- a/demo/adc/adc_interrupt_2ch.c +++ b/demo/adc/adc_interrupt_2ch.c @@ -64,7 +64,7 @@ void main(void) { SYS_SetClock(); // For debug print - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); // Channel: ADC1 ADC_SetChannel(0x01); // ADC Clock = SYSCLK / 2 / (1+15) = SYSCLK / 32 diff --git a/demo/adc/adc_poll_10bit.c b/demo/adc/adc_poll_10bit.c index fbdb1ac..2837dcf 100644 --- a/demo/adc/adc_poll_10bit.c +++ b/demo/adc/adc_poll_10bit.c @@ -34,7 +34,7 @@ void main(void) uint16_t res; SYS_SetClock(); // For debug print - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); // Set ADC1(GPIO P1.1) HIP GPIO_P1_SetMode(GPIO_Pin_1, GPIO_Mode_Input_HIP); // Channel: ADC1 diff --git a/demo/adc/adc_poll_8bit.c b/demo/adc/adc_poll_8bit.c index e3e9ca4..3ba0510 100644 --- a/demo/adc/adc_poll_8bit.c +++ b/demo/adc/adc_poll_8bit.c @@ -34,7 +34,7 @@ void main(void) uint8_t res; SYS_SetClock(); // For debug print - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); // Set ADC1(GPIO P1.1) HIP GPIO_P1_SetMode(GPIO_Pin_1, GPIO_Mode_Input_HIP); // Channel: ADC1 diff --git a/demo/mem/mem_read_chipid.c b/demo/mem/mem_read_chipid.c index 1a314e5..d6e4c88 100644 --- a/demo/mem/mem_read_chipid.c +++ b/demo/mem/mem_read_chipid.c @@ -27,7 +27,7 @@ void main(void) { SYS_SetClock(); // UART1 configuration: baud 115200 with Timer2, 1T mode, no interrupt - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); MEM_ReadChipID(buff); for (i = 0; i < 32; i++) diff --git a/demo/pca/pca_pwm_10bit_2ch.c b/demo/pca/pca_pwm_10bit_2ch.c index b719250..44be86b 100644 --- a/demo/pca/pca_pwm_10bit_2ch.c +++ b/demo/pca/pca_pwm_10bit_2ch.c @@ -21,7 +21,7 @@ void main(void) { SYS_SetClock(); // UART1, baud 115200, baud source Timer1, 1T mode, no interrupt - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200); // Set GPIO pin mode GPIO_P3_SetMode(GPIO_Pin_2|GPIO_Pin_3, GPIO_Mode_Output_PP); diff --git a/demo/pca/pca_pwm_8bit_2ch.c b/demo/pca/pca_pwm_8bit_2ch.c index 626866b..4801a47 100644 --- a/demo/pca/pca_pwm_8bit_2ch.c +++ b/demo/pca/pca_pwm_8bit_2ch.c @@ -21,7 +21,7 @@ void main(void) { SYS_SetClock(); // UART1, baud 115200, baud source Timer1, 1T mode, no interrupt - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200); // Set GPIO pin mode GPIO_P3_SetMode(GPIO_Pin_2|GPIO_Pin_3, GPIO_Mode_Output_PP); diff --git a/demo/pwm/pwm_2ch_complementary.c b/demo/pwm/pwm_2ch_complementary.c index afcedf6..701e78e 100644 --- a/demo/pwm/pwm_2ch_complementary.c +++ b/demo/pwm/pwm_2ch_complementary.c @@ -30,7 +30,7 @@ void main(void) SYS_SetClock(); // UART1, baud 115200, baud source Timer2, 1T mode - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); // Set GPIO pins output mode GPIO_P1_SetMode(GPIO_Pin_0|GPIO_Pin_1, GPIO_Mode_Output_PP); diff --git a/demo/pwm/pwm_2ch_timer2_interrupt.c b/demo/pwm/pwm_2ch_timer2_interrupt.c index b837679..c488935 100644 --- a/demo/pwm/pwm_2ch_timer2_interrupt.c +++ b/demo/pwm/pwm_2ch_timer2_interrupt.c @@ -46,7 +46,7 @@ void main(void) { SYS_SetClock(); // UART1, baud 115200, baud source Timer1, 1T mode - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200); // Set GPIO pins output mode GPIO_P1_SetMode(GPIO_Pin_0|GPIO_Pin_1, GPIO_Mode_Output_PP); diff --git a/demo/spi/nrf24l01/nrf24l01_stc8h1k.c b/demo/spi/nrf24l01/nrf24l01_stc8h1k.c index 2d3060c..0c7ff2c 100644 --- a/demo/spi/nrf24l01/nrf24l01_stc8h1k.c +++ b/demo/spi/nrf24l01/nrf24l01_stc8h1k.c @@ -87,7 +87,7 @@ void main(void) GPIO_Init(); // UART1, baud 115200, baud source Timer1, 1T mode, no interrupt - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200); UART1_TxString("UART Initialized\r\n"); SPI_Init(); diff --git a/demo/tim/timer0_print_cpuid.c b/demo/tim/timer0_print_cpuid.c index 408e5e1..00472ee 100644 --- a/demo/tim/timer0_print_cpuid.c +++ b/demo/tim/timer0_print_cpuid.c @@ -110,7 +110,7 @@ void main(void) // Set system clock. Remove this line if system clock is already set by STC-ISP SYS_SetClock(); // UART1 configuration: baud 115200 with Timer2, 1T mode, no interrupt - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); // Timer0 configuration: 16-bit auto-reload, interrupt enabled TIM_Timer0_Config(HAL_State_ON, TIM_TimerMode_16BitAuto, 1000); EXTI_Timer0_SetIntState(HAL_State_ON); diff --git a/demo/tim/timer0_timer_1t.c b/demo/tim/timer0_timer_1t.c index bf511a6..11defec 100644 --- a/demo/tim/timer0_timer_1t.c +++ b/demo/tim/timer0_timer_1t.c @@ -31,7 +31,7 @@ void main(void) // Set system clock. Remove this line if system clock is already set by STC-ISP SYS_SetClock(); // UART1 configuration: baud 115200 with Timer1, 1T mode, no interrupt - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200); TIM_Timer0_Config(HAL_State_ON, TIM_TimerMode_16BitAuto, 1000); EXTI_Timer0_SetIntState(HAL_State_ON); EXTI_Timer0_SetIntPriority(EXTI_IntPriority_High); diff --git a/demo/tim/timer2_timer_12t.c b/demo/tim/timer2_timer_12t.c index 6988245..7ae3ef5 100644 --- a/demo/tim/timer2_timer_12t.c +++ b/demo/tim/timer2_timer_12t.c @@ -23,7 +23,7 @@ void main(void) { SYS_SetClock(); // UART1 configuration: baud 115200 with Timer1, 1T mode, no interrupt - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200); // 12T mode, prescaler:255+1, frequency: 1, interrupt: ON TIM_Timer2_Config(HAL_State_OFF, 0xFF, 1); EXTI_Timer2_SetIntState(HAL_State_ON); diff --git a/demo/tim/timer2_timer_1t.c b/demo/tim/timer2_timer_1t.c index e0b3287..bfb0897 100644 --- a/demo/tim/timer2_timer_1t.c +++ b/demo/tim/timer2_timer_1t.c @@ -30,7 +30,7 @@ void main(void) { SYS_SetClock(); // UART1 configuration: baud 115200 with Timer1, 1T mode, no interrupt - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200); // 1T mode, prescaler:255+1, frequency: 5, interrupt: ON TIM_Timer2_Config(HAL_State_ON, 0xFF, 5); EXTI_Timer2_SetIntState(HAL_State_ON); diff --git a/demo/uart/uart1_timer1_tx.c b/demo/uart/uart1_timer1_tx.c index f9971d2..5d35ac3 100644 --- a/demo/uart/uart1_timer1_tx.c +++ b/demo/uart/uart1_timer1_tx.c @@ -18,7 +18,7 @@ void main(void) { SYS_SetClock(); // UART1, baud 115200, baud source Timer1, 1T mode, no interrupt - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200); while(1) { UART1_TxChar('T'); diff --git a/demo/uart/uart1_timer2_interrupt_tx_rx.c b/demo/uart/uart1_timer2_interrupt_tx_rx.c index 52f94b1..04acf82 100644 --- a/demo/uart/uart1_timer2_interrupt_tx_rx.c +++ b/demo/uart/uart1_timer2_interrupt_tx_rx.c @@ -25,7 +25,7 @@ void main(void) { SYS_SetClock(); // UART1, baud 115200, baud source Timer2, 1T mode, interrupt on - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); UART1_SetRxState(HAL_State_ON); // Enable UART1 interrupt EXTI_Global_SetIntState(HAL_State_ON); diff --git a/demo/uart/uart1_timer2_tx.c b/demo/uart/uart1_timer2_tx.c index f93f4e3..7146ba0 100644 --- a/demo/uart/uart1_timer2_tx.c +++ b/demo/uart/uart1_timer2_tx.c @@ -18,7 +18,7 @@ void main(void) { SYS_SetClock(); // UART1, baud 115200, baud source Timer2, 1T mode, no interrupt - UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); + UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); while(1) { UART1_TxChar('T'); diff --git a/include/fw_exti.h b/include/fw_exti.h index a970a7d..f482dea 100644 --- a/include/fw_exti.h +++ b/include/fw_exti.h @@ -131,11 +131,20 @@ typedef enum #define EXTI_INT_CompFall_ON SFR_SET(CMPCR1, 4) #define EXTI_INT_CompFall_OFF SFR_RESET(CMPCR1, 4) -#define EXTI_I2C_SetMstIntState(__STATE__) SFR_ASSIGN(I2CMSCR, 7, __STATE__) -#define EXTI_I2C_SetSlvStartIntState(__STATE__) SFRX_SET(I2CSLCR, 6, __STATE__) -#define EXTI_I2C_SetSlvRecvIntState(__STATE__) SFRX_SET(I2CSLCR, 5, __STATE__) -#define EXTI_I2C_SetSlvSendIntState(__STATE__) SFRX_SET(I2CSLCR, 4, __STATE__) -#define EXTI_I2C_SetSlvStopIntState(__STATE__) SFRX_SET(I2CSLCR, 3, __STATE__) +#define EXTI_I2C_SetMstIntState(__STATE__) SFRX_ASSIGN(I2CMSCR, 7, __STATE__) +#define EXTI_I2C_SetSlvStartIntState(__STATE__) SFRX_ASSIGN(I2CSLCR, 6, __STATE__) +#define EXTI_I2C_SetSlvRecvIntState(__STATE__) SFRX_ASSIGN(I2CSLCR, 5, __STATE__) +#define EXTI_I2C_SetSlvSendIntState(__STATE__) SFRX_ASSIGN(I2CSLCR, 4, __STATE__) +#define EXTI_I2C_SetSlvStopIntState(__STATE__) SFRX_ASSIGN(I2CSLCR, 3, __STATE__) + +#define EXTI_RTC_SetAlarmIntState(__STATE__) SFRX_ASSIGN(RTCIEN, 7, __STATE__) +#define EXTI_RTC_SetDayIntState(__STATE__) SFRX_ASSIGN(RTCIEN, 6, __STATE__) +#define EXTI_RTC_SetHourIntState(__STATE__) SFRX_ASSIGN(RTCIEN, 5, __STATE__) +#define EXTI_RTC_SetMinuteIntState(__STATE__) SFRX_ASSIGN(RTCIEN, 4, __STATE__) +#define EXTI_RTC_SetSecondIntState(__STATE__) SFRX_ASSIGN(RTCIEN, 3, __STATE__) +#define EXTI_RTC_SetSecondDiv2IntState(__STATE__) SFRX_ASSIGN(RTCIEN, 2, __STATE__) +#define EXTI_RTC_SetSecondDiv8IntState(__STATE__) SFRX_ASSIGN(RTCIEN, 1, __STATE__) +#define EXTI_RTC_SetSecondDiv32IntState(__STATE__) SFRX_ASSIGN(RTCIEN, 0, __STATE__) #define EXTI_INT_PWMA_Break_ON SFRX_SET(PWMA_IER, 7) #define EXTI_INT_PWMA_Break_OFF SFRX_RESET(PWMA_IER, 7) diff --git a/include/fw_sys.h b/include/fw_sys.h index 9b749e3..0aa40fa 100644 --- a/include/fw_sys.h +++ b/include/fw_sys.h @@ -18,6 +18,18 @@ #include "fw_conf.h" #include "fw_types.h" +typedef enum +{ + SYS_ExternalOSC_Clock = 0x00, + SYS_ExternalOSC_Crystal = 0x01, +} SYS_ExternalOSC_t; + +typedef enum +{ + SYS_SysClkOutputPort_P54 = 0x00, + SYS_SysClkOutputPort_P16 = 0x01, +} SYS_SysClkOutputPort_t; + /** * STC8H Clock: * MCKSEL ||===> MCLKODIV ==> MCLKO_S => P1.6/P5.4 @@ -34,6 +46,103 @@ LIRTRIM = ((__LIRTRIM__) & 0x03); \ } while(0) +/** + * Enable high speed internal oscillator +*/ +#define SYS_EnableOscillatorHSI() do { \ + SFRX_ON(); \ + (HIRCCR) = (HIRCCR) | (0x01 << 7); \ + while (!(HIRCCR & 0x01)); \ + SFRX_OFF(); \ + } while(0) +/** + * Disable high speed internal oscillator +*/ +#define SYS_DisableOscillatorHSI() do { \ + SFRX_ON(); \ + (HIRCCR) = (HIRCCR) & ~(0x01 << 7); \ + SFRX_OFF(); \ + } while(0) + +/** + * Enable low speed internal oscillator +*/ +#define SYS_EnableOscillatorLSI() do { \ + SFRX_ON(); \ + (IRC32KCR) = (IRC32KCR) | (0x01 << 7); \ + while (!(IRC32KCR & 0x01)); \ + SFRX_OFF(); \ + } while(0) +/** + * Disable low speed internal oscillator +*/ +#define SYS_DisableOscillatorLSI() do { \ + SFRX_ON(); \ + (IRC32KCR) = (IRC32KCR) & ~(0x01 << 7); \ + SFRX_OFF(); \ + } while(0) + +/** + * Enable high speed external oscillator +*/ +#define SYS_EnableOscillatorHSE() do { \ + SFRX_ON(); \ + (XOSCCR) = (XOSCCR) | (0x01 << 7); \ + while (!(HIRCCR & 0x01)); \ + SFRX_OFF(); \ + } while(0) +/** + * Disable high speed external oscillator +*/ +#define SYS_DisableOscillatorHSE() do { \ + SFRX_ON(); \ + (XOSCCR) = (XOSCCR) & ~(0x01 << 7); \ + SFRX_OFF(); \ + } while(0) +/** + * Set high speed external oscillator type +*/ +#define SYS_SetExternalOscType(__TYPE__) SFRX_ASSIGN(XOSCCR, 6, (__TYPE__)) + +/** + * Enable low speed external oscillator +*/ +#define SYS_EnableOscillatorLSE() do { \ + SFRX_ON(); \ + (X32KCR) = (X32KCR) | (0x01 << 7); \ + while (!(X32KCR & 0x01)); \ + SFRX_OFF(); \ + } while(0) +/** + * Disable low speed external oscillator +*/ +#define SYS_DisableOscillatorLSE() do { \ + SFRX_ON(); \ + (X32KCR) = (X32KCR) & ~(0x01 << 7); \ + SFRX_OFF(); \ + } while(0) +/** + * Set low speed external oscillator gain +*/ +#define SYS_SetExternal32kHighGain(__STATE__) SFRX_ASSIGN(X32KCR, 6, (__STATE__)) + +/** + * System clock output + * 0:No output, Foutput = SYSCLK / __DIV__ +*/ +#define SYS_SetSysClockOutputDivider(__DIV__) do { \ + SFRX_ON(); \ + (MCLKOCR) = (MCLKOCR) & ~(0x7F) | (__DIV__); \ + SFRX_OFF(); \ + } while(0) + +/** + * System clock output pin + * 0:P5.4, 1:P1.6 +*/ +#define SYS_SetClockOutputPin(__PORT__) SFRX_ASSIGN(MCLKOCR, 7, (__STATE__)) + + void SYS_SetClock(void); void SYS_Delay(uint16_t t); void SYS_DelayUs(uint16_t t); diff --git a/include/fw_uart.h b/include/fw_uart.h index 0a107fb..81b3caf 100644 --- a/include/fw_uart.h +++ b/include/fw_uart.h @@ -73,11 +73,11 @@ typedef enum /** * Mode1: 8-bit UART, dynamic baud-rate, provided by Timer1 or Timer2 */ -void UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_t baudSource, HAL_State_t _1TMode, uint32_t baudrate); +void UART1_Config8bitUart(UART1_BaudSource_t baudSource, HAL_State_t _1TMode, uint32_t baudrate); /** * Mode3: 9-bit UART, dynamic baud-rate, provided by Timer1 or Timer2 */ -void UART1_ConfigMode3Dyn9bitUart(UART1_BaudSource_t baudSource, HAL_State_t _1TMode, uint32_t baudrate); +void UART1_Config9bitUart(UART1_BaudSource_t baudSource, HAL_State_t _1TMode, uint32_t baudrate); void UART1_InterruptHandler(void); diff --git a/src/fw_uart.c b/src/fw_uart.c index 1519b51..360ecd9 100644 --- a/src/fw_uart.c +++ b/src/fw_uart.c @@ -56,7 +56,7 @@ void _UART1_ConfigDynUart(UART1_BaudSource_t baudSource, HAL_State_t _1TMode, in TIM_Timer2_SetRunState(HAL_State_ON); } } -void UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_t baudSource, HAL_State_t _1TMode, uint32_t baudrate) +void UART1_Config8bitUart(UART1_BaudSource_t baudSource, HAL_State_t _1TMode, uint32_t baudrate) { uint16_t init; uint32_t sysclk; @@ -66,7 +66,7 @@ void UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_t baudSource, HAL_State_t _1T _UART1_ConfigDynUart(baudSource, _1TMode, init); } -void UART1_ConfigMode3Dyn9bitUart(UART1_BaudSource_t baudSource, HAL_State_t _1TMode, uint32_t baudrate) +void UART1_Config9bitUart(UART1_BaudSource_t baudSource, HAL_State_t _1TMode, uint32_t baudrate) { uint16_t init; uint32_t sysclk;