Files @ eaec1e7070c3
Branch filter:

Location: seniordesign-firmware/slave/slave/slave.c

ethanzonca@CL-ENS241-08.cedarville.edu
Tweaked config for actual launch
/*
 * Slave Firmware
 *
 * Wireless Observational Modular Aerial Network
 *
 * Kyle Ripperger
 * Ethan Zonca
 * Matthew Kanning
 * Matthew Kroening
 *
 */


#include "config.h"

#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <avr/io.h>
#include <compat/twi.h>
#include <util/delay.h>
#include <avr/cpufunc.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include "modules.h"
#include "lib/serial.h"
#include "lib/serparser.h"
#include "lib/led.h"
#include "lib/inputOutput.h"
#include "lib/i2c.h"
#include "lib/spi.h"
#include "lib/geiger.h"
#include "lib/sensors.h"
#include "lib/cameras.h"
#include "lib/loopTimer.h"
#include "lib/masterComm.h"
#include "lib/watchdog.h"

bool WDTreset = false;

void micro_setup()
{
	// Generic microcontroller config options
	WDTreset = ((MCUSR & 0b00001000) > 0);	// Check if WDT reset occured
	MCUSR = 0;		// Clear reset flags
	wdt_disable();	// Disable WDT
	_delay_ms(20);	// Power debounce
	sei();			// Enable interrupts
}



int main(void)
{

		
	// Initialize	
	micro_setup();			// Generic microcontroller config options
	time_setup();			// Setup loop timer and interrupts (TIMER0)
	watchdog_setup();		// Setup watchdog timer
	led_configure();		// Configure ports and registers for LED operation
	io_configure();			// Configure IO ports and registers
	i2c_init();				// Setup I2C
	serial0_setup();		// Config serial port 0
	serial1_setup();		// Config serial port 1
	
	_delay_ms(50);	// Setup hold delay
	if(WDTreset) led_on(1);	// Turn on LED if WDT reset has occurred	
	
	io_readModuleId();
	modules_setup(io_getModuleId());	// Run setup functions for specific module

	uint32_t lastLoop = 0;	// Time in ms when last loop occurred 
	
	// Serial output //DEBUG
	char buff[128];						//Buffer for serial output //DEBUG
	serial1_sendString("Starting Slave\r\n");	//DEBUG
			
    while(1)
    {	
		wdt_reset();	// Resets WDT (to prevent restart)
		
		// Master communication
		masterComm_checkParser();	//Checks parser for data requests from master

		// Main slave operations
		if ((time_millis() - lastLoop) > SENSOR_LOOP)	// Uses program timer to run every so often. Time interval defined in config.h
		{
			led_on(0);
			
			sensors_readBatt();				// Read Battery level
			sensors_readBoardTemp();		// Read board temperature sensor (Common on all slaves) (Data Read)
			modules_run(io_getModuleId());	// Runs specific module functions (like data reading)
			
			io_regulateTemp();			// Gets board temperature and enables heater if below threshold

			snprintf(buff,128,"|ModuleID: %u |BoardTemp: %i |Millis: %lu |Lux: %lu |Pressure: %lu |Altitude: %lu |Battery: %u \r\n ",io_getModuleId(),sensors_getBoardTemp(),time_millis(),sensors_getLux(),sensors_getPressure(),sensors_getAltitude(),sensors_getBatt()); //DEBUG
			serial1_sendString(buff); //DEBUG
			
			led_off(0);
			lastLoop = time_millis();
			
// Writes ID to EEPROM, change for all modules and delete after programming
// 0 is for generic setup,	 1 is for sensors,	 2 is for Geiger,	3 is for cameras
//i2c_write(EEPROM_ADDR, 0x05, 0x03);			
			
		}

    }
	
	return 0;
}





		/********Examples of data reading and getting******************
		x = geiger_getCpm();				//Data get
		x = sensors_getSpiTemp();			//Data get
		x = sensors_getBoardTemp();			//Data get
		
		sensors_readSpiTemp();				//Data Read
		sensors_readBoardTemp();			//Data Read
		
		led_output(0xFF);					//Output value to LED array
		i2c_write(RTC_ADDR, 0x05, 0x3A);	//i2c Write Example
		
		PORTA &= ~(1 << PA1);	//OFF
		PORTA |= (1 << PA1);	//ON
		PORTB ^= (1 << PB0);	//Toggle
		
		sprintf(buff, "log: %u,%u,%u,%u\r\n", temp,temp2,temp3,temp4);
		serial0_sendString(buff);
		
		**************************************************************/