1 Star 0 Fork 2

lelongGITEE/bldc

forked from zhanshenrui/bldc 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
comm_usb.c 3.75 KB
一键复制 编辑 原始数据 按行查看 历史
zhanshenrui 提交于 2018-09-19 21:06 . b
/*
Copyright 2016 Benjamin Vedder benjamin@vedder.se
This file is part of the VESC firmware.
The VESC firmware 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.
The VESC firmware 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 "ch.h"
#include "hal.h"
#include "comm_usb.h"
#include "packet.h"
#include "comm_usb_serial.h"
#include "commands.h"
// Settings
#define PACKET_HANDLER 0
// 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;
static THD_WORKING_AREA(serial_read_thread_wa, 512);
static THD_WORKING_AREA(serial_process_thread_wa, 4096);
static mutex_t send_mutex;
static thread_t *process_tp;
// Private functions
static void process_packet(unsigned char *data, unsigned int len);
static void send_packet(unsigned char *buffer, unsigned int len);
static void send_packet_wrapper(unsigned char *data, unsigned int len);
//从SDU1设备里读数据到serial_rx_buffer里,然后发信号给process_tp线程
static THD_FUNCTION(serial_read_thread, arg) {
(void)arg;
chRegSetThreadName("USB-Serial read");
uint8_t buffer[128];
int i;
int len;
int had_data = 0;
for(;;) {
len = chSequentialStreamRead(&SDU1, (uint8_t*) buffer, 1);
for (i = 0;i < len;i++) {
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;
}
}
}
//
static THD_FUNCTION(serial_process_thread, arg) {
(void)arg;
chRegSetThreadName("USB-Serial process");
process_tp = chThdGetSelfX();
for(;;) {
chEvtWaitAny((eventmask_t) 1);
while (serial_rx_read_pos != serial_rx_write_pos) {
packet_process_byte(serial_rx_buffer[serial_rx_read_pos++], PACKET_HANDLER);
if (serial_rx_read_pos == SERIAL_RX_BUFFER_SIZE) {
serial_rx_read_pos = 0;
}
}
}
}
//设置发送函数,根据不同上位机命令作不同处理
static void process_packet(unsigned char *data, unsigned int len) {
commands_set_send_func(send_packet_wrapper);//这里只是设置发送函数,并没有调用发送函数
commands_process_packet(data, len); //根据不同上位机命令作不同处理
}
//申请互斥锁,然后将数据data添加到handler_num所在的发送缓冲区里,然后由handler_num代表的硬件发送缓冲区里的数据,最后释放互斥锁
static void send_packet_wrapper(unsigned char *data, unsigned int len) {
chMtxLock(&send_mutex);
packet_send_packet(data, len, PACKET_HANDLER);//将数据data添加到handler_num所在的发送缓冲区里,然后由handler_num代表的硬件发送缓冲区里的数据
chMtxUnlock(&send_mutex);
}
static void send_packet(unsigned char *buffer, unsigned int len) {
chSequentialStreamWrite(&SDU1, buffer, len);
}
//初始化并启动USB串口,设置串口接收发送函数,分配互斥锁,创建线程serial_read_thread,serial_process_thread
void comm_usb_init(void) {
comm_usb_serial_init();//初始化并启动USB串口
packet_init(send_packet, process_packet, PACKET_HANDLER);//设置接收,发送函数
chMtxObjectInit(&send_mutex);
// 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);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/lelongz/bldc.git
git@gitee.com:lelongz/bldc.git
lelongz
bldc
bldc
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385