/*
* SPI testing utility (using spidev driver)
*
* Copyright (c) 2007 MontaVista Software, Inc.
* Copyright (c) 2007 Anton Vorontsov <avorontsov@ru.mvista.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* Cross-compile with cross-gcc -I/path/to/cross-kernel/include
*/
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/types.h>
#define NRSECSPI2_Enable system("echo 0 >/sys/class/gpio/gpio44/value")
#define NRSECSPI2_Disable system("echo 1 >/sys/class/gpio/gpio44/value")
#define NRSECSPI2_SCK_LOW system("echo 0 >/sys/class/gpio/gpio45/value")
#define NRSECSPI2_SCK_HIGH system("echo 1 >/sys/class/gpio/gpio45/value")
#define NRSECSPI2_MOSI_HIGH system("echo 1 >/sys/class/gpio/gpio46/value")
#define NRSECSPI2_MOSI_LOW system("echo 0 >/sys/class/gpio/gpio46/value")
delay(int us)
{
int i,j;
for(i=0;i<us;i++)
{
;
}
}
init_p1_init(void)
{
system("echo 36 > /sys/class/gpio/export"); // pwr_en
system("echo out >/sys/class/gpio/gpio36/direction");
system("echo 44 > /sys/class/gpio/export"); // spi_cs
system("echo out >/sys/class/gpio/gpio44/direction");
system("echo 1 >/sys/class/gpio/gpio44/value");
system("echo 45 > /sys/class/gpio/export"); //spi_sck
system("echo out >/sys/class/gpio/gpio45/direction");
system("echo 1 >/sys/class/gpio/gpio45/value");
system("echo 46 > /sys/class/gpio/export"); //spi_mosi
system("echo out >/sys/class/gpio/gpio46/direction");
system("echo 1 >/sys/class/gpio/gpio46/value");
system("echo 47 > /sys/class/gpio/export"); //spi_miso
system("echo in >/sys/class/gpio/gpio47/direction");
}
void spi_pwr_restart(void)
{
system("echo 1 >/sys/class/gpio/gpio36/value");
delay(1000);
system("echo 0 >/sys/class/gpio/gpio36/value");
delay(1000);
}
int fd,dev_fd;
unsigned char *ptr;
void spi_write(char* buf,int length)
{
int i,j;
unsigned int value;
//NRSECSPI2_Enable;
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value&0xefff;
usleep(50);
for(j=0;j<length;j++)
{
for(i=0;i<8;i++)
{
if (*buf & 0x80)
{
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value|0x4000;
}
else
{
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value&0xbfff;
}
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value&0xdfff;
delay(10);
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value|0x2000;
delay(10);
if (i == 7)
{
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value|0x4000;
}
*buf <<= 1; /* ×óÒÆÒ»¸öbit */
delay(1000);
}
*buf++;
}
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value|0x1000;
usleep(50);
}
int spi_read(char* buf,int length)
{
int i,j;
int value_buf;
unsigned char temp;
char value_str[3];
unsigned int value;
// unsigned char *ptr;
unsigned char *map_base;
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value&0xefff;
for(j=0;j<length;j++)
{
for(i=0;i<8;i++)
{
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value&0xdfff;
temp <<= 1;
value = *(volatile unsigned int *)(ptr+0x48);
if(value&0x8000)
{
temp++;
}
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value|0x2000;
delay(100);
}
*buf++=temp;
}
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value|0x1000;
usleep(50);
return 1;
}
int main(int argc, char *argv[])
{
int ret = 0;
int i;
char sendbuf[8];
char revbuf[256];
spi_pwr_restart();
init_p1_init();
dev_fd = open("/dev/mem", O_RDWR | O_SYNC);
ptr=mmap(0, 0x50,PROT_READ|PROT_WRITE , MAP_SHARED, dev_fd, 0xB8003000 );
usleep(100);
while(1)
{
sendbuf[0]=0x55;
sendbuf[1]=0x00;
sendbuf[2]=0x84;
sendbuf[3]=0x00;
sendbuf[4]=0x08;
sendbuf[5]=0x00;
sendbuf[6]=0x00;
sendbuf[7]=0x73;
spi_write(sendbuf,8);
usleep(100);
spi_read(revbuf,17);
for(i=0;i<17;i++)
{
printf("%2x ",revbuf[i]);
}
printf("\n");
usleep(100);
}
return ret;
* SPI testing utility (using spidev driver)
*
* Copyright (c) 2007 MontaVista Software, Inc.
* Copyright (c) 2007 Anton Vorontsov <avorontsov@ru.mvista.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* Cross-compile with cross-gcc -I/path/to/cross-kernel/include
*/
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/types.h>
#define NRSECSPI2_Enable system("echo 0 >/sys/class/gpio/gpio44/value")
#define NRSECSPI2_Disable system("echo 1 >/sys/class/gpio/gpio44/value")
#define NRSECSPI2_SCK_LOW system("echo 0 >/sys/class/gpio/gpio45/value")
#define NRSECSPI2_SCK_HIGH system("echo 1 >/sys/class/gpio/gpio45/value")
#define NRSECSPI2_MOSI_HIGH system("echo 1 >/sys/class/gpio/gpio46/value")
#define NRSECSPI2_MOSI_LOW system("echo 0 >/sys/class/gpio/gpio46/value")
delay(int us)
{
int i,j;
for(i=0;i<us;i++)
{
;
}
}
init_p1_init(void)
{
system("echo 36 > /sys/class/gpio/export"); // pwr_en
system("echo out >/sys/class/gpio/gpio36/direction");
system("echo 44 > /sys/class/gpio/export"); // spi_cs
system("echo out >/sys/class/gpio/gpio44/direction");
system("echo 1 >/sys/class/gpio/gpio44/value");
system("echo 45 > /sys/class/gpio/export"); //spi_sck
system("echo out >/sys/class/gpio/gpio45/direction");
system("echo 1 >/sys/class/gpio/gpio45/value");
system("echo 46 > /sys/class/gpio/export"); //spi_mosi
system("echo out >/sys/class/gpio/gpio46/direction");
system("echo 1 >/sys/class/gpio/gpio46/value");
system("echo 47 > /sys/class/gpio/export"); //spi_miso
system("echo in >/sys/class/gpio/gpio47/direction");
}
void spi_pwr_restart(void)
{
system("echo 1 >/sys/class/gpio/gpio36/value");
delay(1000);
system("echo 0 >/sys/class/gpio/gpio36/value");
delay(1000);
}
int fd,dev_fd;
unsigned char *ptr;
void spi_write(char* buf,int length)
{
int i,j;
unsigned int value;
//NRSECSPI2_Enable;
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value&0xefff;
usleep(50);
for(j=0;j<length;j++)
{
for(i=0;i<8;i++)
{
if (*buf & 0x80)
{
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value|0x4000;
}
else
{
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value&0xbfff;
}
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value&0xdfff;
delay(10);
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value|0x2000;
delay(10);
if (i == 7)
{
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value|0x4000;
}
*buf <<= 1; /* ×óÒÆÒ»¸öbit */
delay(1000);
}
*buf++;
}
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value|0x1000;
usleep(50);
}
int spi_read(char* buf,int length)
{
int i,j;
int value_buf;
unsigned char temp;
char value_str[3];
unsigned int value;
// unsigned char *ptr;
unsigned char *map_base;
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value&0xefff;
for(j=0;j<length;j++)
{
for(i=0;i<8;i++)
{
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value&0xdfff;
temp <<= 1;
value = *(volatile unsigned int *)(ptr+0x48);
if(value&0x8000)
{
temp++;
}
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value|0x2000;
delay(100);
}
*buf++=temp;
}
value = *(volatile unsigned int *)(ptr+0x44);
*(volatile unsigned int *)(ptr+0x44)=value|0x1000;
usleep(50);
return 1;
}
int main(int argc, char *argv[])
{
int ret = 0;
int i;
char sendbuf[8];
char revbuf[256];
spi_pwr_restart();
init_p1_init();
dev_fd = open("/dev/mem", O_RDWR | O_SYNC);
ptr=mmap(0, 0x50,PROT_READ|PROT_WRITE , MAP_SHARED, dev_fd, 0xB8003000 );
usleep(100);
while(1)
{
sendbuf[0]=0x55;
sendbuf[1]=0x00;
sendbuf[2]=0x84;
sendbuf[3]=0x00;
sendbuf[4]=0x08;
sendbuf[5]=0x00;
sendbuf[6]=0x00;
sendbuf[7]=0x73;
spi_write(sendbuf,8);
usleep(100);
spi_read(revbuf,17);
for(i=0;i<17;i++)
{
printf("%2x ",revbuf[i]);
}
printf("\n");
usleep(100);
}
return ret;
}
本程序主要/dev/mem 对芯片物理寄存器 对应的虚拟内存操作。以及对//sys/class/gpio/ 的gpio操作。
两种方式实现gpio 模拟的控制。