From 9e8bd90cbd47df0645f86030156d63db8507ff9a Mon Sep 17 00:00:00 2001 From: IOsetting Date: Thu, 30 Dec 2021 22:48:21 +0800 Subject: [PATCH] fix:uart output issue caused by clkdiv; feat: spi --- demo/adc/adc_interrupt_10bit.c | 3 +- demo/adc/adc_interrupt_2ch.c | 2 +- demo/adc/adc_poll_10bit.c | 4 +- demo/adc/adc_poll_8bit.c | 4 +- demo/tim/timer0_print_cpuid.c | 16 ++++---- demo/tim/timer0_timer_1t.c | 13 +++--- demo/tim/timer2_timer_12t.c | 9 ++--- demo/tim/timer2_timer_1t.c | 6 ++- demo/uart/uart1_interrupt_tx_rx.c | 3 +- demo/uart/uart1_tx.c | 3 +- demo/uart/uart2_tx.c | 3 +- include/fw_adc.h | 2 +- include/fw_conf.h | 4 ++ include/fw_hal.h | 1 + include/fw_spi.h | 67 +++++++++++++++++++++++++++++++ include/fw_sys.h | 20 ++++----- include/fw_tim.h | 17 ++------ include/fw_uart.h | 12 ++++++ include/fw_util.h | 2 +- src/fw_sys.c | 34 ++++++++-------- src/fw_tim.c | 38 +++++------------- src/fw_uart.c | 2 +- src/fw_util.c | 17 ++++---- 23 files changed, 171 insertions(+), 111 deletions(-) create mode 100644 include/fw_spi.h diff --git a/demo/adc/adc_interrupt_10bit.c b/demo/adc/adc_interrupt_10bit.c index 56e0214..8d844a6 100644 --- a/demo/adc/adc_interrupt_10bit.c +++ b/demo/adc/adc_interrupt_10bit.c @@ -42,8 +42,7 @@ INTERRUPT(ADC_Routine, EXTI_VectADC) void main(void) { - - SYS_Init(); + SYS_SetClock(); // For debug print UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200, HAL_State_OFF); // Set ADC1(GPIO P1.1) HIP diff --git a/demo/adc/adc_interrupt_2ch.c b/demo/adc/adc_interrupt_2ch.c index 72d6dfb..f70f2ab 100644 --- a/demo/adc/adc_interrupt_2ch.c +++ b/demo/adc/adc_interrupt_2ch.c @@ -62,7 +62,7 @@ INTERRUPT(ADC_Routine, EXTI_VectADC) void main(void) { - SYS_Init(); + SYS_SetClock(); // For debug print UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200, HAL_State_OFF); // Channel: ADC1 diff --git a/demo/adc/adc_poll_10bit.c b/demo/adc/adc_poll_10bit.c index ce1af4f..904d77e 100644 --- a/demo/adc/adc_poll_10bit.c +++ b/demo/adc/adc_poll_10bit.c @@ -32,7 +32,7 @@ void main(void) { uint16_t res; - SYS_Init(); + SYS_SetClock(); // For debug print UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200, HAL_State_OFF); // Set ADC1(GPIO P1.1) HIP @@ -51,7 +51,7 @@ void main(void) ADC_Start(); NOP(); NOP(); - while (!ADC_SamplingUnfinished()); + while (!ADC_SamplingFinished()); ADC_ClearInterrupt(); /* res = ADC_RESL; diff --git a/demo/adc/adc_poll_8bit.c b/demo/adc/adc_poll_8bit.c index 13669b9..09d1c9c 100644 --- a/demo/adc/adc_poll_8bit.c +++ b/demo/adc/adc_poll_8bit.c @@ -32,7 +32,7 @@ void main(void) { uint8_t res; - SYS_Init(); + SYS_SetClock(); // For debug print UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200, HAL_State_OFF); // Set ADC1(GPIO P1.1) HIP @@ -51,7 +51,7 @@ void main(void) ADC_Start(); NOP(); NOP(); - while (!ADC_SamplingUnfinished()); + while (!ADC_SamplingFinished()); ADC_ClearInterrupt(); res = ADC_RES; diff --git a/demo/tim/timer0_print_cpuid.c b/demo/tim/timer0_print_cpuid.c index 17b1ddf..91282e5 100644 --- a/demo/tim/timer0_print_cpuid.c +++ b/demo/tim/timer0_print_cpuid.c @@ -93,18 +93,16 @@ INTERRUPT(Timer0_Routine, EXTI_VectTimer0) void main(void) { - SYS_Init(); + // 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, HAL_State_OFF); - // Timer0 configuration: 1T mode, 16-bit auto-reload, frequency 1000, interrupt enabled - TIM_Timer0_Config( - HAL_State_ON, - TIM_TimerMode_16BitAuto, - 1000, - HAL_State_ON, - EXTI_IntPriority_High); - TIM_Timer0_SetRunState(HAL_State_ON); + // Timer0 configuration: 16-bit auto-reload, interrupt enabled + TIM_Timer0_Config(HAL_State_ON, TIM_TimerMode_16BitAuto, 1000); + EXTI_Timer0_SetIntState(HAL_State_ON); + EXTI_Timer0_SetIntPriority(EXTI_IntPriority_High); EXTI_Global_SetIntState(HAL_State_ON); + TIM_Timer0_SetRunState(HAL_State_ON); while(1); } \ No newline at end of file diff --git a/demo/tim/timer0_timer_1t.c b/demo/tim/timer0_timer_1t.c index 4b9af59..e4bbd00 100644 --- a/demo/tim/timer0_timer_1t.c +++ b/demo/tim/timer0_timer_1t.c @@ -14,17 +14,14 @@ INTERRUPT(Timer0_Routine, EXTI_VectTimer0) void main(void) { - SYS_Init(); + // 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, HAL_State_OFF); + TIM_Timer0_Config(HAL_State_ON, TIM_TimerMode_16BitAuto, 1000); + EXTI_Timer0_SetIntState(HAL_State_ON); + EXTI_Timer0_SetIntPriority(EXTI_IntPriority_High); EXTI_Global_SetIntState(HAL_State_ON); - TIM_Timer0_Config( - HAL_State_ON, - TIM_TimerMode_16BitAuto, - 1000, - HAL_State_ON, - EXTI_IntPriority_High); TIM_Timer0_SetRunState(HAL_State_ON); - while(1); } \ No newline at end of file diff --git a/demo/tim/timer2_timer_12t.c b/demo/tim/timer2_timer_12t.c index a4a78d2..7906116 100644 --- a/demo/tim/timer2_timer_12t.c +++ b/demo/tim/timer2_timer_12t.c @@ -1,7 +1,5 @@ #include "fw_hal.h" -static uint8_t counter = 0; - INTERRUPT(Timer2_Routine, EXTI_VectTimer2) { UART1_TxString("hello\r\n"); @@ -9,12 +7,13 @@ INTERRUPT(Timer2_Routine, EXTI_VectTimer2) void main(void) { - SYS_Init(); + SYS_SetClock(); // UART1 configuration: baud 115200 with Timer1, 1T mode, no interrupt UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200, HAL_State_OFF); - EXTI_Global_SetIntState(HAL_State_ON); // 12T mode, prescaler:255+1, frequency: 1, interrupt: ON - TIM_Timer2_Config(HAL_State_OFF, 0xFF, 1, HAL_State_ON); + TIM_Timer2_Config(HAL_State_OFF, 0xFF, 1); + EXTI_Timer2_SetIntState(HAL_State_ON); + EXTI_Global_SetIntState(HAL_State_ON); TIM_Timer2_SetRunState(HAL_State_ON); while(1); diff --git a/demo/tim/timer2_timer_1t.c b/demo/tim/timer2_timer_1t.c index 42321b5..0d28a7c 100644 --- a/demo/tim/timer2_timer_1t.c +++ b/demo/tim/timer2_timer_1t.c @@ -14,12 +14,14 @@ INTERRUPT(Timer2_Routine, EXTI_VectTimer2) void main(void) { - SYS_Init(); + SYS_SetClock(); // UART1 configuration: baud 115200 with Timer1, 1T mode, no interrupt UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200, HAL_State_OFF); EXTI_Global_SetIntState(HAL_State_ON); // 1T mode, prescaler:255+1, frequency: 5, interrupt: ON - TIM_Timer2_Config(HAL_State_ON, 0xFF, 5, HAL_State_ON); + TIM_Timer2_Config(HAL_State_ON, 0xFF, 5); + EXTI_Timer2_SetIntState(HAL_State_ON); + EXTI_Global_SetIntState(HAL_State_ON); TIM_Timer2_SetRunState(HAL_State_ON); while(1); diff --git a/demo/uart/uart1_interrupt_tx_rx.c b/demo/uart/uart1_interrupt_tx_rx.c index 0e8c5a7..cb09086 100644 --- a/demo/uart/uart1_interrupt_tx_rx.c +++ b/demo/uart/uart1_interrupt_tx_rx.c @@ -9,7 +9,8 @@ INTERRUPT(UART1_Routine, EXTI_VectUART1) void main(void) { - SYS_Init(); + SYS_SetClock(); + // UART1, baud 115200, baud source Timer2, 1T mode, interrupt on UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200, HAL_State_ON); UART1_SetRxState(HAL_State_ON); while(1) diff --git a/demo/uart/uart1_tx.c b/demo/uart/uart1_tx.c index 8cc853c..5f31ee7 100644 --- a/demo/uart/uart1_tx.c +++ b/demo/uart/uart1_tx.c @@ -2,7 +2,8 @@ void main(void) { - SYS_Init(); + SYS_SetClock(); + // UART1, baud 115200, baud source Timer2, 1T mode, no interrupt UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200, HAL_State_OFF); while(1) { diff --git a/demo/uart/uart2_tx.c b/demo/uart/uart2_tx.c index b917f6e..bb3b8f8 100644 --- a/demo/uart/uart2_tx.c +++ b/demo/uart/uart2_tx.c @@ -2,7 +2,8 @@ void main(void) { - SYS_Init(); + SYS_SetClock(); + // UART2, baud 115200, baud source Timer2, 1T mode, no interrupt UART2_ConfigMode0Dyn8bitUart(HAL_State_ON, 115200, HAL_State_OFF); while(1) { diff --git a/include/fw_adc.h b/include/fw_adc.h index 704e2f0..5857dcc 100644 --- a/include/fw_adc.h +++ b/include/fw_adc.h @@ -20,7 +20,7 @@ #define ADC_SetPowerState(__STATE__) SFR_ASSIGN(ADC_CONTR, 7, __STATE__) #define ADC_Start() SFR_SET(ADC_CONTR, 6) -#define ADC_SamplingUnfinished() (ADC_CONTR & (0x01 << 5)) +#define ADC_SamplingFinished() (ADC_CONTR & (0x01 << 5)) #define ADC_ClearInterrupt() SFR_RESET(ADC_CONTR, 5) #define ADC_SetPWMTriggerState(__STATE__) SFR_ASSIGN(ADC_CONTR, 4, __STATE__) /** diff --git a/include/fw_conf.h b/include/fw_conf.h index d989b69..ebc67a8 100644 --- a/include/fw_conf.h +++ b/include/fw_conf.h @@ -54,6 +54,10 @@ #define __CONF_FOSC 24000000UL #endif +#ifndef __CONF_CLKDIV + #define __CONF_CLKDIV 0x00 +#endif + #ifndef __CONF_IRCBAND #define __CONF_IRCBAND 0x00 #endif diff --git a/include/fw_hal.h b/include/fw_hal.h index 00e4249..935fe48 100644 --- a/include/fw_hal.h +++ b/include/fw_hal.h @@ -24,6 +24,7 @@ #include "fw_tim.h" #include "fw_uart.h" #include "fw_adc.h" +#include "fw_spi.h" #include "fw_util.h" #endif diff --git a/include/fw_spi.h b/include/fw_spi.h new file mode 100644 index 0000000..78b9b7e --- /dev/null +++ b/include/fw_spi.h @@ -0,0 +1,67 @@ +// Copyright 2021 IOsetting +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef ___FW_SPI_H___ +#define ___FW_SPI_H___ + +#include "fw_conf.h" +#include "fw_types.h" + +/** + * STC8H1K08(TSSOP20) STC8H3K32S2(TSSOP20) + * #1 #4 #1 #3 #4 + * SPI SS -> 1 16 19 7 16(P35) + * SPI MOSI -> 2 15 15(P34) + * SPI MISO -> 3 14 14(P33) + * SPI CLK -> 4 13 13(P32) +*/ +typedef enum +{ + // SS MOSI MISO SCLK + SPI_AlterPort_P12P54_P13_P14_P15 = 0x00, + SPI_AlterPort_P22_P23_P24_P25 = 0x01, + SPI_AlterPort_P54_P40_P41_P43 = 0x10, + SPI_AlterPort_P35_P34_P33_P32 = 0x11, +} SPI_AlterPort_t; + +typedef enum +{ + SPI_ClockPreScaler_4 = 0x00, + SPI_ClockPreScaler_8 = 0x01, + SPI_ClockPreScaler_16 = 0x02, + SPI_ClockPreScaler_32or2 = 0x03, +} SPI_ClockPreScaler_t; + +#define SPI_RxTxFinished() (SPSTAT & (0x01 << 7)) +#define SPI_ClearInterrupt() SFR_RESET(SPSTAT, 7) +#define SPI_ClearWriteConflictInterrupt() SFR_RESET(SPSTAT, 6) + +#define SPI_IgnoreSlaveSelect(__STATE__) SFR_ASSIGN(SPCTL, 7, __STATE__) +#define SPI_SetEnableState(__STATE__) SFR_ASSIGN(SPCTL, 6, __STATE__) +#define SPI_SetDataOrderLSB(__STATE__) SFR_ASSIGN(SPCTL, 5, __STATE__) +#define SPI_SetMasterMode(__STATE__) SFR_ASSIGN(SPCTL, 4, __STATE__) +// CPOL, 0:idle low, 1:idl high +#define SPI_SetClockPolarHigh(__STATE__) SFR_ASSIGN(SPCTL, 3, __STATE__) +// CPHA, 0:SS low drive, 1:SCLK front edge drive +#define SPI_SetClockFrontEdgeDrive(__STATE__) SFR_ASSIGN(SPCTL, 2, __STATE__) +/** + * SPI Clock +*/ +#define SPI_SetClockPrescaler(__PRE_SCALER__) (SPCTL = SPCTL & ~0x03 | ((__PRE_SCALER__) << 0)) +/** + * Alternative port selection +*/ +#define SPI_SwitchPort(__ALTER_PORT__) (P_SW1 = P_SW1 & ~(0x03 << 2) | ((__ALTER_PORT__) << 2)) + +#endif diff --git a/include/fw_sys.h b/include/fw_sys.h index 552a24d..d9421ee 100644 --- a/include/fw_sys.h +++ b/include/fw_sys.h @@ -18,13 +18,6 @@ #include "fw_conf.h" #include "fw_types.h" -#define SYS_SetFOSC(__IRCBAND__, __VRTRIM__, __IRTRIM__, __LIRTRIM__) do { \ - IRCBAND = ((__IRCBAND__) & 0x03); \ - VRTRIM = (__VRTRIM__); \ - IRTRIM = (__IRTRIM__); \ - LIRTRIM = ((__LIRTRIM__) & 0x03); \ - } while(0) - /** * STC8H Clock: * MCKSEL ||===> MCLKODIV ==> MCLKO_S => P1.6/P5.4 @@ -33,10 +26,17 @@ * 10 External 32KHz | * 11 Internal 32KHz | */ -void SYS_Init(void); + +#define SYS_SetFOSC(__IRCBAND__, __VRTRIM__, __IRTRIM__, __LIRTRIM__) do { \ + IRCBAND = ((__IRCBAND__) & 0x03); \ + VRTRIM = (__VRTRIM__); \ + IRTRIM = (__IRTRIM__); \ + LIRTRIM = ((__LIRTRIM__) & 0x03); \ + } while(0) + +void SYS_SetClock(void); void SYS_Delay(uint16_t t); void SYS_DelayUs(uint16_t t); -void SYS_SetSysClkDiv(uint8_t div); -uint32_t SYS_GetSysClk(void); +uint32_t SYS_GetSysClock(void); #endif diff --git a/include/fw_tim.h b/include/fw_tim.h index 900d700..d623b02 100644 --- a/include/fw_tim.h +++ b/include/fw_tim.h @@ -27,6 +27,7 @@ typedef enum TIM_TimerMode_16BitAutoNoInt = 0x03 // Uninterruptable 16-bit auto-reload, Timer0 only } TIM_TimerMode_t; +int16_t TIM_Timer0n1_CalculateInitValue(uint16_t frequency, HAL_State_t freq1t, uint16_t limit); /***************************** / * Timer 0 @@ -42,12 +43,7 @@ typedef enum #define TIM_Timer0_SetMode(__TIM_TIMER_MODE__) (TMOD = TMOD & ~(0x03 << 0) | ((__TIM_TIMER_MODE__) << 0)) #define TIM_Timer0_SetInitValue(__TH__, __TL__) do{ TH0 = (__TH__); TL0 = (__TL__); }while(0) -void TIM_Timer0_Config( - HAL_State_t freq1t, - TIM_TimerMode_t mode, - uint16_t frequency, - HAL_State_t intState, - EXTI_IntPriority_t intPriority); +void TIM_Timer0_Config(HAL_State_t freq1t, TIM_TimerMode_t mode, uint16_t frequency); /***************************** / @@ -64,12 +60,7 @@ void TIM_Timer0_Config( #define TIM_Timer1_SetMode(__TIM_TIMER_MODE__) (TMOD = TMOD & ~(0x03 << 4) | ((__TIM_TIMER_MODE__) << 4)) #define TIM_Timer1_SetInitValue(__TH__, __TL__) do{ TH1 = (__TH__); TL1 = (__TL__); }while(0) -void TIM_Timer1_Config( - HAL_State_t freq1t, - TIM_TimerMode_t mode, - uint16_t frequency, - HAL_State_t intState, - EXTI_IntPriority_t intPriority); +void TIM_Timer1_Config(HAL_State_t freq1t, TIM_TimerMode_t mode, uint16_t frequency); /***************************** / @@ -92,7 +83,7 @@ void TIM_Timer1_Config( // Timer2 Prescaler: [0, 255] #define TIM_Timer2_SetPreScaler(__PRE__) do{P_SW2 = 0x80; TM2PS = (__PRE__); P_SW2 = 0x00;}while(0) -void TIM_Timer2_Config(HAL_State_t freq1t, uint8_t prescaler, uint16_t frequency, HAL_State_t intState); +void TIM_Timer2_Config(HAL_State_t freq1t, uint8_t prescaler, uint16_t frequency); /***************************** / diff --git a/include/fw_uart.h b/include/fw_uart.h index 8f3a982..d1dd4f8 100644 --- a/include/fw_uart.h +++ b/include/fw_uart.h @@ -30,6 +30,14 @@ typedef enum UART1_BaudSource_Timer2 = 0x01, } UART1_BaudSource_t; +typedef enum +{ + UART1_AlterPort_P30_P31 = 0x00, + UART1_AlterPort_P36_P37 = 0x01, + UART1_AlterPort_P16_P17 = 0x10, + UART1_AlterPort_P43_P44 = 0x11, +} UART1_AlterPort_t; + #define UART1_SetRxState(__STATE__) SBIT_ASSIGN(REN, __STATE__) #define UART1_ClearTxInterrupt SBIT_RESET(TI) #define UART1_ClearRxInterrupt SBIT_RESET(RI) @@ -48,6 +56,10 @@ typedef enum */ #define UART1_ConfigMode2Fixed9bitUart(__STATE__) do{ SM0=1; SM1=0; SFR_ASSIGN(PCON, 7, __STATE__);}while(0) #define UART1_SetTimer1Mode2Baudx2(__STATE__) SFR_ASSIGN(PCON, 7, __STATE__) +/** + * Alternative port selection: P30/P31, P36/P37, P16/P17, P43/P44 +*/ +#define UART1_SwitchPort(__ALTER_PORT__) (P_SW1 = P_SW1 & ~(0x03 << 6) | ((__ALTER_PORT__) << 6)) /** * Mode1: 8-bit UART, dynamic baud-rate, provided by Timer1 or Timer2 */ diff --git a/include/fw_util.h b/include/fw_util.h index 7f28423..598dea8 100644 --- a/include/fw_util.h +++ b/include/fw_util.h @@ -25,6 +25,6 @@ void UTIL_Uart1_33M1776_115200_Init(void); void UTIL_Uart1_35M_9600_Init(void); void UTIL_Uart1_36M864_9600_Init(void); void UTIL_Uart1_36M864_115200_Init(void); -void UTIL_ItrimScan(uint8_t ircband, uint8_t *str); +// void UTIL_ItrimScan(uint8_t ircband, uint8_t *str); #endif diff --git a/src/fw_sys.c b/src/fw_sys.c index ee52a6f..d2c9b0f 100644 --- a/src/fw_sys.c +++ b/src/fw_sys.c @@ -18,16 +18,26 @@ static const uint16_t ticks_ms = (__CONF_FOSC / (float)1000 / 13 - 46); static const uint8_t ticks_us = (__CONF_FOSC / (float)12100000UL); static uint8_t clkdiv = 0x1; -void SYS_Init(void) +/** + * Change system clock + * - invoke this in the beginning of code + * - don't invoke this if the target frequency is already set by STC-ISP +*/ +void SYS_SetClock(void) { - if (__CONF_IRCBAND != 0x00 || __CONF_VRTRIM != 0x00 || __CONF_IRTRIM != 0x00) + uint16_t i = 0; uint8_t j = 5; + P_SW2 = 0x80; + if (CLKDIV != (__CONF_CLKDIV)) { - SYS_SetSysClkDiv(0); - SYS_SetFOSC(__CONF_IRCBAND, __CONF_VRTRIM, __CONF_IRTRIM, __CONF_LIRTRIM); - // Wait a while till sysclk stable, or it may block the main process - uint16_t i = ticks_ms; - while (--i); + CLKDIV = (__CONF_CLKDIV); + do { // Wait a while after clock changed, or it may block the main process + while (--i); + } while (--j); } + P_SW2 = 0x00; + clkdiv = (__CONF_CLKDIV == 0)? 1 : __CONF_CLKDIV; + SYS_SetFOSC(__CONF_IRCBAND, __CONF_VRTRIM, __CONF_IRTRIM, __CONF_LIRTRIM); + while (--i); // Wait } void SYS_Delay(uint16_t t) @@ -50,15 +60,7 @@ void SYS_DelayUs(uint16_t t) } while (--t); } -void SYS_SetSysClkDiv(uint8_t div) -{ - P_SW2 = 0x80; - CLKDIV = div; - P_SW2 = 0x00; - clkdiv = (div == 0)? 1 : div; -} - -uint32_t SYS_GetSysClk(void) +uint32_t SYS_GetSysClock(void) { return ((uint32_t)__CONF_FOSC) / clkdiv; } diff --git a/src/fw_tim.c b/src/fw_tim.c index 44a86ba..e8a5625 100644 --- a/src/fw_tim.c +++ b/src/fw_tim.c @@ -17,15 +17,15 @@ #include "fw_util.h" /** - * Calculate the initial value of timer counter + * Calculate the initial value of Timer0 & Timer1 counter * - If the frequency is too high, it will return the value of `limit`, so the timer * will run in the highest frequency * - If the frequency is too low, it will return 0, so the timer will run in the * lowest possible frequency */ -int16_t _TIM_InitValueCalculate(uint16_t frequency, HAL_State_t freq1t, uint16_t limit) +int16_t TIM_Timer0n1_CalculateInitValue(uint16_t frequency, HAL_State_t freq1t, uint16_t limit) { - uint32_t value = SYS_GetSysClk(); + uint32_t value = SYS_GetSysClock(); if (!freq1t) value = value / 12; value = value / frequency; @@ -35,58 +35,44 @@ int16_t _TIM_InitValueCalculate(uint16_t frequency, HAL_State_t freq1t, uint16_t return limit - value; } -void TIM_Timer0_Config( - HAL_State_t freq1t, - TIM_TimerMode_t mode, - uint16_t frequency, - HAL_State_t intState, - EXTI_IntPriority_t intPriority) +void TIM_Timer0_Config(HAL_State_t freq1t, TIM_TimerMode_t mode, uint16_t frequency) { uint16_t init; TIM_Timer0_Set1TMode(freq1t); TIM_Timer0_SetMode(mode); if (mode == TIM_TimerMode_8BitAuto) { - init = _TIM_InitValueCalculate(frequency, freq1t, 0xFF); + init = TIM_Timer0n1_CalculateInitValue(frequency, freq1t, 0xFF); TIM_Timer0_SetInitValue(init & 0xFF, init & 0xFF); } else { - init = _TIM_InitValueCalculate(frequency, freq1t, 0xFFFF); + init = TIM_Timer0n1_CalculateInitValue(frequency, freq1t, 0xFFFF); TIM_Timer0_SetInitValue(init >> 8, init & 0xFF); } - EXTI_Timer0_SetIntState(intState); - EXTI_Timer0_SetIntPriority(intPriority); } -void TIM_Timer1_Config( - HAL_State_t freq1t, - TIM_TimerMode_t mode, - uint16_t frequency, - HAL_State_t intState, - EXTI_IntPriority_t intPriority) +void TIM_Timer1_Config(HAL_State_t freq1t, TIM_TimerMode_t mode, uint16_t frequency) { uint16_t init; TIM_Timer1_Set1TMode(freq1t); TIM_Timer1_SetMode(mode); if (mode == TIM_TimerMode_8BitAuto) { - init = _TIM_InitValueCalculate(frequency, freq1t, 0xFF); + init = TIM_Timer0n1_CalculateInitValue(frequency, freq1t, 0xFF); TIM_Timer1_SetInitValue(init & 0xFF, init & 0xFF); } else { - init = _TIM_InitValueCalculate(frequency, freq1t, 0xFFFF); + init = TIM_Timer0n1_CalculateInitValue(frequency, freq1t, 0xFF); TIM_Timer1_SetInitValue(init >> 8, init & 0xFF); } - EXTI_Timer1_SetIntState(intState); - EXTI_Timer1_SetIntPriority(intPriority); } int16_t _TIM_Timer234_InitValueCalculate( uint16_t frequency, uint8_t prescaler, HAL_State_t freq1t) { - uint32_t value = SYS_GetSysClk(); + uint32_t value = SYS_GetSysClock(); if (!freq1t) value = value / 12; value = value / ((prescaler + 1) * frequency); @@ -104,14 +90,12 @@ int16_t _TIM_Timer234_InitValueCalculate( * 2. frequency = SYSCLK / (TMxPS + 1) / (0xFFFF - TxH,TxL) / (1T? 1 : 12) */ -void TIM_Timer2_Config( - HAL_State_t freq1t, uint8_t prescaler, uint16_t frequency, HAL_State_t intState) +void TIM_Timer2_Config(HAL_State_t freq1t, uint8_t prescaler, uint16_t frequency) { uint16_t init = _TIM_Timer234_InitValueCalculate(frequency, prescaler, freq1t); TIM_Timer2_Set1TMode(freq1t); TIM_Timer2_SetPreScaler(prescaler); TIM_Timer2_SetInitValue(init >> 8, init & 0xFF); - EXTI_Timer2_SetIntState(intState); } void TIM_Timer3_Config( diff --git a/src/fw_uart.c b/src/fw_uart.c index aaaa8c0..42a03f2 100644 --- a/src/fw_uart.c +++ b/src/fw_uart.c @@ -25,7 +25,7 @@ __bit busy; */ int16_t _UART1_Timer_InitValueCalculate(HAL_State_t freq1t, uint32_t baudrate) { - uint32_t value, sysclk = SYS_GetSysClk(); + uint32_t value, sysclk = SYS_GetSysClock(); value = sysclk / (4 * baudrate); if (!freq1t) value = value / 12; diff --git a/src/fw_util.c b/src/fw_util.c index 2e6c49d..370242e 100644 --- a/src/fw_util.c +++ b/src/fw_util.c @@ -100,6 +100,7 @@ void UTIL_Uart1_36M864_115200_Init(void) TR1 = 1; } +/* void UTIL_ItrimScan(uint8_t ircband, uint8_t *str) { uint8_t i = 0xFF, j; @@ -114,14 +115,14 @@ void UTIL_ItrimScan(uint8_t ircband, uint8_t *str) { SYS_SetFOSC(ircband, 0, i, j); SYS_Delay(1); - // UTIL_PrintHex(IRCBAND); - // UTIL_PrintChar(' '); - // UTIL_PrintHex(IRTRIM); - // UTIL_PrintChar(0x20); - // UTIL_PrintHex(LIRTRIM); - // UTIL_PrintChar(0x20); - // UTIL_PrintString(str); + UTIL_PrintHex(IRCBAND); + UTIL_PrintChar(' '); + UTIL_PrintHex(IRTRIM); + UTIL_PrintChar(0x20); + UTIL_PrintHex(LIRTRIM); + UTIL_PrintChar(0x20); + UTIL_PrintString(str); SYS_Delay(5); } while (j--); } while (i--); -} \ No newline at end of file +}*/ \ No newline at end of file