CC 2530シリアルポート駆動
12077 ワード
IARを使用してCC 2530のシリアルポート0、シリアルポート1を駆動し、データ送信及びprintfを実現し、受信データを中断する
uart.c
uart.h
//printfからシリアルポートへの再定義
初期化
uart.c
/*************************************************************************************************************
* : uart.c
* : CC2530
* : [email protected]
* : 2013-06-07 21:33
* :2013-06-07
* :
1/16
*************************************************************************************************************/
#include "system.h"
#include "uart.h"
// UART
typedef struct
{
u8 BuffFull; // Buff
u8 *RxBuff; // Buff
u16 RxBuffSize; // ,
u16 UartRxCnt; //
} UartRx_TypeDef;
static UartRx_TypeDef UART_RX[2];
static const u8 BAUD_M[11] = {59, 59, 59, 216, 59, 216, 59, 216, 59, 216, 216}; //32MHZ
static const u8 BAUD_E[11] = {6, 7 ,8, 8, 9, 9, 10, 10, 11, 11, 12}; //32MHZ
/*************************************************************************************************************************
* : void UART_Init(UART_CH ch, USART_BAUD Baud, FunctionalState RxIntEn)
* :
* : ch: ,UART_CH0,UART_CH1
Baud: , USART_BAUD
RxIntEn:ENABLE:
* :
* :
* : [email protected]
* : 2013-05-20
* : 2013-06-11
* : ,8 , ,
*************************************************************************************************************************/
void UART_Init(UART_CH ch, USART_BAUD Baud, FunctionalState RxIntEn)
{
switch(ch)
{
case UART_CH0:
{
U0CSR = BIT7 + BIT6; //UART ,
U0UCR = BIT1; // , ,8bit,1 , ,
U0GCR = BAUD_E[Baud]; //
U0BAUD = BAUD_M[Baud]; //
P0SEL |= BIT2 + BIT3; //P0.3 TXD,P0.2 RXD
IEN2 &= ~(1 << 3); //
URX0IF = 0; //
UTX0IF = 0; //
URX0IE = (RxIntEn == ENABLE) ? 1 : 0; //
}break;
case UART_CH1:
{
U1CSR = BIT7 + BIT6; //UART ,
U1UCR = BIT1; // , ,8bit,1 , ,
U1GCR = BAUD_E[Baud]; //
U1BAUD = BAUD_M[Baud]; //
P1SEL |= BIT4 + BIT5; //P0.5 TXD,P0.4 RXD
URX1IF = 0; //
UTX1IF = 0; //
URX1IE = (RxIntEn == ENABLE) ? 1 : 0; //
}break;
default : return;
}
UART_SetRxBuff(ch, NULL, 0); //
}
/*************************************************************************************************************************
* : void UART_SendByte(UART_CH ch, u8 data)
* : UART
* : ch: ,UART_CH0,UART_CH1
data:
* :
* :
* : [email protected]
* : 2013-06-11
* : 2013-06-11
* :
*************************************************************************************************************************/
void UART_SendByte(UART_CH ch, u8 data)
{
switch(ch)
{
case UART_CH0:
{
U0DBUF = data; //
while(!(U0CSR & BIT1)); //
U0CSR &= ~BIT1;
}break;
case UART_CH1:
{
U1DBUF = data; //
while(!(U1CSR & BIT1)); //
U1CSR &= ~BIT1;
}break;
default : break;
}
}
/*************************************************************************************************************************
* : void UART2_SendData(u8 *pbuff, u16 len)
* :
* : ch: ,UART_CH0,UART_CH1
pbuff: ,len:
* :
* :
* : [email protected]
* : 2013-06-11
* : 2013-06-11
* :
*************************************************************************************************************************/
void UART_SendData(UART_CH ch, u8 *pbuff, u16 len)
{
u16 i;
switch(ch)
{
case UART_CH0:
{
for(i = 0;i < len;i ++)
{
U0DBUF = pbuff[i]; //
while(!(U0CSR & BIT1)); //
U0CSR &= ~BIT1;
}
}break;
case UART_CH1:
{
for(i = 0;i < len;i ++)
{
U1DBUF = pbuff[i]; //
while(!(U1CSR & BIT1)); //
U1CSR &= ~BIT1;
}
}break;
default : break;
}
}
/*************************************************************************************************************************
* : void UART2_SendString(UART_CH ch, const char *pStr)
* : UART
* : ch: ,UART_CH0,UART_CH1
pStr:
* :
* :
* : [email protected]
* : 2013-06-11
* : 2013-06-11
* : '0\'
*************************************************************************************************************************/
void UART_SendString(UART_CH ch, const char *pStr)
{
while(*pStr != '\0')
{
UART_SendByte(ch, *pStr ++);
}
}
/*************************************************************************************************************************
* : void UART_RxEnable(UART_CH ch, FunctionalState Enable)
* : UART
* : ch: ,UART_CH0,UART_CH1
Enable:ENABLE: ,DISABLE:
* :
* :
* : [email protected]
* : 2013-06-11
* : 2013-06-11
* :
*************************************************************************************************************************/
void UART_RxEnable(UART_CH ch, FunctionalState Enable)
{
switch(ch)
{
case UART_CH0:
{
U0CSR = (Enable == ENABLE) ? (U0CSR|BIT6) : (U0CSR&(~BIT6)); //
}break;
case UART_CH1:
{
U1CSR = (Enable == ENABLE) ? (U1CSR|BIT6) : (U1CSR&(~BIT6)); //
}break;
default : break;
}
}
//UART0
#pragma vector=URX0_VECTOR
__interrupt void UART0_IRQHandler(void)
{
if(UART_RX[0].RxBuffSize > 0)
{
UART_RX[0].RxBuff[UART_RX[0].UartRxCnt ++] = U0DBUF;
if(UART_RX[0].UartRxCnt == UART_RX[0].RxBuffSize)
{
UART_RX[0].UartRxCnt = 0;
UART_RX[0].BuffFull = 1;
}
}
else
{
URX0IF = 0; //
}
}
//UART1
#pragma vector=URX1_VECTOR
__interrupt void UART1_IRQHandler(void)
{
if(UART_RX[1].RxBuffSize > 0)
{
UART_RX[1].RxBuff[UART_RX[1].UartRxCnt ++] = U1DBUF;
if(UART_RX[1].UartRxCnt == UART_RX[1].RxBuffSize)
{
UART_RX[1].UartRxCnt = 0;
UART_RX[1].BuffFull = 1;
}
}
else
{
URX1IF = 0; //
}
}
/*************************************************************************************************************************
* : bool UART_GetNewData(UART_CH ch, u8 *pData)
* :
* : ch: ,UART_CH0,UART_CH1
pData:
* : TRUE: ,FALSE:
* :
* : [email protected]
* : 2013-06-11
* : 2013-06-11
* :
*************************************************************************************************************************/
bool UART_GetNewData(UART_CH ch, u8 *pData)
{
switch(ch)
{
case UART_CH0:
{
if(U0CSR & BIT2)
{
*pData = U0DBUF;
return TRUE;
}
return FALSE;
}break;
case UART_CH1:
{
if(U1CSR & BIT2)
{
*pData = U1DBUF;
return TRUE;
}
return FALSE;
}break;
default : return FALSE;
}
}
/*************************************************************************************************************************
* : bool UART_GetRxBuffFullFlag(UART_CH ch)
* :
* : ch: ,UART_CH0,UART_CH1
* : TRUE: ,FALSE:
* :
* : [email protected]
* : 2013-06-11
* : 2013-06-11
* : ,
*************************************************************************************************************************/
bool UART_GetRxBuffFullFlag(UART_CH ch)
{
if(UART_RX[ch].BuffFull) //
{
UART_RX[ch].BuffFull = 0; //
return TRUE;
}
return FALSE;
}
/*************************************************************************************************************************
* : void UART_SetRxBuff(UART_CH ch, u8 *pRxBuff, u16 BuffSize)
* :
* : ch: ,UART_CH0,UART_CH1
pRxBuff: ,BuffSize:
* :
* :
* : [email protected]
* : 2013-06-11
* : 2013-06-11
* :
*************************************************************************************************************************/
void UART_SetRxBuff(UART_CH ch, u8 *pRxBuff, u16 BuffSize)
{
UART_RX[ch].RxBuffSize = BuffSize; //
UART_RX[ch].RxBuff = pRxBuff; //
UART_RX[ch].UartRxCnt = 0; //
}
/*************************************************************************************************************************
* : u16 UART_GetRxCnt(UART_CH ch)
* :
* : ch: ,UART_CH0,UART_CH1
* :
* :
* : [email protected]
* : 2013-06-11
* : 2013-06-11
* :
*************************************************************************************************************************/
u16 UART_GetRxCnt(UART_CH ch)
{
return UART_RX[ch].UartRxCnt; //
}
/*************************************************************************************************************************
* : void UART_ClearRxCnt(UART_CH ch)
* :
* : ch: ,UART_CH0,UART_CH1
* :
* :
* : [email protected]
* : 2013-06-11
* : 2013-06-11
* :
*************************************************************************************************************************/
void UART_ClearRxCnt(UART_CH ch)
{
UART_RX[ch].UartRxCnt = 0; //
}
uart.h
/*************************************************************************************************************
* : uart.h
* : CC2530
* : [email protected]
* : 2013-06-07 21:33
* :2013-06-07
* :
*************************************************************************************************************/
#ifndef _UART_H_
#define _UART_H_
#include "system.h"
#include "stdio.h"
//
typedef enum
{
BAUD_2400 = 0, //2400
BAUD_4800 = 1, //4800
BAUD_9600 = 2, //9600
BAUD_14400 = 3, //14400
BAUD_19200 = 4, //19200
BAUD_28800 = 5, //28800
BAUD_38400 = 6, //38400
BAUD_57600 = 7, //57600
BAUD_76800 = 8, //76800
BAUD_115200 = 9, //115200
BAUD_230400 = 10, //230400
} USART_BAUD;
//
typedef enum
{
UART_CH0 = 0, // 0, 0
UART_CH1 = 1, // 1, 1
} UART_CH;
//UAR
void UART_Init(UART_CH ch, USART_BAUD Baud, FunctionalState RxIntEn); //UART
void UART_SendByte(UART_CH ch, u8 data);
void UART_SendData(UART_CH ch, u8 *pbuff, u16 len); //
void UART_SendString(UART_CH ch, const char *pStr); //UART
void UART_RxEnable(UART_CH ch, FunctionalState Enable); //UART
bool UART_GetNewData(UART_CH ch, u8 *pData); //
bool UART_GetRxBuffFullFlag(UART_CH ch); //
void UART_SetRxBuff(UART_CH ch, u8 *pRxBuff, u16 BuffSize); //
u16 UART_GetRxCnt(UART_CH ch); //
void UART_ClearRxCnt(UART_CH ch); //
#endif //_UART_H_
//printfからシリアルポートへの再定義
#if _PRINTF_EN_
#include "uart.h"
#include "stdio.h"
//#define __CODE_MODEL__ = __CM_BANKED__
__near_func int putchar(int ch)
{
UART_SendByte(UART_CH0, ch);
return ch;
}
#endif
初期化
//
int main(void)
{
SYS_ClockInit();
UART_Init(UART_CH0, BAUD_115200,ENABLE);
LED_Init();
clock_init();
SYS_EnableInt();
process_init();
process_start(&etimer_process, NULL);
autostart_start(autostart_processes);
printf("Processes running
");
while(1)
{
do
{
} while(process_run()> 0);
SYS_PowerIdle(); //
}
}