📄 uart.c
字号:
/*
* Purpose: Initialize ColdFire 5307
*
*/
#define IMMaddr 0x10000000
#define SDRAMaddr 0x00000000
#define SDRAMsize 0x00800000 //8M
#define SRAMaddr 0x00800000
#define SRAMsize (4 * 1024) //4K
#define EXT_SRAMaddr 0xFE400000
#define EXT_SRAMsize (128 * 1024) //128K
#define NULL 0x00
#include "..\NUCLEUS\mcf5307.h"
#include "..\NUCLEUS\nucleus.h"
#include ".\uart.h"
//Global Pointer
extern MCF5307_IMM *imm;
extern NU_SEMAPHORE Semaphore_0;
extern NU_SEMAPHORE Semaphore_1;
extern NU_SEMAPHORE Semaphore_2;
extern NU_SEMAPHORE Semaphore_3;
NU_HISR HISR_Control;
CHAR HISR_Stack[1024];
//Global for test
unsigned char RxData[4096];
unsigned char TxData[4096];
unsigned char UartBusy; //if 0x01 means trans. busy!!!!
//Global Variable Used For Uart1
unsigned char * Uart1_RxPointer; //Pointer to Rx Buf
unsigned long int Uart1_RxRequestLength; //how many Data to receive
unsigned char Uart1_RxCompleted; //0xff means receive ok
unsigned long int Uart1_RxReceivedLength; //how many have been received
unsigned char Uart1_RxEnable; //0xff to enable Receive
unsigned char * Uart1_TxPointer; //Pointer to Rx Buf
unsigned long int Uart1_TxRequestLength; //how many Data to receive
unsigned char Uart1_TxCompleted; //0xff means receive ok
unsigned long int Uart1_TxTransmitLength; //how many have been received
unsigned char Uart1_TxEnable; //0xff to enable Transmit
unsigned char ResultOfDisplayFpga;
void Uart1_ReInit();
void Uart1_ReadyToReceive();
void Uart1_ThreadWaittingForData();
void Uart1_ThreadWaittingForSendCompleted();
void Uart1_ReadyToTransmit();
void Uart1_ThreadWaittingForData(void);
void Uart1_ThreadWaittingForSendCompleted(void);
void WaitLastByte(void);
void PPrintf(char * Index);
void Printf(char * Index);
unsigned long GetKey(void);
VOID Example_LISR(INT vector);
void RegUartLISR(void);
VOID Example_HISR(void);
void RegUartHISR(void);
void Uart1_ReInit( MCF5307_IMM *imm)
{
imm->uart1.UCR1=0x10; /*to reset the Mode Register Pointer*/
imm->uart1.UCR1=0x20; /*to reset the Receiver*/
imm->uart1.UCR1=0x30; /*to reset the Transimitter*/
imm->uart1.UCR1=0x40; /*to reset the Error Stauts*/
imm->uart1.UCR1=0x50; /*to reset the Break Change Interrupt*/
imm->uart1.UMR11_UMR21=0x13; //no Parity , 8bits
imm->uart1.UMR11_UMR21=0x07; //mode Normal,one stop bit
imm->uart1.USR1_UCSR1=0xdd; //use Timer as Clock
imm->uart1.UISR1_UIMR1=0x00; //Disenable Interrupt
imm->uart1.UBG11=0x00;
imm->uart1.UBG21=0x49; //19200; //0x92; //bus Freq=45MHz,9600 bps
imm->uart1.UCR1=0x10; /*to reset the Mode Register Pointer*/
imm->uart1.UCR1=0x20; /*to reset the Receiver*/
imm->uart1.UCR1=0x30; /*to reset the Transimitter*/
imm->uart1.UCR1=0x05; /*to Enable Receiver & Transmitter*/
imm->uart1.UISR1_UIMR1=0x02; //Enable Interrupt
imm->sim.ICR4=0x84; //enable auto vect. ,level 1 ,to vect NUMBER 0X19
//other parameter
Uart1_TxCompleted=0x00;
Uart1_RxCompleted=0x00;
UartBusy=0x00;
Uart1_RxPointer=RxData;
Uart1_RxRequestLength=0x01;
Uart1_ReadyToReceive();
RegUartHISR();
RegUartLISR();
}
//extern "C"
void Interrupt_UART1()
{
unsigned char Status;
unsigned char DataReceived;
unsigned char DataTransmit;
Status=imm->uart1.UISR1_UIMR1;
//receive subrouting
while( ( (Status&0x02)==0x02)&&(Uart1_RxReceivedLength!=Uart1_RxRequestLength)&&(Uart1_RxEnable==0xff) )
{
DataReceived=imm->uart1.URB1_UTB1;
*Uart1_RxPointer=DataReceived;
Uart1_RxReceivedLength++;
Uart1_RxPointer++;
Status=imm->uart1.UISR1_UIMR1;
if( (Uart1_RxReceivedLength==Uart1_RxRequestLength)||(DataReceived==0x0d) ) //received 0x0d
Uart1_RxCompleted=0xff;
}
//transmit subrouting
if( ((Status&0x01)==0x01)&&(Uart1_TxTransmitLength!=Uart1_TxRequestLength)&&(Uart1_TxEnable==0xff) )
{
DataTransmit=*Uart1_TxPointer;
imm->uart1.URB1_UTB1=DataTransmit;
Uart1_TxPointer++;
Uart1_TxTransmitLength++;
if(Uart1_TxTransmitLength==(Uart1_TxRequestLength))
{
Uart1_TxCompleted=0xff;
imm->uart1.UISR1_UIMR1=0x02; //no longer enable TxRdy interrupt
}
}
}
void Uart1_ReadyToReceive()
{
Uart1_RxCompleted=0x00;
Uart1_RxReceivedLength=0x00;
imm->uart1.UCR1=0x20; /*to reset the Receiver*/
imm->uart1.UCR1=0x40; /*to reset the Error Stauts*/
imm->uart1.UCR1=0x50; /*to reset the Break Change Interrupt*/
imm->uart1.UCR1=0x05; /*to Enable Receiver & Transmitter*/
Uart1_RxEnable=0xff;
// Uart1_TxEnable=0x00;
}
void Uart1_ReadyToTransmit()
{
Uart1_TxCompleted=0x00;
Uart1_TxTransmitLength=0x00;
imm->uart1.UCR1=0x30; /*to reset the Transmit*/
imm->uart1.UCR1=0x40; /*to reset the Error Stauts*/
imm->uart1.UCR1=0x50; /*to reset the Break Change Interrupt*/
imm->uart1.UCR1=0x05; /*to Enable Receiver & Transmitter*/
imm->uart1.UISR1_UIMR1=0x03; //enable TxRdy interrupt
Uart1_TxEnable=0xff;
}
void Uart1_ThreadWaittingForData()
{
while(Uart1_RxCompleted!=0xff)
{
NU_Sleep(10);
}
}
void Uart1_ThreadWaittingForSendCompleted()
{
unsigned char Sta;
while( Uart1_TxCompleted!=0xff) //wait for the last bytes to be transfer
{
NU_Sleep(10);
}
Sta=imm->uart1.USR1_UCSR1;
while( (Sta&0x08)==0x00 ) //TxEMP
{
Sta=imm->uart1.USR1_UCSR1; //wait for the last byte transfered ok!
NU_Sleep(10);
}
}
void WaitLastByte(void)
{
unsigned char Sta;
Sta=imm->uart1.USR1_UCSR1;
while( (Sta&0x08)==0x00 ) //TxEMP
{
Sta=imm->uart1.USR1_UCSR1; //wait for the last byte transfered ok!
NU_Sleep(10);
}
}
void PPrintf(char * Index)
{
int i;
for(i=0;Index[i]!=NULL;i++)
{
TxData[i]=Index[i];
}
NU_Reset_Semaphore(&Semaphore_0,0);
Uart1_TxPointer=TxData;
Uart1_TxRequestLength=i;
Uart1_ReadyToTransmit();
NU_Obtain_Semaphore(&Semaphore_0,NU_SUSPEND);
//WaitLastByte();
Uart1_ThreadWaittingForSendCompleted();
Uart1_TxCompleted=0x00;
}
void Printf(char * Index) //Printf reentrancy!
{
NU_Obtain_Semaphore(&Semaphore_1,NU_SUSPEND);
// NU_Reset_Semaphore(&Semaphore_1,0);
while(UartBusy!=0x00) //this code not very good!
{
NU_Sleep(20);
NU_Sleep(20);
}
UartBusy=0xff;
PPrintf(Index);
UartBusy=0x00;
NU_Release_Semaphore(&Semaphore_1);
}
unsigned long GetKey(void)
{
NU_Reset_Semaphore(&Semaphore_3,0);
Uart1_RxPointer=RxData;
Uart1_RxRequestLength=300;
Uart1_ReadyToReceive();
NU_Obtain_Semaphore(&Semaphore_3,NU_SUSPEND);
Uart1_RxCompleted=0x00;
return Uart1_RxReceivedLength;
}
VOID Example_LISR(INT vector)
{
Interrupt_UART1();
NU_Activate_HISR(&HISR_Control);
}
void RegUartLISR(void)
{
VOID (*old_lisr)(INT);
/* Register the LISR with vector 10. The previously registered
LISR is returned in old_lisr. */
NU_Register_LISR(0x19, Example_LISR, &old_lisr);
}
VOID Example_HISR(void)
{
if(Uart1_TxCompleted==0xff)
NU_Release_Semaphore(&Semaphore_0); //notify the Printf function
if(Uart1_RxCompleted==0xff)
NU_Release_Semaphore(&Semaphore_3);
}
void RegUartHISR(void)
{
STATUS status;
void (*old_lisr)(INT);
status=NU_Create_HISR(&HISR_Control, "EXMPHISR",Example_HISR, 2, HISR_Stack, 500);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -