我正在尝试让我的软件在具有Raspbian(Debian)Linux的Raspberry Pi 3上读写CAT25512 EEPROM。在写入存储器之前,必须使用命令0x06设置写使能锁存器(WEL)。写入成功。然后使用读取状态寄存器命令0x05对其进行检查,该命令也将成功执行。然后,写入,读取和连续状态读取命令将没有响应和/或失败。
我尝试添加一些延迟以等待硬件。我还多次重组了代码。
我为完整文件事先表示歉意,但是我不确定问题出在哪里。
#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;
}
我目前两次遇到Transfer Error!!! First Byte Was: 0x5
错误,表明WAIT_FOR_EEPROM(fd)
命令未正确执行。
首先,使用命令0x06将WEL位置1,并将状态正确报告为2。从EEPROM读取的数据已读出为0或211,这取决于我如何调整代码。应该是0xAA(170)。
任何建议,我将不胜感激。
答案 0 :(得分:0)
我猜想transfer()函数负责设置和执行每个调用一个SPI命令。 为什么要使用'struct spi_ioc_transfer'数组? 为什么要遍历缓冲区中的字节数并为每个缓冲区设置一个“ spi_ioc_transfer”结构?
看看https://raw.githubusercontent.com/raspberrypi/linux/rpi-3.10.y/Documentation/spi/spidev_test.c