Hello,
I have recently purchased a Vim 3 Pro and I am having problems when configuring SPI. According to the documentation (VIM3/3L SPI [Khadas Docs]), I have modified /boot/env.txt file, but chip select is not working. MOSI and clock signals work fine. I do not know if it is a board configuration problem or a code problem.
Edit: I am using mode 3 (CPOL = 1 and CPHA = 1)
#include <errno.h>
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <fcntl.h>
#include <time.h>
#include <sys/ioctl.h>
#include <linux/ioctl.h>
#include <sys/stat.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
int ade9430_fd;
void ade9430_write_16bit(int fd, uint16_t address, uint16_t value){
int ret;
uint8_t data_tx[4] = {(address >> 4),
(address & 0x0F) << 4,
(value & 0xFF00) >> 8,
(value & 0x00FF)};
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)data_tx,
//.rx_buf = (unsigned long)data_rx,
.len = WRITE16_BYTES, // 4 bytes
.delay_usecs = 0,
.speed_hz = speed, // 15.0 MHz
// .cs_change = 0,
.bits_per_word = bits, // 8
};
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1){
printf("can't send spi message\n");
}
}
int ade9430_check_comms(int fd){
if (fd < 0){
perror("Error Opening SPI Bus\n");
return -1;
}
/* Setup of the SPI Bus */
if(ioctl(fd, SPI_IOC_WR_MODE, &mode) < 0){
perror("Error setting SPI mode\n");
close(fd);
return -1;
}
if(ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0){
perror("Error setting SPI speed\n");
close(fd);
return -1;
}
if(ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0){
perror("Error setting SPI mode\n");
close(fd);
return -1;
}
return 0;
}
int main(int argc, char *argv[]){
int num = 0;
/* Stablish & configure ADE9430 SPI communications */
printf("Preparing SPI BUS\n");
/* Open the SPI bus file descriptor */
ade9430_fd = -1;
ade9430_fd = open("/dev/spidev1.1", O_RDWR);
if (ade9430_fd < 0){
perror("Error Opening SPI Bus.\n");
return -1;
}
if (ade9430_check_comms(ade9430_fd) != 0){
printf("SPI configuration failure.\n");
return -1;
}
/* Loop */
while(1){
system("sleep 0.25");
ade9430_write_16bit(ade9430_fd, 0x483, 0xDC75);
printf("SPI TX number %d\n", num);
num++;
}
return 0;
}