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>
47 #if defined(__ARM_EABI__) || defined(__AVR__)
49 static char rx_buf[64];
50 static struct ucg_cirbuf rx_cirbuf;
51 static char tx_buf[64];
52 static struct ucg_cirbuf tx_cirbuf;
53 /* generic uart struct */
54 struct ucg_uart main_uart;
57 #if defined(__ARM_EABI__)
59 #include <ucg_gloss_chardev.h>
60 #include <ucg_stm32_uart.h>
62 static struct ucg_stm32_uart stm32_uart_data = {
64 .rcc_uart = RCC_APB1Periph_USART2,
65 .rcc_gpio = RCC_AHB1Periph_GPIOA,
67 .gpio_af = GPIO_AF_USART2,
68 .gpio_pins = GPIO_Pin_2 | GPIO_Pin_3,
69 .gpio_speed = GPIO_Speed_25MHz,
71 .irq_preempt_prio = 2,
76 uart_write_r(__attribute__((unused)) struct _reent *r,
77 __attribute__((unused)) int fd,
78 const void *ptr, size_t len)
81 const char *buf = ptr;
83 for (i = 0; i < len; i++)
84 ucg_uart_send(&main_uart, buf[i], WAIT);
90 uart_read_r(__attribute__((unused)) struct _reent *r,
91 __attribute__((unused)) int fd,
92 void *ptr, size_t len)
98 c = ucg_uart_recv(&main_uart, NOWAIT);
107 void USART2_IRQHandler(void)
109 if ((USART2->SR & USART_FLAG_TXE))
110 ucg_uart_tx_intr(&main_uart);
111 if ((USART2->SR & USART_FLAG_RXNE))
112 ucg_uart_rx_intr(&main_uart);
115 struct ucg_chardev uart_stdin_dev = {
119 .read_r = uart_read_r,
123 struct ucg_chardev uart_stdout_dev = {
128 .write_r = uart_write_r,
131 struct ucg_chardev uart_stderr_dev = {
136 .write_r = uart_write_r,
139 static void uart_register_stdio(void)
141 ucg_chardev_register(&uart_stdin_dev);
142 ucg_chardev_register(&uart_stdout_dev);
143 ucg_chardev_register(&uart_stderr_dev);
145 /* Disable buffering */
147 setbuf(stdout, NULL);
148 setbuf(stderr, NULL);
151 #elif defined(__AVR__)
153 #include <ucg_avr_uart.h>
154 /* avr-specific uart struct */
155 static struct ucg_avr_uart avr_uart_data = {
157 .reg_ucsra = &UCSR0A,
158 .reg_ucsrb = &UCSR0B,
159 .reg_ucsrc = &UCSR0C,
160 .reg_ubrrl = &UBRR0L,
161 .reg_ubrrh = &UBRR0H,
172 static int std_send(char c, FILE *f)
175 ucg_uart_send(&main_uart, c, WAIT);
180 static int std_recv(FILE *f)
185 c = ucg_uart_recv(&main_uart, NOWAIT);
192 SIGNAL(USART_RX_vect)
194 ucg_uart_rx_intr(&main_uart);
197 SIGNAL(USART_UDRE_vect)
199 ucg_uart_tx_intr(&main_uart);
206 #if defined(__ARM_EABI__) || defined(__AVR__)
207 struct ucg_uart_config conf;
208 const void *uart_ops;
211 #if defined(__ARM_EABI__)
212 uart_ops = &stm32_uart_ops;
213 uart_data = &stm32_uart_data;
214 #elif defined(__AVR__)
215 uart_ops = &avr_uart_ops;
216 uart_data = &avr_uart_data;
219 ret = ucg_uart_init(&main_uart, uart_ops, uart_data,
220 &rx_cirbuf, rx_buf, sizeof(rx_buf),
221 &tx_cirbuf, tx_buf, sizeof(tx_buf));
225 ucg_uart_getconf(&main_uart, &conf);
226 conf.baudrate = 57600;
227 ret = ucg_uart_setconf(&main_uart, &conf);
231 #if defined(__ARM_EABI__)
232 uart_register_stdio();
233 #elif defined(__AVR__)
234 fdevopen(std_send, std_recv);