commit cc9ca04e4d58158606627a644331754a3fd64e91
Author: Tom Mullins <tmullins@andrew.cmu.edu>
Date:   Tue Oct 30 15:26:26 2012 -0400

    Fixed pins to work with 128rfa1 instead of 2560

diff --git a/scout_avr/Makefile b/scout_avr/Makefile
index 9ddd33f..bf84754 100644
--- a/scout_avr/Makefile
+++ b/scout_avr/Makefile
@@ -1,15 +1,15 @@
 
-#PART=m128rfa1
-#MCU=atmega128rfa1
+PART=m128rfa1
+MCU=atmega128rfa1
 
 #PART=m328p
 #MCU=atmega328
 
-PART=m2560
-MCU=atmega2560
+#PART=m2560
+#MCU=atmega2560
 
-#PROG=avrispMKII
-PROG=stk600
+PROG=avrispMKII
+#PROG=stk600
 
 F_CPU=16000000
 SRC=src/*.cpp src/ros_lib/*.cpp
@@ -25,7 +25,7 @@ scout_avr.elf: $(SRC) $(HDR)
 	avr-g++ $(FLAGS) $(SRC) -o scout_avr.elf
 
 program: scout_avr.hex
-	avrdude -p $(PART) -c $(PROG) -P usb -B 1 -F -U flash:w:scout_avr.hex
+	avrdude -p $(PART) -c $(PROG) -P usb -B 5 -U flash:w:scout_avr.hex
 
 clean:
 	rm -f scout_avr.elf scout_avr.hex
diff --git a/scout_avr/src/bom.cpp b/scout_avr/src/bom.cpp
index 65e8043..3436eb6 100644
--- a/scout_avr/src/bom.cpp
+++ b/scout_avr/src/bom.cpp
@@ -53,7 +53,7 @@ char get_robot_id(void) {
 
 void bom_isr() {
   if (out_high) {
-    BOM_EMIT ^= out_pin_mask;
+    PORT_BOM_EMIT ^= out_pin_mask;
   }
 }
 
@@ -66,12 +66,12 @@ static void init_38kHz_signal() {
 
 static void start_38kHz_signal() {
   TCNT3 = 0;
-  BOM_EMIT |= out_pin_mask;
+  PORT_BOM_EMIT |= out_pin_mask;
   out_high = 1;
 }
 
 static void stop_38kHz_signal() {
-  BOM_EMIT &= ~ out_pin_mask;
+  PORT_BOM_EMIT &= ~ out_pin_mask;
   out_high = 0;
 }
 
@@ -126,7 +126,7 @@ void bom_init(void) {
   PCICR |= _BV(PCIE0);
   
   // BOM_EMIT as output
-  DDRH |= _BV(DDH4) | _BV(DDH5) | _BV(DDH6) | _BV(DDH7);
+  DDRF |= _BV(DDF4) | _BV(DDF5) | _BV(DDF6) | _BV(DDF7);
 
   init_38kHz_signal();
   init_data_signal();
@@ -134,10 +134,10 @@ void bom_init(void) {
 
 void bom_send(char dir) {
   switch (dir) {
-    case BOM_FRONT: out_pin_mask = _BV(BOM_EMIT0); break;
-    case BOM_LEFT:  out_pin_mask = _BV(BOM_EMIT1); break;
-    case BOM_RIGHT: out_pin_mask = _BV(BOM_EMIT2); break;
-    case BOM_BACK:  out_pin_mask = _BV(BOM_EMIT3); break;
+    case BOM_FRONT: out_pin_mask = _BV(P_BOM_EMIT0); break;
+    case BOM_LEFT:  out_pin_mask = _BV(P_BOM_EMIT1); break;
+    case BOM_RIGHT: out_pin_mask = _BV(P_BOM_EMIT2); break;
+    case BOM_BACK:  out_pin_mask = _BV(P_BOM_EMIT3); break;
   }
   out_counter = 16;
   out_msg = sharp_msg_make(0x2A, bom_msg_make(robot_id, dir));
@@ -188,12 +188,12 @@ static void recv_edge(char is_rising, struct bom_rx_t *rx) {
 }
 
 ISR(PCINT0_vect) {
-  uint8_t bom_sig = BOM_SIG;
+  uint8_t bom_sig = PIN_BOM_SIG;
   uint8_t changed = bom_sig ^ prev_bom_sig;
-  if (changed & _BV(BOM_SIG0)) recv_edge(bom_sig & _BV(BOM_SIG0), &bom_rx[0]);
-  if (changed & _BV(BOM_SIG1)) recv_edge(bom_sig & _BV(BOM_SIG1), &bom_rx[1]);
-  if (changed & _BV(BOM_SIG2)) recv_edge(bom_sig & _BV(BOM_SIG2), &bom_rx[2]);
-  if (changed & _BV(BOM_SIG3)) recv_edge(bom_sig & _BV(BOM_SIG3), &bom_rx[3]);
+  if (changed & _BV(P_BOM_SIG0)) recv_edge(bom_sig & _BV(P_BOM_SIG0), &bom_rx[0]);
+  if (changed & _BV(P_BOM_SIG1)) recv_edge(bom_sig & _BV(P_BOM_SIG1), &bom_rx[1]);
+  if (changed & _BV(P_BOM_SIG2)) recv_edge(bom_sig & _BV(P_BOM_SIG2), &bom_rx[2]);
+  if (changed & _BV(P_BOM_SIG3)) recv_edge(bom_sig & _BV(P_BOM_SIG3), &bom_rx[3]);
   prev_bom_sig = bom_sig;
 }
 
diff --git a/scout_avr/src/bom.h b/scout_avr/src/bom.h
index 6f5eae3..03b5ea5 100644
--- a/scout_avr/src/bom.h
+++ b/scout_avr/src/bom.h
@@ -22,16 +22,17 @@ extern "C" {
 
 // i/o pins
 // if these are changed, remember to change bom_init and/or ISRs
-#define BOM_SIG PINB
-#define BOM_SIG0 PB0
-#define BOM_SIG1 PB1
-#define BOM_SIG2 PB2
-#define BOM_SIG3 PB3
-#define BOM_EMIT PORTH
-#define BOM_EMIT0 PH4
-#define BOM_EMIT1 PH5
-#define BOM_EMIT2 PH6
-#define BOM_EMIT3 PH7
+#define PIN_BOM_SIG PINB
+#define P_BOM_SIG0 PB0
+#define P_BOM_SIG1 PB1
+#define P_BOM_SIG2 PB2
+#define P_BOM_SIG3 PB3
+
+#define PORT_BOM_EMIT PORTF
+#define P_BOM_EMIT0 PF4
+#define P_BOM_EMIT1 PF5
+#define P_BOM_EMIT2 PF6
+#define P_BOM_EMIT3 PF7
 
 typedef uint8_t bom_msg_t;
 
@@ -48,7 +49,7 @@ inline bom_msg_t bom_msg_make(char id, char dir) {
 void set_robot_id(char id);
 char get_robot_id(void);
 
-// NOTE: call range_init before bom_init to ensure timer 1 is set up!
+// NOTE: call range_init before bom_init to ensure timer 5 is set up!
 void bom_init(void);
 void bom_send(char dir);
 int bom_get(char dir);
diff --git a/scout_avr/src/main.cpp b/scout_avr/src/main.cpp
index 39b506f..dab0da0 100644
--- a/scout_avr/src/main.cpp
+++ b/scout_avr/src/main.cpp
@@ -1,4 +1,4 @@
-#if 0 ///////////////////////////////////////////////
+#if 1 ///////////////////////////////////////////////
 
 #include "ros.h"
 #include <std_msgs/Int16.h>
diff --git a/scout_avr/src/range.cpp b/scout_avr/src/range.cpp
index 2c932fe..3d54b53 100644
--- a/scout_avr/src/range.cpp
+++ b/scout_avr/src/range.cpp
@@ -1,10 +1,9 @@
-#include "range.h"
-
 extern "C"
 {
 #include <avr/io.h>
 #include <avr/interrupt.h>
 }
+#include "range.h"
 
 /* Ultrasonic Sensor:
  * -if RX pin is left open, it will continuously take readings
@@ -12,19 +11,9 @@ extern "C"
  * -PW will be high for a maximum of 37.5ms if no target is detected
  * 
  * 37.5ms * 8 MHz / 8 prescaler = 37500 max wait
+ * 37.5ms * 16 MHz / 8 prescaler = problem
  */
 
-// so that we can test on a 328 or the stk600
-#if defined(__AVR_ATmega128RFA1__) || defined(__AVR_ATmega2560__)
-#  define read_INT0 (PIND & _BV(PD0))
-#  define read_INT1 (PIND & _BV(PD1))
-#elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__)
-#  define read_INT0 (PIND & _BV(PD2))
-#  define read_INT1 (PIND & _BV(PD3))
-#else
-#  error "Please define read_INTx for this device"
-#endif
-
 struct range_t {
   unsigned int start; // timer value on rising edge
   unsigned int value; // last measured range
@@ -33,17 +22,15 @@ struct range_t {
 static void on_edge(int which)
 {
   unsigned char int_high;
-  // TODO centralize timer 5
-  // TODO ensure this is in microseconds even for 16MHz
   unsigned int time = TCNT5;
   
   if (which)
   {
-    int_high = read_INT1;
+    int_high = PIN_SONAR_PWM & _BV(P_SONAR_PWM1);
   }
   else
   {
-    int_high = read_INT0;
+    int_high = PIN_SONAR_PWM & _BV(P_SONAR_PWM0);
   }
   
   if (int_high)
@@ -57,12 +44,12 @@ static void on_edge(int which)
   }
 }
 
-ISR(INT0_vect)
+ISR(INT3_vect)
 {
   on_edge(0);
 }
 
-ISR(INT1_vect)
+ISR(INT2_vect)
 {
   on_edge(1);
 }
@@ -70,9 +57,9 @@ ISR(INT1_vect)
 void range_init()
 {
   // ISCx = 1, edge triggered
-  EICRA |= _BV(ISC10) | _BV(ISC00);
-  // enable INT0 and INT1
-  EIMSK |= _BV(INT1) | _BV(INT0);
+  EICRA |= _BV(ISC20) | _BV(ISC30);
+  // enable INT2 and INT3
+  EIMSK |= _BV(INT2) | _BV(INT3);
   
   // CS1 = 2, 1/8 prescaler
   // if this is changed, remember to change recv_edge in bom.cpp!
diff --git a/scout_avr/src/range.h b/scout_avr/src/range.h
index ea4699f..76d75de 100644
--- a/scout_avr/src/range.h
+++ b/scout_avr/src/range.h
@@ -3,6 +3,13 @@
 
 #define RANGE_ERR 0xFFFF
 
+#define PIN_SONAR_PWM PIND
+#define P_SONAR_PWM1 PD2
+#define P_SONAR_PWM0 PD3
+
+#define PORT_SONAR_TX PORTG
+#define P_SONAR_TX PG1
+
 void range_init();
 unsigned int range_get(int which);
 
