diff options
-rw-r--r-- | hal/usart.cpp | 7 | ||||
-rw-r--r-- | hal/usart.h | 12 | ||||
-rw-r--r-- | xbee.cpp | 51 | ||||
-rw-r--r-- | xbee.h | 49 |
4 files changed, 65 insertions, 54 deletions
diff --git a/hal/usart.cpp b/hal/usart.cpp new file mode 100644 index 0000000..c446302 --- /dev/null +++ b/hal/usart.cpp @@ -0,0 +1,7 @@ +#include "usart.h" + +template<> +void interrupt<Interrupt::USART1>() { + USART1.DR; + //GPIOB.ODR ^= 1 << 1; +} diff --git a/hal/usart.h b/hal/usart.h index 8fde39a..484d89e 100644 --- a/hal/usart.h +++ b/hal/usart.h @@ -1,13 +1,11 @@ #ifndef USART_H #define USART_H -template<> -void interrupt<Interrupt::USART1>() { - USART1.DR; - //GPIOB.ODR ^= 1 << 1; -} +#include "stm32.h" +#include "interrupt.h" +#include "thread.h" -void usart_enable() { +inline void usart_enable() { RCC.enable(RCC.USART1); USART1.BRR = 625; // 115200 baud USART1.CR1 = 0x202c; @@ -15,7 +13,7 @@ void usart_enable() { Interrupt::enable(Interrupt::USART1); } -void usart_send(uint8_t data) { +inline void usart_send(uint8_t data) { while(!(USART1.SR & 0x80)) { Thread::yield(); } // Wait for TXE. diff --git a/xbee.cpp b/xbee.cpp new file mode 100644 index 0000000..efee619 --- /dev/null +++ b/xbee.cpp @@ -0,0 +1,51 @@ +#include "xbee.h" + +#include "usart.h" +#include "mutex.h" + +Mutex xbee_mutex; + +void xbee_send(uint16_t type, int len, const uint8_t* buf) { + xbee_mutex.lock(); + + // Start and length. + usart_send(0x7e); + usart_send(((len + 16) >> 8) & 0xff); + usart_send((len + 16) & 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(0xff); + usart_send(0xfe); + usart_send(0x00); + usart_send(0x00); + + uint8_t chsum = 0x83; + + usart_send(type & 0xff); + chsum -= type & 0xff; + usart_send(type >> 8); + chsum -= type >> 8; + + // Payload + for(int i = 0; i < len; i++) { + usart_send(buf[i]); + chsum -= buf[i]; + } + + usart_send(chsum); + + xbee_mutex.unlock(); +} @@ -1,53 +1,8 @@ #ifndef XBEE_H #define XBEE_H -#include "mutex.h" +#include <stdint.h> -Mutex xbm; - -void xbee_send(uint16_t type, int len, const uint8_t* buf) { - xbm.lock(); - - // Start and length. - usart_send(0x7e); - usart_send(((len + 16) >> 8) & 0xff); - usart_send((len + 16) & 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(0xff); - usart_send(0xfe); - usart_send(0x00); - usart_send(0x00); - - uint8_t chsum = 0x83; - - usart_send(type & 0xff); - chsum -= type & 0xff; - usart_send(type >> 8); - chsum -= type >> 8; - - // Payload - for(int i = 0; i < len; i++) { - usart_send(buf[i]); - chsum -= buf[i]; - } - - usart_send(chsum); - - xbm.unlock(); -} +void xbee_send(uint16_t type, int len, const uint8_t* buf); #endif |