Changeset - 981b6da12ce1
[Not reviewed]
default
0 6 2
kripperger@CL-SEC241-09.cedarville.edu - 12 years ago 2012-11-09 16:31:04
kripperger@CL-SEC241-09.cedarville.edu
Adding SPI
8 files changed with 243 insertions and 30 deletions:
0 comments (0 inline, 0 general)
slave/slave/lib/inputOutput.c
Show inline comments
 
@@ -5,14 +5,13 @@
 
 *  Author: kripperger
 
 */ 
 
 
 #include <avr/io.h>
 
 
 
 uint8_t io_getModuleId() {
 
	//get ID from rotary dip and return it. 
 
	
 
	// Get ID from rotary dip and return it. 
 
	uint8_t id;
 
 
	id = 0; //DEBUG
 
 
	return id;
 
 }
 
\ No newline at end of file
slave/slave/lib/inputOutput.h
Show inline comments
 
@@ -6,11 +6,11 @@
 
 */ 
 
 
 
 #ifndef IO_H_
 
 #define IO_H_
 
 
 uint8_t io_getModuleId();	//get ID from rotary dip and return it.
 
 uint8_t io_getModuleId();	// Get ID from rotary dip and return it.
 
 
 
 
 #endif /* IO_H_ */
 
\ No newline at end of file
slave/slave/lib/spi.c
Show inline comments
 
new file 100644
 
/* 
 
 * 
 
 *
 
 *
 
 *
 
 *
 
 *
 
 */
 

	
 

	
 
#include "spi.h"
 

	
 

	
 
void setup_spi(uint8_t mode, int dord, int interrupt, uint8_t clock)
 
{
 
  // specify pin directions for SPI pins on port B
 
  if (clock == SPI_SLAVE) { // if slave SS and SCK is input
 
    DDRB &= ~(1<<SPI_MOSI_PIN); // input
 
    DDRB |= (1<<SPI_MISO_PIN); // output
 
    DDRB &= ~(1<<SPI_SS_PIN); // input
 
    DDRB &= ~(1<<SPI_SCK_PIN);// input
 
  } else {
 
    DDRB |= (1<<SPI_MOSI_PIN); // output
 
    DDRB &= ~(1<<SPI_MISO_PIN); // input
 
    DDRB |= (1<<SPI_SCK_PIN);// output
 
    DDRB |= (1<<SPI_SS_PIN);// output
 
  }
 
  SPCR0 = ((interrupt ? 1 : 0)<<SPIE0) // interrupt enabled
 
    | (1<<SPE0) // enable SPI
 
    | (dord<<DORD0) // LSB or MSB
 
    | (((clock != SPI_SLAVE) ? 1 : 0) <<MSTR0) // Slave or Master
 
    | (((mode & 0x02) == 2) << CPOL0) // clock timing mode CPOL
 
    | (((mode & 0x01)) << CPHA0) // clock timing mode CPHA
 
    | (((clock & 0x02) == 2) << SPR10) // cpu clock divisor SPR1
 
    | ((clock & 0x01) << SPR00); // cpu clock divisor SPR0
 
  SPSR0 = (((clock & 0x04) == 4) << SPI2X0); // clock divisor SPI2X
 
}
 

	
 
void disable_spi()
 
{
 
  SPCR0 = 0;
 
}
 

	
 
uint8_t send_spi(uint8_t out)
 
{
 
  SPDR0 = out;
 
  while (!(SPSR0 & (1<<SPIF0)));
 
  return SPDR0;
 
}
 

	
 
uint8_t received_from_spi(uint8_t data)
 
{
 
  SPDR0 = data;
 
  return SPDR0;
 
}
slave/slave/lib/spi.h
Show inline comments
 
new file 100644
 
/* 
 
 *
 
 *
 
 *
 
 *
 
 *
 
 */
 

	
 

	
 
#ifndef SPI_H_
 
#define SPI_H_
 

	
 
#include <avr/io.h>
 

	
 

	
 
#define SPI_SS_PIN PORTB4
 
#define SPI_SCK_PIN PORTB7
 
#define SPI_MOSI_PIN PORTB5
 
#define SPI_MISO_PIN PORTB6
 

	
 

	
 
// SPI clock modes
 
#define SPI_MODE_0 0x00 /* Sample (Rising) Setup (Falling) CPOL=0, CPHA=0 */
 
#define SPI_MODE_1 0x01 /* Setup (Rising) Sample (Falling) CPOL=0, CPHA=1 */
 
#define SPI_MODE_2 0x02 /* Sample (Falling) Setup (Rising) CPOL=1, CPHA=0 */
 
#define SPI_MODE_3 0x03 /* Setup (Falling) Sample (Rising) CPOL=1, CPHA=1 */
 

	
 
// data direction
 
#define SPI_LSB 1 /* send least significant bit (bit 0) first */
 
#define SPI_MSB 0 /* send most significant bit (bit 7) first */
 

	
 
// whether to raise interrupt when data received (SPIF bit received)
 
#define SPI_NO_INTERRUPT 0
 
#define SPI_INTERRUPT 1
 

	
 
// slave or master with clock diviser
 
#define SPI_SLAVE 0xF0
 
#define SPI_MSTR_CLK4 0x00 /* chip clock/4 */
 
#define SPI_MSTR_CLK16 0x01 /* chip clock/16 */
 
#define SPI_MSTR_CLK64 0x02 /* chip clock/64 */
 
#define SPI_MSTR_CLK128 0x03 /* chip clock/128 */
 
#define SPI_MSTR_CLK2 0x04 /* chip clock/2 */
 
#define SPI_MSTR_CLK8 0x05 /* chip clock/8 */
 
#define SPI_MSTR_CLK32 0x06 /* chip clock/32 */
 

	
 

	
 

	
 
// setup spi
 
void setup_spi(uint8_t mode,   // timing mode SPI_MODE[0-4]
 
	       int dord,             // data direction SPI_LSB|SPI_MSB
 
	       int interrupt,        // whether to raise interrupt on recieve
 
	       uint8_t clock); // clock diviser
 

	
 
// disable spi
 
void disable_spi(void);
 

	
 
// send and receive a byte of data (master mode)
 
uint8_t send_spi(uint8_t out);
 

	
 
// receive the byte of data waiting on the SPI buffer and
 
// set the next byte to transfer - for use in slave mode
 
// when interrupts are enabled.
 
uint8_t received_from_spi(uint8_t out);
 

	
 

	
 

	
 
#endif /* SPI_H_ */
 

	
slave/slave/modules.c
Show inline comments
 
@@ -2,31 +2,116 @@
 
 * modules.c
 
 *
 
 * Created: 11/9/2012 11:44:22 AM
 
 *  Author: kripperger
 
 */ 
 
 
 #include <inttypes.h>
 
 #include <avr/io.h>
 
 #include "modules.h"
 
 
 
 
 
 
 
 void modules_setup(uint8_t id)
 
 {
 
 
	switch(id)
 
	{
 
		case 0:
 
			modules_generic_setup();
 
			break;
 
			
 
		case 1:
 
			modules_sensors_setup();
 
			break;
 
			
 
		case 2:
 
			modules_geiger_setup();
 
			break;
 
			
 
		case 3:
 
			modules_cameras_setup();
 
			break;
 
			
 
		default:
 
			modules_generic_setup();
 
			break;
 
	}
 
	  
 
 }
 
 
 
 void modules_run(uint8_t id)
 
 {
 
 
	 switch(id)
 
	 {
 
		 case 0:
 
			modules_generic();
 
			break;
 
		  
 
		 case 1:
 
			modules_sensors();
 
			break;
 
		  
 
		 case 2:
 
			modules_geiger();
 
			break;
 
		  
 
		 case 3:
 
			modules_cameras();
 
			break;
 
		  
 
		 default:
 
			modules_generic();
 
			break;
 
	 }
 
	  
 
 }
 
 
 
 
 
 void modules_generic_setup()
 
 {
 
	  
 
 }
 
  
 
 void modules_sensors_setup()
 
 {
 
	  
 
 }
 
  
 
 void modules_geiger_setup()
 
 {
 
	  
 
 }
 
  
 
 void modules_cameras_setup()
 
 {
 
	  	  
 
 }
 
  
 
 
 
 
 void modules_generic()
 
 {
 
	// Gathers data and performs functions for generic daughter board
 
 
 }
 
  
 
 void modules_sensors()
 
 {
 
	// Gathers data and performs functions for sensor daughter board
 
	 
 
 }
 
  
 
 void modules_geiger()
 
 {
 
	// Gathers data and performs functions for geiger daughter board
 
	  
 
 }
 
  
 
 void modules_cameras()
 
 {
 
	// Gathers data and performs functions for cameras daughter board
 
  
 
 } 
 
  
 
\ No newline at end of file
slave/slave/modules.h
Show inline comments
 
@@ -6,12 +6,22 @@
 
 */ 
 
 
 
#ifndef MODULES_H_
 
#define MODULES_H_
 
 
 void modules_setup(uint8_t id);
 
 void modules_run(uint8_t id);
 
 
 
 // Setup functions for microcontroller configuration per specific daughter board
 
 void modules_generic_setup();
 
 void modules_sensors_setup();
 
 void modules_geiger_setup();
 
 void modules_cameras_setup();
 
 
 // Data acquisition for specific daughter board
 
 void modules_generic();
 
 void modules_sensors();
 
 void modules_geiger();
 
 void modules_cameras();
 
 
slave/slave/slave.c
Show inline comments
 
@@ -9,31 +9,32 @@
 
 * Matthew Kroening
 
 *
 
 */
 
 
 
#include "config.h"
 
#include "modules.h"
 
 
#include <inttypes.h>
 
#include <avr/io.h>
 
#include <util/delay.h>
 
#include <compat/twi.h>
 
 
#include "modules.h"
 
//#include "lib/serial.h"		//Not made yet
 
#include "lib/led.h"
 
#include "lib/inputOutput.h"
 
#include "lib/i2c.h"
 
 
#include "lib/spi.h"
 
 
void micro_setup()
 
{
 
	// Generic microcontroller config options
 
	
 
	DDRA=0xFF; //PORTA is output //DEBUG
 
	
 
	
 
}
 
 
 
 
int main(void)
 
{
 
@@ -41,54 +42,43 @@ int main(void)
 
	micro_setup();			// Generic microcontroller config options
 
	led_configure();		//
 
	i2c_init();				// Setup I2C
 
	//serial_setup();		// Config serial ports
 
	
 
	uint8_t moduleID = io_getModuleId(); // Slave Module ID from rotary dip switch
 
	modules_setup(moduleID);
 
 
	
 
	
 
	uint8_t temp;	//DEBUG
 
	
 
	
 
	
 
	
 
    while(1)
 
    {
 
		
 
 
		modules_run(moduleID);
 
 
 
 
 
 
 
 
 
 
        //serial_SendCommand('0','A',0,0);	//DEBUG: EXAMPLE
 
		
 
		//i2c_write(RTC_ADDR, 0x05, 0x3A);	//DEBUG: EXAMPLE
 
 
		_delay_ms(10);
 
		temp = i2c_read(RTC_ADDR, 0x02);		//DEBUG: EXAMPLE: seconds
 
		
 
		PORTA = temp;
 
 
 
		switch(moduleID)
 
		{
 
			case 0:
 
			
 
				break;
 
			
 
			case 1:
 
			
 
				break;
 
			
 
			case 2:
 
			
 
				break;
 
			
 
			case 3:
 
			
 
				break;
 
				
 
			default:
 
			
 
				break;
 
		}
 
        PORTA = temp;		//DEBUG
 
 
 
 
    }
 
	
 
	return 0;
slave/slave/slave.cproj
Show inline comments
 
@@ -129,12 +129,18 @@
 
    <Compile Include="lib\led.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\led.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\spi.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\spi.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="modules.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="modules.h">
 
      <SubType>compile</SubType>
 
    </Compile>
0 comments (0 inline, 0 general)