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

📄 uart.c

📁 Basic Application Loading over the Serial Interface for the DaVinci TMS320DM644x
💻 C
字号:
/* -------------------------------------------------------------------------- *
 * uart.c - Implementation of uart setup and use.                             *
 * -------------------------------------------------------------------------- */

#include "dm644x.h"
#include "uart.h"

// Send specified number of bytes 
unsigned int UARTSendData(unsigned char* seq)
{
	unsigned int i, numBytes, status = 0;
	unsigned int timerStatus = 1;
	
	numBytes = GetStringLen(seq);
	
	for(i=0;i<numBytes;i++) {
		/* Enable Timer one time */
		TIMER0Start();
		do{
			status = (UART0->LSR)&(0x20);
			timerStatus = TIMER0Status();
		} while (!status && timerStatus);

		if(timerStatus == 0)
			return E_TIMEOUT;
		
		// Send byte 
		(UART0->THR) = seq[i];
	}
	return E_PASS;
}

unsigned int UARTSendInt(unsigned int value)
{
	char seq[9];
	unsigned int i,shift,temp;

	for( i = 0; i < 8; i++)
	{
		shift = ((7-i)*4);
		temp = ((value>>shift) & (0x0000000F));
		if (temp > 9)
		{
			temp = temp + 55;
		}
		else
		{
			temp = temp + 48;
		}
		seq[i] = temp;	
	}
	seq[8] = 0;
	return UARTSendData(seq);
}

// Get string length by finding null terminating char
unsigned int GetStringLen(unsigned char* seq)
{
	unsigned int i = 0;
	while ((seq[i] != 0) && (i<MAXSTRLEN)){ i++;}
	if (i == MAXSTRLEN)
		return 0;
	else
		return i;
}

// Receive data from UART 
unsigned int UARTRecvData(unsigned int numBytes, unsigned char* seq)
{
	unsigned int i, status = 0;
	unsigned int timerStatus = 1;
	
	for(i=0;i<numBytes;i++) {
		/* Enable Timer one time */
		TIMER0Start();
		do{
			status = (UART0->LSR)&(0x01);
			timerStatus = TIMER0Status();
		} while (!status && timerStatus);

		if(timerStatus == 0)
			return E_TIMEOUT;
		
		// Recv byte 
		seq[i] = UART0->RBR;

		// Echo byte back 
		UART0->THR = seq[i];
		do{
			status = (UART0->LSR)&(0x20);
		} while (!status);
	}
	return E_PASS;
}

⌨️ 快捷键说明

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