summaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2011-06-12 18:20:01 +0200
committerVegard Storheil Eriksen <zyp@jvnv.net>2011-06-12 18:20:01 +0200
commitc10db42abea2d1a0b3f6843d3563f39af183fa37 (patch)
treee3fa22ba3774ab8802755967ed8a121c90d3ffb8 /main.cpp
parent5364cdbeafa52c442c7a4a9b8ce9ac1db377fc00 (diff)
Enable USART and send messages via xbee.
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp49
1 files changed, 48 insertions, 1 deletions
diff --git a/main.cpp b/main.cpp
index 6bdabd0..179f2e8 100644
--- a/main.cpp
+++ b/main.cpp
@@ -71,10 +71,53 @@ void i2c_read_reg(uint8_t addr, uint8_t reg, uint8_t len, uint8_t* buf) {
}
}
+void usart_send(uint8_t data) {
+ while(!(USART1.SR & 0x80)); // Wait for TXE.
+
+ USART1.DR = data;
+}
+
+void xbee_send(int len, const uint8_t* buf) {
+ // Start and length.
+ usart_send(0x7e);
+ usart_send(((len + 14) >> 8) & 0xff);
+ usart_send((len + 14) & 0xff);
+
+ // Frame type and ID.
+ usart_send(0x10);
+ usart_send(0x01);
+
+ // Destination address.
+ usart_send(0x00);
+ usart_send(0x13);
+ usart_send(0xa2);
+ usart_send(0x00);
+ usart_send(0x40);
+ usart_send(0x6f);
+ usart_send(0x19);
+ usart_send(0xf1);
+
+ usart_send(0x00);
+ usart_send(0x00);
+ usart_send(0x00);
+ usart_send(0x00);
+
+ uint8_t chsum = 0x80;
+
+ // Payload
+ for(int i = 0; i < len; i++) {
+ usart_send(buf[i]);
+ chsum -= buf[i];
+ }
+
+ usart_send(chsum);
+}
+
int main() {
- RCC.APB2ENR |= 0xd;
+ RCC.APB2ENR |= 0x400d;
GPIOA.CRL = 0x44344444;
+ GPIOA.CRH = 0x444444b4;
GPIOA.ODR = 1 << 5;
GPIOB.CRH = 0x4444ff44;
@@ -99,6 +142,9 @@ int main() {
uint8_t buf[6];
+ USART1.BRR = 7500; // 9600 baud
+ USART1.CR1 = 0x2008;
+
while(1) {
cnt++;
if(cnt & (1 << 20)) {
@@ -109,6 +155,7 @@ int main() {
if(!(cnt & ((1 << 20) - 1))) {
i2c_read_reg(0x68, 0x1d, 6, buf);
+ xbee_send(6, buf);
}
}
}