ser_simple_avr.c
Go to the documentation of this file.00001
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 #include "ser_simple_avr.h"
00069
00070 #include <cfg/compiler.h>
00071 #include <appconfig.h>
00072 #include <cfg/macros.h>
00073 #include "hw_cpu.h"
00074
00075 #include <avr/io.h>
00076
00082 int _ser_putchar(int c)
00083 {
00084
00085 UCSR0B &= ~BV(RXEN);
00086
00087 UCSR0B |= BV(TXEN);
00088
00089 UDR0 = c;
00090
00091 while (!(UCSR0A & BV(TXC))) {}
00092
00093 UCSR0B &= ~BV(TXEN);
00094
00095 UCSR0B |= BV(RXEN);
00096
00097 UCSR0A |= BV(TXC);
00098 return c;
00099 }
00100
00101
00109 int _ser_getchar(void)
00110 {
00111
00112 while (!(UCSR0A & BV(RXC))) {}
00113 return UDR0;
00114
00115 }
00116
00117
00123 int _ser_getchar_nowait(void)
00124 {
00125 if (!(UCSR0A & BV(RXC))) return EOF;
00126 else return UDR0;
00127 }
00128
00129 void _ser_settimeouts(void)
00130 {
00131 }
00132
00136 void _ser_setbaudrate(unsigned long rate)
00137 {
00138
00139 uint16_t period = DIV_ROUND(CLOCK_FREQ / 16UL, rate) - 1;
00140
00141 UBRR0H = (period) >> 8;
00142 UBRR0L = (period);
00143 }
00144
00148 int _ser_print(const char *s)
00149 {
00150 while(*s) _ser_putchar(*s++);
00151 return 0;
00152 }
00153
00154
00155 void _ser_setparity(int parity)
00156 {
00157
00158 UCSR0C |= (UCSR0C & ~(BV(UPM1) | BV(UPM0))) | (parity << UPM0);
00159 }
00160
00164 void _ser_purge(void)
00165 {
00166 while (_ser_getchar_nowait() != EOF) {}
00167 }
00168
00172 struct Serial * _ser_open(void)
00173 {
00174
00175
00176
00177
00178 DDRE &= ~(BV(PE0)|BV(PE1));
00179 PORTE &= ~BV(PE0);
00180 PORTE |= BV(PE1);
00181
00182 UCSR0B = BV(RXEN);
00183 return NULL;
00184 }
00185
00186
00190 void _ser_close(void)
00191 {
00192
00193 UCSR0B &= ~(BV(RXEN) | BV(TXEN));
00194 }