got the output check stuff actually compiling and closer to working
diff --git a/bbb_cape/src/cape/data_struct.h b/bbb_cape/src/cape/data_struct.h
index d60a0e9..6d747bb 100644
--- a/bbb_cape/src/cape/data_struct.h
+++ b/bbb_cape/src/cape/data_struct.h
@@ -82,9 +82,12 @@
int32_t left_drive;
int32_t right_drive;
- // The length of the pulse from the ultrasonic sensor in 100kHZ ticks.
+ // The length of the pulse from the ultrasonic sensor in 100KHz ticks.
uint32_t ultrasonic_pulse_length;
+ // The length of the pulse from the sidecar PWM output in 10MHz ticks.
+ uint32_t output_check_pulse_length;
+
int32_t shooter_position, pusher_distal_posedge_position,
pusher_proximal_posedge_position;
diff --git a/bbb_cape/src/cape/digital.h b/bbb_cape/src/cape/digital.h
index 2f227a5..6071ca8 100644
--- a/bbb_cape/src/cape/digital.h
+++ b/bbb_cape/src/cape/digital.h
@@ -8,7 +8,7 @@
// For all of the digital functions, a high voltage level on the input reads as
// 1 (and a low to high transition is a positive edge).
// Encoder inputs 0-7 A and B are mapped to "digital inputs" 12-27 (12 is 0A,
-13 is B, 14 is 1A, etc).
+// 13 is B, 14 is 1A, etc).
static inline int digital_read(int num) {
switch (num) {
diff --git a/bbb_cape/src/cape/peripherial_usage.notes b/bbb_cape/src/cape/peripherial_usage.notes
index 757f14a..c8005c3 100644
--- a/bbb_cape/src/cape/peripherial_usage.notes
+++ b/bbb_cape/src/cape/peripherial_usage.notes
@@ -57,4 +57,4 @@
[robots]
robot_comp
TIM11
- TIM11_IRQ:3 (aka TIM1_TRG_COM)
+ TIM11_IRQ:1 (aka TIM1_TRG_COM)
diff --git a/bbb_cape/src/cape/robot_comp.c b/bbb_cape/src/cape/robot_comp.c
index cab3e28..03e529a 100644
--- a/bbb_cape/src/cape/robot_comp.c
+++ b/bbb_cape/src/cape/robot_comp.c
@@ -6,9 +6,7 @@
#include "cape/analog.h"
#include "cape/digital.h"
#include "cape/util.h"
-
-// TIM11.1 on PB9, aka digital input 6.
-static volatile uint32_t ultrasonic_pulse_length = 0;
+#include "cape/timer.h"
typedef struct {
uint32_t posedges, negedges;
@@ -91,27 +89,15 @@
CLAW(10, 11, 9, bottom_claw, 7);
SHOOTER(7, 5, 4, 8, 0)
-void TIM1_TRG_COM_TIM11_IRQHandler(void) {
- TIM11->SR = ~TIM_SR_CC1IF;
- if (digital_read(6)) {
- TIM11->EGR = TIM_EGR_UG;
- } else {
- ultrasonic_pulse_length = TIM11->CCR1;
- }
-}
+// TIM11.1 on PB9, aka digital input 6.
+timer_declare(TIM11, TIM1_TR_GCOM_TIM11_IRQHandler, 1, 1, ultrasonic)
void robot_init(void) {
gpio_setup_alt(GPIOB, 9, 3);
RCC->APB2ENR |= RCC_APB2ENR_TIM11EN;
- TIM11->CR1 = TIM_CR1_URS;
- TIM11->DIER = TIM_DIER_CC1IE;
TIM11->CCMR1 = TIM_CCMR1_CC1S_0 /* input pin 1 -> timer input 1 */;
- TIM11->CCER = TIM_CCER_CC1P | TIM_CCER_CC1NP | TIM_CCER_CC1E;
- TIM11->EGR = TIM_EGR_UG;
- TIM11->PSC = 1200 - 1; // 100kHZ timer
- TIM11->CR1 |= TIM_CR1_CEN;
- NVIC_SetPriority(TIM1_TRG_COM_TIM11_IRQn, 3);
- NVIC_EnableIRQ(TIM1_TRG_COM_TIM11_IRQn);
+ TIM11->PSC = 1200 - 1; // 100KHz timer
+ timer_setup(TIM11, TIM1_TRG_COM_TIM11_IRQn, 1);
}
void robot_fill_packet(struct DataStruct *packet) {
@@ -125,7 +111,7 @@
packet->main.battery_voltage_high = analog_get(5);
packet->main.battery_voltage_low = analog_get(3);
- packet->main.ultrasonic_pulse_length = ultrasonic_pulse_length;
+ packet->main.ultrasonic_pulse_length = ultrasonic_length;
fill_top_claw_values(packet);
fill_bottom_claw_values(packet);
diff --git a/bbb_cape/src/cape/timer.h b/bbb_cape/src/cape/timer.h
new file mode 100644
index 0000000..a356de1
--- /dev/null
+++ b/bbb_cape/src/cape/timer.h
@@ -0,0 +1,34 @@
+#ifndef CAPE_TIMER_H_
+#define CAPE_TIMER_H_
+
+#include <STM32F2XX.h>
+
+// inverted is 1 for timing low periods, 0 for high ones.
+#define timer_declare(timer, irq, input, inverted, name) \
+ static volatile uint32_t name##_length; \
+ void irq(void) { \
+ timer->SR = ~TIM_SR_CC##input##IF; \
+ const uint32_t ccer = timer->CCER; \
+ timer->CCER = ccer ^ (TIM_CCER_CC##input##P | TIM_CCER_CC##input##NP); \
+ const uint32_t rising = ccer & TIM_CCER_CC##input##P; \
+ if (inverted ? rising : !rising) { \
+ name##_length = timer->CCR##input; \
+ } else { \
+ timer->EGR = TIM_EGR_UG; \
+ } \
+ }
+
+// You need to enable the clock, set up the alt function for the input pin,
+// set the prescaler, and set up the timer input before calling this.
+#define timer_setup(timer, irq, input) \
+ do { \
+ timer->CR1 = TIM_CR1_URS; \
+ timer->DIER = TIM_DIER_CC##input##IE; \
+ timer->CCER = TIM_CCER_CC##input##P | TIM_CCER_CC##input##E; \
+ timer->EGR = TIM_EGR_UG; \
+ timer->CR1 |= TIM_CR1_CEN; \
+ NVIC_SetPriority(irq, 1); \
+ NVIC_EnableIRQ(irq); \
+ } while (0)
+
+#endif // CAPE_TIMER_H_