jsluke
Starting Member
- Total Posts : 69
- Reward points : 0
- Joined: 2010/11/05 09:24:51
- Location: 0
- Status: offline
SPI and MMC
I'm using MPLab 8.35 with Microchip C18 compiler. I'm attempting to write a File to an SD Card in a form that can be read directly into a PC using a Card Reader. I have a code example from the book "Advanced PIC Microcontroller Projects in C" that uses the functions: Mmc_Init Mmc_Fat_Init Mmc_Fat_Assign Mmc_Fat_Write I can't find these functions in any of the header files. Any ideas where I find the appropriate libraries? Thanks. James.
|
Jim Nickerson
User 452
- Total Posts : 6912
- Reward points : 0
- Joined: 2003/11/07 12:35:10
- Location: San Diego, CA
- Status: offline
Re:SPI and MMC
2011/01/07 18:38:44
(permalink)
|
jsluke
Starting Member
- Total Posts : 69
- Reward points : 0
- Joined: 2010/11/05 09:24:51
- Location: 0
- Status: offline
Re:SPI and MMC
2011/01/11 11:28:54
(permalink)
Thanks Jim I've worked through the document including taking the demo project (MDD File System-SD Card) and trying to modify it to work with the PIC18F452. I think I've made great progress in finally getting it to compile and then program my chip. Sadly, it still doesn't work and I'm not sure where to start wading through FSIO.c and SD-SPI.c. I'm very disappointed with the complete lack of a simple example on how to do this. I've searched the internet extensively and all I can find is other people who are having the same issues that I am. This should be a relatively simple thing to achieve - I was originally following an example in "Advanced PIC Microcontroller Projects in C" however I have now discovered that these projects were implemented using mikroC. With MPLAB it just seems impossible. If anyone has managed to put together a straight forward, replicable, understandable example of logging data from a PIC18F452 to an SD Card, I'd be forever grateful. Thanks. James.
|
rmteo
Super Member
- Total Posts : 1007
- Reward points : 0
- Joined: 2007/05/02 20:33:25
- Location: Colorado, USA
- Status: offline
Re:SPI and MMC
2011/01/11 12:08:00
(permalink)
Why don't you use mikroC then?
Why pay for overpriced toys when you can have professional grade tools for FREE!!! 
|
jsluke
Starting Member
- Total Posts : 69
- Reward points : 0
- Joined: 2010/11/05 09:24:51
- Location: 0
- Status: offline
Re:SPI and MMC
2011/01/11 13:22:57
(permalink)
Because it's $249 and sadly I can't afford it.
|
Jim Nickerson
User 452
- Total Posts : 6912
- Reward points : 0
- Joined: 2003/11/07 12:35:10
- Location: San Diego, CA
- Status: offline
Re:SPI and MMC
2011/01/11 14:21:40
(permalink)
I wonder if the 18F452 has enough resources available. From trhe Readme.pdf in the C:\Microchip Solutions v2010-08-04\Microchip\MDD File System\Documentation dir Total memory usage* C18: Unoptimized Program memory- 72702 bytes Unoptimized Data memory- 1976 bytes Optimized Program memory- 38134 bytes Optimized Data Memory- 1976 bytes
|
jsluke
Starting Member
- Total Posts : 69
- Reward points : 0
- Joined: 2010/11/05 09:24:51
- Location: 0
- Status: offline
Re:SPI and MMC
2011/01/11 14:45:42
(permalink)
I'm 50:50 on this. Clearly, using the Microchip Application Libraries is pushing the limits. The only way I could build the program was to comment out some functionality. I suspect there is a lot more functionality that could be removed in that I only want to write a single file to the SD Card in a format that can be read from a PC. The complexity of the libraries make it difficult to do this. Conversely, based on the example in the reference book, the mikroC examples do appear to be quite lightweight. I'm relatively new to PIC development and I've made a few mistakes in selecting which PIC and development tools to use. However, I've started to make good progress with the 18F452 and am really pleased with the project is going. I'd just like to nail this issue of writing a file system to the SD Card.
|
Klevang
New Member
- Total Posts : 4
- Reward points : 0
- Joined: 2011/01/11 13:20:50
- Location: 0
- Status: offline
Re:SPI and MMC
2011/01/11 18:46:57
(permalink)
As mentioned in the other thread I am having the exact same issue as you jsluke. Just with a different PIC. I have found a book that containes an almost tutorial like section on how to setup and use the Microchip Application Libraries. -SD Card Projects Using the PIC Microcontroller - D. Ibrahim (Newnes, 2010) I'm in the process of going through the steps. I'll let you know if it goes well.
|
jsluke
Starting Member
- Total Posts : 69
- Reward points : 0
- Joined: 2010/11/05 09:24:51
- Location: 0
- Status: offline
Re:SPI and MMC
2011/01/12 02:39:59
(permalink)
Please do - I saw that book myself but wasn't sure if it would help. The issue seems to be in configuring the supplied demo to run with a different hardware profile. Ideally, the libraries would either cover all hardware out of the box (i.e. no bespoke configuration) or come supplied with demos that covered all hardware. I was considering moving to the hardware covered in the demo, however all the chips seem to be surface mount technology and that's not something I can prototype with at this stage.
|
Jim Nickerson
User 452
- Total Posts : 6912
- Reward points : 0
- Joined: 2003/11/07 12:35:10
- Location: San Diego, CA
- Status: offline
Re:SPI and MMC
2011/01/12 08:52:48
(permalink)
I am wondering what the minimum features you think you need to write a file to the SD card that can be read in a pc? Do you need to add files to an SD card that already has other files on it? Do you need to over write an existing file ? Do you need to add data to the end of an existing file when the device experiences a power cycle? Do you need to handle an SD card where the pc has manipulated the directory structure and the data, deleting files etc?
|
Klevang
New Member
- Total Posts : 4
- Reward points : 0
- Joined: 2011/01/11 13:20:50
- Location: 0
- Status: offline
Re:SPI and MMC
2011/01/12 09:31:47
(permalink)
I've been throught the book now. With some tweaks I got it configured to my hardware. Sadly I ended up getting the exact same error as when using the demonstraition projects that come with the Microchip Solutions library. So no luck here.
|
jsluke
Starting Member
- Total Posts : 69
- Reward points : 0
- Joined: 2010/11/05 09:24:51
- Location: 0
- Status: offline
Re:SPI and MMC
2011/01/12 09:35:56
(permalink)
The minimum features are really simple => I need to be able to create and write one file to the SD Card. I need to be able to open that file on the PC. I need to be able to delete that file from the PC so that when the SD Card is returned to the PIC a new file is created. Basically, I'm looking for the simplest way to transfer a data log from the application to a PC using an SD Card (the hardware design inhibits the use of USB for the data transfer). I have considered working through the Application Libraries and stripping out the unnecessary code, however the libraries are quite complex so it isn't a quick job. I've also thought of building my own library from scratch. Both approaches will obviously take time and delay my product development. I've also looked at switching to one of the PICs used in the supplied demos (e.g. PIC18F46J50), however the components are only supplied in surface mount packages which makes it difficult (impossible) for me to prototype. James.
|
Jim Nickerson
User 452
- Total Posts : 6912
- Reward points : 0
- Joined: 2003/11/07 12:35:10
- Location: San Diego, CA
- Status: offline
Re:SPI and MMC
2011/01/12 10:46:23
(permalink)
jsluke The minimum features are really simple => I need to be able to create and write one file to the SD Card. I need to be able to open that file on the PC. I need to be able to delete that file from the PC so that when the SD Card is returned to the PIC a new file is created. Basically, I'm looking for the simplest way to transfer a data log from the application to a PC using an SD Card (the hardware design inhibits the use of USB for the data transfer). I have considered working through the Application Libraries and stripping out the unnecessary code, however the libraries are quite complex so it isn't a quick job. I've also thought of building my own library from scratch. Both approaches will obviously take time and delay my product development. I've also looked at switching to one of the PICs used in the supplied demos (e.g. PIC18F46J50), however the components are only supplied in surface mount packages which makes it difficult (impossible) for me to prototype. James. If you are willing to use the "first" ( and only ) directory entry on the SD card I think you can do this without the full library. Are you willing to open and process the file on the pc with purpose built software ? If so the location of the directory entry and the data storage could always be in the "same" locations making it un necessary to decode this data from the fat.
|
jsluke
Starting Member
- Total Posts : 69
- Reward points : 0
- Joined: 2010/11/05 09:24:51
- Location: 0
- Status: offline
Re:SPI and MMC
2011/01/12 10:54:35
(permalink)
What was the error you got? I didn't get any errors - it just didn't work. I stepped through the code with the MPLAB debugger and the problem is somewhere in a call to either MDD_InitIO or DiskMount. Not sure which - trying to lock that down now. Testing the pins on the SD Card Reader there doesn't seem to be any activity so I don't believe the Card is being initialised properly. That suggests to me that the problem is either wiring (I keep checking it but can't find anything wrong) or hardware configuration. James.
|
jsluke
Starting Member
- Total Posts : 69
- Reward points : 0
- Joined: 2010/11/05 09:24:51
- Location: 0
- Status: offline
Re:SPI and MMC
2011/01/12 11:05:13
(permalink)
Yes - no issue with using purpose built software on the PC. In fact, I've been hunting around trying to find examples of how to read raw data from an SD Card using C, C++ or Java. I'm looking for the simplest, in terms of implementation time, possible way of transferring the log data onto the PC. I appreciate your help with this. James.
|
rmteo
Super Member
- Total Posts : 1007
- Reward points : 0
- Joined: 2007/05/02 20:33:25
- Location: Colorado, USA
- Status: offline
Re:SPI and MMC
2011/01/12 11:15:31
(permalink)
It's been a while since I last used mikroC (or PIC's for that matter) but I do recall that using their FAT16 libraries, it required about 15-16KBytes of FLASH and around 1.5KBytes of RAM on a PIC18F4620. Perhaps, you could go to the mikroE forums and ask someone to compile an example program for you to try out.
Why pay for overpriced toys when you can have professional grade tools for FREE!!! 
|
Jim Nickerson
User 452
- Total Posts : 6912
- Reward points : 0
- Joined: 2003/11/07 12:35:10
- Location: San Diego, CA
- Status: offline
Re:SPI and MMC
2011/01/12 11:27:26
(permalink)
jsluke Yes - no issue with using purpose built software on the PC. In fact, I've been hunting around trying to find examples of how to read raw data from an SD Card using C, C++ or Java. I'm looking for the simplest, in terms of implementation time, possible way of transferring the log data onto the PC. I appreciate your help with this. James. If you create a sample data file of the maximum length on the SD card using the pc you will have an example of the data layout and where to place the data and the dir fat entry with the linked list. If you use the first sector of the file to store a pointer to where the next data should go you could On a power cycle read the first sector ( it is at a fixed location in this use ), read in the next location to store to. Collect data till you have 512 bytes ( a sector as I recall ). Write the 512 bytes, update the next pointer, update the first sector. Have a look in the library at the low level routines that write a sector, they only need the data in a "512" byte ( as I recall ) chunk of ram and a starting location to write to.
|
jsluke
Starting Member
- Total Posts : 69
- Reward points : 0
- Joined: 2010/11/05 09:24:51
- Location: 0
- Status: offline
Re:SPI and MMC
2011/01/12 17:31:27
(permalink)
Not sure if this is going to help much but I thought it best to post just in case it reveals a possible cause of the problem. My code seems to get stuck in a loop in the function MDD_SDSPI_MediaInitialise. I've been stepping through the code in the debugger and it appears (I'm not 100% confident I'm afraid) to get stuck in a loop whilst executing C:\MCC18\src\traditional\math\fxd3232u.c. The debugger only shows assembly for this code - the endless loop is between address 6644 and 6676 => --- C:\MCC18\src\traditional\math\fxd3232u.c --------------------------------------------------- 6632 6A26 CLRF 0x26, ACCESS 6634 6A25 CLRF 0x25, ACCESS 6636 6A24 CLRF 0x24, ACCESS 6638 6A23 CLRF 0x23, ACCESS 663A 0E20 MOVLW 0x20 663C 6EE7 MOVWF 0xfe7, ACCESS 663E 90D8 BCF 0xfd8, 0, ACCESS 6640 3627 RLCF 0x27, F, ACCESS 6642 3628 RLCF 0x28, F, ACCESS 6644 3629 RLCF 0x29, F, ACCESS 6646 362A RLCF 0x2a, F, ACCESS 6648 3623 RLCF 0x23, F, ACCESS 664A 3624 RLCF 0x24, F, ACCESS 664C 3625 RLCF 0x25, F, ACCESS 664E 3626 RLCF 0x26, F, ACCESS 6650 502C MOVF 0x2c, W, ACCESS 6652 5C23 SUBWF 0x23, W, ACCESS 6654 502D MOVF 0x2d, W, ACCESS 6656 5824 SUBWFB 0x24, W, ACCESS 6658 502E MOVF 0x2e, W, ACCESS 665A 5825 SUBWFB 0x25, W, ACCESS 665C 502F MOVF 0x2f, W, ACCESS 665E 5826 SUBWFB 0x26, W, ACCESS 6660 E309 BNC 0x6674 6662 502C MOVF 0x2c, W, ACCESS 6664 5E23 SUBWF 0x23, F, ACCESS 6666 502D MOVF 0x2d, W, ACCESS 6668 5A24 SUBWFB 0x24, F, ACCESS 666A 502E MOVF 0x2e, W, ACCESS 666C 5A25 SUBWFB 0x25, F, ACCESS 666E 502F MOVF 0x2f, W, ACCESS 6670 5A26 SUBWFB 0x26, F, ACCESS 6672 2A27 INCF 0x27, F, ACCESS 6674 2EE7 DECFSZ 0xfe7, F, ACCESS 6676 D7E4 BRA 0x6640 6678 0012 RETURN 0 I know that posting this is a long shot but I'm so desperate I'll try just about anything :-) Thanks. James.
|
jsluke
Starting Member
- Total Posts : 69
- Reward points : 0
- Joined: 2010/11/05 09:24:51
- Location: 0
- Status: offline
Re:SPI and MMC
2011/01/16 17:41:28
(permalink)
Well ......... I've just spent another full weekend on this and am absolutely no further forwards! I even switched chips and started using the PIC18F26J50 as this is almost identical to the PIC18F46J50 used in the PIC18F starter kit. I would actually have gone for the PIC18F46J50 but it's only available in surface mount. I used the USB Device - Mass Storage - SD Card reader project as a basis to ensure that I had my project structure correct. I modified the project to use the PIC18F26J50 and after the usual fun and games managed to compile and program my device. Once again however the application just sits in an endless loop whilst initialising the media. Every internet search just returns a list of people battling to achieve the same thing - write data from a PIC to an SD Card. I really don't understand why Microchip can't provide a simple example. James.
|
Jim Nickerson
User 452
- Total Posts : 6912
- Reward points : 0
- Joined: 2003/11/07 12:35:10
- Location: San Diego, CA
- Status: offline
Re:SPI and MMC
2011/01/16 20:02:29
(permalink)
|