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