#include "hw_config.h"
#include "uart.h"
ISR(USART0_RX_vect)
{
u16 data_len;
s8 data[1];
data[0] = UDR0;
// echo service
usart0_putc(data[0]);
}
void bsp_usart0_init(u32 dwFOSC, enumBaudrate nBaudRate, enumDataBits nDataBits, enumParity nParity, enumStopBits nStopBits, byte bDoubleSpeed )
{
#if 1
u16 wUBRR;
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
//- Warning - UBBRnH ¼³Á¤ ÈÄ UBBRnL¸¦ ³ªÁß¿¡ Write ÇØ¾ß ÇÑ´Ù.
if( bDoubleSpeed == TRUE )
{
// UCSRA(USART Control and Status Register A) Initial
// U2X: Double the USART Transmission Speed
sbi(UCSR0A,U2X0);
wUBRR = ((float)dwFOSC/(8. * (float)nBaudRate) - 1) * 1000.;
}
else
{
// UCSRA(USART Control and Status Register A) Initial
// U2X: Double the USART Transmission Speed
cbi(UCSR0A,U2X0);
wUBRR = ((float)dwFOSC/(16. * (float)nBaudRate) - 1) * 1000.;
}
// ¹Ý¿Ã¸²
if( wUBRR % 10000 >= 500 )
wUBRR = (wUBRR + 500)/1000;
else
wUBRR = wUBRR/1000;
// RX Complete Interrupt ON
// TX Complete Interrupt OFF
// USART Data Register Empty Interrupt OFF
// Receiver Enable
// Transmitter Enable
// Character Size UCSZ2 = 0
UCSR0B = (1 << TXEN0 ) | (1 << RXEN0 ); // | (1<<RXCIE);
/*-----------------------------------
UART UCSZ Bits Settings
-------------------------------------
UCSZ2 UCSZ1 UCSZ0 Character Size
=====================================
0 0 0 5-bit
0 0 1 6-bit
0 1 0 7-bit
0 1 1 8-bit
1 0 0 Reserved
1 0 1 Reserved
1 1 0 Reserved
1 1 1 9-bit
------------------------------------*/
// USART Mode = Asynchronous
// Parity Mode = Disabled(0,0)
// Stop Bit Select = 1-bit
// Character Size UCSZ = 8Bit(0, 1, 1)
// Clock Polarity = Not Setting(synchronous mode only)
if( nDataBits == EData8 )
{
// default °ªÀÌ 8Bit ·Î ¼³Á¤ µÇ¾î ÀÖ´Ù.
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
}
// UCSRC(USART Control and Status Register C) Initial
/*-----------------------------------
UART UPM Bits Settings
-------------------------------------
UPM1 UPM0 Parity Mode
=====================================
0 0 Disabled
0 1 Reserved
1 0 Enabled, Even Parity
1 1 Enabled, Odd Parity
------------------------------------*/
if( nParity == EParNone )
{
cbi(UCSR0C,UPM01); // UART UPM Bits Setting Disabled
cbi(UCSR0C,UPM00); // UART UPM Bits Setting Disabled
}
/*-----------------------------------
UART USBS Bit Settings
-------------------------------------
USBS UStop Bit(s)
=====================================
0 1-bit
1 2-bit
------------------------------------*/
if( nStopBits == EStop1 )
cbi(UCSR0C,USBS0); // USBS Bit Settings 1-bit
else
sbi(UCSR0C,USBS0); // USBS Bit Settings 2-bit
#ifdef USE_AVR_STUDIO
UBRR0L = (unsigned char)wUBRR;
UBRR0H = (unsigned char)wUBRR >> 8;
#else
// ICCAVRÀºbaudrate°¡ ¼ö½ÄÀ¸·Î Àß ¾ÈµÇ³×¿ä.
// ³ªÁß¿¡ º¸¿Ï ÇÊ¿ä
if( F_CPU == 16000000UL )
{
if( nBaudRate == EBaud38400 )
{
UBRR0L= 25;
UBRR0H= 0x00;
}
else if( nBaudRate == EBaud115200 )
{
UBRR0L= 8;
UBRR0H= 0x00;
}
}
else if( F_CPU == 7372800UL )
{
if( nBaudRate == EBaud38400 )
{
UBRR0L= 11;
UBRR0H= 0x00;
}
else if( nBaudRate == EBaud115200 )
{
UBRR0L= 3;
UBRR0H= 0x00;
}
}
else if( F_CPU == 8000000UL )
{
if( nBaudRate == EBaud38400 )
{
UBRR0L= 12;
UBRR0H= 0x00;
}
else if( nBaudRate == EBaud115200 )
{
UBRR0L= 3;
UBRR0H= 0x00;
}
}
#endif
#endif
}
void usart0_putc(char ch)
{
while (((UCSR0A>>UDRE0)&0x01) == 0) ; // UDRE, data register empty
UDR0 = ch;
}
void usart0_puts(char *data)
{
int len, count;
len = strlen(data);
for (count = 0; count < len; count++)
usart0_putc(*(data+count));
}
void bsp_usart0_gpio_init()
{
// RXD¸¦ ÀÔ·ÂÀ¸·Î
cbi(USART0_RX_DDR, USART0_RX_PIN_NO);
// TXD¸¦ Ãâ·ÂÀ¸·Î
sbi(USART0_TX_DDR, USART0_TX_PIN_NO);
}
void bsp_usart0_interrupt_enable()
{
sbi(UCSR0B, RXCIE0);
}
void main(void)
{
CLI(); // all interrupt disable
bsp_usart0_gpio_init();
bsp_usart0_init(F_CPU, EBaud115200, EData8, EParNone, EStop1, FALSE );
bsp_usart0_interrupt_enable();
usart0_puts("\r\nUart0 echo service started.\r\n");
SEI(); // all interrupt enable
while(1)
{
}
}
|