nuc970 Linux io 模拟spi 时序

/*
 * 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 模拟的控制。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

solomon_大卫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值