Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
uart_arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Antoine Drouin <poinix@gmail.com>
3  *
4  * This file is part of Paparazzi.
5  *
6  * Paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * Paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
26 #include "mcu_periph/uart.h"
27 
28 #include <stdint.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "serial_port.h"
35 
36 // #define TRACE(fmt,args...) fprintf(stderr, fmt, args)
37 #define TRACE(fmt,args...)
38 
39 
40 void uart_periph_set_baudrate(struct uart_periph* periph, uint32_t baud) {
41  struct SerialPort* port;
42  // close serial port if already open
43  if (periph->reg_addr != NULL) {
44  port = (struct SerialPort*)(periph->reg_addr);
45  serial_port_close(port);
46  serial_port_free(port);
47  }
48  // open serial port
49  port = serial_port_new();
50  // use register address to store SerialPort structure pointer...
51  periph->reg_addr = (void*)port;
52 
53  //TODO: set device name in application and pass as argument
54  // FIXME: paparazzi baud is 9600 for B9600 while open_raw needs 12 for B9600
55  printf("opening %s on uart0 at termios.h baud value=%d\n", periph->dev, baud);
56  int ret = serial_port_open_raw(port,periph->dev, baud);
57  if (ret != 0)
58  {
59  TRACE("Error opening %s code %d\n",periph->dev,ret);
60  }
61 }
62 
63 void uart_transmit(struct uart_periph* periph, uint8_t data) {
64  uint16_t temp = (periph->tx_insert_idx + 1) % UART_TX_BUFFER_SIZE;
65 
66  if (temp == periph->tx_extract_idx)
67  return; // no room
68 
69  // check if in process of sending data
70  if (periph->tx_running) { // yes, add to queue
71  periph->tx_buf[periph->tx_insert_idx] = data;
72  periph->tx_insert_idx = temp;
73  }
74  else { // no, set running flag and write to output register
75  periph->tx_running = TRUE;
76  struct SerialPort* port = (struct SerialPort*)(periph->reg_addr);
77  int ret = write((int)(port->fd), &data, 1);
78  if (ret < 1)
79  {
80  TRACE("w %x [%d]\n",data,ret);
81  }
82  }
83 }
84 
85 #include <errno.h>
86 
87 static inline void uart_handler(struct uart_periph* periph) {
88  unsigned char c='D';
89 
90  if (periph->reg_addr == NULL) return; // device not initialized ?
91 
92  struct SerialPort* port = (struct SerialPort*)(periph->reg_addr);
93  int fd = port->fd;
94 
95  // check if more data to send
96  if (periph->tx_insert_idx != periph->tx_extract_idx) {
97  int ret = write(fd, &(periph->tx_buf[periph->tx_extract_idx]), 1);
98  if (ret < 1)
99  {
100  TRACE("w %x [%d: %s]\n", periph->tx_buf[periph->tx_extract_idx], ret, strerror(errno));
101  }
102  periph->tx_extract_idx++;
104  }
105  else {
106  periph->tx_running = FALSE; // clear running flag
107  }
108 
109  if(read(fd,&c,1) > 0){
110  //printf("r %x %c\n",c,c);
111  uint16_t temp = (periph->rx_insert_idx + 1) % UART_RX_BUFFER_SIZE;
112  periph->rx_buf[periph->rx_insert_idx] = c;
113  // check for more room in queue
114  if (temp != periph->rx_extract_idx)
115  periph->rx_insert_idx = temp; // update insert index
116  }
117 
118 }
119 
120 #if USE_UART0
121 
122 void uart0_init( void ) {
123  uart_periph_init(&uart0);
124  strcpy(uart0.dev, UART0_DEV);
125  uart_periph_set_baudrate(&uart0, UART0_BAUD);
126 }
127 
128 
129 void uart0_handler(void) {
130  uart_handler(&uart0);
131 }
132 
133 #endif /* USE_UART0 */
134 
135 #if USE_UART1
136 
137 void uart1_init( void ) {
138  uart_periph_init(&uart1);
139  strcpy(uart1.dev, UART1_DEV);
140  uart_periph_set_baudrate(&uart1, UART1_BAUD);
141 }
142 
143 void uart1_handler(void) {
144  uart_handler(&uart1);
145 }
146 
147 #endif /* USE_UART1 */
148 
unsigned short uint16_t
Definition: types.h:16
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
char dev[UART_DEV_NAME_SIZE]
UART Dev (linux)
Definition: uart.h:68
uint8_t tx_running
Definition: uart.h:64
void * reg_addr
UART Register.
Definition: uart.h:66
uint16_t tx_extract_idx
Definition: uart.h:63
int fd
void uart_transmit(struct uart_periph *p, uint8_t data)
Definition: uart_arch.c:75
#define FALSE
Definition: imu_chimu.h:141
void serial_port_free(struct SerialPort *me)
Definition: serial_port.c:21
UART peripheral.
Definition: uart.h:55
#define TRACE(fmt, args...)
Definition: uart_arch.c:37
#define UART_TX_BUFFER_SIZE
Definition: uart.h:35
void uart_periph_init(struct uart_periph *p)
Definition: uart.c:181
unsigned long uint32_t
Definition: types.h:18
uint16_t rx_insert_idx
Definition: uart.h:58
#define TRUE
Definition: imu_chimu.h:144
uint8_t tx_buf[UART_TX_BUFFER_SIZE]
Transmit buffer.
Definition: uart.h:61
static void uart_handler(struct uart_periph *periph)
Definition: uart_arch.c:87
unsigned char uint8_t
Definition: types.h:14
struct SerialPort * serial_port_new(void)
Definition: serial_port.c:16
void serial_port_close(struct SerialPort *me)
Definition: serial_port.c:111
uint16_t tx_insert_idx
Definition: uart.h:62
int serial_port_open_raw(struct SerialPort *me, const char *device, speed_t speed)
Definition: serial_port.c:46
uint8_t rx_buf[UART_RX_BUFFER_SIZE]
Receive buffer.
Definition: uart.h:57
uint16_t rx_extract_idx
Definition: uart.h:59
#define UART_RX_BUFFER_SIZE
Definition: uart.h:34
void uart_periph_set_baudrate(struct uart_periph *p, uint32_t baud)
Definition: uart_arch.c:65