summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt5
-rw-r--r--src/demo.c117
-rw-r--r--src/usart_helpers.c144
-rw-r--r--src/usart_helpers.h13
4 files changed, 79 insertions, 200 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b97355d..7b28316 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -15,7 +15,10 @@ project (libusbhost C)
set (USE_STM32F4_FS TRUE CACHE BOOL "Use USB full speed (FS) host periphery")
set (USE_STM32F4_HS TRUE CACHE BOOL "Use USB high speed (HS) host periphery")
-set (USE_USART_DEBUG TRUE CACHE BOOL "Use debug uart output")
+set (USE_USART_DEBUG TRUE CACHE BOOL "Enable human-readable serial debug output")
+set (DEBUG_USART USART1 CACHE STRING "USART to use for debug output")
+set (DEBUG_USART_DMA DMA2 CACHE STRING "DMA controller to use for debug usart")
+set (DEBUG_USART_DMA_STREAM_NUM 7 CACHE STRING "DMA stream number to use for debug usart. This must be the stream mapped to the [DEBUG_USART]_TX channel")
# Set compiler and linker flags
diff --git a/src/demo.c b/src/demo.c
index 4228616..d61460f 100644
--- a/src/demo.c
+++ b/src/demo.c
@@ -38,38 +38,30 @@
#include <string.h>
#include <stdlib.h>
+#define UNUSED(var) ((void)var)
-static inline void delay_ms_busy_loop(uint32_t ms)
-{
- volatile uint32_t i;
- for (i = 0; i < 14903*ms; i++);
+static inline void delay_ms_busy_loop(uint32_t ms) {
+ for (volatile uint32_t i = 0; i < 14903*ms; i++);
}
/* Set STM32 to 168 MHz. */
-static void clock_setup(void)
-{
+static void clock_setup(void) {
rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]);
- // GPIO
- rcc_periph_clock_enable(RCC_GPIOA); // USART + OTG_FS + button
- rcc_periph_clock_enable(RCC_GPIOB); // OTG_HS
- rcc_periph_clock_enable(RCC_GPIOC); // OTG_FS charge pump
- rcc_periph_clock_enable(RCC_GPIOD); // LEDS
-
- // periphery
- rcc_periph_clock_enable(RCC_USART1); // USART
- rcc_periph_clock_enable(RCC_OTGFS); // OTG_FS
- rcc_periph_clock_enable(RCC_OTGHS); // OTG_HS
- rcc_periph_clock_enable(RCC_TIM6); // TIM6
+ rcc_periph_clock_enable(RCC_GPIOA);
+ rcc_periph_clock_enable(RCC_GPIOE);
+
+ rcc_periph_clock_enable(RCC_USART1);
+ rcc_periph_clock_enable(RCC_USART2);
+ rcc_periph_clock_enable(RCC_OTGFS);
+ rcc_periph_clock_enable(RCC_OTGHS);
+ rcc_periph_clock_enable(RCC_TIM6);
}
-/*
- * setup 10kHz timer
- */
-static void tim6_setup(void)
-{
+/* setup 10kHz timer */
+static void tim6_setup(void) {
timer_reset(TIM6);
timer_set_prescaler(TIM6, 8400 - 1); // 84Mhz/10kHz - 1
timer_set_period(TIM6, 65535); // Overflow in ~6.5 seconds
@@ -88,28 +80,23 @@ static uint32_t tim6_get_time_us(void)
static void gpio_setup(void)
{
- /* Set GPIO12-15 (in GPIO port D) to 'output push-pull'. */
- gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT,
- GPIO_PUPD_NONE, GPIO12 | GPIO13 | GPIO14 | GPIO15);
+ /* D2, D3 LEDs */
+ gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO6 | GPIO7);
- /* Set */
- gpio_mode_setup(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO0);
- gpio_clear(GPIOC, GPIO0);
-
- // OTG_FS
+ /* USB OTG FS phy outputs */
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12);
gpio_set_af(GPIOA, GPIO_AF10, GPIO11 | GPIO12);
- // OTG_HS
- gpio_mode_setup(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO15 | GPIO14);
- gpio_set_af(GPIOB, GPIO_AF12, GPIO14 | GPIO15);
-
- // USART
+ /* USART1 (debug) */
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9 | GPIO10);
gpio_set_af(GPIOA, GPIO_AF7, GPIO9 | GPIO10);
- // button
- gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO0);
+ /* USART2 (host link) */
+ gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2 | GPIO3);
+ gpio_set_af(GPIOA, GPIO_AF7, GPIO2 | GPIO3);
+
+ /* K0 (PE4)/K1 (PE3) buttons */
+ gpio_mode_setup(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO3 | GPIO4);
}
static const usbh_dev_driver_t *device_drivers[] = {
@@ -119,20 +106,14 @@ static const usbh_dev_driver_t *device_drivers[] = {
};
static const usbh_low_level_driver_t * const lld_drivers[] = {
-#ifdef USE_STM32F4_USBH_DRIVER_FS
&usbh_lld_stm32f4_driver_fs, // Make sure USE_STM32F4_USBH_DRIVER_FS is defined in usbh_config.h
-#endif
-
-#ifdef USE_STM32F4_USBH_DRIVER_HS
- &usbh_lld_stm32f4_driver_hs, // Make sure USE_STM32F4_USBH_DRIVER_HS is defined in usbh_config.h
-#endif
NULL
- };
+};
static void hid_in_message_handler(uint8_t device_id, const uint8_t *data, uint32_t length)
{
- (void)device_id;
- (void)data;
+ UNUSED(device_id);
+ UNUSED(data);
if (length < 4) {
LOG_PRINTF("data too short, type=%d\n", hid_get_type(device_id));
return;
@@ -143,11 +124,6 @@ static void hid_in_message_handler(uint8_t device_id, const uint8_t *data, uint3
LOG_PRINTF("HID EVENT %02X %02X %02X %02X \n", data[0], data[1], data[2], data[3]);
/*
if (hid_get_type(device_id) == HID_TYPE_KEYBOARD) {
- static int x = 0;
- if (x != data[2]) {
- x = data[2];
- hid_set_report(device_id, x);
- }
}
*/
}
@@ -161,24 +137,17 @@ int main(void)
clock_setup();
gpio_setup();
- // provides time_curr_us to usbh_poll function
+ /* provides time_curr_us to usbh_poll function */
tim6_setup();
-#ifdef USART_DEBUG
- //USART_BRR(USART1) = rcc_apb2_frequency / (16 * 1000000);
- usart_init(USART1, 1000000);
-#endif
- LOG_PRINTF("\n\n\n\n\n###################\nInit\n");
+ usart_init(USART2, 1000000);
+ debug_usart_init();
+
+ LOG_PRINTF("SecureHID device side initializing");
- /**
- * device driver initialization
- *
- * Pass configuration struct where the callbacks are defined
- */
hid_driver_init(&hid_config);
hub_driver_init();
- gpio_set(GPIOD, GPIO13);
/**
* Pass array of supported low level drivers
* In case of stm32f407, there are up to two supported OTG hosts on one chip.
@@ -187,28 +156,12 @@ int main(void)
* Pass array of supported device drivers
*/
usbh_init(lld_drivers, device_drivers);
- gpio_clear(GPIOD, GPIO13);
LOG_PRINTF("USB init complete\n");
- LOG_FLUSH();
-
- while (1) {
- // set busy led
- gpio_set(GPIOD, GPIO14);
-
- uint32_t time_curr_us = tim6_get_time_us();
-
- usbh_poll(time_curr_us);
-
- // clear busy led
- gpio_clear(GPIOD, GPIO14);
-
- LOG_FLUSH();
-
- // approx 1ms interval between usbh_poll()
- delay_ms_busy_loop(1);
+ while (23) {
+ usbh_poll(tim6_get_time_us());
+ delay_ms_busy_loop(1); /* approx 1ms interval between usbh_poll() */
}
-
return 0;
}
diff --git a/src/usart_helpers.c b/src/usart_helpers.c
index fc32333..97aa0f0 100644
--- a/src/usart_helpers.c
+++ b/src/usart_helpers.c
@@ -32,6 +32,8 @@
#include <stdio.h>
#include <libopencm3/stm32/usart.h>
+uint32_t debug_usart = 0;
+
#define USART_FIFO_OUT_SIZE (4096)
uint8_t usart_fifo_out_data[USART_FIFO_OUT_SIZE];
uint32_t usart_fifo_out_len = 0;
@@ -128,118 +130,46 @@ void usart_init(uint32_t arg_usart, uint32_t baudrate)
usart_set_mode(arg_usart, USART_MODE_TX | USART_MODE_RX);
usart_set_parity(arg_usart, USART_PARITY_NONE);
usart_set_stopbits(arg_usart, USART_STOPBITS_1);
-
- usart_enable_rx_interrupt(arg_usart);
usart_enable(arg_usart);
- usart = arg_usart;
}
-void usart_interrupt(void)
-{
- if (usart_get_interrupt_source(usart, USART_SR_RXNE)) {
- uint8_t data = usart_recv(usart);
- usart_fifo_in_push(data);
- if ( data != 3 && data != '\r' && data != '\n') {
- usart_fifo_push(data);
- } else {
- LOG_PRINTF("\n>>");
- }
- }
+#define WRITE_BUF_LEN 256
+struct tx_buf {
+ buf[WRITE_BUF_LEN];
+ int pos;
+} tx_buf[2];
+int tx_buf_active;
+
+#define DEBUG_USART_DMA_STREAM (DMA_STREAM##DEBUG_USART_DMA_STREAM_NUM)
+#define DEBUG_USART_NVIC_DMA_IRQ (NVIC_##DEBUG_USART_DMA##_##DEBUG_USART_DMA_STREAM##_IRQ)
+#define DEBUG_USART_DMA_ISR (DEBUG_USART_DMA##_##DEBUG_USART_DMA_STREAM##_IRQHandler)
+void debug_usart_init() {
+ tx_buf[0].pos = tx_buf[1].pos = 0;
+ tx_buf_active = 1;
+
+ usart_init(DEBUG_USART, DEBUG_BAUDRATE);
+
+ dma_stream_reset(DEBUG_USART_DMA, DEBUG_USART_DMA_STREAM);
+ dma_set_peripheral_address(DEBUG_USART_DMA, DEBUG_USART_DMA_STREAM, (uint32_t)&DEBUG_USART##_DR);
+ dma_set_memory_address(DEBUG_USART_DMA, DEBUG_USART_DMA_STREAM, (uint32_t)tx_buf[1].buf);
+ dma_set_number_of_data(DEBUG_USART_DMA, DEBUG_USART_DMA_STREAM, size);
+ dma_set_read_from_memory(DEBUG_USART_DMA, DEBUG_USART_DMA_STREAM);
+ dma_enable_memory_increment_mode(DEBUG_USART_DMA, DEBUG_USART_DMA_STREAM);
+ dma_set_peripheral_size(DEBUG_USART_DMA, DEBUG_USART_DMA_STREAM, DMA_CCR_PSIZE_8BIT);
+ dma_set_memory_size(DEBUG_USART_DMA, DEBUG_USART_DMA_STREAM, DMA_CCR_MSIZE_8BIT);
+ dma_set_priority(DEBUG_USART_DMA, DEBUG_USART_DMA_STREAM, DMA_CCR_PL_VERY_HIGH);
+ dma_enable_transfer_complete_interrupt(DEBUG_USART_DMA, DEBUG_USART_DMA_STREAM);
+ dma_enable_channel(DEBUG_USART_DMA, DEBUG_USART_DMA_STREAM);
+ usart_enable_tx_dma(DEBUG_USART);
+ nvic_enable_irq(DEBUG_USART_NVIC_DMA_IRQ);
}
-void usart_fifo_send(void)
-{
- while(usart_fifo_out_len) {
- uint8_t data = usart_fifo_pop();
- usart_wait_send_ready(usart);
- usart_send(usart, data);
- }
-}
-static char command[128];
-static uint8_t command_len = 0;
-static uint8_t command_argindex = 0;
+void DEBUG_USART_DMA_ISR
-static uint8_t usart_read_command(void)
-{
- uint32_t fifo_len = usart_fifo_in_len;
- while (fifo_len) {
- uint8_t data = usart_fifo_in_pop();
-
- if ((data >= 'A') && (data <= 'Z')) {
- data += 'a'-'A';
- }
-
- if (((data >= 'a') && (data <= 'z')) || ((data >='0') && (data<='9'))) {
- command[command_len++] = data;
- } else if (data == ' ') {
- if (command_len) {
- if (command_argindex == 0) {
- command[command_len++] = 0;
- command_argindex = command_len;
- } else {
- command[command_len++] = ' ';
- }
- }
- } else if (data == '\r' || data == '\n') {
- if (command_len) {
- command[command_len++] = 0;
- if (!command_argindex) {
- command_argindex = command_len;
- }
- return 1;
- }
- } else if (data == 127) {
- if (command_len) {
- if (command_argindex) {
- if (command_len == command_argindex) {
- command_argindex = 0;
- }
- }
- command[command_len] = '\0';
- command_len--;
- }
- } else if (data == 3) {
- command_len = 0;
- command_argindex = 0;
- } else {
- LOG_PRINTF("%d ",data);
- }
-
- fifo_len--;
- }
- return 0;
+void usart_fifo_push(uint8_t c) {
+ struct tx_buf *buf = &tx_buf[!tx_buf_active]; /* select inactive buffer */
+ buf->buf[buf->pos++] = c;
+ nvic_disable_irq(DEBUG_USART_NVIC_DMA_IRQ);
+ if
}
-void usart_call_cmd(struct usart_commands * commands)
-{
- uint32_t i = 0;
- if(!usart_read_command()) {
- return;
- }
- if (!command_len) {
- LOG_PRINTF("#2");
- return;
- }
- i=0;
- while(commands[i].cmd != NULL) {
- if (!strcmp((char*)command, (char*)commands[i].cmd)) {
- if (commands[i].callback) {
- if(command_argindex == command_len) {
- commands[i].callback(NULL);
- } else {
- commands[i].callback(&command[command_argindex]);
- }
- }
- LOG_PRINTF("\n>>");
- command_len = 0;
- command_argindex = 0;
- return;
- } else {
-
- }
- i++;
- }
- command_len = 0;
- command_argindex = 0;
- LOG_PRINTF("INVALID COMMAND\n>>");
-}
diff --git a/src/usart_helpers.h b/src/usart_helpers.h
index e62f67d..445a477 100644
--- a/src/usart_helpers.h
+++ b/src/usart_helpers.h
@@ -29,24 +29,17 @@
BEGIN_DECLS
-struct usart_commands{
- const char * cmd;
- void (*callback)(const char * arg);
-};
-
-
#ifdef USART_DEBUG
void usart_init(uint32_t usart, uint32_t baudrate);
void usart_printf(const char *str, ...);
void usart_fifo_send(void);
-
-void usart_call_cmd(struct usart_commands * commands);
void usart_interrupt(void);
+
+void debug_usart_init() {
+
#define LOG_PRINTF(format, ...) usart_printf(format, ##__VA_ARGS__);
-#define LOG_FLUSH() usart_fifo_send()
#else
#define LOG_PRINTF(dummy, ...) ((void)dummy)
-#define LOG_FLUSH()
#endif
END_DECLS