diff options
-rw-r--r-- | main.cpp | 49 |
1 files changed, 48 insertions, 1 deletions
@@ -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); } } } |