當前位置:首頁 > 公眾號精選 > 嵌入式技術開發(fā)
[導讀]SWM32S單片機有1個SDIO接口,支持多媒體卡(MMC)、SD 存儲卡、SDIO 卡等設備,可以使用軟件方法或者 DMA 方法(SDIO 模塊內部 DMA,與芯片 DMA 模塊無關)進行數(shù)據(jù)傳輸。

SWM32S單片機有1個SDIO接口,支持多媒體卡(MMC)、SD 存儲卡、SDIO 卡等設備,可以使用軟件方法或者 DMA 方法(SDIO 模塊內部 DMA,與芯片 DMA 模塊無關)進行數(shù)據(jù)傳輸。其特點如下:



1.SDIO配置 SDIO的引腳不像UART和SPI等數(shù)字外設一樣可以靈活配置,它是固定的幾個引腳,如下圖 使用起來也比較簡單,相關的庫函數(shù)已經封裝好了,直接調用就行。 首先需要配置一下相關引腳的復用功能為SDIO:
 PORT_Init(PORTB, PIN1, PORTB_PIN1_SD_CLK, 0); PORT_Init(PORTB, PIN2, PORTB_PIN2_SD_CMD, 1); PORT_Init(PORTB, PIN3, PORTB_PIN3_SD_D0,  1); PORT_Init(PORTB, PIN4, PORTB_PIN4_SD_D1,  1); PORT_Init(PORTB, PIN5, PORTB_PIN5_SD_D2,  1); PORT_Init(PORTB, PIN6, PORTB_PIN6_SD_D3,  1);

然后直接調用初始化的函數(shù)就行:
result = SDIO_Init(10000000);

其中形參為SDIO的時鐘頻率。 SD卡讀寫的相關函數(shù)也已經封裝好了,包括DMA和非DMA方式,直接調用即可:
uint32_t SDIO_BlockWrite(uint32_t block_addr, uint32_t buff[]);uint32_t SDIO_BlockRead(uint32_t block_addr, uint32_t buff[]);uint32_t SDIO_MultiBlockWrite(uint32_t block_addr, uint16_t block_cnt, uint32_t buff[]);uint32_t SDIO_MultiBlockRead(uint32_t block_addr, uint16_t block_cnt, uint32_t buff[]);uint32_t SDIO_DMABlockWrite(uint32_t block_addr, uint16_t block_cnt, uint32_t buff[]);uint32_t SDIO_DMABlockRead(uint32_t block_addr, uint16_t block_cnt, uint32_t buff[]);



2.FatFs移植 底層讀寫函數(shù)有了以后,移植FatFs也就比較簡單了,首先將FatFs的相關文件添加到工程中: 我們需要實現(xiàn)的接口函數(shù)在diskio.c文件中,包括至少以下3個函數(shù): 初始化函數(shù),直接將端口配置、SDIO初始化添加到該函數(shù)中:
DSTATUS disk_initialize ( BYTE pdrv        /* Physical drive nmuber to identify the drive */){ DSTATUS stat; int result;  switch (pdrv) { case DEV_RAM : //result = RAM_disk_initialize(); // translate the reslut code here stat = STA_NOINIT; return stat; case DEV_MMC : //result = MMC_disk_initialize(); // translate the reslut code here PORT_Init(PORTB, PIN1, PORTB_PIN1_SD_CLK, 0); PORT_Init(PORTB, PIN2, PORTB_PIN2_SD_CMD, 1); PORT_Init(PORTB, PIN3, PORTB_PIN3_SD_D0,  1); PORT_Init(PORTB, PIN4, PORTB_PIN4_SD_D1,  1); PORT_Init(PORTB, PIN5, PORTB_PIN5_SD_D2,  1); PORT_Init(PORTB, PIN6, PORTB_PIN6_SD_D3,  1);  result = SDIO_Init(10000000); if(result == SD_RES_OK) { stat = RES_OK; sd_initialized = 1; } else { stat = STA_NOINIT; sd_initialized = 0; } return stat; case DEV_USB : //result = USB_disk_initialize(); // translate the reslut code here stat = STA_NOINIT; return stat; } return STA_NOINIT;}
讀函數(shù)
DRESULT disk_read ( BYTE pdrv,    /* Physical drive nmuber to identify the drive */ BYTE *buff,    /* Data buffer to store read data */ LBA_t sector,  /* Start sector in LBA */ UINT count    /* Number of sectors to read */){ DRESULT res; int result;  switch (pdrv) { case DEV_RAM : // translate the arguments here //result = RAM_disk_read(buff, sector, count); // translate the reslut code here res = RES_PARERR; return res; case DEV_MMC :  if(count == 1) { result = SDIO_BlockRead(sector, (uint32_t *)buff); } else { //result = SDIO_MultiBlockRead(sector, count, (uint32_t *)buff); result = SDIO_DMABlockRead(sector, count, (uint32_t *)buff);//使用DMA或非DMA模式均可 }  if(result == SD_RES_OK) res = RES_OK; else res = RES_ERROR;  return res; case DEV_USB : // translate the arguments here //result = USB_disk_read(buff, sector, count); // translate the reslut code here  res = RES_PARERR; return res; } return RES_PARERR;}
寫函數(shù)
DRESULT disk_write ( BYTE pdrv,      /* Physical drive nmuber to identify the drive */ const BYTE *buff,  /* Data to be written */ LBA_t sector,    /* Start sector in LBA */ UINT count      /* Number of sectors to write */){ DRESULT res; int result;  switch (pdrv) { case DEV_RAM : // translate the arguments here //result = RAM_disk_write(buff, sector, count); // translate the reslut code here res = RES_PARERR; return res; case DEV_MMC : if(count == 1) { result = SDIO_BlockWrite(sector, (uint32_t *)buff); } else { //result = SDIO_MultiBlockWrite(sector, count, (uint32_t *)buff); result = SDIO_DMABlockWrite(sector, count, (uint32_t *)buff);//使用DMA或非DMA模式均可 } if(result == SD_RES_OK) res = RES_OK; else res = RES_ERROR; return res; case DEV_USB : // translate the arguments here //result = USB_disk_write(buff, sector, count); // translate the reslut code here res = RES_PARERR; return res; } return RES_PARERR;}
還有一個函數(shù)是用戶獲取SD卡容量等信息的,如下:
DRESULT disk_ioctl ( BYTE pdrv,    /* Physical drive nmuber (0..) */ BYTE cmd,    /* Control code */ void *buff    /* Buffer to send/receive control data */){ DRESULT res; //int result;  switch (pdrv) { case DEV_RAM : // Process of the command for the RAM drive res = RES_PARERR; return res; case DEV_MMC : switch ( cmd )  {      //fatfs內核使用cmd調用 case GET_SECTOR_COUNT:  //sector count *(DWORD*)buff = SD_cardInfo.CardCapacity / 512; return RES_OK; case GET_SECTOR_SIZE:  //sector size, 傳入block size(SD),單位bytes *(DWORD*)buff = 512; return RES_OK; case GET_BLOCK_SIZE:  //block size, 由上文可得,對于SD2.0卡最大8192,最小 1 *(DWORD*)buff = 1;  //單位為 sector(FatFs) return RES_OK; case CTRL_SYNC:      //同步命令,貌似FatFs內核用來判斷寫操作是否完成 return RES_OK; } res = RES_OK; return res; case DEV_USB : // Process of the command the USB drive res = RES_PARERR; return res; } return RES_PARERR;}
接口部分實現(xiàn)后,就可以調用相關的函數(shù)實現(xiàn)文件的讀寫了,測試程序如下:
res = f_mount(&fatfs, "sd:", 1);if(res != FR_OK){ printf("sdcard init fail!\r\n");}res = f_open(&filw, "sd:test.txt", FA_CREATE_ALWAYS | FA_WRITE);if(res != FR_OK){ printf("create file fail!\r\n");}res = f_write(&filw, str, strlen(str), &len);if(res != FR_OK){ printf("write file fail!\r\n");}f_close(&filw);

文件系統(tǒng)的配置可以在"ffconf.h"文件中根據(jù)實際需求進行修改。

本站聲明: 本文章由作者或相關機構授權發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內容真實性等。需要轉載請聯(lián)系該專欄作者,如若文章內容侵犯您的權益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或將催生出更大的獨角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉型技術解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術公司SODA.Auto推出其旗艦產品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關鍵字: 汽車 人工智能 智能驅動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務能7×24不間斷運行,同時企業(yè)卻面臨越來越多業(yè)務中斷的風險,如企業(yè)系統(tǒng)復雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務連續(xù)性,提升韌性,成...

關鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報道,騰訊和網易近期正在縮減他們對日本游戲市場的投資。

關鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產業(yè)博覽會開幕式在貴陽舉行,華為董事、質量流程IT總裁陶景文發(fā)表了演講。

關鍵字: 華為 12nm EDA 半導體

8月28日消息,在2024中國國際大數(shù)據(jù)產業(yè)博覽會上,華為常務董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權最終是由生態(tài)的繁榮決定的。

關鍵字: 華為 12nm 手機 衛(wèi)星通信

要點: 有效應對環(huán)境變化,經營業(yè)績穩(wěn)中有升 落實提質增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務引領增長 以科技創(chuàng)新為引領,提升企業(yè)核心競爭力 堅持高質量發(fā)展策略,塑強核心競爭優(yōu)勢...

關鍵字: 通信 BSP 電信運營商 數(shù)字經濟

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術學會聯(lián)合牽頭組建的NVI技術創(chuàng)新聯(lián)盟在BIRTV2024超高清全產業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術創(chuàng)新聯(lián)...

關鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(集團)股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關鍵字: BSP 信息技術
關閉