#include "lpc214x.h" // Header file for Phillips LPC2148 controller #include "sd.h" // Library for fundamental function for SD/MMC card #include "uart.h" // Library for use module UART0,UART1 #include "stdio.h" // Library for use puts function(For UART1) #include "string.h" // Library for use string group function
#ifndef REG_CSD #define REG_CSD 9 // Define for used CMD9(read CSD register value) #endif #ifndef REG_CID #define REG_CID 10 // Define for used CMD10(read CID register value) #endif
// Struct variable for keep CSD register value from card(For calculate capacity size only) struct _csd { unsigned char READ_BLK_LEN; unsigned char C_SIZE_MULT; unsigned short C_SIZE; }CSD; // Struct variable for keep CID register value from card struct _cid { unsigned char MID; unsigned short OID; char PNM[7]; unsigned char PRV; unsigned char PSN[4]; unsigned char MDT; unsigned char CRC7; }CID;
//------------------------------------------------------------------------------------------------// //---------------------------- Get CSD or CID register value from card ---------------------------// //------------------------------------------------------------------------------------------------// int sd_get_register(unsigned char reg) { unsigned short Checksum; // For keep CRC check but not used unsigned char cmd[6]; // For keep command for send to card unsigned char buff[17]; // For return value from card unsigned char status=0; // For keep status when error SPI_ENABLE(); // Connect to SD card // send MMC CMD9(SEND_CSD) to read the data from MMC card cmd[0] = 0x40 | reg; cmd[1] = 0; cmd[2] = 0; cmd[3] = 0; cmd[4] = 0; // checksum is no longer required but we always send 0xFF cmd[5] = 0xFF; spi_send(cmd,6); // if mmc_response returns 1 then we failed to get a 0x00 response if((sd_response(0x00))==1) { status = READ_BLOCK_TIMEOUT; // Load error even SPI_DISABLE(); // Disconnect to SD card return status; // Report error even } // wait for data token if((sd_response(0xFE))==1) { status = READ_BLOCK_DATA_TOKEN_MISSING; // Load error even SPI_DISABLE(); // Disconnect to SD card return status; // Report error even } // Get the block of data based on the length spi_read_stream(buff,16); // Get data 16 byte from card(register value) // CRC bytes that are not needed Checksum = spi_read_byte(); Checksum = Checksum << 0x08 | spi_read_byte(); SPI_DISABLE(); // Disconnect to SD card spi_read_byte();
// Filter process for CSD register value if(reg==REG_CSD) { CSD.C_SIZE = (buff[6]<<10) | (buff[7]<<2) | ((buff[8] & 0xC0)>>6); CSD.READ_BLK_LEN = (buff[5] &0x0F); CSD.C_SIZE_MULT = ((buff[9] & 0x03)<<1) | ((buff[10] & 0x80)>>7); } // Filter process for CID register value else if(reg==REG_CID) { CID.MID = buff[0]; CID.OID = (buff[1]<<8) | (buff[2]); CID.PNM[0] = buff[3]; CID.PNM[1] = buff[4]; CID.PNM[2] = buff[5]; CID.PNM[3] = buff[6]; CID.PNM[4] = buff[7]; CID.PNM[5] = buff[8]; CID.PNM[6] = '\0'; CID.PRV = buff[9]; CID.PSN[0] = buff[10]; CID.PSN[1] = buff[11]; CID.PSN[2] = buff[12]; CID.PSN[3] = buff[13]; CID.MDT = buff[14]; CID.CRC7 = buff[15]; } return 0; } //------------------------------------------------------------------------------------------------// //---------------------------- Function for Initial system clock --------------------------------// //------------------------------------------------------------------------------------------------// void init() { PLL0CFG=0x24; // MSEL = 4,PSEL = 2 PLL0FEED=0xAA; // Feed process PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; // Feed process PLL0FEED=0x55;
while(!(PLL0STAT & 0x400)) ; // Wait until PLL Locked PLL0CON=0x3; // Connect the PLL as the clock source PLL0FEED=0xAA; // Feed process PLL0FEED=0x55; MAMCR=0x2; // Enabling MAM and setting number of clocks used for Flash memory fetch (4 cclks in this case) MAMTIM=0x4; VPBDIV=0x02; // PCLK at 30 MHz
} //------------------------------------------------------------------------------------------------// //--------------------- Function for calculate capacity size for SD/MMC card ---------------------// //------------------------------------------------------------------------------------------------// float sd_get_capacity() { float cap; // For keep capacity result sd_get_register(REG_CSD); // Get CSD register value from card cap = (CSD.C_SIZE+1)*(1<<(CSD.C_SIZE_MULT+2))*(1< return(cap/1048567); // Return capacity size of card } //------------------------------------------------------------------------------------------------// //---------------------------------- Main Program ------------------------------------------------// //------------------------------------------------------------------------------------------------// int main (void) { unsigned long block_num=0; init(); uart1_init(9600); // Use UATR1 Monitor from Terminal program SCS = 0x03; // Select the "fast" version of the I/O ports PINSEL1 = 0x40004000; // Select pin connect spi_init(); // Initialize SPI for SD card puts("SD Card Test...\r\n"); // Display message for first time if(sd_init() != 0) // Test SD card connect? { puts("SD Card Not Found!\r\n"); // When SD card not found } else { sd_get_register(REG_CSD); // Get CSD register value from card sd_get_register(REG_CID); // Get CID register value from card
printf("\rMEMORY = %.2f MB\n", sd_get_capacity()); // Display Capacity size printf("\r\n"); // New line printf("\rManufacture ID = 0x%x\n",CID.MID); // Display Manufacture ID printf("\rOEM/Application ID = 0x%x\n",CID.OID); // Display OEM/Application ID printf("\rProduct Name = %s\n",CID.PNM); // Display Product Name // Write data 10 block process(Block 0-9) for(block_num=1;block_num<11;block_num++) { sprintf(DATA_OUT,"data Block %d",block_num); // Keep data sd_write_block(block_num); // Write data } // Read data 10 block process(Block 0-9) for(block_num=1;block_num<11;block_num++) { sd_read_block(block_num); // Read data printf("Block(%d): %s\r\n",block_num,DATA_IN); // Display data } } while (1); // Loop forever }
|
No feedbacks found. Be the first to respond...
|