From: Nicolas D. <Ba...@us...> - 2011-03-19 17:58:34
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "krobot". The branch, master has been updated via 93954a49f74cb2037113e6746db7812d3e0b85e5 (commit) via a15cb73960f845d1290f66fafa70b17e5377dd47 (commit) via 4bfac45208500bd33f1e2deb6e3a0de3b983cf74 (commit) via e9f522ea891327263f6c9f19130a78d357d59848 (commit) from 2c65274daf59dc657959b6cc7b3bc400e3779c7c (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit 93954a49f74cb2037113e6746db7812d3e0b85e5 Author: Nicolas Dandrimont <Nic...@cr...> Date: Sat Mar 19 18:56:07 2011 +0100 [USB_CAN/Firmware] Rewrite copyright notices commit a15cb73960f845d1290f66fafa70b17e5377dd47 Author: Nicolas Dandrimont <Nic...@cr...> Date: Sat Mar 19 18:54:38 2011 +0100 [USB_CAN/Firmware] Implement the CAN sending logic commit 4bfac45208500bd33f1e2deb6e3a0de3b983cf74 Author: Nicolas Dandrimont <Nic...@cr...> Date: Sat Mar 19 18:53:14 2011 +0100 [USB_CAN/Firmware] Move timestamp computation to the usb_can_emit function commit e9f522ea891327263f6c9f19130a78d357d59848 Author: Nicolas Dandrimont <Nic...@cr...> Date: Sat Mar 19 17:40:43 2011 +0100 [USB_CAN/Firmware] Remove ugly printf's ----------------------------------------------------------------------- Changes: diff --git a/elec/boards/USB_CAN/Firmware/bertos/drv/can.c b/elec/boards/USB_CAN/Firmware/bertos/drv/can.c index d245c2d..a4137dd 100644 --- a/elec/boards/USB_CAN/Firmware/bertos/drv/can.c +++ b/elec/boards/USB_CAN/Firmware/bertos/drv/can.c @@ -33,6 +33,7 @@ * \brief CAN driver (implementation) * * \author Nicolas Dandrimont <Nic...@cr...> + * */ #include <drv/can.h> @@ -115,22 +116,16 @@ bool can_transmit(can_driver *drv, can_tx_frame *frame, ticks_t timeout) { bool ret; proc_forbid(); - kprintf("Lock xmit (timeout=%d)\n", timeout); while ((drv->state == CAN_SLEEP) || !can_hw_can_transmit(drv)) { - kprintf("Can't xmit\n"); ret = event_waitTimeout(&drv->tx_empty_event, timeout); - kprintf("After wait (%d)\n", ret); if (ret != true) { proc_permit(); return false; } } - - kprintf("Before xmit\n"); + can_hw_transmit(drv, frame); - kprintf("After xmit\n"); proc_permit(); - kprintf("Unlock xmit\n"); return true; } diff --git a/elec/boards/USB_CAN/Firmware/bertos/drv/can.h b/elec/boards/USB_CAN/Firmware/bertos/drv/can.h index 07a7d81..9d47528 100644 --- a/elec/boards/USB_CAN/Firmware/bertos/drv/can.h +++ b/elec/boards/USB_CAN/Firmware/bertos/drv/can.h @@ -42,6 +42,8 @@ * $WIZ$ module_name = "can" * $WIZ$ module_configuration = "bertos/cfg/cfg_can.h" * $WIZ$ module_supports = "stm32" + * $WIZ$ module_depends = "timer", "signal" + * */ diff --git a/elec/boards/USB_CAN/Firmware/usb_can/main.c b/elec/boards/USB_CAN/Firmware/usb_can/main.c index 01442bf..c86fca5 100644 --- a/elec/boards/USB_CAN/Firmware/usb_can/main.c +++ b/elec/boards/USB_CAN/Firmware/usb_can/main.c @@ -1,11 +1,15 @@ /** - * \file - * <!-- - * This file is part of BeRTOS. + * USB-CAN converter * - * Bertos is free software; you can redistribute it and/or modify + * This file is the entry point for an USB <-> CAN transceiver. + * + * Copyright © 2011 Nicolas Dandrimont <ol...@cr...> + * Authors: Nicolas Dandrimont <ol...@cr...> + * Xavier Lagorce <Xav...@cr...> + * + * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or + * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, @@ -14,29 +18,8 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * As a special exception, you may use this file as part of a free software - * library without restriction. Specifically, if other files instantiate - * templates or use macros or inline functions from this file, or you compile - * this file and link it with other files to produce an executable, this - * file does not by itself cause the resulting executable to be covered by - * the GNU General Public License. This exception does not however - * invalidate any other reasons why the executable file might be covered by - * the GNU General Public License. - * - * Copyright 2010 Develer S.r.l. (http://www.develer.com/) - * - * --> - * - * \author Andrea Righi <ar...@de...> + * along with this program. If not, see <http://www.gnu.org/licenses/>. * - * \brief Kernel project. - * - * This is a minimalist kernel project: it just initializes the hardware and - * creates an independent process to blink an LED, while the main loop - * continues to monitor the stack utilization of all the processes. */ #include "hw/hw_led.h" @@ -112,23 +95,11 @@ static void NORETURN serial_receive_process(void) int nbytes, retval, i = 0; char command[MAX_CMD_SIZE+1]; - can_tx_frame frame; - - frame.ide = 1; - frame.eid = 0x42; - frame.dlc = 4; - frame.data8[0] = 1; - frame.data8[1] = 1; - frame.data8[2] = 1; - frame.data8[3] = 1; - for (;;) { i = !i; nbytes = kfile_gets(&ser.fd, command, MAX_CMD_SIZE+1); if (nbytes != EOF) { retval = usb_can_execute_command(CAND1, &ser, command); - can_transmit(CAND1, &frame, ms_to_ticks(42)); - kprintf("got %d bytes: [%s]\n", nbytes, command); if (i) LED1_ON(); else @@ -143,13 +114,15 @@ static void NORETURN can_receive_process(void) { can_rx_frame frame; int retval; - int32_t timestamp; + bool received = false; + for (;;) { - can_receive(CAND1, &frame, ms_to_ticks(100)); - timestamp = ticks_to_ms(timer_clock()) % 60000; - retval = usb_can_emit(CAND1, &ser, &frame, (uint16_t)timestamp); - kprintf("received something... %d %08lx %08lx\n", frame.ide ? frame.eid:frame.sid, frame.data32[0], frame.data32[1]); + received = can_receive(CAND1, &frame, ms_to_ticks(100)); + if (received) { + retval = usb_can_emit(CAND1, &ser, &frame); + kprintf("received something... %d %08lx %08lx\n", frame.ide ? frame.eid:frame.sid, frame.data32[0], frame.data32[1]); + } } } diff --git a/elec/boards/USB_CAN/Firmware/usb_can/usb_can.c b/elec/boards/USB_CAN/Firmware/usb_can/usb_can.c index bb16436..03e505e 100644 --- a/elec/boards/USB_CAN/Firmware/usb_can/usb_can.c +++ b/elec/boards/USB_CAN/Firmware/usb_can/usb_can.c @@ -1,14 +1,144 @@ +/** + * USB-CAN converter + * + * This file contains the logic for the USB <-> CAN transceiver. + * + * Copyright © 2011 Nicolas Dandrimont <ol...@cr...> + * Authors: Nicolas Dandrimont <ol...@cr...> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + #include "usb_can.h" -int usb_can_execute_command(UNUSED_ARG(can_driver *, candrv), UNUSED_ARG(struct Serial *, serial), UNUSED_ARG(char *, command)) { +INLINE uint32_t nibble_to_uint32(char *nibbles, size_t length) { + + char *p; + uint32_t ret = 0; + uint8_t tmp = 0; + + for (p = nibbles; p < nibbles + length; p++) { + ret = (ret << 4); + if ('0' <= *p && *p <= '9') + tmp = *p - '0'; + else if ('A' <= *p && *p <= 'F') + tmp = (*p - 'A') + 10; + else if ('a' <= *p && *p <= 'f') + tmp = (*p - 'a') + 10; + else + tmp = 0; + ret = ret + tmp; + } + + return ret; +} + + +int usb_can_execute_command(can_driver *candrv, struct Serial *serial, char *command) { + + can_tx_frame frame; + bool send, ret; + int i; + + frame.rtr = 0; + frame.ide = 0; + frame.eid = 0; + frame.sid = 0; + frame.dlc = 0; + frame.data32[0] = 0; + frame.data32[1] = 0; + + switch (command[0]) { + case 'V': + /* Version number */ + kfile_write(&serial->fd, "V0402\r", 6); + break; + case 'N': + /* Serial number */ + kfile_write(&serial->fd, "NKROB\r", 6); + break; + case 'O': + /* Open CAN Channel */ + /* FIXME: Implement! */ + break; + case 'S': + /* Set CAN Channel Baudrate numerically */ + /* FIXME: Implement! */ + break; + case 's': + /* Set CAN Channel Baudrate with BTR0/1 */ + /* FIXME: Implement! */ + break; + case 'R': + /* Send an extended RTR frame */ + frame.rtr = 1; + frame.ide = 1; + frame.eid = nibble_to_uint32(&command[1], 8); + frame.dlc = nibble_to_uint32(&command[9], 1); + for (i = 0; i < frame.dlc; i++) + frame.data8[i] = nibble_to_uint32(&command[10 + 2*i], 2); + send = true; + break; + case 'r': + /* Send a classic RTR frame */ + frame.rtr = 1; + frame.sid = nibble_to_uint32(&command[1], 3); + frame.dlc = nibble_to_uint32(&command[4], 1); + for (i = 0; i < frame.dlc; i++) + frame.data8[i] = nibble_to_uint32(&command[5 + 2*i], 2); + send = true; + break; + case 'T': + frame.ide = 1; + frame.eid = nibble_to_uint32(&command[1], 8); + frame.dlc = nibble_to_uint32(&command[9], 1); + for (i = 0; i < frame.dlc; i++) + frame.data8[i] = nibble_to_uint32(&command[10 + 2*i], 2); + send = true; + break; + case 't': + frame.sid = nibble_to_uint32(&command[1], 3); + frame.dlc = nibble_to_uint32(&command[4], 1); + for (i = 0; i < frame.dlc; i++) + frame.data8[i] = nibble_to_uint32(&command[5 + 2*i], 2); + send = true; + break; + default: + break; + } + + if (send) { + ret = can_transmit(candrv, &frame, ms_to_ticks(10)); + if (ret) + kfile_write(&serial->fd, frame.ide ? "Z\r" : "z\r", 2); + else + kfile_write(&serial->fd, "\a", 1); + } return 0; } -int usb_can_emit(UNUSED_ARG(can_driver *, candrv), struct Serial *serial, can_rx_frame *frame, uint16_t timestamp) { +int usb_can_emit(UNUSED_ARG(can_driver *, candrv), struct Serial *serial, can_rx_frame *frame) { char buffer[32] = ""; int i = 0, j = 0; + uint16_t timestamp; + + /* The timestamp should wrap around every minute (it works until + the 32 bits of timer_clock are exhausted) */ + timestamp = ticks_to_ms(timer_clock()) % 60000; buffer[0] = frame->rtr ? 'r' : 't'; diff --git a/elec/boards/USB_CAN/Firmware/usb_can/usb_can.h b/elec/boards/USB_CAN/Firmware/usb_can/usb_can.h index 5a8c1ee..efc7d0c 100644 --- a/elec/boards/USB_CAN/Firmware/usb_can/usb_can.h +++ b/elec/boards/USB_CAN/Firmware/usb_can/usb_can.h @@ -16,6 +16,6 @@ int usb_can_execute_command(can_driver *candrv, struct Serial *serial, char *command); -int usb_can_emit(can_driver *candrv, struct Serial *serial, can_rx_frame *frame, uint16_t timestamp); +int usb_can_emit(can_driver *candrv, struct Serial *serial, can_rx_frame *frame); #endif /* !USB_CAN_H */ hooks/post-receive -- krobot |