Linux Spidev spi 9bits 模式参考代码

本文档提供了一个使用spidev驱动在Linux下进行SPI通信的测试工具代码,特别地,它展示了如何在9位模式下进行操作。代码中包含了设置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 <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 void pabort(const char *s)
{
perror(s);
abort();
}


static const char *device = "/dev/spidev1.1";
static uint8_t mode;
static uint8_t bits = 9;
static uint32_t speed = 500000;
static uint16_t delay;


static void transfer(int fd)
{
int ret;
uint8_t tx[] = {
//0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
//0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
//0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
//0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
//0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
//0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,
//0xF0, 0x0D,
0xA5,0xF0,0x85
};
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);
if
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值