]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0+ | |
5 | */ | |
6 | ||
7 | #include <avr/io.h> | |
8 | #include <avr/interrupt.h> | |
9 | #include <util/atomic.h> | |
10 | #include <errno.h> | |
11 | #include <stdio.h> | |
12 | ||
13 | #include "ring.h" | |
14 | #include "serial.h" | |
15 | ||
16 | ||
17 | static int _write(char c, FILE *stream); | |
18 | static FILE mystdout = FDEV_SETUP_STREAM(_write, | |
19 | NULL, _FDEV_SETUP_WRITE); | |
20 | ||
21 | ||
22 | ||
23 | #define BUFFER_SIZE 128 | |
24 | ||
25 | #if ((BUFFER_SIZE-1) & BUFFER_SIZE) | |
26 | # error: BUFFER_SIZE not power of 2 | |
27 | #endif | |
28 | ||
29 | #if ((BUFFER_SIZE) > 256) | |
30 | # error: BUFFER_SIZE | |
31 | #endif | |
32 | ||
33 | struct ring rx_ring; | |
34 | struct ring tx_ring; | |
35 | uint8_t rx_ring_buffer[BUFFER_SIZE]; | |
36 | uint8_t tx_ring_buffer[BUFFER_SIZE]; | |
37 | ||
38 | ||
39 | ||
40 | /* Initialize UART */ | |
41 | ||
42 | void usart0_setup(unsigned long baud) { | |
43 | ||
44 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { | |
45 | ||
46 | PRR0 &= ~_BV(PRUSART0); | |
47 | UCSR0B = 0; | |
48 | UCSR0A = 0; | |
49 | UBRR0 = (F_CPU + (baud/2) + 8) / baud / 16 - 1; | |
50 | UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); | |
51 | UCSR0C = 3 << UCSZ00; | |
52 | ||
53 | /* Initialize ring buffers. */ | |
54 | ring_init(&rx_ring, rx_ring_buffer, BUFFER_SIZE); | |
55 | ring_init(&tx_ring, tx_ring_buffer, BUFFER_SIZE); | |
56 | }; | |
57 | } | |
58 | ||
59 | /*--------------------------------------------------------------------------*/ | |
60 | ||
61 | /* UART RXC interrupt */ | |
62 | ||
63 | ISR(USART0_RX_vect) | |
64 | { | |
65 | uint8_t d; | |
66 | ||
67 | d = UDR0; | |
68 | ring_write_ch(&rx_ring, d); | |
69 | } | |
70 | ||
71 | /* UART UDRE interrupt */ | |
72 | ||
73 | ISR(USART0_UDRE_vect) | |
74 | { | |
75 | int d = ring_read_ch(&tx_ring); | |
76 | ||
77 | if (d < 0) { | |
78 | /* Disable TX empty interrupt. */ | |
79 | UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); | |
80 | } else { | |
81 | UDR0 = d; | |
82 | } | |
83 | } | |
84 | ||
85 | /*--------------------------------------------------------------------------*/ | |
86 | ||
87 | void serial_setup(unsigned long baud) | |
88 | { | |
89 | stdout = &mystdout; | |
90 | usart0_setup(baud); | |
91 | } | |
92 | ||
93 | /*--------------------------------------------------------------------------*/ | |
94 | ||
95 | int _write(char c, FILE *stream) | |
96 | { | |
97 | (void) stream; | |
98 | ||
99 | if (c == '\n') | |
100 | serial_putc('\r'); | |
101 | serial_putc(c); | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
106 | int serial_getc(void) | |
107 | { | |
108 | return ring_read_ch(&rx_ring); | |
109 | } | |
110 | ||
111 | void serial_putc(char data) | |
112 | { | |
113 | while (ring_write_ch(&tx_ring, data) < 0) | |
114 | ; | |
115 | ||
116 | /* Enable the TXE interrupt. */ | |
117 | UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0) | _BV(UDRIE0); | |
118 | } | |
119 | ||
120 | uint_fast8_t serial_tstc(void) | |
121 | { | |
122 | return !ring_is_empty(&rx_ring); | |
123 | } |