ATmega168 Raw SD/MMC library Code
Brought to you by:
dustin-robotics
/*=============================================================================* * ATmega168_Slash - This project was developed to use an ATmega168 in a * * Arduino board to control a remote control car. It was developed to become * * more familiar with the ATmega168. * * * * Specific interfaces covered in this example code: * * SPI interface with microSD card to log ATD values, port values * * * * Copyright (C) 2010 Dustin Reynolds * *=============================================================================* * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the Free * * Software Foundation; either version 3 of the License, or (at your option) * * any later version. * * * * This program is distributed in the hope that it will be useful, but WITHOUT * * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * * more details. * * * * You should have received a copy of the GNU General Public License along * * with this program; if not, see <http://www.gnu.org/licenses/>. * *-----------------------------------------------------------------------------* * Dieses Programm ist freie Software. Sie k�nnen es unter den Bedingungen der * * GNU General Public License, wie von der Free Software Foundation * * ver�ffentlicht, weitergeben und/oder modifizieren, entweder gem�� Version 3 * * der Lizenz oder (nach Ihrer Option) jeder sp�teren Version. * * * * Die Ver�ffentlichung dieses Programms erfolgt in der Hoffnung, da� es Ihnen * * von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die * * implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT F�R EINEN * * BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License. * * * * Sie sollten ein Exemplar der GNU General Public License zusammen mit * * diesem Programm erhalten haben. Falls nicht, * * siehe <http://www.gnu.org/licenses/>. * *=============================================================================* * * * This file contains source code, programming environment, hardware * * descriptions, and schematics so that the entire project can be recreated. * * Hardware modifications, when performed, will be documented so that those * * same modifications could be performed by anyone with access to this file. * * * * * * File structure: * * /binaries - contains Makefile. Download project to ATmega168 with * * 'make program' * * /include - contains header files * * /source - contains all c function files * * * * Programming Environment: * * Ubuntu Linux 10.04 * * avr-gcc version 4.3.4 * * avrdude version 5.10 * * avr-objcopy version 2.20 of GNU objcopy * * * * Programmer: * * AVR Pocket Programmer: * * http://www.sparkfun.com/commerce/product_info.php?products_id=9231 * * Setup for programmer: Needed to use a USB hub to use programmer, since * * by default the programmer draws more current than the maximum USB * * current of 500mA, so to prevent this from happening, I use a USB hub. * * * * Serial Hyperterminal: * * Excellent viewer here: * * http://www.der-hammer.info/terminal/index.htm * * Comments: It lets you view hex, dec, binary, everything.. for free! * * Also lets you save the output to a file, which will be great for this * * code since everything logged on the SD card has to be transfered over * * serial to be viewed on the computer. This was used in a windows VM * * using VirtualBox, using two XBee's to establish the communication * * channel. * * * *=============================================================================*/ Project notes: 20100531 - Currently working on getting microSD read and write support, for raw, not FAT32. Tried to implement FAT32 but it was too big for the ATmega168 so I cut it out. I had trouble connecting to the microSD card, turns out I needed to use the SS pin. It would have been good to use that pin for 16 bit PWM, so I will have to settle for 8. After I found out about SS, I had trouble sending char's to the micro processor. I troubleshooted by connecting TX to RX so it would be talking with itself, and it received it just fine, so it was the bluetooth/xbee adapter that had problems. When I run the program, it is able to detect the card, but when I try to erase, read, or write it always is successful, failed, and successful regardless if card is present. Good luck!! PS. I also examined the output from size.txt and found that I'm running out of SRAM!!! To calculate FLASH and SRAM: FLASH: text + data SRAM: data + bss The ATmega168 has 16K Flash, 512 Bytes EEPROM, and 1K SRAM So the reason that it is not displaying the long text sections is that it has ran out of SRAM. 20100605 - MicroSD read and write is successful now! Only thing I really changed was a while loop, line 291-300 in SD_card.c, to a do-while loop. What seemed to work is to look deeper and deeper into a problem file until the location of where the error occurs becomes apparent. What to do then is still a mystery. In this case, changing a while loop to a do-while did the trick. Manufacturer data after downloading /home/dustin/Documents/Projects/Arduino/SDcard/sd-reader_source_20100110 .manuf: 0x02 .oem: TM .prod: SD01G .rev: 32 .serial: 0x918a00b8 .date: 5/8 .size: 947MB .copy: 0 .wr.pr.: 0/0 .format: 0 .free: 986398720/986726400 File is downloaded into atmega168 by 'make' followed by 'make flash' Able to read and write succesfully. When erase is performed, card returns error the first time that read/write is attempted. The second time is successful. 20100611 - Fixed erase feature by having it wait until card is idle before leaving erase function. ERROR Read, write, and erase timing is around 770 ms to complete. ERROR!!! Previously stated that it too 770 ms, but I was doing this: USART_write_dec(unsigned char!). Shame on me! Read failed at 2000000,2222222,3333333,... block, but erase was successful. Theroetically, should be only able to write, read, erase up to a little above 1900000. Set max to 1900000. Read, write, and erase can take as little as 2ms. Sometimes can take about 100ms If read error, noticed it took 336 ms. Calculating how long it will take to read 500000 blocks, 0.002 ms * 500000 / 60 sec = 1.66667 seconds If SD card failure occurs, continue but report error to USART just in case. After getting ADC working, added option to graphical gui to run LogDisable, but after adding that option, size increased to data 374 .bss 566, so SRAM was 940. This was not enough for normal operations and it wasn't able to perform the interrupt for logging. Will learn how to call strings from flash rather than ram, https://www.mainframe.cx/~ckuethe/avr-c-tutorial/#adc-in. 20100613 - Read works perfectly now, printing log works great as well. Printing strings from memory has reduced SRAM to only variables. Great Work!!! Looking Forward to getting 1us clock working, and maybe using any pin as PWM :-)