added a single-byte uart interface
diff --git a/bbb_cape/src/cape/Makefile b/bbb_cape/src/cape/Makefile
index 101552a..244d160 100644
--- a/bbb_cape/src/cape/Makefile
+++ b/bbb_cape/src/cape/Makefile
@@ -32,12 +32,13 @@
OBJECTS_main := main \
uart_common \
uart_dma \
+ uart \
fill_packet \
cows \
OBJECTS_bootloader := bootloader \
uart_common \
- uart \
+ uart_byte \
OUTPUTS := main bootloader
diff --git a/bbb_cape/src/cape/fill_packet.c b/bbb_cape/src/cape/fill_packet.c
index fe0bb2c..e8516a9 100644
--- a/bbb_cape/src/cape/fill_packet.c
+++ b/bbb_cape/src/cape/fill_packet.c
@@ -5,6 +5,7 @@
#include <STM32F2XX.h>
#include "cape/uart_dma.h"
+#include "cape/uart_common.h"
#include "cape/cows.h"
static uint8_t buffer1[DATA_STRUCT_SEND_SIZE] __attribute__((aligned(4)));
@@ -39,5 +40,6 @@
void fill_packet_start(void) {
RCC->AHB1ENR |= RCC_AHB1ENR_CRCEN;
- uart_dma_configure(3000000, DATA_STRUCT_SEND_SIZE, buffer1, buffer2);
+ uart_common_configure(3000000);
+ uart_dma_configure(DATA_STRUCT_SEND_SIZE, buffer1, buffer2);
}
diff --git a/bbb_cape/src/cape/peripherial_usage.notes b/bbb_cape/src/cape/peripherial_usage.notes
index ee5c0a8..5f361f7 100644
--- a/bbb_cape/src/cape/peripherial_usage.notes
+++ b/bbb_cape/src/cape/peripherial_usage.notes
@@ -5,12 +5,15 @@
with choosing things like timers.
[BBB communication]
-USART1
-uart.[ch]
+uart_common
+ USART1
+uart
USART1_IRQ:3
-uart_dma.[ch]
+uart_dma
DMA2.7:2
DMA2.7_IRQ:6
+uart_byte
+ TIM7
[gyro communication]
SPI3
@@ -19,10 +22,10 @@
SPI2
[encoders]
-encoder.c
+encoder
TIM1,TIM2,TIM3,TIM4,TIM5,TIM8
[sensor packet sending]
-fill_packet.c
+fill_packet
TIM6
CRC
diff --git a/bbb_cape/src/cape/uart.c b/bbb_cape/src/cape/uart.c
index 45fa744..63620ab 100644
--- a/bbb_cape/src/cape/uart.c
+++ b/bbb_cape/src/cape/uart.c
@@ -43,8 +43,7 @@
}
}
-void uart_configure(int baud) {
- uart_common_configure(baud);
+void uart_configure(void) {
NVIC_SetPriority(USART1_IRQn, 3);
NVIC_EnableIRQ(USART1_IRQn);
}
diff --git a/bbb_cape/src/cape/uart.h b/bbb_cape/src/cape/uart.h
index 7ed24f4..fd43c99 100644
--- a/bbb_cape/src/cape/uart.h
+++ b/bbb_cape/src/cape/uart.h
@@ -6,8 +6,8 @@
// This file deals with USART1. It sends bytes from a buffer or receives bytes
// into a buffer and then calls a callback function.
-// See uart_common_configure in uart_common.h for details.
-void uart_configure(int baud);
+// uart_common_configure must be called before this.
+void uart_configure(void);
// Callbacks to be implemented by the user.
// Implemented as weak symbols that do nothing by default.
diff --git a/bbb_cape/src/cape/uart_byte.c b/bbb_cape/src/cape/uart_byte.c
new file mode 100644
index 0000000..76297e3
--- /dev/null
+++ b/bbb_cape/src/cape/uart_byte.c
@@ -0,0 +1,31 @@
+#include "cape/uart_byte.h"
+#include "cape/uart_common_private.h"
+
+#include <STM32F2XX.h>
+
+#define TIMEOUT_TIM TIM7
+
+void uart_byte_configure(void) {
+ TIMEOUT_TIM->CR1 = TIM_CR1_UDIS;
+}
+
+int uart_byte_receive(uint16_t timeout_count, uint16_t timeout_divider) {
+ TIMEOUT_TIM->PSC = timeout_divider;
+ TIMEOUT_TIM->EGR = TIM_EGR_UG;
+ TIMEOUT_TIM->CR1 |= TIM_CR1_CEN;
+
+ while ((UART->SR & USART_SR_RXNE) == 0) {
+ if (TIMEOUT_TIM->CNT >= timeout_count) {
+ TIMEOUT_TIM->CR1 &= ~TIM_CR1_CEN;
+ return -1;
+ }
+ }
+
+ TIMEOUT_TIM->CR1 &= ~TIM_CR1_CEN;
+ return UART->DR;
+}
+
+void uart_byte_send(uint8_t value) {
+ while ((UART->SR & USART_SR_TXE) == 0) {}
+ UART->DR = value;
+}
diff --git a/bbb_cape/src/cape/uart_byte.h b/bbb_cape/src/cape/uart_byte.h
new file mode 100644
index 0000000..7aa7f43
--- /dev/null
+++ b/bbb_cape/src/cape/uart_byte.h
@@ -0,0 +1,17 @@
+#ifndef CAPE_UART_BYTE_H_
+#define CAPE_UART_BYTE_H_
+
+#include <stdint.h>
+
+// uart_common_configure must be called before this.
+void uart_byte_configure(void);
+
+// Spins until 1 byte is received or some amount of time. The timeout is
+// timeout_count*timeout_divider/30MHz.
+// The result is <0 for timeout or the received byte.
+int uart_byte_receive(uint16_t timeout_count, uint16_t timeout_divider);
+
+// Spins until 1 byte can be written out.
+void uart_byte_send(uint8_t value);
+
+#endif // CAPE_UART_BYTE_H_
diff --git a/bbb_cape/src/cape/uart_dma.c b/bbb_cape/src/cape/uart_dma.c
index 4b2ec45..4d4da1c 100644
--- a/bbb_cape/src/cape/uart_dma.c
+++ b/bbb_cape/src/cape/uart_dma.c
@@ -37,10 +37,7 @@
}
}
-void uart_dma_configure(int baud, int bytes,
- uint8_t *buffer1_in, uint8_t *buffer2_in) {
- uart_common_configure(baud);
-
+void uart_dma_configure(int bytes, uint8_t *buffer1_in, uint8_t *buffer2_in) {
buffer1 = buffer1_in;
buffer2 = buffer2_in;
uart_dma_callback(buffer1);
diff --git a/bbb_cape/src/cape/uart_dma.h b/bbb_cape/src/cape/uart_dma.h
index 83e7637..cc68eed 100644
--- a/bbb_cape/src/cape/uart_dma.h
+++ b/bbb_cape/src/cape/uart_dma.h
@@ -12,10 +12,9 @@
// new_buffer is the buffer that should be filled out to be written next.
void uart_dma_callback(uint8_t *new_buffer);
-// See uart_common_configure in uart_common.h for details about baud.
+// uart_common_configure must be called before this.
// bytes is the size off buffer1 and buffer2.
// Calls uart_dma_callback twice (for each buffer) to get started.
-void uart_dma_configure(int baud, int bytes,
- uint8_t *buffer1, uint8_t *buffer2);
+void uart_dma_configure(int bytes, uint8_t *buffer1, uint8_t *buffer2);
#endif // CAPE_UART_DMA_H_