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
 
@@ -7,53 +7,24 @@
 
 
#include <string.h>
 
#include <avr/pgmspace.h>
 
#include <avr/sleep.h>
 
#include "sd/fat.h"
 
#include "sd/fat_config.h"
 
#include "sd/partition.h"
 
#include "sd/sd_raw.h"
 
#include "sd/sd_raw_config.h"
 
#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
 
  * the configuration defines in the files fat_config.h, partition_config.h,
 
  * sd_raw_config.h and sd-reader_config.h. For example, you could disable
 
  * write support completely if you only need read support.
 

	
 
 */
 
 
void logger_setup()
 
{
 
	while(1)
 
@@ -160,25 +131,25 @@ void logger_setup()
 
				
 
				fat_close_file(fd);
 
				continue;
 
			}
 

	
 
			/* read text from the shell and write it to the file */
 
			uint8_t data_len;
 
			while(1)
 
			{
 
				/* 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;
 
				}
 
			}
 

	
 
			fat_close_file(fd); //may want to leave file open ??
 
		}
 
		//simplified version of console END	
 
		
 
		
 
		//prepare for closing SD connection BEGIN
 
		/* close directory */
 
		fat_close_dir(dd); //fat.c
master/master/lib/sd/sd_raw.c
Show inline comments
 
@@ -2,24 +2,25 @@
 
/*
 
 * Copyright (c) 2006-2012 by Roland Riegel <feedback@roland-riegel.de>
 
 *
 
 * This file is free software; you can redistribute it and/or modify
 
 * it under the terms of either the GNU General Public License version 2
 
 * or the GNU Lesser General Public License version 2.1, both as
 
 * published by the Free Software Foundation.
 
 */
 

	
 
#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
 
 *
 
 * This module implements read and write access to MMC, SD
 
 * and SDHC cards. It serves as a low-level driver for the
 
 * higher level modules such as partition and file system
 
 * access.
 
 *
 
 * @{
 
 */
 
/**
 
@@ -175,33 +176,33 @@ uint8_t sd_raw_init()
 
    configure_pin_available();
 
    configure_pin_locked();
 

	
 
    /* enable outputs for MOSI, SCK, SS, input for MISO */
 
    configure_pin_mosi();
 
    configure_pin_sck();
 
    configure_pin_ss();
 
    configure_pin_miso();
 

	
 
    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;
 
    
 
    if(!sd_raw_available())
 
        return 0;
 

	
 
    /* card needs 74 cycles minimum to start up */
 
    for(uint8_t i = 0; i < 10; ++i)
 
    {
 
        /* wait 8 clock cycles */
 
        sd_raw_rec_byte();
 
@@ -305,26 +306,26 @@ uint8_t sd_raw_init()
 

	
 
    /* set block size to 512 bytes */
 
    if(sd_raw_send_command(CMD_SET_BLOCKLEN, 512))
 
    {
 
        unselect_card();
 
        return 0;
 
    }
 

	
 
    /* deaddress card */
 
    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 */
 
    raw_block_address = (offset_t) -1;
 
#if SD_RAW_WRITE_BUFFERING
 
    raw_block_written = 1;
 
#endif
 
    if(!sd_raw_read(0, raw_block, sizeof(raw_block)))
 
        return 0;
 
#endif
 

	
 
    return 1;
 
@@ -352,45 +353,45 @@ uint8_t sd_raw_locked()
 
    return get_pin_locked() == 0x00;
 
}
 

	
 
/**
 
 * \ingroup sd_raw
 
 * Sends a raw byte to the memory card.
 
 *
 
 * \param[in] b The byte to sent.
 
 * \see sd_raw_rec_byte
 
 */
 
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);
 
}
 

	
 
/**
 
 * \ingroup sd_raw
 
 * Receives a raw byte from the memory card.
 
 *
 
 * \returns The byte which should be read.
 
 * \see sd_raw_send_byte
 
 */
 
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;
 
}
 

	
 
/**
 
 * \ingroup sd_raw
 
 * Send a command to the memory card which responses with a R1 response (and possibly others).
 
 *
 
 * \param[in] command The command to send.
 
 * \param[in] arg The argument for command.
 
 * \returns The command answer.
 
 */
 
uint8_t sd_raw_send_command(uint8_t command, uint32_t arg)
 
{
master/master/lib/sd/sd_raw_config.h
Show inline comments
 
@@ -97,30 +97,40 @@ extern "C"
 
    #define select_card() PORTB &= ~(1 << PORTB4)
 
    #define unselect_card() PORTB |= (1 << PORTB4)
 
#elif defined(__AVR_ATmega64__) || \
 
      defined(__AVR_ATmega128__) || \
 
      defined(__AVR_ATmega169__)
 
    #define configure_pin_mosi() DDRB |= (1 << DDB2)
 
    #define configure_pin_sck() DDRB |= (1 << DDB1)
 
    #define configure_pin_ss() DDRB |= (1 << DDB0)
 
    #define configure_pin_miso() DDRB &= ~(1 << DDB3)
 

	
 
    #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))
 

	
 
#if SD_RAW_SDHC
 
    typedef uint64_t offset_t;
 
#else
 
    typedef uint32_t offset_t;
 
#endif
 

	
 
/* configuration checks */
 
#if SD_RAW_WRITE_SUPPORT
0 comments (0 inline, 0 general)