01 > ȸҰ

 
작성일 : 18-09-14 10:17
RASPGA 보드 SPI , PWM LED DIMMING UART 테스트 로직코드
 글쓴이 : 이로직…
조회 : 5,618  
   Sail_Raspga_logic.zip (4.4M) [29] DATE : 2018-09-14 10:17:37


RASPGA 보드 SPI , UART 테스트 로직코드

4 채널 GPS 모듈을 연결한 사진

테스트 코드



#include
#include
#include
#include
#include
#include
#include
#include
#include

#include


#include

#include
#include
#include


#define BCM2708_PERI_BASE 0x3f000000
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000)
#define GET_GPIO(g) (*(GPIO_base+13)&(1<#define GPIO_OUT_10_19() *(GPIO_base+1) = 0x012024 // gpio 19

#define BLOCK_SIZE (4 * 1024)
#define PAGE_SIZE (4 * 1024)

#define INC_MENT 0x40
#define NINC_MENT 0x00

#define PWM_REG_BASE 0x20

#define UART_BASE_ADDR 0x10

//================================================
// uarrt reg address
#define UART_CH_0_TX (0x10 | 0x00)
#define UART_CH_0_RX (0x10 | 0x00)

#define UART_CH_0_SPEED (0x10 | 0x03)
#define UART_CH_0_FF_CNT (0x10 | 0x02)
#define UART_CH_0_FF_RST (0x10 | 0x01)


#define UART_CH_1_TX (0x10 | 0x04)
#define UART_CH_1_RX (0x10 | 0x04)

#define UART_CH_1_SPEED (0x10 | 0x04 | 0x03)
#define UART_CH_1_FF_CNT (0x10 | 0x04 | 0x02)
#define UART_CH_1_FF_RST (0x10 | 0x04 | 0x01)

#define UART_CH_2_TX (0x10 | 0x08)
#define UART_CH_2_RX (0x10 | 0x08)

#define UART_CH_2_SPEED (0x10 | 0x08 | 0x03)
#define UART_CH_2_FF_CNT (0x10 | 0x08 | 0x02)
#define UART_CH_2_FF_RST (0x10 | 0x08 | 0x01)


#define UART_CH_3_TX (0x10 | 0x0c)
#define UART_CH_3_RX (0x10 | 0x0c)

#define UART_CH_3_SPEED (0x10 | 0x0C | 0x03)
#define UART_CH_3_FF_CNT (0x10 | 0x0C | 0x02)
#define UART_CH_3_FF_RST (0x10 | 0x0C | 0x01)

#define UART_CH_3_ID (0x10 | 0x0C | 0x03)

//int mem_fd;
void *gpio_map;

int fd_mpu6090;

int fd;

// I/O access
volatile unsigned *GPIO_base; // addr * 4 => addr 0,1,2.....

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

void uart_received_test(void);

static void pabort(const char *s)
{
perror(s);
abort();
}

static const char *device = "/dev/spidev0.0";
static uint8_t mode = SPI_MODE_0; // MODE 1 EDGE , MODE 0 FALLING
static uint8_t mode1 = SPI_MODE_1 ; //1
static uint8_t bits = 8;
static uint32_t speed = 50000000;
static uint16_t delay;

void setup_io();
void spi_init(int file);
void bmp280_driver() ;
int mpu6090_process(void);


int data_buff[128] ;

// spi port word write function

int write_word(int file, int addr, int data)
{
int ret;
uint8_t tx[4] ;
tx[0] = (uint8_t)addr&0xff & 0x7f;
tx[2] = (uint8_t)((data&0xff00) >> 8);
tx[3] = (uint8_t)(data&0x00ff) ;
uint8_t rx[ARRAY_SIZE(tx)] = {0, };

struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = ARRAY_SIZE(tx),
.delay_usecs = delay,
.speed_hz = speed,
.bits_per_word = bits,
};

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

return ret;
}

int write_16bit_word(int file,int inc, int length,int addr, int *data)
{
int ret;
int i,j=0;
uint8_t tx[128] ;
int inc_bit;

inc_bit = inc &0x40;
tx[0] = (uint8_t)length&0x3f | inc_bit; // 16BIT MODE
tx[1] = (uint8_t)(addr&0xff);


for(i=0;itx[j+2] = (uint8_t)(data[i]>>8) & 0xff ; //uppper 16
tx[j+1+2] = (uint8_t)(data[i]) & 0xff ; // lower 8
j=j+2;
}

uint8_t rx[ARRAY_SIZE(tx)] = {0, };

struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = (length*2)+2,
.delay_usecs = delay,
.speed_hz = speed,
.bits_per_word = bits,
};

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

return ret;
}


// spi port word read function
int read_word(int file, int addr,int *data)
{

int ret;
uint8_t tx[4] ;
uint8_t rx[ARRAY_SIZE(tx)] = {0, };
tx[0] = (uint8_t)addr&0xff | 0x80;

struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = ARRAY_SIZE(tx),
.delay_usecs = delay,
.speed_hz = speed,
.bits_per_word = bits,
};

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

*data = (rx[2]<<8) | rx[3];

return ret;
}



int read_16bit_word(int file, int inc, int length, int addr,int *data)
{

int ret;
uint8_t tx[128] ;
uint8_t rx[ARRAY_SIZE(tx)] = {0, };
int data_sum;
int i,j=0;

int inc_bit;
inc_bit = inc & 0x40;
tx[0] = (uint8_t)length&0x3f | inc_bit | 0x80 ; // 16bit read mode mode
tx[1] = (uint8_t)(addr&0xff);

struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = (length*2)+2,
.delay_usecs = delay,
.speed_hz = speed,
.bits_per_word = bits,
};

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

for(i=0;idata[i] = ((rx[j+2])<<8) | rx[j+1+2];
j=j+2;

}

return ret;
}


int PWM_ch_write(int file, int channel ,int reg, int data)
{
int ret = 0;

int addr;
addr = PWM_REG_BASE | channel & 0x1c | reg & 0x03;

write_word(file, addr, data);
return ret;
}

int BMP_write(int file, int addr,int reg)
{
int ret;
write_word(file, addr, reg);
return 0;
}

int BMP_read(int file, int addr,int reg, int *data)
{
int ret;
int loop_count = 0;
write_word(file, addr, reg);
while(1){
//usleep(100);
ret = GET_GPIO(19);
if(ret == 0) break;
if(loop_count++ > 0xfff){
printf("bmp280 error: %x \n",loop_count);
return -1;
}
}
read_word(file, addr,data);
return 0;
}


int BMP_Data_write(int file, char *reg, int length)
{
int ret,i;
int addr = 0x20;
int reg_inc;

reg_inc = (reg[0]<<8) | reg[1];
write_word(file, addr, reg_inc & 0x7fff);

return 0;
}


void BMP280_Init(int file)
{
int addr = 0x22 ; // clock diver
int reg= 0x2;
BMP_write(file, addr,reg);
usleep(100);
addr = 0x20; // bmp280 fifo reg
write_word(file, addr, 0xe000 & 0x7fff); // bmp internal reg reset

}

int BMP_Data_read(int file, char reg, char *data,int length)
{
int ret;
int i;
int addr = 0x20;
int w_data[1];

int reg_inc;
reg_inc = (int)reg ;
for(i=0;i

ret = BMP_read(file, addr, reg_inc<<8, &w_data[0] );
data[i]= (char)w_data[0];
reg_inc++;
if(ret == -1)
return ret;
//usleep(100);
}
return i;

}


void uart_speed_set(void)
{
//data_buff[0] = 175000000/(9600*16*2); //1139 175MHZ 115200//9 -> 926000;
data_buff[0] = 148000000/(9600*16); //1139 175MHZ 115200//9 -> 926000;
write_16bit_word(fd, 0x00, 0x01, UART_CH_0_SPEED,data_buff);
write_16bit_word(fd, 0x00, 0x01, UART_CH_1_SPEED,data_buff);
write_16bit_word(fd, 0x00, 0x01, UART_CH_2_SPEED,data_buff);
write_16bit_word(fd, 0x00, 0x01, UART_CH_3_SPEED,data_buff);
}
void uart_all_fifo_reset()
{

data_buff[0] = 0;


write_16bit_word(fd, 0x00, 0x01, UART_CH_0_FF_RST,data_buff);
write_16bit_word(fd, 0x00, 0x01, UART_CH_1_FF_RST,data_buff);
write_16bit_word(fd, 0x00, 0x01, UART_CH_2_FF_RST,data_buff);
write_16bit_word(fd, 0x00, 0x01, UART_CH_3_FF_RST,data_buff);


}

void uart_ch_0_send (int data)
{
data_buff[0] = 0x0055;

write_16bit_word(fd, 0x00, 0x01, UART_CH_0_TX ,data_buff );

}

void uart_ch_1_send(int data)
{
data_buff[0] = 0x00aa;

write_16bit_word(fd, 0x00, 0x01, UART_CH_1_TX ,data_buff);

}

void uart_ch_2_send(int data)
{
data_buff[0] = 0x0055;

write_16bit_word(fd, 0x00, 0x01, UART_CH_2_TX ,data_buff);

}

void uart_ch_3_send(int data)
{
data_buff[0] = 0x00aa;

write_16bit_word(fd, 0x00, 0x01, UART_CH_3_TX ,data_buff);

}


//======================================

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

int send = 0;
int BMP_ID_data;
int BMP_STS_data;
int fpga_data=0;
int data_count;
int i;

uint8_t bd_data[10];
fd = open(device, O_RDWR);
if (fd < 0) pabort("can't open device");

spi_init(fd);

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


uart_speed_set(); // uart clock speed set
uart_all_fifo_reset(); // uart fifo reset

read_16bit_word(fd, 0x00 ,0x01 ,UART_CH_3_ID ,data_buff);

printf("SPI FPGA ID VALUE: %x\n",data_buff[0]);
while(1){

uart_received_test();


//uart_ch_0_send(0x00);
//uart_ch_1_send(0x00);

//uart_ch_2_send(0x00);

//uart_ch_3_send(0x00);


}

close(fd);

return ret;
}

void spi_init(int file)
{

int ret;

ret = ioctl(file, SPI_IOC_WR_MODE, &mode);
if (ret == -1)
pabort("can't set spi mode");

ret = ioctl(file, SPI_IOC_RD_MODE, &mode1);
if (ret == -1)
pabort("can't get spi mode");


ret = ioctl(file, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't set bits per word");

ret = ioctl(file, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't get bits per word");


ret = ioctl(file, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -1)
pabort("can't set max speed hz");

ret = ioctl(file, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (ret == -1)
pabort("can't get max speed hz");
}

void setup_io()
{

int mem_fd;
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
printf("can't open /dev/mem \n");
exit(-1);
}


gpio_map = mmap(
NULL, //Any adddress in our space will do
BLOCK_SIZE, //Map length
PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory
MAP_SHARED, //Shared with other processes
mem_fd, //File to map
GPIO_BASE //Offset to GPIO peripheral
);

close(mem_fd); //No need to keep mem_fd open after mmap

if (gpio_map == MAP_FAILED) {
printf("mmap error %d\n", (int)gpio_map);//errno also set!
exit(-1);
}

// Always use volatile pointer!
GPIO_base = (volatile unsigned *)gpio_map;


} // setup_io


void uart_received_test(void)
{

int data_count,i;
uart_speed_set(); // uart clock speed set
uart_all_fifo_reset(); // uart fifo reset

read_16bit_word(fd, 0x00 ,0x01 ,UART_CH_3_ID ,data_buff);

printf("SPI FPGA ID VALUE: %x\n",data_buff[0]);
while(1){
read_16bit_word(fd, 0x00 ,0x01 ,UART_CH_0_FF_CNT ,&data_count);
//usleep(1);
if((data_count & 0xff) > 0){

read_16bit_word(fd, 0x00 ,data_count ,UART_CH_0_RX ,data_buff);
for(i=0;i

//printf(" uart 0 received count:%x\n",data_count);

}
//usleep(10);

//usleep(50);
read_16bit_word(fd, 0x00 ,0x01 ,UART_CH_1_FF_CNT ,&data_count);
if((data_count & 0xff) > 10){

read_16bit_word(fd, 0x00 ,data_count ,UART_CH_1_RX ,data_buff);
for(i=0;i

//printf(" uart 1 received count:%x\n",data_count);

}

//usleep(50);
read_16bit_word(fd, 0x00 ,0x01 ,UART_CH_2_FF_CNT ,&data_count);
if((data_count & 0xff) > 10){

read_16bit_word(fd, 0x00 ,data_count,UART_CH_2_RX ,data_buff);
for(i=0;i

//printf(" uart 2 received count:%x\n",data_count);
}
//usleep(50);
read_16bit_word(fd, 0x00 ,0x01 ,UART_CH_3_FF_CNT ,&data_count);
if((data_count & 0xff) > 10){

read_16bit_word(fd, 0x00 ,data_count ,UART_CH_3_RX ,data_buff);
for(i=0;i

//printf(" uart 3 received count:%x\n",data_count);
}

usleep(5000);
}
}