访问max7219 LED矩阵的spidev用户界面

时间:2016-11-05 05:40:12

标签: c linux-device-driver embedded-linux spi intel-galileo

我正在尝试使用SPI总线使用max7219 8x8 LED矩阵。

我在Galileo主板上完成了以下GPIO设置。

数字引脚功能Linux电平转换器Mux引脚                                         L:Dir_out H:dir_in
IO11 SPI MOSI spidev1.0 gpio24 gpio44(H)                                                                 gpio72(L) IO12 gpio for                 Slave Sel gpio15 gpio42 -

IO13 SPI SCK spidev1.0 gpio30 gpio46(H)

我按如下方式设置上述gpio - 根据需要设置多路复用引脚。 - 导出gpio24,gpio 42,gpio30,gpio15并将方向字段设置为“OUT”,将值字段设置为“0”, - 将gpio15(从选择值)设置为“0”。

我使用以下代码访问LED矩阵并设置模式:

int fd;
uint8_t array1[2];

uint8_t array [] = {    
        0x0F, 0x00,
        0x0C, 0x01,
        0x09, 0x00,
        0x0A, 0x01,
        0x0B, 0x07,
        0x01, 0x06,
        0x02, 0x06, };

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

static uint8_t mode;
static uint8_t bits = 8;
static uint32_t speed = 50000;
static uint16_t delay;

fd= open(DEVICE,O_RDWR);
struct spi_ioc_transfer tr = 
    {
        .tx_buf = (unsigned long)array1,
        .rx_buf = 0,
        .len = ARRAY_SIZE(array1),
        .speed_hz = speed,
        .bits_per_word = bits,
        .cs_change = 1,
    };
array1[0] = array [i];
array1[1] = array [i+1];
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
            if(ret<0)
                {
                    printf("ioctl Failed\n");
                }
        usleep(500000);
        close (fd);

1 个答案:

答案 0 :(得分:0)

对于Galileo 1,您可以使用此脚本来设置spidev1.0

#!/bin/bash -e
# v0od0ochild

echo "Configuring Spidev1.0 for Galileo.."
gpio_path=/sys/class/gpio
gpio4="out 1"
gpio42="out 0 strong"
gpio43="out 0 strong"
gpio54="out 0 strong"
gpio55="out 0 strong"
gpios="4 42 43 54 55"
setup_gpio() {
  echo "Gpio ${1}: direction: $2, value: $3, drive: $4"
  echo -n $2 > $1/direction
  echo -n $3 > $1/value
  echo -n $4 > $1/drive
}
for i in ${gpios}; do
  gpio=${gpio_path}/gpio${i}
  if [ ! -d ${gpio} ]; then
  echo "Exporting gpio ${i}"
  echo -n ${i} > ${gpio_path}/export
  fi
done
for i in ${gpios}; do
  gpio=${gpio_path}/gpio${i}
  if [ -d ${gpio} ]; then
  gpiovar=gpio${i}
  setup_gpio ${gpio} ${!gpiovar}
  fi
done
echo "Spi ready"

如果您使用的是Galileo Gen2:

#!/bin/bash -e

# v0od0ochild

echo "Configuring Spidev1.0 for Galileo-Gen2.."

#pin10
echo 26 > /sys/class/gpio/export || echo "gpio26 already exported"
echo 74 > /sys/class/gpio/export || echo "gpio74 already exported"
echo 27 > /sys/class/gpio/export || echo "gpio27 already exported"
echo low > /sys/class/gpio/gpio26/direction || echo "Failed to set gpio26 low"
echo low > /sys/class/gpio/gpio74/direction || echo "Failed to set gpio74 low"
echo in > /sys/class/gpio/gpio27/direction || echo "Failed to set gpio27 direction in"

#pin11
echo 24 > /sys/class/gpio/export || echo "gpio24 already exported"
echo 44 > /sys/class/gpio/export || echo "gpio44 already exported"
echo 72 > /sys/class/gpio/export || echo "gpio72 already exported"
echo 25 > /sys/class/gpio/export || echo "gpio25 already exported"
echo low > /sys/class/gpio/gpio24/direction || echo "Failed to set gpio24 low"
echo high > /sys/class/gpio/gpio44/direction || echo "Failed to set gpio44 high"
echo low > /sys/class/gpio/gpio72/direction || echo "Failed to set gpio72 low"
echo in > /sys/class/gpio/gpio25/direction || echo "Failed to set gpio25 direction in"

#pin12
echo 42 > /sys/class/gpio/export || echo "gpio42 already exported"
echo 43 > /sys/class/gpio/export || echo "gpio43 already exported"
echo low > /sys/class/gpio/gpio42/direction || echo "Failed to set gpio42 low"
echo in > /sys/class/gpio/gpio43/direction || echo "Failed to set gpio43 direction in"

#pin13
echo 30 > /sys/class/gpio/export || echo "gpio30 alreay exported"
echo 46 > /sys/class/gpio/export || echo "gpio46 already exported"
echo 31 > /sys/class/gpio/export || echo "gpio31 already exported"
echo low > /sys/class/gpio/gpio30/direction || "Failed to set gpio30 low"
echo high > /sys/class/gpio/gpio46/direction || "Failed to set gpio46 high"
echo in > /sys/class/gpio/gpio31/direction || "Failed to set gpio31 direction in"
echo "Spi ready"
相关问题