简体   繁体   中英

Access spidev user interface for max7219 LED matrix

I am trying to use the max7219 8x8 LED matrix using SPI bus.

I have done the following GPIO setup on Galileo board.

Digital pin Function Linux Level shifter Mux pins 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)

I am setting the above gpio as follows -setting the mux pins as desired. -exporting gpio24, gpio 42, gpio30, gpio15 and setting the direction field as "OUT" and the value field as "0", -setting the gpio15 (slave select value) to "0".

I am using the following code to access the LED matrix and set a pattern:

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);

for Galileo 1 you can use this script to setup 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"

If you are using a 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM