From 7f89aea2016ebde61b02914abc7984df50537f29 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 25 Jan 2017 22:02:09 +0100 Subject: o Improving the mcu-stm32 cmake library a bit. Starting on a USB example. --- CMakeLists.txt | 1 + I2C.md | 28 ++++++++++ apps/CMakeLists.txt | 2 +- apps/dma1/CMakeLists.txt | 19 ++++--- apps/test1/test1.cpp | 101 ---------------------------------- cmake/mcu-stm32/CMakeLists.txt | 76 +++++++++++++++++++++++++ cmake/mcu-stm32/conf/stm32f10x_conf.h | 18 ++++++ cmake/stm32.toolchain.cmake | 17 +++++- gdb-apps | 17 ++++++ 9 files changed, 166 insertions(+), 113 deletions(-) create mode 100644 I2C.md delete mode 100644 apps/test1/test1.cpp create mode 100644 cmake/mcu-stm32/CMakeLists.txt create mode 100644 cmake/mcu-stm32/conf/stm32f10x_conf.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bb47da..fbc73aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,4 +59,5 @@ target_include_directories(playground PUBLIC ) target_compile_definitions(playground PUBLIC ${STM32F10X_STDPERIPH_DEFINES}) +add_subdirectory(cmake/mcu-stm32) add_subdirectory(apps) diff --git a/I2C.md b/I2C.md new file mode 100644 index 0000000..7c76c6e --- /dev/null +++ b/I2C.md @@ -0,0 +1,28 @@ +# Registers + +## I2C_SR1 + +| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | +|-------|-------|-------|-------|-------|-------|-------|-------|-----|------|------|-------|-------|-----|------|----| +| SMB | TIME | Res. | PEC | OVR | AF | ARLO | BERR | TxE | RxNE | Res. | STOPF | ADD10 | BTF | ADDR | SB | +| ALERT | OUT | | ERR | | | | | | | | | | | | | +| rc_w0 | rc_w0 | | rc_w0 | rc_w0 | rc_w0 | rc_w0 | rc_w0 | r | r | | r | r | r | r | r | + +## I2C_SR2 + +| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | +| PEC[7:0] | DUALF | SMB | SMBDE | GEN | Res. | TRA | BUSY | MSL | +| | | HOST | FAULT | CALL | | | | | +| r | r | r | r | r | r | r | r | r | r | r | r | | r | r | r | + + + + Expected: 00030040: + + SR2: 0000 0000 0000 0011 = BUSY, MSL + SR1: 0000 0000 0100 0000 = RxNE + + Actual: 0x00070084: + SR2: 0000 0000 0000 0111 = TRA, BUSY, MSL + SR1: 0000 0000 1000 0100 = TxE, BTF + diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index ae23c31..90885ed 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -1,10 +1,10 @@ add_subdirectory(can1) add_subdirectory(cpp1) add_subdirectory(dma1) +add_subdirectory(i2c1) add_subdirectory(os1) add_subdirectory(os2) add_subdirectory(serial1) add_subdirectory(serial2) add_subdirectory(stepper1) add_subdirectory(stepper2) -add_subdirectory(test1) diff --git a/apps/dma1/CMakeLists.txt b/apps/dma1/CMakeLists.txt index 689e437..19321d8 100644 --- a/apps/dma1/CMakeLists.txt +++ b/apps/dma1/CMakeLists.txt @@ -1,19 +1,20 @@ +mcu_stm32_add_library(TARGET dma1-mcu-stm32 + DEVICE STM32F10X_MD + PARTS SPI GPIO DMA + DEFINES_VAR DEFINES_FROM_MCU_STM32) + add_executable(dma1.elf dma1.cpp - ${STM32F10X_STDPERIPH_LIB}/Libraries/CMSIS/CM3/CoreSupport/core_cm3.c - ${STM32F10X_STDPERIPH_LIB}/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.c - ${STM32F10X_STDPERIPH_LIB}/Libraries/STM32F10x_StdPeriph_Driver/src/misc.c - ${STM32F10X_STDPERIPH_LIB}/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_rcc.c - ${STM32F10X_STDPERIPH_LIB}/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_spi.c - ${STM32F10X_STDPERIPH_LIB}/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_gpio.c - ${STM32F10X_STDPERIPH_LIB}/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_dma.c - $ + ${PLAYGROUND_DIR}/src/init_low.s ${PLAYGROUND_DIR}/src/init_high.cpp ${PLAYGROUND_DIR}/include/init_high.h + ${PLAYGROUND_DIR}/include/playground.h + ${PLAYGROUND_DIR}/src/debug.cpp ${PLAYGROUND_DIR}/include/debug.h ) target_include_directories(dma1.elf PUBLIC $ ) -target_compile_definitions(dma1.elf PUBLIC ${STM32F10X_STDPERIPH_DEFINES}) +target_compile_definitions(dma1.elf PUBLIC ${DEFINES_FROM_MCU_STM32}) target_link_libraries(dma1.elf tinyprintf) +target_link_libraries(dma1.elf dma1-mcu-stm32) set_target_properties(dma1.elf PROPERTIES LINK_FLAGS "-nostartfiles -T${CMAKE_SOURCE_DIR}/cmake/stm32.ld") add_extra_commands(dma1.elf) diff --git a/apps/test1/test1.cpp b/apps/test1/test1.cpp deleted file mode 100644 index a883876..0000000 --- a/apps/test1/test1.cpp +++ /dev/null @@ -1,101 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "playground.h" - -extern "C" -__attribute__((naked)) -void HardFault_Handler_C(uint32_t *hardfault_args); - -extern "C" void high(); -extern "C" void low(); - -SCB_Type *__SCB = ((SCB_Type *) SCB_BASE); -//extern SCB_Type *__SCB; - -struct hardfault_data_t { - uint32_t r0; - uint32_t r1; - uint32_t r2; - uint32_t r3; - uint32_t r12; - uint32_t lr; - uint32_t pc; - uint32_t psr; -}; - -volatile struct hardfault_data_t *hardfault_data = (volatile struct hardfault_data_t *) 0x20000800; - -void HardFault_Handler_C(uint32_t *hardfault_args) { - hardfault_data->r0 = hardfault_args[0]; - hardfault_data->r1 = hardfault_args[1]; - hardfault_data->r2 = hardfault_args[2]; - hardfault_data->r3 = hardfault_args[3]; - hardfault_data->r12 = hardfault_args[4]; - hardfault_data->lr = hardfault_args[5]; - hardfault_data->pc = hardfault_args[6]; - hardfault_data->psr = hardfault_args[7]; - - halt(); -} - -void send_command(int command, void *message) { - bool active = (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) == CoreDebug_DHCSR_C_DEBUGEN_Msk; - - if (!active) { - return; - } - - __asm volatile ( - "mov r0, %[cmd];" - "mov r1, %[msg];" - "bkpt #0xAB" : : [cmd] "r"(command), [msg] "r"(message) : "r0", "r1", "memory" - ); -} - -size_t strlen(const char *s) { - size_t size = 0; - while (*s++ != '\0') size++; - return size; -} - -int run = 1; - -/* - * When we get there the stack pointer is set - */ -int main() { - SystemInit(); -// SystemCoreClockUpdate(); - - SCB->SHCSR |= SCB_SHCSR_USGFAULTENA_Msk | SCB_SHCSR_MEMFAULTPENDED_Msk | SCB_SHCSR_BUSFAULTENA_Msk; - - RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, ENABLE); - RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, DISABLE); - - RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, ENABLE); - RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, DISABLE); - - RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA, - ENABLE); - - GPIO_InitTypeDef init; - GPIO_StructInit(&init); - init.GPIO_Mode = GPIO_Mode_Out_PP; - GPIO_Init(GPIOB, &init); - - while (run) { -// GPIO_SetBits(GPIOA, GPIO_Pin_All); - GPIO_SetBits(GPIOB, GPIO_Pin_All); -// GPIO_SetBits(GPIOC, GPIO_Pin_All); - -// GPIO_ResetBits(GPIOA, GPIO_Pin_All); - GPIO_ResetBits(GPIOB, GPIO_Pin_All); -// GPIO_ResetBits(GPIOC, GPIO_Pin_All); - } - - return 0; -} diff --git a/cmake/mcu-stm32/CMakeLists.txt b/cmake/mcu-stm32/CMakeLists.txt new file mode 100644 index 0000000..424d3ab --- /dev/null +++ b/cmake/mcu-stm32/CMakeLists.txt @@ -0,0 +1,76 @@ +set(MCU_BASEDIR "${CMAKE_CURRENT_LIST_DIR}" CACHE INTERNAL "foo" FORCE) + +if (POLICY CMP0057) + cmake_policy(SET CMP0057 NEW) +else () + # We need IN_LIST + message(FATAL_ERROR "CMake 3.3+ is required") +endif () + +function(mcu_stm32_add_library) + set(options) + set(oneValueArgs TARGET DEVICE DEFAULT_ASSERT_PARAM DEFINES_VAR) + set(multiValueArgs PARTS) + cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + # TODO: validate the device + + add_library(${ARG_TARGET} + ${STM32F10X_STDPERIPH_LIB}/Libraries/CMSIS/CM3/CoreSupport/core_cm3.c + ${STM32F10X_STDPERIPH_LIB}/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.c + ${STM32F10X_STDPERIPH_LIB}/Libraries/STM32F10x_StdPeriph_Driver/src/misc.c + ${STM32F10X_STDPERIPH_LIB}/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_rcc.c + ) + list(REMOVE_ITEM ARG_PARTS RCC) + + set(EXPORT_DEFINES ${ARG_DEVICE} + USE_STDPERIPH_DRIVER) + + target_include_directories(${ARG_TARGET} PUBLIC + ${STM32F10X_STDPERIPH_LIB}/Libraries/CMSIS/CM3/CoreSupport + ${STM32F10X_STDPERIPH_LIB}/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x + ${STM32F10X_STDPERIPH_LIB}/Libraries/STM32F10x_StdPeriph_Driver/inc + ) + + if (NOT DEFINED ARG_DEFAULT_ASSERT_PARAM) + set(ARG_DEFAULT_ASSERT_PARAM TRUE) + endif () + + if (ARG_DEFAULT_ASSERT_PARAM) + target_include_directories(${ARG_TARGET} PUBLIC ${MCU_BASEDIR}/conf) + endif () + + if (DMA IN_LIST ARG_PARTS) + list(REMOVE_ITEM ARG_PARTS DMA) + target_sources(${ARG_TARGET} PRIVATE ${STM32F10X_STDPERIPH_LIB}/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_dma.c) + endif () + + if (GPIO IN_LIST ARG_PARTS) + list(REMOVE_ITEM ARG_PARTS GPIO) + target_sources(${ARG_TARGET} PRIVATE ${STM32F10X_STDPERIPH_LIB}/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_gpio.c) + endif () + + if (SPI IN_LIST ARG_PARTS) + list(REMOVE_ITEM ARG_PARTS SPI) + target_sources(${ARG_TARGET} PRIVATE ${STM32F10X_STDPERIPH_LIB}/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_spi.c) + endif () + + if (USART IN_LIST ARG_PARTS) + list(REMOVE_ITEM ARG_PARTS USART) + target_sources(${ARG_TARGET} PRIVATE ${STM32F10X_STDPERIPH_LIB}/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_usart.c) + endif () + + list(LENGTH ARG_PARTS l) + if (${l} GREATER 0) + message(FATAL_ERROR "Unknown parts: ${ARG_PARTS}") + endif () + + if (DEFINES_VAR) + set(" ${DEFINES_FROM_MCU_STM32} " ${EXPORT_DEFINES} PARENT_SCOPE) + endif () + + # Set all the defines we want the consumers to use on these objects too + target_compile_definitions(${ARG_TARGET} PUBLIC + ${EXPORT_DEFINES}) + +endfunction() diff --git a/cmake/mcu-stm32/conf/stm32f10x_conf.h b/cmake/mcu-stm32/conf/stm32f10x_conf.h new file mode 100644 index 0000000..c8f4a4d --- /dev/null +++ b/cmake/mcu-stm32/conf/stm32f10x_conf.h @@ -0,0 +1,18 @@ +#ifndef __STM32F10x_CONF_H +#define __STM32F10x_CONF_H + +#ifdef USE_FULL_ASSERT +#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) + +#ifdef __cplusplus +extern "C" { +#endif + void assert_failed(uint8_t* file, uint32_t line); +#ifdef __cplusplus +} +#endif +#else +#define assert_param(expr) ((void)0) +#endif + +#endif diff --git a/cmake/stm32.toolchain.cmake b/cmake/stm32.toolchain.cmake index 05a9090..3d39bce 100644 --- a/cmake/stm32.toolchain.cmake +++ b/cmake/stm32.toolchain.cmake @@ -37,8 +37,21 @@ set(LINKER_FLAGS "-nostdlib -Wl,--gc-sections ${TARGET_FLAGS}") set(CMAKE_EXE_LINKER_FLAGS "${LINKER_FLAGS}" CACHE STRING "linker flags" FORCE) -cmake_force_c_compiler("${TOOLCHAIN_ROOT}/bin/${TRIPLE}-gcc" GNU) -cmake_force_cxx_compiler("${TOOLCHAIN_ROOT}/bin/${TRIPLE}-g++" GNU) +# Old style +#cmake_force_c_compiler("${TOOLCHAIN_ROOT}/bin/${TRIPLE}-gcc" GNU) +#cmake_force_cxx_compiler("${TOOLCHAIN_ROOT}/bin/${TRIPLE}-g++" GNU) + +find_program(ARM_CC arm-none-eabi-gcc ${TOOLCHAIN_ROOT}/bin) +find_program(ARM_CXX arm-none-eabi-g++ ${TOOLCHAIN_ROOT}/bin) +find_program(ARM_OBJCOPY arm-none-eabi-objcopy ${TOOLCHAIN_ROOT}/bin) +find_program(ARM_SIZE arm-none-eabi-size ${TOOLCHAIN_ROOT}/bin) +find_program(ARM_NM arm-none-eabi-nm ${TOOLCHAIN_ROOT}/bin) + +# New style, from 3.6 +set(CMAKE_C_COMPILER ${ARM_CC} CACHE FILE "") +set(CMAKE_CXX_COMPILER ${ARM_CXX} CACHE FILE "") +set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) + # search for programs in the build host directories set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) # for libraries and headers in the target directories diff --git a/gdb-apps b/gdb-apps index f4be9b7..1850c52 100644 --- a/gdb-apps +++ b/gdb-apps @@ -31,6 +31,23 @@ define flash_can1_b set variable is_client = 1 end +define flash_dma1 + shell cd build && make dma1.elf + + monitor arm semihosting enable + monitor reset halt + + set confirm off + file build/apps/dma1/dma1.elf + load build/apps/dma1/dma1.elf + set confirm on + + hbreak halt + hbreak main + c + set variable is_client = 1 +end + define flash_test1 shell cd build && make test1.elf -- cgit v1.2.3