From bec6f263662c1bb41d6e06ea7131da9937525dfd Mon Sep 17 00:00:00 2001 From: tkl Date: Wed, 17 Aug 2016 21:51:00 +0200 Subject: [PATCH] uart - rx in it mode, tx in blocking mode --- .cproject | 15 +++++- .../arch/stm32f4xx/driver/stm32f4_uart.c | 49 ++++++++++++++++++- source/test/shell/main.c | 43 ++++++++++++++++ source/test/shell/shell.mk | 1 + 4 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 source/test/shell/main.c create mode 100644 source/test/shell/shell.mk diff --git a/.cproject b/.cproject index 41f4393..aadf958 100755 --- a/.cproject +++ b/.cproject @@ -146,6 +146,7 @@ make + all true true @@ -153,6 +154,7 @@ make + clean true true @@ -160,7 +162,6 @@ make - distclean true false @@ -216,6 +217,7 @@ make + all true true @@ -247,6 +249,7 @@ make + distclean true true @@ -276,6 +279,14 @@ false true + + make + TEST_APP=shell BOARD=stm32f4-discovery DEBUG=y + test + true + false + true + make APP=example_radio_rx BOARD=msp430-ccrf DEBUG=y @@ -302,6 +313,7 @@ make + all true true @@ -309,6 +321,7 @@ make + clean true true diff --git a/source/firmware/arch/stm32f4xx/driver/stm32f4_uart.c b/source/firmware/arch/stm32f4xx/driver/stm32f4_uart.c index e2f8967..27f5c5f 100644 --- a/source/firmware/arch/stm32f4xx/driver/stm32f4_uart.c +++ b/source/firmware/arch/stm32f4xx/driver/stm32f4_uart.c @@ -19,13 +19,29 @@ struct stm32f4_uart_obj { UART_HandleTypeDef *uart2_handle; UART_HandleTypeDef *uart3_handle; UART_HandleTypeDef *uart6_handle; + const void *uart1_callback; //!< Interrupt callback. + const void *uart1_parameter; //!< argument for the callback. + const void *uart2_callback; //!< Interrupt callback. + const void *uart2_parameter; //!< argument for the callback. + const void *uart3_callback; //!< Interrupt callback. + const void *uart3_parameter; //!< argument for the callback. + const void *uart6_callback; //!< Interrupt callback. + const void *uart6_parameter; //!< argument for the callback. }; static volatile struct stm32f4_uart_obj uart_obj = { .uart1_handle = NULL, + .uart1_callback = NULL, + .uart1_parameter = NULL, .uart2_handle = NULL, + .uart2_callback = NULL, + .uart2_parameter = NULL, .uart3_handle = NULL, + .uart3_callback = NULL, + .uart3_parameter = NULL, .uart6_handle = NULL, + .uart6_callback = NULL, + .uart6_parameter = NULL, }; int stm32f4_uart_open(const void *this) @@ -63,6 +79,8 @@ int stm32f4_uart_open(const void *this) HAL_UART_Init(uart->uart_handle); HAL_NVIC_SetPriority(irq_type, 5, 1); HAL_NVIC_EnableIRQ(USART1_IRQn); + __HAL_UART_ENABLE_IT(uart->uart_handle, UART_IT_RXNE); + __HAL_UART_DISABLE_IT(uart->uart_handle, UART_IT_TXE); return (0); } @@ -99,6 +117,8 @@ int stm32f4_uart_close(const void *this) int stm32f4_uart_read(const void *this, char *buffer, int len) { + struct stm32f4_uart *uart = (struct stm32f4_uart *)this; + *buffer = uart->uart_handle->Instance->DR; return (1); } @@ -107,12 +127,29 @@ int stm32f4_uart_write(const void *this, const char *buffer, int len) if(NULL == this) return -1; struct stm32f4_uart *uart = (struct stm32f4_uart *)this; - HAL_UART_Transmit_IT(uart->uart_handle, (uint8_t *)buffer, len); + HAL_UART_Transmit(uart->uart_handle, (uint8_t *)buffer, len, 1000); return len; } int stm32f4_uart_set_cb(const void *this, const void *callback, const void *param) { + struct stm32f4_uart *uart = (struct stm32f4_uart *)this; + if(uart->uart_handle->Instance == USART1) { + uart_obj.uart1_callback = callback; + uart_obj.uart1_parameter = param; + } + else if(uart->uart_handle->Instance == USART2) { + uart_obj.uart2_callback = callback; + uart_obj.uart2_parameter = param; + } + else if(uart->uart_handle->Instance == USART3) { + uart_obj.uart3_callback = callback; + uart_obj.uart3_parameter = param; + } + else if(uart->uart_handle->Instance == USART6) { + uart_obj.uart6_callback = callback; + uart_obj.uart6_parameter = param; + } return (0); } @@ -120,6 +157,14 @@ int stm32f4_uart_set_cb(const void *this, const void *callback, const void *para void USART1_IRQHandler(void) { enter_isr(); - HAL_UART_IRQHandler(uart_obj.uart1_handle); + uint32_t tmp1 = 0U, tmp2 = 0U; + tmp1 = __HAL_UART_GET_FLAG(uart_obj.uart1_handle, UART_FLAG_RXNE); + tmp2 = __HAL_UART_GET_IT_SOURCE(uart_obj.uart1_handle, UART_IT_RXNE); + if((tmp1 != RESET) && (tmp2 != RESET)) { + if(uart_obj.uart1_callback) { + void (*cb)(const void *) = uart_obj.uart1_callback; + cb(uart_obj.uart1_parameter); + } + } exit_isr(); } diff --git a/source/test/shell/main.c b/source/test/shell/main.c new file mode 100644 index 0000000..1a7bb3c --- /dev/null +++ b/source/test/shell/main.c @@ -0,0 +1,43 @@ + + +/* + * main.c + * + * Created on: Aug 2, 2016 + * Author: tkl + */ + +#include + +#include "driver.h" +#include "board.h" +#include "stack.h" +#include "queue.h" +#include "kernel.h" +#include "driver.h" +#include "list.h" +#include "shell.h" + +void *uname_fct(const char *line) +{ + if(NULL == line) + return NULL; + + return NULL; +} + +static struct command cmd = { + .command = "uname", + .command_callback = uname_fct +}; + +int main(void) +{ + board_init(); + shell_init(&uart_1); + shell_add_command(&cmd); + schedule_start(); + + return 0; +} + diff --git a/source/test/shell/shell.mk b/source/test/shell/shell.mk new file mode 100644 index 0000000..721cccf --- /dev/null +++ b/source/test/shell/shell.mk @@ -0,0 +1 @@ +SRC_DIR += source/test/shell