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 75 insertions and 96 deletions:
0 comments (0 inline, 0 general)
master/master/lib/afsk.c
Show inline comments
 
@@ -212,16 +212,12 @@ void afsk_setup() {
 
	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();
master/master/lib/led.c
Show inline comments
 
@@ -6,19 +6,23 @@
 
 */ 
 
 
#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
 
@@ -23,116 +23,109 @@
 
  * 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];
 
		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)
 
			{
 
				//error open file handling
 
				continue;
 
			PORTA |= 0b00000110;
 
			return;
 
			}
 

	
 
			int32_t offset = 5;//strtolong(offset_value);
 
		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);
 
				continue;
 
			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 !! */
 
@@ -141,14 +134,13 @@ void logger_setup()
 
					//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
 

	
 
@@ -156,13 +148,12 @@ void logger_setup()
 
		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
 
}
master/master/lib/sd/sd_raw.c
Show inline comments
 
@@ -188,15 +188,21 @@ uint8_t sd_raw_init()
 
    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;
 
@@ -336,23 +342,28 @@ uint8_t sd_raw_init()
 
 * 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.
 
@@ -375,14 +386,17 @@ void sd_raw_send_byte(uint8_t b)
 
 * \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;
 
}
 

	
 
/**
master/master/lib/sd/sd_raw_config.h
Show inline comments
 
@@ -68,73 +68,38 @@ extern "C"
 
#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 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
master/master/master.c
Show inline comments
 
@@ -15,34 +15,43 @@
 

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