2 * Copyright Droids Corporation
3 * Olivier Matz <zer0@droids-corp.org>
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.
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.
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
19 * Revision : $Id: ax12_user.c,v 1.4 2009-04-24 19:30:42 zer0 Exp $
24 #include <aversive/list.h>
25 #include <aversive/wait.h>
26 #include <aversive/error.h>
32 #include <clock_time.h>
37 #include <control_system_manager.h>
38 #include <blocking_detection_manager.h>
42 #include <parse_string.h>
43 #include <parse_num.h>
46 #include "ax12_user.h"
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
53 * Cmdline interface for AX12. Use the PC to command a daisy-chain of
54 * AX12 actuators with a nice command line interface.
56 * The circuit should be as following:
59 * | uart3|------->--- PC (baudrate=57600)
63 * | uart0|---->---+-- AX12 (baudrate 115200)
67 * Note that RX and TX pins of UART1 are connected together to provide
68 * a half-duplex UART emulation.
73 #define AX12_TIMEOUT 15000L /* in us */
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;
81 /********************************* AX12 commands */
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.
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.
93 static volatile uint8_t ax12_state = AX12_STATE_READ;
94 static volatile uint8_t ax12_nsent = 0;
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)
101 uart_send(UART_AX12_NUM, c);
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)
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);
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)
126 microseconds t = time_get_us2();
129 c = uart_recv_nowait(UART_AX12_NUM);
137 if ((time_get_us2() - t) > AX12_TIMEOUT)
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)
153 if (state == AX12_STATE_WRITE) {
156 while (uart_recv_nowait(UART_AX12_NUM) != -1);
158 ax12_state = AX12_STATE_WRITE;
163 if (CIRBUF_IS_EMPTY(&g_tx_fifo[UART_AX12_NUM]))
164 UCSRxB &= ~(1<<TXEN);
165 ax12_state = AX12_STATE_READ;
173 static void ax12_print_error(uint8_t err, uint16_t line)
177 /* no more than 1 log per sec */
180 if (t2 - t_prev_msg < 1000000L) {
184 AX12_ERROR("AX12 error %x at line %d (%ld messages dropped)",
185 err, line, ax12_dropped_logs);
186 ax12_dropped_logs = 0;
190 uint8_t __ax12_user_write_byte(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
191 uint8_t data, uint16_t line)
197 for (i=0; i<AX12_MAX_TRIES ; i++) {
198 err = AX12_write_byte(ax12, id, address, data);
201 wait_ms(2); /* BAD HACK XXX */
207 ax12_print_error(err, line);
212 uint8_t __ax12_user_write_int(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
213 uint16_t data, uint16_t line)
219 for (i=0; i<AX12_MAX_TRIES ; i++) {
220 err = AX12_write_int(ax12, id, address, data);
223 wait_ms(2); /* BAD HACK XXX */
229 ax12_print_error(err, line);
234 uint8_t __ax12_user_read_byte(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
235 uint8_t *val, uint16_t line)
241 for (i=0; i<AX12_MAX_TRIES ; i++) {
242 err = AX12_read_byte(ax12, id, address, val);
245 wait_ms(2); /* BAD HACK XXX */
249 /* XXX hack for broadcast */
250 if (id == AX12_BROADCAST_ID)
255 ax12_print_error(err, line);
260 uint8_t __ax12_user_read_int(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
261 uint16_t *val, uint16_t line)
267 for (i=0; i<AX12_MAX_TRIES ; i++) {
268 err = AX12_read_int(ax12, id, address, val);
271 wait_ms(2); /* BAD HACK XXX */
275 /* XXX hack for broadcast */
276 if (id == AX12_BROADCAST_ID)
281 ax12_print_error(err, line);
286 void ax12_dump_stats(void)
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);
295 void ax12_user_init(void)
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();