SPI以及UART的测试DEMO

SPI的测试DEMO

spi_test.c

#include <getopt.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.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 void pabort(const char *s)
{
      perror(s);
      abort();
}

static void usage(char *name) {
        fprintf(stderr, "Usage: %s -B bus -S slave -a [cpha 0|1] -l [cpol 0|1] -s speed -b bits buf\n",
               name);
		exit(1);
}

uint8_t tx[256] = {      //定义待发送的数据
             0x01, 0x20, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00,
};



static char device[20]; //设备名
static uint8_t mode;

static uint32_t bus = 7;
static uint32_t slave = 0;
static uint32_t cpol = 0;
static uint32_t cpha = 1;
static uint32_t speed = 4500000;
static uint32_t bits = 8;

static struct option opts[] = {
	{"bus", required_argument, NULL, 'B'},
	{"slave", required_argument, NULL, 'S'},
	{"cpha", required_argument, NULL, 'a'},
	{"cpol", required_argument, NULL, 'l'},
	{"speed", required_argument, NULL, 's'},
	{"bits", required_argument, NULL, 'b'},
	{"help", no_argument, NULL, 'h'},
};

static uint16_t delay;
static void transfer(int fd)
{
      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 = ARRAY_SIZE(tx),                    
             .delay_usecs = delay,
             .speed_hz = speed,
             .bits_per_word = bits,
      };

      ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);//执行spidev.c中ioctl的default进行数据传输
      if (ret == 1)
             pabort("can't send spi message");
	  printf("spi rx:\n");
      for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {
             if (!(ret % 32))
                    puts("");
             printf("%.2X ", rx[ret]);      //打印接收到的数据
      }
      puts("");
}

int main(int argc, char *argv[])
{

      int ret = 0;
      int fd;
      char data;
      int c;
      int i;

      while((c = getopt_long(argc, argv, "B:S:a:l:s:b:h", opts, NULL)) != -1) {

        switch (c) {
        case 'B':
            bus = atol(optarg);
            break;
        case 'S':
            slave = atol(optarg);
            break;
        case 'a':
	    	cpha = atol(optarg);
            break;
        case 'l':
	    	cpol = atol(optarg);
            break;
        case 's':
            speed = atol(optarg);
            break;
        case 'b':
            bits = atol(optarg);
            break;
        case 'h':
			usage(argv[0]);
		case '?':
			printf("not support option %c\n", optopt);
			usage(argv[0]);
            break;
        }
      }


      if (cpha) {
      	mode |= SPI_CPHA;
      }
      if (cpol) {
      	mode |= SPI_CPOL;
      }
      mode &= ~SPI_CS_HIGH;

	  printf("argc %d\n", argc);
	  printf("optind %d\n", optind);
	  if (argc > optind) {
      	for (i = 0; i < argc - optind; i++) {
	     	sscanf(argv[optind + i], "%x", &data);
	      	tx[i] = data;
      	}
	  }
      printf("spi tx:\n");
      for (i = 0; i < 256; i++) {
             if (!(i % 32))
                    puts("");
             printf("%.2X ", tx[i]);      //打印接收到的数据
 
      }
      printf("\n");

      //sprintf(device, "/dev/spidev%d.%d", bus, slave);
      fd = open("/dev/spidev32766.0", O_RDWR);       //打开"/dev/spidev1.0"
      if (fd < 0)
             pabort("can't open device");

      ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);  //SPI模式设置可写
      if (ret == -1)
            pabort("can't set spi mode");
            ret = ioctl(fd, SPI_IOC_RD_MODE, &mode); //SPI模式设置可读

            if (ret == -1)
                  pabort("can't get spi mode");

      ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);  //SPI的bit/word设置可写
      if (ret == -1)
             pabort("can't set bits per word");

      ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);   //SPI的bit/word设置可读
      if (ret == -1)
             pabort("can't get bits per word");

      ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);     //SPI的波特率设置可写
      if (ret == -1)
             pabort("can't set max speed hz");

      ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);   //SPI的波特率设置可读
      if (ret == -1)
             pabort("can't get max speed hz");

      printf("spi mode: %d\n", mode);
      printf("bits per word: %d\n", bits);
      printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
      transfer(fd);                                                        //数据传输
      close(fd);

      return ret;

}

Android.mk

LOCAL_PATH := $(call my-dir)

spi_test_shared_libraries := \
  libcutils

# spitest
# =============================================================================
include $(CLEAR_VARS)
LOCAL_MODULE := spi-test
LOCAL_SRC_FILES := \
  spi_test.c

LOCAL_SHARED_LIBRARIES := $(spi_test_shared_libraries)
#LOCAL_CFLAGS := -Werror -Wall
include $(BUILD_EXECUTABLE)

UART的测试DEMO

serial.cpp

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<unistd.h>

#include<termios.h>

#include<string.h>

 

int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)

{

	 struct termios newtio,oldtio;

	 if  ( tcgetattr( fd,&oldtio)  !=  0) {

	  perror("SetupSerial 1");

	  return -1;

	 }

	 
	 bzero( &newtio, sizeof( newtio ) );

	 newtio.c_cflag  |=  CLOCAL | CREAD; //CLOCAL:忽略modem控制线  CREAD:打开接受者

	 newtio.c_cflag &= ~CSIZE; //字符长度掩码。取值为:CS5,CS6,CS7或CS8

 

	 switch( nBits )

	 {

	 case 7:

	  newtio.c_cflag |= CS7;

	  break;

	 case 8:

	  newtio.c_cflag |= CS8;

	  break;

	 }

 

	 switch( nEvent )

	 {

	 case 'O':

	  newtio.c_cflag |= PARENB; //允许输出产生奇偶信息以及输入到奇偶校验

	  newtio.c_cflag |= PARODD;  //输入和输出是奇及校验

	  newtio.c_iflag |= (INPCK | ISTRIP); // INPACK:启用输入奇偶检测;ISTRIP:去掉第八位

	  break;

	 case 'E':

	  newtio.c_iflag |= (INPCK | ISTRIP);

	  newtio.c_cflag |= PARENB;

	  newtio.c_cflag &= ~PARODD;

	  break;

	 case 'N': 

	  newtio.c_cflag &= ~PARENB;

	  break;

	 }

 

	 switch( nSpeed )

	 {

	 case 2400:

	  cfsetispeed(&newtio, B2400);

	  cfsetospeed(&newtio, B2400);

	  break;

	 case 4800:

	  cfsetispeed(&newtio, B4800);

	  cfsetospeed(&newtio, B4800);

	  break;

	 case 9600:

	  cfsetispeed(&newtio, B9600);

	  cfsetospeed(&newtio, B9600);

	  break;
	  
	 case 38400:

	  cfsetispeed(&newtio, B38400);

	  cfsetospeed(&newtio, B38400);

	  break;
	  
	 case 115200:

	  cfsetispeed(&newtio, B115200);

	  cfsetospeed(&newtio, B115200);

	  break;

	 case 460800:

	  cfsetispeed(&newtio, B460800);

	  cfsetospeed(&newtio, B460800);

	  break;

	 default:

	  cfsetispeed(&newtio, B9600);

	  cfsetospeed(&newtio, B9600);

	  break;

	 }

 

	 if( nStop == 1 )

	  newtio.c_cflag &=  ~CSTOPB; //CSTOPB:设置两个停止位,而不是一个

	 else if ( nStop == 2 )

	 newtio.c_cflag |=  CSTOPB;

	 

	 newtio.c_cc[VTIME]  = 0; //VTIME:非cannoical模式读时的延时,以十分之一秒位单位

	 newtio.c_cc[VMIN] = 0; //VMIN:非canonical模式读到最小字符数

	 tcflush(fd,TCIFLUSH); // 改变在所有写入 fd 引用的对象的输出都被传输后生效,所有已接受但未读入的输入都在改变发生前丢弃。

	 if((tcsetattr(fd,TCSANOW,&newtio))!=0) //TCSANOW:改变立即发生

	 {

	  perror("com set error");

	  return -1;

	 }

	 printf("set done!\n\r");

	 return 0;

}

 

int main(void)

{

	 int fd1,nset,nread,ret;

	 char buf[8]={'h','e','l','l','o'};
	 char buf1[100];

 

	 fd1 = open( "/dev/ttyS0", O_RDWR);

	 if (fd1 == -1)
	  exit(1);

	 printf("open   ttyS0 success!!\n");

	 nset = set_opt(fd1, 38400, 8, 'N', 1);

	 if (nset == -1)
	  exit(1);
	 
	 printf("SET  ttyS3 success!!\n");

	 printf("enter the loop!!\n");

 

	 while (1)
	 { 

	   memset(buf1, 0, sizeof(buf1));

	   ret = write(fd1, buf, 5);

	   if( ret > 0){

	      printf("write success!  wait data receive\n");

	   }

	   
	   nread = read(fd1, buf1, 5);

	   if(nread > 0){

		printf("redatad: nread = %s\n\n\r", buf1);

	   }

	   sleep(1);

	  //nread = read(fd1, buf1,1);

	  //if(buf1[0] == 'q')

	   //break;

	 }

 close(fd1);

 

 return 0;

}

Android.mk

# Copyright (C) 2008 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


LOCAL_PATH := $(call my-dir)
# HAL module implemenation, not prelinked and stored in
# hw/<LIGHTS_HARDWARE_MODULE_ID>.default.so
include $(CLEAR_VARS)

LOCAL_MODULE_RELATIVE_PATH := hw
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_SRC_FILES := serial.cpp
LOCAL_MODULE := serial_test
LOCAL_MODULE_TAGS := optional
LOCAL_PROPRIETARY_MODULE := true

include $(BUILD_EXECUTABLE)

猜你喜欢

转载自blog.csdn.net/zhuyong006/article/details/82804287