Changeset - e70ab4bd3445
[Not reviewed]
default
0 3 0
mkanning@CL-SEC241-10.cedarville.edu - 13 years ago 2012-11-08 11:54:46
mkanning@CL-SEC241-10.cedarville.edu
Finished first pass on SD Card communication
3 files changed with 33 insertions and 51 deletions:
0 comments (0 inline, 0 general)
master/master/lib/logger.c
Show inline comments
 
@@ -16,35 +16,6 @@
 
#include "logger.h"
 
 
/* 
 
	//console prompts and responses
 
 * I implemented an example application providing a simple command prompt which is accessible
 
 * via the UART at 9600 Baud. With commands similiar to the Unix shell you can browse different
 
 * directories, read and write files, create new ones and delete them again. Not all commands are
 
 * available in all software configurations.
 
 * - <tt>cat \<file\></tt>\n
 
 *   Writes a hexdump of \<file\> to the terminal.
 
 * - <tt>cd \<directory\></tt>\n
 
 *   Changes current working directory to \<directory\>.
 
 * - <tt>disk</tt>\n
 
 *   Shows card manufacturer, status, filesystem capacity and free storage space.
 
 * - <tt>init</tt>\n
 
 *   Reinitializes and reopens the memory card.
 
 * - <tt>ls</tt>\n
 
 *   Shows the content of the current directory.
 
 * - <tt>mkdir \<directory\></tt>\n
 
 *   Creates a directory called \<directory\>.
 
 * - <tt>mv \<file\> \<file_new\></tt>\n
 
 *   Renames \<file\> to \<file_new\>.
 
 * - <tt>rm \<file\></tt>\n
 
 *   Deletes \<file\>.
 
 * - <tt>sync</tt>\n
 
 *   Ensures all buffered data is written to the card.
 
 * - <tt>touch \<file\></tt>\n
 
 *   Creates \<file\>.
 
 * - <tt>write \<file\> \<offset\></tt>\n
 
 *   Writes text to \<file\>, starting from \<offset\>. The text is read
 
 *   from the UART, line by line. Finish with an empty line.
 
 
 
	//config edits
 
  * By changing the MCU* variables in the Makefile, you can use other Atmel
 
  * microcontrollers or different clock speeds. You might also want to change
 
@@ -169,7 +140,7 @@ void logger_setup()
 
				/* write text to file !! */
 
				if(fat_write_file(fd, (uint8_t*) buffer, data_len) != data_len)
 
				{
 
					uart_puts_p(PSTR("error writing to file\n"));
 
					//uart_puts_p(PSTR("error writing to file\n"));
 
					break;
 
				}
 
			}
master/master/lib/sd/sd_raw.c
Show inline comments
 
@@ -11,6 +11,7 @@
 
#include <string.h>
 
#include <avr/io.h>
 
#include "sd_raw.h"
 
#include "sd_raw_config.h"
 

	
 
/**
 
 * \addtogroup sd_raw MMC/SD/SDHC card raw access
 
@@ -184,15 +185,15 @@ uint8_t sd_raw_init()
 
    unselect_card();
 

	
 
    /* initialize SPI with lowest frequency; max. 400kHz during identification mode of card */
 
    SPCR = (0 << SPIE) | /* SPI Interrupt Enable */
 
           (1 << SPE)  | /* SPI Enable */
 
           (0 << DORD) | /* Data Order: MSB first */
 
           (1 << MSTR) | /* Master mode */
 
           (0 << CPOL) | /* Clock Polarity: SCK low when idle */
 
           (0 << CPHA) | /* Clock Phase: sample on rising SCK edge */
 
           (1 << SPR1) | /* Clock Frequency: f_OSC / 128 */
 
           (1 << SPR0);
 
    SPSR &= ~(1 << SPI2X); /* No doubled clock frequency */
 
    SPCR0 = (0 << SPIE0) | /* SPI Interrupt Enable */
 
           (1 << SPE0)  | /* SPI Enable */
 
           (0 << DORD0) | /* Data Order: MSB first */
 
           (1 << MSTR0) | /* Master mode */
 
           (0 << CPOL0) | /* Clock Polarity: SCK low when idle */
 
           (0 << CPHA0) | /* Clock Phase: sample on rising SCK edge */
 
           (1 << SPR10) | /* Clock Frequency: f_OSC / 128 */
 
           (1 << SPR00);
 
    SPSR0 &= ~(1 << SPI2X0); /* No doubled clock frequency */
 

	
 
    /* initialization procedure */
 
    sd_raw_card_type = 0;
 
@@ -314,8 +315,8 @@ uint8_t sd_raw_init()
 
    unselect_card();
 

	
 
    /* switch to highest SPI frequency possible */
 
    SPCR &= ~((1 << SPR1) | (1 << SPR0)); /* Clock Frequency: f_OSC / 4 */
 
    SPSR |= (1 << SPI2X); /* Doubled Clock Frequency: f_OSC / 2 */
 
    SPCR0 &= ~((1 << SPR10) | (1 << SPR00)); /* Clock Frequency: f_OSC / 4 */
 
    SPSR0 |= (1 << SPI2X0); /* Doubled Clock Frequency: f_OSC / 2 */
 

	
 
#if !SD_RAW_SAVE_RAM
 
    /* the first block is likely to be accessed first, so precache it here */
 
@@ -361,10 +362,10 @@ uint8_t sd_raw_locked()
 
 */
 
void sd_raw_send_byte(uint8_t b)
 
{
 
    SPDR = b;
 
    SPDR0 = b;
 
    /* wait for byte to be shifted out */
 
    while(!(SPSR & (1 << SPIF)));
 
    SPSR &= ~(1 << SPIF);
 
    while(!(SPSR0 & (1 << SPIF0)));
 
    SPSR0 &= ~(1 << SPIF0);
 
}
 

	
 
/**
 
@@ -377,11 +378,11 @@ void sd_raw_send_byte(uint8_t b)
 
uint8_t sd_raw_rec_byte()
 
{
 
    /* send dummy data for receiving some */
 
    SPDR = 0xff;
 
    while(!(SPSR & (1 << SPIF)));
 
    SPSR &= ~(1 << SPIF);
 
    SPDR0 = 0xff;
 
    while(!(SPSR0 & (1 << SPIF0)));
 
    SPSR0 &= ~(1 << SPIF0);
 

	
 
    return SPDR;
 
    return SPDR0;
 
}
 

	
 
/**
master/master/lib/sd/sd_raw_config.h
Show inline comments
 
@@ -106,12 +106,22 @@ extern "C"
 

	
 
    #define select_card() PORTB &= ~(1 << PORTB0)
 
    #define unselect_card() PORTB |= (1 << PORTB0)
 
#elif defined(__AVR_ATmega164P__) || \
 
	  defined(__AVR_ATmega324P__) || \
 
	  defined(__AVR_ATmega664P__)
 
    #define configure_pin_mosi() DDRB |= (1 << DDRB5) //PB5
 
    #define configure_pin_sck() DDRB |= (1 << DDRB7) //PB7
 
    #define configure_pin_ss() DDRB |= (1 << DDRB0) //PB0 - custom pin
 
    #define configure_pin_miso() DDRB &= ~(1 << DDRB6) //PB6
 
	
 
    #define select_card() PORTB &= ~(1 << PORTB0)
 
    #define unselect_card() PORTB |= (1 << PORTB0)
 
#else
 
    //#error "no sd/mmc pin mapping available!" //commented out due to build error ??
 
    #error "no sd/mmc pin mapping available!" //sends error if micro not specified
 
#endif
 

	
 
#define configure_pin_available() DDRC &= ~(1 << DDC4)
 
#define configure_pin_locked() DDRC &= ~(1 << DDC5)
 
#define configure_pin_available() DDRC &= ~(1 << DDRC4)
 
#define configure_pin_locked() DDRC &= ~(1 << DDRC5)
 

	
 
#define get_pin_available() (PINC & (1 << PINC4))
 
#define get_pin_locked() (PINC & (1 << PINC5))
0 comments (0 inline, 0 general)