summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAtle H. Havsø <atle@havso.net>2014-07-24 11:11:32 +0200
committerAtle H. Havsø <atle@havso.net>2014-07-24 11:11:32 +0200
commit180e4e22fbfae91c83e2594b73c576244b940eba (patch)
treebcbfc85cfc5575f159bf4532ea7cb1c4372638a6
parente5d4b9bb19623c9e643faaaa26e064c891b22afe (diff)
non-working spi test.
-rw-r--r--drivers/hmc5983.h76
-rw-r--r--main.cpp77
2 files changed, 150 insertions, 3 deletions
diff --git a/drivers/hmc5983.h b/drivers/hmc5983.h
new file mode 100644
index 0000000..3d05e9c
--- /dev/null
+++ b/drivers/hmc5983.h
@@ -0,0 +1,76 @@
+#ifndef HMC5983_H
+#define HMC5983_H
+
+#include <spi/spi.h>
+#include <gpio/gpio.h>
+#include <os/time.h>
+
+class HMC5983 {
+ private:
+ Pin cs;
+ SPI_t& spi;
+ uint8_t raw_data[6];
+
+ void read_register(uint8_t reg, uint8_t len, uint8_t *data) {
+ cs.off();
+ if (len > 1) {
+ spi.transfer_byte(reg | (1 << 8) | (1 << 7));
+ } else {
+ spi.transfer_byte(reg | (1 << 8));
+ }
+ while(len--) {
+ *data++ = spi.transfer_byte();
+ }
+ cs.on();
+ }
+
+ void write_register(uint8_t reg, uint8_t data) {
+ cs.off();
+ spi.transfer_byte(reg);
+ spi.transfer_byte(data);
+ cs.on();
+ }
+ public:
+ float x, y, z;
+ uint8_t ident;
+ HMC5983(Pin cs_pin, SPI_t& spi_bus) : cs(cs_pin), spi(spi_bus) {}
+
+ void init() {
+ spi.reg.CR1 = (1 << 9) | (1 << 8) | (1 << 6) | (2 << 3) | (1 << 2) | (1 << 1) | (1 << 0);
+ spi.reg.CR2 = (1 << 12) | ( 7 << 8);
+ Time::sleep(100);
+ ident = 'O';
+ while(ident != 'H'){
+ read_register(0x0A, 1, &ident);
+ }
+ write_register(0x00, (1 << 7) | (1 << 5) | (2 << 2));
+ write_register(0x01, 0x00);
+ write_register(0x02, 0x00);
+ Time::sleep(600);
+ }
+
+ uint8_t update() {
+ uint16_t raw_x, raw_y, raw_z;
+ uint8_t status;
+ spi.reg.CR1 = (1 << 9) | (1 <<8) | (1 << 6) | (2 << 3) | (1 << 2) | (1 << 1) | (1 << 0);
+ spi.reg.CR2 = (1 << 12) | ( 7 << 8);
+ // Read status;
+ read_register(0x09, 1, &status);
+ if (status & 1) {
+ read_register(0x03, 6, raw_data);
+ raw_x = raw_data[0] << 8;
+ raw_x |= raw_data[1];
+ raw_z = raw_data[2] << 8;
+ raw_z |= raw_data[3];
+ raw_y = raw_data[4] << 8;
+ raw_y |= raw_data[5];
+
+ x = float(raw_x);
+ y = float(raw_y);
+ z = float(raw_z);
+ }
+ return status;
+ }
+};
+
+#endif
diff --git a/main.cpp b/main.cpp
index 24962f4..cba7c30 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,13 +1,29 @@
#include <rcc/rcc.h>
#include <os/time.h>
+#include <interrupt/interrupt.h>
+#include <timer/timer.h>
+
#include <gpio/gpio.h>
#include <usb/usb.h>
#include <usb/descriptor.h>
+#include "hmc5983.h"
+
Pin usb_vbus = GPIOB[5];
Pin usb_dm = GPIOA[11];
Pin usb_dp = GPIOA[12];
+Pin spi2_sck = GPIOB[13];
+Pin spi2_miso = GPIOB[14];
+Pin spi2_mosi = GPIOB[15];
+Pin bar_cs = GPIOC[13];
+Pin mag_cs = GPIOC[14];
+Pin mpu_cs = GPIOC[15];
+Pin flash_cs = GPIOB[2];
+
+Pin s1 = GPIOA[0];
+Pin s2 = GPIOA[1];
+
auto dev_desc = device_desc(0x200, 0, 0, 0, 64, 0x1234, 0x5678, 0, 0, 0, 0, 1);
auto conf_desc = configuration_desc(1, 1, 0, 0xc0, 0,
interface_desc(1, 0, 1, 0xff, 0x00, 0x00, 0,
@@ -41,6 +57,8 @@ class USB_TM : public USB_class_driver {
USB_TM usb_tm(usb);
+HMC5983 magn(mag_cs, SPI2);
+
int main() {
rcc_init();
@@ -49,6 +67,8 @@ int main() {
STK.CTRL = 0x03;
RCC.enable(RCC.GPIOA);
+ RCC.enable(RCC.GPIOB);
+ RCC.enable(RCC.GPIOC);
usb_dm.set_mode(Pin::AF);
usb_dm.set_af(14);
@@ -56,13 +76,64 @@ int main() {
usb_dp.set_af(14);
RCC.enable(RCC.USB);
-
usb.init();
+
+ //RCC.enable(RCC.SPI2);
+ /*
+ spi2_sck.set_mode(Pin::AF);
+ spi2_sck.set_af(5);
+ spi2_mosi.set_mode(Pin::AF);
+ spi2_mosi.set_af(5);
+ spi2_miso.set_mode(Pin::AF);
+ spi2_miso.set_af(5);
+ mag_cs.set_mode(Pin::Output);
+ mag_cs.on();
+ bar_cs.set_mode(Pin::Output);
+ bar_cs.on();
+ mpu_cs.set_mode(Pin::Output);
+ mpu_cs.on();
+ flash_cs.set_mode(Pin::Output);
+ flash_cs.on();
+
+ magn.init();
+ float buf[3] = {0,0,0};
+ */
+ s1.set_mode(Pin::AF);
+ s1.set_af(1);
+ s2.set_mode(Pin::AF);
+ s2.set_af(1);
+
+ RCC.enable(RCC.TIM8);
+
+ TIM8.PSC = 168 - 1;
+ TIM8.ARR = 2500;
+ TIM8.CCER = (1 << 12) | (1 << 8) | (1 << 4) | (1 << 0);
+ TIM8.CCMR1 = (6 << 12) | (1 << 11) | (6 << 4) | (1 << 3);
+ TIM8.CCMR2 = (6 << 12) | (1 << 11) | (6 << 4) | (1 << 3);
+ TIM8.BDTR = (1 << 15);
+ TIM8.CCR1 = 1000;
+ TIM8.CCR2 = 1000;
+ TIM8.CCR3 = 1000;
+ TIM8.CCR4 = 1000;
+ TIM8.CR1 = (1 << 2) | (1 << 0);
+
while(1) {
usb.process();
+ //uint8_t magn_status = magn.update();
+
+ //buf[0] = magn.x;
+ //buf[1] = magn.y;
+ //buf[2] = magn.z;
+ TIM8.CCR1 = 1000 + 100;
+ TIM8.CCR2 = 1000 + 100;
+ TIM8.CCR3 = 1000 + 100;
+ TIM8.CCR4 = 1000 + 100;
+
+
if(usb.ep_ready(1)) {
- char a = 'A';
- usb.write(1, (uint32_t*)&a, sizeof(a));
+ //usb.write(1, (uint32_t*)&magn.ident, sizeof(magn.ident));
+ //usb.write(1, (uint32_t*)buf, sizeof(buf));
+ //usb.write(1, (uint32_t*)&magn_status, sizeof(magn_status));
}
}
} \ No newline at end of file