summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--i2c.cpp74
-rw-r--r--main.cpp27
-rw-r--r--openocd.cfg2
-rw-r--r--ppmsum.cpp36
-rw-r--r--ppmsum.h3
5 files changed, 75 insertions, 67 deletions
diff --git a/i2c.cpp b/i2c.cpp
index 90c0ce1..77eb67a 100644
--- a/i2c.cpp
+++ b/i2c.cpp
@@ -3,71 +3,71 @@
#include "stm32.h"
void I2C::enable() {
- RCC.enable(RCC.I2C2);
+ RCC.enable(RCC.I2C1);
asm volatile("nop");
- I2C2.CR1 = 0x8000;
- I2C2.CR1 = 0;
+ I2C1.CR1 = 0x8000;
+ I2C1.CR1 = 0;
- I2C2.CR2 = 36;
- I2C2.TRISE = 37;
- I2C2.CCR = 180;
- I2C2.CR1 = 1;
+ I2C1.CR2 = 36;
+ I2C1.TRISE = 37;
+ I2C1.CCR = 180;
+ I2C1.CR1 = 1;
}
void I2C::write_reg(uint8_t addr, uint8_t reg, uint8_t data) {
- I2C2.CR1 |= 0x100;
- while(!(I2C2.SR1 & 0x01));
+ I2C1.CR1 |= 0x100;
+ while(!(I2C1.SR1 & 0x01));
- I2C2.DR = (addr << 1) | 0;
- while (!(I2C2.SR1 & 0x02));
- I2C2.SR2;
+ I2C1.DR = (addr << 1) | 0;
+ while (!(I2C1.SR1 & 0x02));
+ I2C1.SR2;
- I2C2.DR = reg;
- while (!(I2C2.SR1 & 0x80));
+ I2C1.DR = reg;
+ while (!(I2C1.SR1 & 0x80));
- I2C2.DR = data;
- while (!(I2C2.SR1 & 0x04));
+ I2C1.DR = data;
+ while (!(I2C1.SR1 & 0x04));
- I2C2.CR1 |= 0x200;
+ I2C1.CR1 |= 0x200;
}
void I2C::read_reg(uint8_t addr, uint8_t reg, uint8_t len, uint8_t* buf) {
- I2C2.CR1 |= 0x100;
- while(!(I2C2.SR1 & 0x01));
+ I2C1.CR1 |= 0x100;
+ while(!(I2C1.SR1 & 0x01));
- I2C2.DR = (addr << 1) | 0;
- while (!(I2C2.SR1 & 0x02));
- I2C2.SR2;
+ I2C1.DR = (addr << 1) | 0;
+ while (!(I2C1.SR1 & 0x02));
+ I2C1.SR2;
- I2C2.DR = reg;
- while (!(I2C2.SR1 & 0x80));
+ I2C1.DR = reg;
+ while (!(I2C1.SR1 & 0x80));
- I2C2.CR1 |= 0x100;
- while(!(I2C2.SR1 & 0x01));
+ I2C1.CR1 |= 0x100;
+ while(!(I2C1.SR1 & 0x01));
- I2C2.DR = (addr << 1) | 1;
- while (!(I2C2.SR1 & 0x02));
- I2C2.SR2;
+ I2C1.DR = (addr << 1) | 1;
+ while (!(I2C1.SR1 & 0x02));
+ I2C1.SR2;
- I2C2.CR1 |= 0x400; // Set ACK.
+ I2C1.CR1 |= 0x400; // Set ACK.
while(len) {
if(len == 3) {
- while (!(I2C2.SR1 & 0x04)); // Wait for BTF
+ while (!(I2C1.SR1 & 0x04)); // Wait for BTF
- I2C2.CR1 &= ~0x400; // Clear ACK.
- *buf++ = I2C2.DR;
+ I2C1.CR1 &= ~0x400; // Clear ACK.
+ *buf++ = I2C1.DR;
len--;
- I2C2.CR1 |= 0x200; // Set STOP.
- *buf++ = I2C2.DR;
+ I2C1.CR1 |= 0x200; // Set STOP.
+ *buf++ = I2C1.DR;
len--;
} else {
- while (!(I2C2.SR1 & 0x40)); // Wait for RXNE
+ while (!(I2C1.SR1 & 0x40)); // Wait for RXNE
- *buf++ = I2C2.DR;
+ *buf++ = I2C1.DR;
len--;
}
}
diff --git a/main.cpp b/main.cpp
index 72c0389..d3c14b3 100644
--- a/main.cpp
+++ b/main.cpp
@@ -57,11 +57,12 @@ int main() {
RCC.enable(RCC.IOPA);
RCC.enable(RCC.IOPB);
- GPIOA.CRL = 0x4434bbbb;
+ GPIOA.CRL = 0x4444bbbb;
GPIOA.CRH = 0x444444b4;
- GPIOA.ODR = 1 << 5;
- GPIOB.CRH = 0x4444ff44;
+ GPIOB.CRL = 0xff444434;
+ GPIOB.CRH = 0x44444444;
+ GPIOB.ODR = 1 << 1;
I2C i2c;
i2c.enable();
@@ -81,9 +82,9 @@ int main() {
TIM2.CR1 = 0x05;
- PID pid_pitch(3000, 0, 0);
- PID pid_roll(3000, 0, 0);
- PID pid_yaw(2000, 0, 0);
+ PID pid_pitch(6000, 0, 0);
+ PID pid_roll(6000, 0, 0);
+ PID pid_yaw(6000, 0, 0);
while(1) {
// Wait for a new update.
@@ -97,9 +98,9 @@ int main() {
// Generate motor mix.
int16_t throttle = ppmsum.channels[2] - 1000;
- int16_t pitch = pid_pitch.update((ppmsum.channels[1] - 1500) * 3 - gyro.x);
- int16_t roll = pid_roll.update((ppmsum.channels[0] - 1500) * 3 - gyro.y);
- int16_t yaw = pid_yaw.update((ppmsum.channels[3] - 1500) * 5 - gyro.z);
+ int16_t pitch = pid_pitch.update((ppmsum.channels[1] - 1500) * 1 - gyro.x);
+ int16_t roll = pid_roll.update((ppmsum.channels[0] - 1500) * 1 - gyro.y);
+ int16_t yaw = pid_yaw.update((ppmsum.channels[3] - 1500) * -1 - gyro.z);
int16_t max = throttle > 250 ? 250 : throttle;
saturate(pitch, max);
@@ -107,10 +108,10 @@ int main() {
saturate(yaw, max);
int cmds[] = {
- 1000 + throttle + pitch + roll + yaw,
- 1000 + throttle + pitch - roll - yaw,
- 1000 + throttle - pitch + roll - yaw,
- 1000 + throttle - pitch - roll + yaw,
+ 1000 + throttle + pitch - roll + yaw,
+ 1000 + throttle - pitch - roll - yaw,
+ 1000 + throttle - pitch + roll + yaw,
+ 1000 + throttle + pitch + roll - yaw,
};
TIM2.CCR1 = limit(cmds[0], 1000, 2000);
diff --git a/openocd.cfg b/openocd.cfg
index 2343753..1321413 100644
--- a/openocd.cfg
+++ b/openocd.cfg
@@ -9,7 +9,7 @@ ft2232_device_desc "USB<=>JTAG&RS232"
reset_config trst_and_srst
-source [find target/stm32.cfg]
+source [find target/stm32f1x.cfg]
proc flash_chip {} {
halt
diff --git a/ppmsum.cpp b/ppmsum.cpp
index 75faa17..927efa8 100644
--- a/ppmsum.cpp
+++ b/ppmsum.cpp
@@ -3,18 +3,23 @@
PPMSum* PPMSum::self = 0;
template<>
-void interrupt<Interrupt::TIM4>() {
+void interrupt<Interrupt::TIM1_UP>() {
+ PPMSum::self->irq();
+}
+
+template<>
+void interrupt<Interrupt::TIM1_CC>() {
PPMSum::self->irq();
}
void PPMSum::irq() {
- int16_t sr = TIM4.SR;
- TIM4.SR = 0;
+ int16_t sr = TIM1.SR;
+ TIM1.SR = 0;
if(sr & 0x06) {
- GPIOA.ODR = 1 << 5;
+ GPIOB.ODR = 1 << 1;
} else {
- GPIOA.ODR = 0;
+ GPIOB.ODR = 0;
}
@@ -26,7 +31,7 @@ void PPMSum::irq() {
} else if(sr & 0x02) {
// Period.
- if(TIM4.CCR1 > 5000) {
+ if(TIM1.CCR1 > 5000) {
synced = true;
index = 0;
} else {
@@ -36,19 +41,20 @@ void PPMSum::irq() {
} else if(sr & 0x04) {
// Pulsewidth.
- channels[index] = TIM4.CCR2;
+ channels[index] = TIM1.CCR2;
}
}
void PPMSum::enable() {
- RCC.enable(RCC.TIM4);
- TIM4.PSC = 72;
- TIM4.CCMR1 = 0x0201;
- TIM4.SMCR = 0x54;
- TIM4.CCER = 0x31;
- TIM4.DIER = 0x07;
+ RCC.enable(RCC.TIM1);
+ TIM1.PSC = 72;
+ TIM1.CCMR1 = 0x0201;
+ TIM1.SMCR = 0x54;
+ TIM1.CCER = 0x31;
+ TIM1.DIER = 0x07;
- Interrupt::enable(Interrupt::TIM4);
+ Interrupt::enable(Interrupt::TIM1_UP);
+ Interrupt::enable(Interrupt::TIM1_CC);
- TIM4.CR1 = 0x05;
+ TIM1.CR1 = 0x05;
}
diff --git a/ppmsum.h b/ppmsum.h
index efc7287..90bb860 100644
--- a/ppmsum.h
+++ b/ppmsum.h
@@ -5,7 +5,8 @@
#include "interrupt.h"
class PPMSum {
- friend void interrupt<Interrupt::TIM4>();
+ friend void interrupt<Interrupt::TIM1_UP>();
+ friend void interrupt<Interrupt::TIM1_CC>();
private:
static PPMSum* self;