lpc1768 SD卡基于ucos的fatfs文件系统
- UID
- 1029342
- 性别
- 男
|
lpc1768 SD卡基于ucos的fatfs文件系统
一、应用程序使用步骤
1、初始化
[cpp] view plaincopy
- Init_Temp = disk_initialize( 1 ); // 为二底层驱动初始化 获取mmc信息
2、加载逻辑驱动器
[cpp] view plaincopy
- Fs_result = f_mount(0, &fs);//卸载或挂在一个逻辑驱动器
3.1、打开文件夹
[cpp] view plaincopy
- res = f_opendir(&dirs, "Music");
3.2打开文件
[cpp] view plaincopy
- Fs_result = f_open(&fsrc, "ftxt.txt", FA_OPEN_EXISTING | FA_READ);
4读取文件
[cpp] view plaincopy
- res = f_read(&fsrc, buffer, sizeof(buffer), &br);
5、关闭文件
[cpp] view plaincopy
6、卸载驱动器
[cpp] view plaincopy
二 驱动程序
1、SPI初始化
[cpp] view plaincopy
- void SPIInit(void)
- {
- /* 设置MX25L1602的控制引脚 */
- // PINSEL0 = 0;
- FIO0DIR |= F016B_CS;
- /* 设置硬件SPI的通讯脚 */
- PINSEL0 |= 0xc0000000; // 设置P0.15脚为SCK脚
- PINSEL1 = (0x03 << 2) | (0x03 << 4); // 设置P0.17、P0.18引脚为SPI引脚
- S0SPCCR = 0x40; // 设置SPI时钟分频,可按需求配置
- /* 设置SPI的工作方式 */
- S0SPCR = (0 << 2) | // SPI控制器每次传输发送和接收8位数据。
- (0 << 3) | // CPHA = 0, 数据在SCK 的第一个时钟沿采样
- (0 << 4) | // CPOL = 0, SCK 为低有效
- (1 << 5) | // MSTR = 1, SPI 处于主模式
- (0 << 6) | // LSBF = 0, SPI 数据传输MSB (位7)在先
- (0 << 7); // SPIE = 0, SPI 中断被禁止
- }
2、MMC初始化
[cpp] view plaincopy
- int MMC_disk_initialize(void)
- {
- /* Initialize and enable the Flash Card. */
- U32 i,r1;
- /* Initialize SPI interface and enable Flash Card SPI mode. */
- spi_init ();
- spi_ss (1); spi_send (0xFF);
- spi_hi_speed (__FALSE);
- /* Send SPI Command with card not selected at 400 KBit. */
- for (i = 0; i < 16; i++) {
- spi_send (0xFF);
- }
- /* Reset the card, send CMD0. */
- spi_ss (0);
- r1 = mmc_command (GO_IDLE_STATE, 0);
- for (i = 0; i < 100; i++) {
- if (r1 == 0x01) {
- break;
- }
- r1 = spi_send (0xFF);
- }
- spi_ss (1); spi_send (0xFF);
- if (r1 != 0x01) {
- /* Failed to Reset Flash Card. */
- return (__FALSE);
- }
- spi_hi_speed (__TRUE);
- CardType = CARD_NONE;
- /* Check if SD card, send ACMD41 */
- for (i = 0; i < 50000; i++) {
- spi_ss (0);
- r1 = mmc_command (APP_CMD, 0);
- spi_ss (1);
- if (r1 & 0x04) {
- /* Illegal Command. */
- break;
- }
- if (r1 == 0x01) {
- spi_ss (0);
- r1 = mmc_command (SD_SEND_OP_COND, 0);
- spi_ss (1); spi_send (0xFF);
- if (r1 == 0x00) {
- /* OK, SD card initialized. */
- CardType = CARD_SD;
- break;
- }
- }
- }
- if (CardType == CARD_NONE) {
- /* Initialize MMC Card, send CMD1. */
- for (i = 0; i < 50000; i++) {
- spi_ss (0);
- r1 = mmc_command (SEND_OP_COND, 0);
- spi_ss (1);spi_send (0xFF);
- if (r1 != 0x01) {
- break;
- }
- }
- if (r1 != 0x00) {
- /* Failed to Initialize the card. */
- return (__FALSE);
- }
- CardType = CARD_MMC;
- }
- /* Set block length in the Flash Card to 512 bytes. */
- spi_ss (0);
- r1 = mmc_command (SET_BLOCKLEN, 512);
- spi_ss (1); spi_send (0xFF);
- if (r1 != 0x00) {
- return (__FALSE);
- }
- /* Turn Off CRC option. */
- spi_ss (0);
- r1 = mmc_command (CRC_ON_OFF, 0);
- spi_ss (1); spi_send (0xFF);
- if (r1 != 0x00) {
- return (__FALSE);
- }
- /* Success, card initialized. */
- return (__TRUE);
- }
3、底层SPI收发函数
[cpp] view plaincopy
- void Send_Byte(INT8U data)
- {
- S0SPDR = data;
- while ( 0 == (S0SPSR & 0x80)); // 等待SPIF置位,即等待数据发送完毕
- data = S0SPSR; // 清除SPIF标志
- }
- /************************************************************************
- ** 函数名称:Get_Byte
- ** 函数功能:通过硬件SPI接口接收一个字节到处理器
- ** 入口参数:无
- ** 出口参数:无
- ************************************************************************/
- INT8U Get_Byte(void)
- {
- INT8U temp;
- S0SPDR = 0xff; // 发送该数据用以产生时钟,数据本身没有用处
- while ( 0 == (S0SPSR & 0x80)); // 等待SPIF置位,即等待数据发送完毕
- temp = S0SPSR; // 清除SPIF标志
- temp=temp;
- return (INT8U)(S0SPDR); // 返回接收到的数据
- }
|
|
|
|
|
|