diff --git a/.cproject b/.cproject index 91234d7..e426cac 100755 --- a/.cproject +++ b/.cproject @@ -197,6 +197,14 @@ false true + + make + TEST_APP=shell + test + true + false + true + make @@ -261,6 +269,38 @@ false true + + make + TEST_APP=shell BOARD=stm32f4-discovery DEBUG=y + test + true + false + true + + + make + TEST_APP=shell BOARD=stm32f4-discovery DEBUG=y + install + true + false + true + + + make + TEST_APP=pwm BOARD=stm32f4-discovery DEBUG=y + test + true + false + true + + + make + TEST_APP=pwm BOARD=stm32f4-discovery DEBUG=y + install + true + false + true + make APP=example_radio_rx BOARD=msp430-ccrf DEBUG=y diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 80ffa55..00627ca 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,7 +1,7 @@ variables: SW_KERNEL: "0" SW_MAJOR: "0" - SW_MINOR: "1" + SW_MINOR: "2" before_script: - "echo $CI_BUILD_ID" diff --git a/Makefile b/Makefile index d874baa..1f44a67 100755 --- a/Makefile +++ b/Makefile @@ -12,6 +12,11 @@ endif MAINFILE = $(EXE_DIR)/lib$(OS_NAME)-$(ARCH)-$(BOARD)$(VERSION)$(DBG_EXT)$(LIB_EXT) DEPLOY_PACKET = lib$(OS_NAME)-$(ARCH)-$(BOARD)$(VERSION)$(DBG_EXT).tar.xz +TEST_FILE = $(EXE_DIR)/$(TEST_APP)$(ELF_EXT) +BIN_FILE = $(EXE_DIR)/$(TEST_APP)$(BIN_EXT) +HEX_FILE = $(EXE_DIR)/$(TEST_APP)$(HEX_EXT) +SIZE_FILE = $(SIZE_DIR)/$(TEST_APP)$(SIZE_EXT) + INCLUDES += $(SRC_DIR) SUB_FOLDER := @@ -25,6 +30,10 @@ DEPS = $(SOURCES:$(ROOT_DIR)/%.c=$(OBJ_DIR)/%.d) #include subfolders include source/firmware/firmware.mk +ifdef TEST_APP +include source/test/test.mk +CFLAGS += -DTEST_APP +endif SOURCES += $(foreach folder, $(SUB_FOLDER), $(wildcard $(ROOT_DIR)/$(folder)/*.c)) CHECKSOURCES += $(foreach folder, $(CHECK_FOLDER), $(wildcard $(ROOT_DIR)/$(folder)/*.c)) @@ -74,6 +83,22 @@ doc: @$(MKDIR) $(DOC_DIR) (cat $(DOXYFILE) ; echo "INPUT=$(DOC_SRC)" ; echo "OUTPUT_DIRECTORY=$(DOC_DIR)") | doxygen - +test: $(OBJECTS) $(ASM_OBJECTS) + @$(MKDIR) $(EXE_DIR) + @$(MKDIR) $(MAP_DIR) + @$(MKDIR) $(SIZE_DIR) + $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $(ASM_OBJECTS) -o $(TEST_FILE) + $(OBJCOPY) $(TEST_FILE) -O binary $(BIN_FILE) + $(OBJCOPY) $(TEST_FILE) -O ihex $(HEX_FILE) + $(NM) --size-sort --print-size $(TEST_FILE) > $(SIZE_FILE) + @echo + @$(SIZE) --format=berkeley -x $(TEST_FILE) + @echo + +install: test + $(PRE_PROGRAM) + $(PROGRAM) + ifneq "$(MAKECMDGOALS)" "clean" -include $(DEPS) else diff --git a/config/linker/stm32_flash.ld b/config/linker/stm32_flash.ld new file mode 100755 index 0000000..7cfdc96 --- /dev/null +++ b/config/linker/stm32_flash.ld @@ -0,0 +1,170 @@ +/* +***************************************************************************** +** +** File : stm32_flash.ld +** +** Abstract : Linker script for STM32F407VG Device with +** 1024KByte FLASH, 192KByte RAM +** +** Set heap size, stack size and stack location according +** to application requirements. +** +** Set memory bank area and size if external memory is used. +** +** Target : STMicroelectronics STM32 +** +** Environment : Atollic TrueSTUDIO(R) +** +** Distribution: The file is distributed “as is,” without any warranty +** of any kind. +** +** (c)Copyright Atollic AB. +** You may use this file as-is or modify it according to the needs of your +** project. Distribution of this file (unmodified or modified) is not +** permitted. Atollic AB permit registered Atollic TrueSTUDIO(R) users the +** rights to distribute the assembled, compiled & linked contents of this +** file as part of an application binary file, provided that it is built +** using the Atollic TrueSTUDIO(R) toolchain. +** +***************************************************************************** +*/ + +/* Entry Point */ +ENTRY(Reset_Handler) + +/* Highest address of the user mode stack */ +_estack = 0x20020000; /* end of 128K RAM on AHB bus*/ + +/* Generate a link error if heap and stack don't fit into RAM */ +_Min_Heap_Size = 0; /* required amount of heap */ +_Min_Stack_Size = 0x400; /* required amount of stack */ + +/* Specify the memory areas */ +MEMORY +{ + FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 1024K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K + MEMORY_B1 (rx) : ORIGIN = 0x60000000, LENGTH = 0K +} + +/* Define output sections */ +SECTIONS +{ + /* The startup code goes first into FLASH */ + .isr_vector : + { + . = ALIGN(4); + KEEP(*(.isr_vector)) /* Startup code */ + . = ALIGN(4); + } >FLASH + + /* The program code and other data goes into FLASH */ + .text : + { + . = ALIGN(4); + *(.text) /* .text sections (code) */ + *(.text*) /* .text* sections (code) */ + *(.rodata) /* .rodata sections (constants, strings, etc.) */ + *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ + *(.glue_7) /* glue arm to thumb code */ + *(.glue_7t) /* glue thumb to arm code */ + *(.eh_frame) + + KEEP (*(.init)) + KEEP (*(.fini)) + + . = ALIGN(4); + _etext = .; /* define a global symbols at end of code */ + } >FLASH + + + .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH + .ARM : { + __exidx_start = .; + *(.ARM.exidx*) + __exidx_end = .; + } >FLASH + + .preinit_array : + { + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP (*(.preinit_array*)) + PROVIDE_HIDDEN (__preinit_array_end = .); + } >FLASH + .init_array : + { + PROVIDE_HIDDEN (__init_array_start = .); + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array*)) + PROVIDE_HIDDEN (__init_array_end = .); + } >FLASH + .fini_array : + { + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP (*(.fini_array*)) + KEEP (*(SORT(.fini_array.*))) + PROVIDE_HIDDEN (__fini_array_end = .); + } >FLASH + + /* used by the startup to initialize data */ + _sidata = .; + + /* Initialized data sections goes into RAM, load LMA copy after code */ + .data : AT ( _sidata ) + { + . = ALIGN(4); + _sdata = .; /* create a global symbol at data start */ + *(.data) /* .data sections */ + *(.data*) /* .data* sections */ + + . = ALIGN(4); + _edata = .; /* define a global symbol at data end */ + } >RAM + + /* Uninitialized data section */ + . = ALIGN(4); + .bss : + { + /* This is used by the startup in order to initialize the .bss secion */ + _sbss = .; /* define a global symbol at bss start */ + __bss_start__ = _sbss; + *(.bss) + *(.bss*) + *(COMMON) + + . = ALIGN(4); + _ebss = .; /* define a global symbol at bss end */ + __bss_end__ = _ebss; + } >RAM + + /* User_heap_stack section, used to check that there is enough RAM left */ + ._user_heap_stack : + { + . = ALIGN(4); + PROVIDE ( end = . ); + PROVIDE ( _end = . ); + . = . + _Min_Heap_Size; + . = . + _Min_Stack_Size; + . = ALIGN(4); + } >RAM + + /* MEMORY_bank1 section, code must be located here explicitly */ + /* Example: extern int foo(void) __attribute__ ((section (".mb1text"))); */ + .memory_b1_text : + { + *(.mb1text) /* .mb1text sections (code) */ + *(.mb1text*) /* .mb1text* sections (code) */ + *(.mb1rodata) /* read-only data (constants) */ + *(.mb1rodata*) + } >MEMORY_B1 + + /* Remove information from the standard libraries */ + /DISCARD/ : + { + libc.a ( * ) + libm.a ( * ) + libgcc.a ( * ) + } + + .ARM.attributes 0 : { *(.ARM.attributes) } +} diff --git a/config/make/rules.mk b/config/make/rules.mk index 582a77a..5e9a8ec 100755 --- a/config/make/rules.mk +++ b/config/make/rules.mk @@ -1,14 +1,14 @@ -.PHONY: clean distclean doc +.PHONY: clean distclean doc test ROOT_DIR := $(shell pwd | sed "s/\/source//g") -ifeq ($(BOARD), msp430-ccrf) -include $(ROOT_DIR)/config/make/msp430.mk -endif ifeq ($(BOARD), stm32f4-discovery) include $(ROOT_DIR)/config/make/stm32f4xx.mk endif +OS_LIB = kosmos-$(ARCH)-$(BOARD)$(DBG_EXT) +LIBS += $(OS_LIB) + CFLAGS += \ -Wno-unused-function \ -O$(OPTIM) \ diff --git a/config/make/stm32f4xx.mk b/config/make/stm32f4xx.mk index 563d1b2..252ca65 100644 --- a/config/make/stm32f4xx.mk +++ b/config/make/stm32f4xx.mk @@ -9,8 +9,8 @@ endif CROSS_COMPILE=arm-none-eabi- INCLUDES += \ - /usr/lib/arm-none-eabi/include \ - /usr/lib/gcc/arm-none-eabi/4.8/include + /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 = 0 @@ -23,7 +23,7 @@ endif CFLAGS += \ -mthumb \ - -T $(ROOT_DIR)/source/firmware/arch/stm32f4xx/linker/stm32_flash.ld \ + -T $(ROOT_DIR)/config/linker/stm32_flash.ld \ -D USE_STDPERIPH_DRIVER\ -D VECT_TAB_FLASH\ -D GCC_ARMCM4\ @@ -35,7 +35,6 @@ CFLAGS += \ -mfloat-abi=softfp \ -fdata-sections \ -ffunction-sections -# -D inline= -mthumb\ CPPCHECK_FLAGS += \ -D USE_STDPERIPH_DRIVER\ @@ -54,7 +53,7 @@ LDFLAGS=\ ASFLAGS=-mapcs-32 -g ARFLAGS=rcs -OOCD_IMAGE=$(BINFILE) +OOCD_IMAGE=$(BIN_FILE) OOCD_CFG_FILE=$(EXE_DIR)/openocd.cfg PRE_PROGRAM = echo "telnet_port 4444\ninit\nreset halt\nflash write_image erase $(OOCD_IMAGE) 0x08000000 bin\nreset run\n shutdown\n" > $(OOCD_CFG_FILE) 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 d393075..ea87330 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 @@ -13,15 +13,18 @@ #include "driver.h" #include "gpio.h" +#include "pwm.h" #include "timer.h" #include "uart.h" #include "ringbuffer.h" #include "sys_tick.h" #include "stm32f4xx.h" #include "stm32f4_gpio.h" +#include "stm32f4_pwm.h" #include "stm32f4_uart.h" #include "stm32_sys_tick.h" + // SYSTEM TICK static const enum stm32_sys_tick_time_base stm23_sys_tick_time_base = STM32_SYS_TICK_TIME_BASE_MS; @@ -36,6 +39,161 @@ static const struct loki_timer timer_1 = { &timer_fp }; +// PWM CHANNEL 4 +/* apb1 clock = 84MHz */ +/* period_reg = src_clk / presc / cnt_clk */ +/* 4199 = 84MHZ / (0 + 1) / 20kHz - 1 */ +static const TIM_TimeBaseInitTypeDef timer_4_cfg = { + .TIM_RepetitionCounter = 0x0000, + .TIM_Prescaler = 0, + .TIM_ClockDivision = TIM_CKD_DIV1, + .TIM_CounterMode = TIM_CounterMode_Up, + .TIM_Period = 4199 +}; + +static const TIM_OCInitTypeDef t4_output_compare_cfg = { + .TIM_OutputNState = TIM_OutputNState_Disable, + .TIM_OCNPolarity = TIM_OCPolarity_High, + .TIM_OCIdleState = TIM_OCIdleState_Reset, + .TIM_OCNIdleState = TIM_OCNIdleState_Set, + .TIM_OCMode = TIM_OCMode_PWM1, + .TIM_OCPolarity = TIM_OCPolarity_High, + .TIM_OutputState = TIM_OutputState_Enable, + .TIM_Pulse = 0, +}; + +static const GPIO_InitTypeDef port_cfg_D15 = { + .GPIO_Pin = GPIO_Pin_15, + .GPIO_Mode = GPIO_Mode_AF, + .GPIO_OType = GPIO_OType_PP, + .GPIO_PuPd = GPIO_PuPd_UP, + .GPIO_Speed = GPIO_Speed_100MHz, +}; + +static struct stm32f4_pwm str32f4_pwm_4 = { + .timer = TIM4, + .timer_cfg = &timer_4_cfg, + .output_compare_cfg = &t4_output_compare_cfg, + .port = GPIOD, + .pin_src = GPIO_PinSource15, + .port_cfg = &port_cfg_D15, + .channel = channel_4, +}; + +static const struct pwm pwm_ch4 = { + .arch_dep_device = &str32f4_pwm_4, + .fp = &stm32f4_pwm_fp, +}; + +#ifdef TEST_APP +static const struct driver pwm_4 = { +#else +const struct driver pwm_4 = { +#endif + DRIVER_TYPE_PWM, + &pwm_ch4, +}; + +// PWM Channel 3 +static const GPIO_InitTypeDef port_cfg_D14 = { + .GPIO_Pin = GPIO_Pin_14, + .GPIO_Mode = GPIO_Mode_AF, + .GPIO_OType = GPIO_OType_PP, + .GPIO_PuPd = GPIO_PuPd_UP, + .GPIO_Speed = GPIO_Speed_100MHz, +}; + +static struct stm32f4_pwm str32f4_pwm_3 = { + .timer = TIM4, + .timer_cfg = &timer_4_cfg, + .output_compare_cfg = &t4_output_compare_cfg, + .port = GPIOD, + .pin_src = GPIO_PinSource14, + .port_cfg = &port_cfg_D14, + .channel = channel_3, +}; + +static const struct pwm pwm_ch3 = { + .arch_dep_device = &str32f4_pwm_3, + .fp = &stm32f4_pwm_fp, +}; + +#ifdef TEST_APP +static const struct driver pwm_3 = { +#else +const struct driver pwm_3 = { +#endif + DRIVER_TYPE_PWM, + &pwm_ch3, +}; + +// PWM Channel 2 +static const GPIO_InitTypeDef port_cfg_D13 = { + .GPIO_Pin = GPIO_Pin_13, + .GPIO_Mode = GPIO_Mode_AF, + .GPIO_OType = GPIO_OType_PP, + .GPIO_PuPd = GPIO_PuPd_UP, + .GPIO_Speed = GPIO_Speed_100MHz, +}; + +static struct stm32f4_pwm str32f4_pwm_2 = { + .timer = TIM4, + .timer_cfg = &timer_4_cfg, + .output_compare_cfg = &t4_output_compare_cfg, + .port = GPIOD, + .pin_src = GPIO_PinSource13, + .port_cfg = &port_cfg_D13, + .channel = channel_2, +}; + +static const struct pwm pwm_ch2 = { + .arch_dep_device = &str32f4_pwm_2, + .fp = &stm32f4_pwm_fp, +}; + +#ifdef TEST_APP +static const struct driver pwm_2 = { +#else +const struct driver pwm_2 = { +#endif + DRIVER_TYPE_PWM, + &pwm_ch2, +}; + +// PWM Channel 2 +static const GPIO_InitTypeDef port_cfg_D12 = { + .GPIO_Pin = GPIO_Pin_12, + .GPIO_Mode = GPIO_Mode_AF, + .GPIO_OType = GPIO_OType_PP, + .GPIO_PuPd = GPIO_PuPd_UP, + .GPIO_Speed = GPIO_Speed_100MHz, +}; + +static struct stm32f4_pwm str32f4_pwm_1 = { + .timer = TIM4, + .timer_cfg = &timer_4_cfg, + .output_compare_cfg = &t4_output_compare_cfg, + .port = GPIOD, + .pin_src = GPIO_PinSource12, + .port_cfg = &port_cfg_D12, + .channel = channel_1, +}; + +static const struct pwm pwm_ch1 = { + .arch_dep_device = &str32f4_pwm_1, + .fp = &stm32f4_pwm_fp, +}; + +#ifdef TEST_APP +static const struct driver pwm_1 = { +#else +const struct driver pwm_1 = { +#endif + DRIVER_TYPE_PWM, + &pwm_ch3, +}; + +// UART 1 static char console_linear_buffer[80]; static struct ringbuffer console_buffer = { console_linear_buffer, @@ -86,160 +244,143 @@ static const struct uart __uart_1 = { &console_buffer, }; +#ifdef TEST_APP +static const struct driver uart_1 = { +#else const struct driver uart_1 = { +#endif DRIVER_TYPE_UART, &__uart_1, }; -// LED 3 -static const GPIO_InitTypeDef stm32_f4_discovery_led_3_gpio = { - GPIO_Pin_12, - GPIO_Mode_OUT, - GPIO_Speed_100MHz, - GPIO_OType_PP, - GPIO_PuPd_NOPULL -}; - -static const struct stm32f4_gpio stm32_f4_discovery_led_3 = { - GPIOD, - &stm32_f4_discovery_led_3_gpio, - NULL, - NULL, - NULL, - NULL -}; - -static const struct gpio __led_3 = { - (void*)&stm32_f4_discovery_led_3, - &gpio_fp -}; - -const struct driver led_3 = { - DRIVER_TYPE_GPIO, - &__led_3, -}; - -// LED 4 -static const GPIO_InitTypeDef stm32_f4_discovery_led_4_gpio = { - GPIO_Pin_13, - GPIO_Mode_OUT, - GPIO_Speed_100MHz, - GPIO_OType_PP, - GPIO_PuPd_NOPULL -}; - -static const struct stm32f4_gpio stm32_f4_discovery_led_4 = { - GPIOD, - &stm32_f4_discovery_led_4_gpio, - NULL, - NULL, - NULL, - NULL -}; - -static const struct gpio __led_4 = { - (void*)&stm32_f4_discovery_led_4, - &gpio_fp -}; - -const struct driver led_4 = { - DRIVER_TYPE_GPIO, - &__led_4, -}; - -// LED 5 -static const GPIO_InitTypeDef stm32_f4_discovery_led_5_gpio = { - GPIO_Pin_14, - GPIO_Mode_OUT, - GPIO_Speed_100MHz, - GPIO_OType_PP, - GPIO_PuPd_NOPULL -}; - -static const struct stm32f4_gpio stm32_f4_discovery_led_5 = { - GPIOD, - &stm32_f4_discovery_led_5_gpio, - NULL, - NULL, - NULL, - NULL -}; - -static const struct gpio __led_5 = { - (void*)&stm32_f4_discovery_led_5, - &gpio_fp -}; - -const struct driver led_5 = { - DRIVER_TYPE_GPIO, - &__led_5, -}; - -// LED 6 -static const GPIO_InitTypeDef stm32_f4_discovery_led_6_gpio = { - GPIO_Pin_15, - GPIO_Mode_OUT, - GPIO_Speed_100MHz, - GPIO_OType_PP, - GPIO_PuPd_NOPULL -}; - -static const struct stm32f4_gpio stm32_f4_discovery_led_6 = { - GPIOD, - &stm32_f4_discovery_led_6_gpio, - NULL, - NULL, - NULL, - NULL -}; - -static const struct gpio __led_6 = { - (void*)&stm32_f4_discovery_led_6, - &gpio_fp -}; - -const struct driver led_6 = { - DRIVER_TYPE_GPIO, - &__led_6, -}; - -// BUTTON -static const GPIO_InitTypeDef stm32_f4_discovery_user_button_gpio = { +// GPIOC0 +static const GPIO_InitTypeDef port_cfg_C0 = { GPIO_Pin_0, - GPIO_Mode_IN, + GPIO_Mode_OUT, GPIO_Speed_100MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL }; -static const EXTI_InitTypeDef stm32_f4_discovery_user_button_exti = { - EXTI_Line0, - EXTI_Mode_Interrupt, - EXTI_Trigger_Rising_Falling, - ENABLE -}; - -static const NVIC_InitTypeDef stm32_f4_discovery_user_button_nvic = { - EXTI0_IRQn, - 0x0F, - 0x0F, - ENABLE -}; - -static const struct stm32f4_gpio stm32_f4_discovery_user_button = { - GPIOA, - &stm32_f4_discovery_user_button_gpio, - &stm32_f4_discovery_user_button_exti, - &stm32_f4_discovery_user_button_nvic, +static const struct stm32f4_gpio stm32_f4_gpio_C0 = { + GPIOC, + &port_cfg_C0, + NULL, + NULL, NULL, NULL }; -static const struct gpio user_button = { - (void*)&stm32_f4_discovery_user_button, +static const struct gpio __gpio_c0 = { + (void*)&stm32_f4_gpio_C0, &gpio_fp }; +#ifdef TEST_APP +static const struct driver gpio_c0 = { +#else +const struct driver gpio_c0 = { +#endif + DRIVER_TYPE_GPIO, + &__gpio_c0, +}; + +// GPIO_C1 +static const GPIO_InitTypeDef port_cfg_C1 = { + GPIO_Pin_1, + GPIO_Mode_OUT, + GPIO_Speed_100MHz, + GPIO_OType_PP, + GPIO_PuPd_NOPULL +}; + +static const struct stm32f4_gpio stm32_f4_gpio_C1 = { + GPIOC, + &port_cfg_C1, + NULL, + NULL, + NULL, + NULL +}; + +static const struct gpio __gpio_c1 = { + (void*)&stm32_f4_gpio_C1, + &gpio_fp +}; + +#ifdef TEST_APP +static const struct driver gpio_c1 = { +#else +const struct driver gpio_c1 = { +#endif + DRIVER_TYPE_GPIO, + &__gpio_c1, +}; + +// GPIO_C2 +static const GPIO_InitTypeDef port_cfg_C2 = { + GPIO_Pin_2, + GPIO_Mode_OUT, + GPIO_Speed_100MHz, + GPIO_OType_PP, + GPIO_PuPd_NOPULL +}; + +static const struct stm32f4_gpio stm32_f4_gpio_C2 = { + GPIOC, + &port_cfg_C2, + NULL, + NULL, + NULL, + NULL +}; + +static const struct gpio __gpio_c2 = { + (void*)&stm32_f4_gpio_C2, + &gpio_fp +}; + +#ifdef TEST_APP +static const struct driver gpio_c2 = { +#else +const struct driver gpio_c2 = { +#endif + DRIVER_TYPE_GPIO, + &__gpio_c2, +}; + +// GPIO_C3 +static const GPIO_InitTypeDef port_cfg_C3 = { + GPIO_Pin_1, + GPIO_Mode_OUT, + GPIO_Speed_100MHz, + GPIO_OType_PP, + GPIO_PuPd_NOPULL +}; + +static const struct stm32f4_gpio stm32_f4_gpio_C3 = { + GPIOC, + &port_cfg_C3, + NULL, + NULL, + NULL, + NULL +}; + +static const struct gpio __gpio_c3 = { + (void*)&stm32_f4_gpio_C3, + &gpio_fp +}; + +#ifdef TEST_APP +static const struct driver gpio_c3 = { +#else +const struct driver gpio_c3 = { +#endif + DRIVER_TYPE_GPIO, + &__gpio_c3, +}; + //! \brief Setup the hardware of the stm32f4-discovery board. void board_init(void); diff --git a/source/firmware/arch/stm32f4xx/driver/include/stm32f4_pwm.h b/source/firmware/arch/stm32f4xx/driver/include/stm32f4_pwm.h new file mode 100644 index 0000000..c46174f --- /dev/null +++ b/source/firmware/arch/stm32f4xx/driver/include/stm32f4_pwm.h @@ -0,0 +1,40 @@ +/* + * stm32f4_pwm.h + * + * Created on: Aug 9, 2016 + * Author: tkl + */ + +#ifndef SOURCE_FIRMWARE_ARCH_STM32F4XX_DRIVER_INCLUDE_STM32F4_PWM_H_ +#define SOURCE_FIRMWARE_ARCH_STM32F4XX_DRIVER_INCLUDE_STM32F4_PWM_H_ + +enum stm32f4_pwm_channel { + channel_1 = 1, + channel_2, + channel_3, + channel_4 +}; + +struct stm32f4_pwm { + TIM_TypeDef *timer; + const TIM_TimeBaseInitTypeDef *timer_cfg; + const TIM_OCInitTypeDef *output_compare_cfg; + const TIM_BDTRInitTypeDef *bdtr_cfg; + GPIO_TypeDef *port; + uint8_t pin_src; + const GPIO_InitTypeDef *port_cfg; + enum stm32f4_pwm_channel channel; +}; + + +int stm32f4_pwm_open(const void *pwm); +int stm32f4_pwm_close(const void *pwm); +int stm32f4_pwm_set_duty_cycle(const void *pwm, unsigned int duty_cycle_percent); + +static const struct pwm_fp stm32f4_pwm_fp = { + .open = stm32f4_pwm_open, + .close = stm32f4_pwm_close, + .set_duty_cycle = stm32f4_pwm_set_duty_cycle, +}; + +#endif /* SOURCE_FIRMWARE_ARCH_STM32F4XX_DRIVER_INCLUDE_STM32F4_PWM_H_ */ diff --git a/source/firmware/arch/stm32f4xx/driver/stm32f4_pwm.c b/source/firmware/arch/stm32f4xx/driver/stm32f4_pwm.c new file mode 100644 index 0000000..80faa5d --- /dev/null +++ b/source/firmware/arch/stm32f4xx/driver/stm32f4_pwm.c @@ -0,0 +1,113 @@ +/* + * stm32f4_pwm.c + * + * Created on: Aug 9, 2016 + * Author: tkl + */ +#include +#include + +#include "stm32f4xx.h" + +#include "pwm.h" +#include "stm32f4_pwm.h" + +struct stm32f4_pwm_object { + uint8_t used_channels; + uint32_t channel_1_max_period; + uint32_t channel_2_max_period; + uint32_t channel_3_max_period; + uint32_t channel_4_max_period; +}; + +static struct stm32f4_pwm_object stm32f4_pwm_object = { + .used_channels = 0, + .channel_1_max_period = 0, + .channel_2_max_period = 0, + .channel_3_max_period = 0, + .channel_4_max_period = 0, +}; + +int stm32f4_pwm_open(const void *pwm) +{ + if(NULL == pwm) + return -1; + struct stm32f4_pwm *this = (struct stm32f4_pwm *)pwm; + uint32_t clk_ahb_timer = 0, clk_ahb_gpio = 0; + uint8_t gpio_af_timer = 0; + if(this->timer == TIM4) { + clk_ahb_timer = RCC_APB1Periph_TIM4; + gpio_af_timer = GPIO_AF_TIM4; + } + RCC_APB1PeriphClockCmd(clk_ahb_timer, ENABLE); + if(this->port == GPIOD) { + clk_ahb_gpio = RCC_AHB1Periph_GPIOD; + } + RCC_AHB1PeriphClockCmd(clk_ahb_gpio, ENABLE); + GPIO_Init(this->port, (GPIO_InitTypeDef *)this->port_cfg); + GPIO_PinAFConfig(this->port, this->pin_src, gpio_af_timer); + + TIM_TimeBaseInit(this->timer, (TIM_TimeBaseInitTypeDef *)this->timer_cfg); + + switch(this->channel) { + case channel_1: + TIM_OC1Init(this->timer, (TIM_OCInitTypeDef *)this->output_compare_cfg); + TIM_OC1PreloadConfig(this->timer, TIM_OCPreload_Enable); + stm32f4_pwm_object.channel_1_max_period = this->timer_cfg->TIM_Period + 1; + break; + case channel_2: + TIM_OC2Init(this->timer, (TIM_OCInitTypeDef *)this->output_compare_cfg); + TIM_OC2PreloadConfig(this->timer, TIM_OCPreload_Enable); + stm32f4_pwm_object.channel_2_max_period = this->timer_cfg->TIM_Period + 1; + break; + case channel_3: + TIM_OC3Init(this->timer, (TIM_OCInitTypeDef *)this->output_compare_cfg); + TIM_OC3PreloadConfig(this->timer, TIM_OCPreload_Enable); + stm32f4_pwm_object.channel_3_max_period = this->timer_cfg->TIM_Period + 1; + break; + case channel_4: + TIM_OC4Init(this->timer, (TIM_OCInitTypeDef *)this->output_compare_cfg); + TIM_OC4PreloadConfig(this->timer, TIM_OCPreload_Enable); + stm32f4_pwm_object.channel_4_max_period = this->timer_cfg->TIM_Period + 1; + break; + } + TIM_ARRPreloadConfig(this->timer, ENABLE); + TIM_Cmd(this->timer, ENABLE); + stm32f4_pwm_object.used_channels++; + return 0; +} + +int stm32f4_pwm_close(const void *pwm) +{ + if(NULL == pwm) + return -1; + struct stm32f4_pwm *this = (struct stm32f4_pwm *)pwm; + stm32f4_pwm_set_duty_cycle(pwm, 0); + stm32f4_pwm_object.used_channels--; + if(stm32f4_pwm_object.used_channels == 0) { + TIM_Cmd(this->timer, DISABLE); + } + return 0; +} + +int stm32f4_pwm_set_duty_cycle(const void *pwm, unsigned int duty_cycle_percent) +{ + if(NULL == pwm) + return -1; + struct stm32f4_pwm *this = (struct stm32f4_pwm *)pwm; + switch(this->channel) { + case channel_1: + TIM_SetCompare1(this->timer, stm32f4_pwm_object.channel_1_max_period * duty_cycle_percent / 100); + break; + case channel_2: + TIM_SetCompare2(this->timer, stm32f4_pwm_object.channel_2_max_period * duty_cycle_percent / 100); + break; + case channel_3: + TIM_SetCompare3(this->timer, stm32f4_pwm_object.channel_3_max_period * duty_cycle_percent / 100); + break; + case channel_4: + TIM_SetCompare4(this->timer, stm32f4_pwm_object.channel_4_max_period * duty_cycle_percent / 100); + break; + } + return 0; +} diff --git a/source/firmware/kernel/driver/driver.c b/source/firmware/kernel/driver/driver.c index 06b4747..654c647 100644 --- a/source/firmware/kernel/driver/driver.c +++ b/source/firmware/kernel/driver/driver.c @@ -11,6 +11,7 @@ #include "adc.h" #include "gpio.h" #include "i2c.h" +#include "pwm.h" #include "rtc.h" #include "spi.h" #include "uart.h" @@ -30,6 +31,9 @@ int open(const struct driver *driver) case DRIVER_TYPE_I2C: ret = i2c_open((struct i2c *)(driver->device_driver)); break; + case DRIVER_TYPE_PWM: + ret = pwm_open((const struct pwm *)(driver->device_driver)); + break; case DRIVER_TYPE_RTC: ret = rtc_open((const struct rtc *)(driver->device_driver)); break; @@ -58,6 +62,9 @@ int close(const struct driver *driver) case DRIVER_TYPE_I2C: ret = i2c_close((struct i2c *)(driver->device_driver)); break; + case DRIVER_TYPE_PWM: + ret = pwm_close((const struct pwm *)(driver->device_driver)); + break; case DRIVER_TYPE_RTC: ret = rtc_close((const struct rtc *)(driver->device_driver)); break; @@ -88,6 +95,9 @@ int read(const struct driver *driver, char *buffer, int len) break; case DRIVER_TYPE_I2C: break; + case DRIVER_TYPE_PWM: + ret = -1; + break; case DRIVER_TYPE_RTC: break; case DRIVER_TYPE_SPI: @@ -118,6 +128,9 @@ int write(const struct driver *driver, const char *buffer, int len) break; case DRIVER_TYPE_I2C: break; + case DRIVER_TYPE_PWM: + ret = -1; + break; case DRIVER_TYPE_RTC: break; case DRIVER_TYPE_SPI: @@ -128,3 +141,31 @@ int write(const struct driver *driver, const char *buffer, int len) } return ret; } + +int ioctl(const struct driver *driver, unsigned int cmd, const void *data) +{ + int ret = -1; + if(NULL == driver) + return ret; + switch(driver->driver_type) { + case DRIVER_TYPE_ADC: + break; + case DRIVER_TYPE_GPIO: + break; + case DRIVER_TYPE_I2C: + break; + case DRIVER_TYPE_PWM: + if(cmd == IOCTL_PWM_SET_DUTY_CYCLE) { + unsigned int *duty = (unsigned int *)data; + pwm_set_duty_cycle((const struct pwm *)(driver->device_driver), *duty); + } + break; + case DRIVER_TYPE_RTC: + break; + case DRIVER_TYPE_SPI: + break; + case DRIVER_TYPE_UART: + break; + } + return ret; +} diff --git a/source/firmware/kernel/driver/include/pwm.h b/source/firmware/kernel/driver/include/pwm.h new file mode 100644 index 0000000..6d1d883 --- /dev/null +++ b/source/firmware/kernel/driver/include/pwm.h @@ -0,0 +1,35 @@ +/* + * pwm.h + * + * Created on: Aug 9, 2016 + * Author: tkl + */ + +#ifndef SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_PWM_H_ +#define SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_PWM_H_ + +//! \brief Function pointer to the open function. +typedef int (*pwm_fp_open_t)(const void*); + +//! \brief Function pointer to the close function. +typedef int (*pwm_fp_close_t)(const void*); + +//! \brief Function pointer to the read function. +typedef int (*pwm_fp_set_duty_cycle_t)(const void*, unsigned int duty_cycle_percent); + +struct pwm_fp { + const pwm_fp_open_t open; + const pwm_fp_close_t close; + const pwm_fp_set_duty_cycle_t set_duty_cycle; +}; + +struct pwm { + const void *arch_dep_device; //!< Architecture depended pwm device (i.e. stm32f10x_pwm_t). + const struct pwm_fp *fp; //!< Function pointer for the pwm driver access. +}; + +int pwm_open(const struct pwm *device); +int pwm_close(const struct pwm *device); +int pwm_set_duty_cycle(const struct pwm *device, unsigned int duty_cycle_percent); + +#endif /* SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_PWM_H_ */ diff --git a/source/firmware/kernel/driver/pwm.c b/source/firmware/kernel/driver/pwm.c new file mode 100644 index 0000000..a8e733e --- /dev/null +++ b/source/firmware/kernel/driver/pwm.c @@ -0,0 +1,33 @@ +/* + * pwm.c + * + * Created on: Aug 9, 2016 + * Author: tkl + */ + +#include +#include + +int pwm_open(const struct pwm *device) +{ + if(NULL == device) + return -1; + pwm_fp_open_t open = device->fp->open; + return open(device->arch_dep_device); +} + +int pwm_close(const struct pwm *device) +{ + if(NULL == device) + return -1; + pwm_fp_close_t close = device->fp->close; + return close(device->arch_dep_device); +} + +int pwm_set_duty_cycle(const struct pwm *device, unsigned int duty_cycle_percent) +{ + if(NULL == device) + return -1; + pwm_fp_set_duty_cycle_t set = device->fp->set_duty_cycle; + return set(device->arch_dep_device, duty_cycle_percent); +} diff --git a/source/firmware/kernel/interface/driver.h b/source/firmware/kernel/interface/driver.h index ee82abf..14923d9 100644 --- a/source/firmware/kernel/interface/driver.h +++ b/source/firmware/kernel/interface/driver.h @@ -8,10 +8,13 @@ #ifndef SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_DRIVER_H_ #define SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_DRIVER_H_ +#define IOCTL_PWM_SET_DUTY_CYCLE 0 + enum driver_type { DRIVER_TYPE_ADC, DRIVER_TYPE_GPIO, DRIVER_TYPE_I2C, + DRIVER_TYPE_PWM, DRIVER_TYPE_RTC, DRIVER_TYPE_SPI, DRIVER_TYPE_UART @@ -26,5 +29,6 @@ int open(const struct driver *driver); int close(const struct driver *driver); int read(const struct driver *driver, char *buffer, int len); int write(const struct driver *driver, const char *buffer, int len); +int ioctl(const struct driver *driver, unsigned int cmd, const void *data); #endif /* SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_DRIVER_H_ */ diff --git a/source/firmware/kernel/include/list.h b/source/firmware/kernel/interface/list.h similarity index 100% rename from source/firmware/kernel/include/list.h rename to source/firmware/kernel/interface/list.h diff --git a/source/firmware/kernel/interface/shell.h b/source/firmware/kernel/interface/shell.h new file mode 100644 index 0000000..81fd032 --- /dev/null +++ b/source/firmware/kernel/interface/shell.h @@ -0,0 +1,22 @@ +/* + * shell.h + * + * Created on: Aug 1, 2016 + * Author: tkl + */ + +#ifndef SOURCE_FIRMWARE_KERNEL_INTERFACE_SHELL_H_ +#define SOURCE_FIRMWARE_KERNEL_INTERFACE_SHELL_H_ + +typedef void *(*command_callback)(const char*); + +struct command { + const char *command; + const command_callback command_callback; + struct list_node item; +}; + +int shell_init(const struct driver *shell_device); +int shell_add_command(struct command *command); + +#endif /* SOURCE_FIRMWARE_KERNEL_INTERFACE_SHELL_H_ */ diff --git a/source/firmware/kernel/shell.c b/source/firmware/kernel/shell.c new file mode 100644 index 0000000..3a832da --- /dev/null +++ b/source/firmware/kernel/shell.c @@ -0,0 +1,85 @@ +/* + * shell.c + * + * Created on: Aug 1, 2016 + * Author: tkl + */ + +#include +#include +#include + +#include "stack.h" +#include "queue.h" +#include "driver.h" +#include "kernel.h" +#include "list.h" +#include "shell.h" + +struct shell_object { + struct list command_list; + const struct driver *shell_device; +}; + +struct shell_object shell_object; + +#define TH_STACK_SIZE 256 +stack_t th_stack[TH_STACK_SIZE]; +struct thread_context th_ctx; + +static void parse(const char *buffer, unsigned int len) +{ + if(NULL == buffer) + return; + struct list_node *it = shell_object.command_list.front; + while(it != NULL) { + struct command *cmd = (struct command *)it->data; + if(strstr(buffer, cmd->command)) { + cmd->command_callback(buffer); + return; + } + it = it->next; + } +} + +static void rx_func(void *arg) +{ + char buffer[81]; + unsigned int index = 0; + int ret = 0; + open(shell_object.shell_device); + while(1) { + ret = read(shell_object.shell_device, &buffer[index], + sizeof(buffer) / sizeof(buffer[0]) - index - 1); + if(ret) { + write(shell_object.shell_device, &buffer[index], ret); // echo + if((buffer[index + ret - 1] == '\n') || (buffer[index + ret - 1] == '\r')) { + buffer[index + ret - 1] = '\n'; + parse(buffer, index + ret); + buffer[index + ret] = '\0'; + index = 0; + } + else + index += ret; + } + } +} + +int shell_init(const struct driver *shell_device) +{ + if(NULL == shell_device) + return -1; + list_init(&shell_object.command_list); + shell_object.shell_device = shell_device; + thread_create(&th_ctx, th_stack, TH_STACK_SIZE, rx_func, NULL, THREAD_PRIO_LOW); + return 0; +} + +int shell_add_command(struct command *command) +{ + if(NULL == command) + return -1; + command->item.data = (unsigned int) command; + list_add(&shell_object.command_list, &command->item); + return 1; +} diff --git a/source/scripts/board_interface.py b/source/scripts/board_interface.py index 3fc6c0e..a260ea8 100755 --- a/source/scripts/board_interface.py +++ b/source/scripts/board_interface.py @@ -16,7 +16,8 @@ def parse_board_file(boardfile): if match("^.*struct driver.*$", line): line = sub("^.*struct driver", "", line).strip() line = sub(" .*$","",line).strip() - ret.append(line) + if not line in ret: + ret.append(line) return ret def create_board_interface(device_list, outfile): diff --git a/source/test/pwm/main.c b/source/test/pwm/main.c new file mode 100644 index 0000000..482e65e --- /dev/null +++ b/source/test/pwm/main.c @@ -0,0 +1,119 @@ +/* + * 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" + +struct engine_ctrl { + const struct driver *pwm; + const struct driver *enable; +}; + +struct drive_ctrl { + struct engine_ctrl *left_forward; + struct engine_ctrl *left_backward; + struct engine_ctrl *right_forward; + struct engine_ctrl *right_backward; +}; + + +static struct engine_ctrl right_forward = { + .pwm = &pwm_1, + .enable = &gpio_c1, +}; + +static struct engine_ctrl right_backward = { + .pwm = &pwm_2, + .enable = &gpio_c0, +}; + +static struct engine_ctrl left_forward = { + .pwm = &pwm_3, + .enable = &gpio_c3, +}; + +static struct engine_ctrl left_backward = { + .pwm = &pwm_4, + .enable = &gpio_c2, +}; + +static struct drive_ctrl drive_ctrl = { + .left_forward = &left_forward, + .left_backward = &left_backward, + .right_forward = &right_forward, + .right_backward = &right_backward, +}; + +#define TH_STACK_SIZE 256 +stack_t th_stack[TH_STACK_SIZE]; +struct thread_context th_ctx; +static void th_func(void *arg) +{ + unsigned int duty = 0; + + /* open enable pins */ + open(drive_ctrl.left_forward->enable); + write(drive_ctrl.left_forward->enable, "0", 1); + open(drive_ctrl.left_backward->enable); + write(drive_ctrl.left_backward->enable, "0", 1); + open(drive_ctrl.right_forward->enable); + write(drive_ctrl.right_forward->enable, "0", 1); + open(drive_ctrl.right_backward->enable); + write(drive_ctrl.right_backward->enable, "0", 1); + + /* open pwm's*/ + open(drive_ctrl.left_backward->pwm); + ioctl(drive_ctrl.left_backward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); + open(drive_ctrl.left_forward->pwm); + ioctl(drive_ctrl.left_forward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); + open(drive_ctrl.right_backward->pwm); + ioctl(drive_ctrl.right_backward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); + open(drive_ctrl.right_forward->pwm); + ioctl(drive_ctrl.right_forward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); + + /* enable enable pins */ + write(drive_ctrl.left_forward->enable, "1", 1); + write(drive_ctrl.left_backward->enable, "1", 1); + write(drive_ctrl.right_forward->enable, "1", 1); + write(drive_ctrl.right_backward->enable, "1", 1); + while(1) { + duty = 0; + ioctl(drive_ctrl.left_backward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); + ioctl(drive_ctrl.right_backward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); + for(duty = 0; duty < 100; duty++) { + ioctl(drive_ctrl.left_forward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); + ioctl(drive_ctrl.right_forward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); + sleep_ms(100); + } + duty = 0; + ioctl(drive_ctrl.left_forward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); + ioctl(drive_ctrl.right_forward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); + sleep_ms(100); + for(duty = 0; duty < 100; duty++) { + ioctl(drive_ctrl.left_backward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); + ioctl(drive_ctrl.right_backward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); + sleep_ms(100); + } + sleep_ms(100); + } +} + +int main(void) +{ + thread_create(&th_ctx, th_stack, TH_STACK_SIZE, th_func, NULL, THREAD_PRIO_LOW); + schedule_start(); + + return 0; +} diff --git a/source/test/pwm/pwm.mk b/source/test/pwm/pwm.mk new file mode 100644 index 0000000..0ce5cc1 --- /dev/null +++ b/source/test/pwm/pwm.mk @@ -0,0 +1,4 @@ +CHECK_FOLDER += source/test/pwm +SUB_FOLDER += source/test/pwm +INCLUDES += source/test/pwm +DOC_SRC += source/test/pwm diff --git a/source/test/shell/main.c b/source/test/shell/main.c new file mode 100644 index 0000000..a1c4210 --- /dev/null +++ b/source/test/shell/main.c @@ -0,0 +1,39 @@ +/* + * 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) +{ + 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..d07baa2 --- /dev/null +++ b/source/test/shell/shell.mk @@ -0,0 +1,4 @@ +CHECK_FOLDER += source/test/shell +SUB_FOLDER += source/test/shell +INCLUDES += source/test/shell +DOC_SRC += source/test/shell diff --git a/source/test/test.mk b/source/test/test.mk new file mode 100644 index 0000000..e16b6ff --- /dev/null +++ b/source/test/test.mk @@ -0,0 +1,6 @@ +ifeq ($(TEST_APP), shell) +include source/test/shell/shell.mk +endif +ifeq ($(TEST_APP), pwm) +include source/test/pwm/pwm.mk +endif