Skip to content

Commit

Permalink
SPI support (slave): receive and display frames
Browse files Browse the repository at this point in the history
  • Loading branch information
joh committed Jan 15, 2011
1 parent 29676d5 commit 71049b8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 36 deletions.
6 changes: 3 additions & 3 deletions Makefile
Expand Up @@ -26,8 +26,8 @@
# dependent on your programmer and operating system.
# This settings works for my mac

PROGRAMMER = avrispmkII
PROGRAMMER_PORT = usb
PROGRAMMER = stk500
PROGRAMMER_PORT = /dev/ttyUSB0

# This are the hardware settings for the Blinken Buttons for Beginners. There is
# normally no need tho change these (or anything below) (if you do not change
Expand All @@ -40,7 +40,7 @@ PROGRAMMER_PORT = usb

DEVICE = atmega168
CLOCK = 8000000
OBJECTS = main.o core/animation.o core/display.o core/random.o core/state.o flash-content/sprites.o
OBJECTS = main.o core/display.o core/spi.o
FUSES = -Ulfuse:w:0xe2:m -Uhfuse:w:0xdf:m -Uefuse:w:0x1:m


Expand Down
21 changes: 0 additions & 21 deletions core/display.c
Expand Up @@ -35,8 +35,6 @@
#include <avr/pgmspace.h>
#include <string.h>

#include "state.h"
#include "../flash-content/sprites.h"
#include "display.h"

/*
Expand Down Expand Up @@ -188,25 +186,6 @@ display_advance_buffer(void)
display_status |= DISPLAY_BUFFER_ADVANCE;
}

/*
* Loads the test pattern.
* TODO come up with a better test pattern solution!
*/
void
display_load_default_sequence(void)
{
uint8_t default_load_buffer[8] =
{ 0, 0, 0, 0, 0, 0, 0, 0 };

display_current_buffer = 0;
copy_to_buffer(predefined_sprites[DEFAULT_1], default_load_buffer);
display_load_sprite(default_load_buffer);
display_current_buffer = 1;
copy_to_buffer(predefined_sprites[DEFAULT_2], default_load_buffer);
display_load_sprite(default_load_buffer);
display_current_buffer = 0;
}

/*
* Configures the column timer (Timer 0).
* at 156.25kHz?
Expand Down
55 changes: 55 additions & 0 deletions core/spi.c
@@ -0,0 +1,55 @@
/**
* SPI support
*/

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

#include "display.h"

#define SS PB2
#define MOSI PB3
#define MISO PB4
#define SCK PB5

// Sprite buffer
uint8_t sprite[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uint8_t i = 0;

/**
* Initialize SPI in slave mode
*/
void spi_init(void)
{
volatile char IOReg;

// Set MISO output
DDRB |= (1<<MISO);

// Enable SPI (slave) and SPI interrupts
SPCR = (1<<SPE) | (1<<SPIE);

// Clear SPIF bit in SPSR
IOReg = SPSR;
IOReg = SPDR;
}

/**
* SPI interrupt routine
*/
ISR(SPI_STC_vect)
{
// Save sprite into buffer
sprite[i++] = SPDR;

if (i == 8) {
// Sprite completed
i = 0;

display_load_sprite(sprite);
display_advance_buffer();
}
}


10 changes: 10 additions & 0 deletions core/spi.h
@@ -0,0 +1,10 @@
/**
* SPI support
*/

#ifndef SPI_H_
#define SPI_H_

void spi_init(void);

#endif /* SPI_H_ */
23 changes: 11 additions & 12 deletions main.c
Expand Up @@ -55,14 +55,12 @@
#include <stdlib.h>
#include <avr/interrupt.h>

// state manages the triggering of calculations
// according to Timer 2
#include "core/state.h"
// animations.c contains all routines for rendering the animations from single images
#include "core/animation.h"
// display.c is responsible for rendering the images on the display.
#include "core/display.h"

// spi support
#include "core/spi.h"

/*
* This is the main routine. The main routine gets executed when the ATmega powers up.
* It initializes all components and keeps them running, by checking if one of the state
Expand All @@ -76,9 +74,14 @@ main(void)
* CPU components first and power up only those components we need later.
* So here we switch anything of
*/
power_all_disable();
//now start the animations
animation_init();
// TODO: This makes SPI not work...
//power_all_disable();

// Initialize display
display_init();

// Initialize SPI
spi_init();

/*
* now we have initialized all components and can now switch to reactive mode.
Expand All @@ -92,10 +95,6 @@ main(void)
*/
for( ; ;)
{
/*
* by state_process we check if a new image has to be loaded and call the load routine
*/
state_process();
}
}

0 comments on commit 71049b8

Please sign in to comment.