Changeset - 5109fd6856e1
[Not reviewed]
default
0 6 0
ethanzonca@CL-SEC241-08.cedarville.edu - 13 years ago 2012-11-09 00:00:05
ethanzonca@CL-SEC241-08.cedarville.edu
Work on SD card
6 files changed with 102 insertions and 123 deletions:
0 comments (0 inline, 0 general)
master/master/lib/afsk.c
Show inline comments
 
@@ -205,28 +205,24 @@ void afsk_setup() {
 

	
 
	// Do non-inverting PWM on pin OC2B (arduino pin 3) (p.159).
 
	// OC2A (arduino pin 11) stays in normal port operation:
 
	// COM2B1=1, COM2B0=0, COM2A1=0, COM2A0=0
 
	TCCR2A = (TCCR2A | _BV(COM2B1)) & ~(_BV(COM2B0) | _BV(COM2A1) | _BV(COM2A0));
 

	
 
	// No prescaler (p.162)
 
	TCCR2B = (TCCR2B & ~(_BV(CS22) | _BV(CS21))) | _BV(CS20);
 
	
 
	// Enable interrupt when TCNT2 reaches TOP (0xFF) (p.151, 163)
 
	//TIMSK2 |= _BV(TOIE2);
 
	TIMSK2 |= 0b00000001;
 

	
 
	// Initialize PORTA
 
	DDRA = 0xff;
 
	PORTA = 0x00;
 
	
 
	DDRD = 0xff;
 
	PORTD = 0xff;
 
	
 
	// todo: init radio, maybe in main
 
	
 
	sei();
 
}
 

	
 

	
 
void afsk_test() {
 
	uint8_t flap[26] = {"abcdefghijklmnopqrstuvwxyz"};
master/master/lib/led.c
Show inline comments
 
/*
 
 * led.c
 
 *
 
 * Created: 10/25/2012 3:34:03 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
#include <avr/io.h>
 
 
void led_setup() {
 
	// Configure ports/pins for LEDs
 
	DDRA = 0xff;
 
	PORTA = 0x00;
 
}
 
 
void led_On(uint8_t led) {
 
	// Turn the specified LED on
 
	PORTA |= led;
 
}
 
 
void led_Off(uint8_t led) {
 
	// Turn the specified LED off
 
	PORTA &= ~(led);
 
}
 
 
void led_Toggle(uint8_t led) {
 
	// Toggle the specified LED 
 
}
 
\ No newline at end of file
master/master/lib/logger.c
Show inline comments
 
@@ -17,157 +17,148 @@
 
 
/* 
 
	//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)
 
	{
 
		//check for SD exist/power/ready BEGIN
 
		/* setup SD card slot */
 
 
		if(!sd_raw_init()) //sd_raw.c this function may need an overhaul
 
		{
 
	#if DEBUG
 
			uart_puts_p(PSTR("MMC/SD initialization failed\n"));
 
	#endif
 
			continue;
 
			// initialization failed!!!!!!!
 
			PORTA |= 0b00000001;
 
			return;
 
		}
 
return;
 
		//check for SD exist/power/ready END
 
	
 
		//create partition BEGIN
 
		/* open first partition */ 
 
		struct partition_struct* partition = partition_open(sd_raw_read,
 
															sd_raw_read_interval,
 
	//#if SD_RAW_WRITE_SUPPORT //probably want this to be true ??
 
															sd_raw_write,
 
															sd_raw_write_interval,
 
	//#endif
 
															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.
 
				*/
 
			//what is MBR ??
 
			partition = partition_open(sd_raw_read,
 
										sd_raw_read_interval,
 
	#if SD_RAW_WRITE_SUPPORT //probably want this to be true ??
 
										sd_raw_write,
 
										sd_raw_write_interval,
 
	#else
 
										0,
 
										0,
 
	#endif
 
										-1
 
										);
 
			if(!partition)
 
			{
 
	#if DEBUG
 
				uart_puts_p(PSTR("opening partition failed\n"));
 
	#endif
 
				continue;
 
				// oh frick opening partition failed
 
				PORTA |= 0b00000010;
 
				return;
 
			}
 
		}
 
		//open partition END
 
	
 
	
 
		//open file system BEGIN
 
		/* open file system */
 
		struct fat_fs_struct* fs = fat_open(partition);
 
		if(!fs)
 
		{
 
			#if DEBUG
 
			uart_puts_p(PSTR("opening file system failed\n"));
 
			#endif
 
			continue;
 
			// 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)
 
		{
 
			#if DEBUG
 
			uart_puts_p(PSTR("opening root directory failed\n"));
 
			#endif
 
			continue;
 
			// frick opening root dir failed
 
			PORTA |= 0b00000101;
 
			return;
 
		}
 
		//open directory END
 
		
 
		
 
		//simplified version of console BEGIN
 
		/* provide a simple shell */
 
		char buffer[24];
 

	
 
		/* 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)
 
		{
 
			/* execute command */
 
			/* 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)
 
			/* write text to file !! */
 
			if(fat_write_file(fd, (uint8_t*) buffer, data_len) != data_len)
 
			{
 
				//error open file handling
 
				continue;
 
				//uart_puts_p(PSTR("error writing to file\n"));
 
				break;
 
			}
 

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

	
 
			/* 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 ??
 
		}
 
		//simplified version of console END	
 
		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;
master/master/lib/sd/sd_raw.c
Show inline comments
 
@@ -182,27 +182,33 @@ uint8_t sd_raw_init()
 
    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);
 
           (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
 
 
 

	
 
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();
 
@@ -330,35 +336,40 @@ uint8_t sd_raw_init()
 

	
 
    return 1;
 
}
 

	
 
/**
 
 * \ingroup sd_raw
 
 * Checks wether a memory card is located in the slot.
 
 *
 
 * \returns 1 if the card is available, 0 if it is not.
 
 */
 
uint8_t sd_raw_available()
 
{
 
	int i;
 
	return 1; // !!TODO: OH GOSH CHANGE ME
 
    return get_pin_available() == 0x00;
 
}
 

	
 
/**
 
 * \ingroup sd_raw
 
 * Checks wether the memory card is locked for write access.
 
 *
 
 * \returns 1 if the card is locked, 0 if it is not.
 
 */
 
uint8_t sd_raw_locked()
 
{
 
	int i;
 
	// !!TODO oh gosh change me
 
	return 0;
 
    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)
 
{
 
@@ -369,26 +380,29 @@ void sd_raw_send_byte(uint8_t b)
 
}
 

	
 
/**
 
 * \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 */
 

	
 
    SPDR0 = 0xff;
 
		PORTA = 0x01;
 
    while(!(SPSR0 & (1 << SPIF0)));
 
	PORTA = 0x02;
 
    SPSR0 &= ~(1 << SPIF0);
 

	
 
    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.
master/master/lib/sd/sd_raw_config.h
Show inline comments
 
@@ -62,85 +62,50 @@ extern "C"
 
 * \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
 

	
 
/**
 
 * @}
 
 */
 

	
 
/* defines for customisation of sd/mmc port access */
 
#if defined(__AVR_ATmega8__) || \
 
    defined(__AVR_ATmega48__) || \
 
    defined(__AVR_ATmega48P__) || \
 
    defined(__AVR_ATmega88__) || \
 
    defined(__AVR_ATmega88P__) || \
 
    defined(__AVR_ATmega168__) || \
 
    defined(__AVR_ATmega168P__) || \
 
    defined(__AVR_ATmega328P__)
 
    #define configure_pin_mosi() DDRB |= (1 << DDB3)
 
    #define configure_pin_sck() DDRB |= (1 << DDB5)
 
    #define configure_pin_ss() DDRB |= (1 << DDB2)
 
    #define configure_pin_miso() DDRB &= ~(1 << DDB4)
 

	
 
    #define select_card() PORTB &= ~(1 << PORTB2)
 
    #define unselect_card() PORTB |= (1 << PORTB2)
 
#elif defined(__AVR_ATmega16__) || \
 
      defined(__AVR_ATmega32__)
 
    #define configure_pin_mosi() DDRB |= (1 << DDB5)
 
    #define configure_pin_sck() DDRB |= (1 << DDB7)
 
    #define configure_pin_ss() DDRB |= (1 << DDB4)
 
    #define configure_pin_miso() DDRB &= ~(1 << DDB6)
 
#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)
 

	
 
    #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!" //sends error if micro not specified
 
#endif
 

	
 
#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
master/master/master.c
Show inline comments
 
@@ -9,40 +9,49 @@
 
 * 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();
 

	
 

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

	
 
	_delay_ms(400);	
 
	_delay_ms(400);	
 
	
 
	logger_setup();
 
	
 
    while(1)
 
    {
 
		//afsk_test();
 
		aprs_send();
 
		_delay_ms(400);
 
		_delay_ms(400);
 
		//aprs_send();
 

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