⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uartdebug.c

📁 基于cc1000和avr128处理器的指定路由多跳网络的试验程序
💻 C
字号:
/*
****************************************************************************
*              宁波中科集成电路设计中心  版权所有 Copyright 2005
*						http:\\www.nbicc.com
*文件名:  fun.h
*程序员:  夏鹏		xpsonny@nbicc.com
*主要内容:常用函数
*
*如有问题或BUG,请登录www.wsn.net.cn 上的BBS到相关版面提问!
*
****************************************************************************
*/

#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;
 
   // 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语句,因为一般来说uart只是在sink节点才会用到,对于sink节点来说pMsg->length
		//////包括了mac头和数据部分的长度,但是如果想uart传送本地产生的数据,由于没有经过mac层,pMsg->length只是包含了
		//////数据部分的长度如果使用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写入数据寄存器UDR0 
	    * (volatile unsigned char *)(0x0B + 0x20) |= 1 << 6;	// 向TXC位写逻辑1,从而清零该位
		dbgBufCount = 1;
		dbgTail = 2 + pMsg->length;
		appendByte = 0;
	}
}

/***************************************************************************
                      __vector_20(void)
*功能描述:UART发送完成中断
*参数说明:无
*返回值:  无
**************************************************************************/

void __attribute((signal))   __vector_20(void)
{
	char byte;
    if(dbgBufCount > 0 && dbgBufCount < dbgTail) {
		if (appendByte == 0) // 不是发送附加字节
		{
			byte = dbgBuf[dbgBufCount++];
			if (byte == 0x7E)
			{
				appendByte = 0x5E;
				* (volatile unsigned char *)(0x0C + 0x20) = 0x7D;		// 将data写入数据寄存器UDR0 
				* (volatile unsigned char *)(0x0B + 0x20) |= 1 << 6;	// 向TXC位写逻辑1,从而清零该
				return;
			}
			if (byte == 0x7D)
			{
				appendByte = 0x5D;
				* (volatile unsigned char *)(0x0C + 0x20) = 0x7D;		// 将data写入数据寄存器UDR0 
				* (volatile unsigned char *)(0x0B + 0x20) |= 1 << 6;	// 向TXC位写逻辑1,从而清零该
				return;
			}
			if (byte == 0x8E)
			{
			    appendByte = 0x6E;
				* (volatile unsigned char *)(0x0C + 0x20) = 0x8D;		// 将data写入数据寄存器UDR0 
				* (volatile unsigned char *)(0x0B + 0x20) |= 1 << 6;	// 向TXC位写逻辑1,从而清零该
				return;
			}
			if (byte == 0x8D)
			{
			    appendByte = 0x6D;
				* (volatile unsigned char *)(0x0C + 0x20) = 0x8D;		// 将data写入数据寄存器UDR0 
				* (volatile unsigned char *)(0x0B + 0x20) |= 1 << 6;	// 向TXC位写逻辑1,从而清零该
				return;
			}
			* (volatile unsigned char *)(0x0C + 0x20) = byte;			// 将data写入数据寄存器UDR0 
			* (volatile unsigned char *)(0x0B + 0x20) |= 1 << 6;		// 向TXC位写逻辑1,从而清零该位 
		}
		else				// 发送附加字节
		{
			* (volatile unsigned char *)(0x0C + 0x20) = appendByte;		// 将data写入数据寄存器UDR0 
			* (volatile unsigned char *)(0x0B + 0x20) |= 1 << 6;		// 向TXC位写逻辑1,从而清零该位 
			appendByte = 0;
		}
	 } else if (dbgBufCount == dbgTail){
		* (volatile unsigned char *)(0x0C + 0x20) = dbgBuf[dbgTail];	// 将data写入数据寄存器UDR0 
		* (volatile unsigned char *)(0x0B + 0x20) |= 1 << 6;			// 向TXC位写逻辑1,从而清零该位 
		dbgBufCount = 0;
	 }
	 else{
      UARTState = UART_IDLE;
     }
}

/***************************************************************************
                      __vector_18(void)

*功能描述:UART接收完成中断
*参数说明:无
*返回值:  无
**************************************************************************/

void __attribute((signal))   __vector_18(void)
{
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -