From 532793024452674f844919a818f6c86c73164ac5 Mon Sep 17 00:00:00 2001 From: adustm Date: Tue, 7 Feb 2017 18:29:14 +0100 Subject: [PATCH 01/40] fix for issue #3715: correction in startup files for ARM and IAR, alignment of system_stm32f429xx.c files --- .../TARGET_DISCO_F429ZI/system_stm32f4xx.c | 10 ++++++---- .../TARGET_NUCLEO_F429ZI/system_stm32f4xx.c | 13 +------------ .../TOOLCHAIN_ARM_MICRO/startup_stm32f429xx.s | 6 ++++++ .../device/TOOLCHAIN_ARM_STD/startup_stm32f429xx.s | 6 ++++++ .../device/TOOLCHAIN_IAR/startup_stm32f429xx.S | 6 ++++++ 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_DISCO_F429ZI/system_stm32f4xx.c b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_DISCO_F429ZI/system_stm32f4xx.c index 82b3265504..c8a7fa8909 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_DISCO_F429ZI/system_stm32f4xx.c +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_DISCO_F429ZI/system_stm32f4xx.c @@ -38,7 +38,7 @@ ****************************************************************************** * @attention * - *

© COPYRIGHT 2015 STMicroelectronics

+ *

© COPYRIGHT 2017 STMicroelectronics

* * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: @@ -735,11 +735,13 @@ void SystemInit_ExtMemCtl(void) /* No pull-up, pull-down for PGx pins */ GPIOG->PUPDR = 0x00000000; -/*-- FMC/FSMC Configuration --------------------------------------------------*/ +/*-- FMC/FSMC Configuration --------------------------------------------------*/ /* Enable the FMC/FSMC interface clock */ RCC->AHB3ENR |= 0x00000001; - -#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) + +#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) + /* Delay after an RCC peripheral clock enabling */ + tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN); /* Configure and enable Bank1_SRAM2 */ FMC_Bank1->BTCR[2] = 0x00001011; FMC_Bank1->BTCR[3] = 0x00000201; diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_NUCLEO_F429ZI/system_stm32f4xx.c b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_NUCLEO_F429ZI/system_stm32f4xx.c index 8394e8823e..362a3ac795 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_NUCLEO_F429ZI/system_stm32f4xx.c +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_NUCLEO_F429ZI/system_stm32f4xx.c @@ -38,7 +38,7 @@ ****************************************************************************** * @attention * - *

© COPYRIGHT 2016 STMicroelectronics

+ *

© COPYRIGHT 2017 STMicroelectronics

* * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: @@ -188,10 +188,6 @@ void SetSysClock(void); */ void SystemInit(void) { - /* FPU settings ------------------------------------------------------------*/ - #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) - SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ - #endif /* Reset the RCC clock configuration to the default reset state ------------*/ /* Set HSION bit */ RCC->CR |= (uint32_t)0x00000001; @@ -215,13 +211,6 @@ void SystemInit(void) SystemInit_ExtMemCtl(); #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */ - /* Configure the Vector Table location add offset address ------------------*/ -#ifdef VECT_TAB_SRAM - SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ -#else - SCB->VTOR = NVIC_FLASH_VECTOR_ADDRESS; /* Vector Table Relocation in Internal FLASH */ -#endif - /* Configure the Cube driver */ SystemCoreClock = 16000000; // At this stage the HSI is used as system clock HAL_Init(); diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_MICRO/startup_stm32f429xx.s b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_MICRO/startup_stm32f429xx.s index c175588c0d..98fa2ebd32 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_MICRO/startup_stm32f429xx.s +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_MICRO/startup_stm32f429xx.s @@ -197,9 +197,15 @@ __Vectors_Size EQU __Vectors_End - __Vectors ; Reset handler Reset_Handler PROC EXPORT Reset_Handler [WEAK] + IMPORT SystemInitPre + IMPORT HAL_InitPre IMPORT SystemInit IMPORT __main + LDR R0, =SystemInitPre + BLX R0 + LDR R0, =HAL_InitPre + BLX R0 LDR R0, =SystemInit BLX R0 LDR R0, =__main diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_STD/startup_stm32f429xx.s b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_STD/startup_stm32f429xx.s index ad3a376725..2acea79cfa 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_STD/startup_stm32f429xx.s +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_STD/startup_stm32f429xx.s @@ -170,9 +170,15 @@ __Vectors_Size EQU __Vectors_End - __Vectors ; Reset handler Reset_Handler PROC EXPORT Reset_Handler [WEAK] + IMPORT SystemInitPre + IMPORT HAL_InitPre IMPORT SystemInit IMPORT __main + LDR R0, =SystemInitPre + BLX R0 + LDR R0, =HAL_InitPre + BLX R0 LDR R0, =SystemInit BLX R0 LDR R0, =__main diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_IAR/startup_stm32f429xx.S b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_IAR/startup_stm32f429xx.S index 6c829d34a1..6d95580872 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_IAR/startup_stm32f429xx.S +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_IAR/startup_stm32f429xx.S @@ -63,6 +63,8 @@ SECTION .intvec:CODE:NOROOT(2) EXTERN __iar_program_start + EXTERN SystemInitPre + EXTERN HAL_InitPre EXTERN SystemInit PUBLIC __vector_table @@ -188,6 +190,10 @@ __vector_table SECTION .text:CODE:REORDER:NOROOT(2) Reset_Handler + LDR R0, =SystemInitPre + BLX R0 + LDR R0, =HAL_InitPre + BLX R0 LDR R0, =SystemInit BLX R0 LDR R0, =__iar_program_start From 14065c34a893788e4c1c5facc45a95d2fa791fbd Mon Sep 17 00:00:00 2001 From: jeromecoutant Date: Tue, 21 Feb 2017 16:41:31 +0100 Subject: [PATCH 02/40] STM32 remove warning in hal_tick_32b.c file --- targets/TARGET_STM/hal_tick_32b.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_STM/hal_tick_32b.c b/targets/TARGET_STM/hal_tick_32b.c index fb5229d4e0..e8370f3c10 100644 --- a/targets/TARGET_STM/hal_tick_32b.c +++ b/targets/TARGET_STM/hal_tick_32b.c @@ -22,6 +22,8 @@ extern TIM_HandleTypeDef TimMasterHandle; +extern void HAL_IncTick(void); + volatile uint32_t PreviousVal = 0; void us_ticker_irq_handler(void); @@ -90,8 +92,7 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) if (RCC_ClkInitStruct.APB2CLKDivider == RCC_HCLK_DIV1) { #endif TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick - } - else { + } else { TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick } From c57bce96b6394bfc5cb8c25b7db48a6c5517140e Mon Sep 17 00:00:00 2001 From: Pierre-Marie Ancele Date: Wed, 22 Feb 2017 16:44:28 +0100 Subject: [PATCH 03/40] Fix GPIOG usage of STM32L4 by activating VDDIO2 power supply --- targets/TARGET_STM/gpio_api.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/targets/TARGET_STM/gpio_api.c b/targets/TARGET_STM/gpio_api.c index bbcf53fc4c..e4075cfc4a 100644 --- a/targets/TARGET_STM/gpio_api.c +++ b/targets/TARGET_STM/gpio_api.c @@ -73,6 +73,10 @@ GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx) { #endif #if defined GPIOG_BASE case PortG: +#if defined STM32L4 + __HAL_RCC_PWR_CLK_ENABLE(); + HAL_PWREx_EnableVddIO2(); +#endif gpio_add = GPIOG_BASE; __GPIOG_CLK_ENABLE(); break; From 44c60b55a6b0c329445ad5c693aae14b1df2b709 Mon Sep 17 00:00:00 2001 From: Pierre-Marie Ancele Date: Thu, 23 Feb 2017 09:32:12 +0100 Subject: [PATCH 04/40] Use #if defined TARGET_STM32L4 --- targets/TARGET_STM/gpio_api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/targets/TARGET_STM/gpio_api.c b/targets/TARGET_STM/gpio_api.c index e4075cfc4a..d309b95693 100644 --- a/targets/TARGET_STM/gpio_api.c +++ b/targets/TARGET_STM/gpio_api.c @@ -73,7 +73,7 @@ GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx) { #endif #if defined GPIOG_BASE case PortG: -#if defined STM32L4 +#if defined TARGET_STM32L4 __HAL_RCC_PWR_CLK_ENABLE(); HAL_PWREx_EnableVddIO2(); #endif From 86fa3efa154f608c59da6c7d8e2007f51a6a9175 Mon Sep 17 00:00:00 2001 From: "U-ONSEMI\\\\fg64rh" Date: Thu, 23 Feb 2017 18:47:15 +0530 Subject: [PATCH 05/40] Conflict resolved --- targets/targets.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/targets/targets.json b/targets/targets.json index b03789af93..ee38b950b9 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -2600,7 +2600,7 @@ "post_binary_hook": {"function": "NCS36510TargetCode.ncs36510_addfib"}, "macros": ["CM3", "CPU_NCS36510", "TARGET_NCS36510", "LOAD_ADDRESS=0x3000"], "supported_toolchains": ["GCC_ARM", "ARM", "IAR"], - "device_has": ["ANALOGIN", "SERIAL", "I2C", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "LOWPOWERTIMER", "TRNG"], + "device_has": ["ANALOGIN", "SERIAL", "I2C", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "LOWPOWERTIMER", "TRNG", "SPISLAVE"], "release_versions": ["2", "5"] }, "NUMAKER_PFM_M453": { From 7521e30ec6b5b0894c361fbc92689d74f0a94214 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Thu, 23 Feb 2017 12:18:43 -0600 Subject: [PATCH 06/40] lwip: Increase timeout on tests Sometimes when under heavy load, the CI machines can take a significant amount of time to bring up a python process (~10s). The timeouts for the network tests were chosen without much thought, and didn't leave much room for this sort of delay. This patch brings up timeouts for ntetwork tests 20s -> 60s --- .../FEATURE_LWIP/TESTS/mbedmicro-net/gethostbyname/main.cpp | 2 +- features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo/main.cpp | 2 +- .../FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo_parallel/main.cpp | 2 +- .../FEATURE_LWIP/TESTS/mbedmicro-net/tcp_hello_world/main.cpp | 2 +- .../TESTS/mbedmicro-net/udp_dtls_handshake/main.cpp | 2 +- features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo/main.cpp | 2 +- .../FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo_parallel/main.cpp | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/gethostbyname/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/gethostbyname/main.cpp index 247bc159b7..a3d9914dbd 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/gethostbyname/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/gethostbyname/main.cpp @@ -91,7 +91,7 @@ void test_dns_literal_pref() { // Test setup utest::v1::status_t test_setup(const size_t number_of_cases) { - GREENTEA_SETUP(20, "default_auto"); + GREENTEA_SETUP(60, "default_auto"); net_bringup(); return verbose_test_setup_handler(number_of_cases); } diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo/main.cpp index a46f55255e..6a6750b19c 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo/main.cpp @@ -29,7 +29,7 @@ void prep_buffer(char *tx_buffer, size_t tx_size) { } int main() { - GREENTEA_SETUP(20, "tcp_echo"); + GREENTEA_SETUP(60, "tcp_echo"); EthernetInterface eth; eth.connect(); diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo_parallel/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo_parallel/main.cpp index 14bdcea016..15036a2f4a 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo_parallel/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo_parallel/main.cpp @@ -88,7 +88,7 @@ Echo echoers[MBED_CFG_TCP_CLIENT_ECHO_THREADS]; int main() { - GREENTEA_SETUP(20, "tcp_echo"); + GREENTEA_SETUP(60, "tcp_echo"); int err = net.connect(); TEST_ASSERT_EQUAL(0, err); diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_hello_world/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_hello_world/main.cpp index 7040e44f80..c6ddb33bfa 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_hello_world/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_hello_world/main.cpp @@ -36,7 +36,7 @@ bool find_substring(const char *first, const char *last, const char *s_first, co } int main() { - GREENTEA_SETUP(20, "default_auto"); + GREENTEA_SETUP(60, "default_auto"); bool result = false; EthernetInterface eth; diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_dtls_handshake/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_dtls_handshake/main.cpp index 462c74296b..d2368eebb0 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_dtls_handshake/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_dtls_handshake/main.cpp @@ -28,7 +28,7 @@ int udp_dtls_handshake_pattern[] = {MBED_CFG_UDP_DTLS_HANDSHAKE_PATTERN}; const int udp_dtls_handshake_count = sizeof(udp_dtls_handshake_pattern) / sizeof(int); int main() { - GREENTEA_SETUP(20, "udp_shotgun"); + GREENTEA_SETUP(60, "udp_shotgun"); EthernetInterface eth; int err = eth.connect(); diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo/main.cpp index 3c1b927656..cbd15604fc 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo/main.cpp @@ -34,7 +34,7 @@ void prep_buffer(char *tx_buffer, size_t tx_size) { } int main() { - GREENTEA_SETUP(20, "udp_echo"); + GREENTEA_SETUP(60, "udp_echo"); EthernetInterface eth; eth.connect(); diff --git a/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo_parallel/main.cpp b/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo_parallel/main.cpp index b043defaa6..3f42d00c3b 100644 --- a/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo_parallel/main.cpp +++ b/features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo_parallel/main.cpp @@ -99,7 +99,7 @@ Echo echoers[MBED_CFG_UDP_CLIENT_ECHO_THREADS]; int main() { - GREENTEA_SETUP(20, "udp_echo"); + GREENTEA_SETUP(60, "udp_echo"); int err = net.connect(); TEST_ASSERT_EQUAL(0, err); From 6a5d2045a7cf71741c7c27aca4aac34a2b1dd3b2 Mon Sep 17 00:00:00 2001 From: Hovik Melikyan Date: Fri, 24 Feb 2017 11:24:24 +0000 Subject: [PATCH 07/40] Allow to redefine nRF's PSTORAGE_NUM_OF_PAGES outside of the mbed-os source By default the number of pstorage pages is set 1 and all addresses are calculated in the pstorage module accordingly. Nordic recommends changing this macro to whatever number is suitable for the app (see https://devzone.nordicsemi.com/question/53066/what-will-be-the-starting- address-of-pstorage-page-how-we-can-change-it/?answer=53085#post-id-5308 5) which is not quite elegant given that pstorage_platform.h is part of the mbed-os repo. With this modification you can e.g. define PSTORAGE_NUM_OF_PAGES on the command line, however note that you should rebuild mbed-os with this setting as it affects pstorage_platform.c. --- .../sdk/source/drivers_nrf/pstorage/config/pstorage_platform.h | 3 +++ .../sdk/drivers_nrf/pstorage/config/pstorage_platform.h | 3 +++ 2 files changed, 6 insertions(+) diff --git a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_MCU_NRF51822/sdk/source/drivers_nrf/pstorage/config/pstorage_platform.h b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_MCU_NRF51822/sdk/source/drivers_nrf/pstorage/config/pstorage_platform.h index 73540d07d8..2ccca528a6 100644 --- a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_MCU_NRF51822/sdk/source/drivers_nrf/pstorage/config/pstorage_platform.h +++ b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_MCU_NRF51822/sdk/source/drivers_nrf/pstorage/config/pstorage_platform.h @@ -66,7 +66,10 @@ static __INLINE uint32_t pstorage_flash_page_end() #define PSTORAGE_FLASH_PAGE_END pstorage_flash_page_end() +#ifndef PSTORAGE_NUM_OF_PAGES #define PSTORAGE_NUM_OF_PAGES 1 /**< Number of flash pages allocated for the pstorage module excluding the swap page, configurable based on system requirements. */ +#endif + #define PSTORAGE_MIN_BLOCK_SIZE 0x0010 /**< Minimum size of block that can be registered with the module. Should be configured based on system requirements, recommendation is not have this value to be at least size of word. */ #define PSTORAGE_DATA_START_ADDR ((PSTORAGE_FLASH_PAGE_END - PSTORAGE_NUM_OF_PAGES - 1) \ diff --git a/targets/TARGET_NORDIC/TARGET_NRF5/sdk/drivers_nrf/pstorage/config/pstorage_platform.h b/targets/TARGET_NORDIC/TARGET_NRF5/sdk/drivers_nrf/pstorage/config/pstorage_platform.h index 113fd637fc..b0029bcc25 100644 --- a/targets/TARGET_NORDIC/TARGET_NRF5/sdk/drivers_nrf/pstorage/config/pstorage_platform.h +++ b/targets/TARGET_NORDIC/TARGET_NRF5/sdk/drivers_nrf/pstorage/config/pstorage_platform.h @@ -67,7 +67,10 @@ static __INLINE uint32_t pstorage_flash_page_end() #define PSTORAGE_FLASH_PAGE_END pstorage_flash_page_end() +#ifndef PSTORAGE_NUM_OF_PAGES #define PSTORAGE_NUM_OF_PAGES 1 /**< Number of flash pages allocated for the pstorage module excluding the swap page, configurable based on system requirements. */ +#endif + #define PSTORAGE_MIN_BLOCK_SIZE 0x0010 /**< Minimum size of block that can be registered with the module. Should be configured based on system requirements, recommendation is not have this value to be at least size of word. */ #define PSTORAGE_DATA_START_ADDR ((PSTORAGE_FLASH_PAGE_END - PSTORAGE_NUM_OF_PAGES - 1) \ From ed6d03b9cc9dac8a8ee4810d19f8c76f80429ee2 Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Wed, 18 Jan 2017 18:51:07 +0100 Subject: [PATCH 08/40] STM32: gpio SPEED - always set High Speed by default Up to now, speed was set for outputs and alternate, but this is also valid for input configuration. By default, let's configure high speed. This is done firts, because speed for some families like F1 is mixed with Input/Output mode settings, so can be later over-ridden if needed. --- targets/TARGET_STM/pinmap.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/targets/TARGET_STM/pinmap.c b/targets/TARGET_STM/pinmap.c index 2546378af9..3a63263f86 100644 --- a/targets/TARGET_STM/pinmap.c +++ b/targets/TARGET_STM/pinmap.c @@ -71,6 +71,16 @@ void pin_function(PinName pin, int data) // Enable GPIO clock GPIO_TypeDef *gpio = Set_GPIO_Clock(port); + /* Set default speed to high. + * This is done before other settings on purpose: + * For most families there are dedicated registers so it is + * not so important, register can be set at any time. + * But for families like F1, speed only applies to output. so we set + * it here, and then if input is selected, this setting might be + * overriden by the input one. + */ + LL_GPIO_SetPinSpeed(gpio, ll_pin, LL_GPIO_SPEED_FREQ_HIGH); + switch (mode) { case STM_PIN_INPUT: ll_mode = LL_GPIO_MODE_INPUT; @@ -103,7 +113,6 @@ void pin_function(PinName pin, int data) /* For now by default use Speed HIGH for output or alt modes */ if ((mode == STM_PIN_OUTPUT) ||(mode == STM_PIN_ALTERNATE)) { - LL_GPIO_SetPinSpeed(gpio, ll_pin, LL_GPIO_SPEED_FREQ_HIGH); if (STM_PIN_OD(data)) { LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_OPENDRAIN); } else { From 7374f9cc877394c18d45acde11205f772d94d689 Mon Sep 17 00:00:00 2001 From: bcostm Date: Mon, 27 Feb 2017 16:32:44 +0100 Subject: [PATCH 09/40] Typo: update comment (GPIO_IP_WITHOUT_BRR) --- targets/TARGET_STM/gpio_object.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/targets/TARGET_STM/gpio_object.h b/targets/TARGET_STM/gpio_object.h index b4285cc766..230c8a0e26 100644 --- a/targets/TARGET_STM/gpio_object.h +++ b/targets/TARGET_STM/gpio_object.h @@ -43,7 +43,7 @@ extern "C" { /* * Note: reg_clr might actually be same as reg_set. * Depends on family whether BRR is available on top of BSRR - * if BRR does not exist, family shall define GPIO_DOES_NOT_HAVE_BRR + * if BRR does not exist, family shall define GPIO_IP_WITHOUT_BRR */ typedef struct { uint32_t mask; From ef6f87ab0d599df543344b5d418c8334dce09fe9 Mon Sep 17 00:00:00 2001 From: Simon Hughes Date: Mon, 27 Feb 2017 17:37:30 +0000 Subject: [PATCH 10/40] STORAGE: removal of unsupported tests having ported to https://github.com/ARMmbed/sd-driver basic.cpp test. --- .../unsupported/tests/mbed/dir_sd/main.cpp | 124 ------------- features/unsupported/tests/mbed/file/main.cpp | 78 -------- features/unsupported/tests/mbed/fs/main.cpp | 32 ---- features/unsupported/tests/mbed/sd/main.cpp | 120 ------------- .../tests/mbed/sd_perf_fatfs/main.cpp | 168 ------------------ .../tests/mbed/sd_perf_fhandle/main.cpp | 163 ----------------- .../tests/mbed/sd_perf_stdio/main.cpp | 163 ----------------- 7 files changed, 848 deletions(-) delete mode 100644 features/unsupported/tests/mbed/dir_sd/main.cpp delete mode 100644 features/unsupported/tests/mbed/file/main.cpp delete mode 100644 features/unsupported/tests/mbed/fs/main.cpp delete mode 100644 features/unsupported/tests/mbed/sd/main.cpp delete mode 100644 features/unsupported/tests/mbed/sd_perf_fatfs/main.cpp delete mode 100644 features/unsupported/tests/mbed/sd_perf_fhandle/main.cpp delete mode 100644 features/unsupported/tests/mbed/sd_perf_stdio/main.cpp diff --git a/features/unsupported/tests/mbed/dir_sd/main.cpp b/features/unsupported/tests/mbed/dir_sd/main.cpp deleted file mode 100644 index 8b47b247e2..0000000000 --- a/features/unsupported/tests/mbed/dir_sd/main.cpp +++ /dev/null @@ -1,124 +0,0 @@ -#include "mbed.h" -#include "SDFileSystem.h" - -void led_blink(PinName led) -{ - DigitalOut myled(led); - - while (1) { - myled = !myled; - wait(1.0); - } -} - -void notify_completion(bool success) -{ - if (success) - printf("{success}\n"); - else - printf("{failure}\n"); - - printf("{end}\n"); - led_blink(success ? LED1 : LED4); -} - -#define TEST_STRING "Hello World!" - -FILE *test_open(char *path, const char *mode) -{ - FILE *f; - - f = fopen(path, mode); - if (f == NULL) { - printf("Error opening file\n"); - notify_completion(false); - } - - return f; -} - -void test_write(FILE *f, const char *str) -{ - int n = fprintf(f, str); - - if (n != strlen(str)) { - printf("Error writing file\n"); - notify_completion(false); - } -} - -void test_close(FILE *f) -{ - int rc = fclose(f); - - if (rc != 0) { - printf("Error closing file\n"); - notify_completion(false); - } -} - -DigitalOut led2(LED2); - -int main() -{ -#if defined(TARGET_KL25Z) - SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); -#elif defined(TARGET_nRF51822) -//SDFileSystem sd(p20, p22, p25, p24, "sd"); - SDFileSystem sd(p12, p13, p15, p14, "sd"); -#elif defined(TARGET_NUCLEO_F030R8) || \ - defined(TARGET_NUCLEO_F070RB) || \ - defined(TARGET_NUCLEO_F072RB) || \ - defined(TARGET_NUCLEO_F091RC) || \ - defined(TARGET_NUCLEO_F103RB) || \ - defined(TARGET_NUCLEO_F302R8) || \ - defined(TARGET_NUCLEO_F303RE) || \ - defined(TARGET_NUCLEO_F334R8) || \ - defined(TARGET_NUCLEO_F401RE) || \ - defined(TARGET_NUCLEO_F410RB) || \ - defined(TARGET_NUCLEO_F411RE) || \ - defined(TARGET_NUCLEO_L053R8) || \ - defined(TARGET_NUCLEO_L073RZ) || \ - defined(TARGET_NUCLEO_L152RE) - SDFileSystem sd(D11, D12, D13, D10, "sd"); -#elif defined(TARGET_LPC11U37H_401) - SDFileSystem sd(SDMOSI, SDMISO, SDSCLK, SDSSEL, "sd"); -#elif defined(TARGET_NUMAKER_PFM_NUC472) - SDFileSystem sd(PF_0, PD_15, PD_14, PD_13, "sd"); -#elif defined(TARGET_NUMAKER_PFM_M453) - SDFileSystem sd(PD_13, PD_14, PD_15, PD_12, "sd"); -#else - SDFileSystem sd(p11, p12, p13, p14, "sd"); -#endif - led2 = 1; - wait(0.5); - FILE *f; - char *str = TEST_STRING; - char *buffer = (char *)malloc(sizeof(unsigned char) * strlen(TEST_STRING)); - int str_len = strlen(TEST_STRING); - - printf("Write files\n"); - char filename[32]; - for (int i = 0; i < 10; i++) { - sprintf(filename, "/sd/test_%d.txt", i); - printf("Creating file: %s\n", filename); - f = test_open(filename, "w"); - led2 = 0; - test_write(f, str); - test_close(f); - } - - printf("List files:\n"); - DIR *d = opendir("/sd"); - if (d == NULL) { - printf("Error opening directory\n"); - notify_completion(false); - } - - struct dirent *p; - while ((p = readdir(d)) != NULL) - printf("%s\n", p->d_name); - closedir(d); - - notify_completion(true); -} diff --git a/features/unsupported/tests/mbed/file/main.cpp b/features/unsupported/tests/mbed/file/main.cpp deleted file mode 100644 index 8c85711f96..0000000000 --- a/features/unsupported/tests/mbed/file/main.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include "test_env.h" -#include "mbed_semihost_api.h" - -Serial pc(USBTX, USBRX); - -#define FILENAME "/local/out.txt" -#define TEST_STRING "Hello World!" - -FILE *test_open(const char *mode) { - FILE *f = fopen(FILENAME, mode); - if (f == NULL) { - printf("Error opening file"NL); - notify_completion(false); - } - return f; -} - -void test_write(FILE *f, char *str, int str_len) { - int n = fprintf(f, str); - - if (n != str_len) { - printf("Error writing file"NL); - notify_completion(false); - } -} - -void test_read(FILE *f, char *str, int str_len) { - int n = fread(str, sizeof(unsigned char), str_len, f); - - if (n != str_len) { - printf("Error reading file"NL); - notify_completion(false); - } -} - -void test_close(FILE *f) { - int rc = fclose(f); - - if (rc != 0) { - printf("Error closing file"NL); - notify_completion(false); - } -} - -int main() { - MBED_HOSTTEST_TIMEOUT(20); - MBED_HOSTTEST_SELECT(default_auto); - MBED_HOSTTEST_DESCRIPTION(Semihost file system); - MBED_HOSTTEST_START("MBED_A2"); - - pc.printf("Test the Stream class\n"); - - printf("connected: %s\n", (semihost_connected()) ? ("Yes") : ("No")); - - char mac[16]; - mbed_mac_address(mac); - printf("mac address: %02x,%02x,%02x,%02x,%02x,%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - - LocalFileSystem local("local"); - - FILE *f; - char *str = TEST_STRING; - char *buffer = (char *)malloc(sizeof(unsigned char) * strlen(TEST_STRING)); - int str_len = strlen(TEST_STRING); - - // Write - f = test_open("w"); - test_write(f, str, str_len); - test_close(f); - - // Read - f = test_open("r"); - test_read(f, buffer, str_len); - test_close(f); - - // Check the two strings are equal - MBED_HOSTTEST_RESULT((strncmp(buffer, str, str_len) == 0)); -} diff --git a/features/unsupported/tests/mbed/fs/main.cpp b/features/unsupported/tests/mbed/fs/main.cpp deleted file mode 100644 index 2dc525cd08..0000000000 --- a/features/unsupported/tests/mbed/fs/main.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "mbed.h" -#include "rtos.h" -#include "SDFileSystem.h" - -#define FILE_LOC "/sd/test.txt" - -Serial pc(USBTX, USBRX); -Serial gps(p28, p27); -Serial test(p9,p10); - -SDFileSystem sd(p11, p12, p13, p14, "sd"); - -DigitalOut myled(LED1); -DigitalOut sdled(LED2); - -void sd_thread(void const *argument) { - while (true) { - sdled = !sdled; - FILE *fp = NULL; - fp = fopen(FILE_LOC, "w"); - if( fp != NULL ) fclose(fp); - Thread::wait(1000); - } -} - -int main() { - Thread sdTask(sd_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE * 2.25); - while (true) { - myled = !myled; - Thread::wait(1000); - } -} diff --git a/features/unsupported/tests/mbed/sd/main.cpp b/features/unsupported/tests/mbed/sd/main.cpp deleted file mode 100644 index 29f68a0343..0000000000 --- a/features/unsupported/tests/mbed/sd/main.cpp +++ /dev/null @@ -1,120 +0,0 @@ -#include "mbed.h" -#include "SDFileSystem.h" -#include "test_env.h" - -#if defined(TARGET_KL25Z) -SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); - -#elif defined(TARGET_KL46Z) || defined(TARGET_KL43Z) -SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd"); - -#elif defined(TARGET_K64F) || defined(TARGET_K66F) -SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); - -#elif defined(TARGET_K22F) -SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd"); - -#elif defined(TARGET_K20D50M) -SDFileSystem sd(PTD2, PTD3, PTD1, PTC2, "sd"); - -#elif defined(TARGET_nRF51822) -SDFileSystem sd(p12, p13, p15, p14, "sd"); - -#elif defined(TARGET_NUCLEO_F030R8) || \ - defined(TARGET_NUCLEO_F070RB) || \ - defined(TARGET_NUCLEO_F072RB) || \ - defined(TARGET_NUCLEO_F091RC) || \ - defined(TARGET_NUCLEO_F103RB) || \ - defined(TARGET_NUCLEO_F302R8) || \ - defined(TARGET_NUCLEO_F303RE) || \ - defined(TARGET_NUCLEO_F334R8) || \ - defined(TARGET_NUCLEO_F401RE) || \ - defined(TARGET_NUCLEO_F410RB) || \ - defined(TARGET_NUCLEO_F411RE) || \ - defined(TARGET_NUCLEO_L053R8) || \ - defined(TARGET_NUCLEO_L073RZ) || \ - defined(TARGET_NUCLEO_L152RE) -SDFileSystem sd(D11, D12, D13, D10, "sd"); - -#elif defined(TARGET_DISCO_F051R8) || \ - defined(TARGET_NUCLEO_L031K6) -SDFileSystem sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, "sd"); - -#elif defined(TARGET_LPC2368) -SDFileSystem sd(p11, p12, p13, p14, "sd"); - -#elif defined(TARGET_LPC11U68) -SDFileSystem sd(D11, D12, D13, D10, "sd"); - -#elif defined(TARGET_LPC1549) -SDFileSystem sd(D11, D12, D13, D10, "sd"); - -#elif defined(TARGET_RZ_A1H) -SDFileSystem sd(P8_5, P8_6, P8_3, P8_4, "sd"); - -#elif defined(TARGET_LPC11U37H_401) -SDFileSystem sd(SDMOSI, SDMISO, SDSCLK, SDSSEL, "sd"); - -#elif defined(TARGET_NUMAKER_PFM_NUC472) -SDFileSystem sd(PF_0, PD_15, PD_14, PD_13, "sd"); - -#elif defined(TARGET_NUMAKER_PFM_M453) -SDFileSystem sd(PD_13, PD_14, PD_15, PD_12, "sd"); - -#else -SDFileSystem sd(p11, p12, p13, p14, "sd"); -#endif - -namespace { -const char *sd_file_path = "/sd/out.txt"; -const int DATA_SIZE = 256; -} - -int main() { - MBED_HOSTTEST_TIMEOUT(15); - MBED_HOSTTEST_SELECT(default_auto); - MBED_HOSTTEST_DESCRIPTION(SD File System); - MBED_HOSTTEST_START("MBED_A12"); - - uint8_t data_written[DATA_SIZE] = { 0 }; - bool result = false; - - // Fill data_written buffer with random data - // Write these data into the file - bool write_result = false; - { - printf("SD: Writing ... "); - FILE *f = fopen(sd_file_path, "w"); - if (f) { - for (int i = 0; i < DATA_SIZE; i++) { - data_written[i] = rand() % 0XFF; - fprintf(f, "%c", data_written[i]); - } - write_result = true; - fclose(f); - } - printf("[%s]\r\n", write_result ? "OK" : "FAIL"); - } - - // Read back the data from the file and store them in data_read - bool read_result = false; - { - printf("SD: Reading data ... "); - FILE *f = fopen(sd_file_path, "r"); - if (f) { - read_result = true; - for (int i = 0; i < DATA_SIZE; i++) { - uint8_t data = fgetc(f); - if (data != data_written[i]) { - read_result = false; - break; - } - } - fclose(f); - } - printf("[%s]\r\n", read_result ? "OK" : "FAIL"); - } - - result = write_result && read_result; - MBED_HOSTTEST_RESULT(result); -} diff --git a/features/unsupported/tests/mbed/sd_perf_fatfs/main.cpp b/features/unsupported/tests/mbed/sd_perf_fatfs/main.cpp deleted file mode 100644 index 88be844e24..0000000000 --- a/features/unsupported/tests/mbed/sd_perf_fatfs/main.cpp +++ /dev/null @@ -1,168 +0,0 @@ -#include "mbed.h" -#include "SDFileSystem.h" -#include "test_env.h" -#include -#include - -#if defined(TARGET_KL25Z) -SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); - -#elif defined(TARGET_KL46Z) || defined(TARGET_KL43Z) -SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd"); - -#elif defined(TARGET_K64F) || defined(TARGET_K66F) -SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); - -#elif defined(TARGET_K22F) -SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd"); - -#elif defined(TARGET_K20D50M) -SDFileSystem sd(PTD2, PTD3, PTD1, PTC2, "sd"); - -#elif defined(TARGET_nRF51822) -SDFileSystem sd(p12, p13, p15, p14, "sd"); - -#elif defined(TARGET_NUCLEO_F030R8) || \ - defined(TARGET_NUCLEO_F070RB) || \ - defined(TARGET_NUCLEO_F072RB) || \ - defined(TARGET_NUCLEO_F091RC) || \ - defined(TARGET_NUCLEO_F103RB) || \ - defined(TARGET_NUCLEO_F302R8) || \ - defined(TARGET_NUCLEO_F303RE) || \ - defined(TARGET_NUCLEO_F334R8) || \ - defined(TARGET_NUCLEO_F401RE) || \ - defined(TARGET_NUCLEO_F410RB) || \ - defined(TARGET_NUCLEO_F411RE) || \ - defined(TARGET_NUCLEO_L053R8) || \ - defined(TARGET_NUCLEO_L073RZ) || \ - defined(TARGET_NUCLEO_L152RE) -SDFileSystem sd(D11, D12, D13, D10, "sd"); - -#elif defined(TARGET_DISCO_F051R8) -SDFileSystem sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, "sd"); - -#elif defined(TARGET_LPC2368) -SDFileSystem sd(p11, p12, p13, p14, "sd"); - -#elif defined(TARGET_LPC11U68) -SDFileSystem sd(D11, D12, D13, D10, "sd"); - -#elif defined(TARGET_LPC1549) -SDFileSystem sd(D11, D12, D13, D10, "sd"); - -#elif defined(TARGET_LPC11U37H_401) -SDFileSystem sd(SDMOSI, SDMISO, SDSCLK, SDSSEL, "sd"); - -#elif defined(TARGET_NUMAKER_PFM_NUC472) -SDFileSystem sd(PF_0, PD_15, PD_14, PD_13, "sd"); - -#elif defined(TARGET_NUMAKER_PFM_M453) -SDFileSystem sd(PD_13, PD_14, PD_15, PD_12, "sd"); - -#else -SDFileSystem sd(p11, p12, p13, p14, "sd"); -#endif - -namespace { -char buffer[1024]; -const int KIB_RW = 128; -Timer timer; -const char *bin_filename = "0:testfile.bin"; -} - -bool test_sf_file_write_fatfs(const char *filename, const int kib_rw) { - FIL file; - bool result = true; - FRESULT res = f_open(&file, filename, FA_WRITE | FA_CREATE_ALWAYS); - if (res == FR_OK) { - int byte_write = 0; - unsigned int bytes = 0; - timer.start(); - for (int i = 0; i < kib_rw; i++) { - if (f_write(&file, buffer, sizeof(buffer), &bytes) != FR_OK) { - result = false; - f_close(&file); - printf("Write error!\r\n"); - break; - } else { - byte_write++; - } - } - timer.stop(); - f_close(&file); - double test_time_sec = timer.read_us() / 1000000.0; - double speed = kib_rw / test_time_sec; - printf("%d KiB write in %.3f sec with speed of %.4f KiB/s\r\n", byte_write, test_time_sec, speed); - notify_performance_coefficient("write_kibps", speed); - } else { - printf("File '%s' not opened\r\n", filename); - result = false; - } - timer.reset(); - return result; -} - -bool test_sf_file_read_fatfs(const char *filename, const int kib_rw) { - FIL file; - bool result = true; - FRESULT res = f_open(&file, filename, FA_READ | FA_OPEN_EXISTING); - if (res == FR_OK) { - timer.start(); - int byte_read = 0; - unsigned int bytes = 0; - do { - res = f_read(&file, buffer, sizeof(buffer), &bytes); - byte_read++; - } while (res == FR_OK && bytes == sizeof(buffer)); - timer.stop(); - f_close(&file); - double test_time_sec = timer.read_us() / 1000000.0; - double speed = kib_rw / test_time_sec; - printf("%d KiB read in %.3f sec with speed of %.4f KiB/s\r\n", byte_read, test_time_sec, speed); - notify_performance_coefficient("fs_read_kibps", speed); - } else { - printf("File '%s' not opened\r\n", filename); - result = false; - } - timer.reset(); - return result; -} - -char RandomChar() { - return rand() % 100; -} - -int main() { - MBED_HOSTTEST_TIMEOUT(15); - MBED_HOSTTEST_SELECT(default_auto); - MBED_HOSTTEST_DESCRIPTION(SD FatFS RW Speed); - MBED_HOSTTEST_START("PERF_3"); - - // Test header - printf("\r\n"); - printf("SD Card FatFS Performance Test\r\n"); - printf("File name: %s\r\n", bin_filename); - printf("Buffer size: %d KiB\r\n", (KIB_RW * sizeof(buffer)) / 1024); - - // Initialize buffer - srand(testenv_randseed()); - char *buffer_end = buffer + sizeof(buffer); - std::generate (buffer, buffer_end, RandomChar); - - bool result = true; - for (;;) { - printf("Write test...\r\n"); - if (test_sf_file_write_fatfs(bin_filename, KIB_RW) == false) { - result = false; - break; - } - - printf("Read test...\r\n"); - if (test_sf_file_read_fatfs(bin_filename, KIB_RW) == false) { - result = false; - break; - } - break; - } - MBED_HOSTTEST_RESULT(result); -} diff --git a/features/unsupported/tests/mbed/sd_perf_fhandle/main.cpp b/features/unsupported/tests/mbed/sd_perf_fhandle/main.cpp deleted file mode 100644 index e45922df29..0000000000 --- a/features/unsupported/tests/mbed/sd_perf_fhandle/main.cpp +++ /dev/null @@ -1,163 +0,0 @@ -#include "mbed.h" -#include "SDFileSystem.h" -#include "test_env.h" -#include -#include - -#if defined(TARGET_KL25Z) -SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); - -#elif defined(TARGET_KL46Z) || defined(TARGET_KL43Z) -SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd"); - -#elif defined(TARGET_K64F) || defined(TARGET_K66F) -SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); - -#elif defined(TARGET_K22F) -SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd"); - -#elif defined(TARGET_K20D50M) -SDFileSystem sd(PTD2, PTD3, PTD1, PTC2, "sd"); - -#elif defined(TARGET_nRF51822) -SDFileSystem sd(p12, p13, p15, p14, "sd"); - -#elif defined(TARGET_NUCLEO_F030R8) || \ - defined(TARGET_NUCLEO_F070RB) || \ - defined(TARGET_NUCLEO_F072RB) || \ - defined(TARGET_NUCLEO_F091RC) || \ - defined(TARGET_NUCLEO_F103RB) || \ - defined(TARGET_NUCLEO_F302R8) || \ - defined(TARGET_NUCLEO_F303RE) || \ - defined(TARGET_NUCLEO_F334R8) || \ - defined(TARGET_NUCLEO_F401RE) || \ - defined(TARGET_NUCLEO_F410RB) || \ - defined(TARGET_NUCLEO_F411RE) || \ - defined(TARGET_NUCLEO_L053R8) || \ - defined(TARGET_NUCLEO_L073RZ) || \ - defined(TARGET_NUCLEO_L152RE) -SDFileSystem sd(D11, D12, D13, D10, "sd"); - -#elif defined(TARGET_DISCO_F051R8) -SDFileSystem sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, "sd"); - -#elif defined(TARGET_LPC2368) -SDFileSystem sd(p11, p12, p13, p14, "sd"); - -#elif defined(TARGET_LPC11U68) -SDFileSystem sd(D11, D12, D13, D10, "sd"); - -#elif defined(TARGET_LPC1549) -SDFileSystem sd(D11, D12, D13, D10, "sd"); - -#elif defined(TARGET_LPC11U37H_401) -SDFileSystem sd(SDMOSI, SDMISO, SDSCLK, SDSSEL, "sd"); - -#elif defined(TARGET_NUMAKER_PFM_NUC472) -SDFileSystem sd(PF_0, PD_15, PD_14, PD_13, "sd"); - -#elif defined(TARGET_NUMAKER_PFM_M453) -SDFileSystem sd(PD_13, PD_14, PD_15, PD_12, "sd"); - -#else -SDFileSystem sd(p11, p12, p13, p14, "sd"); -#endif - -namespace { -char buffer[1024]; -const int KIB_RW = 128; -Timer timer; -const char *bin_filename = "testfile.bin"; -} - -bool test_sf_file_write_fhandle(const char *filename, const int kib_rw) { - bool result = true; - FileHandle* file = sd.open(filename, O_WRONLY | O_CREAT | O_TRUNC); - if (file != NULL) { - int byte_write = 0; - timer.start(); - for (int i = 0; i < kib_rw; i++) { - if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) { - result = false; - file->close(); - printf("Write error!\r\n"); - break; - } else { - byte_write++; - } - } - timer.stop(); - file->close(); - double test_time_sec = timer.read_us() / 1000000.0; - double speed = kib_rw / test_time_sec; - printf("%d KiB write in %.3f sec with speed of %.4f KiB/s\r\n", byte_write, test_time_sec, speed); - notify_performance_coefficient("write_kibps", speed); - } else { - printf("File '%s' not opened\r\n", filename); - result = false; - } - timer.reset(); - return result; -} - -bool test_sf_file_read_fhandle(const char *filename, const int kib_rw) { - bool result = true; - FileHandle* file = sd.open(filename, O_RDONLY); - if (file) { - timer.start(); - int byte_read = 0; - while (file->read(buffer, sizeof(buffer)) == sizeof(buffer)) { - byte_read++; - } - timer.stop(); - file->close(); - double test_time_sec = timer.read_us() / 1000000.0; - double speed = kib_rw / test_time_sec; - printf("%d KiB read in %.3f sec with speed of %.4f KiB/s\r\n", byte_read, test_time_sec, speed); - notify_performance_coefficient("fs_read_kibps", speed); - } else { - printf("File '%s' not opened\r\n", filename); - result = false; - } - timer.reset(); - return result; -} - -char RandomChar() { - return rand() % 100; -} - -int main() { - MBED_HOSTTEST_TIMEOUT(15); - MBED_HOSTTEST_SELECT(default_auto); - MBED_HOSTTEST_DESCRIPTION(SD FileHandle RW Speed); - MBED_HOSTTEST_START("PERF_2"); - - // Test header - printf("\r\n"); - printf("SD Card FileHandle Performance Test\r\n"); - printf("File name: %s\r\n", bin_filename); - printf("Buffer size: %d KiB\r\n", (KIB_RW * sizeof(buffer)) / 1024); - - // Initialize buffer - srand(testenv_randseed()); - char *buffer_end = buffer + sizeof(buffer); - std::generate (buffer, buffer_end, RandomChar); - - bool result = true; - for (;;) { - printf("Write test...\r\n"); - if (test_sf_file_write_fhandle(bin_filename, KIB_RW) == false) { - result = false; - break; - } - - printf("Read test...\r\n"); - if (test_sf_file_read_fhandle(bin_filename, KIB_RW) == false) { - result = false; - break; - } - break; - } - MBED_HOSTTEST_RESULT(result); -} diff --git a/features/unsupported/tests/mbed/sd_perf_stdio/main.cpp b/features/unsupported/tests/mbed/sd_perf_stdio/main.cpp deleted file mode 100644 index ed0f862fd3..0000000000 --- a/features/unsupported/tests/mbed/sd_perf_stdio/main.cpp +++ /dev/null @@ -1,163 +0,0 @@ -#include "mbed.h" -#include "SDFileSystem.h" -#include "test_env.h" -#include -#include - -#if defined(TARGET_KL25Z) -SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); - -#elif defined(TARGET_KL46Z) || defined(TARGET_KL43Z) -SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd"); - -#elif defined(TARGET_K64F) || defined(TARGET_K66F) -SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); - -#elif defined(TARGET_K22F) -SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd"); - -#elif defined(TARGET_K20D50M) -SDFileSystem sd(PTD2, PTD3, PTD1, PTC2, "sd"); - -#elif defined(TARGET_nRF51822) -SDFileSystem sd(p12, p13, p15, p14, "sd"); - -#elif defined(TARGET_NUCLEO_F030R8) || \ - defined(TARGET_NUCLEO_F070RB) || \ - defined(TARGET_NUCLEO_F072RB) || \ - defined(TARGET_NUCLEO_F091RC) || \ - defined(TARGET_NUCLEO_F103RB) || \ - defined(TARGET_NUCLEO_F302R8) || \ - defined(TARGET_NUCLEO_F303RE) || \ - defined(TARGET_NUCLEO_F334R8) || \ - defined(TARGET_NUCLEO_F401RE) || \ - defined(TARGET_NUCLEO_F410RB) || \ - defined(TARGET_NUCLEO_F411RE) || \ - defined(TARGET_NUCLEO_L053R8) || \ - defined(TARGET_NUCLEO_L073RZ) || \ - defined(TARGET_NUCLEO_L152RE) -SDFileSystem sd(D11, D12, D13, D10, "sd"); - -#elif defined(TARGET_DISCO_F051R8) -SDFileSystem sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, "sd"); - -#elif defined(TARGET_LPC2368) -SDFileSystem sd(p11, p12, p13, p14, "sd"); - -#elif defined(TARGET_LPC11U68) -SDFileSystem sd(D11, D12, D13, D10, "sd"); - -#elif defined(TARGET_LPC1549) -SDFileSystem sd(D11, D12, D13, D10, "sd"); - -#elif defined(TARGET_LPC11U37H_401) -SDFileSystem sd(SDMOSI, SDMISO, SDSCLK, SDSSEL, "sd"); - -#elif defined(TARGET_NUMAKER_PFM_NUC472) -SDFileSystem sd(PF_0, PD_15, PD_14, PD_13, "sd"); - -#elif defined(TARGET_NUMAKER_PFM_M453) -SDFileSystem sd(PD_13, PD_14, PD_15, PD_12, "sd"); - -#else -SDFileSystem sd(p11, p12, p13, p14, "sd"); -#endif - -namespace { -char buffer[1024]; -const int KIB_RW = 128; -Timer timer; -const char *bin_filename = "/sd/testfile.bin"; -} - -bool test_sf_file_write_stdio(const char *filename, const int kib_rw) { - bool result = true; - FILE* file = fopen(filename, "w"); - if (file != NULL) { - int byte_write = 0; - timer.start(); - for (int i = 0; i < kib_rw; i++) { - if (fwrite(buffer, sizeof(char), sizeof(buffer), file) != sizeof(buffer)) { - result = false; - fclose(file); - printf("Write error!\r\n"); - break; - } else { - byte_write++; - } - } - timer.stop(); - fclose(file); - double test_time_sec = timer.read_us() / 1000000.0; - double speed = kib_rw / test_time_sec; - printf("%d KiB write in %.3f sec with speed of %.4f KiB/s\r\n", byte_write, test_time_sec, speed); - notify_performance_coefficient("write_kibps", speed); - } else { - printf("File '%s' not opened\r\n", filename); - result = false; - } - timer.reset(); - return result; -} - -bool test_sf_file_read_stdio(const char *filename, const int kib_rw) { - bool result = true; - FILE* file = fopen(filename, "r"); - if (file) { - timer.start(); - int byte_read = 0; - while (fread(buffer, sizeof(char), sizeof(buffer), file) == sizeof(buffer)) { - byte_read++; - } - timer.stop(); - fclose(file); - double test_time_sec = timer.read_us() / 1000000.0; - double speed = kib_rw / test_time_sec; - printf("%d KiB read in %.3f sec with speed of %.4f KiB/s\r\n", byte_read, test_time_sec, speed); - notify_performance_coefficient("fs_read_kibps", speed); - } else { - printf("File '%s' not opened\r\n", filename); - result = false; - } - timer.reset(); - return result; -} - -char RandomChar() { - return rand() % 100; -} - -int main() { - MBED_HOSTTEST_TIMEOUT(15); - MBED_HOSTTEST_SELECT(default_auto); - MBED_HOSTTEST_DESCRIPTION(SD stdio RW Speed); - MBED_HOSTTEST_START("PERF_1"); - - // Test header - printf("\r\n"); - printf("SD Card Stdio Performance Test\r\n"); - printf("File name: %s\r\n", bin_filename); - printf("Buffer size: %d KiB\r\n", (KIB_RW * sizeof(buffer)) / 1024); - - // Initialize buffer - srand(testenv_randseed()); - char *buffer_end = buffer + sizeof(buffer); - std::generate (buffer, buffer_end, RandomChar); - - bool result = true; - for (;;) { - printf("Write test...\r\n"); - if (test_sf_file_write_stdio(bin_filename, KIB_RW) == false) { - result = false; - break; - } - - printf("Read test...\r\n"); - if (test_sf_file_read_stdio(bin_filename, KIB_RW) == false) { - result = false; - break; - } - break; - } - MBED_HOSTTEST_RESULT(result); -} From 9ba340a964edbccec0040a16087c8cc8a1bbcf71 Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Tue, 28 Feb 2017 17:00:29 +0100 Subject: [PATCH 11/40] STM32: change spi error to debug warning In case the selected frequency is higher than the requested one, it is better to send a debug warning rather than an blocking error. In case of such warning, user may need to redefine the clock tree setting at higher level (reducing peripheral's input clocks during init phase). --- targets/TARGET_STM/stm_spi_api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/targets/TARGET_STM/stm_spi_api.c b/targets/TARGET_STM/stm_spi_api.c index 1928253bb6..f756102e39 100644 --- a/targets/TARGET_STM/stm_spi_api.c +++ b/targets/TARGET_STM/stm_spi_api.c @@ -308,7 +308,7 @@ void spi_frequency(spi_t *obj, int hz) { /* In case maximum pre-scaler still gives too high freq, raise an error */ if (spi_hz > hz) { - error("Couldn't set suitable spi freq: request:%d, lowest:%d\r\n", hz, spi_hz); + DEBUG_PRINTF("WARNING: lowest SPI freq (%d) higher than requested (%d)\r\n", spi_hz, hz); } DEBUG_PRINTF("spi_frequency, request:%d, select:%d\r\n", hz, spi_hz); From 854d2f3fe5e8c41f4e98ed34da00de8a9d70ef59 Mon Sep 17 00:00:00 2001 From: Mike Fiore Date: Wed, 1 Mar 2017 12:53:31 -0600 Subject: [PATCH 12/40] Define GPIO_IP_WITHOUT_BRR for xDot platform. Resolves #3823. --- targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/objects.h | 1 + 1 file changed, 1 insertion(+) diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/objects.h b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/objects.h index 73e9d5a294..d643f64c3c 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/objects.h @@ -65,6 +65,7 @@ struct dac_s { PinName pin; }; +#define GPIO_IP_WITHOUT_BRR #include "common_objects.h" #ifdef __cplusplus From feb7569da7002836f82e3e30e84ff06845c8adf6 Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Thu, 2 Mar 2017 14:18:23 -0600 Subject: [PATCH 13/40] Add post-build hook white-list to exporters We have scripts (written in python) that are run after a binary image is created in the tools. These scripts are not really exportable, as they are part of the tools and may include and use any bits of python code they please. So we don't export them. This patch disables export combinations that would not work because the post-binary hook is not exported. A white-list is used for forward compatibility. --- tools/export/embitz/__init__.py | 12 +++++++++--- tools/export/exporters.py | 17 +++++++++++++++++ tools/export/gnuarmeclipse/__init__.py | 11 +++++++---- tools/export/makefile/__init__.py | 16 +++++++++------- tools/export/makefile/make-gcc-arm_nxp.tmpl | 11 ----------- 5 files changed, 42 insertions(+), 25 deletions(-) delete mode 100644 tools/export/makefile/make-gcc-arm_nxp.tmpl diff --git a/tools/export/embitz/__init__.py b/tools/export/embitz/__init__.py index 338b996f8b..e75bc835c3 100644 --- a/tools/export/embitz/__init__.py +++ b/tools/export/embitz/__init__.py @@ -16,14 +16,20 @@ limitations under the License. """ from os.path import splitext, basename from tools.targets import TARGET_MAP -from tools.export.exporters import Exporter +from tools.export.exporters import Exporter, filter_supported + + +POST_BINARY_WHITELIST = set([ + "TEENSY3_1Code.binary_hook" +]) + class EmBitz(Exporter): NAME = 'EmBitz' TOOLCHAIN = 'GCC_ARM' - TARGETS = [target for target, obj in TARGET_MAP.iteritems() - if "GCC_ARM" in obj.supported_toolchains] + + TARGETS = filter_supported("GCC_ARM", POST_BINARY_WHITELIST) MBED_CONFIG_HEADER_SUPPORTED = True diff --git a/tools/export/exporters.py b/tools/export/exporters.py index 58797acb02..e0fa5ae19b 100644 --- a/tools/export/exporters.py +++ b/tools/export/exporters.py @@ -176,3 +176,20 @@ class Exporter(object): def generate(self): """Generate an IDE/tool specific project file""" raise NotImplemented("Implement a generate function in Exporter child class") + + +def filter_supported(compiler, whitelist): + """Generate a list of supported targets for a given compiler and post-binary hook + white-list.""" + def supported_p(obj): + """Internal inner function used for filtering""" + if compiler not in obj.supported_toolchains: + return False + if not hasattr(obj, "post_binary_hook"): + return True + if obj.post_binary_hook['function'] in whitelist: + return True + else: + return False + return list(target for target, obj in TARGET_MAP.iteritems() + if supported_p(obj)) diff --git a/tools/export/gnuarmeclipse/__init__.py b/tools/export/gnuarmeclipse/__init__.py index ced50dec4c..9f7eb4e72d 100644 --- a/tools/export/gnuarmeclipse/__init__.py +++ b/tools/export/gnuarmeclipse/__init__.py @@ -33,7 +33,7 @@ from os.path import splitext, basename, relpath, dirname, exists, join, dirname from random import randint from json import load -from tools.export.exporters import Exporter +from tools.export.exporters import Exporter, filter_supported from tools.options import list_profiles from tools.targets import TARGET_MAP from tools.utils import NotSupportedException @@ -58,13 +58,16 @@ u = UID() # ============================================================================= +POST_BINARY_WHITELIST = set([ + "TEENSY3_1Code.binary_hook", + "MCU_NRF51Code.binary_hook", +]) + class GNUARMEclipse(Exporter): NAME = 'GNU ARM Eclipse' TOOLCHAIN = 'GCC_ARM' - # Indirectly support all GCC_ARM targets. - TARGETS = [target for target, obj in TARGET_MAP.iteritems() - if 'GCC_ARM' in obj.supported_toolchains] + TARGETS = filter_supported("GCC_ARM", POST_BINARY_WHITELIST) # override @property diff --git a/tools/export/makefile/__init__.py b/tools/export/makefile/__init__.py index a76369cda7..0df1a58a31 100644 --- a/tools/export/makefile/__init__.py +++ b/tools/export/makefile/__init__.py @@ -21,7 +21,7 @@ import sys from subprocess import check_output, CalledProcessError, Popen, PIPE import shutil from jinja2.exceptions import TemplateNotFound -from tools.export.exporters import Exporter +from tools.export.exporters import Exporter, filter_supported from tools.utils import NotSupportedException from tools.targets import TARGET_MAP @@ -35,6 +35,11 @@ class Makefile(Exporter): MBED_CONFIG_HEADER_SUPPORTED = True + POST_BINARY_WHITELIST = set([ + "MCU_NRF51Code.binary_hook", + "TEENSY3_1Code.binary_hook" + ]) + def generate(self): """Generate the makefile @@ -168,8 +173,7 @@ class Makefile(Exporter): class GccArm(Makefile): """GCC ARM specific makefile target""" - TARGETS = [target for target, obj in TARGET_MAP.iteritems() - if "GCC_ARM" in obj.supported_toolchains] + TARGETS = filter_supported("GCC_ARM", Makefile.POST_BINARY_WHITELIST) NAME = 'Make-GCC-ARM' TEMPLATE = 'make-gcc-arm' TOOLCHAIN = "GCC_ARM" @@ -187,8 +191,7 @@ class GccArm(Makefile): class Armc5(Makefile): """ARM Compiler 5 specific makefile target""" - TARGETS = [target for target, obj in TARGET_MAP.iteritems() - if "ARM" in obj.supported_toolchains] + TARGETS = filter_supported("ARM", Makefile.POST_BINARY_WHITELIST) NAME = 'Make-ARMc5' TEMPLATE = 'make-armc5' TOOLCHAIN = "ARM" @@ -206,8 +209,7 @@ class Armc5(Makefile): class IAR(Makefile): """IAR specific makefile target""" - TARGETS = [target for target, obj in TARGET_MAP.iteritems() - if "IAR" in obj.supported_toolchains] + TARGETS = filter_supported("IAR", Makefile.POST_BINARY_WHITELIST) NAME = 'Make-IAR' TEMPLATE = 'make-iar' TOOLCHAIN = "IAR" diff --git a/tools/export/makefile/make-gcc-arm_nxp.tmpl b/tools/export/makefile/make-gcc-arm_nxp.tmpl deleted file mode 100644 index d9ebb15a27..0000000000 --- a/tools/export/makefile/make-gcc-arm_nxp.tmpl +++ /dev/null @@ -1,11 +0,0 @@ -{% extends "makefile/make-gcc-arm.tmpl" %} - -{% block target_project_elf %} -{{ super() }} - @echo "" - @echo "*****" - @echo "***** You must modify vector checksum value in *.bin and *.hex files." - @echo "*****" - @echo "" -{% endblock %} - From deb7be13762e1f24e3eb1b6d22e2dbc3e8252a10 Mon Sep 17 00:00:00 2001 From: adustm Date: Fri, 3 Mar 2017 17:05:28 +0100 Subject: [PATCH 14/40] DISCO_F469NI: allow the use of CAN2 instance when CAN1 is not activated --- targets/TARGET_STM/can_api.c | 1 + 1 file changed, 1 insertion(+) diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index 4c588e7250..e5f0a040d6 100644 --- a/targets/TARGET_STM/can_api.c +++ b/targets/TARGET_STM/can_api.c @@ -43,6 +43,7 @@ void can_init(can_t *obj, PinName rd, PinName td) } #if defined(CAN2_BASE) && (CAN_NUM == 2) else if (obj->can == CAN_2) { + __HAL_RCC_CAN1_CLK_ENABLE(); // needed to set filters __HAL_RCC_CAN2_CLK_ENABLE(); obj->index = 1; } From acf7012a20d2aead8295f2fa61a545e381210834 Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Mon, 6 Mar 2017 16:53:48 -0600 Subject: [PATCH 15/40] Ignore FuzzyWuzzy warnings Nobody actually cares. --- tools/arm_pack_manager/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/arm_pack_manager/__init__.py b/tools/arm_pack_manager/__init__.py index 6cd66399f0..963377dc04 100644 --- a/tools/arm_pack_manager/__init__.py +++ b/tools/arm_pack_manager/__init__.py @@ -7,12 +7,16 @@ from threading import Thread from Queue import Queue from re import compile, sub from sys import stderr, stdout -from fuzzywuzzy import process from itertools import takewhile import argparse from json import dump, load from zipfile import ZipFile from tempfile import gettempdir +import warnings + +warnings.filterwarnings("ignore") + +from fuzzywuzzy import process RootPackURL = "http://www.keil.com/pack/index.idx" From 6dde4e63160695cacdbc24eb510d9548d9270fc0 Mon Sep 17 00:00:00 2001 From: Russ Butler Date: Mon, 6 Mar 2017 17:15:51 -0600 Subject: [PATCH 16/40] Prevent underflow in heap size calculation If the free memory on a device is small enough then the calculation to determine heap size could underflow to a large value. If this happens then malloc will never return failure and instead will cause a crash on allocation. This patch prevents the underflow so malloc works as expected even with low amounts of free memory. --- rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h b/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h index 679f850a7f..b96ef812c3 100644 --- a/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h +++ b/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h @@ -478,7 +478,7 @@ void set_stack_heap(void) { mbed_stack_isr_start = ISR_STACK_START; #else /* Interrupt stack - reserve space at the end of the free block */ - mbed_stack_isr_size = ISR_STACK_SIZE; + mbed_stack_isr_size = ISR_STACK_SIZE < free_size ? ISR_STACK_SIZE : free_size; mbed_stack_isr_start = free_start + free_size - mbed_stack_isr_size; free_size -= mbed_stack_isr_size; #endif From 6aab786ca5bf684a3081d97d74cca3359f1d4772 Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Thu, 9 Mar 2017 12:44:49 +0000 Subject: [PATCH 17/40] Fix reference to sleep in hal_patch override The `sleep` function as been changed into `hal_sleep` by #3607. Unfortunately the call to `sleep` in the hal_patch for the NRF51822 has not been updated to `hal_sleep`. The result was a link time error for targets based on NRF51822_LEGACY compiling with the mbed OS 5 tree. --- .../targets/TARGET_NORDIC/TARGET_MCU_NRF51822/hal_patch/sleep.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_MCU_NRF51822/hal_patch/sleep.c b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_MCU_NRF51822/hal_patch/sleep.c index 7c178188a5..aec280a0fb 100644 --- a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_MCU_NRF51822/hal_patch/sleep.c +++ b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_MCU_NRF51822/hal_patch/sleep.c @@ -66,6 +66,6 @@ void hal_sleep(void) void hal_deepsleep(void) { - sleep(); + hal_sleep(); // NRF_POWER->SYSTEMOFF=1; } From 0789c24710938e350e1f28c1cca51f53234f43a7 Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Thu, 16 Feb 2017 11:18:23 +0100 Subject: [PATCH 18/40] STM32 L1: Define PWM Channels in PeripheralsPins.c As done for other families, let's define the PWM channel in the PWM pins table definition rather than driver. --- .../TARGET_MOTE_L152RC/PeripheralPins.c | 68 +++++++++---------- .../TARGET_NUCLEO_L152RE/PeripheralPins.c | 68 +++++++++---------- .../TARGET_NZ32_SC151/PeripheralPins.c | 68 +++++++++---------- .../TARGET_XDOT_L151CC/PeripheralPins.c | 14 ++-- .../TARGET_STM32L1/common_objects.h | 2 + .../TARGET_STM/TARGET_STM32L1/pwmout_api.c | 53 ++++++--------- 6 files changed, 133 insertions(+), 140 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/PeripheralPins.c b/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/PeripheralPins.c index 8c8be93926..8a26780f57 100755 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/PeripheralPins.c +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/PeripheralPins.c @@ -92,40 +92,40 @@ const PinMap PinMap_I2C_SCL[] = { // TIM5 cannot be used because already used by the us_ticker. const PinMap PinMap_PWM[] = { -// {PA_0, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH1 - {PA_1, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2 -// {PA_1, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH1 - {PA_2, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3 -// {PA_2, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH3 -// {PA_2, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1 - {PA_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4 -// {PA_3, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH4 -// {PA_3, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2 - {PA_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 -// {PA_6, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PA_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 -// {PA_7, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PB_0, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3 - {PB_1, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4 - {PB_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2 - {PB_4, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 - {PB_5, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 - {PB_6, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH1 - {PB_7, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH2 - {PB_8, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH3 -// {PB_8, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PB_9, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH4 -// {PB_9, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PB_10, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3 - {PB_11, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4 - {PB_12, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PB_13, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1 - {PB_14, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2 - {PB_15, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PC_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 - {PC_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 - {PC_8, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3 - {PC_9, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4 +// {PA_0, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 1, 0)}, // TIM5_CH1 + {PA_1, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 2, 0)}, // TIM2_CH2 +// {PA_1, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 1, 0)}, // TIM5_CH1 + {PA_2, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 3, 0)}, // TIM2_CH3 +// {PA_2, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 3, 0)}, // TIM5_CH3 +// {PA_2, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 + {PA_3, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 4, 0)}, // TIM2_CH4 +// {PA_3, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 4, 0)}, // TIM5_CH4 +// {PA_3, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 + {PA_6, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 +// {PA_6, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PA_7, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 +// {PA_7, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PB_0, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 3, 0)}, // TIM3_CH3 + {PB_1, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 4, 0)}, // TIM3_CH4 + {PB_3, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 2, 0)}, // TIM2_CH2 + {PB_4, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 + {PB_5, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 + {PB_6, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 1, 0)}, // TIM4_CH1 + {PB_7, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 2, 0)}, // TIM4_CH2 + {PB_8, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 3, 0)}, // TIM4_CH3 +// {PB_8, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PB_9, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 4, 0)}, // TIM4_CH4 +// {PB_9, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PB_10, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 3, 0)}, // TIM2_CH3 + {PB_11, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 4, 0)}, // TIM2_CH4 + {PB_12, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PB_13, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 + {PB_14, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 + {PB_15, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PC_6, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 + {PC_7, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 + {PC_8, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 3, 0)}, // TIM3_CH3 + {PC_9, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 4, 0)}, // TIM3_CH4 {NC, NC, 0} }; diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/PeripheralPins.c b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/PeripheralPins.c index 89293e669a..64910782d6 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/PeripheralPins.c +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/PeripheralPins.c @@ -92,40 +92,40 @@ const PinMap PinMap_I2C_SCL[] = { // TIM5 cannot be used because already used by the us_ticker. const PinMap PinMap_PWM[] = { -// {PA_0, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH1 - {PA_1, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2 -// {PA_1, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH1 - {PA_2, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3 -// {PA_2, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH3 -// {PA_2, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1 - {PA_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4 -// {PA_3, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH4 -// {PA_3, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2 - {PA_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 -// {PA_6, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PA_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 -// {PA_7, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PB_0, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3 - {PB_1, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4 - {PB_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2 - {PB_4, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 - {PB_5, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 - {PB_6, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH1 - {PB_7, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH2 - {PB_8, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH3 -// {PB_8, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PB_9, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH4 -// {PB_9, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PB_10, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3 - {PB_11, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4 - {PB_12, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PB_13, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1 - {PB_14, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2 - {PB_15, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PC_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 - {PC_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 - {PC_8, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3 - {PC_9, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4 +// {PA_0, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 1, 0)}, // TIM5_CH1 + {PA_1, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 2, 0)}, // TIM2_CH2 +// {PA_1, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 1, 0)}, // TIM5_CH1 + {PA_2, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 3, 0)}, // TIM2_CH3 +// {PA_2, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 3, 0)}, // TIM5_CH3 +// {PA_2, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 + {PA_3, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 4, 0)}, // TIM2_CH4 +// {PA_3, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 4, 0)}, // TIM5_CH4 +// {PA_3, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 + {PA_6, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 +// {PA_6, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PA_7, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 +// {PA_7, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PB_0, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 3, 0)}, // TIM3_CH3 + {PB_1, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 4, 0)}, // TIM3_CH4 + {PB_3, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 2, 0)}, // TIM2_CH2 + {PB_4, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 + {PB_5, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 + {PB_6, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 1, 0)}, // TIM4_CH1 + {PB_7, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 2, 0)}, // TIM4_CH2 + {PB_8, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 3, 0)}, // TIM4_CH3 +// {PB_8, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PB_9, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 4, 0)}, // TIM4_CH4 +// {PB_9, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PB_10, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 3, 0)}, // TIM2_CH3 + {PB_11, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 4, 0)}, // TIM2_CH4 + {PB_12, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PB_13, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 + {PB_14, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 + {PB_15, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PC_6, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 + {PC_7, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 + {PC_8, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 3, 0)}, // TIM3_CH3 + {PC_9, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 4, 0)}, // TIM3_CH4 {NC, NC, 0} }; diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/PeripheralPins.c b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/PeripheralPins.c index 5db8665982..e63f41f54e 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/PeripheralPins.c +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/PeripheralPins.c @@ -92,40 +92,40 @@ const PinMap PinMap_I2C_SCL[] = { // TIM5 cannot be used because already used by the us_ticker. const PinMap PinMap_PWM[] = { -// {PA_0, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH1 - {PA_1, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2 -// {PA_1, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH1 - {PA_2, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3 -// {PA_2, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH3 -// {PA_2, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1 - {PA_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4 -// {PA_3, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH4 -// {PA_3, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2 - {PA_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 -// {PA_6, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PA_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 -// {PA_7, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PB_0, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3 - {PB_1, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4 - {PB_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2 - {PB_4, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 - {PB_5, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 - {PB_6, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH1 - {PB_7, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH2 - {PB_8, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH3 -// {PB_8, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PB_9, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH4 -// {PB_9, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PB_10, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3 - {PB_11, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4 - {PB_12, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PB_13, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1 - {PB_14, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2 - {PB_15, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PC_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 - {PC_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 - {PC_8, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3 - {PC_9, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4 +// {PA_0, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 1, 0)}, // TIM5_CH1 + {PA_1, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 2, 0)}, // TIM2_CH2 +// {PA_1, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 1, 0)}, // TIM5_CH1 + {PA_2, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 3, 0)}, // TIM2_CH3 +// {PA_2, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 3, 0)}, // TIM5_CH3 +// {PA_2, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 + {PA_3, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 4, 0)}, // TIM2_CH4 +// {PA_3, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 4, 0)}, // TIM5_CH4 +// {PA_3, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 + {PA_6, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 +// {PA_6, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PA_7, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 +// {PA_7, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PB_0, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 3, 0)}, // TIM3_CH3 + {PB_1, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 4, 0)}, // TIM3_CH4 + {PB_3, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 2, 0)}, // TIM2_CH2 + {PB_4, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 + {PB_5, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 + {PB_6, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 1, 0)}, // TIM4_CH1 + {PB_7, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 2, 0)}, // TIM4_CH2 + {PB_8, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 3, 0)}, // TIM4_CH3 +// {PB_8, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PB_9, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 4, 0)}, // TIM4_CH4 +// {PB_9, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PB_10, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 3, 0)}, // TIM2_CH3 + {PB_11, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 4, 0)}, // TIM2_CH4 + {PB_12, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PB_13, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 + {PB_14, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 + {PB_15, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PC_6, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 + {PC_7, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 + {PC_8, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 3, 0)}, // TIM3_CH3 + {PC_9, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 4, 0)}, // TIM3_CH4 {NC, NC, 0} }; diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c index 784776ee7f..2219954bfe 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c @@ -79,13 +79,13 @@ const PinMap PinMap_I2C_SCL[] = { // TIM2 cannot be used because it is used to clock the SE // TIM5 cannot be used because already used by the us_ticker. const PinMap PinMap_PWM[] = { - {PB_0, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3 - {PB_8, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH3 - {PB_9, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH4 - {PB_12, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PB_13, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1 - {PB_14, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2 - {PB_15, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 + {PB_0, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 3, 0)}, // TIM3_CH3 + {PB_8, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 3, 0)}, // TIM4_CH3 + {PB_9, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 4, 0)}, // TIM4_CH4 + {PB_12, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PB_13, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 + {PB_14, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 + {PB_15, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11), 1, 0}, // TIM11_CH1 {NC, NC, 0} }; diff --git a/targets/TARGET_STM/TARGET_STM32L1/common_objects.h b/targets/TARGET_STM/TARGET_STM32L1/common_objects.h index ae4f8cf9f8..f40ffcdb32 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32L1/common_objects.h @@ -45,6 +45,8 @@ struct pwmout_s { uint32_t prescaler; uint32_t period; uint32_t pulse; + uint8_t channel; + uint8_t inverted; }; struct serial_s { diff --git a/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c index 6e4e85e835..02ce8a22b5 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c +++ b/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c @@ -47,6 +47,12 @@ void pwmout_init(pwmout_t* obj, PinName pin) error("PWM error: pinout mapping failed."); } + // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object + uint32_t function = pinmap_function(pin, PinMap_PWM); + MBED_ASSERT(function != (uint32_t)NC); + obj->channel = STM_PIN_CHANNEL(function); + obj->inverted = STM_PIN_INVERTED(function); + // Enable TIM clock if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); @@ -92,50 +98,35 @@ void pwmout_write(pwmout_t* obj, float value) sConfig.OCMode = TIM_OCMODE_PWM1; sConfig.Pulse = obj->pulse / obj->prescaler; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_ENABLE; + sConfig.OCFastMode = TIM_OCFAST_DISABLE; +#ifdef TIM_OCIDLESTATE_RESET + sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; +#endif +#ifdef TIM_OCNIDLESTATE_RESET + sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; +#endif - switch (obj->pin) { - // Channels 1 - case PA_6: - case PB_4: - case PB_6: - case PB_12: - case PB_13: - case PB_15: - case PC_6: + switch (obj->channel) { + case 1: channel = TIM_CHANNEL_1; break; - // Channels 2 - case PA_1: - case PA_7: - case PB_3: - case PB_5: - case PB_7: - case PB_14: - case PC_7: + case 2: channel = TIM_CHANNEL_2; break; - // Channels 3 - case PA_2: - case PB_0: - case PB_8: - case PB_10: - case PC_8: + case 3: channel = TIM_CHANNEL_3; break; - // Channels 4 - case PA_3: - case PB_1: - case PB_9: - case PB_11: - case PC_9: + case 4: channel = TIM_CHANNEL_4; break; default: return; } - HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel); + if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { + error("Cannot initialize PWM\n"); + } + HAL_TIM_PWM_Start(&TimHandle, channel); } From 1f825c1877134e21faf21ffd78e25c696e1a73d7 Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Thu, 16 Feb 2017 15:28:25 +0100 Subject: [PATCH 19/40] STM32: make PWM driver into a common file The pwmout driver is very similar for each STM32 family. The only family specific part is defined in pwmout_device.h file. It mainly contains few specific information: - The mapping of PWM/TIMERS to APB1 or APB2 so that we can get the clock - The clock calculation uses the right APB clock, which was sometimes not the case before and could have lead to errors in case dividers were enabled on APB clock settings. This case is now covered. - Inactivation of inverted support on feaw families --- .../TARGET_STM/TARGET_STM32F0/pwmout_api.c | 214 ------------- .../TARGET_STM/TARGET_STM32F0/pwmout_device.h | 83 ++++++ .../TARGET_STM/TARGET_STM32F1/pwmout_api.c | 205 ------------- .../TARGET_STM/TARGET_STM32F1/pwmout_device.h | 72 +++++ targets/TARGET_STM/TARGET_STM32F2/objects.h | 1 + .../TARGET_STM/TARGET_STM32F2/pwmout_api.c | 282 ------------------ .../TARGET_STM/TARGET_STM32F2/pwmout_device.h | 96 ++++++ .../TARGET_STM/TARGET_STM32F3/pwmout_api.c | 259 ---------------- .../TARGET_STM/TARGET_STM32F3/pwmout_device.h | 87 ++++++ .../TARGET_STM/TARGET_STM32F4/pwmout_device.h | 96 ++++++ .../TARGET_STM/TARGET_STM32F7/pwmout_api.c | 253 ---------------- .../TARGET_STM/TARGET_STM32F7/pwmout_device.h | 96 ++++++ .../TARGET_STM/TARGET_STM32L0/pwmout_api.c | 204 ------------- .../TARGET_STM/TARGET_STM32L0/pwmout_device.h | 74 +++++ .../TARGET_STM/TARGET_STM32L1/pwmout_api.c | 212 ------------- .../TARGET_STM/TARGET_STM32L1/pwmout_device.h | 84 ++++++ .../TARGET_STM/TARGET_STM32L4/pwmout_api.c | 222 -------------- .../TARGET_STM/TARGET_STM32L4/pwmout_device.h | 87 ++++++ .../{TARGET_STM32F4 => }/pwmout_api.c | 137 ++++----- 19 files changed, 841 insertions(+), 1923 deletions(-) delete mode 100644 targets/TARGET_STM/TARGET_STM32F0/pwmout_api.c create mode 100644 targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h delete mode 100644 targets/TARGET_STM/TARGET_STM32F1/pwmout_api.c create mode 100644 targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h delete mode 100644 targets/TARGET_STM/TARGET_STM32F2/pwmout_api.c create mode 100644 targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h delete mode 100644 targets/TARGET_STM/TARGET_STM32F3/pwmout_api.c create mode 100644 targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h create mode 100644 targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h delete mode 100644 targets/TARGET_STM/TARGET_STM32F7/pwmout_api.c create mode 100644 targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h delete mode 100644 targets/TARGET_STM/TARGET_STM32L0/pwmout_api.c create mode 100644 targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h delete mode 100644 targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c create mode 100644 targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h delete mode 100644 targets/TARGET_STM/TARGET_STM32L4/pwmout_api.c create mode 100644 targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h rename targets/TARGET_STM/{TARGET_STM32F4 => }/pwmout_api.c (75%) diff --git a/targets/TARGET_STM/TARGET_STM32F0/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32F0/pwmout_api.c deleted file mode 100644 index 63368cc4c1..0000000000 --- a/targets/TARGET_STM/TARGET_STM32F0/pwmout_api.c +++ /dev/null @@ -1,214 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2014, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "pwmout_api.h" - -#if DEVICE_PWMOUT - -#include "cmsis.h" -#include "pinmap.h" -#include "mbed_error.h" -#include "PeripheralPins.h" - -static TIM_HandleTypeDef TimHandle; - -void pwmout_init(pwmout_t* obj, PinName pin) { - // Get the peripheral name from the pin and assign it to the object - obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - MBED_ASSERT(obj->pwm != (PWMName)NC); - - // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object - uint32_t function = pinmap_function(pin, PinMap_PWM); - MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); - obj->inverted = STM_PIN_INVERTED(function); - - // Enable TIM clock -#if defined(TIM1_BASE) - if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE(); -#endif -#if defined(TIM2_BASE) - if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); -#endif -#if defined(TIM3_BASE) - if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); -#endif -#if defined(TIM14_BASE) - if (obj->pwm == PWM_14) __TIM14_CLK_ENABLE(); -#endif -#if defined(TIM15_BASE) - if (obj->pwm == PWM_15) __TIM15_CLK_ENABLE(); -#endif -#if defined(TIM16_BASE) - if (obj->pwm == PWM_16) __TIM16_CLK_ENABLE(); -#endif -#if defined(TIM17_BASE) - if (obj->pwm == PWM_17) __TIM17_CLK_ENABLE(); -#endif - - // Configure GPIO - pinmap_pinout(pin, PinMap_PWM); - - obj->pin = pin; - obj->period = 0; - obj->pulse = 0; - obj->prescaler = 1; - - pwmout_period_us(obj, 20000); // 20 ms per default -} - -void pwmout_free(pwmout_t* obj) { - // Configure GPIO - pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); -} - -void pwmout_write(pwmout_t* obj, float value) { - TIM_OC_InitTypeDef sConfig; - int channel = 0; - - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - if (value < (float)0.0) { - value = 0.0; - } else if (value > (float)1.0) { - value = 1.0; - } - - obj->pulse = (uint32_t)((float)obj->period * value); - - // Configure channels - sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse / obj->prescaler; - sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_DISABLE; - sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; - sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; - - switch (obj->channel) { - case 1: - channel = TIM_CHANNEL_1; - break; - case 2: - channel = TIM_CHANNEL_2; - break; - case 3: - channel = TIM_CHANNEL_3; - break; - case 4: - channel = TIM_CHANNEL_4; - break; - default: - return; - } - - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { - error("Cannot initialize PWM\n"); - } - - if (obj->inverted) { - HAL_TIMEx_PWMN_Start(&TimHandle, channel); - } else { - HAL_TIM_PWM_Start(&TimHandle, channel); - } -} - -float pwmout_read(pwmout_t* obj) { - float value = 0; - if (obj->period > 0) { - value = (float)(obj->pulse) / (float)(obj->period); - } - return ((value > (float)1.0) ? (float)(1.0) : (value)); -} - -void pwmout_period(pwmout_t* obj, float seconds) { - pwmout_period_us(obj, seconds * 1000000.0f); -} - -void pwmout_period_ms(pwmout_t* obj, int ms) { - pwmout_period_us(obj, ms * 1000); -} - -void pwmout_period_us(pwmout_t* obj, int us) { - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - float dc = pwmout_read(obj); - - __HAL_TIM_DISABLE(&TimHandle); - - /* To make it simple, we use to possible prescaler values which lead to: - * pwm unit = 1us, period/pulse can be from 1us to 65535us - * or - * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec - * Be careful that all the channels of a PWM shares the same prescaler - */ - if (us > 0xFFFF) { - obj->prescaler = 500; - } else { - obj->prescaler = 1; - } - TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1; - - if (TimHandle.Init.Prescaler > 0xFFFF) - error("PWM: out of range prescaler"); - - TimHandle.Init.Period = (us - 1) / obj->prescaler; - if (TimHandle.Init.Period > 0xFFFF) - error("PWM: out of range period"); - - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - - if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { - error("Cannot initialize PWM"); - } - - // Save for future use - obj->period = us; - - // Set duty cycle again - pwmout_write(obj, dc); - - __HAL_TIM_ENABLE(&TimHandle); -} - -void pwmout_pulsewidth(pwmout_t* obj, float seconds) { - pwmout_pulsewidth_us(obj, seconds * 1000000.0f); -} - -void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { - pwmout_pulsewidth_us(obj, ms * 1000); -} - -void pwmout_pulsewidth_us(pwmout_t* obj, int us) { - float value = (float)us / (float)obj->period; - pwmout_write(obj, value); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h new file mode 100644 index 0000000000..2862c30e6b --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h @@ -0,0 +1,83 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_UNKNOWN = 1 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +/* there is only 1 APB clock in F0 family */ +#define PWMOUT_APB2_NOT_SUPPORTED + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM14_BASE) + {PWM_14, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB1}, +#endif +#if defined(TIM15_BASE) + {PWM_15, PWMOUT_ON_APB1}, +#endif +#if defined(TIM16_BASE) + {PWM_16, PWMOUT_ON_APB1}, +#endif +#if defined(TIM17_BASE) + {PWM_17, PWMOUT_ON_APB1}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32F1/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32F1/pwmout_api.c deleted file mode 100644 index 4eb3a28c4f..0000000000 --- a/targets/TARGET_STM/TARGET_STM32F1/pwmout_api.c +++ /dev/null @@ -1,205 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2014, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "pwmout_api.h" - -#if DEVICE_PWMOUT - -#include "cmsis.h" -#include "pinmap.h" -#include "mbed_error.h" -#include "PeripheralPins.h" - -static TIM_HandleTypeDef TimHandle; - -void pwmout_init(pwmout_t* obj, PinName pin) -{ - // Get the peripheral name from the pin and assign it to the object - obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - MBED_ASSERT(obj->pwm != (PWMName)NC); - - // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object - uint32_t function = pinmap_function(pin, PinMap_PWM); - MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); - obj->inverted = STM_PIN_INVERTED(function); - - // Enable TIM clock - if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE(); - if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); - if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); - - // Configure GPIO - pinmap_pinout(pin, PinMap_PWM); - - obj->pin = pin; - obj->period = 0; - obj->pulse = 0; - obj->prescaler = 1; - - pwmout_period_us(obj, 20000); // 20 ms per default -} - -void pwmout_free(pwmout_t* obj) -{ - // Configure GPIO - pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); -} - -void pwmout_write(pwmout_t* obj, float value) -{ - TIM_OC_InitTypeDef sConfig; - int channel = 0; - int complementary_channel = 0; - - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - if (value < (float)0.0) { - value = 0.0; - } else if (value > (float)1.0) { - value = 1.0; - } - - obj->pulse = (uint32_t)((float)obj->period * value); - - // Configure channels - sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse / obj->prescaler; - sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_DISABLE; - sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; - sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; - - switch (obj->channel) { - case 1: - channel = TIM_CHANNEL_1; - break; - case 2: - channel = TIM_CHANNEL_2; - break; - case 3: - channel = TIM_CHANNEL_3; - break; - case 4: - channel = TIM_CHANNEL_4; - break; - default: - return; - } - - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { - error("Cannot initialize PWM\n"); - } - - if (obj->inverted) { - HAL_TIMEx_PWMN_Start(&TimHandle, channel); - } else { - HAL_TIM_PWM_Start(&TimHandle, channel); - } -} - -float pwmout_read(pwmout_t* obj) -{ - float value = 0; - if (obj->period > 0) { - value = (float)(obj->pulse) / (float)(obj->period); - } - return ((value > (float)1.0) ? (float)(1.0) : (value)); -} - -void pwmout_period(pwmout_t* obj, float seconds) -{ - pwmout_period_us(obj, seconds * 1000000.0f); -} - -void pwmout_period_ms(pwmout_t* obj, int ms) -{ - pwmout_period_us(obj, ms * 1000); -} - -void pwmout_period_us(pwmout_t* obj, int us) -{ - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - float dc = pwmout_read(obj); - - __HAL_TIM_DISABLE(&TimHandle); - - /* To make it simple, we use to possible prescaler values which lead to: - * pwm unit = 1us, period/pulse can be from 1us to 65535us - * or - * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec - * Be careful that all the channels of a PWM shares the same prescaler - */ - if (us > 0xFFFF) { - obj->prescaler = 500; - } else { - obj->prescaler = 1; - } - - TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1; - - if (TimHandle.Init.Prescaler > 0xFFFF) - error("PWM: out of range prescaler"); - - TimHandle.Init.Period = (us - 1) / obj->prescaler; - if (TimHandle.Init.Period > 0xFFFF) - error("PWM: out of range period"); - - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - HAL_TIM_PWM_Init(&TimHandle); - - // Save for future use - obj->period = us; - - // Set duty cycle again - pwmout_write(obj, dc); - - __HAL_TIM_ENABLE(&TimHandle); -} - -void pwmout_pulsewidth(pwmout_t* obj, float seconds) -{ - pwmout_pulsewidth_us(obj, seconds * 1000000.0f); -} - -void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) -{ - pwmout_pulsewidth_us(obj, ms * 1000); -} - -void pwmout_pulsewidth_us(pwmout_t* obj, int us) -{ - float value = (float)us / (float)obj->period; - pwmout_write(obj, value); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h new file mode 100644 index 0000000000..9f105e7ec6 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h @@ -0,0 +1,72 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_ON_APB2 = 1, + PWMOUT_UNKNOWN = 2 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32F2/objects.h b/targets/TARGET_STM/TARGET_STM32F2/objects.h index 1313627b84..3bd0a18c6c 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F2/objects.h @@ -128,6 +128,7 @@ struct i2c_s { struct pwmout_s { PWMName pwm; PinName pin; + uint32_t prescaler; uint32_t period; uint32_t pulse; uint8_t channel; diff --git a/targets/TARGET_STM/TARGET_STM32F2/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32F2/pwmout_api.c deleted file mode 100644 index e8c58bf4b2..0000000000 --- a/targets/TARGET_STM/TARGET_STM32F2/pwmout_api.c +++ /dev/null @@ -1,282 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2016, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "pwmout_api.h" - -#if DEVICE_PWMOUT - -#include "cmsis.h" -#include "pinmap.h" -#include "mbed_error.h" -#include "PeripheralPins.h" - -static TIM_HandleTypeDef TimHandle; - -void pwmout_init(pwmout_t* obj, PinName pin) -{ - // Get the peripheral name from the pin and assign it to the object - obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - MBED_ASSERT(obj->pwm != (PWMName)NC); - - // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object - uint32_t function = pinmap_function(pin, PinMap_PWM); - MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); - obj->inverted = STM_PIN_INVERTED(function); - - // Enable TIM clock -#if defined(TIM1_BASE) - if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE(); -#endif -#if defined(TIM2_BASE) - if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE(); -#endif -#if defined(TIM3_BASE) - if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE(); -#endif -#if defined(TIM4_BASE) - if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE(); -#endif -#if defined(TIM5_BASE) - if (obj->pwm == PWM_5) __HAL_RCC_TIM5_CLK_ENABLE(); -#endif -#if defined(TIM8_BASE) - if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE(); -#endif -#if defined(TIM9_BASE) - if (obj->pwm == PWM_9) __HAL_RCC_TIM9_CLK_ENABLE(); -#endif -#if defined(TIM10_BASE) - if (obj->pwm == PWM_10) __HAL_RCC_TIM10_CLK_ENABLE(); -#endif -#if defined(TIM11_BASE) - if (obj->pwm == PWM_11) __HAL_RCC_TIM11_CLK_ENABLE(); -#endif -#if defined(TIM12_BASE) - if (obj->pwm == PWM_12) __HAL_RCC_TIM12_CLK_ENABLE(); -#endif -#if defined(TIM13_BASE) - if (obj->pwm == PWM_13) __HAL_RCC_TIM13_CLK_ENABLE(); -#endif -#if defined(TIM14_BASE) - if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE(); -#endif - - // Configure GPIO - pinmap_pinout(pin, PinMap_PWM); - - obj->pin = pin; - obj->period = 0; - obj->pulse = 0; - - pwmout_period_us(obj, 20000); // 20 ms per default -} - -void pwmout_free(pwmout_t* obj) -{ - // Configure GPIO - pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); -} - -void pwmout_write(pwmout_t* obj, float value) -{ - TIM_OC_InitTypeDef sConfig; - int channel = 0; - - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - if (value < (float)0.0) { - value = 0.0; - } else if (value > (float)1.0) { - value = 1.0; - } - - obj->pulse = (uint32_t)((float)obj->period * value); - - // Configure channels - sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse; - sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_DISABLE; - sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; - sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; - - switch (obj->channel) { - case 1: - channel = TIM_CHANNEL_1; - break; - case 2: - channel = TIM_CHANNEL_2; - break; - case 3: - channel = TIM_CHANNEL_3; - break; - case 4: - channel = TIM_CHANNEL_4; - break; - default: - return; - } - - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { - error("Cannot initialize PWM\n"); - } - - if (obj->inverted) { - HAL_TIMEx_PWMN_Start(&TimHandle, channel); - } else { - HAL_TIM_PWM_Start(&TimHandle, channel); - } -} - -float pwmout_read(pwmout_t* obj) -{ - float value = 0; - if (obj->period > 0) { - value = (float)(obj->pulse) / (float)(obj->period); - } - return ((value > (float)1.0) ? (float)(1.0) : (value)); -} - -void pwmout_period(pwmout_t* obj, float seconds) -{ - pwmout_period_us(obj, seconds * 1000000.0f); -} - -void pwmout_period_ms(pwmout_t* obj, int ms) -{ - pwmout_period_us(obj, ms * 1000); -} - -void pwmout_period_us(pwmout_t* obj, int us) -{ - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - RCC_ClkInitTypeDef RCC_ClkInitStruct; - uint32_t PclkFreq; - uint32_t APBxCLKDivider; - float dc = pwmout_read(obj); - - __HAL_TIM_DISABLE(&TimHandle); - - // Get clock configuration - // Note: PclkFreq contains here the Latency (not used after) - HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq); - - // Get the PCLK and APBCLK divider related to the timer - switch (obj->pwm) { - - // APB1 clock -#if defined(TIM2_BASE) - case PWM_2: -#endif -#if defined(TIM3_BASE) - case PWM_3: -#endif -#if defined(TIM4_BASE) - case PWM_4: -#endif -#if defined(TIM5_BASE) - case PWM_5: -#endif -#if defined(TIM12_BASE) - case PWM_12: -#endif -#if defined(TIM13_BASE) - case PWM_13: -#endif -#if defined(TIM14_BASE) - case PWM_14: -#endif - PclkFreq = HAL_RCC_GetPCLK1Freq(); - APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider; - break; - - // APB2 clock -#if defined(TIM1_BASE) - case PWM_1: -#endif -#if defined(TIM8_BASE) - case PWM_8: -#endif -#if defined(TIM9_BASE) - case PWM_9: -#endif -#if defined(TIM10_BASE) - case PWM_10: -#endif -#if defined(TIM11_BASE) - case PWM_11: -#endif - PclkFreq = HAL_RCC_GetPCLK2Freq(); - APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider; - break; - default: - return; - } - - TimHandle.Init.Period = us - 1; - // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx - if (APBxCLKDivider == RCC_HCLK_DIV1) - TimHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick - else - TimHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - - if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { - error("Cannot initialize PWM\n"); - } - - // Set duty cycle again - pwmout_write(obj, dc); - - // Save for future use - obj->period = us; - - __HAL_TIM_ENABLE(&TimHandle); -} - -void pwmout_pulsewidth(pwmout_t* obj, float seconds) -{ - pwmout_pulsewidth_us(obj, seconds * 1000000.0f); -} - -void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) -{ - pwmout_pulsewidth_us(obj, ms * 1000); -} - -void pwmout_pulsewidth_us(pwmout_t* obj, int us) -{ - float value = (float)us / (float)obj->period; - pwmout_write(obj, value); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h new file mode 100644 index 0000000000..480493e3a1 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h @@ -0,0 +1,96 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_ON_APB2 = 1, + PWMOUT_UNKNOWN = 2 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM12_BASE) + {PWM_12, PWMOUT_ON_APB1}, +#endif +#if defined(TIM13_BASE) + {PWM_13, PWMOUT_ON_APB1}, +#endif +#if defined(TIM14_BASE) + {PWM_14, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM9_BASE) + {PWM_9, PWMOUT_ON_APB2}, +#endif +#if defined(TIM10_BASE) + {PWM_10, PWMOUT_ON_APB2}, +#endif +#if defined(TIM11_BASE) + {PWM_11, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32F3/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32F3/pwmout_api.c deleted file mode 100644 index 2f3c8a925b..0000000000 --- a/targets/TARGET_STM/TARGET_STM32F3/pwmout_api.c +++ /dev/null @@ -1,259 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2015, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "pwmout_api.h" - -#if DEVICE_PWMOUT - -#include "cmsis.h" -#include "pinmap.h" -#include "mbed_error.h" -#include "PeripheralPins.h" - -static TIM_HandleTypeDef TimHandle; - -void pwmout_init(pwmout_t* obj, PinName pin) -{ - // Get the peripheral name from the pin and assign it to the object - obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - MBED_ASSERT(obj->pwm != (PWMName)NC); - - // Get the pin function and assign the used channel to the object - uint32_t function = pinmap_function(pin, PinMap_PWM); - MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); - obj->inverted = STM_PIN_INVERTED(function); - -#if defined(TIM1_BASE) - if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE(); -#endif -#if defined(TIM2_BASE) - if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE(); -#endif -#if defined(TIM3_BASE) - if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE(); -#endif -#if defined(TIM4_BASE) - if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE(); -#endif -#if defined(TIM5_BASE) - if (obj->pwm == PWM_5) __HAL_RCC_TIM5_CLK_ENABLE(); -#endif -#if defined(TIM8_BASE) - if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE(); -#endif -#if defined(TIM9_BASE) - if (obj->pwm == PWM_9) __HAL_RCC_TIM9_CLK_ENABLE(); -#endif -#if defined(TIM10_BASE) - if (obj->pwm == PWM_10) __HAL_RCC_TIM10_CLK_ENABLE(); -#endif -#if defined(TIM11_BASE) - if (obj->pwm == PWM_11) __HAL_RCC_TIM11_CLK_ENABLE(); -#endif -#if defined(TIM12_BASE) - if (obj->pwm == PWM_12) __HAL_RCC_TIM12_CLK_ENABLE(); -#endif -#if defined(TIM13_BASE) - if (obj->pwm == PWM_13) __HAL_RCC_TIM13_CLK_ENABLE(); -#endif -#if defined(TIM14_BASE) - if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE(); -#endif -#if defined(TIM15_BASE) - if (obj->pwm == PWM_15) __HAL_RCC_TIM15_CLK_ENABLE(); -#endif -#if defined(TIM16_BASE) - if (obj->pwm == PWM_16) __HAL_RCC_TIM16_CLK_ENABLE(); -#endif -#if defined(TIM17_BASE) - if (obj->pwm == PWM_17) __HAL_RCC_TIM17_CLK_ENABLE(); -#endif -#if defined(TIM18_BASE) - if (obj->pwm == PWM_18) __HAL_RCC_TIM18_CLK_ENABLE(); -#endif -#if defined(TIM19_BASE) - if (obj->pwm == PWM_19) __HAL_RCC_TIM19_CLK_ENABLE(); -#endif -#if defined(TIM20_BASE) - if (obj->pwm == PWM_20) __HAL_RCC_TIM20_CLK_ENABLE(); -#endif - - // Configure GPIO - pinmap_pinout(pin, PinMap_PWM); - - obj->pin = pin; - obj->period = 0; - obj->pulse = 0; - obj->prescaler = 1; - - pwmout_period_us(obj, 20000); // 20 ms per default -} - -void pwmout_free(pwmout_t* obj) -{ - // Configure GPIO - pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); -} - -void pwmout_write(pwmout_t* obj, float value) -{ - TIM_OC_InitTypeDef sConfig; - int channel = 0; - - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - if (value < (float)0.0) { - value = 0.0; - } else if (value > (float)1.0) { - value = 1.0; - } - - obj->pulse = (uint32_t)((float)obj->period * value); - - // Configure channels - sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse / obj->prescaler; - sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_DISABLE; - sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; - sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; - - switch (obj->channel) { - case 1: - channel = TIM_CHANNEL_1; - break; - case 2: - channel = TIM_CHANNEL_2; - break; - case 3: - channel = TIM_CHANNEL_3; - break; - case 4: - channel = TIM_CHANNEL_4; - break; - default: - return; - } - - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { - error("Cannot initialize PWM"); - } - - if (obj->inverted) { - HAL_TIMEx_PWMN_Start(&TimHandle, channel); - } else { - HAL_TIM_PWM_Start(&TimHandle, channel); - } -} - -float pwmout_read(pwmout_t* obj) -{ - float value = 0; - if (obj->period > 0) { - value = (float)(obj->pulse) / (float)(obj->period); - } - return ((value > (float)1.0) ? (float)(1.0) : (value)); -} - -void pwmout_period(pwmout_t* obj, float seconds) -{ - pwmout_period_us(obj, seconds * 1000000.0f); -} - -void pwmout_period_ms(pwmout_t* obj, int ms) -{ - pwmout_period_us(obj, ms * 1000); -} - -void pwmout_period_us(pwmout_t* obj, int us) -{ - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - float dc = pwmout_read(obj); - - __HAL_TIM_DISABLE(&TimHandle); - - // Update the SystemCoreClock variable - SystemCoreClockUpdate(); - - /* To make it simple, we use to possible prescaler values which lead to: - * pwm unit = 1us, period/pulse can be from 1us to 65535us - * or - * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec - * Be careful that all the channels of a PWM shares the same prescaler - */ - if (us > 0xFFFF) { - obj->prescaler = 500; - } else { - obj->prescaler = 1; - } - TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1; - - if (TimHandle.Init.Prescaler > 0xFFFF) - error("PWM: out of range prescaler"); - - TimHandle.Init.Period = (us - 1) / obj->prescaler; - if (TimHandle.Init.Period > 0xFFFF) - error("PWM: out of range period"); - - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - - if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { - error("Cannot initialize PWM"); - } - - // Save for future use - obj->period = us; - - // Set duty cycle again - pwmout_write(obj, dc); - - __HAL_TIM_ENABLE(&TimHandle); -} - -void pwmout_pulsewidth(pwmout_t* obj, float seconds) -{ - pwmout_pulsewidth_us(obj, seconds * 1000000.0f); -} - -void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) -{ - pwmout_pulsewidth_us(obj, ms * 1000); -} - -void pwmout_pulsewidth_us(pwmout_t* obj, int us) -{ - float value = (float)us / (float)obj->period; - pwmout_write(obj, value); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h new file mode 100644 index 0000000000..a0af9d6b01 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h @@ -0,0 +1,87 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_ON_APB2 = 1, + PWMOUT_UNKNOWN = 2 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM15_BASE) + {PWM_15, PWMOUT_ON_APB2}, +#endif +#if defined(TIM16_BASE) + {PWM_16, PWMOUT_ON_APB2}, +#endif +#if defined(TIM17_BASE) + {PWM_17, PWMOUT_ON_APB2}, +#endif +#if defined(TIM20_BASE) + {PWM_20, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h new file mode 100644 index 0000000000..480493e3a1 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h @@ -0,0 +1,96 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_ON_APB2 = 1, + PWMOUT_UNKNOWN = 2 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM12_BASE) + {PWM_12, PWMOUT_ON_APB1}, +#endif +#if defined(TIM13_BASE) + {PWM_13, PWMOUT_ON_APB1}, +#endif +#if defined(TIM14_BASE) + {PWM_14, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM9_BASE) + {PWM_9, PWMOUT_ON_APB2}, +#endif +#if defined(TIM10_BASE) + {PWM_10, PWMOUT_ON_APB2}, +#endif +#if defined(TIM11_BASE) + {PWM_11, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32F7/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32F7/pwmout_api.c deleted file mode 100644 index 1e95b476fb..0000000000 --- a/targets/TARGET_STM/TARGET_STM32F7/pwmout_api.c +++ /dev/null @@ -1,253 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2015, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "pwmout_api.h" - -#if DEVICE_PWMOUT - -#include "cmsis.h" -#include "pinmap.h" -#include "mbed_error.h" -#include "PeripheralPins.h" - -static TIM_HandleTypeDef TimHandle; - -void pwmout_init(pwmout_t* obj, PinName pin) -{ - // Get the peripheral name from the pin and assign it to the object - obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - MBED_ASSERT(obj->pwm != (PWMName)NC); - - // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object - uint32_t function = pinmap_function(pin, PinMap_PWM); - MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); - obj->inverted = STM_PIN_INVERTED(function); - - // Enable TIM clock - if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE(); - if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE(); - if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE(); - if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE(); - if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE(); - if (obj->pwm == PWM_9) __HAL_RCC_TIM9_CLK_ENABLE(); - if (obj->pwm == PWM_10) __HAL_RCC_TIM10_CLK_ENABLE(); - if (obj->pwm == PWM_11) __HAL_RCC_TIM11_CLK_ENABLE(); - if (obj->pwm == PWM_12) __HAL_RCC_TIM12_CLK_ENABLE(); - if (obj->pwm == PWM_13) __HAL_RCC_TIM13_CLK_ENABLE(); - if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE(); - - // Configure GPIO - pinmap_pinout(pin, PinMap_PWM); - - obj->pin = pin; - obj->period = 0; - obj->pulse = 0; - obj->prescaler = 1; - - pwmout_period_us(obj, 20000); // 20 ms per default -} - -void pwmout_free(pwmout_t* obj) -{ - // Configure GPIO - pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); -} - -void pwmout_write(pwmout_t* obj, float value) -{ - TIM_OC_InitTypeDef sConfig; - int channel = 0; - - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - if (value < (float)0.0) { - value = 0.0; - } else if (value > (float)1.0) { - value = 1.0; - } - - obj->pulse = (uint32_t)((float)obj->period * value); - - // Configure channels - sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse / obj->prescaler; - sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_DISABLE; - sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; - sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; - - switch (obj->channel) { - case 1: - channel = TIM_CHANNEL_1; - break; - case 2: - channel = TIM_CHANNEL_2; - break; - case 3: - channel = TIM_CHANNEL_3; - break; - case 4: - channel = TIM_CHANNEL_4; - break; - default: - return; - } - - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { - error("Cannot configure PWM channel\n"); - } - - if (obj->inverted) { - HAL_TIMEx_PWMN_Start(&TimHandle, channel); - } else { - HAL_TIM_PWM_Start(&TimHandle, channel); - } -} - -float pwmout_read(pwmout_t* obj) -{ - float value = 0; - if (obj->period > 0) { - value = (float)(obj->pulse) / (float)(obj->period); - } - return ((value > (float)1.0) ? (float)(1.0) : (value)); -} - -void pwmout_period(pwmout_t* obj, float seconds) -{ - pwmout_period_us(obj, seconds * 1000000.0f); -} - -void pwmout_period_ms(pwmout_t* obj, int ms) -{ - pwmout_period_us(obj, ms * 1000); -} - -void pwmout_period_us(pwmout_t* obj, int us) -{ - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - RCC_ClkInitTypeDef RCC_ClkInitStruct; - uint32_t PclkFreq; - uint32_t APBxCLKDivider; - float dc = pwmout_read(obj); - - __HAL_TIM_DISABLE(&TimHandle); - - // Get clock configuration - // Note: PclkFreq contains here the Latency (not used after) - HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq); - - // Get the PCLK and APBCLK divider related to the timer - switch (obj->pwm) { - - // APB1 clock - case PWM_2: - case PWM_3: - case PWM_4: - case PWM_5: - case PWM_12: - case PWM_13: - case PWM_14: - PclkFreq = HAL_RCC_GetPCLK1Freq(); - APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider; - break; - - // APB2 clock - case PWM_1: - case PWM_8: - case PWM_9: - case PWM_10: - case PWM_11: - PclkFreq = HAL_RCC_GetPCLK2Freq(); - APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider; - break; - default: - return; - } - - /* To make it simple, we use to possible prescaler values which lead to: - * pwm unit = 1us, period/pulse can be from 1us to 65535us - * or - * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec - * Be careful that all the channels of a PWM shares the same prescaler - */ - if (us > 0xFFFF) { - obj->prescaler = 500; - } else { - obj->prescaler = 1; - } - - // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx - if (APBxCLKDivider == RCC_HCLK_DIV1) - TimHandle.Init.Prescaler = (uint16_t)(((PclkFreq) / 1000000) * obj->prescaler) - 1; // 1 us tick - else - TimHandle.Init.Prescaler = (uint16_t)(((PclkFreq * 2) / 1000000) * obj->prescaler) - 1; // 1 us tick - - if (TimHandle.Init.Prescaler > 0xFFFF) - error("PWM: out of range prescaler"); - - TimHandle.Init.Period = (us - 1) / obj->prescaler; - if (TimHandle.Init.Period > 0xFFFF) - error("PWM: out of range period"); - - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - - if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { - error("Cannot initialize PWM\n"); - } - - // Save for future use - obj->period = us; - - // Set duty cycle again - pwmout_write(obj, dc); - - __HAL_TIM_ENABLE(&TimHandle); -} - -void pwmout_pulsewidth(pwmout_t* obj, float seconds) -{ - pwmout_pulsewidth_us(obj, seconds * 1000000.0f); -} - -void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) -{ - pwmout_pulsewidth_us(obj, ms * 1000); -} - -void pwmout_pulsewidth_us(pwmout_t* obj, int us) -{ - float value = (float)us / (float)obj->period; - pwmout_write(obj, value); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h new file mode 100644 index 0000000000..480493e3a1 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h @@ -0,0 +1,96 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_ON_APB2 = 1, + PWMOUT_UNKNOWN = 2 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM12_BASE) + {PWM_12, PWMOUT_ON_APB1}, +#endif +#if defined(TIM13_BASE) + {PWM_13, PWMOUT_ON_APB1}, +#endif +#if defined(TIM14_BASE) + {PWM_14, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM9_BASE) + {PWM_9, PWMOUT_ON_APB2}, +#endif +#if defined(TIM10_BASE) + {PWM_10, PWMOUT_ON_APB2}, +#endif +#if defined(TIM11_BASE) + {PWM_11, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32L0/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32L0/pwmout_api.c deleted file mode 100644 index 4f2ef410c9..0000000000 --- a/targets/TARGET_STM/TARGET_STM32L0/pwmout_api.c +++ /dev/null @@ -1,204 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2015, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "pwmout_api.h" - -#if DEVICE_PWMOUT - -#include "cmsis.h" -#include "pinmap.h" -#include "mbed_error.h" -#include "PeripheralPins.h" - -static TIM_HandleTypeDef TimHandle; - -void pwmout_init(pwmout_t* obj, PinName pin) -{ - // Get the peripheral name from the pin and assign it to the object - obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - MBED_ASSERT(obj->pwm != (PWMName)NC); - - // Get the pin function and assign the used channel to the object - uint32_t function = pinmap_function(pin, PinMap_PWM); - MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); - obj->inverted = STM_PIN_INVERTED(function); - - // Enable TIM clock - if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); -#if defined(TIM3_BASE) - if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); -#endif - if (obj->pwm == PWM_21) __TIM21_CLK_ENABLE(); -#if defined(TIM22_BASE) - if (obj->pwm == PWM_22) __TIM22_CLK_ENABLE(); -#endif - - // Configure GPIO - pinmap_pinout(pin, PinMap_PWM); - - obj->pin = pin; - obj->period = 0; - obj->pulse = 0; - obj->prescaler = 1; - - pwmout_period_us(obj, 20000); // 20 ms per default -} - -void pwmout_free(pwmout_t* obj) -{ - // Configure GPIO - pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); -} - -void pwmout_write(pwmout_t* obj, float value) -{ - TIM_OC_InitTypeDef sConfig; - int channel = 0; - - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - if (value < (float)0.0) { - value = 0.0; - } else if (value > (float)1.0) { - value = 1.0; - } - - obj->pulse = (uint32_t)((float)obj->period * value); - - // Configure channels - sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse / obj->prescaler; - sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_ENABLE; - - switch (obj->channel) { - case 1: - channel = TIM_CHANNEL_1; - break; - case 2: - channel = TIM_CHANNEL_2; - break; - case 3: - channel = TIM_CHANNEL_3; - break; - case 4: - channel = TIM_CHANNEL_4; - break; - default: - return; - } - - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { - error("Cannot initialize PWM"); - } - - HAL_TIM_PWM_Start(&TimHandle, channel); -} - -float pwmout_read(pwmout_t* obj) -{ - float value = 0; - if (obj->period > 0) { - value = (float)(obj->pulse) / (float)(obj->period); - } - return ((value > (float)1.0) ? (float)(1.0) : (value)); -} - -void pwmout_period(pwmout_t* obj, float seconds) -{ - pwmout_period_us(obj, seconds * 1000000.0f); -} - -void pwmout_period_ms(pwmout_t* obj, int ms) -{ - pwmout_period_us(obj, ms * 1000); -} - -void pwmout_period_us(pwmout_t* obj, int us) -{ - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - float dc = pwmout_read(obj); - - __HAL_TIM_DISABLE(&TimHandle); - - /* To make it simple, we use to possible prescaler values which lead to: - * pwm unit = 1us, period/pulse can be from 1us to 65535us - * or - * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec - * Be careful that all the channels of a PWM shares the same prescaler - */ - if (us > 0xFFFF) { - obj->prescaler = 500; - } else { - obj->prescaler = 1; - } - TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1; - - if (TimHandle.Init.Prescaler > 0xFFFF) - error("PWM: out of range prescaler"); - - TimHandle.Init.Period = (us - 1) / obj->prescaler; - if (TimHandle.Init.Period > 0xFFFF) - error("PWM: out of range period"); - - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - - if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { - error("Cannot initialize PWM"); - } - - // Save for future use - obj->period = us; - - // Set duty cycle again - pwmout_write(obj, dc); - - __HAL_TIM_ENABLE(&TimHandle); -} - -void pwmout_pulsewidth(pwmout_t* obj, float seconds) -{ - pwmout_pulsewidth_us(obj, seconds * 1000000.0f); -} - -void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) -{ - pwmout_pulsewidth_us(obj, ms * 1000); -} - -void pwmout_pulsewidth_us(pwmout_t* obj, int us) -{ - float value = (float)us / (float)obj->period; - pwmout_write(obj, value); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h new file mode 100644 index 0000000000..71f38e4f3f --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h @@ -0,0 +1,74 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +#define PWMOUT_INVERTED_NOT_SUPPORTED + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_ON_APB2 = 1, + PWMOUT_UNKNOWN = 2 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM21_BASE) + {PWM_21, PWMOUT_ON_APB2}, +#endif +#if defined(TIM22_BASE) + {PWM_22, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c deleted file mode 100644 index 02ce8a22b5..0000000000 --- a/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c +++ /dev/null @@ -1,212 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2014, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "pwmout_api.h" - -#if DEVICE_PWMOUT - -#include "cmsis.h" -#include "pinmap.h" -#include "mbed_error.h" -#include "PeripheralPins.h" - -static TIM_HandleTypeDef TimHandle; - -void pwmout_init(pwmout_t* obj, PinName pin) -{ - // Get the peripheral name from the pin and assign it to the object - obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - - if (obj->pwm == (PWMName)NC) { - error("PWM error: pinout mapping failed."); - } - - // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object - uint32_t function = pinmap_function(pin, PinMap_PWM); - MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); - obj->inverted = STM_PIN_INVERTED(function); - - // Enable TIM clock - if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); - if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); - if (obj->pwm == PWM_4) __TIM4_CLK_ENABLE(); - if (obj->pwm == PWM_5) __TIM5_CLK_ENABLE(); - if (obj->pwm == PWM_9) __TIM9_CLK_ENABLE(); - if (obj->pwm == PWM_10) __TIM10_CLK_ENABLE(); - if (obj->pwm == PWM_11) __TIM11_CLK_ENABLE(); - - // Configure GPIO - pinmap_pinout(pin, PinMap_PWM); - - obj->pin = pin; - obj->period = 0; - obj->pulse = 0; - obj->prescaler = 1; - - pwmout_period_us(obj, 20000); // 20 ms per default -} - -void pwmout_free(pwmout_t* obj) -{ - // Configure GPIO - pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); -} - -void pwmout_write(pwmout_t* obj, float value) -{ - TIM_OC_InitTypeDef sConfig; - int channel = 0; - - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - if (value < (float)0.0) { - value = 0.0; - } else if (value > (float)1.0) { - value = 1.0; - } - - obj->pulse = (uint32_t)((float)obj->period * value); - - // Configure channels - sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse / obj->prescaler; - sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_DISABLE; -#ifdef TIM_OCIDLESTATE_RESET - sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; -#endif -#ifdef TIM_OCNIDLESTATE_RESET - sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; -#endif - - switch (obj->channel) { - case 1: - channel = TIM_CHANNEL_1; - break; - case 2: - channel = TIM_CHANNEL_2; - break; - case 3: - channel = TIM_CHANNEL_3; - break; - case 4: - channel = TIM_CHANNEL_4; - break; - default: - return; - } - - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { - error("Cannot initialize PWM\n"); - } - - HAL_TIM_PWM_Start(&TimHandle, channel); -} - -float pwmout_read(pwmout_t* obj) -{ - float value = 0; - if (obj->period > 0) { - value = (float)(obj->pulse) / (float)(obj->period); - } - return ((value > (float)1.0) ? (float)(1.0) : (value)); -} - -void pwmout_period(pwmout_t* obj, float seconds) -{ - pwmout_period_us(obj, seconds * 1000000.0f); -} - -void pwmout_period_ms(pwmout_t* obj, int ms) -{ - pwmout_period_us(obj, ms * 1000); -} - -void pwmout_period_us(pwmout_t* obj, int us) -{ - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - float dc = pwmout_read(obj); - - __HAL_TIM_DISABLE(&TimHandle); - - /* To make it simple, we use to possible prescaler values which lead to: - * pwm unit = 1us, period/pulse can be from 1us to 65535us - * or - * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec - * Be careful that all the channels of a PWM shares the same prescaler - */ - if (us > 0xFFFF) { - obj->prescaler = 500; - } else { - obj->prescaler = 1; - } - TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1; - - if (TimHandle.Init.Prescaler > 0xFFFF) - error("PWM: out of range prescaler"); - - TimHandle.Init.Period = (us - 1) / obj->prescaler; - if (TimHandle.Init.Period > 0xFFFF) - error("PWM: out of range period"); - - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - - if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { - error("Cannot initialize PWM"); - } - - // Save for future use - obj->period = us; - - // Set duty cycle again - pwmout_write(obj, dc); - - __HAL_TIM_ENABLE(&TimHandle); -} - -void pwmout_pulsewidth(pwmout_t* obj, float seconds) -{ - pwmout_pulsewidth_us(obj, seconds * 1000000.0f); -} - -void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) -{ - pwmout_pulsewidth_us(obj, ms * 1000); -} - -void pwmout_pulsewidth_us(pwmout_t* obj, int us) -{ - float value = (float)us / (float)obj->period; - pwmout_write(obj, value); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h new file mode 100644 index 0000000000..50cc41cd9e --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h @@ -0,0 +1,84 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +/* L1 HAL do not offer Output Compare idle nor inverted mode*/ +#define PWMOUT_INVERTED_NOT_SUPPORTED + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_ON_APB2 = 1, + PWMOUT_UNKNOWN = 2 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM9_BASE) + {PWM_9, PWMOUT_ON_APB2}, +#endif +#if defined(TIM10_BASE) + {PWM_10, PWMOUT_ON_APB2}, +#endif +#if defined(TIM11_BASE) + {PWM_11, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32L4/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32L4/pwmout_api.c deleted file mode 100644 index 8b8cfe9068..0000000000 --- a/targets/TARGET_STM/TARGET_STM32L4/pwmout_api.c +++ /dev/null @@ -1,222 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2015, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "pwmout_api.h" - -#if DEVICE_PWMOUT - -#include "cmsis.h" -#include "pinmap.h" -#include "mbed_error.h" -#include "PeripheralPins.h" - -static TIM_HandleTypeDef TimHandle; - -void pwmout_init(pwmout_t* obj, PinName pin) -{ - // Get the peripheral name from the pin and assign it to the object - obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - MBED_ASSERT(obj->pwm != (PWMName)NC); - - // Get the pin function and assign the used channel to the object - uint32_t function = pinmap_function(pin, PinMap_PWM); - MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); - obj->inverted = STM_PIN_INVERTED(function); - - // Enable TIM clock - if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE(); - if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE(); -#if defined(TIM3_BASE) - if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE(); -#endif -#if defined(TIM4_BASE) - if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE(); -#endif -#if defined(TIM5_BASE) - if (obj->pwm == PWM_5) __HAL_RCC_TIM5_CLK_ENABLE(); -#endif -#if defined(TIM8_BASE) - if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE(); -#endif - if (obj->pwm == PWM_15) __HAL_RCC_TIM15_CLK_ENABLE(); - if (obj->pwm == PWM_16) __HAL_RCC_TIM16_CLK_ENABLE(); -#if defined(TIM17_BASE) - if (obj->pwm == PWM_17) __HAL_RCC_TIM17_CLK_ENABLE(); -#endif - - // Configure GPIO - pinmap_pinout(pin, PinMap_PWM); - - obj->pin = pin; - obj->period = 0; - obj->pulse = 0; - obj->prescaler = 1; - - pwmout_period_us(obj, 20000); // 20 ms per default -} - -void pwmout_free(pwmout_t* obj) -{ - // Configure GPIO - pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); -} - -void pwmout_write(pwmout_t* obj, float value) -{ - TIM_OC_InitTypeDef sConfig; - int channel = 0; - - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - if (value < (float)0.0) { - value = 0.0; - } else if (value > (float)1.0) { - value = 1.0; - } - - obj->pulse = (uint32_t)((float)obj->period * value); - - // Configure channels - sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse / obj->prescaler; - sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_ENABLE; - sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; - sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; - - switch (obj->channel) { - case 1: - channel = TIM_CHANNEL_1; - break; - case 2: - channel = TIM_CHANNEL_2; - break; - case 3: - channel = TIM_CHANNEL_3; - break; - case 4: - channel = TIM_CHANNEL_4; - break; - default: - return; - } - - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { - error("Cannot initialize PWM\n"); - } - - if (obj->inverted) { - HAL_TIMEx_PWMN_Start(&TimHandle, channel); - } else { - HAL_TIM_PWM_Start(&TimHandle, channel); - } -} - -float pwmout_read(pwmout_t* obj) -{ - float value = 0; - if (obj->period > 0) { - value = (float)(obj->pulse) / (float)(obj->period); - } - return ((value > (float)1.0) ? (float)(1.0) : (value)); -} - -void pwmout_period(pwmout_t* obj, float seconds) -{ - pwmout_period_us(obj, seconds * 1000000.0f); -} - -void pwmout_period_ms(pwmout_t* obj, int ms) -{ - pwmout_period_us(obj, ms * 1000); -} - -void pwmout_period_us(pwmout_t* obj, int us) -{ - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - float dc = pwmout_read(obj); - - __HAL_TIM_DISABLE(&TimHandle); - - /* To make it simple, we use to possible prescaler values which lead to: - * pwm unit = 1us, period/pulse can be from 1us to 65535us - * or - * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec - * Be careful that all the channels of a PWM shares the same prescaler - */ - if (us > 0xFFFF) { - obj->prescaler = 500; - } else { - obj->prescaler = 1; - } - TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1; - - if (TimHandle.Init.Prescaler > 0xFFFF) - error("PWM: out of range prescaler"); - - TimHandle.Init.Period = (us - 1) / obj->prescaler; - if (TimHandle.Init.Period > 0xFFFF) - error("PWM: out of range period"); - - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - - if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { - error("Cannot initialize PWM\n"); - } - - // Save for future use - obj->period = us; - - // Set duty cycle again - pwmout_write(obj, dc); - - __HAL_TIM_ENABLE(&TimHandle); -} - -void pwmout_pulsewidth(pwmout_t* obj, float seconds) -{ - pwmout_pulsewidth_us(obj, seconds * 1000000.0f); -} - -void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) -{ - pwmout_pulsewidth_us(obj, ms * 1000); -} - -void pwmout_pulsewidth_us(pwmout_t* obj, int us) -{ - float value = (float)us / (float)obj->period; - pwmout_write(obj, value); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h new file mode 100644 index 0000000000..382d6fb149 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h @@ -0,0 +1,87 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_ON_APB2 = 1, + PWMOUT_UNKNOWN = 2 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM15_BASE) + {PWM_15, PWMOUT_ON_APB2}, +#endif +#if defined(TIM16_BASE) + {PWM_16, PWMOUT_ON_APB2}, +#endif +#if defined(TIM17_BASE) + {PWM_17, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32F4/pwmout_api.c b/targets/TARGET_STM/pwmout_api.c similarity index 75% rename from targets/TARGET_STM/TARGET_STM32F4/pwmout_api.c rename to targets/TARGET_STM/pwmout_api.c index 04a66505f9..f8a22e7035 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/pwmout_api.c +++ b/targets/TARGET_STM/pwmout_api.c @@ -35,6 +35,7 @@ #include "pinmap.h" #include "mbed_error.h" #include "PeripheralPins.h" +#include "pwmout_device.h" static TIM_HandleTypeDef TimHandle; @@ -87,7 +88,30 @@ void pwmout_init(pwmout_t* obj, PinName pin) #if defined(TIM14_BASE) if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE(); #endif - +#if defined(TIM15_BASE) + if (obj->pwm == PWM_15) __HAL_RCC_TIM15_CLK_ENABLE(); +#endif +#if defined(TIM16_BASE) + if (obj->pwm == PWM_16) __HAL_RCC_TIM16_CLK_ENABLE(); +#endif +#if defined(TIM17_BASE) + if (obj->pwm == PWM_17) __HAL_RCC_TIM17_CLK_ENABLE(); +#endif +#if defined(TIM18_BASE) + if (obj->pwm == PWM_18) __HAL_RCC_TIM18_CLK_ENABLE(); +#endif +#if defined(TIM19_BASE) + if (obj->pwm == PWM_19) __HAL_RCC_TIM19_CLK_ENABLE(); +#endif +#if defined(TIM20_BASE) + if (obj->pwm == PWM_20) __HAL_RCC_TIM20_CLK_ENABLE(); +#endif +#if defined(TIM21_BASE) + if (obj->pwm == PWM_21) __HAL_RCC_TIM21_CLK_ENABLE(); +#endif +#if defined(TIM22_BASE) + if (obj->pwm == PWM_22) __HAL_RCC_TIM22_CLK_ENABLE(); +#endif // Configure GPIO pinmap_pinout(pin, PinMap_PWM); @@ -124,10 +148,14 @@ void pwmout_write(pwmout_t* obj, float value) sConfig.OCMode = TIM_OCMODE_PWM1; sConfig.Pulse = obj->pulse / obj->prescaler; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfig.OCFastMode = TIM_OCFAST_DISABLE; +#if defined(TIM_OCIDLESTATE_RESET) sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; +#endif +#if defined(TIM_OCNIDLESTATE_RESET) + sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; +#endif switch (obj->channel) { case 1: @@ -150,9 +178,12 @@ void pwmout_write(pwmout_t* obj, float value) error("Cannot initialize PWM\n"); } +#if !defined(PWMOUT_INVERTED_NOT_SUPPORTED) if (obj->inverted) { HAL_TIMEx_PWMN_Start(&TimHandle, channel); - } else { + } else +#endif + { HAL_TIM_PWM_Start(&TimHandle, channel); } } @@ -180,9 +211,10 @@ void pwmout_period_us(pwmout_t* obj, int us) { TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); RCC_ClkInitTypeDef RCC_ClkInitStruct; - uint32_t PclkFreq; - uint32_t APBxCLKDivider; + uint32_t PclkFreq = 0; + uint32_t APBxCLKDivider = RCC_HCLK_DIV1; float dc = pwmout_read(obj); + uint8_t i = 0; __HAL_TIM_DISABLE(&TimHandle); @@ -190,82 +222,43 @@ void pwmout_period_us(pwmout_t* obj, int us) // Note: PclkFreq contains here the Latency (not used after) HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq); - // Get the PCLK and APBCLK divider related to the timer - switch (obj->pwm) { - - // APB1 clock -#if defined(TIM2_BASE) - case PWM_2: -#endif -#if defined(TIM3_BASE) - case PWM_3: -#endif -#if defined(TIM4_BASE) - case PWM_4: -#endif -#if defined(TIM5_BASE) - case PWM_5: -#endif -#if defined(TIM12_BASE) - case PWM_12: -#endif -#if defined(TIM13_BASE) - case PWM_13: -#endif -#if defined(TIM14_BASE) - case PWM_14: -#endif - PclkFreq = HAL_RCC_GetPCLK1Freq(); - APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider; - break; - - // APB2 clock -#if defined(TIM1_BASE) - case PWM_1: -#endif -#if defined(TIM8_BASE) - case PWM_8: -#endif -#if defined(TIM9_BASE) - case PWM_9: -#endif -#if defined(TIM10_BASE) - case PWM_10: -#endif -#if defined(TIM11_BASE) - case PWM_11: -#endif - PclkFreq = HAL_RCC_GetPCLK2Freq(); - APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider; - break; - default: - return; + /* Parse the pwm / apb mapping table to find the right entry */ + while(pwm_apb_map_table[i].pwm != obj->pwm) { + i++; } - /* To make it simple, we use to possible prescaler values which lead to: - * pwm unit = 1us, period/pulse can be from 1us to 65535us - * or - * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec - * Be careful that all the channels of a PWM shares the same prescaler - */ - if (us > 0xFFFF) { - obj->prescaler = 500; + if(pwm_apb_map_table[i].pwm == 0) + error("Unknown PWM instance"); + + if(pwm_apb_map_table[i].pwmoutApb == PWMOUT_ON_APB1) { + PclkFreq = HAL_RCC_GetPCLK1Freq(); + APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider; } else { - obj->prescaler = 1; +#if !defined(PWMOUT_APB2_NOT_SUPPORTED) + PclkFreq = HAL_RCC_GetPCLK2Freq(); + APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider; +#endif } + + /* By default use, 1us as SW pre-scaler */ + obj->prescaler = 1; // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx if (APBxCLKDivider == RCC_HCLK_DIV1) - TimHandle.Init.Prescaler = (uint16_t)(((PclkFreq) / 1000000) * obj->prescaler) - 1; // 1 us tick + TimHandle.Init.Prescaler = (((PclkFreq) / 1000000)) - 1; // 1 us tick else - TimHandle.Init.Prescaler = (uint16_t)(((PclkFreq * 2) / 1000000) * obj->prescaler) - 1; // 1 us tick + TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000)) - 1; // 1 us tick + TimHandle.Init.Period = (us - 1); - if (TimHandle.Init.Prescaler > 0xFFFF) - error("PWM: out of range prescaler"); - - TimHandle.Init.Period = (us - 1) / obj->prescaler; - if (TimHandle.Init.Period > 0xFFFF) - error("PWM: out of range period"); + /* In case period or pre-scalers are out of range, loop-in to get valid values */ + while ((TimHandle.Init.Period > 0xFFFF) || (TimHandle.Init.Period > 0xFFFF)) { + obj->prescaler = obj->prescaler * 2; + if (APBxCLKDivider == RCC_HCLK_DIV1) + TimHandle.Init.Prescaler = (((PclkFreq) / 1000000) * obj->prescaler) - 1; + else + TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000) * obj->prescaler) - 1; + TimHandle.Init.Period = (us - 1) / obj->prescaler; + } TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; From ae139d6c2262093fb2f20e0031b4d2c75cb6dec4 Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Mon, 27 Feb 2017 13:47:34 +0100 Subject: [PATCH 20/40] STM32: pwm period and prescaler calculation Correct the while loop limit and add a safe guard to avoid infinite loop. --- targets/TARGET_STM/pwmout_api.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/targets/TARGET_STM/pwmout_api.c b/targets/TARGET_STM/pwmout_api.c index f8a22e7035..aa01556073 100644 --- a/targets/TARGET_STM/pwmout_api.c +++ b/targets/TARGET_STM/pwmout_api.c @@ -251,13 +251,19 @@ void pwmout_period_us(pwmout_t* obj, int us) TimHandle.Init.Period = (us - 1); /* In case period or pre-scalers are out of range, loop-in to get valid values */ - while ((TimHandle.Init.Period > 0xFFFF) || (TimHandle.Init.Period > 0xFFFF)) { + while ((TimHandle.Init.Period > 0xFFFF) || (TimHandle.Init.Prescaler > 0xFFFF)) { obj->prescaler = obj->prescaler * 2; if (APBxCLKDivider == RCC_HCLK_DIV1) TimHandle.Init.Prescaler = (((PclkFreq) / 1000000) * obj->prescaler) - 1; else TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000) * obj->prescaler) - 1; TimHandle.Init.Period = (us - 1) / obj->prescaler; + /* Period decreases and prescaler increases over loops, so check for + * possible out of range cases */ + if ((TimHandle.Init.Period < 0xFFFF) && (TimHandle.Init.Prescaler > 0xFFFF)) { + error("Cannot initialize PWM\n"); + break; + } } TimHandle.Init.ClockDivision = 0; From 68a5f7af427a3f23873b937420dc0673762c9c08 Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Mon, 27 Feb 2017 13:48:55 +0100 Subject: [PATCH 21/40] STM32: move pwmout device tables to C file In order to avoid possible multiple definitions errors, move the table initialization to the C file instead of header file --- .../TARGET_STM/TARGET_STM32F0/pwmout_device.c | 62 +++++++++++++++ .../TARGET_STM/TARGET_STM32F0/pwmout_device.h | 26 +------ .../TARGET_STM/TARGET_STM32F1/pwmout_device.c | 53 +++++++++++++ .../TARGET_STM/TARGET_STM32F1/pwmout_device.h | 17 +--- .../TARGET_STM/TARGET_STM32F2/pwmout_device.c | 77 +++++++++++++++++++ .../TARGET_STM/TARGET_STM32F2/pwmout_device.h | 41 +--------- .../TARGET_STM/TARGET_STM32F3/pwmout_device.c | 68 ++++++++++++++++ .../TARGET_STM/TARGET_STM32F3/pwmout_device.h | 32 +------- .../TARGET_STM/TARGET_STM32F4/pwmout_device.c | 77 +++++++++++++++++++ .../TARGET_STM/TARGET_STM32F4/pwmout_device.h | 41 +--------- .../TARGET_STM/TARGET_STM32F7/pwmout_device.c | 77 +++++++++++++++++++ .../TARGET_STM/TARGET_STM32F7/pwmout_device.h | 41 +--------- .../TARGET_STM/TARGET_STM32L0/pwmout_device.c | 53 +++++++++++++ .../TARGET_STM/TARGET_STM32L0/pwmout_device.h | 17 +--- .../TARGET_STM/TARGET_STM32L1/pwmout_device.c | 62 +++++++++++++++ .../TARGET_STM/TARGET_STM32L1/pwmout_device.h | 26 +------ .../TARGET_STM/TARGET_STM32L4/pwmout_device.c | 68 ++++++++++++++++ .../TARGET_STM/TARGET_STM32L4/pwmout_device.h | 32 +------- 18 files changed, 606 insertions(+), 264 deletions(-) create mode 100644 targets/TARGET_STM/TARGET_STM32F0/pwmout_device.c create mode 100644 targets/TARGET_STM/TARGET_STM32F1/pwmout_device.c create mode 100644 targets/TARGET_STM/TARGET_STM32F2/pwmout_device.c create mode 100644 targets/TARGET_STM/TARGET_STM32F3/pwmout_device.c create mode 100644 targets/TARGET_STM/TARGET_STM32F4/pwmout_device.c create mode 100644 targets/TARGET_STM/TARGET_STM32F7/pwmout_device.c create mode 100644 targets/TARGET_STM/TARGET_STM32L0/pwmout_device.c create mode 100644 targets/TARGET_STM/TARGET_STM32L1/pwmout_device.c create mode 100644 targets/TARGET_STM/TARGET_STM32L4/pwmout_device.c diff --git a/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.c new file mode 100644 index 0000000000..369c7443ec --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.c @@ -0,0 +1,62 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM14_BASE) + {PWM_14, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB1}, +#endif +#if defined(TIM15_BASE) + {PWM_15, PWMOUT_ON_APB1}, +#endif +#if defined(TIM16_BASE) + {PWM_16, PWMOUT_ON_APB1}, +#endif +#if defined(TIM17_BASE) + {PWM_17, PWMOUT_ON_APB1}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h index 2862c30e6b..91837e6347 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h @@ -52,31 +52,7 @@ typedef struct pwm_apb_map { /* there is only 1 APB clock in F0 family */ #define PWMOUT_APB2_NOT_SUPPORTED -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM14_BASE) - {PWM_14, PWMOUT_ON_APB1}, -#endif -#if defined(TIM1_BASE) - {PWM_1, PWMOUT_ON_APB1}, -#endif -#if defined(TIM15_BASE) - {PWM_15, PWMOUT_ON_APB1}, -#endif -#if defined(TIM16_BASE) - {PWM_16, PWMOUT_ON_APB1}, -#endif -#if defined(TIM17_BASE) - {PWM_17, PWMOUT_ON_APB1}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.c new file mode 100644 index 0000000000..3331a660ca --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.c @@ -0,0 +1,53 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h index 9f105e7ec6..0d81dd547a 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h @@ -50,22 +50,7 @@ typedef struct pwm_apb_map { PwmoutApb pwmoutApb; } pwm_apb_map_t; -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM1_BASE) - {PWM_1, PWMOUT_ON_APB2}, -#endif -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM4_BASE) - {PWM_4, PWMOUT_ON_APB1}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.c new file mode 100644 index 0000000000..a1e131e998 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.c @@ -0,0 +1,77 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM12_BASE) + {PWM_12, PWMOUT_ON_APB1}, +#endif +#if defined(TIM13_BASE) + {PWM_13, PWMOUT_ON_APB1}, +#endif +#if defined(TIM14_BASE) + {PWM_14, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM9_BASE) + {PWM_9, PWMOUT_ON_APB2}, +#endif +#if defined(TIM10_BASE) + {PWM_10, PWMOUT_ON_APB2}, +#endif +#if defined(TIM11_BASE) + {PWM_11, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h index 480493e3a1..0d81dd547a 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h @@ -50,46 +50,7 @@ typedef struct pwm_apb_map { PwmoutApb pwmoutApb; } pwm_apb_map_t; -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM4_BASE) - {PWM_4, PWMOUT_ON_APB1}, -#endif -#if defined(TIM5_BASE) - {PWM_5, PWMOUT_ON_APB1}, -#endif -#if defined(TIM12_BASE) - {PWM_12, PWMOUT_ON_APB1}, -#endif -#if defined(TIM13_BASE) - {PWM_13, PWMOUT_ON_APB1}, -#endif -#if defined(TIM14_BASE) - {PWM_14, PWMOUT_ON_APB1}, -#endif -#if defined(TIM1_BASE) - {PWM_1, PWMOUT_ON_APB2}, -#endif -#if defined(TIM8_BASE) - {PWM_8, PWMOUT_ON_APB2}, -#endif -#if defined(TIM9_BASE) - {PWM_9, PWMOUT_ON_APB2}, -#endif -#if defined(TIM10_BASE) - {PWM_10, PWMOUT_ON_APB2}, -#endif -#if defined(TIM11_BASE) - {PWM_11, PWMOUT_ON_APB2}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.c new file mode 100644 index 0000000000..939331a3fd --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.c @@ -0,0 +1,68 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM15_BASE) + {PWM_15, PWMOUT_ON_APB2}, +#endif +#if defined(TIM16_BASE) + {PWM_16, PWMOUT_ON_APB2}, +#endif +#if defined(TIM17_BASE) + {PWM_17, PWMOUT_ON_APB2}, +#endif +#if defined(TIM20_BASE) + {PWM_20, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h index a0af9d6b01..0d81dd547a 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h @@ -50,37 +50,7 @@ typedef struct pwm_apb_map { PwmoutApb pwmoutApb; } pwm_apb_map_t; -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM4_BASE) - {PWM_4, PWMOUT_ON_APB1}, -#endif -#if defined(TIM1_BASE) - {PWM_1, PWMOUT_ON_APB2}, -#endif -#if defined(TIM8_BASE) - {PWM_8, PWMOUT_ON_APB2}, -#endif -#if defined(TIM15_BASE) - {PWM_15, PWMOUT_ON_APB2}, -#endif -#if defined(TIM16_BASE) - {PWM_16, PWMOUT_ON_APB2}, -#endif -#if defined(TIM17_BASE) - {PWM_17, PWMOUT_ON_APB2}, -#endif -#if defined(TIM20_BASE) - {PWM_20, PWMOUT_ON_APB2}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.c new file mode 100644 index 0000000000..a1e131e998 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.c @@ -0,0 +1,77 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM12_BASE) + {PWM_12, PWMOUT_ON_APB1}, +#endif +#if defined(TIM13_BASE) + {PWM_13, PWMOUT_ON_APB1}, +#endif +#if defined(TIM14_BASE) + {PWM_14, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM9_BASE) + {PWM_9, PWMOUT_ON_APB2}, +#endif +#if defined(TIM10_BASE) + {PWM_10, PWMOUT_ON_APB2}, +#endif +#if defined(TIM11_BASE) + {PWM_11, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h index 480493e3a1..0d81dd547a 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h @@ -50,46 +50,7 @@ typedef struct pwm_apb_map { PwmoutApb pwmoutApb; } pwm_apb_map_t; -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM4_BASE) - {PWM_4, PWMOUT_ON_APB1}, -#endif -#if defined(TIM5_BASE) - {PWM_5, PWMOUT_ON_APB1}, -#endif -#if defined(TIM12_BASE) - {PWM_12, PWMOUT_ON_APB1}, -#endif -#if defined(TIM13_BASE) - {PWM_13, PWMOUT_ON_APB1}, -#endif -#if defined(TIM14_BASE) - {PWM_14, PWMOUT_ON_APB1}, -#endif -#if defined(TIM1_BASE) - {PWM_1, PWMOUT_ON_APB2}, -#endif -#if defined(TIM8_BASE) - {PWM_8, PWMOUT_ON_APB2}, -#endif -#if defined(TIM9_BASE) - {PWM_9, PWMOUT_ON_APB2}, -#endif -#if defined(TIM10_BASE) - {PWM_10, PWMOUT_ON_APB2}, -#endif -#if defined(TIM11_BASE) - {PWM_11, PWMOUT_ON_APB2}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.c new file mode 100644 index 0000000000..a1e131e998 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.c @@ -0,0 +1,77 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM12_BASE) + {PWM_12, PWMOUT_ON_APB1}, +#endif +#if defined(TIM13_BASE) + {PWM_13, PWMOUT_ON_APB1}, +#endif +#if defined(TIM14_BASE) + {PWM_14, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM9_BASE) + {PWM_9, PWMOUT_ON_APB2}, +#endif +#if defined(TIM10_BASE) + {PWM_10, PWMOUT_ON_APB2}, +#endif +#if defined(TIM11_BASE) + {PWM_11, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h index 480493e3a1..0d81dd547a 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h @@ -50,46 +50,7 @@ typedef struct pwm_apb_map { PwmoutApb pwmoutApb; } pwm_apb_map_t; -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM4_BASE) - {PWM_4, PWMOUT_ON_APB1}, -#endif -#if defined(TIM5_BASE) - {PWM_5, PWMOUT_ON_APB1}, -#endif -#if defined(TIM12_BASE) - {PWM_12, PWMOUT_ON_APB1}, -#endif -#if defined(TIM13_BASE) - {PWM_13, PWMOUT_ON_APB1}, -#endif -#if defined(TIM14_BASE) - {PWM_14, PWMOUT_ON_APB1}, -#endif -#if defined(TIM1_BASE) - {PWM_1, PWMOUT_ON_APB2}, -#endif -#if defined(TIM8_BASE) - {PWM_8, PWMOUT_ON_APB2}, -#endif -#if defined(TIM9_BASE) - {PWM_9, PWMOUT_ON_APB2}, -#endif -#if defined(TIM10_BASE) - {PWM_10, PWMOUT_ON_APB2}, -#endif -#if defined(TIM11_BASE) - {PWM_11, PWMOUT_ON_APB2}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.c new file mode 100644 index 0000000000..c91d83f902 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.c @@ -0,0 +1,53 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM21_BASE) + {PWM_21, PWMOUT_ON_APB2}, +#endif +#if defined(TIM22_BASE) + {PWM_22, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h index 71f38e4f3f..bdb493818d 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h @@ -52,22 +52,7 @@ typedef struct pwm_apb_map { PwmoutApb pwmoutApb; } pwm_apb_map_t; -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM21_BASE) - {PWM_21, PWMOUT_ON_APB2}, -#endif -#if defined(TIM22_BASE) - {PWM_22, PWMOUT_ON_APB2}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.c new file mode 100644 index 0000000000..1b9580f2ea --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.c @@ -0,0 +1,62 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM9_BASE) + {PWM_9, PWMOUT_ON_APB2}, +#endif +#if defined(TIM10_BASE) + {PWM_10, PWMOUT_ON_APB2}, +#endif +#if defined(TIM11_BASE) + {PWM_11, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h index 50cc41cd9e..822156a709 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h @@ -53,31 +53,7 @@ typedef struct pwm_apb_map { PwmoutApb pwmoutApb; } pwm_apb_map_t; -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM4_BASE) - {PWM_4, PWMOUT_ON_APB1}, -#endif -#if defined(TIM5_BASE) - {PWM_5, PWMOUT_ON_APB1}, -#endif -#if defined(TIM9_BASE) - {PWM_9, PWMOUT_ON_APB2}, -#endif -#if defined(TIM10_BASE) - {PWM_10, PWMOUT_ON_APB2}, -#endif -#if defined(TIM11_BASE) - {PWM_11, PWMOUT_ON_APB2}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.c new file mode 100644 index 0000000000..708d30534a --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.c @@ -0,0 +1,68 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM15_BASE) + {PWM_15, PWMOUT_ON_APB2}, +#endif +#if defined(TIM16_BASE) + {PWM_16, PWMOUT_ON_APB2}, +#endif +#if defined(TIM17_BASE) + {PWM_17, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h index 382d6fb149..0d81dd547a 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h @@ -50,37 +50,7 @@ typedef struct pwm_apb_map { PwmoutApb pwmoutApb; } pwm_apb_map_t; -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM4_BASE) - {PWM_4, PWMOUT_ON_APB1}, -#endif -#if defined(TIM5_BASE) - {PWM_5, PWMOUT_ON_APB1}, -#endif -#if defined(TIM1_BASE) - {PWM_1, PWMOUT_ON_APB2}, -#endif -#if defined(TIM8_BASE) - {PWM_8, PWMOUT_ON_APB2}, -#endif -#if defined(TIM15_BASE) - {PWM_15, PWMOUT_ON_APB2}, -#endif -#if defined(TIM16_BASE) - {PWM_16, PWMOUT_ON_APB2}, -#endif -#if defined(TIM17_BASE) - {PWM_17, PWMOUT_ON_APB2}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT From 1268ed8dcb3b874dfa8efb4a7058f38ecf8681f5 Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Mon, 27 Feb 2017 13:58:33 +0100 Subject: [PATCH 22/40] STM32: fix formatting Use the recommended style if (condition) { do(); } else { do_else(); } --- targets/TARGET_STM/pwmout_api.c | 102 ++++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 30 deletions(-) diff --git a/targets/TARGET_STM/pwmout_api.c b/targets/TARGET_STM/pwmout_api.c index aa01556073..552fde0a79 100644 --- a/targets/TARGET_STM/pwmout_api.c +++ b/targets/TARGET_STM/pwmout_api.c @@ -53,64 +53,104 @@ void pwmout_init(pwmout_t* obj, PinName pin) // Enable TIM clock #if defined(TIM1_BASE) - if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE(); + if (obj->pwm == PWM_1){ + __HAL_RCC_TIM1_CLK_ENABLE(); + } #endif #if defined(TIM2_BASE) - if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE(); + if (obj->pwm == PWM_2) { + __HAL_RCC_TIM2_CLK_ENABLE(); + } #endif #if defined(TIM3_BASE) - if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE(); + if (obj->pwm == PWM_3) { + __HAL_RCC_TIM3_CLK_ENABLE(); + } #endif #if defined(TIM4_BASE) - if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE(); + if (obj->pwm == PWM_4) { + __HAL_RCC_TIM4_CLK_ENABLE(); + } #endif #if defined(TIM5_BASE) - if (obj->pwm == PWM_5) __HAL_RCC_TIM5_CLK_ENABLE(); + if (obj->pwm == PWM_5) { + __HAL_RCC_TIM5_CLK_ENABLE(); + } #endif #if defined(TIM8_BASE) - if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE(); + if (obj->pwm == PWM_8) { + __HAL_RCC_TIM8_CLK_ENABLE(); + } #endif #if defined(TIM9_BASE) - if (obj->pwm == PWM_9) __HAL_RCC_TIM9_CLK_ENABLE(); + if (obj->pwm == PWM_9) { + __HAL_RCC_TIM9_CLK_ENABLE(); + } #endif #if defined(TIM10_BASE) - if (obj->pwm == PWM_10) __HAL_RCC_TIM10_CLK_ENABLE(); + if (obj->pwm == PWM_10) { + __HAL_RCC_TIM10_CLK_ENABLE(); + } #endif #if defined(TIM11_BASE) - if (obj->pwm == PWM_11) __HAL_RCC_TIM11_CLK_ENABLE(); + if (obj->pwm == PWM_11) { + __HAL_RCC_TIM11_CLK_ENABLE(); + } #endif #if defined(TIM12_BASE) - if (obj->pwm == PWM_12) __HAL_RCC_TIM12_CLK_ENABLE(); + if (obj->pwm == PWM_12) { + __HAL_RCC_TIM12_CLK_ENABLE(); + } #endif #if defined(TIM13_BASE) - if (obj->pwm == PWM_13) __HAL_RCC_TIM13_CLK_ENABLE(); + if (obj->pwm == PWM_13) { + __HAL_RCC_TIM13_CLK_ENABLE(); + } #endif #if defined(TIM14_BASE) - if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE(); + if (obj->pwm == PWM_14) { + __HAL_RCC_TIM14_CLK_ENABLE(); + } #endif #if defined(TIM15_BASE) - if (obj->pwm == PWM_15) __HAL_RCC_TIM15_CLK_ENABLE(); + if (obj->pwm == PWM_15) { + __HAL_RCC_TIM15_CLK_ENABLE(); + } #endif #if defined(TIM16_BASE) - if (obj->pwm == PWM_16) __HAL_RCC_TIM16_CLK_ENABLE(); + if (obj->pwm == PWM_16) { + __HAL_RCC_TIM16_CLK_ENABLE(); + } #endif #if defined(TIM17_BASE) - if (obj->pwm == PWM_17) __HAL_RCC_TIM17_CLK_ENABLE(); + if (obj->pwm == PWM_17) { + __HAL_RCC_TIM17_CLK_ENABLE(); + } #endif #if defined(TIM18_BASE) - if (obj->pwm == PWM_18) __HAL_RCC_TIM18_CLK_ENABLE(); + if (obj->pwm == PWM_18) { + __HAL_RCC_TIM18_CLK_ENABLE(); + } #endif #if defined(TIM19_BASE) - if (obj->pwm == PWM_19) __HAL_RCC_TIM19_CLK_ENABLE(); + if (obj->pwm == PWM_19) { + __HAL_RCC_TIM19_CLK_ENABLE(); + } #endif #if defined(TIM20_BASE) - if (obj->pwm == PWM_20) __HAL_RCC_TIM20_CLK_ENABLE(); + if (obj->pwm == PWM_20) { + __HAL_RCC_TIM20_CLK_ENABLE(); + } #endif #if defined(TIM21_BASE) - if (obj->pwm == PWM_21) __HAL_RCC_TIM21_CLK_ENABLE(); + if (obj->pwm == PWM_21) { + __HAL_RCC_TIM21_CLK_ENABLE(); + } #endif #if defined(TIM22_BASE) - if (obj->pwm == PWM_22) __HAL_RCC_TIM22_CLK_ENABLE(); + if (obj->pwm == PWM_22) { + __HAL_RCC_TIM22_CLK_ENABLE(); + } #endif // Configure GPIO pinmap_pinout(pin, PinMap_PWM); @@ -244,20 +284,22 @@ void pwmout_period_us(pwmout_t* obj, int us) /* By default use, 1us as SW pre-scaler */ obj->prescaler = 1; // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx - if (APBxCLKDivider == RCC_HCLK_DIV1) - TimHandle.Init.Prescaler = (((PclkFreq) / 1000000)) - 1; // 1 us tick - else - TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000)) - 1; // 1 us tick - TimHandle.Init.Period = (us - 1); + if (APBxCLKDivider == RCC_HCLK_DIV1) { + TimHandle.Init.Prescaler = (((PclkFreq) / 1000000)) - 1; // 1 us tick + } else { + TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000)) - 1; // 1 us tick + } + TimHandle.Init.Period = (us - 1); /* In case period or pre-scalers are out of range, loop-in to get valid values */ while ((TimHandle.Init.Period > 0xFFFF) || (TimHandle.Init.Prescaler > 0xFFFF)) { obj->prescaler = obj->prescaler * 2; - if (APBxCLKDivider == RCC_HCLK_DIV1) - TimHandle.Init.Prescaler = (((PclkFreq) / 1000000) * obj->prescaler) - 1; - else - TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000) * obj->prescaler) - 1; - TimHandle.Init.Period = (us - 1) / obj->prescaler; + if (APBxCLKDivider == RCC_HCLK_DIV1) { + TimHandle.Init.Prescaler = (((PclkFreq) / 1000000) * obj->prescaler) - 1; + } else { + TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000) * obj->prescaler) - 1; + } + TimHandle.Init.Period = (us - 1) / obj->prescaler; /* Period decreases and prescaler increases over loops, so check for * possible out of range cases */ if ((TimHandle.Init.Period < 0xFFFF) && (TimHandle.Init.Prescaler > 0xFFFF)) { From 461aa66040328b00343fed770da42aa043eb8629 Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Tue, 7 Mar 2017 17:22:25 +0100 Subject: [PATCH 23/40] Fix XDOT compilation error Typo with misplaced closing parenthesis leads to compilation error, which is fixed with this patch --- .../TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c index 2219954bfe..4a8ab11503 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c @@ -85,7 +85,7 @@ const PinMap PinMap_PWM[] = { {PB_12, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 {PB_13, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 {PB_14, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 - {PB_15, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11), 1, 0}, // TIM11_CH1 + {PB_15, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 {NC, NC, 0} }; From 74695e74cf09a6c4058aa4901e73459e910372f8 Mon Sep 17 00:00:00 2001 From: jeromecoutant Date: Thu, 23 Feb 2017 09:38:41 +0100 Subject: [PATCH 24/40] STM32 CAN API: correct format and type astyle done --- targets/TARGET_STM/can_api.c | 42 ++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index e5f0a040d6..7a1ca7c58f 100644 --- a/targets/TARGET_STM/can_api.c +++ b/targets/TARGET_STM/can_api.c @@ -262,14 +262,14 @@ int can_read(can_t *obj, CAN_Message *msg, int handle) } /* Get the Id */ - msg->format = (CANFormat)((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR); + msg->format = (CANFormat)(((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR) >> 2); if (!msg->format) { msg->id = (uint32_t)0x000007FF & (can->sFIFOMailBox[handle].RIR >> 21); } else { msg->id = (uint32_t)0x1FFFFFFF & (can->sFIFOMailBox[handle].RIR >> 3); } - msg->type = (CANType)((uint8_t)0x02 & can->sFIFOMailBox[handle].RIR); + msg->type = (CANType)(((uint8_t)0x02 & can->sFIFOMailBox[handle].RIR) >> 1); /* Get the DLC */ msg->len = (uint8_t)0x0F & can->sFIFOMailBox[handle].RDTR; /* Get the FMI */ @@ -375,10 +375,10 @@ int can_mode(can_t *obj, CanMode mode) return success; } -int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) +int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) { int retval = 0; - + // filter for CANAny format cannot be configured for STM32 if ((format == CANStandard) || (format == CANExtended)) { CanHandle.Instance = (CAN_TypeDef *)(obj->can); @@ -398,11 +398,11 @@ int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t sFilterConfig.FilterMaskIdHigh = mask >> 13; sFilterConfig.FilterMaskIdLow = (0x00FF & (mask << 3)) | (1 << 2); } - + sFilterConfig.FilterFIFOAssignment = 0; sFilterConfig.FilterActivation = ENABLE; sFilterConfig.BankNumber = 14 + handle; - + HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig); retval = handle; } @@ -461,37 +461,47 @@ static void can_irq(CANName name, int id) } #if defined(TARGET_STM32F0) -void CAN_IRQHandler(void) { +void CAN_IRQHandler(void) +{ can_irq(CAN_1, 0); } #elif defined(TARGET_STM32F3) -void CAN_RX0_IRQHandler(void) { +void CAN_RX0_IRQHandler(void) +{ can_irq(CAN_1, 0); } -void CAN_TX_IRQHandler(void) { +void CAN_TX_IRQHandler(void) +{ can_irq(CAN_1, 0); } -void CAN_SCE_IRQHandler(void) { +void CAN_SCE_IRQHandler(void) +{ can_irq(CAN_1, 0); } #else -void CAN1_RX0_IRQHandler(void) { +void CAN1_RX0_IRQHandler(void) +{ can_irq(CAN_1, 0); } -void CAN1_TX_IRQHandler(void) { +void CAN1_TX_IRQHandler(void) +{ can_irq(CAN_1, 0); } -void CAN1_SCE_IRQHandler(void) { +void CAN1_SCE_IRQHandler(void) +{ can_irq(CAN_1, 0); } #if defined(CAN2_BASE) && (CAN_NUM == 2) -void CAN2_RX0_IRQHandler(void) { +void CAN2_RX0_IRQHandler(void) +{ can_irq(CAN_2, 1); } -void CAN2_TX_IRQHandler(void) { +void CAN2_TX_IRQHandler(void) +{ can_irq(CAN_2, 1); } -void CAN2_SCE_IRQHandler(void) { +void CAN2_SCE_IRQHandler(void) +{ can_irq(CAN_2, 1); } #endif // defined(CAN2_BASE) && (CAN_NUM == 2) From 2129b29bed8efa19c234c0f07c67e71f8e35d411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C5=82=C4=85bek=2C=20Andrzej?= Date: Mon, 27 Feb 2017 13:35:58 +0100 Subject: [PATCH 25/40] TARGET_NRF: corrected spi_init() to properly handle re-initialization on the same pins + minor editorial corrections. --- targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c | 28 +++++++++++++++------ 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c b/targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c index 985d24fcc6..ce917aa9c1 100644 --- a/targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c +++ b/targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c @@ -90,22 +90,22 @@ void SPI0_TWI0_IRQHandler(void); void SPI1_TWI1_IRQHandler(void); void SPIM2_SPIS2_SPI2_IRQHandler(void); -static const peripheral_handler_desc_t spi_hanlder_desc[SPI_COUNT] = { +static const peripheral_handler_desc_t spi_handler_desc[SPI_COUNT] = { #if SPI0_ENABLED { - SPIS0_IRQ, + SPI0_IRQ, (uint32_t) SPI0_TWI0_IRQHandler }, #endif #if SPI1_ENABLED { - SPIS1_IRQ, + SPI1_IRQ, (uint32_t) SPI1_TWI1_IRQHandler }, #endif #if SPI2_ENABLED { - SPIS2_IRQ, + SPI2_IRQ, (uint32_t) SPIM2_SPIS2_SPI2_IRQHandler }, #endif @@ -254,12 +254,24 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { int i; + for (i = 0; i < SPI_COUNT; ++i) { + spi_info_t *p_spi_info = &m_spi_info[i]; + if (p_spi_info->initialized && + p_spi_info->mosi_pin == (uint8_t)mosi && + p_spi_info->miso_pin == (uint8_t)miso && + p_spi_info->sck_pin == (uint8_t)sclk && + p_spi_info->ss_pin == (uint8_t)ssel) { + // Reuse the already allocated SPI instance (instead of allocating + // a new one), if it appears to be initialized with exactly the same + // pin assignments. + SPI_IDX(obj) = i; + return; + } + } + for (i = 0; i < SPI_COUNT; ++i) { spi_info_t *p_spi_info = &m_spi_info[i]; if (!p_spi_info->initialized) { - - NVIC_SetVector(spi_hanlder_desc[i].IRQn, spi_hanlder_desc[i].vector); - p_spi_info->sck_pin = (uint8_t)sclk; p_spi_info->mosi_pin = (mosi != NC) ? (uint8_t)mosi : NRF_DRV_SPI_PIN_NOT_USED; @@ -270,6 +282,8 @@ void spi_init(spi_t *obj, p_spi_info->spi_mode = (uint8_t)NRF_DRV_SPI_MODE_0; p_spi_info->frequency = NRF_DRV_SPI_FREQ_1M; + NVIC_SetVector(spi_handler_desc[i].IRQn, spi_handler_desc[i].vector); + // By default each SPI instance is initialized to work as a master. // Should the slave mode be used, the instance will be reconfigured // appropriately in 'spi_format'. From 4257bfe7742fcbeeb0fc32a7c921f601b8233d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C5=82=C4=85bek=2C=20Andrzej?= Date: Thu, 2 Mar 2017 13:47:37 +0100 Subject: [PATCH 26/40] TARGET_NRF: added a description of the recent change in spi_init(). --- targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c b/targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c index ce917aa9c1..2c07af5e8b 100644 --- a/targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c +++ b/targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c @@ -254,6 +254,11 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { int i; + + // This block is only a workaround that allows to create SPI object several + // times, what would be otherwise impossible in the current implementation + // of mbed driver that does not call spi_free() from SPI destructor. + // Once this mbed's imperfection is corrected, this block should be removed. for (i = 0; i < SPI_COUNT; ++i) { spi_info_t *p_spi_info = &m_spi_info[i]; if (p_spi_info->initialized && From 404b58c4dffdb2a61d6df485f4099219cba3f0e7 Mon Sep 17 00:00:00 2001 From: bcostm Date: Mon, 27 Feb 2017 14:08:19 +0100 Subject: [PATCH 27/40] STM32L4xx: set APB2 clock to 80MHz (instead of 40MHz) --- .../TARGET_STM32L476xG/TARGET_DISCO_L476VG/system_stm32l4xx.c | 2 +- .../TARGET_STM32L476xG/TARGET_NUCLEO_L476RG/system_stm32l4xx.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_DISCO_L476VG/system_stm32l4xx.c b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_DISCO_L476VG/system_stm32l4xx.c index 79dd4f9ef6..45359dd009 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_DISCO_L476VG/system_stm32l4xx.c +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_DISCO_L476VG/system_stm32l4xx.c @@ -569,7 +569,7 @@ uint8_t SetSysClock_PLL_MSI(void) RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; /* 80 MHz */ RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; /* 80 MHz */ RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; /* 80 MHz */ - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; /* 40 MHz */ + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; /* 80 MHz */ if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) { return 0; // FAIL diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_NUCLEO_L476RG/system_stm32l4xx.c b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_NUCLEO_L476RG/system_stm32l4xx.c index 79dd4f9ef6..45359dd009 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_NUCLEO_L476RG/system_stm32l4xx.c +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_NUCLEO_L476RG/system_stm32l4xx.c @@ -569,7 +569,7 @@ uint8_t SetSysClock_PLL_MSI(void) RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; /* 80 MHz */ RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; /* 80 MHz */ RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; /* 80 MHz */ - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; /* 40 MHz */ + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; /* 80 MHz */ if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) { return 0; // FAIL From a48647f4f1c3e438776e8b4f047dd75bf0e1e5a8 Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Tue, 28 Feb 2017 14:04:54 -0600 Subject: [PATCH 28/40] Ignore build directory from scan resources This is a bug fix for the following bug (Github issue #437): If two builds were run specifying a non-default build folder, the second build would fail to link with duplicate symbols and may not fit on the device. The root of this problem is that these non-default build folders are not ignored by scan-resources, and therefore included in the build. We fix this bug by ignoring the build directory passed into the tools. --- tools/build_api.py | 37 +++++++++++++------------- tools/project_api.py | 8 +++--- tools/test/build_api/build_api_test.py | 9 +++---- tools/toolchains/__init__.py | 15 ++++++----- tools/toolchains/arm.py | 4 ++- tools/toolchains/gcc.py | 5 ++-- tools/toolchains/iar.py | 4 ++- 7 files changed, 44 insertions(+), 38 deletions(-) diff --git a/tools/build_api.py b/tools/build_api.py index 0c5202188b..adfd9c870a 100644 --- a/tools/build_api.py +++ b/tools/build_api.py @@ -121,7 +121,7 @@ def get_config(src_paths, target, toolchain_name): src_paths = [src_paths] # Pass all params to the unified prepare_resources() - toolchain = prepare_toolchain(src_paths, target, toolchain_name) + toolchain = prepare_toolchain(src_paths, None, target, toolchain_name) # Scan src_path for config files resources = toolchain.scan_resources(src_paths[0]) @@ -299,7 +299,7 @@ def add_regions_to_profile(profile, config, toolchain_class): % (region.name, region.size, region.start)) -def prepare_toolchain(src_paths, target, toolchain_name, +def prepare_toolchain(src_paths, build_dir, target, toolchain_name, macros=None, clean=False, jobs=1, notify=None, silent=False, verbose=False, extra_verbose=False, config=None, @@ -339,7 +339,7 @@ def prepare_toolchain(src_paths, target, toolchain_name, add_regions_to_profile(build_profile, config, cur_tc) # Toolchain instance - toolchain = cur_tc(target, notify, macros, silent, + toolchain = cur_tc(target, notify, macros, silent, build_dir=build_dir, extra_verbose=extra_verbose, build_profile=build_profile) toolchain.config = config @@ -475,8 +475,8 @@ def build_project(src_paths, build_path, target, toolchain_name, # Pass all params to the unified prepare_toolchain() toolchain = prepare_toolchain( - src_paths, target, toolchain_name, macros=macros, clean=clean, - jobs=jobs, notify=notify, silent=silent, verbose=verbose, + src_paths, build_path, target, toolchain_name, macros=macros, + clean=clean, jobs=jobs, notify=notify, silent=silent, verbose=verbose, extra_verbose=extra_verbose, config=config, app_config=app_config, build_profile=build_profile) @@ -509,8 +509,7 @@ def build_project(src_paths, build_path, target, toolchain_name, resources.linker_script = linker_script # Compile Sources - objects = toolchain.compile_sources(resources, build_path, - resources.inc_dirs) + objects = toolchain.compile_sources(resources, resources.inc_dirs) resources.objects.extend(objects) # Link Program @@ -629,9 +628,9 @@ def build_library(src_paths, build_path, target, toolchain_name, # Pass all params to the unified prepare_toolchain() toolchain = prepare_toolchain( - src_paths, target, toolchain_name, macros=macros, clean=clean, - jobs=jobs, notify=notify, silent=silent, verbose=verbose, - extra_verbose=extra_verbose, app_config=app_config, + src_paths, build_path, target, toolchain_name, macros=macros, + clean=clean, jobs=jobs, notify=notify, silent=silent, + verbose=verbose, extra_verbose=extra_verbose, app_config=app_config, build_profile=build_profile) # The first path will give the name to the library @@ -687,8 +686,7 @@ def build_library(src_paths, build_path, target, toolchain_name, resources=resources) # Compile Sources - objects = toolchain.compile_sources(resources, abspath(tmp_path), - resources.inc_dirs) + objects = toolchain.compile_sources(resources, resources.inc_dirs) resources.objects.extend(objects) if archive: @@ -815,6 +813,7 @@ def build_lib(lib_id, target, toolchain_name, verbose=False, toolchain.VERBOSE = verbose toolchain.jobs = jobs toolchain.build_all = clean + toolchain.build_dir = build_path toolchain.info("Building library %s (%s, %s)" % (name.upper(), target.name, toolchain_name)) @@ -869,8 +868,7 @@ def build_lib(lib_id, target, toolchain_name, verbose=False, # Compile Sources objects = [] for resource in resources: - objects.extend(toolchain.compile_sources(resource, tmp_path, - dependencies_include_dir)) + objects.extend(toolchain.compile_sources(resource, dependencies_include_dir)) needed_update = toolchain.build_library(objects, bin_path, name) @@ -962,6 +960,11 @@ def build_mbed_libs(target, toolchain_name, verbose=False, toolchain.jobs = jobs toolchain.build_all = clean + tmp_path = join(MBED_LIBRARIES, '.temp', toolchain.obj_path) + mkdir(tmp_path) + + toolchain.build_dir = tmp_path + # Take into account the library configuration (MBED_CONFIG_FILE) config = Config(target) toolchain.config = config @@ -973,8 +976,6 @@ def build_mbed_libs(target, toolchain_name, verbose=False, build_toolchain = join(build_target, "TOOLCHAIN_" + toolchain.name) mkdir(build_toolchain) - tmp_path = join(MBED_LIBRARIES, '.temp', toolchain.obj_path) - mkdir(tmp_path) # CMSIS toolchain.info("Building library %s (%s, %s)" % @@ -1015,7 +1016,7 @@ def build_mbed_libs(target, toolchain_name, verbose=False, toolchain.copy_files(hal_implementation.linker_script, build_toolchain) toolchain.copy_files(hal_implementation.bin_files, build_toolchain) incdirs = toolchain.scan_resources(build_target).inc_dirs - objects = toolchain.compile_sources(hal_implementation, tmp_path, + objects = toolchain.compile_sources(hal_implementation, library_incdirs + incdirs) toolchain.copy_files(objects, build_toolchain) @@ -1024,7 +1025,7 @@ def build_mbed_libs(target, toolchain_name, verbose=False, for dir in [MBED_DRIVERS, MBED_PLATFORM, MBED_HAL]: mbed_resources += toolchain.scan_resources(dir) - objects = toolchain.compile_sources(mbed_resources, tmp_path, + objects = toolchain.compile_sources(mbed_resources, library_incdirs + incdirs) # A number of compiled files need to be copied as objects as opposed to diff --git a/tools/project_api.py b/tools/project_api.py index f55f57ebf0..728f61ee4f 100644 --- a/tools/project_api.py +++ b/tools/project_api.py @@ -186,10 +186,10 @@ def export_project(src_paths, export_path, target, ide, libraries_paths=None, _, toolchain_name = get_exporter_toolchain(ide) # Pass all params to the unified prepare_resources() - toolchain = prepare_toolchain(paths, target, toolchain_name, macros=macros, - jobs=jobs, notify=notify, silent=silent, - verbose=verbose, extra_verbose=extra_verbose, - config=config, build_profile=build_profile) + toolchain = prepare_toolchain( + paths, export_path, target, toolchain_name, macros=macros, jobs=jobs, + notify=notify, silent=silent, verbose=verbose, + extra_verbose=extra_verbose, config=config, build_profile=build_profile) # The first path will give the name to the library if name is None: name = basename(normpath(abspath(src_paths[0]))) diff --git a/tools/test/build_api/build_api_test.py b/tools/test/build_api/build_api_test.py index 019544546b..1220edcad5 100644 --- a/tools/test/build_api/build_api_test.py +++ b/tools/test/build_api/build_api_test.py @@ -58,7 +58,7 @@ class BuildApiTests(unittest.TestCase): @patch('tools.utils.run_cmd', return_value=("", "", 0)) def test_always_complete_build(self, *_): with MagicMock() as notify: - toolchain = prepare_toolchain(self.src_paths, self.target, + toolchain = prepare_toolchain(self.src_paths, self.build_path, self.target, self.toolchain_name, notify=notify) res = scan_resources(self.src_paths, toolchain) @@ -66,9 +66,8 @@ class BuildApiTests(unittest.TestCase): toolchain.RESPONSE_FILES=False toolchain.config_processed = True toolchain.config_file = "junk" - toolchain.compile_sources(res, self.build_path) + toolchain.compile_sources(res) - print notify.mock_calls assert any('percent' in msg[0] and msg[0]['percent'] == 100.0 for _, msg, _ in notify.mock_calls if msg) @@ -90,7 +89,7 @@ class BuildApiTests(unittest.TestCase): mock_target, False) - prepare_toolchain(self.src_paths, self.target, self.toolchain_name, + prepare_toolchain(self.src_paths, None, self.target, self.toolchain_name, app_config=app_config) mock_config_init.assert_called_once_with(self.target, self.src_paths, @@ -112,7 +111,7 @@ class BuildApiTests(unittest.TestCase): mock_target, False) - prepare_toolchain(self.src_paths, self.target, self.toolchain_name) + prepare_toolchain(self.src_paths, None, self.target, self.toolchain_name) mock_config_init.assert_called_once_with(self.target, self.src_paths, app_config=None) diff --git a/tools/toolchains/__init__.py b/tools/toolchains/__init__.py index ffce23e65d..16b1ae50bc 100644 --- a/tools/toolchains/__init__.py +++ b/tools/toolchains/__init__.py @@ -256,7 +256,8 @@ class mbedToolchain: profile_template = {'common':[], 'c':[], 'cxx':[], 'asm':[], 'ld':[]} - def __init__(self, target, notify=None, macros=None, silent=False, extra_verbose=False, build_profile=None): + def __init__(self, target, notify=None, macros=None, silent=False, + extra_verbose=False, build_profile=None, build_dir=None): self.target = target self.name = self.__class__.__name__ @@ -295,7 +296,7 @@ class mbedToolchain: self.build_all = False # Build output dir - self.build_dir = None + self.build_dir = build_dir self.timestamp = time() # Output build naming based on target+toolchain combo (mbed 2.0 builds) @@ -580,7 +581,8 @@ class mbedToolchain: self.add_ignore_patterns(root, base_path, lines) # Skip the whole folder if ignored, e.g. .mbedignore containing '*' - if self.is_ignored(join(relpath(root, base_path),"")): + if (self.is_ignored(join(relpath(root, base_path),"")) or + self.build_dir == join(relpath(root, base_path))): dirs[:] = [] continue @@ -773,7 +775,7 @@ class mbedToolchain: # THIS METHOD IS BEING CALLED BY THE MBED ONLINE BUILD SYSTEM # ANY CHANGE OF PARAMETERS OR RETURN VALUES WILL BREAK COMPATIBILITY - def compile_sources(self, resources, build_path, inc_dirs=None): + def compile_sources(self, resources, inc_dirs=None): # Web IDE progress bar for project build files_to_compile = resources.s_sources + resources.c_sources + resources.cpp_sources self.to_be_compiled = len(files_to_compile) @@ -790,8 +792,6 @@ class mbedToolchain: inc_paths = sorted(set(inc_paths)) # Unique id of all include paths self.inc_md5 = md5(' '.join(inc_paths)).hexdigest() - # Where to store response files - self.build_dir = build_path objects = [] queue = [] @@ -804,7 +804,8 @@ class mbedToolchain: # Sort compile queue for consistency files_to_compile.sort() for source in files_to_compile: - object = self.relative_object_path(build_path, resources.file_basepath[source], source) + object = self.relative_object_path( + self.build_dir, resources.file_basepath[source], source) # Queue mode (multiprocessing) commands = self.compile_command(source, object, inc_paths) diff --git a/tools/toolchains/arm.py b/tools/toolchains/arm.py index 29255034d0..9fc0146e11 100644 --- a/tools/toolchains/arm.py +++ b/tools/toolchains/arm.py @@ -40,8 +40,10 @@ class ARM(mbedToolchain): return mbedToolchain.generic_check_executable("ARM", 'armcc', 2, 'bin') def __init__(self, target, notify=None, macros=None, - silent=False, extra_verbose=False, build_profile=None): + silent=False, extra_verbose=False, build_profile=None, + build_dir=None): mbedToolchain.__init__(self, target, notify, macros, silent, + build_dir=build_dir, extra_verbose=extra_verbose, build_profile=build_profile) diff --git a/tools/toolchains/gcc.py b/tools/toolchains/gcc.py index 46d9f1a23d..1211d2aa27 100644 --- a/tools/toolchains/gcc.py +++ b/tools/toolchains/gcc.py @@ -29,10 +29,11 @@ class GCC(mbedToolchain): INDEX_PATTERN = re.compile('(?P\s*)\^') def __init__(self, target, notify=None, macros=None, - silent=False, extra_verbose=False, build_profile=None): + silent=False, extra_verbose=False, build_profile=None, + build_dir=None): mbedToolchain.__init__(self, target, notify, macros, silent, extra_verbose=extra_verbose, - build_profile=build_profile) + build_profile=build_profile, build_dir=build_dir) tool_path=TOOLCHAIN_PATHS['GCC_ARM'] # Add flags for current size setting diff --git a/tools/toolchains/iar.py b/tools/toolchains/iar.py index 8b61c3af3d..f14eed231c 100644 --- a/tools/toolchains/iar.py +++ b/tools/toolchains/iar.py @@ -37,8 +37,10 @@ class IAR(mbedToolchain): return mbedToolchain.generic_check_executable("IAR", 'iccarm', 2, "bin") def __init__(self, target, notify=None, macros=None, - silent=False, extra_verbose=False, build_profile=None): + silent=False, extra_verbose=False, build_profile=None, + build_dir=None): mbedToolchain.__init__(self, target, notify, macros, silent, + build_dir=build_dir, extra_verbose=extra_verbose, build_profile=build_profile) if target.core == "Cortex-M7F" or target.core == "Cortex-M7FD": From 0b15a05a54a761966ed14cd6087d057a6585b557 Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Mon, 6 Mar 2017 18:23:16 -0600 Subject: [PATCH 29/40] Re-factor mbed2 lib builds to use prepare_toolchain The prior patch in this series makes the assumption that any building will go through `build_api.prepare_toolchain`. This was not a valid assumption for the mbed2 build process. So, instead of maintaining 2 ways of using the toolchain classes, I elected to unify on `prepare_toolchain`. --- tools/build_api.py | 63 +++++++++++++++++++++------------------------- tools/make.py | 2 +- 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/tools/build_api.py b/tools/build_api.py index adfd9c870a..c4aaf85aac 100644 --- a/tools/build_api.py +++ b/tools/build_api.py @@ -731,6 +731,9 @@ def build_library(src_paths, build_path, target, toolchain_name, ### Legacy methods ### ###################### +def mbed2_obj_path(target_name, toolchain_name): + return join("TARGET_" + target_name, "TOOLCHAIN_" + toolchain_name) + def build_lib(lib_id, target, toolchain_name, verbose=False, clean=False, macros=None, notify=None, jobs=1, silent=False, report=None, properties=None, extra_verbose=False, @@ -807,20 +810,23 @@ def build_lib(lib_id, target, toolchain_name, verbose=False, try: # Toolchain instance - toolchain = TOOLCHAIN_CLASSES[toolchain_name]( - target, macros=macros, notify=notify, silent=silent, - extra_verbose=extra_verbose, build_profile=build_profile) - toolchain.VERBOSE = verbose - toolchain.jobs = jobs - toolchain.build_all = clean - toolchain.build_dir = build_path + # Create the desired build directory structure + bin_path = join(build_path, mbed2_obj_path(target.name, toolchain_name)) + mkdir(bin_path) + tmp_path = join(build_path, '.temp', mbed2_obj_path(target.name, + toolchain_name)) + mkdir(tmp_path) + + toolchain = prepare_toolchain( + src_paths, tmp_path, target, toolchain_name, macros=macros, + notify=notify, silent=silent, extra_verbose=extra_verbose, + build_profile=build_profile, jobs=jobs, clean=clean) toolchain.info("Building library %s (%s, %s)" % (name.upper(), target.name, toolchain_name)) # Take into account the library configuration (MBED_CONFIG_FILE) - config = Config(target) - toolchain.config = config + config = toolchain.config config.add_config_files([MBED_CONFIG_FILE]) # Scan Resources @@ -851,11 +857,6 @@ def build_lib(lib_id, target, toolchain_name, verbose=False, config.load_resources(res) toolchain.set_config_data(toolchain.config.get_config_data()) - # Create the desired build directory structure - bin_path = join(build_path, toolchain.obj_path) - mkdir(bin_path) - tmp_path = join(build_path, '.temp', toolchain.obj_path) - mkdir(tmp_path) # Copy Headers for resource in resources: @@ -952,30 +953,24 @@ def build_mbed_libs(target, toolchain_name, verbose=False, return False try: - # Toolchain - toolchain = TOOLCHAIN_CLASSES[toolchain_name]( - target, macros=macros, notify=notify, silent=silent, - extra_verbose=extra_verbose, build_profile=build_profile) - toolchain.VERBOSE = verbose - toolchain.jobs = jobs - toolchain.build_all = clean - - tmp_path = join(MBED_LIBRARIES, '.temp', toolchain.obj_path) - mkdir(tmp_path) - - toolchain.build_dir = tmp_path - - # Take into account the library configuration (MBED_CONFIG_FILE) - config = Config(target) - toolchain.config = config - config.add_config_files([MBED_CONFIG_FILE]) - toolchain.set_config_data(toolchain.config.get_config_data()) - # Source and Build Paths build_target = join(MBED_LIBRARIES, "TARGET_" + target.name) - build_toolchain = join(build_target, "TOOLCHAIN_" + toolchain.name) + build_toolchain = join(build_target, "TOOLCHAIN_" + toolchain_name) mkdir(build_toolchain) + # Toolchain + tmp_path = join(MBED_LIBRARIES, '.temp', mbed2_obj_path(target.name, toolchain_name)) + mkdir(tmp_path) + + toolchain = prepare_toolchain( + [""], tmp_path, target, toolchain_name, macros=macros, + notify=notify, silent=silent, extra_verbose=extra_verbose, + build_profile=build_profile, jobs=jobs, clean=clean) + + # Take into account the library configuration (MBED_CONFIG_FILE) + config = toolchain.config + config.add_config_files([MBED_CONFIG_FILE]) + toolchain.set_config_data(toolchain.config.get_config_data()) # CMSIS toolchain.info("Building library %s (%s, %s)" % diff --git a/tools/make.py b/tools/make.py index e4a6d5fca0..bfcb68e469 100644 --- a/tools/make.py +++ b/tools/make.py @@ -269,7 +269,7 @@ if __name__ == '__main__': try: bin_file = build_project(test.source_dir, build_dir, mcu, toolchain, - test.dependencies, + set(test.dependencies), linker_script=options.linker_script, clean=options.clean, verbose=options.verbose, From ed60ef142b760c4e26eea4afe469cd2d5006a94f Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Mon, 6 Mar 2017 18:29:46 -0600 Subject: [PATCH 30/40] Remove static analysis scan An earlier patch in this series relies on the assumption that all toolchain construction goes through `prepare_toolchain`. This is still not the case. The only remaining user of the `mbedToolchain` object that does not go through `prepare_toolchain` is the static analysis scanner. It's basically dead-code at this point. I say we remove it. So this patch removes it. --- tools/build.py | 115 +++++---------- tools/build_api.py | 277 ----------------------------------- tools/toolchains/__init__.py | 3 - 3 files changed, 40 insertions(+), 355 deletions(-) diff --git a/tools/build.py b/tools/build.py index e05120293f..999ebb3700 100644 --- a/tools/build.py +++ b/tools/build.py @@ -34,7 +34,6 @@ from tools.options import get_default_options_parser from tools.options import extract_profile from tools.build_api import build_library, build_mbed_libs, build_lib from tools.build_api import mcu_toolchain_matrix -from tools.build_api import static_analysis_scan, static_analysis_scan_lib, static_analysis_scan_library from tools.build_api import print_build_results from tools.settings import CPPCHECK_CMD, CPPCHECK_MSG_FORMAT from utils import argparse_filestring_type, args_error @@ -120,12 +119,6 @@ if __name__ == '__main__': default=None, help='For some commands you can use filter to filter out results') - parser.add_argument("--cppcheck", - action="store_true", - dest="cppcheck_validation", - default=False, - help="Forces 'cppcheck' static code analysis") - parser.add_argument("-j", "--jobs", type=int, dest="jobs", default=0, help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)") parser.add_argument("-N", "--artifact-name", dest="artifact_name", @@ -212,84 +205,56 @@ if __name__ == '__main__': for toolchain in toolchains: for target in targets: - # CPPCHECK code validation - if options.cppcheck_validation: + tt_id = "%s::%s" % (toolchain, target) + if toolchain not in TARGET_MAP[target].supported_toolchains: + # Log this later + print "%s skipped: toolchain not supported" % tt_id + skipped.append(tt_id) + else: try: mcu = TARGET_MAP[target] - # CMSIS and MBED libs analysis profile = extract_profile(parser, options, toolchain) - static_analysis_scan( - mcu, toolchain, CPPCHECK_CMD, CPPCHECK_MSG_FORMAT, - verbose=options.verbose, jobs=options.jobs, - build_profile=profile) + if options.source_dir: + lib_build_res = build_library(options.source_dir, options.build_dir, mcu, toolchain, + extra_verbose=options.extra_verbose_notify, + verbose=options.verbose, + silent=options.silent, + jobs=options.jobs, + clean=options.clean, + archive=(not options.no_archive), + macros=options.macros, + name=options.artifact_name, + build_profile=profile) + else: + lib_build_res = build_mbed_libs(mcu, toolchain, + extra_verbose=options.extra_verbose_notify, + verbose=options.verbose, + silent=options.silent, + jobs=options.jobs, + clean=options.clean, + macros=options.macros, + build_profile=profile) + for lib_id in libraries: - # Static check for library - static_analysis_scan_lib( - lib_id, mcu, toolchain, CPPCHECK_CMD, - CPPCHECK_MSG_FORMAT, - extra_verbose=options.extra_verbose_notify, - verbose=options.verbose, jobs=options.jobs, - clean=options.clean, macros=options.macros, - build_profile=profile) - pass + build_lib(lib_id, mcu, toolchain, + extra_verbose=options.extra_verbose_notify, + verbose=options.verbose, + silent=options.silent, + clean=options.clean, + macros=options.macros, + jobs=options.jobs, + build_profile=profile) + if lib_build_res: + successes.append(tt_id) + else: + skipped.append(tt_id) except Exception, e: if options.verbose: import traceback traceback.print_exc(file=sys.stdout) sys.exit(1) + failures.append(tt_id) print e - else: - # Build - tt_id = "%s::%s" % (toolchain, target) - if toolchain not in TARGET_MAP[target].supported_toolchains: - # Log this later - print "%s skipped: toolchain not supported" % tt_id - skipped.append(tt_id) - else: - try: - mcu = TARGET_MAP[target] - profile = extract_profile(parser, options, toolchain) - if options.source_dir: - lib_build_res = build_library(options.source_dir, options.build_dir, mcu, toolchain, - extra_verbose=options.extra_verbose_notify, - verbose=options.verbose, - silent=options.silent, - jobs=options.jobs, - clean=options.clean, - archive=(not options.no_archive), - macros=options.macros, - name=options.artifact_name, - build_profile=profile) - else: - lib_build_res = build_mbed_libs(mcu, toolchain, - extra_verbose=options.extra_verbose_notify, - verbose=options.verbose, - silent=options.silent, - jobs=options.jobs, - clean=options.clean, - macros=options.macros, - build_profile=profile) - - for lib_id in libraries: - build_lib(lib_id, mcu, toolchain, - extra_verbose=options.extra_verbose_notify, - verbose=options.verbose, - silent=options.silent, - clean=options.clean, - macros=options.macros, - jobs=options.jobs, - build_profile=profile) - if lib_build_res: - successes.append(tt_id) - else: - skipped.append(tt_id) - except Exception, e: - if options.verbose: - import traceback - traceback.print_exc(file=sys.stdout) - sys.exit(1) - failures.append(tt_id) - print e # Write summary of the builds diff --git a/tools/build_api.py b/tools/build_api.py index c4aaf85aac..36d944bfcc 100644 --- a/tools/build_api.py +++ b/tools/build_api.py @@ -1199,283 +1199,6 @@ def get_target_supported_toolchains(target): else None -def static_analysis_scan(target, toolchain_name, cppcheck_cmd, - cppcheck_msg_format, verbose=False, - clean=False, macros=None, notify=None, jobs=1, - extra_verbose=False, build_profile=None): - """Perform static analysis on a target and toolchain combination - - Positional arguments: - target - the target to fake the build for - toolchain_name - pretend you would compile with this toolchain - cppcheck_cmd - the command used to do static analysis - cppcheck_msg_format - the format of the check messages - - Keyword arguments: - verbose - more printing! - clean - start from a clean slate - macros - extra macros to compile with - notify - the notification event handling function - jobs - number of commands to run at once - extra_verbose - even moar printing - build_profile - a dict of flags that will be passed to the compiler - """ - # Toolchain - toolchain = TOOLCHAIN_CLASSES[toolchain_name](target, macros=macros, - notify=notify, - extra_verbose=extra_verbose, - build_profile=build_profile) - toolchain.VERBOSE = verbose - toolchain.jobs = jobs - toolchain.build_all = clean - - # Source and Build Paths - build_target = join(MBED_LIBRARIES, "TARGET_" + target.name) - build_toolchain = join(build_target, "TOOLCHAIN_" + toolchain.name) - mkdir(build_toolchain) - - tmp_path = join(MBED_LIBRARIES, '.temp', toolchain.obj_path) - mkdir(tmp_path) - - # CMSIS - toolchain.info("Static analysis for %s (%s, %s)" % - ('CMSIS', target.name, toolchain_name)) - cmsis_src = MBED_CMSIS_PATH - resources = toolchain.scan_resources(cmsis_src) - - # Copy files before analysis - toolchain.copy_files(resources.headers, build_target) - toolchain.copy_files(resources.linker_script, build_toolchain) - - # Gather include paths, c, cpp sources and macros to transfer to cppcheck - # command line - includes = ["-I%s"% i for i in resources.inc_dirs] - includes.append("-I%s"% str(build_target)) - c_sources = " ".join(resources.c_sources) - cpp_sources = " ".join(resources.cpp_sources) - macros = ["-D%s"% s for s in toolchain.get_symbols() + toolchain.macros] - - includes = [inc.strip() for inc in includes] - macros = [mac.strip() for mac in macros] - - check_cmd = cppcheck_cmd - check_cmd += cppcheck_msg_format - check_cmd += includes - check_cmd += macros - - # We need to pass some params via file to avoid "command line too long in - # some OSs" - tmp_file = tempfile.NamedTemporaryFile(delete=False) - tmp_file.writelines(line + '\n' for line in c_sources.split()) - tmp_file.writelines(line + '\n' for line in cpp_sources.split()) - tmp_file.close() - check_cmd += ["--file-list=%s"% tmp_file.name] - - _stdout, _stderr, _ = run_cmd(check_cmd) - if verbose: - print _stdout - print _stderr - - # ========================================================================= - - # MBED - toolchain.info("Static analysis for %s (%s, %s)" % - ('MBED', target.name, toolchain_name)) - - # Common Headers - toolchain.copy_files([MBED_HEADER], MBED_LIBRARIES) - toolchain.copy_files(toolchain.scan_resources(MBED_DRIVERS).headers, - MBED_LIBRARIES) - toolchain.copy_files(toolchain.scan_resources(MBED_PLATFORM).headers, - MBED_LIBRARIES) - toolchain.copy_files(toolchain.scan_resources(MBED_HAL).headers, - MBED_LIBRARIES) - - # Target specific sources - hal_src = join(MBED_TARGETS_PATH, "hal") - hal_implementation = toolchain.scan_resources(hal_src) - - # Copy files before analysis - toolchain.copy_files(hal_implementation.headers + - hal_implementation.hex_files, build_target, - resources=hal_implementation) - incdirs = toolchain.scan_resources(build_target) - - target_includes = ["-I%s" % i for i in incdirs.inc_dirs] - target_includes.append("-I%s"% str(build_target)) - target_includes.append("-I%s"% str(hal_src)) - target_c_sources = " ".join(incdirs.c_sources) - target_cpp_sources = " ".join(incdirs.cpp_sources) - target_macros = ["-D%s"% s for s in - toolchain.get_symbols() + toolchain.macros] - - # Common Sources - mbed_resources = toolchain.scan_resources(MBED_COMMON) - - # Gather include paths, c, cpp sources and macros to transfer to cppcheck - # command line - mbed_includes = ["-I%s" % i for i in mbed_resources.inc_dirs] - mbed_includes.append("-I%s"% str(build_target)) - mbed_includes.append("-I%s"% str(MBED_DRIVERS)) - mbed_includes.append("-I%s"% str(MBED_PLATFORM)) - mbed_includes.append("-I%s"% str(MBED_HAL)) - mbed_c_sources = " ".join(mbed_resources.c_sources) - mbed_cpp_sources = " ".join(mbed_resources.cpp_sources) - - target_includes = [inc.strip() for inc in target_includes] - mbed_includes = [inc.strip() for inc in mbed_includes] - target_macros = [mac.strip() for mac in target_macros] - - check_cmd = cppcheck_cmd - check_cmd += cppcheck_msg_format - check_cmd += target_includes - check_cmd += mbed_includes - check_cmd += target_macros - - # We need to pass some parames via file to avoid "command line too long in - # some OSs" - tmp_file = tempfile.NamedTemporaryFile(delete=False) - tmp_file.writelines(line + '\n' for line in target_c_sources.split()) - tmp_file.writelines(line + '\n' for line in target_cpp_sources.split()) - tmp_file.writelines(line + '\n' for line in mbed_c_sources.split()) - tmp_file.writelines(line + '\n' for line in mbed_cpp_sources.split()) - tmp_file.close() - check_cmd += ["--file-list=%s"% tmp_file.name] - - _stdout, _stderr, _ = run_cmd_ext(check_cmd) - if verbose: - print _stdout - print _stderr - - -def static_analysis_scan_lib(lib_id, target, toolchain, cppcheck_cmd, - cppcheck_msg_format, verbose=False, - clean=False, macros=None, notify=None, jobs=1, - extra_verbose=False, build_profile=None): - """Perform static analysis on a library as if it were to be compiled for a - particular target and toolchain combination - """ - lib = Library(lib_id) - if lib.is_supported(target, toolchain): - static_analysis_scan_library( - lib.source_dir, lib.build_dir, target, toolchain, cppcheck_cmd, - cppcheck_msg_format, lib.dependencies, verbose=verbose, - clean=clean, macros=macros, notify=notify, jobs=jobs, - extra_verbose=extra_verbose, build_profile=build_profile) - else: - print('Library "%s" is not yet supported on target %s with toolchain %s' - % (lib_id, target.name, toolchain)) - - -def static_analysis_scan_library(src_paths, build_path, target, toolchain_name, - cppcheck_cmd, cppcheck_msg_format, - dependencies_paths=None, - name=None, clean=False, notify=None, - verbose=False, macros=None, jobs=1, - extra_verbose=False, build_profile=None): - """ Function scans library for statically detectable defects - - Positional arguments: - src_paths - the list of library paths to scan - build_path - the location directory of result files - target - the target to fake the build for - toolchain_name - pretend you would compile with this toolchain - cppcheck_cmd - the command used to do static analysis - cppcheck_msg_format - the format of the check messages - - Keyword arguments: - dependencies_paths - the paths to sources that this library depends on - name - the name of this library - clean - start from a clean slate - notify - the notification event handling function - verbose - more printing! - macros - extra macros to compile with - jobs - number of commands to run at once - extra_verbose - even moar printing - build_profile - a dict of flags that will be passed to the compiler - """ - if type(src_paths) != ListType: - src_paths = [src_paths] - - for src_path in src_paths: - if not exists(src_path): - raise Exception("The library source folder does not exist: %s", - src_path) - - # Toolchain instance - toolchain = TOOLCHAIN_CLASSES[toolchain_name](target, macros=macros, - notify=notify, - extra_verbose=extra_verbose, - build_profile=build_profile) - toolchain.VERBOSE = verbose - toolchain.jobs = jobs - - # The first path will give the name to the library - name = basename(src_paths[0]) - toolchain.info("Static analysis for library %s (%s, %s)" % - (name.upper(), target.name, toolchain_name)) - - # Scan Resources - resources = [] - for src_path in src_paths: - resources.append(toolchain.scan_resources(src_path)) - - # Dependencies Include Paths - dependencies_include_dir = [] - if dependencies_paths is not None: - for path in dependencies_paths: - lib_resources = toolchain.scan_resources(path) - dependencies_include_dir.extend(lib_resources.inc_dirs) - - # Create the desired build directory structure - bin_path = join(build_path, toolchain.obj_path) - mkdir(bin_path) - tmp_path = join(build_path, '.temp', toolchain.obj_path) - mkdir(tmp_path) - - # Gather include paths, c, cpp sources and macros to transfer to cppcheck - # command line - includes = ["-I%s" % i for i in dependencies_include_dir + src_paths] - c_sources = " " - cpp_sources = " " - macros = ['-D%s' % s for s in toolchain.get_symbols() + toolchain.macros] - - # Copy Headers - for resource in resources: - toolchain.copy_files(resource.headers, build_path, resources=resource) - includes += ["-I%s" % i for i in resource.inc_dirs] - c_sources += " ".join(resource.c_sources) + " " - cpp_sources += " ".join(resource.cpp_sources) + " " - - dependencies_include_dir.extend( - toolchain.scan_resources(build_path).inc_dirs) - - includes = [inc.strip() for inc in includes] - macros = [mac.strip() for mac in macros] - - check_cmd = cppcheck_cmd - check_cmd += cppcheck_msg_format - check_cmd += includes - check_cmd += macros - - # We need to pass some parameters via file to avoid "command line too long - # in some OSs". A temporary file is created to store e.g. cppcheck list of - # files for command line - tmp_file = tempfile.NamedTemporaryFile(delete=False) - tmp_file.writelines(line + '\n' for line in c_sources.split()) - tmp_file.writelines(line + '\n' for line in cpp_sources.split()) - tmp_file.close() - check_cmd += ["--file-list=%s"% tmp_file.name] - - # This will allow us to grab result from both stdio and stderr outputs (so - # we can show them) We assume static code analysis tool is outputting - # defects on STDERR - _stdout, _stderr, _ = run_cmd_ext(check_cmd) - if verbose: - print _stdout - print _stderr - - def print_build_results(result_list, build_name): """ Generate result string for build results diff --git a/tools/toolchains/__init__.py b/tools/toolchains/__init__.py index 16b1ae50bc..cc68ae1d52 100644 --- a/tools/toolchains/__init__.py +++ b/tools/toolchains/__init__.py @@ -299,9 +299,6 @@ class mbedToolchain: self.build_dir = build_dir self.timestamp = time() - # Output build naming based on target+toolchain combo (mbed 2.0 builds) - self.obj_path = join("TARGET_"+target.name, "TOOLCHAIN_"+self.name) - # Number of concurrent build jobs. 0 means auto (based on host system cores) self.jobs = 0 From 8a14c4a9c37e3ea3f581cc3680fce6315127bdca Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Mon, 6 Mar 2017 18:37:46 -0600 Subject: [PATCH 31/40] Update test_api call to prepare_toolchain An earlier patch in this series changed the API for `build_api.prepare_toolchain`. This commit updates the `find_test` function to call `prepare_toolchain` correctly. --- tools/test_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/test_api.py b/tools/test_api.py index 56f536582e..4f84044263 100644 --- a/tools/test_api.py +++ b/tools/test_api.py @@ -2013,7 +2013,7 @@ def find_tests(base_dir, target_name, toolchain_name, app_config=None): tests = {} # Prepare the toolchain - toolchain = prepare_toolchain([base_dir], target_name, toolchain_name, + toolchain = prepare_toolchain([base_dir], None, target_name, toolchain_name, silent=True, app_config=app_config) # Scan the directory for paths to probe for 'TESTS' folders From 3947dbfa045031e440830647b2b7e7dd1d77cf47 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Wed, 1 Mar 2017 16:11:40 -0600 Subject: [PATCH 32/40] bd: Fix missing const attributes on functions --- features/filesystem/bd/BlockDevice.h | 8 ++++---- features/filesystem/bd/ChainingBlockDevice.cpp | 2 +- features/filesystem/bd/ChainingBlockDevice.h | 2 +- features/filesystem/bd/HeapBlockDevice.cpp | 2 +- features/filesystem/bd/HeapBlockDevice.h | 2 +- features/filesystem/bd/SlicingBlockDevice.cpp | 2 +- features/filesystem/bd/SlicingBlockDevice.h | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/features/filesystem/bd/BlockDevice.h b/features/filesystem/bd/BlockDevice.h index 5a0a283f8d..a45dfee298 100644 --- a/features/filesystem/bd/BlockDevice.h +++ b/features/filesystem/bd/BlockDevice.h @@ -117,7 +117,7 @@ public: * * @return Size of the underlying device in bytes */ - virtual bd_size_t size() = 0; + virtual bd_size_t size() const = 0; /** Convenience function for checking block read validity * @@ -125,7 +125,7 @@ public: * @param size Size to read in bytes * @return True if read is valid for underlying block device */ - bool is_valid_read(bd_addr_t addr, bd_size_t size) + bool is_valid_read(bd_addr_t addr, bd_size_t size) const { return ( addr % get_read_size() == 0 && @@ -139,7 +139,7 @@ public: * @param size Size to write in bytes * @return True if program is valid for underlying block device */ - bool is_valid_program(bd_addr_t addr, bd_size_t size) + bool is_valid_program(bd_addr_t addr, bd_size_t size) const { return ( addr % get_program_size() == 0 && @@ -153,7 +153,7 @@ public: * @param size Size to erase in bytes * @return True if erase is valid for underlying block device */ - bool is_valid_erase(bd_addr_t addr, bd_size_t size) + bool is_valid_erase(bd_addr_t addr, bd_size_t size) const { return ( addr % get_erase_size() == 0 && diff --git a/features/filesystem/bd/ChainingBlockDevice.cpp b/features/filesystem/bd/ChainingBlockDevice.cpp index 76f8214c3f..9dcfbae562 100644 --- a/features/filesystem/bd/ChainingBlockDevice.cpp +++ b/features/filesystem/bd/ChainingBlockDevice.cpp @@ -190,7 +190,7 @@ bd_size_t ChainingBlockDevice::get_erase_size() const return _erase_size; } -bd_size_t ChainingBlockDevice::size() +bd_size_t ChainingBlockDevice::size() const { return _size; } diff --git a/features/filesystem/bd/ChainingBlockDevice.h b/features/filesystem/bd/ChainingBlockDevice.h index 6b0e394c15..9547c4f701 100644 --- a/features/filesystem/bd/ChainingBlockDevice.h +++ b/features/filesystem/bd/ChainingBlockDevice.h @@ -140,7 +140,7 @@ public: * * @return Size of the underlying device in bytes */ - virtual bd_size_t size(); + virtual bd_size_t size() const; protected: BlockDevice **_bds; diff --git a/features/filesystem/bd/HeapBlockDevice.cpp b/features/filesystem/bd/HeapBlockDevice.cpp index b05e3ead8b..9d95d18b89 100644 --- a/features/filesystem/bd/HeapBlockDevice.cpp +++ b/features/filesystem/bd/HeapBlockDevice.cpp @@ -77,7 +77,7 @@ bd_size_t HeapBlockDevice::get_erase_size() const return _erase_size; } -bd_size_t HeapBlockDevice::size() +bd_size_t HeapBlockDevice::size() const { return _count * _erase_size; } diff --git a/features/filesystem/bd/HeapBlockDevice.h b/features/filesystem/bd/HeapBlockDevice.h index 53e17f406d..9144319383 100644 --- a/features/filesystem/bd/HeapBlockDevice.h +++ b/features/filesystem/bd/HeapBlockDevice.h @@ -119,7 +119,7 @@ public: * * @return Size of the underlying device in bytes */ - virtual bd_size_t size(); + virtual bd_size_t size() const; private: bd_size_t _read_size; diff --git a/features/filesystem/bd/SlicingBlockDevice.cpp b/features/filesystem/bd/SlicingBlockDevice.cpp index d0f2888464..f7b3482d06 100644 --- a/features/filesystem/bd/SlicingBlockDevice.cpp +++ b/features/filesystem/bd/SlicingBlockDevice.cpp @@ -108,7 +108,7 @@ bd_size_t SlicingBlockDevice::get_erase_size() const return _bd->get_erase_size(); } -bd_size_t SlicingBlockDevice::size() +bd_size_t SlicingBlockDevice::size() const { return _stop - _start; } diff --git a/features/filesystem/bd/SlicingBlockDevice.h b/features/filesystem/bd/SlicingBlockDevice.h index 2904d68c72..74103dabcd 100644 --- a/features/filesystem/bd/SlicingBlockDevice.h +++ b/features/filesystem/bd/SlicingBlockDevice.h @@ -138,7 +138,7 @@ public: * * @return Size of the underlying device in bytes */ - virtual bd_size_t size(); + virtual bd_size_t size() const; protected: BlockDevice *_bd; From 4c344d76a9c8649f3c9f1680262d1e1207fb79f6 Mon Sep 17 00:00:00 2001 From: bcostm Date: Fri, 3 Mar 2017 16:57:16 +0100 Subject: [PATCH 33/40] Add AnalogIn pins on PF3, PF5 and PF10. Don't know why it was missing ? --- .../TARGET_NUCLEO_F446ZE/PeripheralPins.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/TARGET_NUCLEO_F446ZE/PeripheralPins.c b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/TARGET_NUCLEO_F446ZE/PeripheralPins.c index c450ae7ad8..c36a61492c 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/TARGET_NUCLEO_F446ZE/PeripheralPins.c +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/TARGET_NUCLEO_F446ZE/PeripheralPins.c @@ -81,14 +81,14 @@ const PinMap PinMap_ADC[] = { //{PC_4, ADC_2, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 14, 0)}, // ADC2_IN14 {PC_5, ADC_2, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 15, 0)}, // ADC2_IN15 //{PC_5, ADC_1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 15, 0)}, // ADC1_IN15 - //{PF_3, ADC_3, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 9, 0)}, // ADC3_IN9 - ARDUINO A3 + {PF_3, ADC_3, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 9, 0)}, // ADC3_IN9 - ARDUINO A3 {PF_4, ADC_3, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 14, 0)}, // ADC3_IN14 - //{PF_5, ADC_3, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 15, 0)}, // ADC3_IN15 - ARDUINO A4 + {PF_5, ADC_3, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 15, 0)}, // ADC3_IN15 - ARDUINO A4 {PF_6, ADC_3, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 4, 0)}, // ADC3_IN4 {PF_7, ADC_3, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 5, 0)}, // ADC3_IN5 {PF_8, ADC_3, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 6, 0)}, // ADC3_IN6 {PF_9, ADC_3, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 7, 0)}, // ADC3_IN7 - //{PF_10, ADC_3, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 8, 0)}, // ADC3_IN8 - ARDUINO A5 + {PF_10, ADC_3, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 8, 0)}, // ADC3_IN8 - ARDUINO A5 {ADC_TEMP, ADC_1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 16, 0)}, // See in analogin_api.c the correct ADC channel used {ADC_VREF, ADC_1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 17, 0)}, // See in analogin_api.c the correct ADC channel used {ADC_VBAT, ADC_1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 18, 0)}, // See in analogin_api.c the correct ADC channel used From febfa8b7208c7fe3bca50bf505ac4bacd2702533 Mon Sep 17 00:00:00 2001 From: Andres AG Date: Fri, 3 Mar 2017 13:30:37 +0000 Subject: [PATCH 34/40] Update mbed TLS feature to mbedtls-2.4.2 --- features/mbedtls/VERSION.txt | 2 +- features/mbedtls/importer/Makefile | 2 +- features/mbedtls/inc/mbedtls/cmac.h | 10 +- features/mbedtls/inc/mbedtls/compat-1.3.h | 1 - features/mbedtls/inc/mbedtls/config.h | 62 +++--- features/mbedtls/inc/mbedtls/rsa.h | 2 +- features/mbedtls/inc/mbedtls/ssl.h | 17 +- features/mbedtls/inc/mbedtls/version.h | 8 +- features/mbedtls/inc/mbedtls/x509.h | 2 +- features/mbedtls/src/base64.c | 6 +- features/mbedtls/src/bignum.c | 7 +- features/mbedtls/src/cipher.c | 4 +- features/mbedtls/src/cmac.c | 241 +++++++++++++--------- features/mbedtls/src/ctr_drbg.c | 3 +- features/mbedtls/src/debug.c | 2 +- features/mbedtls/src/ecp_curves.c | 6 +- features/mbedtls/src/md2.c | 2 +- features/mbedtls/src/net_sockets.c | 7 +- features/mbedtls/src/pem.c | 22 +- features/mbedtls/src/pk.c | 11 +- features/mbedtls/src/pk_wrap.c | 18 ++ features/mbedtls/src/pkparse.c | 12 +- features/mbedtls/src/platform.c | 4 +- features/mbedtls/src/ssl_cookie.c | 2 +- features/mbedtls/src/ssl_tls.c | 28 ++- features/mbedtls/src/x509.c | 139 ++++++++----- features/mbedtls/src/x509_crl.c | 7 +- features/mbedtls/src/x509_crt.c | 37 ++-- features/mbedtls/src/x509_csr.c | 2 +- 29 files changed, 405 insertions(+), 261 deletions(-) diff --git a/features/mbedtls/VERSION.txt b/features/mbedtls/VERSION.txt index 1d157787dc..0226ae069f 100644 --- a/features/mbedtls/VERSION.txt +++ b/features/mbedtls/VERSION.txt @@ -1 +1 @@ -mbedtls-2.4.0 +mbedtls-2.4.2 diff --git a/features/mbedtls/importer/Makefile b/features/mbedtls/importer/Makefile index 9fcf84bcf6..dae04b8aa6 100644 --- a/features/mbedtls/importer/Makefile +++ b/features/mbedtls/importer/Makefile @@ -27,7 +27,7 @@ # # Set the mbed TLS release to import (this can/should be edited before import) -MBED_TLS_RELEASE ?= mbedtls-2.4.0 +MBED_TLS_RELEASE ?= mbedtls-2.4.2 # Translate between mbed TLS namespace and mbed namespace TARGET_PREFIX:=../ diff --git a/features/mbedtls/inc/mbedtls/cmac.h b/features/mbedtls/inc/mbedtls/cmac.h index 75e0b97c4a..9a2b96bc92 100644 --- a/features/mbedtls/inc/mbedtls/cmac.h +++ b/features/mbedtls/inc/mbedtls/cmac.h @@ -58,9 +58,13 @@ struct mbedtls_cmac_context_t /** * \brief Set the CMAC key and prepare to authenticate the input * data. - * Should be called with an initialised cipher context. + * Should be called with an initialized cipher context. * - * \param ctx Cipher context + * \param ctx Cipher context. This should be a cipher context, + * initialized to be one of the following types: + * MBEDTLS_CIPHER_AES_128_ECB, MBEDTLS_CIPHER_AES_192_ECB, + * MBEDTLS_CIPHER_AES_256_ECB or + * MBEDTLS_CIPHER_DES_EDE3_ECB. * \param key CMAC key * \param keybits length of the CMAC key in bits * (must be acceptable by the cipher) @@ -115,7 +119,7 @@ int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx, int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx ); /** - * \brief Output = Generic_CMAC( hmac key, input buffer ) + * \brief Output = Generic_CMAC( cmac key, input buffer ) * * \param cipher_info message digest info * \param key CMAC key diff --git a/features/mbedtls/inc/mbedtls/compat-1.3.h b/features/mbedtls/inc/mbedtls/compat-1.3.h index 27abbd9720..af51b5f820 100644 --- a/features/mbedtls/inc/mbedtls/compat-1.3.h +++ b/features/mbedtls/inc/mbedtls/compat-1.3.h @@ -2453,7 +2453,6 @@ #define ssl_set_arc4_support mbedtls_ssl_conf_arc4_support #define ssl_set_authmode mbedtls_ssl_conf_authmode #define ssl_set_bio mbedtls_ssl_set_bio -#define ssl_set_bio mbedtls_ssl_set_bio_timeout #define ssl_set_ca_chain mbedtls_ssl_conf_ca_chain #define ssl_set_cbc_record_splitting mbedtls_ssl_conf_cbc_record_splitting #define ssl_set_ciphersuites mbedtls_ssl_conf_ciphersuites diff --git a/features/mbedtls/inc/mbedtls/config.h b/features/mbedtls/inc/mbedtls/config.h index 790e5e344b..5ff325ace7 100644 --- a/features/mbedtls/inc/mbedtls/config.h +++ b/features/mbedtls/inc/mbedtls/config.h @@ -1540,7 +1540,7 @@ * library/pkwrite.c * library/x509_create.c * library/x509write_crt.c - * library/mbedtls_x509write_csr.c + * library/x509write_csr.c */ #define MBEDTLS_ASN1_WRITE_C @@ -1901,7 +1901,7 @@ * * Enable the generic message digest layer. * - * Module: library/mbedtls_md.c + * Module: library/md.c * Caller: * * Uncomment to enable generic message digest wrappers. @@ -1913,7 +1913,7 @@ * * Enable the MD2 hash algorithm. * - * Module: library/mbedtls_md2.c + * Module: library/md2.c * Caller: * * Uncomment to enable support for (rare) MD2-signed X.509 certs. @@ -1925,7 +1925,7 @@ * * Enable the MD4 hash algorithm. * - * Module: library/mbedtls_md4.c + * Module: library/md4.c * Caller: * * Uncomment to enable support for (rare) MD4-signed X.509 certs. @@ -1937,8 +1937,8 @@ * * Enable the MD5 hash algorithm. * - * Module: library/mbedtls_md5.c - * Caller: library/mbedtls_md.c + * Module: library/md5.c + * Caller: library/md.c * library/pem.c * library/ssl_tls.c * @@ -1995,11 +1995,11 @@ * library/rsa.c * library/x509.c * library/x509_create.c - * library/mbedtls_x509_crl.c - * library/mbedtls_x509_crt.c - * library/mbedtls_x509_csr.c + * library/x509_crl.c + * library/x509_crt.c + * library/x509_csr.c * library/x509write_crt.c - * library/mbedtls_x509write_csr.c + * library/x509write_csr.c * * This modules translates between OIDs and internal values. */ @@ -2027,9 +2027,9 @@ * Module: library/pem.c * Caller: library/dhm.c * library/pkparse.c - * library/mbedtls_x509_crl.c - * library/mbedtls_x509_crt.c - * library/mbedtls_x509_csr.c + * library/x509_crl.c + * library/x509_crt.c + * library/x509_csr.c * * Requires: MBEDTLS_BASE64_C * @@ -2045,7 +2045,7 @@ * Module: library/pem.c * Caller: library/pkwrite.c * library/x509write_crt.c - * library/mbedtls_x509write_csr.c + * library/x509write_csr.c * * Requires: MBEDTLS_BASE64_C * @@ -2075,8 +2075,8 @@ * Enable the generic public (asymetric) key parser. * * Module: library/pkparse.c - * Caller: library/mbedtls_x509_crt.c - * library/mbedtls_x509_csr.c + * Caller: library/x509_crt.c + * library/x509_csr.c * * Requires: MBEDTLS_PK_C * @@ -2167,8 +2167,8 @@ * * Enable the RIPEMD-160 hash algorithm. * - * Module: library/mbedtls_ripemd160.c - * Caller: library/mbedtls_md.c + * Module: library/ripemd160.c + * Caller: library/md.c * */ //#define MBEDTLS_RIPEMD160_C @@ -2196,8 +2196,8 @@ * * Enable the SHA1 cryptographic hash algorithm. * - * Module: library/mbedtls_sha1.c - * Caller: library/mbedtls_md.c + * Module: library/sha1.c + * Caller: library/md.c * library/ssl_cli.c * library/ssl_srv.c * library/ssl_tls.c @@ -2212,9 +2212,9 @@ * * Enable the SHA-224 and SHA-256 cryptographic hash algorithms. * - * Module: library/mbedtls_sha256.c + * Module: library/sha256.c * Caller: library/entropy.c - * library/mbedtls_md.c + * library/md.c * library/ssl_cli.c * library/ssl_srv.c * library/ssl_tls.c @@ -2229,9 +2229,9 @@ * * Enable the SHA-384 and SHA-512 cryptographic hash algorithms. * - * Module: library/mbedtls_sha512.c + * Module: library/sha512.c * Caller: library/entropy.c - * library/mbedtls_md.c + * library/md.c * library/ssl_cli.c * library/ssl_srv.c * @@ -2379,9 +2379,9 @@ * Enable X.509 core for using certificates. * * Module: library/x509.c - * Caller: library/mbedtls_x509_crl.c - * library/mbedtls_x509_crt.c - * library/mbedtls_x509_csr.c + * Caller: library/x509_crl.c + * library/x509_crt.c + * library/x509_csr.c * * Requires: MBEDTLS_ASN1_PARSE_C, MBEDTLS_BIGNUM_C, MBEDTLS_OID_C, * MBEDTLS_PK_PARSE_C @@ -2395,7 +2395,7 @@ * * Enable X.509 certificate parsing. * - * Module: library/mbedtls_x509_crt.c + * Module: library/x509_crt.c * Caller: library/ssl_cli.c * library/ssl_srv.c * library/ssl_tls.c @@ -2411,8 +2411,8 @@ * * Enable X.509 CRL parsing. * - * Module: library/mbedtls_x509_crl.c - * Caller: library/mbedtls_x509_crt.c + * Module: library/x509_crl.c + * Caller: library/x509_crt.c * * Requires: MBEDTLS_X509_USE_C * @@ -2425,7 +2425,7 @@ * * Enable X.509 Certificate Signing Request (CSR) parsing. * - * Module: library/mbedtls_x509_csr.c + * Module: library/x509_csr.c * Caller: library/x509_crt_write.c * * Requires: MBEDTLS_X509_USE_C diff --git a/features/mbedtls/inc/mbedtls/rsa.h b/features/mbedtls/inc/mbedtls/rsa.h index 35185dfda1..54653dfdcd 100644 --- a/features/mbedtls/inc/mbedtls/rsa.h +++ b/features/mbedtls/inc/mbedtls/rsa.h @@ -206,7 +206,7 @@ int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rs * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code * * \note This function does NOT take care of message - * padding. Also, be sure to set input[0] = 0 or assure that + * padding. Also, be sure to set input[0] = 0 or ensure that * input is smaller than N. * * \note The input and output buffers must be large diff --git a/features/mbedtls/inc/mbedtls/ssl.h b/features/mbedtls/inc/mbedtls/ssl.h index ba499d2bde..495e02cb0e 100644 --- a/features/mbedtls/inc/mbedtls/ssl.h +++ b/features/mbedtls/inc/mbedtls/ssl.h @@ -1146,7 +1146,7 @@ void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout ) * * \note See the documentation of \c mbedtls_ssl_set_timer_t and * \c mbedtls_ssl_get_timer_t for the conventions this pair of - * callbacks must fallow. + * callbacks must follow. * * \note On some platforms, timing.c provides * \c mbedtls_timing_set_delay() and @@ -2183,7 +2183,7 @@ void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_ /** * \brief Set record counter threshold for periodic renegotiation. - * (Default: 2^64 - 256.) + * (Default: 2^48 - 1) * * Renegotiation is automatically triggered when a record * counter (outgoing or ingoing) crosses the defined @@ -2194,9 +2194,17 @@ void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_ * Lower values can be used to enforce policies such as "keys * must be refreshed every N packets with cipher X". * + * The renegotiation period can be disabled by setting + * conf->disable_renegotiation to + * MBEDTLS_SSL_RENEGOTIATION_DISABLED. + * + * \note When the configured transport is + * MBEDTLS_SSL_TRANSPORT_DATAGRAM the maximum renegotiation + * period is 2^48 - 1, and for MBEDTLS_SSL_TRANSPORT_STREAM, + * the maximum renegotiation period is 2^64 - 1. + * * \param conf SSL configuration * \param period The threshold value: a big-endian 64-bit number. - * Set to 2^64 - 1 to disable periodic renegotiation */ void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, const unsigned char period[8] ); @@ -2428,7 +2436,7 @@ int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len ) * \param len how many bytes must be written * * \return the number of bytes actually written (may be less than len), - * or MBEDTLS_ERR_SSL_WANT_WRITE of MBEDTLS_ERR_SSL_WANT_READ, + * or MBEDTLS_ERR_SSL_WANT_WRITE or MBEDTLS_ERR_SSL_WANT_READ, * or another negative error code. * * \note If this function returns something other than a positive @@ -2513,7 +2521,6 @@ void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ); * \param transport MBEDTLS_SSL_TRANSPORT_STREAM for TLS, or * MBEDTLS_SSL_TRANSPORT_DATAGRAM for DTLS * \param preset a MBEDTLS_SSL_PRESET_XXX value - * (currently unused). * * \note See \c mbedtls_ssl_conf_transport() for notes on DTLS. * diff --git a/features/mbedtls/inc/mbedtls/version.h b/features/mbedtls/inc/mbedtls/version.h index 6c0cc9b088..f00e158c41 100644 --- a/features/mbedtls/inc/mbedtls/version.h +++ b/features/mbedtls/inc/mbedtls/version.h @@ -39,16 +39,16 @@ */ #define MBEDTLS_VERSION_MAJOR 2 #define MBEDTLS_VERSION_MINOR 4 -#define MBEDTLS_VERSION_PATCH 0 +#define MBEDTLS_VERSION_PATCH 2 /** * The single version number has the following structure: * MMNNPP00 * Major version | Minor version | Patch version */ -#define MBEDTLS_VERSION_NUMBER 0x02040000 -#define MBEDTLS_VERSION_STRING "2.4.0" -#define MBEDTLS_VERSION_STRING_FULL "mbed TLS 2.4.0" +#define MBEDTLS_VERSION_NUMBER 0x02040200 +#define MBEDTLS_VERSION_STRING "2.4.2" +#define MBEDTLS_VERSION_STRING_FULL "mbed TLS 2.4.2" #if defined(MBEDTLS_VERSION_C) diff --git a/features/mbedtls/inc/mbedtls/x509.h b/features/mbedtls/inc/mbedtls/x509.h index 54dac166b4..f219bf128a 100644 --- a/features/mbedtls/inc/mbedtls/x509.h +++ b/features/mbedtls/inc/mbedtls/x509.h @@ -157,7 +157,7 @@ #define MBEDTLS_X509_EXT_INIHIBIT_ANYPOLICY (1 << 13) #define MBEDTLS_X509_EXT_FRESHEST_CRL (1 << 14) -#define MBEDTLS_X509_EXT_NS_CERT_TYPE (1 << 16) /* Parsed (and then ?) */ +#define MBEDTLS_X509_EXT_NS_CERT_TYPE (1 << 16) /* * Storage format identifiers diff --git a/features/mbedtls/src/base64.c b/features/mbedtls/src/base64.c index 5cb12cba75..f06b57b31f 100644 --- a/features/mbedtls/src/base64.c +++ b/features/mbedtls/src/base64.c @@ -192,7 +192,11 @@ int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, return( 0 ); } - n = ( ( n * 6 ) + 7 ) >> 3; + /* The following expression is to calculate the following formula without + * risk of integer overflow in n: + * n = ( ( n * 6 ) + 7 ) >> 3; + */ + n = ( 6 * ( n >> 3 ) ) + ( ( 6 * ( n & 0x7 ) + 7 ) >> 3 ); n -= j; if( dst == NULL || dlen < n ) diff --git a/features/mbedtls/src/bignum.c b/features/mbedtls/src/bignum.c index 4c99e04d6f..8b9082cdcb 100644 --- a/features/mbedtls/src/bignum.c +++ b/features/mbedtls/src/bignum.c @@ -534,7 +534,12 @@ int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, n = mbedtls_mpi_bitlen( X ); if( radix >= 4 ) n >>= 1; if( radix >= 16 ) n >>= 1; - n += 3; + /* + * Round up the buffer length to an even value to ensure that there is + * enough room for hexadecimal values that can be represented in an odd + * number of digits. + */ + n += 3 + ( ( n + 1 ) & 1 ); if( buflen < n ) { diff --git a/features/mbedtls/src/cipher.c b/features/mbedtls/src/cipher.c index a88343869c..e9e0b223e5 100644 --- a/features/mbedtls/src/cipher.c +++ b/features/mbedtls/src/cipher.c @@ -326,9 +326,9 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i * If there is not enough data for a full block, cache it. */ if( ( ctx->operation == MBEDTLS_DECRYPT && - ilen + ctx->unprocessed_len <= block_size ) || + ilen <= block_size - ctx->unprocessed_len ) || ( ctx->operation == MBEDTLS_ENCRYPT && - ilen + ctx->unprocessed_len < block_size ) ) + ilen < block_size - ctx->unprocessed_len ) ) { memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input, ilen ); diff --git a/features/mbedtls/src/cmac.c b/features/mbedtls/src/cmac.c index ee2fe056ce..b2fe713a03 100644 --- a/features/mbedtls/src/cmac.c +++ b/features/mbedtls/src/cmac.c @@ -26,7 +26,7 @@ * * - NIST SP 800-38B Recommendation for Block Cipher Modes of Operation: The * CMAC Mode for Authentication - * http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38b.pdf + * http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38b.pdf * * - RFC 4493 - The AES-CMAC Algorithm * https://tools.ietf.org/html/rfc4493 @@ -62,7 +62,7 @@ #if defined(MBEDTLS_SELF_TEST) #include #define mbedtls_printf printf -#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C || MBEDTLS_DES_C */ +#endif /* MBEDTLS_SELF_TEST */ #endif /* MBEDTLS_PLATFORM_C */ /* Implementation that should never be optimized out by the compiler */ @@ -80,7 +80,7 @@ static void mbedtls_zeroize( void *v, size_t n ) { * with R_64 = 0x1B and R_128 = 0x87 * * Input and output MUST NOT point to the same buffer - * Block size must be 8 byes or 16 bytes - the block sizes for DES and AES. + * Block size must be 8 bytes or 16 bytes - the block sizes for DES and AES. */ static int cmac_multiply_by_u( unsigned char *output, const unsigned char *input, @@ -105,7 +105,7 @@ static int cmac_multiply_by_u( unsigned char *output, return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); } - for( i = blocksize - 1; i >= 0; i-- ) + for( i = (int)blocksize - 1; i >= 0; i-- ) { output[i] = input[i] << 1 | overflow; overflow = input[i] >> 7; @@ -209,7 +209,7 @@ int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx, if( ctx == NULL || ctx->cipher_info == NULL || key == NULL ) return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); - if( ( retval = mbedtls_cipher_setkey( ctx, key, keybits, + if( ( retval = mbedtls_cipher_setkey( ctx, key, (int)keybits, MBEDTLS_ENCRYPT ) ) != 0 ) return( retval ); @@ -244,8 +244,8 @@ int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx, { mbedtls_cmac_context_t* cmac_ctx; unsigned char *state; - int n, j, ret = 0; - size_t olen, block_size; + int ret = 0; + size_t n, j, olen, block_size; if( ctx == NULL || ctx->cipher_info == NULL || input == NULL || ctx->cmac_ctx == NULL ) @@ -280,8 +280,9 @@ int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx, /* n is the number of blocks including any final partial block */ n = ( ilen + block_size - 1 ) / block_size; - /* Iterate across the input data in block sized chunks */ - for( j = 0; j < n - 1; j++ ) + /* Iterate across the input data in block sized chunks, excluding any + * final partial or complete block */ + for( j = 1; j < n; j++ ) { cmac_xor_block( state, input, state, block_size ); @@ -469,8 +470,9 @@ exit: #if defined(MBEDTLS_SELF_TEST) /* - * CMAC test data from SP800-38B Appendix D.1 (corrected) - * http://csrc.nist.gov/publications/nistpubs/800-38B/Updated_CMAC_Examples.pdf + * CMAC test data for SP800-38B + * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/AES_CMAC.pdf + * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/TDES_CMAC.pdf * * AES-CMAC-PRF-128 test data from RFC 4615 * https://tools.ietf.org/html/rfc4615#page-4 @@ -482,128 +484,148 @@ exit: #if defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) /* All CMAC test inputs are truncated from the same 64 byte buffer. */ static const unsigned char test_message[] = { - 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, - 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, - 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, - 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, - 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, - 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, - 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, - 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 + /* PT */ + 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, + 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, + 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, + 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, + 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, + 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, + 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, + 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 }; #endif /* MBEDTLS_AES_C || MBEDTLS_DES_C */ #if defined(MBEDTLS_AES_C) /* Truncation point of message for AES CMAC tests */ static const unsigned int aes_message_lengths[NB_CMAC_TESTS_PER_KEY] = { + /* Mlen */ 0, 16, - 40, + 20, 64 }; -/* AES 128 CMAC Test Data */ +/* CMAC-AES128 Test Data */ static const unsigned char aes_128_key[16] = { - 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, - 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c + 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }; static const unsigned char aes_128_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = { { - 0xfb, 0xee, 0xd6, 0x18, 0x35, 0x71, 0x33, 0x66, - 0x7c, 0x85, 0xe0, 0x8f, 0x72, 0x36, 0xa8, 0xde + /* K1 */ + 0xfb, 0xee, 0xd6, 0x18, 0x35, 0x71, 0x33, 0x66, + 0x7c, 0x85, 0xe0, 0x8f, 0x72, 0x36, 0xa8, 0xde }, { - 0xf7, 0xdd, 0xac, 0x30, 0x6a, 0xe2, 0x66, 0xcc, - 0xf9, 0x0b, 0xc1, 0x1e, 0xe4, 0x6d, 0x51, 0x3b + /* K2 */ + 0xf7, 0xdd, 0xac, 0x30, 0x6a, 0xe2, 0x66, 0xcc, + 0xf9, 0x0b, 0xc1, 0x1e, 0xe4, 0x6d, 0x51, 0x3b } }; static const unsigned char aes_128_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = { { - 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28, - 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46 + /* Example #1 */ + 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28, + 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46 }, { - 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44, - 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c + /* Example #2 */ + 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44, + 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c }, { - 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30, - 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27 + /* Example #3 */ + 0x7d, 0x85, 0x44, 0x9e, 0xa6, 0xea, 0x19, 0xc8, + 0x23, 0xa7, 0xbf, 0x78, 0x83, 0x7d, 0xfa, 0xde }, { - 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92, - 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe + /* Example #4 */ + 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92, + 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe } }; -/* AES 192 CMAC Test Data */ +/* CMAC-AES192 Test Data */ static const unsigned char aes_192_key[24] = { - 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, - 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, - 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b + 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, + 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, + 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b }; static const unsigned char aes_192_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = { { - 0x44, 0x8a, 0x5b, 0x1c, 0x93, 0x51, 0x4b, 0x27, - 0x3e, 0xe6, 0x43, 0x9d, 0xd4, 0xda, 0xa2, 0x96 + /* K1 */ + 0x44, 0x8a, 0x5b, 0x1c, 0x93, 0x51, 0x4b, 0x27, + 0x3e, 0xe6, 0x43, 0x9d, 0xd4, 0xda, 0xa2, 0x96 }, { - 0x89, 0x14, 0xb6, 0x39, 0x26, 0xa2, 0x96, 0x4e, - 0x7d, 0xcc, 0x87, 0x3b, 0xa9, 0xb5, 0x45, 0x2c + /* K2 */ + 0x89, 0x14, 0xb6, 0x39, 0x26, 0xa2, 0x96, 0x4e, + 0x7d, 0xcc, 0x87, 0x3b, 0xa9, 0xb5, 0x45, 0x2c } }; static const unsigned char aes_192_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = { { - 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5, - 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67 + /* Example #1 */ + 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5, + 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67 }, { - 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90, - 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84 + /* Example #2 */ + 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90, + 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84 }, { - 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad, - 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e + /* Example #3 */ + 0x3d, 0x75, 0xc1, 0x94, 0xed, 0x96, 0x07, 0x04, + 0x44, 0xa9, 0xfa, 0x7e, 0xc7, 0x40, 0xec, 0xf8 }, { - 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79, - 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11 + /* Example #4 */ + 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79, + 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11 } }; -/* AES 256 CMAC Test Data */ +/* CMAC-AES256 Test Data */ static const unsigned char aes_256_key[32] = { - 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, - 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, - 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, - 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 + 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, + 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, + 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, + 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 }; static const unsigned char aes_256_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = { { - 0xca, 0xd1, 0xed, 0x03, 0x29, 0x9e, 0xed, 0xac, - 0x2e, 0x9a, 0x99, 0x80, 0x86, 0x21, 0x50, 0x2f + /* K1 */ + 0xca, 0xd1, 0xed, 0x03, 0x29, 0x9e, 0xed, 0xac, + 0x2e, 0x9a, 0x99, 0x80, 0x86, 0x21, 0x50, 0x2f }, { - 0x95, 0xa3, 0xda, 0x06, 0x53, 0x3d, 0xdb, 0x58, - 0x5d, 0x35, 0x33, 0x01, 0x0c, 0x42, 0xa0, 0xd9 + /* K2 */ + 0x95, 0xa3, 0xda, 0x06, 0x53, 0x3d, 0xdb, 0x58, + 0x5d, 0x35, 0x33, 0x01, 0x0c, 0x42, 0xa0, 0xd9 } }; static const unsigned char aes_256_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = { { - 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e, - 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83 + /* Example #1 */ + 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e, + 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83 }, { - 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82, - 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c + /* Example #2 */ + 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82, + 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c }, { - 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2, - 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6 + /* Example #3 */ + 0x15, 0x67, 0x27, 0xdc, 0x08, 0x78, 0x94, 0x4a, + 0x02, 0x3c, 0x1f, 0xe0, 0x3b, 0xad, 0x6d, 0x93 }, { - 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5, - 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10 + /* Example #4 */ + 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5, + 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10 } }; #endif /* MBEDTLS_AES_C */ @@ -612,66 +634,84 @@ static const unsigned char aes_256_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTL /* Truncation point of message for 3DES CMAC tests */ static const unsigned int des3_message_lengths[NB_CMAC_TESTS_PER_KEY] = { 0, - 8, + 16, 20, 32 }; -/* 3DES 2 Key CMAC Test Data */ +/* CMAC-TDES (Generation) - 2 Key Test Data */ static const unsigned char des3_2key_key[24] = { - 0x4c, 0xf1, 0x51, 0x34, 0xa2, 0x85, 0x0d, 0xd5, - 0x8a, 0x3d, 0x10, 0xba, 0x80, 0x57, 0x0d, 0x38, - 0x4c, 0xf1, 0x51, 0x34, 0xa2, 0x85, 0x0d, 0xd5 + /* Key1 */ + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, + /* Key2 */ + 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xEF, 0x01, + /* Key3 */ + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; static const unsigned char des3_2key_subkeys[2][8] = { { - 0x8e, 0xcf, 0x37, 0x3e, 0xd7, 0x1a, 0xfa, 0xef + /* K1 */ + 0x0d, 0xd2, 0xcb, 0x7a, 0x3d, 0x88, 0x88, 0xd9 }, { - 0x1d, 0x9e, 0x6e, 0x7d, 0xae, 0x35, 0xf5, 0xc5 + /* K2 */ + 0x1b, 0xa5, 0x96, 0xf4, 0x7b, 0x11, 0x11, 0xb2 } }; static const unsigned char des3_2key_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_DES3_BLOCK_SIZE] = { { - 0xbd, 0x2e, 0xbf, 0x9a, 0x3b, 0xa0, 0x03, 0x61 + /* Sample #1 */ + 0x79, 0xce, 0x52, 0xa7, 0xf7, 0x86, 0xa9, 0x60 }, { - 0x4f, 0xf2, 0xab, 0x81, 0x3c, 0x53, 0xce, 0x83 + /* Sample #2 */ + 0xcc, 0x18, 0xa0, 0xb7, 0x9a, 0xf2, 0x41, 0x3b }, { - 0x62, 0xdd, 0x1b, 0x47, 0x19, 0x02, 0xbd, 0x4e + /* Sample #3 */ + 0xc0, 0x6d, 0x37, 0x7e, 0xcd, 0x10, 0x19, 0x69 }, { - 0x31, 0xb1, 0xe4, 0x31, 0xda, 0xbc, 0x4e, 0xb8 + /* Sample #4 */ + 0x9c, 0xd3, 0x35, 0x80, 0xf9, 0xb6, 0x4d, 0xfb } }; -/* 3DES 3 Key CMAC Test Data */ +/* CMAC-TDES (Generation) - 3 Key Test Data */ static const unsigned char des3_3key_key[24] = { - 0x8a, 0xa8, 0x3b, 0xf8, 0xcb, 0xda, 0x10, 0x62, - 0x0b, 0xc1, 0xbf, 0x19, 0xfb, 0xb6, 0xcd, 0x58, - 0xbc, 0x31, 0x3d, 0x4a, 0x37, 0x1c, 0xa8, 0xb5 + /* Key1 */ + 0x01, 0x23, 0x45, 0x67, 0x89, 0xaa, 0xcd, 0xef, + /* Key2 */ + 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, + /* Key3 */ + 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23 }; static const unsigned char des3_3key_subkeys[2][8] = { { - 0x91, 0x98, 0xe9, 0xd3, 0x14, 0xe6, 0x53, 0x5f + /* K1 */ + 0x9d, 0x74, 0xe7, 0x39, 0x33, 0x17, 0x96, 0xc0 }, { - 0x23, 0x31, 0xd3, 0xa6, 0x29, 0xcc, 0xa6, 0xa5 + /* K2 */ + 0x3a, 0xe9, 0xce, 0x72, 0x66, 0x2f, 0x2d, 0x9b } }; static const unsigned char des3_3key_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_DES3_BLOCK_SIZE] = { { - 0xb7, 0xa6, 0x88, 0xe1, 0x22, 0xff, 0xaf, 0x95 + /* Sample #1 */ + 0x7d, 0xb0, 0xd3, 0x7d, 0xf9, 0x36, 0xc5, 0x50 }, { - 0x8e, 0x8f, 0x29, 0x31, 0x36, 0x28, 0x37, 0x97 + /* Sample #2 */ + 0x30, 0x23, 0x9c, 0xf1, 0xf5, 0x2e, 0x66, 0x09 }, { - 0x74, 0x3d, 0xdb, 0xe0, 0xce, 0x2d, 0xc2, 0xed + /* Sample #3 */ + 0x6c, 0x9f, 0x3e, 0xe4, 0x92, 0x3f, 0x6b, 0xe2 }, { - 0x33, 0xe6, 0xb1, 0x09, 0x24, 0x00, 0xea, 0xe5 + /* Sample #4 */ + 0x99, 0x42, 0x9b, 0xd0, 0xbF, 0x79, 0x04, 0xe5 } }; @@ -680,8 +720,9 @@ static const unsigned char des3_3key_expected_result[NB_CMAC_TESTS_PER_KEY][MBED #if defined(MBEDTLS_AES_C) /* AES AES-CMAC-PRF-128 Test Data */ static const unsigned char PRFK[] = { - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + /* Key */ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xed, 0xcb }; @@ -692,25 +733,25 @@ static const size_t PRFKlen[NB_PRF_TESTS] = { 10 }; -/* PRF M */ +/* Message */ static const unsigned char PRFM[] = { - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 }; static const unsigned char PRFT[NB_PRF_TESTS][16] = { { - 0x84, 0xa3, 0x48, 0xa4, 0xa4, 0x5d, 0x23, 0x5b, - 0xab, 0xff, 0xfc, 0x0d, 0x2b, 0x4d, 0xa0, 0x9a + 0x84, 0xa3, 0x48, 0xa4, 0xa4, 0x5d, 0x23, 0x5b, + 0xab, 0xff, 0xfc, 0x0d, 0x2b, 0x4d, 0xa0, 0x9a }, { - 0x98, 0x0a, 0xe8, 0x7b, 0x5f, 0x4c, 0x9c, 0x52, - 0x14, 0xf5, 0xb6, 0xa8, 0x45, 0x5e, 0x4c, 0x2d + 0x98, 0x0a, 0xe8, 0x7b, 0x5f, 0x4c, 0x9c, 0x52, + 0x14, 0xf5, 0xb6, 0xa8, 0x45, 0x5e, 0x4c, 0x2d }, { - 0x29, 0x0d, 0x9e, 0x11, 0x2e, 0xdb, 0x09, 0xee, - 0x14, 0x1f, 0xcf, 0x64, 0xc0, 0xb7, 0x2f, 0x3d + 0x29, 0x0d, 0x9e, 0x11, 0x2e, 0xdb, 0x09, 0xee, + 0x14, 0x1f, 0xcf, 0x64, 0xc0, 0xb7, 0x2f, 0x3d } }; #endif /* MBEDTLS_AES_C */ diff --git a/features/mbedtls/src/ctr_drbg.c b/features/mbedtls/src/ctr_drbg.c index 386f8adb07..55612c7fc9 100644 --- a/features/mbedtls/src/ctr_drbg.c +++ b/features/mbedtls/src/ctr_drbg.c @@ -290,7 +290,8 @@ int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx, unsigned char seed[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT]; size_t seedlen = 0; - if( ctx->entropy_len + len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT ) + if( ctx->entropy_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT || + len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - ctx->entropy_len ) return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG ); memset( seed, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT ); diff --git a/features/mbedtls/src/debug.c b/features/mbedtls/src/debug.c index a9cd814be4..f9229b3606 100644 --- a/features/mbedtls/src/debug.c +++ b/features/mbedtls/src/debug.c @@ -71,7 +71,7 @@ static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level, */ #if defined(MBEDTLS_THREADING_C) char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */ - mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", ssl, str ); + mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str ); ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr ); #else ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str ); diff --git a/features/mbedtls/src/ecp_curves.c b/features/mbedtls/src/ecp_curves.c index 9a6e8eb187..a2a5495a91 100644 --- a/features/mbedtls/src/ecp_curves.c +++ b/features/mbedtls/src/ecp_curves.c @@ -1213,7 +1213,7 @@ static inline int ecp_mod_koblitz( mbedtls_mpi *N, mbedtls_mpi_uint *Rp, size_t int ret; size_t i; mbedtls_mpi M, R; - mbedtls_mpi_uint Mp[P_KOBLITZ_MAX + P_KOBLITZ_R]; + mbedtls_mpi_uint Mp[P_KOBLITZ_MAX + P_KOBLITZ_R + 1]; if( N->n < p_limbs ) return( 0 ); @@ -1235,7 +1235,7 @@ static inline int ecp_mod_koblitz( mbedtls_mpi *N, mbedtls_mpi_uint *Rp, size_t memcpy( Mp, N->p + p_limbs - adjust, M.n * sizeof( mbedtls_mpi_uint ) ); if( shift != 0 ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &M, shift ) ); - M.n += R.n - adjust; /* Make room for multiplication by R */ + M.n += R.n; /* Make room for multiplication by R */ /* N = A0 */ if( mask != 0 ) @@ -1257,7 +1257,7 @@ static inline int ecp_mod_koblitz( mbedtls_mpi *N, mbedtls_mpi_uint *Rp, size_t memcpy( Mp, N->p + p_limbs - adjust, M.n * sizeof( mbedtls_mpi_uint ) ); if( shift != 0 ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &M, shift ) ); - M.n += R.n - adjust; /* Make room for multiplication by R */ + M.n += R.n; /* Make room for multiplication by R */ /* N = A0 */ if( mask != 0 ) diff --git a/features/mbedtls/src/md2.c b/features/mbedtls/src/md2.c index 8976701312..95cbcce658 100644 --- a/features/mbedtls/src/md2.c +++ b/features/mbedtls/src/md2.c @@ -158,7 +158,7 @@ void mbedtls_md2_update( mbedtls_md2_context *ctx, const unsigned char *input, s while( ilen > 0 ) { - if( ctx->left + ilen > 16 ) + if( ilen > 16 - ctx->left ) fill = 16 - ctx->left; else fill = ilen; diff --git a/features/mbedtls/src/net_sockets.c b/features/mbedtls/src/net_sockets.c index cc06cbfad3..80be6ec6a4 100644 --- a/features/mbedtls/src/net_sockets.c +++ b/features/mbedtls/src/net_sockets.c @@ -133,7 +133,8 @@ void mbedtls_net_init( mbedtls_net_context *ctx ) /* * Initiate a TCP connection with host:port and the given protocol */ -int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char *port, int proto ) +int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, + const char *port, int proto ) { int ret; struct addrinfo hints, *addr_list, *cur; @@ -238,7 +239,7 @@ int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char } } - /* I we ever get there, it's a success */ + /* Bind was successful */ ret = 0; break; } @@ -322,7 +323,7 @@ int mbedtls_net_accept( mbedtls_net_context *bind_ctx, { /* TCP: actual accept() */ ret = client_ctx->fd = (int) accept( bind_ctx->fd, - (struct sockaddr *) &client_addr, &n ); + (struct sockaddr *) &client_addr, &n ); } else { diff --git a/features/mbedtls/src/pem.c b/features/mbedtls/src/pem.c index 1ee3966e1e..8dd86a4ac9 100644 --- a/features/mbedtls/src/pem.c +++ b/features/mbedtls/src/pem.c @@ -44,12 +44,12 @@ #define mbedtls_free free #endif +#if defined(MBEDTLS_PEM_PARSE_C) /* Implementation that should never be optimized out by the compiler */ static void mbedtls_zeroize( void *v, size_t n ) { volatile unsigned char *p = v; while( n-- ) *p++ = 0; } -#if defined(MBEDTLS_PEM_PARSE_C) void mbedtls_pem_init( mbedtls_pem_context *ctx ) { memset( ctx, 0, sizeof( mbedtls_pem_context ) ); @@ -249,7 +249,7 @@ int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const enc = 0; - if( memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 ) + if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 ) { #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \ ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) ) @@ -262,22 +262,22 @@ int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const #if defined(MBEDTLS_DES_C) - if( memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 ) + if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 ) { enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC; s1 += 23; - if( pem_get_iv( s1, pem_iv, 8 ) != 0 ) + if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 ) return( MBEDTLS_ERR_PEM_INVALID_ENC_IV ); s1 += 16; } - else if( memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 ) + else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 ) { enc_alg = MBEDTLS_CIPHER_DES_CBC; s1 += 18; - if( pem_get_iv( s1, pem_iv, 8) != 0 ) + if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 ) return( MBEDTLS_ERR_PEM_INVALID_ENC_IV ); s1 += 16; @@ -285,9 +285,11 @@ int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const #endif /* MBEDTLS_DES_C */ #if defined(MBEDTLS_AES_C) - if( memcmp( s1, "DEK-Info: AES-", 14 ) == 0 ) + if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 ) { - if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 ) + if( s2 - s1 < 22 ) + return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG ); + else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 ) enc_alg = MBEDTLS_CIPHER_AES_128_CBC; else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 ) enc_alg = MBEDTLS_CIPHER_AES_192_CBC; @@ -297,7 +299,7 @@ int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG ); s1 += 22; - if( pem_get_iv( s1, pem_iv, 16 ) != 0 ) + if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 ) return( MBEDTLS_ERR_PEM_INVALID_ENC_IV ); s1 += 32; @@ -316,7 +318,7 @@ int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */ } - if( s1 == s2 ) + if( s1 >= s2 ) return( MBEDTLS_ERR_PEM_INVALID_DATA ); ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 ); diff --git a/features/mbedtls/src/pk.c b/features/mbedtls/src/pk.c index 10bd0a5828..8d13bc5ce3 100644 --- a/features/mbedtls/src/pk.c +++ b/features/mbedtls/src/pk.c @@ -29,6 +29,8 @@ #include "mbedtls/pk.h" #include "mbedtls/pk_internal.h" +#include "mbedtls/bignum.h" + #if defined(MBEDTLS_RSA_C) #include "mbedtls/rsa.h" #endif @@ -39,6 +41,8 @@ #include "mbedtls/ecdsa.h" #endif +#include + /* Implementation that should never be optimized out by the compiler */ static void mbedtls_zeroize( void *v, size_t n ) { volatile unsigned char *p = v; while( n-- ) *p++ = 0; @@ -209,6 +213,11 @@ int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options, int ret; const mbedtls_pk_rsassa_pss_options *pss_opts; +#if defined(MBEDTLS_HAVE_INT64) + if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len ) + return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); +#endif /* MBEDTLS_HAVE_INT64 */ + if( options == NULL ) return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); @@ -232,7 +241,7 @@ int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options, return( 0 ); #else return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE ); -#endif +#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */ } /* General case: no options */ diff --git a/features/mbedtls/src/pk_wrap.c b/features/mbedtls/src/pk_wrap.c index 712ad48323..db6274cbf9 100644 --- a/features/mbedtls/src/pk_wrap.c +++ b/features/mbedtls/src/pk_wrap.c @@ -30,6 +30,7 @@ /* Even if RSA not activated, for the sake of RSA-alt */ #include "mbedtls/rsa.h" +#include "mbedtls/bignum.h" #include @@ -49,6 +50,8 @@ #define mbedtls_free free #endif +#include + #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) /* Implementation that should never be optimized out by the compiler */ static void mbedtls_zeroize( void *v, size_t n ) { @@ -74,6 +77,11 @@ static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg, { int ret; +#if defined(MBEDTLS_HAVE_INT64) + if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len ) + return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); +#endif /* MBEDTLS_HAVE_INT64 */ + if( sig_len < ((mbedtls_rsa_context *) ctx)->len ) return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); @@ -93,6 +101,11 @@ static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, unsigned char *sig, size_t *sig_len, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) { +#if defined(MBEDTLS_HAVE_INT64) + if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len ) + return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); +#endif /* MBEDTLS_HAVE_INT64 */ + *sig_len = ((mbedtls_rsa_context *) ctx)->len; return( mbedtls_rsa_pkcs1_sign( (mbedtls_rsa_context *) ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, @@ -402,6 +415,11 @@ static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, { mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx; +#if defined(MBEDTLS_HAVE_INT64) + if( UINT_MAX < hash_len ) + return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); +#endif /* MBEDTLS_HAVE_INT64 */ + *sig_len = rsa_alt->key_len_func( rsa_alt->key ); return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, diff --git a/features/mbedtls/src/pkparse.c b/features/mbedtls/src/pkparse.c index 275429e603..efdf437466 100644 --- a/features/mbedtls/src/pkparse.c +++ b/features/mbedtls/src/pkparse.c @@ -1187,12 +1187,12 @@ int mbedtls_pk_parse_key( mbedtls_pk_context *pk, #endif /* MBEDTLS_PEM_PARSE_C */ /* - * At this point we only know it's not a PEM formatted key. Could be any - * of the known DER encoded private key formats - * - * We try the different DER format parsers to see if one passes without - * error - */ + * At this point we only know it's not a PEM formatted key. Could be any + * of the known DER encoded private key formats + * + * We try the different DER format parsers to see if one passes without + * error + */ #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C) if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk, key, keylen, pwd, pwdlen ) ) == 0 ) diff --git a/features/mbedtls/src/platform.c b/features/mbedtls/src/platform.c index 2591c45d75..8b336c38ec 100644 --- a/features/mbedtls/src/platform.c +++ b/features/mbedtls/src/platform.c @@ -237,7 +237,7 @@ int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len ) } fclose( file ); - return( n ); + return( (int)n ); } int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len ) @@ -255,7 +255,7 @@ int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len ) } fclose( file ); - return( n ); + return( (int)n ); } #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */ diff --git a/features/mbedtls/src/ssl_cookie.c b/features/mbedtls/src/ssl_cookie.c index 9fb32de7a6..caf119990d 100644 --- a/features/mbedtls/src/ssl_cookie.c +++ b/features/mbedtls/src/ssl_cookie.c @@ -98,7 +98,7 @@ void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx ) mbedtls_md_free( &ctx->hmac_ctx ); #if defined(MBEDTLS_THREADING_C) - mbedtls_mutex_init( &ctx->mutex ); + mbedtls_mutex_free( &ctx->mutex ); #endif mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) ); diff --git a/features/mbedtls/src/ssl_tls.c b/features/mbedtls/src/ssl_tls.c index 84a04ae53f..d9ab83291b 100644 --- a/features/mbedtls/src/ssl_tls.c +++ b/features/mbedtls/src/ssl_tls.c @@ -3428,7 +3428,7 @@ static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl ) if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ) { - /* Dont check write errors as we can't do anything here. + /* Don't check write errors as we can't do anything here. * If the error is permanent we'll catch it later, * if it's not, then hopefully it'll work next time. */ (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len ); @@ -6006,8 +6006,9 @@ int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **prot const char **p; /* - * "Empty strings MUST NOT be included and byte strings MUST NOT be - * truncated". Check lengths now rather than later. + * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings + * MUST NOT be truncated." + * We check lengths now rather than later. */ tot_len = 0; for( p = protos; *p != NULL; p++ ) @@ -6481,6 +6482,10 @@ int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) */ static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl ) { + size_t ep_len = ssl_ep_len( ssl ); + int in_ctr_cmp; + int out_ctr_cmp; + if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ) @@ -6488,8 +6493,12 @@ static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl ) return( 0 ); } - if( memcmp( ssl->in_ctr, ssl->conf->renego_period, 8 ) <= 0 && - memcmp( ssl->out_ctr, ssl->conf->renego_period, 8 ) <= 0 ) + in_ctr_cmp = memcmp( ssl->in_ctr + ep_len, + ssl->conf->renego_period + ep_len, 8 - ep_len ); + out_ctr_cmp = memcmp( ssl->out_ctr + ep_len, + ssl->conf->renego_period + ep_len, 8 - ep_len ); + + if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 ) { return( 0 ); } @@ -7230,8 +7239,8 @@ int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, #if defined(MBEDTLS_SSL_RENEGOTIATION) conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; - memset( conf->renego_period, 0xFF, 7 ); - conf->renego_period[7] = 0x00; + memset( conf->renego_period, 0x00, 2 ); + memset( conf->renego_period + 2, 0xFF, 6 ); #endif #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) @@ -7585,7 +7594,7 @@ int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, * and, for DTLS, to/from TLS equivalent. * * For TLS this is the identity. - * For DTLS, use one complement (v -> 255 - v, and then map as follows: + * For DTLS, use 1's complement (v -> 255 - v, and then map as follows: * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1) * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2) */ @@ -7644,8 +7653,7 @@ int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) #if defined(MBEDTLS_MD5_C) case MBEDTLS_SSL_HASH_MD5: - ssl->handshake->calc_verify = ssl_calc_verify_tls; - break; + return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; #endif #if defined(MBEDTLS_SHA1_C) case MBEDTLS_SSL_HASH_SHA1: diff --git a/features/mbedtls/src/x509.c b/features/mbedtls/src/x509.c index fad390d857..e4387707d1 100644 --- a/features/mbedtls/src/x509.c +++ b/features/mbedtls/src/x509.c @@ -480,14 +480,20 @@ int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end, } } -static int x509_parse_int(unsigned char **p, unsigned n, int *res){ +static int x509_parse_int( unsigned char **p, size_t n, int *res ) +{ *res = 0; - for( ; n > 0; --n ){ - if( ( **p < '0') || ( **p > '9' ) ) return MBEDTLS_ERR_X509_INVALID_DATE; + + for( ; n > 0; --n ) + { + if( ( **p < '0') || ( **p > '9' ) ) + return ( MBEDTLS_ERR_X509_INVALID_DATE ); + *res *= 10; - *res += (*(*p)++ - '0'); + *res += ( *(*p)++ - '0' ); } - return 0; + + return( 0 ); } static int x509_date_is_valid(const mbedtls_x509_time *time) @@ -517,6 +523,70 @@ static int x509_date_is_valid(const mbedtls_x509_time *time) return( 0 ); } +/* + * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4) + * field. + */ +static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen, + mbedtls_x509_time *time ) +{ + int ret; + + /* + * Minimum length is 10 or 12 depending on yearlen + */ + if ( len < yearlen + 8 ) + return ( MBEDTLS_ERR_X509_INVALID_DATE ); + len -= yearlen + 8; + + /* + * Parse year, month, day, hour, minute + */ + CHECK( x509_parse_int( p, yearlen, &time->year ) ); + if ( 2 == yearlen ) + { + if ( time->year < 50 ) + time->year += 100; + + time->year += 1900; + } + + CHECK( x509_parse_int( p, 2, &time->mon ) ); + CHECK( x509_parse_int( p, 2, &time->day ) ); + CHECK( x509_parse_int( p, 2, &time->hour ) ); + CHECK( x509_parse_int( p, 2, &time->min ) ); + + /* + * Parse seconds if present + */ + if ( len >= 2 ) + { + CHECK( x509_parse_int( p, 2, &time->sec ) ); + len -= 2; + } + else + return ( MBEDTLS_ERR_X509_INVALID_DATE ); + + /* + * Parse trailing 'Z' if present + */ + if ( 1 == len && 'Z' == **p ) + { + (*p)++; + len--; + } + + /* + * We should have parsed all characters at this point + */ + if ( 0 != len ) + return ( MBEDTLS_ERR_X509_INVALID_DATE ); + + CHECK( x509_date_is_valid( time ) ); + + return ( 0 ); +} + /* * Time ::= CHOICE { * utcTime UTCTime, @@ -526,7 +596,7 @@ int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end, mbedtls_x509_time *time ) { int ret; - size_t len; + size_t len, year_len; unsigned char tag; if( ( end - *p ) < 1 ) @@ -536,55 +606,20 @@ int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end, tag = **p; if( tag == MBEDTLS_ASN1_UTC_TIME ) - { - (*p)++; - ret = mbedtls_asn1_get_len( p, end, &len ); - - if( ret != 0 ) - return( MBEDTLS_ERR_X509_INVALID_DATE + ret ); - - CHECK( x509_parse_int( p, 2, &time->year ) ); - CHECK( x509_parse_int( p, 2, &time->mon ) ); - CHECK( x509_parse_int( p, 2, &time->day ) ); - CHECK( x509_parse_int( p, 2, &time->hour ) ); - CHECK( x509_parse_int( p, 2, &time->min ) ); - if( len > 10 ) - CHECK( x509_parse_int( p, 2, &time->sec ) ); - if( len > 12 && *(*p)++ != 'Z' ) - return( MBEDTLS_ERR_X509_INVALID_DATE ); - - time->year += 100 * ( time->year < 50 ); - time->year += 1900; - - CHECK( x509_date_is_valid( time ) ); - - return( 0 ); - } + year_len = 2; else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME ) - { - (*p)++; - ret = mbedtls_asn1_get_len( p, end, &len ); - - if( ret != 0 ) - return( MBEDTLS_ERR_X509_INVALID_DATE + ret ); - - CHECK( x509_parse_int( p, 4, &time->year ) ); - CHECK( x509_parse_int( p, 2, &time->mon ) ); - CHECK( x509_parse_int( p, 2, &time->day ) ); - CHECK( x509_parse_int( p, 2, &time->hour ) ); - CHECK( x509_parse_int( p, 2, &time->min ) ); - if( len > 12 ) - CHECK( x509_parse_int( p, 2, &time->sec ) ); - if( len > 14 && *(*p)++ != 'Z' ) - return( MBEDTLS_ERR_X509_INVALID_DATE ); - - CHECK( x509_date_is_valid( time ) ); - - return( 0 ); - } + year_len = 4; else return( MBEDTLS_ERR_X509_INVALID_DATE + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ); + + (*p)++; + ret = mbedtls_asn1_get_len( p, end, &len ); + + if( ret != 0 ) + return( MBEDTLS_ERR_X509_INVALID_DATE + ret ); + + return x509_parse_time( p, len, year_len, time ); } int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig ) @@ -661,7 +696,7 @@ int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x50 /* * X.509 Extensions (No parsing of extensions, pointer should - * be either manually updated or extensions should be parsed! + * be either manually updated or extensions should be parsed!) */ int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *ext, int tag ) diff --git a/features/mbedtls/src/x509_crl.c b/features/mbedtls/src/x509_crl.c index 7b2b4733bc..76c49f1353 100644 --- a/features/mbedtls/src/x509_crl.c +++ b/features/mbedtls/src/x509_crl.c @@ -525,16 +525,17 @@ int mbedtls_x509_crl_parse( mbedtls_x509_crl *chain, const unsigned char *buf, s if( ( ret = mbedtls_x509_crl_parse_der( chain, pem.buf, pem.buflen ) ) != 0 ) { + mbedtls_pem_free( &pem ); return( ret ); } - - mbedtls_pem_free( &pem ); } - else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) + else if( is_pem ) { mbedtls_pem_free( &pem ); return( ret ); } + + mbedtls_pem_free( &pem ); } /* In the PEM case, buflen is 1 at the end, for the terminated NULL byte. * And a valid CRL cannot be less than 1 byte anyway. */ diff --git a/features/mbedtls/src/x509_crt.c b/features/mbedtls/src/x509_crt.c index 60e14f90e7..234f14563e 100644 --- a/features/mbedtls/src/x509_crt.c +++ b/features/mbedtls/src/x509_crt.c @@ -969,8 +969,8 @@ int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *bu */ int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen ) { - int success = 0, first_error = 0, total_failed = 0; #if defined(MBEDTLS_PEM_PARSE_C) + int success = 0, first_error = 0, total_failed = 0; int buf_format = MBEDTLS_X509_FORMAT_DER; #endif @@ -1122,7 +1122,7 @@ int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path ) p = filename + len; filename[len++] = '*'; - w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir, + w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir, MAX_PATH - 3 ); if( w_ret == 0 ) return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); @@ -1904,6 +1904,7 @@ static int x509_crt_verify_top( int check_path_cnt; unsigned char hash[MBEDTLS_MD_MAX_SIZE]; const mbedtls_md_info_t *md_info; + mbedtls_x509_crt *future_past_ca = NULL; if( mbedtls_x509_time_is_past( &child->valid_to ) ) *flags |= MBEDTLS_X509_BADCERT_EXPIRED; @@ -1958,16 +1959,6 @@ static int x509_crt_verify_top( continue; } - if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) ) - { - continue; - } - - if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) ) - { - continue; - } - if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk, child->sig_md, hash, mbedtls_md_get_size( md_info ), child->sig.p, child->sig.len ) != 0 ) @@ -1975,6 +1966,20 @@ static int x509_crt_verify_top( continue; } + if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) || + mbedtls_x509_time_is_future( &trust_ca->valid_from ) ) + { + if ( future_past_ca == NULL ) + future_past_ca = trust_ca; + + continue; + } + + break; + } + + if( trust_ca != NULL || ( trust_ca = future_past_ca ) != NULL ) + { /* * Top of chain is signed by a trusted CA */ @@ -1982,8 +1987,6 @@ static int x509_crt_verify_top( if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 ) *flags |= MBEDTLS_X509_BADCERT_BAD_KEY; - - break; } /* @@ -2003,6 +2006,12 @@ static int x509_crt_verify_top( ((void) ca_crl); #endif + if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) ) + ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED; + + if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) ) + ca_flags |= MBEDTLS_X509_BADCERT_FUTURE; + if( NULL != f_vrfy ) { if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, diff --git a/features/mbedtls/src/x509_csr.c b/features/mbedtls/src/x509_csr.c index 603d06b64f..f92b66c58f 100644 --- a/features/mbedtls/src/x509_csr.c +++ b/features/mbedtls/src/x509_csr.c @@ -265,8 +265,8 @@ int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr, */ int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen ) { - int ret; #if defined(MBEDTLS_PEM_PARSE_C) + int ret; size_t use_len; mbedtls_pem_context pem; #endif From 80fbde313c05360f5b0f049d0fa84da04d3797ff Mon Sep 17 00:00:00 2001 From: Mohammad Azim Khan Date: Tue, 7 Mar 2017 13:59:00 +0000 Subject: [PATCH 35/40] Fix heap size for NUCLEO_F746ZG on IAR --- .../TARGET_STM32F746xG/device/TOOLCHAIN_IAR/stm32f746xg.icf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/device/TOOLCHAIN_IAR/stm32f746xg.icf b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/device/TOOLCHAIN_IAR/stm32f746xg.icf index 2982e0ddc7..99918b9898 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/device/TOOLCHAIN_IAR/stm32f746xg.icf +++ b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/device/TOOLCHAIN_IAR/stm32f746xg.icf @@ -19,9 +19,9 @@ define region RAM_region = mem:[from __region_RAM_start__ to __region_RAM_end__] define region ITCMRAM_region = mem:[from __region_ITCMRAM_start__ to __region_ITCMRAM_end__]; /* Stack and Heap */ -/*Heap 1/4 of ram and stack 1/8*/ +/*Heap 1/4 of ram and stack 1/12 */ define symbol __size_cstack__ = 0x4000; -define symbol __size_heap__ = 0x8000; +define symbol __size_heap__ = 0x13000; define block CSTACK with alignment = 8, size = __size_cstack__ { }; define block HEAP with alignment = 8, size = __size_heap__ { }; define block STACKHEAP with fixed order { block HEAP, block CSTACK }; From 44a515dd472d34d167a4b1522a1d6205065ddd1b Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Wed, 1 Mar 2017 15:42:52 -0600 Subject: [PATCH 36/40] Fixed size_t issue for mbed 2 builds --- platform/mbed_retarget.h | 1 + 1 file changed, 1 insertion(+) diff --git a/platform/mbed_retarget.h b/platform/mbed_retarget.h index dd27ff0d51..8d3b74cebf 100644 --- a/platform/mbed_retarget.h +++ b/platform/mbed_retarget.h @@ -20,6 +20,7 @@ #define RETARGET_H #include +#include /* We can get the following standard types from sys/types for gcc, but we * need to define the types ourselves for the other compilers that normally From 745b8effd2ceea5cc0b96ff4bd51aa64228189b1 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Thu, 9 Mar 2017 12:03:14 -0600 Subject: [PATCH 37/40] Filesystem: Restored LocalFileSystem tests for mbed 2 --- drivers/FileSystemLike.h | 2 +- features/unsupported/tests/mbed/file/main.cpp | 78 +++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 features/unsupported/tests/mbed/file/main.cpp diff --git a/drivers/FileSystemLike.h b/drivers/FileSystemLike.h index c08b8c94eb..47ece539d9 100644 --- a/drivers/FileSystemLike.h +++ b/drivers/FileSystemLike.h @@ -114,7 +114,7 @@ public: * 0 on success or un-needed, * -1 on error */ - virtual int stat(const char *name, struct stat *st) = 0; + virtual int stat(const char *name, struct stat *st) { return -1; }; }; } // namespace mbed diff --git a/features/unsupported/tests/mbed/file/main.cpp b/features/unsupported/tests/mbed/file/main.cpp new file mode 100644 index 0000000000..8c85711f96 --- /dev/null +++ b/features/unsupported/tests/mbed/file/main.cpp @@ -0,0 +1,78 @@ +#include "test_env.h" +#include "mbed_semihost_api.h" + +Serial pc(USBTX, USBRX); + +#define FILENAME "/local/out.txt" +#define TEST_STRING "Hello World!" + +FILE *test_open(const char *mode) { + FILE *f = fopen(FILENAME, mode); + if (f == NULL) { + printf("Error opening file"NL); + notify_completion(false); + } + return f; +} + +void test_write(FILE *f, char *str, int str_len) { + int n = fprintf(f, str); + + if (n != str_len) { + printf("Error writing file"NL); + notify_completion(false); + } +} + +void test_read(FILE *f, char *str, int str_len) { + int n = fread(str, sizeof(unsigned char), str_len, f); + + if (n != str_len) { + printf("Error reading file"NL); + notify_completion(false); + } +} + +void test_close(FILE *f) { + int rc = fclose(f); + + if (rc != 0) { + printf("Error closing file"NL); + notify_completion(false); + } +} + +int main() { + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(default_auto); + MBED_HOSTTEST_DESCRIPTION(Semihost file system); + MBED_HOSTTEST_START("MBED_A2"); + + pc.printf("Test the Stream class\n"); + + printf("connected: %s\n", (semihost_connected()) ? ("Yes") : ("No")); + + char mac[16]; + mbed_mac_address(mac); + printf("mac address: %02x,%02x,%02x,%02x,%02x,%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + + LocalFileSystem local("local"); + + FILE *f; + char *str = TEST_STRING; + char *buffer = (char *)malloc(sizeof(unsigned char) * strlen(TEST_STRING)); + int str_len = strlen(TEST_STRING); + + // Write + f = test_open("w"); + test_write(f, str, str_len); + test_close(f); + + // Read + f = test_open("r"); + test_read(f, buffer, str_len); + test_close(f); + + // Check the two strings are equal + MBED_HOSTTEST_RESULT((strncmp(buffer, str, str_len) == 0)); +} From f6388de8f42edde92a4f183343741b2fe40e889d Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Fri, 10 Mar 2017 11:08:56 -0600 Subject: [PATCH 38/40] Fix toolchain path names in Mbed 2 builds I accidentally broke only uARM mbed 2 builds. Here is the story: When scanning for resources, toolchains look for any `TOOLCHAIN_` folders to include. These ``s mostly match the name passed in on the command line with one exception: `uARM` on the command line maps to `ARM_MICRO` the class. This would not be a problem except for the bug that I introduced in a prior commit. The bug is that the mbed2 library builds will use the name passed in on the command line to construct `TOOLCHAIN_`. This will not match when scanning. I fixed it by translating the `` into the ``. --- tools/build_api.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/build_api.py b/tools/build_api.py index 36d944bfcc..865971452c 100644 --- a/tools/build_api.py +++ b/tools/build_api.py @@ -732,7 +732,8 @@ def build_library(src_paths, build_path, target, toolchain_name, ###################### def mbed2_obj_path(target_name, toolchain_name): - return join("TARGET_" + target_name, "TOOLCHAIN_" + toolchain_name) + real_tc_name = TOOLCHAIN_CLASSES[toolchain_name].__name__ + return join("TARGET_" + target_name, "TOOLCHAIN_" + real_tc_name) def build_lib(lib_id, target, toolchain_name, verbose=False, clean=False, macros=None, notify=None, jobs=1, silent=False, @@ -955,7 +956,7 @@ def build_mbed_libs(target, toolchain_name, verbose=False, try: # Source and Build Paths build_target = join(MBED_LIBRARIES, "TARGET_" + target.name) - build_toolchain = join(build_target, "TOOLCHAIN_" + toolchain_name) + build_toolchain = join(MBED_LIBRARIES, mbed2_obj_path(target.name, toolchain_name)) mkdir(build_toolchain) # Toolchain From c76d8ffdfcb440f0914be552e2b9c6dfbe9e2527 Mon Sep 17 00:00:00 2001 From: Anna Bridge Date: Mon, 13 Mar 2017 11:14:55 +0000 Subject: [PATCH 39/40] Update MBED_LIBRARY_VERSION to v138 --- mbed.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mbed.h b/mbed.h index d9764740be..4f6d1b1c68 100644 --- a/mbed.h +++ b/mbed.h @@ -22,7 +22,7 @@ // RTOS present, this is valid only for mbed OS 5 #define MBED_MAJOR_VERSION 5 #define MBED_MINOR_VERSION 4 -#define MBED_PATCH_VERSION 0 +#define MBED_PATCH_VERSION 1 #else // mbed 2 From c0351446cf28746fb40f187d8a88c34392ff54f7 Mon Sep 17 00:00:00 2001 From: Simon Vogl Date: Thu, 23 Feb 2017 09:26:45 +0100 Subject: [PATCH 40/40] can_write(): return error code when no tx mailboxes are available. --- targets/TARGET_STM/can_api.c | 48 +++++++++++++++++------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index 7a1ca7c58f..8b6340fedf 100644 --- a/targets/TARGET_STM/can_api.c +++ b/targets/TARGET_STM/can_api.c @@ -207,7 +207,7 @@ int can_frequency(can_t *obj, int f) int can_write(can_t *obj, CAN_Message msg, int cc) { - uint32_t transmitmailbox = 5; + uint32_t transmitmailbox = CAN_TXSTATUS_NOMAILBOX; CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); /* Select one empty transmit mailbox */ @@ -218,33 +218,31 @@ int can_write(can_t *obj, CAN_Message msg, int cc) } else if ((can->TSR & CAN_TSR_TME2) == CAN_TSR_TME2) { transmitmailbox = 2; } else { - transmitmailbox = CAN_TXSTATUS_NOMAILBOX; + return 0; } - if (transmitmailbox != CAN_TXSTATUS_NOMAILBOX) { - can->sTxMailBox[transmitmailbox].TIR &= CAN_TI0R_TXRQ; - if (!(msg.format)) { - can->sTxMailBox[transmitmailbox].TIR |= ((msg.id << 21) | msg.type); - } else { - can->sTxMailBox[transmitmailbox].TIR |= ((msg.id << 3) | CAN_ID_EXT | msg.type); - } - - /* Set up the DLC */ - can->sTxMailBox[transmitmailbox].TDTR &= (uint32_t)0xFFFFFFF0; - can->sTxMailBox[transmitmailbox].TDTR |= (msg.len & (uint8_t)0x0000000F); - - /* Set up the data field */ - can->sTxMailBox[transmitmailbox].TDLR = (((uint32_t)msg.data[3] << 24) | - ((uint32_t)msg.data[2] << 16) | - ((uint32_t)msg.data[1] << 8) | - ((uint32_t)msg.data[0])); - can->sTxMailBox[transmitmailbox].TDHR = (((uint32_t)msg.data[7] << 24) | - ((uint32_t)msg.data[6] << 16) | - ((uint32_t)msg.data[5] << 8) | + can->sTxMailBox[transmitmailbox].TIR &= CAN_TI0R_TXRQ; + if (!(msg.format)) { + can->sTxMailBox[transmitmailbox].TIR |= ((msg.id << 21) | msg.type); + } else { + can->sTxMailBox[transmitmailbox].TIR |= ((msg.id << 3) | CAN_ID_EXT | msg.type); + } + + /* Set up the DLC */ + can->sTxMailBox[transmitmailbox].TDTR &= (uint32_t)0xFFFFFFF0; + can->sTxMailBox[transmitmailbox].TDTR |= (msg.len & (uint8_t)0x0000000F); + + /* Set up the data field */ + can->sTxMailBox[transmitmailbox].TDLR = (((uint32_t)msg.data[3] << 24) | + ((uint32_t)msg.data[2] << 16) | + ((uint32_t)msg.data[1] << 8) | + ((uint32_t)msg.data[0])); + can->sTxMailBox[transmitmailbox].TDHR = (((uint32_t)msg.data[7] << 24) | + ((uint32_t)msg.data[6] << 16) | + ((uint32_t)msg.data[5] << 8) | ((uint32_t)msg.data[4])); - /* Request transmission */ - can->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ; - } + /* Request transmission */ + can->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ; return 1; }