#include "DSP240x_Device.h"
/*
* Baud Rate Calculations/Definitions
*/
/* #define BAUD_9600 0x0103 */
#define BAUD_9600 389
#define BAUD_19200 194
#define BAUD_38400 96
#define BAUD_43000 86
#define BAUD_115200 31
/* 29.4912 MHz SYSCLK */
/* baud = 29,491,200 /[383 + 1] * 8 */
/* baud = 29,491,200 / 384 * 8 */
/* baud = 29,491,200 / 3072 */
/* baud = 9600.000 */
/* % error = [9600-9600] /9600 */
/* % error = 0.0 */
/*----------for reference ------------*/
/* 20 MHz = 20 MHz SYSCLK */
/* baud = 20,000,000 /[259 + 1] * 8 */
/* baud = 20,000,000 / 260 * 8 */
/* baud = 20,000,000 / 2080 */
/* baud = 9615.384 */
/* % error = [9615-9600] /9600 */
/* % error = 15 / 9600 */
/* % error = 0.0015625 */
/*
This module defines the bit positions in various sci registers. For other
registers more definitions are needed.
*/
/*
SCInCCR
*/
#define STOP_BITS 0x80
#define EVEN_PARITY 0x40
#define PARITY_EN 0x20
#define LOOP_BACK 0x10
#define ADDR_IDLE 0x08
#define SCI_CHAR_8 0x07
#define SCI_CHAR_7 0x06
#define SCI_CHAR_6 0x05
#define SCI_CHAR_5 0x04
#define SCI_CHAR_4 0x03
#define SCI_CHAR_3 0x02
#define SCI_CHAR_2 0x01
#define SCI_CHAR_1 0x00
/*
SCInCTL1
*/
#define RX_ERR_INT_ENA 0x40
#define SW_RESET 0x20
#define TXWAKE 0x08
#define SLEEP 0x04
#define TX_ENA 0x02
#define RX_ENA 0x01
/*
SCInCTL2
*/
#define TX_RDY 0x0080
#define TX_EMPTY 0x0040
#define RX_INT_ENA 0x02
#define TX_INT_ENA 0x01
/*
SCInRXST
*/
#define RX_ERROR 0x80
#define RX_RDY 0x40
#define BRKDT 0x20
#define FE 0x10
#define OE 0x08
#define PE 0x04
#define RXWAKE 0x02
/*
SCInPRI
*/
#define PRI_TXPRI 0x40
#define PRI_RXPRI 0x20
#define PRI_SOFT 0x10
#define PRI_FREE 0x08
//通过串口接受的指令定义
#define No_1 0x01
#define No_2 0x02
#define No_3 0x03
#define No_4 0x04
#define No_5 0x05
#define No_6 0x06
#define No_7 0x07
#define No_8 0x08
#define No_9 0x09
#define No_10 0x0A
#define No_11 0x0B
#define No_12 0x0C
#define No_13 0x0D
#define No_14 0x0E
#define No_15 0x0F
extern unsigned int uart_rec_char[256];
//unsigned char uart1_test_msg[] = {"This is a test message from uart #1"};
void Init_Sci(void);
//设定通信接口通信控制寄存器参数
//SCICCR_PARA_SET(STOP_BITS),两个停止位,默认为1个
//SCICCR_PARA_SET(EVEN_PARITY),偶校验,默认为奇校验
//SCICCR_PARA_SET(PARITY_ENA),使能奇、偶校验,默认为不校验
//SCICCR_PARA_SET(LOOP_BACK),使能自测试模式,
//SCICCR_PARA_SET(ADDR_IDLE)选择地址位多处理器模式
//SCICCR_PARA_SET(SCI_CHAR_x )SCI字符长度选择
void SCICCR_PARA_SET(unsigned int para);
//取消通信接口通信控制寄存器参数设定
//SCICCR_PARA_UNSET(STOP_BITS),设置为一个停止位
//SCICCR_PARA_UNSET(EVEN_PARITY)奇校验
//SCICCR_PARA_UNSET(PARITY_ENA),不进行奇偶校验
//SCICCR_PARA_UNSET(LOOP_BACK),禁止自测试模式,
//SCICCR_PARA_SET(ADDR_IDLE)选择空闲线多处理器模式
void SCICCR_PARA_UNSET(unsigned int para);
//SCI软件复位位,在系统复位后,应该将运行此函数来重新使能SCI
void SCI_RESET(void);
//设定通信接口通信控制寄存器参数
//SCICTL1_PARA_SET(RX_ERR_INT_ENA),使能接受错误中断
//SCICTL1_PARA_SET(SW_RESET),SCI软件复位
//SCICTL1_PARA_SET(TWAKE),SCI发送器唤醒方法选择位,发送特征由SCICCR的ADDR_IDLE位决定
//SCICTL1_PARA_SET(SLEEP),使能SCI休眠方式,
//SCICTL1_PARA_SET(TX_ENA),使能SCI发送
//SCICTL1_PARA_SET(RX_ENA),使能SCI接收
void SCICTL1_PARA_SET(unsigned int para);
//设定通信接口通信控制寄存器1参数
//SCICTL1_PARA_UNSET(RX_ERR_INT_ENA),禁止接受错误中断
//SCICTL1_PARA_UNSET(SW_RESET),初始化SC状态机和操作标志至复位条件
//SCICTL1_PARA_UNSET(TWAKE),SCI发送器无选定的发送特征
//SCICTL1_PARA_UNSET(SLEEP),禁止SCI休眠方式,
//SCICTL1_PARA_UNSET(TX_ENA),禁止SCI发送
//SCICTL1_PARA_UNSET(RX_ENA),禁止SCI接收
void SCICTL1_PARA_UNSET(unsigned int para);
//通信接口通信控制寄存器2参数获取
//SCICTL1_PARA_GET(SLEEP)休眠模式参数获得
unsigned int SCICTL1_PARA_GET(unsigned int para);
//通信接口通信控制寄存器2参数设定
//SCICTL2_PARA_SET(RX_INT_ENA),使能RXRDY中断
//SCICTL2_PARA_SET(TX_INT_ENA),使能TXRDY中断
unsigned int SCICTL2_PARA_SET(unsigned int para);
//通信接口通信控制寄存器2参数设定
//SCICTL2_PARA_UNSET(RX_INT_ENA),禁止RXRDY中断
//SCICTL2_PARA_UNSET(TX_INT_ENA),禁止TXRDY中断
unsigned int SCICTL2_PARA_UNSET(unsigned int para);
//通信接口通信控制寄存器2参数获取
//SCICTL2_PARA_GET(TX_RDY),返回1,发送缓冲寄存器空,准备发送下一个数据
//返回0,发送缓冲寄存器满,无法发送下一个数据
//SCICTL2_PARA_GET(STX_EMPTY),S返回1,发送缓冲寄存器SCITXBUF和TXSHF空,准备发送下一个数据
//返回0,发送缓冲寄存器SCITXBUF和TXSHF满,无法发送下一个数据
unsigned int SCICTL2_PARA_GET(unsigned int para);
//通信接口通信控制寄存器2参数获取
//SCICTL2_PARA_GET(TX_RDY),返回1,发送缓冲寄存器空,准备发送下一个数据
//返回0,发送缓冲寄存器满,无法发送下一个数据
//SCICTL2_PARA_GET(STX_EMPTY),S返回1,发送缓冲寄存器SCITXBUF和TXSHF空,准备发送下一个数据
//返回0,发送缓冲寄存器SCITXBUF和TXSHF满,无法发送下一个数据
void SCI_BAUD_SET(unsigned int baud);
//通信接口接受状态寄存器参数获取
//SCICTL2_PARA_GET(TX_RDY),返回1,发送缓冲寄存器空,准备发送下一个数据
//返回0,发送缓冲寄存器满,无法发送下一个数据
//SCICTL2_PARA_GET(STX_EMPTY),S返回1,发送缓冲寄存器SCITXBUF和TXSHF空,准备发送下一个数据
//返回0,发送缓冲寄存器SCITXBUF和TXSHF满,无法发送下一个数据
unsigned int SCIRXST_PARA_GET(unsigned int para);
//通信接口接受状态寄存器参数获取
//SCIRXST_PARA_GET(RX_ERROR),有错误标志被置位
//SCIRXST_PARA_GET(RX_RDY),SCI接收器准备好标志位,如果RX_INT_ENA为1,则产生接收中断
//SCIRXST_PARA_GET(BRKDT),是否满足中断条件
//SCIRXST_PARA_GET(FE),是否检测到帧错误
//SCIRXST_PARA_GET(OE),是否检测到超时错误
//SCIRXST_PARA_GET(PE),是否检验到奇偶校验错误
//SCIRXST_PARA_GET(RXWAKE),接收器唤醒检测标志位
unsigned int SCIRXST_PARA_GET(unsigned int para);
//检查接收状态
unsigned int Chk_Sci_Rec_Status(void);
//检查发送状态
unsigned int Chk_Sci_Send_Status(void);
unsigned int Sci_Get_Cmd_Idle(void);
unsigned int Sci_Get_Char(void);
void Sci_Get_String(unsigned int *p);
void Sci_Add_String(unsigned int num,unsigned int * source,unsigned int *dest);
unsigned int Sci_Put_Char(unsigned int out_char);
unsigned int Sci_Put_String(unsigned int * out_string);
void SCI_TEST();