2015-01-17 2 views
0

나는 내 자신을 위해 spidev와 그의 테스트 코드를 사용하여 Raspberry PI에 SPI를 사용하려고합니다. 테이블 크기에 문제가 있습니다. 함수에 파라미터로서 전송 된 배열이 취급된다spidev를 사용하는 linux (raspberry PI)의 SPI

// spi.cpp : Defines the entry point for the console application. 
// 


#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> 

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

static const char *device = "/dev/spidev0.0"; 
static uint8_t mode; 
static uint8_t bits = 8; 
static uint32_t speed = 1000000; 
static uint16_t delay; 

//this function gives problems with diffrent size of array 
static void transfer(int fd, uint8_t tx[]) 
{ 
     printf("transfer1"); 
     printf(" rozmiar tab=%d ", ARRAY_SIZE(tx)); 
     int ret; 
     uint8_t rx[ARRAY_SIZE(tx)] = { 0, }; 
     struct spi_ioc_transfer tr = { 
       .tx_buf = (unsigned long)tx, 
       .rx_buf = (unsigned long)rx, 
       .len = 3, 
       .delay_usecs = delay, 
       .speed_hz = speed, 
       .bits_per_word = bits, 
     }; 

     ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); 

     for (ret = 0; ret < ARRAY_SIZE(tx); ret++) { 
       if (!(ret % 6)) 
         puts(""); 

       printf("%d. %.2X ", ret,rx[ret]); 

     } 
     puts(""); 
     } 

int main(int argc, char *argv[]) 
{ 
     int ret = 0; 
     int fd; 

     fd = open(device, O_RDWR); 

     /* 
     * spi mode 
     */ 
     ret = ioctl(fd, SPI_IOC_WR_MODE, &mode); 

     ret = ioctl(fd, SPI_IOC_RD_MODE, &mode); 

     /* 
     * bits per word 
     */ 
     ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); 

     ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); 

     /* 
     * max speed hz 
     */ 
     ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); 

     ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed); 

     printf("spi mode: %d\n", mode); 
     printf("bits per word: %d\n", bits); 
     printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000); 

     uint8_t tx1[] = { 
       0x0, 0x1b, 0xa5 
     }; 
//here I'm passing table to function 
     transfer(fd, tx1); 
     uint8_t tx2[] = { 
       0x0, 0x33, 0x30, 0x01, 0x02 
     }; 
     printf(" %d. ", ARRAY_SIZE(tx2)); 
     transfer(fd, tx2); 
     uint8_t tx3[] = { 
       0x0, 0x52, 0x90 
     }; 
     transfer(fd, tx3); 
     uint8_t tx4[] = { 
       0x80, 0x60 
     }; 
     printf(" %d. ", ARRAY_SIZE(tx4)); 
     transfer(fd, tx4); 

     close(fd); 

     return ret; 
} 

답변

0

: 그것은 다음 4 갖는 함수 안에 내 표 3 개 요소를 가지고, 즉 그 함수를 전송할 전달 전에 내 표는 다른 크기를 갖는 것은 내 코드 바늘. ARRAY_SIZE(a)은 대략 (sizeof(uint8_t*)/sizeof(uint8_t))으로 확장되며 32 비트 플랫폼에서는 4와 같습니다.

당신은 명시 적으로 3 매개 변수로 배열의 크기를 통과해야

: transfer() 내부에 수정되지 않을

transfer(fd, tx1, ARRAY_SIZE(tx1)); 

tx 상태 :

void transfer(int fd, const uint8_t *tx, size_t size) { ... } 

, 당신은 제대로 계산하는 매크로 배열 크기를 사용할 수 있습니다 , 그것은 좋은 맛의 문제입니다. const.

+0

당신의 도움에 감사드립니다. 당신이 나에게 말한 것을 한 후에 나는 동적 인 메모리 할당을해야만했지만, 그것을 스스로 얻었습니다. 또한 왜 그런 일이 일어나는지 설명 할 수있는 자원을 가르쳐 주시겠습니까? – Eggboard

관련 문제