![]() |
Forum Index : Microcontroller and PC projects : MM2(5.2): load and save programs to SD
Author | Message | ||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10066 |
With the new facilities in 5.2 I have been able to further improve the SDcard handling for the MX170 Micromite. It is now possible to load programs from SD card and save them to SD card using the same syntax as the MM+. ![]() LOAD "program.bas" SAVE "newprog.bas" There is only one limitation in that the file to be "saved" must exist before using the SAVE command which will then overwrite it. The attached ZIP file contains a suitable blank file "BLANK.BAS" This can be renamed and written to the SDcard on a PC with as many different filenames as you require for the Micromite. I have also further simplified and improved the general MM2 SD card handling by using the new links to the "Error" routine in the MM firmware. This means that all the SD card routines are now subroutines rather than functions and errors will be reported in the same way as any other MMBasic error. The ZIP file includes two variants of the CFunction, one works with the normal SPI pins as per the manual but should be fully compatible with TFT panels and touch. The second uses the second SPI channel on the Micromite to connect to the SD card. Pins for the second channel are: 28-pin: SPI2OUT-24, SPI2IN-6, SPI2CLK-26 44-pin: SPI2OUT-11, SPI2IN-23, SPI2CLK-15 The CFunction must be stored in the library and you should amend mm.startup to define the correct pin for the SD card chip select. The Cfunction uses just 10K of program space. The calls in this new version have been simplified as follows: SDCard fileopen,filename$ This opens the file defined by the string literal or variable "filename$". It will automatically initialise and mount the disk before opening the file. SDcard readfile,buff$ or buff(),start_byte,#_of_bytes_requested,#_of_bytes_read This reads a specified number of bytes into a Basic variable "buff" defines where the bytes read will be put. For 255 bytes or less this should be a string variable e.g. buff$. For greater than 255 bytes it should be an integer array dimensioned such that the number of bytes requested divided by 8 should be less than or equal to the array size e.g. DIM INTEGER buff(127) can receive 1024 bytes "start_byte" defines the number of bytes from the start of the file to start the read. Use zero to position at the beginning of the file. Use any negative number to read from the current file location "#_of_bytes_requested" defines the number of bytes you wish to read. "#_of_bytes_read" returns the number of bytes actually read. This should be the same as the number requested unless the end-of-file has been reached so it provides an easy test for end-of-file SDcard writefile,buff$ or buff(),start_byte,#_of_bytes_to_be_written,#_of_bytes_written This writes a specified number of bytes from a Basic variable "buff" defines where the bytes to be written are stored. For 255 bytes or less this should be a string variable e.g. buff$. For greater than 255 bytes it should be an integer array dimensioned such that the number of bytes to be written divided by 8 should be less than or equal to the array size e.g. DIM INTEGER buff(127) can store 1024 bytes for output to the SDcard. "start_byte" defines the number of bytes from the start of the file to start the write. Use zero to position at the beginning of the file. Use any negative number to write from the current file location "#_of_bytes_to_be_written" defines the number of bytes you wish to write. "#_of_bytes_written" returns the number of bytes actually written. This should be the same as the number requested unless the end-of-file has been reached so it provides an easy test for attempting to write past the end-of-file SDCard opendir,directoryname$ This opens the directory defined by the string literal or variable "directoryname$". It will automatically initialise and mount the disk before opening the directory. SDCard readdir,filename$ This reads the next filename from the open directory and returns it in the string variable "filename$". A blank filename indicates that the last valid file or directory name has been read. Sub-directories are indicated by the prefix "<DIR>" The code has been tested on both 28 and 44-pin Micromites with both FAT and FAT32 formatted SD cards. A test program for the SD card routines is included in the ZIP file together with the C source of the CFunction. For any aspiring Cfunction writer this will show how to use some of the new facilities in 5.2. UDATED at 11:03GMT 14/5/16 2016-05-14_110302_sdcard.zip |
||||
twofingers Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1526 |
WOW ![]() Thank you! ![]() ![]() Regards Michael causality ≠ correlation ≠ coincidence |
||||
cosmic frog Senior Member ![]() Joined: 09/02/2012 Location: United KingdomPosts: 300 |
Fantastic! This is very handy. Thanks. Dave. |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
I just wanted to say thank you so much Peter. This just saved my bacon My computer totally locked up and I lost all this mornings work. I was able to restart the computer and save the program loaded in the MM Thank you ![]() |
||||
sagt3k![]() Guru ![]() Joined: 01/02/2015 Location: ItalyPosts: 313 |
Hi matherp Very good news. Because it is not possible to think a version with usb and integrate SD capability in the firmware and use Pic32mx270 or new? Antonio |
||||
g8kig Newbie ![]() Joined: 09/04/2020 Location: United KingdomPosts: 1 |
I know this is an old thread but there is one more restriction. According to the underlying Petit FAT File System Module documentation, the file written cannnot be extended in size. I couldn't figure out why writes weren't working on a zero length file. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |