gpt4 book ai didi

c++ - SPI 代码不稳定 - RPi 上的 Raspbian (Debian) Linux

转载 作者:太空宇宙 更新时间:2023-11-04 11:50:22 25 4
gpt4 key购买 nike

我正在尝试让我的软件通过 Raspbian (Debian) Linux 从 Raspberry Pi 3 读写 CAT25512 EEPROM。在写入存储器之前,必须使用命令 0x06 设置写入启用锁存器 (WEL)。本次写入成功。然后使用读取状态寄存器命令 0x05 检查它,该命令也成功。然后写入、读取和连续的状态读取命令得不到响应和/或失败。

我尝试添加一些延迟以等待 HW。我也多次重构代码。

对于完整的文件,我提前表示歉意,但我不确定问题出在哪里。

#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <string>
#include <cstring>

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#define WAIT_FOR_EEPROM(a) do { for (int z=0; z<0x3FFF; z++); } while (eepromBusy((a) != 0x02));

const char *device = "/dev/spidev0.0";

uint8_t mode=SPI_MODE_0;
uint8_t bits=8;
uint32_t baud=500000;

uint8_t buffer[4] = {0};

int transfer(int spi_file, uint8_t* buffer, int length); // Prototype

int eepromBusy(int spi_file) {
buffer[0] = 0x05;
buffer[1] = 0x00;

transfer(spi_file, buffer, 2);
return (buffer[1]);
}

int main() {
int fd = open(device,O_RDWR);
if (fd < 0) printf("can't open device");

int ret;
ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -1) printf("can't set spi mode");

ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1) printf("can't set bits!");

ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &baud);
if (ret == -1) printf("can't set speed!");

ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
if (ret == -1) printf("can't set spi mode");

ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1) printf("can't set bits!");

ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &baud);
if (ret == -1) printf("can't set speed!");

printf("spi mode set as %u\n", mode);

printf("bits per byte set as %u\n", bits);
printf("max speed set at %u\n", baud);

do {
// Write Enable
buffer[0] = 0x06;
transfer(fd, buffer, 1);

// Read Status
buffer[0] = 0x05;
buffer[1] = 0x00;
transfer(fd, buffer, 2);
} while (!(buffer[1] & 0x02));

printf("Status Reg: %x\n", buffer[1]);

WAIT_FOR_EEPROM(fd)

// usleep(100);

// Write Byte

buffer[0] = 0x02;
buffer[1] = 0x00;
buffer[2] = 0x10;
buffer[3] = 0xAA;
transfer(fd, buffer, 4);

/* uint8_t busy = -1;
do {
usleep(50);
buffer[0] = 0x05;
buffer[1] = 0x00;
transfer(fd, buffer, 2);
busy = buffer[1] & 0x01;
} while (busy);
*/
WAIT_FOR_EEPROM(fd)

// Read Byte
buffer[0] = 0x03;
buffer[1] = 0x00;
buffer[2] = 0x10;
buffer[3] = 0x00;
transfer(fd, buffer, 4);
printf("Received byte: %i\n", buffer[3]);

if (close(fd) > 0) printf("can't close device");

return 0;
}

int transfer(int spi_file, uint8_t *buffer, int length) {
//struct spi_ioc_transfer spi[length] = {0};
int ret = -1;
struct spi_ioc_transfer tr[length] = {0};

for (int x=0; x<length; ++x) {
tr[x].tx_buf = (unsigned long)(buffer+x);
tr[x].rx_buf = (unsigned long)(buffer+x);
tr[x].len = sizeof(*(buffer+x));
tr[x].delay_usecs = 0;
tr[x].speed_hz = baud;
tr[x].bits_per_word = bits;
tr[x].cs_change = 0;
};

ret = ioctl(spi_file, SPI_IOC_MESSAGE(length), &tr);

if (ret < 1) printf("Transfer Error!!! First Byte Was: 0x%x\n", buffer[0]);

return ret;
}

我目前收到传输错误!!! First Byte Was: 0x5 错误两次,表示 WAIT_FOR_EEPROM(fd) 命令未正确执行。

一开始,WEL 位使用命令 0x06 设置,状态正确报告为 2。从 EEPROM 读取的数据读出为 0 或 211,具体取决于我如何调整代码。它应该是 0xAA (170)。

如果有任何建议,我将不胜感激。

最佳答案

我猜 transfer() 函数负责在每次调用时设置和执行一个 SPI 命令。为什么要使用“struct spi_ioc_transfer”数组?为什么要遍历缓冲区中的字节数并为每个字节设置一个“spi_ioc_transfer”结构?

看看https://raw.githubusercontent.com/raspberrypi/linux/rpi-3.10.y/Documentation/spi/spidev_test.c

关于c++ - SPI 代码不稳定 - RPi 上的 Raspbian (Debian) Linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56246148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com