2014-09-19 18:22:38 -07:00
|
|
|
/*
|
2016-11-04 07:18:34 -07:00
|
|
|
Copyright 2016 Benjamin Vedder benjamin@vedder.se
|
2014-09-19 18:22:38 -07:00
|
|
|
|
2016-11-04 07:18:34 -07:00
|
|
|
This file is part of the VESC firmware.
|
|
|
|
|
|
|
|
The VESC firmware is free software: you can redistribute it and/or modify
|
2014-09-19 18:22:38 -07:00
|
|
|
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.
|
|
|
|
|
2016-11-04 07:18:34 -07:00
|
|
|
The VESC firmware is distributed in the hope that it will be useful,
|
2014-09-19 18:22:38 -07:00
|
|
|
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 "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
#include "comm_usb.h"
|
|
|
|
#include "packet.h"
|
2015-10-08 14:09:39 -07:00
|
|
|
#include "comm_usb_serial.h"
|
2014-09-19 18:22:38 -07:00
|
|
|
#include "commands.h"
|
|
|
|
|
|
|
|
// Private variables
|
|
|
|
#define SERIAL_RX_BUFFER_SIZE 2048
|
|
|
|
static uint8_t serial_rx_buffer[SERIAL_RX_BUFFER_SIZE];
|
|
|
|
static int serial_rx_read_pos = 0;
|
|
|
|
static int serial_rx_write_pos = 0;
|
2020-03-22 09:36:14 -07:00
|
|
|
static THD_WORKING_AREA(serial_read_thread_wa, 256);
|
2015-10-08 14:09:39 -07:00
|
|
|
static THD_WORKING_AREA(serial_process_thread_wa, 4096);
|
|
|
|
static mutex_t send_mutex;
|
|
|
|
static thread_t *process_tp;
|
2020-01-12 12:25:21 -08:00
|
|
|
static volatile unsigned int write_timeout_cnt = 0;
|
2020-01-12 12:58:35 -08:00
|
|
|
static volatile bool was_timeout = false;
|
2021-01-12 14:15:01 -08:00
|
|
|
static PACKET_STATE_t packet_state;
|
2014-09-19 18:22:38 -07:00
|
|
|
|
|
|
|
// Private functions
|
2015-05-20 07:13:48 -07:00
|
|
|
static void process_packet(unsigned char *data, unsigned int len);
|
2019-02-18 10:30:19 -08:00
|
|
|
static void send_packet_raw(unsigned char *buffer, unsigned int len);
|
2014-09-19 18:22:38 -07:00
|
|
|
|
2015-10-08 14:09:39 -07:00
|
|
|
static THD_FUNCTION(serial_read_thread, arg) {
|
2014-09-19 18:22:38 -07:00
|
|
|
(void)arg;
|
|
|
|
|
2020-01-12 12:25:21 -08:00
|
|
|
chRegSetThreadName("USB read");
|
2014-09-19 18:22:38 -07:00
|
|
|
|
2020-01-12 12:25:21 -08:00
|
|
|
uint8_t buffer[2];
|
2014-09-19 18:22:38 -07:00
|
|
|
int had_data = 0;
|
|
|
|
|
|
|
|
for(;;) {
|
2019-02-19 13:06:34 -08:00
|
|
|
int len = chSequentialStreamRead(&SDU1, (uint8_t*) buffer, 1);
|
2014-09-19 18:22:38 -07:00
|
|
|
|
2019-02-19 13:06:34 -08:00
|
|
|
for (int i = 0;i < len;i++) {
|
2014-09-19 18:22:38 -07:00
|
|
|
serial_rx_buffer[serial_rx_write_pos++] = buffer[i];
|
|
|
|
|
|
|
|
if (serial_rx_write_pos == SERIAL_RX_BUFFER_SIZE) {
|
|
|
|
serial_rx_write_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
had_data = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (had_data) {
|
|
|
|
chEvtSignal(process_tp, (eventmask_t) 1);
|
|
|
|
had_data = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 14:09:39 -07:00
|
|
|
static THD_FUNCTION(serial_process_thread, arg) {
|
2014-09-19 18:22:38 -07:00
|
|
|
(void)arg;
|
|
|
|
|
2020-01-12 12:25:21 -08:00
|
|
|
chRegSetThreadName("USB process");
|
2014-09-19 18:22:38 -07:00
|
|
|
|
2015-10-08 14:09:39 -07:00
|
|
|
process_tp = chThdGetSelfX();
|
2014-09-19 18:22:38 -07:00
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
chEvtWaitAny((eventmask_t) 1);
|
|
|
|
|
|
|
|
while (serial_rx_read_pos != serial_rx_write_pos) {
|
2021-01-12 14:15:01 -08:00
|
|
|
packet_process_byte(serial_rx_buffer[serial_rx_read_pos++], &packet_state);
|
2014-09-19 18:22:38 -07:00
|
|
|
|
|
|
|
if (serial_rx_read_pos == SERIAL_RX_BUFFER_SIZE) {
|
|
|
|
serial_rx_read_pos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 07:13:48 -07:00
|
|
|
static void process_packet(unsigned char *data, unsigned int len) {
|
2019-03-04 10:23:38 -08:00
|
|
|
commands_process_packet(data, len, comm_usb_send_packet);
|
2014-09-19 18:22:38 -07:00
|
|
|
}
|
|
|
|
|
2019-02-18 10:30:19 -08:00
|
|
|
static void send_packet_raw(unsigned char *buffer, unsigned int len) {
|
2020-01-12 12:25:21 -08:00
|
|
|
/*
|
|
|
|
* Only write to USB if the cable has been connected at least once. If a timeout occurs
|
|
|
|
* make sure that this call does not stall on the next call, as the timeout probably occured
|
|
|
|
* because noone is listening on the USB.
|
|
|
|
*/
|
|
|
|
if (comm_usb_serial_configured_cnt() > 0) {
|
|
|
|
unsigned int written = 0;
|
|
|
|
if (was_timeout) {
|
|
|
|
written = SDU1.vmt->writet(&SDU1, buffer, len, TIME_IMMEDIATE);
|
|
|
|
} else {
|
|
|
|
written = SDU1.vmt->writet(&SDU1, buffer, len, MS2ST(100));
|
|
|
|
}
|
|
|
|
|
|
|
|
was_timeout = written != len;
|
|
|
|
if (was_timeout) {
|
|
|
|
write_timeout_cnt++;
|
|
|
|
}
|
|
|
|
}
|
2014-09-19 18:22:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void comm_usb_init(void) {
|
2015-10-08 14:09:39 -07:00
|
|
|
comm_usb_serial_init();
|
2021-01-12 14:15:01 -08:00
|
|
|
packet_init(send_packet_raw, process_packet, &packet_state);
|
2014-09-19 18:22:38 -07:00
|
|
|
|
2015-10-08 14:09:39 -07:00
|
|
|
chMtxObjectInit(&send_mutex);
|
2014-09-19 18:22:38 -07:00
|
|
|
|
|
|
|
// Threads
|
|
|
|
chThdCreateStatic(serial_read_thread_wa, sizeof(serial_read_thread_wa), NORMALPRIO, serial_read_thread, NULL);
|
|
|
|
chThdCreateStatic(serial_process_thread_wa, sizeof(serial_process_thread_wa), NORMALPRIO, serial_process_thread, NULL);
|
|
|
|
}
|
2019-02-18 10:30:19 -08:00
|
|
|
|
|
|
|
void comm_usb_send_packet(unsigned char *data, unsigned int len) {
|
|
|
|
chMtxLock(&send_mutex);
|
2021-01-12 14:15:01 -08:00
|
|
|
packet_send_packet(data, len, &packet_state);
|
2019-02-18 10:30:19 -08:00
|
|
|
chMtxUnlock(&send_mutex);
|
|
|
|
}
|
2020-01-12 12:25:21 -08:00
|
|
|
|
|
|
|
unsigned int comm_usb_get_write_timeout_cnt(void) {
|
|
|
|
return write_timeout_cnt;
|
|
|
|
}
|