【Android驱动】SPI具体例子的一个代码框架

//连接到soc

aliases {
    
    spi1 = &spi_1
};
&spi_1 {
    aaaa_spi@0 {
        compatible = "AAAAAAAA";
        reg = <0>;
        spi-max-frequency = <4000000>;
        spi-cpha = <0>;
        spi-cpol = <0>;
        spi-cs-low;
        spi-msb-first;
        status = "okey";
    };
}
驱动框架:

//驱动全局变量
struct spi_device *chaoDevice_spi;


static void chaoDeivce_config_init(struct spi_device* spi)
{
	//初始化spi设备成员,dtsi里也可初始化
	spi->mode = SPI_MODE_0;
	spi->bit_per_word = 8;
	spi->max_speed_hz = 19200 * 1000;
	spi->chip_select = 0;
	//spi->controller_data = &chaoDevice_spi_config;
	mdelay(1000);
}

static int chaoDevice_spi_probe(struct spi_device *spi)
{
	int ret;
	chaoDevice_spi = spi;
	chaoDevice_spi_config_init(chaoDevice_spi);
}

static cosnt struct of_device_id chapDeivice_id_table[] = {
	{.compatible = "AAAAAAA"},
};

static struct spi_driver chao_spi_driver = {
	.driver = {
		.name = "chaoDevice_spi",
		.bus = &spi_bus_type,
		.owner = THIS_MODULE,
		.of_match_table = chaoDeivce_id_table,
	},
	.probe = chaoDevice_probe,
	.remove = chaoDevice_remove,
};

static void __exit rc522_spi_exit(void)
{
	spi_unregister_driver(&chao_spi_driver);
}
读:

int chaoDevice_spi_read_byte(struct spi_device *spi, uint8_t addr, uint8_t *data)
{
	int ret;
	struct spi_transfer xfer = {0,};
	struct spi_message msg;
	
    //读写buf填充xfer结构体
	uint8_t tx_buf[3] = {0,0,0};
	uint8_t rx_buf[3] = {0xff,0xff,0x0};
	
	tx_buf[0] = addr & 0xff;
	tx_buf[1] = 0x0;
	
	xfer.tx_buf = &tx_buf;
	xfer.rx_buf = &rx_buf;
	xfer.len = 2;
	
	spi_message_init(&msg);
	spi_message_add_tail(&xfer, &msg);
	ret = spi_sync(spi, &msg);
	if(ret < 0)
	{
		printk("%s spi read error\n", __func__);
		return ret;
	}
	
	*data = rx_buf[1];
	return ret;
}   
写:
int chaoDevice_spi_write_byte(struct spi_device *spi, uint8_t addr, uint8_t data)
{
	int ret - 0;
	struct spi_transfer xfer = {0, };
	struct spi_message msg;
	
	uint8_t tx_buf[3] = {0,0,0};
	uint8_t rx_buf[3] = {0,0,0};
	
	tx_buf[0] = addr & 0xff;
	tx_buf[0] = data & 0xff;
	
	xfer.tx_buf = &tx_buf;
	xfer.rx_buf = &rx_buf;
	xfer.len = 2;
	
	spi_message_init(&msg);
	spi_message_add_tail(&xfer, &msg);
	ret = spi_sync(spi, &msg);
	
	if(ret < 0)
	{
		printk("%s spi write error", __func__);
		return -1;
	}
	
	return ret;
}
调用:

    unsigned char result = 0;
    chaoDevice_spi_read_byte(chaoDevice_spi, 0x22, &result);

    chaoDevice_spi_write_byte(chaoDevice_spi, 0x33, 250);






------------------------------
读写总体来看,比较简单,内核核心层已经把api做好了,太底层api的不关注。

从类的角度看,就是实例化一个spi_transfer对象,填充结构体成员,然后通过接口发出去就完事了。

读写字节代码甚至说是差不多,只是读,传回了地址,从地址读出寄存器的值。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请参考下面的代码:// Initialize SPI // SPIx is the SPI peripheral e.g. SPI1 void SPI_init (SPI_TypeDef* SPIx){ // Enable clock for SPIx if (SPIx == SPI1) RCC_APB2PeriphClockCmd (RCC_APB2Periph_SPI1, ENABLE); else if (SPIx == SPI2) RCC_APB1PeriphClockCmd (RCC_APB1Periph_SPI2, ENABLE); // Set MOSI, MISO and SCK as AF GPIO_PinAFConfig (GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); GPIO_PinAFConfig (GPIOA, GPIO_PinSource6, GPIO_AF_SPI1); GPIO_PinAFConfig (GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); // Configure SPI pins: SCK, MISO and MOSI GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init (GPIOA, &GPIO_InitStructure); // SPI configuration SPI_InitTypeDef SPI_InitStructure; SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_Init (SPIx, &SPI_InitStructure); // Enable SPIx SPI_Cmd (SPIx, ENABLE); }// SPI write and read // SPIx is the SPI peripheral e.g. SPI1 // dataOut is the byte to transmit // dataIn is the received byte // return the received byte uint8_t SPI_write_read (SPI_TypeDef* SPIx, uint8_t dataOut){ uint8_t dataIn; // Wait until the transmit buffer is empty while (SPI_I2S_GetFlagStatus (SPIx, SPI_I2S_FLAG_TXE) == RESET); // Send the byte SPI_I2S_SendData (SPIx, dataOut); // Wait to receive a byte while (SPI_I2S_GetFlagStatus (SPIx, SPI_I2S_FLAG_RXNE) == RESET); // Return the byte read from the SPI bus dataIn = SPI_I2S_ReceiveData (SPIx); return dataIn; }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值