summaryrefslogtreecommitdiff
path: root/analog
diff options
context:
space:
mode:
authorNicolas Schodet2008-04-14 00:55:34 +0200
committerNicolas Schodet2008-04-14 00:55:34 +0200
commit8e79e204e963eb10df09c58044df23f33612fe33 (patch)
tree24f498e9d6777007e71fea9a53a193d9089e765d /analog
parent9ec79f63c3a1299a44d389e77bb1b1edf9cb1f41 (diff)
* analog/motor-power-avr/src:
- added SPI protocol usage.
Diffstat (limited to 'analog')
-rw-r--r--analog/motor-power-avr/src/Makefile2
-rw-r--r--analog/motor-power-avr/src/main.c38
2 files changed, 39 insertions, 1 deletions
diff --git a/analog/motor-power-avr/src/Makefile b/analog/motor-power-avr/src/Makefile
index 42055767..e5178ba4 100644
--- a/analog/motor-power-avr/src/Makefile
+++ b/analog/motor-power-avr/src/Makefile
@@ -1,7 +1,7 @@
BASE = ../../../digital/avr
AVR_PROGS = mp
mp_SOURCES = main.c mp_pwm_L_.c mp_pwm_R_.c mp_pwm_LR_.c
-MODULES = proto uart utils math/fixed
+MODULES = proto uart utils math/fixed spi
CONFIGFILE = avrconfig.h
# atmega8, atmega8535, atmega128...
AVR_MCU = atmega16
diff --git a/analog/motor-power-avr/src/main.c b/analog/motor-power-avr/src/main.c
index b098fde3..f5928e2c 100644
--- a/analog/motor-power-avr/src/main.c
+++ b/analog/motor-power-avr/src/main.c
@@ -30,6 +30,7 @@
#include "modules/utils/utils.avr.h"
#include "modules/utils/byte.h"
#include "modules/math/fixed/fixed.h"
+#include "modules/spi/spi.h"
#include "io.h"
#include "mp_pwm_LR_.h"
#include "mp_pwm_L_.h"
@@ -69,6 +70,12 @@ uint16_t envTest_cpt, envTest_period, envTest_autosend;
// current limit stats
uint8_t curLim_stat_cpt, curLim_stat_period;
+/** Currently read SPI frame. */
+uint8_t spi_frame[4];
+/** Current size of the received frame. */
+uint8_t spi_frame_size;
+
+
/* +AutoDec */
/** Main loop. */
@@ -92,6 +99,8 @@ main (int argc, char **argv)
//PORTA = 0xff;
uart0_init ();
+ spi_init (SPI_IT_DISABLE | SPI_ENABLE | SPI_MSB_FIRST | SPI_SLAVE |
+ SPI_CPOL_FALLING | SPI_CPHA_SETUP | SPI_FOSC_DIV16);
init_timer_LR_ ();
init_curLim ();
//postrack_init ();
@@ -146,6 +155,35 @@ main_loop (void)
if (uart0_poll ())
proto_accept (uart0_getc ());
+ /* SPI. */
+ if (SPSR & _BV (SPIF))
+ {
+ spi_frame[spi_frame_size++] = SPDR;
+ if (spi_frame_size == 4)
+ {
+ /* Check integrity. */
+ if ((spi_frame[0] ^ spi_frame[1] ^ spi_frame[2] ^ spi_frame[3])
+ == 0x42)
+ {
+ uint8_t left_val = ((spi_frame[0] & 0x70) << 1) | (spi_frame[1] >> 3);
+ uint8_t left_dir = (spi_frame[0] & 0x80) ? 1 : 0;
+ if (left_dir)
+ left_val = -left_val;
+ uint8_t right_val = ((spi_frame[0] & 0x07) << 5) | (spi_frame[2] >> 3);
+ uint8_t right_dir = (spi_frame[0] & 0x08) ? 1 : 0;
+ if (right_dir)
+ right_val = -right_val;
+ start_motor_L_ (left_val, left_dir);
+ start_motor_R_ (right_val, right_dir);
+ }
+ spi_frame_size = 0;
+ }
+ }
+ if (PINB & _BV (SPI_BIT_SS))
+ {
+ spi_frame_size = 0;
+ }
+
/* Counter for launching environemental tests */
if (!(envTest_cpt --)) {
envTest_cpt = envTest_period;