From: Nicolas D. <Ba...@us...> - 2011-04-10 18:55:02
|
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 5a886f436c126dc5f45bcb7f6c51daac842b0247 (commit) via 8fb9994adad41e9ee8eded43f0ede77b4e0d56d3 (commit) from a76fb714ce409ede1b0357665e51a6b6c3bb3878 (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 5a886f436c126dc5f45bcb7f6c51daac842b0247 Author: Nicolas Dandrimont <Nic...@cr...> Date: Sun Apr 10 20:51:21 2011 +0200 [USB_CAN] Drop BeRTOS's serial driver for one of our own This driver should be lighter and thus faster than the one from BeRTOS. The card now also sends an error CAN packet to the USB side when it gets unexpected data from the serial port. commit 8fb9994adad41e9ee8eded43f0ede77b4e0d56d3 Author: Nicolas Dandrimont <Nic...@cr...> Date: Sun Apr 10 20:50:40 2011 +0200 [USB_CAN] Fix debug module ----------------------------------------------------------------------- Changes: diff --git a/elec/boards/USB_CAN/Firmware/bertos/cpu/cortex-m3/drv/kdebug_stm32.c b/elec/boards/USB_CAN/Firmware/bertos/cpu/cortex-m3/drv/kdebug_stm32.c index a60d19e..d6b1582 100644 --- a/elec/boards/USB_CAN/Firmware/bertos/cpu/cortex-m3/drv/kdebug_stm32.c +++ b/elec/boards/USB_CAN/Firmware/bertos/cpu/cortex-m3/drv/kdebug_stm32.c @@ -77,9 +77,9 @@ INLINE void kdbg_hw_init(void) RCC->APB2ENR |= RCC_APB2_AFIO; /* Configure USART pins */ #if CONFIG_KDEBUG_PORT == 0 - RCC->APB2ENR |= RCC_APB2_GPIOA; + RCC->APB2ENR |= RCC_APB2_GPIOB; RCC->APB2ENR |= RCC_APB2_USART1; - stm32_gpioPinConfig((struct stm32_gpio *)GPIOA_BASE, GPIO_USART1_TX_PIN, + stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, GPIO_USART1_TX_PIN, GPIO_MODE_AF_PP, GPIO_SPEED_50MHZ); #elif CONFIG_KDEBUG_PORT == 1 RCC->APB2ENR |= RCC_APB2_GPIOA; diff --git a/elec/boards/USB_CAN/Firmware/bertos/cpu/cortex-m3/drv/ser_stm32.c b/elec/boards/USB_CAN/Firmware/bertos/cpu/cortex-m3/drv/ser_stm32.c index 0b8c4f3..5ba253a 100644 --- a/elec/boards/USB_CAN/Firmware/bertos/cpu/cortex-m3/drv/ser_stm32.c +++ b/elec/boards/USB_CAN/Firmware/bertos/cpu/cortex-m3/drv/ser_stm32.c @@ -82,10 +82,10 @@ static const struct gpio_uart_info gpio_uart[SER_CNT] = { /* UART1 */ { - .base = GPIOA_BASE, + .base = GPIOB_BASE, .rx_pin = GPIO_USART1_RX_PIN, .tx_pin = GPIO_USART1_TX_PIN, - .sysctl_gpio = RCC_APB2_GPIOA, + .sysctl_gpio = RCC_APB2_GPIOB, .sysctl_usart = RCC_APB2_USART1, }, /* UART2 */ diff --git a/elec/boards/USB_CAN/Firmware/bertos/cpu/cortex-m3/io/stm32.h b/elec/boards/USB_CAN/Firmware/bertos/cpu/cortex-m3/io/stm32.h index 49ee9b1..7496baa 100644 --- a/elec/boards/USB_CAN/Firmware/bertos/cpu/cortex-m3/io/stm32.h +++ b/elec/boards/USB_CAN/Firmware/bertos/cpu/cortex-m3/io/stm32.h @@ -57,8 +57,8 @@ #define GPIO_USART2_TX_PIN BV(2) #define GPIO_USART2_RX_PIN BV(3) #elif CPU_CM3_STM32F103RB || CPU_CM3_STM32F103RE - #define GPIO_USART1_TX_PIN BV(9) - #define GPIO_USART1_RX_PIN BV(10) + #define GPIO_USART1_TX_PIN BV(6) + #define GPIO_USART1_RX_PIN BV(7) #define GPIO_USART2_TX_PIN BV(2) #define GPIO_USART2_RX_PIN BV(3) #define GPIO_USART3_TX_PIN BV(10) diff --git a/elec/boards/USB_CAN/Firmware/project.bertos b/elec/boards/USB_CAN/Firmware/project.bertos index db0a5ab..14fd002 100644 --- a/elec/boards/USB_CAN/Firmware/project.bertos +++ b/elec/boards/USB_CAN/Firmware/project.bertos @@ -29,50 +29,46 @@ p13 (lp14 S'signal' p15 -aS'debug' -p16 aS'kernel' +p16 +aS'monitor' p17 -aS'ser' -p18 aS'kfile' -p19 +p18 aS'sprintf' +p19 +aS'timer' p20 -aS'monitor' +aS'formatwr' p21 -aS'timer' +aS'semaphores' p22 -aS'formatwr' +aS'heap' p23 -aS'semaphores' +aS'can' p24 -aS'heap' +aS'i2c' p25 -aS'can' +asS'CPU_NAME' p26 -aS'i2c' +VSTM32F103RE p27 -asS'CPU_NAME' +sS'PROJECT_HW_PATH' p28 -VSTM32F103RE +Vusb_can p29 -sS'PROJECT_HW_PATH' +sS'PROJECT_SRC_PATH' p30 Vusb_can p31 -sS'PROJECT_SRC_PATH' -p32 -Vusb_can -p33 sS'PRESET' -p34 +p32 NsS'PROJECT_SRC_PATH_FROM_MAKEFILE' -p35 +p33 g7 sS'OUTPUT' -p36 -(lp37 +p34 +(lp35 S'codelite' -p38 +p36 as. \ No newline at end of file diff --git a/elec/boards/USB_CAN/Firmware/usb_can/cfg/cfg_context_switch.h b/elec/boards/USB_CAN/Firmware/usb_can/cfg/cfg_context_switch.h index fe0b4f8..2607d50 100644 --- a/elec/boards/USB_CAN/Firmware/usb_can/cfg/cfg_context_switch.h +++ b/elec/boards/USB_CAN/Firmware/usb_can/cfg/cfg_context_switch.h @@ -45,7 +45,7 @@ * $WIZ$ module= "boolean" * $WIZ$ conditional_deps = "ser" */ -#define CONFIG_USE_HP_TIMER 1 +#define CONFIG_USE_HP_TIMER 0 /** diff --git a/elec/boards/USB_CAN/Firmware/usb_can/cfg/cfg_debug.h b/elec/boards/USB_CAN/Firmware/usb_can/cfg/cfg_debug.h index e922362..1063984 100644 --- a/elec/boards/USB_CAN/Firmware/usb_can/cfg/cfg_debug.h +++ b/elec/boards/USB_CAN/Firmware/usb_can/cfg/cfg_debug.h @@ -49,7 +49,7 @@ * Baudrate for the debug console. * $WIZ$ type = "int"; min = 300 */ -#define CONFIG_KDEBUG_BAUDRATE 38400UL +#define CONFIG_KDEBUG_BAUDRATE 115200UL /** * Clock source for the UART module. You need to write the code to reprogram the respective clock at the required frequency in your project before calling kdbg_init(). diff --git a/elec/boards/USB_CAN/Firmware/usb_can/main.c b/elec/boards/USB_CAN/Firmware/usb_can/main.c index 3f61e9a..4f984b8 100644 --- a/elec/boards/USB_CAN/Firmware/usb_can/main.c +++ b/elec/boards/USB_CAN/Firmware/usb_can/main.c @@ -28,11 +28,11 @@ #include <cpu/irq.h> +#include <drv/clock_stm32.h> #include <drv/gpio_stm32.h> #include <drv/can.h> #include <drv/i2c.h> -#include <drv/ser.h> #include <drv/timer.h> #include <kern/monitor.h> @@ -40,14 +40,13 @@ #include <io/kfile.h> #include "battery_monitoring/battery_monitoring.h" +#include "usb_can/serial.h" #include "usb_can/usb_can.h" #define SERIAL_BAUDRATE 1000000 PROC_DEFINE_STACK(stack_blinky, KERN_MINSTACKSIZE * 2); -static struct Serial ser; - static I2c i2c; static void init(void) @@ -67,8 +66,11 @@ static void init(void) /* Enable all the interrupts */ IRQ_ENABLE; + //RCC->APB2ENR |= RCC_APB2_AFIO; + //stm32_gpioRemap(GPIO_REMAP_USART1, GPIO_REMAP_ENABLE); + /* Initialize debugging module (allow kprintf(), etc.) */ - kdbg_init(); + //kdbg_init(); /* Initialize system timer */ timer_init(); @@ -84,8 +86,7 @@ static void init(void) LED3_ON(); /* Initialize Serial driver */ - ser_init(&ser, SER_UART3); - ser_setbaudrate(&ser, SERIAL_BAUDRATE); + serial_init(SERIAL_BAUDRATE); /* Initialize I2c interface */ i2c_init(&i2c, 0, CONFIG_I2C_FREQ); @@ -97,7 +98,7 @@ static void init(void) proc_init(); /* Initialize USB-CAN logic */ - usbcan = usb_can_init(CAND1, &ser); + usbcan = usb_can_init(CAND1); /* Initialize battery monitoring */ battery_monitoring_init(usbcan, &i2c); diff --git a/elec/boards/USB_CAN/Firmware/usb_can/usb_can.mk b/elec/boards/USB_CAN/Firmware/usb_can/usb_can.mk index 5467363..2f28cec 100644 --- a/elec/boards/USB_CAN/Firmware/usb_can/usb_can.mk +++ b/elec/boards/USB_CAN/Firmware/usb_can/usb_can.mk @@ -4,7 +4,7 @@ # # Constants automatically defined by the selected modules -Firmware_DEBUG = 1 + # Our target application TRG += usb_can @@ -19,26 +19,24 @@ usb_can_HW_PATH = usb_can # Files automatically generated by the wizard. DO NOT EDIT, USE usb_can_USER_CSRC INSTEAD! usb_can_WIZARD_CSRC = \ - bertos/cpu/cortex-m3/drv/ser_stm32.c \ - bertos/cpu/cortex-m3/hw/switch_ctx_cm3.c \ - bertos/mware/event.c \ - bertos/kern/sem.c \ - bertos/kern/monitor.c \ - bertos/cpu/cortex-m3/drv/timer_cm3.c \ - bertos/struct/heap.c \ - bertos/drv/i2c_bitbang.c \ bertos/cpu/cortex-m3/drv/can_stm32.c \ - bertos/io/kfile.c \ - bertos/mware/formatwr.c \ - bertos/mware/sprintf.c \ - bertos/drv/can.c \ bertos/cpu/cortex-m3/drv/i2c_stm32.c \ + bertos/cpu/cortex-m3/drv/timer_cm3.c \ + bertos/cpu/cortex-m3/hw/switch_ctx_cm3.c \ + bertos/drv/can.c \ + bertos/drv/i2c.c \ + bertos/drv/i2c_bitbang.c \ bertos/drv/timer.c \ - bertos/kern/signal.c \ + bertos/io/kfile.c \ + bertos/kern/monitor.c \ bertos/kern/proc.c \ - bertos/drv/ser.c \ + bertos/kern/sem.c \ + bertos/kern/signal.c \ + bertos/mware/event.c \ + bertos/mware/formatwr.c \ bertos/mware/hex.c \ - bertos/drv/i2c.c \ + bertos/mware/sprintf.c \ + bertos/struct/heap.c \ # # Files automatically generated by the wizard. DO NOT EDIT, USE usb_can_USER_PCSRC INSTEAD! diff --git a/elec/boards/USB_CAN/Firmware/usb_can/usb_can/serial.c b/elec/boards/USB_CAN/Firmware/usb_can/usb_can/serial.c new file mode 100644 index 0000000..39f973f --- /dev/null +++ b/elec/boards/USB_CAN/Firmware/usb_can/usb_can/serial.c @@ -0,0 +1,119 @@ +/** + * USB-CAN converter + * + * This file contains the interface for a very simple serial driver for STM32 + * + * 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 "serial.h" + +#define ENDLINE '\r' +#define RX_BUF_SIZE 4096 + +static char receive_buffer[RX_BUF_SIZE]; +static uint64_t read; +static uint64_t write; + +static DECLARE_ISR(uart_irq_handler) { + struct stm32_usart *base = (struct stm32_usart *)USART3_BASE; + + base->CR1 &= ~(BV(CR1_RXNEIE)); + while (base->SR & (BV(SR_RXNE) | BV(SR_ORE))) { + + receive_buffer[write % RX_BUF_SIZE] = base->DR; + write++; + } + base->CR1 |= BV(CR1_RXNEIE); +} + +// Hardcoded to port 3 +struct stm32_usart *serial_init(unsigned long baudrate) { + struct stm32_usart *base = (struct stm32_usart *)USART3_BASE; + + read = 0; + write = 0; + + // Enable clocks + RCC->APB2ENR |= RCC_APB2_AFIO | RCC_APB2_GPIOB; + RCC->APB1ENR |= RCC_APB1_USART3; + + // Enable pins + stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, BV(10), + GPIO_MODE_AF_PP, GPIO_SPEED_50MHZ); + stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, BV(11), + GPIO_MODE_IN_FLOATING, GPIO_SPEED_50MHZ); + + // Clear registers + base->CR2 = 0; + base->CR1 = 0; + base->CR3 = 0; + base->SR = 0; + + // Set baudrate + base->BRR = evaluate_brr(base, CPU_FREQ, baudrate); + + // No parity, 8 bits, 1 stop bit : no further configuration needed + + // Enable TX and RX + base->CR1 |= BV(CR1_TE) | BV(CR1_RE); + + // Enable the RX interrupt + sysirq_setHandler(USART3_IRQHANDLER, uart_irq_handler); + + base->CR1 |= BV(CR1_RXNEIE); + + // Start up + base->CR1 |= BV(CR1_UE); + + return base; +} + +size_t serial_readline(char *buffer, size_t max_len) { + size_t i = 0; + char c; + + while (i < max_len-1) { + if (read == write) { + cpu_relax(); + continue; + } + c = receive_buffer[read % RX_BUF_SIZE]; + read++; + if (c == ENDLINE) { + break; + } + buffer[i] = c; + i++; + } + buffer[i] = '\0'; + + return i; +} + +void serial_transmit(const char *buffer, size_t len) { + struct stm32_usart *base = (struct stm32_usart *)USART3_BASE; + + size_t i; + + for(i=0; i < len; i++) { + while (!(base->SR & BV(SR_TXE))) + cpu_relax(); + base->DR = buffer[i]; + } +} diff --git a/elec/boards/USB_CAN/Firmware/usb_can/usb_can/serial.h b/elec/boards/USB_CAN/Firmware/usb_can/usb_can/serial.h new file mode 100644 index 0000000..c6e5488 --- /dev/null +++ b/elec/boards/USB_CAN/Firmware/usb_can/usb_can/serial.h @@ -0,0 +1,48 @@ +/** + * USB-CAN converter + * + * This file contains the interface for a very simple serial driver for STM32 + * + * 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/>. + * + */ + +#ifndef SERIAL_H +#define SERIAL_H + +#include <string.h> + +#include <cpu/irq.h> +#include <cpu/power.h> + +#include <drv/clock_stm32.h> +#include <drv/gpio_stm32.h> +#include <drv/irq_cm3.h> +#include <drv/timer.h> + +#include "hw/hw_led.h" + +#include <io/stm32_uart.h> + +#include <kern/sem.h> + +struct stm32_usart *serial_init(unsigned long baudrate); + +size_t serial_readline(char *buffer, size_t max_len); +void serial_transmit(const char *buffer, size_t len); + +#endif diff --git a/elec/boards/USB_CAN/Firmware/usb_can/usb_can/usb_can.c b/elec/boards/USB_CAN/Firmware/usb_can/usb_can/usb_can.c index 1e5f360..f1e6314 100644 --- a/elec/boards/USB_CAN/Firmware/usb_can/usb_can/usb_can.c +++ b/elec/boards/USB_CAN/Firmware/usb_can/usb_can/usb_can.c @@ -62,10 +62,9 @@ uint16_t get_timestamp(void) { return ticks_to_ms(timer_clock()) % 60000; } -usb_can *usb_can_init(can_driver *can, struct Serial *ser) { +usb_can *usb_can_init(can_driver *can) { usbcan->can = can; - usbcan->ser = ser; usbcan->is_open = false; usbcan->timestamped = false; usbcan->get_timestamp = get_timestamp; @@ -79,35 +78,38 @@ usb_can *usb_can_init(can_driver *can, struct Serial *ser) { void usb_can_open(usb_can *usbcan) { if (usbcan->is_open) - kfile_write(&usbcan->ser->fd, "\a", 1); + serial_transmit("\a", 1); else { usbcan->is_open = true; - kfile_write(&usbcan->ser->fd, "\r", 1); + serial_transmit("\r", 1); } } void usb_can_close(usb_can *usbcan) { if (usbcan->is_open) { usbcan->is_open = false; - kfile_write(&usbcan->ser->fd, "\r", 1); + serial_transmit("\r", 1); } else { - kfile_write(&usbcan->ser->fd, "\a", 1); + serial_transmit("\a", 1); } } void usb_can_set_baudrate(usb_can *usbcan, char *baudrate) { /* FIXME: Implement it for real! */ (void)baudrate; - kfile_write(&usbcan->ser->fd, "\r", 1); + (void)usbcan; + serial_transmit("\r", 1); } -int usb_can_execute_command(usb_can *usbcan, char *command) { +int usb_can_execute_command(usb_can *usbcan, char *command, size_t len) { can_tx_frame frame; - bool send = false, ret = false; + bool send = false, ret = false, broken = false; int i; + can_rx_frame error_frame; + frame.rtr = 0; frame.ide = 0; frame.eid = 0; @@ -116,16 +118,51 @@ int usb_can_execute_command(usb_can *usbcan, char *command) { frame.data32[0] = 0; frame.data32[1] = 0; - sem_obtain(&usbcan->sem_receive); + error_frame.rtr = 0; + error_frame.ide = 1; + error_frame.eid = 0x4242; + error_frame.dlc = 0; + + while(!sem_attempt(&usbcan->sem_receive)) + cpu_relax(); switch (command[0]) { + case 'R': + /* Send an extended RTR frame */ + frame.rtr = 1; + /* Fallthrough */ + 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); + if (len == (size_t)(8 + 1 + 2*frame.dlc)) + send = true; + else + broken = true; + break; + case 'r': + /* Send a classic RTR frame */ + frame.rtr = 1; + /* Fallthrough */ + 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); + if (len == (size_t)(3 + 1 + 2*frame.dlc)) + send = true; + else + broken = true; + break; case 'V': /* Version number */ - kfile_write(&usbcan->ser->fd, "V0402\r", strlen("V0402\r")); + serial_transmit("V0402\r", strlen("V0402\r")); break; case 'N': /* Serial number */ - kfile_write(&usbcan->ser->fd, "NKROB\r", strlen("NKROB\r")); + serial_transmit("NKROB\r", strlen("NKROB\r")); break; case 'O': /* Open CAN Channel */ @@ -143,55 +180,30 @@ int usb_can_execute_command(usb_can *usbcan, char *command) { usbcan->timestamped = false; else if (command[0] == '1') usbcan->timestamped = true; - kfile_write(&usbcan->ser->fd, "\r", 1); - 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; + serial_transmit("\r", 1); break; default: break; } if (send) { - ret = can_transmit(usbcan->can, &frame, ms_to_ticks(10)); - if (ret) - kfile_write(&usbcan->ser->fd, "\r", 1); - else - kfile_write(&usbcan->ser->fd, "\a", 1); + if (!broken) { + ret = can_transmit(usbcan->can, &frame, ms_to_ticks(10)); + if (ret) + serial_transmit("\r", 1); + else + serial_transmit("\a", 1); + } else { + serial_transmit("\a", 1); + } } + + // You are in a maze of twisty little deadlocks, all alike sem_release(&usbcan->sem_receive); + if (send && (broken || !ret)) + usb_can_emit(usbcan, &error_frame); + return 0; } @@ -202,8 +214,8 @@ int usb_can_emit(usb_can *usbcan, can_rx_frame *frame) { uint16_t timestamp; // Do not interfere with the send ACK - if (!sem_attempt(&usbcan->sem_receive)) - return 1; + while(!sem_attempt(&usbcan->sem_receive)) + cpu_relax(); buffer[0] = frame->rtr ? 'r' : 't'; @@ -234,7 +246,8 @@ int usb_can_emit(usb_can *usbcan, can_rx_frame *frame) { buffer[i] = '\r'; i++; - kfile_write(&usbcan->ser->fd, buffer, i); + serial_transmit(buffer, i); + //serial_transmit("T000044420\r", strlen("T000044420\r")); sem_release(&usbcan->sem_receive); return 0; @@ -243,18 +256,17 @@ int usb_can_emit(usb_can *usbcan, can_rx_frame *frame) { static void NORETURN serial_receive_process(void) { int nbytes = 0, retval, i = 0; - char command[MAX_CMD_SIZE+1]; + char command[MAX_CMD_SIZE]; for (;;) { i = !i; - nbytes = kfile_gets(&usbcan->ser->fd, command, MAX_CMD_SIZE+1); - if (nbytes != EOF) { - retval = usb_can_execute_command(usbcan, command); - if (i) - LED1_ON(); - else - LED1_OFF(); - } + nbytes = serial_readline(command, MAX_CMD_SIZE); + retval = usb_can_execute_command(usbcan, command, nbytes); + if (i) + LED1_ON(); + else + LED1_OFF(); + cpu_relax(); } } diff --git a/elec/boards/USB_CAN/Firmware/usb_can/usb_can/usb_can.h b/elec/boards/USB_CAN/Firmware/usb_can/usb_can/usb_can.h index 6aed083..3f027ce 100644 --- a/elec/boards/USB_CAN/Firmware/usb_can/usb_can/usb_can.h +++ b/elec/boards/USB_CAN/Firmware/usb_can/usb_can/usb_can.h @@ -29,16 +29,16 @@ #include <kern/sem.h> #include <drv/can.h> -#include <drv/ser.h> #include <drv/timer.h> #include <hw/hw_led.h> #include <io/kfile.h> +#include "serial.h" + typedef uint16_t (usb_can_timestamp(void)); typedef struct _usb_can { can_driver *can; // CAN Device - struct Serial *ser; // Serial Device bool is_open; // Channel open? bool timestamped; // Emit timestamps? usb_can_timestamp *get_timestamp; // Get Timestamp function @@ -47,14 +47,14 @@ typedef struct _usb_can { uint16_t get_timestamp(void); -usb_can *usb_can_init(can_driver *can, struct Serial *ser); +usb_can *usb_can_init(can_driver *can); void usb_can_open(usb_can *usbcan); void usb_can_close(usb_can *usbcan); void usb_can_set_baudrate(usb_can *usbcan, char *baudrate); -int usb_can_execute_command(usb_can *usbcan, char *command); +int usb_can_execute_command(usb_can *usbcan, char *command, size_t len); int usb_can_emit(usb_can *usbcan, can_rx_frame *frame); diff --git a/elec/boards/USB_CAN/Firmware/usb_can/usb_can_user.mk b/elec/boards/USB_CAN/Firmware/usb_can/usb_can_user.mk index 8bfea09..d6bff03 100644 --- a/elec/boards/USB_CAN/Firmware/usb_can/usb_can_user.mk +++ b/elec/boards/USB_CAN/Firmware/usb_can/usb_can_user.mk @@ -11,6 +11,7 @@ usb_can_PROGRAMMER_PORT = none usb_can_USER_CSRC = \ $(usb_can_SRC_PATH)/battery_monitoring/ads7828.c \ $(usb_can_SRC_PATH)/battery_monitoring/battery_monitoring.c \ + $(usb_can_SRC_PATH)/usb_can/serial.c \ $(usb_can_SRC_PATH)/usb_can/usb_can.c \ $(usb_can_SRC_PATH)/main.c \ # hooks/post-receive -- krobot |