8 changed files with 340 additions and 100 deletions
-
4.vscode/settings.json
-
48CMakeLists.txt
-
93README.md
-
180demo/adc_capture/libiflytop_adc_capture.c
-
58demo/adc_capture/libiflytop_adc_capture.h
-
37demo/adc_capture/main.cpp
-
4sh/build.sh
-
16sh/deploy.sh
@ -1,77 +1,28 @@ |
|||
# linux_demo_project |
|||
|
|||
``` |
|||
功能: |
|||
1. 打通CAN通道 |
|||
2. 打通ws通道, |
|||
2. 解析CAN消息,并上报给ws |
|||
3. 解析ws消息并发送给CAN |
|||
|
|||
TOOD: |
|||
1. 机械臂扫码功能 |
|||
2. |
|||
|
|||
测试: |
|||
1. websocket下发一条can指令,can卡能够收到 |
|||
2. can卡上报一条指令,websocket能够收到 |
|||
|
|||
TODO: |
|||
1. 删除掉虚拟从设备的相关功能 |
|||
2. 增加扩展寄存器包上报拦截汇总功能 |
|||
3. 增加httpRPC借口 |
|||
4. 增加ws事件上报通道 |
|||
5. 增加EEPROM |
|||
``` |
|||
|
|||
## 编译RK3328 |
|||
```bash |
|||
# 板子初始化 |
|||
./boardinit.sh \ |
|||
--device_info "DEVICE_ID=a8000_1" \ |
|||
--device_info "FRP_SERVER_ADDR=47.92.195.73" \ |
|||
--device_info "STARTUP_DEFAULT_URL=127.0.0.1:80" \ |
|||
--ip 192.168.1.164 |
|||
|
|||
|
|||
# 初始化环境 |
|||
./build.sh envsetup |
|||
|
|||
# 仅编译 |
|||
./build.sh build |
|||
# 编译调试版本,Cmake使用的是cmakepc.cmake |
|||
./build.sh buildhost |
|||
|
|||
# 编译打包部署(整包) |
|||
./build.sh --ip <ip> flash |
|||
|
|||
# 编译打包部署,重启系统(整包) |
|||
./build.sh --ip <ip> flash reboot |
|||
|
|||
# 编译并只部署应用 |
|||
./build.sh --ip <ip> flashapp "app.out" |
|||
|
|||
# 只部署webapp |
|||
./build.sh --ip <ip> flashwebapp |
|||
|
|||
# 配置ssh-key,免密登录 |
|||
./build.sh --ip <ip> ssh_copy_id |
|||
1. 485 |
|||
2. 232-1 |
|||
3. 232-2 |
|||
3. USB |
|||
4. 网口*2 |
|||
5. WIFI |
|||
6. 4G |
|||
7. 硬盘 |
|||
|
|||
--------------------------------- |
|||
DEMO: |
|||
MQTT |
|||
FTP |
|||
HTTP |
|||
485 |
|||
232-1 |
|||
232-2 |
|||
ADC |
|||
4G |
|||
硬盘 |
|||
修改用户 |
|||
DC |
|||
|
|||
``` |
|||
|
|||
测试指令 |
|||
```json |
|||
{ |
|||
"channel":"can0", |
|||
"protocol":"iflytopCanProtocolStackV1", |
|||
"message": { |
|||
"attribute": "normal", |
|||
"priority": 4, |
|||
"type": "read", |
|||
"targetId": 129, |
|||
"sourceId": 1, |
|||
"seq": 0, |
|||
"regAdd": 0, |
|||
"regValue": 100 |
|||
} |
|||
} |
|||
``` |
@ -0,0 +1,180 @@ |
|||
#include "libiflytop_adc_capture.h" |
|||
|
|||
#include <errno.h> |
|||
#include <fcntl.h> |
|||
#include <getopt.h> |
|||
#include <linux/spi/spidev.h> |
|||
#include <linux/types.h> |
|||
#include <pthread.h> |
|||
#include <stdbool.h> |
|||
#include <stdint.h> |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> //for strerror() |
|||
#include <sys/ioctl.h> |
|||
#include <unistd.h> |
|||
static const char *m_deviceName = NULL; |
|||
static sound_capture_callback_t m_callback = NULL; |
|||
static int m_fd = 0; |
|||
static int m_sys_error = 0; |
|||
static error_code_t m_last_error = ksp_success; |
|||
static int m_cs_gpio_id = 0; |
|||
|
|||
static int m_gpiofd = 0; |
|||
|
|||
static pthread_t sound_read_thread; |
|||
static bool inited = false; |
|||
static bool started = false; |
|||
|
|||
#if 0 |
|||
int spi_readwrte(unsigned char *txbuf, unsigned char *rxbuf, int len) { |
|||
struct spi_ioc_transfer tr = { |
|||
.tx_buf = (unsigned long *)txbuf, // 发送缓存区 |
|||
.rx_buf = (unsigned long *)rxbuf, // 接收缓存区 |
|||
.len = len / 4, |
|||
.delay_usecs = delay, // 发送时间间隔 |
|||
.speed_hz = speed, // 总线速率 |
|||
.bits_per_word = bits, // 收发的一个字的二进制位数 |
|||
}; |
|||
|
|||
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); |
|||
if (ret < 1) pabort("can't send spi message"); |
|||
|
|||
for (ret = 0; ret < len / 4; ret++) { |
|||
if (!(ret % 6)) puts(""); |
|||
printf("%.2X ", rx[ret]); |
|||
} |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
int spi_close() { |
|||
close(fd); |
|||
return 0; |
|||
} |
|||
#endif |
|||
|
|||
#define IOCTL(vfd, vkey, vvalue, errorcode) \ |
|||
{ \ |
|||
int ret = ioctl(vfd, vkey, &vvalue); \ |
|||
if (ret != 0) { \ |
|||
close(vfd); \ |
|||
vfd = -1; \ |
|||
m_sys_error = errno; \ |
|||
m_last_error = errorcode; \ |
|||
return errorcode; \ |
|||
} \ |
|||
} |
|||
|
|||
static void *sound_read_thread_func(void *v); |
|||
|
|||
static void gpio_set_value(int value) { write(m_gpiofd, value ? "1\n" : "0\n", 2); } |
|||
|
|||
error_code_t sound_capturer_init(const char *deviceName, int csgpionameId, sound_capture_callback_t callback) { |
|||
m_callback = callback; |
|||
m_cs_gpio_id = csgpionameId; |
|||
// |
|||
m_fd = open(deviceName, O_RDWR); |
|||
if (m_fd < 0) { |
|||
m_sys_error = errno; |
|||
m_last_error = ksp_init_spi_error_open_fail; |
|||
return ksp_init_spi_error_open_fail; |
|||
} |
|||
// 设置模式 |
|||
int mode = SPI_MODE_3; |
|||
int bits = 8; |
|||
int speed = 3000000; |
|||
|
|||
// SPI初始化 |
|||
IOCTL(m_fd, SPI_IOC_WR_MODE, mode, ksp_init_spi_error_set_mode_fail); |
|||
IOCTL(m_fd, SPI_IOC_WR_BITS_PER_WORD, bits, ksp_init_spi_error_set_mode_fail); |
|||
IOCTL(m_fd, SPI_IOC_WR_MAX_SPEED_HZ, speed, ksp_init_spi_error_set_mode_fail); |
|||
|
|||
char cmd[256] = {0}; |
|||
sprintf(cmd, "echo %d > /sys/class/gpio/export", m_cs_gpio_id); |
|||
system(cmd); |
|||
|
|||
sprintf(cmd, "echo out > /sys/class/gpio/gpio%d/direction", m_cs_gpio_id); |
|||
system(cmd); |
|||
|
|||
sprintf(cmd, "/sys/class/gpio/gpio%d/value", m_cs_gpio_id); |
|||
m_gpiofd = open(cmd, O_RDWR); |
|||
if (m_gpiofd < 0) { |
|||
m_sys_error = errno; |
|||
m_last_error = ksp_init_spi_cs_pin_open_fail; |
|||
return ksp_init_spi_cs_pin_open_fail; |
|||
} |
|||
inited = true; |
|||
return ksp_success; |
|||
} |
|||
error_code_t sound_capturer_start(void) { |
|||
// |
|||
if (!inited) { |
|||
return ksp_module_not_inited; |
|||
} |
|||
|
|||
if (started) { |
|||
return ksp_module_has_started; |
|||
} |
|||
|
|||
int ret = pthread_create(&sound_read_thread, NULL, sound_read_thread_func, NULL); |
|||
if (ret != 0) { |
|||
m_sys_error = errno; |
|||
return ksp_create_thread_fail; |
|||
} |
|||
|
|||
started = true; |
|||
return ksp_success; |
|||
} |
|||
|
|||
int spi_read(unsigned char *rxbuf, int len) { |
|||
// gpio_set_value(0); |
|||
// usleep(1000); |
|||
int ret = read(m_fd, rxbuf, len); |
|||
// gpio_set_value(1); |
|||
|
|||
printf("read %d bytes\n", ret); |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
void readvoice() { |
|||
static int32_t voicebuf[4800]; // 50ms |
|||
static int32_t voiceNum; // |
|||
|
|||
voiceNum = 0; |
|||
memset(voicebuf, 0, sizeof(voicebuf)); |
|||
|
|||
gpio_set_value(0); |
|||
usleep(20); |
|||
int ret = read(m_fd, &voiceNum, 4); |
|||
printf("read voiceNum:%d\n", voiceNum); |
|||
if (voiceNum <= 0) { |
|||
gpio_set_value(1); |
|||
return; |
|||
} |
|||
if (voiceNum > 4800) { |
|||
printf("WARNING: voiceNum(%d) > voiceBufSize \n", voiceNum); |
|||
voiceNum = 4800; |
|||
} |
|||
|
|||
ret = read(m_fd, voicebuf, voiceNum * 4); |
|||
gpio_set_value(1); |
|||
return; |
|||
} |
|||
|
|||
static void *sound_read_thread_func(void *v) { |
|||
while (true) { |
|||
// 一次系统调用时间大概500us |
|||
readvoice(); |
|||
usleep(5000); |
|||
} |
|||
return NULL; |
|||
} |
|||
|
|||
void sound_capturer_dump_last_error() { |
|||
printf("----------------sound_capturer_dump_last_error-----------------\n"); |
|||
printf("- module error: %s(%d)\n", error_code_to_string(m_last_error), m_last_error); |
|||
printf("- sys_ error : %s(%d)\n", strerror(m_sys_error), m_sys_error); |
|||
printf("-\n"); |
|||
} |
@ -0,0 +1,58 @@ |
|||
#pragma once |
|||
#include <stdint.h> |
|||
|
|||
#ifdef cplusplus |
|||
extern "C" { |
|||
#endif |
|||
typedef void (*sound_capture_callback_t)(uint32_t* data, uint32_t len); |
|||
|
|||
typedef enum { |
|||
ksp_success, |
|||
ksp_module_not_inited, |
|||
ksp_module_has_started, |
|||
ksp_create_thread_fail, |
|||
ksp_init_spi_cs_pin_open_fail, |
|||
ksp_init_spi_error_open_fail, |
|||
ksp_init_spi_error_set_speed_fail, |
|||
ksp_init_spi_error_set_nbits_fail, |
|||
ksp_init_spi_error_set_mode_fail, |
|||
} error_code_t; |
|||
|
|||
static inline const char* error_code_to_string(error_code_t code) { |
|||
switch (code) { |
|||
case ksp_success: |
|||
return "success"; |
|||
case ksp_module_not_inited: |
|||
return "module not inited"; |
|||
case ksp_module_has_started: |
|||
return "module has started"; |
|||
case ksp_create_thread_fail: |
|||
return "create thread fail"; |
|||
case ksp_init_spi_cs_pin_open_fail: |
|||
return "init spi cs pin open fail"; |
|||
case ksp_init_spi_error_open_fail: |
|||
return "init spi error open fail"; |
|||
case ksp_init_spi_error_set_speed_fail: |
|||
return "init spi error set speed fail"; |
|||
case ksp_init_spi_error_set_nbits_fail: |
|||
return "init spi error set nbits fail"; |
|||
case ksp_init_spi_error_set_mode_fail: |
|||
return "init spi error set mode fail"; |
|||
default: |
|||
return "unknown error"; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* @brief 初始化声音采集模块 |
|||
* |
|||
* @param deviceName |
|||
* @param callback |
|||
*/ |
|||
error_code_t sound_capturer_init(const char* deviceName, int csgpionameId, sound_capture_callback_t callback); |
|||
error_code_t sound_capturer_start(void); |
|||
|
|||
void sound_capturer_dump_last_error(); |
|||
#ifdef cplusplus |
|||
} |
|||
#endif |
@ -0,0 +1,37 @@ |
|||
#include <errno.h>
|
|||
#include <fcntl.h>
|
|||
#include <getopt.h>
|
|||
#include <linux/spi/spidev.h>
|
|||
#include <linux/types.h>
|
|||
#include <stdbool.h>
|
|||
#include <stdint.h>
|
|||
#include <stdio.h>
|
|||
#include <stdlib.h>
|
|||
#include <string.h>
|
|||
#include <sys/ioctl.h>
|
|||
#include <unistd.h>
|
|||
extern "C" { |
|||
#include "libiflytop_adc_capture.h"
|
|||
} |
|||
|
|||
void sound_capture_callback(uint32_t* data, uint32_t len) {} |
|||
|
|||
int main(int argc, char const* argv[]) { |
|||
error_code_t erro_code = sound_capturer_init("/dev/spidev1.0", 59, sound_capture_callback); |
|||
if (erro_code != ksp_success) { |
|||
sound_capturer_dump_last_error(); |
|||
return -1; |
|||
} |
|||
|
|||
erro_code = sound_capturer_start(); |
|||
if (erro_code != ksp_success) { |
|||
sound_capturer_dump_last_error(); |
|||
return -1; |
|||
} |
|||
|
|||
while (true) { |
|||
sleep(1); |
|||
} |
|||
|
|||
return 0; |
|||
} |
@ -0,0 +1,4 @@ |
|||
mkdir -p build |
|||
cd build |
|||
cmake .. |
|||
make -j8 install |
@ -0,0 +1,16 @@ |
|||
#!/bin/bash |
|||
# ./sh/deploy.sh username ip |
|||
|
|||
|
|||
set -e |
|||
|
|||
# 1. Build |
|||
./sh/build.sh |
|||
|
|||
|
|||
cd build |
|||
make -j8 install |
|||
tar -cvf app.tar app |
|||
|
|||
scp app.tar $1@$2:/home/$1/ |
|||
ssh $1@$2 "cd /home/$1/ && tar -xvf app.tar" |
Write
Preview
Loading…
Cancel
Save
Reference in new issue