Home

User Manual - Brush Electronics

image

Contents

1. M 13 E 13 E uic M 14 Copyright 2004 2012 Andrew Smallridge Page 1 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 Sample Application oet e Ete ER n eee e E Fen a Ea ao ae LE Re aa ek vessteesalaseateaninas 15 SD MMC Card Integration and Troubleshooting eene 20 Power Supply iis eir REESEN FE ERROR UR ERE RA 21 SI DEE 21 Media Der 22 Lacensing Consid ratiOns sasise ote intern oett entia ok RR YA SEE ERRRR Eia ENE KAREE A 23 Copyright 2004 2012 Andrew Smallridge Page 2 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 Introduction The Brush Electronics SD MMC Embedded FAT file system is a software driver for Microchip microcontrollers enabling the microcontrollers to read and or write standard and high capacity SDHC SD MMC cards using FAT16 or FAT32 formatted media FAT file system support enables the media to be exchanged between other devices that support the FAT16 and or FAT32 file system For example files created by an embedded system using this driver can be read from and or written to on a Windows PC Variants of the file system driver are available for the Microchip PIC18F PIC24 dsPIC33 and PIC32 families of
2. Px DefTRIS Port x Default TRIS specifies the default I O configuration of a port Input versus Output VMIMMMMB MM LMLHMMMMMMMMPMMMMMMMMMEMEMPPMMMOUM MM MP MA TATA TTT TTT ILIE define PA DefData 0b0000000000000000 define PA_DefTRIS 0b1111 00000000 define PB_DefData 0b0111000000000010 define PB_DefTRIS 0b1000 1111101 1 SD CS top slot of J5 define PC DefData 0b0000000000000000 define PC_DefTRIS 0b1111 1111111 define PD DefData 0b1001000000000000 define PD_DefTRIS 0b0110 1111111 15 XEE_CS EXPLORER16 non rev 4 12 XEE_CS EXPLORER16_REV4 define PE_DefData 0b0000000000000000 define PE_DefTRIS 0b1111111111111111 define PF_DefData 0b0000000000101000 define PF_DefTRIS 0b1111111010010111 8 SPI1_SDO SD MMC SPI SDO T SPI1_SDI SD MMC SPI SDI 6 SPI1_CLK SD MMC SPI CLK 5 U2TX 4 U2RX fi lt 3 U1TX ff 32 U1LRX 1 SD WP SD MMC Write Protect switch input 0 SD_CD SD MMC Card Detect switch input define PG_DefData 0b0000000000000000 define PG_DefTRIS 0b1111111010111111 8 SPI1_SDO SD MMC SPI SDO E SPI1_SDI SD MMC SPI SDI 6 SPI1_CLK SD MMC SPI CLK VMIMIMMMMMMMMMMPMMMIIMMMEPMBMMIMP PM MMMMMPMPMPWMBMPMWMB dd dd dd EE SD MMC PICTail Interfce Connections for Top Slot ee CARR RACING CRG RI Cen ee an MOO define SD_SPI_PORT define SD_CS Pei ee SD MMC Chip Select Active low define SD_CD PORTFbits RFO card detect pin VMIMMMMMMUMIMMMPMPMPMBPMl MBPlPllllllWlllllldd VV V VL P P P P P PWMPWPPPP PB Pg P
3. Parameters path is a pointer to the fully qualified 8 3 formatted null terminated filename path string finfo is a pointer to a FILINFO file information data structure Returns FRESULT return code Function Returns the file status of the target file or directory The information is populated into the FILINFO data structure pointed to be finfo Example FIL fdata FILINFO finfo FRESULT result result f open path amp finfo if result printf File status error r n f sync Prototype BRE SUM ie f syne FIL fp Syntax result f_sync amp fdata Parameters fp is a pointer to a FIL file control block handle Returns FRESULT return code Function Flushes cached file data to the media Used to force the synchronization between the file system and media Typically used in data logging applications when small data samples are logged infrequently and data loss of the data in the file buffer could occur is the power failed or the media was removed without closing the file f_close automatically invokes f_sync Example FIL fdata FRESULT result result f sync amp fdata if result printf File sync error r n f unlink Prototype FRESULT f unlink char path Syntax result f unlink fname Parameters path is a pointer to the fully qualified 8 3 formatted null terminated filename path string Returns F RESULT retur
4. printf write_test FS ERROR on f_write r n goto Ex_write_test Copyright 2004 2012 Andrew Smallridge Page 17 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 write another short string to destination file strcpy mesg r nHello r n btw strlen mesg result f write amp fsrc mesg btw amp bw if result printf write_test FS ERROR on second f_write r n goto Ex write test example of syncing the file system not really needed here becasue we are about the close the file anyway printf syncing the file r n result f sync amp fsrc if result printf write test Error returned from f_sync r n goto Ex write test printf about to close the file r n f close amp fsrc printf r n return result Ex_write_test Close the file f close amp fsrc return result Ex write test no close printf r n return result Copyright 2004 2012 Andrew Smallridge Page 18 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 void main FRESULT FS_Status char target 16 disable_interrupts INTR_GLOBAL init pic initialise the interrupts this will intialise both the global and priority interrupts clear interrupt INT RDA e
5. 400KHz The SPI LOW prescalars are used by the media io driver low SPI speed clock rate define SPI LOW PRESCALE PRI 0 define SPI LOW PRESCALE SEC 0 to configure the Primary Scaler 64 Secondary Scaler 8 1 During the later stages of the media initialization the clock rate is increased The SPI HI prescalars are used by the media io driver to configure the high SPI speed clock rate define SPI HI PRESCALE PRI 1 Primary Scaler 16 1 define SPI HI PRESCALE SEC 7 Secondary Scaler 1 C24HJ256GP610 specific configuration Page 5 of 23 Brush Electronics endif 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 This is the HARDWARE SPECIFIC configuration Baudrate constants for console port define CONSOLE BAUDRATE 115200 Define UART I O define CONSOLE UART 2 define CONSOLE TX TRIS TRISFbits TRISF5 define CONSOLE RX TRIS TRISFbits TRISF4 VMIMIMMMMMMMBMBMMMaMIEMMMMEMEIEIEIIBBPCGMgM aM dd dd TTT Default I O configuration used by the init PIC function Px DefData Port x Default Data specifies the default output level for port bits configured as outputs SPI ports are often shared by multiple peripherals It is important that all chip selects are configured for a logic high output to avoid SPI bus conflict
6. Media Format SD MMC media are shipped pre formatted from the manufacturer The Brush Electronics SD MMC File System Drivers and the Utilities software interpret the data structures that are present on the SD MMC card During the format process some operating systems examine the data structures that exist on a card and format the card based on the existing structures In rare situations it is possible that the master boot record containing a partition table structure is corrupt and is not correctly dealt with by the format programs In this event the Brush Electronics software may not be able to correctly interpret and file system structure and a file system read error may result If this occurs format the media with a device that does perform a complete low level format such as a digit camera Copyright 2004 2012 Andrew Smallridge Page 22 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 Licensing Considerations Aside for hardware and software challenges the integration of SD and MMC cards in embedded systems is not without it headaches Licenses for integration of SD Cards or MMC Cards into products may be required from Microsoft IBM the SD Card Association for SD Cards and the Multimedia Association for MMC Cards Microsoft has been granted patents covering the File Allocation Table FAT file system These patents pertain to the integr
7. Smallridge Page 10 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 f_open Prototype FRESULT f_open FIL fp char path BYTE mode Syntax result f_open amp fdata fname modeflags Parameters fp is a pointer FIL file control block handle path is a pointer to the fully qualified 8 3 formatted null terminated filename path string mode is the read write create file access mode flags Returns FRESULT return code Function Opens or creates a file for read and or write operations Example FIL fdata FRESULT result result f_open amp fdata path FA_OPEN_ALWAYS FA_WRITE if result printf File open error r n f open Mode Flags FA CREATE ALWAYS Creates a new file If the file already exists it is truncated and overwritten FA OPEN ALWAYS Opens the file if it exists creates it otherwise To append data to the file use f lseek function after file open with this mode flag FA OPEN EXISTING Opens the file The function fails if the file does not exist To append data to the file use f lseek function after file with this mode flag FA READ Specifies read access to the object Data can be read from the file Combine with FA WRITE for read write access FA WRITE Specifies write access to the object Data can be written to the file Combine with FA READ
8. belongs to the sample application and NOT to the file system driver Before a developer attempts to integrate the file system driver into their own application Brush Electronics STRONGLY RECOMMENDS the developer first adapt the supplied sample application to run on their target hardware platform This will provide the developer the necessary insight into the configuration and operation of the driver Experience has shown that this approach will significantly reduce the time it takes the developer to integrate the driver directly into their user application Following this approach it typically takes less than two hours to have our sample application operational on your target platform Understanding the platform h file The platform h file contains target specific configuration information such as processor config fuse settings oscillator frequency peripheral pin mapping peripheral usage control pin assignments and I O port configuration The bulk of this information is used to configure the I O of the target platform in the init PIC function of the sample application This section will examine the platform h file and its integration into the sample application The examples used in this manual will be for the CCS PCD compiler however the concept and principles are the same for the CCS PCH compiler and the Microchip C18 C30 and C32 compilers The following is an extract from the sample application supplied with the file system driver for the
9. block handle ods is the desired offset from the start of the file Returns FRESULT return code Function Moves the file pointer Typically used to seek to the end of the file in order to append to the end of an existing file Example FIL fdata FRESULT result open the file If the file does not exist then create it result f open amp fdata path FA OPEN ALWAYS FA WRITE if result printf File open error r n seek to the end of the file if fdata fsize 0 result f lseek amp fdata fdata fsize f mkdir Prototype FRESULT f mkdir char path Syntax result f mkdir fname Parameters path is a pointer to the fully qualified 8 3 formatted null terminated filename path string Returns FRESULT return code Function Creates a directory Example FRESULT result result f mkdir path if result printf Directory creation error r n f mountdrv Prototype FRESULT f_mountdrv void Syntax result f mountdrv Parameters Returns FRESULT return code Function Initializes the media and the file system Example BYTE x FRESULT result initialize the file system x 10 maximum number of retries allowed do d waiting for the media to initialize result f mountdrv if result delay ms 200 delay and then retry while result amp amp x Copyright 2004 2012 Andrew
10. index Current index DIR File Status structure typedef struct _FILINFO DWORD fsize Size WORD fdate Date WORD Crime Time BYTE fattrib Attribute char fname 8 1 3 1 Name 8 3 format FILINFO File System Functions f_chmod Prototype FRESULT f chmod char path BYTE flags BYTE mask Syntax result f chmod path flags mask Parameters path is a pointer to the fully qualified 8 3 formatted null terminated filename path string flags attributes to be set mask attributes to be cleared Returns FRESULT return code Function Modify the file system attributes of a file Example FRESULT result set the READ ONLY attribute clear the HIDDEN and ARCHIVE attributes result f chmod path AM BDO AM HID AM ARC if result printf File attribute error r n f chmod attribute flags AM ARC Archive AM DIR Directory AM HID Hidden AM RDO Read only AM SYS System AM VOL Volume Label Copyright 2004 2012 Andrew Smallridge Page 8 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info pbrushelectronics com f_close ABN 50087977811 Prototype FRESULT f close FIL fp Syntax result f close amp fdata Parameters fp is a pointer to a FIL file control block handle of the file to be closed Returns FRESULT return code Function
11. 0 endif end of P Copyright 2004 2012 Andrew Smallridge This is the target processor This section contains the PROCESSOR SPECIFIC configuration include lt 24HJ256GP610 h gt specify the configuration fuse settings These configuration settings should be copied from the application fuse settings to be used by the final application fuses HS PR_PLL NOOSCIO NOWDT DEBUG NOWRTB NOPUT NOWRTSS fuses NOJTAG NOPROTECT NORSS NOWRT External oscillator frequency the clock frequency and PLL configuration are required to derive the baud rate configuration used by the sample application this information is NOT used by the file system driver define XTAL FREQ 8000000 OSC freq in Hz define PLLMODE 10 On chip PLL setting use delay clock 80000000 used by the CCS compiler to generate delays the following defines are used by the init_PIC in the sample application to configure the PIC define PLL multiplier define PLLFBD_Def 38 default value to be assigned to PLLFBR define CLKDIV_Def 0x0000 default value to be assigned to CLKDIV Setup Port IOs as digital define ADIPCFGL Def Oxffff default value to be assigned to ADIPCFGL define ADIPCFGH Def Oxffff default value to be assigned to ADIPCFGH Setup SPI bus pre and post scalars During the initial communications with the SD MC card the SPI clock rate should ideally be less than
12. Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 Brush Electronics SD MMC Embedded FAT File System User Manual Andrew Smallridge Contents Hire ereilente T M Customizing ee EE Understanding the plattom bh Die File System API function prototypes enne enne enne entere F RESULT File system function return codes esssesessseeseeeeeneeeeeenen nemen le Object structure P eg Directory Object Structure nore tereti n EE PAS E EIER REN TENE EN EUREN OE EE Re ERU RN File Status e E File System EUnctiOns decet ter eset eee RR RERO NEUE e ex eR NOE NEIN Eee ean Cay a OLX S GRE seeds eui EE f chmod attribute flags rre tr tenerent ne tno een meon ee etg een ge i i eias dm En f SEL EEN Beete Fssacec sages Dees Tee Eege See E RE leede ECH 10 uacuum 10 L re e O EE 10 diu site 11 openO Mode Blass ue other ies eE BEC Pe Vel ER oce bed er seve nena a cheese 11 ee Vb T E M 11 SE E BE 12 LS Te e E M BM 12 f rename RIDERE M 12 SE DN sehen 13 iR a ae D
13. Closes the file and flushes the file control block to the media Example FIL fdata FRESULT result result f close amp fdata if result printf File close error r n f get error mesg Prototype void f get error mesg FRESULT Mesg char destination Syntax f get error mesg FRESULT result char destination Parameters Mesg FRESULT error code destination character array to hold the returned NULL terminated message Returns Function Convert the FRESULT error code into an ASCII NULL terminated string Example FIL fdata char destination 64 FRESULT result f get error mesg result destination printf FRESULT error s r n destination f getfree Prototype FRESULT f getfree DWORD nclust Syntax result f getfree amp nclust Parameters nclust number of free clusters on the media Returns FRESULT return code Function Returns the number of free clusters Example DWORD nclust FRESULT result result f getfree amp nclust if result printf File system error r n Copyright 2004 2012 Andrew Smallridge Page 9 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 f Iseek Prototype FRESULT f lseek FIL fp DWORD ofs Syntax result f lseek amp fdata ofs Parameters fp is a pointer to a FIL file control
14. MIIEIEIMPBMdMMTMMPMgMUMMMMPMBPMgM MMMIMP AM PMMMgM UMELIMIMPMPMPMDPMVLlllllllllld d i setup_spi FALSE setup_spi2 FALSE setup_wdt WDT_OFF if defined ADIPCFG Def ADIPCFG ADIPCFG Def if defined ADIPCFGL Def ADIPCFGL DIPCFGL Def ll D if defined ADIPCFGH Def ADIPCFGH DIPCFGH Def ll D if defined PA DefData initialise port A PORTA PA DefData TRISA PA DefTRIS endif if defined PB DefData initialise port B PORTB PB DefData TRISB PB DefTRIS endif if defined PC DefData initialise port C PORTC PC DefData TRISC PC DefTRIS endif if defined PD DefData initialise port D PORTD PD DefData TRISD PD DefTRIS endif ifdef PE DefData PORTE PE DefData TRISE PE DefTRIS endif ifdef PF DefData PORTF PF DefData TRISF PF DefTRIS endif ifdef PG DefData PORTG PG DefData TRISG PG DefTRIS endif ifdef PH DefData PORTH PH DefData TRISH PH DefTRIS endif ifdef PJ DefData PORTJ PJ_DefData TRISJ PJ_DefTRIS endif Copyright 2004 2012 Andrew Smallridge Page 16 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 if defined PLLFBD_Def PLLFBD PLLFBD_Def endif if defined CLKDIV_Def CLKDIV CLKDIV_Def endif if defined UART1_TX_RP
15. PCD compiler CASE ignore case define USE FAT LITE configure the file system for FAT Lite operation include platform h include the platform specific I O configuration build stack 512 required for the CCS PCD compiler include DataTypes h typedefs used by the driver include stdio h include lt string h gt include ctype h include lt stdlib h gt include media io h low level SD MMC media driver include ff h file system driver define get fattime 0 null FAT time function Copyright 2004 2012 Andrew Smallridge Page 4 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 The following is an extract of the platform h file This extract covers the selection and configuration of the Microchip Explorer16 fitted with a PIC24HJ256GP610 PIM processor module Specify the target hardware platform being used define EXPLORER16_REV4 define CCS DSP ANALOG BOARD is this the dif defined EXPLORER16 in Lf target platform it is defined EXPLORER16_REV4 The Explorer 16 supports multiple PIC families and types Select the target microcontroller This is where processor specific configuration is performed This includes fuse setting clock divisors and SPI prescalar definitions dif defined __dsPIC30F3014__ include lt 30F3014 h gt elif defined PIC24HJ256GP61
16. R UART1 TX RPR 3 UART1 TX endif if defined UART1 RX RP UART1 RX RPR UART1 RX RP endif if defined UART2 TX RPR UART2 TX RPR 5 UART2 TX endif if defined UART2 RX RP UART2 RX RPR UART2 RX RP endif if defined SPI1 SDI RP SPI1_SDI_RPR SPI1_SDI_RP SPI1_SDO_RPR 7 SPI1 SDO SPI1_CLK_RPR 8 SPI1 CLK endif if defined SPI2_SDI_RP SPI2 SDI RPR SPI2 SDI RP SPI2 SDO RPR 10 SPI2 SDO SPI2 CLK RPR 11 SPI2 CLK endif BYTE write test char ptr VIMIMMMMMMMMMBGMMaMMMMMMMMMMMMBM e MPEPIPCG IEEE E MB GM dd dd dd dd dd BYTE write test char ptr Opens the file pointed to by ptr and writes to it If the file exists it is appended to If it does not exist it is created The file this then closed VMIMIMMMMMMMMMBMBMMMIMMMMMEIEEPPBGM AMMMMMMPMBM aMMEEEEEMPMLPMBGMMAllilldlil IPM YI M I T P g HIM FIL fsrc file structures FRESULT result FatFs function common result code WORD btw bw File R W count char mesg 64 open the file assumes target is the filename result f open amp fsrc ptr FA OPEN ALWAYS FA WRITE if result 1 printf write test FS ERROR on file open rin goto Ex write test no close Move to end of the file to append data result f lseek amp fsrc fsrc fsize write a short string to destination file strcpy mesg My String btw strlen mesg result f write amp fsrc mesg btw amp bw if result
17. R NOT READY Media not correctly initialized FR NO FILE File not found FR NO PATH Path not found FR INVALID NAME Invalid file or path name FR DENIED Access denied 7j R_DISK_FULL Disk full 1 R RW ERROR Read or write error zl R INCORRECT DISK CHANGE Media changed without reinitializing the file system 1 R WRITE PROTECTED Media is write protected 1 R NOT ENABLED Driver has not been initialized see f mountdrv 1 R NO FILESYSTEM Not FAT file system found on the media File Object structure typedef struct FIL DWORD fptr DWORD fsize DWORD org_clust DWORD curr_clust DWORD curr_sect BYTE dir_ptr endif BYTE flag BYTE sect_clust File R W pointer File size File start cluster Current cluster Current sector ifndef _FS_READONLY DWORD dir sect Sector containing the directory entry 7 Pointer to the directory entry in the window File status flags Left sectors in cluster ifndef USE_FAT_LITE BYTE buffer 5 endif FIL 12 File R W buffer Copyright 2004 2012 Andrew Smallridge Page 7 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 Directory Object structure typedef struct _DIROS DWORD sclust Start cluster DWORD clust Current cluster DWORD sect Current sector WORD
18. a different supply voltage to the SD MMC card level conversion logic is required between the microcontroller and the SD MMC cards There are multiple ways of implementing level conversion The following link gives an example using a pair of 74ACT125M transceivers to perform the level conversion http www ljcv net picnet1 picnetl dsO pdf Here is another example using a single transceiver combined with resistors http www brushelectronics com download BE_Reference_Design_PIC18F4620_ENC28J60 zip The SPI bus must be correctly terminated The SDI CS CD and WP lines all require pull up resistors in the range of 10K to 100K A common omission is the pull up resistor on the SDI line which can result in intermittent problems with the SD MMC initialisation sequence The SPI BUS frequency should in principle be less than or equal to 400KHz for the initialisation phase of the card and then can ramped up to the desired clock frequency typically 10MHz for subsequent operation Operating the SPI bus beyond 20MHz can result in read write errors The SPI bus is often a shared system resource An SPI bus conflict occurs when two or more chip select lines to peripherals sharing the SPI bus are asserted concurrently This can result in read write errors and can result in an SPI device isolating itself from the SPI bus SPI bus conflicts is a common first time implementation problem which typically occurs when bringing up a new system during development
19. aTypes h include lt stdio h gt include lt string h gt include lt ctype h gt include lt stdlib h gt include media io h include ff h define get_fattime 0 void file_list char ptr VMIMMMMMMIPPMPMPMWMPMPMMMIIIMMIMPGMHPPgMMMMLEMPANE T MMCMPL TTT TT void file_list char ptr Lists the contents of a text file JALLAI gMJUBHTMIMMEMEPP o AVI MMMMMMMMMPMPMMMEMEMEP B gMa ll ll l Pg H Pg M MgMIMMIM FIL farc FRESULT result FatFs function common result code char mesg 32 result f open amp fsrc ptr FA OPEN EXISTING FA READ display the contents of the file if result FR OK WORD i br do result f_read amp fsrc mesg sizeof mesg amp br for i 0 i lt br i putc mesg i while result FR OK amp amp br if result FR OK printf TYPE command ERROR r n f get error mesg result mesg printf FILE SYSTEM ERROR s r n mesg Close all files f close amp fsrc printf r n else f_get_error_mesg result mesg printf FILE SYSTEM ERROR s r n mesqg Copyright 2004 2012 Andrew Smallridge Page 15 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 void init_pic void TITITTTTTTTTL TTT TTT AAT TTT TTT TTT TTA TTT TTT TTT TTT lll LALELA void init_pic void Initialise the hardware defaults VMIMIMMMM
20. ation of long file names with legacy DOS file names in the same data structure In order to avoid infringing on Microsoft s patents in this area our software does not support long file names nor does it support formatting the media IBM holds patents pertaining to extended file system attributes Our code does not support extended file attributes It is up to the purchaser of this software to ascertain for themselves if licenses are required from any or all of these or other organizations for the integration of the media or our software into their products In the event licenses are required from any of these or other organizations it is up to the purchaser of this software to acquire at their own cost such licenses directly from the license holders Brush Electronics 2 Brush Court Canning Vale Western Australia 6155 Australia Tel 61 0 894676358 Email info brushelectronics com www www brushelectronics com Copyright 2004 2012 Andrew Smallridge Page 23 of 23
21. ctronics com index php page projects For a 3 3volt powered PIC system the minimum interface between an SD MMC card and the PIC is four I O lines which include the three SPI bus lines SCK SDO and SDJ the fourth line is the chip select Optionally two additional lines connect to the SD MMC socket for Card Detect CD and Write Protect WP For 5 volt powered PICs level translation is required between the 5 volt I O of the PIC and the 3 3 volt I O of the SD MMC Card Level translation for the PIC SDO SCK and CS outputs can be implemented with simple resistor voltage dividers Coming from DO of the SD MMC Card to the SDI input of the PIC is not so straight forward and requires a TTL buffer This is because the PIC in SPI mode has the SDI input configured as a Schmidt trigger input and the guaranteed logic high out of the SD MMC card is less than the guaranteed logic high level of the PIC When selecting a PIC for applications that write to an SD MMC card there are three main criteria there is sufficient RAM to support the driver mode there is sufficient program memory to accommodate the file system and the PIC supports an SPI bus SD MMC Cards must be written in 512 byte blocks This is also the sector size of the SD MMC card For example to append a byte to a file the sector must be read into a sector buffer the appropriate location modified in the sector buffer and the sector written back to the media If using the Brush Electronics file
22. dir Prototype FRESULT f readdir DIR scan FILINFO finfo Syntax result f readdir amp dir amp finfo Parameters scan is a pointer to a DIR object data structure finfo is a pointer to a FILINFO file information data structure Returns FRESULT return code Function Reads a directory item Repeated calls read successive items from the target directory Example DIR dir FILINFO finfo FRESULT result result f readdir amp dir amp finfo if result printf Directory read error r n else f rename Prototype FRESULT f rename char path old char path new Syntax result f open amp fdata fname modeflags Parameters path old is a pointer to the fully qualified 8 3 formatted null terminated filename path string of the existing file path new is a pointer to the fully qualified 8 3 formatted null terminated filename path string of the new file or directory Returns FRESULT return code Function Renames a file or directory Example FRESULT result result f rename path old path new if result printf File rename error r n Copyright 2004 2012 Andrew Smallridge Page 12 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 f_stat Prototype FRESULI f stat char pati FILINFO finfo Syntax result f stat path amp finfo
23. e as practical to the SD MMC card socket between the power supply pins of the socket that correspond to pins 3 and 4 of the media CAUTION although SD MMC card socket pin numbers often match the pin outs of the SD MMC media this is not always the case A 100nF power supply decoupling capacitor should be placed as close as practical to the SD MMC card socket between the power supply pins of the socket that correspond to pins 3 and 4 of the media SPI Bus The SPI bus interface between the Microcontroller and the SD MMC card is straight forward however it is the area that causes the most problems for first time implementers The interface includes the following mandatory signals Microcontroller SD MMC Card SDI input DO output SDO output DI input SCK output SCK input Copyright 2004 2012 Andrew Smallridge Page 21 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 CS output CS input The microcontrollers SPI hardware interface drives the SDI SDO and SCK control lines The CS line from the PIC is an I O pin configured as an output that provides the active low chip select input of the SD MMC card Two optional control lines from the SD MMC card to the microcontroller are the CD Card detect and WP Write protect switch outputs By convention these outputs are active low In dual voltage systems where the microcontroller runs at
24. for read write access f opendir Prototype FRESULT f opendir DIR scan char path Syntax result f opendir scan path Parameters scan is a pointer to a DIR object data structure path is a pointer to the directory string Returns FRESULT return code Function Opens an existing directory and populates the DIR structure Example DIR scan FRESULT result result f opendir amp scan path if result printf Directory open error r n Copyright 2004 2012 Andrew Smallridge Page 11 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 f_read Prototype FRESULT f read FIL fp void buff WORD btr WORD br Syntax result f read amp fdata buffer btr amp br Parameters fp is a pointer to a FIL file control block handle buff is a pointer to a buffer for the data being fetched btr is the number of Bytes To Read from the file into the buffer br is the actual number of bytes read A br value less than btr or a value of 0 can be used as an indication the end of file has been reached Returns FRESULT return code Function Reads the specified amount of data from a file into a buffer Example FIL fdata BYTE buff 64 BYTE br FRESULT result result f read amp fdata buff 64 amp br if result printf File read error r n f read
25. g Pg M ed l VH P P Pl P LEH GE Status LEDs VMIMIMMMMMIBMBGMMdagMggfll d P P TATA TTT TTT TAA TTT TTT TTT dd dd dd dd define LED Status LATAbits LATAO define LED Status TRIS TRISAbits TRISAO define LEDO LED_Status define LEDO_TRIS LED_Status_TRIS define LED1 LATAbits LATA1 define LED1 TRIS TRISAbits TRISA1 end of the HARDWARE SPECIFIC configuration Copyright 2004 2012 Andrew Smallridge Page 6 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia Tel 61 894676358 infoWbrushelectronics com ABN 50087977811 File System API function prototypes zj nj RESULT f close F RESULT f getfree RESULT f lseek F RESULT f mkdir c RESULT f mountdrv RESULT f open FI RESULT f opendir RESULT f read FI RESULT f readdir RESULT f rename RESULT f sync FI RESULT f unlink RESULT f write F r nr Hj z mn nm onm 4 RESULT f chmod char path BYTE value BYTE mask IL fp oid f get error mesg FRESULT Mesg char destination DWORD nclust IL fp DWORD ofs har path void L fp char path BYTE mode DIR scan char path L fp void buff WORD btr WORD br DIR scan FILINFO finfo char path old char path new RESULT f stat char path FILINFO finfo L fp char path IL fp void buff WORD btw WORD bw F RESULT File system function return codes FR OK File operation successful F
26. microcontrollers for associated Microchip C18 C30 and C32 compilers Variants are also available for the PIC18F PIC24 and dsPIC33 families using the CCS PCH and PCH compilers The file system driver supports the standard 8 3 filename convention used by DOS This code is an implementation of the ELM Generic File System The driver has been extensively debugged modified and enhanced with a DOS like CLI The file system driver comprises two driver subsystems the low level SD MMC media driver media 10 for performing low level read and write operations on the media and the file system driver ff for file based access to the media utilizing the FAT16 and or FAT32 file system The file system driver is provided with a sample application demonstrating the use of the file system driver for typical use case applications For the PIC18F series microcontrollers the driver is supplied as a proof of concept data logger application featuring a DOS like command line interface and incorporating a software real time clock optionally used by the file system The data logger application demonstrates the concept of caching the incoming raw data while SD MMC write operations of the data are in effect thereby decoupling data acquisition and logging This sample application is based on a production data logger implementation The marriage of the data logger capability and the DOS command line interface are intended to demonstrate to the developer how to use the va
27. n code Function Delete a file or an empty directory Example FRESULT result result f unlink path if result printf File delete error r n Copyright 2004 2012 Andrew Smallridge Page 13 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 f_write Prototype FRESULT f_write FIL fp void buff WORD btw WORD bw Syntax result f_write amp fdata buffer btw amp bw Parameters fp is a pointer to a FIL file control block handle buff is a pointer to a buffer for the data being written btw is the number of bytes to write to the file from the buffer bw is the actual number of bytes written A bw value less than btw is an indication of an error Returns FRESULT return code Function Writes the specified amount of data from the buffer to the file Example FIL fdata BYTE buff 64 BYTE bw FRESULT result result f_write amp fdata buff 64 amp bw if result printf File write error r n Copyright 2004 2012 Andrew Smallridge Page 14 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 Sample Application CASE define USE_FAT_LITE configure FAT Lite operation include lt platform h gt platform specific I O configuration build stack 512 include Dat
28. nable interrupts INT RDA enable interrupts INTR GLOBAL printf r nBasic Application compiled s s r n r n DATE TIME initialise the media and filesystem this will loop until the card is found do FS_Status f_mountdrv Delay_ms 200 while FS_Status build the file name strcpy target event log printf Performing write testing r n write_test target printf Displaying the contents of the file r n display the file contents file_list target printf That s all Folks r n for H End of main Copyright 2004 2012 Andrew Smallridge Page 19 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 SD MMC Card Integration and Troubleshooting SD MMC cards provide a low cost solution for data logging and storage applications for embedded systems SD MMC cards can be easily interfaced with a Microcontroller using an SPI interface and between one and three control lines The driver includes the low level SD MMC SPI drivers the low level disk I O drivers and file level operations The sample application where applicable demonstrates a DOS like user interface For information on how to implement the electrical interface between an SD MMC card and a PIC microcontroller refer to our hardware reference designs located on our projects page http www brushele
29. oftware http www brushelectronics com index php page software SDUTIL for bringing up new hardware and gain familiarity with the SD MMC integration before moving on to implementing a File System The troubleshooting guidelines are split into three different areas e Power Supply e SPIBUS e Media Power Supply SD MMC cards provide a relatively low power storage solution for embedded controllers however implementers often do not pay enough attention to the peak power requirements and inrush currents and may encounter situations where the embedded system resets when an SD MMC card is inserted or the SD MMC card may stop communicating with the microcontroller The power supply circuitry must be able to deal with the momentary inrush current when a card is inserted This inrush current can be of the order of 200mA Generally this is handled using large value power supply filter capacitors before and after the voltage regulator Ignoring the requirements of other components of an embedded system typical values for the power supply input capacitor are in the range of 220uF to 470uF Alternatively lower value capacitors that feature low ESR characteristics Effective Series Resistance could be used A typical filter capacitor after the regulator is a 47uF tantalum capacitor In situations where the SD MMC card socket is not located in close proximity to the regulator filter capacitors an additional 22uF tantalum capacitor should be placed as clos
30. rious functions The file system drivers support standard high performance and LITE configuration modes The standard mode implements a separate 512 byte read write buffer for the file system and for each file The LITE mode enabled via a define directive in the source code offers all the features of the standard mode at a lower level of performance The LITE mode is well suited to applications and microcontrollers that have limited RAM available The LITE mode shares a single 512 byte read write buffer between the file system and all open files and is capable of supporting hundreds of files open concurrently For high performance applications that require two or more files open concurrently the standard mode is recommended RAM requirements Standard Lite File System overhead 560 560 Per File Overhead 540 38 For the PIC24 dsPIC33 series microcontrollers the driver is supplied with a sample application featuring a DOS like command line interface Copyright 2004 2012 Andrew Smallridge Page 3 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 Customizing the driver The supplied sample application uses a series of define directives to customise the file system for the target hardware platform and the operational mode of the driver This configuration is contained in the platform h file used by the sample application The platform h file
31. system driver in standard mode high performance a 512 byte sector buffer is required for file system management and one 512 byte buffer is required for each open file This means a PIC with 1500 bytes of RAM such as the PIC18F452 when using the standard file system configuration mode can realistically have only a single file open at a time The PIC18F4620 which is pin compatible with the PIC18F452 has 3900 bytes of ram available and therefore can have multiple files open simultaneously When the file system driver is configured for LITE mode a single sector buffer is shared by the file system and all open files This means a PIC with 1500 bytes of memory such as the PIC18F452 can have multiple files open simultaneously The LITE configuration mode is not recommended for high performance applications that require access to multiple open files While the electrical interface is relatively straight forward successfully implementing a solution can be time consuming for the initial implementation This section looks at some of the common pitfalls encountered Copyright 2004 2012 Andrew Smallridge Page 20 of 23 Brush Electronics 2 Brush Court Canning Vale WA 6155 Australia e Tel 61 894676358 e info brushelectronics com ABN 50087977811 For developers that are implementing an SD MMC interface for the first time on their own hardware platform Brush Electronics STRONGLY recommends using our SD MMC Card driver and test utilities s

Download Pdf Manuals

image

Related Search

Related Contents

GLOCK INSTRUCTION FOR USE  la cuisine de vos rêves  Intel SR1500        User's Manual    Catalogue - Les Éditeurs réunis  User Manual  

Copyright © All rights reserved.
Failed to retrieve file