2 * Copyright (c) 2015, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #if defined(__ARM_EABI__)
29 #include "stm32f4xx.h"
30 #elif defined(__AVR__)
32 #include <avr/interrupt.h>
33 #include <avr/pgmspace.h>
34 #include <util/delay.h>
42 #include <ucg_cirbuf.h>
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;
55 #if defined(__ARM_EABI__)
57 #include <ucg_gloss_chardev.h>
58 #include <ucg_stm32_uart.h>
60 static struct ucg_stm32_uart stm32_uart_data = {
62 .rcc_uart = RCC_APB1Periph_USART2,
63 .rcc_gpio = RCC_AHB1Periph_GPIOA,
65 .gpio_af = GPIO_AF_USART2,
66 .gpio_pins = GPIO_Pin_2 | GPIO_Pin_3,
67 .gpio_speed = GPIO_Speed_25MHz,
69 .irq_preempt_prio = 2,
74 uart_write_r(__attribute__((unused)) struct _reent *r,
75 __attribute__((unused)) int fd,
76 const void *ptr, size_t len)
79 const char *buf = ptr;
81 for (i = 0; i < len; i++)
82 ucg_uart_send(&main_uart, buf[i], WAIT);
88 uart_read_r(__attribute__((unused)) struct _reent *r,
89 __attribute__((unused)) int fd,
90 void *ptr, size_t len)
95 c = ucg_uart_recv(&main_uart, NOWAIT);
104 void USART2_IRQHandler(void)
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);
112 struct ucg_chardev uart_stdin_dev = {
116 .read_r = uart_read_r,
120 struct ucg_chardev uart_stdout_dev = {
125 .write_r = uart_write_r,
128 struct ucg_chardev uart_stderr_dev = {
133 .write_r = uart_write_r,
136 static void uart_register_stdio(void)
138 ucg_chardev_register(&uart_stdin_dev);
139 ucg_chardev_register(&uart_stdout_dev);
140 ucg_chardev_register(&uart_stderr_dev);
142 /* Disable buffering */
144 setbuf(stdout, NULL);
145 setbuf(stderr, NULL);
148 #elif defined(__AVR__)
150 #include <ucg_avr_uart.h>
151 /* avr-specific uart struct */
152 static struct ucg_avr_uart avr_uart_data = {
154 .reg_ucsra = &UCSR0A,
155 .reg_ucsrb = &UCSR0B,
156 .reg_ucsrc = &UCSR0C,
157 .reg_ubrrl = &UBRR0L,
158 .reg_ubrrh = &UBRR0H,
169 static int std_send(char c, FILE *f)
172 ucg_uart_send(&main_uart, c, WAIT);
177 static int std_recv(FILE *f)
182 c = ucg_uart_recv(&main_uart, NOWAIT);
189 SIGNAL(USART_RX_vect)
191 ucg_uart_rx_intr(&main_uart);
194 SIGNAL(USART_UDRE_vect)
196 ucg_uart_tx_intr(&main_uart);
203 struct ucg_uart_config conf;
204 const void *uart_ops;
207 #if defined(__ARM_EABI__)
208 uart_ops = &stm32_uart_ops;
209 uart_data = &stm32_uart_data;
211 uart_ops = &avr_uart_ops;
212 uart_data = &avr_uart_data;
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));
221 ucg_uart_getconf(&main_uart, &conf);
222 conf.baudrate = 57600;
223 ret = ucg_uart_setconf(&main_uart, &conf);
227 #if defined(__ARM_EABI__)
228 uart_register_stdio();
229 #elif defined(__AVR__)
230 fdevopen(std_send, std_recv);