Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
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 "fms/fms_serial_port.h"
35 
36 
38  struct FmsSerialPort* fmssp;
39  // close serial port if already open
40  if (p->reg_addr != NULL) {
41  fmssp = (struct FmsSerialPort*)(p->reg_addr);
42  serial_port_close(fmssp);
43  serial_port_free(fmssp);
44  }
45  // open serial port
46  fmssp = serial_port_new();
47  // use register address to store SerialPort structure pointer...
48  p->reg_addr = (void*)fmssp;
49 
50  //TODO: set device name in application and pass as argument
51  printf("opening %s on uart0 at %d\n",p->dev,baud);
52  serial_port_open_raw(fmssp,p->dev,baud);
53 }
54 
55 void uart_transmit(struct uart_periph* p, uint8_t data ) {
56  uint16_t temp = (p->tx_insert_idx + 1) % UART_TX_BUFFER_SIZE;
57 
58  if (temp == p->tx_extract_idx)
59  return; // no room
60 
61  // check if in process of sending data
62  if (p->tx_running) { // yes, add to queue
63  p->tx_buf[p->tx_insert_idx] = data;
64  p->tx_insert_idx = temp;
65  }
66  else { // no, set running flag and write to output register
67  p->tx_running = TRUE;
68  struct FmsSerialPort* fmssp = (struct FmsSerialPort*)(p->reg_addr);
69  write((int)(fmssp->fd),&data,1);
70  //printf("w %x\n",data);
71  }
72 }
73 
74 static inline void uart_handler(struct uart_periph* p) {
75  unsigned char c='D';
76 
77  if (p->reg_addr == NULL) return; // device not initialized ?
78 
79  struct FmsSerialPort* fmssp = (struct FmsSerialPort*)(p->reg_addr);
80  int fd = fmssp->fd;
81 
82  // check if more data to send
83  if (p->tx_insert_idx != p->tx_extract_idx) {
84  write(fd,&(p->tx_buf[p->tx_extract_idx]),1);
85  //printf("w %x\n",p->tx_buf[p->tx_extract_idx]);
86  p->tx_extract_idx++;
88  }
89  else {
90  p->tx_running = FALSE; // clear running flag
91  }
92 
93  if(read(fd,&c,1) > 0){
94  //printf("r %x %c\n",c,c);
95  uint16_t temp = (p->rx_insert_idx + 1) % UART_RX_BUFFER_SIZE;
96  p->rx_buf[p->rx_insert_idx] = c;
97  // check for more room in queue
98  if (temp != p->rx_extract_idx)
99  p->rx_insert_idx = temp; // update insert index
100  }
101 
102 }
103 
104 #ifdef USE_UART0
105 
106 void uart0_init( void ) {
107  uart_periph_init(&uart0);
108  strcpy(uart0.dev, UART0_DEV);
109  uart_periph_set_baudrate(&uart0, UART0_BAUD);
110 }
111 
112 
113 void uart0_handler(void) {
114  uart_handler(&uart0);
115 }
116 
117 #endif /* USE_UART0 */
118 
119 #ifdef USE_UART1
120 
121 void uart1_init( void ) {
122  uart_periph_init(&uart1);
123  strcpy(uart1.dev, UART1_DEV);
124  uart_periph_set_baudrate(&uart1, UART1_BAUD);
125 }
126 
127 void uart1_handler(void) {
128  uart_handler(&uart1);
129 }
130 
131 #endif /* USE_UART1 */
132 
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:69
uint8_t tx_running
Definition: uart.h:65
void * reg_addr
UART Register.
Definition: uart.h:67
uint16_t tx_extract_idx
Definition: uart.h:64
void uart_transmit(struct uart_periph *p, uint8_t data)
Definition: uart_arch.c:71
#define FALSE
Definition: imu_chimu.h:141
UART peripheral.
Definition: uart.h:56
static void uart_handler(struct uart_periph *p)
Definition: uart_arch.c:74
#define UART_TX_BUFFER_SIZE
Definition: uart.h:35
void uart_periph_init(struct uart_periph *p)
Definition: uart.c:53
unsigned long uint32_t
Definition: types.h:18
uint16_t rx_insert_idx
Definition: uart.h:59
#define TRUE
Definition: imu_chimu.h:144
uint8_t tx_buf[UART_TX_BUFFER_SIZE]
Transmit buffer.
Definition: uart.h:62
unsigned char uint8_t
Definition: types.h:14
uint16_t tx_insert_idx
Definition: uart.h:63
uint8_t rx_buf[UART_RX_BUFFER_SIZE]
Receive buffer.
Definition: uart.h:58
static float p[2][2]
uint16_t rx_extract_idx
Definition: uart.h:60
static struct point c
Definition: discsurvey.c:39
#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