vt100: include pgmspace.h as we use PROGMEM macro
[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 static volatile uint8_t ax12_nsent = 0;
95
96 /* Called by ax12 module to send a character on serial line. Count the
97  * number of transmitted bytes. It will be used in ax12_recv_char() to
98  * drop the bytes that we transmitted. */
99 static int8_t ax12_send_char(uint8_t c)
100 {
101         uart_send(UART_AX12_NUM, c);
102         ax12_nsent++;
103         return 0;
104 }
105
106 /* for atmega256 */
107 #ifndef TXEN
108 #define TXEN TXEN0
109 #endif
110
111 /* called by uart module when the character has been written in
112  * UDR. It does not mean that the byte is physically transmitted. */
113 static void ax12_send_callback(__attribute__((unused)) char c)
114 {
115         if (ax12_state == AX12_STATE_READ) {
116                 /* disable TX when last byte is pushed. */
117                 if (CIRBUF_IS_EMPTY(&g_tx_fifo[UART_AX12_NUM]))
118                         UCSRxB &= ~(1<<TXEN);
119         }
120 }
121
122 /* Called by ax12 module when we want to receive a char. Note that we
123  * also receive the bytes we sent ! So we need to drop them. */
124 static int16_t ax12_recv_char(void)
125 {
126         microseconds t = time_get_us2();
127         int c;
128         while (1) {
129                 c = uart_recv_nowait(UART_AX12_NUM);
130                 if (c != -1) {
131                         if (ax12_nsent == 0)
132                                 return c;
133                         ax12_nsent --;
134                 }
135
136                 /* 5 ms timeout */
137                 if ((time_get_us2() - t) > AX12_TIMEOUT)
138                         return -1;
139         }
140         return c;
141 }
142
143 /* called by ax12 module when we want to switch serial line. As we
144  * work in interruption mode, this function can be called to switch
145  * back in read mode even if the bytes are not really transmitted on
146  * the line. That's why in this case we do nothing, we will fall back
147  * in read mode in any case when xmit is finished -- see in
148  * ax12_send_callback() -- */
149 static void ax12_switch_uart(uint8_t state)
150 {
151         uint8_t flags;
152
153         if (state == AX12_STATE_WRITE) {
154                 IRQ_LOCK(flags);
155                 ax12_nsent=0;
156                 while (uart_recv_nowait(UART_AX12_NUM) != -1);
157                 UCSRxB |= (1<<TXEN);
158                 ax12_state = AX12_STATE_WRITE;
159                 IRQ_UNLOCK(flags);
160         }
161         else {
162                 IRQ_LOCK(flags);
163                 if (CIRBUF_IS_EMPTY(&g_tx_fifo[UART_AX12_NUM]))
164                         UCSRxB &= ~(1<<TXEN);
165                 ax12_state = AX12_STATE_READ;
166                 IRQ_UNLOCK(flags);
167         }
168 }
169
170 /* ----- */ 
171
172 /* log rate limit */
173 static void ax12_print_error(uint8_t err, uint16_t line)
174 {
175         microseconds t2;
176         
177         /* no more than 1 log per sec */
178         t2 = time_get_us2();
179
180         if (t2 - t_prev_msg < 1000000L) {
181                 ax12_dropped_logs++;
182                 return;
183         }
184         AX12_ERROR("AX12 error %x at line %d (%ld messages dropped)",
185                    err, line, ax12_dropped_logs);
186         ax12_dropped_logs = 0;
187         t_prev_msg = t2;
188 }
189
190 uint8_t __ax12_user_write_byte(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
191                                uint8_t data, uint16_t line)
192 {
193         uint8_t err, i;
194
195         ax12_stats_ops++;
196
197         for (i=0; i<AX12_MAX_TRIES ; i++) {
198                 err = AX12_write_byte(ax12, id, address, data);
199                 if (err == 0)
200                         break;
201                 wait_ms(2); /* BAD HACK XXX */
202                 ax12_stats_fails++;
203         }
204         if (err == 0)
205                 return 0;
206
207         ax12_print_error(err, line);
208         ax12_stats_drops++;
209         return err;
210 }
211
212 uint8_t __ax12_user_write_int(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
213                             uint16_t data, uint16_t line)
214 {
215         uint8_t err, i;
216
217         ax12_stats_ops++;
218
219         for (i=0; i<AX12_MAX_TRIES ; i++) {
220                 err = AX12_write_int(ax12, id, address, data);
221                 if (err == 0)
222                         break;
223                 wait_ms(2); /* BAD HACK XXX */
224                 ax12_stats_fails++;
225         }
226         if (err == 0)
227                 return 0;
228
229         ax12_print_error(err, line);
230         ax12_stats_drops++;
231         return err;
232 }
233
234 uint8_t __ax12_user_read_byte(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
235                             uint8_t *val, uint16_t line)
236 {
237         uint8_t err, i;
238
239         ax12_stats_ops++;
240
241         for (i=0; i<AX12_MAX_TRIES ; i++) {
242                 err = AX12_read_byte(ax12, id, address, val);
243                 if (err == 0)
244                         break;
245                 wait_ms(2); /* BAD HACK XXX */
246                 ax12_stats_fails++;
247         }
248         if (err == 0) {
249                 /* XXX hack for broadcast */
250                 if (id == AX12_BROADCAST_ID)
251                         wait_ms(1);
252                 return 0;
253         }
254
255         ax12_print_error(err, line);
256         ax12_stats_drops++;
257         return err;
258 }
259
260 uint8_t __ax12_user_read_int(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
261                            uint16_t *val, uint16_t line)
262 {
263         uint8_t err, i;
264
265         ax12_stats_ops++;
266
267         for (i=0; i<AX12_MAX_TRIES ; i++) {
268                 err = AX12_read_int(ax12, id, address, val);
269                 if (err == 0)
270                         break;
271                 wait_ms(2); /* BAD HACK XXX */
272                 ax12_stats_fails++;
273         }
274         if (err == 0) {
275                 /* XXX hack for broadcast */
276                 if (id == AX12_BROADCAST_ID)
277                         wait_ms(1);
278                 return 0;
279         }
280
281         ax12_print_error(err, line);
282         ax12_stats_drops++;
283         return err;
284 }
285
286 void ax12_dump_stats(void)
287 {
288         printf_P(PSTR("AX12 stats:\r\n"));
289         printf_P(PSTR("  total ops:   %ld\r\n"), ax12_stats_ops);
290         printf_P(PSTR("  total fails: %ld\r\n"), ax12_stats_fails);
291         printf_P(PSTR("  total drops: %ld\r\n"), ax12_stats_drops);
292         printf_P(PSTR("  logs dropped since last message: %ld\r\n"), ax12_dropped_logs);
293 }
294
295 void ax12_user_init(void)
296 {
297         /* AX12 */
298         AX12_init(&gen.ax12);
299         AX12_set_hardware_send(&gen.ax12, ax12_send_char);
300         AX12_set_hardware_recv(&gen.ax12, ax12_recv_char);
301         AX12_set_hardware_switch(&gen.ax12, ax12_switch_uart);
302         uart_register_tx_event(UART_AX12_NUM, ax12_send_callback);
303         t_prev_msg = time_get_us2();
304 }