From 4a9da1a9fdd83a484bb899548c0aef2c14b5994b Mon Sep 17 00:00:00 2001 From: tkl Date: Wed, 17 Aug 2016 10:06:18 +0200 Subject: [PATCH] uart tx running --- .cproject | 23 ++-- config/make/stm32f4xx.mk | 20 ++-- .../include/stm32f4-discovery.h | 89 +++++++-------- .../stm32f4xx/driver/include/stm32f4_uart.h | 62 +++++++++++ .../arch/stm32f4xx/driver/stm32_sys_tick.c | 8 ++ .../arch/stm32f4xx/driver/stm32f4_gpio.c | 18 ++++ .../arch/stm32f4xx/driver/stm32f4_uart._c | 102 ------------------ .../arch/stm32f4xx/driver/stm32f4_uart.c | 94 ++++++++++++++++ ...=> stm32f4xx_hal_timebase_tim_template._c} | 0 source/firmware/kernel/driver/driver.c | 4 +- .../kernel/driver/{uart._c => uart.c} | 0 source/test/test.mk | 18 ++-- source/test/uart/main.c | 53 +++++++++ source/test/uart/uart.mk | 1 + 14 files changed, 324 insertions(+), 168 deletions(-) create mode 100644 source/firmware/arch/stm32f4xx/driver/include/stm32f4_uart.h delete mode 100644 source/firmware/arch/stm32f4xx/driver/stm32f4_uart._c create mode 100644 source/firmware/arch/stm32f4xx/driver/stm32f4_uart.c rename source/firmware/arch/stm32f4xx/lib/system/src/stm32f4-hal/{stm32f4xx_hal_timebase_tim_template.c => stm32f4xx_hal_timebase_tim_template._c} (100%) rename source/firmware/kernel/driver/{uart._c => uart.c} (100%) create mode 100644 source/test/uart/main.c create mode 100644 source/test/uart/uart.mk diff --git a/.cproject b/.cproject index 7fd91ec..41f4393 100755 --- a/.cproject +++ b/.cproject @@ -146,7 +146,6 @@ make - all true true @@ -154,7 +153,6 @@ make - clean true true @@ -162,6 +160,7 @@ make + distclean true false @@ -217,7 +216,6 @@ make - all true true @@ -249,7 +247,6 @@ make - distclean true true @@ -263,6 +260,22 @@ false true + + make + TEST_APP=uart BOARD=stm32f4-discovery DEBUG=y + test + true + false + true + + + make + TEST_APP=uart BOARD=stm32f4-discovery DEBUG=y + install + true + false + true + make APP=example_radio_rx BOARD=msp430-ccrf DEBUG=y @@ -289,7 +302,6 @@ make - all true true @@ -297,7 +309,6 @@ make - clean true true diff --git a/config/make/stm32f4xx.mk b/config/make/stm32f4xx.mk index db22f3f..784d33e 100644 --- a/config/make/stm32f4xx.mk +++ b/config/make/stm32f4xx.mk @@ -12,16 +12,7 @@ INCLUDES += \ /opt/arm-2011.09/arm-none-eabi/include \ /opt/arm-2011.09/lib/gcc/arm-none-eabi/4.6.1/include -ifeq ($(DEBUG),y) -OPTIM = g -GEN_FLAGS += -g3 -DBG_EXT = -dbg -else -OPTIM = s -DBG_EXT = -endif - -GEN_FLAGS := \ +GEN_FLAGS += \ -mcpu=cortex-m4 \ -mthumb \ -mfloat-abi=hard \ @@ -77,6 +68,15 @@ L_FLAGS := \ -L"config/linker" \ --specs=nano.specs +ifeq ($(DEBUG),y) +OPTIM = g +GEN_FLAGS += -g3 +DBG_EXT = -dbg +else +OPTIM = s +DBG_EXT = +endif + AS_FLAGS := -mapcs-32 -g AR_FLAGS := rcs diff --git a/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/stm32f4-discovery.h b/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/stm32f4-discovery.h index e290263..57c89d5 100755 --- a/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/stm32f4-discovery.h +++ b/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/stm32f4-discovery.h @@ -10,13 +10,14 @@ #include #if 0 #include "../../../driver/include/stm32f4_pwm.h" -#include "../../../driver/include/stm32f4_uart.h" #include "pwm.h" -#include "uart.h" -#include "ringbuffer.h" #endif #include "stm32f4xx.h" +#include "uart.h" +#include "stm32f4_uart.h" +#include "ringbuffer.h" + #include "timer.h" #include "stm32_sys_tick.h" #include "sys_tick.h" @@ -222,51 +223,55 @@ const struct driver pwm_1 = { &pwm_ch1, }; #endif -#if 0 // UART 1 -static char console_linear_buffer[80]; +static const GPIO_InitTypeDef port_cfg_uart1 = { + .Pin = GPIO_PIN_6 | GPIO_PIN_7, + .Mode = GPIO_MODE_AF_PP, + .Pull = GPIO_PULLUP, + .Speed = GPIO_SPEED_FREQ_HIGH, + .Alternate = GPIO_AF7_USART1, +}; + +static const struct stm32f4_gpio stm32f4_uart1_gpio = { + .port = GPIOB, + .pin = &port_cfg_uart1, +}; + +static UART_HandleTypeDef stm32f4_discovery_uart1_handle = { + .Instance = USART1, + .Init.BaudRate = 115200, + .Init.WordLength = UART_WORDLENGTH_8B, + .Init.StopBits = UART_STOPBITS_1, + .Init.Parity = UART_PARITY_NONE, + .Init.Mode = UART_MODE_TX_RX, + .Init.HwFlowCtl = UART_HWCONTROL_NONE, + .Init.OverSampling = UART_OVERSAMPLING_16, + .pTxBuffPtr = NULL, + .TxXferSize = 0, + .TxXferCount = 0, + .pRxBuffPtr = NULL, + .RxXferSize = 0, + .RxXferCount = 0, + .hdmatx = NULL, + .hdmarx = NULL, + .Lock = HAL_UNLOCKED, + .gState = HAL_UART_STATE_RESET, + .ErrorCode = 0, +}; + +static const struct stm32f4_uart stm32f4_uart1 = { + .uart_gpio = &stm32f4_uart1_gpio, + .uart_handle = &stm32f4_discovery_uart1_handle, +}; + +static char console_linear_buffer[10]; static struct ringbuffer console_buffer = { console_linear_buffer, console_linear_buffer, console_linear_buffer, - sizeof(console_linear_buffer), + 10, 0 }; -static const GPIO_InitTypeDef stm32f4_discovery_uart1_gpio = { - GPIO_Pin_6 | GPIO_Pin_7, - GPIO_Mode_AF, - GPIO_Speed_50MHz, - GPIO_OType_PP, - GPIO_PuPd_UP -}; - -static const USART_InitTypeDef stm32f4_discovery_uart1_usart_init = { - 115200, - USART_WordLength_8b, - USART_StopBits_1, - USART_Parity_No, - USART_Mode_Tx | USART_Mode_Rx, - USART_HardwareFlowControl_None, -}; - -static const NVIC_InitTypeDef stm32f4_discovery_uart1_nvic_init = { - USART1_IRQn, - 0, - 0, - ENABLE, -}; - -static const struct stm32f4_uart stm32f4_uart1 = { - &stm32f4_discovery_uart1_gpio, - GPIOB, - GPIO_PinSource7, - GPIO_PinSource6, - &stm32f4_discovery_uart1_usart_init, - USART1, - USART_IT_RXNE /* | USART_IT_TXE*/, - &stm32f4_discovery_uart1_nvic_init, - -}; static const struct uart __uart_1 = { &stm32f4_uart1, @@ -282,7 +287,7 @@ const struct driver uart_1 = { DRIVER_TYPE_UART, &__uart_1, }; -#endif + #if 0 // GPIOC0 static const GPIO_InitTypeDef port_cfg_C0 = { diff --git a/source/firmware/arch/stm32f4xx/driver/include/stm32f4_uart.h b/source/firmware/arch/stm32f4xx/driver/include/stm32f4_uart.h new file mode 100644 index 0000000..cdd7ff1 --- /dev/null +++ b/source/firmware/arch/stm32f4xx/driver/include/stm32f4_uart.h @@ -0,0 +1,62 @@ +/* + * stm32f4_uart.h + * + * Created on: Jul 24, 2016 + * Author: tkl + */ + +#ifndef SOURCE_FIRMWARE_ARCH_STM32F4XX_DRIVER_UART_STM32_UART_H_ +#define SOURCE_FIRMWARE_ARCH_STM32F4XX_DRIVER_UART_STM32_UART_H_ + +#include "driver.h" +#include "uart.h" + +//! \brief Stm32f4 uart device. +#pragma pack(push) +#pragma pack(1) +struct stm32f4_uart { + const struct stm32f4_gpio *uart_gpio; + UART_HandleTypeDef *uart_handle; +}; +#pragma pack(pop) + +//! \brief Open an uart device. +//! \param this The uart to open. +//! \retval -1 in error case. +int stm32f4_uart_open(const void *this); + +//! \brief Close an uart device. +//! \param this The uart to close. +//! \retval -1 in error case. +int stm32f4_uart_close(const void *this); + +//! \brief Read from an uart device. +//! \param this The uart to read from. +//! \param buffer The buffer to read to. +//! \param len The length of the buffer. +//! \retval -1 in error case, otherwise number of read characters. +int stm32f4_uart_read(const void *this, char *buffer, int len); + +//! \brief Write to an uart device. +//! \param this The uart to write to. +//! \param buffer The buffer to write. +//! \param len The number of characters to write. +//! \retval -1 in error case, otherwise number of written characters. +int stm32f4_uart_write(const void *this, const char *buffer, int len); + +//! \brief Set a callback for interrupt handling of the uart. +//! \param this The uart. +//! \param callback The callback to execute in interrupt case. +//! \param param The argument for the callback. +//! \retval -1 in error case. +int stm32f4_uart_set_cb(const void *this, const void *callback, const void *param); + +static const struct uart_fp stm32f4_uart_fp = { + stm32f4_uart_open, + stm32f4_uart_close, + stm32f4_uart_read, + stm32f4_uart_write, + stm32f4_uart_set_cb +}; + +#endif /* SOURCE_FIRMWARE_ARCH_STM32F4XX_DRIVER_UART_STM32_UART_H_ */ diff --git a/source/firmware/arch/stm32f4xx/driver/stm32_sys_tick.c b/source/firmware/arch/stm32f4xx/driver/stm32_sys_tick.c index 4d00ec7..30be74d 100755 --- a/source/firmware/arch/stm32f4xx/driver/stm32_sys_tick.c +++ b/source/firmware/arch/stm32f4xx/driver/stm32_sys_tick.c @@ -68,6 +68,9 @@ void SysTick_Handler(void) { enter_isr(); + #if defined(USE_HAL_DRIVER) + HAL_IncTick(); + #endif if(stm32_sys_tick_obj.sys_tick_cb != NULL) { stm32_sys_tick_cb_t cb = stm32_sys_tick_obj.sys_tick_cb; void *param = stm32_sys_tick_obj.sys_tick_cb_param; @@ -76,3 +79,8 @@ void SysTick_Handler(void) exit_isr(); } + +HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority) +{ + return HAL_OK; +} diff --git a/source/firmware/arch/stm32f4xx/driver/stm32f4_gpio.c b/source/firmware/arch/stm32f4xx/driver/stm32f4_gpio.c index 4198178..2043c8e 100755 --- a/source/firmware/arch/stm32f4xx/driver/stm32f4_gpio.c +++ b/source/firmware/arch/stm32f4xx/driver/stm32f4_gpio.c @@ -83,6 +83,24 @@ int stm32f4_gpio_close(const void *gpio) return -1; struct stm32f4_gpio *this = (struct stm32f4_gpio *)gpio; HAL_GPIO_DeInit(this->port, this->pin->Pin); + if(this->port == GPIOA) + __HAL_RCC_GPIOA_CLK_DISABLE(); + else if(this->port == GPIOB) + __HAL_RCC_GPIOB_CLK_DISABLE(); + else if(this->port == GPIOC) + __HAL_RCC_GPIOC_CLK_DISABLE(); + else if(this->port == GPIOD) + __HAL_RCC_GPIOD_CLK_DISABLE(); + else if(this->port == GPIOE) + __HAL_RCC_GPIOE_CLK_DISABLE(); + else if(this->port == GPIOF) + __HAL_RCC_GPIOF_CLK_DISABLE(); + else if(this->port == GPIOG) + __HAL_RCC_GPIOG_CLK_DISABLE(); + else if(this->port == GPIOH) + __HAL_RCC_GPIOH_CLK_DISABLE(); + else if(this->port == GPIOI) + __HAL_RCC_GPIOI_CLK_DISABLE(); return 0; } diff --git a/source/firmware/arch/stm32f4xx/driver/stm32f4_uart._c b/source/firmware/arch/stm32f4xx/driver/stm32f4_uart._c deleted file mode 100644 index a5165db..0000000 --- a/source/firmware/arch/stm32f4xx/driver/stm32f4_uart._c +++ /dev/null @@ -1,102 +0,0 @@ -/* - * stm32f4_uart.c - * - * Created on: Jul 24, 2016 - * Author: tkl - */ - -#include -#include - -#include "stm32f4xx.h" -#include "stm32f4xx_isr.h" -#include "stm32f4_uart.h" - -struct stm32f4_uart_obj { - const void *callback; //!< Interrupt callback. - const void *parameter; //!< argument for the callback. -}; - -static volatile struct stm32f4_uart_obj uart1_obj; - -int stm32f4_uart_open(const void *this) -{ - struct stm32f4_uart *uart = (struct stm32f4_uart *)this; - uint8_t gpio_af = 0; - uint32_t rcc_apb_uart = 0, rcc_apb_gpio = 0; - - if(uart->usart_port == USART1) { - gpio_af = GPIO_AF_USART1; - rcc_apb_uart = RCC_APB2Periph_USART1; - } - if(uart->gpio_port == GPIOA) { - rcc_apb_gpio = RCC_AHB1Periph_GPIOA; - } - else if(uart->gpio_port == GPIOB) { - rcc_apb_gpio = RCC_AHB1Periph_GPIOB; - } - - RCC_APB2PeriphClockCmd(rcc_apb_uart, ENABLE); - RCC_AHB1PeriphClockCmd(rcc_apb_gpio, ENABLE); - - GPIO_Init(uart->gpio_port, (GPIO_InitTypeDef *)uart->gpio_init); - - GPIO_PinAFConfig(uart->gpio_port, uart->pin_src_rx, gpio_af); - GPIO_PinAFConfig(uart->gpio_port, uart->pin_src_tx, gpio_af); - - USART_Init(uart->usart_port, (USART_InitTypeDef *)uart->usart_init); - USART_ITConfig(uart->usart_port, uart->usart_it_select, ENABLE); - NVIC_Init((NVIC_InitTypeDef *)uart->nvic_init); - USART_Cmd(uart->usart_port, ENABLE); - return (0); -} - -int stm32f4_uart_close(const void *this) -{ - struct stm32f4_uart *uart = (struct stm32f4_uart *)this; - USART_Cmd(uart->usart_port, DISABLE); - return (0); -} - -int stm32f4_uart_read(const void *this, char *buffer, int len) -{ - struct stm32f4_uart *uart = (struct stm32f4_uart *)this; - *buffer = uart->usart_port->DR; - return (1); -} - -int stm32f4_uart_write(const void *this, const char *buffer, int len) -{ - struct stm32f4_uart *uart = (struct stm32f4_uart *)this; - int i; - for(i = 0; i < len; i++) { - // wait until data register is empty - while(!(uart->usart_port->SR & 0x00000040)); - USART_SendData(uart->usart_port, buffer[i]); - } - return (i); -} - -int stm32f4_uart_set_cb(const void *this, const void *callback, const void *param) -{ - struct stm32f4_uart *uart = (struct stm32f4_uart *)this; - if(uart->usart_port == USART1) { - uart1_obj.callback = callback; - uart1_obj.parameter = param; - } - return (0); -} - -// this is the interrupt request handler (IRQ) for ALL USART1 interrupts -void USART1_IRQHandler(void) -{ - enter_isr(); - // check if the USART1 receive interrupt flag was set - if(USART_GetITStatus(USART1, USART_IT_RXNE)) { - if(uart1_obj.callback) { - void (*cb)(const void *) = uart1_obj.callback; - cb(uart1_obj.parameter); - } - } - exit_isr(); -} diff --git a/source/firmware/arch/stm32f4xx/driver/stm32f4_uart.c b/source/firmware/arch/stm32f4xx/driver/stm32f4_uart.c new file mode 100644 index 0000000..cfcbca2 --- /dev/null +++ b/source/firmware/arch/stm32f4xx/driver/stm32f4_uart.c @@ -0,0 +1,94 @@ +/* + * stm32f4_uart.c + * + * Created on: Jul 24, 2016 + * Author: tkl + */ + +#include +#include + +#include "stm32f4xx.h" +#include "stm32f4xx_isr.h" +#include "stm32f4_uart.h" +#include "gpio.h" +#include "stm32f4_gpio.h" + +struct stm32f4_uart_obj { + const void *callback; //!< Interrupt callback. + const void *parameter; //!< argument for the callback. +}; + +static volatile struct stm32f4_uart_obj uart1_obj; + +int stm32f4_uart_open(const void *this) +{ + if(NULL == this) + return -1; + struct stm32f4_uart *uart = (struct stm32f4_uart *)this; + + /* init gpio */ + stm32f4_gpio_open(uart->uart_gpio); + + /* uart clock enable */ + if(uart->uart_handle->Instance == USART1) + __HAL_RCC_USART1_CLK_ENABLE(); + else if(uart->uart_handle->Instance == USART2) + __HAL_RCC_USART2_CLK_ENABLE(); + else if(uart->uart_handle->Instance == USART3) + __HAL_RCC_USART3_CLK_ENABLE(); + else if(uart->uart_handle->Instance == USART6) + __HAL_RCC_USART6_CLK_ENABLE(); + + /* init uart */ + HAL_UART_Init((UART_HandleTypeDef *)uart->uart_handle); + + return (0); +} + +int stm32f4_uart_close(const void *this) +{ + if(NULL == this) + return -1; + struct stm32f4_uart *uart = (struct stm32f4_uart *)this; + + HAL_UART_DeInit((UART_HandleTypeDef *)uart->uart_handle); + if(uart->uart_handle->Instance == USART1) + __HAL_RCC_USART1_CLK_DISABLE(); + else if(uart->uart_handle->Instance == USART2) + __HAL_RCC_USART2_CLK_DISABLE(); + else if(uart->uart_handle->Instance == USART3) + __HAL_RCC_USART3_CLK_DISABLE(); + else if(uart->uart_handle->Instance == USART6) + __HAL_RCC_USART6_CLK_DISABLE(); + + stm32f4_gpio_close(uart->uart_gpio); + return (0); +} + +int stm32f4_uart_read(const void *this, char *buffer, int len) +{ + return (1); +} + +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((UART_HandleTypeDef *)uart->uart_handle, (uint8_t *)buffer, len, 1000); + return len; +} + +int stm32f4_uart_set_cb(const void *this, const void *callback, const void *param) +{ + return (0); +} + +// this is the interrupt request handler (IRQ) for ALL USART1 interrupts +void USART1_IRQHandler(void) +{ + enter_isr(); + // check if the USART1 receive interrupt flag was set + exit_isr(); +} diff --git a/source/firmware/arch/stm32f4xx/lib/system/src/stm32f4-hal/stm32f4xx_hal_timebase_tim_template.c b/source/firmware/arch/stm32f4xx/lib/system/src/stm32f4-hal/stm32f4xx_hal_timebase_tim_template._c similarity index 100% rename from source/firmware/arch/stm32f4xx/lib/system/src/stm32f4-hal/stm32f4xx_hal_timebase_tim_template.c rename to source/firmware/arch/stm32f4xx/lib/system/src/stm32f4-hal/stm32f4xx_hal_timebase_tim_template._c diff --git a/source/firmware/kernel/driver/driver.c b/source/firmware/kernel/driver/driver.c index 4a0bb0e..aa36474 100644 --- a/source/firmware/kernel/driver/driver.c +++ b/source/firmware/kernel/driver/driver.c @@ -41,7 +41,7 @@ int drv_open(const struct driver *driver) ret = spi_open((const struct spi *)(driver->device_driver)); break; case DRIVER_TYPE_UART: -// ret = uart_open((const struct uart *)(driver->device_driver)); + ret = uart_open((const struct uart *)(driver->device_driver)); break; } return ret; @@ -136,7 +136,7 @@ int drv_write(const struct driver *driver, const char *buffer, int len) case DRIVER_TYPE_SPI: break; case DRIVER_TYPE_UART: -// ret = uart_write((const struct uart *)(driver->device_driver), buffer, len); + ret = uart_write((const struct uart *)(driver->device_driver), buffer, len); break; } return ret; diff --git a/source/firmware/kernel/driver/uart._c b/source/firmware/kernel/driver/uart.c similarity index 100% rename from source/firmware/kernel/driver/uart._c rename to source/firmware/kernel/driver/uart.c diff --git a/source/test/test.mk b/source/test/test.mk index 3c53ed5..ba014f1 100644 --- a/source/test/test.mk +++ b/source/test/test.mk @@ -1,9 +1,15 @@ -ifeq ($(TEST_APP), shell) -include source/test/shell/shell.mk -endif -ifeq ($(TEST_APP), pwm) -include source/test/pwm/pwm.mk -endif ifeq ($(TEST_APP), blinky) include source/test/blinky/blinky.mk endif + +ifeq ($(TEST_APP), pwm) +include source/test/pwm/pwm.mk +endif + +ifeq ($(TEST_APP), shell) +include source/test/shell/shell.mk +endif + +ifeq ($(TEST_APP), uart) +include source/test/uart/uart.mk +endif diff --git a/source/test/uart/main.c b/source/test/uart/main.c new file mode 100644 index 0000000..1f9a9ae --- /dev/null +++ b/source/test/uart/main.c @@ -0,0 +1,53 @@ +// +// This file is part of the GNU ARM Eclipse distribution. +// Copyright (c) 2014 Liviu Ionescu. +// + +#include +#include +#include + +#include "stack.h" +#include "queue.h" +#include "kernel.h" +#include "board.h" +#include "driver.h" + +// Sample pragmas to cope with warnings. Please note the related line at +// the end of this function, used to pop the compiler diagnostics status. +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#pragma GCC diagnostic ignored "-Wmissing-declarations" +#pragma GCC diagnostic ignored "-Wreturn-type" + +#define TH_STACK_SIZE 256 +stack_t th_stack[TH_STACK_SIZE]; +struct thread_context th_ctx; +static void th_func(void *arg) +{ + drv_open(&uart_1); + drv_open(&gpio_d12); + drv_write(&gpio_d12, "0", 1); + while(1) { + sleep_ms(1000); + drv_write(&uart_1, "set_led\r\n", 9); + drv_write(&gpio_d12, "1", 1); + sleep_ms(1000); + drv_write(&uart_1, "unset_led\r\n", 11); + drv_write(&gpio_d12, "0", 1); + } +} + + +int main(int argc, char* argv[]) +{ + board_init(); + timer_open(&timer_1); + thread_create(&th_ctx, th_stack, TH_STACK_SIZE, th_func, NULL, THREAD_PRIO_LOW); + + schedule_start(); + + return 0; +} + +#pragma GCC diagnostic pop diff --git a/source/test/uart/uart.mk b/source/test/uart/uart.mk new file mode 100644 index 0000000..7d480a1 --- /dev/null +++ b/source/test/uart/uart.mk @@ -0,0 +1 @@ +SRC_DIR += source/test/uart