📄 simple_uart_isr.c
字号:
//
#include "excalibur.h"
#include "buffer.h" // defines RXBUFSIZE and TXBUFSIZE
//Global variables
unsigned char RxBuf[RXBUFSIZE]; // the receiver buffer.
int RxHead = 0; // the circular buffer index
int RxTail = 0;
unsigned char TxBuf[TXBUFSIZE]; // the transmit buffer.
int TxHead = 0; // the circular buffer index
int TxTail = 0;
unsigned char _getchar();
void serial_simple_ISR(int context);
int _putchar(int in_char);
int main(void)
{
int context = 0;
nr_installuserisr(na_uart1_irq, serial_simple_ISR, context); // install UART ISR
na_uart1->np_uartcontrol = np_uartcontrol_irrdy_mask; // enable rx interrupts
while(1)
{
_putchar( (int)(_getchar()) );
}
return 0;
}
/************************************************************************/
void serial_simple_ISR(int data)
{
// need to retrieve the status register
int sr = na_uart1 ->np_uartstatus;
if(sr & np_uartstatus_rrdy_mask)
{ // something ready for the receiver.
// put new char into buffer from UART
RxBuf[RxHead] = na_uart1->np_uartrxdata;
// clear the errors and interrupts
na_uart1->np_uartstatus=0;
if ((++RxHead) > (RXBUFSIZE-1))
{
RxHead = 0;
}
} // end of receiver
if(sr & np_uartstatus_trdy_mask)
{ // transmitter ready
if(na_uart1->np_uartcontrol & np_uartcontrol_itrdy_mask)
{ // ready to handle transmitter interrupts
if (TxTail != TxHead)
{
na_uart1->np_uarttxdata = TxBuf[TxTail]; // send a byte of data
if (++TxTail > (TXBUFSIZE -1)) // check for wrap around.
TxTail = 0;
}
else
{ /* tx buffer empty, disable iTRDY. Since this ISR only deals with iTRDY
and iRRDY, we will accomplish this by enabling just iRRDY. */
na_uart1->np_uartcontrol = np_uartcontrol_irrdy_mask;
}
}
} // end of transmitter
} // end of ISR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -