initial revision
[ucgine.git] / examples / test-uart / uart.c
1 /*
2  * Copyright (c) 2015, Olivier MATZ <zer0@droids-corp.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #if defined(__ARM_EABI__)
29 #include "stm32f4xx.h"
30 #elif defined(__AVR__)
31 #include <avr/io.h>
32 #include <avr/interrupt.h>
33 #include <avr/pgmspace.h>
34 #include <util/delay.h>
35 #endif
36
37 #include <stdio.h>
38 #include <errno.h>
39 #include <ctype.h>
40
41 #include <ucg_irq.h>
42 #include <ucg_cirbuf.h>
43 #include <ucg_uart.h>
44
45 #include "uart.h"
46
47 /* rx & tx buffers */
48 static char rx_buf[64];
49 static struct ucg_cirbuf rx_cirbuf;
50 static char tx_buf[64];
51 static struct ucg_cirbuf tx_cirbuf;
52 /* generic uart struct */
53 struct ucg_uart main_uart;
54
55 #if defined(__ARM_EABI__)
56
57 #include <ucg_gloss_chardev.h>
58 #include <ucg_stm32_uart.h>
59
60 static struct ucg_stm32_uart stm32_uart_data = {
61         .uart = USART2,
62         .rcc_uart = RCC_APB1Periph_USART2,
63         .rcc_gpio = RCC_AHB1Periph_GPIOA,
64         .gpio = GPIOA,
65         .gpio_af = GPIO_AF_USART2,
66         .gpio_pins = GPIO_Pin_2 | GPIO_Pin_3,
67         .gpio_speed = GPIO_Speed_25MHz,
68         .irq = USART2_IRQn,
69         .irq_preempt_prio = 2,
70         .irq_sub_prio = 0,
71 };
72
73 static _ssize_t
74 uart_write_r(__attribute__((unused)) struct _reent *r,
75         __attribute__((unused)) int fd,
76         const void *ptr, size_t len)
77 {
78         size_t i;
79         const char *buf = ptr;
80
81         for (i = 0; i < len; i++)
82                 ucg_uart_send(&main_uart, buf[i], WAIT);
83
84         return len;
85 }
86
87 static _ssize_t
88 uart_read_r(__attribute__((unused)) struct _reent *r,
89         __attribute__((unused)) int fd,
90         void *ptr, size_t len)
91 {
92         int c;
93         char *buf = ptr;
94
95         c = ucg_uart_recv(&main_uart, NOWAIT);
96         if (c < 0) {
97                 r->_errno = EAGAIN;
98                 return 0;
99         }
100         buf[0] = c;
101         return 1;
102 }
103
104 void USART2_IRQHandler(void)
105 {
106         if ((USART2->SR & USART_FLAG_TXE))
107                 ucg_uart_tx_intr(&main_uart);
108         if ((USART2->SR & USART_FLAG_RXNE))
109                 ucg_uart_rx_intr(&main_uart);
110 }
111
112 struct ucg_chardev uart_stdin_dev = {
113         .name = "stdin",
114         .open_r = NULL,
115         .close_r = NULL,
116         .read_r = uart_read_r,
117         .write_r = NULL,
118 };
119
120 struct ucg_chardev uart_stdout_dev = {
121         .name = "stdout",
122         .open_r = NULL,
123         .close_r = NULL,
124         .read_r = NULL,
125         .write_r = uart_write_r,
126 };
127
128 struct ucg_chardev uart_stderr_dev = {
129         .name = "stderr",
130         .open_r = NULL,
131         .close_r = NULL,
132         .read_r = NULL,
133         .write_r = uart_write_r,
134 };
135
136 static void uart_register_stdio(void)
137 {
138         ucg_chardev_register(&uart_stdin_dev);
139         ucg_chardev_register(&uart_stdout_dev);
140         ucg_chardev_register(&uart_stderr_dev);
141
142         /* Disable buffering */
143         setbuf(stdin, NULL);
144         setbuf(stdout, NULL);
145         setbuf(stderr, NULL);
146 }
147
148 #elif defined(__AVR__)
149
150 #include <ucg_avr_uart.h>
151 /* avr-specific uart struct */
152 static struct ucg_avr_uart avr_uart_data = {
153         .reg_udr = &UDR0,
154         .reg_ucsra = &UCSR0A,
155         .reg_ucsrb = &UCSR0B,
156         .reg_ucsrc = &UCSR0C,
157         .reg_ubrrl = &UBRR0L,
158         .reg_ubrrh = &UBRR0H,
159         .bit_udre = UDRE0,
160         .bit_rxc = RXC0,
161         .bit_udrie = UDRIE0,
162         .bit_rxen = RXEN0,
163         .bit_txen = TXEN0,
164         .bit_rxcie = RXCIE0,
165         .bit_u2x = U2X0,
166 };
167
168 /* send on stdout */
169 static int std_send(char c, FILE *f)
170 {
171         (void)f;
172         ucg_uart_send(&main_uart, c, WAIT);
173         return 0;
174 }
175
176 /* recv on stdin */
177 static int std_recv(FILE *f)
178 {
179         int16_t c;
180
181         (void)f;
182         c = ucg_uart_recv(&main_uart, NOWAIT);
183         if (c < 0)
184                 return _FDEV_EOF;
185
186         return c;
187 }
188
189 SIGNAL(USART_RX_vect)
190 {
191         ucg_uart_rx_intr(&main_uart);
192 }
193
194 SIGNAL(USART_UDRE_vect)
195 {
196         ucg_uart_tx_intr(&main_uart);
197 }
198 #endif
199
200 int uart_init(void)
201 {
202         int ret;
203         struct ucg_uart_config conf;
204         const void *uart_ops;
205         void *uart_data;
206
207 #if defined(__ARM_EABI__)
208         uart_ops = &stm32_uart_ops;
209         uart_data = &stm32_uart_data;
210 #else
211         uart_ops = &avr_uart_ops;
212         uart_data = &avr_uart_data;
213 #endif
214
215         ret = ucg_uart_init(&main_uart, uart_ops, uart_data,
216                 &rx_cirbuf, rx_buf, sizeof(rx_buf),
217                 &tx_cirbuf, tx_buf, sizeof(tx_buf));
218         if (ret < 0)
219                 return ret;
220
221         ucg_uart_getconf(&main_uart, &conf);
222         conf.baudrate = 57600;
223         ret = ucg_uart_setconf(&main_uart, &conf);
224         if (ret < 0)
225                 return ret;
226
227 #if defined(__ARM_EABI__)
228         uart_register_stdio();
229 #elif defined(__AVR__)
230         fdevopen(std_send, std_recv);
231 #endif
232         return ret;
233 }