feat: add uart3, uart4

This commit is contained in:
IOsetting 2021-12-31 00:28:03 +08:00
parent 59d150e2b2
commit e35d3a28a5
13 changed files with 139 additions and 81 deletions

View File

@ -44,7 +44,7 @@ void main(void)
{
SYS_SetClock();
// For debug print
UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200, HAL_State_OFF);
UART1_ConfigMode1Dyn8bitUart(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

View File

@ -64,7 +64,7 @@ void main(void)
{
SYS_SetClock();
// For debug print
UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200, HAL_State_OFF);
UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200);
// Channel: ADC1
ADC_SetChannel(0x01);
// ADC Clock = SYSCLK / 2 / (1+15) = SYSCLK / 32

View File

@ -34,7 +34,7 @@ void main(void)
uint16_t res;
SYS_SetClock();
// For debug print
UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200, HAL_State_OFF);
UART1_ConfigMode1Dyn8bitUart(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

View File

@ -34,7 +34,7 @@ void main(void)
uint8_t res;
SYS_SetClock();
// For debug print
UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200, HAL_State_OFF);
UART1_ConfigMode1Dyn8bitUart(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

View File

@ -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, HAL_State_OFF);
UART1_ConfigMode1Dyn8bitUart(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);

View File

@ -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, HAL_State_OFF);
UART1_ConfigMode1Dyn8bitUart(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);

View File

@ -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, HAL_State_OFF);
UART1_ConfigMode1Dyn8bitUart(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);

View File

@ -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, HAL_State_OFF);
UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200);
EXTI_Global_SetIntState(HAL_State_ON);
// 1T mode, prescaler:255+1, frequency: 5, interrupt: ON
TIM_Timer2_Config(HAL_State_ON, 0xFF, 5);

View File

@ -25,8 +25,11 @@ void main(void)
{
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_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200);
UART1_SetRxState(HAL_State_ON);
// Enable UART1 interrupt
EXTI_Global_SetIntState(HAL_State_ON);
EXTI_UART1_SetIntState(HAL_State_ON);
while(1)
{
UART1_IntTxChar('R');

View File

@ -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, HAL_State_OFF);
UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200);
while(1)
{
UART1_TxChar('T');

View File

@ -18,7 +18,8 @@ void main(void)
{
SYS_SetClock();
// UART2, baud 115200, baud source Timer2, 1T mode, no interrupt
UART2_ConfigMode0Dyn8bitUart(HAL_State_ON, 115200, HAL_State_OFF);
UART2_Set8bitUART();
UART2_Config(HAL_State_ON, 115200);
while(1)
{
UART2_TxChar('T');

View File

@ -21,9 +21,11 @@
#define UART_RX_BUFF_SIZE 0x20
/***************************** /
/**************************************************************************** /
* UART1
*/
typedef enum
{
UART1_BaudSource_Timer1 = 0x00,
@ -39,8 +41,8 @@ typedef enum
} UART1_AlterPort_t;
#define UART1_SetRxState(__STATE__) SBIT_ASSIGN(REN, __STATE__)
#define UART1_ClearTxInterrupt SBIT_RESET(TI)
#define UART1_ClearRxInterrupt SBIT_RESET(RI)
#define UART1_ClearTxInterrupt() SBIT_RESET(TI)
#define UART1_ClearRxInterrupt() SBIT_RESET(RI)
#define UART1_WriteBuffer(__DATA__) (SBUF = (__DATA__))
#define UART1_SetFrameErrDetect(__STATE__) SFR_ASSIGN(PCON, 6, __STATE__)
#define UART1_SetBaudSource(__BAUD_SRC__) SFR_ASSIGN(AUXR, 0, __BAUD_SRC__)
@ -63,13 +65,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 freq1t, uint32_t baudrate, HAL_State_t enableInterrupt);
void UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_t baudSource, HAL_State_t freq1t, 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 freq1t, uint32_t baudrate, HAL_State_t enableInterrupt);
void UART1_ConfigMode3Dyn9bitUart(UART1_BaudSource_t baudSource, HAL_State_t freq1t, uint32_t baudrate);
void UART1_InterruptHandler(void);
@ -85,27 +85,64 @@ void UART1_TxHex(uint8_t hex);
void UART1_TxString(uint8_t *str);
/***************************** /
/**************************************************************************** /
* UART2
*/
#define UART2_SetRxState(__STATE__) SFR_ASSIGN(S2CON, 4, __STATE__)
#define UART2_ClearTxInterrupt SFR_RESET(S2CON, 1)
#define UART2_ClearRxInterrupt SFR_RESET(S2CON, 0)
#define UART2_WriteBuffer(__DATA__) (S2BUF = (__DATA__))
#define UART2_SetRxState(__STATE__) SFR_ASSIGN(S2CON, 4, __STATE__)
#define UART2_ClearTxInterrupt() SFR_RESET(S2CON, 1)
#define UART2_ClearRxInterrupt() SFR_RESET(S2CON, 0)
#define UART2_WriteBuffer(__DATA__) (S2BUF = (__DATA__))
#define UART2_TxFinished() (S2CON & (0x01 << 1))
#define UART2_Set8bitUART() SFR_RESET(S2CON, 7)
#define UART2_Set9bitUART() SFR_SET(S2CON, 7)
/**
* Mode0: 8-bit UART, dynamic baud-rate, provided by Timer2
* Dynamic baud-rate, provided by Timer2
*/
void UART2_ConfigMode0Dyn8bitUart(
HAL_State_t freq1t, uint32_t baudrate, HAL_State_t enableInterrupt);
/**
* Mode1: 9-bit UART, dynamic baud-rate, provided by Timer2
*/
void UART2_ConfigMode1Dyn9bitUart(
HAL_State_t freq1t, uint32_t baudrate, HAL_State_t enableInterrupt);
void UART2_Config(HAL_State_t freq1t, uint32_t baudrate);
void UART2_TxChar(char dat);
void UART2_TxHex(uint8_t hex);
void UART2_TxString(uint8_t *str);
/**************************************************************************** /
* UART3
*/
#define UART3_SetBaudSource(__BAUD_SRC__) SFR_ASSIGN(S3CON, 6, __BAUD_SRC__)
#define UART3_SetRxState(__STATE__) SFR_ASSIGN(S3CON, 4, __STATE__)
#define UART3_Set8bitUART() SFR_RESET(S3CON, 7)
#define UART3_Set9bitUART() SFR_SET(S3CON, 7)
#define UART3_ClearTxInterrupt() SFR_RESET(S3CON, 1)
#define UART3_ClearRxInterrupt() SFR_RESET(S3CON, 0)
#define UART3_WriteBuffer(__DATA__) (S3BUF = (__DATA__))
/**
* dynamic baud-rate from timer2 or timer3
*/
void UART3_ConfigOnTimer2(HAL_State_t freq1t, uint32_t baudrate);
void UART3_ConfigOnTimer3(HAL_State_t freq1t, uint32_t baudrate);
/**************************************************************************** /
* UART4
*/
#define UART4_SetBaudSource(__BAUD_SRC__) SFR_ASSIGN(S4CON, 6, __BAUD_SRC__)
#define UART4_SetRxState(__STATE__) SFR_ASSIGN(S4CON, 4, __STATE__)
#define UART4_Set8bitUART() SFR_RESET(S4CON, 7)
#define UART4_Set9bitUART() SFR_SET(S4CON, 7)
#define UART4_ClearTxInterrupt() SFR_RESET(S4CON, 1)
#define UART4_ClearRxInterrupt() SFR_RESET(S4CON, 0)
#define UART4_WriteBuffer(__DATA__) (S4BUF = (__DATA__))
/**
* dynamic baud-rate from timer2 or timer4
*/
void UART4_ConfigOnTimer2(HAL_State_t freq1t, uint32_t baudrate);
void UART4_ConfigOnTimer4(HAL_State_t freq1t, uint32_t baudrate);
#endif

View File

@ -20,9 +20,11 @@ static const char hexTable[16] = { '0','1','2','3','4','5','6','7','8','9','A','
char wptr, rptr, UART1_RxBuffer[UART_RX_BUFF_SIZE];
__bit busy;
/***************************** /
/**************************************************************************** /
* UART1
*/
int16_t _UART1_Timer_InitValueCalculate(HAL_State_t freq1t, uint32_t baudrate)
{
uint32_t value, sysclk = SYS_GetSysClock();
@ -35,7 +37,7 @@ int16_t _UART1_Timer_InitValueCalculate(HAL_State_t freq1t, uint32_t baudrate)
return 0xFFFF - value + 1;
}
void _UART1_ConfigDynUart(UART1_BaudSource_t baudSource, HAL_State_t freq1t, uint32_t baudrate, HAL_State_t enableInterrupt)
void _UART1_ConfigDynUart(UART1_BaudSource_t baudSource, HAL_State_t freq1t, uint32_t baudrate)
{
uint16_t init = _UART1_Timer_InitValueCalculate(freq1t, baudrate);
UART1_SetBaudSource(baudSource);
@ -55,39 +57,29 @@ void _UART1_ConfigDynUart(UART1_BaudSource_t baudSource, HAL_State_t freq1t, uin
TIM_Timer2_SetInitValue(init >> 8, init & 0xFF);
TIM_Timer2_SetRunState(HAL_State_ON);
}
if (enableInterrupt)
{
// Enable UART1 interrupt
EXTI_Global_SetIntState(HAL_State_ON);
EXTI_UART1_SetIntState(HAL_State_ON);
EXTI_UART1_SetIntPriority(EXTI_IntPriority_Highest);
}
}
void UART1_ConfigMode1Dyn8bitUart(
UART1_BaudSource_t baudSource, HAL_State_t freq1t, uint32_t baudrate, HAL_State_t enableInterrupt)
void UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_t baudSource, HAL_State_t freq1t, uint32_t baudrate)
{
SM0=0; SM1=1;
_UART1_ConfigDynUart(baudSource, freq1t, baudrate, enableInterrupt);
_UART1_ConfigDynUart(baudSource, freq1t, baudrate);
}
void UART1_ConfigMode3Dyn9bitUart(
UART1_BaudSource_t baudSource, HAL_State_t freq1t, uint32_t baudrate, HAL_State_t enableInterrupt)
void UART1_ConfigMode3Dyn9bitUart(UART1_BaudSource_t baudSource, HAL_State_t freq1t, uint32_t baudrate)
{
SM0=1; SM1=1;
_UART1_ConfigDynUart(baudSource, freq1t, baudrate, enableInterrupt);
_UART1_ConfigDynUart(baudSource, freq1t, baudrate);
}
void UART1_InterruptHandler(void)
{
if (TI)
{
TI = 0;
UART1_ClearTxInterrupt();
busy = 0;
}
if (RI)
{
RI = 0;
UART1_ClearRxInterrupt();
UART1_RxBuffer[rptr++] = SBUF;
rptr = rptr % UART_RX_BUFF_SIZE;
}
@ -97,7 +89,7 @@ void UART1_IntTxChar(char dat)
{
while (busy);
busy = 1;
SBUF = dat;
UART1_WriteBuffer(dat);
}
void UART1_IntTxHex(uint8_t hex)
@ -113,9 +105,9 @@ void UART1_IntTxString(uint8_t *str)
void UART1_TxChar(char dat)
{
SBUF = dat;
UART1_WriteBuffer(dat);
while(!TI);
SBIT_RESET(TI);
UART1_ClearTxInterrupt();
}
void UART1_TxHex(uint8_t hex)
@ -130,47 +122,24 @@ void UART1_TxString(uint8_t *str)
}
/***************************** /
/**************************************************************************** /
* UART2
*/
void _UART2_ConfigDynUart(HAL_State_t freq1t, uint32_t baudrate, HAL_State_t enableInterrupt)
void UART2_Config(HAL_State_t freq1t, uint32_t baudrate)
{
uint16_t init = _UART1_Timer_InitValueCalculate(freq1t, baudrate);
// Timer2: 1T mode and initial value. prescaler is ignored, no interrupt.
TIM_Timer2_Set1TMode(freq1t);
TIM_Timer2_SetInitValue(init >> 8, init & 0xFF);
TIM_Timer2_SetRunState(HAL_State_ON);
if (enableInterrupt)
{
// Enable UART1 interrupt
EXTI_Global_SetIntState(HAL_State_ON);
EXTI_UART2_SetIntState(HAL_State_ON);
EXTI_UART2_SetIntPriority(EXTI_IntPriority_Highest);
}
}
void UART2_ConfigMode0Dyn8bitUart(
HAL_State_t freq1t, uint32_t baudrate, HAL_State_t enableInterrupt)
{
SFR_RESET(S2CON, 7);
_UART2_ConfigDynUart(freq1t, baudrate, enableInterrupt);
}
/**
* Mode1: 9-bit UART, dynamic baud-rate, provided by Timer2
*/
void UART2_ConfigMode1Dyn9bitUart(
HAL_State_t freq1t, uint32_t baudrate, HAL_State_t enableInterrupt)
{
SFR_SET(S2CON, 7);
_UART2_ConfigDynUart(freq1t, baudrate, enableInterrupt);
}
void UART2_TxChar(char dat)
{
S2BUF = dat;
while(!(S2CON & B00000010));
SFR_RESET(S2CON, 1);
UART2_WriteBuffer(dat);
while(!UART2_TxFinished());
UART2_ClearTxInterrupt();
}
void UART2_TxHex(uint8_t hex)
@ -183,3 +152,51 @@ void UART2_TxString(uint8_t *str)
{
while (*str) UART2_TxChar(*str++);
}
/**************************************************************************** /
* UART3
*/
void UART3_ConfigOnTimer2(HAL_State_t freq1t, uint32_t baudrate)
{
UART3_SetBaudSource(0x00);
uint16_t init = _UART1_Timer_InitValueCalculate(freq1t, baudrate);
// Timer2: 1T mode and initial value. prescaler is ignored, no interrupt.
TIM_Timer2_Set1TMode(freq1t);
TIM_Timer2_SetInitValue(init >> 8, init & 0xFF);
TIM_Timer2_SetRunState(HAL_State_ON);
}
void UART3_ConfigOnTimer3(HAL_State_t freq1t, uint32_t baudrate)
{
UART3_SetBaudSource(0x01);
uint16_t init = _UART1_Timer_InitValueCalculate(freq1t, baudrate);
// Timer3: 1T mode and initial value. prescaler is ignored, no interrupt.
TIM_Timer3_Set1TMode(freq1t);
TIM_Timer3_SetInitValue(init >> 8, init & 0xFF);
TIM_Timer3_SetRunState(HAL_State_ON);
}
/**************************************************************************** /
* UART4
*/
void UART4_ConfigOnTimer2(HAL_State_t freq1t, uint32_t baudrate)
{
UART4_SetBaudSource(0x00);
uint16_t init = _UART1_Timer_InitValueCalculate(freq1t, baudrate);
TIM_Timer2_Set1TMode(freq1t);
TIM_Timer2_SetInitValue(init >> 8, init & 0xFF);
TIM_Timer2_SetRunState(HAL_State_ON);
}
void UART4_ConfigOnTimer4(HAL_State_t freq1t, uint32_t baudrate)
{
UART4_SetBaudSource(0x01);
uint16_t init = _UART1_Timer_InitValueCalculate(freq1t, baudrate);
TIM_Timer4_Set1TMode(freq1t);
TIM_Timer4_SetInitValue(init >> 8, init & 0xFF);
TIM_Timer4_SetRunState(HAL_State_ON);
}