indent
[aversive.git] / projects / microb2010 / cobboard / ax12_user.c
1 /*  
2  *  Copyright Droids Corporation
3  *  Olivier Matz <zer0@droids-corp.org>
4  * 
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *  Revision : $Id: ax12_user.c,v 1.4 2009-04-24 19:30:42 zer0 Exp $
20  *
21  */
22
23 #include <aversive.h>
24 #include <aversive/list.h>
25 #include <aversive/wait.h>
26 #include <aversive/error.h>
27
28 #include <i2c.h>
29 #include <ax12.h>
30 #include <uart.h>
31 #include <pwm_ng.h>
32 #include <clock_time.h>
33 #include <spi.h>
34
35 #include <pid.h>
36 #include <quadramp.h>
37 #include <control_system_manager.h>
38 #include <blocking_detection_manager.h>
39
40 #include <rdline.h>
41 #include <parse.h>
42 #include <parse_string.h>
43 #include <parse_num.h>
44
45 #include "main.h"
46 #include "ax12_user.h"
47
48 #define AX12_ERROR(args...) ERROR(E_USER_AX12, args)
49 #define AX12_NOTICE(args...) NOTICE(E_USER_AX12, args)
50 #define AX12_MAX_TRIES 3
51
52 /*
53  * Cmdline interface for AX12. Use the PC to command a daisy-chain of
54  * AX12 actuators with a nice command line interface.
55  * 
56  * The circuit should be as following:
57  *
58  *    |----------|
59  *    |     uart3|------->--- PC (baudrate=57600)
60  *    |          |-------<---
61  *    | atmega128|
62  *    |          |
63  *    |     uart0|---->---+-- AX12 (baudrate 115200)
64  *    |          |----<---| 
65  *    |----------|
66  *
67  * Note that RX and TX pins of UART1 are connected together to provide
68  * a half-duplex UART emulation.
69  *
70  */
71
72 #define UCSRxB UCSR0B
73 #define AX12_TIMEOUT 15000L /* in us */
74
75 static uint32_t ax12_stats_ops = 0;   /* total ops */
76 static uint32_t ax12_stats_fails = 0; /* number of fails */
77 static uint32_t ax12_stats_drops = 0; /* number of drops (3 fails) */
78 static uint32_t ax12_dropped_logs = 0; /* error messages that were not displayed */
79 static microseconds t_prev_msg = 0;
80
81 /********************************* AX12 commands */
82
83 /*
84  * We use synchronous access (not interrupt driven) to the hardware
85  * UART, because we have to be sure that the transmission/reception is
86  * really finished when we return from the functions.
87  *
88  * We don't use the CM-5 circuit as described in the AX12
89  * documentation, we simply connect TX and RX and use TXEN + RXEN +
90  * DDR to manage the port directions.
91  */
92
93 static volatile uint8_t ax12_state = AX12_STATE_READ;
94 extern volatile struct cirbuf g_tx_fifo[]; /* uart fifo */
95 static volatile uint8_t ax12_nsent = 0;
96
97 /* Called by ax12 module to send a character on serial line. Count the
98  * number of transmitted bytes. It will be used in ax12_recv_char() to
99  * drop the bytes that we transmitted. */
100 static int8_t ax12_send_char(uint8_t c)
101 {
102         uart_send(UART_AX12_NUM, c);
103         ax12_nsent++;
104         return 0;
105 }
106
107 /* for atmega256 */
108 #ifndef TXEN
109 #define TXEN TXEN0
110 #endif
111
112 /* called by uart module when the character has been written in
113  * UDR. It does not mean that the byte is physically transmitted. */
114 static void ax12_send_callback(__attribute__((unused)) char c)
115 {
116         if (ax12_state == AX12_STATE_READ) {
117                 /* disable TX when last byte is pushed. */
118                 if (CIRBUF_IS_EMPTY(&g_tx_fifo[UART_AX12_NUM]))
119                         UCSRxB &= ~(1<<TXEN);
120         }
121 }
122
123 /* Called by ax12 module when we want to receive a char. Note that we
124  * also receive the bytes we sent ! So we need to drop them. */
125 static int16_t ax12_recv_char(void)
126 {
127         microseconds t = time_get_us2();
128         int c;
129         while (1) {
130                 c = uart_recv_nowait(UART_AX12_NUM);
131                 if (c != -1) {
132                         if (ax12_nsent == 0)
133                                 return c;
134                         ax12_nsent --;
135                 }
136
137                 /* 5 ms timeout */
138                 if ((time_get_us2() - t) > AX12_TIMEOUT)
139                         return -1;
140         }
141         return c;
142 }
143
144 /* called by ax12 module when we want to switch serial line. As we
145  * work in interruption mode, this function can be called to switch
146  * back in read mode even if the bytes are not really transmitted on
147  * the line. That's why in this case we do nothing, we will fall back
148  * in read mode in any case when xmit is finished -- see in
149  * ax12_send_callback() -- */
150 static void ax12_switch_uart(uint8_t state)
151 {
152         uint8_t flags;
153
154         if (state == AX12_STATE_WRITE) {
155                 IRQ_LOCK(flags);
156                 ax12_nsent=0;
157                 while (uart_recv_nowait(UART_AX12_NUM) != -1);
158                 UCSRxB |= (1<<TXEN);
159                 ax12_state = AX12_STATE_WRITE;
160                 IRQ_UNLOCK(flags);
161         }
162         else {
163                 IRQ_LOCK(flags);
164                 if (CIRBUF_IS_EMPTY(&g_tx_fifo[UART_AX12_NUM]))
165                         UCSRxB &= ~(1<<TXEN);
166                 ax12_state = AX12_STATE_READ;
167                 IRQ_UNLOCK(flags);
168         }
169 }
170
171 /* ----- */ 
172
173 /* log rate limit */
174 static void ax12_print_error(uint8_t err, uint16_t line)
175 {
176         microseconds t2;
177         
178         /* no more than 1 log per sec */
179         t2 = time_get_us2();
180
181         if (t2 - t_prev_msg < 1000000L) {
182                 ax12_dropped_logs++;
183                 return;
184         }
185         AX12_ERROR("AX12 error %x at line %d (%ld messages dropped)",
186                    err, line, ax12_dropped_logs);
187         ax12_dropped_logs = 0;
188         t_prev_msg = t2;
189 }
190
191 uint8_t __ax12_user_write_byte(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
192                                uint8_t data, uint16_t line)
193 {
194         uint8_t err, i;
195
196         ax12_stats_ops++;
197
198         for (i=0; i<AX12_MAX_TRIES ; i++) {
199                 err = AX12_write_byte(ax12, id, address, data);
200                 if (err == 0)
201                         break;
202                 wait_ms(2); /* BAD HACK XXX */
203                 ax12_stats_fails++;
204         }
205         if (err == 0)
206                 return 0;
207
208         ax12_print_error(err, line);
209         ax12_stats_drops++;
210         return err;
211 }
212
213 uint8_t __ax12_user_write_int(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
214                             uint16_t data, uint16_t line)
215 {
216         uint8_t err, i;
217
218         ax12_stats_ops++;
219
220         for (i=0; i<AX12_MAX_TRIES ; i++) {
221                 err = AX12_write_int(ax12, id, address, data);
222                 if (err == 0)
223                         break;
224                 wait_ms(2); /* BAD HACK XXX */
225                 ax12_stats_fails++;
226         }
227         if (err == 0)
228                 return 0;
229
230         ax12_print_error(err, line);
231         ax12_stats_drops++;
232         return err;
233 }
234
235 uint8_t __ax12_user_read_byte(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
236                             uint8_t *val, uint16_t line)
237 {
238         uint8_t err, i;
239
240         ax12_stats_ops++;
241
242         for (i=0; i<AX12_MAX_TRIES ; i++) {
243                 err = AX12_read_byte(ax12, id, address, val);
244                 if (err == 0)
245                         break;
246                 wait_ms(2); /* BAD HACK XXX */
247                 ax12_stats_fails++;
248         }
249         if (err == 0) {
250                 /* XXX hack for broadcast */
251                 if (id == AX12_BROADCAST_ID)
252                         wait_ms(1);
253                 return 0;
254         }
255
256         ax12_print_error(err, line);
257         ax12_stats_drops++;
258         return err;
259 }
260
261 uint8_t __ax12_user_read_int(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
262                            uint16_t *val, uint16_t line)
263 {
264         uint8_t err, i;
265
266         ax12_stats_ops++;
267
268         for (i=0; i<AX12_MAX_TRIES ; i++) {
269                 err = AX12_read_int(ax12, id, address, val);
270                 if (err == 0)
271                         break;
272                 wait_ms(2); /* BAD HACK XXX */
273                 ax12_stats_fails++;
274         }
275         if (err == 0) {
276                 /* XXX hack for broadcast */
277                 if (id == AX12_BROADCAST_ID)
278                         wait_ms(1);
279                 return 0;
280         }
281
282         ax12_print_error(err, line);
283         ax12_stats_drops++;
284         return err;
285 }
286
287 void ax12_dump_stats(void)
288 {
289         printf_P(PSTR("AX12 stats:\r\n"));
290         printf_P(PSTR("  total ops:   %ld\r\n"), ax12_stats_ops);
291         printf_P(PSTR("  total fails: %ld\r\n"), ax12_stats_fails);
292         printf_P(PSTR("  total drops: %ld\r\n"), ax12_stats_drops);
293         printf_P(PSTR("  logs dropped since last message: %ld\r\n"), ax12_dropped_logs);
294 }
295
296 void ax12_user_init(void)
297 {
298         /* AX12 */
299         AX12_init(&gen.ax12);
300         AX12_set_hardware_send(&gen.ax12, ax12_send_char);
301         AX12_set_hardware_recv(&gen.ax12, ax12_recv_char);
302         AX12_set_hardware_switch(&gen.ax12, ax12_switch_uart);
303         uart_register_tx_event(UART_AX12_NUM, ax12_send_callback);
304         t_prev_msg = time_get_us2();
305 }