?? uartdebug.c
字號(hào):
/*
****************************************************************************
* 寧波中科集成電路設(shè)計(jì)中心 版權(quán)所有 Copyright 2005
* http:\\www.nbicc.com
*文件名: fun.h
*程序員: 夏鵬 xpsonny@nbicc.com
*主要內(nèi)容:常用函數(shù)
*
*如有問題或BUG,請登錄www.wsn.net.cn 上的BBS到相關(guān)版面提問!
*
****************************************************************************
*/
#include "type.h"
#include "uartDebug.h"
#include "message.h"
#define DBG_BUF_LEN 60
#define UART_IDLE 0
#define UART_BUSY 1
char UARTState;
char dbgBuf[DBG_BUF_LEN];
uint8_t dbgHead;
uint8_t dbgTail;
uint8_t dbgBufCount;
uint8_t appendByte;
void uartDebug_init()
{
UARTState = UART_IDLE;
dbgBufCount = 0;
// dbgHead = 0;
// dbgTail = 0;
// initialize UART
* (volatile unsigned char *)0x90 = 0; /* UBRR0H = 0 */
* (volatile unsigned char *)(0x09 + 0x20) = 15; /* UBRR0L = 15 */
/* UCSR0A中的U2X0 = 1,即傳輸速率倍速 */
* (volatile unsigned char *)(0x0B + 0x20) = 1 << 1;
/* UCSR0C中UCSZ1 = 1,UCSZ0 = 1,即傳送或接收字符長為8bit */
* (volatile unsigned char *)0x95 = (1 << 2) | (1 << 1);
/* UCSR0B中的RXCIE,TXCIE,RXEN和TXEN都置為1 */
* (volatile unsigned char *)(0x0A + 0x20) = (((1 << 7) | (1 << 6)) | (1 << 4)) | (1 << 3);
}
void uartDebug_txPacket(OSMACMsgPtr pMsg)
{
int i;
if (UARTState == UART_IDLE)
{
UARTState = UART_BUSY;
dbgBuf[0] = 0x8E;
dbgBuf[1] = 0x42;
dbgBuf[2] = pMsg->length;//
dbgBuf[3] = pMsg->type;
dbgBuf[4] = pMsg->toAddr;
dbgBuf[5] = pMsg->fromAddr;
dbgBuf[6] = pMsg->group;
///////這里需要注意的是i < pMsg->length-7語句,因?yàn)橐话銇碚fuart只是在sink節(jié)點(diǎn)才會(huì)用到,對于sink節(jié)點(diǎn)來說pMsg->length
//////包括了mac頭和數(shù)據(jù)部分的長度,但是如果想uart傳送本地產(chǎn)生的數(shù)據(jù),由于沒有經(jīng)過mac層,pMsg->length只是包含了
//////數(shù)據(jù)部分的長度如果使用pMsg->length-7就不對了
for (i = 0; i < pMsg->length-7; i++)
dbgBuf[7+i] = pMsg->data[i];
dbgBuf[pMsg->length] = pMsg->crc >> 8 & 0xFF;
dbgBuf[1+pMsg->length] = pMsg->crc & 0xFF;
dbgBuf[2+pMsg->length] = 0x7E;
* (volatile unsigned char *)(0x0C + 0x20) = dbgBuf[0]; // 將data寫入數(shù)據(jù)寄存器UDR0
* (volatile unsigned char *)(0x0B + 0x20) |= 1 << 6; // 向TXC位寫邏輯1,從而清零該位
dbgBufCount = 1;
dbgTail = 2 + pMsg->length;
appendByte = 0;
}
}
/***************************************************************************
__vector_20(void)
*功能描述:UART發(fā)送完成中斷
*參數(shù)說明:無
*返回值: 無
**************************************************************************/
void __attribute((signal)) __vector_20(void)
{
char byte;
if(dbgBufCount > 0 && dbgBufCount < dbgTail) {
if (appendByte == 0) // 不是發(fā)送附加字節(jié)
{
byte = dbgBuf[dbgBufCount++];
if (byte == 0x7E)
{
appendByte = 0x5E;
* (volatile unsigned char *)(0x0C + 0x20) = 0x7D; // 將data寫入數(shù)據(jù)寄存器UDR0
* (volatile unsigned char *)(0x0B + 0x20) |= 1 << 6; // 向TXC位寫邏輯1,從而清零該
return;
}
if (byte == 0x7D)
{
appendByte = 0x5D;
* (volatile unsigned char *)(0x0C + 0x20) = 0x7D; // 將data寫入數(shù)據(jù)寄存器UDR0
* (volatile unsigned char *)(0x0B + 0x20) |= 1 << 6; // 向TXC位寫邏輯1,從而清零該
return;
}
if (byte == 0x8E)
{
appendByte = 0x6E;
* (volatile unsigned char *)(0x0C + 0x20) = 0x8D; // 將data寫入數(shù)據(jù)寄存器UDR0
* (volatile unsigned char *)(0x0B + 0x20) |= 1 << 6; // 向TXC位寫邏輯1,從而清零該
return;
}
if (byte == 0x8D)
{
appendByte = 0x6D;
* (volatile unsigned char *)(0x0C + 0x20) = 0x8D; // 將data寫入數(shù)據(jù)寄存器UDR0
* (volatile unsigned char *)(0x0B + 0x20) |= 1 << 6; // 向TXC位寫邏輯1,從而清零該
return;
}
* (volatile unsigned char *)(0x0C + 0x20) = byte; // 將data寫入數(shù)據(jù)寄存器UDR0
* (volatile unsigned char *)(0x0B + 0x20) |= 1 << 6; // 向TXC位寫邏輯1,從而清零該位
}
else // 發(fā)送附加字節(jié)
{
* (volatile unsigned char *)(0x0C + 0x20) = appendByte; // 將data寫入數(shù)據(jù)寄存器UDR0
* (volatile unsigned char *)(0x0B + 0x20) |= 1 << 6; // 向TXC位寫邏輯1,從而清零該位
appendByte = 0;
}
} else if (dbgBufCount == dbgTail){
* (volatile unsigned char *)(0x0C + 0x20) = dbgBuf[dbgTail]; // 將data寫入數(shù)據(jù)寄存器UDR0
* (volatile unsigned char *)(0x0B + 0x20) |= 1 << 6; // 向TXC位寫邏輯1,從而清零該位
dbgBufCount = 0;
}
else{
UARTState = UART_IDLE;
}
}
/***************************************************************************
__vector_18(void)
*功能描述:UART接收完成中斷
*參數(shù)說明:無
*返回值: 無
**************************************************************************/
void __attribute((signal)) __vector_18(void)
{
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -