Changeset - 7c7bd3044e69
[Not reviewed]
default
0 4 0
ethanzonca@CL-SEC241-08.cedarville.edu - 13 years ago 2012-11-09 17:00:43
ethanzonca@CL-SEC241-08.cedarville.edu
SD card datalogging working, master just writes hello world to sd card in a loop
4 files changed with 38 insertions and 38 deletions:
0 comments (0 inline, 0 general)
master/master/lib/logger.c
Show inline comments
 
/*
 
 * CFile1.c
 
 *
 
 * Created: 11/7/2012 8:05:44 PM
 
 *  Author: mkanning
 
 */ 
 
 
#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"
 
 
/* 
 
	//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()
 
{
 
 
		if(!sd_raw_init()) //sd_raw.c this function may need an overhaul
 
		{
 
			// initialization failed!!!!!!!
 
			PORTA |= 0b00000001;
 
			return;
 
		}
 
return;
 
 
		//check for SD exist/power/ready END
 
	
 
		/* open first partition */ 
 
		struct partition_struct* partition = partition_open(sd_raw_read,
 
															sd_raw_read_interval,
 
															sd_raw_write,
 
															sd_raw_write_interval,
 
															0
 
															);
 
		//check that partition was created correctly
 
		if(!partition)
 
		{
 
			// OH GOSH TERRIBLE ERROR HERE
 
			/* If the partition did not open, assume the storage device
 
				* is a "superfloppy", i.e. has no MBR.
 
				*/
 
			partition = partition_open(sd_raw_read,
 
										sd_raw_read_interval,
 
										sd_raw_write,
 
										sd_raw_write_interval,
 
										-1
 
										);
 
			if(!partition)
 
			{
 
				// oh frick opening partition failed
 
				PORTA |= 0b00000010;
 
				return;
 
			}
 
		}
 
		//open partition END
 
	
 
	
 
		//open file system BEGIN
 
		struct fat_fs_struct* fs = fat_open(partition);
 
		if(!fs)
 
		{
 
			// opening fs failed!
 
			PORTA |= 0b00000100;
 
			return;
 
		}
 
		//open file system END
 
	
 
	
 
		//open directory BEGIN
 
		/* open root directory */
 
		struct fat_dir_entry_struct directory;
 
		fat_get_dir_entry_of_path(fs, "/", &directory);
 

	
 
		struct fat_dir_struct* dd = fat_open_dir(fs, &directory);
 
		if(!dd)
 
		{
 
			// frick opening root dir failed
 
			PORTA |= 0b00000101;
 
			return;
 
		}
 
		//open directory END
 
		
 
		
 
		//simplified version of console BEGIN
 
		char buffer[24];
 
		char buffer[24] = "Omg this is cool, haha!";
 
		buffer[23] = 0xd;
 
		buffer[24] = 0xa;
 

	
 
		/* search file in current directory and open it */
 
		struct fat_file_struct* fd = open_file_in_dir(fs, dd, "data.csv"); //logger.h
 
		if(!fd)
 
		{
 
			PORTA |= 0b00000110;
 
			return;
 
		}
 

	
 
		int32_t offset = 0;//strtolong(offset_value);
 
		if(!fat_seek_file(fd, &offset, FAT_SEEK_SET)) //seek to begin or end or what ??
 
		{
 
			//error seek to file handling
 
			PORTA |= 0b00000111;	
 
			fat_close_file(fd);
 
			return;
 
		}
 

	
 
		buffer[0] = 'H';
 
		buffer[1] = 'e';
 
		buffer[2] = 'l';
 
		buffer[3] = 'l';
 
		buffer[4] = 'o';
 
		buffer[5] = 'w';
 
		buffer[6] = 'o';
 
		buffer[7] = 'r';
 
		buffer[8] = 'l';
 
		buffer[9] = 'd';
 

	
 
		
 
		/* read text from the shell and write it to the file */
 
		uint8_t data_len = sizeof(buffer);
 
			
 
		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"));
 
				break;
 
			}
 
		}
 

	
 
		fat_close_file(fd); //may want to leave file open ??
 
		
 
 
		
 
		//prepare for closing SD connection BEGIN
 
		/* close directory */
 
		fat_close_dir(dd); //fat.c
 

	
 
		/* close file system */
 
		fat_close(fs); //fat.c
 

	
 
		/* close partition */
 
		partition_close(partition); //partition.c
 
		//prepare for closing SD connection END
 
}	
 
 
//writes a single line to the SD card
 
uint8_t logger_writeLine(char* dateLine, uint8_t length)
 
{
 
	return length; //does not actually return length
 
}
 
 
//i think opens a file so it can be read/written
 
struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name)
 
{
 
	struct fat_dir_entry_struct file_entry;
 
	if(!find_file_in_dir(fs, dd, name, &file_entry))
 
	return 0;
 

	
 
	return fat_open_file(fs, &file_entry); //fat.h
 
}
 

	
 
//i think searches for a file
 
uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry)
 
{
master/master/lib/sd/sd_raw.c
Show inline comments
 
@@ -138,113 +138,115 @@
 
#define DR_STATUS_MASK 0x0e
 
#define DR_STATUS_ACCEPTED 0x05
 
#define DR_STATUS_CRC_ERR 0x0a
 
#define DR_STATUS_WRITE_ERR 0x0c
 

	
 
/* status bits for card types */
 
#define SD_RAW_SPEC_1 0
 
#define SD_RAW_SPEC_2 1
 
#define SD_RAW_SPEC_SDHC 2
 

	
 
#if !SD_RAW_SAVE_RAM
 
/* static data buffer for acceleration */
 
static uint8_t raw_block[512];
 
/* offset where the data within raw_block lies on the card */
 
static offset_t raw_block_address;
 
#if SD_RAW_WRITE_BUFFERING
 
/* flag to remember if raw_block was written to the card */
 
static uint8_t raw_block_written;
 
#endif
 
#endif
 

	
 
/* card type state */
 
static uint8_t sd_raw_card_type;
 

	
 
/* private helper functions */
 
static void sd_raw_send_byte(uint8_t b);
 
static uint8_t sd_raw_rec_byte();
 
static uint8_t sd_raw_send_command(uint8_t command, uint32_t arg);
 

	
 
/**
 
 * \ingroup sd_raw
 
 * Initializes memory card communication.
 
 *
 
 * \returns 0 on failure, 1 on success.
 
 */
 
uint8_t sd_raw_init()
 
{
 
    /* enable inputs for reading card status */
 
    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
 
    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); // commentnig this out means /64, which gives over 100khz as required
 
    SPSR0 &= ~(1 << SPI2X0); // No doubled clock frequency 
 

	
 
    /* initialize SPI with lowest frequency; max. 400kHz during identification mode of card */
 
    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); // commentnig this out means /64, which gives over 100khz as required
 
    SPSR0 &= ~(1 << SPI2X0); /* No doubled clock frequency */
 
// transmit a char
 
 while ( ! ( SPSR0 & ( 1 << SPIF0 )));  //  wait for completion of
 
 SPDR0 = 'c';                            // begin transmission
 
 
 
/*
 
	while(1) {
 
		SPDR0 = 'a';					//Load byte to Data register
 
		while(!(SPSR0 & (1<<SPIF0))); 	// Wait for transmission complete
 
	}
 
*/
 

	
 
return;
 

	
 
    /* 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();
 
    }
 

	
 
    /* address card */
 
    select_card();
 

	
 
    /* reset card */
 
    uint8_t response;
 
    for(uint16_t i = 0; ; ++i)
 
    {
 
        response = sd_raw_send_command(CMD_GO_IDLE_STATE, 0);
 
        if(response == (1 << R1_IDLE_STATE))
 
            break;
 

	
 
        if(i == 0x1ff)
 
        {
 
            unselect_card();
 
            return 0;
 
        }
 
    }
 

	
 
#if SD_RAW_SDHC
 
    /* check for version of SD card specification */
 
    response = sd_raw_send_command(CMD_SEND_IF_COND, 0x100 /* 2.7V - 3.6V */ | 0xaa /* test pattern */);
 
    if((response & (1 << R1_ILL_COMMAND)) == 0)
 
    {
 
        sd_raw_rec_byte();
 
        sd_raw_rec_byte();
 
        if((sd_raw_rec_byte() & 0x01) == 0)
 
            return 0; /* card operation voltage range doesn't match */
 
        if(sd_raw_rec_byte() != 0xaa)
 
            return 0; /* wrong test pattern */
 

	
 
        /* card conforms to SD 2 card specification */
 
        sd_raw_card_type |= (1 << SD_RAW_SPEC_2);
 
    }
 
    else
master/master/lib/sd/sd_raw_config.h
Show inline comments
 
@@ -28,87 +28,87 @@ extern "C"
 
 * MMC/SD support configuration (license: GPLv2 or LGPLv2.1)
 
 */
 

	
 
/**
 
 * \ingroup sd_raw_config
 
 * Controls MMC/SD write support.
 
 *
 
 * Set to 1 to enable MMC/SD write support, set to 0 to disable it.
 
 */
 
#define SD_RAW_WRITE_SUPPORT 1
 

	
 
/**
 
 * \ingroup sd_raw_config
 
 * Controls MMC/SD write buffering.
 
 *
 
 * Set to 1 to buffer write accesses, set to 0 to disable it.
 
 *
 
 * \note This option has no effect when SD_RAW_WRITE_SUPPORT is 0.
 
 */
 
#define SD_RAW_WRITE_BUFFERING 1
 

	
 
/**
 
 * \ingroup sd_raw_config
 
 * Controls MMC/SD access buffering.
 
 * 
 
 * Set to 1 to save static RAM, but be aware that you will
 
 * lose performance.
 
 *
 
 * \note When SD_RAW_WRITE_SUPPORT is 1, SD_RAW_SAVE_RAM will
 
 *       be reset to 0.
 
 */
 
#define SD_RAW_SAVE_RAM 1
 

	
 
/**
 
 * \ingroup sd_raw_config
 
 * Controls support for SDHC cards.
 
 *
 
 * Set to 1 to support so-called SDHC memory cards, i.e. SD
 
 * cards with more than 2 gigabytes of memory.
 
 */
 
#define SD_RAW_SDHC 0
 

	
 
/**
 
 * @}
 
 */
 

	
 
#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_ss() DDRB |= (1 << DDRB0) | (1 << DDRB4); PORTB |= (1<<DDRB4) //PB0 - custom pin, but pb4 must be set as output
 
#define configure_pin_miso() DDRB &= ~(1 << DDRB6) //PB6
 
	
 
#define select_card() PORTB &= ~(1 << PORTB0)
 
#define unselect_card() PORTB |= (1 << PORTB0)
 

	
 

	
 

	
 
#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
 
#undef SD_RAW_SAVE_RAM
 
#define SD_RAW_SAVE_RAM 0
 
#else
 
#undef SD_RAW_WRITE_BUFFERING
 
#define SD_RAW_WRITE_BUFFERING 0
 
#endif
 

	
 
#ifdef __cplusplus
 
}
 
#endif
 

	
 
#endif
 

	
master/master/master.c
Show inline comments
 
/*
 
 * Master Firmware
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Kyle Ripperger
 
 * Matthew Kroening
 
 *
 
 */
 

	
 

	
 
#include "config.h"
 

	
 
#include <avr/io.h>
 
#include <util/delay.h>
 

	
 
#include "lib/serial.h"
 
#include "lib/aprs.h"
 
#include "lib/afsk.h"
 
#include "lib/led.h"
 
#include "lib/logger.h"
 

	
 
#include "lib/sd/sd_raw_config.h"
 

	
 
void micro_setup() {
 
	// Generic microcontroller config options
 

	
 
}
 

	
 

	
 
int main(void)
 
{
 
	// Initialize
 
	micro_setup();
 
	led_setup();
 
	//micro_setup();
 
	//led_setup();
 

	
 

	
 
	serial_setup(); // Config serial ports
 
	afsk_setup();
 
	//serial_setup(); // Config serial ports
 
	//afsk_setup();
 

	
 
	_delay_ms(400);	
 
	_delay_ms(400);	
 
	logger_setup(); // right now this blocks and writes text to the SD card
 
	
 
	logger_setup();
 
	
 

	
 
   
 
    while(1)
 
    {
 
		
 
		//afsk_test();
 
		//aprs_send();
 

	
 
		aprs_send();
 
		
 
		_delay_ms(400);
 
		_delay_ms(400);
 
		_delay_ms(400);
 
		_delay_ms(400);
 
		_delay_ms(400);
 
        //serial_SendCommand('0','A',0,0);
 
		
 
		
 
    }
 
}
 
\ No newline at end of file
0 comments (0 inline, 0 general)