4 * Created on: 04.05.2014
12 #include <libopencm3/stm32/gpio.h>
13 #include <libopencm3/stm32/usart.h>
14 #include <libopencm3/cm3/nvic.h>
18 #define USART_CONSOLE USART1
20 int _write(int fd
, char *ptr
, int len
) __attribute__((used
));
31 #define BUFFER_SIZE 256
35 uint8_t rx_ring_buffer
[BUFFER_SIZE
];
36 uint8_t tx_ring_buffer
[BUFFER_SIZE
];
39 static void ring_init(struct ring
*ring
, uint8_t *buf
, int size
)
47 static int ring_write_ch(struct ring
*ring
, uint8_t ch
)
49 int ep
= (ring
->end
+ 1) % ring
->size
;
51 if ((ep
) != ring
->begin
) {
52 ring
->data
[ring
->end
] = ch
;
60 static int ring_write(struct ring
*ring
, uint8_t *data
, int size
)
64 for (i
= 0; i
< size
; i
++) {
65 if (ring_write_ch(ring
, data
[i
]) < 0)
72 static int ring_read_ch(struct ring
*ring
)
76 if (ring
->begin
!= ring
->end
) {
77 ret
= ring
->data
[ring
->begin
];
78 ring
->begin
= (ring
->begin
+1) % ring
->size
;
84 void usart_setup(void)
86 /* Initialize output ring buffer. */
87 ring_init(&rx_ring
, rx_ring_buffer
, BUFFER_SIZE
);
88 ring_init(&tx_ring
, tx_ring_buffer
, BUFFER_SIZE
);
90 /* Enable the USART1 interrupt. */
91 nvic_enable_irq(NVIC_USART1_IRQ
);
93 /* Setup GPIO pin GPIO_USART1_TX/LED_GREEN_PIN on GPIO port A for transmit. */
94 /* TODO: USART1 --> USART_CONSOLE */
96 gpio_set_mode(GPIOA
, GPIO_MODE_OUTPUT_50_MHZ
,
97 GPIO_CNF_OUTPUT_ALTFN_PUSHPULL
, GPIO_USART1_TX
);
99 /* Setup GPIO pin GPIO_USART1_RE_RX on GPIO port B for receive. */
100 gpio_set_mode(GPIOA
, GPIO_MODE_INPUT
,
101 GPIO_CNF_INPUT_FLOAT
, GPIO_USART1_RX
);
103 /* Setup UART parameters. */
104 // usart_set_baudrate(USART_CONSOLE, 38400);
105 usart_set_baudrate(USART_CONSOLE
, 115200);
106 usart_set_databits(USART_CONSOLE
, 8);
107 usart_set_stopbits(USART_CONSOLE
, USART_STOPBITS_1
);
108 usart_set_parity(USART_CONSOLE
, USART_PARITY_NONE
);
109 usart_set_flow_control(USART_CONSOLE
, USART_FLOWCONTROL_NONE
);
110 usart_set_mode(USART_CONSOLE
, USART_MODE_TX_RX
);
112 /* Enable USART1 Receive interrupt. */
113 USART_CR1(USART1
) |= USART_CR1_RXNEIE
;
115 /* Finally enable the USART. */
116 usart_enable(USART_CONSOLE
);
119 void usart1_isr(void)
121 /* Check if we were called because of RXNE. */
122 if (((USART_CR1(USART1
) & USART_CR1_RXNEIE
) != 0) &&
123 ((USART_SR(USART1
) & USART_SR_RXNE
) != 0)) {
125 /* Retrieve the data from the peripheral. */
126 ring_write_ch(&rx_ring
, usart_recv(USART1
));
130 /* Check if we were called because of TXE. */
131 if (((USART_CR1(USART1
) & USART_CR1_TXEIE
) != 0) &&
132 ((USART_SR(USART1
) & USART_SR_TXE
) != 0)) {
136 data
= ring_read_ch(&tx_ring
);
139 /* Disable the TXE interrupt, it's no longer needed. */
140 USART_CR1(USART1
) &= ~USART_CR1_TXEIE
;
142 /* Put data into the transmit register. */
143 usart_send(USART1
, data
);
148 /*--------------------------------------------------------------------------*/
150 void serial_setup(void)
155 /*--------------------------------------------------------------------------*/
158 * Use USART_CONSOLE as a console.
159 * This is a syscall for newlib
165 int _write(int fd
, char *ptr
, int len
)
169 if (fd
== STDOUT_FILENO
|| fd
== STDERR_FILENO
) {
170 for (i
= 0; i
< len
; i
++) {
171 if (ptr
[i
] == '\n') {
182 int serial_getc(void)
184 return ring_read_ch(&rx_ring
);
187 void serial_putc(uint8_t data
)
189 while (ring_write_ch(&tx_ring
, data
) < 0)
192 /* Enable the TXE interrupt. */
193 USART_CR1(USART1
) |= USART_CR1_TXEIE
;