基于硬件?SPI?的數(shù)據(jù)抽象實(shí)例(附代碼)
作者?|??Acuity
1.寫在前面
spi(Serial Peripheral Interface)即串行外設(shè)接口。與i2c一樣,spi也常用外設(shè)設(shè)備通信的總線,從事嵌入式開發(fā)必不可少的掌握。根據(jù)本人以往的經(jīng)歷,對spi進(jìn)行總結(jié)(主要是MCU范疇,Linux已有成熟的驅(qū)動設(shè)備),主要目的及實(shí)現(xiàn):1)spi總線與spi設(shè)備分離;2)快速使用新的硬件spi或者模擬spi;3)方便移植spi總線設(shè)備及spi外設(shè)程序到不同mcu平臺。2.spi總線抽象
此部分實(shí)現(xiàn)源碼為:spi_core.c spi_core.h2.1 spi總線模型對外接口(API)
/*extern?function*/
extern?int?spi_send_then_recv(struct?spi_dev_device?*spi_dev,const?void?*send_buff,
?????????unsigned?short?send_size,void?*recv_buff,unsigned?short?recv_size);
extern?int?spi_send_then_send(struct?spi_dev_device?*spi_dev,const?void?*send_buff1,
?????????unsigned?short?send_size1,const?void?*send_buff2,unsigned?short?send_size2);
extern?int?spi_send_recv(struct?spi_dev_device?*spi_dev,const?void?*send_buff,void?*recv_buff,unsigned?short?data_size);
extern?int?spi_send(struct?spi_dev_device?*spi_dev,const?void?*send_buff,unsigned?short?send_size);
1)spi_send_then_recv,標(biāo)準(zhǔn)spi,常規(guī)操作,發(fā)送完一幀再接收,如讀取某芯片寄存器的值;2)spi_send_then_send,標(biāo)準(zhǔn)spi,常規(guī)操作,發(fā)送完一幀再發(fā)送,如向某芯片寄存器(地址)寫入數(shù)據(jù);3)spi_send_recv,非標(biāo)spi,具體看芯片時序圖,產(chǎn)生時鐘信號,發(fā)送完成的同時,也接收完成;第二種情況是,只接收,發(fā)送動作只是用來產(chǎn)生時鐘信號,如一些AD芯片;4)spi_send,標(biāo)準(zhǔn)或非標(biāo)spi都使用,只發(fā)送無返回值或者無須理會返回值,如spi LCD屏。2.2 spi總線抽象API實(shí)現(xiàn)
以“spi_send_then_recv”函數(shù)為例:1)spi_dev:spi設(shè)備指針,類型為“struct spi_dev_device”,驅(qū)動一個spi外設(shè)時,首先需要對此指針進(jìn)行初始化;2)send_buff:待發(fā)送數(shù)據(jù)(緩存);3)send_size:發(fā)送數(shù)據(jù)量大?。▎挝蛔止?jié));4):recv_buff:存放返回值數(shù)據(jù)緩存(地址);5):recv_size:返回數(shù)據(jù)量大小。另外3個函數(shù),第一個參數(shù)都為spi設(shè)備指針,其他參數(shù)為發(fā)送/接收緩沖區(qū),收發(fā)數(shù)據(jù)量等,通過變量名即可看出。2.3 struct spi_de_device
該結(jié)構(gòu)體為關(guān)鍵,調(diào)用API驅(qū)動一個外設(shè)時,需要先初始化(類似Linux的注冊設(shè)備驅(qū)動)。一個完整的spi外設(shè),包括片選和總線量部分,一個總線可和多個片選組成,驅(qū)動多個外設(shè),因此struct spi_dev_device設(shè)計原型為:struct?spi_dev_device
{?
????void?(*spi_cs)(unsigned?char?state);?
????struct?spi_bus_device?*spi_bus;?
};
1)第一個參數(shù)為函數(shù)指針,主要功能的實(shí)現(xiàn)spi外設(shè)片選的選擇(拉低/拉高)功能;2)第二個參數(shù)為spi總線相關(guān)的結(jié)構(gòu)體指針,主要是底層相關(guān)收據(jù)收發(fā)的的功能,具體繼續(xù)往下看改結(jié)構(gòu)體。2.4 struct spi_bus_device *spi_bus
該結(jié)構(gòu)體為底層硬件相關(guān)的spi總線實(shí)現(xiàn),具體由實(shí)際需求實(shí)現(xiàn),如用硬件spi還是用模擬spi。struct spi_bus_device*spi_bus原型為:struct?spi_bus_device
{?
????int?(*spi_bus_xfer)(struct?spi_dev_device?*spi_bus,struct?spi_dev_message?*msg);
????void?*spi_phy;
????unsigned?char?data_width;
};
1)第一個參數(shù)是函數(shù)指針,為spi總線收發(fā)函數(shù),這部分就是我們平常寫裸機(jī)代碼時候?qū)懙降?,只是這里把它放在一個結(jié)構(gòu)體里面,以函數(shù)指針的方式實(shí)現(xiàn);這樣的好處是,上層接口不變,更好其他MCU或者使用模擬spi時,只需修改此部分的函數(shù)實(shí)體,上層代碼不需變動。2)第二個參數(shù),一個指針,表示具體物理spi,如stm32的SPI1、SPI2,或者模擬spi;3)第三參數(shù),數(shù)據(jù)寬度,一般是8bit或者16bit。其他參數(shù),如數(shù)據(jù)速率、spi模式等,其實(shí)也可以放在此處,只是個人覺得此類參數(shù)不常變動,為了節(jié)約內(nèi)存,故不加入此結(jié)構(gòu)體配置中。下面中斷分析函數(shù)指針“int?(*spi_bus_xfer)?(struct spi_dev_device *spi_bus,struct spi_dev_message *msg)”的實(shí)現(xiàn)。
2.5 spi_bus_xfer
該函數(shù)指針入口參數(shù)為spi設(shè)備指針(struct spi_dev_device )、spi設(shè)備信息幀指針(struct spi_dev_message)。struct spi_dev_device與前面提及的為同一類參數(shù),struct spi_dev_message為收發(fā)數(shù)據(jù)信息幀,其原型如下:struct?spi_dev_message
{
????const?void??*send_buf;
????void????????*recv_buf;
????int??length;
????unsigned?char?cs_take????:?1;
????unsigned?char?cs_release?:?1;
};
1)send_buf:待發(fā)送數(shù)據(jù)(緩存);2)recv_buf:存放返回值數(shù)據(jù)緩存(地址);3)length:發(fā)送/接收數(shù)據(jù)長度;4)cs_take:使能片選;5)cs_release:釋放片選。3. spi總線抽象實(shí)現(xiàn)
此部分實(shí)現(xiàn)源碼為:spi_hw.c spi_hw.h3.1 spi總線抽象API實(shí)現(xiàn)
第一步:“spi_send_then_recv”,實(shí)現(xiàn)代碼如下:int?spi_send_then_recv(struct?spi_dev_device?*spi_dev,const?void?*send_buff,unsigned?short?send_size,void?*recv_buff,unsigned?short?recv_size)
{
????struct?spi_dev_message?message;
?
????message.length?????=?send_size;
????message.send_buf???=?send_buff;
????message.recv_buf???=?0;
????message.cs_take????=?1;
????message.cs_release?=?0;
????spi_dev->spi_bus->spi_bus_xfer(spi_dev,