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>
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 extern volatile struct cirbuf g_tx_fifo[]; /* uart fifo */
95 static volatile uint8_t ax12_nsent = 0;
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)
102 uart_send(UART_AX12_NUM, c);
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)
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);
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)
127 microseconds t = time_get_us2();
130 c = uart_recv_nowait(UART_AX12_NUM);
138 if ((time_get_us2() - t) > AX12_TIMEOUT)
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)
154 if (state == AX12_STATE_WRITE) {
157 while (uart_recv_nowait(UART_AX12_NUM) != -1);
159 ax12_state = AX12_STATE_WRITE;
164 if (CIRBUF_IS_EMPTY(&g_tx_fifo[UART_AX12_NUM]))
165 UCSRxB &= ~(1<<TXEN);
166 ax12_state = AX12_STATE_READ;
174 static void ax12_print_error(uint8_t err, uint16_t line)
178 /* no more than 1 log per sec */
181 if (t2 - t_prev_msg < 1000000L) {
185 AX12_ERROR("AX12 error %x at line %d (%ld messages dropped)",
186 err, line, ax12_dropped_logs);
187 ax12_dropped_logs = 0;
191 uint8_t __ax12_user_write_byte(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
192 uint8_t data, uint16_t line)
198 for (i=0; i<AX12_MAX_TRIES ; i++) {
199 err = AX12_write_byte(ax12, id, address, data);
202 wait_ms(2); /* BAD HACK XXX */
208 ax12_print_error(err, line);
213 uint8_t __ax12_user_write_int(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
214 uint16_t data, uint16_t line)
220 for (i=0; i<AX12_MAX_TRIES ; i++) {
221 err = AX12_write_int(ax12, id, address, data);
224 wait_ms(2); /* BAD HACK XXX */
230 ax12_print_error(err, line);
235 uint8_t __ax12_user_read_byte(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
236 uint8_t *val, uint16_t line)
242 for (i=0; i<AX12_MAX_TRIES ; i++) {
243 err = AX12_read_byte(ax12, id, address, val);
246 wait_ms(2); /* BAD HACK XXX */
250 /* XXX hack for broadcast */
251 if (id == AX12_BROADCAST_ID)
256 ax12_print_error(err, line);
261 uint8_t __ax12_user_read_int(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
262 uint16_t *val, uint16_t line)
268 for (i=0; i<AX12_MAX_TRIES ; i++) {
269 err = AX12_read_int(ax12, id, address, val);
272 wait_ms(2); /* BAD HACK XXX */
276 /* XXX hack for broadcast */
277 if (id == AX12_BROADCAST_ID)
282 ax12_print_error(err, line);
287 void ax12_dump_stats(void)
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);
296 void ax12_user_init(void)
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();