Compare commits

...

3 Commits

Author SHA1 Message Date
b4131a9a40 feat(spi):update spi 2026-07-06 20:06:44 +08:00
a699f03d72 feat(spi):update spi mode to MODE_2 to match MCU 2026-07-02 14:38:23 +08:00
aa98124c35 feat(notify):notify can signal success 2026-06-24 23:14:51 +08:00
6 changed files with 72 additions and 33 deletions

View File

@@ -49,7 +49,7 @@ else
echo "--- build boost, target host: ${TARGET_HOST} ----" echo "--- build boost, target host: ${TARGET_HOST} ----"
fi fi
#${BASE_DIR}/build_boost.sh ${BASE_DIR}/build_boost.sh
if [ "rk3576" = ${TARGET_HOST} ];then if [ "rk3576" = ${TARGET_HOST} ];then
echo "--- cross build vsomeip, target host: ${TARGET_HOST} ----" echo "--- cross build vsomeip, target host: ${TARGET_HOST} ----"
@@ -70,7 +70,7 @@ else
echo "--- build vsomeip, target host: ${TARGET_HOST} ----" echo "--- build vsomeip, target host: ${TARGET_HOST} ----"
fi fi
#${BASE_DIR}/build_vsomeip.sh ${BASE_DIR}/build_vsomeip.sh
#${BASE_DIR}/build_example.sh #${BASE_DIR}/build_example.sh
${BASE_DIR}/build_soa_server.sh ${BASE_DIR}/build_soa_server.sh

Binary file not shown.

View File

@@ -183,7 +183,7 @@ std::vector<byte_t> notifier_can5(void){
bool epsilon_func(std::shared_ptr<vsomeip::payload> _old, std::shared_ptr<vsomeip::payload> _new) bool epsilon_func(std::shared_ptr<vsomeip::payload> _old, std::shared_ptr<vsomeip::payload> _new)
{ {
// TODO: add your logic here to compare _old and _new // TODO: add your logic here to compare _old and _new
return false; return true;
}; };

View File

@@ -75,8 +75,10 @@ void my_spi_server_thread_func(void* arg) {
uint8_t buf[FRAME_LEN]; uint8_t buf[FRAME_LEN];
uint8_t last_buf[FRAME_LEN] = {0};
int fd, ret; int fd, ret;
unsigned int frame_count = 0; unsigned int frame_count = 0;
int data_changed = 0;
/* 注册退出信号 */ /* 注册退出信号 */
signal(SIGINT, sigint_handler); signal(SIGINT, sigint_handler);
@@ -137,17 +139,29 @@ void my_spi_server_thread_func(void* arg) {
/* 6. 循环接收数据 */ /* 6. 循环接收数据 */
while (keep_running) { while (keep_running) {
memset(buf, 0, FRAME_LEN); memset(buf, 0, FRAME_LEN);
ret = read(fd, buf, FRAME_LEN);
if (ret < 0) {
perror("read");
break;
} else if (ret == 0) {
/* 非阻塞模式可能返回0但这里用了阻塞模式一般不会出现 */
//continue;
}
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)NULL,
.rx_buf = (unsigned long)buf,
.len = FRAME_LEN,
.speed_hz = SPI_SPEED,
.bits_per_word = SPI_BITS,
};
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 0) {
perror("spi transfer");
break;
}
ret = FRAME_LEN; /* transfer 成功则返回 FRAME_LEN */
frame_count++; frame_count++;
/* 判断数据是否有变化 */
data_changed = (memcmp(last_buf, buf, FRAME_LEN) != 0);
if (!data_changed)
continue;
memcpy(last_buf, buf, FRAME_LEN);
printf("\n========== Frame #%u (%d bytes) ==========\n", frame_count, ret); printf("\n========== Frame #%u (%d bytes) ==========\n", frame_count, ret);
printf("HEX : "); printf("HEX : ");
for (int i = 0; i < ret; i++) for (int i = 0; i < ret; i++)

Binary file not shown.

View File

@@ -27,14 +27,21 @@
/* ========== SPI配置必须与MCU主设备完全一致========== */ /* ========== SPI配置必须与MCU主设备完全一致========== */
#define SPI_DEVICE "/dev/spidev4.0" #define SPI_DEVICE "/dev/spidev4.0"
#define SPI_MODE SPI_MODE_0 /* CPOL=1, CPHA=1 */ #define SPI_MODE SPI_MODE_2 /* CPOL=1, CPHA=1 */
#define SPI_BITS 8 #define SPI_BITS 8
#define SPI_SPEED 1000000 /* 1MHz */ #define SPI_SPEED 1000000 /* 1MHz */
#define FRAME_LEN 128 /* 每帧64字节 */ #define FRAME_LEN 128 /* 每帧256字节 */
/* ===================================================== */ /* ===================================================== */
static volatile int keep_running = 1; static volatile int keep_running = 1;
static int is_all_zero(const uint8_t *buf, int len) {
for (int i = 0; i < len; i++)
if (buf[i] != 0)
return 0;
return 1;
}
void sigint_handler(int sig) { void sigint_handler(int sig) {
(void)sig; (void)sig;
keep_running = 0; keep_running = 0;
@@ -43,7 +50,13 @@ void sigint_handler(int sig) {
int main(void) int main(void)
{ {
uint8_t buf[FRAME_LEN]; uint8_t rx_buf[FRAME_LEN];
uint8_t tx_buf[FRAME_LEN] = {0xAA, 0xBB, 0xCC, 0xDD};
tx_buf[FRAME_LEN-4] = 0x11;
tx_buf[FRAME_LEN-3] = 0x22;
tx_buf[FRAME_LEN-2] = 0x33;
tx_buf[FRAME_LEN-1] = 0x44;
int fd, ret; int fd, ret;
unsigned int frame_count = 0; unsigned int frame_count = 0;
@@ -105,32 +118,44 @@ int main(void)
/* 6. 循环接收数据 */ /* 6. 循环接收数据 */
while (keep_running) { while (keep_running) {
memset(buf, 0, FRAME_LEN); memset(rx_buf, 0, FRAME_LEN);
ret = read(fd, buf, FRAME_LEN);
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx_buf,
.rx_buf = (unsigned long)rx_buf,
.len = FRAME_LEN,
.speed_hz = SPI_SPEED,
.bits_per_word = SPI_BITS,
};
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 0) { if (ret < 0) {
perror("read"); perror("spi transfer");
break; break;
} else if (ret == 0) {
/* 非阻塞模式可能返回0但这里用了阻塞模式一般不会出现 */
//continue;
} }
ret = FRAME_LEN; /* transfer 成功则返回 FRAME_LEN */
frame_count++; frame_count++;
/* 只有 tx 或 rx 数据不全为零时才打印 */
if (is_all_zero(tx_buf, FRAME_LEN) && is_all_zero(rx_buf, FRAME_LEN))
continue;
printf("\n========== Frame #%u (%d bytes) ==========\n", frame_count, ret); printf("\n========== Frame #%u (%d bytes) ==========\n", frame_count, ret);
printf("HEX : ");
for (int i = 0; i < ret; i++)
printf("0x%02X ", buf[i]);
printf("\n");
printf("ASCII: "); if (!is_all_zero(tx_buf, FRAME_LEN)) {
for (int i = 0; i < ret; i++) printf("TX HEX: ");
printf(" %c ", (buf[i] >= 0x20 && buf[i] < 0x7F) ? buf[i] : '.'); for (int i = 0; i < ret; i++)
printf("\n"); printf("0x%02X ", tx_buf[i]);
printf("\n");
}
if (!is_all_zero(rx_buf, FRAME_LEN)) {
printf("RX HEX: ");
for (int i = 0; i < ret; i++)
printf("0x%02X ", rx_buf[i]);
printf("\n");
}
printf("DEC : ");
for (int i = 0; i < ret; i++)
printf("%3d ", buf[i]);
printf("\n");
printf("=========================================\n"); printf("=========================================\n");
} }