#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <pthread.h>
#define SPI_DEVICE "/dev/spidev1.0"
#define GPIO_DEVICE "/dev/gpio-P3.21"
#define RUN_VALUE "/tmp/digi"
uint8_t led_value_table[] = {0xC0, 0xF9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
static uint8_t mode = 0;
static uint8_t bits = 8;
static uint32_t speed = 10000;
static uint16_t delay = 0;
enum {
SET_GPIO_HIGHT = 9,
SET_GPIO_LOW,
GET_GPIO_VALUE,
};
static void show_led_num(int fd_spi, int fd_gpio, int value, int num)
{
int ret;
uint8_t tx[] = {
led_value_table[value],
(1 << num),
};
struct spi_ioc_transfer tr_txrx[] = {
{
.tx_buf = (unsigned long)tx,
.rx_buf = 0,
.len = 2,
.delay_usecs = delay,
.speed_hz = speed,
.bits_per_word = bits,
},
};
ret = ioctl(fd_spi, SPI_IOC_MESSAGE(1), &tr_txrx[0]);
if (ret == 1) {
printf("can't revieve spi message");
return;
}
ioctl(fd_gpio,SET_GPIO_LOW);
usleep(100);
ioctl(fd_gpio,SET_GPIO_HIGHT);
}
int fd_spi = 0;
int fd_gpio = 0;
int led_value[4] = {0};
void *Print(void *threadid){
int i=0;
while(1)
{
for(i=0;i<4;i++)
show_led_num(fd_spi, fd_gpio, led_value[i], i);
}
}
int main(int argc, char *argv[])
{
int ret = 0,i=0,rc=0;
pthread_t thread;
long t;
int value;
int led_num = 0;
void *status;
if (argc != 2) {
printf("cmd : ./spi_led_test led_value \n ");
return -1;
}
value = atoi(argv[1]);
led_value[0]=value/1000;
led_value[1]=(value%1000)/100;
led_value[2]=(value%100)/10;
led_value[3]=(value%10);
fd_spi = open(SPI_DEVICE, O_RDWR);
if (fd_spi < 0) {
printf("can't open %s \n", SPI_DEVICE);
return -1;
}
fd_gpio = open(GPIO_DEVICE, O_RDWR);
if (fd_gpio < 0) {
printf("can't open %s device\n", GPIO_DEVICE);
return -1;
}
ret = ioctl(fd_spi, SPI_IOC_WR_MODE, &mode);
if (ret == -1) {
printf("can't set wr spi mode\n");
return -1;
}
ret = ioctl(fd_spi, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1) {
printf("can't set bits per word\n");
return -1;
}
ret = ioctl(fd_spi, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -1) {
printf("can't set max speed hz\n");
return -1;
}
rc = pthread_create(&thread, NULL, Print, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
rc = pthread_join(thread, &status);
close(fd_gpio);
close(fd_spi);
return ret;
}
|