#include "ddl.h"
#include "lpuart.h"
#include "bt.h"
#include "clk.h"
#include "lpm.h"
#include "gpio.h"
#include "uart.h"
/******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
/******************************************************************************
* Global variable definitions (declared in header file with 'extern')
******************************************************************************/
/******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/
/******************************************************************************
* Local function prototypes ('static')
******************************************************************************/
/******************************************************************************
* Local variable definitions ('static') *
******************************************************************************/
/******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
/*****************************************************************************
* Function implementation - global ('extern') and local ('static')
******************************************************************************/
/**
******************************************************************************
** \brief Main function of project
**
** \return uint32_t return value, if needed
**
** This sample
**
******************************************************************************/
//static stc_adt_cntstate_cfg_t stcAdt4CntState;
//static uint16_t u16Capture;
/******************************************************************************
* Local variable definitions ('static') *
******************************************************************************/
/*****************************************************************************
* Function implementation - global ('extern') and local ('static')
******************************************************************************/
uint8_t u8RxData[2]={0x50,0x00};
uint8_t u8Rx**=0;
void RxIntCallback(void)
{
u8RxData[1]=M0P_LPUART->SBUF;
u8Rx** = 1;
LPUart_ClrStatus(LPUartRxFull);//LPUartRxFull
}
void ErrIntCallback(void)
{
LPUart_ClrStatus(LPUartRFRAMEError);
}
uint8_t u8RxDataUartch1[2]={0x51,0x00};
uint8_t u8Rx**Uartch1=0;
void RxIntCallbackUartch1(void)
{
u8RxDataUartch1[1]=M0P_UART1->SBUF;
u8Rx**Uartch1=1;
Uart_ClrStatus(UARTCH1,UartRxFull);
}
void ErrIntCallbackUartch1(void)
{
Uart_ClrStatus(UARTCH1,UartRFRAMEError);
}
int32_t main(void)
{
uint16_t timer=0;
uint32_t pclk=0;
uint16_t u16timer=0;
uint32_t u32sclk=0;
volatile uint32_t u32Val = 0;
stc_lpuart_config_t stcConfig;
stc_lpuart_irq_cb_t stcLPUartIrqCb;
stc_lpuart_multimode_t stcMulti;
stc_lpuart_sclk_sel_t stcLpuart_clk;
stc_lpuart_mode_t stcRunMode;
stc_lpuart_baud_config_t stcBaud;
stc_bt_config_t stcBtConfig;
stc_uart_config_t stcConfigUartch1;
stc_uart_irq_cb_t stcUartIrqCbUartch1;
stc_uart_multimode_t stcMultiUartch1;
stc_uart_baud_config_t stcBaudUartch1;
stc_bt_config_t stcBtConfigUartch1;
DDL_ZERO_STRUCT(stcConfig);
DDL_ZERO_STRUCT(stcLPUartIrqCb);
DDL_ZERO_STRUCT(stcMulti);
DDL_ZERO_STRUCT(stcBtConfig);
DDL_ZERO_STRUCT(stcConfigUartch1);
DDL_ZERO_STRUCT(stcUartIrqCbUartch1);
DDL_ZERO_STRUCT(stcMultiUartch1);
DDL_ZERO_STRUCT(stcBtConfigUartch1);
DDL_ZERO_STRUCT(stcBaudUartch1);
Gpio_InitIO(0,3,GpioDirOut);//指示灯
Gpio_SetIO(0,3,1);
Clk_SetRCLFreq(ClkFreq38_4K);
Clk_SwitchTo(ClkRCL);
Clk_SetRCHFreq(ClkFreq22_12Mhz);
Clk_SwitchTo(ClkRCH);
Clk_SetPeripheralGate(ClkPeripheralLpUart,TRUE); //使能LPUART时钟
Clk_SetPeripheralGate(ClkPeripheralBt,TRUE);
Gpio_InitIOExt(2,5,GpioDirOut,TRUE,FALSE,FALSE,FALSE);
Gpio_InitIOExt(2,6,GpioDirOut,TRUE,FALSE,FALSE,FALSE);
Gpio_SetFunc_UART2RX_P25();
Gpio_SetFunc_UART2TX_P26();
stcLpuart_clk.enSclk_sel = LPUart_Pclk;//LPUart_Rcl;
stcLpuart_clk.enSclk_Prs = LPUartDiv1;///<div1
stcConfig.pstcLpuart_clk = &stcLpuart_clk;
stcRunMode.enLpMode = LPUartNoLPMode;//正常工作模式
stcRunMode.enMode = LPUartMode1;///<模式1
stcConfig.pstcRunMode = &stcRunMode;
stcLPUartIrqCb.pfnRxIrqCb = RxIntCallback;
stcLPUartIrqCb.pfnTxIrqCb = NULL;
stcLPUartIrqCb.pfnRxErrIrqCb = ErrIntCallback;
stcConfig.pstcIrqCb = &stcLPUartIrqCb;
stcConfig.bTouchNvic = TRUE;
stcMulti.enMulti_mode = LPUartNormal;//只有模式2/3才有多主机模式
stcConfig.pstcMultiMode = &stcMulti;
LPUart_EnableIrq(LPUartRxIrq);
LPUart_Init(&stcConfig);
if(LPUart_Pclk == stcLpuart_clk.enSclk_sel)
u32sclk = Clk_GetPClkFreq();
else if(LPUart_Rcl == stcLpuart_clk.enSclk_sel)
u32sclk = 38400u;//此处建议用户使用内部38.4K时钟,如果用户使用32.768K时钟的,此处更新成32768
stcBaud.u32Baud = 19200u;
stcBaud.bDbaud = 1u;//
stcBaud.u8LpMode = LPUartNoLPMode;///<非低功耗模式
stcBaud.u8Mode = LPUartMode3;
u16timer = LPUart_SetBaudRate(u32sclk,stcLpuart_clk.enSclk_Prs,&stcBaud);
stcBtConfig.enMD = BtMode2;///< 自动重装载16位计数器/定时器
stcBtConfig.enCT = BtTimer; ///< 定时器功能
stcBtConfig.enTog = BtTogEnable;///< 翻转输出使能
Bt_Init(TIM2, &stcBtConfig);//调用basetimer2设置函数产生波特率
Bt_ARRSet(TIM2,u16timer);//重载值设置
Bt_Cnt16Set(TIM2,u16timer);//6位计数器初值设置
Bt_Run(TIM2);
LPUart_EnableFunc(LPUartRx);
Gpio_InitIOExt(2,3,GpioDirOut,TRUE,FALSE,FALSE,FALSE);
Gpio_InitIOExt(2,4,GpioDirOut,TRUE,FALSE,FALSE,FALSE);
Gpio_SetFunc_UART1TX_P23();
Gpio_SetFunc_UART1RX_P24();
//外设时钟使能
Clk_SetPeripheralGate(ClkPeripheralBt,TRUE);//模式0/2可以不使能
Clk_SetPeripheralGate(ClkPeripheralUart1,TRUE);
stcUartIrqCbUartch1.pfnRxIrqCb = RxIntCallbackUartch1;
stcUartIrqCbUartch1.pfnTxIrqCb = NULL;
stcUartIrqCbUartch1.pfnRxErrIrqCb = ErrIntCallbackUartch1;
stcConfigUartch1.pstcIrqCb = &stcUartIrqCbUartch1;
stcConfigUartch1.bTouchNvic = TRUE;
stcConfigUartch1.enRunMode = UartMode1;//测试项,更改此处来转换4种模式测试
stcMultiUartch1.enMulti_mode = UartNormal;//测试项,更改此处来转换多主机模式,mode2/3才有多主机模式
stcConfigUartch1.pstcMultiMode = &stcMultiUartch1;
stcBaudUartch1.bDbaud = 1u;//双倍波特率功能
stcBaudUartch1.u32Baud = 19200u;//更新波特率位置
stcBaudUartch1.u8Mode = UartMode1; //计算波特率需要模式参数
pclk = Clk_GetPClkFreq();
timer=Uart_SetBaudRate(UARTCH1,pclk,&stcBaudUartch1);
stcBtConfigUartch1.enMD = BtMode2;
stcBtConfigUartch1.enCT = BtTimer;
Bt_Init(TIM1, &stcBtConfigUartch1);//调用basetimer1设置函数产生波特率
Bt_ARRSet(TIM1,timer);
Bt_Cnt16Set(TIM1,timer);
Bt_Run(TIM1);
Uart_Init(UARTCH1, &stcConfigUartch1);
Uart_ClrStatus(UARTCH1,UartRxFull);
Uart_EnableIrq(UARTCH1,UartRxIrq);
Uart_ClrStatus(UARTCH1,UartRxFull);
Uart_EnableFunc(UARTCH1,UartRx);
while (1)
{
Gpio_SetIO(0,3,u8Rx**||u8Rx**Uartch1);
if(u8Rx**Uartch1)
{
Uart_SendData(UARTCH1,u8RxDataUartch1[0]);
Uart_SendData(UARTCH1,u8RxDataUartch1[1]);
u8Rx**Uartch1=0;
}
if(u8Rx**)
{
u8Rx**=0;
LPUart_SendData(u8RxData[0]);
LPUart_SendData(u8RxData[1]);
if(0x99==u8RxData[1])
{
LPUart_SendData((uint8_t)('<'));
LPUart_SendData((uint8_t)('R'));
LPUart_SendData((uint8_t)('D'));
LPUart_SendData((uint8_t)('_'));
LPUart_SendData((uint8_t)('N'));
LPUart_SendData((uint8_t)('A'));
LPUart_SendData((uint8_t)('M'));
LPUart_SendData((uint8_t)('E'));
LPUart_SendData((uint8_t)('>'));
}
}
}
} |