mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Files at this revision

API Documentation at this revision

Comitter:
<>
Date:
Tue Mar 14 16:40:56 2017 +0000
Parent:
159:612c381a210f
Child:
161:2cc1468da177
Commit message:
This updates the lib to the mbed lib v138

Changed in this revision

cmsis/core_cmSecureAccess.h Show annotated file Show diff for this revision Revisions of this file
drivers/DigitalIn.h Show annotated file Show diff for this revision Revisions of this file
drivers/DigitalInOut.h Show annotated file Show diff for this revision Revisions of this file
drivers/DigitalOut.h Show annotated file Show diff for this revision Revisions of this file
drivers/DirHandle.h Show annotated file Show diff for this revision Revisions of this file
drivers/FileBase.cpp Show annotated file Show diff for this revision Revisions of this file
drivers/FileBase.h Show annotated file Show diff for this revision Revisions of this file
drivers/FileHandle.h Show annotated file Show diff for this revision Revisions of this file
drivers/FileLike.cpp Show diff for this revision Revisions of this file
drivers/FileLike.h Show annotated file Show diff for this revision Revisions of this file
drivers/FilePath.cpp Show annotated file Show diff for this revision Revisions of this file
drivers/FilePath.h Show annotated file Show diff for this revision Revisions of this file
drivers/FileSystemLike.cpp Show annotated file Show diff for this revision Revisions of this file
drivers/FileSystemLike.h Show annotated file Show diff for this revision Revisions of this file
drivers/FlashIAP.cpp Show annotated file Show diff for this revision Revisions of this file
drivers/FlashIAP.h Show annotated file Show diff for this revision Revisions of this file
drivers/InterruptIn.cpp Show annotated file Show diff for this revision Revisions of this file
drivers/InterruptIn.h Show annotated file Show diff for this revision Revisions of this file
drivers/InterruptManager.cpp Show annotated file Show diff for this revision Revisions of this file
drivers/LocalFileSystem.cpp Show annotated file Show diff for this revision Revisions of this file
drivers/PortIn.h Show annotated file Show diff for this revision Revisions of this file
drivers/PortInOut.h Show annotated file Show diff for this revision Revisions of this file
drivers/PortOut.h Show annotated file Show diff for this revision Revisions of this file
drivers/PwmOut.h Show annotated file Show diff for this revision Revisions of this file
drivers/RawSerial.cpp Show annotated file Show diff for this revision Revisions of this file
drivers/SPI.cpp Show annotated file Show diff for this revision Revisions of this file
drivers/Serial.cpp Show annotated file Show diff for this revision Revisions of this file
drivers/SerialBase.cpp Show annotated file Show diff for this revision Revisions of this file
drivers/SerialBase.h Show annotated file Show diff for this revision Revisions of this file
drivers/Stream.cpp Show annotated file Show diff for this revision Revisions of this file
drivers/Stream.h Show annotated file Show diff for this revision Revisions of this file
drivers/Ticker.cpp Show annotated file Show diff for this revision Revisions of this file
drivers/Ticker.h Show annotated file Show diff for this revision Revisions of this file
drivers/Timer.cpp Show annotated file Show diff for this revision Revisions of this file
hal/TARGET_FLASH_CMSIS_ALGO/flash_common_algo.c Show annotated file Show diff for this revision Revisions of this file
hal/TARGET_FLASH_CMSIS_ALGO/flash_data.h Show annotated file Show diff for this revision Revisions of this file
hal/flash_api.h Show annotated file Show diff for this revision Revisions of this file
hal/mbed_ticker_api.c Show annotated file Show diff for this revision Revisions of this file
hal/sleep_api.h Show annotated file Show diff for this revision Revisions of this file
mbed.h Show annotated file Show diff for this revision Revisions of this file
platform/CallChain.cpp Show annotated file Show diff for this revision Revisions of this file
platform/CallChain.h Show annotated file Show diff for this revision Revisions of this file
platform/Callback.h Show annotated file Show diff for this revision Revisions of this file
platform/CircularBuffer.h Show annotated file Show diff for this revision Revisions of this file
platform/FunctionPointer.h Show annotated file Show diff for this revision Revisions of this file
platform/critical.h Show annotated file Show diff for this revision Revisions of this file
platform/mbed_alloc_wrappers.cpp Show annotated file Show diff for this revision Revisions of this file
platform/mbed_application.c Show annotated file Show diff for this revision Revisions of this file
platform/mbed_application.h Show annotated file Show diff for this revision Revisions of this file
platform/mbed_assert.c Show annotated file Show diff for this revision Revisions of this file
platform/mbed_board.c Show annotated file Show diff for this revision Revisions of this file
platform/mbed_critical.c Show annotated file Show diff for this revision Revisions of this file
platform/mbed_critical.h Show annotated file Show diff for this revision Revisions of this file
platform/mbed_error.c Show annotated file Show diff for this revision Revisions of this file
platform/mbed_interface.c Show annotated file Show diff for this revision Revisions of this file
platform/mbed_mem_trace.c Show annotated file Show diff for this revision Revisions of this file
platform/mbed_retarget.cpp Show annotated file Show diff for this revision Revisions of this file
platform/mbed_retarget.h Show annotated file Show diff for this revision Revisions of this file
platform/mbed_rtc_time.cpp Show annotated file Show diff for this revision Revisions of this file
platform/mbed_rtc_time.h Show annotated file Show diff for this revision Revisions of this file
platform/mbed_semihost_api.c Show annotated file Show diff for this revision Revisions of this file
platform/mbed_semihost_api.h Show annotated file Show diff for this revision Revisions of this file
platform/mbed_sleep.h Show annotated file Show diff for this revision Revisions of this file
platform/mbed_toolchain.h Show annotated file Show diff for this revision Revisions of this file
platform/mbed_wait_api.h Show annotated file Show diff for this revision Revisions of this file
platform/mbed_wait_api_no_rtos.c Show annotated file Show diff for this revision Revisions of this file
platform/mbed_wait_api_rtos.cpp Show annotated file Show diff for this revision Revisions of this file
platform/platform.h Show annotated file Show diff for this revision Revisions of this file
platform/retarget.cpp Show diff for this revision Revisions of this file
platform/rtc_time.h Show annotated file Show diff for this revision Revisions of this file
platform/semihost_api.h Show annotated file Show diff for this revision Revisions of this file
platform/sleep.h Show annotated file Show diff for this revision Revisions of this file
platform/toolchain.h Show annotated file Show diff for this revision Revisions of this file
platform/wait_api.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ARM_SSG/TARGET_BEETLE/analogin_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ARM_SSG/TARGET_BEETLE/i2c_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ARM_SSG/TARGET_BEETLE/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ARM_SSG/TARGET_BEETLE/spi_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ARM_SSG/TARGET_IOTSS/SDK/ETH_MPS2.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ARM_SSG/TARGET_IOTSS/SDK/mps2_ethernet_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ARM_SSG/TARGET_IOTSS/ethernet_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ARM_SSG/TARGET_IOTSS/i2c_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ARM_SSG/TARGET_IOTSS/spi_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ARM_SSG/TARGET_MPS2/SDK/ETH_MPS2.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ARM_SSG/TARGET_MPS2/SDK/mps2_ethernet_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ARM_SSG/TARGET_MPS2/ethernet_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ARM_SSG/TARGET_MPS2/i2c_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ARM_SSG/TARGET_MPS2/spi_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Atmel/TARGET_SAM_CortexM0P/sleep_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Atmel/TARGET_SAM_CortexM4/drivers/pmc/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Atmel/TARGET_SAM_CortexM4/sleep_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Freescale/TARGET_K20XX/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Freescale/TARGET_KLXX/TARGET_KL46Z/flash_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Freescale/TARGET_KLXX/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/device/TOOLCHAIN_ARM_STD/MK64FN1M0xxx12.sct Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/device/TOOLCHAIN_GCC_ARM/MK64FN1M0xxx12.ld Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/device/TOOLCHAIN_IAR/MK64FN1M0xxx12.icf Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/flash_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/api/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Maxim/TARGET_MAX32600/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Maxim/TARGET_MAX32610/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Maxim/TARGET_MAX32620/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Maxim/TARGET_MAX32625/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NORDIC/TARGET_MCU_NRF51822/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NORDIC/TARGET_NRF5/i2c_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NORDIC/TARGET_NRF5/sdk/drivers_nrf/pstorage/config/pstorage_platform.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NORDIC/TARGET_NRF5/sdk/drivers_nrf/twi_master/nrf_drv_twi.c Show diff for this revision Revisions of this file
targets/TARGET_NORDIC/TARGET_NRF5/sdk/drivers_nrf/twi_master/nrf_drv_twi.h Show diff for this revision Revisions of this file
targets/TARGET_NORDIC/TARGET_NRF5/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NORDIC/TARGET_NRF5/us_ticker.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_M451/can_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_M451/i2c_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_M451/lp_ticker.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_M451/rtc_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_M451/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_M451/us_ticker.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/can_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/crypto/aes/aes_alt.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/crypto/des/des_alt.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_ARM_MICRO/NUC472.sct Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_ARM_MICRO/TARGET_NU_XRAM_SUPPORTED/NUC472.sct Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_ARM_MICRO/TARGET_NU_XRAM_UNSUPPORTED/NUC472.sct Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_ARM_STD/NUC472.sct Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_ARM_STD/TARGET_NU_XRAM_SUPPORTED/NUC472.sct Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_ARM_STD/TARGET_NU_XRAM_UNSUPPORTED/NUC472.sct Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_GCC_ARM/NUC472.ld Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_GCC_ARM/TARGET_NU_XRAM_SUPPORTED/NUC472.ld Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_GCC_ARM/TARGET_NU_XRAM_UNSUPPORTED/NUC472.ld Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_IAR/NUC472_442.icf Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_IAR/TARGET_NU_XRAM_SUPPORTED/NUC472_442.icf Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_IAR/TARGET_NU_XRAM_UNSUPPORTED/NUC472_442.icf Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/device/system_NUC472_442.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/i2c_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/lp_ticker.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/rtc_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NUVOTON/TARGET_NUC472/us_ticker.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC11U6X/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC11UXX/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC11XX_11CXX/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC13XX/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC15XX/us_ticker.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC176X/TARGET_UBLOX_C027/C027_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC176X/ethernet_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC176X/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC23XX/ethernet_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC2460/ethernet_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088/ethernet_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088_DM/ethernet_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC408X/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC43XX/ethernet_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC43XX/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC81X/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_NXP/TARGET_LPC82X/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ONSEMI/TARGET_NCS36510/fncs36510_sleep.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ONSEMI/TARGET_NCS36510/i2c_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ONSEMI/TARGET_NCS36510/ncs36510_i2c.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ONSEMI/TARGET_NCS36510/ncs36510_lp_ticker_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ONSEMI/TARGET_NCS36510/ncs36510_trng_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ONSEMI/TARGET_NCS36510/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ONSEMI/TARGET_NCS36510/sleep.h Show diff for this revision Revisions of this file
targets/TARGET_ONSEMI/TARGET_NCS36510/sleep_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_RENESAS/TARGET_RZ_A1H/ethernet_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_RENESAS/TARGET_VK_RZ_A1H/ethernet_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/PinNamesTypes.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F0/analogin_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F0/can_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F0/can_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F0/gpio_irq_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F0/gpio_irq_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F0/gpio_irq_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F0/pin_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F0/pinmap.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F0/port_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F0/pwmout_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F0/pwmout_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F1/TARGET_BLUEPILL_F103C8/PeripheralPins.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB/PeripheralPins.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F1/analogin_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F1/can_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F1/can_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F1/gpio_irq_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F1/gpio_irq_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F1/gpio_irq_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F1/pin_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F1/pinmap.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F1/port_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F1/pwmout_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F1/pwmout_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F2/analogin_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F2/can_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F2/can_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F2/gpio_irq_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F2/gpio_irq_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F2/gpio_irq_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F2/objects.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F2/pin_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F2/pinmap.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F2/port_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F2/pwmout_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F2/pwmout_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F3/analogin_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F3/can_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F3/can_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F3/gpio_irq_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F3/gpio_irq_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F3/gpio_irq_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F3/pin_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F3/pinmap.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F3/port_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F3/pwmout_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F3/pwmout_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_DISCO_F429ZI/system_stm32f4xx.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_NUCLEO_F429ZI/system_stm32f4xx.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_MICRO/startup_stm32f429xx.s Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_MICRO/stm32f429xx.sct Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_STD/startup_stm32f429xx.s Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_STD/stm32f429xx.sct Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_GCC_ARM/STM32F429xI.ld Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_IAR/startup_stm32f429xx.S Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_IAR/stm32f429xx_flash.icf Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/cmsis_nvic.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/flash_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/nvic_addr.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/system_init_pre.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_NUCLEO_F439ZI/system_stm32f4xx.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/device/cmsis_nvic.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/device/flash_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/device/nvic_addr.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/TARGET_NUCLEO_F446ZE/PeripheralPins.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/TOOLCHAIN_ARM_MICRO/stm32f439xx.sct Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/TOOLCHAIN_ARM_STD/stm32f439xx.sct Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/TOOLCHAIN_GCC_ARM/STM32F439ZI.ld Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/TOOLCHAIN_IAR/stm32f439xx_flash.icf Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/cmsis_nvic.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/nvic_addr.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/system_stm32f4xx.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/flash_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/analogin_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/can_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/can_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/gpio_irq_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/gpio_irq_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/gpio_irq_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/pin_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/pinmap.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/port_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/pwmout_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/pwmout_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/device/TOOLCHAIN_IAR/stm32f746xg.icf Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F7/analogin_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F7/can_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F7/can_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F7/gpio_irq_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F7/gpio_irq_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F7/gpio_irq_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F7/pin_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F7/pinmap.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F7/port_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F7/pwmout_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F7/pwmout_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L0/analogin_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L0/gpio_irq_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L0/gpio_irq_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L0/gpio_irq_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L0/pin_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L0/pinmap.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L0/port_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L0/pwmout_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L0/pwmout_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/PeripheralPins.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/PeripheralPins.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/PeripheralPins.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/objects.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L1/analogin_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L1/common_objects.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L1/gpio_irq_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L1/gpio_irq_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L1/gpio_irq_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L1/pin_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L1/pinmap.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L1/port_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L1/pwmout_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_DISCO_L476VG/system_stm32l4xx.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_NUCLEO_L476RG/system_stm32l4xx.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L4/analogin_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L4/can_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L4/can_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L4/gpio_irq_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L4/gpio_irq_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L4/gpio_irq_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L4/pin_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L4/pinmap.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L4/port_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L4/pwmout_api.c Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L4/pwmout_device.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/can_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/gpio_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/gpio_irq_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/gpio_object.h Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/hal_tick_32b.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/i2c_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/pinmap.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/port_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/pwmout_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/rtc_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_STM/stm_spi_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Silicon_Labs/TARGET_EFM32/lp_ticker.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_Silicon_Labs/TARGET_EFM32/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_WIZNET/TARGET_W7500x/analogin_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_WIZNET/TARGET_W7500x/i2c_api.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_WIZNET/TARGET_W7500x/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ublox/TARGET_HI2110/lp_ticker.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ublox/TARGET_HI2110/sleep.c Show annotated file Show diff for this revision Revisions of this file
targets/TARGET_ublox/TARGET_HI2110/us_ticker.c Show annotated file Show diff for this revision Revisions of this file
targets/targets.json Show annotated file Show diff for this revision Revisions of this file
--- a/cmsis/core_cmSecureAccess.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/cmsis/core_cmSecureAccess.h	Tue Mar 14 16:40:56 2017 +0000
@@ -53,7 +53,7 @@
  * @param value[in]    Value to write at the address location.
  */
 #define SECURE_WRITE(address, value) \
-    uvisor_write(main, UVISOR_RGW_SHARED, address, value, UVISOR_RGW_OP_WRITE, 0xFFFFFFFFUL)
+    uvisor_write(public_box, UVISOR_RGW_SHARED, address, value, UVISOR_RGW_OP_WRITE, 0xFFFFFFFFUL)
 
 /** Get the value at the target address.
  *
@@ -61,7 +61,7 @@
  * @returns The value `*address`.
  */
 #define SECURE_READ(address) \
-    uvisor_read(main, UVISOR_RGW_SHARED, address, UVISOR_RGW_OP_READ, 0xFFFFFFFFUL)
+    uvisor_read(public_box, UVISOR_RGW_SHARED, address, UVISOR_RGW_OP_READ, 0xFFFFFFFFUL)
 
 /** Get the selected bits at the target address.
  *
@@ -70,7 +70,7 @@
  * @returns The value `*address & mask`.
  */
 #define SECURE_BITS_GET(address, mask) \
-    UVISOR_BITS_GET(main, UVISOR_RGW_SHARED, address, mask)
+    UVISOR_BITS_GET(public_box, UVISOR_RGW_SHARED, address, mask)
 
 /** Check the selected bits at the target address.
  *
@@ -79,7 +79,7 @@
  * @returns The value `((*address & mask) == mask)`.
  */
 #define SECURE_BITS_CHECK(address, mask) \
-    UVISOR_BITS_CHECK(main, UVISOR_RGW_SHARED, address, mask)
+    UVISOR_BITS_CHECK(public_box, UVISOR_RGW_SHARED, address, mask)
 
 /** Set the selected bits to 1 at the target address.
  *
@@ -88,7 +88,7 @@
  * @param mask[in]     Bits to select out of the target address
  */
 #define SECURE_BITS_SET(address, mask) \
-    UVISOR_BITS_SET(main, UVISOR_RGW_SHARED, address, mask)
+    UVISOR_BITS_SET(public_box, UVISOR_RGW_SHARED, address, mask)
 
 /** Clear the selected bits at the target address.
  *
@@ -97,7 +97,7 @@
  * @param mask[in]     Bits to select out of the target address
  */
 #define SECURE_BITS_CLEAR(address, mask) \
-    UVISOR_BITS_CLEAR(main, UVISOR_RGW_SHARED, address, mask)
+    UVISOR_BITS_CLEAR(public_box, UVISOR_RGW_SHARED, address, mask)
 
 /** Set the selected bits at the target address to the given value.
  *
@@ -108,7 +108,7 @@
  *                     must be already shifted to the correct bit position
  */
 #define SECURE_BITS_SET_VALUE(address, mask, value) \
-    UVISOR_BITS_SET_VALUE(main, UVISOR_RGW_SHARED, address, mask, value)
+    UVISOR_BITS_SET_VALUE(public_box, UVISOR_RGW_SHARED, address, mask, value)
 
 /** Toggle the selected bits at the target address.
  *
@@ -117,7 +117,7 @@
  * @param mask[in]     Bits to select out of the target address
  */
 #define SECURE_BITS_TOGGLE(address, mask) \
-    UVISOR_BITS_TOGGLE(main, UVISOR_RGW_SHARED, address, mask)
+    UVISOR_BITS_TOGGLE(public_box, UVISOR_RGW_SHARED, address, mask)
 
 #else
 
--- a/drivers/DigitalIn.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/DigitalIn.h	Tue Mar 14 16:40:56 2017 +0000
@@ -19,7 +19,7 @@
 #include "platform/platform.h"
 
 #include "hal/gpio_api.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 namespace mbed {
 /** \addtogroup drivers */
--- a/drivers/DigitalInOut.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/DigitalInOut.h	Tue Mar 14 16:40:56 2017 +0000
@@ -19,7 +19,7 @@
 #include "platform/platform.h"
 
 #include "hal/gpio_api.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 namespace mbed {
 /** \addtogroup drivers */
--- a/drivers/DigitalOut.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/DigitalOut.h	Tue Mar 14 16:40:56 2017 +0000
@@ -18,7 +18,7 @@
 
 #include "platform/platform.h"
 #include "hal/gpio_api.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 namespace mbed {
 /** \addtogroup drivers */
--- a/drivers/DirHandle.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/DirHandle.h	Tue Mar 14 16:40:56 2017 +0000
@@ -16,20 +16,11 @@
 #ifndef MBED_DIRHANDLE_H
 #define MBED_DIRHANDLE_H
 
-#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
-#   define NAME_MAX 255
-typedef int mode_t;
-
-#else
-#   include <sys/syslimits.h>
-#endif
+#include <stdint.h>
+#include "platform/platform.h"
 
 #include "FileHandle.h"
 
-struct dirent {
-    char d_name[NAME_MAX+1];
-};
-
 namespace mbed {
 /** \addtogroup drivers */
 /** @{*/
@@ -50,8 +41,12 @@
  *  @Note Synchronization level: Set by subclass
  */
 class DirHandle {
+public:
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
+        "Replaced by File")
+    DirHandle() {}
 
-public:
     /** Closes the directory.
      *
      *  @returns
@@ -103,22 +98,18 @@
     virtual void unlock() {
         // Stub
     }
+
+protected:
+    /** Internal-only constructor to work around deprecated notices when not used
+     *. due to nested deprecations and difficulty of compilers finding their way around
+     *  the class hierarchy
+     */
+    friend class FileSystemLike;
+    DirHandle(int) {}
 };
 
 } // namespace mbed
 
-typedef mbed::DirHandle DIR;
-
-extern "C" {
-    DIR *opendir(const char*);
-    struct dirent *readdir(DIR *);
-    int closedir(DIR*);
-    void rewinddir(DIR*);
-    long telldir(DIR*);
-    void seekdir(DIR*, long);
-    int mkdir(const char *name, mode_t n);
-};
-
 #endif /* MBED_DIRHANDLE_H */
 
 /** @}*/
--- a/drivers/FileBase.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/FileBase.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 #include "drivers/FileBase.h"
+#include "drivers/FileLike.h"
 
 namespace mbed {
 
@@ -49,6 +50,11 @@
         }
     }
     _mutex->unlock();
+
+    if (getPathType() == FilePathType) {
+        extern void remove_filehandle(FileLike *file);
+        remove_filehandle(static_cast<FileLike*>(this));
+    }
 }
 
 FileBase *FileBase::lookup(const char *name, unsigned int len) {
--- a/drivers/FileBase.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/FileBase.h	Tue Mar 14 16:40:56 2017 +0000
@@ -21,26 +21,6 @@
 #include <cstdio>
 #include <cstring>
 
-#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
-#    define O_RDONLY 0
-#    define O_WRONLY 1
-#    define O_RDWR   2
-#    define O_CREAT  0x0200
-#    define O_TRUNC  0x0400
-#    define O_APPEND 0x0008
-
-#    define NAME_MAX 255
-
-typedef int mode_t;
-typedef int ssize_t;
-typedef long off_t;
-
-#else
-#    include <sys/fcntl.h>
-#    include <sys/types.h>
-#    include <sys/syslimits.h>
-#endif
-
 #include "platform/platform.h"
 #include "platform/SingletonPtr.h"
 #include "platform/PlatformMutex.h"
@@ -57,7 +37,6 @@
 class FileBase {
 public:
     FileBase(const char *name, PathType t);
-
     virtual ~FileBase();
 
     const char* getName(void);
--- a/drivers/FileHandle.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/FileHandle.h	Tue Mar 14 16:40:56 2017 +0000
@@ -19,14 +19,7 @@
 typedef int FILEHANDLE;
 
 #include <stdio.h>
-
-#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
-typedef int ssize_t;
-typedef long off_t;
-
-#else
-#   include <sys/types.h>
-#endif
+#include "platform/platform.h"
 
 namespace mbed {
 /** \addtogroup drivers */
@@ -46,6 +39,11 @@
 class FileHandle {
 
 public:
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
+        "Replaced by File")
+    FileHandle() {}
+
     /** Write the contents of a buffer to the file
      *
      *  @param buffer the buffer to write from
@@ -120,7 +118,7 @@
         return res;
     }
 
-    virtual ~FileHandle();
+    virtual ~FileHandle() {};
 
 protected:
 
--- a/drivers/FileLike.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,28 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "drivers/FileLike.h"
-
-namespace mbed {
-
-FileLike::FileLike(const char *name) : FileHandle(), FileBase(name, FilePathType) {
-
-}
-
-FileLike::~FileLike() {
-
-}
-
-} // namespace mbed
--- a/drivers/FileLike.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/FileLike.h	Tue Mar 14 16:40:56 2017 +0000
@@ -16,36 +16,139 @@
 #ifndef MBED_FILELIKE_H
 #define MBED_FILELIKE_H
 
+#include "platform/mbed_toolchain.h"
 #include "drivers/FileBase.h"
-#include "drivers/FileHandle.h"
 
 namespace mbed {
 /** \addtogroup drivers */
 /** @{*/
 
+
 /* Class FileLike
  *  A file-like object is one that can be opened with fopen by
- *  fopen("/name", mode). It is intersection of the classes Base and
- *  FileHandle.
+ *  fopen("/name", mode).
  *
  *  @Note Synchronization level: Set by subclass
  */
-class FileLike : public FileHandle, public FileBase {
+class FileLike : public FileBase {
+public:
+    /** Constructor FileLike
+     *
+     *  @param name     The name to use to open the file.
+     */
+    FileLike(const char *name = NULL) : FileBase(name, FilePathType) {}
+    virtual ~FileLike() {}
+
+    /** Read the contents of a file into a buffer
+     *
+     *  @param buffer   The buffer to read in to
+     *  @param size     The number of bytes to read
+     *  @return         The number of bytes read, 0 at end of file, negative error on failure
+     */
+    virtual ssize_t read(void *buffer, size_t len) = 0;
+
+    /** Write the contents of a buffer to a file
+     *
+     *  @param buffer   The buffer to write from
+     *  @param size     The number of bytes to write 
+     *  @return         The number of bytes written, negative error on failure
+     */
+    virtual ssize_t write(const void *buffer, size_t len) = 0;
 
-public:
-    /* Constructor FileLike
+    /** Close a file
+     *
+     *  @return         0 on success, negative error code on failure
+     */
+    virtual int close() = 0;
+
+    /** Flush any buffers associated with the file
+     *
+     *  @return         0 on success, negative error code on failure
+     */
+    virtual int sync() = 0;
+
+    /** Check if the file in an interactive terminal device
+     *
+     *  @return         True if the file is a terminal
+     */
+    virtual int isatty() = 0;
+
+    /** Move the file position to a given offset from from a given location
+     *
+     *  @param offset   The offset from whence to move to
+     *  @param whence   The start of where to seek
+     *      SEEK_SET to start from beginning of file,
+     *      SEEK_CUR to start from current position in file,
+     *      SEEK_END to start from end of file
+     *  @return         The new offset of the file
+     */
+    virtual off_t seek(off_t offset, int whence = SEEK_SET) = 0;
+
+    /** Get the file position of the file
      *
-     * Variables
-     *  name - The name to use to open the file.
+     *  @return         The current offset in the file
+     */
+    virtual off_t tell() = 0;
+
+    /** Rewind the file position to the beginning of the file
+     *
+     *  @note This is equivalent to file_seek(file, 0, FS_SEEK_SET)
+     */
+    virtual void rewind() = 0;
+
+    /** Get the size of the file
+     *
+     *  @return         Size of the file in bytes
+     */
+    virtual size_t size() = 0;
+
+    /** Move the file position to a given offset from a given location.
+     *
+     *  @param offset The offset from whence to move to
+     *  @param whence SEEK_SET for the start of the file, SEEK_CUR for the
+     *   current file position, or SEEK_END for the end of the file.
+     *
+     *  @returns
+     *    new file position on success,
+     *    -1 on failure or unsupported
      */
-    FileLike(const char *name);
+    MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::seek")
+    virtual off_t lseek(off_t offset, int whence) { return seek(offset, whence); }
+
+    /** Flush any buffers associated with the FileHandle, ensuring it
+     *  is up to date on disk
+     *
+     *  @returns
+     *    0 on success or un-needed,
+     *   -1 on error
+     */
+    MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::sync")
+    virtual int fsync() { return sync(); }
 
-    virtual ~FileLike();
+    /** Find the length of the file
+     *
+     *  @returns
+     *   Length of the file
+     */
+    MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::size")
+    virtual off_t flen() { return size(); }
 
+protected:
+    /** Acquire exclusive access to this object.
+     */
+    virtual void lock() {
+        // Stub
+    }
+
+    /** Release exclusive access to this object.
+     */
+    virtual void unlock() {
+        // Stub
+    }
 };
 
+
+/** @}*/
 } // namespace mbed
 
 #endif
-
-/** @}*/
--- a/drivers/FilePath.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/FilePath.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -49,9 +49,9 @@
     return (fb->getPathType() == FileSystemPathType);
 }
 
-FileSystemLike* FilePath::fileSystem(void) {
+FileSystem* FilePath::fileSystem(void) {
     if (isFileSystem()) {
-        return (FileSystemLike*)fb;
+        return (FileSystem*)fb;
     }
     return NULL;
 }
--- a/drivers/FilePath.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/FilePath.h	Tue Mar 14 16:40:56 2017 +0000
@@ -25,6 +25,8 @@
 /** \addtogroup drivers */
 /** @{*/
 
+class FileSystem;
+
 class FilePath {
 public:
     FilePath(const char* file_path);
@@ -32,7 +34,7 @@
     const char* fileName(void);
 
     bool          isFileSystem(void);
-    FileSystemLike* fileSystem(void);
+    FileSystem* fileSystem(void);
 
     bool    isFile(void);
     FileLike* file(void);
--- a/drivers/FileSystemLike.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/FileSystemLike.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -29,7 +29,7 @@
     off_t n;
     struct dirent cur_entry;
 
-    BaseDirHandle() : n(0), cur_entry() {
+    BaseDirHandle() : DirHandle(0), n(0), cur_entry() {
     }
 
     virtual int closedir() {
--- a/drivers/FileSystemLike.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/FileSystemLike.h	Tue Mar 14 16:40:56 2017 +0000
@@ -41,10 +41,16 @@
      *
      *  @param name The name to use for the filesystem.
      */
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
+        "Replaced by FileSystem")
     FileSystemLike(const char *name);
 
     virtual ~FileSystemLike();
 
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
+        "Replaced by FileSystem")
     static DirHandle *opendir();
     friend class BaseDirHandle;
 
@@ -100,7 +106,15 @@
      */
     virtual int mkdir(const char *name, mode_t mode) { (void) name, (void) mode; return -1; }
 
-    // TODO other filesystem functions (mkdir, rm, rn, ls etc)
+    /** Store information about file in stat structure
+     *
+     *  @param name The name of the file to find information about
+     *  @param st The stat buffer to write to
+     *  @returns
+     *    0 on success or un-needed,
+     *   -1 on error
+     */
+    virtual int stat(const char *name, struct stat *st) { return -1; };
 };
 
 } // namespace mbed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/drivers/FlashIAP.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,167 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2017 ARM Limited
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <string.h>
+#include "FlashIAP.h"
+#include "mbed_assert.h"
+
+
+#ifdef DEVICE_FLASH
+
+namespace mbed {
+
+SingletonPtr<PlatformMutex> FlashIAP::_mutex;
+
+static inline bool is_aligned(uint32_t number, uint32_t alignment)
+{
+    if ((number % alignment) != 0) {
+        return false;
+    } else {
+        return true;
+    }
+}
+
+FlashIAP::FlashIAP()
+{
+
+}
+
+FlashIAP::~FlashIAP()
+{
+
+}
+
+int FlashIAP::init()
+{
+    int ret = 0;
+    _mutex->lock();
+    if (flash_init(&_flash)) {
+        ret = -1; 
+    }
+    _mutex->unlock();
+    return ret;
+}
+
+int FlashIAP::deinit()
+{
+    int ret = 0;
+    _mutex->lock();
+    if (flash_free(&_flash)) {
+        ret = -1; 
+    }
+    _mutex->unlock();
+    return ret;
+}
+
+
+int FlashIAP::read(void *buffer, uint32_t addr, uint32_t size)
+{
+    _mutex->lock();
+    memcpy(buffer, (const void *)addr, size);
+    _mutex->unlock();
+    return 0;
+}
+
+int FlashIAP::program(const void *buffer, uint32_t addr, uint32_t size)
+{
+    uint32_t page_size = get_page_size();
+    uint32_t current_sector_size = flash_get_sector_size(&_flash, addr);
+    // addr and size should be aligned to page size, and multiple of page size
+    // page program should not cross sector boundaries
+    if (!is_aligned(addr, page_size) ||
+        !is_aligned(size, page_size) ||
+        (size < page_size) ||
+        (((addr % current_sector_size) + size) > current_sector_size)) {
+        return -1;
+    }
+
+    int ret = 0;
+    _mutex->lock();
+    if (flash_program_page(&_flash, addr, (const uint8_t *)buffer, size)) {
+        ret = -1;
+    }
+    _mutex->unlock();
+    return ret;
+}
+
+bool FlashIAP::is_aligned_to_sector(uint32_t addr, uint32_t size)
+{
+    uint32_t current_sector_size = flash_get_sector_size(&_flash, addr);
+    if (!is_aligned(size, current_sector_size) ||
+        !is_aligned(addr, current_sector_size)) {
+        return false;
+    } else {
+        return true;
+    }
+}
+
+int FlashIAP::erase(uint32_t addr, uint32_t size)
+{
+    uint32_t current_sector_size = 0UL;
+
+    if (!is_aligned_to_sector(addr, size)) {
+        return -1;
+    }
+
+    int32_t ret = 0;
+    _mutex->lock();
+    while (size) {
+        ret = flash_erase_sector(&_flash, addr);
+        if (ret != 0) {
+            ret = -1;
+            break;
+        }
+        current_sector_size = flash_get_sector_size(&_flash, addr);
+        if (!is_aligned_to_sector(addr, size)) {
+            ret = -1;
+            break;
+        }
+        size -= current_sector_size;
+        addr += current_sector_size;
+    }
+    _mutex->unlock();
+    return ret;
+}
+
+uint32_t FlashIAP::get_page_size() const
+{
+    return flash_get_page_size(&_flash);
+}
+
+uint32_t FlashIAP::get_sector_size(uint32_t addr) const
+{
+    return flash_get_sector_size(&_flash, addr);
+}
+
+uint32_t FlashIAP::get_flash_start() const
+{
+    return flash_get_start_address(&_flash);
+}
+
+uint32_t FlashIAP::get_flash_size() const
+{
+    return flash_get_size(&_flash);
+}
+
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/drivers/FlashIAP.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,138 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2017 ARM Limited
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#ifndef MBED_FLASHIAP_H
+#define MBED_FLASHIAP_H
+
+#ifdef DEVICE_FLASH
+
+#include "flash_api.h"
+#include "platform/SingletonPtr.h"
+#include "platform/PlatformMutex.h"
+
+namespace mbed {
+
+/** \addtogroup drivers */
+/** @{*/
+
+/** Flash IAP driver. It invokes flash HAL functions.
+ *
+ * Note Synchronization level: Thread safe
+ */
+class FlashIAP {
+public:
+    FlashIAP();
+    ~FlashIAP();
+
+    /** Initialize a flash IAP device
+     *
+     *  Should be called once per lifetime of the object.
+     *  @return 0 on success or a negative error code on failure
+     */
+    int init();
+
+    /** Deinitialize a flash IAP device
+     *
+     *  @return 0 on success or a negative error code on failure
+     */
+    int deinit();
+
+    /** Read data from a flash device. 
+     *
+     *  This method invokes memcpy - reads number of bytes from the address
+     *
+     *  @param buffer Buffer to write to
+     *  @param addr   Flash address to begin reading from
+     *  @param size   Size to read in bytes
+     *  @return       0 on success, negative error code on failure
+     */
+    int read(void *buffer, uint32_t addr, uint32_t size);
+
+    /** Program data to pages
+     *
+     *  The sectors must have been erased prior to being programmed
+     *
+     *  @param buffer Buffer of data to be written
+     *  @param addr   Address of a page to begin writing to, must be a multiple of program and sector sizes
+     *  @param size   Size to write in bytes, must be a multiple of program and sector sizes
+     *  @return       0 on success, negative error code on failure
+     */
+    int program(const void *buffer, uint32_t addr, uint32_t size);
+
+    /** Erase sectors
+     *
+     *  The state of an erased sector is undefined until it has been programmed
+     *
+     *  @param addr Address of a sector to begin erasing, must be a multiple of the sector size
+     *  @param size Size to erase in bytes, must be a multiple of the sector size
+     *  @return     0 on success, negative error code on failure
+     */
+    int erase(uint32_t addr, uint32_t size);
+
+    /** Get the sector size at the defined address
+     *
+     *  Sector size might differ at address ranges. 
+     *  An example <0-0x1000, sector size=1024; 0x10000-0x20000, size=2048>
+     *
+     *  @param addr Address of or inside the sector to query
+     *  @return Size of a sector in bytes or MBED_FLASH_INVALID_SIZE if not mapped
+     */
+    uint32_t get_sector_size(uint32_t addr) const;
+
+    /** Get the flash start address 
+     *
+     *  @return Flash start address 
+     */
+    uint32_t get_flash_start() const;
+
+    /** Get the flash size
+     *
+     *  @return Flash size 
+     */
+    uint32_t get_flash_size() const;
+
+    /** Get the program page size
+     *
+     *  @return Size of a program page in bytes
+     */
+    uint32_t get_page_size() const;
+
+private:
+
+    /** Check if address and size are aligned to a sector
+     *
+     *  @param addr Address of block to check for alignment
+     *  @param size Size of block to check for alignment
+     *  @return true if the block is sector aligned, false otherwise
+     */
+    bool is_aligned_to_sector(uint32_t addr, uint32_t size);
+
+    flash_t _flash;
+    static SingletonPtr<PlatformMutex> _mutex;
+};
+
+} /* namespace mbed */
+
+#endif  /* DEVICE_FLASH */
+
+#endif  /* MBED_FLASHIAP_H */
+
+/** @}*/
--- a/drivers/InterruptIn.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/InterruptIn.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -27,8 +27,8 @@
                                         _fall() {
     // No lock needed in the constructor
 
-    _rise.attach(donothing);
-    _fall.attach(donothing);
+    _rise = donothing;
+    _fall = donothing;
 
     gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this);
     gpio_init_in(&gpio, pin);
@@ -53,10 +53,10 @@
 void InterruptIn::rise(Callback<void()> func) {
     core_util_critical_section_enter();
     if (func) {
-        _rise.attach(func);
+        _rise = func;
         gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
     } else {
-        _rise.attach(donothing);
+        _rise = donothing;
         gpio_irq_set(&gpio_irq, IRQ_RISE, 0);
     }
     core_util_critical_section_exit();
@@ -65,10 +65,10 @@
 void InterruptIn::fall(Callback<void()> func) {
     core_util_critical_section_enter();
     if (func) {
-        _fall.attach(func);
+        _fall = func;
         gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
     } else {
-        _fall.attach(donothing);
+        _fall = donothing;
         gpio_irq_set(&gpio_irq, IRQ_FALL, 0);
     }
     core_util_critical_section_exit();
@@ -77,8 +77,8 @@
 void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) {
     InterruptIn *handler = (InterruptIn*)id;
     switch (event) {
-        case IRQ_RISE: handler->_rise.call(); break;
-        case IRQ_FALL: handler->_fall.call(); break;
+        case IRQ_RISE: handler->_rise(); break;
+        case IRQ_FALL: handler->_fall(); break;
         case IRQ_NONE: break;
     }
 }
--- a/drivers/InterruptIn.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/InterruptIn.h	Tue Mar 14 16:40:56 2017 +0000
@@ -23,8 +23,8 @@
 #include "hal/gpio_api.h"
 #include "hal/gpio_irq_api.h"
 #include "platform/Callback.h"
-#include "platform/critical.h"
-#include "platform/toolchain.h"
+#include "platform/mbed_critical.h"
+#include "platform/mbed_toolchain.h"
 
 namespace mbed {
 /** \addtogroup drivers */
--- a/drivers/InterruptManager.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/InterruptManager.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -17,7 +17,7 @@
 #if defined(NVIC_NUM_VECTORS)
 
 #include "drivers/InterruptManager.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 #include <string.h>
 
 #define CHAIN_INITIAL_SIZE    4
--- a/drivers/LocalFileSystem.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/LocalFileSystem.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -17,7 +17,7 @@
 
 #if DEVICE_LOCALFILESYSTEM
 
-#include "platform/semihost_api.h"
+#include "platform/mbed_semihost_api.h"
 #include <string.h>
 #include <stdio.h>
 
--- a/drivers/PortIn.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/PortIn.h	Tue Mar 14 16:40:56 2017 +0000
@@ -21,7 +21,7 @@
 #if DEVICE_PORTIN
 
 #include "hal/port_api.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 namespace mbed {
 /** \addtogroup drivers */
--- a/drivers/PortInOut.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/PortInOut.h	Tue Mar 14 16:40:56 2017 +0000
@@ -21,7 +21,7 @@
 #if DEVICE_PORTINOUT
 
 #include "hal/port_api.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 namespace mbed {
 /** \addtogroup drivers */
--- a/drivers/PortOut.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/PortOut.h	Tue Mar 14 16:40:56 2017 +0000
@@ -21,7 +21,7 @@
 #if DEVICE_PORTOUT
 
 #include "hal/port_api.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 namespace mbed {
 /** \addtogroup drivers */
--- a/drivers/PwmOut.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/PwmOut.h	Tue Mar 14 16:40:56 2017 +0000
@@ -20,7 +20,7 @@
 
 #if DEVICE_PWMOUT
 #include "hal/pwmout_api.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 namespace mbed {
 /** \addtogroup drivers */
--- a/drivers/RawSerial.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/RawSerial.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 #include "drivers/RawSerial.h"
-#include "platform/wait_api.h"
+#include "platform/mbed_wait_api.h"
 #include <cstdarg>
 
 #if DEVICE_SERIAL
--- a/drivers/SPI.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/SPI.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 #include "drivers/SPI.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 #if DEVICE_SPI
 
--- a/drivers/Serial.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/Serial.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 #include "drivers/Serial.h"
-#include "platform/wait_api.h"
+#include "platform/mbed_wait_api.h"
 
 #if DEVICE_SERIAL
 
--- a/drivers/SerialBase.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/SerialBase.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 #include "drivers/SerialBase.h"
-#include "platform/wait_api.h"
-#include "platform/critical.h"
+#include "platform/mbed_wait_api.h"
+#include "platform/mbed_critical.h"
 
 #if DEVICE_SERIAL
 
@@ -32,7 +32,7 @@
     // No lock needed in the constructor
 
     for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
-        _irq[i].attach(donothing);
+        _irq[i] = donothing;
     }
 
     serial_init(&_serial, tx, rx);
@@ -73,10 +73,10 @@
     // Disable interrupts when attaching interrupt handler
     core_util_critical_section_enter();
     if (func) {
-        _irq[type].attach(func);
+        _irq[type] = func;
         serial_irq_set(&_serial, (SerialIrq)type, 1);
     } else {
-        _irq[type].attach(donothing);
+        _irq[type] = donothing;
         serial_irq_set(&_serial, (SerialIrq)type, 0);
     }
     core_util_critical_section_exit();
@@ -85,7 +85,7 @@
 
 void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) {
     SerialBase *handler = (SerialBase*)id;
-    handler->_irq[irq_type].call();
+    handler->_irq[irq_type]();
 }
 
 int SerialBase::_base_getc() {
--- a/drivers/SerialBase.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/SerialBase.h	Tue Mar 14 16:40:56 2017 +0000
@@ -23,7 +23,7 @@
 #include "Stream.h"
 #include "Callback.h"
 #include "serial_api.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 
 #if DEVICE_SERIAL_ASYNCH
 #include "CThunk.h"
--- a/drivers/Stream.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/Stream.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -94,19 +94,26 @@
     return ptr - (const char*)buffer;
 }
 
-off_t Stream::lseek(off_t offset, int whence) {
+off_t Stream::seek(off_t offset, int whence) {
     return 0;
 }
 
+off_t Stream::tell() {
+    return 0;
+}
+
+void Stream::rewind() {
+}
+
 int Stream::isatty() {
     return 0;
 }
 
-int Stream::fsync() {
+int Stream::sync() {
     return 0;
 }
 
-off_t Stream::flen() {
+size_t Stream::size() {
     return 0;
 }
 
--- a/drivers/Stream.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/Stream.h	Tue Mar 14 16:40:56 2017 +0000
@@ -18,6 +18,7 @@
 
 #include "platform/platform.h"
 #include "drivers/FileLike.h"
+#include "drivers/FileHandle.h"
 #include <cstdarg>
 
 namespace mbed {
@@ -53,10 +54,12 @@
     virtual int close();
     virtual ssize_t write(const void* buffer, size_t length);
     virtual ssize_t read(void* buffer, size_t length);
-    virtual off_t lseek(off_t offset, int whence);
+    virtual off_t seek(off_t offset, int whence);
+    virtual off_t tell();
+    virtual void rewind();
     virtual int isatty();
-    virtual int fsync();
-    virtual off_t flen();
+    virtual int sync();
+    virtual size_t size();
 
     virtual int _putc(int c) = 0;
     virtual int _getc() = 0;
--- a/drivers/Ticker.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/Ticker.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -18,14 +18,14 @@
 #include "drivers/TimerEvent.h"
 #include "platform/FunctionPointer.h"
 #include "hal/ticker_api.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 namespace mbed {
 
 void Ticker::detach() {
     core_util_critical_section_enter();
     remove();
-    _function.attach(0);
+    _function = 0;
     core_util_critical_section_exit();
 }
 
@@ -39,7 +39,7 @@
 
 void Ticker::handler() {
     insert(event.timestamp + _delay);
-    _function.call();
+    _function();
 }
 
 } // namespace mbed
--- a/drivers/Ticker.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/Ticker.h	Tue Mar 14 16:40:56 2017 +0000
@@ -18,7 +18,7 @@
 
 #include "drivers/TimerEvent.h"
 #include "platform/Callback.h"
-#include "platform/toolchain.h"
+#include "platform/mbed_toolchain.h"
 
 namespace mbed {
 /** \addtogroup drivers */
@@ -101,7 +101,7 @@
      *  @param t the time between calls in micro-seconds
      */
     void attach_us(Callback<void()> func, timestamp_t t) {
-        _function.attach(func);
+        _function = func;
         setup(t);
     }
 
--- a/drivers/Timer.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/drivers/Timer.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -16,7 +16,7 @@
 #include "drivers/Timer.h"
 #include "hal/ticker_api.h"
 #include "hal/us_ticker_api.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 namespace mbed {
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hal/TARGET_FLASH_CMSIS_ALGO/flash_common_algo.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,169 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "flash_api.h"
+#include "flash_data.h"
+#include "mbed_critical.h"
+
+#define MBED_FLASH_ALGO_ERASE   1UL
+#define MBED_FLASH_ALGO_PROGRAM 2UL
+
+extern uint32_t SystemCoreClock;
+
+/*
+    This binary blob (thumb code) sets r9 (static base) as the code we are jumping to
+    is PIC (position independent code).
+
+    These are the instructions (r0 is a pointer to arg_t):
+        push {r5, lr, r4}
+        mov r5, r9
+        push {r5}
+        ldr r5, [r0, #20]
+        ldr r3, [r0, #16]
+        mov r9, r3
+        ldr r3, [r0, #12]
+        ldr r2, [r0, #8]
+        ldr r1, [r0, #4]
+        ldr r0, [r0, #0]
+        blx r5
+        pop {r5}
+        mov r9, r5
+        pop {r4-r5, pc}
+        bx r14
+*/
+static uint32_t jump_to_flash_algo[] = {
+    0x464DB530,
+    0x6945B420,
+    0x46996903,
+    0x688268C3,
+    0x68006841,
+    0xBC2047A8,
+    0xBD3046A9
+};
+
+// should be called within critical section
+static int32_t flash_algo_init(flash_t *obj, uint32_t address, uint32_t function)
+{
+    args_t arguments = {
+        .r0 = address,
+        .r1 = SystemCoreClock,
+        .r2 = function,
+        .r3 = 0,
+        .r9 = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->static_base,
+        .pc = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->init
+    };
+    return ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments);
+}
+
+// should be called within critical section
+static int32_t flash_algo_uninit(flash_t *obj, uint32_t address, uint32_t function)
+{
+    args_t arguments = {
+        .r0 = address,
+        .r1 = SystemCoreClock,
+        .r2 = function,
+        .r3 = 0,
+        .r9 = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->static_base,
+        .pc = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->uninit
+    };
+    return ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments);
+}
+
+
+int32_t flash_init(flash_t *obj)
+{
+    flash_set_target_config(obj);
+    return 0;
+}
+
+int32_t flash_free(flash_t *obj)
+{
+    return 0;
+}
+
+int32_t flash_erase_sector(flash_t *obj, uint32_t address)
+{
+    core_util_critical_section_enter();
+    flash_algo_init(obj, address, MBED_FLASH_ALGO_ERASE);
+
+    args_t arguments = {
+        .r0 = address,
+        .r1 = 0,
+        .r2 = 0,
+        .r3 = 0,
+        .r9 = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->static_base,
+        .pc = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->erase_sector
+    };
+    int32_t ret = ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments);
+
+    flash_algo_uninit(obj, address, MBED_FLASH_ALGO_ERASE);
+    core_util_critical_section_exit();
+    return ret ? -1 : 0;
+}
+
+
+int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size)
+{
+    core_util_critical_section_enter();
+    flash_algo_init(obj, address, MBED_FLASH_ALGO_PROGRAM);
+
+    args_t arguments = {
+        .r0 = address,
+        .r1 = size,
+        .r2 = (uint32_t)data,
+        .r3 = 0,
+        .r9 = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->static_base,
+        .pc = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->program_page
+    };
+    int32_t ret = ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments);
+
+    flash_algo_uninit(obj, address, MBED_FLASH_ALGO_PROGRAM);
+    core_util_critical_section_exit();
+    return ret ? -1 : 0;
+}
+
+
+uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
+{
+    const sector_info_t *sectors = obj->target_config->sectors;
+
+    if (address >= obj->target_config->flash_start + obj->target_config->flash_size) {
+        return MBED_FLASH_INVALID_SIZE;
+    }
+
+    int sector_index = obj->target_config->sector_info_count - 1;
+    for (; sector_index >= 0; sector_index--) {
+        if (address >= sectors[sector_index].start) {
+            return sectors[sector_index].size;
+        }
+    }
+    return MBED_FLASH_INVALID_SIZE;
+}
+
+uint32_t flash_get_page_size(const flash_t *obj)
+{
+    return obj->target_config->page_size;
+}
+
+uint32_t flash_get_start_address(const flash_t *obj)
+{
+    return obj->target_config->flash_start;
+}
+
+uint32_t flash_get_size(const flash_t *obj)
+{
+    return obj->target_config->flash_size;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hal/TARGET_FLASH_CMSIS_ALGO/flash_data.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,83 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef MBED_FLASH_DATA_H
+#define MBED_FLASH_DATA_H
+
+#include <stdint.h>
+
+// Target flash algorithm structure
+typedef struct {
+    const uint32_t init;
+    const uint32_t uninit;
+    const uint32_t erase_sector;
+    const uint32_t program_page;
+    const uint32_t static_base;
+    uint32_t *algo_blob;
+} flash_algo_t;
+
+typedef struct {
+    const uint32_t start;
+    const uint32_t size;
+} sector_info_t;
+
+typedef struct {
+    const uint32_t page_size;
+    const uint32_t flash_start;
+    const uint32_t flash_size;
+    const sector_info_t *sectors;
+    const uint32_t sector_info_count;
+} flash_target_config_t;
+
+// Target flash configuration
+struct flash_s {
+    const flash_target_config_t *target_config;
+    const flash_algo_t *flash_algo;
+};
+
+typedef struct {
+    uint32_t r0;
+    uint32_t r1;
+    uint32_t r2;
+    uint32_t r3;
+    uint32_t r9;
+    uint32_t pc;
+} args_t;
+
+typedef int32_t (*flash_algo_jump_t)(args_t*);
+
+// prototypes for flash algo CMSIS API
+
+typedef int (*CMSIS_Algo_Function_Init)(unsigned long adr, unsigned long clk, unsigned long fnc);
+typedef int (*CMSIS_Algo_Function_UnInit)(unsigned long fnc);
+typedef int (*CMSIS_Algo_Function_EraseSector)(unsigned long adr);
+typedef int (*CMSIS_Algo_Function_EraseChip)(void);
+typedef int (*CMSIS_Algo_Function_ProgramPage)(unsigned long adr, unsigned long sz, unsigned char *buf);
+typedef unsigned long (*CMSIS_Algo_Function_Verify)(unsigned long adr, unsigned long sz, unsigned char *buf);
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Set target configuration
+ */
+void flash_set_target_config(flash_t *obj);
+
+#ifdef __cplusplus
+};
+#endif
+
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hal/flash_api.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,119 @@
+/** \addtogroup hal */
+/** @{*/
+
+/* mbed Microcontroller Library
+ * Copyright (c) 2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef MBED_FLASH_API_H
+#define MBED_FLASH_API_H
+
+#include "device.h"
+#include <stdint.h>
+
+#if DEVICE_FLASH
+
+#define MBED_FLASH_INVALID_SIZE     0xFFFFFFFF
+
+typedef struct flash_s flash_t;
+
+#if TARGET_FLASH_CMSIS_ALGO
+#include "flash_data.h"
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \defgroup flash_hal Flash HAL API
+ * @{
+ */
+
+/** Initialize the flash peripheral and the flash_t object
+ * 
+ * @param obj The flash object
+ * @return 0 for success, -1 for error
+ */
+int32_t flash_init(flash_t *obj);
+
+/** Uninitialize the flash peripheral and the flash_t object
+ * 
+ * @param obj The flash object
+ * @return 0 for success, -1 for error
+ */
+int32_t flash_free(flash_t *obj);
+
+/** Erase one sector starting at defined address
+ *
+ * The address should be at sector boundary. This function does not do any check for address alignments
+ * @param obj The flash object
+ * @param address The sector starting address
+ * @return 0 for success, -1 for error
+ */
+int32_t flash_erase_sector(flash_t *obj, uint32_t address);
+
+/** Program one page starting at defined address
+ * 
+ * The page should be at page boundary, should not cross multiple sectors. 
+ * This function does not do any check for address alignments or if size is aligned to a page size.
+ * @param obj The flash object
+ * @param address The sector starting address
+ * @param data The data buffer to be programmed
+ * @param size The number of bytes to program
+ * @return 0 for success, -1 for error
+ */
+int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size);
+
+/** Get sector size
+ * 
+ * @param obj The flash object
+ * @param address The sector starting address
+ * @return The size of a sector
+ */
+uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address);
+
+/** Get page size
+ * 
+ * @param obj The flash object
+ * @param address The page starting address
+ * @return The size of a page
+ */
+uint32_t flash_get_page_size(const flash_t *obj);
+
+/** Get start address for the flash region
+ * 
+ * @param obj The flash object
+ * @return The start address for the flash region
+ */
+uint32_t flash_get_start_address(const flash_t *obj);
+
+/** Get the flash region size
+ * 
+ * @param obj The flash object
+ * @return The flash region size
+ */
+uint32_t flash_get_size(const flash_t *obj);
+
+/**@}*/
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
+#endif
+
+/** @}*/
--- a/hal/mbed_ticker_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/hal/mbed_ticker_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -15,7 +15,7 @@
  */
 #include <stddef.h>
 #include "hal/ticker_api.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 void ticker_set_handler(const ticker_data_t *const data, ticker_event_handler handler) {
     data->interface->init();
--- a/hal/sleep_api.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/hal/sleep_api.h	Tue Mar 14 16:40:56 2017 +0000
@@ -41,7 +41,7 @@
  * Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
  * able to access the LocalFileSystem
  */
-void sleep(void);
+void hal_sleep(void);
 
 /** Send the microcontroller to deep sleep
  *
@@ -56,7 +56,7 @@
  * Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
  * able to access the LocalFileSystem
  */
-void deepsleep(void);
+void hal_deepsleep(void);
 
 #ifdef __cplusplus
 }
--- a/mbed.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/mbed.h	Tue Mar 14 16:40:56 2017 +0000
@@ -16,13 +16,13 @@
 #ifndef MBED_H
 #define MBED_H
 
-#define MBED_LIBRARY_VERSION 137
+#define MBED_LIBRARY_VERSION 138
 
 #if MBED_CONF_RTOS_PRESENT
 // RTOS present, this is valid only for mbed OS 5
 #define MBED_MAJOR_VERSION 5
-#define MBED_MINOR_VERSION 3
-#define MBED_PATCH_VERSION 6
+#define MBED_MINOR_VERSION 4
+#define MBED_PATCH_VERSION 1
 
 #else
 // mbed 2
@@ -47,8 +47,13 @@
 #include "events/mbed_events.h"
 #endif
 
-#include "platform/toolchain.h"
+#if MBED_CONF_FILESYSTEM_PRESENT
+#include "filesystem/mbed_filesystem.h"
+#endif
+
+#include "platform/mbed_toolchain.h"
 #include "platform/platform.h"
+#include "platform/mbed_application.h"
 
 // Useful C libraries
 #include <math.h>
@@ -80,6 +85,7 @@
 #include "drivers/Ethernet.h"
 #include "drivers/CAN.h"
 #include "drivers/RawSerial.h"
+#include "drivers/FlashIAP.h"
 
 // mbed Internal components
 #include "drivers/Timer.h"
@@ -90,9 +96,10 @@
 #include "drivers/LowPowerTimer.h"
 #include "drivers/LocalFileSystem.h"
 #include "drivers/InterruptIn.h"
-#include "platform/wait_api.h"
+#include "platform/mbed_wait_api.h"
 #include "hal/sleep_api.h"
-#include "platform/rtc_time.h"
+#include "platform/mbed_sleep.h"
+#include "platform/mbed_rtc_time.h"
 
 // mbed Non-hardware components
 #include "platform/Callback.h"
--- a/platform/CallChain.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/CallChain.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -1,6 +1,6 @@
 #include "platform/CallChain.h"
 #include "cmsis.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 namespace mbed {
 
--- a/platform/CallChain.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/CallChain.h	Tue Mar 14 16:40:56 2017 +0000
@@ -17,7 +17,7 @@
 #define MBED_CALLCHAIN_H
 
 #include "platform/Callback.h"
-#include "platform/toolchain.h"
+#include "platform/mbed_toolchain.h"
 #include <string.h>
 
 namespace mbed {
--- a/platform/Callback.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/Callback.h	Tue Mar 14 16:40:56 2017 +0000
@@ -20,7 +20,7 @@
 #include <stdint.h>
 #include <new>
 #include "platform/mbed_assert.h"
-#include "platform/toolchain.h"
+#include "platform/mbed_toolchain.h"
 
 namespace mbed {
 /** \addtogroup platform */
@@ -268,7 +268,11 @@
 
     /** Attach a static function
      *  @param func     Static function to attach
-     */
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
+     */
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)()) {
         this->~Callback();
         new (this) Callback(func);
@@ -276,7 +280,11 @@
 
     /** Attach a Callback
      *  @param func     The Callback to attach
-     */
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
+     */
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const Callback<R()> &func) {
         this->~Callback();
         new (this) Callback(func);
@@ -285,8 +293,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(U *obj, R (T::*method)()) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -295,8 +307,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const U *obj, R (T::*method)() const) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -305,8 +321,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(volatile U *obj, R (T::*method)() volatile) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -315,8 +335,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const volatile U *obj, R (T::*method)() const volatile) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -325,8 +349,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(T*), U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -335,8 +363,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(const T*), const U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -345,8 +377,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(volatile T*), volatile U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -355,8 +391,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(const volatile T*), const volatile U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -365,8 +405,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(), &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -378,8 +422,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const F f, typename detail::enable_if<
                 detail::is_type<R (F::*)() const, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -391,8 +439,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(volatile F f, typename detail::enable_if<
                 detail::is_type<R (F::*)() volatile, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -404,8 +456,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const volatile F f, typename detail::enable_if<
                 detail::is_type<R (F::*)() const volatile, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -807,7 +863,11 @@
 
     /** Attach a static function
      *  @param func     Static function to attach
-     */
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
+     */
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(A0)) {
         this->~Callback();
         new (this) Callback(func);
@@ -815,7 +875,11 @@
 
     /** Attach a Callback
      *  @param func     The Callback to attach
-     */
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
+     */
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const Callback<R(A0)> &func) {
         this->~Callback();
         new (this) Callback(func);
@@ -824,8 +888,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(U *obj, R (T::*method)(A0)) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -834,8 +902,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const U *obj, R (T::*method)(A0) const) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -844,8 +916,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(volatile U *obj, R (T::*method)(A0) volatile) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -854,8 +930,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const volatile U *obj, R (T::*method)(A0) const volatile) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -864,8 +944,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(T*, A0), U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -874,8 +958,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(const T*, A0), const U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -884,8 +972,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(volatile T*, A0), volatile U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -894,8 +986,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(const volatile T*, A0), const volatile U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -904,8 +1000,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0), &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -917,8 +1017,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0) const, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -930,8 +1034,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(volatile F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0) volatile, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -943,8 +1051,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const volatile F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0) const volatile, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -1346,7 +1458,11 @@
 
     /** Attach a static function
      *  @param func     Static function to attach
-     */
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
+     */
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(A0, A1)) {
         this->~Callback();
         new (this) Callback(func);
@@ -1354,7 +1470,11 @@
 
     /** Attach a Callback
      *  @param func     The Callback to attach
-     */
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
+     */
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const Callback<R(A0, A1)> &func) {
         this->~Callback();
         new (this) Callback(func);
@@ -1363,8 +1483,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(U *obj, R (T::*method)(A0, A1)) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -1373,8 +1497,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const U *obj, R (T::*method)(A0, A1) const) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -1383,8 +1511,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(volatile U *obj, R (T::*method)(A0, A1) volatile) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -1393,8 +1525,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const volatile U *obj, R (T::*method)(A0, A1) const volatile) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -1403,8 +1539,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(T*, A0, A1), U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -1413,8 +1553,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(const T*, A0, A1), const U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -1423,8 +1567,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(volatile T*, A0, A1), volatile U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -1433,8 +1581,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(const volatile T*, A0, A1), const volatile U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -1443,8 +1595,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0, A1), &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -1456,8 +1612,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0, A1) const, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -1469,8 +1629,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(volatile F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0, A1) volatile, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -1482,8 +1646,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const volatile F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0, A1) const volatile, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -1885,7 +2053,11 @@
 
     /** Attach a static function
      *  @param func     Static function to attach
-     */
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
+     */
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(A0, A1, A2)) {
         this->~Callback();
         new (this) Callback(func);
@@ -1893,7 +2065,11 @@
 
     /** Attach a Callback
      *  @param func     The Callback to attach
-     */
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
+     */
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const Callback<R(A0, A1, A2)> &func) {
         this->~Callback();
         new (this) Callback(func);
@@ -1902,8 +2078,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(U *obj, R (T::*method)(A0, A1, A2)) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -1912,8 +2092,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const U *obj, R (T::*method)(A0, A1, A2) const) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -1922,8 +2106,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(volatile U *obj, R (T::*method)(A0, A1, A2) volatile) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -1932,8 +2120,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const volatile U *obj, R (T::*method)(A0, A1, A2) const volatile) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -1942,8 +2134,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(T*, A0, A1, A2), U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -1952,8 +2148,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(const T*, A0, A1, A2), const U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -1962,8 +2162,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(volatile T*, A0, A1, A2), volatile U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -1972,8 +2176,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(const volatile T*, A0, A1, A2), const volatile U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -1982,8 +2190,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0, A1, A2), &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -1995,8 +2207,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0, A1, A2) const, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -2008,8 +2224,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(volatile F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0, A1, A2) volatile, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -2021,8 +2241,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const volatile F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0, A1, A2) const volatile, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -2424,7 +2648,11 @@
 
     /** Attach a static function
      *  @param func     Static function to attach
-     */
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
+     */
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(A0, A1, A2, A3)) {
         this->~Callback();
         new (this) Callback(func);
@@ -2432,7 +2660,11 @@
 
     /** Attach a Callback
      *  @param func     The Callback to attach
-     */
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
+     */
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const Callback<R(A0, A1, A2, A3)> &func) {
         this->~Callback();
         new (this) Callback(func);
@@ -2441,8 +2673,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(U *obj, R (T::*method)(A0, A1, A2, A3)) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -2451,8 +2687,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const U *obj, R (T::*method)(A0, A1, A2, A3) const) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -2461,8 +2701,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(volatile U *obj, R (T::*method)(A0, A1, A2, A3) volatile) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -2471,8 +2715,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const volatile U *obj, R (T::*method)(A0, A1, A2, A3) const volatile) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -2481,8 +2729,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(T*, A0, A1, A2, A3), U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -2491,8 +2743,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(const T*, A0, A1, A2, A3), const U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -2501,8 +2757,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(volatile T*, A0, A1, A2, A3), volatile U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -2511,8 +2771,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(const volatile T*, A0, A1, A2, A3), const volatile U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -2521,8 +2785,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0, A1, A2, A3), &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -2534,8 +2802,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0, A1, A2, A3) const, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -2547,8 +2819,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(volatile F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0, A1, A2, A3) volatile, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -2560,8 +2836,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const volatile F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0, A1, A2, A3) const volatile, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -2963,7 +3243,11 @@
 
     /** Attach a static function
      *  @param func     Static function to attach
-     */
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
+     */
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(A0, A1, A2, A3, A4)) {
         this->~Callback();
         new (this) Callback(func);
@@ -2971,7 +3255,11 @@
 
     /** Attach a Callback
      *  @param func     The Callback to attach
-     */
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
+     */
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const Callback<R(A0, A1, A2, A3, A4)> &func) {
         this->~Callback();
         new (this) Callback(func);
@@ -2980,8 +3268,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(U *obj, R (T::*method)(A0, A1, A2, A3, A4)) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -2990,8 +3282,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const U *obj, R (T::*method)(A0, A1, A2, A3, A4) const) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -3000,8 +3296,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(volatile U *obj, R (T::*method)(A0, A1, A2, A3, A4) volatile) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -3010,8 +3310,12 @@
     /** Attach a member function
      *  @param obj      Pointer to object to invoke member function on
      *  @param method   Member function to attach
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template<typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const volatile U *obj, R (T::*method)(A0, A1, A2, A3, A4) const volatile) {
         this->~Callback();
         new (this) Callback(obj, method);
@@ -3020,8 +3324,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(T*, A0, A1, A2, A3, A4), U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -3030,8 +3338,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(const T*, A0, A1, A2, A3, A4), const U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -3040,8 +3352,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(volatile T*, A0, A1, A2, A3, A4), volatile U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -3050,8 +3366,12 @@
     /** Attach a static function with a bound pointer
      *  @param func     Static function to attach
      *  @param arg      Pointer argument to function
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename T, typename U>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(R (*func)(const volatile T*, A0, A1, A2, A3, A4), const volatile U *arg) {
         this->~Callback();
         new (this) Callback(func, arg);
@@ -3060,8 +3380,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0, A1, A2, A3, A4), &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -3073,8 +3397,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0, A1, A2, A3, A4) const, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -3086,8 +3414,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(volatile F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0, A1, A2, A3, A4) volatile, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
@@ -3099,8 +3431,12 @@
     /** Attach a function object
      *  @param func     Function object to attach
      *  @note The function object is limited to a single word of storage
+     *  @deprecated
+     *      Replaced by simple assignment 'Callback cb = func'
      */
     template <typename F>
+    MBED_DEPRECATED_SINCE("mbed-os-5.4",
+        "Replaced by simple assignment 'Callback cb = func")
     void attach(const volatile F f, typename detail::enable_if<
                 detail::is_type<R (F::*)(A0, A1, A2, A3, A4) const volatile, &F::operator()>::value &&
                 sizeof(F) <= sizeof(uintptr_t)
--- a/platform/CircularBuffer.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/CircularBuffer.h	Tue Mar 14 16:40:56 2017 +0000
@@ -16,7 +16,7 @@
 #ifndef MBED_CIRCULARBUFFER_H
 #define MBED_CIRCULARBUFFER_H
 
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 namespace mbed {
 /** \addtogroup platform */
--- a/platform/FunctionPointer.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/FunctionPointer.h	Tue Mar 14 16:40:56 2017 +0000
@@ -17,7 +17,7 @@
 #define MBED_FUNCTIONPOINTER_H
 
 #include "platform/Callback.h"
-#include "platform/toolchain.h"
+#include "platform/mbed_toolchain.h"
 #include <string.h>
 #include <stdint.h>
 
--- a/platform/critical.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/critical.h	Tue Mar 14 16:40:56 2017 +0000
@@ -1,6 +1,3 @@
-
-/** \addtogroup platform */
-/** @{*/
 /*
  * Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
  * SPDX-License-Identifier: Apache-2.0
@@ -18,342 +15,10 @@
  * limitations under the License.
  */
 
-#ifndef __MBED_UTIL_CRITICAL_H__
-#define __MBED_UTIL_CRITICAL_H__
-
-#include <stdbool.h>
-#include <stdint.h>
-#include <stddef.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-
-/** Determine the current interrupts enabled state
-  *
-  * This function can be called to determine whether or not interrupts are currently enabled.
-  * \note
-  * NOTE:
-  * This function works for both cortex-A and cortex-M, although the underlyng implementation
-  * differs.
-  * @return true if interrupts are enabled, false otherwise
-  */
-bool core_util_are_interrupts_enabled(void);
-
-/** Mark the start of a critical section
-  *
-  * This function should be called to mark the start of a critical section of code.
-  * \note
-  * NOTES:
-  * 1) The use of this style of critical section is targetted at C based implementations.
-  * 2) These critical sections can be nested.
-  * 3) The interrupt enable state on entry to the first critical section (of a nested set, or single
-  *    section) will be preserved on exit from the section.
-  * 4) This implementation will currently only work on code running in privileged mode.
-  */
-void core_util_critical_section_enter(void);
-
-/** Mark the end of a critical section
-  *
-  * This function should be called to mark the end of a critical section of code.
-  * \note
-  * NOTES:
-  * 1) The use of this style of critical section is targetted at C based implementations.
-  * 2) These critical sections can be nested.
-  * 3) The interrupt enable state on entry to the first critical section (of a nested set, or single
-  *    section) will be preserved on exit from the section.
-  * 4) This implementation will currently only work on code running in privileged mode.
-  */
-void core_util_critical_section_exit(void);
-
-/**
- * Atomic compare and set. It compares the contents of a memory location to a
- * given value and, only if they are the same, modifies the contents of that
- * memory location to a given new value. This is done as a single atomic
- * operation. The atomicity guarantees that the new value is calculated based on
- * up-to-date information; if the value had been updated by another thread in
- * the meantime, the write would fail due to a mismatched expectedCurrentValue.
- *
- * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
- * you to the article on compare-and swap].
- *
- * @param  ptr                  The target memory location.
- * @param[in,out] expectedCurrentValue A pointer to some location holding the
- *                              expected current value of the data being set atomically.
- *                              The computed 'desiredValue' should be a function of this current value.
- *                              @Note: This is an in-out parameter. In the
- *                              failure case of atomic_cas (where the
- *                              destination isn't set), the pointee of expectedCurrentValue is
- *                              updated with the current value.
- * @param[in] desiredValue      The new value computed based on '*expectedCurrentValue'.
- *
- * @return                      true if the memory location was atomically
- *                              updated with the desired value (after verifying
- *                              that it contained the expectedCurrentValue),
- *                              false otherwise. In the failure case,
- *                              exepctedCurrentValue is updated with the new
- *                              value of the target memory location.
- *
- * pseudocode:
- * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
- *     if *p != *old {
- *         *old = *p
- *         return false
- *     }
- *     *p = new
- *     return true
- * }
- *
- * @Note: In the failure case (where the destination isn't set), the value
- * pointed to by expectedCurrentValue is still updated with the current value.
- * This property helps writing concise code for the following incr:
- *
- * function incr(p : pointer to int, a : int) returns int {
- *     done = false
- *     value = *p // This fetch operation need not be atomic.
- *     while not done {
- *         done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
- *     }
- *     return value + a
- * }
- */
-bool core_util_atomic_cas_u8(uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue);
-
-/**
- * Atomic compare and set. It compares the contents of a memory location to a
- * given value and, only if they are the same, modifies the contents of that
- * memory location to a given new value. This is done as a single atomic
- * operation. The atomicity guarantees that the new value is calculated based on
- * up-to-date information; if the value had been updated by another thread in
- * the meantime, the write would fail due to a mismatched expectedCurrentValue.
- *
- * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
- * you to the article on compare-and swap].
- *
- * @param  ptr                  The target memory location.
- * @param[in,out] expectedCurrentValue A pointer to some location holding the
- *                              expected current value of the data being set atomically.
- *                              The computed 'desiredValue' should be a function of this current value.
- *                              @Note: This is an in-out parameter. In the
- *                              failure case of atomic_cas (where the
- *                              destination isn't set), the pointee of expectedCurrentValue is
- *                              updated with the current value.
- * @param[in] desiredValue      The new value computed based on '*expectedCurrentValue'.
- *
- * @return                      true if the memory location was atomically
- *                              updated with the desired value (after verifying
- *                              that it contained the expectedCurrentValue),
- *                              false otherwise. In the failure case,
- *                              exepctedCurrentValue is updated with the new
- *                              value of the target memory location.
- *
- * pseudocode:
- * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
- *     if *p != *old {
- *         *old = *p
- *         return false
- *     }
- *     *p = new
- *     return true
- * }
- *
- * @Note: In the failure case (where the destination isn't set), the value
- * pointed to by expectedCurrentValue is still updated with the current value.
- * This property helps writing concise code for the following incr:
- *
- * function incr(p : pointer to int, a : int) returns int {
- *     done = false
- *     value = *p // This fetch operation need not be atomic.
- *     while not done {
- *         done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
- *     }
- *     return value + a
- * }
- */
-bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue);
+#ifndef MBED_OLD_CRITICAL_H
+#define MBED_OLD_CRITICAL_H
 
-/**
- * Atomic compare and set. It compares the contents of a memory location to a
- * given value and, only if they are the same, modifies the contents of that
- * memory location to a given new value. This is done as a single atomic
- * operation. The atomicity guarantees that the new value is calculated based on
- * up-to-date information; if the value had been updated by another thread in
- * the meantime, the write would fail due to a mismatched expectedCurrentValue.
- *
- * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
- * you to the article on compare-and swap].
- *
- * @param  ptr                  The target memory location.
- * @param[in,out] expectedCurrentValue A pointer to some location holding the
- *                              expected current value of the data being set atomically.
- *                              The computed 'desiredValue' should be a function of this current value.
- *                              @Note: This is an in-out parameter. In the
- *                              failure case of atomic_cas (where the
- *                              destination isn't set), the pointee of expectedCurrentValue is
- *                              updated with the current value.
- * @param[in] desiredValue      The new value computed based on '*expectedCurrentValue'.
- *
- * @return                      true if the memory location was atomically
- *                              updated with the desired value (after verifying
- *                              that it contained the expectedCurrentValue),
- *                              false otherwise. In the failure case,
- *                              exepctedCurrentValue is updated with the new
- *                              value of the target memory location.
- *
- * pseudocode:
- * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
- *     if *p != *old {
- *         *old = *p
- *         return false
- *     }
- *     *p = new
- *     return true
- * }
- *
- * @Note: In the failure case (where the destination isn't set), the value
- * pointed to by expectedCurrentValue is still updated with the current value.
- * This property helps writing concise code for the following incr:
- *
- * function incr(p : pointer to int, a : int) returns int {
- *     done = false
- *     value = *p // This fetch operation need not be atomic.
- *     while not done {
- *         done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
- *     }
- *     return value + a
- * }
- */
-bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue);
+#warning critical.h has been replaced by mbed_critical.h, please update to mbed_critical.h [since mbed-os-5.3]
+#include "platform/mbed_critical.h"
 
-/**
- * Atomic compare and set. It compares the contents of a memory location to a
- * given value and, only if they are the same, modifies the contents of that
- * memory location to a given new value. This is done as a single atomic
- * operation. The atomicity guarantees that the new value is calculated based on
- * up-to-date information; if the value had been updated by another thread in
- * the meantime, the write would fail due to a mismatched expectedCurrentValue.
- *
- * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
- * you to the article on compare-and swap].
- *
- * @param  ptr                  The target memory location.
- * @param[in,out] expectedCurrentValue A pointer to some location holding the
- *                              expected current value of the data being set atomically.
- *                              The computed 'desiredValue' should be a function of this current value.
- *                              @Note: This is an in-out parameter. In the
- *                              failure case of atomic_cas (where the
- *                              destination isn't set), the pointee of expectedCurrentValue is
- *                              updated with the current value.
- * @param[in] desiredValue      The new value computed based on '*expectedCurrentValue'.
- *
- * @return                      true if the memory location was atomically
- *                              updated with the desired value (after verifying
- *                              that it contained the expectedCurrentValue),
- *                              false otherwise. In the failure case,
- *                              exepctedCurrentValue is updated with the new
- *                              value of the target memory location.
- *
- * pseudocode:
- * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
- *     if *p != *old {
- *         *old = *p
- *         return false
- *     }
- *     *p = new
- *     return true
- * }
- *
- * @Note: In the failure case (where the destination isn't set), the value
- * pointed to by expectedCurrentValue is still updated with the current value.
- * This property helps writing concise code for the following incr:
- *
- * function incr(p : pointer to int, a : int) returns int {
- *     done = false
- *     value = *p // This fetch operation need not be atomic.
- *     while not done {
- *         done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
- *     }
- *     return value + a
- * }
- */
-bool core_util_atomic_cas_ptr(void **ptr, void **expectedCurrentValue, void *desiredValue);
-
-/**
- * Atomic increment.
- * @param  valuePtr Target memory location being incremented.
- * @param  delta    The amount being incremented.
- * @return          The new incremented value.
- */
-uint8_t core_util_atomic_incr_u8(uint8_t *valuePtr, uint8_t delta);
-
-/**
- * Atomic increment.
- * @param  valuePtr Target memory location being incremented.
- * @param  delta    The amount being incremented.
- * @return          The new incremented value.
- */
-uint16_t core_util_atomic_incr_u16(uint16_t *valuePtr, uint16_t delta);
-
-/**
- * Atomic increment.
- * @param  valuePtr Target memory location being incremented.
- * @param  delta    The amount being incremented.
- * @return          The new incremented value.
- */
-uint32_t core_util_atomic_incr_u32(uint32_t *valuePtr, uint32_t delta);
-
-/**
- * Atomic increment.
- * @param  valuePtr Target memory location being incremented.
- * @param  delta    The amount being incremented in bytes.
- * @return          The new incremented value.
- *
- * @note The type of the pointer argument is not taken into account
- *       and the pointer is incremented by bytes.
- */
-void *core_util_atomic_incr_ptr(void **valuePtr, ptrdiff_t delta);
-
-/**
- * Atomic decrement.
- * @param  valuePtr Target memory location being decremented.
- * @param  delta    The amount being decremented.
- * @return          The new decremented value.
- */
-uint8_t core_util_atomic_decr_u8(uint8_t *valuePtr, uint8_t delta);
-
-/**
- * Atomic decrement.
- * @param  valuePtr Target memory location being decremented.
- * @param  delta    The amount being decremented.
- * @return          The new decremented value.
- */
-uint16_t core_util_atomic_decr_u16(uint16_t *valuePtr, uint16_t delta);
-
-/**
- * Atomic decrement.
- * @param  valuePtr Target memory location being decremented.
- * @param  delta    The amount being decremented.
- * @return          The new decremented value.
- */
-uint32_t core_util_atomic_decr_u32(uint32_t *valuePtr, uint32_t delta);
-
-/**
- * Atomic decrement.
- * @param  valuePtr Target memory location being decremented.
- * @param  delta    The amount being decremented in bytes.
- * @return          The new decremented value.
- *
- * @note The type of the pointer argument is not taken into account
- *       and the pointer is decremented by bytes
- */
-void *core_util_atomic_decr_ptr(void **valuePtr, ptrdiff_t delta);
-
-#ifdef __cplusplus
-} // extern "C"
 #endif
-
-
-#endif // __MBED_UTIL_CRITICAL_H__
-
-/** @}*/
--- a/platform/mbed_alloc_wrappers.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/mbed_alloc_wrappers.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -16,7 +16,7 @@
 
 #include "platform/mbed_mem_trace.h"
 #include "platform/mbed_stats.h"
-#include "platform/toolchain.h"
+#include "platform/mbed_toolchain.h"
 #include "platform/SingletonPtr.h"
 #include "platform/PlatformMutex.h"
 #include <stddef.h>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/platform/mbed_application.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,127 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2017-2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include "device.h"
+#include "platform/mbed_application.h"
+
+#if MBED_APPLICATION_SUPPORT
+
+static void powerdown_nvic(void);
+static void powerdown_scb(uint32_t vtor);
+static void start_new_application(void *sp, void *pc);
+
+void mbed_start_application(uintptr_t address)
+{
+    void *sp;
+    void *pc;
+
+    // Interrupts are re-enabled in start_new_application
+    __disable_irq();
+
+    SysTick->CTRL = 0x00000000;
+    powerdown_nvic();
+    powerdown_scb(address);
+
+    sp = *((void**)address + 0);
+    pc = *((void**)address + 1);
+    start_new_application(sp, pc);
+}
+
+static void powerdown_nvic()
+{
+    int isr_count;
+    int i;
+    int j;
+
+    isr_count = (SCnSCB->ICTR & SCnSCB_ICTR_INTLINESNUM_Msk) >> SCnSCB_ICTR_INTLINESNUM_Pos;
+    for (i = 0; i < isr_count; i++) {
+        NVIC->ICER[i] = 0xFFFFFFFF;
+        NVIC->ICPR[i] = 0xFFFFFFFF;
+        for (j = 0; j < 8; j++) {
+            NVIC->IP[i * 8 + j] = 0x00000000;
+        }
+    }
+}
+
+static void powerdown_scb(uint32_t vtor)
+{
+    int i;
+
+    // SCB->CPUID   - Read only CPU ID register
+    SCB->ICSR = SCB_ICSR_PENDSVCLR_Msk | SCB_ICSR_PENDSTCLR_Msk;
+    SCB->VTOR = vtor;
+    SCB->AIRCR = 0x05FA | 0x0000;
+    SCB->SCR = 0x00000000;
+    // SCB->CCR     - Implementation defined value
+    for (i = 0; i < 12; i++) {
+#if defined(__CORTEX_M7)
+        SCB->SHPR[i] = 0x00;
+#else
+        SCB->SHP[i] = 0x00;
+#endif
+    }
+    SCB->SHCSR = 0x00000000;
+    SCB->CFSR = 0xFFFFFFFF;
+    SCB->HFSR = SCB_HFSR_DEBUGEVT_Msk | SCB_HFSR_FORCED_Msk | SCB_HFSR_VECTTBL_Msk;
+    SCB->DFSR = SCB_DFSR_EXTERNAL_Msk | SCB_DFSR_VCATCH_Msk |
+                SCB_DFSR_DWTTRAP_Msk | SCB_DFSR_BKPT_Msk | SCB_DFSR_HALTED_Msk;
+    // SCB->MMFAR   - Implementation defined value
+    // SCB->BFAR    - Implementation defined value
+    // SCB->AFSR    - Implementation defined value
+    // SCB->PFR     - Read only processor feature register
+    // SCB->DFR     - Read only debug feature registers
+    // SCB->ADR     - Read only auxiliary feature registers
+    // SCB->MMFR    - Read only memory model feature registers
+    // SCB->ISAR    - Read only instruction set attribute registers
+    // SCB->CPACR   - Implementation defined value
+}
+
+#if defined (__CC_ARM)
+
+__asm static void start_new_application(void *sp, void *pc)
+{
+    MOV R2, #0
+    MSR CONTROL, R2         // Switch to main stack
+    MOV SP, R0
+    MSR PRIMASK, R2         // Enable interrupts
+    BX R1
+}
+
+#elif defined (__GNUC__) || defined (__ICCARM__)
+
+void start_new_application(void *sp, void *pc)
+{
+    __asm volatile (
+        "mov    r2, #0      \n"
+        "msr    control, r2 \n" // Switch to main stack
+        "mov    sp, %0      \n"
+        "msr    primask, r2 \n" // Enable interrupts
+        "bx     %1          \n"
+        :
+        : "l" (sp), "l" (pc)
+        : "r2", "cc", "memory"
+    );
+}
+
+#else
+
+#error "Unsupported toolchain"
+
+#endif
+
+#endif /* MBED_APPLICATION_SUPPORT */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/platform/mbed_application.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,50 @@
+
+/** \addtogroup platform */
+/** @{*/
+/* mbed Microcontroller Library
+ * Copyright (c) 2017-2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef MBED_APPLICATION_H
+#define MBED_APPLICATION_H
+
+#include<stdint.h>
+
+#define MBED_APPLICATION_SUPPORT (defined(__CORTEX_M3) || defined(__CORTEX_M4) || defined(__CORTEX_M7))
+#if MBED_APPLICATION_SUPPORT
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ *  Start the application at the given address. This function does
+ *  not return. It is the applications responsibility for flushing to
+ *  or powering down external components such as filesystems or
+ *  socket connections before calling this function. For Cortex-M
+ *  devices this function powers down generic system components such as
+ *  the NVIC and set the vector table to that of the new image followed
+ *  by jumping to the reset handler of the new image.
+ *
+ *  @param address    Starting address of next application to run
+ */
+void mbed_start_application(uintptr_t address);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
+
+#endif
+
+/** @}*/
--- a/platform/mbed_assert.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/mbed_assert.c	Tue Mar 14 16:40:56 2017 +0000
@@ -17,7 +17,7 @@
 #include "device.h"
 
 #include "platform/mbed_interface.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 void mbed_assert_internal(const char *expr, const char *file, int line)
 {
--- a/platform/mbed_board.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/mbed_board.c	Tue Mar 14 16:40:56 2017 +0000
@@ -15,10 +15,10 @@
  */
 #include <stdio.h>
 #include "hal/gpio_api.h"
-#include "platform/wait_api.h"
-#include "platform/toolchain.h"
+#include "platform/mbed_wait_api.h"
+#include "platform/mbed_toolchain.h"
 #include "platform/mbed_interface.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 #include "hal/serial_api.h"
 
 #if DEVICE_SERIAL
@@ -75,16 +75,31 @@
 
 void mbed_error_vfprintf(const char * format, va_list arg) {
 #if DEVICE_SERIAL
+
+#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
+    char stdio_out_prev;
+#endif
+
     core_util_critical_section_enter();
     char buffer[128];
     int size = vsprintf(buffer, format, arg);
     if (size > 0) {
         if (!stdio_uart_inited) {
-        serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
+            serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
         }
-        for (int i = 0; i < size; i++) {
+#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
+        for (unsigned int i = 0; i < size; i++) {
+            if (buffer[i] == '\n' && stdio_out_prev != '\r') {
+                 serial_putc(&stdio_uart, '\r');
+            }
+            serial_putc(&stdio_uart, buffer[i]);
+            stdio_out_prev = buffer[i];
+        }
+#else
+        for (unsigned int i = 0; i < size; i++) {
             serial_putc(&stdio_uart, buffer[i]);
         }
+#endif
     }
     core_util_critical_section_exit();
 #endif
--- a/platform/mbed_critical.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/mbed_critical.c	Tue Mar 14 16:40:56 2017 +0000
@@ -17,11 +17,11 @@
 
 /* Declare __STDC_LIMIT_MACROS so stdint.h defines UINT32_MAX when using C++ */
 #define __STDC_LIMIT_MACROS
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 #include "cmsis.h"
 #include "platform/mbed_assert.h"
-#include "platform/toolchain.h"
+#include "platform/mbed_toolchain.h"
 
 #define EXCLUSIVE_ACCESS (!defined (__CORTEX_M0) && !defined (__CORTEX_M0PLUS))
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/platform/mbed_critical.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,359 @@
+
+/** \addtogroup platform */
+/** @{*/
+/*
+ * Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MBED_UTIL_CRITICAL_H__
+#define __MBED_UTIL_CRITICAL_H__
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stddef.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/** Determine the current interrupts enabled state
+  *
+  * This function can be called to determine whether or not interrupts are currently enabled.
+  * \note
+  * NOTE:
+  * This function works for both cortex-A and cortex-M, although the underlyng implementation
+  * differs.
+  * @return true if interrupts are enabled, false otherwise
+  */
+bool core_util_are_interrupts_enabled(void);
+
+/** Mark the start of a critical section
+  *
+  * This function should be called to mark the start of a critical section of code.
+  * \note
+  * NOTES:
+  * 1) The use of this style of critical section is targetted at C based implementations.
+  * 2) These critical sections can be nested.
+  * 3) The interrupt enable state on entry to the first critical section (of a nested set, or single
+  *    section) will be preserved on exit from the section.
+  * 4) This implementation will currently only work on code running in privileged mode.
+  */
+void core_util_critical_section_enter(void);
+
+/** Mark the end of a critical section
+  *
+  * This function should be called to mark the end of a critical section of code.
+  * \note
+  * NOTES:
+  * 1) The use of this style of critical section is targetted at C based implementations.
+  * 2) These critical sections can be nested.
+  * 3) The interrupt enable state on entry to the first critical section (of a nested set, or single
+  *    section) will be preserved on exit from the section.
+  * 4) This implementation will currently only work on code running in privileged mode.
+  */
+void core_util_critical_section_exit(void);
+
+/**
+ * Atomic compare and set. It compares the contents of a memory location to a
+ * given value and, only if they are the same, modifies the contents of that
+ * memory location to a given new value. This is done as a single atomic
+ * operation. The atomicity guarantees that the new value is calculated based on
+ * up-to-date information; if the value had been updated by another thread in
+ * the meantime, the write would fail due to a mismatched expectedCurrentValue.
+ *
+ * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
+ * you to the article on compare-and swap].
+ *
+ * @param  ptr                  The target memory location.
+ * @param[in,out] expectedCurrentValue A pointer to some location holding the
+ *                              expected current value of the data being set atomically.
+ *                              The computed 'desiredValue' should be a function of this current value.
+ *                              @Note: This is an in-out parameter. In the
+ *                              failure case of atomic_cas (where the
+ *                              destination isn't set), the pointee of expectedCurrentValue is
+ *                              updated with the current value.
+ * @param[in] desiredValue      The new value computed based on '*expectedCurrentValue'.
+ *
+ * @return                      true if the memory location was atomically
+ *                              updated with the desired value (after verifying
+ *                              that it contained the expectedCurrentValue),
+ *                              false otherwise. In the failure case,
+ *                              exepctedCurrentValue is updated with the new
+ *                              value of the target memory location.
+ *
+ * pseudocode:
+ * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
+ *     if *p != *old {
+ *         *old = *p
+ *         return false
+ *     }
+ *     *p = new
+ *     return true
+ * }
+ *
+ * @Note: In the failure case (where the destination isn't set), the value
+ * pointed to by expectedCurrentValue is still updated with the current value.
+ * This property helps writing concise code for the following incr:
+ *
+ * function incr(p : pointer to int, a : int) returns int {
+ *     done = false
+ *     value = *p // This fetch operation need not be atomic.
+ *     while not done {
+ *         done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
+ *     }
+ *     return value + a
+ * }
+ */
+bool core_util_atomic_cas_u8(uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue);
+
+/**
+ * Atomic compare and set. It compares the contents of a memory location to a
+ * given value and, only if they are the same, modifies the contents of that
+ * memory location to a given new value. This is done as a single atomic
+ * operation. The atomicity guarantees that the new value is calculated based on
+ * up-to-date information; if the value had been updated by another thread in
+ * the meantime, the write would fail due to a mismatched expectedCurrentValue.
+ *
+ * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
+ * you to the article on compare-and swap].
+ *
+ * @param  ptr                  The target memory location.
+ * @param[in,out] expectedCurrentValue A pointer to some location holding the
+ *                              expected current value of the data being set atomically.
+ *                              The computed 'desiredValue' should be a function of this current value.
+ *                              @Note: This is an in-out parameter. In the
+ *                              failure case of atomic_cas (where the
+ *                              destination isn't set), the pointee of expectedCurrentValue is
+ *                              updated with the current value.
+ * @param[in] desiredValue      The new value computed based on '*expectedCurrentValue'.
+ *
+ * @return                      true if the memory location was atomically
+ *                              updated with the desired value (after verifying
+ *                              that it contained the expectedCurrentValue),
+ *                              false otherwise. In the failure case,
+ *                              exepctedCurrentValue is updated with the new
+ *                              value of the target memory location.
+ *
+ * pseudocode:
+ * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
+ *     if *p != *old {
+ *         *old = *p
+ *         return false
+ *     }
+ *     *p = new
+ *     return true
+ * }
+ *
+ * @Note: In the failure case (where the destination isn't set), the value
+ * pointed to by expectedCurrentValue is still updated with the current value.
+ * This property helps writing concise code for the following incr:
+ *
+ * function incr(p : pointer to int, a : int) returns int {
+ *     done = false
+ *     value = *p // This fetch operation need not be atomic.
+ *     while not done {
+ *         done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
+ *     }
+ *     return value + a
+ * }
+ */
+bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue);
+
+/**
+ * Atomic compare and set. It compares the contents of a memory location to a
+ * given value and, only if they are the same, modifies the contents of that
+ * memory location to a given new value. This is done as a single atomic
+ * operation. The atomicity guarantees that the new value is calculated based on
+ * up-to-date information; if the value had been updated by another thread in
+ * the meantime, the write would fail due to a mismatched expectedCurrentValue.
+ *
+ * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
+ * you to the article on compare-and swap].
+ *
+ * @param  ptr                  The target memory location.
+ * @param[in,out] expectedCurrentValue A pointer to some location holding the
+ *                              expected current value of the data being set atomically.
+ *                              The computed 'desiredValue' should be a function of this current value.
+ *                              @Note: This is an in-out parameter. In the
+ *                              failure case of atomic_cas (where the
+ *                              destination isn't set), the pointee of expectedCurrentValue is
+ *                              updated with the current value.
+ * @param[in] desiredValue      The new value computed based on '*expectedCurrentValue'.
+ *
+ * @return                      true if the memory location was atomically
+ *                              updated with the desired value (after verifying
+ *                              that it contained the expectedCurrentValue),
+ *                              false otherwise. In the failure case,
+ *                              exepctedCurrentValue is updated with the new
+ *                              value of the target memory location.
+ *
+ * pseudocode:
+ * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
+ *     if *p != *old {
+ *         *old = *p
+ *         return false
+ *     }
+ *     *p = new
+ *     return true
+ * }
+ *
+ * @Note: In the failure case (where the destination isn't set), the value
+ * pointed to by expectedCurrentValue is still updated with the current value.
+ * This property helps writing concise code for the following incr:
+ *
+ * function incr(p : pointer to int, a : int) returns int {
+ *     done = false
+ *     value = *p // This fetch operation need not be atomic.
+ *     while not done {
+ *         done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
+ *     }
+ *     return value + a
+ * }
+ */
+bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue);
+
+/**
+ * Atomic compare and set. It compares the contents of a memory location to a
+ * given value and, only if they are the same, modifies the contents of that
+ * memory location to a given new value. This is done as a single atomic
+ * operation. The atomicity guarantees that the new value is calculated based on
+ * up-to-date information; if the value had been updated by another thread in
+ * the meantime, the write would fail due to a mismatched expectedCurrentValue.
+ *
+ * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
+ * you to the article on compare-and swap].
+ *
+ * @param  ptr                  The target memory location.
+ * @param[in,out] expectedCurrentValue A pointer to some location holding the
+ *                              expected current value of the data being set atomically.
+ *                              The computed 'desiredValue' should be a function of this current value.
+ *                              @Note: This is an in-out parameter. In the
+ *                              failure case of atomic_cas (where the
+ *                              destination isn't set), the pointee of expectedCurrentValue is
+ *                              updated with the current value.
+ * @param[in] desiredValue      The new value computed based on '*expectedCurrentValue'.
+ *
+ * @return                      true if the memory location was atomically
+ *                              updated with the desired value (after verifying
+ *                              that it contained the expectedCurrentValue),
+ *                              false otherwise. In the failure case,
+ *                              exepctedCurrentValue is updated with the new
+ *                              value of the target memory location.
+ *
+ * pseudocode:
+ * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
+ *     if *p != *old {
+ *         *old = *p
+ *         return false
+ *     }
+ *     *p = new
+ *     return true
+ * }
+ *
+ * @Note: In the failure case (where the destination isn't set), the value
+ * pointed to by expectedCurrentValue is still updated with the current value.
+ * This property helps writing concise code for the following incr:
+ *
+ * function incr(p : pointer to int, a : int) returns int {
+ *     done = false
+ *     value = *p // This fetch operation need not be atomic.
+ *     while not done {
+ *         done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
+ *     }
+ *     return value + a
+ * }
+ */
+bool core_util_atomic_cas_ptr(void **ptr, void **expectedCurrentValue, void *desiredValue);
+
+/**
+ * Atomic increment.
+ * @param  valuePtr Target memory location being incremented.
+ * @param  delta    The amount being incremented.
+ * @return          The new incremented value.
+ */
+uint8_t core_util_atomic_incr_u8(uint8_t *valuePtr, uint8_t delta);
+
+/**
+ * Atomic increment.
+ * @param  valuePtr Target memory location being incremented.
+ * @param  delta    The amount being incremented.
+ * @return          The new incremented value.
+ */
+uint16_t core_util_atomic_incr_u16(uint16_t *valuePtr, uint16_t delta);
+
+/**
+ * Atomic increment.
+ * @param  valuePtr Target memory location being incremented.
+ * @param  delta    The amount being incremented.
+ * @return          The new incremented value.
+ */
+uint32_t core_util_atomic_incr_u32(uint32_t *valuePtr, uint32_t delta);
+
+/**
+ * Atomic increment.
+ * @param  valuePtr Target memory location being incremented.
+ * @param  delta    The amount being incremented in bytes.
+ * @return          The new incremented value.
+ *
+ * @note The type of the pointer argument is not taken into account
+ *       and the pointer is incremented by bytes.
+ */
+void *core_util_atomic_incr_ptr(void **valuePtr, ptrdiff_t delta);
+
+/**
+ * Atomic decrement.
+ * @param  valuePtr Target memory location being decremented.
+ * @param  delta    The amount being decremented.
+ * @return          The new decremented value.
+ */
+uint8_t core_util_atomic_decr_u8(uint8_t *valuePtr, uint8_t delta);
+
+/**
+ * Atomic decrement.
+ * @param  valuePtr Target memory location being decremented.
+ * @param  delta    The amount being decremented.
+ * @return          The new decremented value.
+ */
+uint16_t core_util_atomic_decr_u16(uint16_t *valuePtr, uint16_t delta);
+
+/**
+ * Atomic decrement.
+ * @param  valuePtr Target memory location being decremented.
+ * @param  delta    The amount being decremented.
+ * @return          The new decremented value.
+ */
+uint32_t core_util_atomic_decr_u32(uint32_t *valuePtr, uint32_t delta);
+
+/**
+ * Atomic decrement.
+ * @param  valuePtr Target memory location being decremented.
+ * @param  delta    The amount being decremented in bytes.
+ * @return          The new decremented value.
+ *
+ * @note The type of the pointer argument is not taken into account
+ *       and the pointer is decremented by bytes
+ */
+void *core_util_atomic_decr_ptr(void **valuePtr, ptrdiff_t delta);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+
+#endif // __MBED_UTIL_CRITICAL_H__
+
+/** @}*/
--- a/platform/mbed_error.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/mbed_error.c	Tue Mar 14 16:40:56 2017 +0000
@@ -16,7 +16,7 @@
 #include <stdlib.h>
 #include <stdarg.h>
 #include "device.h"
-#include "platform/toolchain.h"
+#include "platform/mbed_toolchain.h"
 #include "platform/mbed_error.h"
 #include "platform/mbed_interface.h"
 #if DEVICE_STDIO_MESSAGES
--- a/platform/mbed_interface.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/mbed_interface.c	Tue Mar 14 16:40:56 2017 +0000
@@ -17,10 +17,10 @@
 #include "platform/mbed_interface.h"
 
 #include "hal/gpio_api.h"
-#include "platform/wait_api.h"
-#include "platform/semihost_api.h"
+#include "platform/mbed_wait_api.h"
+#include "platform/mbed_semihost_api.h"
 #include "platform/mbed_error.h"
-#include "platform/toolchain.h"
+#include "platform/mbed_toolchain.h"
 
 #if DEVICE_SEMIHOST
 
--- a/platform/mbed_mem_trace.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/mbed_mem_trace.c	Tue Mar 14 16:40:56 2017 +0000
@@ -18,7 +18,7 @@
 #include <stdarg.h>
 #include <stdio.h>
 #include "platform/mbed_mem_trace.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 /******************************************************************************
  * Internal variables, functions and helpers
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/platform/mbed_retarget.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,1048 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2015 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "platform/platform.h"
+#include "drivers/FilePath.h"
+#include "hal/serial_api.h"
+#include "platform/mbed_toolchain.h"
+#include "platform/mbed_semihost_api.h"
+#include "platform/mbed_interface.h"
+#include "platform/SingletonPtr.h"
+#include "platform/PlatformMutex.h"
+#include "platform/mbed_error.h"
+#include "platform/mbed_stats.h"
+#if MBED_CONF_FILESYSTEM_PRESENT
+#include "filesystem/FileSystem.h"
+#include "filesystem/File.h"
+#include "filesystem/Dir.h"
+#endif
+#include <stdlib.h>
+#include <string.h>
+#if DEVICE_STDIO_MESSAGES
+#include <stdio.h>
+#endif
+#include <errno.h>
+#include "platform/mbed_retarget.h"
+
+
+#if defined(__ARMCC_VERSION)
+#   include <rt_sys.h>
+#   define PREFIX(x)    _sys##x
+#   define OPEN_MAX     _SYS_OPEN
+#   ifdef __MICROLIB
+#       pragma import(__use_full_stdio)
+#   endif
+
+#elif defined(__ICCARM__)
+#   include <yfuns.h>
+#   define PREFIX(x)        _##x
+#   define OPEN_MAX         16
+
+#   define STDIN_FILENO     0
+#   define STDOUT_FILENO    1
+#   define STDERR_FILENO    2
+
+#else
+#   include <sys/stat.h>
+#   include <sys/syslimits.h>
+#   define PREFIX(x)    x
+#endif
+
+#define FILE_HANDLE_RESERVED    0xFFFFFFFF
+
+using namespace mbed;
+
+#if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
+// Before version 5.03, we were using a patched version of microlib with proper names
+extern const char __stdin_name[]  = ":tt";
+extern const char __stdout_name[] = ":tt";
+extern const char __stderr_name[] = ":tt";
+
+#else
+extern const char __stdin_name[]  = "/stdin";
+extern const char __stdout_name[] = "/stdout";
+extern const char __stderr_name[] = "/stderr";
+#endif
+
+// Heap limits - only used if set
+unsigned char *mbed_heap_start = 0;
+uint32_t mbed_heap_size = 0;
+
+/* newlib has the filehandle field in the FILE struct as a short, so
+ * we can't just return a Filehandle* from _open and instead have to
+ * put it in a filehandles array and return the index into that array
+ * (or rather index+3, as filehandles 0-2 are stdin/out/err).
+ */
+static FileLike *filehandles[OPEN_MAX];
+static SingletonPtr<PlatformMutex> filehandle_mutex;
+
+namespace mbed {
+void remove_filehandle(FileLike *file) {
+    filehandle_mutex->lock();
+    /* Remove all open filehandles for this */
+    for (unsigned int fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) {
+        if (filehandles[fh_i] == file) {
+            filehandles[fh_i] = NULL;
+        }
+    }
+    filehandle_mutex->unlock();
+}
+}
+
+#if DEVICE_SERIAL
+extern int stdio_uart_inited;
+extern serial_t stdio_uart;
+#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
+static char stdio_in_prev;
+static char stdio_out_prev;
+#endif
+#endif
+
+static void init_serial() {
+#if DEVICE_SERIAL
+    if (stdio_uart_inited) return;
+    serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
+#if MBED_CONF_PLATFORM_STDIO_BAUD_RATE
+    serial_baud(&stdio_uart, MBED_CONF_PLATFORM_STDIO_BAUD_RATE);
+#endif
+#endif
+}
+
+static inline int openmode_to_posix(int openmode) {
+    int posix = openmode;
+#ifdef __ARMCC_VERSION
+    if (openmode & OPEN_PLUS) {
+        posix = O_RDWR;
+    } else if(openmode & OPEN_W) {
+        posix = O_WRONLY;
+    } else if(openmode & OPEN_A) {
+        posix = O_WRONLY|O_APPEND;
+    } else {
+        posix = O_RDONLY;
+    }
+    /* a, w, a+, w+ all create if file does not already exist */
+    if (openmode & (OPEN_A|OPEN_W)) {
+        posix |= O_CREAT;
+    }
+    /* w and w+ truncate */
+    if (openmode & OPEN_W) {
+        posix |= O_TRUNC;
+    }
+#elif defined(__ICCARM__)
+    switch (openmode & _LLIO_RDWRMASK) {
+        case _LLIO_RDONLY: posix = O_RDONLY; break;
+        case _LLIO_WRONLY: posix = O_WRONLY; break;
+        case _LLIO_RDWR  : posix = O_RDWR  ; break;
+    }
+    if (openmode & _LLIO_CREAT ) posix |= O_CREAT;
+    if (openmode & _LLIO_APPEND) posix |= O_APPEND;
+    if (openmode & _LLIO_TRUNC ) posix |= O_TRUNC;
+#elif defined(TOOLCHAIN_GCC)
+    posix &= ~O_BINARY;
+#endif
+    return posix;
+}
+
+extern "C" WEAK void mbed_sdk_init(void);
+extern "C" WEAK void mbed_sdk_init(void) {
+}
+
+#if MBED_CONF_FILESYSTEM_PRESENT
+// Internally used file objects with managed memory on close
+class ManagedFile : public File {
+public:
+    virtual int close() {
+        int err = File::close();
+        delete this;
+        return err;
+    }
+};
+
+class ManagedDir : public Dir {
+public:
+     virtual int close() {
+        int err = Dir::close();
+        delete this;
+        return err;
+    }
+};
+#endif
+
+/* @brief 	standard c library fopen() retargeting function.
+ *
+ * This function is invoked by the standard c library retargeting to handle fopen()
+ *
+ * @return
+ *  On success, a valid FILEHANDLE is returned.
+ *  On failure, -1 is returned and errno is set to an appropriate value e.g.
+ *   EBADF		a bad file descriptor was found (default errno setting)
+ *	 EMFILE		the maximum number of open files was exceeded.
+ *
+ * */
+extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
+    #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
+    // Before version 5.03, we were using a patched version of microlib with proper names
+    // This is the workaround that the microlib author suggested us
+    static int n = 0;
+    static int mbed_sdk_inited = 0;
+    if (!mbed_sdk_inited) {
+        mbed_sdk_inited = 1;
+        mbed_sdk_init();
+    }
+    if (!std::strcmp(name, ":tt")) return n++;
+    #else
+    /* Use the posix convention that stdin,out,err are filehandles 0,1,2.
+     */
+    if (std::strcmp(name, __stdin_name) == 0) {
+        init_serial();
+        return 0;
+    } else if (std::strcmp(name, __stdout_name) == 0) {
+        init_serial();
+        return 1;
+    } else if (std::strcmp(name, __stderr_name) == 0) {
+        init_serial();
+        return 2;
+    }
+    #endif
+
+    /* if something goes wrong and errno is not explicly set, errno will be set to EBADF */
+    errno = EBADF;
+
+    // find the first empty slot in filehandles
+    filehandle_mutex->lock();
+    unsigned int fh_i;
+    for (fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) {
+    	/* Take a next free filehandle slot available. */
+        if (filehandles[fh_i] == NULL) break;
+    }
+    if (fh_i >= sizeof(filehandles)/sizeof(*filehandles)) {
+        /* Too many file handles have been opened */
+        errno = EMFILE;
+        filehandle_mutex->unlock();
+        return -1;
+    }
+    filehandles[fh_i] = (FileLike*)FILE_HANDLE_RESERVED;
+    filehandle_mutex->unlock();
+
+    FileLike *res = NULL;
+
+    /* FILENAME: ":0x12345678" describes a FileLike* */
+    if (name[0] == ':') {
+        void *p;
+        sscanf(name, ":%p", &p);
+        res = (FileLike*)p;
+
+    /* FILENAME: "/file_system/file_name" */
+    } else {
+        FilePath path(name);
+
+        if (!path.exists()) {
+            /* The first part of the filename (between first 2 '/') is not a
+             * registered mount point in the namespace.
+             * Free file handle.
+             */
+            filehandles[fh_i] = NULL;
+            errno = ENOENT;
+            return -1;
+        } else if (path.isFile()) {
+            res = path.file();
+#if MBED_CONF_FILESYSTEM_PRESENT
+        } else {
+            FileSystem *fs = path.fileSystem();
+            if (fs == NULL) {
+                /* The filesystem instance managing the namespace under the mount point
+                 * has not been found. Free file handle */
+                errno = ENOENT;
+                filehandles[fh_i] = NULL;
+                return -1;
+            }
+            int posix_mode = openmode_to_posix(openmode);
+            File *file = new ManagedFile;
+            int err = file->open(fs, path.fileName(), posix_mode);
+            if (err < 0) {
+                errno = -err;
+                delete file;
+            } else {
+                res = file;
+            }
+#endif
+        }
+    }
+
+    if (res == NULL) {
+        // Free file handle
+        filehandles[fh_i] = NULL;
+        return -1;
+    }
+    filehandles[fh_i] = res;
+
+    return fh_i + 3; // +3 as filehandles 0-2 are stdin/out/err
+}
+
+extern "C" int PREFIX(_close)(FILEHANDLE fh) {
+    if (fh < 3) return 0;
+
+    errno = EBADF;
+    FileLike* fhc = filehandles[fh-3];
+    filehandles[fh-3] = NULL;
+    if (fhc == NULL) return -1;
+
+    int err = fhc->close();
+    if (err < 0) {
+        errno = -err;
+        return -1;
+    } else {
+        return 0;
+    }
+}
+
+#if defined(__ICCARM__)
+extern "C" size_t    __write (int        fh, const unsigned char *buffer, size_t length) {
+#else
+extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsigned int length, int mode) {
+#endif
+    int n; // n is the number of bytes written
+
+    errno = EBADF;
+    if (fh < 3) {
+#if DEVICE_SERIAL
+        if (!stdio_uart_inited) init_serial();
+#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
+        for (unsigned int i = 0; i < length; i++) {
+            if (buffer[i] == '\n' && stdio_out_prev != '\r') {
+                 serial_putc(&stdio_uart, '\r');
+            }
+            serial_putc(&stdio_uart, buffer[i]);
+            stdio_out_prev = buffer[i];
+        }
+#else
+        for (unsigned int i = 0; i < length; i++) {
+            serial_putc(&stdio_uart, buffer[i]);
+        }
+#endif
+#endif
+        n = length;
+    } else {
+        FileLike* fhc = filehandles[fh-3];
+        if (fhc == NULL) return -1;
+
+        n = fhc->write(buffer, length);
+        if (n < 0) {
+            errno = -n;
+        }
+    }
+#ifdef __ARMCC_VERSION
+    return length-n;
+#else
+    return n;
+#endif
+}
+
+#if defined(__ICCARM__)
+extern "C" size_t    __read (int        fh, unsigned char *buffer, size_t       length) {
+#else
+extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int length, int mode) {
+#endif
+    int n; // n is the number of bytes read
+
+    errno = EBADF;
+    if (fh < 3) {
+        // only read a character at a time from stdin
+#if DEVICE_SERIAL
+        if (!stdio_uart_inited) init_serial();
+#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
+        while (true) {
+            char c = serial_getc(&stdio_uart);
+            if ((c == '\r' && stdio_in_prev != '\n') ||
+                (c == '\n' && stdio_in_prev != '\r')) {
+                stdio_in_prev = c;
+                *buffer = '\n';
+                break;
+            } else if ((c == '\r' && stdio_in_prev == '\n') ||
+                       (c == '\n' && stdio_in_prev == '\r')) {
+                stdio_in_prev = c;
+                // onto next character
+                continue;
+            } else {
+                stdio_in_prev = c;
+                *buffer = c;
+                break;
+            }
+        }
+#else
+        *buffer = serial_getc(&stdio_uart);
+#endif
+#endif
+        n = 1;
+    } else {
+        FileLike* fhc = filehandles[fh-3];
+        if (fhc == NULL) return -1;
+
+        n = fhc->read(buffer, length);
+        if (n < 0) {
+            errno = -n;
+        }
+    }
+#ifdef __ARMCC_VERSION
+    return length-n;
+#else
+    return n;
+#endif
+}
+
+#ifdef __ARMCC_VERSION
+extern "C" int PREFIX(_istty)(FILEHANDLE fh)
+#else
+extern "C" int _isatty(FILEHANDLE fh)
+#endif
+{
+    errno = EBADF;
+    /* stdin, stdout and stderr should be tty */
+    if (fh < 3) return 1;
+
+    FileLike* fhc = filehandles[fh-3];
+    if (fhc == NULL) return -1;
+
+    int err = fhc->isatty();
+    if (err < 0) {
+        errno = -err;
+        return -1;
+    } else {
+        return 0;
+    }
+}
+
+extern "C"
+#if defined(__ARMCC_VERSION)
+int _sys_seek(FILEHANDLE fh, long position)
+#elif defined(__ICCARM__)
+long __lseek(int fh, long offset, int whence)
+#else
+int _lseek(FILEHANDLE fh, int offset, int whence)
+#endif
+{
+    errno = EBADF;
+    if (fh < 3) return 0;
+
+    FileLike* fhc = filehandles[fh-3];
+    if (fhc == NULL) return -1;
+
+#if defined(__ARMCC_VERSION)
+    return fhc->seek(position, SEEK_SET);
+#else
+    return fhc->seek(offset, whence);
+#endif
+}
+
+#ifdef __ARMCC_VERSION
+extern "C" int PREFIX(_ensure)(FILEHANDLE fh) {
+    errno = EBADF;
+    if (fh < 3) return 0;
+
+    FileLike* fhc = filehandles[fh-3];
+    if (fhc == NULL) return -1;
+
+    int err = fhc->sync();
+    if (err < 0) {
+        errno = -err;
+        return -1;
+    } else {
+        return 0;
+    }
+}
+
+extern "C" long PREFIX(_flen)(FILEHANDLE fh) {
+    errno = EBADF;
+    if (fh < 3) return 0;
+
+    FileLike* fhc = filehandles[fh-3];
+    if (fhc == NULL) return -1;
+
+    return fhc->size();
+}
+#endif
+
+
+#if !defined(__ARMCC_VERSION) && !defined(__ICCARM__)
+extern "C" int _fstat(int fd, struct stat *st) {
+    if (fd < 3) {
+        st->st_mode = S_IFCHR;
+        return  0;
+    }
+    errno = EBADF;
+    return -1;
+}
+#endif
+
+namespace std {
+extern "C" int remove(const char *path) {
+#if MBED_CONF_FILESYSTEM_PRESENT
+    errno = EBADF;
+    FilePath fp(path);
+    FileSystem *fs = fp.fileSystem();
+    if (fs == NULL) return -1;
+
+    int err = fs->remove(fp.fileName());
+    if (err < 0) {
+        errno = -err;
+        return -1;
+    } else {
+        return 0;
+    }
+#else
+    errno = ENOSYS;
+    return -1;
+#endif
+}
+
+extern "C" int rename(const char *oldname, const char *newname) {
+#if MBED_CONF_FILESYSTEM_PRESENT
+    errno = EBADF;
+    FilePath fpOld(oldname);
+    FilePath fpNew(newname);
+    FileSystem *fsOld = fpOld.fileSystem();
+    FileSystem *fsNew = fpNew.fileSystem();
+
+    /* rename only if both files are on the same FS */
+    if (fsOld != fsNew || fsOld == NULL) return -1;
+
+    int err = fsOld->rename(fpOld.fileName(), fpNew.fileName());
+    if (err < 0) {
+        errno = -err;
+        return -1;
+    } else {
+        return 0;
+    }
+#else
+    errno = ENOSYS;
+    return -1;
+#endif
+}
+
+extern "C" char *tmpnam(char *s) {
+    errno = EBADF;
+    return NULL;
+}
+
+extern "C" FILE *tmpfile() {
+    errno = EBADF;
+    return NULL;
+}
+} // namespace std
+
+#ifdef __ARMCC_VERSION
+extern "C" char *_sys_command_string(char *cmd, int len) {
+    return NULL;
+}
+#endif
+
+extern "C" DIR *opendir(const char *path) {
+#if MBED_CONF_FILESYSTEM_PRESENT
+    errno = EBADF;
+
+    FilePath fp(path);
+    FileSystem* fs = fp.fileSystem();
+    if (fs == NULL) return NULL;
+
+    Dir *dir = new ManagedDir;
+    int err = dir->open(fs, fp.fileName());
+    if (err < 0) {
+        errno = -err;
+        delete dir;
+        dir = NULL;
+    }
+
+    return dir;
+#else
+    errno = ENOSYS;
+    return 0;
+#endif
+}
+
+extern "C" struct dirent *readdir(DIR *dir) {
+#if MBED_CONF_FILESYSTEM_PRESENT
+    static struct dirent ent;
+    int err = dir->read(&ent);
+    if (err < 1) {
+        if (err < 0) {
+            errno = -err;
+        }
+        return NULL;
+    }
+
+    return &ent;
+#else
+    errno = ENOSYS;
+    return 0;
+#endif
+}
+
+extern "C" int closedir(DIR *dir) {
+#if MBED_CONF_FILESYSTEM_PRESENT
+    int err = dir->close();
+    if (err < 0) {
+        errno = -err;
+        return -1;
+    } else {
+        return 0;
+    }
+#else
+    errno = ENOSYS;
+    return -1;
+#endif
+}
+
+extern "C" void rewinddir(DIR *dir) {
+#if MBED_CONF_FILESYSTEM_PRESENT
+    dir->rewind();
+#else
+    errno = ENOSYS;
+#endif
+}
+
+extern "C" off_t telldir(DIR *dir) {
+#if MBED_CONF_FILESYSTEM_PRESENT
+    return dir->tell();
+#else
+    errno = ENOSYS;
+    return 0;
+#endif
+}
+
+extern "C" void seekdir(DIR *dir, off_t off) {
+#if MBED_CONF_FILESYSTEM_PRESENT
+    dir->seek(off);
+#else
+    errno = ENOSYS;
+#endif
+}
+
+extern "C" int mkdir(const char *path, mode_t mode) {
+#if MBED_CONF_FILESYSTEM_PRESENT
+    FilePath fp(path);
+    FileSystem *fs = fp.fileSystem();
+    if (fs == NULL) return -1;
+
+    int err = fs->mkdir(fp.fileName(), mode);
+    if (err < 0) {
+        errno = -err;
+        return -1;
+    } else {
+        return 0;
+    }
+#else
+    errno = ENOSYS;
+    return -1;
+#endif
+}
+
+extern "C" int stat(const char *path, struct stat *st) {
+#if MBED_CONF_FILESYSTEM_PRESENT
+    FilePath fp(path);
+    FileSystem *fs = fp.fileSystem();
+    if (fs == NULL) return -1;
+
+    int err = fs->stat(fp.fileName(), st);
+    if (err < 0) {
+        errno = -err;
+        return -1;
+    } else {
+        return 0;
+    }
+#else
+    errno = ENOSYS;
+    return -1;
+#endif
+}
+
+#if defined(TOOLCHAIN_GCC)
+/* prevents the exception handling name demangling code getting pulled in */
+#include "mbed_error.h"
+namespace __gnu_cxx {
+    void __verbose_terminate_handler() {
+        error("Exception");
+    }
+}
+extern "C" WEAK void __cxa_pure_virtual(void);
+extern "C" WEAK void __cxa_pure_virtual(void) {
+    exit(1);
+}
+
+#endif
+
+#if defined(TOOLCHAIN_GCC)
+
+#ifdef  FEATURE_UVISOR
+#include "uvisor-lib/uvisor-lib.h"
+#endif/* FEATURE_UVISOR */
+
+
+extern "C" WEAK void software_init_hook_rtos(void)
+{
+    // Do nothing by default.
+}
+
+extern "C" void software_init_hook(void)
+{
+#ifdef   FEATURE_UVISOR
+    int return_code;
+
+    return_code = uvisor_lib_init();
+    if (return_code) {
+        mbed_die();
+    }
+#endif/* FEATURE_UVISOR */
+    mbed_sdk_init();
+    software_init_hook_rtos();
+}
+#endif
+
+// ****************************************************************************
+// mbed_main is a function that is called before main()
+// mbed_sdk_init() is also a function that is called before main(), but unlike
+// mbed_main(), it is not meant for user code, but for the SDK itself to perform
+// initializations before main() is called.
+
+extern "C" WEAK void mbed_main(void);
+extern "C" WEAK void mbed_main(void) {
+}
+
+#if defined(TOOLCHAIN_ARM)
+extern "C" int $Super$$main(void);
+
+extern "C" int $Sub$$main(void) {
+    mbed_main();
+    return $Super$$main();
+}
+
+extern "C" void _platform_post_stackheap_init (void) {
+    mbed_sdk_init();
+}
+
+#elif defined(TOOLCHAIN_GCC)
+extern "C" int __real_main(void);
+
+extern "C" int __wrap_main(void) {
+    mbed_main();
+    return __real_main();
+}
+#elif defined(TOOLCHAIN_IAR)
+// IAR doesn't have the $Super/$Sub mechanism of armcc, nor something equivalent
+// to ld's --wrap. It does have a --redirect, but that doesn't help, since redirecting
+// 'main' to another symbol looses the original 'main' symbol. However, its startup
+// code will call a function to setup argc and argv (__iar_argc_argv) if it is defined.
+// Since mbed doesn't use argc/argv, we use this function to call our mbed_main.
+extern "C" void __iar_argc_argv() {
+    mbed_main();
+}
+#endif
+
+// Provide implementation of _sbrk (low-level dynamic memory allocation
+// routine) for GCC_ARM which compares new heap pointer with MSP instead of
+// SP.  This make it compatible with RTX RTOS thread stacks.
+#if defined(TOOLCHAIN_GCC_ARM) || defined(TOOLCHAIN_GCC_CR)
+// Linker defined symbol used by _sbrk to indicate where heap should start.
+extern "C" int __end__;
+
+#if defined(TARGET_CORTEX_A)
+extern "C" uint32_t  __HeapLimit;
+#endif
+
+// Turn off the errno macro and use actual global variable instead.
+#undef errno
+extern "C" int errno;
+
+// For ARM7 only
+register unsigned char * stack_ptr __asm ("sp");
+
+// Dynamic memory allocation related syscall.
+#if defined(TARGET_NUMAKER_PFM_NUC472) || defined(TARGET_NUMAKER_PFM_M453)
+// Overwrite _sbrk() to support two region model (heap and stack are two distinct regions).
+// __wrap__sbrk() is implemented in:
+// TARGET_NUMAKER_PFM_NUC472    hal/targets/cmsis/TARGET_NUVOTON/TARGET_NUC472/TARGET_NUMAKER_PFM_NUC472/TOOLCHAIN_GCC_ARM/retarget.c
+// TARGET_NUMAKER_PFM_M453      hal/targets/cmsis/TARGET_NUVOTON/TARGET_M451/TARGET_NUMAKER_PFM_M453/TOOLCHAIN_GCC_ARM/retarget.c
+extern "C" void *__wrap__sbrk(int incr);
+extern "C" caddr_t _sbrk(int incr) {
+    return (caddr_t) __wrap__sbrk(incr);
+}
+#else
+extern "C" caddr_t _sbrk(int incr) {
+    static unsigned char* heap = (unsigned char*)&__end__;
+    unsigned char*        prev_heap = heap;
+    unsigned char*        new_heap = heap + incr;
+
+#if defined(TARGET_ARM7)
+    if (new_heap >= stack_ptr) {
+#elif defined(TARGET_CORTEX_A)
+    if (new_heap >= (unsigned char*)&__HeapLimit) {     /* __HeapLimit is end of heap section */
+#else
+    if (new_heap >= (unsigned char*)__get_MSP()) {
+#endif
+        errno = ENOMEM;
+        return (caddr_t)-1;
+    }
+
+    // Additional heap checking if set
+    if (mbed_heap_size && (new_heap >= mbed_heap_start + mbed_heap_size)) {
+        errno = ENOMEM;
+        return (caddr_t)-1;
+    }
+
+    heap = new_heap;
+    return (caddr_t) prev_heap;
+}
+#endif
+#endif
+
+#if defined(TOOLCHAIN_GCC_ARM) || defined(TOOLCHAIN_GCC_CR)
+extern "C" void _exit(int return_code) {
+#else
+namespace std {
+extern "C" void exit(int return_code) {
+#endif
+
+#if DEVICE_STDIO_MESSAGES
+#if MBED_CONF_PLATFORM_STDIO_FLUSH_AT_EXIT
+    fflush(stdout);
+    fflush(stderr);
+#endif
+#endif
+
+#if DEVICE_SEMIHOST
+    if (mbed_interface_connected()) {
+        semihost_exit();
+    }
+#endif
+    if (return_code) {
+        mbed_die();
+    }
+
+    while (1);
+}
+
+#if !defined(TOOLCHAIN_GCC_ARM) && !defined(TOOLCHAIN_GCC_CR)
+} //namespace std
+#endif
+
+#if defined(TOOLCHAIN_ARM) || defined(TOOLCHAIN_GCC)
+
+// This series of function disable the registration of global destructors
+// in a dynamic table which will be called when the application exit.
+// In mbed, program never exit properly, it dies.
+// More informations about this topic for ARMCC here:
+// http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/6449.html
+extern "C" {
+int __aeabi_atexit(void *object, void (*dtor)(void* /*this*/), void *handle) {
+    return 1;
+}
+
+int __cxa_atexit(void (*dtor)(void* /*this*/), void *object, void *handle) {
+    return 1;
+}
+
+void __cxa_finalize(void *handle) {
+}
+
+} // end of extern "C"
+
+#endif
+
+
+#if defined(TOOLCHAIN_GCC)
+
+/*
+ * Depending on how newlib is  configured, it is often not enough to define
+ * __aeabi_atexit, __cxa_atexit and __cxa_finalize in order to override the
+ * behavior regarding the registration of handlers with atexit.
+ *
+ * To overcome this limitation, exit and atexit are overriden here.
+ */
+extern "C"{
+
+/**
+ * @brief Retarget of exit for GCC.
+ * @details Unlike the standard version, this function doesn't call any function
+ * registered with atexit before calling _exit.
+ */
+void __wrap_exit(int return_code) {
+    _exit(return_code);
+}
+
+/**
+ * @brief Retarget atexit from GCC.
+ * @details This function will always fail and never register any handler to be
+ * called at exit.
+ */
+int __wrap_atexit(void (*func)()) {
+    return 1;
+}
+
+}
+
+#endif
+
+
+
+namespace mbed {
+
+void mbed_set_unbuffered_stream(FILE *_file) {
+#if defined (__ICCARM__)
+    char buf[2];
+    std::setvbuf(_file,buf,_IONBF,NULL);    
+#else
+    setbuf(_file, NULL);
+#endif
+}
+
+int mbed_getc(FILE *_file){
+#if defined (__ICCARM__)
+    /*This is only valid for unbuffered streams*/
+    int res = std::fgetc(_file);
+    if (res>=0){
+        _file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */
+        _file->_Rend = _file->_Wend;
+        _file->_Next = _file->_Wend;
+    }    
+    return res;
+#else    
+    return std::fgetc(_file);
+#endif   
+}
+
+char* mbed_gets(char*s, int size, FILE *_file){
+#if defined (__ICCARM__)
+    /*This is only valid for unbuffered streams*/
+    char *str = fgets(s,size,_file);
+    if (str!=NULL){
+        _file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */
+        _file->_Rend = _file->_Wend;
+        _file->_Next = _file->_Wend;
+    }
+    return str;
+#else    
+    return std::fgets(s,size,_file);
+#endif
+}
+
+} // namespace mbed
+
+#if defined (__ICCARM__)
+// Stub out locks when an rtos is not present
+extern "C" WEAK void __iar_system_Mtxinit(__iar_Rmtx *mutex) {}
+extern "C" WEAK void __iar_system_Mtxdst(__iar_Rmtx *mutex) {}
+extern "C" WEAK void __iar_system_Mtxlock(__iar_Rmtx *mutex) {}
+extern "C" WEAK void __iar_system_Mtxunlock(__iar_Rmtx *mutex) {}
+extern "C" WEAK void __iar_file_Mtxinit(__iar_Rmtx *mutex) {}
+extern "C" WEAK void __iar_file_Mtxdst(__iar_Rmtx *mutex) {}
+extern "C" WEAK void __iar_file_Mtxlock(__iar_Rmtx *mutex) {}
+extern "C" WEAK void __iar_file_Mtxunlock(__iar_Rmtx *mutex) {}
+#elif defined(__CC_ARM)
+// Do nothing
+#elif defined (__GNUC__)
+struct _reent;
+// Stub out locks when an rtos is not present
+extern "C" WEAK void __rtos_malloc_lock( struct _reent *_r ) {}
+extern "C" WEAK void __rtos_malloc_unlock( struct _reent *_r ) {}
+extern "C" WEAK void __rtos_env_lock( struct _reent *_r ) {}
+extern "C" WEAK void __rtos_env_unlock( struct _reent *_r ) {}
+
+extern "C" void __malloc_lock( struct _reent *_r )
+{
+    __rtos_malloc_lock(_r);
+}
+
+extern "C" void __malloc_unlock( struct _reent *_r )
+{
+    __rtos_malloc_unlock(_r);
+}
+
+extern "C" void __env_lock( struct _reent *_r )
+{
+    __rtos_env_lock(_r);
+}
+
+extern "C" void __env_unlock( struct _reent *_r )
+{
+    __rtos_env_unlock(_r);
+}
+
+#define CXA_GUARD_INIT_DONE             (1 << 0)
+#define CXA_GUARD_INIT_IN_PROGRESS      (1 << 1)
+#define CXA_GUARD_MASK                  (CXA_GUARD_INIT_DONE | CXA_GUARD_INIT_IN_PROGRESS)
+
+extern "C" int __cxa_guard_acquire(int *guard_object_p)
+{
+    uint8_t *guard_object = (uint8_t *)guard_object_p;
+    if (CXA_GUARD_INIT_DONE == (*guard_object & CXA_GUARD_MASK)) {
+        return 0;
+    }
+    singleton_lock();
+    if (CXA_GUARD_INIT_DONE == (*guard_object & CXA_GUARD_MASK)) {
+        singleton_unlock();
+        return 0;
+    }
+    MBED_ASSERT(0 == (*guard_object & CXA_GUARD_MASK));
+    *guard_object = *guard_object | CXA_GUARD_INIT_IN_PROGRESS;
+    return 1;
+}
+
+extern "C" void __cxa_guard_release(int *guard_object_p)
+{
+    uint8_t *guard_object = (uint8_t *)guard_object_p;
+    MBED_ASSERT(CXA_GUARD_INIT_IN_PROGRESS == (*guard_object & CXA_GUARD_MASK));
+    *guard_object = (*guard_object & ~CXA_GUARD_MASK) | CXA_GUARD_INIT_DONE;
+    singleton_unlock();
+}
+
+extern "C" void __cxa_guard_abort(int *guard_object_p)
+{
+    uint8_t *guard_object = (uint8_t *)guard_object_p;
+    MBED_ASSERT(CXA_GUARD_INIT_IN_PROGRESS == (*guard_object & CXA_GUARD_MASK));
+    *guard_object = *guard_object & ~CXA_GUARD_INIT_IN_PROGRESS;
+    singleton_unlock();
+}
+
+#endif
+
+void *operator new(std::size_t count)
+{
+    void *buffer = malloc(count);
+    if (NULL == buffer) {
+        error("Operator new out of memory\r\n");
+    }
+    return buffer;
+}
+
+void *operator new[](std::size_t count)
+{
+    void *buffer = malloc(count);
+    if (NULL == buffer) {
+        error("Operator new[] out of memory\r\n");
+    }
+    return buffer;
+}
+
+void operator delete(void *ptr)
+{
+    if (ptr != NULL) {
+        free(ptr);
+    }
+}
+void operator delete[](void *ptr)
+{
+    if (ptr != NULL) {
+        free(ptr);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/platform/mbed_retarget.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,185 @@
+/*
+ * mbed Microcontroller Library
+ * Copyright (c) 2006-2016 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef RETARGET_H
+#define RETARGET_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+/* 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
+ * target embedded systems */
+#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
+typedef int ssize_t;    ///< Signed size type, usually encodes negative errors
+typedef long off_t;     ///< Offset in a data stream
+typedef int mode_t;     ///< Mode for opening files
+
+#define O_RDONLY 0
+#define O_WRONLY 1
+#define O_RDWR   2
+#define O_CREAT  0x0200
+#define O_TRUNC  0x0400
+#define O_APPEND 0x0008
+
+#define NAME_MAX 255    ///< Maximum size of a name in a file path
+
+#else
+#include <sys/fcntl.h>
+#include <sys/types.h>
+#include <sys/syslimits.h>
+#endif
+
+
+/* DIR declarations must also be here */
+#if __cplusplus
+namespace mbed { class Dir; }
+typedef mbed::Dir DIR;
+
+extern "C" {
+    DIR *opendir(const char*);
+    struct dirent *readdir(DIR *);
+    int closedir(DIR*);
+    void rewinddir(DIR*);
+    long telldir(DIR*);
+    void seekdir(DIR*, long);
+    int mkdir(const char *name, mode_t n);
+};
+#endif
+
+
+#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
+/* The intent of this section is to unify the errno error values to match
+ * the POSIX definitions for the GCC_ARM, ARMCC and IAR compilers. This is
+ * necessary because the ARMCC/IAR errno.h, or sys/stat.h are missing some
+ * symbol definitions used by the POSIX filesystem API to return errno codes.
+ * Note also that ARMCC errno.h defines some symbol values differently from
+ * the GCC_ARM/IAR/standard POSIX definitions. The definitions guard against
+ * this and future changes by changing the symbol definition as shown below. */
+#ifdef ENOENT
+#undef ENOENT
+#endif
+#define ENOENT      2       /* No such file or directory. */
+
+#ifdef EIO
+#undef EIO
+#endif
+#define EIO         5       /* I/O error */
+
+#ifdef ENXIO
+#undef ENXIO
+#endif
+#define ENXIO       6       /* No such device or address */
+
+#ifdef ENOEXEC
+#undef ENOEXEC
+#endif
+#define ENOEXEC     8       /* Exec format error */
+
+#ifdef EBADF
+#undef EBADF
+#endif
+#define EBADF       9       /* Bad file number */
+
+#ifdef ENOMEM
+#undef ENOMEM
+#endif
+#define ENOMEM      12      /* Not enough space */
+
+#ifdef EACCES
+#undef EACCES
+#endif
+#define EACCES      13      /* Permission denied */
+
+#ifdef EFAULT
+#undef EFAULT
+#endif
+#define EFAULT      14      /* Bad address */
+
+#ifdef EEXIST
+#undef EEXIST
+#endif
+#define EEXIST      17      /* File exists */
+
+#ifdef EINVAL
+#undef EINVAL
+#endif
+#define EINVAL      22      /* Invalid argument */
+
+#ifdef ENFILE
+#undef ENFILE
+#endif
+#define ENFILE      23      /* Too many open files in system */
+
+#ifdef EMFILE
+#undef EMFILE
+#endif
+#define EMFILE      24      /* File descriptor value too large */
+
+#ifdef ENOSYS
+#undef ENOSYS
+#endif
+#define ENOSYS      38      /* Function not implemented */
+
+/* Missing stat.h defines.
+ * The following are sys/stat.h definitions not currently present in the ARMCC
+ * errno.h. Note, ARMCC errno.h defines some symbol values differing from
+ * GCC_ARM/IAR/standard POSIX definitions. Guard against this and future
+ * changes by changing the symbol definition for filesystem use. */
+#define     _IFDIR  0040000 /* directory */
+#define     _IFREG  0100000 /* regular */
+
+#define S_IFDIR     _IFDIR
+#define S_IFREG     _IFREG
+
+#define S_IRWXU     (S_IRUSR | S_IWUSR | S_IXUSR)
+#define     S_IRUSR 0000400 /* read permission, owner */
+#define     S_IWUSR 0000200 /* write permission, owner */
+#define     S_IXUSR 0000100/* execute/search permission, owner */
+#define S_IRWXG     (S_IRGRP | S_IWGRP | S_IXGRP)
+#define     S_IRGRP 0000040 /* read permission, group */
+#define     S_IWGRP 0000020 /* write permission, grougroup */
+#define     S_IXGRP 0000010/* execute/search permission, group */
+#define S_IRWXO     (S_IROTH | S_IWOTH | S_IXOTH)
+#define     S_IROTH 0000004 /* read permission, other */
+#define     S_IWOTH 0000002 /* write permission, other */
+#define     S_IXOTH 0000001/* execute/search permission, other */
+
+#endif /* defined(__ARMCC_VERSION) || defined(__ICCARM__) */
+
+
+/* The following are dirent.h definitions are declared here to garuntee
+ * consistency where structure may be different with different toolchains */
+struct dirent {
+    char d_name[NAME_MAX+1];
+    uint8_t d_type;
+};
+
+enum {
+    DT_UNKNOWN, // The file type could not be determined.
+    DT_FIFO,    // This is a named pipe (FIFO).
+    DT_CHR,     // This is a character device.
+    DT_DIR,     // This is a directory.
+    DT_BLK,     // This is a block device.
+    DT_REG,     // This is a regular file.
+    DT_LNK,     // This is a symbolic link.
+    DT_SOCK,    // This is a UNIX domain socket.
+};
+
+
+#endif /* RETARGET_H */
--- a/platform/mbed_rtc_time.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/mbed_rtc_time.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -16,8 +16,8 @@
 #include "hal/rtc_api.h"
 
 #include <time.h>
-#include "platform/critical.h"
-#include "platform/rtc_time.h"
+#include "platform/mbed_critical.h"
+#include "platform/mbed_rtc_time.h"
 #include "hal/us_ticker_api.h"
 #include "platform/SingletonPtr.h"
 #include "platform/PlatformMutex.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/platform/mbed_rtc_time.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,92 @@
+
+/** \addtogroup platform */
+/** @{*/
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <time.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** Implementation of the C time.h functions
+ *
+ * Provides mechanisms to set and read the current time, based
+ * on the microcontroller Real-Time Clock (RTC), plus some
+ * standard C manipulation and formating functions.
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ *
+ * int main() {
+ *     set_time(1256729737);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
+ *
+ *     while(1) {
+ *         time_t seconds = time(NULL);
+ *
+ *         printf("Time as seconds since January 1, 1970 = %d\n", seconds);
+ *
+ *         printf("Time as a basic string = %s", ctime(&seconds));
+ *
+ *         char buffer[32];
+ *         strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
+ *         printf("Time as a custom formatted string = %s", buffer);
+ *
+ *         wait(1);
+ *     }
+ * }
+ * @endcode
+ */
+
+/** Set the current time
+ *
+ * Initialises and sets the time of the microcontroller Real-Time Clock (RTC)
+ * to the time represented by the number of seconds since January 1, 1970
+ * (the UNIX timestamp).
+ *
+ * @param t Number of seconds since January 1, 1970 (the UNIX timestamp)
+ *
+ * @Note Synchronization level: Thread safe
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ *
+ * int main() {
+ *     set_time(1256729737); // Set time to Wed, 28 Oct 2009 11:35:37
+ * }
+ * @endcode
+ */
+void set_time(time_t t);
+
+/** Attach an external RTC to be used for the C time functions
+ *
+ * @Note Synchronization level: Thread safe
+ *
+ * @param read_rtc pointer to function which returns current UNIX timestamp
+ * @param write_rtc pointer to function which sets current UNIX timestamp, can be NULL
+ * @param init_rtc pointer to funtion which initializes RTC, can be NULL
+ * @param isenabled_rtc pointer to function wich returns if the rtc is enabled, can be NULL
+ */
+void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void));
+
+#ifdef __cplusplus
+}
+#endif
+
+/** @}*/
--- a/platform/mbed_semihost_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/mbed_semihost_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 #include "cmsis.h"
-#include "platform/semihost_api.h"
+#include "platform/mbed_semihost_api.h"
 
 #include <stdint.h>
 #include <string.h>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/platform/mbed_semihost_api.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,98 @@
+
+/** \addtogroup platform */
+/** @{*/
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef MBED_SEMIHOST_H
+#define MBED_SEMIHOST_H
+
+#include "device.h"
+#include "platform/mbed_toolchain.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if DEVICE_SEMIHOST
+
+#ifndef __CC_ARM
+
+#if defined(__ICCARM__)
+static inline int __semihost(int reason, const void *arg) {
+    return __semihosting(reason, (void*)arg);
+}
+#else
+
+#ifdef __thumb__
+#   define AngelSWI            0xAB
+#   define AngelSWIInsn        "bkpt"
+#   define AngelSWIAsm          bkpt
+#else
+#   define AngelSWI            0x123456
+#   define AngelSWIInsn        "swi"
+#   define AngelSWIAsm          swi
+#endif
+
+static inline int __semihost(int reason, const void *arg) {
+    int value;
+
+    asm volatile (
+       "mov r0, %1"          "\n\t"
+       "mov r1, %2"          "\n\t"
+       AngelSWIInsn " %a3"   "\n\t"
+       "mov %0, r0"
+       : "=r" (value)                                         /* output operands             */
+       : "r" (reason), "r" (arg), "i" (AngelSWI)              /* input operands              */
+       : "r0", "r1", "r2", "r3", "ip", "lr", "memory", "cc"   /* list of clobbered registers */
+    );
+
+    return value;
+}
+#endif
+#endif
+
+#if DEVICE_LOCALFILESYSTEM
+FILEHANDLE semihost_open(const char* name, int openmode);
+int semihost_close (FILEHANDLE fh);
+int semihost_read  (FILEHANDLE fh, unsigned char* buffer, unsigned int length, int mode);
+int semihost_write (FILEHANDLE fh, const unsigned char* buffer, unsigned int length, int mode);
+int semihost_ensure(FILEHANDLE fh);
+long semihost_flen (FILEHANDLE fh);
+int semihost_seek  (FILEHANDLE fh, long position);
+int semihost_istty (FILEHANDLE fh);
+
+int semihost_remove(const char *name);
+int semihost_rename(const char *old_name, const char *new_name);
+#endif
+
+int semihost_uid(char *uid);
+int semihost_reset(void);
+int semihost_vbus(void);
+int semihost_powerdown(void);
+int semihost_exit(void);
+
+int semihost_connected(void);
+int semihost_disabledebug(void);
+
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
+/** @}*/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/platform/mbed_sleep.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,85 @@
+
+/** \addtogroup platform */
+/** @{*/
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef MBED_SLEEP_H
+#define MBED_SLEEP_H
+
+#include "sleep_api.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** Send the microcontroller to sleep
+ *
+ * @note This function can be a noop if not implemented by the platform.
+ * @note This function will only put device to sleep in release mode (small profile or when NDEBUG is defined).
+ *
+ * The processor is setup ready for sleep, and sent to sleep using __WFI(). In this mode, the
+ * system clock to the core is stopped until a reset or an interrupt occurs. This eliminates
+ * dynamic power used by the processor, memory systems and buses. The processor, peripheral and
+ * memory state are maintained, and the peripherals continue to work and can generate interrupts.
+ *
+ * The processor can be woken up by any internal peripheral interrupt or external pin interrupt.
+ *
+ * @note
+ *  The mbed interface semihosting is disconnected as part of going to sleep, and can not be restored.
+ * Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
+ * able to access the LocalFileSystem
+ */
+__INLINE static void sleep(void)
+{
+#ifdef NDEBUG
+#if DEVICE_SLEEP
+    hal_sleep();
+#endif /* DEVICE_SLEEP */
+#endif /* NDEBUG */
+}
+
+/** Send the microcontroller to deep sleep
+ *
+ * @note This function can be a noop if not implemented by the platform.
+ * @note This function will only put device to sleep in release mode (small profile or when NDEBUG is defined).
+ *
+ * This processor is setup ready for deep sleep, and sent to sleep using __WFI(). This mode
+ * has the same sleep features as sleep plus it powers down peripherals and clocks. All state
+ * is still maintained.
+ *
+ * The processor can only be woken up by an external interrupt on a pin or a watchdog timer.
+ *
+ * @note
+ *  The mbed interface semihosting is disconnected as part of going to sleep, and can not be restored.
+ * Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
+ * able to access the LocalFileSystem
+ */
+__INLINE static void deepsleep(void)
+{
+#ifdef NDEBUG
+#if DEVICE_SLEEP
+    hal_deepsleep();
+#endif /* DEVICE_SLEEP */
+#endif /* NDEBUG */
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
+/** @}*/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/platform/mbed_toolchain.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,305 @@
+
+/** \addtogroup platform */
+/** @{*/
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef MBED_TOOLCHAIN_H
+#define MBED_TOOLCHAIN_H
+
+#include "mbed_preprocessor.h"
+
+
+// Warning for unsupported compilers
+#if !defined(__GNUC__)   /* GCC        */ \
+ && !defined(__CC_ARM)   /* ARMCC      */ \
+ && !defined(__clang__)  /* LLVM/Clang */ \
+ && !defined(__ICCARM__) /* IAR        */
+#warning "This compiler is not yet supported."
+#endif
+
+
+// Attributes
+
+/** MBED_PACKED
+ *  Pack a structure, preventing any padding from being added between fields.
+ *
+ *  @code
+ *  #include "mbed_toolchain.h"
+ *
+ *  MBED_PACKED(struct) foo {
+ *      char x;
+ *      int y;
+ *  };
+ *  @endcode
+ */
+#ifndef MBED_PACKED
+#if defined(__ICCARM__)
+#define MBED_PACKED(struct) __packed struct
+#else
+#define MBED_PACKED(struct) struct __attribute__((packed))
+#endif
+#endif
+
+/** MBED_ALIGN(N)
+ *  Declare a variable to be aligned on an N-byte boundary.
+ *
+ *  @note
+ *  IAR does not support alignment greater than word size on the stack
+ *  
+ *  @code
+ *  #include "mbed_toolchain.h"
+ *
+ *  MBED_ALIGN(16) char a;
+ *  @endcode
+ */
+#ifndef MBED_ALIGN
+#if defined(__ICCARM__)
+#define MBED_ALIGN(N) _Pragma(MBED_STRINGIFY(data_alignment=N))
+#else
+#define MBED_ALIGN(N) __attribute__((aligned(N)))
+#endif
+#endif
+
+/** MBED_UNUSED
+ *  Declare a function argument to be unused, suppressing compiler warnings
+ *
+ *  @code
+ *  #include "mbed_toolchain.h"
+ *
+ *  void foo(MBED_UNUSED int arg) {
+ *
+ *  }
+ *  @endcode
+ */
+#ifndef MBED_UNUSED
+#if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
+#define MBED_UNUSED __attribute__((__unused__))
+#else
+#define MBED_UNUSED
+#endif
+#endif
+
+/** MBED_WEAK
+ *  Mark a function as being weak.
+ *  
+ *  @note
+ *  weak functions are not friendly to making code re-usable, as they can only
+ *  be overridden once (and if they are multiply overridden the linker will emit
+ *  no warning). You should not normally use weak symbols as part of the API to
+ *  re-usable modules.
+ *  
+ *  @code
+ *  #include "mbed_toolchain.h"
+ *  
+ *  MBED_WEAK void foo() {
+ *      // a weak implementation of foo that can be overriden by a definition
+ *      // without  __weak
+ *  }
+ *  @endcode
+ */
+#ifndef MBED_WEAK
+#if defined(__ICCARM__)
+#define MBED_WEAK __weak
+#else
+#define MBED_WEAK __attribute__((weak))
+#endif
+#endif
+
+/** MBED_PURE
+ *  Hint to the compiler that a function depends only on parameters
+ *
+ *  @code
+ *  #include "mbed_toolchain.h"
+ *
+ *  MBED_PURE int foo(int arg){
+ *      // no access to global variables
+ *  }
+ *  @endcode
+ */
+#ifndef MBED_PURE
+#if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
+#define MBED_PURE __attribute__((const))
+#else
+#define MBED_PURE
+#endif
+#endif
+
+/** MBED_FORCEINLINE
+ *  Declare a function that must always be inlined. Failure to inline
+ *  such a function will result in an error.
+ *
+ *  @code
+ *  #include "mbed_toolchain.h"
+ *  
+ *  MBED_FORCEINLINE void foo() {
+ *  
+ *  }
+ *  @endcode
+ */
+#ifndef MBED_FORCEINLINE
+#if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
+#define MBED_FORCEINLINE static inline __attribute__((always_inline))
+#elif defined(__ICCARM__)
+#define MBED_FORCEINLINE _Pragma("inline=forced") static
+#else
+#define MBED_FORCEINLINE static inline
+#endif
+#endif
+
+/** MBED_NORETURN
+ *  Declare a function that will never return.
+ *
+ *  @code
+ *  #include "mbed_toolchain.h"
+ *  
+ *  MBED_NORETURN void foo() {
+ *      // must never return
+ *      while (1) {}
+ *  }
+ *  @endcode
+ */
+#ifndef MBED_NORETURN
+#if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
+#define MBED_NORETURN __attribute__((noreturn))
+#elif defined(__ICCARM__)
+#define MBED_NORETURN __noreturn
+#else
+#define MBED_NORETURN
+#endif
+#endif
+
+/** MBED_UNREACHABLE
+ *  An unreachable statement. If the statement is reached,
+ *  behaviour is undefined. Useful in situations where the compiler
+ *  cannot deduce the unreachability of code.
+ *
+ *  @code
+ *  #include "mbed_toolchain.h"
+ *
+ *  void foo(int arg) {
+ *      switch (arg) {
+ *          case 1: return 1;
+ *          case 2: return 2;
+ *          ...
+ *      }
+ *      MBED_UNREACHABLE;
+ *  }
+ *  @endcode
+ */
+#ifndef MBED_UNREACHABLE
+#if (defined(__GNUC__) || defined(__clang__)) && !defined(__CC_ARM)
+#define MBED_UNREACHABLE __builtin_unreachable()
+#else
+#define MBED_UNREACHABLE while (1)
+#endif
+#endif
+
+/** MBED_DEPRECATED("message string")
+ *  Mark a function declaration as deprecated, if it used then a warning will be
+ *  issued by the compiler possibly including the provided message. Note that not
+ *  all compilers are able to display the message.
+ *
+ *  @code
+ *  #include "mbed_toolchain.h"
+ *  
+ *  MBED_DEPRECATED("don't foo any more, bar instead")
+ *  void foo(int arg);
+ *  @endcode
+ */
+#ifndef MBED_DEPRECATED
+#if defined(__CC_ARM)
+#define MBED_DEPRECATED(M) __attribute__((deprecated))
+#elif defined(__GNUC__) || defined(__clang__)
+#define MBED_DEPRECATED(M) __attribute__((deprecated(M)))
+#else
+#define MBED_DEPRECATED(M)
+#endif
+#endif
+
+/** MBED_DEPRECATED_SINCE("version", "message string")
+ *  Mark a function declaration as deprecated, noting that the declaration was
+ *  deprecated on the specified version. If the function is used then a warning
+ *  will be issued by the compiler possibly including the provided message.
+ *  Note that not all compilers are able to display this message.
+ *
+ *  @code
+ *  #include "mbed_toolchain.h"
+ *
+ *  MBED_DEPRECATED_SINCE("mbed-os-5.1", "don't foo any more, bar instead")
+ *  void foo(int arg);
+ *  @endcode
+ */
+#define MBED_DEPRECATED_SINCE(D, M) MBED_DEPRECATED(M " [since " D "]")
+
+/** MBED_CALLER_ADDR()
+ * Returns the caller of the current function.
+ *
+ * @note
+ * This macro is only implemented for GCC and ARMCC.
+ *
+ * @code
+ * #include "mbed_toolchain.h"
+ *
+ * printf("This function was called from %p", MBED_CALLER_ADDR());
+ * @endcode
+ *
+ * @return Address of the calling function
+ */
+#ifndef MBED_CALLER_ADDR
+#if (defined(__GNUC__) || defined(__clang__)) && !defined(__CC_ARM)
+#define MBED_CALLER_ADDR() __builtin_extract_return_addr(__builtin_return_address(0))
+#elif defined(__CC_ARM)
+#define MBED_CALLER_ADDR() __builtin_return_address(0)
+#else
+#define MBED_CALLER_ADDR() (NULL)
+#endif
+#endif
+
+#ifndef MBED_SECTION
+#if (defined(__GNUC__) || defined(__clang__)) || defined(__CC_ARM)
+#define MBED_SECTION(name) __attribute__ ((section (name)))
+#elif defined(__ICCARM__)
+#define MBED_SECTION(name) _Pragma(MBED_STRINGIFY(location=name))
+#else
+#error "Missing MBED_SECTION directive"
+#endif
+#endif
+
+// FILEHANDLE declaration
+#if defined(TOOLCHAIN_ARM)
+#include <rt_sys.h>
+#endif
+
+#ifndef FILEHANDLE
+typedef int FILEHANDLE;
+#endif
+
+// Backwards compatibility
+#ifndef WEAK
+#define WEAK MBED_WEAK
+#endif
+
+#ifndef PACKED
+#define PACKED MBED_PACKED()
+#endif
+
+#ifndef EXTERN
+#define EXTERN extern
+#endif
+
+#endif
+
+/** @}*/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/platform/mbed_wait_api.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,71 @@
+
+/** \addtogroup platform */
+/** @{*/
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef MBED_WAIT_API_H
+#define MBED_WAIT_API_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** Generic wait functions.
+ *
+ * These provide simple NOP type wait capabilities.
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ *
+ * DigitalOut heartbeat(LED1);
+ *
+ * int main() {
+ *     while (1) {
+ *         heartbeat = 1;
+ *         wait(0.5);
+ *         heartbeat = 0;
+ *         wait(0.5);
+ *     }
+ * }
+ */
+
+/** Waits for a number of seconds, with microsecond resolution (within
+ *  the accuracy of single precision floating point).
+ *
+ *  @param s number of seconds to wait
+ */
+void wait(float s);
+
+/** Waits a number of milliseconds.
+ *
+ *  @param ms the whole number of milliseconds to wait
+ */
+void wait_ms(int ms);
+
+/** Waits a number of microseconds.
+ *
+ *  @param us the whole number of microseconds to wait
+ */
+void wait_us(int us);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
+/** @}*/
--- a/platform/mbed_wait_api_no_rtos.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/mbed_wait_api_no_rtos.c	Tue Mar 14 16:40:56 2017 +0000
@@ -18,7 +18,7 @@
 // if the RTOS is not present.
 #ifndef MBED_CONF_RTOS_PRESENT
 
-#include "platform/wait_api.h"
+#include "platform/mbed_wait_api.h"
 #include "hal/us_ticker_api.h"
 
 void wait(float s) {
--- a/platform/mbed_wait_api_rtos.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/mbed_wait_api_rtos.cpp	Tue Mar 14 16:40:56 2017 +0000
@@ -18,10 +18,10 @@
 // if the RTOS is present.
 #ifdef MBED_CONF_RTOS_PRESENT
 
-#include "platform/wait_api.h"
+#include "platform/mbed_wait_api.h"
 #include "hal/us_ticker_api.h"
 #include "rtos/rtos.h"
-#include "platform/critical.h"
+#include "platform/mbed_critical.h"
 
 void wait(float s) {
     wait_us(s * 1000000.0f);
--- a/platform/platform.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/platform.h	Tue Mar 14 16:40:56 2017 +0000
@@ -24,6 +24,8 @@
 #include <cstdio>
 #include <cstring>
 
+#include "platform/mbed_retarget.h"
+#include "platform/mbed_toolchain.h"
 #include "device.h"
 #include "PinNames.h"
 #include "PeripheralNames.h"
--- a/platform/retarget.cpp	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,853 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2015 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "platform/platform.h"
-#include "drivers/FileHandle.h"
-#include "drivers/FileSystemLike.h"
-#include "drivers/FilePath.h"
-#include "hal/serial_api.h"
-#include "platform/toolchain.h"
-#include "platform/semihost_api.h"
-#include "platform/mbed_interface.h"
-#include "platform/SingletonPtr.h"
-#include "platform/PlatformMutex.h"
-#include "platform/mbed_error.h"
-#include "platform/mbed_stats.h"
-#include <stdlib.h>
-#include <string.h>
-#if DEVICE_STDIO_MESSAGES
-#include <stdio.h>
-#endif
-#include <errno.h>
-
-#if defined(__ARMCC_VERSION)
-#   include <rt_sys.h>
-#   define PREFIX(x)    _sys##x
-#   define OPEN_MAX     _SYS_OPEN
-#   ifdef __MICROLIB
-#       pragma import(__use_full_stdio)
-#   endif
-
-#elif defined(__ICCARM__)
-#   include <yfuns.h>
-#   define PREFIX(x)        _##x
-#   define OPEN_MAX         16
-
-#   define STDIN_FILENO     0
-#   define STDOUT_FILENO    1
-#   define STDERR_FILENO    2
-
-#else
-#   include <sys/stat.h>
-#   include <sys/unistd.h>
-#   include <sys/syslimits.h>
-#   define PREFIX(x)    x
-#endif
-
-#define FILE_HANDLE_RESERVED    0xFFFFFFFF
-
-using namespace mbed;
-
-#if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
-// Before version 5.03, we were using a patched version of microlib with proper names
-extern const char __stdin_name[]  = ":tt";
-extern const char __stdout_name[] = ":tt";
-extern const char __stderr_name[] = ":tt";
-
-#else
-extern const char __stdin_name[]  = "/stdin";
-extern const char __stdout_name[] = "/stdout";
-extern const char __stderr_name[] = "/stderr";
-#endif
-
-// Heap limits - only used if set
-unsigned char *mbed_heap_start = 0;
-uint32_t mbed_heap_size = 0;
-
-/* newlib has the filehandle field in the FILE struct as a short, so
- * we can't just return a Filehandle* from _open and instead have to
- * put it in a filehandles array and return the index into that array
- * (or rather index+3, as filehandles 0-2 are stdin/out/err).
- */
-static FileHandle *filehandles[OPEN_MAX];
-static SingletonPtr<PlatformMutex> filehandle_mutex;
-
-FileHandle::~FileHandle() {
-    filehandle_mutex->lock();
-    /* Remove all open filehandles for this */
-    for (unsigned int fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) {
-        if (filehandles[fh_i] == this) {
-            filehandles[fh_i] = NULL;
-        }
-    }
-    filehandle_mutex->unlock();
-}
-
-#if DEVICE_SERIAL
-extern int stdio_uart_inited;
-extern serial_t stdio_uart;
-#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
-static char stdio_in_prev;
-static char stdio_out_prev;
-#endif
-#endif
-
-static void init_serial() {
-#if DEVICE_SERIAL
-    if (stdio_uart_inited) return;
-    serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
-#if MBED_CONF_PLATFORM_STDIO_BAUD_RATE
-    serial_baud(&stdio_uart, MBED_CONF_PLATFORM_STDIO_BAUD_RATE);
-#endif
-#endif
-}
-
-static inline int openmode_to_posix(int openmode) {
-    int posix = openmode;
-#ifdef __ARMCC_VERSION
-    if (openmode & OPEN_PLUS) {
-        posix = O_RDWR;
-    } else if(openmode & OPEN_W) {
-        posix = O_WRONLY;
-    } else if(openmode & OPEN_A) {
-        posix = O_WRONLY|O_APPEND;
-    } else {
-        posix = O_RDONLY;
-    }
-    /* a, w, a+, w+ all create if file does not already exist */
-    if (openmode & (OPEN_A|OPEN_W)) {
-        posix |= O_CREAT;
-    }
-    /* w and w+ truncate */
-    if (openmode & OPEN_W) {
-        posix |= O_TRUNC;
-    }
-#elif defined(__ICCARM__)
-    switch (openmode & _LLIO_RDWRMASK) {
-        case _LLIO_RDONLY: posix = O_RDONLY; break;
-        case _LLIO_WRONLY: posix = O_WRONLY; break;
-        case _LLIO_RDWR  : posix = O_RDWR  ; break;
-    }
-    if (openmode & _LLIO_CREAT ) posix |= O_CREAT;
-    if (openmode & _LLIO_APPEND) posix |= O_APPEND;
-    if (openmode & _LLIO_TRUNC ) posix |= O_TRUNC;
-#elif defined(TOOLCHAIN_GCC)
-    posix &= ~O_BINARY;
-#endif
-    return posix;
-}
-
-extern "C" WEAK void mbed_sdk_init(void);
-extern "C" WEAK void mbed_sdk_init(void) {
-}
-
-extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
-    #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
-    // Before version 5.03, we were using a patched version of microlib with proper names
-    // This is the workaround that the microlib author suggested us
-    static int n = 0;
-    static int mbed_sdk_inited = 0;
-    if (!mbed_sdk_inited) {
-        mbed_sdk_inited = 1;
-        mbed_sdk_init();
-    }
-    if (!std::strcmp(name, ":tt")) return n++;
-    #else
-    /* Use the posix convention that stdin,out,err are filehandles 0,1,2.
-     */
-    if (std::strcmp(name, __stdin_name) == 0) {
-        init_serial();
-        return 0;
-    } else if (std::strcmp(name, __stdout_name) == 0) {
-        init_serial();
-        return 1;
-    } else if (std::strcmp(name, __stderr_name) == 0) {
-        init_serial();
-        return 2;
-    }
-    #endif
-
-    // find the first empty slot in filehandles
-    filehandle_mutex->lock();
-    unsigned int fh_i;
-    for (fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) {
-        if (filehandles[fh_i] == NULL) break;
-    }
-    if (fh_i >= sizeof(filehandles)/sizeof(*filehandles)) {
-        filehandle_mutex->unlock();
-        return -1;
-    }
-    filehandles[fh_i] = (FileHandle*)FILE_HANDLE_RESERVED;
-    filehandle_mutex->unlock();
-
-    FileHandle *res;
-
-    /* FILENAME: ":0x12345678" describes a FileLike* */
-    if (name[0] == ':') {
-        void *p;
-        sscanf(name, ":%p", &p);
-        res = (FileHandle*)p;
-
-    /* FILENAME: "/file_system/file_name" */
-    } else {
-        FilePath path(name);
-
-        if (!path.exists()) {
-            // Free file handle
-            filehandles[fh_i] = NULL;
-            return -1;
-        } else if (path.isFile()) {
-            res = path.file();
-        } else {
-            FileSystemLike *fs = path.fileSystem();
-            if (fs == NULL) {
-                // Free file handle
-                filehandles[fh_i] = NULL;
-                return -1;
-            }
-            int posix_mode = openmode_to_posix(openmode);
-            res = fs->open(path.fileName(), posix_mode); /* NULL if fails */
-        }
-    }
-
-    if (res == NULL) {
-        // Free file handle
-        filehandles[fh_i] = NULL;
-        return -1;
-    }
-    filehandles[fh_i] = res;
-
-    return fh_i + 3; // +3 as filehandles 0-2 are stdin/out/err
-}
-
-extern "C" int PREFIX(_close)(FILEHANDLE fh) {
-    if (fh < 3) return 0;
-
-    FileHandle* fhc = filehandles[fh-3];
-    filehandles[fh-3] = NULL;
-    if (fhc == NULL) return -1;
-
-    return fhc->close();
-}
-
-#if defined(__ICCARM__)
-extern "C" size_t    __write (int        fh, const unsigned char *buffer, size_t length) {
-#else
-extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsigned int length, int mode) {
-#endif
-    int n; // n is the number of bytes written
-    if (fh < 3) {
-#if DEVICE_SERIAL
-        if (!stdio_uart_inited) init_serial();
-#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
-        for (unsigned int i = 0; i < length; i++) {
-            if (buffer[i] == '\n' && stdio_out_prev != '\r') {
-                 serial_putc(&stdio_uart, '\r');
-            }
-            serial_putc(&stdio_uart, buffer[i]);
-            stdio_out_prev = buffer[i];
-        }
-#else
-        for (unsigned int i = 0; i < length; i++) {
-            serial_putc(&stdio_uart, buffer[i]);
-        }
-#endif
-#endif
-        n = length;
-    } else {
-        FileHandle* fhc = filehandles[fh-3];
-        if (fhc == NULL) return -1;
-
-        n = fhc->write(buffer, length);
-    }
-#ifdef __ARMCC_VERSION
-    return length-n;
-#else
-    return n;
-#endif
-}
-
-#if defined(__ICCARM__)
-extern "C" size_t    __read (int        fh, unsigned char *buffer, size_t       length) {
-#else
-extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int length, int mode) {
-#endif
-    int n; // n is the number of bytes read
-    if (fh < 3) {
-        // only read a character at a time from stdin
-#if DEVICE_SERIAL
-        if (!stdio_uart_inited) init_serial();
-#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
-        while (true) {
-            char c = serial_getc(&stdio_uart);
-            if ((c == '\r' && stdio_in_prev != '\n') ||
-                (c == '\n' && stdio_in_prev != '\r')) {
-                stdio_in_prev = c;
-                *buffer = '\n';
-                break;
-            } else if ((c == '\r' && stdio_in_prev == '\n') ||
-                       (c == '\n' && stdio_in_prev == '\r')) {
-                stdio_in_prev = c;
-                // onto next character
-                continue;
-            } else {
-                stdio_in_prev = c;
-                *buffer = c;
-                break;
-            }
-        }
-#else
-        *buffer = serial_getc(&stdio_uart);
-#endif
-#endif
-        n = 1;
-    } else {
-        FileHandle* fhc = filehandles[fh-3];
-        if (fhc == NULL) return -1;
-
-        n = fhc->read(buffer, length);
-    }
-#ifdef __ARMCC_VERSION
-    return length-n;
-#else
-    return n;
-#endif
-}
-
-#ifdef __ARMCC_VERSION
-extern "C" int PREFIX(_istty)(FILEHANDLE fh)
-#else
-extern "C" int _isatty(FILEHANDLE fh)
-#endif
-{
-    /* stdin, stdout and stderr should be tty */
-    if (fh < 3) return 1;
-
-    FileHandle* fhc = filehandles[fh-3];
-    if (fhc == NULL) return -1;
-
-    return fhc->isatty();
-}
-
-extern "C"
-#if defined(__ARMCC_VERSION)
-int _sys_seek(FILEHANDLE fh, long position)
-#elif defined(__ICCARM__)
-long __lseek(int fh, long offset, int whence)
-#else
-int _lseek(FILEHANDLE fh, int offset, int whence)
-#endif
-{
-    if (fh < 3) return 0;
-
-    FileHandle* fhc = filehandles[fh-3];
-    if (fhc == NULL) return -1;
-
-#if defined(__ARMCC_VERSION)
-    return fhc->lseek(position, SEEK_SET);
-#else
-    return fhc->lseek(offset, whence);
-#endif
-}
-
-#ifdef __ARMCC_VERSION
-extern "C" int PREFIX(_ensure)(FILEHANDLE fh) {
-    if (fh < 3) return 0;
-
-    FileHandle* fhc = filehandles[fh-3];
-    if (fhc == NULL) return -1;
-
-    return fhc->fsync();
-}
-
-extern "C" long PREFIX(_flen)(FILEHANDLE fh) {
-    if (fh < 3) return 0;
-
-    FileHandle* fhc = filehandles[fh-3];
-    if (fhc == NULL) return -1;
-
-    return fhc->flen();
-}
-#endif
-
-
-#if !defined(__ARMCC_VERSION) && !defined(__ICCARM__)
-extern "C" int _fstat(int fd, struct stat *st) {
-    if ((STDOUT_FILENO == fd) || (STDERR_FILENO == fd) || (STDIN_FILENO == fd)) {
-        st->st_mode = S_IFCHR;
-        return  0;
-    }
-
-    errno = EBADF;
-    return -1;
-}
-#endif
-
-namespace std {
-extern "C" int remove(const char *path) {
-    FilePath fp(path);
-    FileSystemLike *fs = fp.fileSystem();
-    if (fs == NULL) return -1;
-
-    return fs->remove(fp.fileName());
-}
-
-extern "C" int rename(const char *oldname, const char *newname) {
-    FilePath fpOld(oldname);
-    FilePath fpNew(newname);
-    FileSystemLike *fsOld = fpOld.fileSystem();
-    FileSystemLike *fsNew = fpNew.fileSystem();
-
-    /* rename only if both files are on the same FS */
-    if (fsOld != fsNew || fsOld == NULL) return -1;
-
-    return fsOld->rename(fpOld.fileName(), fpNew.fileName());
-}
-
-extern "C" char *tmpnam(char *s) {
-    return NULL;
-}
-
-extern "C" FILE *tmpfile() {
-    return NULL;
-}
-} // namespace std
-
-#ifdef __ARMCC_VERSION
-extern "C" char *_sys_command_string(char *cmd, int len) {
-    return NULL;
-}
-#endif
-
-extern "C" DIR *opendir(const char *path) {
-    /* root dir is FileSystemLike */
-    if (path[0] == '/' && path[1] == 0) {
-        return FileSystemLike::opendir();
-    }
-
-    FilePath fp(path);
-    FileSystemLike* fs = fp.fileSystem();
-    if (fs == NULL) return NULL;
-
-    return fs->opendir(fp.fileName());
-}
-
-extern "C" struct dirent *readdir(DIR *dir) {
-    return dir->readdir();
-}
-
-extern "C" int closedir(DIR *dir) {
-    return dir->closedir();
-}
-
-extern "C" void rewinddir(DIR *dir) {
-    dir->rewinddir();
-}
-
-extern "C" off_t telldir(DIR *dir) {
-    return dir->telldir();
-}
-
-extern "C" void seekdir(DIR *dir, off_t off) {
-    dir->seekdir(off);
-}
-
-extern "C" int mkdir(const char *path, mode_t mode) {
-    FilePath fp(path);
-    FileSystemLike *fs = fp.fileSystem();
-    if (fs == NULL) return -1;
-
-    return fs->mkdir(fp.fileName(), mode);
-}
-
-#if defined(TOOLCHAIN_GCC)
-/* prevents the exception handling name demangling code getting pulled in */
-#include "mbed_error.h"
-namespace __gnu_cxx {
-    void __verbose_terminate_handler() {
-        error("Exception");
-    }
-}
-extern "C" WEAK void __cxa_pure_virtual(void);
-extern "C" WEAK void __cxa_pure_virtual(void) {
-    exit(1);
-}
-
-#endif
-
-#if defined(TOOLCHAIN_GCC)
-
-#ifdef  FEATURE_UVISOR
-#include "uvisor-lib/uvisor-lib.h"
-#endif/* FEATURE_UVISOR */
-
-
-extern "C" WEAK void software_init_hook_rtos(void)
-{
-    // Do nothing by default.
-}
-
-extern "C" void software_init_hook(void)
-{
-#ifdef   FEATURE_UVISOR
-    int return_code;
-
-    return_code = uvisor_lib_init();
-    if (return_code) {
-        mbed_die();
-    }
-#endif/* FEATURE_UVISOR */
-    mbed_sdk_init();
-    software_init_hook_rtos();
-}
-#endif
-
-// ****************************************************************************
-// mbed_main is a function that is called before main()
-// mbed_sdk_init() is also a function that is called before main(), but unlike
-// mbed_main(), it is not meant for user code, but for the SDK itself to perform
-// initializations before main() is called.
-
-extern "C" WEAK void mbed_main(void);
-extern "C" WEAK void mbed_main(void) {
-}
-
-#if defined(TOOLCHAIN_ARM)
-extern "C" int $Super$$main(void);
-
-extern "C" int $Sub$$main(void) {
-    mbed_main();
-    return $Super$$main();
-}
-
-extern "C" void _platform_post_stackheap_init (void) {
-    mbed_sdk_init();
-}
-
-#elif defined(TOOLCHAIN_GCC)
-extern "C" int __real_main(void);
-
-extern "C" int __wrap_main(void) {
-    mbed_main();
-    return __real_main();
-}
-#elif defined(TOOLCHAIN_IAR)
-// IAR doesn't have the $Super/$Sub mechanism of armcc, nor something equivalent
-// to ld's --wrap. It does have a --redirect, but that doesn't help, since redirecting
-// 'main' to another symbol looses the original 'main' symbol. However, its startup
-// code will call a function to setup argc and argv (__iar_argc_argv) if it is defined.
-// Since mbed doesn't use argc/argv, we use this function to call our mbed_main.
-extern "C" void __iar_argc_argv() {
-    mbed_main();
-}
-#endif
-
-// Provide implementation of _sbrk (low-level dynamic memory allocation
-// routine) for GCC_ARM which compares new heap pointer with MSP instead of
-// SP.  This make it compatible with RTX RTOS thread stacks.
-#if defined(TOOLCHAIN_GCC_ARM) || defined(TOOLCHAIN_GCC_CR)
-// Linker defined symbol used by _sbrk to indicate where heap should start.
-extern "C" int __end__;
-
-#if defined(TARGET_CORTEX_A)
-extern "C" uint32_t  __HeapLimit;
-#endif
-
-// Turn off the errno macro and use actual global variable instead.
-#undef errno
-extern "C" int errno;
-
-// For ARM7 only
-register unsigned char * stack_ptr __asm ("sp");
-
-// Dynamic memory allocation related syscall.
-#if defined(TARGET_NUMAKER_PFM_NUC472) || defined(TARGET_NUMAKER_PFM_M453)
-// Overwrite _sbrk() to support two region model (heap and stack are two distinct regions).
-// __wrap__sbrk() is implemented in:
-// TARGET_NUMAKER_PFM_NUC472    hal/targets/cmsis/TARGET_NUVOTON/TARGET_NUC472/TARGET_NUMAKER_PFM_NUC472/TOOLCHAIN_GCC_ARM/retarget.c
-// TARGET_NUMAKER_PFM_M453      hal/targets/cmsis/TARGET_NUVOTON/TARGET_M451/TARGET_NUMAKER_PFM_M453/TOOLCHAIN_GCC_ARM/retarget.c
-extern "C" void *__wrap__sbrk(int incr);
-extern "C" caddr_t _sbrk(int incr) {
-    return (caddr_t) __wrap__sbrk(incr);
-}
-#else
-extern "C" caddr_t _sbrk(int incr) {
-    static unsigned char* heap = (unsigned char*)&__end__;
-    unsigned char*        prev_heap = heap;
-    unsigned char*        new_heap = heap + incr;
-
-#if defined(TARGET_ARM7)
-    if (new_heap >= stack_ptr) {
-#elif defined(TARGET_CORTEX_A)
-    if (new_heap >= (unsigned char*)&__HeapLimit) {     /* __HeapLimit is end of heap section */
-#else
-    if (new_heap >= (unsigned char*)__get_MSP()) {
-#endif
-        errno = ENOMEM;
-        return (caddr_t)-1;
-    }
-
-    // Additional heap checking if set
-    if (mbed_heap_size && (new_heap >= mbed_heap_start + mbed_heap_size)) {
-        errno = ENOMEM;
-        return (caddr_t)-1;
-    }
-
-    heap = new_heap;
-    return (caddr_t) prev_heap;
-}
-#endif
-#endif
-
-#if defined(TOOLCHAIN_GCC_ARM) || defined(TOOLCHAIN_GCC_CR)
-extern "C" void _exit(int return_code) {
-#else
-namespace std {
-extern "C" void exit(int return_code) {
-#endif
-
-#if DEVICE_STDIO_MESSAGES
-#if MBED_CONF_PLATFORM_STDIO_FLUSH_AT_EXIT
-    fflush(stdout);
-    fflush(stderr);
-#endif
-#endif
-
-#if DEVICE_SEMIHOST
-    if (mbed_interface_connected()) {
-        semihost_exit();
-    }
-#endif
-    if (return_code) {
-        mbed_die();
-    }
-
-    while (1);
-}
-
-#if !defined(TOOLCHAIN_GCC_ARM) && !defined(TOOLCHAIN_GCC_CR)
-} //namespace std
-#endif
-
-#if defined(TOOLCHAIN_ARM) || defined(TOOLCHAIN_GCC)
-
-// This series of function disable the registration of global destructors
-// in a dynamic table which will be called when the application exit.
-// In mbed, program never exit properly, it dies.
-// More informations about this topic for ARMCC here:
-// http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/6449.html
-extern "C" {
-int __aeabi_atexit(void *object, void (*dtor)(void* /*this*/), void *handle) {
-    return 1;
-}
-
-int __cxa_atexit(void (*dtor)(void* /*this*/), void *object, void *handle) {
-    return 1;
-}
-
-void __cxa_finalize(void *handle) {
-}
-
-} // end of extern "C"
-
-#endif
-
-
-#if defined(TOOLCHAIN_GCC)
-
-/*
- * Depending on how newlib is  configured, it is often not enough to define
- * __aeabi_atexit, __cxa_atexit and __cxa_finalize in order to override the
- * behavior regarding the registration of handlers with atexit.
- *
- * To overcome this limitation, exit and atexit are overriden here.
- */
-extern "C"{
-
-/**
- * @brief Retarget of exit for GCC.
- * @details Unlike the standard version, this function doesn't call any function
- * registered with atexit before calling _exit.
- */
-void __wrap_exit(int return_code) {
-    _exit(return_code);
-}
-
-/**
- * @brief Retarget atexit from GCC.
- * @details This function will always fail and never register any handler to be
- * called at exit.
- */
-int __wrap_atexit(void (*func)()) {
-    return 1;
-}
-
-}
-
-#endif
-
-
-
-namespace mbed {
-
-void mbed_set_unbuffered_stream(FILE *_file) {
-#if defined (__ICCARM__)
-    char buf[2];
-    std::setvbuf(_file,buf,_IONBF,NULL);    
-#else
-    setbuf(_file, NULL);
-#endif
-}
-
-int mbed_getc(FILE *_file){
-#if defined (__ICCARM__)
-    /*This is only valid for unbuffered streams*/
-    int res = std::fgetc(_file);
-    if (res>=0){
-        _file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */
-        _file->_Rend = _file->_Wend;
-        _file->_Next = _file->_Wend;
-    }    
-    return res;
-#else    
-    return std::fgetc(_file);
-#endif   
-}
-
-char* mbed_gets(char*s, int size, FILE *_file){
-#if defined (__ICCARM__)
-    /*This is only valid for unbuffered streams*/
-    char *str = fgets(s,size,_file);
-    if (str!=NULL){
-        _file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */
-        _file->_Rend = _file->_Wend;
-        _file->_Next = _file->_Wend;
-    }
-    return str;
-#else    
-    return std::fgets(s,size,_file);
-#endif
-}
-
-} // namespace mbed
-
-#if defined (__ICCARM__)
-// Stub out locks when an rtos is not present
-extern "C" WEAK void __iar_system_Mtxinit(__iar_Rmtx *mutex) {}
-extern "C" WEAK void __iar_system_Mtxdst(__iar_Rmtx *mutex) {}
-extern "C" WEAK void __iar_system_Mtxlock(__iar_Rmtx *mutex) {}
-extern "C" WEAK void __iar_system_Mtxunlock(__iar_Rmtx *mutex) {}
-extern "C" WEAK void __iar_file_Mtxinit(__iar_Rmtx *mutex) {}
-extern "C" WEAK void __iar_file_Mtxdst(__iar_Rmtx *mutex) {}
-extern "C" WEAK void __iar_file_Mtxlock(__iar_Rmtx *mutex) {}
-extern "C" WEAK void __iar_file_Mtxunlock(__iar_Rmtx *mutex) {}
-#elif defined(__CC_ARM)
-// Do nothing
-#elif defined (__GNUC__)
-struct _reent;
-// Stub out locks when an rtos is not present
-extern "C" WEAK void __rtos_malloc_lock( struct _reent *_r ) {}
-extern "C" WEAK void __rtos_malloc_unlock( struct _reent *_r ) {}
-extern "C" WEAK void __rtos_env_lock( struct _reent *_r ) {}
-extern "C" WEAK void __rtos_env_unlock( struct _reent *_r ) {}
-
-extern "C" void __malloc_lock( struct _reent *_r )
-{
-    __rtos_malloc_lock(_r);
-}
-
-extern "C" void __malloc_unlock( struct _reent *_r )
-{
-    __rtos_malloc_unlock(_r);
-}
-
-extern "C" void __env_lock( struct _reent *_r )
-{
-    __rtos_env_lock(_r);
-}
-
-extern "C" void __env_unlock( struct _reent *_r )
-{
-    __rtos_env_unlock(_r);
-}
-
-#define CXA_GUARD_INIT_DONE             (1 << 0)
-#define CXA_GUARD_INIT_IN_PROGRESS      (1 << 1)
-#define CXA_GUARD_MASK                  (CXA_GUARD_INIT_DONE | CXA_GUARD_INIT_IN_PROGRESS)
-
-extern "C" int __cxa_guard_acquire(int *guard_object_p)
-{
-    uint8_t *guard_object = (uint8_t *)guard_object_p;
-    if (CXA_GUARD_INIT_DONE == (*guard_object & CXA_GUARD_MASK)) {
-        return 0;
-    }
-    singleton_lock();
-    if (CXA_GUARD_INIT_DONE == (*guard_object & CXA_GUARD_MASK)) {
-        singleton_unlock();
-        return 0;
-    }
-    MBED_ASSERT(0 == (*guard_object & CXA_GUARD_MASK));
-    *guard_object = *guard_object | CXA_GUARD_INIT_IN_PROGRESS;
-    return 1;
-}
-
-extern "C" void __cxa_guard_release(int *guard_object_p)
-{
-    uint8_t *guard_object = (uint8_t *)guard_object_p;
-    MBED_ASSERT(CXA_GUARD_INIT_IN_PROGRESS == (*guard_object & CXA_GUARD_MASK));
-    *guard_object = (*guard_object & ~CXA_GUARD_MASK) | CXA_GUARD_INIT_DONE;
-    singleton_unlock();
-}
-
-extern "C" void __cxa_guard_abort(int *guard_object_p)
-{
-    uint8_t *guard_object = (uint8_t *)guard_object_p;
-    MBED_ASSERT(CXA_GUARD_INIT_IN_PROGRESS == (*guard_object & CXA_GUARD_MASK));
-    *guard_object = *guard_object & ~CXA_GUARD_INIT_IN_PROGRESS;
-    singleton_unlock();
-}
-
-#endif
-
-void *operator new(std::size_t count)
-{
-    void *buffer = malloc(count);
-    if (NULL == buffer) {
-        error("Operator new out of memory\r\n");
-    }
-    return buffer;
-}
-
-void *operator new[](std::size_t count)
-{
-    void *buffer = malloc(count);
-    if (NULL == buffer) {
-        error("Operator new[] out of memory\r\n");
-    }
-    return buffer;
-}
-
-void operator delete(void *ptr)
-{
-    if (ptr != NULL) {
-        free(ptr);
-    }
-}
-void operator delete[](void *ptr)
-{
-    if (ptr != NULL) {
-        free(ptr);
-    }
-}
--- a/platform/rtc_time.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/rtc_time.h	Tue Mar 14 16:40:56 2017 +0000
@@ -1,92 +1,24 @@
-
-/** \addtogroup platform */
-/** @{*/
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
+/*
+ * Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
+ * SPDX-License-Identifier: Apache-2.0
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
-#include <time.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/** Implementation of the C time.h functions
- *
- * Provides mechanisms to set and read the current time, based
- * on the microcontroller Real-Time Clock (RTC), plus some
- * standard C manipulation and formating functions.
- *
- * Example:
- * @code
- * #include "mbed.h"
- *
- * int main() {
- *     set_time(1256729737);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
- *
- *     while(1) {
- *         time_t seconds = time(NULL);
- *
- *         printf("Time as seconds since January 1, 1970 = %d\n", seconds);
- *
- *         printf("Time as a basic string = %s", ctime(&seconds));
- *
- *         char buffer[32];
- *         strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
- *         printf("Time as a custom formatted string = %s", buffer);
- *
- *         wait(1);
- *     }
- * }
- * @endcode
- */
+#ifndef MBED_OLD_RTC_TIME_H
+#define MBED_OLD_RTC_TIME_H
 
-/** Set the current time
- *
- * Initialises and sets the time of the microcontroller Real-Time Clock (RTC)
- * to the time represented by the number of seconds since January 1, 1970
- * (the UNIX timestamp).
- *
- * @param t Number of seconds since January 1, 1970 (the UNIX timestamp)
- *
- * @Note Synchronization level: Thread safe
- *
- * Example:
- * @code
- * #include "mbed.h"
- *
- * int main() {
- *     set_time(1256729737); // Set time to Wed, 28 Oct 2009 11:35:37
- * }
- * @endcode
- */
-void set_time(time_t t);
+#warning rtc_time.h has been replaced by mbed_rtc_time.h, please update to mbed_rtc_time.h [since mbed-os-5.3]
+#include "platform/mbed_rtc_time.h"
 
-/** Attach an external RTC to be used for the C time functions
- *
- * @Note Synchronization level: Thread safe
- *
- * @param read_rtc pointer to function which returns current UNIX timestamp
- * @param write_rtc pointer to function which sets current UNIX timestamp, can be NULL
- * @param init_rtc pointer to funtion which initializes RTC, can be NULL
- * @param isenabled_rtc pointer to function wich returns if the rtc is enabled, can be NULL
- */
-void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void));
-
-#ifdef __cplusplus
-}
 #endif
-
-/** @}*/
--- a/platform/semihost_api.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/semihost_api.h	Tue Mar 14 16:40:56 2017 +0000
@@ -1,98 +1,24 @@
-
-/** \addtogroup platform */
-/** @{*/
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
+/*
+ * Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
+ * SPDX-License-Identifier: Apache-2.0
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#ifndef MBED_SEMIHOST_H
-#define MBED_SEMIHOST_H
-
-#include "device.h"
-#include "platform/toolchain.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#if DEVICE_SEMIHOST
-
-#ifndef __CC_ARM
-
-#if defined(__ICCARM__)
-static inline int __semihost(int reason, const void *arg) {
-    return __semihosting(reason, (void*)arg);
-}
-#else
-
-#ifdef __thumb__
-#   define AngelSWI            0xAB
-#   define AngelSWIInsn        "bkpt"
-#   define AngelSWIAsm          bkpt
-#else
-#   define AngelSWI            0x123456
-#   define AngelSWIInsn        "swi"
-#   define AngelSWIAsm          swi
-#endif
-
-static inline int __semihost(int reason, const void *arg) {
-    int value;
 
-    asm volatile (
-       "mov r0, %1"          "\n\t"
-       "mov r1, %2"          "\n\t"
-       AngelSWIInsn " %a3"   "\n\t"
-       "mov %0, r0"
-       : "=r" (value)                                         /* output operands             */
-       : "r" (reason), "r" (arg), "i" (AngelSWI)              /* input operands              */
-       : "r0", "r1", "r2", "r3", "ip", "lr", "memory", "cc"   /* list of clobbered registers */
-    );
-
-    return value;
-}
-#endif
-#endif
+#ifndef MBED_OLD_SEMIHOST_API_H
+#define MBED_OLD_SEMIHOST_API_H
 
-#if DEVICE_LOCALFILESYSTEM
-FILEHANDLE semihost_open(const char* name, int openmode);
-int semihost_close (FILEHANDLE fh);
-int semihost_read  (FILEHANDLE fh, unsigned char* buffer, unsigned int length, int mode);
-int semihost_write (FILEHANDLE fh, const unsigned char* buffer, unsigned int length, int mode);
-int semihost_ensure(FILEHANDLE fh);
-long semihost_flen (FILEHANDLE fh);
-int semihost_seek  (FILEHANDLE fh, long position);
-int semihost_istty (FILEHANDLE fh);
-
-int semihost_remove(const char *name);
-int semihost_rename(const char *old_name, const char *new_name);
-#endif
-
-int semihost_uid(char *uid);
-int semihost_reset(void);
-int semihost_vbus(void);
-int semihost_powerdown(void);
-int semihost_exit(void);
-
-int semihost_connected(void);
-int semihost_disabledebug(void);
+#warning semihost_api.h has been replaced by mbed_semihost_api.h, please update to mbed_semihost_api.h [since mbed-os-5.3]
+#include "platform/mbed_semihost_api.h"
 
 #endif
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
-
-/** @}*/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/platform/sleep.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef MBED_OLD_SLEEP_H
+#define MBED_OLD_SLEEP_H
+
+#warning sleep.h has been replaced by mbed_sleep.h, please update to mbed_sleep.h [since mbed-os-5.3]
+#include "platform/mbed_sleep.h"
+
+#endif
--- a/platform/toolchain.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/toolchain.h	Tue Mar 14 16:40:56 2017 +0000
@@ -16,280 +16,11 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#ifndef MBED_TOOLCHAIN_H
-#define MBED_TOOLCHAIN_H
-
-#include "mbed_preprocessor.h"
-
-
-// Warning for unsupported compilers
-#if !defined(__GNUC__)   /* GCC        */ \
- && !defined(__CC_ARM)   /* ARMCC      */ \
- && !defined(__clang__)  /* LLVM/Clang */ \
- && !defined(__ICCARM__) /* IAR        */
-#warning "This compiler is not yet supported."
-#endif
-
-
-// Attributes
-
-/** MBED_PACKED
- *  Pack a structure, preventing any padding from being added between fields.
- *
- *  @code
- *  #include "toolchain.h"
- *
- *  MBED_PACKED(struct) foo {
- *      char x;
- *      int y;
- *  };
- *  @endcode
- */
-#ifndef MBED_PACKED
-#if defined(__ICCARM__)
-#define MBED_PACKED(struct) __packed struct
-#else
-#define MBED_PACKED(struct) struct __attribute__((packed))
-#endif
-#endif
-
-/** MBED_ALIGN(N)
- *  Declare a variable to be aligned on an N-byte boundary.
- *
- *  @note
- *  IAR does not support alignment greater than word size on the stack
- *  
- *  @code
- *  #include "toolchain.h"
- *
- *  MBED_ALIGN(16) char a;
- *  @endcode
- */
-#ifndef MBED_ALIGN
-#if defined(__ICCARM__)
-#define MBED_ALIGN(N) _Pragma(MBED_STRINGIFY(data_alignment=N))
-#else
-#define MBED_ALIGN(N) __attribute__((aligned(N)))
-#endif
-#endif
-
-/** MBED_UNUSED
- *  Declare a function argument to be unused, suppressing compiler warnings
- *
- *  @code
- *  #include "toolchain.h"
- *
- *  void foo(MBED_UNUSED int arg) {
- *
- *  }
- *  @endcode
- */
-#ifndef MBED_UNUSED
-#if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
-#define MBED_UNUSED __attribute__((__unused__))
-#else
-#define MBED_UNUSED
-#endif
-#endif
-
-/** MBED_WEAK
- *  Mark a function as being weak.
- *  
- *  @note
- *  weak functions are not friendly to making code re-usable, as they can only
- *  be overridden once (and if they are multiply overridden the linker will emit
- *  no warning). You should not normally use weak symbols as part of the API to
- *  re-usable modules.
- *  
- *  @code
- *  #include "toolchain.h"
- *  
- *  MBED_WEAK void foo() {
- *      // a weak implementation of foo that can be overriden by a definition
- *      // without  __weak
- *  }
- *  @endcode
- */
-#ifndef MBED_WEAK
-#if defined(__ICCARM__)
-#define MBED_WEAK __weak
-#else
-#define MBED_WEAK __attribute__((weak))
-#endif
-#endif
-
-/** MBED_PURE
- *  Hint to the compiler that a function depends only on parameters
- *
- *  @code
- *  #include "toolchain.h"
- *
- *  MBED_PURE int foo(int arg){
- *      // no access to global variables
- *  }
- *  @endcode
- */
-#ifndef MBED_PURE
-#if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
-#define MBED_PURE __attribute__((const))
-#else
-#define MBED_PURE
-#endif
-#endif
 
-/** MBED_FORCEINLINE
- *  Declare a function that must always be inlined. Failure to inline
- *  such a function will result in an error.
- *
- *  @code
- *  #include "toolchain.h"
- *  
- *  MBED_FORCEINLINE void foo() {
- *  
- *  }
- *  @endcode
- */
-#ifndef MBED_FORCEINLINE
-#if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
-#define MBED_FORCEINLINE static inline __attribute__((always_inline))
-#elif defined(__ICCARM__)
-#define MBED_FORCEINLINE _Pragma("inline=forced") static
-#else
-#define MBED_FORCEINLINE static inline
-#endif
-#endif
-
-/** MBED_NORETURN
- *  Declare a function that will never return.
- *
- *  @code
- *  #include "toolchain.h"
- *  
- *  MBED_NORETURN void foo() {
- *      // must never return
- *      while (1) {}
- *  }
- *  @endcode
- */
-#ifndef MBED_NORETURN
-#if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
-#define MBED_NORETURN __attribute__((noreturn))
-#elif defined(__ICCARM__)
-#define MBED_NORETURN __noreturn
-#else
-#define MBED_NORETURN
-#endif
-#endif
-
-/** MBED_UNREACHABLE
- *  An unreachable statement. If the statement is reached,
- *  behaviour is undefined. Useful in situations where the compiler
- *  cannot deduce the unreachability of code.
- *
- *  @code
- *  #include "toolchain.h"
- *
- *  void foo(int arg) {
- *      switch (arg) {
- *          case 1: return 1;
- *          case 2: return 2;
- *          ...
- *      }
- *      MBED_UNREACHABLE;
- *  }
- *  @endcode
- */
-#ifndef MBED_UNREACHABLE
-#if (defined(__GNUC__) || defined(__clang__)) && !defined(__CC_ARM)
-#define MBED_UNREACHABLE __builtin_unreachable()
-#else
-#define MBED_UNREACHABLE while (1)
-#endif
-#endif
+#ifndef MBED_OLD_TOOLCHAIN_H
+#define MBED_OLD_TOOLCHAIN_H
 
-/** MBED_DEPRECATED("message string")
- *  Mark a function declaration as deprecated, if it used then a warning will be
- *  issued by the compiler possibly including the provided message. Note that not
- *  all compilers are able to display the message.
- *
- *  @code
- *  #include "toolchain.h"
- *  
- *  MBED_DEPRECATED("don't foo any more, bar instead")
- *  void foo(int arg);
- *  @endcode
- */
-#ifndef MBED_DEPRECATED
-#if defined(__CC_ARM)
-#define MBED_DEPRECATED(M) __attribute__((deprecated))
-#elif defined(__GNUC__) || defined(__clang__)
-#define MBED_DEPRECATED(M) __attribute__((deprecated(M)))
-#else
-#define MBED_DEPRECATED(M)
-#endif
-#endif
-
-/** MBED_DEPRECATED_SINCE("version", "message string")
- *  Mark a function declaration as deprecated, noting that the declaration was
- *  deprecated on the specified version. If the function is used then a warning
- *  will be issued by the compiler possibly including the provided message.
- *  Note that not all compilers are able to display this message.
- *
- *  @code
- *  #include "toolchain.h"
- *
- *  MBED_DEPRECATED_SINCE("mbed-os-5.1", "don't foo any more, bar instead")
- *  void foo(int arg);
- *  @endcode
- */
-#define MBED_DEPRECATED_SINCE(D, M) MBED_DEPRECATED(M " [since " D "]")
-
-/** MBED_CALLER_ADDR()
- * Returns the caller of the current function.
- *
- * @note
- * This macro is only implemented for GCC and ARMCC.
- *
- * @code
- * #include "toolchain.h"
- *
- * printf("This function was called from %p", MBED_CALLER_ADDR());
- * @endcode
- *
- * @return Address of the calling function
- */
-#ifndef MBED_CALLER_ADDR
-#if (defined(__GNUC__) || defined(__clang__)) && !defined(__CC_ARM)
-#define MBED_CALLER_ADDR() __builtin_extract_return_addr(__builtin_return_address(0))
-#elif defined(__CC_ARM)
-#define MBED_CALLER_ADDR() __builtin_return_address(0)
-#else
-#define MBED_CALLER_ADDR() (NULL)
-#endif
-#endif
-
-// FILEHANDLE declaration
-#if defined(TOOLCHAIN_ARM)
-#include <rt_sys.h>
-#endif
-
-#ifndef FILEHANDLE
-typedef int FILEHANDLE;
-#endif
-
-// Backwards compatibility
-#ifndef WEAK
-#define WEAK MBED_WEAK
-#endif
-
-#ifndef PACKED
-#define PACKED MBED_PACKED()
-#endif
-
-#ifndef EXTERN
-#define EXTERN extern
-#endif
+#warning toolchain.h has been replaced by mbed_toolchain.h, please update to mbed_toolchain.h [since mbed-os-5.3]
+#include "platform/mbed_toolchain.h"
 
 #endif
-
-/** @}*/
--- a/platform/wait_api.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/platform/wait_api.h	Tue Mar 14 16:40:56 2017 +0000
@@ -1,71 +1,24 @@
-
-/** \addtogroup platform */
-/** @{*/
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
+/*
+ * Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
+ * SPDX-License-Identifier: Apache-2.0
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#ifndef MBED_WAIT_API_H
-#define MBED_WAIT_API_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
 
-/** Generic wait functions.
- *
- * These provide simple NOP type wait capabilities.
- *
- * Example:
- * @code
- * #include "mbed.h"
- *
- * DigitalOut heartbeat(LED1);
- *
- * int main() {
- *     while (1) {
- *         heartbeat = 1;
- *         wait(0.5);
- *         heartbeat = 0;
- *         wait(0.5);
- *     }
- * }
- */
+#ifndef MBED_OLD_WAIT_API_H
+#define MBED_OLD_WAIT_API_H
 
-/** Waits for a number of seconds, with microsecond resolution (within
- *  the accuracy of single precision floating point).
- *
- *  @param s number of seconds to wait
- */
-void wait(float s);
-
-/** Waits a number of milliseconds.
- *
- *  @param ms the whole number of milliseconds to wait
- */
-void wait_ms(int ms);
-
-/** Waits a number of microseconds.
- *
- *  @param us the whole number of microseconds to wait
- */
-void wait_us(int us);
-
-#ifdef __cplusplus
-}
-#endif
+#warning wait_api.h has been replaced by mbed_wait_api.h, please update to mbed_wait_api.h [since mbed-os-5.3]
+#include "platform/mbed_wait_api.h"
 
 #endif
-
-/** @}*/
--- a/targets/TARGET_ARM_SSG/TARGET_BEETLE/analogin_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ARM_SSG/TARGET_BEETLE/analogin_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -16,7 +16,7 @@
  */
 #include "cmsis.h"
 #include "mbed_assert.h"
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "analogin_api.h"
 #include "gpio_api.h"
 #include "spi_api.h"
--- a/targets/TARGET_ARM_SSG/TARGET_BEETLE/i2c_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ARM_SSG/TARGET_BEETLE/i2c_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -18,7 +18,7 @@
 #include "cmsis.h"
 #include "pinmap.h"
 #include "mbed_error.h"
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 /* States of a possibly combined I2C transfer */
 typedef enum i2c_transfer_state_t {
     I2C_TRANSFER_SINGLE, /* Non combined transfer */
--- a/targets/TARGET_ARM_SSG/TARGET_BEETLE/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ARM_SSG/TARGET_BEETLE/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -16,13 +16,13 @@
 #include "sleep_api.h"
 #include "cmsis.h"
 
- void sleep(void)
+ void hal_sleep(void)
  {
     SystemPowerSuspend(POWER_MODE_SLEEP);
     SystemPowerResume(POWER_MODE_SLEEP);
  }
 
-void deepsleep(void)
+void hal_deepsleep(void)
 {
     SystemPowerSuspend(POWER_MODE_DEEP_SLEEP);
     SystemPowerResume(POWER_MODE_DEEP_SLEEP);
--- a/targets/TARGET_ARM_SSG/TARGET_BEETLE/spi_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ARM_SSG/TARGET_BEETLE/spi_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -20,7 +20,7 @@
 #include "cmsis.h"
 #include "pinmap.h"
 #include "mbed_error.h"
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 
 /*
  * Driver private data structure that should not be shared by multiple
--- a/targets/TARGET_ARM_SSG/TARGET_IOTSS/SDK/ETH_MPS2.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ARM_SSG/TARGET_IOTSS/SDK/ETH_MPS2.c	Tue Mar 14 16:40:56 2017 +0000
@@ -35,7 +35,7 @@
  */
 
 #include <stdio.h>
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "ETH_MPS2.h"
 
 // SMSC9220 low-level operations
--- a/targets/TARGET_ARM_SSG/TARGET_IOTSS/SDK/mps2_ethernet_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ARM_SSG/TARGET_IOTSS/SDK/mps2_ethernet_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -18,10 +18,10 @@
 #include "mps2_ethernet_api.h"
 #include "cmsis.h"
 #include "mbed_interface.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 #include "mbed_error.h"
 #include "ETH_MPS2.h"
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 
 #define TX_PKT_SIZE 256
 #define RX_PKT_SIZE 300
--- a/targets/TARGET_ARM_SSG/TARGET_IOTSS/ethernet_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ARM_SSG/TARGET_IOTSS/ethernet_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -19,10 +19,10 @@
 #include "mps2_ethernet_api.h"
 #include "cmsis.h"
 #include "mbed_interface.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 #include "mbed_error.h"
 #include "ETH_MPS2.h"
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 
 #define TX_PKT_SIZE 256
 #define RX_PKT_SIZE 300
--- a/targets/TARGET_ARM_SSG/TARGET_IOTSS/i2c_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ARM_SSG/TARGET_IOTSS/i2c_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -18,7 +18,7 @@
 #include "pinmap.h"
 #include "mbed_error.h"
 #include "SMM_MPS2.h"
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "fpga.h"
 
 // Types
--- a/targets/TARGET_ARM_SSG/TARGET_IOTSS/spi_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ARM_SSG/TARGET_IOTSS/spi_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -20,7 +20,7 @@
 #include "cmsis.h"
 #include "pinmap.h"
 #include "mbed_error.h"
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 
 static const PinMap PinMap_SPI_SCLK[] = {
     {SCLK_SPI , SPI_0, 0},
--- a/targets/TARGET_ARM_SSG/TARGET_MPS2/SDK/ETH_MPS2.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ARM_SSG/TARGET_MPS2/SDK/ETH_MPS2.c	Tue Mar 14 16:40:56 2017 +0000
@@ -35,7 +35,7 @@
  */
 
 #include <stdio.h>
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "ETH_MPS2.h"
 
 // SMSC9220 low-level operations
--- a/targets/TARGET_ARM_SSG/TARGET_MPS2/SDK/mps2_ethernet_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ARM_SSG/TARGET_MPS2/SDK/mps2_ethernet_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -18,10 +18,10 @@
 #include "mps2_ethernet_api.h"
 #include "cmsis.h"
 #include "mbed_interface.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 #include "mbed_error.h"
 #include "ETH_MPS2.h"
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 
 #define TX_PKT_SIZE 256
 #define RX_PKT_SIZE 300
--- a/targets/TARGET_ARM_SSG/TARGET_MPS2/ethernet_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ARM_SSG/TARGET_MPS2/ethernet_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -19,10 +19,10 @@
 #include "mps2_ethernet_api.h"
 #include "cmsis.h"
 #include "mbed_interface.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 #include "mbed_error.h"
 #include "ETH_MPS2.h"
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 
 #define TX_PKT_SIZE 256
 #define RX_PKT_SIZE 300
--- a/targets/TARGET_ARM_SSG/TARGET_MPS2/i2c_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ARM_SSG/TARGET_MPS2/i2c_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -18,7 +18,7 @@
 #include "pinmap.h"
 #include "mbed_error.h"
 #include "SMM_MPS2.h"
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "fpga.h"
 
 // Types
--- a/targets/TARGET_ARM_SSG/TARGET_MPS2/spi_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ARM_SSG/TARGET_MPS2/spi_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -20,7 +20,7 @@
 #include "cmsis.h"
 #include "pinmap.h"
 #include "mbed_error.h"
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 
 static const PinMap PinMap_SPI_SCLK[] = {
     {SCLK_SPI , SPI_0, 0},
--- a/targets/TARGET_Atmel/TARGET_SAM_CortexM0P/sleep_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_Atmel/TARGET_SAM_CortexM0P/sleep_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -25,7 +25,7 @@
  * @param[void] void
  * @return      void
  */
-void sleep(void)
+void hal_sleep(void)
 {
 #if (SAMD21) || (SAMR21)
     system_set_sleepmode(SYSTEM_SLEEPMODE_IDLE_2);
@@ -43,7 +43,7 @@
  * @param[void] void
  * @return      void
  */
-void deepsleep(void)
+void hal_deepsleep(void)
 {
     system_set_sleepmode(SYSTEM_SLEEPMODE_STANDBY);
     system_sleep();
--- a/targets/TARGET_Atmel/TARGET_SAM_CortexM4/drivers/pmc/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_Atmel/TARGET_SAM_CortexM4/drivers/pmc/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -45,7 +45,7 @@
  */
 
 #include <compiler.h>
-#include "sleep.h"
+#include "mbed_sleep.h"
 
 /* SAM3 and SAM4 series */
 #if (SAM3S || SAM3N || SAM3XA || SAM3U || SAM4S || SAM4E || SAM4N || SAM4C || \
--- a/targets/TARGET_Atmel/TARGET_SAM_CortexM4/sleep_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_Atmel/TARGET_SAM_CortexM4/sleep_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -24,7 +24,7 @@
  * @param[void] void
  * @return      void
  */
-void sleep(void)
+void hal_sleep(void)
 {
     enum sleepmgr_mode sleep_mode;
 
@@ -40,10 +40,10 @@
  * @param[void] void
  * @return      void
  */
-void deepsleep(void)
+void hal_deepsleep(void)
 {
     enum sleepmgr_mode sleep_mode;
 
     sleep_mode = SLEEPMGR_SLEEP_WFE;
     sleepmgr_sleep(sleep_mode);
-}
\ No newline at end of file
+}
--- a/targets/TARGET_Freescale/TARGET_K20XX/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_Freescale/TARGET_K20XX/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -17,7 +17,7 @@
 #include "cmsis.h"
 
 //Normal wait mode
-void sleep(void)
+void hal_sleep(void)
 {
     SMC->PMPROT = SMC_PMPROT_AVLLS_MASK | SMC_PMPROT_ALLS_MASK | SMC_PMPROT_AVLP_MASK;
 
@@ -27,7 +27,7 @@
 }
 
 //Very low-power stop mode
-void deepsleep(void)
+void hal_deepsleep(void)
 {
     //Check if ADC is enabled and HS mode is set, if yes disable it (lowers power consumption by 60uA)
     uint8_t ADC_HSC = 0;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_Freescale/TARGET_KLXX/TARGET_KL46Z/flash_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,101 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "flash_api.h"
+#include "flash_data.h"
+#include "mbed_critical.h"
+
+// This file is automagically generated
+
+// This is a flash algo binary blob. It is PIC (position independent code) that should be stored in RAM
+static uint32_t FLASH_ALGO[] = {
+    0xb5104935, 0x60084449, 0x21004834, 0x48356001, 0x44484a33, 0x22016002, 0x04926041, 0x02926082,
+    0x220560c2, 0x61420692, 0x03122201, 0x46026182, 0x70113220, 0x62411e49, 0xf939f000, 0xd0002800,
+    0xbd102001, 0x47702000, 0xb5084a27, 0x0349447a, 0x0c0a9200, 0x48234601, 0x44482300, 0xf9c4f000,
+    0xd0002800, 0xbd082001, 0x491fb508, 0x481d4479, 0x44483920, 0xf8a1f000, 0xd10e2800, 0x4478481a,
+    0x38324b1a, 0x9000447b, 0x22044816, 0x44484918, 0xf967f000, 0xd0002800, 0xbd082001, 0x4b12b510,
+    0x4601447b, 0x2201480f, 0x02923b54, 0xf0004448, 0xbd10f8b6, 0x460cb538, 0x4479490b, 0x9100396e,
+    0x48084601, 0x46224613, 0xf0004448, 0x2800f94a, 0x2001d000, 0x0000bd38, 0x00000004, 0x40048100,
+    0x40020000, 0x00000008, 0x000000a5, 0x0000055c, 0x0000040c, 0x4604b570, 0x25006800, 0x061b7803,
+    0x2370d5fc, 0x20007003, 0x0003e03a, 0xfa5df000, 0x0f0b070c, 0x1f1b1713, 0x2f2b2723, 0x68263633,
+    0x71f37813, 0x6826e02a, 0x71b37853, 0x6826e026, 0x71737893, 0x6826e022, 0x713378d3, 0x6826e01e,
+    0x72f37913, 0x6826e01a, 0x72b37953, 0x6826e016, 0x72737993, 0x6826e012, 0x723379d3, 0x6826e00e,
+    0x73f37a13, 0x6826e00a, 0x73b37a53, 0x6826e006, 0x73737a93, 0x6826e002, 0x73337ad3, 0xb2c01c40,
+    0xd9c24288, 0x20806821, 0xe0037008, 0x1c416a60, 0x4780d000, 0x78006820, 0xd5f70600, 0x78006820,
+    0xd5010681, 0xe0062504, 0xd50106c1, 0xe0022508, 0xd00007c0, 0x46282510, 0xb508bd70, 0x2244460b,
+    0x700a4669, 0x2100466a, 0xbd084798, 0x4614b538, 0xd002078a, 0x300120ff, 0x6842bd38, 0xd803428a,
+    0x18d36883, 0xd80d428b, 0x428b68c3, 0x6902d803, 0x428a189a, 0x2002d801, 0x2201bd38, 0x05d21ac9,
+    0xe0001889, 0x22081a89, 0x701a466b, 0x705a0c0a, 0x709a0a0a, 0x466a70d9, 0x47a02103, 0xb5ffbd38,
+    0x4615b081, 0x27019a01, 0x26006852, 0x02bf1948, 0xd804428a, 0x689b9b01, 0x428318d3, 0x9a01d20f,
+    0x428a68d2, 0x9b01d804, 0x18d3691b, 0xd2014283, 0xe0292602, 0x21011a88, 0x184405c9, 0x1a8ce000,
+    0x46204639, 0xf904f000, 0xd0022900, 0x360126ff, 0x4639e01a, 0xf0004628, 0x2900f8fb, 0x2601d012,
+    0x2009e012, 0x70084669, 0x70480c20, 0x70880a20, 0x9b0470cc, 0x2103466a, 0x47989801, 0xd1030006,
+    0x19e41bed, 0xd1ec2d00, 0xb0054630, 0xb5f0bdf0, 0x24006801, 0x0612780a, 0x2270d5fc, 0x6802700a,
+    0x71d12103, 0x22806801, 0x6803718a, 0x71592100, 0x23fc6805, 0x6803712b, 0x680373d9, 0x6802701a,
+    0x061b7813, 0x7a55d5fc, 0x07177a12, 0x0f3f2201, 0x10560412, 0xf000003b, 0x0910f968, 0x0b0d0b0b,
+    0x0b0b0b0b, 0x090d0b0b, 0x0e090b0b, 0xe0026102, 0xe0006101, 0x072a6106, 0x00130f12, 0xf955f000,
+    0x0c090910, 0x1815120f, 0x091f1d1b, 0x09090909, 0x61c10a09, 0xbdf04620, 0x03092101, 0x2101e7f9,
+    0xe7f602c9, 0x02892101, 0x2101e7f3, 0xe7f00249, 0x310121ff, 0x2180e7ed, 0x2140e7eb, 0x2120e7e9,
+    0xb5fee7e7, 0x46074616, 0x2000461d, 0x078b198a, 0x20ffd002, 0xbdfe3001, 0xd00107b3, 0xbdfe2001,
+    0x428b687b, 0x68bcd803, 0x4294191c, 0x68fbd20d, 0xd803428b, 0x191c693c, 0xd2014294, 0xbdfe2002,
+    0x1ac92201, 0x188c05d2, 0x1acce01b, 0x2006e019, 0x70084669, 0x70480c20, 0x70880a20, 0x78e870cc,
+    0x78a87108, 0x78687148, 0x78287188, 0x9b0871c8, 0x2107466a, 0x47984638, 0xd1e02800, 0x1f361d24,
+    0x2e001d2d, 0xbdfed1e3, 0x4615b5fe, 0x68424604, 0x184000a8, 0x428a461e, 0x68a3d803, 0x428b18d3,
+    0x68e3d808, 0xd803428b, 0x19db6927, 0xd801428b, 0xbdfe2002, 0xd8054282, 0x18d368a3, 0xd3014283,
+    0xe00a1a8f, 0x428268e2, 0x6923d903, 0x428318d3, 0x1a88d3ee, 0x05c92101, 0x21041847, 0xf0004638,
+    0x2900f817, 0x20ffd002, 0xbdfe3001, 0x46692001, 0x0c387008, 0x0a387048, 0x70cf7088, 0x71080a28,
+    0x718e714d, 0x466a9b08, 0x46202106, 0xbdfe4798, 0x09032200, 0xd32c428b, 0x428b0a03, 0x2300d311,
+    0xe04e469c, 0x430b4603, 0x2200d43c, 0x428b0843, 0x0903d331, 0xd31c428b, 0x428b0a03, 0x4694d301,
+    0x09c3e03f, 0xd301428b, 0x1ac001cb, 0x09834152, 0xd301428b, 0x1ac0018b, 0x09434152, 0xd301428b,
+    0x1ac0014b, 0x09034152, 0xd301428b, 0x1ac0010b, 0x08c34152, 0xd301428b, 0x1ac000cb, 0x08834152,
+    0xd301428b, 0x1ac0008b, 0x08434152, 0xd301428b, 0x1ac0004b, 0x1a414152, 0x4601d200, 0x46104152,
+    0xe05d4770, 0xd0000fca, 0x10034249, 0x4240d300, 0x22004053, 0x0903469c, 0xd32d428b, 0x428b0a03,
+    0x22fcd312, 0xba120189, 0x428b0a03, 0x0189d30c, 0x428b1192, 0x0189d308, 0x428b1192, 0x0189d304,
+    0x1192d03a, 0x0989e000, 0x428b09c3, 0x01cbd301, 0x41521ac0, 0x428b0983, 0x018bd301, 0x41521ac0,
+    0x428b0943, 0x014bd301, 0x41521ac0, 0x428b0903, 0x010bd301, 0x41521ac0, 0x428b08c3, 0x00cbd301,
+    0x41521ac0, 0x428b0883, 0x008bd301, 0x41521ac0, 0x0843d2d9, 0xd301428b, 0x1ac0004b, 0x1a414152,
+    0x4601d200, 0x41524663, 0x4610105b, 0x4240d301, 0xd5002b00, 0x47704249, 0x105b4663, 0x4240d300,
+    0x2000b501, 0x46c046c0, 0xb430bd02, 0x1e644674, 0x1c647825, 0xd20042ab, 0x5d63461d, 0x18e3005b,
+    0x4718bc30, 0xfffffffe, 0xffffffff, 0xfffffffe, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
+    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000
+};
+
+static const flash_algo_t flash_algo_config = {
+    .init = 0x1,
+    .uninit = 0x45,
+    .erase_sector = 0x9d,
+    .program_page = 0xb5,
+    .static_base = 0x5f0,
+    .algo_blob = FLASH_ALGO
+};
+
+static const sector_info_t sectors_info[] = {
+    {0x0, 0x400},
+};
+
+static const flash_target_config_t flash_target_config = {
+    .page_size  = 0x100,
+    .flash_start = 0x0,
+    .flash_size = 0x40000,
+    .sectors = sectors_info,
+    .sector_info_count = sizeof(sectors_info) / sizeof(sector_info_t)
+};
+
+void flash_set_target_config(flash_t *obj)
+{
+    obj->flash_algo = &flash_algo_config;
+    obj->target_config = &flash_target_config;
+}
--- a/targets/TARGET_Freescale/TARGET_KLXX/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_Freescale/TARGET_KLXX/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -18,7 +18,7 @@
 #include "PeripheralPins.h"
 
 //Normal wait mode
-void sleep(void)
+void hal_sleep(void)
 {
     SMC->PMPROT = SMC_PMPROT_AVLLS_MASK | SMC_PMPROT_ALLS_MASK | SMC_PMPROT_AVLP_MASK;
 
@@ -28,7 +28,7 @@
 }
 
 //Very low-power stop mode
-void deepsleep(void)
+void hal_deepsleep(void)
 {
     //Check if ADC is enabled and HS mode is set, if yes disable it (lowers power consumption by 60uA)
     uint8_t ADC_HSC = 0;
--- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/device/TOOLCHAIN_ARM_STD/MK64FN1M0xxx12.sct	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/device/TOOLCHAIN_ARM_STD/MK64FN1M0xxx12.sct	Tue Mar 14 16:40:56 2017 +0000
@@ -60,14 +60,22 @@
   #define __ram_vector_table_size__    0x00000000
 #endif
 
-#define m_interrupts_start             0x00000000
+#if !defined(MBED_APP_START)
+  #define MBED_APP_START 0
+#endif
+
+#if !defined(MBED_APP_SIZE)
+  #define MBED_APP_SIZE 0x100000
+#endif
+
+#define m_interrupts_start             MBED_APP_START
 #define m_interrupts_size              0x00000400
 
-#define m_flash_config_start           0x00000400
+#define m_flash_config_start           MBED_APP_START + 0x400
 #define m_flash_config_size            0x00000010
 
-#define m_text_start                   0x00000410
-#define m_text_size                    0x000FFBF0
+#define m_text_start                   MBED_APP_START + 0x410
+#define m_text_size                    MBED_APP_SIZE - 0x410
 
 #define m_interrupts_ram_start         0x1FFF0000
 #define m_interrupts_ram_size          __ram_vector_table_size__
--- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/device/TOOLCHAIN_GCC_ARM/MK64FN1M0xxx12.ld	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/device/TOOLCHAIN_GCC_ARM/MK64FN1M0xxx12.ld	Tue Mar 14 16:40:56 2017 +0000
@@ -63,6 +63,14 @@
  * heap and the page heap in uVisor applications. */
 __heap_size__ = 0x6000;
 
+#if !defined(MBED_APP_START)
+  #define MBED_APP_START 0
+#endif
+
+#if !defined(MBED_APP_SIZE)
+  #define MBED_APP_SIZE 0x100000
+#endif
+
 HEAP_SIZE  = DEFINED(__heap_size__)  ? __heap_size__  : 0x0400;
 STACK_SIZE = DEFINED(__stack_size__) ? __stack_size__ : 0x0400;
 M_VECTOR_RAM_SIZE = DEFINED(__ram_vector_table__) ? 0x0400 : 0x0;
@@ -70,9 +78,9 @@
 /* Specify the memory areas */
 MEMORY
 {
-  m_interrupts          (RX)  : ORIGIN = 0x00000000, LENGTH = 0x00000400
-  m_flash_config        (RX)  : ORIGIN = 0x00000400, LENGTH = 0x00000010
-  m_text                (RX)  : ORIGIN = 0x00000410, LENGTH = 0x000FFBF0
+  m_interrupts          (RX)  : ORIGIN = MBED_APP_START, LENGTH = 0x400
+  m_flash_config        (RX)  : ORIGIN = MBED_APP_START + 0x400, LENGTH = 0x10
+  m_text                (RX)  : ORIGIN = MBED_APP_START + 0x410, LENGTH = MBED_APP_SIZE - 0x410
   m_data                (RW)  : ORIGIN = 0x1FFF0000, LENGTH = 0x00010000
   m_data_2              (RW)  : ORIGIN = 0x20000000, LENGTH = 0x00030000
 }
--- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/device/TOOLCHAIN_IAR/MK64FN1M0xxx12.icf	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/device/TOOLCHAIN_IAR/MK64FN1M0xxx12.icf	Tue Mar 14 16:40:56 2017 +0000
@@ -53,17 +53,25 @@
 define symbol __stack_size__=0x8000;
 define symbol __heap_size__=0x10000;
 
+if (!isdefinedsymbol(MBED_APP_START)) {
+    define symbol MBED_APP_START = 0;
+}
+
+if (!isdefinedsymbol(MBED_APP_SIZE)) {
+    define symbol MBED_APP_SIZE = 0x100000;
+}
+
 define symbol __ram_vector_table_size__ =  isdefinedsymbol(__ram_vector_table__) ? 0x00000400 : 0;
 define symbol __ram_vector_table_offset__ =  isdefinedsymbol(__ram_vector_table__) ? 0x000003FF : 0;
 
-define symbol m_interrupts_start       = 0x00000000;
-define symbol m_interrupts_end         = 0x000003FF;
+define symbol m_interrupts_start       = MBED_APP_START;
+define symbol m_interrupts_end         = MBED_APP_START + 0x3FF;
 
-define symbol m_flash_config_start     = 0x00000400;
-define symbol m_flash_config_end       = 0x0000040F;
+define symbol m_flash_config_start     = MBED_APP_START + 0x400;
+define symbol m_flash_config_end       = MBED_APP_START + 0x40F;
 
-define symbol m_text_start             = 0x00000410;
-define symbol m_text_end               = 0x000FFFFF;
+define symbol m_text_start             = MBED_APP_START + 0x410;
+define symbol m_text_end               = MBED_APP_START + MBED_APP_SIZE - 1;
 
 define symbol m_interrupts_ram_start   = 0x1FFF0000;
 define symbol m_interrupts_ram_end     = 0x1FFF0000 + __ram_vector_table_offset__;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/flash_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,87 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "flash_api.h"
+#include "flash_data.h"
+#include "mbed_critical.h"
+
+// This file is automagically generated
+
+// This is a flash algo binary blob. It is PIC (position independent code) that should be stored in RAM
+static uint32_t FLASH_ALGO[] = {
+    0xb5104938, 0x60084449, 0xf24c4837, 0x81c15120, 0x1128f64d, 0x880181c1, 0x0101f021, 0x48348001,
+    0x44484932, 0x1280f44f, 0x21006001, 0x1201e9c0, 0x52a0f04f, 0xf44f6142, 0x61824280, 0x1020f880,
+    0x62411e49, 0xf939f000, 0xd0002800, 0xbd102001, 0x47702000, 0x4a27b508, 0x9200447a, 0x02cff3c1,
+    0x48234601, 0x44482300, 0xf92cf000, 0xd0002800, 0xbd082001, 0x491fb508, 0x481d4479, 0x44483920,
+    0xf89ff000, 0xd10f2800, 0x4478481a, 0x38324b1a, 0x9000447b, 0x22084816, 0x6181f44f, 0xf0004448,
+    0x2800f959, 0x2001d000, 0x4b12bd08, 0x4601447b, 0x3b54480f, 0x5280f44f, 0xf0004448, 0xb508b8b6,
+    0x1dc94613, 0x0207f021, 0x4479490a, 0x91003972, 0x48074601, 0xf0004448, 0x2800f93d, 0x2001d000,
+    0x0000bd08, 0x00000004, 0x40052000, 0x40020000, 0x00000008, 0x000000a1, 0x0000037c, 0x4604b570,
+    0x25006800, 0x061b7803, 0x2370d5fc, 0x20007003, 0x280ce03a, 0xe8dfd236, 0x0a06f000, 0x1a16120e,
+    0x2a26221e, 0x6826322e, 0x71f37813, 0x6826e02a, 0x71b37853, 0x6826e026, 0x71737893, 0x6826e022,
+    0x713378d3, 0x6826e01e, 0x72f37913, 0x6826e01a, 0x72b37953, 0x6826e016, 0x72737993, 0x6826e012,
+    0x723379d3, 0x6826e00e, 0x73f37a13, 0x6826e00a, 0x73b37a53, 0x6826e006, 0x73737a93, 0x6826e002,
+    0x73337ad3, 0xb2c01c40, 0xd9c24288, 0x20806821, 0xe0037008, 0x1c416a60, 0x4780d000, 0x78006820,
+    0xd5f70600, 0x78006820, 0xd5010681, 0xe0062504, 0xd50106c1, 0xe0022508, 0xd00007c0, 0x46282510,
+    0xb508bd70, 0x460b2244, 0x2000f88d, 0x2100466a, 0xbd084798, 0x4614b538, 0xd002070a, 0x7080f44f,
+    0x6843bd38, 0xd803428b, 0x441a6882, 0xd80c428a, 0x428b68c3, 0x6902d803, 0x428a441a, 0x2002d801,
+    0x1ac9bd38, 0x0100f501, 0x1ac9e000, 0xf88d2208, 0x0c0a2000, 0x2001f88d, 0xf88d0a0a, 0xf88d2002,
+    0x466a1003, 0x47a02103, 0xe92dbd38, 0x460745f8, 0x46164698, 0x2000687b, 0x428b198a, 0x68bcd803,
+    0x4294441c, 0x68fbd20e, 0xd803428b, 0x441c693c, 0xd2024294, 0xe8bd2002, 0x1acc85f8, 0x0400f504,
+    0xe0022500, 0xf44f1acc, 0xfbb45580, 0xfb05f1f5, 0xb1114111, 0x7080f44f, 0xfbb6e7ed, 0xfb05f1f5,
+    0xb1a96111, 0xe7e62001, 0xa000f88d, 0xf88d0c20, 0x0a200001, 0x0002f88d, 0x4003f88d, 0x2103466a,
+    0x46434638, 0x28004798, 0x1b76d1d5, 0xe001442c, 0x0a09f04f, 0xd1e72e00, 0x4601e7cd, 0x61082000,
+    0x477061c8, 0x41fce92d, 0x9d086846, 0x1402eb01, 0xd803428e, 0x44376887, 0xd80a428f, 0x428f68c7,
+    0xf8d0d804, 0x4467c010, 0xd802428f, 0xe8bd2002, 0x42a681fc, 0x6887d805, 0x42a74437, 0x1b89d301,
+    0x68c6e009, 0xd90342a6, 0x44376907, 0xd3ed42a7, 0xf5011b89, 0x24100100, 0xf6f4fbb1, 0x1416fb04,
+    0xf44fb114, 0xe7e27080, 0xf88d2401, 0x0c0c4000, 0x4001f88d, 0xf88d0a0c, 0xf88d4002, 0x0a111003,
+    0x1004f88d, 0x2005f88d, 0x3006f88d, 0x2106466a, 0xe7cc47a8, 0x43fee92d, 0x46074616, 0x2000461c,
+    0xf8dd198a, 0x074b8028, 0xf44fd003, 0xe8bd7080, 0x077383fe, 0x2001d001, 0x687be7f9, 0xd803428b,
+    0x441d68bd, 0xd20c4295, 0x428b68fb, 0x693dd803, 0x4295441d, 0x2002d201, 0x1acde7e9, 0x0500f505,
+    0x1acde02e, 0x2007e02c, 0x0000f88d, 0xf88d0c28, 0x0a280001, 0x0002f88d, 0x5003f88d, 0xf88d78e0,
+    0x78a00004, 0x0005f88d, 0xf88d7860, 0x78200006, 0x0007f88d, 0xf88d79e0, 0x79a00008, 0x0009f88d,
+    0xf88d7960, 0x7920000a, 0x000bf88d, 0x210b466a, 0x46434638, 0x28004798, 0x3508d1b9, 0x34083e08,
+    0xd1d02e00, 0x0000e7b3, 0xfffffffe, 0x00000000, 0xffffffff, 0xfffffffe, 0x00000000, 0x00000000,
+    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
+    0x00000000, 0x00000000
+};
+
+static const flash_algo_t flash_algo_config = {
+    .init = 0x1,
+    .uninit = 0x51,
+    .erase_sector = 0xab,
+    .program_page = 0xbf,
+    .static_base = 0x418,
+    .algo_blob = FLASH_ALGO
+};
+
+static const sector_info_t sectors_info[] = {
+    {0x0, 0x1000},
+};
+
+static const flash_target_config_t flash_target_config = {
+    .page_size  = 0x200,
+    .flash_start = 0x0,
+    .flash_size = 0x100000,
+    .sectors = sectors_info,
+    .sector_info_count = sizeof(sectors_info) / sizeof(sector_info_t)
+};
+
+void flash_set_target_config(flash_t *obj)
+{
+    obj->flash_algo = &flash_algo_config;
+    obj->target_config = &flash_target_config;
+}
--- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/api/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/api/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -18,14 +18,14 @@
 #include "fsl_smc.h"
 #include "fsl_clock_config.h"
 
-void sleep(void)
+void hal_sleep(void)
 {
     SMC_SetPowerModeProtection(SMC, kSMC_AllowPowerModeAll);
 
     SMC_SetPowerModeWait(SMC);
 }
 
-void deepsleep(void)
+void hal_deepsleep(void)
 {
 #if (defined(FSL_FEATURE_SOC_MCG_COUNT) && FSL_FEATURE_SOC_MCG_COUNT)
     mcg_mode_t mode = CLOCK_GetMode();
--- a/targets/TARGET_Maxim/TARGET_MAX32600/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_Maxim/TARGET_MAX32600/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -41,7 +41,7 @@
 static mxc_uart_regs_t *stdio_uart = (mxc_uart_regs_t*)STDIO_UART;
 
 // Normal wait mode
-void sleep(void)
+void hal_sleep(void)
 {
     // Normal sleep mode for ARM core
     SCB->SCR = 0;
@@ -70,7 +70,7 @@
 }
 
 // Low-power stop mode
-void deepsleep(void)
+void hal_deepsleep(void)
 {
     __disable_irq();
 
--- a/targets/TARGET_Maxim/TARGET_MAX32610/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_Maxim/TARGET_MAX32610/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -41,7 +41,7 @@
 static mxc_uart_regs_t *stdio_uart = (mxc_uart_regs_t*)STDIO_UART;
 
 // Normal wait mode
-void sleep(void)
+void hal_sleep(void)
 {
     // Normal sleep mode for ARM core
     SCB->SCR = 0;
@@ -70,7 +70,7 @@
 }
 
 // Low-power stop mode
-void deepsleep(void)
+void hal_deepsleep(void)
 {
     __disable_irq();
 
--- a/targets/TARGET_Maxim/TARGET_MAX32620/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_Maxim/TARGET_MAX32620/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -54,7 +54,7 @@
 static int restore_usb;
 static usb_state_t usb_state;
 
-void sleep(void)
+void hal_sleep(void)
 {
     // Normal sleep mode for ARM core
     SCB->SCR = 0;
@@ -109,7 +109,7 @@
 }
 
 // Low-power stop mode
-void deepsleep(void)
+void hal_deepsleep(void)
 {
     unsigned int part_rev = MXC_PWRMAN->mask_id0 & MXC_F_PWRMAN_MASK_ID0_REVISION_ID;
 
--- a/targets/TARGET_Maxim/TARGET_MAX32625/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_Maxim/TARGET_MAX32625/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -34,13 +34,13 @@
 #include "sleep_api.h"
 #include "lp.h"
 
-void sleep(void)
+void hal_sleep(void)
 {
     LP_EnterLP2();
 }
 
 // Low-power stop mode
-void deepsleep(void)
+void hal_deepsleep(void)
 {
-    sleep();
+    hal_sleep();
 }
--- a/targets/TARGET_NORDIC/TARGET_MCU_NRF51822/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NORDIC/TARGET_MCU_NRF51822/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -16,9 +16,9 @@
 #include "sleep_api.h"
 #include "cmsis.h"
 #include "mbed_interface.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 
-MBED_WEAK void sleep(void)
+MBED_WEAK void hal_sleep(void)
 {
     // ensure debug is disconnected if semihost is enabled....
     NRF_POWER->TASKS_LOWPWR = 1;
@@ -26,8 +26,8 @@
     __WFE();
 }
 
-MBED_WEAK void deepsleep(void)
+MBED_WEAK void hal_deepsleep(void)
 {
-    sleep();
+    hal_sleep();
     //   NRF_POWER->SYSTEMOFF=1;
 }
--- a/targets/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c	Tue Mar 14 16:40:56 2017 +0000
@@ -19,7 +19,7 @@
 #include "cmsis.h"
 #include "PeripheralNames.h"
 #include "nrf_delay.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 
 /*
  * Note: The micro-second timer API on the nRF51 platform is implemented using
--- a/targets/TARGET_NORDIC/TARGET_NRF5/i2c_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NORDIC/TARGET_NRF5/i2c_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -1,5 +1,5 @@
 /* 
- * Copyright (c) 2013 Nordic Semiconductor ASA
+ * Copyright (c) 2017 Nordic Semiconductor ASA
  * All rights reserved.
  * 
  * Redistribution and use in source and binary forms, with or without modification,
@@ -43,8 +43,17 @@
 
 #include "mbed_assert.h"
 #include "mbed_error.h"
-#include "nrf_drv_twi.h"
+#include "nrf_twi.h"
+#include "nrf_drv_common.h"
+#include "nrf_drv_config.h"
 #include "app_util_platform.h"
+#include "nrf_gpio.h"
+#include "nrf_delay.h"
+
+// An arbitrary value used as the counter in loops waiting for given event
+// (e.g. STOPPED), needed to avoid infinite loops (and not involve any timers
+// or tickers).
+#define TIMEOUT_VALUE  1000
 
 #if DEVICE_I2C_ASYNCH
     #define TWI_IDX(obj)    ((obj)->i2c.twi_idx)
@@ -54,59 +63,36 @@
 #define TWI_INFO(obj)   (&m_twi_info[TWI_IDX(obj)])
 
 typedef struct {
-    bool initialized;
-    nrf_drv_twi_config_t config;
-    volatile bool transfer_finished;
+    bool                initialized;
+    uint32_t            pselsda;
+    uint32_t            pselscl;
+    nrf_twi_frequency_t frequency;
+    bool                start_twi;
 
-    #if DEVICE_I2C_ASYNCH
-    volatile uint32_t events;
-    void (*handler)(void);
-    uint32_t event_mask;
-    #endif
+#if DEVICE_I2C_ASYNCH
+    volatile bool   active;
+    uint8_t const  *tx;
+    size_t          tx_length;
+    uint8_t        *rx;
+    size_t          rx_length;
+    bool            stop;
+
+    volatile uint32_t   events;
+    void              (*handler)(void);
+    uint32_t            evt_mask;
+#endif // DEVICE_I2C_ASYNCH
 } twi_info_t;
 static twi_info_t m_twi_info[TWI_COUNT];
 
-static nrf_drv_twi_t const m_twi_instances[TWI_COUNT] = {
+static NRF_TWI_Type * const m_twi_instances[TWI_COUNT] = {
 #if TWI0_ENABLED
-    NRF_DRV_TWI_INSTANCE(0),
+    NRF_TWI0,
 #endif
 #if TWI1_ENABLED
-    NRF_DRV_TWI_INSTANCE(1),
+    NRF_TWI1,
 #endif
 };
 
-static void twi_event_handler(nrf_drv_twi_evt_t const *event, void *context)
-{
-    twi_info_t * twi_info = TWI_INFO((i2c_t *)context);
-    twi_info->transfer_finished = true;
-
-#if DEVICE_I2C_ASYNCH
-    switch (event->type) {
-    case NRF_DRV_TWI_EVT_DONE:
-        twi_info->events |= I2C_EVENT_TRANSFER_COMPLETE;
-        break;
-
-    case NRF_DRV_TWI_EVT_ADDRESS_NACK:
-        twi_info->events |= I2C_EVENT_ERROR_NO_SLAVE;
-        break;
-
-    case NRF_DRV_TWI_EVT_DATA_NACK:
-        twi_info->events |= I2C_EVENT_ERROR;
-        break;
-    }
-
-    if (twi_info->handler) {
-        twi_info->handler();
-    }
-#endif // DEVICE_I2C_ASYNCH
-}
-
-static uint8_t twi_address(int i2c_address)
-{
-    // The TWI driver requires 7-bit slave address (without R/W bit).
-    return (i2c_address >> 1);
-}
-
 void SPI0_TWI0_IRQHandler(void);
 void SPI1_TWI1_IRQHandler(void);
 
@@ -123,207 +109,580 @@
         SPI1_TWI1_IRQn,
         (uint32_t) SPI1_TWI1_IRQHandler
     }
-    #endif 
+    #endif
 };
+#ifdef NRF51
+    #define TWI_IRQ_PRIORITY  APP_IRQ_PRIORITY_LOW
+#elif defined(NRF52)
+    #define TWI_IRQ_PRIORITY  APP_IRQ_PRIORITY_LOWEST
+#endif
+
+
+#if DEVICE_I2C_ASYNCH
+static void start_asynch_rx(twi_info_t *twi_info, NRF_TWI_Type *twi)
+{
+    if (twi_info->rx_length == 1 && twi_info->stop) {
+        nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_STOP_MASK);
+    } else {
+        nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_SUSPEND_MASK);
+    }
+    nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTRX);
+}
+
+static void twi_irq_handler(uint8_t instance_idx)
+{
+    twi_info_t *twi_info = &m_twi_info[instance_idx];
+
+    NRF_TWI_Type *twi = m_twi_instances[instance_idx];
+    if (nrf_twi_event_check(twi, NRF_TWI_EVENT_ERROR)) {
+        nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
+
+        // In case of an error, force STOP.
+        // The current transfer may be suspended (if it is RX), so it must be
+        // resumed before the STOP task is triggered.
+        nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
+        nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
+
+        uint32_t errorsrc = nrf_twi_errorsrc_get_and_clear(twi);
+        twi_info->events |= I2C_EVENT_ERROR;
+        if (errorsrc & NRF_TWI_ERROR_ADDRESS_NACK) {
+            twi_info->events |= I2C_EVENT_ERROR_NO_SLAVE;
+        }
+        if (errorsrc & NRF_TWI_ERROR_DATA_NACK) {
+            twi_info->events |= I2C_EVENT_TRANSFER_EARLY_NACK;
+        }
+    }
+
+    bool finished = false;
+
+    if (nrf_twi_event_check(twi, NRF_TWI_EVENT_TXDSENT)) {
+        nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
+
+        MBED_ASSERT(twi_info->tx_length > 0);
+        --(twi_info->tx_length);
+        // Send next byte if there is still something to be sent.
+        if (twi_info->tx_length > 0) {
+            nrf_twi_txd_set(twi, *(twi_info->tx));
+            ++(twi_info->tx);
+        // It TX is done, start RX if requested.
+        } else if (twi_info->rx_length > 0) {
+            start_asynch_rx(twi_info, twi);
+        // If there is nothing more to do, finalize the transfer.
+        } else {
+            if (twi_info->stop) {
+                nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
+            } else {
+                nrf_twi_task_trigger(twi, NRF_TWI_TASK_SUSPEND);
+                finished = true;
+            }
+            twi_info->events |= I2C_EVENT_TRANSFER_COMPLETE;
+        }
+    }
+
+    if (nrf_twi_event_check(twi, NRF_TWI_EVENT_RXDREADY)) {
+        nrf_twi_event_clear(twi, NRF_TWI_EVENT_RXDREADY);
+
+        MBED_ASSERT(twi_info->rx_length > 0);
+        *(twi_info->rx) = nrf_twi_rxd_get(twi);
+        ++(twi_info->rx);
+        --(twi_info->rx_length);
+
+        if (twi_info->rx_length > 0) {
+            // If more bytes should be received, resume the transfer
+            // (in case the stop condition should be generated after the next
+            // byte, change the shortcuts configuration first).
+            if (twi_info->rx_length == 1 && twi_info->stop) {
+                nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_STOP_MASK);
+            }
+            nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
+        } else {
+            // If all requested bytes were received, finalize the transfer.
+            finished = true;
+            twi_info->events |= I2C_EVENT_TRANSFER_COMPLETE;
+        }
+    }
+
+    if (finished ||
+        nrf_twi_event_check(twi, NRF_TWI_EVENT_STOPPED) ||
+        (nrf_twi_int_enable_check(twi, NRF_TWI_INT_SUSPENDED_MASK) &&
+         nrf_twi_event_check(twi, NRF_TWI_EVENT_SUSPENDED))) {
+        // There is no need to clear the STOPPED and SUSPENDED events here,
+        // they will no longer generate the interrupt - see below.
+
+        nrf_twi_shorts_set(twi, 0);
+        // Disable all interrupt sources.
+        nrf_twi_int_disable(twi, UINT32_MAX);
+        twi_info->active = false;
+
+        if (twi_info->handler) {
+            twi_info->handler();
+        }
+    }
+}
+
+#if TWI0_ENABLED
+static void irq_handler_twi0(void)
+{
+    twi_irq_handler(TWI0_INSTANCE_INDEX);
+}
+#endif
+#if TWI1_ENABLED
+static void irq_handler_twi1(void)
+{
+    twi_irq_handler(TWI1_INSTANCE_INDEX);
+}
+#endif
+static nrf_drv_irq_handler_t const m_twi_irq_handlers[TWI_COUNT] =
+{
+#if TWI0_ENABLED
+    irq_handler_twi0,
+#endif
+#if TWI1_ENABLED
+    irq_handler_twi1,
+#endif
+};
+#endif // DEVICE_I2C_ASYNCH
+
+
+static void configure_twi_pin(uint32_t pin, nrf_gpio_pin_dir_t dir)
+{
+    nrf_gpio_cfg(pin,
+        dir,
+        NRF_GPIO_PIN_INPUT_CONNECT,
+        NRF_GPIO_PIN_PULLUP,
+        NRF_GPIO_PIN_S0D1,
+        NRF_GPIO_PIN_NOSENSE);
+}
+
+static void twi_clear_bus(twi_info_t *twi_info)
+{
+    // Try to set SDA high, and check if no slave tries to drive it low.
+    nrf_gpio_pin_set(twi_info->pselsda);
+    configure_twi_pin(twi_info->pselsda, NRF_GPIO_PIN_DIR_OUTPUT);
+    // In case SDA is low, make up to 9 cycles on SCL line to help the slave
+    // that pulls SDA low release it.
+    if (!nrf_gpio_pin_read(twi_info->pselsda)) {
+        nrf_gpio_pin_set(twi_info->pselscl);
+        configure_twi_pin(twi_info->pselscl, NRF_GPIO_PIN_DIR_OUTPUT);
+        nrf_delay_us(4);
+
+        for (int i = 0; i < 9; i++) {
+            if (nrf_gpio_pin_read(twi_info->pselsda)) {
+                break;
+            }
+            nrf_gpio_pin_clear(twi_info->pselscl);
+            nrf_delay_us(4);
+            nrf_gpio_pin_set(twi_info->pselscl);
+            nrf_delay_us(4);
+        }
+
+        // Finally, generate STOP condition to put the bus into initial state.
+        nrf_gpio_pin_clear(twi_info->pselsda);
+        nrf_delay_us(4);
+        nrf_gpio_pin_set(twi_info->pselsda);
+    }
+}
 
 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
 {
     int i;
     for (i = 0; i < TWI_COUNT; ++i) {
         if (m_twi_info[i].initialized &&
-            m_twi_info[i].config.sda == (uint32_t)sda &&
-            m_twi_info[i].config.scl == (uint32_t)scl) {
+            m_twi_info[i].pselsda == (uint32_t)sda &&
+            m_twi_info[i].pselscl == (uint32_t)scl) {
             TWI_IDX(obj) = i;
-            TWI_INFO(obj)->config.frequency = NRF_TWI_FREQ_100K;
+            TWI_INFO(obj)->frequency = NRF_TWI_FREQ_100K;
             i2c_reset(obj);
             return;
         }
     }
 
-    nrf_drv_twi_config_t const config = {
-        .scl                = scl,
-        .sda                = sda,
-        .frequency          = NRF_TWI_FREQ_100K,  
-#ifdef NRF51
-        .interrupt_priority = APP_IRQ_PRIORITY_LOW
-#elif defined(NRF52)
-        .interrupt_priority = APP_IRQ_PRIORITY_LOWEST
-#endif
-        
-    };
-
     for (i = 0; i < TWI_COUNT; ++i) {
         if (!m_twi_info[i].initialized) {
+            TWI_IDX(obj) = i;
 
-            NVIC_SetVector(twi_handlers[i].IRQn, twi_handlers[i].vector);
+            twi_info_t *twi_info = TWI_INFO(obj);
+            twi_info->initialized = true;
+            twi_info->pselsda     = (uint32_t)sda;
+            twi_info->pselscl     = (uint32_t)scl;
+            twi_info->frequency   = NRF_TWI_FREQ_100K;
+            twi_info->start_twi   = false;
+#if DEVICE_I2C_ASYNCH
+            twi_info->active      = false;
+#endif
+
+            twi_clear_bus(twi_info);
 
-            nrf_drv_twi_t const *twi = &m_twi_instances[i];
-            ret_code_t ret_code =
-                nrf_drv_twi_init(twi, &config, twi_event_handler, obj);
-            if (ret_code == NRF_SUCCESS) {
-                TWI_IDX(obj) = i;
-                TWI_INFO(obj)->initialized = true;
-                TWI_INFO(obj)->config = config;
+            configure_twi_pin(twi_info->pselsda, NRF_GPIO_PIN_DIR_INPUT);
+            configure_twi_pin(twi_info->pselscl, NRF_GPIO_PIN_DIR_INPUT);
+
+            i2c_reset(obj);
 
-                nrf_drv_twi_enable(twi);
-                return;
-            }
+#if DEVICE_I2C_ASYNCH
+            nrf_drv_common_per_res_acquire(m_twi_instances[i],
+                m_twi_irq_handlers[i]);
+            NVIC_SetVector(twi_handlers[i].IRQn, twi_handlers[i].vector);
+            nrf_drv_common_irq_enable(twi_handlers[i].IRQn, TWI_IRQ_PRIORITY);
+#endif
+
+            return;
         }
     }
 
-    // No available peripheral
     error("No available I2C peripheral\r\n");
 }
 
 void i2c_reset(i2c_t *obj)
 {
     twi_info_t *twi_info = TWI_INFO(obj);
-    nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)];
+    NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
 
-    nrf_drv_twi_uninit(twi);
-    nrf_drv_twi_init(twi, &twi_info->config, twi_event_handler, obj);
-    nrf_drv_twi_enable(twi);
+    nrf_twi_disable(twi);
+    nrf_twi_pins_set(twi, twi_info->pselscl, twi_info->pselsda);
+    nrf_twi_frequency_set(twi, twi_info->frequency);
+    nrf_twi_enable(twi);
 }
 
 int i2c_start(i2c_t *obj)
 {
-    (void)obj;
+    twi_info_t *twi_info = TWI_INFO(obj);
+#if DEVICE_I2C_ASYNCH
+    if (twi_info->active) {
+        return I2C_ERROR_BUS_BUSY;
+    }
+#endif
+    twi_info->start_twi = true;
 
-    return -1; // Not implemented.
+    return 0;
 }
 
 int i2c_stop(i2c_t *obj)
 {
-    (void)obj;
+    NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
 
-    return -1; // Not implemented.
+    // The current transfer may be suspended (if it is RX), so it must be
+    // resumed before the STOP task is triggered.
+    nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
+    nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
+    uint32_t remaining_time = TIMEOUT_VALUE;
+    do {
+        if (nrf_twi_event_check(twi, NRF_TWI_EVENT_STOPPED)) {
+            return 0;
+        }
+    } while (--remaining_time);
+
+    return 1;
 }
 
 void i2c_frequency(i2c_t *obj, int hz)
 {
     twi_info_t *twi_info = TWI_INFO(obj);
-    nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)];
+    NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
 
     if (hz < 250000) {
-        twi_info->config.frequency = NRF_TWI_FREQ_100K;
+        twi_info->frequency = NRF_TWI_FREQ_100K;
     } else if (hz < 400000) {
-        twi_info->config.frequency = NRF_TWI_FREQ_250K;
+        twi_info->frequency = NRF_TWI_FREQ_250K;
     } else {
-        twi_info->config.frequency = NRF_TWI_FREQ_400K;
+        twi_info->frequency = NRF_TWI_FREQ_400K;
     }
-    nrf_twi_frequency_set(twi->reg.p_twi, twi_info->config.frequency);
+    nrf_twi_frequency_set(twi, twi_info->frequency);
+}
+
+static uint8_t twi_address(int i2c_address)
+{
+    // The TWI peripheral requires 7-bit slave address (without R/W bit).
+    return (i2c_address >> 1);
+}
+
+static void start_twi_read(NRF_TWI_Type *twi, int address)
+{
+    nrf_twi_event_clear(twi, NRF_TWI_EVENT_STOPPED);
+    nrf_twi_event_clear(twi, NRF_TWI_EVENT_RXDREADY);
+    nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
+    (void)nrf_twi_errorsrc_get_and_clear(twi);
+
+    nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_SUSPEND_MASK);
+
+    nrf_twi_address_set(twi, twi_address(address));
+    nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
+    nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTRX);
 }
 
 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
 {
-    (void)stop;
+    // Zero-length RX transfers are not supported. Such transfers cannot
+    // be easily achieved with TWI peripheral (some dirty tricks would be
+    // required for this), and they are actually useless (TX can be used
+    // to check if the address is acknowledged by a slave).
+    MBED_ASSERT(length > 0);
 
     twi_info_t *twi_info = TWI_INFO(obj);
-    nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)];
+#if DEVICE_I2C_ASYNCH
+    if (twi_info->active) {
+        return I2C_ERROR_BUS_BUSY;
+    }
+#endif
+    twi_info->start_twi = false;
+
+    NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
+    start_twi_read(twi, address);
+
+    int result = length;
+    while (length > 0) {
+        int byte_read_result = i2c_byte_read(obj, (stop && length == 1));
+        if (byte_read_result < 0) {
+            // When an error occurs, return the number of bytes that have been
+            // received successfully.
+            result -= length;
+            // Force STOP condition.
+            stop = 1;
+            break;
+        }
+        *data++ = (uint8_t)byte_read_result;
+        --length;
+    }
+
+    if (stop) {
+        (void)i2c_stop(obj);
+    }
+
+    return result;
+}
 
-    twi_info->transfer_finished = false;
-    ret_code_t ret_code = nrf_drv_twi_rx(twi, twi_address(address),
-        (uint8_t *)data, length);
-    if (ret_code != NRF_SUCCESS) {
-        return 0;
-    }
-    while (!twi_info->transfer_finished) {}
-    return nrf_drv_twi_data_count_get(twi);
+static uint8_t twi_byte_write(NRF_TWI_Type *twi, uint8_t data)
+{
+    nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
+    nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
+
+    nrf_twi_txd_set(twi, data);
+    uint32_t remaining_time = TIMEOUT_VALUE;
+    do {
+        if (nrf_twi_event_check(twi, NRF_TWI_EVENT_TXDSENT)) {
+            nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
+            return 1; // ACK received
+        }
+        if (nrf_twi_event_check(twi, NRF_TWI_EVENT_ERROR)) {
+            nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
+            return 0; // some error occurred
+        }
+    } while (--remaining_time);
+
+    return 2; // timeout;
+}
+
+static void start_twi_write(NRF_TWI_Type *twi, int address)
+{
+    nrf_twi_event_clear(twi, NRF_TWI_EVENT_STOPPED);
+    nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
+    nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
+    (void)nrf_twi_errorsrc_get_and_clear(twi);
+
+    nrf_twi_shorts_set(twi, 0);
+
+    nrf_twi_address_set(twi, twi_address(address));
+    nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
+    nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTTX);
 }
 
 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
 {
     twi_info_t *twi_info = TWI_INFO(obj);
-    nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)];
+#if DEVICE_I2C_ASYNCH
+    if (twi_info->active) {
+        return I2C_ERROR_BUS_BUSY;
+    }
+#endif
+    twi_info->start_twi = false;
+
+    NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
+    start_twi_write(twi, address);
+
+    // Special case - transaction with no data.
+    // It can be used to check if a slave acknowledges the address.
+    if (length == 0) {
+        nrf_twi_event_t event;
+        if (stop) {
+            event = NRF_TWI_EVENT_STOPPED;
+            nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
+        } else {
+            event = NRF_TWI_EVENT_SUSPENDED;
+            nrf_twi_event_clear(twi, event);
+            nrf_twi_task_trigger(twi, NRF_TWI_TASK_SUSPEND);
+        }
+        uint32_t remaining_time = TIMEOUT_VALUE;
+        do {
+            if (nrf_twi_event_check(twi, event)) {
+                break;
+            }
+        } while (--remaining_time);
 
-    twi_info->transfer_finished = false;
-    ret_code_t ret_code = nrf_drv_twi_tx(twi, twi_address(address),
-        (uint8_t const *)data, length, (stop == 0));
-    if (ret_code != NRF_SUCCESS) {
-        return 0;
+        uint32_t errorsrc = nrf_twi_errorsrc_get_and_clear(twi);
+        if (errorsrc & NRF_TWI_ERROR_ADDRESS_NACK) {
+            if (!stop) {
+                i2c_stop(obj);
+            }
+            return I2C_ERROR_NO_SLAVE;
+        }
+
+        return (remaining_time ? 0 : I2C_ERROR_BUS_BUSY);
     }
-    while (!twi_info->transfer_finished) {}
-    return nrf_drv_twi_data_count_get(twi);
+
+    int result = length;
+    do {
+        uint8_t byte_write_result = twi_byte_write(twi, (uint8_t)*data++);
+        if (byte_write_result != 1) {
+            if (byte_write_result == 0) {
+                // Check what kind of error has been signaled by TWI.
+                uint32_t errorsrc = nrf_twi_errorsrc_get_and_clear(twi);
+                if (errorsrc & NRF_TWI_ERROR_ADDRESS_NACK) {
+                    result = I2C_ERROR_NO_SLAVE;
+                } else {
+                    // Some other error - return the number of bytes that
+                    // have been sent successfully.
+                    result -= length;
+                }
+            } else {
+                result = I2C_ERROR_BUS_BUSY;
+            }
+            // Force STOP condition.
+            stop = 1;
+            break;
+        }
+        --length;
+    } while (length > 0);
+
+    if (stop) {
+        (void)i2c_stop(obj);
+    }
+
+    return result;
 }
 
 int i2c_byte_read(i2c_t *obj, int last)
 {
-    (void)obj;
-    (void)last;
+    NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
+
+    if (last) {
+        nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_STOP_MASK);
+    }
+    nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
 
-    return -1; // Not implemented.
+    uint32_t remaining_time = TIMEOUT_VALUE;
+    do {
+        if (nrf_twi_event_check(twi, NRF_TWI_EVENT_RXDREADY)) {
+            nrf_twi_event_clear(twi, NRF_TWI_EVENT_RXDREADY);
+            return nrf_twi_rxd_get(twi);
+        }
+        if (nrf_twi_event_check(twi, NRF_TWI_EVENT_ERROR)) {
+            nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
+            return I2C_ERROR_NO_SLAVE;
+        }
+    } while (--remaining_time);
+
+    return I2C_ERROR_BUS_BUSY;
 }
 
 int i2c_byte_write(i2c_t *obj, int data)
 {
-    (void)obj;
-    (void)data;
+    NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
+    twi_info_t *twi_info = TWI_INFO(obj);
+    if (twi_info->start_twi) {
+        twi_info->start_twi = false;
 
-    return -1; // Not implemented.
+        if (data & 1) {
+            start_twi_read(twi, data);
+        } else {
+            start_twi_write(twi, data);
+        }
+        return 1;
+    } else {
+        nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
+        // 0 - TWI signaled error (NAK is the only possibility here)
+        // 1 - ACK received
+        // 2 - timeout (clock stretched for too long?)
+        return twi_byte_write(twi, (uint8_t)data);
+    }
 }
 
 
 #if DEVICE_I2C_ASYNCH
-
 void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length,
                          void *rx, size_t rx_length, uint32_t address,
                          uint32_t stop, uint32_t handler,
                          uint32_t event, DMAUsage hint)
 {
-    (void)stop;
     (void)hint;
 
-    if (i2c_active(obj)) {
-        return;
-    }
-    if ((tx_length == 0) && (rx_length == 0)) {
-        return;
-    }
-
     twi_info_t *twi_info = TWI_INFO(obj);
-    twi_info->events     = 0;
-    twi_info->handler    = (void (*)(void))handler;
-    twi_info->event_mask = event;
+    if (twi_info->active) {
+        return;
+    }
+    twi_info->active    = true;
+    twi_info->events    = 0;
+    twi_info->handler   = (void (*)(void))handler;
+    twi_info->evt_mask  = event;
+    twi_info->tx_length = tx_length;
+    twi_info->tx        = tx;
+    twi_info->rx_length = rx_length;
+    twi_info->rx        = rx;
+    twi_info->stop      = stop;
 
-    uint8_t twi_addr = twi_address(address);
-    nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)];
+    NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
+
+    nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
+    nrf_twi_event_clear(twi, NRF_TWI_EVENT_RXDREADY);
+    nrf_twi_event_clear(twi, NRF_TWI_EVENT_STOPPED);
+    nrf_twi_event_clear(twi, NRF_TWI_EVENT_SUSPENDED);
+    nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
+    (void)nrf_twi_errorsrc_get_and_clear(twi);
 
-    if ((tx_length > 0) && (rx_length == 0)) {
-        nrf_drv_twi_xfer_desc_t const xfer =
-            NRF_DRV_TWI_XFER_DESC_TX(twi_addr, (uint8_t *)tx, tx_length);
-        nrf_drv_twi_xfer(twi, &xfer,
-            stop ? 0 : NRF_DRV_TWI_FLAG_TX_NO_STOP);
+    nrf_twi_address_set(twi, twi_address(address));
+    nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
+    // TX only, or TX + RX (after a repeated start).
+    if (tx_length > 0) {
+        nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTTX);
+        nrf_twi_txd_set(twi, *(twi_info->tx));
+        ++(twi_info->tx);
+    // RX only.
+    } else if (rx_length > 0) {
+        start_asynch_rx(twi_info, twi);
+    // Both 'tx_length' and 'rx_length' are 0 - this case may be used
+    // to test if the slave is presentand ready for transfer (by just
+    // sending the address and checking if it is acknowledged).
+    } else {
+        nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTTX);
+        if (stop) {
+            nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
+        } else {
+            nrf_twi_task_trigger(twi, NRF_TWI_TASK_SUSPEND);
+            nrf_twi_int_enable(twi, NRF_TWI_INT_SUSPENDED_MASK);
+        }
+        twi_info->events |= I2C_EVENT_TRANSFER_COMPLETE;
     }
-    else if ((tx_length == 0) && (rx_length > 0)) {
-        nrf_drv_twi_xfer_desc_t const xfer =
-            NRF_DRV_TWI_XFER_DESC_RX(twi_addr, rx, rx_length);
-        nrf_drv_twi_xfer(twi, &xfer, 0);
-    }
-    else if ((tx_length > 0) && (rx_length > 0)) {
-        nrf_drv_twi_xfer_desc_t const xfer =
-            NRF_DRV_TWI_XFER_DESC_TXRX(twi_addr,
-                (uint8_t *)tx, tx_length, rx, rx_length);
-        nrf_drv_twi_xfer(twi, &xfer, 0);
-    }
+
+    nrf_twi_int_enable(twi, NRF_TWI_INT_TXDSENT_MASK |
+                            NRF_TWI_INT_RXDREADY_MASK |
+                            NRF_TWI_INT_STOPPED_MASK |
+                            NRF_TWI_INT_ERROR_MASK);
 }
 
 uint32_t i2c_irq_handler_asynch(i2c_t *obj)
 {
     twi_info_t *twi_info = TWI_INFO(obj);
-    return (twi_info->events & twi_info->event_mask);
+    return (twi_info->events & twi_info->evt_mask);
 }
 
 uint8_t i2c_active(i2c_t *obj)
 {
-    nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)];
-    return nrf_drv_twi_is_busy(twi);
+    twi_info_t *twi_info = TWI_INFO(obj);
+    return twi_info->active;
 }
 
 void i2c_abort_asynch(i2c_t *obj)
 {
     i2c_reset(obj);
 }
-
 #endif // DEVICE_I2C_ASYNCH
 
 #endif // DEVICE_I2C
--- a/targets/TARGET_NORDIC/TARGET_NRF5/sdk/drivers_nrf/pstorage/config/pstorage_platform.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NORDIC/TARGET_NRF5/sdk/drivers_nrf/pstorage/config/pstorage_platform.h	Tue Mar 14 16:40:56 2017 +0000
@@ -67,7 +67,10 @@
 
 #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) \
--- a/targets/TARGET_NORDIC/TARGET_NRF5/sdk/drivers_nrf/twi_master/nrf_drv_twi.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1005 +0,0 @@
-/* 
- * Copyright (c) 2015 Nordic Semiconductor ASA
- * 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, except as embedded into a Nordic Semiconductor ASA 
- *      integrated circuit in a product or a software update for such product, 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 Nordic Semiconductor ASA nor the names of its contributors may be 
- *      used to endorse or promote products derived from this software without specific prior 
- *      written permission.
- *
- *   4. This software, with or without modification, must only be used with a 
- *      Nordic Semiconductor ASA integrated circuit.
- *
- *   5. Any software provided in binary or object form under this license must not be reverse 
- *      engineered, decompiled, modified and/or disassembled. 
- * 
- * 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 "nrf_drv_twi.h"
-#include "nrf_drv_common.h"
-#include "nrf_gpio.h"
-#include "nrf_assert.h"
-#include "app_util_platform.h"
-#include "nrf_delay.h"
-
-#include <stdio.h>
-
-#define TWI0_IRQ_HANDLER    SPI0_TWI0_IRQHandler
-#define TWI1_IRQ_HANDLER    SPI1_TWI1_IRQHandler
-
-#if (defined(TWIM_IN_USE) && defined(TWI_IN_USE))
-    // TWIM and TWI combined
-    #define CODE_FOR_TWIM(code) if (p_instance->use_easy_dma) { code }
-    #define CODE_FOR_TWI(code)  else { code }
-#elif (defined(TWIM_IN_USE) && !defined(TWI_IN_USE))
-    // TWIM only
-    #define CODE_FOR_TWIM(code) { code }
-    #define CODE_FOR_TWI(code)
-#elif (!defined(TWIM_IN_USE) && defined(TWI_IN_USE))
-    // TWI only
-    #define CODE_FOR_TWIM(code)
-    #define CODE_FOR_TWI(code)  { code }
-#else
-    #error "Wrong configuration."
-#endif
-
-// All interrupt flags
-#define DISABLE_ALL  0xFFFFFFFF
-
-#define SCL_PIN_CONF        ((GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)    \
-                            | (GPIO_PIN_CNF_DRIVE_S0D1     << GPIO_PIN_CNF_DRIVE_Pos)   \
-                            | (GPIO_PIN_CNF_PULL_Pullup    << GPIO_PIN_CNF_PULL_Pos)    \
-                            | (GPIO_PIN_CNF_INPUT_Connect  << GPIO_PIN_CNF_INPUT_Pos)   \
-                            | (GPIO_PIN_CNF_DIR_Input      << GPIO_PIN_CNF_DIR_Pos))
-
-#define SDA_PIN_CONF        SCL_PIN_CONF
-
-#define SCL_PIN_CONF_CLR    ((GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)    \
-                            | (GPIO_PIN_CNF_DRIVE_S0D1     << GPIO_PIN_CNF_DRIVE_Pos)   \
-                            | (GPIO_PIN_CNF_PULL_Pullup    << GPIO_PIN_CNF_PULL_Pos)    \
-                            | (GPIO_PIN_CNF_INPUT_Connect  << GPIO_PIN_CNF_INPUT_Pos)   \
-                            | (GPIO_PIN_CNF_DIR_Output     << GPIO_PIN_CNF_DIR_Pos))
-
-#define SDA_PIN_CONF_CLR    SCL_PIN_CONF_CLR
-
-// Control block - driver instance local data.
-typedef struct
-{
-    nrf_drv_twi_evt_handler_t handler;
-    void *                    p_context;
-    volatile uint32_t         int_mask;
-    nrf_drv_twi_xfer_desc_t   xfer_desc;
-    uint32_t                  flags;
-    uint8_t *                 p_curr_buf;
-    uint8_t                   curr_length;
-    bool                      curr_no_stop;
-    nrf_drv_state_t           state;
-    bool                      error;
-    volatile bool             busy;
-    bool                      repeated;
-    uint8_t                   bytes_transferred;
-} twi_control_block_t;
-
-static twi_control_block_t m_cb[TWI_COUNT];
-
-static nrf_drv_twi_config_t const m_default_config[TWI_COUNT] = {
-#if TWI0_ENABLED
-    NRF_DRV_TWI_DEFAULT_CONFIG(0),
-#endif
-#if TWI1_ENABLED
-    NRF_DRV_TWI_DEFAULT_CONFIG(1),
-#endif
-};
-
-#if PERIPHERAL_RESOURCE_SHARING_ENABLED
-    #define IRQ_HANDLER_NAME(n) irq_handler_for_instance_##n
-    #define IRQ_HANDLER(n)      static void IRQ_HANDLER_NAME(n)(void)
-
-    #if TWI0_ENABLED
-        IRQ_HANDLER(0);
-    #endif
-    #if TWI1_ENABLED
-        IRQ_HANDLER(1);
-    #endif
-    static nrf_drv_irq_handler_t const m_irq_handlers[TWI_COUNT] = {
-    #if TWI0_ENABLED
-        IRQ_HANDLER_NAME(0),
-    #endif
-    #if TWI1_ENABLED
-        IRQ_HANDLER_NAME(1),
-    #endif
-    };
-#else
-    #define IRQ_HANDLER(n) void SPI##n##_TWI##n##_IRQHandler(void)
-#endif // PERIPHERAL_RESOURCE_SHARING_ENABLED
-
-static void twi_clear_bus(nrf_drv_twi_t const * const p_instance,
-                          nrf_drv_twi_config_t const * p_config)
-{
-    NRF_GPIO->PIN_CNF[p_config->scl] = SCL_PIN_CONF;
-    NRF_GPIO->PIN_CNF[p_config->sda] = SDA_PIN_CONF;
-
-    nrf_gpio_pin_set(p_config->scl);
-    nrf_gpio_pin_set(p_config->sda);
-
-    NRF_GPIO->PIN_CNF[p_config->scl] = SCL_PIN_CONF_CLR;
-    NRF_GPIO->PIN_CNF[p_config->sda] = SDA_PIN_CONF_CLR;
-
-    nrf_delay_us(4);
-
-    for(int i = 0; i < 9; i++)
-    {
-        if (nrf_gpio_pin_read(p_config->sda))
-        {
-            if(i == 0)
-            {
-                return;
-            }
-            else
-            {
-                break;
-            }
-        }
-        nrf_gpio_pin_clear(p_config->scl);
-        nrf_delay_us(4);
-        nrf_gpio_pin_set(p_config->scl);
-        nrf_delay_us(4);
-    }
-    nrf_gpio_pin_clear(p_config->sda);
-    nrf_delay_us(4);
-    nrf_gpio_pin_set(p_config->sda);
-}
-
-ret_code_t nrf_drv_twi_init(nrf_drv_twi_t const *        p_instance,
-                            nrf_drv_twi_config_t const * p_config,
-                            nrf_drv_twi_evt_handler_t    event_handler,
-                            void *                       p_context)
-{
-    twi_control_block_t * p_cb  = &m_cb[p_instance->drv_inst_idx];
-
-    if (p_cb->state != NRF_DRV_STATE_UNINITIALIZED)
-    {
-        return NRF_ERROR_INVALID_STATE;
-    }
-
-    if (p_config == NULL)
-    {
-        p_config = &m_default_config[p_instance->drv_inst_idx];
-    }
-
-#if PERIPHERAL_RESOURCE_SHARING_ENABLED
-    if (nrf_drv_common_per_res_acquire(p_instance->reg.p_twi,
-            m_irq_handlers[p_instance->drv_inst_idx]) != NRF_SUCCESS)
-    {
-        return NRF_ERROR_BUSY;
-    }
-#endif // PERIPHERAL_RESOURCE_SHARING_ENABLED
-
-    p_cb->handler   = event_handler;
-    p_cb->p_context = p_context;
-    p_cb->int_mask  = 0;
-    p_cb->repeated  = false;
-    p_cb->busy      = false;
-
-    twi_clear_bus(p_instance, p_config);
-
-    /* To secure correct signal levels on the pins used by the TWI
-       master when the system is in OFF mode, and when the TWI master is
-       disabled, these pins must be configured in the GPIO peripheral.
-    */
-    NRF_GPIO->PIN_CNF[p_config->scl] = SCL_PIN_CONF;
-    NRF_GPIO->PIN_CNF[p_config->sda] = SDA_PIN_CONF;
-
-    CODE_FOR_TWIM
-    (
-        NRF_TWIM_Type * p_twim = p_instance->reg.p_twim;
-        nrf_twim_pins_set(p_twim, p_config->scl, p_config->sda);
-        nrf_twim_frequency_set(p_twim,
-            (nrf_twim_frequency_t)p_config->frequency);
-    )
-    CODE_FOR_TWI
-    (
-        NRF_TWI_Type * p_twi = p_instance->reg.p_twi;
-        nrf_twi_pins_set(p_twi, p_config->scl, p_config->sda);
-        nrf_twi_frequency_set(p_twi,
-            (nrf_twi_frequency_t)p_config->frequency);
-    )
-
-    if (p_cb->handler)
-    {
-        CODE_FOR_TWIM
-        (
-            nrf_drv_common_irq_enable(nrf_drv_get_IRQn((void *)p_instance->reg.p_twim),
-                p_config->interrupt_priority);
-        )
-        CODE_FOR_TWI
-        (
-            nrf_drv_common_irq_enable(nrf_drv_get_IRQn((void *)p_instance->reg.p_twi),
-                p_config->interrupt_priority);
-        )
-    }
-
-    p_cb->state = NRF_DRV_STATE_INITIALIZED;
-
-    return NRF_SUCCESS;
-}
-
-void nrf_drv_twi_uninit(nrf_drv_twi_t const * p_instance)
-{
-    twi_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
-    ASSERT(p_cb->state != NRF_DRV_STATE_UNINITIALIZED);
-
-    if (p_cb->handler)
-    {
-        CODE_FOR_TWIM
-        (
-            nrf_drv_common_irq_disable(nrf_drv_get_IRQn((void *)p_instance->reg.p_twim));
-        )
-        CODE_FOR_TWI
-        (
-            nrf_drv_common_irq_disable(nrf_drv_get_IRQn((void *)p_instance->reg.p_twi));
-        )
-    }
-    nrf_drv_twi_disable(p_instance);
-
-#if PERIPHERAL_RESOURCE_SHARING_ENABLED
-    nrf_drv_common_per_res_release(p_instance->reg.p_twi);
-#endif
-
-    p_cb->state = NRF_DRV_STATE_UNINITIALIZED;
-}
-
-void nrf_drv_twi_enable(nrf_drv_twi_t const * p_instance)
-{
-    twi_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
-    ASSERT(p_cb->state == NRF_DRV_STATE_INITIALIZED);
-
-    CODE_FOR_TWIM
-    (
-        NRF_TWIM_Type * p_twim = p_instance->reg.p_twim;
-
-        nrf_twim_enable(p_twim);
-    )
-    CODE_FOR_TWI
-    (
-        NRF_TWI_Type * p_twi = p_instance->reg.p_twi;
-
-        nrf_twi_enable(p_twi);
-    )
-
-    p_cb->state = NRF_DRV_STATE_POWERED_ON;
-}
-
-void nrf_drv_twi_disable(nrf_drv_twi_t const * p_instance)
-{
-    twi_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
-    ASSERT(p_cb->state != NRF_DRV_STATE_UNINITIALIZED);
-
-    CODE_FOR_TWIM
-    (
-        NRF_TWIM_Type * p_twim = p_instance->reg.p_twim;
-        p_cb->int_mask = 0;
-        nrf_twim_int_disable(p_twim, DISABLE_ALL);
-        nrf_twim_shorts_disable(p_twim, DISABLE_ALL);
-        nrf_twim_disable(p_twim);
-    )
-    CODE_FOR_TWI
-    (
-        NRF_TWI_Type * p_twi = p_instance->reg.p_twi;
-        nrf_twi_int_disable(p_twi, DISABLE_ALL);
-        nrf_twi_shorts_disable(p_twi, DISABLE_ALL);
-        nrf_twi_disable(p_twi);
-    )
-
-    p_cb->state = NRF_DRV_STATE_INITIALIZED;
-}
-
-#ifdef TWI_IN_USE
-static bool twi_send_byte(NRF_TWI_Type  * p_twi,
-                          uint8_t const * p_data,
-                          uint8_t         length,
-                          uint8_t       * p_bytes_transferred,
-                          bool            no_stop)
-{
-    if (*p_bytes_transferred < length)
-    {
-        nrf_twi_txd_set(p_twi, p_data[*p_bytes_transferred]);
-        ++(*p_bytes_transferred);
-    }
-    else
-    {
-        if (no_stop)
-        {
-            nrf_twi_task_trigger(p_twi, NRF_TWI_TASK_SUSPEND);
-            return false;
-        }
-        else
-        {
-            nrf_twi_task_trigger(p_twi, NRF_TWI_TASK_STOP);
-        }
-    }
-    return true;
-}
-
-static void twi_receive_byte(NRF_TWI_Type * p_twi,
-                             uint8_t      * p_data,
-                             uint8_t        length,
-                             uint8_t      * p_bytes_transferred)
-{
-    if (*p_bytes_transferred < length)
-    {
-        p_data[*p_bytes_transferred] = nrf_twi_rxd_get(p_twi);
-
-        ++(*p_bytes_transferred);
-
-        if (*p_bytes_transferred == length-1)
-        {
-            nrf_twi_shorts_set(p_twi, NRF_TWI_SHORT_BB_STOP_MASK);
-        }
-        else if (*p_bytes_transferred == length)
-        {
-            return;
-        }
-
-        nrf_twi_task_trigger(p_twi, NRF_TWI_TASK_RESUME);
-    }
-}
-
-static bool twi_transfer(NRF_TWI_Type  * p_twi,
-                         bool          * p_error,
-                         uint8_t       * p_bytes_transferred,
-                         uint8_t       * p_data,
-                         uint8_t         length,
-                         bool            no_stop)
-{
-    bool do_stop_check;
-
-    if ((*p_error == true) || (*p_bytes_transferred == length))
-    {
-        do_stop_check = true;
-    }
-    else
-    {
-        do_stop_check = false;
-    }
-
-    if (*p_error)
-    {
-        nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_ERROR);
-        nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_TXDSENT);
-        nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_RXDREADY);
-    }
-    else if (nrf_twi_event_check(p_twi, NRF_TWI_EVENT_ERROR))
-    {
-        nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_ERROR);
-        nrf_twi_task_trigger(p_twi, NRF_TWI_TASK_STOP);
-        *p_error = true;
-    }
-    else
-    {
-        if (nrf_twi_event_check(p_twi, NRF_TWI_EVENT_TXDSENT))
-        {
-            nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_TXDSENT);
-            if (nrf_twi_event_check(p_twi, NRF_TWI_EVENT_ERROR))
-            {
-                nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_ERROR);
-                nrf_twi_task_trigger(p_twi, NRF_TWI_TASK_STOP);
-                *p_error = true;
-            }
-            else
-            {
-                if (!twi_send_byte(p_twi, p_data, length, p_bytes_transferred, no_stop))
-                {
-                    return false;
-                }
-            }
-        }
-        else if (nrf_twi_event_check(p_twi, NRF_TWI_EVENT_RXDREADY))
-        {
-            nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_RXDREADY);
-            if (nrf_twi_event_check(p_twi, NRF_TWI_EVENT_ERROR))
-            {
-                nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_ERROR);
-                nrf_twi_task_trigger(p_twi, NRF_TWI_TASK_STOP);
-                *p_error = true;
-            }
-            else
-            {
-                twi_receive_byte(p_twi, p_data, length, p_bytes_transferred);
-            }
-        }
-    }
-
-    if (do_stop_check && nrf_twi_event_check(p_twi, NRF_TWI_EVENT_STOPPED))
-    {
-        nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_STOPPED);
-        return false;
-    }
-
-    return true;
-}
-
-static ret_code_t twi_tx_start_transfer(twi_control_block_t * p_cb,
-                                        NRF_TWI_Type *        p_twi,
-                                        uint8_t const *       p_data,
-                                        uint8_t               length,
-                                        bool                  no_stop)
-{
-    ret_code_t ret_code = NRF_SUCCESS;
-
-    nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_STOPPED);
-    nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_ERROR);
-    nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_TXDSENT);
-    nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_RXDREADY);
-    nrf_twi_shorts_set(p_twi, 0);
-
-    p_cb->bytes_transferred = 0;
-    p_cb->error             = false;
-
-    // In case TWI is suspended resume its operation.
-    nrf_twi_task_trigger(p_twi, NRF_TWI_TASK_RESUME);
-    nrf_twi_task_trigger(p_twi, NRF_TWI_TASK_STARTTX);
-
-    (void)twi_send_byte(p_twi, p_data, length, &p_cb->bytes_transferred, no_stop);
-
-    if (p_cb->handler)
-    {
-        p_cb->int_mask = NRF_TWI_INT_STOPPED_MASK   |
-                        NRF_TWI_INT_ERROR_MASK     |
-                        NRF_TWI_INT_TXDSENT_MASK   |
-                        NRF_TWI_INT_RXDREADY_MASK;
-        nrf_twi_int_enable(p_twi, p_cb->int_mask);
-    }
-    else
-    {
-        while (twi_transfer(p_twi, &p_cb->error, &p_cb->bytes_transferred, (uint8_t *)p_data, length, no_stop))
-        {}
-
-        if (p_cb->error)
-        {
-            ret_code = NRF_ERROR_INTERNAL;
-        }
-    }
-    return ret_code;
-}
-
-static ret_code_t twi_rx_start_transfer(twi_control_block_t * p_cb,
-                                        NRF_TWI_Type *        p_twi,
-                                        uint8_t const *       p_data,
-                                        uint8_t               length)
-{
-    ret_code_t ret_code = NRF_SUCCESS;
-
-    nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_STOPPED);
-    nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_ERROR);
-    nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_TXDSENT);
-    nrf_twi_event_clear(p_twi, NRF_TWI_EVENT_RXDREADY);
-
-    p_cb->bytes_transferred = 0;
-    p_cb->error             = false;
-
-    if (length == 1)
-    {
-        nrf_twi_shorts_set(p_twi, NRF_TWI_SHORT_BB_STOP_MASK);
-    }
-    else
-    {
-        nrf_twi_shorts_set(p_twi, NRF_TWI_SHORT_BB_SUSPEND_MASK);
-    }
-    // In case TWI is suspended resume its operation.
-    nrf_twi_task_trigger(p_twi, NRF_TWI_TASK_RESUME);
-    nrf_twi_task_trigger(p_twi, NRF_TWI_TASK_STARTRX);
-
-    if (p_cb->handler)
-    {
-        p_cb->int_mask = NRF_TWI_INT_STOPPED_MASK   |
-                        NRF_TWI_INT_ERROR_MASK     |
-                        NRF_TWI_INT_TXDSENT_MASK   |
-                        NRF_TWI_INT_RXDREADY_MASK;
-        nrf_twi_int_enable(p_twi, p_cb->int_mask);
-    }
-    else
-    {
-        while (twi_transfer(p_twi, &p_cb->error, &p_cb->bytes_transferred, (uint8_t*)p_data, length, false))
-        {}
-
-        if (p_cb->error)
-        {
-            ret_code = NRF_ERROR_INTERNAL;
-        }
-    }
-    return ret_code;
-}
-
-__STATIC_INLINE ret_code_t twi_xfer(twi_control_block_t           * p_cb,
-                                    NRF_TWI_Type                  * p_twi,
-                                    nrf_drv_twi_xfer_desc_t const * p_xfer_desc,
-                                    uint32_t                        flags)
-{
-    ret_code_t ret = NRF_SUCCESS;
-
-    /* Block TWI interrupts to ensure that function is not interrupted by TWI interrupt. */
-    nrf_twi_int_disable(p_twi, DISABLE_ALL);
-
-    if (p_cb->busy)
-    {
-        nrf_twi_int_enable(p_twi, p_cb->int_mask);
-        return NRF_ERROR_BUSY;
-    }
-    else
-    {
-        p_cb->busy = (NRF_DRV_TWI_FLAG_NO_XFER_EVT_HANDLER & flags) ? false : true;
-    }
-
-    if (flags & NRF_DRV_TWI_FLAG_HOLD_XFER)
-    {
-        return NRF_ERROR_NOT_SUPPORTED;
-    }
-
-    p_cb->flags       = flags;
-    p_cb->xfer_desc   = *p_xfer_desc;
-    p_cb->curr_length = p_xfer_desc->primary_length;
-    p_cb->p_curr_buf  = p_xfer_desc->p_primary_buf;
-    nrf_twi_address_set(p_twi, p_xfer_desc->address);
-
-    if (p_xfer_desc->type != NRF_DRV_TWI_XFER_RX)
-    {
-        p_cb->curr_no_stop = ((p_xfer_desc->type == NRF_DRV_TWI_XFER_TX) &&
-                             !(flags & NRF_DRV_TWI_FLAG_TX_NO_STOP)) ? false : true;
-        ret = twi_tx_start_transfer(p_cb, p_twi, p_xfer_desc->p_primary_buf, p_xfer_desc->primary_length, p_cb->curr_no_stop);
-    }
-    else
-    {
-        p_cb->curr_no_stop = false;
-        ret = twi_rx_start_transfer(p_cb, p_twi, p_xfer_desc->p_primary_buf, p_xfer_desc->primary_length);
-    }
-    if (p_cb->handler == NULL)
-    {
-        p_cb->busy = false;
-    }
-    return ret;
-}
-#endif
-
-#ifdef TWIM_IN_USE
-__STATIC_INLINE void twim_list_enable_handle(NRF_TWIM_Type * p_twim, uint32_t flags)
-{
-    if (NRF_DRV_TWI_FLAG_TX_POSTINC & flags)
-    {
-        nrf_twim_tx_list_enable(p_twim);
-    }
-    else
-    {
-        nrf_twim_tx_list_disable(p_twim);
-    }
-
-    if (NRF_DRV_TWI_FLAG_RX_POSTINC & flags)
-    {
-        nrf_twim_rx_list_enable(p_twim);
-    }
-    else
-    {
-        nrf_twim_rx_list_disable(p_twim);
-    }
-#ifndef NRF52_PAN_46
-#endif
-}
-__STATIC_INLINE ret_code_t twim_xfer(twi_control_block_t           * p_cb,
-                                     NRF_TWIM_Type                 * p_twim,
-                                     nrf_drv_twi_xfer_desc_t const * p_xfer_desc,
-                                     uint32_t                        flags)
-{
-    ret_code_t ret = NRF_SUCCESS;
-    nrf_twim_task_t  start_task = NRF_TWIM_TASK_STARTTX;
-    nrf_twim_event_t evt_to_wait = NRF_TWIM_EVENT_STOPPED;
-
-    if (!nrf_drv_is_in_RAM(p_xfer_desc->p_primary_buf))
-    {
-        return NRF_ERROR_INVALID_ADDR;
-    }
-    /* Block TWI interrupts to ensure that function is not interrupted by TWI interrupt. */
-    nrf_twim_int_disable(p_twim, DISABLE_ALL);
-    if (p_cb->busy)
-    {
-        nrf_twim_int_enable(p_twim, p_cb->int_mask);
-        return NRF_ERROR_BUSY;
-    }
-    else
-    {
-
-        p_cb->busy = ((NRF_DRV_TWI_FLAG_NO_XFER_EVT_HANDLER & flags) ||
-                      (NRF_DRV_TWI_FLAG_REPEATED_XFER & flags)) ? false: true;
-    }
-
-    p_cb->xfer_desc = *p_xfer_desc;
-    p_cb->repeated = (flags & NRF_DRV_TWI_FLAG_REPEATED_XFER) ? true : false;
-    nrf_twim_address_set(p_twim, p_xfer_desc->address);
-
-    nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_STOPPED);
-    nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_ERROR);
-
-    twim_list_enable_handle(p_twim, flags);
-    switch (p_xfer_desc->type)
-    {
-    case NRF_DRV_TWI_XFER_TXTX:
-        ASSERT(!(flags & NRF_DRV_TWI_FLAG_REPEATED_XFER));
-        ASSERT(!(flags & NRF_DRV_TWI_FLAG_HOLD_XFER));
-        ASSERT(!(flags & NRF_DRV_TWI_FLAG_NO_XFER_EVT_HANDLER));
-        if (!nrf_drv_is_in_RAM(p_xfer_desc->p_secondary_buf))
-        {
-            return NRF_ERROR_INVALID_ADDR;
-        }
-        nrf_twim_shorts_set(p_twim, NRF_TWIM_SHORT_LASTTX_SUSPEND_MASK);
-        nrf_twim_tx_buffer_set(p_twim, p_xfer_desc->p_primary_buf, p_xfer_desc->primary_length);
-        nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_TXSTARTED);
-        nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_LASTTX);
-        nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_SUSPENDED);
-        nrf_twim_task_trigger(p_twim, NRF_TWIM_TASK_RESUME);
-        nrf_twim_task_trigger(p_twim, NRF_TWIM_TASK_STARTTX);
-        while(!nrf_twim_event_check(p_twim, NRF_TWIM_EVENT_TXSTARTED))
-        {}
-        nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_TXSTARTED);
-        nrf_twim_tx_buffer_set(p_twim, p_xfer_desc->p_secondary_buf, p_xfer_desc->secondary_length);
-        p_cb->int_mask = NRF_TWIM_INT_SUSPENDED_MASK | NRF_TWIM_INT_ERROR_MASK;
-        break;
-    case NRF_DRV_TWI_XFER_TXRX:
-        nrf_twim_tx_buffer_set(p_twim, p_xfer_desc->p_primary_buf, p_xfer_desc->primary_length);
-        nrf_twim_rx_buffer_set(p_twim, p_xfer_desc->p_secondary_buf, p_xfer_desc->secondary_length);
-        nrf_twim_shorts_set(p_twim, NRF_TWIM_SHORT_LASTTX_STARTRX_MASK |
-                                    NRF_TWIM_SHORT_LASTRX_STOP_MASK);
-        p_cb->int_mask = NRF_TWIM_INT_STOPPED_MASK | NRF_TWIM_INT_ERROR_MASK;
-        break;
-    case NRF_DRV_TWI_XFER_TX:
-        nrf_twim_tx_buffer_set(p_twim, p_xfer_desc->p_primary_buf, p_xfer_desc->primary_length);
-        if (NRF_DRV_TWI_FLAG_TX_NO_STOP & flags)
-        {
-            nrf_twim_shorts_set(p_twim, NRF_TWIM_SHORT_LASTTX_SUSPEND_MASK);
-            p_cb->int_mask = NRF_TWIM_INT_SUSPENDED_MASK | NRF_TWIM_INT_ERROR_MASK;
-            nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_SUSPENDED);
-            evt_to_wait = NRF_TWIM_EVENT_SUSPENDED;
-        }
-        else
-        {
-            nrf_twim_shorts_set(p_twim, NRF_TWIM_SHORT_LASTTX_STOP_MASK);
-            p_cb->int_mask = NRF_TWIM_INT_STOPPED_MASK | NRF_TWIM_INT_ERROR_MASK;
-        }
-        nrf_twim_task_trigger(p_twim, NRF_TWIM_TASK_RESUME);
-        break;
-    case NRF_DRV_TWI_XFER_RX:
-        nrf_twim_rx_buffer_set(p_twim, p_xfer_desc->p_primary_buf, p_xfer_desc->primary_length);
-        nrf_twim_shorts_set(p_twim, NRF_TWIM_SHORT_LASTRX_STOP_MASK);
-        p_cb->int_mask = NRF_TWIM_INT_STOPPED_MASK | NRF_TWIM_INT_ERROR_MASK;
-        start_task = NRF_TWIM_TASK_STARTRX;
-        nrf_twim_task_trigger(p_twim, NRF_TWIM_TASK_RESUME);
-        break;
-    default:
-        ret = NRF_ERROR_INVALID_PARAM;
-        break;
-    }
-
-    if (!(flags & NRF_DRV_TWI_FLAG_HOLD_XFER) && (p_xfer_desc->type != NRF_DRV_TWI_XFER_TXTX))
-    {
-        nrf_twim_task_trigger(p_twim, start_task);
-    }
-
-    if (p_cb->handler)
-    {
-        if (flags & NRF_DRV_TWI_FLAG_NO_XFER_EVT_HANDLER)
-        {
-            p_cb->int_mask = NRF_TWIM_INT_ERROR_MASK;
-        }
-        nrf_twim_int_enable(p_twim, p_cb->int_mask);
-    }
-    else
-    {
-        while (!nrf_twim_event_check(p_twim, evt_to_wait))
-        {
-            if (nrf_twim_event_check(p_twim, NRF_TWIM_EVENT_ERROR))
-            {
-                nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_ERROR);
-                nrf_twim_task_trigger(p_twim, NRF_TWIM_TASK_RESUME);
-                nrf_twim_task_trigger(p_twim, NRF_TWIM_TASK_STOP);
-                evt_to_wait = NRF_TWIM_EVENT_STOPPED;
-            }
-        }
-
-        uint32_t errorsrc =  nrf_twim_errorsrc_get_and_clear(p_twim);
-
-        p_cb->busy = false;
-
-        if (errorsrc)
-        {
-            ret = NRF_ERROR_INTERNAL;
-        }
-    }
-    return ret;
-}
-#endif
-
-ret_code_t nrf_drv_twi_xfer(nrf_drv_twi_t           const * p_instance,
-                            nrf_drv_twi_xfer_desc_t const * p_xfer_desc,
-                            uint32_t                        flags)
-{
-    ret_code_t ret = NRF_SUCCESS;
-    twi_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
-
-    // TXRX and TXTX transfers are support only in non-blocking mode.
-    ASSERT( !((p_cb->handler == NULL) && (p_xfer_desc->type == NRF_DRV_TWI_XFER_TXRX)));
-    ASSERT( !((p_cb->handler == NULL) && (p_xfer_desc->type == NRF_DRV_TWI_XFER_TXTX)));
-
-    CODE_FOR_TWIM
-    (
-        ret = twim_xfer(p_cb, (NRF_TWIM_Type *)p_instance->reg.p_twim, p_xfer_desc, flags);
-    )
-    CODE_FOR_TWI
-    (
-        if ( (NRF_DRV_TWI_FLAG_TX_POSTINC | NRF_DRV_TWI_FLAG_RX_POSTINC) & flags)
-        {
-            return NRF_ERROR_NOT_SUPPORTED;
-        }
-        ret = twi_xfer(p_cb, (NRF_TWI_Type  *)p_instance->reg.p_twi, p_xfer_desc, flags);
-    )
-    return ret;
-}
-
-bool nrf_drv_twi_is_busy(nrf_drv_twi_t const * p_instance)
-{
-    twi_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
-    return p_cb->busy;
-}
-
-ret_code_t nrf_drv_twi_tx(nrf_drv_twi_t const * p_instance,
-                          uint8_t               address,
-                          uint8_t const *       p_data,
-                          uint8_t               length,
-                          bool                  no_stop)
-{
-    nrf_drv_twi_xfer_desc_t xfer = NRF_DRV_TWI_XFER_DESC_TX(address, (uint8_t*)p_data, length);
-
-    return nrf_drv_twi_xfer(p_instance, &xfer, no_stop ? NRF_DRV_TWI_FLAG_TX_NO_STOP : 0);
-}
-
-ret_code_t nrf_drv_twi_rx(nrf_drv_twi_t const * p_instance,
-                          uint8_t               address,
-                          uint8_t *             p_data,
-                          uint8_t               length)
-{
-    nrf_drv_twi_xfer_desc_t xfer = NRF_DRV_TWI_XFER_DESC_RX(address, p_data, length);
-    return nrf_drv_twi_xfer(p_instance, &xfer, 0);
-}
-
-uint32_t nrf_drv_twi_data_count_get(nrf_drv_twi_t const * const p_instance)
-{
-    CODE_FOR_TWIM
-    (
-        ASSERT(false);
-        return 0;
-    )
-    CODE_FOR_TWI
-    (
-        return m_cb[p_instance->drv_inst_idx].bytes_transferred;
-    )
-}
-uint32_t nrf_drv_twi_start_task_get(nrf_drv_twi_t const * p_instance, nrf_drv_twi_xfer_type_t xfer_type)
-{
-    CODE_FOR_TWIM
-    (
-        return (uint32_t)nrf_twim_task_address_get(p_instance->reg.p_twim,
-            (xfer_type != NRF_DRV_TWI_XFER_RX) ? NRF_TWIM_TASK_STARTTX : NRF_TWIM_TASK_STARTRX);
-    )
-    CODE_FOR_TWI
-    (
-        return (uint32_t)nrf_twi_task_address_get(p_instance->reg.p_twi,
-                (xfer_type != NRF_DRV_TWI_XFER_RX) ? NRF_TWI_TASK_STARTTX : NRF_TWI_TASK_STARTRX);
-    )
-}
-
-uint32_t nrf_drv_twi_stopped_event_get(nrf_drv_twi_t const * p_instance)
-{
-    CODE_FOR_TWIM
-    (
-        return (uint32_t)nrf_twim_event_address_get(p_instance->reg.p_twim, NRF_TWIM_EVENT_STOPPED);
-    )
-    CODE_FOR_TWI
-    (
-        return (uint32_t)nrf_twi_event_address_get(p_instance->reg.p_twi, NRF_TWI_EVENT_STOPPED);
-    )
-}
-
-#ifdef TWIM_IN_USE
-static void irq_handler_twim(NRF_TWIM_Type * p_twim, twi_control_block_t * p_cb)
-{
-    ASSERT(p_cb->handler);
-
-    if (nrf_twim_event_check(p_twim, NRF_TWIM_EVENT_ERROR))
-    {
-        nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_ERROR);
-        if (!nrf_twim_event_check(p_twim, NRF_TWIM_EVENT_STOPPED))
-        {
-            nrf_twim_int_disable(p_twim, p_cb->int_mask);
-            p_cb->int_mask = NRF_TWIM_INT_STOPPED_MASK;
-            nrf_twim_int_enable(p_twim, p_cb->int_mask);
-
-            nrf_twim_task_trigger(p_twim, NRF_TWIM_TASK_RESUME);
-            nrf_twim_task_trigger(p_twim, NRF_TWIM_TASK_STOP);
-            return;
-        }
-    }
-
-    nrf_drv_twi_evt_t event;
-
-    if (nrf_twim_event_check(p_twim, NRF_TWIM_EVENT_STOPPED))
-    {
-        nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_STOPPED);
-        event.xfer_desc = p_cb->xfer_desc;
-        if (p_cb->error)
-        {
-
-            event.xfer_desc.primary_length = (p_cb->xfer_desc.type == NRF_DRV_TWI_XFER_RX) ?
-                (uint8_t)nrf_twim_rxd_amount_get(p_twim) : (uint8_t)nrf_twim_txd_amount_get(p_twim);
-            event.xfer_desc.secondary_length = (p_cb->xfer_desc.type == NRF_DRV_TWI_XFER_TXRX) ?
-                (uint8_t)nrf_twim_rxd_amount_get(p_twim) : (uint8_t)nrf_twim_txd_amount_get(p_twim);
-
-        }
-        nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_LASTTX);
-        nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_LASTRX);
-        if (!p_cb->repeated || p_cb->error)
-        {
-            nrf_twim_shorts_set(p_twim, 0);
-            p_cb->int_mask = 0;
-            nrf_twim_int_disable(p_twim, DISABLE_ALL);
-        }
-    }
-    else
-    {
-        nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_SUSPENDED);
-        if (p_cb->xfer_desc.type == NRF_DRV_TWI_XFER_TX)
-        {
-            event.xfer_desc = p_cb->xfer_desc;
-            if (!p_cb->repeated)
-            {
-                nrf_twim_shorts_set(p_twim, 0);
-                p_cb->int_mask = 0;
-                nrf_twim_int_disable(p_twim, DISABLE_ALL);
-            }
-        }
-        else
-        {
-            nrf_twim_shorts_set(p_twim, NRF_TWIM_SHORT_LASTTX_STOP_MASK);
-            p_cb->int_mask = NRF_TWIM_INT_STOPPED_MASK | NRF_TWIM_INT_ERROR_MASK;
-            nrf_twim_int_disable(p_twim, DISABLE_ALL);
-            nrf_twim_int_enable(p_twim, p_cb->int_mask);
-            nrf_twim_task_trigger(p_twim, NRF_TWIM_TASK_STARTTX);
-            nrf_twim_task_trigger(p_twim, NRF_TWIM_TASK_RESUME);
-            return;
-        }
-    }
-
-    uint32_t errorsrc = nrf_twim_errorsrc_get_and_clear(p_twim);
-    if (errorsrc & NRF_TWIM_ERROR_ADDRESS_NACK)
-    {
-        event.type = NRF_DRV_TWI_EVT_ADDRESS_NACK;
-    }
-    else if (errorsrc & NRF_TWIM_ERROR_DATA_NACK)
-    {
-        event.type = NRF_DRV_TWI_EVT_DATA_NACK;
-    }
-    else
-    {
-        event.type = NRF_DRV_TWI_EVT_DONE;
-    }
-
-    if (!p_cb->repeated)
-    {
-        p_cb->busy = false;
-    }
-    p_cb->handler(&event, p_cb->p_context);
-}
-#endif // TWIM_IN_USE
-
-#ifdef TWI_IN_USE
-static void irq_handler_twi(NRF_TWI_Type * p_twi, twi_control_block_t * p_cb)
-{
-    ASSERT(p_cb->handler);
-
-    if (twi_transfer(p_twi, &p_cb->error, &p_cb->bytes_transferred, p_cb->p_curr_buf, p_cb->curr_length, p_cb->curr_no_stop ))
-    {
-        return;
-    }
-
-    if (!p_cb->error &&
-        ((p_cb->xfer_desc.type == NRF_DRV_TWI_XFER_TXRX) ||
-         (p_cb->xfer_desc.type == NRF_DRV_TWI_XFER_TXTX)) &&
-        p_cb->p_curr_buf == p_cb->xfer_desc.p_primary_buf)
-    {
-        p_cb->p_curr_buf   = p_cb->xfer_desc.p_secondary_buf;
-        p_cb->curr_length  = p_cb->xfer_desc.secondary_length;
-        p_cb->curr_no_stop = (p_cb->flags & NRF_DRV_TWI_FLAG_TX_NO_STOP);
-
-        if (p_cb->xfer_desc.type == NRF_DRV_TWI_XFER_TXTX)
-        {
-            (void)twi_tx_start_transfer(p_cb, p_twi, p_cb->p_curr_buf, p_cb->curr_length, p_cb->curr_no_stop);
-        }
-        else
-        {
-            (void)twi_rx_start_transfer(p_cb, p_twi, p_cb->p_curr_buf, p_cb->curr_length);
-        }
-    }
-    else
-    {
-        nrf_drv_twi_evt_t event;
-        event.xfer_desc = p_cb->xfer_desc;
-
-        if (p_cb->error)
-        {
-            uint32_t errorsrc = nrf_twi_errorsrc_get_and_clear(p_twi);
-            if (errorsrc & NRF_TWI_ERROR_ADDRESS_NACK)
-            {
-                event.type = NRF_DRV_TWI_EVT_ADDRESS_NACK;
-            }
-            else if (errorsrc & NRF_TWI_ERROR_DATA_NACK)
-            {
-                event.type = NRF_DRV_TWI_EVT_DATA_NACK;
-            }
-        }
-        else
-        {
-            event.type = NRF_DRV_TWI_EVT_DONE;
-        }
-
-        p_cb->busy = false;
-
-        if (!(NRF_DRV_TWI_FLAG_NO_XFER_EVT_HANDLER & p_cb->flags))
-        {
-            p_cb->handler(&event, p_cb->p_context);
-        }
-    }
-
-}
-#endif // TWI_IN_USE
-
-#if TWI0_ENABLED
-IRQ_HANDLER(0)
-{
-    #if (TWI0_USE_EASY_DMA == 1) && defined(NRF52)
-        irq_handler_twim(NRF_TWIM0,
-    #else
-        irq_handler_twi(NRF_TWI0,
-    #endif
-            &m_cb[TWI0_INSTANCE_INDEX]);
-}
-#endif // TWI0_ENABLED
-
-#if TWI1_ENABLED
-IRQ_HANDLER(1)
-{
-    #if (TWI1_USE_EASY_DMA == 1)
-        irq_handler_twim(NRF_TWIM1,
-    #else
-        irq_handler_twi(NRF_TWI1,
-    #endif
-            &m_cb[TWI1_INSTANCE_INDEX]);
-}
-#endif // TWI1_ENABLED
--- a/targets/TARGET_NORDIC/TARGET_NRF5/sdk/drivers_nrf/twi_master/nrf_drv_twi.h	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,421 +0,0 @@
-/* 
- * Copyright (c) 2015 Nordic Semiconductor ASA
- * 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, except as embedded into a Nordic Semiconductor ASA 
- *      integrated circuit in a product or a software update for such product, 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 Nordic Semiconductor ASA nor the names of its contributors may be 
- *      used to endorse or promote products derived from this software without specific prior 
- *      written permission.
- *
- *   4. This software, with or without modification, must only be used with a 
- *      Nordic Semiconductor ASA integrated circuit.
- *
- *   5. Any software provided in binary or object form under this license must not be reverse 
- *      engineered, decompiled, modified and/or disassembled. 
- * 
- * 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.
- * 
- */
-
-
-/**
- *
- * @defgroup nrf_twi Two-wire interface (TWI)
- * @ingroup nrf_drivers
- * @brief Two-wire interface (TWI) APIs.
- *
- * @defgroup nrf_twi_master TWI master HAL and driver
- * @ingroup nrf_twi
- * @brief TWI master APIs.
- * @details The TWI and TWIM HALs provide basic APIs for accessing the registers of the TWI and TWIM peripherals, respectively. 
- *
- * The TWI master driver provides APIs on a higher level.
- *
- */
-
-#ifndef NRF_DRV_TWI_H__
-#define NRF_DRV_TWI_H__
-
-#include "nordic_common.h"
-#include "nrf_drv_config.h"
-
-// This set of macros makes it possible to exclude parts of code when one type
-// of supported peripherals is not used.
-#if ((TWI0_ENABLED == 1 && TWI0_USE_EASY_DMA == 1) || \
-     (TWI1_ENABLED == 1 && TWI1_USE_EASY_DMA == 1))
-    #define TWIM_IN_USE
-#endif
-#if ((TWI0_ENABLED == 1 && TWI0_USE_EASY_DMA != 1) || \
-     (TWI1_ENABLED == 1 && TWI1_USE_EASY_DMA != 1))
-    #define TWI_IN_USE
-#endif
-
-#include "nrf_twi.h"
-#ifdef TWIM_IN_USE
-    #include "nrf_twim.h"
-#endif
-#include "sdk_errors.h"
-
-#if defined(NRF52)
-    #define NRF_DRV_TWI_PERIPHERAL(id)           \
-        (CONCAT_3(TWI, id, _USE_EASY_DMA) == 1 ? \
-            (void *)CONCAT_2(NRF_TWIM, id)       \
-          : (void *)CONCAT_2(NRF_TWI, id))
-#else
-    #define NRF_DRV_TWI_PERIPHERAL(id)  (void *)CONCAT_2(NRF_TWI, id)
-#endif
-
-/**
- * @defgroup nrf_drv_twi TWI master driver
- * @{
- * @ingroup nrf_twi_master
- * @brief   Multi-instance TWI master driver.
- */
-
-/**
- * @brief Structure for the TWI master driver instance.
- */
-typedef struct
-{
-    union
-    {
-#ifdef TWIM_IN_USE
-        NRF_TWIM_Type * p_twim; ///< Pointer to a structure with TWIM registers.
-#endif
-        NRF_TWI_Type  * p_twi;  ///< Pointer to a structure with TWI registers.
-    } reg;
-    uint8_t drv_inst_idx; ///< Driver instance index.
-    bool    use_easy_dma; ///< True if the peripheral with EasyDMA (TWIM) shall be used.
-} nrf_drv_twi_t;
-
-/**
- * @brief Macro for creating a TWI master driver instance.
- */
-#define NRF_DRV_TWI_INSTANCE(id)                        \
-{                                                       \
-    .reg          = {NRF_DRV_TWI_PERIPHERAL(id)},       \
-    .drv_inst_idx = CONCAT_3(TWI, id, _INSTANCE_INDEX), \
-    .use_easy_dma = CONCAT_3(TWI, id, _USE_EASY_DMA)    \
-}
-
-/**
- * @brief Structure for the TWI master driver instance configuration.
- */
-typedef struct
-{
-    uint32_t            scl;                ///< SCL pin number.
-    uint32_t            sda;                ///< SDA pin number.
-    nrf_twi_frequency_t frequency;          ///< TWI frequency.
-    uint8_t             interrupt_priority; ///< Interrupt priority.
-} nrf_drv_twi_config_t;
-
-/**
- * @brief TWI master driver instance default configuration.
- */
-#define NRF_DRV_TWI_DEFAULT_CONFIG(id)                            \
-{                                                                 \
-    .frequency          = CONCAT_3(TWI, id, _CONFIG_FREQUENCY),   \
-    .scl                = CONCAT_3(TWI, id, _CONFIG_SCL),         \
-    .sda                = CONCAT_3(TWI, id, _CONFIG_SDA),         \
-    .interrupt_priority = CONCAT_3(TWI, id, _CONFIG_IRQ_PRIORITY) \
-}
-
-#define NRF_DRV_TWI_FLAG_TX_POSTINC          (1UL << 0) /**< TX buffer address incremented after transfer. */
-#define NRF_DRV_TWI_FLAG_RX_POSTINC          (1UL << 1) /**< RX buffer address incremented after transfer. */
-#define NRF_DRV_TWI_FLAG_NO_XFER_EVT_HANDLER (1UL << 2) /**< Interrupt after each transfer is suppressed, and the event handler is not called. */
-#define NRF_DRV_TWI_FLAG_HOLD_XFER           (1UL << 3) /**< Set up the transfer but do not start it. */
-#define NRF_DRV_TWI_FLAG_REPEATED_XFER       (1UL << 4) /**< Flag indicating that the transfer will be executed multiple times. */
-#define NRF_DRV_TWI_FLAG_TX_NO_STOP          (1UL << 5) /**< Flag indicating that the TX transfer will not end with a stop condition. */
-
-/**
- * @brief TWI master driver event types.
- */
-typedef enum
-{
-    NRF_DRV_TWI_EVT_DONE,         ///< Transfer completed event.
-    NRF_DRV_TWI_EVT_ADDRESS_NACK, ///< Error event: NACK received after sending the address.
-    NRF_DRV_TWI_EVT_DATA_NACK     ///< Error event: NACK received after sending a data byte.
-} nrf_drv_twi_evt_type_t;
-
-/**
- * @brief TWI master driver transfer types.
- */
-typedef enum
-{
-    NRF_DRV_TWI_XFER_TX,          ///< TX transfer.
-    NRF_DRV_TWI_XFER_RX,          ///< RX transfer.
-    NRF_DRV_TWI_XFER_TXRX,        ///< TX transfer followed by RX transfer with repeated start.
-    NRF_DRV_TWI_XFER_TXTX         ///< TX transfer followed by TX transfer with repeated start.
-} nrf_drv_twi_xfer_type_t;
-
-/**
- * @brief Structure for a TWI transfer descriptor.
- */
-typedef struct
-{
-    nrf_drv_twi_xfer_type_t type;             ///< Type of transfer.
-    uint8_t                 address;          ///< Slave address.
-    uint8_t                 primary_length;   ///< Number of bytes transferred.
-    uint8_t                 secondary_length; ///< Number of bytes transferred.
-    uint8_t *               p_primary_buf;    ///< Pointer to transferred data.
-    uint8_t *               p_secondary_buf;  ///< Pointer to transferred data.
-} nrf_drv_twi_xfer_desc_t;
-
-
-/**@brief Macro for setting the TX transfer descriptor. */
-#define NRF_DRV_TWI_XFER_DESC_TX(addr, p_data, length)                 \
-    {                                                                  \
-        .type = NRF_DRV_TWI_XFER_TX,                                   \
-        .address = addr,                                               \
-        .primary_length = length,                                      \
-        .p_primary_buf  = p_data,                                      \
-    }
-
-/**@brief Macro for setting the RX transfer descriptor. */
-#define NRF_DRV_TWI_XFER_DESC_RX(addr, p_data, length)                 \
-    {                                                                  \
-        .type = NRF_DRV_TWI_XFER_RX,                                   \
-        .address = addr,                                               \
-        .primary_length = length,                                      \
-        .p_primary_buf  = p_data,                                      \
-    }
-
-/**@brief Macro for setting the TXRX transfer descriptor. */
-#define NRF_DRV_TWI_XFER_DESC_TXRX(addr, p_tx, tx_len, p_rx, rx_len)   \
-    {                                                                  \
-        .type = NRF_DRV_TWI_XFER_TXRX,                                 \
-        .address = addr,                                               \
-        .primary_length   = tx_len,                                    \
-        .secondary_length = rx_len,                                    \
-        .p_primary_buf    = p_tx,                                      \
-        .p_secondary_buf  = p_rx,                                      \
-    }
-
-/**@brief Macro for setting the TXTX transfer descriptor. */
-#define NRF_DRV_TWI_XFER_DESC_TXTX(addr, p_tx, tx_len, p_tx2, tx_len2) \
-    {                                                                  \
-        .type = NRF_DRV_TWI_XFER_TXTX,                                 \
-        .address = addr,                                               \
-        .primary_length   = tx_len,                                    \
-        .secondary_length = tx_len2,                                   \
-        .p_primary_buf    = p_tx,                                      \
-        .p_secondary_buf  = p_tx2,                                     \
-    }
-
-/**
- * @brief Structure for a TWI event.
- */
-typedef struct
-{
-    nrf_drv_twi_evt_type_t  type;      ///< Event type.
-    nrf_drv_twi_xfer_desc_t xfer_desc; ///< Transfer details.
-} nrf_drv_twi_evt_t;
-
-/**
- * @brief TWI event handler prototype.
- */
-typedef void (* nrf_drv_twi_evt_handler_t)(nrf_drv_twi_evt_t const * p_event,
-                                           void *                    p_context);
-
-/**
- * @brief Function for initializing the TWI instance.
- *
- * @param[in] p_instance      TWI instance.
- * @param[in] p_config        Initial configuration. If NULL, the default configuration is used.
- * @param[in] event_handler   Event handler provided by the user. If NULL, blocking mode is enabled.
- * @param[in] p_context       Context passed to event handler.
- *
- * @retval NRF_SUCCESS             If initialization was successful.
- * @retval NRF_ERROR_INVALID_STATE If the driver is in invalid state.
- * @retval NRF_ERROR_BUSY          If some other peripheral with the same
- *                                 instance ID is already in use. This is 
- *                                 possible only if PERIPHERAL_RESOURCE_SHARING_ENABLED 
- *                                 is set to a value other than zero.
- */
-ret_code_t nrf_drv_twi_init(nrf_drv_twi_t const *        p_instance,
-                            nrf_drv_twi_config_t const * p_config,
-                            nrf_drv_twi_evt_handler_t    event_handler,
-                            void *                       p_context);
-
-/**
- * @brief Function for uninitializing the TWI instance.
- *
- * @param[in] p_instance  TWI instance.
- */
-void nrf_drv_twi_uninit(nrf_drv_twi_t const * p_instance);
-
-/**
- * @brief Function for enabling the TWI instance.
- *
- * @param[in] p_instance  TWI instance.
- */
-void nrf_drv_twi_enable(nrf_drv_twi_t const * p_instance);
-
-/**
- * @brief Function for disabling the TWI instance.
- *
- * @param[in] p_instance  TWI instance.
- */
-void nrf_drv_twi_disable(nrf_drv_twi_t const * p_instance);
-
-/**
- * @brief Function for sending data to a TWI slave.
- *
- * The transmission will be stopped when an error occurs. If a transfer is ongoing,
- * the function returns the error code @ref NRF_ERROR_BUSY.
- *
- * @param[in] p_instance TWI instance.
- * @param[in] address    Address of a specific slave device (only 7 LSB).
- * @param[in] p_data     Pointer to a transmit buffer.
- * @param[in] length     Number of bytes to send.
- * @param[in] no_stop    If set, the stop condition is not generated on the bus
- *                       after the transfer has completed successfully (allowing
- *                       for a repeated start in the next transfer).
- *
- * @retval NRF_SUCCESS        If the procedure was successful.
- * @retval NRF_ERROR_BUSY     If the driver is not ready for a new transfer.
- * @retval NRF_ERROR_INTERNAL If an error was detected by hardware.
- */
-ret_code_t nrf_drv_twi_tx(nrf_drv_twi_t const * p_instance,
-                          uint8_t               address,
-                          uint8_t const *       p_data,
-                          uint8_t               length,
-                          bool                  no_stop);
-
-/**
- * @brief Function for reading data from a TWI slave.
- *
- * The transmission will be stopped when an error occurs. If a transfer is ongoing,
- * the function returns the error code @ref NRF_ERROR_BUSY. 
- *
- * @param[in] p_instance TWI instance.
- * @param[in] address    Address of a specific slave device (only 7 LSB).
- * @param[in] p_data     Pointer to a receive buffer.
- * @param[in] length     Number of bytes to be received.
- *
- * @retval NRF_SUCCESS             If the procedure was successful.
- * @retval NRF_ERROR_BUSY          If the driver is not ready for a new transfer.
- * @retval NRF_ERROR_INTERNAL      If an error was detected by hardware.
- */
-ret_code_t nrf_drv_twi_rx(nrf_drv_twi_t const * p_instance,
-                          uint8_t               address,
-                          uint8_t *             p_data,
-                          uint8_t               length);
-
-/**
- * @brief Function for preparing a TWI transfer.
- *
- * The following transfer types can be configured (@ref nrf_drv_twi_xfer_desc_t::type):
- * - @ref NRF_DRV_TWI_XFER_TXRX<span></span>: Write operation followed by a read operation (without STOP condition in between).
- * - @ref NRF_DRV_TWI_XFER_TXTX<span></span>: Write operation followed by a write operation (without STOP condition in between).
- * - @ref NRF_DRV_TWI_XFER_TX<span></span>:   Write operation (with or without STOP condition).
- * - @ref NRF_DRV_TWI_XFER_RX<span></span>:   Read operation  (with STOP condition).
- *
- * Additional options are provided using the flags parameter:
- * - @ref NRF_DRV_TWI_FLAG_TX_POSTINC and @ref NRF_DRV_TWI_FLAG_RX_POSTINC<span></span>: Post-incrementation of buffer addresses. Supported only by TWIM.
- * - @ref NRF_DRV_TWI_FLAG_NO_XFER_EVT_HANDLER<span></span>: No user event handler after transfer completion. In most cases, this also means no interrupt at the end of the transfer.
- * - @ref NRF_DRV_TWI_FLAG_HOLD_XFER<span></span>: Driver is not starting the transfer. Use this flag if the transfer is triggered externally by PPI. Supported only by TWIM.
- *   Use @ref nrf_drv_twi_start_task_get to get the address of the start task.
- * - @ref NRF_DRV_TWI_FLAG_REPEATED_XFER<span></span>: Prepare for repeated transfers. You can set up a number of transfers that will be triggered externally (for example by PPI).
- *   An example is a TXRX transfer with the options @ref NRF_DRV_TWI_FLAG_RX_POSTINC, @ref NRF_DRV_TWI_FLAG_NO_XFER_EVT_HANDLER, and @ref NRF_DRV_TWI_FLAG_REPEATED_XFER.
- *   After the transfer is set up, a set of transfers can be triggered by PPI that will read, for example, the same register of an
- *   external component and put it into a RAM buffer without any interrupts. @ref nrf_drv_twi_stopped_event_get can be used to get the
- *   address of the STOPPED event, which can be used to count the number of transfers. If @ref NRF_DRV_TWI_FLAG_REPEATED_XFER is used,
- *   the driver does not set the instance into busy state, so you must ensure that the next transfers are set up
- *   when TWIM is not active. Supported only by TWIM.
- * - @ref NRF_DRV_TWI_FLAG_TX_NO_STOP<span></span>: No stop condition after TX transfer.
- *
- * @note
- * Some flag combinations are invalid:
- * - @ref NRF_DRV_TWI_FLAG_TX_NO_STOP with @ref nrf_drv_twi_xfer_desc_t::type different than @ref NRF_DRV_TWI_XFER_TX
- * - @ref NRF_DRV_TWI_FLAG_REPEATED_XFER with @ref nrf_drv_twi_xfer_desc_t::type set to @ref NRF_DRV_TWI_XFER_TXTX
- *
- * If @ref nrf_drv_twi_xfer_desc_t::type is set to @ref NRF_DRV_TWI_XFER_TX and the @ref NRF_DRV_TWI_FLAG_TX_NO_STOP and @ref NRF_DRV_TWI_FLAG_REPEATED_XFER
- * flags are set, two tasks must be used to trigger a transfer: TASKS_RESUME followed by TASKS_STARTTX. If no stop condition is generated,
- * TWIM is in SUSPENDED state. Therefore, it must be resumed before the transfer can be started.
- *
- * @note
- * This function should be used only if the instance is configured to work in non-blocking mode. If the function is used in blocking mode, the driver asserts.
- * @note If you are using this function with TWI, the only supported flag is @ref NRF_DRV_TWI_FLAG_TX_NO_STOP. All other flags require TWIM.
-  *
- * @param[in] p_instance        TWI instance.
- * @param[in] p_xfer_desc       Pointer to the transfer descriptor.
- * @param[in] flags             Transfer options (0 for default settings).
- *
- * @retval NRF_SUCCESS             If the procedure was successful.
- * @retval NRF_ERROR_BUSY          If the driver is not ready for a new transfer.
- * @retval NRF_ERROR_NOT_SUPPORTED If the provided parameters are not supported.
- */
-ret_code_t nrf_drv_twi_xfer(nrf_drv_twi_t           const * p_instance,
-                            nrf_drv_twi_xfer_desc_t const * p_xfer_desc,
-                            uint32_t                        flags);
-
-/**
- * @brief Function for checking the TWI driver state.
- *
- * @param[in] p_instance TWI instance.
- *
- * @retval true  If the TWI driver is currently busy performing a transfer.
- * @retval false If the TWI driver is ready for a new transfer.
- */
-bool nrf_drv_twi_is_busy(nrf_drv_twi_t const * p_instance);
-
-/**
- * @brief Function for getting the transferred data count.
- *
- * This function provides valid results only in legacy mode.
- *
- * @param[in] p_instance TWI instance.
- *
- * @return     Data count.
- */
-uint32_t nrf_drv_twi_data_count_get(nrf_drv_twi_t const * const p_instance);
-
-/**
- * @brief Function for returning the address of a TWI/TWIM start task.
- *
- * This function should be used if @ref nrf_drv_twi_xfer was called with the flag @ref NRF_DRV_TWI_FLAG_HOLD_XFER.
- * In that case, the transfer is not started by the driver, but it must be started externally by PPI.
- *
- * @param[in]  p_instance TWI instance.
- * @param[in]  xfer_type  Transfer type used in the last call of the @ref nrf_drv_twi_xfer function.
- *
- * @return     Start task address (TX or RX) depending on the value of xfer_type.
- */
-uint32_t nrf_drv_twi_start_task_get(nrf_drv_twi_t const * p_instance, nrf_drv_twi_xfer_type_t xfer_type);
-
-/**
- * @brief Function for returning the address of a STOPPED TWI/TWIM event.
- *
- * A STOPPED event can be used to detect the end of a transfer if the @ref NRF_DRV_TWI_FLAG_NO_XFER_EVT_HANDLER
- * option is used.
- *
- * @param[in]  p_instance  TWI instance.
- *
- * @return     STOPPED event address.
- */
-uint32_t nrf_drv_twi_stopped_event_get(nrf_drv_twi_t const * p_instance);
-/**
- *@}
- **/
-
-#endif // NRF_DRV_TWI_H__
--- a/targets/TARGET_NORDIC/TARGET_NRF5/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NORDIC/TARGET_NRF5/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -25,7 +25,7 @@
 
 #define FPU_EXCEPTION_MASK 0x0000009F
 
-void sleep(void)
+void hal_sleep(void)
 {
     // ensure debug is disconnected if semihost is enabled....
 
@@ -73,8 +73,8 @@
     }
 }
 
-void deepsleep(void)
+void hal_deepsleep(void)
 {
-    sleep();
+    hal_sleep();
     //   NRF_POWER->SYSTEMOFF=1;
 }
--- a/targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -90,22 +90,22 @@
 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,29 @@
               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 &&
+            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 +287,8 @@
             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'.
--- a/targets/TARGET_NORDIC/TARGET_NRF5/us_ticker.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NORDIC/TARGET_NRF5/us_ticker.c	Tue Mar 14 16:40:56 2017 +0000
@@ -233,7 +233,7 @@
 // alternative source of RTOS ticks.
 #if defined(TARGET_MCU_NRF51822)
 
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 
 
 #define MAX_RTC_COUNTER_VAL     ((1uL << RTC_COUNTER_BITS) - 1)
--- a/targets/TARGET_NUVOTON/TARGET_M451/can_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_M451/can_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -26,7 +26,7 @@
  #include "nu_modutil.h"
  #include "nu_miscutil.h"
  #include "nu_bitutil.h"
- #include "critical.h"
+ #include "mbed_critical.h"
  
  #define NU_CAN_DEBUG    0
  #define CAN_NUM         1
--- a/targets/TARGET_NUVOTON/TARGET_M451/i2c_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_M451/i2c_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -24,7 +24,7 @@
 #include "nu_modutil.h"
 #include "nu_miscutil.h"
 #include "nu_bitutil.h"
-#include "critical.h"
+#include "mbed_critical.h"
 
 #define NU_I2C_DEBUG    0
 
--- a/targets/TARGET_NUVOTON/TARGET_M451/lp_ticker.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_M451/lp_ticker.c	Tue Mar 14 16:40:56 2017 +0000
@@ -21,7 +21,7 @@
 #include "sleep_api.h"
 #include "nu_modutil.h"
 #include "nu_miscutil.h"
-#include "critical.h"
+#include "mbed_critical.h"
 
 // lp_ticker tick = us = timestamp
 #define US_PER_TICK             (1)
--- a/targets/TARGET_NUVOTON/TARGET_M451/rtc_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_M451/rtc_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -18,7 +18,7 @@
 
 #if DEVICE_RTC
 
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "mbed_error.h"
 #include "nu_modutil.h"
 #include "nu_miscutil.h"
--- a/targets/TARGET_NUVOTON/TARGET_M451/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_M451/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -36,7 +36,7 @@
 /**
  * Enter Idle mode.
  */
-void sleep(void)
+void hal_sleep(void)
 {
     struct sleep_s sleep_obj;
     sleep_obj.powerdown = 0;
@@ -47,7 +47,7 @@
 /**
  * Enter Power-down mode while no peripheral is active; otherwise, enter Idle mode.
  */
-void deepsleep(void)
+void hal_deepsleep(void)
 {
     struct sleep_s sleep_obj;
     sleep_obj.powerdown = 1;
--- a/targets/TARGET_NUVOTON/TARGET_M451/us_ticker.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_M451/us_ticker.c	Tue Mar 14 16:40:56 2017 +0000
@@ -19,7 +19,7 @@
 #include "mbed_assert.h"
 #include "nu_modutil.h"
 #include "nu_miscutil.h"
-#include "critical.h"
+#include "mbed_critical.h"
 
 // us_ticker tick = us = timestamp
 #define US_PER_TICK             1
--- a/targets/TARGET_NUVOTON/TARGET_NUC472/can_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/can_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -26,7 +26,7 @@
  #include "nu_modutil.h"
  #include "nu_miscutil.h"
  #include "nu_bitutil.h"
- #include "critical.h"
+ #include "mbed_critical.h"
  
  #define NU_CAN_DEBUG    0
  #define CAN_NUM         2
--- a/targets/TARGET_NUVOTON/TARGET_NUC472/crypto/aes/aes_alt.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/crypto/aes/aes_alt.c	Tue Mar 14 16:40:56 2017 +0000
@@ -38,7 +38,7 @@
 #include "mbedtls/aes.h"
 
 #include "NUC472_442.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 #include "mbed_assert.h"
 
 //static int aes_init_done = 0;
--- a/targets/TARGET_NUVOTON/TARGET_NUC472/crypto/des/des_alt.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/crypto/des/des_alt.c	Tue Mar 14 16:40:56 2017 +0000
@@ -31,7 +31,7 @@
 #include "des_alt.h"
 #include "crypto-misc.h"
 #include "nu_bitutil.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 
 // Must be a multiple of 64-bit block size
 #define MAXSIZE_DMABUF  (8 * 5)
--- a/targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_ARM_MICRO/NUC472.sct	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,35 +0,0 @@
-
-LR_IROM1 0x00000000 {
-  ER_IROM1 0x00000000 {  ; load address = execution address
-   *(RESET, +First)
-   *(InRoot$$Sections)
-   .ANY (+RO)
-  }
-  
-  ;UVISOR AlignExpr(+0, 16) {  ; 16 byte-aligned
-  ;  uvisor-lib.a (+RW +ZI)
-  ;}
-  
-  ARM_LIB_STACK 0x20000000 EMPTY 0x800 {
-  }
-  
-  ER_IRAMVEC 0x20000800 EMPTY (4*(16 + 142)) {  ; Reserve for vectors
-  }
-  
-  RW_IRAM1 AlignExpr(+0, 16) {  ; 16 byte-aligned
-   .ANY (+RW +ZI)
-  }
-  
-  ; Too large to place into internal SRAM. So place into external SRAM instead.
-  ER_XRAM1 0x60000000 {
-    *sal-stack-lwip* (+ZI)
-  }
-  
-  ; Extern SRAM for HEAP
-  ARM_LIB_HEAP AlignExpr(+0, 16) EMPTY (0x60000000 + 0x100000 - AlignExpr(ImageLimit(ER_XRAM1), 16)) {
-  }
-}
-ScatterAssert(LoadLimit(LR_IROM1) <= 0x00080000)    ; 512 KB APROM
-ScatterAssert(ImageLimit(RW_IRAM1) <= 0x20010000)   ; 64 KB SRAM (internal)
-ScatterAssert(ImageLimit(ARM_LIB_HEAP) <= 0x60100000)   ; 1 MB SRAM (external)
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_ARM_MICRO/TARGET_NU_XRAM_SUPPORTED/NUC472.sct	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,35 @@
+
+LR_IROM1 0x00000000 {
+  ER_IROM1 0x00000000 {  ; load address = execution address
+   *(RESET, +First)
+   *(InRoot$$Sections)
+   .ANY (+RO)
+  }
+  
+  ;UVISOR AlignExpr(+0, 16) {  ; 16 byte-aligned
+  ;  uvisor-lib.a (+RW +ZI)
+  ;}
+  
+  ARM_LIB_STACK 0x20000000 EMPTY 0x800 {
+  }
+  
+  ER_IRAMVEC 0x20000800 EMPTY (4*(16 + 142)) {  ; Reserve for vectors
+  }
+  
+  RW_IRAM1 AlignExpr(+0, 16) {  ; 16 byte-aligned
+   .ANY (+RW +ZI)
+  }
+  
+  ; Too large to place into internal SRAM. So place into external SRAM instead.
+  ER_XRAM1 0x60000000 {
+    *sal-stack-lwip* (+ZI)
+  }
+  
+  ; Extern SRAM for HEAP
+  ARM_LIB_HEAP AlignExpr(+0, 16) EMPTY (0x60000000 + 0x100000 - AlignExpr(ImageLimit(ER_XRAM1), 16)) {
+  }
+}
+ScatterAssert(LoadLimit(LR_IROM1) <= 0x00080000)    ; 512 KB APROM
+ScatterAssert(ImageLimit(RW_IRAM1) <= 0x20010000)   ; 64 KB SRAM (internal)
+ScatterAssert(ImageLimit(ARM_LIB_HEAP) <= 0x60100000)   ; 1 MB SRAM (external)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_ARM_MICRO/TARGET_NU_XRAM_UNSUPPORTED/NUC472.sct	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,29 @@
+
+LR_IROM1 0x00000000 {
+  ER_IROM1 0x00000000 {  ; load address = execution address
+   *(RESET, +First)
+   *(InRoot$$Sections)
+   .ANY (+RO)
+  }
+  
+  ;UVISOR AlignExpr(+0, 16) {  ; 16 byte-aligned
+  ;  uvisor-lib.a (+RW +ZI)
+  ;}
+  
+  ARM_LIB_STACK 0x20000000 EMPTY 0x800 {
+  }
+  
+  ER_IRAMVEC 0x20000800 EMPTY (4*(16 + 142)) {  ; Reserve for vectors
+  }
+  
+  RW_IRAM1 AlignExpr(+0, 16) {  ; 16 byte-aligned
+   .ANY (+RW +ZI)
+  }
+  
+  ; Extern SRAM for HEAP
+  ARM_LIB_HEAP AlignExpr(+0, 16) EMPTY (0x20000000 + 0x10000 - AlignExpr(ImageLimit(RW_IRAM1), 16)) {
+  }
+}
+ScatterAssert(LoadLimit(LR_IROM1) <= 0x00080000)    ; 512 KB APROM
+ScatterAssert(ImageLimit(RW_IRAM1) <= 0x20010000)   ; 64 KB SRAM (internal)
+
--- a/targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_ARM_STD/NUC472.sct	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-
-LR_IROM1 0x00000000 {
-  ER_IROM1 0x00000000 {  ; load address = execution address
-   *(RESET, +First)
-   *(InRoot$$Sections)
-   .ANY (+RO)
-  }
-  
-  ;UVISOR AlignExpr(+0, 16) {  ; 16 byte-aligned
-  ;  uvisor-lib.a (+RW +ZI)
-  ;}
-  
-  ARM_LIB_STACK 0x20000000 EMPTY 0x800 {
-  }
-  
-  ER_IRAMVEC 0x20000800 EMPTY (4*(16 + 142)) {  ; Reserve for vectors
-  }
-  
-  RW_IRAM1 AlignExpr(+0, 16) {  ; 16 byte-aligned
-   .ANY (+RW +ZI)
-  }
-  
-  ; Too large to place into internal SRAM. So place into external SRAM instead.
-  ER_XRAM1 0x60000000 {
-    *lwip_* (+ZI)
-    aes.o (+ZI)
-    mesh_system.o (+ZI)
-  }
-  
-  ; Extern SRAM for HEAP
-  ARM_LIB_HEAP AlignExpr(+0, 16) EMPTY (0x60000000 + 0x100000 - AlignExpr(ImageLimit(ER_XRAM1), 16)) {
-  }
-}
-ScatterAssert(LoadLimit(LR_IROM1) <= 0x00080000)    ; 512 KB APROM
-ScatterAssert(ImageLimit(RW_IRAM1) <= 0x20010000)   ; 64 KB SRAM (internal)
-ScatterAssert(ImageLimit(ARM_LIB_HEAP) <= 0x60100000)   ; 1 MB SRAM (external)
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_ARM_STD/TARGET_NU_XRAM_SUPPORTED/NUC472.sct	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,37 @@
+
+LR_IROM1 0x00000000 {
+  ER_IROM1 0x00000000 {  ; load address = execution address
+   *(RESET, +First)
+   *(InRoot$$Sections)
+   .ANY (+RO)
+  }
+  
+  ;UVISOR AlignExpr(+0, 16) {  ; 16 byte-aligned
+  ;  uvisor-lib.a (+RW +ZI)
+  ;}
+  
+  ARM_LIB_STACK 0x20000000 EMPTY 0x800 {
+  }
+  
+  ER_IRAMVEC 0x20000800 EMPTY (4*(16 + 142)) {  ; Reserve for vectors
+  }
+  
+  RW_IRAM1 AlignExpr(+0, 16) {  ; 16 byte-aligned
+   .ANY (+RW +ZI)
+  }
+  
+  ; Too large to place into internal SRAM. So place into external SRAM instead.
+  ER_XRAM1 0x60000000 {
+    *lwip_* (+ZI)
+    aes.o (+ZI)
+    mesh_system.o (+ZI)
+  }
+  
+  ; Extern SRAM for HEAP
+  ARM_LIB_HEAP AlignExpr(+0, 16) EMPTY (0x60000000 + 0x100000 - AlignExpr(ImageLimit(ER_XRAM1), 16)) {
+  }
+}
+ScatterAssert(LoadLimit(LR_IROM1) <= 0x00080000)    ; 512 KB APROM
+ScatterAssert(ImageLimit(RW_IRAM1) <= 0x20010000)   ; 64 KB SRAM (internal)
+ScatterAssert(ImageLimit(ARM_LIB_HEAP) <= 0x60100000)   ; 1 MB SRAM (external)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_ARM_STD/TARGET_NU_XRAM_UNSUPPORTED/NUC472.sct	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,29 @@
+
+LR_IROM1 0x00000000 {
+  ER_IROM1 0x00000000 {  ; load address = execution address
+   *(RESET, +First)
+   *(InRoot$$Sections)
+   .ANY (+RO)
+  }
+  
+  ;UVISOR AlignExpr(+0, 16) {  ; 16 byte-aligned
+  ;  uvisor-lib.a (+RW +ZI)
+  ;}
+  
+  ARM_LIB_STACK 0x20000000 EMPTY 0x800 {
+  }
+  
+  ER_IRAMVEC 0x20000800 EMPTY (4*(16 + 142)) {  ; Reserve for vectors
+  }
+  
+  RW_IRAM1 AlignExpr(+0, 16) {  ; 16 byte-aligned
+   .ANY (+RW +ZI)
+  }
+  
+  ; Extern SRAM for HEAP
+  ARM_LIB_HEAP AlignExpr(+0, 16) EMPTY (0x20000000 + 0x10000 - AlignExpr(ImageLimit(RW_IRAM1), 16)) {
+  }
+}
+ScatterAssert(LoadLimit(LR_IROM1) <= 0x00080000)    ; 512 KB APROM
+ScatterAssert(ImageLimit(RW_IRAM1) <= 0x20010000)   ; 64 KB SRAM (internal)
+
--- a/targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_GCC_ARM/NUC472.ld	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,269 +0,0 @@
-/*
- * Nuvoton NUC472 GCC linker script file
- */
-
-StackSize = 0x800;
-
-MEMORY
-{
-    
-  VECTORS (rx)          : ORIGIN = 0x00000000, LENGTH = 0x00000400
-  FLASH (rx)            : ORIGIN = 0x00000400, LENGTH = 0x00080000 - 0x00000400
-  RAM_INTERN (rwx)      : ORIGIN = 0x20000000, LENGTH = 0x00010000 - 0x00000000
-  RAM_EXTERN (rwx)      : ORIGIN = 0x60000000, LENGTH = 0x00100000
-}
-
-/**
- * Must match cmsis_nvic.h
- */
-__vector_size = 4 * (16 + 142);
- 
-/* Linker script to place sections and symbol values. Should be used together
- * with other linker script that defines memory regions FLASH and RAM.
- * It references following symbols, which must be defined in code:
- * Reset_Handler : Entry of reset handler
- *
- * It defines following symbols, which code can use without definition:
- * __exidx_start
- * __exidx_end
- * __etext
- * __data_start__
- * __preinit_array_start
- * __preinit_array_end
- * __init_array_start
- * __init_array_end
- * __fini_array_start
- * __fini_array_end
- * __data_end__
- * __bss_start__
- * __bss_end__
- * __end__
- * end
- * __HeapLimit
- * __StackLimit
- * __StackTop
- * __stack
- */
-ENTRY(Reset_Handler)
-
-SECTIONS
-{
-    .isr_vector :
-    {
-        __vector_table = .;
-        KEEP(*(.vector_table))
-         . = ALIGN(4);
-    } > VECTORS
-
-    /* ensure that uvisor bss is at the beginning of memory */
-    .uvisor.bss (NOLOAD):
-    {
-        . = ALIGN(32);
-        __uvisor_bss_start = .;
-
-        /* protected uvisor main bss */
-        . = ALIGN(32);
-        __uvisor_bss_main_start = .;
-        KEEP(*(.keep.uvisor.bss.main))
-        . = ALIGN(32);
-        __uvisor_bss_main_end = .;
-
-        /* protected uvisor secure boxes bss */
-        . = ALIGN(32);
-        __uvisor_bss_boxes_start = .;
-        KEEP(*(.keep.uvisor.bss.boxes))
-        . = ALIGN(32);
-        __uvisor_bss_boxes_end = .;
-
-        /* Ensure log2(size) alignment of the uvisor region, to ensure that the region can be effectively protected by the MPU. */
-        . = ALIGN(1 << LOG2CEIL(__uvisor_bss_boxes_end - __uvisor_bss_start));
-        __uvisor_bss_end = .;
-    } > RAM_INTERN
-
-    .text :
-    {
-        /* uVisor code and data */
-        . = ALIGN(4);
-        __uvisor_main_start = .;
-        *(.uvisor.main)
-        __uvisor_main_end = .;
-
-        *(.text*)
-
-        KEEP(*(.init))
-        KEEP(*(.fini))
-
-        /* .ctors */
-        *crtbegin.o(.ctors)
-        *crtbegin?.o(.ctors)
-        *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
-        *(SORT(.ctors.*))
-        *(.ctors)
-
-        /* .dtors */
-        *crtbegin.o(.dtors)
-        *crtbegin?.o(.dtors)
-        *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
-        *(SORT(.dtors.*))
-        *(.dtors)
-
-        *(.rodata*)
-
-        KEEP(*(.eh_frame*))
-    } > FLASH
-
-    .ARM.extab :
-    {
-        *(.ARM.extab* .gnu.linkonce.armextab.*)
-    } > FLASH
-
-    .ARM.exidx :
-    {
-       __exidx_start = .;
-        *(.ARM.exidx* .gnu.linkonce.armexidx.*)
-       __exidx_end = .;
-    } > FLASH
-
-    /* .stack section doesn't contains any symbols. It is only
-     * used for linker to reserve space for the main stack section
-     * WARNING: .stack should come immediately after the last secure memory
-     * section.  This provides stack overflow detection. */
-    .stack (NOLOAD):
-    {
-        __StackLimit = .;
-        *(.stack*);
-        . += StackSize - (. - __StackLimit);
-    } > RAM_INTERN
-
-    /* Set stack top to end of RAM, and stack limit move down by
-     * size of stack_dummy section */
-    __StackTop = ADDR(.stack) + SIZEOF(.stack);
-    __StackLimit = ADDR(.stack);
-    PROVIDE(__stack = __StackTop);
-
-    /* Relocate vector table in SRAM */
-    .isr_vector.reloc (NOLOAD) :
-    {
-        . = ALIGN(1 << LOG2CEIL(__vector_size));
-        PROVIDE(__start_vector_table__ = .);
-        . += __vector_size;
-        PROVIDE(__end_vector_table__ = .);
-    } > RAM_INTERN
-    
-    .data :
-    {
-        PROVIDE( __etext = LOADADDR(.data) );
-
-        __data_start__ = .;
-        *(vtable)
-        *(.data*)
-
-        . = ALIGN(4);
-        /* preinit data */
-        PROVIDE_HIDDEN (__preinit_array_start = .);
-        KEEP(*(.preinit_array))
-        PROVIDE_HIDDEN (__preinit_array_end = .);
-
-        . = ALIGN(4);
-        /* init data */
-        PROVIDE_HIDDEN (__init_array_start = .);
-        KEEP(*(SORT(.init_array.*)))
-        KEEP(*(.init_array))
-        PROVIDE_HIDDEN (__init_array_end = .);
-
-        . = ALIGN(4);
-        /* finit data */
-        PROVIDE_HIDDEN (__fini_array_start = .);
-        KEEP(*(SORT(.fini_array.*)))
-        KEEP(*(.fini_array))
-        PROVIDE_HIDDEN (__fini_array_end = .);
-
-        /* All data end */
-        . = ALIGN(32);
-        __data_end__ = .;
-
-    } >RAM_INTERN AT>FLASH
-
-    /* uvisor configuration data */
-    .uvisor.secure :
-    {
-        . = ALIGN(32);
-        __uvisor_secure_start = .;
-
-        /* uvisor secure boxes configuration tables */
-        . = ALIGN(32);
-        __uvisor_cfgtbl_start = .;
-        KEEP(*(.keep.uvisor.cfgtbl))
-        . = ALIGN(32);
-        __uvisor_cfgtbl_end = .;
-
-        /* pointers to uvisor secure boxes configuration tables */
-        /* note: no further alignment here, we need to have the exact list of pointers */
-        __uvisor_cfgtbl_ptr_start = .;
-        KEEP(*(.keep.uvisor.cfgtbl_ptr_first))
-        KEEP(*(.keep.uvisor.cfgtbl_ptr))
-        __uvisor_cfgtbl_ptr_end = .;
-
-        /* the following symbols are kept for backward compatibility and will be soon
-         * deprecated; applications actively using uVisor (__uvisor_mode == UVISOR_ENABLED)
-         * will need to use uVisor 0.8.x or above, or the security assertions will halt the
-         * system */
-        /************************/
-        __uvisor_data_src = .;
-        __uvisor_data_start = .;
-        __uvisor_data_end = .;
-        /************************/
-
-        . = ALIGN(32);
-        __uvisor_secure_end = .;
-    } >FLASH
-
-    .uninitialized (NOLOAD):
-    {
-        . = ALIGN(32);
-        __uninitialized_start = .;
-        *(.uninitialized)
-        KEEP(*(.keep.uninitialized))
-        . = ALIGN(32);
-        __uninitialized_end = .;
-    } > RAM_INTERN
-
-    .bss.extern (NOLOAD):
-    {
-        __bss_extern_start__ = .;
-        /**
-         * Place large .bss* sections into external SRAM if internal SRAM is insufficient.
-         * Such memory arrangement requires initializing .bss.extern section to zero in startup file. Check startup fiile in cmsis-core-* for support or not.
-         */
-        *lwip_*.o(.bss*)
-        *lwip_*.o(COMMON)
-        *mesh_system.o(.bss*)
-       __bss_extern_end__ = .;
-    } > RAM_EXTERN
-    
-    .bss (NOLOAD):
-    {
-        __bss_start__ = .;
-        *(.bss*)
-        *(COMMON)
-        __bss_end__ = .;
-    } > RAM_INTERN
-
-    .heap (NOLOAD):
-    {
-        __end__ = .;
-        end = __end__;
-        *(.heap*);
-        . += (ORIGIN(RAM_EXTERN) + LENGTH(RAM_EXTERN) - .);
-        __HeapLimit = .;
-    } > RAM_EXTERN
-    PROVIDE(__heap_size = SIZEOF(.heap));
-    PROVIDE(__mbed_sbrk_start = ADDR(.heap));
-    PROVIDE(__mbed_krbs_start = ADDR(.heap) + SIZEOF(.heap));
-    
-    /* Provide physical memory boundaries for uVisor. */
-    __uvisor_flash_start = ORIGIN(VECTORS);
-    __uvisor_flash_end = ORIGIN(FLASH) + LENGTH(FLASH);
-    __uvisor_sram_start = ORIGIN(RAM_INTERN);
-    __uvisor_sram_end = ORIGIN(RAM_INTERN) + LENGTH(RAM_INTERN);
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_GCC_ARM/TARGET_NU_XRAM_SUPPORTED/NUC472.ld	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,269 @@
+/*
+ * Nuvoton NUC472 GCC linker script file
+ */
+
+StackSize = 0x800;
+
+MEMORY
+{
+    
+  VECTORS (rx)          : ORIGIN = 0x00000000, LENGTH = 0x00000400
+  FLASH (rx)            : ORIGIN = 0x00000400, LENGTH = 0x00080000 - 0x00000400
+  RAM_INTERN (rwx)      : ORIGIN = 0x20000000, LENGTH = 0x00010000 - 0x00000000
+  RAM_EXTERN (rwx)      : ORIGIN = 0x60000000, LENGTH = 0x00100000
+}
+
+/**
+ * Must match cmsis_nvic.h
+ */
+__vector_size = 4 * (16 + 142);
+ 
+/* Linker script to place sections and symbol values. Should be used together
+ * with other linker script that defines memory regions FLASH and RAM.
+ * It references following symbols, which must be defined in code:
+ * Reset_Handler : Entry of reset handler
+ *
+ * It defines following symbols, which code can use without definition:
+ * __exidx_start
+ * __exidx_end
+ * __etext
+ * __data_start__
+ * __preinit_array_start
+ * __preinit_array_end
+ * __init_array_start
+ * __init_array_end
+ * __fini_array_start
+ * __fini_array_end
+ * __data_end__
+ * __bss_start__
+ * __bss_end__
+ * __end__
+ * end
+ * __HeapLimit
+ * __StackLimit
+ * __StackTop
+ * __stack
+ */
+ENTRY(Reset_Handler)
+
+SECTIONS
+{
+    .isr_vector :
+    {
+        __vector_table = .;
+        KEEP(*(.vector_table))
+         . = ALIGN(4);
+    } > VECTORS
+
+    /* ensure that uvisor bss is at the beginning of memory */
+    .uvisor.bss (NOLOAD):
+    {
+        . = ALIGN(32);
+        __uvisor_bss_start = .;
+
+        /* protected uvisor main bss */
+        . = ALIGN(32);
+        __uvisor_bss_main_start = .;
+        KEEP(*(.keep.uvisor.bss.main))
+        . = ALIGN(32);
+        __uvisor_bss_main_end = .;
+
+        /* protected uvisor secure boxes bss */
+        . = ALIGN(32);
+        __uvisor_bss_boxes_start = .;
+        KEEP(*(.keep.uvisor.bss.boxes))
+        . = ALIGN(32);
+        __uvisor_bss_boxes_end = .;
+
+        /* Ensure log2(size) alignment of the uvisor region, to ensure that the region can be effectively protected by the MPU. */
+        . = ALIGN(1 << LOG2CEIL(__uvisor_bss_boxes_end - __uvisor_bss_start));
+        __uvisor_bss_end = .;
+    } > RAM_INTERN
+
+    .text :
+    {
+        /* uVisor code and data */
+        . = ALIGN(4);
+        __uvisor_main_start = .;
+        *(.uvisor.main)
+        __uvisor_main_end = .;
+
+        *(.text*)
+
+        KEEP(*(.init))
+        KEEP(*(.fini))
+
+        /* .ctors */
+        *crtbegin.o(.ctors)
+        *crtbegin?.o(.ctors)
+        *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
+        *(SORT(.ctors.*))
+        *(.ctors)
+
+        /* .dtors */
+        *crtbegin.o(.dtors)
+        *crtbegin?.o(.dtors)
+        *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
+        *(SORT(.dtors.*))
+        *(.dtors)
+
+        *(.rodata*)
+
+        KEEP(*(.eh_frame*))
+    } > FLASH
+
+    .ARM.extab :
+    {
+        *(.ARM.extab* .gnu.linkonce.armextab.*)
+    } > FLASH
+
+    .ARM.exidx :
+    {
+       __exidx_start = .;
+        *(.ARM.exidx* .gnu.linkonce.armexidx.*)
+       __exidx_end = .;
+    } > FLASH
+
+    /* .stack section doesn't contains any symbols. It is only
+     * used for linker to reserve space for the main stack section
+     * WARNING: .stack should come immediately after the last secure memory
+     * section.  This provides stack overflow detection. */
+    .stack (NOLOAD):
+    {
+        __StackLimit = .;
+        *(.stack*);
+        . += StackSize - (. - __StackLimit);
+    } > RAM_INTERN
+
+    /* Set stack top to end of RAM, and stack limit move down by
+     * size of stack_dummy section */
+    __StackTop = ADDR(.stack) + SIZEOF(.stack);
+    __StackLimit = ADDR(.stack);
+    PROVIDE(__stack = __StackTop);
+
+    /* Relocate vector table in SRAM */
+    .isr_vector.reloc (NOLOAD) :
+    {
+        . = ALIGN(1 << LOG2CEIL(__vector_size));
+        PROVIDE(__start_vector_table__ = .);
+        . += __vector_size;
+        PROVIDE(__end_vector_table__ = .);
+    } > RAM_INTERN
+    
+    .data :
+    {
+        PROVIDE( __etext = LOADADDR(.data) );
+
+        __data_start__ = .;
+        *(vtable)
+        *(.data*)
+
+        . = ALIGN(4);
+        /* preinit data */
+        PROVIDE_HIDDEN (__preinit_array_start = .);
+        KEEP(*(.preinit_array))
+        PROVIDE_HIDDEN (__preinit_array_end = .);
+
+        . = ALIGN(4);
+        /* init data */
+        PROVIDE_HIDDEN (__init_array_start = .);
+        KEEP(*(SORT(.init_array.*)))
+        KEEP(*(.init_array))
+        PROVIDE_HIDDEN (__init_array_end = .);
+
+        . = ALIGN(4);
+        /* finit data */
+        PROVIDE_HIDDEN (__fini_array_start = .);
+        KEEP(*(SORT(.fini_array.*)))
+        KEEP(*(.fini_array))
+        PROVIDE_HIDDEN (__fini_array_end = .);
+
+        /* All data end */
+        . = ALIGN(32);
+        __data_end__ = .;
+
+    } >RAM_INTERN AT>FLASH
+
+    /* uvisor configuration data */
+    .uvisor.secure :
+    {
+        . = ALIGN(32);
+        __uvisor_secure_start = .;
+
+        /* uvisor secure boxes configuration tables */
+        . = ALIGN(32);
+        __uvisor_cfgtbl_start = .;
+        KEEP(*(.keep.uvisor.cfgtbl))
+        . = ALIGN(32);
+        __uvisor_cfgtbl_end = .;
+
+        /* pointers to uvisor secure boxes configuration tables */
+        /* note: no further alignment here, we need to have the exact list of pointers */
+        __uvisor_cfgtbl_ptr_start = .;
+        KEEP(*(.keep.uvisor.cfgtbl_ptr_first))
+        KEEP(*(.keep.uvisor.cfgtbl_ptr))
+        __uvisor_cfgtbl_ptr_end = .;
+
+        /* the following symbols are kept for backward compatibility and will be soon
+         * deprecated; applications actively using uVisor (__uvisor_mode == UVISOR_ENABLED)
+         * will need to use uVisor 0.8.x or above, or the security assertions will halt the
+         * system */
+        /************************/
+        __uvisor_data_src = .;
+        __uvisor_data_start = .;
+        __uvisor_data_end = .;
+        /************************/
+
+        . = ALIGN(32);
+        __uvisor_secure_end = .;
+    } >FLASH
+
+    .uninitialized (NOLOAD):
+    {
+        . = ALIGN(32);
+        __uninitialized_start = .;
+        *(.uninitialized)
+        KEEP(*(.keep.uninitialized))
+        . = ALIGN(32);
+        __uninitialized_end = .;
+    } > RAM_INTERN
+
+    .bss.extern (NOLOAD):
+    {
+        __bss_extern_start__ = .;
+        /**
+         * Place large .bss* sections into external SRAM if internal SRAM is insufficient.
+         * Such memory arrangement requires initializing .bss.extern section to zero in startup file. Check startup fiile in cmsis-core-* for support or not.
+         */
+        *lwip_*.o(.bss*)
+        *lwip_*.o(COMMON)
+        *mesh_system.o(.bss*)
+       __bss_extern_end__ = .;
+    } > RAM_EXTERN
+    
+    .bss (NOLOAD):
+    {
+        __bss_start__ = .;
+        *(.bss*)
+        *(COMMON)
+        __bss_end__ = .;
+    } > RAM_INTERN
+
+    .heap (NOLOAD):
+    {
+        __end__ = .;
+        end = __end__;
+        *(.heap*);
+        . += (ORIGIN(RAM_EXTERN) + LENGTH(RAM_EXTERN) - .);
+        __HeapLimit = .;
+    } > RAM_EXTERN
+    PROVIDE(__heap_size = SIZEOF(.heap));
+    PROVIDE(__mbed_sbrk_start = ADDR(.heap));
+    PROVIDE(__mbed_krbs_start = ADDR(.heap) + SIZEOF(.heap));
+    
+    /* Provide physical memory boundaries for uVisor. */
+    __uvisor_flash_start = ORIGIN(VECTORS);
+    __uvisor_flash_end = ORIGIN(FLASH) + LENGTH(FLASH);
+    __uvisor_sram_start = ORIGIN(RAM_INTERN);
+    __uvisor_sram_end = ORIGIN(RAM_INTERN) + LENGTH(RAM_INTERN);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_GCC_ARM/TARGET_NU_XRAM_UNSUPPORTED/NUC472.ld	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,268 @@
+/*
+ * Nuvoton NUC472 GCC linker script file
+ */
+
+StackSize = 0x800;
+
+MEMORY
+{
+    
+  VECTORS (rx)          : ORIGIN = 0x00000000, LENGTH = 0x00000400
+  FLASH (rx)            : ORIGIN = 0x00000400, LENGTH = 0x00080000 - 0x00000400
+  RAM_INTERN (rwx)      : ORIGIN = 0x20000000, LENGTH = 0x00010000 - 0x00000000
+}
+
+/**
+ * Must match cmsis_nvic.h
+ */
+__vector_size = 4 * (16 + 142);
+ 
+/* Linker script to place sections and symbol values. Should be used together
+ * with other linker script that defines memory regions FLASH and RAM.
+ * It references following symbols, which must be defined in code:
+ * Reset_Handler : Entry of reset handler
+ *
+ * It defines following symbols, which code can use without definition:
+ * __exidx_start
+ * __exidx_end
+ * __etext
+ * __data_start__
+ * __preinit_array_start
+ * __preinit_array_end
+ * __init_array_start
+ * __init_array_end
+ * __fini_array_start
+ * __fini_array_end
+ * __data_end__
+ * __bss_start__
+ * __bss_end__
+ * __end__
+ * end
+ * __HeapLimit
+ * __StackLimit
+ * __StackTop
+ * __stack
+ */
+ENTRY(Reset_Handler)
+
+SECTIONS
+{
+    .isr_vector :
+    {
+        __vector_table = .;
+        KEEP(*(.vector_table))
+         . = ALIGN(4);
+    } > VECTORS
+
+    /* ensure that uvisor bss is at the beginning of memory */
+    .uvisor.bss (NOLOAD):
+    {
+        . = ALIGN(32);
+        __uvisor_bss_start = .;
+
+        /* protected uvisor main bss */
+        . = ALIGN(32);
+        __uvisor_bss_main_start = .;
+        KEEP(*(.keep.uvisor.bss.main))
+        . = ALIGN(32);
+        __uvisor_bss_main_end = .;
+
+        /* protected uvisor secure boxes bss */
+        . = ALIGN(32);
+        __uvisor_bss_boxes_start = .;
+        KEEP(*(.keep.uvisor.bss.boxes))
+        . = ALIGN(32);
+        __uvisor_bss_boxes_end = .;
+
+        /* Ensure log2(size) alignment of the uvisor region, to ensure that the region can be effectively protected by the MPU. */
+        . = ALIGN(1 << LOG2CEIL(__uvisor_bss_boxes_end - __uvisor_bss_start));
+        __uvisor_bss_end = .;
+    } > RAM_INTERN
+
+    .text :
+    {
+        /* uVisor code and data */
+        . = ALIGN(4);
+        __uvisor_main_start = .;
+        *(.uvisor.main)
+        __uvisor_main_end = .;
+
+        *(.text*)
+
+        KEEP(*(.init))
+        KEEP(*(.fini))
+
+        /* .ctors */
+        *crtbegin.o(.ctors)
+        *crtbegin?.o(.ctors)
+        *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
+        *(SORT(.ctors.*))
+        *(.ctors)
+
+        /* .dtors */
+        *crtbegin.o(.dtors)
+        *crtbegin?.o(.dtors)
+        *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
+        *(SORT(.dtors.*))
+        *(.dtors)
+
+        *(.rodata*)
+
+        KEEP(*(.eh_frame*))
+    } > FLASH
+
+    .ARM.extab :
+    {
+        *(.ARM.extab* .gnu.linkonce.armextab.*)
+    } > FLASH
+
+    .ARM.exidx :
+    {
+       __exidx_start = .;
+        *(.ARM.exidx* .gnu.linkonce.armexidx.*)
+       __exidx_end = .;
+    } > FLASH
+
+    /* .stack section doesn't contains any symbols. It is only
+     * used for linker to reserve space for the main stack section
+     * WARNING: .stack should come immediately after the last secure memory
+     * section.  This provides stack overflow detection. */
+    .stack (NOLOAD):
+    {
+        __StackLimit = .;
+        *(.stack*);
+        . += StackSize - (. - __StackLimit);
+    } > RAM_INTERN
+
+    /* Set stack top to end of RAM, and stack limit move down by
+     * size of stack_dummy section */
+    __StackTop = ADDR(.stack) + SIZEOF(.stack);
+    __StackLimit = ADDR(.stack);
+    PROVIDE(__stack = __StackTop);
+
+    /* Relocate vector table in SRAM */
+    .isr_vector.reloc (NOLOAD) :
+    {
+        . = ALIGN(1 << LOG2CEIL(__vector_size));
+        PROVIDE(__start_vector_table__ = .);
+        . += __vector_size;
+        PROVIDE(__end_vector_table__ = .);
+    } > RAM_INTERN
+    
+    .data :
+    {
+        PROVIDE( __etext = LOADADDR(.data) );
+
+        __data_start__ = .;
+        *(vtable)
+        *(.data*)
+
+        . = ALIGN(4);
+        /* preinit data */
+        PROVIDE_HIDDEN (__preinit_array_start = .);
+        KEEP(*(.preinit_array))
+        PROVIDE_HIDDEN (__preinit_array_end = .);
+
+        . = ALIGN(4);
+        /* init data */
+        PROVIDE_HIDDEN (__init_array_start = .);
+        KEEP(*(SORT(.init_array.*)))
+        KEEP(*(.init_array))
+        PROVIDE_HIDDEN (__init_array_end = .);
+
+        . = ALIGN(4);
+        /* finit data */
+        PROVIDE_HIDDEN (__fini_array_start = .);
+        KEEP(*(SORT(.fini_array.*)))
+        KEEP(*(.fini_array))
+        PROVIDE_HIDDEN (__fini_array_end = .);
+
+        /* All data end */
+        . = ALIGN(32);
+        __data_end__ = .;
+
+    } >RAM_INTERN AT>FLASH
+
+    /* uvisor configuration data */
+    .uvisor.secure :
+    {
+        . = ALIGN(32);
+        __uvisor_secure_start = .;
+
+        /* uvisor secure boxes configuration tables */
+        . = ALIGN(32);
+        __uvisor_cfgtbl_start = .;
+        KEEP(*(.keep.uvisor.cfgtbl))
+        . = ALIGN(32);
+        __uvisor_cfgtbl_end = .;
+
+        /* pointers to uvisor secure boxes configuration tables */
+        /* note: no further alignment here, we need to have the exact list of pointers */
+        __uvisor_cfgtbl_ptr_start = .;
+        KEEP(*(.keep.uvisor.cfgtbl_ptr_first))
+        KEEP(*(.keep.uvisor.cfgtbl_ptr))
+        __uvisor_cfgtbl_ptr_end = .;
+
+        /* the following symbols are kept for backward compatibility and will be soon
+         * deprecated; applications actively using uVisor (__uvisor_mode == UVISOR_ENABLED)
+         * will need to use uVisor 0.8.x or above, or the security assertions will halt the
+         * system */
+        /************************/
+        __uvisor_data_src = .;
+        __uvisor_data_start = .;
+        __uvisor_data_end = .;
+        /************************/
+
+        . = ALIGN(32);
+        __uvisor_secure_end = .;
+    } >FLASH
+
+    .uninitialized (NOLOAD):
+    {
+        . = ALIGN(32);
+        __uninitialized_start = .;
+        *(.uninitialized)
+        KEEP(*(.keep.uninitialized))
+        . = ALIGN(32);
+        __uninitialized_end = .;
+    } > RAM_INTERN
+
+    .bss.extern (NOLOAD):
+    {
+        __bss_extern_start__ = .;
+        /**
+         * Place large .bss* sections into external SRAM if internal SRAM is insufficient.
+         * Such memory arrangement requires initializing .bss.extern section to zero in startup file. Check startup fiile in cmsis-core-* for support or not.
+         */
+        *lwip_*.o(.bss*)
+        *lwip_*.o(COMMON)
+        *mesh_system.o(.bss*)
+       __bss_extern_end__ = .;
+    } > RAM_INTERN
+    
+    .bss (NOLOAD):
+    {
+        __bss_start__ = .;
+        *(.bss*)
+        *(COMMON)
+        __bss_end__ = .;
+    } > RAM_INTERN
+
+    .heap (NOLOAD):
+    {
+        __end__ = .;
+        end = __end__;
+        *(.heap*);
+        . += (ORIGIN(RAM_INTERN) + LENGTH(RAM_INTERN) - .);
+        __HeapLimit = .;
+    } > RAM_INTERN
+    PROVIDE(__heap_size = SIZEOF(.heap));
+    PROVIDE(__mbed_sbrk_start = ADDR(.heap));
+    PROVIDE(__mbed_krbs_start = ADDR(.heap) + SIZEOF(.heap));
+    
+    /* Provide physical memory boundaries for uVisor. */
+    __uvisor_flash_start = ORIGIN(VECTORS);
+    __uvisor_flash_end = ORIGIN(FLASH) + LENGTH(FLASH);
+    __uvisor_sram_start = ORIGIN(RAM_INTERN);
+    __uvisor_sram_end = ORIGIN(RAM_INTERN) + LENGTH(RAM_INTERN);
+}
--- a/targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_IAR/NUC472_442.icf	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,41 +0,0 @@
-/*###ICF### Section handled by ICF editor, don't touch! ****/
-/*-Editor annotation file-*/
-/* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */
-/*-Specials-*/
-define symbol __ICFEDIT_intvec_start__ = 0x00000000;
-/*-Memory Regions-*/
-define symbol __ICFEDIT_region_ROM_start__ = 0x00000000;
-define symbol __ICFEDIT_region_ROM_end__   = 0x00080000;
-define symbol __ICFEDIT_region_IRAM_start__ = 0x20000000;
-define symbol __ICFEDIT_region_IRAM_end__   = 0x20010000;
-define symbol __ICFEDIT_region_XRAM_start__ = 0x60000000;
-define symbol __ICFEDIT_region_XRAM_end__   = 0x60100000;
-/*-Sizes-*/
-define symbol __ICFEDIT_size_cstack__ = 0x800;
-define symbol __ICFEDIT_size_heap__   = 0xC0000;
-/**** End of ICF editor section. ###ICF###*/
-
-
-define memory mem with size = 4G;
-define region ROM_region   = mem:[from __ICFEDIT_region_ROM_start__   to __ICFEDIT_region_ROM_end__];
-define region IRAM_region  = mem:[from __ICFEDIT_region_IRAM_start__  to __ICFEDIT_region_IRAM_end__];
-define region XRAM_region  = mem:[from __ICFEDIT_region_XRAM_start__  to __ICFEDIT_region_XRAM_end__];
-
-define block CSTACK    with alignment = 8, size = __ICFEDIT_size_cstack__   { };
-define block HEAP      with alignment = 8, size = __ICFEDIT_size_heap__     { };
-/* NOTE: Vector table base requires to be aligned to the power of vector table size. Give a safe value here. */
-define block IRAMVEC   with alignment = 1024, size = 4 * (16 + 142)         { };
-/* Move non-critical libraries to external SRAM while internal SRAM is insufficient. */
-define block XRAM_NC   with alignment = 8                                   { zeroinit object *lwip_*, zeroinit object *mesh_system.o };
-
-
-initialize by copy { readwrite };
-do not initialize  { section .noinit };
-
-place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec };
-
-place in ROM_region   { readonly };
-place at start of IRAM_region   { block CSTACK };
-place in IRAM_region   { block IRAMVEC };
-place in IRAM_region   { readwrite };
-place in XRAM_region   { block XRAM_NC, block HEAP };
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_IAR/TARGET_NU_XRAM_SUPPORTED/NUC472_442.icf	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,41 @@
+/*###ICF### Section handled by ICF editor, don't touch! ****/
+/*-Editor annotation file-*/
+/* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */
+/*-Specials-*/
+define symbol __ICFEDIT_intvec_start__ = 0x00000000;
+/*-Memory Regions-*/
+define symbol __ICFEDIT_region_ROM_start__ = 0x00000000;
+define symbol __ICFEDIT_region_ROM_end__   = 0x00080000;
+define symbol __ICFEDIT_region_IRAM_start__ = 0x20000000;
+define symbol __ICFEDIT_region_IRAM_end__   = 0x20010000;
+define symbol __ICFEDIT_region_XRAM_start__ = 0x60000000;
+define symbol __ICFEDIT_region_XRAM_end__   = 0x60100000;
+/*-Sizes-*/
+define symbol __ICFEDIT_size_cstack__ = 0x800;
+define symbol __ICFEDIT_size_heap__   = 0xC0000;
+/**** End of ICF editor section. ###ICF###*/
+
+
+define memory mem with size = 4G;
+define region ROM_region   = mem:[from __ICFEDIT_region_ROM_start__   to __ICFEDIT_region_ROM_end__];
+define region IRAM_region  = mem:[from __ICFEDIT_region_IRAM_start__  to __ICFEDIT_region_IRAM_end__];
+define region XRAM_region  = mem:[from __ICFEDIT_region_XRAM_start__  to __ICFEDIT_region_XRAM_end__];
+
+define block CSTACK    with alignment = 8, size = __ICFEDIT_size_cstack__   { };
+define block HEAP      with alignment = 8, size = __ICFEDIT_size_heap__     { };
+/* NOTE: Vector table base requires to be aligned to the power of vector table size. Give a safe value here. */
+define block IRAMVEC   with alignment = 1024, size = 4 * (16 + 142)         { };
+/* Move non-critical libraries to external SRAM while internal SRAM is insufficient. */
+define block XRAM_NC   with alignment = 8                                   { zeroinit object *lwip_*, zeroinit object *mesh_system.o };
+
+
+initialize by copy { readwrite };
+do not initialize  { section .noinit };
+
+place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec };
+
+place in ROM_region   { readonly };
+place at start of IRAM_region   { block CSTACK };
+place in IRAM_region   { block IRAMVEC };
+place in IRAM_region   { readwrite };
+place in XRAM_region   { block XRAM_NC, block HEAP };
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_IAR/TARGET_NU_XRAM_UNSUPPORTED/NUC472_442.icf	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,36 @@
+/*###ICF### Section handled by ICF editor, don't touch! ****/
+/*-Editor annotation file-*/
+/* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */
+/*-Specials-*/
+define symbol __ICFEDIT_intvec_start__ = 0x00000000;
+/*-Memory Regions-*/
+define symbol __ICFEDIT_region_ROM_start__ = 0x00000000;
+define symbol __ICFEDIT_region_ROM_end__   = 0x00080000;
+define symbol __ICFEDIT_region_IRAM_start__ = 0x20000000;
+define symbol __ICFEDIT_region_IRAM_end__   = 0x20010000;
+/*-Sizes-*/
+define symbol __ICFEDIT_size_cstack__ = 0x800;
+define symbol __ICFEDIT_size_heap__   = 0x8000;
+/**** End of ICF editor section. ###ICF###*/
+
+
+define memory mem with size = 4G;
+define region ROM_region   = mem:[from __ICFEDIT_region_ROM_start__   to __ICFEDIT_region_ROM_end__];
+define region IRAM_region  = mem:[from __ICFEDIT_region_IRAM_start__  to __ICFEDIT_region_IRAM_end__];
+
+define block CSTACK    with alignment = 8, size = __ICFEDIT_size_cstack__   { };
+define block HEAP      with alignment = 8, size = __ICFEDIT_size_heap__     { };
+/* NOTE: Vector table base requires to be aligned to the power of vector table size. Give a safe value here. */
+define block IRAMVEC   with alignment = 1024, size = 4 * (16 + 142)         { };
+
+
+initialize by copy { readwrite };
+do not initialize  { section .noinit };
+
+place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec };
+
+place in ROM_region   { readonly };
+place at start of IRAM_region   { block CSTACK };
+place in IRAM_region   { block IRAMVEC };
+place in IRAM_region   { readwrite };
+place in IRAM_region   { block HEAP };
\ No newline at end of file
--- a/targets/TARGET_NUVOTON/TARGET_NUC472/device/system_NUC472_442.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/device/system_NUC472_442.c	Tue Mar 14 16:40:56 2017 +0000
@@ -19,7 +19,7 @@
 uint32_t CyclesPerUs      = (__HSI / 1000000);  /*!< Cycles per micro second            */
 uint32_t gau32ClkSrcTbl[] = {__HXT, __LXT, 0, __LIRC, 0, 0, 0, __HIRC}; /*!< System clock source table */
 
-#if defined TARGET_NUMAKER_PFM_NUC472
+#if defined TARGET_NU_XRAM_SUPPORTED
 static void nu_ebi_init(void);
 #endif
 
@@ -113,13 +113,13 @@
     /*------------------------------------------------------------------------*/
 #endif
 
-#if defined TARGET_NUMAKER_PFM_NUC472
+#if defined TARGET_NU_XRAM_SUPPORTED
     // NOTE: C-runtime not initialized yet. Ensure no static memory (global variable) are accessed in this function.
     nu_ebi_init();
 #endif
 }
 
-#if defined TARGET_NUMAKER_PFM_NUC472
+#if defined TARGET_NU_XRAM_SUPPORTED
 void nu_ebi_init(void)
 {
     /* Enable IP clock */
--- a/targets/TARGET_NUVOTON/TARGET_NUC472/i2c_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/i2c_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -24,7 +24,7 @@
 #include "nu_modutil.h"
 #include "nu_miscutil.h"
 #include "nu_bitutil.h"
-#include "critical.h"
+#include "mbed_critical.h"
 
 #define NU_I2C_DEBUG    0
 
--- a/targets/TARGET_NUVOTON/TARGET_NUC472/lp_ticker.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/lp_ticker.c	Tue Mar 14 16:40:56 2017 +0000
@@ -21,7 +21,7 @@
 #include "sleep_api.h"
 #include "nu_modutil.h"
 #include "nu_miscutil.h"
-#include "critical.h"
+#include "mbed_critical.h"
 
 // lp_ticker tick = us = timestamp
 #define US_PER_TICK             (1)
--- a/targets/TARGET_NUVOTON/TARGET_NUC472/rtc_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/rtc_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -18,7 +18,7 @@
 
 #if DEVICE_RTC
 
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "mbed_error.h"
 #include "nu_modutil.h"
 #include "nu_miscutil.h"
--- a/targets/TARGET_NUVOTON/TARGET_NUC472/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -36,7 +36,7 @@
 /**
  * Enter Idle mode.
  */
-void sleep(void)
+void hal_sleep(void)
 {
     struct sleep_s sleep_obj;
     sleep_obj.powerdown = 0;
@@ -47,7 +47,7 @@
 /**
  * Enter Power-down mode while no peripheral is active; otherwise, enter Idle mode.
  */
-void deepsleep(void)
+void hal_deepsleep(void)
 {
     struct sleep_s sleep_obj;
     sleep_obj.powerdown = 1;
--- a/targets/TARGET_NUVOTON/TARGET_NUC472/us_ticker.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_NUC472/us_ticker.c	Tue Mar 14 16:40:56 2017 +0000
@@ -19,7 +19,7 @@
 #include "mbed_assert.h"
 #include "nu_modutil.h"
 #include "nu_miscutil.h"
-#include "critical.h"
+#include "mbed_critical.h"
 
 // us_ticker tick = us = timestamp
 #define US_PER_TICK             1
--- a/targets/TARGET_NXP/TARGET_LPC11U6X/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC11U6X/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -19,7 +19,7 @@
 
 #if DEVICE_SLEEP
 
-void sleep(void) {
+void hal_sleep(void) {
 
 #if (DEVICE_SEMIHOST == 1)
     // ensure debug is disconnected
@@ -37,7 +37,7 @@
 }
 
 
-void deepsleep(void) {
+void hal_deepsleep(void) {
 
 #if (DEVICE_SEMIHOST == 1)
     // ensure debug is disconnected
--- a/targets/TARGET_NXP/TARGET_LPC11UXX/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC11UXX/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -17,7 +17,7 @@
 #include "cmsis.h"
 #include "mbed_interface.h"
 
-void sleep(void) {
+void hal_sleep(void) {
     // ensure debug is disconnected
     #if DEVICE_SEMIHOST
     mbed_interface_disconnect();
@@ -59,7 +59,7 @@
 *       We treat a deepsleep() as a normal sleep().
 */
 
-void deepsleep(void) {
+void hal_deepsleep(void) {
     // ensure debug is disconnected
     #if DEVICE_SEMIHOST
     mbed_interface_disconnect();
--- a/targets/TARGET_NXP/TARGET_LPC11XX_11CXX/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC11XX_11CXX/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -17,7 +17,7 @@
 #include "cmsis.h"
 #include "mbed_interface.h"
 
-void sleep(void) {
+void hal_sleep(void) {
     
     // PCON[DPDEN] set to sleep
     LPC_PMU->PCON = 0x0;
@@ -29,7 +29,7 @@
     __WFI();
 }
 
-void deepsleep(void) {
+void hal_deepsleep(void) {
     
     // PCON[DPDEN] set to deepsleep
     LPC_PMU->PCON = 0;
--- a/targets/TARGET_NXP/TARGET_LPC13XX/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC13XX/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -17,7 +17,7 @@
 #include "cmsis.h"
 #include "mbed_interface.h"
 
-void sleep(void) {
+void hal_sleep(void) {
     // PCON[PD] set to sleep
     LPC_PMU->PCON = 0x0;
     
@@ -28,7 +28,7 @@
     __WFI();
 }
 
-void deepsleep(void) {
+void hal_deepsleep(void) {
     // PCON[PD] set to deepsleep
     LPC_PMU->PCON = 0x1;
     
--- a/targets/TARGET_NXP/TARGET_LPC15XX/us_ticker.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC15XX/us_ticker.c	Tue Mar 14 16:40:56 2017 +0000
@@ -16,7 +16,7 @@
 #include <stddef.h>
 #include "us_ticker_api.h"
 #include "PeripheralNames.h"
-#include "critical.h"
+#include "mbed_critical.h"
 
 #define US_TICKER_TIMER_IRQn     SCT3_IRQn
 
--- a/targets/TARGET_NXP/TARGET_LPC176X/TARGET_UBLOX_C027/C027_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC176X/TARGET_UBLOX_C027/C027_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 #include "gpio_api.h"
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "C027_api.h"
 
 static gpio_t mdmEn, mdmLvlOe, mdmILvlOe, mdmUsbDet;
--- a/targets/TARGET_NXP/TARGET_LPC176X/ethernet_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC176X/ethernet_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -18,7 +18,7 @@
 #include <string.h>
 #include "cmsis.h"
 #include "mbed_interface.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 #include "mbed_error.h"
 
 #define NEW_LOGIC       0
--- a/targets/TARGET_NXP/TARGET_LPC176X/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC176X/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -17,7 +17,7 @@
 #include "cmsis.h"
 #include "mbed_interface.h"
 
-void sleep(void) {
+void hal_sleep(void) {
 
 #if (DEVICE_SEMIHOST == 1)
     // ensure debug is disconnected
@@ -60,7 +60,7 @@
 *       We treat a deepsleep() as a normal sleep().
 */
 
-void deepsleep(void) {
+void hal_deepsleep(void) {
 
 #if (DEVICE_SEMIHOST == 1)
     // ensure debug is disconnected
@@ -68,5 +68,5 @@
 #endif
     
     // PCON[PD] set to deepsleep
-    sleep();
+    hal_sleep();
 }
--- a/targets/TARGET_NXP/TARGET_LPC23XX/ethernet_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC23XX/ethernet_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -18,7 +18,7 @@
 #include "ethernet_api.h"
 #include "cmsis.h"
 #include "mbed_interface.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 #include "mbed_error.h"
 
 #define NEW_LOGIC       0
--- a/targets/TARGET_NXP/TARGET_LPC2460/ethernet_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC2460/ethernet_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -18,7 +18,7 @@
 #include "ethernet_api.h"
 #include "cmsis.h"
 #include "mbed_interface.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 #include "mbed_error.h"
 
 #define NEW_LOGIC       0
--- a/targets/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088/ethernet_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088/ethernet_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -18,7 +18,7 @@
 #include "ethernet_api.h"
 #include "cmsis.h"
 #include "mbed_interface.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 #include "mbed_error.h"
 
 #define NEW_LOGIC       0
--- a/targets/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088_DM/ethernet_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088_DM/ethernet_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -18,7 +18,7 @@
 #include "ethernet_api.h"
 #include "cmsis.h"
 #include "mbed_interface.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 #include "mbed_error.h"
 
 #define NEW_LOGIC       0
--- a/targets/TARGET_NXP/TARGET_LPC408X/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC408X/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -17,7 +17,7 @@
 #include "cmsis.h"
 #include "mbed_interface.h"
 
-void sleep(void) {
+void hal_sleep(void) {
     LPC_SC->PCON = 0x0;
     
     // SRC[SLEEPDEEP] set to 0 = sleep
@@ -52,6 +52,6 @@
 *
 *       We treat a deepsleep() as a normal sleep().
 */
-void deepsleep(void) {
-    sleep();
+void hal_deepsleep(void) {
+    hal_sleep();
 }
--- a/targets/TARGET_NXP/TARGET_LPC43XX/ethernet_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC43XX/ethernet_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -20,7 +20,7 @@
 #include <string.h>
 #include "cmsis.h"
 #include "mbed_interface.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 #include "mbed_error.h"
 #include "pinmap.h"
 
--- a/targets/TARGET_NXP/TARGET_LPC43XX/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC43XX/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -19,7 +19,7 @@
 #include "cmsis.h"
 #include "mbed_interface.h"
 
-void sleep(void) {
+void hal_sleep(void) {
     
     // SRC[SLEEPDEEP] set to 0 = sleep
     SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
@@ -31,6 +31,6 @@
 /*
  *  ToDo: Implement deepsleep()
  */
-void deepsleep(void) {
-    sleep();
+void hal_deepsleep(void) {
+    hal_sleep();
 }
--- a/targets/TARGET_NXP/TARGET_LPC81X/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC81X/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -20,7 +20,7 @@
 //#define DEEPSLEEP
 #define POWERDOWN
  
-void sleep(void) {  
+void hal_sleep(void) {  
     //Normal sleep mode for PCON:
     LPC_PMU->PCON &= ~0x03;
     
@@ -36,7 +36,7 @@
 //Deepsleep/powerdown modes assume the device is configured to use its internal RC oscillator directly
  
 #ifdef DEEPSLEEP
-void deepsleep(void) {
+void hal_deepsleep(void) {
     //Deep sleep in PCON
     LPC_PMU->PCON &= ~0x03;
     LPC_PMU->PCON |= 0x01;
@@ -59,7 +59,7 @@
 #endif
  
 #ifdef POWERDOWN
-void deepsleep(void) {
+void hal_deepsleep(void) {
     //Powerdown in PCON
     LPC_PMU->PCON &= ~0x03;
     LPC_PMU->PCON |= 0x02;
--- a/targets/TARGET_NXP/TARGET_LPC82X/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NXP/TARGET_LPC82X/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -20,7 +20,7 @@
 //#define DEEPSLEEP
 #define POWERDOWN
 
-void sleep(void)
+void hal_sleep(void)
 {
     //Normal sleep mode for PCON:
     LPC_PMU->PCON &= ~0x03;
@@ -34,7 +34,7 @@
 
 // Deepsleep/powerdown modes assume the device is configured to use its internal RC oscillator directly
  
-void deepsleep(void)
+void hal_deepsleep(void)
 {
     //Deep sleep in PCON
     LPC_PMU->PCON &= ~0x03;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_ONSEMI/TARGET_NCS36510/fncs36510_sleep.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,63 @@
+/**
+ *******************************************************************************
+ * @file    sleep.c
+ * @brief Implementation of a sleep functionality
+ * @internal
+ * @author  ON Semiconductor
+ * $Rev:  $
+ * $Date: $
+ ******************************************************************************
+ * Copyright 2016 Semiconductor Components Industries LLC (d/b/a “ON Semiconductor”).
+ * All rights reserved.  This software and/or documentation is licensed by ON Semiconductor
+ * under limited terms and conditions.  The terms and conditions pertaining to the software
+ * and/or documentation are available at http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf
+ * (“ON Semiconductor Standard Terms and Conditions of Sale, Section 8 Software”) and
+ * if applicable the software license agreement.  Do not use this software and/or
+ * documentation unless you have carefully read and you agree to the limited terms and
+ * conditions.  By using this software and/or documentation, you agree to the limited
+ * terms and conditions.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
+ * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
+ * ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL,
+ * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
+ * @endinternal
+ *
+ * @ingroup sleep
+ *
+ * @details
+ * Sleep implementation TBD - Dummy function is fine for first release
+ *
+ */
+
+#ifndef SLEEP_H_
+#define SLEEP_H_
+
+/* Orion specific includes */
+#include "types.h"
+#include "memory_map.h"
+#include "gpio_map.h"
+#include "pad_map.h"
+#include "crossbar.h"
+#include "clock.h"
+
+#define SLEEP_TYPE_NONE         0
+#define SLEEP_TYPE_SLEEP        1
+#define SLEEP_TYPE_DEEPSLEEP    2
+#define SLEEP_TYPE_COMA         3
+
+#define SLEEP_TYPE_DEFAULT SLEEP_TYPE_DEEPSLEEP
+
+#define SLEEP_DURATION_SLEEP_MIN        10          /* msec */
+#define SLEEP_DURATION_SLEEP_MAX        200         /* msec */
+#define SLEEP_DURATION_DEEPSLEEP_MAX    500         /* msec */
+#define SLEEP_DURATION_COMA_MAX         1000000000  /* TODO 1000 sec */
+
+void fncs36510_sleep(void);
+
+void fncs36510_deepsleep(void);
+
+void fncs36510_coma(void);
+
+#endif // SLEEP_H_
--- a/targets/TARGET_ONSEMI/TARGET_NCS36510/i2c_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ONSEMI/TARGET_NCS36510/i2c_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -31,7 +31,7 @@
 
 #include "i2c.h"
 #include "i2c_api.h"
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 
 #define I2C_READ_WRITE_BIT_MASK    0xFE
 
--- a/targets/TARGET_ONSEMI/TARGET_NCS36510/ncs36510_i2c.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ONSEMI/TARGET_NCS36510/ncs36510_i2c.c	Tue Mar 14 16:40:56 2017 +0000
@@ -60,7 +60,7 @@
  */
 #if DEVICE_I2C
 #include "i2c.h"
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 
 /* See i2c.h for details */
 void fI2cInit(i2c_t *obj,PinName sda,PinName scl)
--- a/targets/TARGET_ONSEMI/TARGET_NCS36510/ncs36510_lp_ticker_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ONSEMI/TARGET_NCS36510/ncs36510_lp_ticker_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -34,12 +34,10 @@
 #include "device.h"
 #if DEVICE_LOWPOWERTIMER
 
-#include "sleep_api.h"
 #include "cmsis_nvic.h"
 #include "lp_ticker_api.h"
 #include "rtc.h"
 #include "rtc_map.h"
-#include "sleep.h"
 
 /* Initialize the RTC for low power ticker */
 void lp_ticker_init()
--- a/targets/TARGET_ONSEMI/TARGET_NCS36510/ncs36510_trng_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ONSEMI/TARGET_NCS36510/ncs36510_trng_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -38,7 +38,7 @@
 #include "memory_map.h"
 #include "ncs36510_trng.h"
 #include "clock.h"
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 
 /*************************************************************************************************
 *                                                                                                *
--- a/targets/TARGET_ONSEMI/TARGET_NCS36510/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ONSEMI/TARGET_NCS36510/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -31,7 +31,7 @@
  *
  */
 #if DEVICE_SLEEP
-#include "sleep.h"
+#include "mbed_sleep.h"
 #include "sleep_api.h"
 #include "cmsis_nvic.h"
 
--- a/targets/TARGET_ONSEMI/TARGET_NCS36510/sleep.h	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,63 +0,0 @@
-/**
- *******************************************************************************
- * @file    sleep.c
- * @brief Implementation of a sleep functionality
- * @internal
- * @author  ON Semiconductor
- * $Rev:  $
- * $Date: $
- ******************************************************************************
- * Copyright 2016 Semiconductor Components Industries LLC (d/b/a “ON Semiconductor”).
- * All rights reserved.  This software and/or documentation is licensed by ON Semiconductor
- * under limited terms and conditions.  The terms and conditions pertaining to the software
- * and/or documentation are available at http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf
- * (“ON Semiconductor Standard Terms and Conditions of Sale, Section 8 Software”) and
- * if applicable the software license agreement.  Do not use this software and/or
- * documentation unless you have carefully read and you agree to the limited terms and
- * conditions.  By using this software and/or documentation, you agree to the limited
- * terms and conditions.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
- * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
- * ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL,
- * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
- * @endinternal
- *
- * @ingroup sleep
- *
- * @details
- * Sleep implementation TBD - Dummy function is fine for first release
- *
- */
-
-#ifndef SLEEP_H_
-#define SLEEP_H_
-
-/* Orion specific includes */
-#include "types.h"
-#include "memory_map.h"
-#include "gpio_map.h"
-#include "pad_map.h"
-#include "crossbar.h"
-#include "clock.h"
-
-#define SLEEP_TYPE_NONE         0
-#define SLEEP_TYPE_SLEEP        1
-#define SLEEP_TYPE_DEEPSLEEP    2
-#define SLEEP_TYPE_COMA         3
-
-#define SLEEP_TYPE_DEFAULT SLEEP_TYPE_DEEPSLEEP
-
-#define SLEEP_DURATION_SLEEP_MIN        10          /* msec */
-#define SLEEP_DURATION_SLEEP_MAX        200         /* msec */
-#define SLEEP_DURATION_DEEPSLEEP_MAX    500         /* msec */
-#define SLEEP_DURATION_COMA_MAX         1000000000  /* TODO 1000 sec */
-
-void fncs36510_sleep(void);
-
-void fncs36510_deepsleep(void);
-
-void fncs36510_coma(void);
-
-#endif // SLEEP_H_
--- a/targets/TARGET_ONSEMI/TARGET_NCS36510/sleep_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ONSEMI/TARGET_NCS36510/sleep_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -33,18 +33,19 @@
  */
 
 #if DEVICE_SLEEP
-#include "sleep.h"
+
+#include "fncs36510_sleep.h"
 #include "sleep_api.h"
 #include "cmsis_nvic.h"
 
-void sleep()
+void hal_sleep()
 {
     fncs36510_sleep();
 }
 
-void deepsleep()
+void hal_deepsleep()
 {
     fncs36510_deepsleep();
 }
 
-#endif /* DEVICE_SLEEP */
\ No newline at end of file
+#endif /* DEVICE_SLEEP */
--- a/targets/TARGET_RENESAS/TARGET_RZ_A1H/ethernet_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_RENESAS/TARGET_RZ_A1H/ethernet_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -17,7 +17,7 @@
 #include "ethernet_api.h"
 #include "cmsis.h"
 #include "mbed_interface.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 #include "mbed_error.h"
 #include "ether_iodefine.h"
 #include "ethernetext_api.h"
--- a/targets/TARGET_RENESAS/TARGET_VK_RZ_A1H/ethernet_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_RENESAS/TARGET_VK_RZ_A1H/ethernet_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -17,7 +17,7 @@
 #include "ethernet_api.h"
 #include "cmsis.h"
 #include "mbed_interface.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 #include "mbed_error.h"
 #include "ether_iodefine.h"
 #include "ethernetext_api.h"
--- a/targets/TARGET_STM/PinNamesTypes.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/PinNamesTypes.h	Tue Mar 14 16:40:56 2017 +0000
@@ -36,54 +36,118 @@
 extern "C" {
 #endif
 
-#define STM_PIN_DATA(MODE, PUPD, AFNUM)  ((int)(((MODE  & 0x0F) << 0) |\
-                                                ((PUPD  & 0x07) << 4) |\
-                                                ((AFNUM & 0x0F) << 7)))
+/*  STM PIN data as used in pin_function is coded on 32 bits as below
+ *   [2:0]  Function (like in MODER reg) : Input / Output / Alt / Analog 
+ *     [3]  Output Push-Pull / Open Drain (as in OTYPER reg)
+ *   [5:4]  as in PUPDR reg: No Pull, Pull-up, Pull-Donc
+ *   [7:6]  Reserved for speed config (as in OSPEEDR), but not used yet
+ *  [11:8]  Alternate Num (as in AFRL/AFRG reg)
+ * [16:12]  Channel (Analog/Timer specific)
+ *    [17]  Inverted (Analog/Timer specific)
+ *    [18]  Analog ADC control - Only valid for specific families
+ * [32:19]  Reserved
+ */
+
+#define STM_PIN_FUNCTION_MASK 0x07
+#define STM_PIN_FUNCTION_SHIFT 0
+#define STM_PIN_FUNCTION_BITS (STM_PIN_FUNCTION_MASK << STM_PIN_FUNCTION_SHIFT)
+
+#define STM_PIN_OD_MASK 0x01
+#define STM_PIN_OD_SHIFT 3
+#define STM_PIN_OD_BITS (STM_PIN_OD_MASK << STM_PIN_OD_SHIFT)
 
-#define STM_PIN_DATA_EXT(MODE, PUPD, AFNUM, CHANNEL, INVERTED)  ((int)(((MODE     & 0x0F) <<  0) |\
-                                                                       ((PUPD     & 0x07) <<  4) |\
-                                                                       ((AFNUM    & 0x0F) <<  7) |\
-                                                                       ((CHANNEL  & 0x1F) << 11) |\
-                                                                       ((INVERTED & 0x01) << 16)))
+#define STM_PIN_PUPD_MASK 0x03
+#define STM_PIN_PUPD_SHIFT 4
+#define STM_PIN_PUPD_BITS (STM_PIN_PUPD_MASK << STM_PIN_PUPD_SHIFT)
+
+#define STM_PIN_SPEED_MASK 0x03
+#define STM_PIN_SPEED_SHIFT 6
+#define STM_PIN_SPEED_BITS (STM_PIN_SPEED_MASK << STM_PIN_SPEED_SHIFT)
+
+#define STM_PIN_AFNUM_MASK 0x0F
+#define STM_PIN_AFNUM_SHIFT 8
+#define STM_PIN_AFNUM_BITS (STM_PIN_AFNUM_MASK << STM_PIN_AFNUM_SHIFT)
 
-#define STM_PIN_MODE(X)   (((X) >> 0) & 0x0F)
-#define STM_PIN_PUPD(X)   (((X) >> 4) & 0x07)
-#define STM_PIN_AFNUM(X)  (((X) >> 7) & 0x0F)
-#define STM_PIN_CHANNEL(X)  (((X) >> 11) & 0x1F)
-#define STM_PIN_INVERTED(X) (((X) >> 16) & 0x01)
+#define STM_PIN_CHAN_MASK 0x1F
+#define STM_PIN_CHAN_SHIFT 12
+#define STM_PIN_CHANNEL_BIT (STM_PIN_CHAN_MASK << STM_PIN_CHAN_SHIFT)
+
+#define STM_PIN_INV_MASK 0x01
+#define STM_PIN_INV_SHIFT 17
+#define STM_PIN_INV_BIT (STM_PIN_INV_MASK << STM_PIN_INV_SHIFT)
+
+#define STM_PIN_AN_CTRL_MASK 0x01
+#define STM_PIN_AN_CTRL_SHIFT 18
+#define STM_PIN_ANALOG_CONTROL_BIT (STM_PIN_AN_CTRL_MASK << STM_PIN_AN_CTRL_SHIFT)
 
-#define STM_MODE_INPUT              (0)
-#define STM_MODE_OUTPUT_PP          (1)
-#define STM_MODE_OUTPUT_OD          (2)
-#define STM_MODE_AF_PP              (3)
-#define STM_MODE_AF_OD              (4)
-#define STM_MODE_ANALOG             (5)
-#define STM_MODE_IT_RISING          (6)
-#define STM_MODE_IT_FALLING         (7)
-#define STM_MODE_IT_RISING_FALLING  (8)
-#define STM_MODE_EVT_RISING         (9)
-#define STM_MODE_EVT_FALLING        (10)
-#define STM_MODE_EVT_RISING_FALLING (11)
-#define STM_MODE_IT_EVT_RESET       (12)
-// The last mode is only valid for specific families, so we put it in the end
-#define STM_MODE_ANALOG_ADC_CONTROL (13)
+#define STM_PIN_FUNCTION(X)         (((X) >> STM_PIN_FUNCTION_SHIFT) & STM_PIN_FUNCTION_MASK)
+#define STM_PIN_OD(X)               (((X) >> STM_PIN_OD_SHIFT) & STM_PIN_OD_MASK)
+#define STM_PIN_PUPD(X)             (((X) >> STM_PIN_PUPD_SHIFT) & STM_PIN_PUPD_MASK)
+#define STM_PIN_SPEED(X)            (((X) >> STM_PIN_SPEED_SHIFT) & STM_PIN_SPEED_MASK)
+#define STM_PIN_AFNUM(X)            (((X) >> STM_PIN_AFNUM_SHIFT) & STM_PIN_AFNUM_MASK)
+#define STM_PIN_CHANNEL(X)          (((X) >> STM_PIN_CHAN_SHIFT) & STM_PIN_CHAN_MASK)
+#define STM_PIN_INVERTED(X)         (((X) >> STM_PIN_INV_SHIFT) & STM_PIN_INV_MASK)
+#define STM_PIN_ANALOG_CONTROL(X)   (((X) >> STM_PIN_AN_CTRL_SHIFT) & STM_PIN_AN_CTRL_MASK)
+
+#define STM_PIN_DEFINE(FUNC_OD, PUPD, AFNUM)  ((int)(FUNC_OD) |\
+                          ((PUPD  & STM_PIN_PUPD_MASK) << STM_PIN_PUPD_SHIFT) |\
+                          ((AFNUM & STM_PIN_AFNUM_MASK) << STM_PIN_AFNUM_SHIFT))
+
+#define STM_PIN_DEFINE_EXT(FUNC_OD, PUPD, AFNUM, CHAN, INV) \
+                                            ((int)(FUNC_OD) |\
+                       ((PUPD   & STM_PIN_PUPD_MASK) << STM_PIN_PUPD_SHIFT) |\
+                       ((AFNUM  & STM_PIN_AFNUM_MASK) << STM_PIN_AFNUM_SHIFT) |\
+                       ((CHAN   & STM_PIN_CHAN_MASK) << STM_PIN_CHAN_SHIFT) |\
+                       ((INV    & STM_PIN_INV_MASK) << STM_PIN_INV_SHIFT))
+
+/*
+ * MACROS to support the legacy definition of PIN formats
+ * The STM_MODE_ defines contain the function and the Push-pull/OpenDrain
+ * configuration (legacy inheritance).
+ */
+#define STM_PIN_DATA(FUNC_OD, PUPD, AFNUM) \
+            STM_PIN_DEFINE(FUNC_OD, PUPD, AFNUM)
+#define STM_PIN_DATA_EXT(FUNC_OD, PUPD, AFNUM, CHANNEL, INVERTED) \
+            STM_PIN_DEFINE_EXT(FUNC_OD, PUPD, AFNUM, CHANNEL, INVERTED)
+
+typedef enum {
+    STM_PIN_INPUT = 0,
+    STM_PIN_OUTPUT = 1,
+    STM_PIN_ALTERNATE = 2,
+    STM_PIN_ANALOG = 3,
+} StmPinFunction;
+
+#define STM_MODE_INPUT               (STM_PIN_INPUT)
+#define STM_MODE_OUTPUT_PP           (STM_PIN_OUTPUT)
+#define STM_MODE_OUTPUT_OD           (STM_PIN_OUTPUT | STM_PIN_OD_BITS)
+#define STM_MODE_AF_PP               (STM_PIN_ALTERNATE)
+#define STM_MODE_AF_OD               (STM_PIN_ALTERNATE | STM_PIN_OD_BITS)
+#define STM_MODE_ANALOG              (STM_PIN_ANALOG)
+#define STM_MODE_ANALOG_ADC_CONTROL  (STM_PIN_ANALOG | STM_PIN_ANALOG_CONTROL_BIT)
 
 // High nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, 6=G, 7=H)
 // Low nibble  = pin number
 #define STM_PORT(X) (((uint32_t)(X) >> 4) & 0xF)
 #define STM_PIN(X)  ((uint32_t)(X) & 0xF)
 
+/*  Defines to be used by application */
 typedef enum {
-    PIN_INPUT,
+    PIN_INPUT = 0,
     PIN_OUTPUT
 } PinDirection;
 
 typedef enum {
-    PullNone  = 0,
-    PullUp    = 1,
-    PullDown  = 2,
-    OpenDrain = 3,
-    PullDefault = PullNone
+    PullNone          = 0,
+    PullUp            = 1,
+    PullDown          = 2,
+    OpenDrainPullUp   = 3,
+    OpenDrainNoPull   = 4,
+    OpenDrainPullDown = 5,
+    PushPullNoPull    = PullNone,
+    PushPullPullUp    = PullUp,
+    PushPullPullDown  = PullDown,
+    OpenDrain         = OpenDrainPullUp,
+    PullDefault       = PullNone
 } PinMode;
 
 #ifdef __cplusplus
@@ -91,3 +155,4 @@
 #endif
 
 #endif
+
--- a/targets/TARGET_STM/TARGET_STM32F0/analogin_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F0/analogin_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -30,7 +30,7 @@
 
 #if DEVICE_ANALOGIN
 
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "cmsis.h"
 #include "pinmap.h"
 #include "PeripheralPins.h"
--- a/targets/TARGET_STM/TARGET_STM32F0/can_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,477 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2016 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "can_api.h"
-
-#if DEVICE_CAN
-
-#include "cmsis.h"
-#include "pinmap.h"
-#include "PeripheralPins.h"
-#include "mbed_error.h"
-#include <math.h>
-#include <string.h>
-
-#define CAN_NUM    1
-static CAN_HandleTypeDef CanHandle;
-static uint32_t can_irq_ids[CAN_NUM] = {0};
-static can_irq_handler irq_handler;
-
-void can_init(can_t *obj, PinName rd, PinName td) 
-{
-    CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
-    CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
-    obj->can = (CANName)pinmap_merge(can_rd, can_td);
-    MBED_ASSERT((int)obj->can != NC);    
-
-    if (obj->can == CAN_1) {
-        __HAL_RCC_CAN1_CLK_ENABLE();
-        obj->index = 0;
-    }
-
-    // Configure the CAN pins
-    pinmap_pinout(rd, PinMap_CAN_RD);
-    pinmap_pinout(td, PinMap_CAN_TD);
-    if (rd != NC) {
-        pin_mode(rd, PullUp);
-    }
-    if (td != NC) {
-        pin_mode(td, PullUp);
-    }
-
-    CanHandle.Instance = (CAN_TypeDef *)(obj->can);
-
-    CanHandle.Init.TTCM = DISABLE;
-    CanHandle.Init.ABOM = DISABLE;
-    CanHandle.Init.AWUM = DISABLE;
-    CanHandle.Init.NART = DISABLE;
-    CanHandle.Init.RFLM = DISABLE;
-    CanHandle.Init.TXFP = DISABLE;
-    CanHandle.Init.Mode = CAN_MODE_NORMAL;
-    CanHandle.Init.SJW = CAN_SJW_1TQ;
-    CanHandle.Init.BS1 = CAN_BS1_6TQ;
-    CanHandle.Init.BS2 = CAN_BS2_8TQ;
-    CanHandle.Init.Prescaler = 2;
-
-    if (HAL_CAN_Init(&CanHandle) != HAL_OK) {
-       error("Cannot initialize CAN");
-    }
-    // Set initial CAN frequency to 100kb/s
-    can_frequency(obj, 100000);
-
-    can_filter(obj, 0, 0, CANStandard, 0);
-}
-
-void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id) 
-{
-    irq_handler = handler;
-    can_irq_ids[obj->index] = id;    
-}
-
-void can_irq_free(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-  
-    can->IER &= ~(CAN_IT_FMP0 | CAN_IT_FMP1 | CAN_IT_TME | \
-                  CAN_IT_ERR | CAN_IT_EPV | CAN_IT_BOF);
-    can_irq_ids[obj->can] = 0;
-}
-
-void can_free(can_t *obj) 
-{
-    // Reset CAN and disable clock
-    if (obj->can == CAN_1) {
-        __HAL_RCC_CAN1_FORCE_RESET();
-        __HAL_RCC_CAN1_RELEASE_RESET();
-        __HAL_RCC_CAN1_CLK_DISABLE();
-    }
-}
-
-// The following table is used to program bit_timing. It is an adjustment of the sample
-// point by synchronizing on the start-bit edge and resynchronizing on the following edges.
-// This table has the sampling points as close to 75% as possible (most commonly used).
-// The first value is TSEG1, the second TSEG2.
-static const int timing_pts[23][2] = {
-    {0x0, 0x0},      // 2,  50%
-    {0x1, 0x0},      // 3,  67%
-    {0x2, 0x0},      // 4,  75%
-    {0x3, 0x0},      // 5,  80%
-    {0x3, 0x1},      // 6,  67%
-    {0x4, 0x1},      // 7,  71%
-    {0x5, 0x1},      // 8,  75%
-    {0x6, 0x1},      // 9,  78%
-    {0x6, 0x2},      // 10, 70%
-    {0x7, 0x2},      // 11, 73%
-    {0x8, 0x2},      // 12, 75%
-    {0x9, 0x2},      // 13, 77%
-    {0x9, 0x3},      // 14, 71%
-    {0xA, 0x3},      // 15, 73%
-    {0xB, 0x3},      // 16, 75%
-    {0xC, 0x3},      // 17, 76%
-    {0xD, 0x3},      // 18, 78%
-    {0xD, 0x4},      // 19, 74%
-    {0xE, 0x4},      // 20, 75%
-    {0xF, 0x4},      // 21, 76%
-    {0xF, 0x5},      // 22, 73%
-    {0xF, 0x6},      // 23, 70%
-    {0xF, 0x7},      // 24, 67%
-};
-
-static unsigned int can_speed(unsigned int pclk, unsigned int cclk, unsigned char psjw) 
-{
-    uint32_t    btr;
-    uint16_t    brp = 0;
-    uint32_t    calcbit;
-    uint32_t    bitwidth;
-    int         hit = 0;
-    int         bits;
-    
-    bitwidth = (pclk / cclk);
-    
-    brp = bitwidth / 0x18;
-    while ((!hit) && (brp < bitwidth / 4)) {
-        brp++;
-        for (bits = 22; bits > 0; bits--) {
-            calcbit = (bits + 3) * (brp + 1);
-            if (calcbit == bitwidth) {
-                hit = 1;
-                break;
-            }
-        }
-    }
-    
-    if (hit) {
-        btr = ((timing_pts[bits][1] << 20) & 0x00700000)
-            | ((timing_pts[bits][0] << 16) & 0x000F0000)
-            | ((psjw                << 24) & 0x0000C000)
-            | ((brp                 <<  0) & 0x000003FF);
-    } else {
-        btr = 0xFFFFFFFF;
-    }
-    
-    return btr;
-
-}
-
-int can_frequency(can_t *obj, int f) 
-{
-    int pclk = HAL_RCC_GetPCLK1Freq();
-    int btr = can_speed(pclk, (unsigned int)f, 1);
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    if (btr > 0) {
-        can->MCR |= CAN_MCR_INRQ ;
-        while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-        }
-        can->BTR = btr;
-        can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-        while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-        }
-        return 1;
-    } else {
-        return 0;
-    }
-}
-
-int can_write(can_t *obj, CAN_Message msg, int cc) 
-{
-    uint32_t  transmitmailbox = 5;
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    /* Select one empty transmit mailbox */
-    if ((can->TSR&CAN_TSR_TME0) == CAN_TSR_TME0) {
-        transmitmailbox = 0;
-    } else if ((can->TSR&CAN_TSR_TME1) == CAN_TSR_TME1) {
-        transmitmailbox = 1;
-    } else if ((can->TSR&CAN_TSR_TME2) == CAN_TSR_TME2) {
-        transmitmailbox = 2;
-    } else {
-        transmitmailbox = CAN_TXSTATUS_NOMAILBOX;
-    }
-
-    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) |
-                                             ((uint32_t)msg.data[4]));
-    /* Request transmission */
-    can->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ;
-  }
-
-  return 1;
-}
-
-int can_read(can_t *obj, CAN_Message *msg, int handle) 
-{
-    //handle is the FIFO number
-
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    
-    // check FPM0 which holds the pending message count in FIFO 0
-    // if no message is pending, return 0
-    if ((can->RF0R & CAN_RF0R_FMP0) == 0) {
-        return 0;
-    }
-
-    /* Get the Id */
-    msg->format = (CANFormat)((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR);
-    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);
-    /* Get the DLC */
-    msg->len = (uint8_t)0x0F & can->sFIFOMailBox[handle].RDTR;
-//  /* Get the FMI */
-//  msg->FMI = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDTR >> 8);
-    /* Get the data field */
-    msg->data[0] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDLR;
-    msg->data[1] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 8);
-    msg->data[2] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 16);
-    msg->data[3] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 24);
-    msg->data[4] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDHR;
-    msg->data[5] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 8);
-    msg->data[6] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 16);
-    msg->data[7] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 24);
-    
-    /* Release the FIFO */
-    if (handle == CAN_FIFO0) {
-        /* Release FIFO0 */
-        can->RF0R |= CAN_RF0R_RFOM0;
-    } else { /* FIFONumber == CAN_FIFO1 */
-      /* Release FIFO1 */
-      can->RF1R |= CAN_RF1R_RFOM1;
-    }
-
-    return 1;
-}
-
-void can_reset(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    can->MCR |= CAN_MCR_RESET;  
-    can->ESR = 0x0;
-}
-
-unsigned char can_rderror(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    return (can->ESR >> 24) & 0xFF;
-}
-
-unsigned char can_tderror(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    return (can->ESR >> 16) & 0xFF;
-}
-
-void can_monitor(can_t *obj, int silent) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);        
-    
-    can->MCR |= CAN_MCR_INRQ ;
-    while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-    }
-    if (silent) {
-        can->BTR |= ((uint32_t)1 << 31);
-    } else {
-        can->BTR &= ~((uint32_t)1 << 31);
-    }
-    can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-    while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-    }
-}
-
-int can_mode(can_t *obj, CanMode mode) 
-{
-    int success = 0;
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-    can->MCR |= CAN_MCR_INRQ ;
-    while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-    }
-    switch (mode) {
-        case MODE_NORMAL:
-            can->BTR &= ~(CAN_BTR_SILM | CAN_BTR_LBKM);
-            success = 1;
-            break;
-        case MODE_SILENT:
-            can->BTR |= CAN_BTR_SILM;
-            can->BTR &= ~CAN_BTR_LBKM;
-            success = 1;
-            break;
-        case MODE_TEST_GLOBAL:
-        case MODE_TEST_LOCAL:
-            can->BTR |= CAN_BTR_LBKM;
-            can->BTR &= ~CAN_BTR_SILM;
-            success = 1;
-            break;
-        case MODE_TEST_SILENT:
-            can->BTR |= (CAN_BTR_SILM | CAN_BTR_LBKM);
-            success = 1;
-            break;
-        default:
-            success = 0;
-            break;
-    }
-    can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-    while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-    }
-    return success;
-}
-
-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);
-        CAN_FilterConfTypeDef  sFilterConfig;
-        sFilterConfig.FilterNumber = handle;
-        sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
-        sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
-
-        if (format == CANStandard) {
-            sFilterConfig.FilterIdHigh = id << 5;
-            sFilterConfig.FilterIdLow =  0x0;
-            sFilterConfig.FilterMaskIdHigh = mask << 5;
-            sFilterConfig.FilterMaskIdLow = 0x0;	// allows both remote and data frames
-        } else if (format == CANExtended) {
-            sFilterConfig.FilterIdHigh = id >> 13; 	// EXTID[28:13]
-            sFilterConfig.FilterIdLow = (0x00FF & (id << 3)) | (1 << 2);  // EXTID[12:0]
-            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;
-    }
-    return retval;
-}
-
-static void can_irq(CANName name, int id) 
-{
-    uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0;    
-    CanHandle.Instance = (CAN_TypeDef *)name;
-    
-    if (__HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_TME)) {
-        tmp1 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_0);
-        tmp2 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_1);
-        tmp3 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_2);
-        if(tmp1 || tmp2 || tmp3)  
-        {
-            irq_handler(can_irq_ids[id], IRQ_TX);
-        }
-    }
-  
-    tmp1 = __HAL_CAN_MSG_PENDING(&CanHandle, CAN_FIFO0);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_FMP0);
-  
-    if ((tmp1 != 0) && tmp2) {
-         irq_handler(can_irq_ids[id], IRQ_RX);
-    }
-  
-    tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_EPV);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_EPV);
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR); 
-  
-    if (tmp1 && tmp2 && tmp3) {
-         irq_handler(can_irq_ids[id], IRQ_PASSIVE);
-    }
-  
-    tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_BOF);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_BOF);
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);  
-    if (tmp1 && tmp2 && tmp3) {
-        irq_handler(can_irq_ids[id], IRQ_BUS);
-    }
-  
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);  
-    if (tmp1 && tmp2 && tmp3) {
-        irq_handler(can_irq_ids[id], IRQ_ERROR);
-    }  
-}
-
-void CAN_IRQHandler(void)
-{
-    can_irq(CAN_1, 0);
-}
-
-void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
-{
-
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-    IRQn_Type irq_n = (IRQn_Type)0;
-    uint32_t vector = 0;    
-    uint32_t ier;
-
-    if (obj->can == CAN_1) {
-        switch (type) {
-            case IRQ_RX:
-                ier = CAN_IT_FMP0;
-                break;
-            case IRQ_TX:      
-                ier = CAN_IT_TME;
-                break;
-            case IRQ_ERROR:   
-                ier = CAN_IT_ERR;
-                break;
-            case IRQ_PASSIVE:
-                ier = CAN_IT_EPV;
-                break;
-            case IRQ_BUS:
-                ier = CAN_IT_BOF;
-                break;
-            default: return;
-        }
-        irq_n = CEC_CAN_IRQn;
-        vector = (uint32_t)&CAN_IRQHandler;
-    } 
-
-    if (enable) {
-        can->IER |= ier;
-    } else {
-        can->IER &= ~ier;
-    }
-
-    NVIC_SetVector(irq_n, vector);
-    NVIC_EnableIRQ(irq_n);
-}
-
-#endif // DEVICE_CAN
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F0/can_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,42 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef MBED_CAN_DEVICE_H
+#define MBED_CAN_DEVICE_H
+
+#include "cmsis.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef DEVICE_CAN
+
+#define CAN_NUM    1 // Number of CAN peripherals present in the STM32 serie
+
+#define CAN1_IRQ_RX_IRQN        CEC_CAN_IRQn
+#define CAN1_IRQ_RX_VECT        CAN_IRQHandler
+#define CAN1_IRQ_TX_IRQN        CEC_CAN_IRQn
+#define CAN1_IRQ_TX_VECT        CAN_IRQHandler
+#define CAN1_IRQ_ERROR_IRQN     CEC_CAN_IRQn
+#define CAN1_IRQ_ERROR_VECT     CAN_IRQHandler
+#define CAN1_IRQ_PASSIVE_IRQN   CEC_CAN_IRQn
+#define CAN1_IRQ_PASSIVE_VECT   CAN_IRQHandler
+#define CAN1_IRQ_BUS_IRQN       CEC_CAN_IRQn
+#define CAN1_IRQ_BUS_VECT       CAN_IRQHandler
+
+#endif // DEVICE_CAN
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F0/gpio_irq_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,270 +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 <stddef.h>
-#include "cmsis.h"
-#include "gpio_irq_api.h"
-#include "pinmap.h"
-#include "mbed_error.h"
-
-#define EDGE_NONE (0)
-#define EDGE_RISE (1)
-#define EDGE_FALL (2)
-#define EDGE_BOTH (3)
-
-// Number of EXTI irq vectors (EXTI0_1, EXTI2_3, EXTI4_15)
-#define CHANNEL_NUM (3)
-
-// Max pins for one line (max with EXTI4_15)
-#define MAX_PIN_LINE (12)
-
-typedef struct gpio_channel {
-    uint32_t pin_mask;                   // bitmask representing which pins are configured for receiving interrupts
-    uint32_t channel_ids[MAX_PIN_LINE];  // mbed "gpio_irq_t gpio_irq" field of instance
-    uint32_t channel_gpio[MAX_PIN_LINE]; // base address of gpio port group
-    uint32_t channel_pin[MAX_PIN_LINE];  // pin number in port group
-} gpio_channel_t;
-
-static gpio_channel_t channels[CHANNEL_NUM] = {
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0}
-};
-
-// Used to return the index for channels array.
-static uint32_t pin_base_nr[16] = {
-    // EXTI0_1
-    0, // pin 0
-    1, // pin 1
-    // EXTI2_3
-    0, // pin 2
-    1, // pin 3
-    // EXTI4_15
-    0, // pin 4
-    1, // pin 5
-    2, // pin 6
-    3, // pin 7
-    4, // pin 8
-    5, // pin 9
-    6, // pin 10
-    7, // pin 11
-    8, // pin 12
-    9, // pin 13
-   10, // pin 14
-   11  // pin 15
-};
-
-static gpio_irq_handler irq_handler;
-
-static void handle_interrupt_in(uint32_t irq_index, uint32_t max_num_pin_line)
-{
-    gpio_channel_t *gpio_channel = &channels[irq_index];
-    uint32_t gpio_idx;
-
-    for (gpio_idx = 0; gpio_idx < max_num_pin_line; gpio_idx++) {
-        uint32_t current_mask = (1 << gpio_idx);
-
-        if (gpio_channel->pin_mask & current_mask) {
-            // Retrieve the gpio and pin that generate the irq
-            GPIO_TypeDef *gpio = (GPIO_TypeDef *)(gpio_channel->channel_gpio[gpio_idx]);
-            uint32_t pin = (uint32_t)(1 << (gpio_channel->channel_pin[gpio_idx]));
-
-            // Clear interrupt flag
-            if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) {
-                __HAL_GPIO_EXTI_CLEAR_FLAG(pin);
-
-                if (gpio_channel->channel_ids[gpio_idx] == 0) continue;
-
-                // Check which edge has generated the irq
-                if ((gpio->IDR & pin) == 0) {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_FALL);
-                } else  {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_RISE);
-                }
-            }
-        }
-    }
-}
-
-// EXTI lines 0 to 1
-static void gpio_irq0(void)
-{
-    handle_interrupt_in(0, 2);
-}
-
-// EXTI lines 2 to 3
-static void gpio_irq1(void)
-{
-    handle_interrupt_in(1, 2);
-}
-
-// EXTI lines 4 to 15
-static void gpio_irq2(void)
-{
-    handle_interrupt_in(2, 12);
-}
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
-
-int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
-{
-    IRQn_Type irq_n = (IRQn_Type)0;
-    uint32_t vector = 0;
-    uint32_t irq_index;
-    gpio_channel_t *gpio_channel;
-    uint32_t gpio_idx;
-
-    if (pin == NC) return -1;
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Select irq number and interrupt routine
-    if ((pin_index == 0) || (pin_index == 1)) {
-        irq_n = EXTI0_1_IRQn;
-        vector = (uint32_t)&gpio_irq0;
-        irq_index = 0;
-    } else if ((pin_index == 2) || (pin_index == 3)) {
-        irq_n = EXTI2_3_IRQn;
-        vector = (uint32_t)&gpio_irq1;
-        irq_index = 1;
-    } else if ((pin_index > 3) && (pin_index < 16)) {
-        irq_n = EXTI4_15_IRQn;
-        vector = (uint32_t)&gpio_irq2;
-        irq_index = 2;
-    } else {
-        error("InterruptIn error: pin not supported.\n");
-        return -1;
-    }
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-
-    // Configure GPIO
-    pin_function(pin, STM_PIN_DATA(STM_MODE_IT_FALLING, GPIO_NOPULL, 0));
-
-    // Enable EXTI interrupt
-    NVIC_SetVector(irq_n, vector);
-    NVIC_EnableIRQ(irq_n);
-
-    // Save informations for future use
-    obj->irq_n = irq_n;
-    obj->irq_index = irq_index;
-    obj->event = EDGE_NONE;
-    obj->pin = pin;
-
-    gpio_channel = &channels[irq_index];
-    gpio_idx = pin_base_nr[pin_index];
-    gpio_channel->pin_mask |= (1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = id;
-    gpio_channel->channel_gpio[gpio_idx] = gpio_add;
-    gpio_channel->channel_pin[gpio_idx] = pin_index;
-
-    irq_handler = handler;
-
-    return 0;
-}
-
-void gpio_irq_free(gpio_irq_t *obj)
-{
-    gpio_channel_t *gpio_channel = &channels[obj->irq_index];
-    uint32_t pin_index  = STM_PIN(obj->pin);
-    uint32_t gpio_addr = GPIOA_BASE + (GPIOB_BASE-GPIOA_BASE) * STM_PORT(obj->pin);
-    uint32_t gpio_idx = pin_base_nr[pin_index];
-    
-    HAL_GPIO_DeInit((GPIO_TypeDef *)gpio_addr, (1<<pin_index));
-    gpio_channel->pin_mask &= ~(1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = 0;
-    gpio_channel->channel_gpio[gpio_idx] = 0;
-    gpio_channel->channel_pin[gpio_idx] = 0;
-
-    // Disable EXTI line, but don't change pull-up config
-    pin_function_gpiomode(obj->pin, STM_MODE_INPUT);
-    obj->event = EDGE_NONE;
-}
-
-void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
-{
-    uint32_t mode = STM_MODE_IT_EVT_RESET;
-    uint32_t pull = GPIO_NOPULL;
-
-    if (enable) {
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or RISE
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or FALL
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            }
-        }
-    } else { // Disable
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            } else { // NONE or RISE
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            } else { // NONE or FALL
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-    }
-
-    pin_function_gpiomode(obj->pin, mode);
-}
-
-void gpio_irq_enable(gpio_irq_t *obj)
-{
-    NVIC_EnableIRQ(obj->irq_n);
-}
-
-void gpio_irq_disable(gpio_irq_t *obj)
-{
-    NVIC_DisableIRQ(obj->irq_n);
-    obj->event = EDGE_NONE;
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F0/gpio_irq_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,55 @@
+/* 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 "gpio_irq_device.h"
+
+// Used to return the index for channels array.
+const exti_lines_t pin_lines_desc[16] = {
+    // EXTI0_1
+    {.gpio_idx = 0, .irq_index = 0, .irq_n = EXTI0_1_IRQn}, // pin 0
+    {.gpio_idx = 1, .irq_index = 0, .irq_n = EXTI0_1_IRQn}, // pin 1
+    // EXTI2_3
+    {.gpio_idx = 0, .irq_index = 1, .irq_n = EXTI2_3_IRQn}, // pin 2
+    {.gpio_idx = 1, .irq_index = 1, .irq_n = EXTI2_3_IRQn}, // pin 3
+    // EXTI4_15
+    {.gpio_idx = 0, .irq_index = 2, .irq_n = EXTI4_15_IRQn}, // pin 4
+    {.gpio_idx = 1, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 5
+    {.gpio_idx = 2, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 6
+    {.gpio_idx = 3, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 7
+    {.gpio_idx = 4, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 8
+    {.gpio_idx = 5, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 9
+    {.gpio_idx = 6, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 10
+    {.gpio_idx = 7, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 11
+    {.gpio_idx = 8, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 12
+    {.gpio_idx = 9, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 13
+    {.gpio_idx = 10, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 14
+    {.gpio_idx = 11, .irq_index = 2, .irq_n = EXTI4_15_IRQn}// pin 15
+};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F0/gpio_irq_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -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.
+ *******************************************************************************
+ */
+#ifndef MBED_GPIO_IRQ_DEVICE_H
+#define MBED_GPIO_IRQ_DEVICE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "stm32f0xx_ll_exti.h"
+
+// Number of EXTI irq vectors (EXTI0_1, EXTI2_3, EXTI4_15)
+#define CHANNEL_NUM (3)
+
+#define EXTI_IRQ0_NUM_LINES 2
+#define EXTI_IRQ1_NUM_LINES 2
+#define EXTI_IRQ2_NUM_LINES 12
+// Max pins for one line (max with EXTI4_15)
+#define MAX_PIN_LINE (EXTI_IRQ2_NUM_LINES)
+
+/*  Structure to describe how the HW EXTI lines are defined in this HW */
+typedef struct exti_lines {
+    uint32_t gpio_idx;   // an index entry for each EXIT line
+    uint32_t irq_index;  // the IRQ index
+    IRQn_Type  irq_n;    // the corresponding EXTI IRQn
+} exti_lines_t;
+
+// Used to return the index for channels array.
+extern const exti_lines_t pin_lines_desc[];
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F0/pin_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,69 @@
+/* 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.
+ *******************************************************************************
+ */
+#ifndef MBED_PIN_DEVICE_H
+#define MBED_PIN_DEVICE_H
+
+#include "cmsis.h"
+#include "stm32f0xx_ll_gpio.h"
+
+extern const uint32_t ll_pin_defines[16];
+
+/* Family specific implementations */
+static inline void stm_pin_DisconnectDebug(PinName pin)
+{
+    /* empty for now */
+}
+
+static inline void stm_pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t pull_config)
+{
+    switch (pull_config) {
+        case GPIO_PULLUP:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_UP);
+            break;
+        case GPIO_PULLDOWN:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_DOWN);
+            break;
+        default:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_NO);
+            break;
+    }
+}
+
+static inline void stm_pin_SetAFPin( GPIO_TypeDef *gpio, PinName pin, uint32_t afnum)
+{
+    uint32_t ll_pin  = ll_pin_defines[STM_PIN(pin)];
+
+    if (STM_PIN(pin) > 7)
+        LL_GPIO_SetAFPin_8_15(gpio, ll_pin, afnum);
+    else
+        LL_GPIO_SetAFPin_0_7(gpio, ll_pin, afnum);
+}
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F0/pinmap.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,157 +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 "mbed_assert.h"
-#include "pinmap.h"
-#include "PortNames.h"
-#include "mbed_error.h"
-
-// GPIO mode look-up table
-static const uint32_t gpio_mode[13] = {
-    0x00000000, //  0 = GPIO_MODE_INPUT
-    0x00000001, //  1 = GPIO_MODE_OUTPUT_PP
-    0x00000011, //  2 = GPIO_MODE_OUTPUT_OD
-    0x00000002, //  3 = GPIO_MODE_AF_PP
-    0x00000012, //  4 = GPIO_MODE_AF_OD
-    0x00000003, //  5 = GPIO_MODE_ANALOG
-    0x10110000, //  6 = GPIO_MODE_IT_RISING
-    0x10210000, //  7 = GPIO_MODE_IT_FALLING
-    0x10310000, //  8 = GPIO_MODE_IT_RISING_FALLING
-    0x10120000, //  9 = GPIO_MODE_EVT_RISING
-    0x10220000, // 10 = GPIO_MODE_EVT_FALLING
-    0x10320000, // 11 = GPIO_MODE_EVT_RISING_FALLING
-    0x10000000  // 12 = Reset IT and EVT (not in STM32Cube HAL)
-};
-
-// Enable GPIO clock and return GPIO base address
-uint32_t Set_GPIO_Clock(uint32_t port_idx) {
-    uint32_t gpio_add = 0;
-    switch (port_idx) {
-        case PortA:
-            gpio_add = GPIOA_BASE;
-            __GPIOA_CLK_ENABLE();
-            break;
-        case PortB:
-            gpio_add = GPIOB_BASE;
-            __GPIOB_CLK_ENABLE();
-            break;
-#if defined(GPIOC_BASE)
-        case PortC:
-            gpio_add = GPIOC_BASE;
-            __GPIOC_CLK_ENABLE();
-            break;
-#endif
-#if defined(GPIOD_BASE)
-        case PortD:
-            gpio_add = GPIOD_BASE;
-            __GPIOD_CLK_ENABLE();
-            break;
-#endif
-#if defined(GPIOF_BASE)
-        case PortF:
-            gpio_add = GPIOF_BASE;
-            __GPIOF_CLK_ENABLE();
-            break;
-#endif
-        default:
-            error("Pinmap error: wrong port number.");
-            break;
-    }
-    return gpio_add;
-}
-
-/**
- * Configure pin (mode, speed, output type and pull-up/pull-down)
- */
-void pin_function(PinName pin, int data) {
-    MBED_ASSERT(pin != (PinName)NC);
-
-    // Get the pin informations
-    uint32_t mode  = STM_PIN_MODE(data);
-    uint32_t pupd  = STM_PIN_PUPD(data);
-    uint32_t afnum = STM_PIN_AFNUM(data);
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Configure GPIO
-    GPIO_InitTypeDef GPIO_InitStructure;
-    GPIO_InitStructure.Pin       = (uint32_t)(1 << pin_index);
-    GPIO_InitStructure.Mode      = gpio_mode[mode];
-    GPIO_InitStructure.Pull      = pupd;
-    GPIO_InitStructure.Speed     = GPIO_SPEED_HIGH;
-    GPIO_InitStructure.Alternate = afnum;
-    HAL_GPIO_Init(gpio, &GPIO_InitStructure);
-
-    // [TODO] Disconnect SWDIO and SWCLK signals ?
-    // Warning: For debugging it is necessary to reconnect under reset if this is done.
-    //if ((pin == PA_13) || (pin == PA_14)) {
-    //
-    //}
-}
-
-/**
- * Configure pin pull-up/pull-down
- */
-void pin_mode(PinName pin, PinMode mode) {
-    MBED_ASSERT(pin != (PinName)NC);
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Configure pull-up/pull-down resistors
-    uint32_t pupd = (uint32_t)mode;
-    if (pupd > 2) pupd = 0; // Open-drain = No pull-up/No pull-down
-    gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2)));
-    gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
-}
-
-/*  Internal function for setting the gpiomode/function
- *  without changing Pull mode
- */
-void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
-
-    /* Read current pull state from HW to avoid over-write*/
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
-    uint32_t temp = gpio->PUPDR;
-    uint32_t pull = (temp  >> (pin_index * 2U)) & GPIO_PUPDR_PUPDR0;
-
-    /* Then re-use global function for updating the mode part*/
-    pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
-}
--- a/targets/TARGET_STM/TARGET_STM32F0/port_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,97 +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 "port_api.h"
-#include "pinmap.h"
-#include "gpio_api.h"
-#include "mbed_error.h"
-
-#if DEVICE_PORTIN || DEVICE_PORTOUT
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-
-// high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
-// low nibble  = pin number
-PinName port_pin(PortName port, int pin_n) {
-    return (PinName)(pin_n + (port << 4));
-}
-
-void port_init(port_t *obj, PortName port, int mask, PinDirection dir) {
-    uint32_t port_index = (uint32_t)port;
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Fill PORT object structure for future use
-    obj->port      = port;
-    obj->mask      = mask;
-    obj->direction = dir;
-    obj->reg_in    = &gpio->IDR;
-    obj->reg_out   = &gpio->ODR;
-
-    port_dir(obj, dir);
-}
-
-void port_dir(port_t *obj, PinDirection dir) {
-    uint32_t i;
-    obj->direction = dir;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            if (dir == PIN_OUTPUT) {
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
-            } else { // PIN_INPUT
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
-            }
-        }
-    }
-}
-
-void port_mode(port_t *obj, PinMode mode) {
-    uint32_t i;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            pin_mode(port_pin(obj->port, i), mode);
-        }
-    }
-}
-
-void port_write(port_t *obj, int value) {
-    *obj->reg_out = (*obj->reg_out & ~obj->mask) | (value & obj->mask);
-}
-
-int port_read(port_t *obj) {
-    if (obj->direction == PIN_OUTPUT) {
-        return (*obj->reg_out & obj->mask);
-    } else { // PIN_INPUT
-        return (*obj->reg_in & obj->mask);
-    }
-}
-
-#endif
--- a/targets/TARGET_STM/TARGET_STM32F0/pwmout_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,59 @@
+/* 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
+
+extern const pwm_apb_map_t pwm_apb_map_table[];
+
+#endif // DEVICE_PWMOUT
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F1/TARGET_BLUEPILL_F103C8/PeripheralPins.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F1/TARGET_BLUEPILL_F103C8/PeripheralPins.c	Tue Mar 14 16:40:56 2017 +0000
@@ -167,8 +167,8 @@
 };
 
 const PinMap PinMap_CAN_RD[] = {
-    {PA_11, CAN_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, 0)},
-    {PB_8 , CAN_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, 1)},
+    {PA_11, CAN_1, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)},
+    {PB_8 , CAN_1, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 1)},
     {NC,    NC,    0}
 };
 
--- a/targets/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB/PeripheralPins.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB/PeripheralPins.c	Tue Mar 14 16:40:56 2017 +0000
@@ -181,8 +181,8 @@
 };
 
 const PinMap PinMap_CAN_RD[] = {
-    {PA_11, CAN_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, 0)},
-    {PB_8 , CAN_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, 10)}, // Remap CAN_RX to PB_8
+    {PA_11, CAN_1, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)},
+    {PB_8 , CAN_1, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 10)}, // Remap CAN_RX to PB_8
     {NC,    NC,    0}
 };
 
--- a/targets/TARGET_STM/TARGET_STM32F1/analogin_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F1/analogin_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -30,7 +30,7 @@
 
 #if DEVICE_ANALOGIN
 
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "cmsis.h"
 #include "pinmap.h"
 #include "PeripheralPins.h"
--- a/targets/TARGET_STM/TARGET_STM32F1/can_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,494 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2016 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "can_api.h"
-
-#if DEVICE_CAN
-
-#include "cmsis.h"
-#include "pinmap.h"
-#include "PeripheralPins.h"
-#include "mbed_error.h"
-#include <math.h>
-#include <string.h>
-
-#define CAN_NUM    1
-static CAN_HandleTypeDef CanHandle;
-static uint32_t can_irq_ids[CAN_NUM] = {0};
-static can_irq_handler irq_handler;
-
-void can_init(can_t *obj, PinName rd, PinName td) 
-{
-    CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
-    CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
-    obj->can = (CANName)pinmap_merge(can_rd, can_td);
-    MBED_ASSERT((int)obj->can != NC);    
-
-    if(obj->can == CAN_1) {
-        __HAL_RCC_CAN1_CLK_ENABLE();
-        obj->index = 0;
-    }
-
-    // Configure the CAN pins
-    pinmap_pinout(rd, PinMap_CAN_RD);
-    pinmap_pinout(td, PinMap_CAN_TD);
-    if (rd != NC) {
-        pin_mode(rd, PullUp);
-    }
-    if (td != NC) {
-        pin_mode(td, PullUp);
-    }
-
-    CanHandle.Instance = (CAN_TypeDef *)(obj->can);
-
-    CanHandle.Init.TTCM = DISABLE;
-    CanHandle.Init.ABOM = DISABLE;
-    CanHandle.Init.AWUM = DISABLE;
-    CanHandle.Init.NART = DISABLE;
-    CanHandle.Init.RFLM = DISABLE;
-    CanHandle.Init.TXFP = DISABLE;
-    CanHandle.Init.Mode = CAN_MODE_NORMAL;
-    CanHandle.Init.SJW = CAN_SJW_1TQ;
-    CanHandle.Init.BS1 = CAN_BS1_6TQ;
-    CanHandle.Init.BS2 = CAN_BS2_8TQ;
-    CanHandle.Init.Prescaler = 2;
-
-    if (HAL_CAN_Init(&CanHandle) != HAL_OK) {
-       error("Cannot initialize CAN");
-    }
-    // Set initial CAN frequency to 100kb/s
-    can_frequency(obj, 100000);
-
-    can_filter(obj, 0, 0, CANStandard, 0);
-}
-
-void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id) 
-{
-    irq_handler = handler;
-    can_irq_ids[obj->index] = id;    
-}
-
-void can_irq_free(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-  
-    can->IER &= ~(CAN_IT_FMP0 | CAN_IT_FMP1 | CAN_IT_TME | \
-                  CAN_IT_ERR | CAN_IT_EPV | CAN_IT_BOF);
-    can_irq_ids[obj->can] = 0;
-}
-
-void can_free(can_t *obj) 
-{
-    // Reset CAN and disable clock
-    if (obj->can == CAN_1) {
-        __HAL_RCC_CAN1_FORCE_RESET();
-        __HAL_RCC_CAN1_RELEASE_RESET();
-        __HAL_RCC_CAN1_CLK_DISABLE();
-    }
-}
-
-// The following table is used to program bit_timing. It is an adjustment of the sample
-// point by synchronizing on the start-bit edge and resynchronizing on the following edges.
-// This table has the sampling points as close to 75% as possible (most commonly used).
-// The first value is TSEG1, the second TSEG2.
-static const int timing_pts[23][2] = {
-    {0x0, 0x0},      // 2,  50%
-    {0x1, 0x0},      // 3,  67%
-    {0x2, 0x0},      // 4,  75%
-    {0x3, 0x0},      // 5,  80%
-    {0x3, 0x1},      // 6,  67%
-    {0x4, 0x1},      // 7,  71%
-    {0x5, 0x1},      // 8,  75%
-    {0x6, 0x1},      // 9,  78%
-    {0x6, 0x2},      // 10, 70%
-    {0x7, 0x2},      // 11, 73%
-    {0x8, 0x2},      // 12, 75%
-    {0x9, 0x2},      // 13, 77%
-    {0x9, 0x3},      // 14, 71%
-    {0xA, 0x3},      // 15, 73%
-    {0xB, 0x3},      // 16, 75%
-    {0xC, 0x3},      // 17, 76%
-    {0xD, 0x3},      // 18, 78%
-    {0xD, 0x4},      // 19, 74%
-    {0xE, 0x4},      // 20, 75%
-    {0xF, 0x4},      // 21, 76%
-    {0xF, 0x5},      // 22, 73%
-    {0xF, 0x6},      // 23, 70%
-    {0xF, 0x7},      // 24, 67%
-};
-
-static unsigned int can_speed(unsigned int pclk, unsigned int cclk, unsigned char psjw) 
-{
-    uint32_t    btr;
-    uint16_t    brp = 0;
-    uint32_t    calcbit;
-    uint32_t    bitwidth;
-    int         hit = 0;
-    int         bits;
-    
-    bitwidth = (pclk / cclk);
-    
-    brp = bitwidth / 0x18;
-    while ((!hit) && (brp < bitwidth / 4)) {
-        brp++;
-        for (bits = 22; bits > 0; bits--) {
-            calcbit = (bits + 3) * (brp + 1);
-            if (calcbit == bitwidth) {
-                hit = 1;
-                break;
-            }
-        }
-    }
-    
-    if (hit) {
-        btr = ((timing_pts[bits][1] << 20) & 0x00700000)
-            | ((timing_pts[bits][0] << 16) & 0x000F0000)
-            | ((psjw                << 24) & 0x0000C000)
-            | ((brp                 <<  0) & 0x000003FF);
-    } else {
-        btr = 0xFFFFFFFF;
-    }
-    
-    return btr;
-
-}
-
-int can_frequency(can_t *obj, int f) 
-{
-    int pclk = HAL_RCC_GetPCLK1Freq();
-    int btr = can_speed(pclk, (unsigned int)f, 1);
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    if (btr > 0) {
-        can->MCR |= CAN_MCR_INRQ ;
-        while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-        }
-        can->BTR = btr;
-        can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-        while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-        }
-        return 1;
-    } else {
-        return 0;
-    }
-}
-
-int can_write(can_t *obj, CAN_Message msg, int cc) 
-{
-    uint32_t  transmitmailbox = 5;
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    /* Select one empty transmit mailbox */
-    if ((can->TSR&CAN_TSR_TME0) == CAN_TSR_TME0) {
-        transmitmailbox = 0;
-    } else if ((can->TSR&CAN_TSR_TME1) == CAN_TSR_TME1) {
-        transmitmailbox = 1;
-    } else if ((can->TSR&CAN_TSR_TME2) == CAN_TSR_TME2) {
-        transmitmailbox = 2;
-    } else {
-        transmitmailbox = CAN_TXSTATUS_NOMAILBOX;
-    }
-
-    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) |
-                                             ((uint32_t)msg.data[4]));
-    /* Request transmission */
-    can->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ;
-  }
-
-  return 1;
-}
-
-int can_read(can_t *obj, CAN_Message *msg, int handle) 
-{
-    //handle is the FIFO number
-
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    
-    // check FPM0 which holds the pending message count in FIFO 0
-    // if no message is pending, return 0
-    if ((can->RF0R & CAN_RF0R_FMP0) == 0) {
-        return 0;
-    }
-
-    /* Get the Id */
-    msg->format = (CANFormat)((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR);
-    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);
-    /* Get the DLC */
-    msg->len = (uint8_t)0x0F & can->sFIFOMailBox[handle].RDTR;
-//  /* Get the FMI */
-//  msg->FMI = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDTR >> 8);
-    /* Get the data field */
-    msg->data[0] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDLR;
-    msg->data[1] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 8);
-    msg->data[2] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 16);
-    msg->data[3] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 24);
-    msg->data[4] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDHR;
-    msg->data[5] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 8);
-    msg->data[6] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 16);
-    msg->data[7] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 24);
-    
-    /* Release the FIFO */
-    if(handle == CAN_FIFO0) {
-        /* Release FIFO0 */
-        can->RF0R |= CAN_RF0R_RFOM0;
-    } else { /* FIFONumber == CAN_FIFO1 */
-      /* Release FIFO1 */
-      can->RF1R |= CAN_RF1R_RFOM1;
-    }
-
-    return 1;
-}
-
-void can_reset(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    can->MCR |= CAN_MCR_RESET;  
-    can->ESR = 0x0;
-}
-
-unsigned char can_rderror(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    return (can->ESR >> 24) & 0xFF;
-}
-
-unsigned char can_tderror(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    return (can->ESR >> 16) & 0xFF;
-}
-
-void can_monitor(can_t *obj, int silent) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);        
-    
-    can->MCR |= CAN_MCR_INRQ ;
-    while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-    }
-    if (silent) {
-        can->BTR |= ((uint32_t)1 << 31);
-    } else {
-        can->BTR &= ~((uint32_t)1 << 31);
-    }
-    can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-    while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-    }
-}
-
-int can_mode(can_t *obj, CanMode mode) 
-{
-    int success = 0;
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-    can->MCR |= CAN_MCR_INRQ ;
-    while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-    }
-    switch (mode) {
-        case MODE_NORMAL:
-            can->BTR &= ~(CAN_BTR_SILM | CAN_BTR_LBKM);
-            success = 1;
-            break;
-        case MODE_SILENT:
-            can->BTR |= CAN_BTR_SILM;
-            can->BTR &= ~CAN_BTR_LBKM;
-            success = 1;
-            break;
-        case MODE_TEST_GLOBAL:
-        case MODE_TEST_LOCAL:
-            can->BTR |= CAN_BTR_LBKM;
-            can->BTR &= ~CAN_BTR_SILM;
-            success = 1;
-            break;
-        case MODE_TEST_SILENT:
-            can->BTR |= (CAN_BTR_SILM | CAN_BTR_LBKM);
-            success = 1;
-            break;
-        default:
-            success = 0;
-            break;
-    }
-    can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-    while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-    }
-    return success;
-}
-
-int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) 
-{
-    CanHandle.Instance = (CAN_TypeDef *)(obj->can);
-    CAN_FilterConfTypeDef  sFilterConfig;
-    
-    sFilterConfig.FilterNumber = handle;
-    sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
-    sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
-    sFilterConfig.FilterIdHigh = (uint8_t) (id >> 8);
-    sFilterConfig.FilterIdLow = (uint8_t) id;
-    sFilterConfig.FilterMaskIdHigh = (uint8_t) (mask >> 8);
-    sFilterConfig.FilterMaskIdLow = (uint8_t) mask;
-    sFilterConfig.FilterFIFOAssignment = 0;
-    sFilterConfig.FilterActivation = ENABLE;
-    sFilterConfig.BankNumber = 14;
-  
-    HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig);
-      
-    return 0;
-}
-
-static void can_irq(CANName name, int id) 
-{
-    uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0;    
-    CanHandle.Instance = (CAN_TypeDef *)name;
-    
-    if(__HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_TME)) {
-        tmp1 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_0);
-        tmp2 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_1);
-        tmp3 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_2);
-        if (tmp1){
-            __HAL_CAN_CLEAR_FLAG(&CanHandle, CAN_FLAG_RQCP0);
-        }
-        if (tmp2){
-            __HAL_CAN_CLEAR_FLAG(&CanHandle, CAN_FLAG_RQCP1);
-        }
-        if (tmp3){
-            __HAL_CAN_CLEAR_FLAG(&CanHandle, CAN_FLAG_RQCP2);
-        }
-        if(tmp1 || tmp2 || tmp3)  
-        {
-            irq_handler(can_irq_ids[id], IRQ_TX);
-        }
-    }
-  
-    tmp1 = __HAL_CAN_MSG_PENDING(&CanHandle, CAN_FIFO0);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_FMP0);
-  
-    if((tmp1 != 0) && tmp2) {
-         irq_handler(can_irq_ids[id], IRQ_RX);
-    }
-  
-    tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_EPV);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_EPV);
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR); 
-  
-    if(tmp1 && tmp2 && tmp3) {
-         irq_handler(can_irq_ids[id], IRQ_PASSIVE);
-    }
-  
-    tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_BOF);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_BOF);
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);  
-    if(tmp1 && tmp2 && tmp3) {
-        irq_handler(can_irq_ids[id], IRQ_BUS);
-    }
-  
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);  
-    if(tmp1 && tmp2 && tmp3) {
-        irq_handler(can_irq_ids[id], IRQ_ERROR);
-    }  
-}
-
-void CAN1_RX0_IRQHandler(void )
-{
-    can_irq(CAN_1, 0);
-}
-
-void CAN1_TX_IRQHandler(void)
-{
-    can_irq(CAN_1, 0);
-}
-
-void CAN1_SCE_IRQHandler(void)
-{
-    can_irq(CAN_1, 0);
-}
-
-void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
-{
-
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-    IRQn_Type irq_n = (IRQn_Type)0;
-    uint32_t vector = 0;    
-    uint32_t ier;
-
-    if(obj->can == CAN_1) {
-        switch (type) {
-            case IRQ_RX:
-                ier = CAN_IT_FMP0;
-                irq_n = CAN1_RX0_IRQn;
-                vector = (uint32_t)&CAN1_RX0_IRQHandler;
-                break;
-            case IRQ_TX:      
-                ier = CAN_IT_TME;
-                irq_n = CAN1_TX_IRQn;
-                vector = (uint32_t)&CAN1_TX_IRQHandler;
-                break;
-            case IRQ_ERROR:   
-                ier = CAN_IT_ERR;
-                irq_n = CAN1_SCE_IRQn;
-                vector = (uint32_t)&CAN1_SCE_IRQHandler;
-                break;
-            case IRQ_PASSIVE:
-                ier = CAN_IT_EPV;
-                irq_n = CAN1_SCE_IRQn;
-                vector = (uint32_t)&CAN1_SCE_IRQHandler;
-                break;
-            case IRQ_BUS:
-                ier = CAN_IT_BOF;
-                irq_n = CAN1_SCE_IRQn;
-                vector = (uint32_t)&CAN1_SCE_IRQHandler;
-                break;
-            default: return;
-        }
-    } 
-
-    if(enable) {
-        can->IER |= ier;
-    } else {
-        can->IER &= ~ier;
-    }
-
-    NVIC_SetVector(irq_n, vector);
-    NVIC_EnableIRQ(irq_n);
-}
-
-#endif // DEVICE_CAN
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F1/can_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,42 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef MBED_CAN_DEVICE_H
+#define MBED_CAN_DEVICE_H
+
+#include "cmsis.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef DEVICE_CAN
+
+#define CAN_NUM    1 // Number of CAN peripherals present in the STM32 serie
+
+#define CAN1_IRQ_RX_IRQN        CAN1_RX0_IRQn
+#define CAN1_IRQ_RX_VECT        CAN1_RX0_IRQHandler
+#define CAN1_IRQ_TX_IRQN        CAN1_TX_IRQn
+#define CAN1_IRQ_TX_VECT        CAN1_TX_IRQHandler
+#define CAN1_IRQ_ERROR_IRQN     CAN1_SCE_IRQn
+#define CAN1_IRQ_ERROR_VECT     CAN1_SCE_IRQHandler
+#define CAN1_IRQ_PASSIVE_IRQN   CAN1_SCE_IRQn
+#define CAN1_IRQ_PASSIVE_VECT   CAN1_SCE_IRQHandler
+#define CAN1_IRQ_BUS_IRQN       CAN1_SCE_IRQn
+#define CAN1_IRQ_BUS_VECT       CAN1_SCE_IRQHandler
+
+#endif // DEVICE_CAN
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F1/gpio_irq_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,334 +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 <stddef.h>
-#include "cmsis.h"
-#include "gpio_irq_api.h"
-#include "pinmap.h"
-#include "mbed_error.h"
-
-#define EDGE_NONE (0)
-#define EDGE_RISE (1)
-#define EDGE_FALL (2)
-#define EDGE_BOTH (3)
-
-// Number of EXTI irq vectors (EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5_9, EXTI10_15)
-#define CHANNEL_NUM (7)
-
-// Max pins for one line (max with EXTI10_15)
-#define MAX_PIN_LINE (6)
-
-typedef struct gpio_channel {
-    uint32_t pin_mask;                   // bitmask representing which pins are configured for receiving interrupts
-    uint32_t channel_ids[MAX_PIN_LINE];  // mbed "gpio_irq_t gpio_irq" field of instance
-    uint32_t channel_gpio[MAX_PIN_LINE]; // base address of gpio port group
-    uint32_t channel_pin[MAX_PIN_LINE];  // pin number in port group
-} gpio_channel_t;
-
-static gpio_channel_t channels[CHANNEL_NUM] = {
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0}
-};
-
-// Used to return the index for channels array.
-static uint32_t pin_base_nr[16] = {
-    // EXTI0
-    0, // pin 0
-    // EXTI1
-    0, // pin 1
-    // EXTI2
-    0, // pin 2
-    // EXTI3
-    0, // pin 3
-    // EXTI4
-    0, // pin 4
-    // EXTI5_9
-    0, // pin 5
-    1, // pin 6
-    2, // pin 7
-    3, // pin 8
-    4, // pin 9
-    // EXTI10_15
-    0, // pin 10
-    1, // pin 11
-    2, // pin 12
-    3, // pin 13
-    4, // pin 14
-    5  // pin 15
-};
-
-static gpio_irq_handler irq_handler;
-
-static void handle_interrupt_in(uint32_t irq_index, uint32_t max_num_pin_line)
-{
-    gpio_channel_t *gpio_channel = &channels[irq_index];
-    uint32_t gpio_idx;
-
-    for (gpio_idx = 0; gpio_idx < max_num_pin_line; gpio_idx++) {
-        uint32_t current_mask = (1 << gpio_idx);
-
-        if (gpio_channel->pin_mask & current_mask) {
-            // Retrieve the gpio and pin that generate the irq
-            GPIO_TypeDef *gpio = (GPIO_TypeDef *)(gpio_channel->channel_gpio[gpio_idx]);
-            uint32_t pin = (uint32_t)(1 << (gpio_channel->channel_pin[gpio_idx]));
-
-            // Clear interrupt flag
-            if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) {
-                __HAL_GPIO_EXTI_CLEAR_FLAG(pin);
-
-                if (gpio_channel->channel_ids[gpio_idx] == 0) continue;
-
-                // Check which edge has generated the irq
-                if ((gpio->IDR & pin) == 0) {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_FALL);
-                } else  {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_RISE);
-                }
-            }
-        }
-    }
-}
-
-// EXTI line 0
-static void gpio_irq0(void)
-{
-    handle_interrupt_in(0, 1);
-}
-
-// EXTI line 1
-static void gpio_irq1(void)
-{
-    handle_interrupt_in(1, 1);
-}
-
-// EXTI line 2
-static void gpio_irq2(void)
-{
-    handle_interrupt_in(2, 1);
-}
-
-// EXTI line 3
-static void gpio_irq3(void)
-{
-    handle_interrupt_in(3, 1);
-}
-
-// EXTI line 4
-static void gpio_irq4(void)
-{
-    handle_interrupt_in(4, 1);
-}
-
-// EXTI lines 5 to 9
-static void gpio_irq5(void)
-{
-    handle_interrupt_in(5, 5);
-}
-
-// EXTI lines 10 to 15
-static void gpio_irq6(void)
-{
-    handle_interrupt_in(6, 6);
-}
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
-
-int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
-{
-    IRQn_Type irq_n = (IRQn_Type)0;
-    uint32_t vector = 0;
-    uint32_t irq_index;
-    gpio_channel_t *gpio_channel;
-    uint32_t gpio_idx;
-
-    if (pin == NC) return -1;
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Select irq number and interrupt routine
-    switch (pin_index) {
-        case 0:
-            irq_n = EXTI0_IRQn;
-            vector = (uint32_t)&gpio_irq0;
-            irq_index = 0;
-            break;
-        case 1:
-            irq_n = EXTI1_IRQn;
-            vector = (uint32_t)&gpio_irq1;
-            irq_index = 1;
-            break;
-        case 2:
-            irq_n = EXTI2_IRQn;
-            vector = (uint32_t)&gpio_irq2;
-            irq_index = 2;
-            break;
-        case 3:
-            irq_n = EXTI3_IRQn;
-            vector = (uint32_t)&gpio_irq3;
-            irq_index = 3;
-            break;
-        case 4:
-            irq_n = EXTI4_IRQn;
-            vector = (uint32_t)&gpio_irq4;
-            irq_index = 4;
-            break;
-        case 5:
-        case 6:
-        case 7:
-        case 8:
-        case 9:
-            irq_n = EXTI9_5_IRQn;
-            vector = (uint32_t)&gpio_irq5;
-            irq_index = 5;
-            break;
-        case 10:
-        case 11:
-        case 12:
-        case 13:
-        case 14:
-        case 15:
-            irq_n = EXTI15_10_IRQn;
-            vector = (uint32_t)&gpio_irq6;
-            irq_index = 6;
-            break;
-        default:
-            error("InterruptIn error: pin not supported.\n");
-            return -1;
-    }
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-
-    // Configure GPIO
-    pin_function(pin, STM_PIN_DATA(STM_MODE_IT_FALLING, GPIO_NOPULL, 0));
-
-    // Enable EXTI interrupt
-    NVIC_SetVector(irq_n, vector);
-    NVIC_EnableIRQ(irq_n);
-
-    // Save informations for future use
-    obj->irq_n = irq_n;
-    obj->irq_index = irq_index;
-    obj->event = EDGE_NONE;
-    obj->pin = pin;
-
-    gpio_channel = &channels[irq_index];
-    gpio_idx = pin_base_nr[pin_index];
-    gpio_channel->pin_mask |= (1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = id;
-    gpio_channel->channel_gpio[gpio_idx] = gpio_add;
-    gpio_channel->channel_pin[gpio_idx] = pin_index;
-
-    irq_handler = handler;
-
-    return 0;
-}
-
-void gpio_irq_free(gpio_irq_t *obj)
-{
-    gpio_channel_t *gpio_channel = &channels[obj->irq_index];
-    uint32_t pin_index  = STM_PIN(obj->pin);
-    uint32_t gpio_addr = GPIOA_BASE + (GPIOB_BASE-GPIOA_BASE) * STM_PORT(obj->pin);
-    uint32_t gpio_idx = pin_base_nr[pin_index];
-    
-    HAL_GPIO_DeInit((GPIO_TypeDef *)gpio_addr, (1<<pin_index));
-    gpio_channel->pin_mask &= ~(1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = 0;
-    gpio_channel->channel_gpio[gpio_idx] = 0;
-    gpio_channel->channel_pin[gpio_idx] = 0;
-
-    // Disable EXTI line, but don't change pull-up config
-    pin_function_gpiomode(obj->pin, STM_MODE_INPUT);
-    obj->event = EDGE_NONE;
-}
-
-void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
-{
-    uint32_t mode = STM_MODE_IT_EVT_RESET;
-
-    if (enable) {
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or RISE
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or FALL
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            }
-        }
-    } else { // Disable
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            } else { // NONE or RISE
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            } else { // NONE or FALL
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-    }
-
-    pin_function_gpiomode(obj->pin, mode);
-}
-
-void gpio_irq_enable(gpio_irq_t *obj)
-{
-    NVIC_EnableIRQ(obj->irq_n);
-}
-
-void gpio_irq_disable(gpio_irq_t *obj)
-{
-    NVIC_DisableIRQ(obj->irq_n);
-    obj->event = EDGE_NONE;
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F1/gpio_irq_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,59 @@
+/* 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 "cmsis.h"
+#include "gpio_irq_device.h"
+
+// Used to return the index for channels array.
+const exti_lines_t pin_lines_desc[16] = {
+    // EXTI0
+    {.gpio_idx = 0, .irq_index = 0, .irq_n = EXTI0_IRQn}, // pin 0
+    // EXTI1
+    {.gpio_idx = 0, .irq_index = 1, .irq_n = EXTI1_IRQn}, // pin 1
+    // EXTI2
+    {.gpio_idx = 0, .irq_index = 2, .irq_n = EXTI2_IRQn}, // pin 2
+    // EXTI3
+    {.gpio_idx = 0, .irq_index = 3, .irq_n = EXTI3_IRQn}, // pin 3
+    // EXTI4
+    {.gpio_idx = 0, .irq_index = 4, .irq_n = EXTI4_IRQn}, // pin 4
+    // EXTI5_9
+    {.gpio_idx = 0, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 5
+    {.gpio_idx = 1, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 6
+    {.gpio_idx = 2, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 7
+    {.gpio_idx = 3, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 8
+    {.gpio_idx = 4, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 9
+    // EXTI10_15
+    {.gpio_idx = 0, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 10
+    {.gpio_idx = 1, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 11
+    {.gpio_idx = 2, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 12
+    {.gpio_idx = 3, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 13
+    {.gpio_idx = 4, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 14
+    {.gpio_idx = 5, .irq_index = 6, .irq_n = EXTI15_10_IRQn}// pin 15
+};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F1/gpio_irq_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,70 @@
+/* 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_GPIO_IRQ_DEVICE_H
+#define MBED_GPIO_IRQ_DEVICE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "stm32f1xx_ll_exti.h"
+
+// Number of EXTI irq vectors (EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5_9, EXTI10_15)
+#define CHANNEL_NUM (7)
+
+#define EXTI_IRQ0_NUM_LINES 1
+#define EXTI_IRQ1_NUM_LINES 1
+#define EXTI_IRQ2_NUM_LINES 1
+#define EXTI_IRQ3_NUM_LINES 1
+#define EXTI_IRQ4_NUM_LINES 1
+#define EXTI_IRQ5_NUM_LINES 5
+#define EXTI_IRQ6_NUM_LINES 6
+
+// Max pins for one line (max with EXTI10_15)
+#define MAX_PIN_LINE (EXTI_IRQ6_NUM_LINES)
+
+/*  Structure to describe how the HW EXTI lines are defined in this HW */
+typedef struct exti_lines {
+    uint32_t gpio_idx;   // an index entry for each EXIT line
+    uint32_t irq_index;  // the IRQ index
+    IRQn_Type  irq_n;    // the corresponding EXTI IRQn
+} exti_lines_t;
+
+// Used to return the index for channels array.
+extern const exti_lines_t pin_lines_desc[];
+
+/* In F1 family target, SYSCFG is named AFIO */
+#define SYSCFG AFIO
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F1/pin_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,119 @@
+/* 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.
+ *******************************************************************************
+ */
+#ifndef MBED_PIN_DEVICE_H
+#define MBED_PIN_DEVICE_H
+
+#include "cmsis.h"
+#include "stm32f1xx_ll_gpio.h"
+
+extern const uint32_t ll_pin_defines[16];
+
+static inline void stm_pin_DisconnectDebug(PinName pin)
+{
+    // Disconnect JTAG-DP + SW-DP signals.
+    // Warning: Need to reconnect under reset
+    if ((pin == PA_13) || (pin == PA_14)) {
+        __HAL_AFIO_REMAP_SWJ_DISABLE(); // JTAG-DP Disabled and SW-DP Disabled
+    }
+    if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) {
+        __HAL_AFIO_REMAP_SWJ_NOJTAG(); // JTAG-DP Disabled and SW-DP enabled
+    }
+}
+
+/*  The AF selection of F1 family is specific compared to others */
+static inline void stm_pin_SetAFPin(GPIO_TypeDef *gpio, PinName pin, uint32_t afnum)
+{
+    // Enable AFIO clock
+    __HAL_RCC_AFIO_CLK_ENABLE();
+
+    if (afnum > 0) {
+        switch (afnum) {
+        case 1: // Remap SPI1
+            __HAL_AFIO_REMAP_SPI1_ENABLE();
+            break;
+        case 2: // Remap I2C1
+            __HAL_AFIO_REMAP_I2C1_ENABLE();
+            break;
+        case 3: // Remap USART1
+            __HAL_AFIO_REMAP_USART1_ENABLE();
+            break;
+        case 4: // Remap USART2
+            __HAL_AFIO_REMAP_USART2_ENABLE();
+            break;
+        case 5: // Partial Remap USART3
+            __HAL_AFIO_REMAP_USART3_PARTIAL();
+            break;
+        case 6: // Partial Remap TIM1
+            __HAL_AFIO_REMAP_TIM1_PARTIAL();
+            break;
+        case 7: // Partial Remap TIM3
+            __HAL_AFIO_REMAP_TIM3_PARTIAL();
+            break;
+        case 8: // Full Remap TIM2
+            __HAL_AFIO_REMAP_TIM2_ENABLE();
+            break;
+        case 9: // Full Remap TIM3
+            __HAL_AFIO_REMAP_TIM3_ENABLE();
+            break;
+#if defined(AFIO_MAPR_CAN_REMAP_REMAP1)
+        case 10: // CAN_RX mapped to PB8, CAN_TX mapped to PB9
+            __HAL_AFIO_REMAP_CAN1_2();
+            break;
+#endif
+        default:
+            break;
+        }
+    }
+}
+
+static inline void stm_pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t pull_config)
+{
+    uint32_t function = LL_GPIO_GetPinMode(gpio, ll_pin);
+
+    switch (pull_config) {
+        case GPIO_PULLUP:
+            if (function == LL_GPIO_MODE_FLOATING)
+                LL_GPIO_SetPinMode(gpio, ll_pin, LL_GPIO_MODE_INPUT);
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_UP);
+            break;
+        case GPIO_PULLDOWN:
+            if (function == LL_GPIO_MODE_FLOATING)
+                LL_GPIO_SetPinMode(gpio, ll_pin, LL_GPIO_MODE_INPUT);
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_DOWN);
+            break;
+        default:
+            /*  Input+NoPull = Floating for F1 family */
+            if (function == LL_GPIO_MODE_INPUT)
+                LL_GPIO_SetPinMode(gpio, ll_pin, LL_GPIO_MODE_FLOATING);
+            break;
+    }
+}
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F1/pinmap.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,252 +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 "mbed_assert.h"
-#include "pinmap.h"
-#include "PortNames.h"
-#include "mbed_error.h"
-
-// GPIO mode look-up table
-// Warning: the elements order must be the same as the one defined in PinNames.h
-static const uint32_t gpio_mode[13] = {
-    GPIO_MODE_INPUT,              //  0 = STM_MODE_INPUT
-    GPIO_MODE_OUTPUT_PP,          //  1 = STM_MODE_OUTPUT_PP
-    GPIO_MODE_OUTPUT_OD,          //  2 = STM_MODE_OUTPUT_OD
-    GPIO_MODE_AF_PP,              //  3 = STM_MODE_AF_PP
-    GPIO_MODE_AF_OD,              //  4 = STM_MODE_AF_OD
-    GPIO_MODE_ANALOG,             //  5 = STM_MODE_ANALOG
-    GPIO_MODE_IT_RISING,          //  6 = STM_MODE_IT_RISING
-    GPIO_MODE_IT_FALLING,         //  7 = STM_MODE_IT_FALLING
-    GPIO_MODE_IT_RISING_FALLING,  //  8 = STM_MODE_IT_RISING_FALLING
-    GPIO_MODE_EVT_RISING,         //  9 = STM_MODE_EVT_RISING
-    GPIO_MODE_EVT_FALLING,        // 10 = STM_MODE_EVT_FALLING
-    GPIO_MODE_EVT_RISING_FALLING, // 11 = STM_MODE_EVT_RISING_FALLING
-    0x10000000                    // 12 = STM_MODE_IT_EVT_RESET (not in STM32Cube HAL)
-};
-
-// Enable GPIO clock and return GPIO base address
-uint32_t Set_GPIO_Clock(uint32_t port_idx)
-{
-    uint32_t gpio_add = 0;
-    switch (port_idx) {
-        case PortA:
-            gpio_add = GPIOA_BASE;
-            __GPIOA_CLK_ENABLE();
-            break;
-        case PortB:
-            gpio_add = GPIOB_BASE;
-            __GPIOB_CLK_ENABLE();
-            break;
-        case PortC:
-            gpio_add = GPIOC_BASE;
-            __GPIOC_CLK_ENABLE();
-            break;
-        case PortD:
-            gpio_add = GPIOD_BASE;
-            __GPIOD_CLK_ENABLE();
-            break;
-        default:
-            error("Pinmap error: wrong port number.");
-            break;
-    }
-    return gpio_add;
-}
-
-/**
- * Configure pin (input, output, alternate function or analog) + output speed + AF
- */
-void pin_function(PinName pin, int data)
-{
-    MBED_ASSERT(pin != (PinName)NC);
-    // Get the pin informations
-    uint32_t mode  = STM_PIN_MODE(data);
-    uint32_t pupd  = STM_PIN_PUPD(data);
-    uint32_t afnum = STM_PIN_AFNUM(data);
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Enable AFIO clock
-    __HAL_RCC_AFIO_CLK_ENABLE();
-
-    // Configure Alternate Function
-    // Warning: Must be done before the GPIO is initialized
-    if (afnum > 0) {
-        switch (afnum) {
-            case 1: // Remap SPI1
-                __HAL_AFIO_REMAP_SPI1_ENABLE();
-                break;
-            case 2: // Remap I2C1
-                __HAL_AFIO_REMAP_I2C1_ENABLE();
-                break;
-            case 3: // Remap USART1
-                __HAL_AFIO_REMAP_USART1_ENABLE();
-                break;
-            case 4: // Remap USART2
-                __HAL_AFIO_REMAP_USART2_ENABLE();
-                break;
-            case 5: // Partial Remap USART3
-                __HAL_AFIO_REMAP_USART3_PARTIAL();
-                break;
-            case 6: // Partial Remap TIM1
-                __HAL_AFIO_REMAP_TIM1_PARTIAL();
-                break;
-            case 7: // Partial Remap TIM3
-                __HAL_AFIO_REMAP_TIM3_PARTIAL();
-                break;
-            case 8: // Full Remap TIM2
-                __HAL_AFIO_REMAP_TIM2_ENABLE();
-                break;
-            case 9: // Full Remap TIM3
-                __HAL_AFIO_REMAP_TIM3_ENABLE();
-                break;
-            case 10: // CAN_RX mapped to PB8, CAN_TX mapped to PB9
-                __HAL_AFIO_REMAP_CAN1_2();
-                break;
-            default:
-                break;
-        }
-    }
-
-    // Configure GPIO
-    GPIO_InitTypeDef GPIO_InitStructure;
-    GPIO_InitStructure.Pin   = (uint32_t)(1 << pin_index);
-    GPIO_InitStructure.Mode  = gpio_mode[mode];
-    GPIO_InitStructure.Pull  = pupd;
-    GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
-    HAL_GPIO_Init(gpio, &GPIO_InitStructure);
-
-    // Disconnect JTAG-DP + SW-DP signals.
-    // Warning: Need to reconnect under reset
-    if ((pin == PA_13) || (pin == PA_14)) {
-        __HAL_AFIO_REMAP_SWJ_DISABLE(); // JTAG-DP Disabled and SW-DP Disabled
-    }
-    if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) {
-        __HAL_AFIO_REMAP_SWJ_NOJTAG(); // JTAG-DP Disabled and SW-DP enabled
-    }
-}
-
-/**
- * Configure pin pull-up/pull-down
- */
-void pin_mode(PinName pin, PinMode mode)
-{
-    MBED_ASSERT(pin != (PinName)NC);
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-    __IO uint32_t* gpio_reg_hl;//gpio register depends on bit index (high or low)
-    uint32_t shift;
-
-    if (pin_index < 8) {
-        shift = (pin_index * 4);
-        gpio_reg_hl = &(gpio->CRL);
-    } else {
-        shift = (pin_index % 8) * 4;
-        gpio_reg_hl = &(gpio->CRH);
-    }
-
-    // Configure open-drain and pull-up/down
-    switch (mode) {
-        case PullNone:
-            break;
-        case PullUp:
-        case PullDown:
-            // Set pull-up / pull-down for Input mode
-            if ((*gpio_reg_hl & (0x03 << shift)) == 0) { // MODE bits = Input mode
-                *gpio_reg_hl |= (0x08 << shift); // Set pull-up / pull-down
-                *gpio_reg_hl &= ~(0x04 << shift); // ENSURES GPIOx_CRL.CNFx.bit0 = 0
-            }
-            // Now it's time to setup properly if pullup or pulldown. This is done in ODR register:
-            // set pull-up => bit=1, set pull-down => bit = 0
-            if (mode == PullUp) {
-                gpio->ODR |= (0x01 << (pin_index)); // Set pull-up
-            } else {
-                gpio->ODR &= ~(0x01 << (pin_index)); // Set pull-down
-            }
-            break;
-        case OpenDrain:
-            // Set open-drain for Output mode (General Purpose or Alternate Function)
-            if ((*gpio_reg_hl & (0x03 << shift)) > 0) { // MODE bits = Output mode
-                *gpio_reg_hl |= (0x04 << shift); // Set open-drain
-            }
-            break;
-        default:
-            break;
-    }
-}
-
-/*  Internal function for setting the gpiomode/function
- *  without changing Pull mode
- */
-void pin_function_gpiomode(PinName pin, uint32_t gpiomode)
-{
-
-    /* Read current pull state from HW to avoid over-write*/
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
-    uint32_t pull = PullNone;
-    __IO uint32_t* gpio_reg_hl;//gpio register depends on bit index (high or low)
-    uint32_t shift;
-
-    if (pin_index < 8) {
-        shift = (pin_index * 4);
-        gpio_reg_hl = &(gpio->CRL);
-    } else {
-        shift = (pin_index % 8) * 4;
-        gpio_reg_hl = &(gpio->CRH);
-    }
-
-    /*  Check if pull/pull down is active */
-    if (!(*gpio_reg_hl & (0x03 << shift))) {// input
-        if((!!(*gpio_reg_hl & (0x08 << shift))) // pull-up / down
-                && (!(*gpio_reg_hl & (0x04 << shift)))) { // GPIOx_CRL.CNFx.bit0 = 0
-            if (!!(gpio->ODR & (0x01 << pin_index))) {
-                pull = PullUp;
-            } else {
-                pull = PullDown;
-            }
-        }
-    } else { //output
-        if (!!(*gpio_reg_hl & (0x04 << shift))) { //open drain
-            pull = OpenDrain;
-        }
-    }
-
-    /* Then re-use global function for updating the mode part*/
-    pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
-}
--- a/targets/TARGET_STM/TARGET_STM32F1/port_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,103 +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 "port_api.h"
-#include "pinmap.h"
-#include "gpio_api.h"
-#include "mbed_error.h"
-
-#if DEVICE_PORTIN || DEVICE_PORTOUT
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-
-// high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
-// low nibble  = pin number
-PinName port_pin(PortName port, int pin_n)
-{
-    return (PinName)(pin_n + (port << 4));
-}
-
-void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
-{
-    uint32_t port_index = (uint32_t)port;
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Fill PORT object structure for future use
-    obj->port      = port;
-    obj->mask      = mask;
-    obj->direction = dir;
-    obj->reg_in    = &gpio->IDR;
-    obj->reg_out   = &gpio->ODR;
-
-    port_dir(obj, dir);
-}
-
-void port_dir(port_t *obj, PinDirection dir)
-{
-    uint32_t i;
-    obj->direction = dir;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            if (dir == PIN_OUTPUT) {
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
-            } else { // PIN_INPUT
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
-            }
-        }
-    }
-}
-
-void port_mode(port_t *obj, PinMode mode)
-{
-    uint32_t i;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            pin_mode(port_pin(obj->port, i), mode);
-        }
-    }
-}
-
-void port_write(port_t *obj, int value)
-{
-    *obj->reg_out = (*obj->reg_out & ~obj->mask) | (value & obj->mask);
-}
-
-int port_read(port_t *obj)
-{
-    if (obj->direction == PIN_OUTPUT) {
-        return (*obj->reg_out & obj->mask);
-    } else { // PIN_INPUT
-        return (*obj->reg_in & obj->mask);
-    }
-}
-
-#endif
--- a/targets/TARGET_STM/TARGET_STM32F1/pwmout_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,57 @@
+/* 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;
+
+extern const pwm_apb_map_t pwm_apb_map_table[];
+
+#endif // DEVICE_PWMOUT
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F2/analogin_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F2/analogin_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -30,7 +30,7 @@
 
 #if DEVICE_ANALOGIN
 
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "cmsis.h"
 #include "pinmap.h"
 #include "mbed_error.h"
--- a/targets/TARGET_STM/TARGET_STM32F2/can_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,540 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2016 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "can_api.h"
-
-#if DEVICE_CAN
-
-#include "cmsis.h"
-#include "pinmap.h"
-#include "PeripheralPins.h"
-#include "mbed_error.h"
-#include <math.h>
-#include <string.h>
-
-#define CAN_NUM    2
-static CAN_HandleTypeDef CanHandle;
-static uint32_t can_irq_ids[CAN_NUM] = {0};
-static can_irq_handler irq_handler;
-
-void can_init(can_t *obj, PinName rd, PinName td)
-{
-    uint32_t filter_number;
-    CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
-    CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
-    obj->can = (CANName)pinmap_merge(can_rd, can_td);
-    MBED_ASSERT((int)obj->can != NC);
-
-    if (obj->can == CAN_1) {
-        __HAL_RCC_CAN1_CLK_ENABLE();
-        obj->index = 0;
-    } else {
-        __HAL_RCC_CAN2_CLK_ENABLE();
-        obj->index = 1;
-    }
-
-    // Configure the CAN pins
-    pinmap_pinout(rd, PinMap_CAN_RD);
-    pinmap_pinout(td, PinMap_CAN_TD);
-    if (rd != NC) {
-        pin_mode(rd, PullUp);
-    }
-    if (td != NC) {
-        pin_mode(td, PullUp);
-    }
-
-    CanHandle.Instance = (CAN_TypeDef *)(obj->can);
-
-    CanHandle.Init.TTCM = DISABLE;
-    CanHandle.Init.ABOM = DISABLE;
-    CanHandle.Init.AWUM = DISABLE;
-    CanHandle.Init.NART = DISABLE;
-    CanHandle.Init.RFLM = DISABLE;
-    CanHandle.Init.TXFP = DISABLE;
-    CanHandle.Init.Mode = CAN_MODE_NORMAL;
-    CanHandle.Init.SJW = CAN_SJW_1TQ;
-    CanHandle.Init.BS1 = CAN_BS1_6TQ;
-    CanHandle.Init.BS2 = CAN_BS2_8TQ;
-    CanHandle.Init.Prescaler = 2;
-
-    if (HAL_CAN_Init(&CanHandle) != HAL_OK) {
-        error("Cannot initialize CAN");
-    }
-
-    filter_number = (obj->can == CAN_1) ? 0 : 14;
-
-    // Set initial CAN frequency to 100kb/s
-    can_frequency(obj, 100000);
-
-    can_filter(obj, 0, 0, CANStandard, filter_number);
-}
-
-void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id)
-{
-    irq_handler = handler;
-    can_irq_ids[obj->index] = id;
-}
-
-void can_irq_free(can_t *obj)
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    can->IER &= ~(CAN_IT_FMP0 | CAN_IT_FMP1 | CAN_IT_TME | \
-                  CAN_IT_ERR | CAN_IT_EPV | CAN_IT_BOF);
-    can_irq_ids[obj->can] = 0;
-}
-
-void can_free(can_t *obj)
-{
-    // Reset CAN and disable clock
-    if (obj->can == CAN_1) {
-        __HAL_RCC_CAN1_FORCE_RESET();
-        __HAL_RCC_CAN1_RELEASE_RESET();
-        __HAL_RCC_CAN1_CLK_DISABLE();
-    }
-
-    if (obj->can == CAN_2) {
-        __HAL_RCC_CAN2_FORCE_RESET();
-        __HAL_RCC_CAN2_RELEASE_RESET();
-        __HAL_RCC_CAN2_CLK_DISABLE();
-    }
-}
-
-// The following table is used to program bit_timing. It is an adjustment of the sample
-// point by synchronizing on the start-bit edge and resynchronizing on the following edges.
-// This table has the sampling points as close to 75% as possible (most commonly used).
-// The first value is TSEG1, the second TSEG2.
-static const int timing_pts[23][2] = {
-    {0x0, 0x0},      // 2,  50%
-    {0x1, 0x0},      // 3,  67%
-    {0x2, 0x0},      // 4,  75%
-    {0x3, 0x0},      // 5,  80%
-    {0x3, 0x1},      // 6,  67%
-    {0x4, 0x1},      // 7,  71%
-    {0x5, 0x1},      // 8,  75%
-    {0x6, 0x1},      // 9,  78%
-    {0x6, 0x2},      // 10, 70%
-    {0x7, 0x2},      // 11, 73%
-    {0x8, 0x2},      // 12, 75%
-    {0x9, 0x2},      // 13, 77%
-    {0x9, 0x3},      // 14, 71%
-    {0xA, 0x3},      // 15, 73%
-    {0xB, 0x3},      // 16, 75%
-    {0xC, 0x3},      // 17, 76%
-    {0xD, 0x3},      // 18, 78%
-    {0xD, 0x4},      // 19, 74%
-    {0xE, 0x4},      // 20, 75%
-    {0xF, 0x4},      // 21, 76%
-    {0xF, 0x5},      // 22, 73%
-    {0xF, 0x6},      // 23, 70%
-    {0xF, 0x7},      // 24, 67%
-};
-
-static unsigned int can_speed(unsigned int pclk, unsigned int cclk, unsigned char psjw)
-{
-    uint32_t    btr;
-    uint16_t    brp = 0;
-    uint32_t    calcbit;
-    uint32_t    bitwidth;
-    int         hit = 0;
-    int         bits;
-
-    bitwidth = (pclk / cclk);
-
-    brp = bitwidth / 0x18;
-    while ((!hit) && (brp < bitwidth / 4)) {
-        brp++;
-        for (bits = 22; bits > 0; bits--) {
-            calcbit = (bits + 3) * (brp + 1);
-            if (calcbit == bitwidth) {
-                hit = 1;
-                break;
-            }
-        }
-    }
-
-    if (hit) {
-        btr = ((timing_pts[bits][1] << 20) & 0x00700000)
-              | ((timing_pts[bits][0] << 16) & 0x000F0000)
-              | ((psjw                << 24) & 0x0000C000)
-              | ((brp                 <<  0) & 0x000003FF);
-    } else {
-        btr = 0xFFFFFFFF;
-    }
-
-    return btr;
-
-}
-
-int can_frequency(can_t *obj, int f)
-{
-    int pclk = HAL_RCC_GetPCLK1Freq();
-    int btr = can_speed(pclk, (unsigned int)f, 1);
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    if (btr > 0) {
-        can->MCR |= CAN_MCR_INRQ ;
-        while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-        }
-        can->BTR = btr;
-        can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-        while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-        }
-        return 1;
-    } else {
-        return 0;
-    }
-}
-
-int can_write(can_t *obj, CAN_Message msg, int cc)
-{
-    uint32_t  transmitmailbox = 5;
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    /* Select one empty transmit mailbox */
-    if ((can->TSR & CAN_TSR_TME0) == CAN_TSR_TME0) {
-        transmitmailbox = 0;
-    } else if ((can->TSR & CAN_TSR_TME1) == CAN_TSR_TME1) {
-        transmitmailbox = 1;
-    } else if ((can->TSR & CAN_TSR_TME2) == CAN_TSR_TME2) {
-        transmitmailbox = 2;
-    } else {
-        transmitmailbox = CAN_TXSTATUS_NOMAILBOX;
-    }
-
-    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) |
-                                                ((uint32_t)msg.data[4]));
-        /* Request transmission */
-        can->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ;
-    }
-
-    return 1;
-}
-
-int can_read(can_t *obj, CAN_Message *msg, int handle)
-{
-    //handle is the FIFO number
-
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    // check FPM0 which holds the pending message count in FIFO 0
-    // if no message is pending, return 0
-    if ((can->RF0R & CAN_RF0R_FMP0) == 0) {
-        return 0;
-    }
-
-    /* Get the Id */
-    msg->format = (CANFormat)((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR);
-    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);
-    /* Get the DLC */
-    msg->len = (uint8_t)0x0F & can->sFIFOMailBox[handle].RDTR;
-//  /* Get the FMI */
-//  msg->FMI = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDTR >> 8);
-    /* Get the data field */
-    msg->data[0] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDLR;
-    msg->data[1] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 8);
-    msg->data[2] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 16);
-    msg->data[3] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 24);
-    msg->data[4] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDHR;
-    msg->data[5] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 8);
-    msg->data[6] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 16);
-    msg->data[7] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 24);
-
-    /* Release the FIFO */
-    if (handle == CAN_FIFO0) {
-        /* Release FIFO0 */
-        can->RF0R |= CAN_RF0R_RFOM0;
-    } else { /* FIFONumber == CAN_FIFO1 */
-        /* Release FIFO1 */
-        can->RF1R |= CAN_RF1R_RFOM1;
-    }
-
-    return 1;
-}
-
-void can_reset(can_t *obj)
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    can->MCR |= CAN_MCR_RESET;
-    can->ESR = 0x0;
-}
-
-unsigned char can_rderror(can_t *obj)
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-    return (can->ESR >> 24) & 0xFF;
-}
-
-unsigned char can_tderror(can_t *obj)
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-    return (can->ESR >> 16) & 0xFF;
-}
-
-void can_monitor(can_t *obj, int silent)
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    can->MCR |= CAN_MCR_INRQ ;
-    while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-    }
-    if (silent) {
-        can->BTR |= ((uint32_t)1 << 31);
-    } else {
-        can->BTR &= ~((uint32_t)1 << 31);
-    }
-    can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-    while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-    }
-}
-
-int can_mode(can_t *obj, CanMode mode)
-{
-    int success = 0;
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-    can->MCR |= CAN_MCR_INRQ ;
-    while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-    }
-    switch (mode) {
-        case MODE_NORMAL:
-            can->BTR &= ~(CAN_BTR_SILM | CAN_BTR_LBKM);
-            success = 1;
-            break;
-        case MODE_SILENT:
-            can->BTR |= CAN_BTR_SILM;
-            can->BTR &= ~CAN_BTR_LBKM;
-            success = 1;
-            break;
-        case MODE_TEST_GLOBAL:
-        case MODE_TEST_LOCAL:
-            can->BTR |= CAN_BTR_LBKM;
-            can->BTR &= ~CAN_BTR_SILM;
-            success = 1;
-            break;
-        case MODE_TEST_SILENT:
-            can->BTR |= (CAN_BTR_SILM | CAN_BTR_LBKM);
-            success = 1;
-            break;
-        default:
-            success = 0;
-            break;
-    }
-    can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-    while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-    }
-    return success;
-}
-
-int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle)
-{
-    CanHandle.Instance = (CAN_TypeDef *)(obj->can);
-    CAN_FilterConfTypeDef  sFilterConfig;
-
-    sFilterConfig.FilterNumber = handle;
-    sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
-    sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
-    sFilterConfig.FilterIdHigh = (uint8_t)(id >> 8);
-    sFilterConfig.FilterIdLow = (uint8_t) id;
-    sFilterConfig.FilterMaskIdHigh = (uint8_t)(mask >> 8);
-    sFilterConfig.FilterMaskIdLow = (uint8_t) mask;
-    sFilterConfig.FilterFIFOAssignment = 0;
-    sFilterConfig.FilterActivation = ENABLE;
-    sFilterConfig.BankNumber = 14 + handle;
-
-    HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig);
-
-    return 0;
-}
-
-static void can_irq(CANName name, int id)
-{
-    uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0;
-    CanHandle.Instance = (CAN_TypeDef *)name;
-
-    if (__HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_TME)) {
-        tmp1 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_0);
-        tmp2 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_1);
-        tmp3 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_2);
-        if (tmp1 || tmp2 || tmp3) {
-            irq_handler(can_irq_ids[id], IRQ_TX);
-        }
-    }
-
-    tmp1 = __HAL_CAN_MSG_PENDING(&CanHandle, CAN_FIFO0);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_FMP0);
-
-    if ((tmp1 != 0) && tmp2) {
-        irq_handler(can_irq_ids[id], IRQ_RX);
-    }
-
-    tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_EPV);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_EPV);
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);
-
-    if (tmp1 && tmp2 && tmp3) {
-        irq_handler(can_irq_ids[id], IRQ_PASSIVE);
-    }
-
-    tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_BOF);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_BOF);
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);
-    if (tmp1 && tmp2 && tmp3) {
-        irq_handler(can_irq_ids[id], IRQ_BUS);
-    }
-
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);
-    if (tmp1 && tmp2 && tmp3) {
-        irq_handler(can_irq_ids[id], IRQ_ERROR);
-    }
-}
-
-void CAN1_RX0_IRQHandler(void)
-{
-    can_irq(CAN_1, 0);
-}
-
-void CAN1_TX_IRQHandler(void)
-{
-    can_irq(CAN_1, 0);
-}
-
-void CAN1_SCE_IRQHandler(void)
-{
-    can_irq(CAN_1, 0);
-}
-
-void CAN2_RX0_IRQHandler(void)
-{
-    can_irq(CAN_2, 1);
-}
-
-void CAN2_TX_IRQHandler(void)
-{
-    can_irq(CAN_2, 1);
-}
-
-void CAN2_SCE_IRQHandler(void)
-{
-    can_irq(CAN_2, 1);
-}
-
-void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
-{
-
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-    IRQn_Type irq_n = (IRQn_Type)0;
-    uint32_t vector = 0;
-    uint32_t ier;
-
-    if (obj->can == CAN_1) {
-        switch (type) {
-            case IRQ_RX:
-                ier = CAN_IT_FMP0;
-                irq_n = CAN1_RX0_IRQn;
-                vector = (uint32_t)&CAN1_RX0_IRQHandler;
-                break;
-            case IRQ_TX:
-                ier = CAN_IT_TME;
-                irq_n = CAN1_TX_IRQn;
-                vector = (uint32_t)&CAN1_TX_IRQHandler;
-                break;
-            case IRQ_ERROR:
-                ier = CAN_IT_ERR;
-                irq_n = CAN1_SCE_IRQn;
-                vector = (uint32_t)&CAN1_SCE_IRQHandler;
-                break;
-            case IRQ_PASSIVE:
-                ier = CAN_IT_EPV;
-                irq_n = CAN1_SCE_IRQn;
-                vector = (uint32_t)&CAN1_SCE_IRQHandler;
-                break;
-            case IRQ_BUS:
-                ier = CAN_IT_BOF;
-                irq_n = CAN1_SCE_IRQn;
-                vector = (uint32_t)&CAN1_SCE_IRQHandler;
-                break;
-            default:
-                return;
-        }
-    } else {
-        switch (type) {
-            case IRQ_RX:
-                ier = CAN_IT_FMP0;
-                irq_n = CAN2_RX0_IRQn;
-                vector = (uint32_t)&CAN2_RX0_IRQHandler;
-                break;
-            case IRQ_TX:
-                ier = CAN_IT_TME;
-                irq_n = CAN2_TX_IRQn;
-                vector = (uint32_t)&CAN2_TX_IRQHandler;
-                break;
-            case IRQ_ERROR:
-                ier = CAN_IT_ERR;
-                irq_n = CAN2_SCE_IRQn;
-                vector = (uint32_t)&CAN2_SCE_IRQHandler;
-                break;
-            case IRQ_PASSIVE:
-                ier = CAN_IT_EPV;
-                irq_n = CAN2_SCE_IRQn;
-                vector = (uint32_t)&CAN2_SCE_IRQHandler;
-                break;
-            case IRQ_BUS:
-                ier = CAN_IT_BOF;
-                irq_n = CAN2_SCE_IRQn;
-                vector = (uint32_t)&CAN2_SCE_IRQHandler;
-                break;
-            default:
-                return;
-        }
-    }
-
-    if (enable) {
-        can->IER |= ier;
-    } else {
-        can->IER &= ~ier;
-    }
-
-    NVIC_SetVector(irq_n, vector);
-    NVIC_EnableIRQ(irq_n);
-}
-
-#endif // DEVICE_CAN
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F2/can_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,53 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef MBED_CAN_DEVICE_H
+#define MBED_CAN_DEVICE_H
+
+#include "cmsis.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef DEVICE_CAN
+
+#define CAN_NUM 2 // Number of CAN peripherals present in the STM32 serie (1 or 2)
+
+#define CAN1_IRQ_RX_IRQN        CAN1_RX0_IRQn
+#define CAN1_IRQ_RX_VECT        CAN1_RX0_IRQHandler
+#define CAN1_IRQ_TX_IRQN        CAN1_TX_IRQn
+#define CAN1_IRQ_TX_VECT        CAN1_TX_IRQHandler
+#define CAN1_IRQ_ERROR_IRQN     CAN1_SCE_IRQn
+#define CAN1_IRQ_ERROR_VECT     CAN1_SCE_IRQHandler
+#define CAN1_IRQ_PASSIVE_IRQN   CAN1_SCE_IRQn
+#define CAN1_IRQ_PASSIVE_VECT   CAN1_SCE_IRQHandler
+#define CAN1_IRQ_BUS_IRQN       CAN1_SCE_IRQn
+#define CAN1_IRQ_BUS_VECT       CAN1_SCE_IRQHandler
+
+#define CAN2_IRQ_RX_IRQN        CAN2_RX0_IRQn
+#define CAN2_IRQ_RX_VECT        CAN2_RX0_IRQHandler
+#define CAN2_IRQ_TX_IRQN        CAN2_TX_IRQn
+#define CAN2_IRQ_TX_VECT        CAN2_TX_IRQHandler
+#define CAN2_IRQ_ERROR_IRQN     CAN2_SCE_IRQn
+#define CAN2_IRQ_ERROR_VECT     CAN2_SCE_IRQHandler
+#define CAN2_IRQ_PASSIVE_IRQN   CAN2_SCE_IRQn
+#define CAN2_IRQ_PASSIVE_VECT   CAN2_SCE_IRQHandler
+#define CAN2_IRQ_BUS_IRQN       CAN2_SCE_IRQn
+#define CAN2_IRQ_BUS_VECT       CAN2_SCE_IRQHandler
+
+#endif // DEVICE_CAN
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F2/gpio_irq_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,334 +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 <stddef.h>
-#include "cmsis.h"
-#include "gpio_irq_api.h"
-#include "pinmap.h"
-#include "mbed_error.h"
-
-#define EDGE_NONE (0)
-#define EDGE_RISE (1)
-#define EDGE_FALL (2)
-#define EDGE_BOTH (3)
-
-// Number of EXTI irq vectors (EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5_9, EXTI10_15)
-#define CHANNEL_NUM (7)
-
-// Max pins for one line (max with EXTI10_15)
-#define MAX_PIN_LINE (6)
-
-typedef struct gpio_channel {
-    uint32_t pin_mask;                   // bitmask representing which pins are configured for receiving interrupts
-    uint32_t channel_ids[MAX_PIN_LINE];  // mbed "gpio_irq_t gpio_irq" field of instance
-    uint32_t channel_gpio[MAX_PIN_LINE]; // base address of gpio port group
-    uint32_t channel_pin[MAX_PIN_LINE];  // pin number in port group
-} gpio_channel_t;
-
-static gpio_channel_t channels[CHANNEL_NUM] = {
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0}
-};
-
-// Used to return the index for channels array.
-static uint32_t pin_base_nr[16] = {
-    // EXTI0
-    0, // pin 0
-    // EXTI1
-    0, // pin 1
-    // EXTI2
-    0, // pin 2
-    // EXTI3
-    0, // pin 3
-    // EXTI4
-    0, // pin 4
-    // EXTI5_9
-    0, // pin 5
-    1, // pin 6
-    2, // pin 7
-    3, // pin 8
-    4, // pin 9
-    // EXTI10_15
-    0, // pin 10
-    1, // pin 11
-    2, // pin 12
-    3, // pin 13
-    4, // pin 14
-    5  // pin 15
-};
-
-static gpio_irq_handler irq_handler;
-
-static void handle_interrupt_in(uint32_t irq_index, uint32_t max_num_pin_line)
-{
-    gpio_channel_t *gpio_channel = &channels[irq_index];
-    uint32_t gpio_idx;
-
-    for (gpio_idx = 0; gpio_idx < max_num_pin_line; gpio_idx++) {
-        uint32_t current_mask = (1 << gpio_idx);
-
-        if (gpio_channel->pin_mask & current_mask) {
-            // Retrieve the gpio and pin that generate the irq
-            GPIO_TypeDef *gpio = (GPIO_TypeDef *)(gpio_channel->channel_gpio[gpio_idx]);
-            uint32_t pin = (uint32_t)(1 << (gpio_channel->channel_pin[gpio_idx]));
-
-            // Clear interrupt flag
-            if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) {
-                __HAL_GPIO_EXTI_CLEAR_FLAG(pin);
-
-                if (gpio_channel->channel_ids[gpio_idx] == 0) continue;
-
-                // Check which edge has generated the irq
-                if ((gpio->IDR & pin) == 0) {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_FALL);
-                } else  {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_RISE);
-                }
-            }
-        }
-    }
-}
-
-// EXTI line 0
-static void gpio_irq0(void)
-{
-    handle_interrupt_in(0, 1);
-}
-
-// EXTI line 1
-static void gpio_irq1(void)
-{
-    handle_interrupt_in(1, 1);
-}
-
-// EXTI line 2
-static void gpio_irq2(void)
-{
-    handle_interrupt_in(2, 1);
-}
-
-// EXTI line 3
-static void gpio_irq3(void)
-{
-    handle_interrupt_in(3, 1);
-}
-
-// EXTI line 4
-static void gpio_irq4(void)
-{
-    handle_interrupt_in(4, 1);
-}
-
-// EXTI lines 5 to 9
-static void gpio_irq5(void)
-{
-    handle_interrupt_in(5, 5);
-}
-
-// EXTI lines 10 to 15
-static void gpio_irq6(void)
-{
-    handle_interrupt_in(6, 6);
-}
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
-
-int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
-{
-    IRQn_Type irq_n = (IRQn_Type)0;
-    uint32_t vector = 0;
-    uint32_t irq_index;
-    gpio_channel_t *gpio_channel;
-    uint32_t gpio_idx;
-
-    if (pin == NC) return -1;
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Select irq number and interrupt routine
-    switch (pin_index) {
-        case 0:
-            irq_n = EXTI0_IRQn;
-            vector = (uint32_t)&gpio_irq0;
-            irq_index = 0;
-            break;
-        case 1:
-            irq_n = EXTI1_IRQn;
-            vector = (uint32_t)&gpio_irq1;
-            irq_index = 1;
-            break;
-        case 2:
-            irq_n = EXTI2_IRQn;
-            vector = (uint32_t)&gpio_irq2;
-            irq_index = 2;
-            break;
-        case 3:
-            irq_n = EXTI3_IRQn;
-            vector = (uint32_t)&gpio_irq3;
-            irq_index = 3;
-            break;
-        case 4:
-            irq_n = EXTI4_IRQn;
-            vector = (uint32_t)&gpio_irq4;
-            irq_index = 4;
-            break;
-        case 5:
-        case 6:
-        case 7:
-        case 8:
-        case 9:
-            irq_n = EXTI9_5_IRQn;
-            vector = (uint32_t)&gpio_irq5;
-            irq_index = 5;
-            break;
-        case 10:
-        case 11:
-        case 12:
-        case 13:
-        case 14:
-        case 15:
-            irq_n = EXTI15_10_IRQn;
-            vector = (uint32_t)&gpio_irq6;
-            irq_index = 6;
-            break;
-        default:
-            error("InterruptIn error: pin not supported.\n");
-            return -1;
-    }
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-
-    // Configure GPIO
-    pin_function(pin, STM_PIN_DATA(STM_MODE_IT_FALLING, GPIO_NOPULL, 0));
-
-    // Enable EXTI interrupt
-    NVIC_SetVector(irq_n, vector);
-    NVIC_EnableIRQ(irq_n);
-
-    // Save informations for future use
-    obj->irq_n = irq_n;
-    obj->irq_index = irq_index;
-    obj->event = EDGE_NONE;
-    obj->pin = pin;
-
-    gpio_channel = &channels[irq_index];
-    gpio_idx = pin_base_nr[pin_index];
-    gpio_channel->pin_mask |= (1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = id;
-    gpio_channel->channel_gpio[gpio_idx] = gpio_add;
-    gpio_channel->channel_pin[gpio_idx] = pin_index;
-
-    irq_handler = handler;
-
-    return 0;
-}
-
-void gpio_irq_free(gpio_irq_t *obj)
-{
-    gpio_channel_t *gpio_channel = &channels[obj->irq_index];
-    uint32_t pin_index  = STM_PIN(obj->pin);
-    uint32_t gpio_addr = GPIOA_BASE + (GPIOB_BASE-GPIOA_BASE) * STM_PORT(obj->pin);
-    uint32_t gpio_idx = pin_base_nr[pin_index];
-    
-    HAL_GPIO_DeInit((GPIO_TypeDef *)gpio_addr, (1<<pin_index));
-    gpio_channel->pin_mask &= ~(1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = 0;
-    gpio_channel->channel_gpio[gpio_idx] = 0;
-    gpio_channel->channel_pin[gpio_idx] = 0;
-
-    // Disable EXTI line, but don't change pull-up config
-    pin_function_gpiomode(obj->pin, STM_MODE_INPUT);
-    obj->event = EDGE_NONE;
-}
-
-void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
-{
-    uint32_t mode = STM_MODE_IT_EVT_RESET;
-
-    if (enable) {
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or RISE
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or FALL
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            }
-        }
-    } else { // Disable
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            } else { // NONE or RISE
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            } else { // NONE or FALL
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-    }
-
-    pin_function_gpiomode(obj->pin, mode);
-}
-
-void gpio_irq_enable(gpio_irq_t *obj)
-{
-    NVIC_EnableIRQ(obj->irq_n);
-}
-
-void gpio_irq_disable(gpio_irq_t *obj)
-{
-    NVIC_DisableIRQ(obj->irq_n);
-    obj->event = EDGE_NONE;
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F2/gpio_irq_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,59 @@
+/* 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 "gpio_irq_device.h"
+
+// Used to return the index for channels array.
+const exti_lines_t pin_lines_desc[16] = {
+    // EXTI0
+    {.gpio_idx = 0, .irq_index = 0, .irq_n = EXTI0_IRQn}, // pin 0
+    // EXTI1
+    {.gpio_idx = 0, .irq_index = 1, .irq_n = EXTI1_IRQn}, // pin 1
+    // EXTI2
+    {.gpio_idx = 0, .irq_index = 2, .irq_n = EXTI2_IRQn}, // pin 2
+    // EXTI3
+    {.gpio_idx = 0, .irq_index = 3, .irq_n = EXTI3_IRQn}, // pin 3
+    // EXTI4
+    {.gpio_idx = 0, .irq_index = 4, .irq_n = EXTI4_IRQn}, // pin 4
+    // EXTI5_9
+    {.gpio_idx = 0, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 5
+    {.gpio_idx = 1, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 6
+    {.gpio_idx = 2, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 7
+    {.gpio_idx = 3, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 8
+    {.gpio_idx = 4, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 9
+    // EXTI10_15
+    {.gpio_idx = 0, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 10
+    {.gpio_idx = 1, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 11
+    {.gpio_idx = 2, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 12
+    {.gpio_idx = 3, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 13
+    {.gpio_idx = 4, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 14
+    {.gpio_idx = 5, .irq_index = 6, .irq_n = EXTI15_10_IRQn}// pin 15
+};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F2/gpio_irq_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,94 @@
+/* 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_GPIO_IRQ_DEVICE_H
+#define MBED_GPIO_IRQ_DEVICE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// when LL is available, below include can be used
+// #include "stm32f2xx_ll_exti.h"
+// until then let's define locally the required functions
+__STATIC_INLINE void LL_EXTI_EnableRisingTrig_0_31(uint32_t ExtiLine)
+{
+  SET_BIT(EXTI->RTSR, ExtiLine);
+}
+__STATIC_INLINE void LL_EXTI_DisableRisingTrig_0_31(uint32_t ExtiLine)
+{
+  CLEAR_BIT(EXTI->RTSR, ExtiLine);
+}
+__STATIC_INLINE void LL_EXTI_EnableFallingTrig_0_31(uint32_t ExtiLine)
+{
+  SET_BIT(EXTI->FTSR, ExtiLine);
+}
+__STATIC_INLINE void LL_EXTI_DisableFallingTrig_0_31(uint32_t ExtiLine)
+{
+  CLEAR_BIT(EXTI->FTSR, ExtiLine);
+}
+__STATIC_INLINE void LL_EXTI_EnableIT_0_31(uint32_t ExtiLine)
+{
+  SET_BIT(EXTI->IMR, ExtiLine);
+}
+__STATIC_INLINE void LL_EXTI_DisableIT_0_31(uint32_t ExtiLine)
+{
+  CLEAR_BIT(EXTI->IMR, ExtiLine);
+}
+// Above lines shall be later defined in LL
+
+// Number of EXTI irq vectors (EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5_9, EXTI10_15)
+#define CHANNEL_NUM (7)
+
+#define EXTI_IRQ0_NUM_LINES 1
+#define EXTI_IRQ1_NUM_LINES 1
+#define EXTI_IRQ2_NUM_LINES 1
+#define EXTI_IRQ3_NUM_LINES 1
+#define EXTI_IRQ4_NUM_LINES 1
+#define EXTI_IRQ5_NUM_LINES 5
+#define EXTI_IRQ6_NUM_LINES 6
+
+// Max pins for one line (max with EXTI10_15)
+#define MAX_PIN_LINE (EXTI_IRQ6_NUM_LINES)
+
+/*  Structure to describe how the HW EXTI lines are defined in this HW */
+typedef struct exti_lines {
+    uint32_t gpio_idx;   // an index entry for each EXIT line
+    uint32_t irq_index;  // the IRQ index
+    IRQn_Type  irq_n;    // the corresponding EXTI IRQn
+} exti_lines_t;
+
+// Used to return the index for channels array.
+extern const exti_lines_t pin_lines_desc[];
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F2/objects.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F2/objects.h	Tue Mar 14 16:40:56 2017 +0000
@@ -128,6 +128,7 @@
 struct pwmout_s {
     PWMName pwm;
     PinName pin;
+    uint32_t prescaler;
     uint32_t period;
     uint32_t pulse;
     uint8_t channel;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F2/pin_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,139 @@
+/* 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.
+ *******************************************************************************
+ */
+#ifndef MBED_PIN_DEVICE_H
+#define MBED_PIN_DEVICE_H
+
+#include "cmsis.h"
+
+// when LL is available, below include can be used
+// #include "stm32f2xx_ll_gpio.h"
+// until then let's define locally the required functions
+#define LL_GPIO_PIN_0                      GPIO_BSRR_BS0 /*!< Select pin 0 */
+#define LL_GPIO_PIN_1                      GPIO_BSRR_BS1 /*!< Select pin 1 */
+#define LL_GPIO_PIN_2                      GPIO_BSRR_BS2 /*!< Select pin 2 */
+#define LL_GPIO_PIN_3                      GPIO_BSRR_BS3 /*!< Select pin 3 */
+#define LL_GPIO_PIN_4                      GPIO_BSRR_BS4 /*!< Select pin 4 */
+#define LL_GPIO_PIN_5                      GPIO_BSRR_BS5 /*!< Select pin 5 */
+#define LL_GPIO_PIN_6                      GPIO_BSRR_BS6 /*!< Select pin 6 */
+#define LL_GPIO_PIN_7                      GPIO_BSRR_BS7 /*!< Select pin 7 */
+#define LL_GPIO_PIN_8                      GPIO_BSRR_BS8 /*!< Select pin 8 */
+#define LL_GPIO_PIN_9                      GPIO_BSRR_BS9 /*!< Select pin 9 */
+#define LL_GPIO_PIN_10                     GPIO_BSRR_BS10 /*!< Select pin 10 */
+#define LL_GPIO_PIN_11                     GPIO_BSRR_BS11 /*!< Select pin 11 */
+#define LL_GPIO_PIN_12                     GPIO_BSRR_BS12 /*!< Select pin 12 */
+#define LL_GPIO_PIN_13                     GPIO_BSRR_BS13 /*!< Select pin 13 */
+#define LL_GPIO_PIN_14                     GPIO_BSRR_BS14 /*!< Select pin 14 */
+#define LL_GPIO_PIN_15                     GPIO_BSRR_BS15 /*!< Select pin 15 */
+
+#define LL_GPIO_MODE_INPUT                 ((uint32_t)0x00000000U) /*!< Select input mode */
+#define LL_GPIO_MODE_OUTPUT                GPIO_MODER_MODE0_0  /*!< Select output mode */
+#define LL_GPIO_MODE_ALTERNATE             GPIO_MODER_MODE0_1  /*!< Select alternate function mode */
+#define LL_GPIO_MODE_ANALOG                GPIO_MODER_MODE0    /*!< Select analog mode */
+
+#define LL_GPIO_OUTPUT_PUSHPULL            ((uint32_t)0x00000000U) /*!< Select push-pull as output type */
+#define LL_GPIO_OUTPUT_OPENDRAIN           GPIO_OTYPER_OT0 /*!< Select open-drain as output type */
+
+#define LL_GPIO_PULL_NO                    ((uint32_t)0x00000000U) /*!< Select I/O no pull */
+#define LL_GPIO_PULL_UP                    GPIO_PUPDR_PUPD0_0 /*!< Select I/O pull up */
+#define LL_GPIO_PULL_DOWN                  GPIO_PUPDR_PUPD0_1 /*!< Select I/O pull down */
+
+#define LL_GPIO_SPEED_FREQ_LOW             ((uint32_t)0x00000000U) /*!< Select I/O low output speed    */
+#define LL_GPIO_SPEED_FREQ_MEDIUM          GPIO_OSPEEDER_OSPEEDR0_0 /*!< Select I/O medium output speed */
+#define LL_GPIO_SPEED_FREQ_HIGH            GPIO_OSPEEDER_OSPEEDR0_1 /*!< Select I/O fast output speed   */
+#define LL_GPIO_SPEED_FREQ_VERY_HIGH       GPIO_OSPEEDER_OSPEEDR0   /*!< Select I/O high output speed   */
+
+__STATIC_INLINE void LL_GPIO_SetAFPin_0_7(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Alternate)
+{
+  MODIFY_REG(GPIOx->AFR[0], (GPIO_AFRL_AFSEL0 << (POSITION_VAL(Pin) * 4U)),
+             (Alternate << (POSITION_VAL(Pin) * 4U)));
+}
+
+__STATIC_INLINE void LL_GPIO_SetAFPin_8_15(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Alternate)
+{
+  MODIFY_REG(GPIOx->AFR[1], (GPIO_AFRH_AFSEL8 << (POSITION_VAL(Pin >> 8U) * 4U)),
+             (Alternate << (POSITION_VAL(Pin >> 8U) * 4U)));
+}
+__STATIC_INLINE void LL_GPIO_SetPinMode(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Mode)
+{
+  MODIFY_REG(GPIOx->MODER, (GPIO_MODER_MODE0 << (POSITION_VAL(Pin) * 2U)), (Mode << (POSITION_VAL(Pin) * 2U)));
+}
+__STATIC_INLINE uint32_t LL_GPIO_GetPinMode(GPIO_TypeDef *GPIOx, uint32_t Pin)
+{
+  return (uint32_t)(READ_BIT(GPIOx->MODER, ((Pin * Pin) * GPIO_MODER_MODER0)) / (Pin * Pin));
+}
+__STATIC_INLINE void LL_GPIO_SetPinPull(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Pull)
+{
+  MODIFY_REG(GPIOx->PUPDR, (GPIO_PUPDR_PUPD0 << (POSITION_VAL(Pin) * 2U)), (Pull << (POSITION_VAL(Pin) * 2U)));
+}
+__STATIC_INLINE void LL_GPIO_SetPinOutputType(GPIO_TypeDef *GPIOx, uint32_t PinMask, uint32_t OutputType)
+{
+  MODIFY_REG(GPIOx->OTYPER, PinMask, (PinMask * OutputType));
+}
+__STATIC_INLINE void LL_GPIO_SetPinSpeed(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t  Speed)
+{
+  MODIFY_REG(GPIOx->OSPEEDR, (GPIO_OSPEEDER_OSPEEDR0 << (POSITION_VAL(Pin) * 2U)),
+             (Speed << (POSITION_VAL(Pin) * 2U)));
+}
+// Above lines shall be defined in LL when available
+
+extern const uint32_t ll_pin_defines[16];
+
+/* Family specific implementations */
+static inline void stm_pin_DisconnectDebug(PinName pin)
+{
+    /* empty for now */
+}
+
+static inline void stm_pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t pull_config)
+{
+    switch (pull_config) {
+        case GPIO_PULLUP:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_UP);
+            break;
+        case GPIO_PULLDOWN:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_DOWN);
+            break;
+        default:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_NO);
+            break;
+    }
+}
+
+static inline void stm_pin_SetAFPin( GPIO_TypeDef *gpio, PinName pin, uint32_t afnum)
+{
+    uint32_t ll_pin  = ll_pin_defines[STM_PIN(pin)];
+
+    if (STM_PIN(pin) > 7)
+        LL_GPIO_SetAFPin_8_15(gpio, ll_pin, afnum);
+    else
+        LL_GPIO_SetAFPin_0_7(gpio, ll_pin, afnum);
+}
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F2/pinmap.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,197 +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 "mbed_assert.h"
-#include "pinmap.h"
-#include "PortNames.h"
-#include "mbed_error.h"
-
-// GPIO mode look-up table
-static const uint32_t gpio_mode[13] = {
-    0x00000000, //  0 = GPIO_MODE_INPUT
-    0x00000001, //  1 = GPIO_MODE_OUTPUT_PP
-    0x00000011, //  2 = GPIO_MODE_OUTPUT_OD
-    0x00000002, //  3 = GPIO_MODE_AF_PP
-    0x00000012, //  4 = GPIO_MODE_AF_OD
-    0x00000003, //  5 = GPIO_MODE_ANALOG
-    0x10110000, //  6 = GPIO_MODE_IT_RISING
-    0x10210000, //  7 = GPIO_MODE_IT_FALLING
-    0x10310000, //  8 = GPIO_MODE_IT_RISING_FALLING
-    0x10120000, //  9 = GPIO_MODE_EVT_RISING
-    0x10220000, // 10 = GPIO_MODE_EVT_FALLING
-    0x10320000, // 11 = GPIO_MODE_EVT_RISING_FALLING
-    0x10000000  // 12 = Reset GPIO_MODE_IT_EVT
-};
-
-// Enable GPIO clock and return GPIO base address
-uint32_t Set_GPIO_Clock(uint32_t port_idx)
-{
-    uint32_t gpio_add = 0;
-    switch (port_idx) {
-        case PortA:
-            gpio_add = GPIOA_BASE;
-            __GPIOA_CLK_ENABLE();
-            break;
-        case PortB:
-            gpio_add = GPIOB_BASE;
-            __GPIOB_CLK_ENABLE();
-            break;
-        case PortC:
-            gpio_add = GPIOC_BASE;
-            __GPIOC_CLK_ENABLE();
-            break;
-#if defined GPIOD_BASE
-        case PortD:
-            gpio_add = GPIOD_BASE;
-            __GPIOD_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOE_BASE
-        case PortE:
-            gpio_add = GPIOE_BASE;
-            __GPIOE_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOF_BASE
-        case PortF:
-            gpio_add = GPIOF_BASE;
-            __GPIOF_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOG_BASE
-        case PortG:
-            gpio_add = GPIOG_BASE;
-            __GPIOG_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOH_BASE
-        case PortH:
-            gpio_add = GPIOH_BASE;
-            __GPIOH_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOI_BASE
-        case PortI:
-            gpio_add = GPIOI_BASE;
-            __GPIOI_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOJ_BASE
-        case PortJ:
-            gpio_add = GPIOJ_BASE;
-            __GPIOJ_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOK_BASE
-        case PortK:
-            gpio_add = GPIOK_BASE;
-            __GPIOK_CLK_ENABLE();
-            break;
-#endif
-        default:
-            error("Pinmap error: wrong port number.");
-            break;
-    }
-    return gpio_add;
-}
-
-/**
- * Configure pin (mode, speed, output type and pull-up/pull-down)
- */
-void pin_function(PinName pin, int data)
-{
-    MBED_ASSERT(pin != (PinName)NC);
-    // Get the pin informations
-    uint32_t mode  = STM_PIN_MODE(data);
-    uint32_t pupd  = STM_PIN_PUPD(data);
-    uint32_t afnum = STM_PIN_AFNUM(data);
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Configure GPIO
-    GPIO_InitTypeDef GPIO_InitStructure;
-    GPIO_InitStructure.Pin       = (uint32_t)(1 << pin_index);
-    GPIO_InitStructure.Mode      = gpio_mode[mode];
-    GPIO_InitStructure.Pull      = pupd;
-    GPIO_InitStructure.Speed     = GPIO_SPEED_HIGH;
-    GPIO_InitStructure.Alternate = afnum;
-    HAL_GPIO_Init(gpio, &GPIO_InitStructure);
-
-    // [TODO] Disconnect JTAG-DP + SW-DP signals.
-    // Warning: Need to reconnect under reset
-    //if ((pin == PA_13) || (pin == PA_14)) {
-    //
-    //}
-    //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) {
-    //
-    //}
-}
-
-/**
- * Configure pin pull-up/pull-down
- */
-void pin_mode(PinName pin, PinMode mode)
-{
-    MBED_ASSERT(pin != (PinName)NC);
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Configure pull-up/pull-down resistors
-    uint32_t pupd = (uint32_t)mode;
-    if (pupd > 2) {
-        pupd = 0; // Open-drain = No pull-up/No pull-down
-    }
-    gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2)));
-    gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
-}
-
-/*  Internal function for setting the gpiomode/function
- *  without changing Pull mode
- */
-void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
-
-    /* Read current pull state from HW to avoid over-write*/
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
-    uint32_t temp = gpio->PUPDR;
-    uint32_t pull = (temp  >> (pin_index * 2U)) & GPIO_PUPDR_PUPDR0;
-
-    /* Then re-use global function for updating the mode part*/
-    pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
-}
--- a/targets/TARGET_STM/TARGET_STM32F2/port_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,103 +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 "port_api.h"
-#include "pinmap.h"
-#include "gpio_api.h"
-#include "mbed_error.h"
-
-#if DEVICE_PORTIN || DEVICE_PORTOUT
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-
-// high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
-// low nibble  = pin number
-PinName port_pin(PortName port, int pin_n)
-{
-    return (PinName)(pin_n + (port << 4));
-}
-
-void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
-{
-    uint32_t port_index = (uint32_t)port;
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Fill PORT object structure for future use
-    obj->port      = port;
-    obj->mask      = mask;
-    obj->direction = dir;
-    obj->reg_in    = &gpio->IDR;
-    obj->reg_out   = &gpio->ODR;
-
-    port_dir(obj, dir);
-}
-
-void port_dir(port_t *obj, PinDirection dir)
-{
-    uint32_t i;
-    obj->direction = dir;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            if (dir == PIN_OUTPUT) {
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
-            } else { // PIN_INPUT
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
-            }
-        }
-    }
-}
-
-void port_mode(port_t *obj, PinMode mode)
-{
-    uint32_t i;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            pin_mode(port_pin(obj->port, i), mode);
-        }
-    }
-}
-
-void port_write(port_t *obj, int value)
-{
-    *obj->reg_out = (*obj->reg_out & ~obj->mask) | (value & obj->mask);
-}
-
-int port_read(port_t *obj)
-{
-    if (obj->direction == PIN_OUTPUT) {
-        return (*obj->reg_out & obj->mask);
-    } else { // PIN_INPUT
-        return (*obj->reg_in & obj->mask);
-    }
-}
-
-#endif
--- a/targets/TARGET_STM/TARGET_STM32F2/pwmout_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,57 @@
+/* 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;
+
+extern const pwm_apb_map_t pwm_apb_map_table[];
+
+#endif // DEVICE_PWMOUT
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F3/analogin_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F3/analogin_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -30,7 +30,7 @@
 
 #if DEVICE_ANALOGIN
 
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "cmsis.h"
 #include "pinmap.h"
 #include "mbed_error.h"
--- a/targets/TARGET_STM/TARGET_STM32F3/can_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,485 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2016 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "can_api.h"
-
-#if DEVICE_CAN
-
-#include "cmsis.h"
-#include "pinmap.h"
-#include "PeripheralPins.h"
-#include "mbed_error.h"
-#include <math.h>
-#include <string.h>
-
-#define CAN_NUM    1
-static CAN_HandleTypeDef CanHandle;
-static uint32_t can_irq_ids[CAN_NUM] = {0};
-static can_irq_handler irq_handler;
-
-void can_init(can_t *obj, PinName rd, PinName td) 
-{
-    CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
-    CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
-    obj->can = (CANName)pinmap_merge(can_rd, can_td);
-    MBED_ASSERT((int)obj->can != NC);    
-
-    if(obj->can == CAN_1) {
-        __CAN_CLK_ENABLE();
-        obj->index = 0;
-    }
-
-    // Configure the CAN pins
-    pinmap_pinout(rd, PinMap_CAN_RD);
-    pinmap_pinout(td, PinMap_CAN_TD);
-    if (rd != NC) {
-        pin_mode(rd, PullUp);
-    }
-    if (td != NC) {
-        pin_mode(td, PullUp);
-    }
-
-    CanHandle.Instance = (CAN_TypeDef *)(obj->can);
-
-    CanHandle.Init.TTCM = DISABLE;
-    CanHandle.Init.ABOM = DISABLE;
-    CanHandle.Init.AWUM = DISABLE;
-    CanHandle.Init.NART = DISABLE;
-    CanHandle.Init.RFLM = DISABLE;
-    CanHandle.Init.TXFP = DISABLE;
-    CanHandle.Init.Mode = CAN_MODE_NORMAL;
-    CanHandle.Init.SJW = CAN_SJW_1TQ;
-    CanHandle.Init.BS1 = CAN_BS1_6TQ;
-    CanHandle.Init.BS2 = CAN_BS2_8TQ;
-    CanHandle.Init.Prescaler = 2;
-
-    if (HAL_CAN_Init(&CanHandle) != HAL_OK) {
-       error("Cannot initialize CAN");
-    }
-    // Set initial CAN frequency to 100kb/s
-    can_frequency(obj, 100000);
-
-    can_filter(obj, 0, 0, CANStandard, 0);
-}
-
-void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id) 
-{
-    irq_handler = handler;
-    can_irq_ids[obj->index] = id;    
-}
-
-void can_irq_free(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-  
-    can->IER &= ~(CAN_IT_FMP0 | CAN_IT_FMP1 | CAN_IT_TME | \
-                  CAN_IT_ERR | CAN_IT_EPV | CAN_IT_BOF);
-    can_irq_ids[obj->can] = 0;
-}
-
-void can_free(can_t *obj) 
-{
-    // Reset CAN and disable clock
-    if (obj->can == CAN_1) {
-        __CAN_FORCE_RESET();
-        __CAN_RELEASE_RESET();
-        __CAN_CLK_DISABLE();
-    }
-}
-
-// The following table is used to program bit_timing. It is an adjustment of the sample
-// point by synchronizing on the start-bit edge and resynchronizing on the following edges.
-// This table has the sampling points as close to 75% as possible (most commonly used).
-// The first value is TSEG1, the second TSEG2.
-static const int timing_pts[23][2] = {
-    {0x0, 0x0},      // 2,  50%
-    {0x1, 0x0},      // 3,  67%
-    {0x2, 0x0},      // 4,  75%
-    {0x3, 0x0},      // 5,  80%
-    {0x3, 0x1},      // 6,  67%
-    {0x4, 0x1},      // 7,  71%
-    {0x5, 0x1},      // 8,  75%
-    {0x6, 0x1},      // 9,  78%
-    {0x6, 0x2},      // 10, 70%
-    {0x7, 0x2},      // 11, 73%
-    {0x8, 0x2},      // 12, 75%
-    {0x9, 0x2},      // 13, 77%
-    {0x9, 0x3},      // 14, 71%
-    {0xA, 0x3},      // 15, 73%
-    {0xB, 0x3},      // 16, 75%
-    {0xC, 0x3},      // 17, 76%
-    {0xD, 0x3},      // 18, 78%
-    {0xD, 0x4},      // 19, 74%
-    {0xE, 0x4},      // 20, 75%
-    {0xF, 0x4},      // 21, 76%
-    {0xF, 0x5},      // 22, 73%
-    {0xF, 0x6},      // 23, 70%
-    {0xF, 0x7},      // 24, 67%
-};
-
-static unsigned int can_speed(unsigned int pclk, unsigned int cclk, unsigned char psjw) 
-{
-    uint32_t    btr;
-    uint16_t    brp = 0;
-    uint32_t    calcbit;
-    uint32_t    bitwidth;
-    int         hit = 0;
-    int         bits;
-    
-    bitwidth = (pclk / cclk);
-    
-    brp = bitwidth / 0x18;
-    while ((!hit) && (brp < bitwidth / 4)) {
-        brp++;
-        for (bits = 22; bits > 0; bits--) {
-            calcbit = (bits + 3) * (brp + 1);
-            if (calcbit == bitwidth) {
-                hit = 1;
-                break;
-            }
-        }
-    }
-    
-    if (hit) {
-        btr = ((timing_pts[bits][1] << 20) & 0x00700000)
-            | ((timing_pts[bits][0] << 16) & 0x000F0000)
-            | ((psjw                << 24) & 0x0000C000)
-            | ((brp                 <<  0) & 0x000003FF);
-    } else {
-        btr = 0xFFFFFFFF;
-    }
-    
-    return btr;
-
-}
-
-int can_frequency(can_t *obj, int f) 
-{
-    int pclk = HAL_RCC_GetPCLK1Freq();
-    int btr = can_speed(pclk, (unsigned int)f, 1);
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    if (btr > 0) {
-        can->MCR |= CAN_MCR_INRQ ;
-        while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-        }
-        can->BTR = btr;
-        can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-        while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-        }
-        return 1;
-    } else {
-        return 0;
-    }
-}
-
-int can_write(can_t *obj, CAN_Message msg, int cc) 
-{
-    uint32_t  transmitmailbox = 5;
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    /* Select one empty transmit mailbox */
-    if ((can->TSR&CAN_TSR_TME0) == CAN_TSR_TME0) {
-        transmitmailbox = 0;
-    } else if ((can->TSR&CAN_TSR_TME1) == CAN_TSR_TME1) {
-        transmitmailbox = 1;
-    } else if ((can->TSR&CAN_TSR_TME2) == CAN_TSR_TME2) {
-        transmitmailbox = 2;
-    } else {
-        transmitmailbox = CAN_TXSTATUS_NOMAILBOX;
-    }
-
-    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) |
-                                             ((uint32_t)msg.data[4]));
-    /* Request transmission */
-    can->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ;
-  }
-
-  return 1;
-}
-
-int can_read(can_t *obj, CAN_Message *msg, int handle) 
-{
-    //handle is the FIFO number
-
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    
-    // check FPM0 which holds the pending message count in FIFO 0
-    // if no message is pending, return 0
-    if ((can->RF0R & CAN_RF0R_FMP0) == 0) {
-        return 0;
-    }
-
-    /* Get the Id */
-    msg->format = (CANFormat)((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR);
-    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);
-    /* Get the DLC */
-    msg->len = (uint8_t)0x0F & can->sFIFOMailBox[handle].RDTR;
-//  /* Get the FMI */
-//  msg->FMI = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDTR >> 8);
-    /* Get the data field */
-    msg->data[0] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDLR;
-    msg->data[1] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 8);
-    msg->data[2] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 16);
-    msg->data[3] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 24);
-    msg->data[4] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDHR;
-    msg->data[5] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 8);
-    msg->data[6] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 16);
-    msg->data[7] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 24);
-    
-    /* Release the FIFO */
-    if(handle == CAN_FIFO0) {
-        /* Release FIFO0 */
-        can->RF0R |= CAN_RF0R_RFOM0;
-    } else { /* FIFONumber == CAN_FIFO1 */
-      /* Release FIFO1 */
-      can->RF1R |= CAN_RF1R_RFOM1;
-    }
-
-    return 1;
-}
-
-void can_reset(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    can->MCR |= CAN_MCR_RESET;  
-    can->ESR = 0x0;
-}
-
-unsigned char can_rderror(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    return (can->ESR >> 24) & 0xFF;
-}
-
-unsigned char can_tderror(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    return (can->ESR >> 16) & 0xFF;
-}
-
-void can_monitor(can_t *obj, int silent) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);        
-    
-    can->MCR |= CAN_MCR_INRQ ;
-    while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-    }
-    if (silent) {
-        can->BTR |= ((uint32_t)1 << 31);
-    } else {
-        can->BTR &= ~((uint32_t)1 << 31);
-    }
-    can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-    while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-    }
-}
-
-int can_mode(can_t *obj, CanMode mode) 
-{
-    int success = 0;
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-    can->MCR |= CAN_MCR_INRQ ;
-    while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-    }
-    switch (mode) {
-        case MODE_NORMAL:
-            can->BTR &= ~(CAN_BTR_SILM | CAN_BTR_LBKM);
-            success = 1;
-            break;
-        case MODE_SILENT:
-            can->BTR |= CAN_BTR_SILM;
-            can->BTR &= ~CAN_BTR_LBKM;
-            success = 1;
-            break;
-        case MODE_TEST_GLOBAL:
-        case MODE_TEST_LOCAL:
-            can->BTR |= CAN_BTR_LBKM;
-            can->BTR &= ~CAN_BTR_SILM;
-            success = 1;
-            break;
-        case MODE_TEST_SILENT:
-            can->BTR |= (CAN_BTR_SILM | CAN_BTR_LBKM);
-            success = 1;
-            break;
-        default:
-            success = 0;
-            break;
-    }
-    can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-    while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-    }
-    return success;
-}
-
-int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) 
-{
-    CanHandle.Instance = (CAN_TypeDef *)(obj->can);
-    CAN_FilterConfTypeDef  sFilterConfig;
-    
-    sFilterConfig.FilterNumber = handle;
-    sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
-    sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
-    sFilterConfig.FilterIdHigh = (uint8_t) (id >> 8);
-    sFilterConfig.FilterIdLow = (uint8_t) id;
-    sFilterConfig.FilterMaskIdHigh = (uint8_t) (mask >> 8);
-    sFilterConfig.FilterMaskIdLow = (uint8_t) mask;
-    sFilterConfig.FilterFIFOAssignment = 0;
-    sFilterConfig.FilterActivation = ENABLE;
-    sFilterConfig.BankNumber = 14 + handle;
-  
-    HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig);
-      
-    return 0;
-}
-
-static void can_irq(CANName name, int id) 
-{
-    uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0;    
-    CanHandle.Instance = (CAN_TypeDef *)name;
-    
-    if(__HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_TME)) {
-        tmp1 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_0);
-        tmp2 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_1);
-        tmp3 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_2);
-        if(tmp1 || tmp2 || tmp3)  
-        {
-            irq_handler(can_irq_ids[id], IRQ_TX);
-        }
-    }
-  
-    tmp1 = __HAL_CAN_MSG_PENDING(&CanHandle, CAN_FIFO0);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_FMP0);
-  
-    if((tmp1 != 0) && tmp2) {
-         irq_handler(can_irq_ids[id], IRQ_RX);
-    }
-  
-    tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_EPV);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_EPV);
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR); 
-  
-    if(tmp1 && tmp2 && tmp3) {
-         irq_handler(can_irq_ids[id], IRQ_PASSIVE);
-    }
-  
-    tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_BOF);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_BOF);
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);  
-    if(tmp1 && tmp2 && tmp3) {
-        irq_handler(can_irq_ids[id], IRQ_BUS);
-    }
-  
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);  
-    if(tmp1 && tmp2 && tmp3) {
-        irq_handler(can_irq_ids[id], IRQ_ERROR);
-    }  
-}
-
-void CAN_RX0_IRQHandler(void) 
-{
-    can_irq(CAN_1, 0);
-}
-
-void CAN_TX_IRQHandler(void) 
-{
-    can_irq(CAN_1, 0);
-}
-
-void CAN_SCE_IRQHandler(void) 
-{
-    can_irq(CAN_1, 0);
-}
-
-void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
-{
-
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-    IRQn_Type irq_n = (IRQn_Type)0;
-    uint32_t vector = 0;    
-    uint32_t ier;
-
-    if(obj->can == CAN_1) {
-        switch (type) {
-            case IRQ_RX:
-                ier = CAN_IT_FMP0;
-                irq_n = CAN_RX0_IRQn;
-                vector = (uint32_t)&CAN_RX0_IRQHandler;
-                break;
-            case IRQ_TX:      
-                ier = CAN_IT_TME;
-                irq_n = CAN_TX_IRQn;
-                vector = (uint32_t)&CAN_TX_IRQHandler;
-                break;
-            case IRQ_ERROR:   
-                ier = CAN_IT_ERR;
-                irq_n = CAN_SCE_IRQn;
-                vector = (uint32_t)&CAN_SCE_IRQHandler;
-                break;
-            case IRQ_PASSIVE:
-                ier = CAN_IT_EPV;
-                irq_n = CAN_SCE_IRQn;
-                vector = (uint32_t)&CAN_SCE_IRQHandler;
-                break;
-            case IRQ_BUS:
-                ier = CAN_IT_BOF;
-                irq_n = CAN_SCE_IRQn;
-                vector = (uint32_t)&CAN_SCE_IRQHandler;
-                break;
-            default: return;
-        }
-    } 
-
-    if(enable) {
-        can->IER |= ier;
-    } else {
-        can->IER &= ~ier;
-    }
-
-    NVIC_SetVector(irq_n, vector);
-    NVIC_EnableIRQ(irq_n);
-}
-
-#endif // DEVICE_CAN
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F3/can_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,42 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef MBED_CAN_DEVICE_H
+#define MBED_CAN_DEVICE_H
+
+#include "cmsis.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef DEVICE_CAN
+
+#define CAN_NUM    1 // Number of CAN peripherals present in the STM32 serie
+
+#define CAN1_IRQ_RX_IRQN        CAN_RX0_IRQn
+#define CAN1_IRQ_RX_VECT        CAN_RX0_IRQHandler
+#define CAN1_IRQ_TX_IRQN        CAN_TX_IRQn
+#define CAN1_IRQ_TX_VECT        CAN_TX_IRQHandler
+#define CAN1_IRQ_ERROR_IRQN     CAN_SCE_IRQn
+#define CAN1_IRQ_ERROR_VECT     CAN_SCE_IRQHandler
+#define CAN1_IRQ_PASSIVE_IRQN   CAN_SCE_IRQn
+#define CAN1_IRQ_PASSIVE_VECT   CAN_SCE_IRQHandler
+#define CAN1_IRQ_BUS_IRQN       CAN_SCE_IRQn
+#define CAN1_IRQ_BUS_VECT       CAN_SCE_IRQHandler
+
+#endif // DEVICE_CAN
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F3/gpio_irq_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,334 +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 <stddef.h>
-#include "cmsis.h"
-#include "gpio_irq_api.h"
-#include "pinmap.h"
-#include "mbed_error.h"
-
-#define EDGE_NONE (0)
-#define EDGE_RISE (1)
-#define EDGE_FALL (2)
-#define EDGE_BOTH (3)
-
-// Number of EXTI irq vectors (EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5_9, EXTI10_15)
-#define CHANNEL_NUM (7)
-
-// Max pins for one line (max with EXTI10_15)
-#define MAX_PIN_LINE (6)
-
-typedef struct gpio_channel {
-    uint32_t pin_mask;                   // bitmask representing which pins are configured for receiving interrupts
-    uint32_t channel_ids[MAX_PIN_LINE];  // mbed "gpio_irq_t gpio_irq" field of instance
-    uint32_t channel_gpio[MAX_PIN_LINE]; // base address of gpio port group
-    uint32_t channel_pin[MAX_PIN_LINE];  // pin number in port group
-} gpio_channel_t;
-
-static gpio_channel_t channels[CHANNEL_NUM] = {
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0}
-};
-
-// Used to return the index for channels array.
-static uint32_t pin_base_nr[16] = {
-    // EXTI0
-    0, // pin 0
-    // EXTI1
-    0, // pin 1
-    // EXTI2
-    0, // pin 2
-    // EXTI3
-    0, // pin 3
-    // EXTI4
-    0, // pin 4
-    // EXTI5_9
-    0, // pin 5
-    1, // pin 6
-    2, // pin 7
-    3, // pin 8
-    4, // pin 9
-    // EXTI10_15
-    0, // pin 10
-    1, // pin 11
-    2, // pin 12
-    3, // pin 13
-    4, // pin 14
-    5  // pin 15
-};
-
-static gpio_irq_handler irq_handler;
-
-static void handle_interrupt_in(uint32_t irq_index, uint32_t max_num_pin_line)
-{
-    gpio_channel_t *gpio_channel = &channels[irq_index];
-    uint32_t gpio_idx;
-
-    for (gpio_idx = 0; gpio_idx < max_num_pin_line; gpio_idx++) {
-        uint32_t current_mask = (1 << gpio_idx);
-
-        if (gpio_channel->pin_mask & current_mask) {
-            // Retrieve the gpio and pin that generate the irq
-            GPIO_TypeDef *gpio = (GPIO_TypeDef *)(gpio_channel->channel_gpio[gpio_idx]);
-            uint32_t pin = (uint32_t)(1 << (gpio_channel->channel_pin[gpio_idx]));
-
-            // Clear interrupt flag
-            if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) {
-                __HAL_GPIO_EXTI_CLEAR_FLAG(pin);
-
-                if (gpio_channel->channel_ids[gpio_idx] == 0) continue;
-
-                // Check which edge has generated the irq
-                if ((gpio->IDR & pin) == 0) {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_FALL);
-                } else  {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_RISE);
-                }
-            }
-        }
-    }
-}
-
-// EXTI line 0
-static void gpio_irq0(void)
-{
-    handle_interrupt_in(0, 1);
-}
-
-// EXTI line 1
-static void gpio_irq1(void)
-{
-    handle_interrupt_in(1, 1);
-}
-
-// EXTI line 2
-static void gpio_irq2(void)
-{
-    handle_interrupt_in(2, 1);
-}
-
-// EXTI line 3
-static void gpio_irq3(void)
-{
-    handle_interrupt_in(3, 1);
-}
-
-// EXTI line 4
-static void gpio_irq4(void)
-{
-    handle_interrupt_in(4, 1);
-}
-
-// EXTI lines 5 to 9
-static void gpio_irq5(void)
-{
-    handle_interrupt_in(5, 5);
-}
-
-// EXTI lines 10 to 15
-static void gpio_irq6(void)
-{
-    handle_interrupt_in(6, 6);
-}
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
-
-int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
-{
-    IRQn_Type irq_n = (IRQn_Type)0;
-    uint32_t vector = 0;
-    uint32_t irq_index;
-    gpio_channel_t *gpio_channel;
-    uint32_t gpio_idx;
-
-    if (pin == NC) return -1;
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Select irq number and interrupt routine
-    switch (pin_index) {
-        case 0:
-            irq_n = EXTI0_IRQn;
-            vector = (uint32_t)&gpio_irq0;
-            irq_index = 0;
-            break;
-        case 1:
-            irq_n = EXTI1_IRQn;
-            vector = (uint32_t)&gpio_irq1;
-            irq_index = 1;
-            break;
-        case 2:
-            irq_n = EXTI2_TSC_IRQn;
-            vector = (uint32_t)&gpio_irq2;
-            irq_index = 2;
-            break;
-        case 3:
-            irq_n = EXTI3_IRQn;
-            vector = (uint32_t)&gpio_irq3;
-            irq_index = 3;
-            break;
-        case 4:
-            irq_n = EXTI4_IRQn;
-            vector = (uint32_t)&gpio_irq4;
-            irq_index = 4;
-            break;
-        case 5:
-        case 6:
-        case 7:
-        case 8:
-        case 9:
-            irq_n = EXTI9_5_IRQn;
-            vector = (uint32_t)&gpio_irq5;
-            irq_index = 5;
-            break;
-        case 10:
-        case 11:
-        case 12:
-        case 13:
-        case 14:
-        case 15:
-            irq_n = EXTI15_10_IRQn;
-            vector = (uint32_t)&gpio_irq6;
-            irq_index = 6;
-            break;
-        default:
-            error("InterruptIn error: pin not supported.\n");
-            return -1;
-    }
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-
-    // Configure GPIO
-    pin_function(pin, STM_PIN_DATA(STM_MODE_IT_FALLING, GPIO_NOPULL, 0));
-
-    // Enable EXTI interrupt
-    NVIC_SetVector(irq_n, vector);
-    NVIC_EnableIRQ(irq_n);
-
-    // Save informations for future use
-    obj->irq_n = irq_n;
-    obj->irq_index = irq_index;
-    obj->event = EDGE_NONE;
-    obj->pin = pin;
-
-    gpio_channel = &channels[irq_index];
-    gpio_idx = pin_base_nr[pin_index];
-    gpio_channel->pin_mask |= (1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = id;
-    gpio_channel->channel_gpio[gpio_idx] = gpio_add;
-    gpio_channel->channel_pin[gpio_idx] = pin_index;
-
-    irq_handler = handler;
-
-    return 0;
-}
-
-void gpio_irq_free(gpio_irq_t *obj)
-{
-    gpio_channel_t *gpio_channel = &channels[obj->irq_index];
-    uint32_t pin_index  = STM_PIN(obj->pin);
-    uint32_t gpio_addr = GPIOA_BASE + (GPIOB_BASE-GPIOA_BASE) * STM_PORT(obj->pin);
-    uint32_t gpio_idx = pin_base_nr[pin_index];
-    
-    HAL_GPIO_DeInit((GPIO_TypeDef *)gpio_addr, (1<<pin_index));
-    gpio_channel->pin_mask &= ~(1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = 0;
-    gpio_channel->channel_gpio[gpio_idx] = 0;
-    gpio_channel->channel_pin[gpio_idx] = 0;
-
-    // Disable EXTI line, but don't change pull-up config
-    pin_function_gpiomode(obj->pin, STM_MODE_INPUT);
-    obj->event = EDGE_NONE;
-}
-
-void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
-{
-    uint32_t mode = STM_MODE_INPUT;
-
-    if (enable) {
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or RISE
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or FALL
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            }
-        }
-    } else { // Disable
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            } else { // NONE or RISE
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            } else { // NONE or FALL
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-    }
-
-    pin_function_gpiomode(obj->pin, mode);
-}
-
-void gpio_irq_enable(gpio_irq_t *obj)
-{
-    NVIC_EnableIRQ(obj->irq_n);
-}
-
-void gpio_irq_disable(gpio_irq_t *obj)
-{
-    NVIC_DisableIRQ(obj->irq_n);
-    obj->event = EDGE_NONE;
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F3/gpio_irq_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,59 @@
+/* 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 "gpio_irq_device.h"
+
+// Used to return the index for channels array.
+const exti_lines_t pin_lines_desc[16] = {
+    // EXTI0
+    {.gpio_idx = 0, .irq_index = 0, .irq_n = EXTI0_IRQn}, // pin 0
+    // EXTI1
+    {.gpio_idx = 0, .irq_index = 1, .irq_n = EXTI1_IRQn}, // pin 1
+    // EXTI2
+    {.gpio_idx = 0, .irq_index = 2, .irq_n = EXTI2_TSC_IRQn}, // pin 2
+    // EXTI3
+    {.gpio_idx = 0, .irq_index = 3, .irq_n = EXTI3_IRQn}, // pin 3
+    // EXTI4
+    {.gpio_idx = 0, .irq_index = 4, .irq_n = EXTI4_IRQn}, // pin 4
+    // EXTI5_9
+    {.gpio_idx = 0, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 5
+    {.gpio_idx = 1, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 6
+    {.gpio_idx = 2, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 7
+    {.gpio_idx = 3, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 8
+    {.gpio_idx = 4, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 9
+    // EXTI10_15
+    {.gpio_idx = 0, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 10
+    {.gpio_idx = 1, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 11
+    {.gpio_idx = 2, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 12
+    {.gpio_idx = 3, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 13
+    {.gpio_idx = 4, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 14
+    {.gpio_idx = 5, .irq_index = 6, .irq_n = EXTI15_10_IRQn}// pin 15
+};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F3/gpio_irq_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,67 @@
+/* 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_GPIO_IRQ_DEVICE_H
+#define MBED_GPIO_IRQ_DEVICE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "stm32f3xx_ll_exti.h"
+
+// Number of EXTI irq vectors (EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5_9, EXTI10_15)
+#define CHANNEL_NUM (7)
+
+#define EXTI_IRQ0_NUM_LINES 1
+#define EXTI_IRQ1_NUM_LINES 1
+#define EXTI_IRQ2_NUM_LINES 1
+#define EXTI_IRQ3_NUM_LINES 1
+#define EXTI_IRQ4_NUM_LINES 1
+#define EXTI_IRQ5_NUM_LINES 5
+#define EXTI_IRQ6_NUM_LINES 6
+
+// Max pins for one line (max with EXTI10_15)
+#define MAX_PIN_LINE (EXTI_IRQ6_NUM_LINES)
+
+/*  Structure to describe how the HW EXTI lines are defined in this HW */
+typedef struct exti_lines {
+    uint32_t gpio_idx;   // an index entry for each EXIT line
+    uint32_t irq_index;  // the IRQ index
+    IRQn_Type  irq_n;    // the corresponding EXTI IRQn
+} exti_lines_t;
+
+// Used to return the index for channels array.
+extern const exti_lines_t pin_lines_desc[];
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F3/pin_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,70 @@
+/* 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.
+ *******************************************************************************
+ */
+#ifndef MBED_PIN_DEVICE_H
+#define MBED_PIN_DEVICE_H
+
+#include "cmsis.h"
+
+#include "stm32f3xx_ll_gpio.h"
+
+extern const uint32_t ll_pin_defines[16];
+
+/* Family specific implementations */
+static inline void stm_pin_DisconnectDebug(PinName pin)
+{
+    /* empty for now */
+}
+
+static inline void stm_pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t pull_config)
+{
+    switch (pull_config) {
+        case GPIO_PULLUP:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_UP);
+            break;
+        case GPIO_PULLDOWN:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_DOWN);
+            break;
+        default:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_NO);
+            break;
+    }
+}
+
+static inline void stm_pin_SetAFPin( GPIO_TypeDef *gpio, PinName pin, uint32_t afnum)
+{
+    uint32_t ll_pin  = ll_pin_defines[STM_PIN(pin)];
+
+    if (STM_PIN(pin) > 7)
+        LL_GPIO_SetAFPin_8_15(gpio, ll_pin, afnum);
+    else
+        LL_GPIO_SetAFPin_0_7(gpio, ll_pin, afnum);
+}
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F3/pinmap.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,195 +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 "mbed_assert.h"
-#include "pinmap.h"
-#include "PortNames.h"
-#include "mbed_error.h"
-
-// GPIO mode look-up table
-// Warning: the elements order must be the same as the one defined in PinNames.h
-static const uint32_t gpio_mode[13] = {
-    GPIO_MODE_INPUT,              //  0 = STM_MODE_INPUT
-    GPIO_MODE_OUTPUT_PP,          //  1 = STM_MODE_OUTPUT_PP
-    GPIO_MODE_OUTPUT_OD,          //  2 = STM_MODE_OUTPUT_OD
-    GPIO_MODE_AF_PP,              //  3 = STM_MODE_AF_PP
-    GPIO_MODE_AF_OD,              //  4 = STM_MODE_AF_OD
-    GPIO_MODE_ANALOG,             //  5 = STM_MODE_ANALOG
-    GPIO_MODE_IT_RISING,          //  6 = STM_MODE_IT_RISING
-    GPIO_MODE_IT_FALLING,         //  7 = STM_MODE_IT_FALLING
-    GPIO_MODE_IT_RISING_FALLING,  //  8 = STM_MODE_IT_RISING_FALLING
-    GPIO_MODE_EVT_RISING,         //  9 = STM_MODE_EVT_RISING
-    GPIO_MODE_EVT_FALLING,        // 10 = STM_MODE_EVT_FALLING
-    GPIO_MODE_EVT_RISING_FALLING, // 11 = STM_MODE_EVT_RISING_FALLING
-    0x10000000                    // 12 = STM_MODE_IT_EVT_RESET (not in STM32Cube HAL)
-};
-
-// Enable GPIO clock and return GPIO base address
-uint32_t Set_GPIO_Clock(uint32_t port_idx)
-{
-    uint32_t gpio_add = 0;
-    switch (port_idx) {
-        case PortA:
-            gpio_add = GPIOA_BASE;
-            __GPIOA_CLK_ENABLE();
-            break;
-        case PortB:
-            gpio_add = GPIOB_BASE;
-            __GPIOB_CLK_ENABLE();
-            break;
-        case PortC:
-            gpio_add = GPIOC_BASE;
-            __GPIOC_CLK_ENABLE();
-            break;
-        case PortD:
-            gpio_add = GPIOD_BASE;
-            __GPIOD_CLK_ENABLE();
-            break;
-#if defined GPIOE_BASE
-        case PortE:
-            gpio_add = GPIOE_BASE;
-            __GPIOE_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOF_BASE
-        case PortF:
-            gpio_add = GPIOF_BASE;
-            __GPIOF_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOG_BASE
-        case PortG:
-            gpio_add = GPIOG_BASE;
-            __GPIOG_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOH_BASE
-        case PortH:
-            gpio_add = GPIOH_BASE;
-            __GPIOH_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOI_BASE
-        case PortI:
-            gpio_add = GPIOI_BASE;
-            __GPIOI_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOJ_BASE
-        case PortJ:
-            gpio_add = GPIOJ_BASE;
-            __GPIOJ_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOK_BASE
-        case PortK:
-            gpio_add = GPIOK_BASE;
-            __GPIOK_CLK_ENABLE();
-            break;
-#endif
-
-
-        default:
-            error("Pinmap error: wrong port number.");
-            break;
-    }
-    return gpio_add;
-}
-
-/**
- * Configure pin (mode, speed, output type and pull-up/pull-down)
- */
-void pin_function(PinName pin, int data)
-{
-    MBED_ASSERT(pin != (PinName)NC);
-    // Get the pin informations
-    uint32_t mode  = STM_PIN_MODE(data);
-    uint32_t pupd  = STM_PIN_PUPD(data);
-    uint32_t afnum = STM_PIN_AFNUM(data);
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Configure GPIO
-    GPIO_InitTypeDef GPIO_InitStructure;
-    GPIO_InitStructure.Pin       = (uint32_t)(1 << pin_index);
-    GPIO_InitStructure.Mode      = gpio_mode[mode];
-    GPIO_InitStructure.Pull      = pupd;
-    GPIO_InitStructure.Speed     = GPIO_SPEED_HIGH;
-    GPIO_InitStructure.Alternate = afnum;
-    HAL_GPIO_Init(gpio, &GPIO_InitStructure);
-
-    // [TODO] Disconnect JTAG-DP + SW-DP signals.
-    // Warning: Need to reconnect under reset
-    //if ((pin == PA_13) || (pin == PA_14)) {
-    //
-    //}
-}
-
-/**
- * Configure pin pull-up/pull-down
- */
-void pin_mode(PinName pin, PinMode mode)
-{
-    MBED_ASSERT(pin != (PinName)NC);
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Configure pull-up/pull-down resistors
-    uint32_t pupd = (uint32_t)mode;
-    if (pupd > 2) {
-        pupd = 0; // Open-drain = No pull-up/No pull-down
-    }
-    gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2)));
-    gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
-}
-
-/*  Internal function for setting the gpiomode/function
- *  without changing Pull mode
- */
-void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
-
-    /* Read current pull state from HW to avoid over-write*/
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
-    uint32_t temp = gpio->PUPDR;
-    uint32_t pull = (temp  >> (pin_index * 2U)) & GPIO_PUPDR_PUPDR0;
-
-    /* Then re-use global function for updating the mode part*/
-    pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
-}
--- a/targets/TARGET_STM/TARGET_STM32F3/port_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,103 +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 "port_api.h"
-#include "pinmap.h"
-#include "gpio_api.h"
-#include "mbed_error.h"
-
-#if DEVICE_PORTIN || DEVICE_PORTOUT
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-
-// high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
-// low nibble  = pin number
-PinName port_pin(PortName port, int pin_n)
-{
-    return (PinName)(pin_n + (port << 4));
-}
-
-void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
-{
-    uint32_t port_index = (uint32_t)port;
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Fill PORT object structure for future use
-    obj->port      = port;
-    obj->mask      = mask;
-    obj->direction = dir;
-    obj->reg_in    = &gpio->IDR;
-    obj->reg_out   = &gpio->ODR;
-
-    port_dir(obj, dir);
-}
-
-void port_dir(port_t *obj, PinDirection dir)
-{
-    uint32_t i;
-    obj->direction = dir;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            if (dir == PIN_OUTPUT) {
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
-            } else { // PIN_INPUT
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
-            }
-        }
-    }
-}
-
-void port_mode(port_t *obj, PinMode mode)
-{
-    uint32_t i;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            pin_mode(port_pin(obj->port, i), mode);
-        }
-    }
-}
-
-void port_write(port_t *obj, int value)
-{
-    *obj->reg_out = (*obj->reg_out & ~obj->mask) | (value & obj->mask);
-}
-
-int port_read(port_t *obj)
-{
-    if (obj->direction == PIN_OUTPUT) {
-        return (*obj->reg_out & obj->mask);
-    } else { // PIN_INPUT
-        return (*obj->reg_in & obj->mask);
-    }
-}
-
-#endif
--- a/targets/TARGET_STM/TARGET_STM32F3/pwmout_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,57 @@
+/* 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;
+
+extern const pwm_apb_map_t pwm_apb_map_table[];
+
+#endif // DEVICE_PWMOUT
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_DISCO_F429ZI/system_stm32f4xx.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_DISCO_F429ZI/system_stm32f4xx.c	Tue Mar 14 16:40:56 2017 +0000
@@ -38,7 +38,7 @@
   ******************************************************************************
   * @attention
   *
-  * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
+  * <h2><center>&copy; COPYRIGHT 2017 STMicroelectronics</center></h2>
   *
   * 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 @@
   /* 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;
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_NUCLEO_F429ZI/system_stm32f4xx.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_NUCLEO_F429ZI/system_stm32f4xx.c	Tue Mar 14 16:40:56 2017 +0000
@@ -38,7 +38,7 @@
   ******************************************************************************
   * @attention
   *
-  * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
+  * <h2><center>&copy; COPYRIGHT 2017 STMicroelectronics</center></h2>
   *
   * Redistribution and use in source and binary forms, with or without modification,
   * are permitted provided that the following conditions are met:
@@ -80,6 +80,7 @@
 
 #include "stm32f4xx.h"
 #include "hal_tick.h"
+#include "nvic_addr.h"
 
 #if !defined  (HSE_VALUE) 
   #define HSE_VALUE    ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz */
@@ -187,10 +188,6 @@
   */
 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;
@@ -214,13 +211,6 @@
   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 = FLASH_BASE | VECT_TAB_OFFSET; /* 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();
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_MICRO/startup_stm32f429xx.s	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_MICRO/startup_stm32f429xx.s	Tue Mar 14 16:40:56 2017 +0000
@@ -197,9 +197,15 @@
 ; 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
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_MICRO/stm32f429xx.sct	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_MICRO/stm32f429xx.sct	Tue Mar 14 16:40:56 2017 +0000
@@ -1,3 +1,4 @@
+#! armcc -E
 ; Scatter-Loading Description File
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ; Copyright (c) 2015, STMicroelectronics
@@ -27,10 +28,18 @@
 ; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
+#if !defined(MBED_APP_START)
+  #define MBED_APP_START 0x08000000
+#endif
+
+#if !defined(MBED_APP_SIZE)
+  #define MBED_APP_SIZE 0x200000
+#endif
+
 ; 2 MB FLASH (0x200000) + 256 KB SRAM (0x40000)
-LR_IROM1 0x08000000 0x200000  {    ; load region size_region
+LR_IROM1 MBED_APP_START MBED_APP_SIZE  {    ; load region size_region
 
-  ER_IROM1 0x08000000 0x200000  {  ; load address = execution address
+  ER_IROM1 MBED_APP_START MBED_APP_SIZE  {  ; load address = execution address
    *.o (RESET, +First)
    *(InRoot$$Sections)
    .ANY (+RO)
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_STD/startup_stm32f429xx.s	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_STD/startup_stm32f429xx.s	Tue Mar 14 16:40:56 2017 +0000
@@ -170,9 +170,15 @@
 ; 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
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_STD/stm32f429xx.sct	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_ARM_STD/stm32f429xx.sct	Tue Mar 14 16:40:56 2017 +0000
@@ -1,3 +1,4 @@
+#! armcc -E
 ; Scatter-Loading Description File
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ; Copyright (c) 2015, STMicroelectronics
@@ -27,10 +28,18 @@
 ; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
+#if !defined(MBED_APP_START)
+  #define MBED_APP_START 0x08000000
+#endif
+
+#if !defined(MBED_APP_SIZE)
+  #define MBED_APP_SIZE 0x200000
+#endif
+
 ; 2 MB FLASH (0x200000) + 192 KB SRAM (0x30000)
-LR_IROM1 0x08000000 0x200000  {    ; load region size_region
+LR_IROM1 MBED_APP_START MBED_APP_SIZE  {    ; load region size_region
 
-  ER_IROM1 0x08000000 0x200000  {  ; load address = execution address
+  ER_IROM1 MBED_APP_START MBED_APP_SIZE  {  ; load address = execution address
    *.o (RESET, +First)
    *(InRoot$$Sections)
    .ANY (+RO)
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_GCC_ARM/STM32F429xI.ld	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_GCC_ARM/STM32F429xI.ld	Tue Mar 14 16:40:56 2017 +0000
@@ -10,11 +10,19 @@
  * heap and the page heap in uVisor applications. */
 HEAP_SIZE = 0x6000;
 
+#if !defined(MBED_APP_START)
+  #define MBED_APP_START 0x08000000
+#endif
+
+#if !defined(MBED_APP_SIZE)
+  #define MBED_APP_SIZE 2048k
+#endif
+
 /* Specify the memory areas */
 MEMORY
 {
-    VECTORS (rx) : ORIGIN = 0x08000000, LENGTH = 0x400
-    FLASH (rx)   : ORIGIN = 0x08000400, LENGTH = 2048k - 0x400
+    VECTORS (rx) : ORIGIN = MBED_APP_START, LENGTH = 0x400
+    FLASH (rx)   : ORIGIN = MBED_APP_START + 0x400, LENGTH = MBED_APP_SIZE - 0x400
     CCM (rwx)    : ORIGIN = 0x10000000, LENGTH = 64K
     RAM (rwx)    : ORIGIN = 0x20000000, LENGTH = 192k
 }
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_IAR/startup_stm32f429xx.S	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_IAR/startup_stm32f429xx.S	Tue Mar 14 16:40:56 2017 +0000
@@ -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 @@
         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
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_IAR/stm32f429xx_flash.icf	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/TOOLCHAIN_IAR/stm32f429xx_flash.icf	Tue Mar 14 16:40:56 2017 +0000
@@ -1,11 +1,13 @@
 /*###ICF### Section handled by ICF editor, don't touch! ****/
 /*-Editor annotation file-*/
 /* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */
+if (!isdefinedsymbol(MBED_APP_START)) { define symbol MBED_APP_START = 0x08000000; }
+if (!isdefinedsymbol(MBED_APP_SIZE)) { define symbol MBED_APP_SIZE = 0x200000; }
 /*-Specials-*/
-define symbol __ICFEDIT_intvec_start__ = 0x08000000;
+define symbol __ICFEDIT_intvec_start__ = MBED_APP_START;
 /*-Memory Regions-*/
-define symbol __ICFEDIT_region_ROM_start__    = 0x08000000;
-define symbol __ICFEDIT_region_ROM_end__      = 0x081FFFFF;
+define symbol __ICFEDIT_region_ROM_start__    = MBED_APP_START;
+define symbol __ICFEDIT_region_ROM_end__      = MBED_APP_START + MBED_APP_SIZE - 1;
 define symbol __ICFEDIT_region_NVIC_start__   = 0x20000000;
 define symbol __ICFEDIT_region_NVIC_end__     = 0x200001AF;
 define symbol __ICFEDIT_region_RAM_start__    = 0x200001B0;
@@ -13,9 +15,9 @@
 define symbol __ICFEDIT_region_CCMRAM_start__ = 0x10000000;
 define symbol __ICFEDIT_region_CCMRAM_end__   = 0x1000FFFF;
 /*-Sizes-*/
-/*Heap 1/4 of ram and stack 1/8*/
+/*Heap 1/2 of ram and stack 1/8*/
 define symbol __ICFEDIT_size_cstack__ = 0x6000;
-define symbol __ICFEDIT_size_heap__   = 0xC000;
+define symbol __ICFEDIT_size_heap__   = 0x18000;
 /**** End of ICF editor section. ###ICF###*/
 
 
@@ -34,4 +36,4 @@
 
 place in ROM_region   { readonly };
 place in RAM_region   { readwrite,
-                        block CSTACK, block HEAP };
\ No newline at end of file
+                        block CSTACK, block HEAP };
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/cmsis_nvic.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/cmsis_nvic.c	Tue Mar 14 16:40:56 2017 +0000
@@ -29,9 +29,9 @@
  *******************************************************************************
  */ 
 #include "cmsis_nvic.h"
+#include "nvic_addr.h"
 
 #define NVIC_RAM_VECTOR_ADDRESS   (0x20000000)  // Vectors positioned at start of RAM
-#define NVIC_FLASH_VECTOR_ADDRESS (0x08000000)  // Initial vector position in flash
 
 void __NVIC_SetVector(IRQn_Type IRQn, uint32_t vector) {
     uint32_t *vectors = (uint32_t *)SCB->VTOR;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/flash_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,74 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "flash_api.h"
+#include "flash_data.h"
+#include "mbed_critical.h"
+
+// This file is automagically generated
+
+// This is a flash algo binary blob. It is PIC (position independent code) that should be stored in RAM
+static uint32_t FLASH_ALGO[] = {
+    0xf3c04601, 0x28203007, 0x2204bf24, 0x1050eb02, 0x2810d205, 0x2203bf26, 0x1010eb02, 0xf4110880,
+    0xbf181f80, 0x0010f040, 0x48714770, 0x6001496f, 0x60014970, 0x68014870, 0x01f0f041, 0x486f6001,
+    0xf0106800, 0xd1080f20, 0xf245486d, 0x60015155, 0x60412106, 0x71fff640, 0x20006081, 0x49694770,
+    0xf4206808, 0x600a52f8, 0x48676008, 0xf0416801, 0x60014100, 0x47702000, 0xc18cf8df, 0x0000f8dc,
+    0x0004f040, 0x0000f8cc, 0x0000f8dc, 0x4000f440, 0x0000f8cc, 0x0000f8dc, 0x3080f440, 0x0000f8cc,
+    0x0004f1ac, 0xf4116801, 0xbf1c3f80, 0x21aaf64a, 0xd0044a53, 0x68036011, 0x3f80f413, 0xf8dcd1fa,
+    0xf0200000, 0xf8cc0004, 0xf8dc0000, 0xf4200000, 0xf8cc4000, 0x20000000, 0xf3c04770, 0x29203107,
+    0x2204bf24, 0x1151eb02, 0x2910d205, 0x2203bf26, 0x1111eb02, 0xf4100889, 0xbf181f80, 0x0110f041,
+    0x6802483d, 0x02f0f042, 0xf1006002, 0x22020c04, 0x2000f8cc, 0x2000f8dc, 0xea0323f8, 0x431101c1,
+    0x1000f8cc, 0x1000f8dc, 0x3180f441, 0x1000f8cc, 0xf4116801, 0xbf1c3f80, 0x21aaf64a, 0xd0044a30,
+    0x68036011, 0x3f80f413, 0xf8dcd1fa, 0xf0211000, 0xf8cc0102, 0x68011000, 0x0ff0f011, 0x2000bf04,
+    0x68014770, 0x01f0f041, 0x20016001, 0x4b224770, 0x1cc9b430, 0xc000f8d3, 0x0103f031, 0x0cf0f04c,
+    0xc000f8c3, 0x0404f103, 0x0c00f04f, 0xc000f8c4, 0xf240bf18, 0xd0252501, 0xc000f8d4, 0x0c05ea4c,
+    0xc000f8c4, 0xc000f8d2, 0xc000f8c0, 0xc000f8d3, 0x3f80f41c, 0xf8d4d1fa, 0xf02cc000, 0xf8c40c01,
+    0xf8d3c000, 0xf01cc000, 0xd0060ff0, 0xf0406818, 0x601800f0, 0x2001bc30, 0x1d004770, 0xf1021f09,
+    0xd1d90204, 0x2000bc30, 0x00004770, 0x45670123, 0x40023c04, 0xcdef89ab, 0x40023c0c, 0x40023c14,
+    0x40003000, 0x40023c00, 0x40023c10, 0x00000000
+};
+
+static const flash_algo_t flash_algo_config = {
+    .init = 0x2b,
+    .uninit = 0x5f,
+    .erase_sector = 0xdb,
+    .program_page = 0x16f,
+    .static_base = 0x20c,
+    .algo_blob = FLASH_ALGO
+};
+
+static const sector_info_t sectors_info[] = {
+    {0x8000000, 0x4000},
+    {0x8010000, 0x10000},
+    {0x8020000, 0x20000},
+    {0x8100000, 0x4000},
+    {0x8110000, 0x10000},
+    {0x8120000, 0x20000},
+};
+
+static const flash_target_config_t flash_target_config = {
+    .page_size  = 0x400,
+    .flash_start = 0x8000000,
+    .flash_size = 0x200000,
+    .sectors = sectors_info,
+    .sector_info_count = sizeof(sectors_info) / sizeof(sector_info_t)
+};
+
+void flash_set_target_config(flash_t *obj)
+{
+    obj->flash_algo = &flash_algo_config;
+    obj->target_config = &flash_target_config;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/nvic_addr.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,40 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2017-2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef NVIC_ADDR_H
+#define NVIC_ADDR_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if defined(__ICCARM__)
+    #pragma section=".intvec"
+    #define NVIC_FLASH_VECTOR_ADDRESS   ((uint32_t)__section_begin(".intvec"))
+#elif defined(__CC_ARM)
+    extern uint32_t Load$$LR$$LR_IROM1$$Base[];
+    #define NVIC_FLASH_VECTOR_ADDRESS   ((uint32_t)Load$$LR$$LR_IROM1$$Base)
+#elif defined(__GNUC__)
+    extern uint32_t g_pfnVectors[];
+    #define NVIC_FLASH_VECTOR_ADDRESS   ((uint32_t)g_pfnVectors)
+#else
+    #error "Flash vector address not set for this toolchain"
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/system_init_pre.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/system_init_pre.c	Tue Mar 14 16:40:56 2017 +0000
@@ -15,6 +15,7 @@
  * limitations under the License.
  */
 #include "stm32f4xx.h"
+#include "nvic_addr.h"
 
 /*!< Uncomment the following line if you need to relocate your vector Table in
      Internal SRAM. */
@@ -37,6 +38,6 @@
 #ifdef VECT_TAB_SRAM
     SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
 #else
-    SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
+  SCB->VTOR = NVIC_FLASH_VECTOR_ADDRESS; /* Vector Table Relocation in Internal FLASH */
 #endif
 }
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_NUCLEO_F439ZI/system_stm32f4xx.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_NUCLEO_F439ZI/system_stm32f4xx.c	Tue Mar 14 16:40:56 2017 +0000
@@ -80,6 +80,7 @@
 
 #include "stm32f4xx.h"
 #include "hal_tick.h"
+#include "nvic_addr.h"
 
 #if !defined  (HSE_VALUE) 
   #define HSE_VALUE    ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz */
@@ -218,7 +219,7 @@
 #ifdef VECT_TAB_SRAM
   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
 #else
-  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
+  SCB->VTOR = NVIC_FLASH_VECTOR_ADDRESS; /* Vector Table Relocation in Internal FLASH */
 #endif
 
   /* Configure the Cube driver */
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/device/cmsis_nvic.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/device/cmsis_nvic.c	Tue Mar 14 16:40:56 2017 +0000
@@ -29,9 +29,9 @@
  *******************************************************************************
  */ 
 #include "cmsis_nvic.h"
+#include "nvic_addr.h"
 
 #define NVIC_RAM_VECTOR_ADDRESS   (0x20000000)  // Vectors positioned at start of RAM
-#define NVIC_FLASH_VECTOR_ADDRESS (0x08000000)  // Initial vector position in flash
 
 void NVIC_SetVector(IRQn_Type IRQn, uint32_t vector) {
     uint32_t *vectors = (uint32_t *)SCB->VTOR;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/device/flash_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,74 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "flash_api.h"
+#include "flash_data.h"
+#include "mbed_critical.h"
+
+// This file is automagically generated
+
+// This is a flash algo binary blob. It is PIC (position independent code) that should be stored in RAM
+static uint32_t FLASH_ALGO[] = {
+    0xf3c04601, 0x28203007, 0x2204bf24, 0x1050eb02, 0x2810d205, 0x2203bf26, 0x1010eb02, 0xf4110880,
+    0xbf181f80, 0x0010f040, 0x48714770, 0x6001496f, 0x60014970, 0x68014870, 0x01f0f041, 0x486f6001,
+    0xf0106800, 0xd1080f20, 0xf245486d, 0x60015155, 0x60412106, 0x71fff640, 0x20006081, 0x49694770,
+    0xf4206808, 0x600a52f8, 0x48676008, 0xf0416801, 0x60014100, 0x47702000, 0xc18cf8df, 0x0000f8dc,
+    0x0004f040, 0x0000f8cc, 0x0000f8dc, 0x4000f440, 0x0000f8cc, 0x0000f8dc, 0x3080f440, 0x0000f8cc,
+    0x0004f1ac, 0xf4116801, 0xbf1c3f80, 0x21aaf64a, 0xd0044a53, 0x68036011, 0x3f80f413, 0xf8dcd1fa,
+    0xf0200000, 0xf8cc0004, 0xf8dc0000, 0xf4200000, 0xf8cc4000, 0x20000000, 0xf3c04770, 0x29203107,
+    0x2204bf24, 0x1151eb02, 0x2910d205, 0x2203bf26, 0x1111eb02, 0xf4100889, 0xbf181f80, 0x0110f041,
+    0x6802483d, 0x02f0f042, 0xf1006002, 0x22020c04, 0x2000f8cc, 0x2000f8dc, 0xea0323f8, 0x431101c1,
+    0x1000f8cc, 0x1000f8dc, 0x3180f441, 0x1000f8cc, 0xf4116801, 0xbf1c3f80, 0x21aaf64a, 0xd0044a30,
+    0x68036011, 0x3f80f413, 0xf8dcd1fa, 0xf0211000, 0xf8cc0102, 0x68011000, 0x0ff0f011, 0x2000bf04,
+    0x68014770, 0x01f0f041, 0x20016001, 0x4b224770, 0x1cc9b430, 0xc000f8d3, 0x0103f031, 0x0cf0f04c,
+    0xc000f8c3, 0x0404f103, 0x0c00f04f, 0xc000f8c4, 0xf240bf18, 0xd0252501, 0xc000f8d4, 0x0c05ea4c,
+    0xc000f8c4, 0xc000f8d2, 0xc000f8c0, 0xc000f8d3, 0x3f80f41c, 0xf8d4d1fa, 0xf02cc000, 0xf8c40c01,
+    0xf8d3c000, 0xf01cc000, 0xd0060ff0, 0xf0406818, 0x601800f0, 0x2001bc30, 0x1d004770, 0xf1021f09,
+    0xd1d90204, 0x2000bc30, 0x00004770, 0x45670123, 0x40023c04, 0xcdef89ab, 0x40023c0c, 0x40023c14,
+    0x40003000, 0x40023c00, 0x40023c10, 0x00000000
+};
+
+static const flash_algo_t flash_algo_config = {
+    .init = 0x2b,
+    .uninit = 0x5f,
+    .erase_sector = 0xdb,
+    .program_page = 0x16f,
+    .static_base = 0x20c,
+    .algo_blob = FLASH_ALGO
+};
+
+static const sector_info_t sectors_info[] = {
+    {0x8000000, 0x4000},
+    {0x8010000, 0x10000},
+    {0x8020000, 0x20000},
+    {0x8100000, 0x4000},
+    {0x8110000, 0x10000},
+    {0x8120000, 0x20000},
+};
+
+static const flash_target_config_t flash_target_config = {
+    .page_size  = 0x400,
+    .flash_start = 0x8000000,
+    .flash_size = 0x200000,
+    .sectors = sectors_info,
+    .sector_info_count = sizeof(sectors_info) / sizeof(sector_info_t)
+};
+
+void flash_set_target_config(flash_t *obj)
+{
+    obj->flash_algo = &flash_algo_config;
+    obj->target_config = &flash_target_config;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/device/nvic_addr.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,40 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2017-2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef NVIC_ADDR_H
+#define NVIC_ADDR_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if defined(__ICCARM__)
+    #pragma section=".intvec"
+    #define NVIC_FLASH_VECTOR_ADDRESS   ((uint32_t)__section_begin(".intvec"))
+#elif defined(__CC_ARM)
+    extern uint32_t Load$$LR$$LR_IROM1$$Base[];
+    #define NVIC_FLASH_VECTOR_ADDRESS   ((uint32_t)Load$$LR$$LR_IROM1$$Base)
+#elif defined(__GNUC__)
+    extern uint32_t g_pfnVectors[];
+    #define NVIC_FLASH_VECTOR_ADDRESS   ((uint32_t)g_pfnVectors)
+#else
+    #error "Flash vector address not set for this toolchain"
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/TARGET_NUCLEO_F446ZE/PeripheralPins.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/TARGET_NUCLEO_F446ZE/PeripheralPins.c	Tue Mar 14 16:40:56 2017 +0000
@@ -81,14 +81,14 @@
     //{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
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/TOOLCHAIN_ARM_MICRO/stm32f439xx.sct	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/TOOLCHAIN_ARM_MICRO/stm32f439xx.sct	Tue Mar 14 16:40:56 2017 +0000
@@ -1,3 +1,4 @@
+#! armcc -E
 ; Scatter-Loading Description File
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ; Copyright (c) 2014, STMicroelectronics
@@ -27,10 +28,18 @@
 ; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
+#if !defined(MBED_APP_START)
+  #define MBED_APP_START 0x08000000
+#endif
+
+#if !defined(MBED_APP_SIZE)
+  #define MBED_APP_SIZE 0x200000
+#endif
+
 ; STM32F439ZI: 2048 KB FLASH (0x200000) + 256 KB SRAM (0x30000 + 0x10000)
-LR_IROM1 0x08000000 0x200000  {    ; load region size_region
+LR_IROM1 MBED_APP_START MBED_APP_SIZE  {    ; load region size_region
 
-  ER_IROM1 0x08000000 0x200000  {  ; load address = execution address
+  ER_IROM1 MBED_APP_START MBED_APP_SIZE  {  ; load address = execution address
    *.o (RESET, +First)
    *(InRoot$$Sections)
    .ANY (+RO)
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/TOOLCHAIN_ARM_STD/stm32f439xx.sct	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/TOOLCHAIN_ARM_STD/stm32f439xx.sct	Tue Mar 14 16:40:56 2017 +0000
@@ -1,3 +1,4 @@
+#! armcc -E
 ; Scatter-Loading Description File
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ; Copyright (c) 2015, STMicroelectronics
@@ -27,10 +28,18 @@
 ; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
+#if !defined(MBED_APP_START)
+  #define MBED_APP_START 0x08000000
+#endif
+
+#if !defined(MBED_APP_SIZE)
+  #define MBED_APP_SIZE 0x200000
+#endif
+
 ; 2 MB FLASH (0x200000) + 256 KB SRAM (0x30000 + 0x10000)
-LR_IROM1 0x08000000 0x200000  {    ; load region size_region
+LR_IROM1 MBED_APP_START MBED_APP_SIZE  {    ; load region size_region
 
-  ER_IROM1 0x08000000 0x200000  {  ; load address = execution address
+  ER_IROM1 MBED_APP_START MBED_APP_SIZE  {  ; load address = execution address
    *.o (RESET, +First)
    *(InRoot$$Sections)
    .ANY (+RO)
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/TOOLCHAIN_GCC_ARM/STM32F439ZI.ld	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/TOOLCHAIN_GCC_ARM/STM32F439ZI.ld	Tue Mar 14 16:40:56 2017 +0000
@@ -1,7 +1,15 @@
+#if !defined(MBED_APP_START)
+  #define MBED_APP_START 0x08000000
+#endif
+
+#if !defined(MBED_APP_SIZE)
+  #define MBED_APP_SIZE 2048k
+#endif
+
 /* Linker script to configure memory regions. */
 MEMORY
 { 
-  FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 2048k
+  FLASH (rx) : ORIGIN = MBED_APP_START, LENGTH = MBED_APP_SIZE
   CCM (rwx) : ORIGIN = 0x10000000, LENGTH = 64K
   RAM (rwx) : ORIGIN = 0x200001AC, LENGTH = 192k - 0x1AC 
 }
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/TOOLCHAIN_IAR/stm32f439xx_flash.icf	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/TOOLCHAIN_IAR/stm32f439xx_flash.icf	Tue Mar 14 16:40:56 2017 +0000
@@ -1,11 +1,13 @@
 /*###ICF### Section handled by ICF editor, don't touch! ****/
 /*-Editor annotation file-*/
 /* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */
+if (!isdefinedsymbol(MBED_APP_START)) { define symbol MBED_APP_START = 0x08000000; }
+if (!isdefinedsymbol(MBED_APP_SIZE)) { define symbol MBED_APP_SIZE = 0x200000; }
 /*-Specials-*/
-define symbol __ICFEDIT_intvec_start__ = 0x08000000;
+define symbol __ICFEDIT_intvec_start__ = MBED_APP_START;
 /*-Memory Regions-*/
-define symbol __ICFEDIT_region_ROM_start__    = 0x08000000;
-define symbol __ICFEDIT_region_ROM_end__      = 0x081FFFFF;
+define symbol __ICFEDIT_region_ROM_start__    = MBED_APP_START;
+define symbol __ICFEDIT_region_ROM_end__      = MBED_APP_START + MBED_APP_SIZE - 1;
 define symbol __ICFEDIT_region_NVIC_start__   = 0x20000000;
 define symbol __ICFEDIT_region_NVIC_end__     = 0x200001AF;
 define symbol __ICFEDIT_region_RAM_start__    = 0x200001B0;
@@ -13,9 +15,9 @@
 define symbol __ICFEDIT_region_CCMRAM_start__ = 0x10000000;
 define symbol __ICFEDIT_region_CCMRAM_end__   = 0x1000FFFF;
 /*-Sizes-*/
-/*Heap 1/4 of ram and stack 1/8*/
+/*Heap 1/2 of ram and stack 1/8*/
 define symbol __ICFEDIT_size_cstack__ = 0x6000;
-define symbol __ICFEDIT_size_heap__   = 0xC000;
+define symbol __ICFEDIT_size_heap__   = 0x18000;
 /**** End of ICF editor section. ###ICF###*/
 
 
@@ -34,4 +36,4 @@
 
 place in ROM_region   { readonly };
 place in RAM_region   { readwrite,
-                        block CSTACK, block HEAP };
\ No newline at end of file
+                        block CSTACK, block HEAP };
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/cmsis_nvic.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/cmsis_nvic.c	Tue Mar 14 16:40:56 2017 +0000
@@ -29,9 +29,9 @@
  *******************************************************************************
  */ 
 #include "cmsis_nvic.h"
+#include "nvic_addr.h"
 
 #define NVIC_RAM_VECTOR_ADDRESS   (0x20000000)  // Vectors positioned at start of RAM
-#define NVIC_FLASH_VECTOR_ADDRESS (0x08000000)  // Initial vector position in flash
 
 void NVIC_SetVector(IRQn_Type IRQn, uint32_t vector) {
     uint32_t *vectors = (uint32_t *)SCB->VTOR;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/nvic_addr.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,40 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2017-2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef NVIC_ADDR_H
+#define NVIC_ADDR_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if defined(__ICCARM__)
+    #pragma section=".intvec"
+    #define NVIC_FLASH_VECTOR_ADDRESS   ((uint32_t)__section_begin(".intvec"))
+#elif defined(__CC_ARM)
+    extern uint32_t Load$$LR$$LR_IROM1$$Base[];
+    #define NVIC_FLASH_VECTOR_ADDRESS   ((uint32_t)Load$$LR$$LR_IROM1$$Base)
+#elif defined(__GNUC__)
+    extern uint32_t g_pfnVectors[];
+    #define NVIC_FLASH_VECTOR_ADDRESS   ((uint32_t)g_pfnVectors)
+#else
+    #error "Flash vector address not set for this toolchain"
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/system_stm32f4xx.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/system_stm32f4xx.c	Tue Mar 14 16:40:56 2017 +0000
@@ -80,6 +80,7 @@
 
 #include "stm32f4xx.h"
 #include "hal_tick.h"
+#include "nvic_addr.h"
 
 #if !defined  (HSE_VALUE) 
   #define HSE_VALUE    ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz */
@@ -218,7 +219,7 @@
 #ifdef VECT_TAB_SRAM
   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
 #else
-  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
+  SCB->VTOR = NVIC_FLASH_VECTOR_ADDRESS; /* Vector Table Relocation in Internal FLASH */
 #endif
 
   /* Configure the Cube driver */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/flash_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,74 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "flash_api.h"
+#include "flash_data.h"
+#include "mbed_critical.h"
+
+// This file is automagically generated
+
+// This is a flash algo binary blob. It is PIC (position independent code) that should be stored in RAM
+static uint32_t FLASH_ALGO[] = {
+    0xf3c04601, 0x28203007, 0x2204bf24, 0x1050eb02, 0x2810d205, 0x2203bf26, 0x1010eb02, 0xf4110880,
+    0xbf181f80, 0x0010f040, 0x48714770, 0x6001496f, 0x60014970, 0x68014870, 0x01f0f041, 0x486f6001,
+    0xf0106800, 0xd1080f20, 0xf245486d, 0x60015155, 0x60412106, 0x71fff640, 0x20006081, 0x49694770,
+    0xf4206808, 0x600a52f8, 0x48676008, 0xf0416801, 0x60014100, 0x47702000, 0xc18cf8df, 0x0000f8dc,
+    0x0004f040, 0x0000f8cc, 0x0000f8dc, 0x4000f440, 0x0000f8cc, 0x0000f8dc, 0x3080f440, 0x0000f8cc,
+    0x0004f1ac, 0xf4116801, 0xbf1c3f80, 0x21aaf64a, 0xd0044a53, 0x68036011, 0x3f80f413, 0xf8dcd1fa,
+    0xf0200000, 0xf8cc0004, 0xf8dc0000, 0xf4200000, 0xf8cc4000, 0x20000000, 0xf3c04770, 0x29203107,
+    0x2204bf24, 0x1151eb02, 0x2910d205, 0x2203bf26, 0x1111eb02, 0xf4100889, 0xbf181f80, 0x0110f041,
+    0x6802483d, 0x02f0f042, 0xf1006002, 0x22020c04, 0x2000f8cc, 0x2000f8dc, 0xea0323f8, 0x431101c1,
+    0x1000f8cc, 0x1000f8dc, 0x3180f441, 0x1000f8cc, 0xf4116801, 0xbf1c3f80, 0x21aaf64a, 0xd0044a30,
+    0x68036011, 0x3f80f413, 0xf8dcd1fa, 0xf0211000, 0xf8cc0102, 0x68011000, 0x0ff0f011, 0x2000bf04,
+    0x68014770, 0x01f0f041, 0x20016001, 0x4b224770, 0x1cc9b430, 0xc000f8d3, 0x0103f031, 0x0cf0f04c,
+    0xc000f8c3, 0x0404f103, 0x0c00f04f, 0xc000f8c4, 0xf240bf18, 0xd0252501, 0xc000f8d4, 0x0c05ea4c,
+    0xc000f8c4, 0xc000f8d2, 0xc000f8c0, 0xc000f8d3, 0x3f80f41c, 0xf8d4d1fa, 0xf02cc000, 0xf8c40c01,
+    0xf8d3c000, 0xf01cc000, 0xd0060ff0, 0xf0406818, 0x601800f0, 0x2001bc30, 0x1d004770, 0xf1021f09,
+    0xd1d90204, 0x2000bc30, 0x00004770, 0x45670123, 0x40023c04, 0xcdef89ab, 0x40023c0c, 0x40023c14,
+    0x40003000, 0x40023c00, 0x40023c10, 0x00000000
+};
+
+static const flash_algo_t flash_algo_config = {
+    .init = 0x2b,
+    .uninit = 0x5f,
+    .erase_sector = 0xdb,
+    .program_page = 0x16f,
+    .static_base = 0x20c,
+    .algo_blob = FLASH_ALGO
+};
+
+static const sector_info_t sectors_info[] = {
+    {0x8000000, 0x4000},
+    {0x8010000, 0x10000},
+    {0x8020000, 0x20000},
+    {0x8100000, 0x4000},
+    {0x8110000, 0x10000},
+    {0x8120000, 0x20000},
+};
+
+static const flash_target_config_t flash_target_config = {
+    .page_size  = 0x400,
+    .flash_start = 0x8000000,
+    .flash_size = 0x200000,
+    .sectors = sectors_info,
+    .sector_info_count = sizeof(sectors_info) / sizeof(sector_info_t)
+};
+
+void flash_set_target_config(flash_t *obj)
+{
+    obj->flash_algo = &flash_algo_config;
+    obj->target_config = &flash_target_config;
+}
--- a/targets/TARGET_STM/TARGET_STM32F4/analogin_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/analogin_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -30,7 +30,7 @@
 
 #if DEVICE_ANALOGIN
 
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "cmsis.h"
 #include "pinmap.h"
 #include "mbed_error.h"
--- a/targets/TARGET_STM/TARGET_STM32F4/can_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,542 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2016 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "can_api.h"
-
-#if DEVICE_CAN
-
-#include "cmsis.h"
-#include "pinmap.h"
-#include "PeripheralPins.h"
-#include "mbed_error.h"
-#include <math.h>
-#include <string.h>
-
-#define CAN_NUM    2
-static CAN_HandleTypeDef CanHandle;
-static uint32_t can_irq_ids[CAN_NUM] = {0};
-static can_irq_handler irq_handler;
-
-void can_init(can_t *obj, PinName rd, PinName td) 
-{
-    uint32_t filter_number;
-    CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
-    CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
-    obj->can = (CANName)pinmap_merge(can_rd, can_td);
-    MBED_ASSERT((int)obj->can != NC);    
-
-    if(obj->can == CAN_1) {
-        __HAL_RCC_CAN1_CLK_ENABLE();
-        obj->index = 0;
-    } else {
-        __HAL_RCC_CAN2_CLK_ENABLE();
-        obj->index = 1;
-    }
-
-    // Configure the CAN pins
-    pinmap_pinout(rd, PinMap_CAN_RD);
-    pinmap_pinout(td, PinMap_CAN_TD);
-    if (rd != NC) {
-        pin_mode(rd, PullUp);
-    }
-    if (td != NC) {
-        pin_mode(td, PullUp);
-    }
-
-    CanHandle.Instance = (CAN_TypeDef *)(obj->can);
-
-    CanHandle.Init.TTCM = DISABLE;
-    CanHandle.Init.ABOM = DISABLE;
-    CanHandle.Init.AWUM = DISABLE;
-    CanHandle.Init.NART = DISABLE;
-    CanHandle.Init.RFLM = DISABLE;
-    CanHandle.Init.TXFP = DISABLE;
-    CanHandle.Init.Mode = CAN_MODE_NORMAL;
-    CanHandle.Init.SJW = CAN_SJW_1TQ;
-    CanHandle.Init.BS1 = CAN_BS1_6TQ;
-    CanHandle.Init.BS2 = CAN_BS2_8TQ;
-    CanHandle.Init.Prescaler = 2;
-
-    if (HAL_CAN_Init(&CanHandle) != HAL_OK) {
-       error("Cannot initialize CAN");
-    }
-
-    filter_number = (obj->can == CAN_1) ? 0 : 14;
-
-    // Set initial CAN frequency to 100kb/s
-    can_frequency(obj, 100000);
-
-    can_filter(obj, 0, 0, CANStandard, filter_number);
-}
-
-void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id) 
-{
-    irq_handler = handler;
-    can_irq_ids[obj->index] = id;    
-}
-
-void can_irq_free(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-  
-    can->IER &= ~(CAN_IT_FMP0 | CAN_IT_FMP1 | CAN_IT_TME | \
-                  CAN_IT_ERR | CAN_IT_EPV | CAN_IT_BOF);
-    can_irq_ids[obj->can] = 0;
-}
-
-void can_free(can_t *obj) 
-{
-    // Reset CAN and disable clock
-    if (obj->can == CAN_1) {
-        __HAL_RCC_CAN1_FORCE_RESET();
-        __HAL_RCC_CAN1_RELEASE_RESET();
-        __HAL_RCC_CAN1_CLK_DISABLE();
-    }
-
-    if (obj->can == CAN_2) {
-        __HAL_RCC_CAN2_FORCE_RESET();
-        __HAL_RCC_CAN2_RELEASE_RESET();
-        __HAL_RCC_CAN2_CLK_DISABLE();
-    }
-}
-
-// The following table is used to program bit_timing. It is an adjustment of the sample
-// point by synchronizing on the start-bit edge and resynchronizing on the following edges.
-// This table has the sampling points as close to 75% as possible (most commonly used).
-// The first value is TSEG1, the second TSEG2.
-static const int timing_pts[23][2] = {
-    {0x0, 0x0},      // 2,  50%
-    {0x1, 0x0},      // 3,  67%
-    {0x2, 0x0},      // 4,  75%
-    {0x3, 0x0},      // 5,  80%
-    {0x3, 0x1},      // 6,  67%
-    {0x4, 0x1},      // 7,  71%
-    {0x5, 0x1},      // 8,  75%
-    {0x6, 0x1},      // 9,  78%
-    {0x6, 0x2},      // 10, 70%
-    {0x7, 0x2},      // 11, 73%
-    {0x8, 0x2},      // 12, 75%
-    {0x9, 0x2},      // 13, 77%
-    {0x9, 0x3},      // 14, 71%
-    {0xA, 0x3},      // 15, 73%
-    {0xB, 0x3},      // 16, 75%
-    {0xC, 0x3},      // 17, 76%
-    {0xD, 0x3},      // 18, 78%
-    {0xD, 0x4},      // 19, 74%
-    {0xE, 0x4},      // 20, 75%
-    {0xF, 0x4},      // 21, 76%
-    {0xF, 0x5},      // 22, 73%
-    {0xF, 0x6},      // 23, 70%
-    {0xF, 0x7},      // 24, 67%
-};
-
-static unsigned int can_speed(unsigned int pclk, unsigned int cclk, unsigned char psjw) 
-{
-    uint32_t    btr;
-    uint16_t    brp = 0;
-    uint32_t    calcbit;
-    uint32_t    bitwidth;
-    int         hit = 0;
-    int         bits;
-    
-    bitwidth = (pclk / cclk);
-    
-    brp = bitwidth / 0x18;
-    while ((!hit) && (brp < bitwidth / 4)) {
-        brp++;
-        for (bits = 22; bits > 0; bits--) {
-            calcbit = (bits + 3) * (brp + 1);
-            if (calcbit == bitwidth) {
-                hit = 1;
-                break;
-            }
-        }
-    }
-    
-    if (hit) {
-        btr = ((timing_pts[bits][1] << 20) & 0x00700000)
-            | ((timing_pts[bits][0] << 16) & 0x000F0000)
-            | ((psjw                << 24) & 0x0000C000)
-            | ((brp                 <<  0) & 0x000003FF);
-    } else {
-        btr = 0xFFFFFFFF;
-    }
-    
-    return btr;
-
-}
-
-int can_frequency(can_t *obj, int f) 
-{
-    int pclk = HAL_RCC_GetPCLK1Freq();
-    int btr = can_speed(pclk, (unsigned int)f, 1);
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    if (btr > 0) {
-        can->MCR |= CAN_MCR_INRQ ;
-        while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-        }
-        can->BTR = btr;
-        can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-        while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-        }
-        return 1;
-    } else {
-        return 0;
-    }
-}
-
-int can_write(can_t *obj, CAN_Message msg, int cc) 
-{
-    uint32_t  transmitmailbox = 5;
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    /* Select one empty transmit mailbox */
-    if ((can->TSR&CAN_TSR_TME0) == CAN_TSR_TME0) {
-        transmitmailbox = 0;
-    } else if ((can->TSR&CAN_TSR_TME1) == CAN_TSR_TME1) {
-        transmitmailbox = 1;
-    } else if ((can->TSR&CAN_TSR_TME2) == CAN_TSR_TME2) {
-        transmitmailbox = 2;
-    } else {
-        transmitmailbox = CAN_TXSTATUS_NOMAILBOX;
-    }
-
-    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) |
-                                             ((uint32_t)msg.data[4]));
-    /* Request transmission */
-    can->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ;
-  }
-
-  return 1;
-}
-
-int can_read(can_t *obj, CAN_Message *msg, int handle) 
-{
-    //handle is the FIFO number
-
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    
-    // check FPM0 which holds the pending message count in FIFO 0
-    // if no message is pending, return 0
-    if ((can->RF0R & CAN_RF0R_FMP0) == 0) {
-        return 0;
-    }
-
-    /* Get the Id */
-    msg->format = (CANFormat)((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR);
-    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);
-    /* Get the DLC */
-    msg->len = (uint8_t)0x0F & can->sFIFOMailBox[handle].RDTR;
-//  /* Get the FMI */
-//  msg->FMI = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDTR >> 8);
-    /* Get the data field */
-    msg->data[0] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDLR;
-    msg->data[1] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 8);
-    msg->data[2] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 16);
-    msg->data[3] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 24);
-    msg->data[4] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDHR;
-    msg->data[5] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 8);
-    msg->data[6] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 16);
-    msg->data[7] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 24);
-    
-    /* Release the FIFO */
-    if(handle == CAN_FIFO0) {
-        /* Release FIFO0 */
-        can->RF0R |= CAN_RF0R_RFOM0;
-    } else { /* FIFONumber == CAN_FIFO1 */
-      /* Release FIFO1 */
-      can->RF1R |= CAN_RF1R_RFOM1;
-    }
-
-    return 1;
-}
-
-void can_reset(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    can->MCR |= CAN_MCR_RESET;  
-    can->ESR = 0x0;
-}
-
-unsigned char can_rderror(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    return (can->ESR >> 24) & 0xFF;
-}
-
-unsigned char can_tderror(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    return (can->ESR >> 16) & 0xFF;
-}
-
-void can_monitor(can_t *obj, int silent) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);        
-    
-    can->MCR |= CAN_MCR_INRQ ;
-    while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-    }
-    if (silent) {
-        can->BTR |= ((uint32_t)1 << 31);
-    } else {
-        can->BTR &= ~((uint32_t)1 << 31);
-    }
-    can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-    while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-    }
-}
-
-int can_mode(can_t *obj, CanMode mode) 
-{
-    int success = 0;
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-    can->MCR |= CAN_MCR_INRQ ;
-    while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-    }
-    switch (mode) {
-        case MODE_NORMAL:
-            can->BTR &= ~(CAN_BTR_SILM | CAN_BTR_LBKM);
-            success = 1;
-            break;
-        case MODE_SILENT:
-            can->BTR |= CAN_BTR_SILM;
-            can->BTR &= ~CAN_BTR_LBKM;
-            success = 1;
-            break;
-        case MODE_TEST_GLOBAL:
-        case MODE_TEST_LOCAL:
-            can->BTR |= CAN_BTR_LBKM;
-            can->BTR &= ~CAN_BTR_SILM;
-            success = 1;
-            break;
-        case MODE_TEST_SILENT:
-            can->BTR |= (CAN_BTR_SILM | CAN_BTR_LBKM);
-            success = 1;
-            break;
-        default:
-            success = 0;
-            break;
-    }
-    can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-    while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-    }
-    return success;
-}
-
-int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) 
-{
-    CanHandle.Instance = (CAN_TypeDef *)(obj->can);
-    CAN_FilterConfTypeDef  sFilterConfig;
-    
-    sFilterConfig.FilterNumber = handle;
-    sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
-    sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
-    sFilterConfig.FilterIdHigh = (uint8_t) (id >> 8);
-    sFilterConfig.FilterIdLow = (uint8_t) id;
-    sFilterConfig.FilterMaskIdHigh = (uint8_t) (mask >> 8);
-    sFilterConfig.FilterMaskIdLow = (uint8_t) mask;
-    sFilterConfig.FilterFIFOAssignment = 0;
-    sFilterConfig.FilterActivation = ENABLE;
-    sFilterConfig.BankNumber = 14 + handle;
-  
-    HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig);
-      
-    return 0;
-}
-
-static void can_irq(CANName name, int id) 
-{
-    uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0;    
-    CanHandle.Instance = (CAN_TypeDef *)name;
-    
-    if(__HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_TME)) {
-        tmp1 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_0);
-        tmp2 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_1);
-        tmp3 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_2);
-        if(tmp1 || tmp2 || tmp3)  
-        {
-            irq_handler(can_irq_ids[id], IRQ_TX);
-        }
-    }
-  
-    tmp1 = __HAL_CAN_MSG_PENDING(&CanHandle, CAN_FIFO0);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_FMP0);
-  
-    if((tmp1 != 0) && tmp2) {
-         irq_handler(can_irq_ids[id], IRQ_RX);
-    }
-  
-    tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_EPV);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_EPV);
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR); 
-  
-    if(tmp1 && tmp2 && tmp3) {
-         irq_handler(can_irq_ids[id], IRQ_PASSIVE);
-    }
-  
-    tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_BOF);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_BOF);
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);  
-    if(tmp1 && tmp2 && tmp3) {
-        irq_handler(can_irq_ids[id], IRQ_BUS);
-    }
-  
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);  
-    if(tmp1 && tmp2 && tmp3) {
-        irq_handler(can_irq_ids[id], IRQ_ERROR);
-    }  
-}
-
-void CAN1_RX0_IRQHandler(void) 
-{
-    can_irq(CAN_1, 0);
-}
-
-void CAN1_TX_IRQHandler(void) 
-{
-    can_irq(CAN_1, 0);
-}
-
-void CAN1_SCE_IRQHandler(void) 
-{
-    can_irq(CAN_1, 0);
-}
-
-void CAN2_RX0_IRQHandler(void) 
-{
-    can_irq(CAN_2, 1);
-}
-
-void CAN2_TX_IRQHandler(void)
-{
-    can_irq(CAN_2, 1);
-}
-
-void CAN2_SCE_IRQHandler(void)
-{
-    can_irq(CAN_2, 1);
-}
-
-void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
-{
-
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-    IRQn_Type irq_n = (IRQn_Type)0;
-    uint32_t vector = 0;    
-    uint32_t ier;
-
-    if(obj->can == CAN_1) {
-        switch (type) {
-            case IRQ_RX:
-                ier = CAN_IT_FMP0;
-                irq_n = CAN1_RX0_IRQn;
-                vector = (uint32_t)&CAN1_RX0_IRQHandler;
-                break;
-            case IRQ_TX:      
-                ier = CAN_IT_TME;
-                irq_n = CAN1_TX_IRQn;
-                vector = (uint32_t)&CAN1_TX_IRQHandler;
-                break;
-            case IRQ_ERROR:   
-                ier = CAN_IT_ERR;
-                irq_n = CAN1_SCE_IRQn;
-                vector = (uint32_t)&CAN1_SCE_IRQHandler;
-                break;
-            case IRQ_PASSIVE:
-                ier = CAN_IT_EPV;
-                irq_n = CAN1_SCE_IRQn;
-                vector = (uint32_t)&CAN1_SCE_IRQHandler;
-                break;
-            case IRQ_BUS:
-                ier = CAN_IT_BOF;
-                irq_n = CAN1_SCE_IRQn;
-                vector = (uint32_t)&CAN1_SCE_IRQHandler;
-                break;
-            default: return;
-        }
-    } else {
-        switch (type) {
-              case IRQ_RX:
-                  ier = CAN_IT_FMP0;
-                  irq_n = CAN2_RX0_IRQn;
-                  vector = (uint32_t)&CAN2_RX0_IRQHandler;
-                  break;
-              case IRQ_TX:      
-                  ier = CAN_IT_TME;
-                  irq_n = CAN2_TX_IRQn;
-                  vector = (uint32_t)&CAN2_TX_IRQHandler;
-                  break;
-              case IRQ_ERROR:   
-                  ier = CAN_IT_ERR;
-                  irq_n = CAN2_SCE_IRQn;
-                  vector = (uint32_t)&CAN2_SCE_IRQHandler;
-                  break;
-              case IRQ_PASSIVE:
-                  ier = CAN_IT_EPV;
-                  irq_n = CAN2_SCE_IRQn;
-                  vector = (uint32_t)&CAN2_SCE_IRQHandler;
-                  break;
-              case IRQ_BUS:
-                  ier = CAN_IT_BOF;
-                  irq_n = CAN2_SCE_IRQn;
-                  vector = (uint32_t)&CAN2_SCE_IRQHandler;
-                  break;
-              default: return;
-        }
-    }
-
-    if(enable) {
-        can->IER |= ier;
-    } else {
-        can->IER &= ~ier;
-    }
-
-    NVIC_SetVector(irq_n, vector);
-    NVIC_EnableIRQ(irq_n);
-}
-
-#endif // DEVICE_CAN
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/can_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,53 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef MBED_CAN_DEVICE_H
+#define MBED_CAN_DEVICE_H
+
+#include "cmsis.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef DEVICE_CAN
+
+#define CAN_NUM 2 // Number of CAN peripherals present in the STM32 serie (1 or 2)
+
+#define CAN1_IRQ_RX_IRQN        CAN1_RX0_IRQn
+#define CAN1_IRQ_RX_VECT        CAN1_RX0_IRQHandler
+#define CAN1_IRQ_TX_IRQN        CAN1_TX_IRQn
+#define CAN1_IRQ_TX_VECT        CAN1_TX_IRQHandler
+#define CAN1_IRQ_ERROR_IRQN     CAN1_SCE_IRQn
+#define CAN1_IRQ_ERROR_VECT     CAN1_SCE_IRQHandler
+#define CAN1_IRQ_PASSIVE_IRQN   CAN1_SCE_IRQn
+#define CAN1_IRQ_PASSIVE_VECT   CAN1_SCE_IRQHandler
+#define CAN1_IRQ_BUS_IRQN       CAN1_SCE_IRQn
+#define CAN1_IRQ_BUS_VECT       CAN1_SCE_IRQHandler
+
+#define CAN2_IRQ_RX_IRQN        CAN2_RX0_IRQn
+#define CAN2_IRQ_RX_VECT        CAN2_RX0_IRQHandler
+#define CAN2_IRQ_TX_IRQN        CAN2_TX_IRQn
+#define CAN2_IRQ_TX_VECT        CAN2_TX_IRQHandler
+#define CAN2_IRQ_ERROR_IRQN     CAN2_SCE_IRQn
+#define CAN2_IRQ_ERROR_VECT     CAN2_SCE_IRQHandler
+#define CAN2_IRQ_PASSIVE_IRQN   CAN2_SCE_IRQn
+#define CAN2_IRQ_PASSIVE_VECT   CAN2_SCE_IRQHandler
+#define CAN2_IRQ_BUS_IRQN       CAN2_SCE_IRQn
+#define CAN2_IRQ_BUS_VECT       CAN2_SCE_IRQHandler
+
+#endif // DEVICE_CAN
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F4/gpio_irq_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,334 +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 <stddef.h>
-#include "cmsis.h"
-#include "gpio_irq_api.h"
-#include "pinmap.h"
-#include "mbed_error.h"
-
-#define EDGE_NONE (0)
-#define EDGE_RISE (1)
-#define EDGE_FALL (2)
-#define EDGE_BOTH (3)
-
-// Number of EXTI irq vectors (EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5_9, EXTI10_15)
-#define CHANNEL_NUM (7)
-
-// Max pins for one line (max with EXTI10_15)
-#define MAX_PIN_LINE (6)
-
-typedef struct gpio_channel {
-    uint32_t pin_mask;                   // bitmask representing which pins are configured for receiving interrupts
-    uint32_t channel_ids[MAX_PIN_LINE];  // mbed "gpio_irq_t gpio_irq" field of instance
-    uint32_t channel_gpio[MAX_PIN_LINE]; // base address of gpio port group
-    uint32_t channel_pin[MAX_PIN_LINE];  // pin number in port group
-} gpio_channel_t;
-
-static gpio_channel_t channels[CHANNEL_NUM] = {
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0}
-};
-
-// Used to return the index for channels array.
-static uint32_t pin_base_nr[16] = {
-    // EXTI0
-    0, // pin 0
-    // EXTI1
-    0, // pin 1
-    // EXTI2
-    0, // pin 2
-    // EXTI3
-    0, // pin 3
-    // EXTI4
-    0, // pin 4
-    // EXTI5_9
-    0, // pin 5
-    1, // pin 6
-    2, // pin 7
-    3, // pin 8
-    4, // pin 9
-    // EXTI10_15
-    0, // pin 10
-    1, // pin 11
-    2, // pin 12
-    3, // pin 13
-    4, // pin 14
-    5  // pin 15
-};
-
-static gpio_irq_handler irq_handler;
-
-static void handle_interrupt_in(uint32_t irq_index, uint32_t max_num_pin_line)
-{
-    gpio_channel_t *gpio_channel = &channels[irq_index];
-    uint32_t gpio_idx;
-
-    for (gpio_idx = 0; gpio_idx < max_num_pin_line; gpio_idx++) {
-        uint32_t current_mask = (1 << gpio_idx);
-
-        if (gpio_channel->pin_mask & current_mask) {
-            // Retrieve the gpio and pin that generate the irq
-            GPIO_TypeDef *gpio = (GPIO_TypeDef *)(gpio_channel->channel_gpio[gpio_idx]);
-            uint32_t pin = (uint32_t)(1 << (gpio_channel->channel_pin[gpio_idx]));
-
-            // Clear interrupt flag
-            if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) {
-                __HAL_GPIO_EXTI_CLEAR_FLAG(pin);
-
-                if (gpio_channel->channel_ids[gpio_idx] == 0) continue;
-
-                // Check which edge has generated the irq
-                if ((gpio->IDR & pin) == 0) {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_FALL);
-                } else  {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_RISE);
-                }
-            }
-        }
-    }
-}
-
-// EXTI line 0
-static void gpio_irq0(void)
-{
-    handle_interrupt_in(0, 1);
-}
-
-// EXTI line 1
-static void gpio_irq1(void)
-{
-    handle_interrupt_in(1, 1);
-}
-
-// EXTI line 2
-static void gpio_irq2(void)
-{
-    handle_interrupt_in(2, 1);
-}
-
-// EXTI line 3
-static void gpio_irq3(void)
-{
-    handle_interrupt_in(3, 1);
-}
-
-// EXTI line 4
-static void gpio_irq4(void)
-{
-    handle_interrupt_in(4, 1);
-}
-
-// EXTI lines 5 to 9
-static void gpio_irq5(void)
-{
-    handle_interrupt_in(5, 5);
-}
-
-// EXTI lines 10 to 15
-static void gpio_irq6(void)
-{
-    handle_interrupt_in(6, 6);
-}
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
-
-int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
-{
-    IRQn_Type irq_n = (IRQn_Type)0;
-    uint32_t vector = 0;
-    uint32_t irq_index;
-    gpio_channel_t *gpio_channel;
-    uint32_t gpio_idx;
-
-    if (pin == NC) return -1;
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Select irq number and interrupt routine
-    switch (pin_index) {
-        case 0:
-            irq_n = EXTI0_IRQn;
-            vector = (uint32_t)&gpio_irq0;
-            irq_index = 0;
-            break;
-        case 1:
-            irq_n = EXTI1_IRQn;
-            vector = (uint32_t)&gpio_irq1;
-            irq_index = 1;
-            break;
-        case 2:
-            irq_n = EXTI2_IRQn;
-            vector = (uint32_t)&gpio_irq2;
-            irq_index = 2;
-            break;
-        case 3:
-            irq_n = EXTI3_IRQn;
-            vector = (uint32_t)&gpio_irq3;
-            irq_index = 3;
-            break;
-        case 4:
-            irq_n = EXTI4_IRQn;
-            vector = (uint32_t)&gpio_irq4;
-            irq_index = 4;
-            break;
-        case 5:
-        case 6:
-        case 7:
-        case 8:
-        case 9:
-            irq_n = EXTI9_5_IRQn;
-            vector = (uint32_t)&gpio_irq5;
-            irq_index = 5;
-            break;
-        case 10:
-        case 11:
-        case 12:
-        case 13:
-        case 14:
-        case 15:
-            irq_n = EXTI15_10_IRQn;
-            vector = (uint32_t)&gpio_irq6;
-            irq_index = 6;
-            break;
-        default:
-            error("InterruptIn error: pin not supported.\n");
-            return -1;
-    }
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-
-    // Configure GPIO
-    pin_function(pin, STM_PIN_DATA(STM_MODE_IT_FALLING, GPIO_NOPULL, 0));
-
-    // Enable EXTI interrupt
-    NVIC_SetVector(irq_n, vector);
-    NVIC_EnableIRQ(irq_n);
-
-    // Save informations for future use
-    obj->irq_n = irq_n;
-    obj->irq_index = irq_index;
-    obj->event = EDGE_NONE;
-    obj->pin = pin;
-
-    gpio_channel = &channels[irq_index];
-    gpio_idx = pin_base_nr[pin_index];
-    gpio_channel->pin_mask |= (1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = id;
-    gpio_channel->channel_gpio[gpio_idx] = gpio_add;
-    gpio_channel->channel_pin[gpio_idx] = pin_index;
-
-    irq_handler = handler;
-
-    return 0;
-}
-
-void gpio_irq_free(gpio_irq_t *obj)
-{
-    gpio_channel_t *gpio_channel = &channels[obj->irq_index];
-    uint32_t pin_index  = STM_PIN(obj->pin);
-    uint32_t gpio_addr = GPIOA_BASE + (GPIOB_BASE-GPIOA_BASE) * STM_PORT(obj->pin);
-    uint32_t gpio_idx = pin_base_nr[pin_index];
-    
-    HAL_GPIO_DeInit((GPIO_TypeDef *)gpio_addr, (1<<pin_index));
-    gpio_channel->pin_mask &= ~(1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = 0;
-    gpio_channel->channel_gpio[gpio_idx] = 0;
-    gpio_channel->channel_pin[gpio_idx] = 0;
-
-    // Disable EXTI line, but don't change pull-up config
-    pin_function_gpiomode(obj->pin, STM_MODE_INPUT);
-    obj->event = EDGE_NONE;
-}
-
-void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
-{
-    uint32_t mode = STM_MODE_IT_EVT_RESET;
-
-    if (enable) {
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or RISE
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or FALL
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            }
-        }
-    } else { // Disable
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            } else { // NONE or RISE
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            } else { // NONE or FALL
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-    }
-
-    pin_function_gpiomode(obj->pin, mode);
-}
-
-void gpio_irq_enable(gpio_irq_t *obj)
-{
-    NVIC_EnableIRQ(obj->irq_n);
-}
-
-void gpio_irq_disable(gpio_irq_t *obj)
-{
-    NVIC_DisableIRQ(obj->irq_n);
-    obj->event = EDGE_NONE;
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/gpio_irq_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,59 @@
+/* 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 "gpio_irq_device.h"
+
+// Used to return the index for channels array.
+const exti_lines_t pin_lines_desc[16] = {
+    // EXTI0
+    {.gpio_idx = 0, .irq_index = 0, .irq_n = EXTI0_IRQn}, // pin 0
+    // EXTI1
+    {.gpio_idx = 0, .irq_index = 1, .irq_n = EXTI1_IRQn}, // pin 1
+    // EXTI2
+    {.gpio_idx = 0, .irq_index = 2, .irq_n = EXTI2_IRQn}, // pin 2
+    // EXTI3
+    {.gpio_idx = 0, .irq_index = 3, .irq_n = EXTI3_IRQn}, // pin 3
+    // EXTI4
+    {.gpio_idx = 0, .irq_index = 4, .irq_n = EXTI4_IRQn}, // pin 4
+    // EXTI5_9
+    {.gpio_idx = 0, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 5
+    {.gpio_idx = 1, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 6
+    {.gpio_idx = 2, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 7
+    {.gpio_idx = 3, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 8
+    {.gpio_idx = 4, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 9
+    // EXTI10_15
+    {.gpio_idx = 0, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 10
+    {.gpio_idx = 1, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 11
+    {.gpio_idx = 2, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 12
+    {.gpio_idx = 3, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 13
+    {.gpio_idx = 4, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 14
+    {.gpio_idx = 5, .irq_index = 6, .irq_n = EXTI15_10_IRQn}// pin 15
+};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/gpio_irq_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,94 @@
+/* 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_GPIO_IRQ_DEVICE_H
+#define MBED_GPIO_IRQ_DEVICE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// when LL is available, below include can be used
+// #include "stm32f4xx_ll_exti.h"
+// until then let's define locally the required functions
+__STATIC_INLINE void LL_EXTI_EnableRisingTrig_0_31(uint32_t ExtiLine)
+{
+  SET_BIT(EXTI->RTSR, ExtiLine);
+}
+__STATIC_INLINE void LL_EXTI_DisableRisingTrig_0_31(uint32_t ExtiLine)
+{
+  CLEAR_BIT(EXTI->RTSR, ExtiLine);
+}
+__STATIC_INLINE void LL_EXTI_EnableFallingTrig_0_31(uint32_t ExtiLine)
+{
+  SET_BIT(EXTI->FTSR, ExtiLine);
+}
+__STATIC_INLINE void LL_EXTI_DisableFallingTrig_0_31(uint32_t ExtiLine)
+{
+  CLEAR_BIT(EXTI->FTSR, ExtiLine);
+}
+__STATIC_INLINE void LL_EXTI_EnableIT_0_31(uint32_t ExtiLine)
+{
+  SET_BIT(EXTI->IMR, ExtiLine);
+}
+__STATIC_INLINE void LL_EXTI_DisableIT_0_31(uint32_t ExtiLine)
+{
+  CLEAR_BIT(EXTI->IMR, ExtiLine);
+}
+// Above lines shall be later defined in LL
+
+// Number of EXTI irq vectors (EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5_9, EXTI10_15)
+#define CHANNEL_NUM (7)
+
+#define EXTI_IRQ0_NUM_LINES 1
+#define EXTI_IRQ1_NUM_LINES 1
+#define EXTI_IRQ2_NUM_LINES 1
+#define EXTI_IRQ3_NUM_LINES 1
+#define EXTI_IRQ4_NUM_LINES 1
+#define EXTI_IRQ5_NUM_LINES 5
+#define EXTI_IRQ6_NUM_LINES 6
+
+// Max pins for one line (max with EXTI10_15)
+#define MAX_PIN_LINE (EXTI_IRQ6_NUM_LINES)
+
+/*  Structure to describe how the HW EXTI lines are defined in this HW */
+typedef struct exti_lines {
+    uint32_t gpio_idx;   // an index entry for each EXIT line
+    uint32_t irq_index;  // the IRQ index
+    IRQn_Type  irq_n;    // the corresponding EXTI IRQn
+} exti_lines_t;
+
+// Used to return the index for channels array.
+extern const exti_lines_t pin_lines_desc[];
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/pin_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,138 @@
+/* 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.
+ *******************************************************************************
+ */
+#ifndef MBED_PIN_DEVICE_H
+#define MBED_PIN_DEVICE_H
+
+#include "cmsis.h"
+
+// when LL is available, below include can be used
+// #include "stm32f4xx_ll_gpio.h"
+// until then let's define locally the required functions
+#define LL_GPIO_PIN_0                      GPIO_BSRR_BS_0 /*!< Select pin 0 */
+#define LL_GPIO_PIN_1                      GPIO_BSRR_BS_1 /*!< Select pin 1 */
+#define LL_GPIO_PIN_2                      GPIO_BSRR_BS_2 /*!< Select pin 2 */
+#define LL_GPIO_PIN_3                      GPIO_BSRR_BS_3 /*!< Select pin 3 */
+#define LL_GPIO_PIN_4                      GPIO_BSRR_BS_4 /*!< Select pin 4 */
+#define LL_GPIO_PIN_5                      GPIO_BSRR_BS_5 /*!< Select pin 5 */
+#define LL_GPIO_PIN_6                      GPIO_BSRR_BS_6 /*!< Select pin 6 */
+#define LL_GPIO_PIN_7                      GPIO_BSRR_BS_7 /*!< Select pin 7 */
+#define LL_GPIO_PIN_8                      GPIO_BSRR_BS_8 /*!< Select pin 8 */
+#define LL_GPIO_PIN_9                      GPIO_BSRR_BS_9 /*!< Select pin 9 */
+#define LL_GPIO_PIN_10                     GPIO_BSRR_BS_10 /*!< Select pin 10 */
+#define LL_GPIO_PIN_11                     GPIO_BSRR_BS_11 /*!< Select pin 11 */
+#define LL_GPIO_PIN_12                     GPIO_BSRR_BS_12 /*!< Select pin 12 */
+#define LL_GPIO_PIN_13                     GPIO_BSRR_BS_13 /*!< Select pin 13 */
+#define LL_GPIO_PIN_14                     GPIO_BSRR_BS_14 /*!< Select pin 14 */
+#define LL_GPIO_PIN_15                     GPIO_BSRR_BS_15 /*!< Select pin 15 */
+
+#define LL_GPIO_MODE_INPUT                 ((uint32_t)0x00000000U) /*!< Select input mode */
+#define LL_GPIO_MODE_OUTPUT                GPIO_MODER_MODER0_0  /*!< Select output mode */
+#define LL_GPIO_MODE_ALTERNATE             GPIO_MODER_MODER0_1  /*!< Select alternate function mode */
+#define LL_GPIO_MODE_ANALOG                GPIO_MODER_MODER0    /*!< Select analog mode */
+
+#define LL_GPIO_OUTPUT_PUSHPULL            ((uint32_t)0x00000000U) /*!< Select push-pull as output type */
+#define LL_GPIO_OUTPUT_OPENDRAIN           GPIO_OTYPER_OT_0 /*!< Select open-drain as output type */
+
+#define LL_GPIO_PULL_NO                    ((uint32_t)0x00000000U) /*!< Select I/O no pull */
+#define LL_GPIO_PULL_UP                    GPIO_PUPDR_PUPDR0_0 /*!< Select I/O pull up */
+#define LL_GPIO_PULL_DOWN                  GPIO_PUPDR_PUPDR0_1 /*!< Select I/O pull down */
+
+#define LL_GPIO_SPEED_FREQ_LOW             ((uint32_t)0x00000000U) /*!< Select I/O low output speed    */
+#define LL_GPIO_SPEED_FREQ_MEDIUM          GPIO_OSPEEDER_OSPEEDR0_0 /*!< Select I/O medium output speed */
+#define LL_GPIO_SPEED_FREQ_HIGH            GPIO_OSPEEDER_OSPEEDR0_1 /*!< Select I/O fast output speed   */
+#define LL_GPIO_SPEED_FREQ_VERY_HIGH       GPIO_OSPEEDER_OSPEEDR0   /*!< Select I/O high output speed   */
+
+__STATIC_INLINE void LL_GPIO_SetAFPin_0_7(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Alternate)
+{
+  MODIFY_REG(GPIOx->AFR[0], (0xFU << (POSITION_VAL(Pin) * 4U)),
+             (Alternate << (POSITION_VAL(Pin) * 4U)));
+}
+__STATIC_INLINE void LL_GPIO_SetAFPin_8_15(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Alternate)
+{
+  MODIFY_REG(GPIOx->AFR[1], (0xFU << (POSITION_VAL(Pin >> 8U) * 4U)),
+             (Alternate << (POSITION_VAL(Pin >> 8U) * 4U)));
+}
+__STATIC_INLINE void LL_GPIO_SetPinMode(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Mode)
+{
+  MODIFY_REG(GPIOx->MODER, (GPIO_MODER_MODER0 << (POSITION_VAL(Pin) * 2U)), (Mode << (POSITION_VAL(Pin) * 2U)));
+}
+__STATIC_INLINE uint32_t LL_GPIO_GetPinMode(GPIO_TypeDef *GPIOx, uint32_t Pin)
+{
+  return (uint32_t)(READ_BIT(GPIOx->MODER, ((Pin * Pin) * GPIO_MODER_MODER0)) / (Pin * Pin));
+}
+__STATIC_INLINE void LL_GPIO_SetPinPull(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Pull)
+{
+  MODIFY_REG(GPIOx->PUPDR, (GPIO_PUPDR_PUPDR0 << (POSITION_VAL(Pin) * 2U)), (Pull << (POSITION_VAL(Pin) * 2U)));
+}
+__STATIC_INLINE void LL_GPIO_SetPinOutputType(GPIO_TypeDef *GPIOx, uint32_t PinMask, uint32_t OutputType)
+{
+  MODIFY_REG(GPIOx->OTYPER, PinMask, (PinMask * OutputType));
+}
+__STATIC_INLINE void LL_GPIO_SetPinSpeed(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t  Speed)
+{
+  MODIFY_REG(GPIOx->OSPEEDR, (GPIO_OSPEEDER_OSPEEDR0 << (POSITION_VAL(Pin) * 2U)),
+             (Speed << (POSITION_VAL(Pin) * 2U)));
+}
+// Above lines shall be defined in LL when available
+
+extern const uint32_t ll_pin_defines[16];
+
+/* Family specific implementations */
+static inline void stm_pin_DisconnectDebug(PinName pin)
+{
+    /* empty for now */
+}
+
+static inline void stm_pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t pull_config)
+{
+    switch (pull_config) {
+        case GPIO_PULLUP:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_UP);
+            break;
+        case GPIO_PULLDOWN:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_DOWN);
+            break;
+        default:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_NO);
+            break;
+    }
+}
+
+static inline void stm_pin_SetAFPin( GPIO_TypeDef *gpio, PinName pin, uint32_t afnum)
+{
+    uint32_t ll_pin  = ll_pin_defines[STM_PIN(pin)];
+
+    if (STM_PIN(pin) > 7)
+        LL_GPIO_SetAFPin_8_15(gpio, ll_pin, afnum);
+    else
+        LL_GPIO_SetAFPin_0_7(gpio, ll_pin, afnum);
+}
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F4/pinmap.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,197 +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 "mbed_assert.h"
-#include "pinmap.h"
-#include "PortNames.h"
-#include "mbed_error.h"
-
-// GPIO mode look-up table
-static const uint32_t gpio_mode[13] = {
-    0x00000000, //  0 = GPIO_MODE_INPUT
-    0x00000001, //  1 = GPIO_MODE_OUTPUT_PP
-    0x00000011, //  2 = GPIO_MODE_OUTPUT_OD
-    0x00000002, //  3 = GPIO_MODE_AF_PP
-    0x00000012, //  4 = GPIO_MODE_AF_OD
-    0x00000003, //  5 = GPIO_MODE_ANALOG
-    0x10110000, //  6 = GPIO_MODE_IT_RISING
-    0x10210000, //  7 = GPIO_MODE_IT_FALLING
-    0x10310000, //  8 = GPIO_MODE_IT_RISING_FALLING
-    0x10120000, //  9 = GPIO_MODE_EVT_RISING
-    0x10220000, // 10 = GPIO_MODE_EVT_FALLING
-    0x10320000, // 11 = GPIO_MODE_EVT_RISING_FALLING
-    0x10000000  // 12 = Reset GPIO_MODE_IT_EVT
-};
-
-// Enable GPIO clock and return GPIO base address
-uint32_t Set_GPIO_Clock(uint32_t port_idx)
-{
-    uint32_t gpio_add = 0;
-    switch (port_idx) {
-        case PortA:
-            gpio_add = GPIOA_BASE;
-            __GPIOA_CLK_ENABLE();
-            break;
-        case PortB:
-            gpio_add = GPIOB_BASE;
-            __GPIOB_CLK_ENABLE();
-            break;
-        case PortC:
-            gpio_add = GPIOC_BASE;
-            __GPIOC_CLK_ENABLE();
-            break;
-#if defined GPIOD_BASE
-        case PortD:
-            gpio_add = GPIOD_BASE;
-            __GPIOD_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOE_BASE
-        case PortE:
-            gpio_add = GPIOE_BASE;
-            __GPIOE_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOF_BASE
-        case PortF:
-            gpio_add = GPIOF_BASE;
-            __GPIOF_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOG_BASE
-        case PortG:
-            gpio_add = GPIOG_BASE;
-            __GPIOG_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOH_BASE
-        case PortH:
-            gpio_add = GPIOH_BASE;
-            __GPIOH_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOI_BASE
-        case PortI:
-            gpio_add = GPIOI_BASE;
-            __GPIOI_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOJ_BASE
-        case PortJ:
-            gpio_add = GPIOJ_BASE;
-            __GPIOJ_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOK_BASE
-        case PortK:
-            gpio_add = GPIOK_BASE;
-            __GPIOK_CLK_ENABLE();
-            break;
-#endif
-        default:
-            error("Pinmap error: wrong port number.");
-            break;
-    }
-    return gpio_add;
-}
-
-/**
- * Configure pin (mode, speed, output type and pull-up/pull-down)
- */
-void pin_function(PinName pin, int data)
-{
-    MBED_ASSERT(pin != (PinName)NC);
-    // Get the pin informations
-    uint32_t mode  = STM_PIN_MODE(data);
-    uint32_t pupd  = STM_PIN_PUPD(data);
-    uint32_t afnum = STM_PIN_AFNUM(data);
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Configure GPIO
-    GPIO_InitTypeDef GPIO_InitStructure;
-    GPIO_InitStructure.Pin       = (uint32_t)(1 << pin_index);
-    GPIO_InitStructure.Mode      = gpio_mode[mode];
-    GPIO_InitStructure.Pull      = pupd;
-    GPIO_InitStructure.Speed     = GPIO_SPEED_HIGH;
-    GPIO_InitStructure.Alternate = afnum;
-    HAL_GPIO_Init(gpio, &GPIO_InitStructure);
-
-    // [TODO] Disconnect JTAG-DP + SW-DP signals.
-    // Warning: Need to reconnect under reset
-    //if ((pin == PA_13) || (pin == PA_14)) {
-    //
-    //}
-    //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) {
-    //
-    //}
-}
-
-/**
- * Configure pin pull-up/pull-down
- */
-void pin_mode(PinName pin, PinMode mode)
-{
-    MBED_ASSERT(pin != (PinName)NC);
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Configure pull-up/pull-down resistors
-    uint32_t pupd = (uint32_t)mode;
-    if (pupd > 2) {
-        pupd = 0; // Open-drain = No pull-up/No pull-down
-    }
-    gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2)));
-    gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
-}
-
-/*  Internal function for setting the gpiomode/function
- *  without changing Pull mode
- */
-void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
-
-    /* Read current pull state from HW to avoid over-write*/
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
-    uint32_t temp = gpio->PUPDR;
-    uint32_t pull = (temp  >> (pin_index * 2U)) & GPIO_PUPDR_PUPDR0;
-
-    /* Then re-use global function for updating the mode part*/
-    pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
-}
--- a/targets/TARGET_STM/TARGET_STM32F4/port_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,103 +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 "port_api.h"
-#include "pinmap.h"
-#include "gpio_api.h"
-#include "mbed_error.h"
-
-#if DEVICE_PORTIN || DEVICE_PORTOUT
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-
-// high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
-// low nibble  = pin number
-PinName port_pin(PortName port, int pin_n)
-{
-    return (PinName)(pin_n + (port << 4));
-}
-
-void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
-{
-    uint32_t port_index = (uint32_t)port;
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Fill PORT object structure for future use
-    obj->port      = port;
-    obj->mask      = mask;
-    obj->direction = dir;
-    obj->reg_in    = &gpio->IDR;
-    obj->reg_out   = &gpio->ODR;
-
-    port_dir(obj, dir);
-}
-
-void port_dir(port_t *obj, PinDirection dir)
-{
-    uint32_t i;
-    obj->direction = dir;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            if (dir == PIN_OUTPUT) {
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
-            } else { // PIN_INPUT
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
-            }
-        }
-    }
-}
-
-void port_mode(port_t *obj, PinMode mode)
-{
-    uint32_t i;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            pin_mode(port_pin(obj->port, i), mode);
-        }
-    }
-}
-
-void port_write(port_t *obj, int value)
-{
-    *obj->reg_out = (*obj->reg_out & ~obj->mask) | (value & obj->mask);
-}
-
-int port_read(port_t *obj)
-{
-    if (obj->direction == PIN_OUTPUT) {
-        return (*obj->reg_out & obj->mask);
-    } else { // PIN_INPUT
-        return (*obj->reg_in & obj->mask);
-    }
-}
-
-#endif
--- a/targets/TARGET_STM/TARGET_STM32F4/pwmout_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,302 +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 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;
-    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);
-    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;
-    }
-
-    /* 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,57 @@
+/* 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;
+
+extern const pwm_apb_map_t pwm_apb_map_table[];
+
+#endif // DEVICE_PWMOUT
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/device/TOOLCHAIN_IAR/stm32f746xg.icf	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/device/TOOLCHAIN_IAR/stm32f746xg.icf	Tue Mar 14 16:40:56 2017 +0000
@@ -19,9 +19,9 @@
 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 };
--- a/targets/TARGET_STM/TARGET_STM32F7/analogin_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F7/analogin_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -30,7 +30,7 @@
 
 #if DEVICE_ANALOGIN
 
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "cmsis.h"
 #include "pinmap.h"
 #include "PeripheralPins.h"
--- a/targets/TARGET_STM/TARGET_STM32F7/can_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,542 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2016 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "can_api.h"
-
-#if DEVICE_CAN
-
-#include "cmsis.h"
-#include "pinmap.h"
-#include "PeripheralPins.h"
-#include "mbed_error.h"
-#include <math.h>
-#include <string.h>
-
-#define CAN_NUM    2
-static CAN_HandleTypeDef CanHandle;
-static uint32_t can_irq_ids[CAN_NUM] = {0};
-static can_irq_handler irq_handler;
-
-void can_init(can_t *obj, PinName rd, PinName td) 
-{
-    uint32_t filter_number;
-    CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
-    CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
-    obj->can = (CANName)pinmap_merge(can_rd, can_td);
-    MBED_ASSERT((int)obj->can != NC);    
-
-    if(obj->can == CAN_1) {
-        __HAL_RCC_CAN1_CLK_ENABLE();
-        obj->index = 0;
-    } else {
-        __HAL_RCC_CAN2_CLK_ENABLE();
-        obj->index = 1;
-    }
-
-    // Configure the CAN pins
-    pinmap_pinout(rd, PinMap_CAN_RD);
-    pinmap_pinout(td, PinMap_CAN_TD);
-    if (rd != NC) {
-        pin_mode(rd, PullUp);
-    }
-    if (td != NC) {
-        pin_mode(td, PullUp);
-    }
-
-    CanHandle.Instance = (CAN_TypeDef *)(obj->can);
-
-    CanHandle.Init.TTCM = DISABLE;
-    CanHandle.Init.ABOM = DISABLE;
-    CanHandle.Init.AWUM = DISABLE;
-    CanHandle.Init.NART = DISABLE;
-    CanHandle.Init.RFLM = DISABLE;
-    CanHandle.Init.TXFP = DISABLE;
-    CanHandle.Init.Mode = CAN_MODE_NORMAL;
-    CanHandle.Init.SJW = CAN_SJW_1TQ;
-    CanHandle.Init.BS1 = CAN_BS1_6TQ;
-    CanHandle.Init.BS2 = CAN_BS2_8TQ;
-    CanHandle.Init.Prescaler = 2;
-
-    if (HAL_CAN_Init(&CanHandle) != HAL_OK) {
-       error("Cannot initialize CAN");
-    }
-
-    filter_number = (obj->can == CAN_1) ? 0 : 14;
-
-    // Set initial CAN frequency to 100kb/s
-    can_frequency(obj, 100000);
-
-    can_filter(obj, 0, 0, CANStandard, filter_number);
-}
-
-void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id) 
-{
-    irq_handler = handler;
-    can_irq_ids[obj->index] = id;    
-}
-
-void can_irq_free(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-  
-    can->IER &= ~(CAN_IT_FMP0 | CAN_IT_FMP1 | CAN_IT_TME | \
-                  CAN_IT_ERR | CAN_IT_EPV | CAN_IT_BOF);
-    can_irq_ids[obj->can] = 0;
-}
-
-void can_free(can_t *obj) 
-{
-    // Reset CAN and disable clock
-    if (obj->can == CAN_1) {
-        __HAL_RCC_CAN1_FORCE_RESET();
-        __HAL_RCC_CAN1_RELEASE_RESET();
-        __HAL_RCC_CAN1_CLK_DISABLE();
-    }
-
-    if (obj->can == CAN_2) {
-        __HAL_RCC_CAN2_FORCE_RESET();
-        __HAL_RCC_CAN2_RELEASE_RESET();
-        __HAL_RCC_CAN2_CLK_DISABLE();
-    }
-}
-
-// The following table is used to program bit_timing. It is an adjustment of the sample
-// point by synchronizing on the start-bit edge and resynchronizing on the following edges.
-// This table has the sampling points as close to 75% as possible (most commonly used).
-// The first value is TSEG1, the second TSEG2.
-static const int timing_pts[23][2] = {
-    {0x0, 0x0},      // 2,  50%
-    {0x1, 0x0},      // 3,  67%
-    {0x2, 0x0},      // 4,  75%
-    {0x3, 0x0},      // 5,  80%
-    {0x3, 0x1},      // 6,  67%
-    {0x4, 0x1},      // 7,  71%
-    {0x5, 0x1},      // 8,  75%
-    {0x6, 0x1},      // 9,  78%
-    {0x6, 0x2},      // 10, 70%
-    {0x7, 0x2},      // 11, 73%
-    {0x8, 0x2},      // 12, 75%
-    {0x9, 0x2},      // 13, 77%
-    {0x9, 0x3},      // 14, 71%
-    {0xA, 0x3},      // 15, 73%
-    {0xB, 0x3},      // 16, 75%
-    {0xC, 0x3},      // 17, 76%
-    {0xD, 0x3},      // 18, 78%
-    {0xD, 0x4},      // 19, 74%
-    {0xE, 0x4},      // 20, 75%
-    {0xF, 0x4},      // 21, 76%
-    {0xF, 0x5},      // 22, 73%
-    {0xF, 0x6},      // 23, 70%
-    {0xF, 0x7},      // 24, 67%
-};
-
-static unsigned int can_speed(unsigned int pclk, unsigned int cclk, unsigned char psjw) 
-{
-    uint32_t    btr;
-    uint16_t    brp = 0;
-    uint32_t    calcbit;
-    uint32_t    bitwidth;
-    int         hit = 0;
-    int         bits;
-    
-    bitwidth = (pclk / cclk);
-    
-    brp = bitwidth / 0x18;
-    while ((!hit) && (brp < bitwidth / 4)) {
-        brp++;
-        for (bits = 22; bits > 0; bits--) {
-            calcbit = (bits + 3) * (brp + 1);
-            if (calcbit == bitwidth) {
-                hit = 1;
-                break;
-            }
-        }
-    }
-    
-    if (hit) {
-        btr = ((timing_pts[bits][1] << 20) & 0x00700000)
-            | ((timing_pts[bits][0] << 16) & 0x000F0000)
-            | ((psjw                << 24) & 0x0000C000)
-            | ((brp                 <<  0) & 0x000003FF);
-    } else {
-        btr = 0xFFFFFFFF;
-    }
-    
-    return btr;
-
-}
-
-int can_frequency(can_t *obj, int f) 
-{
-    int pclk = HAL_RCC_GetPCLK1Freq();
-    int btr = can_speed(pclk, (unsigned int)f, 1);
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    if (btr > 0) {
-        can->MCR |= CAN_MCR_INRQ ;
-        while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-        }
-        can->BTR = btr;
-        can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-        while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-        }
-        return 1;
-    } else {
-        return 0;
-    }
-}
-
-int can_write(can_t *obj, CAN_Message msg, int cc) 
-{
-    uint32_t  transmitmailbox = 5;
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    /* Select one empty transmit mailbox */
-    if ((can->TSR&CAN_TSR_TME0) == CAN_TSR_TME0) {
-        transmitmailbox = 0;
-    } else if ((can->TSR&CAN_TSR_TME1) == CAN_TSR_TME1) {
-        transmitmailbox = 1;
-    } else if ((can->TSR&CAN_TSR_TME2) == CAN_TSR_TME2) {
-        transmitmailbox = 2;
-    } else {
-        transmitmailbox = CAN_TXSTATUS_NOMAILBOX;
-    }
-
-    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) |
-                                             ((uint32_t)msg.data[4]));
-    /* Request transmission */
-    can->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ;
-  }
-
-  return 1;
-}
-
-int can_read(can_t *obj, CAN_Message *msg, int handle) 
-{
-    //handle is the FIFO number
-
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    
-    // check FPM0 which holds the pending message count in FIFO 0
-    // if no message is pending, return 0
-    if ((can->RF0R & CAN_RF0R_FMP0) == 0) {
-        return 0;
-    }
-
-    /* Get the Id */
-    msg->format = (CANFormat)((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR);
-    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);
-    /* Get the DLC */
-    msg->len = (uint8_t)0x0F & can->sFIFOMailBox[handle].RDTR;
-//  /* Get the FMI */
-//  msg->FMI = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDTR >> 8);
-    /* Get the data field */
-    msg->data[0] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDLR;
-    msg->data[1] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 8);
-    msg->data[2] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 16);
-    msg->data[3] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 24);
-    msg->data[4] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDHR;
-    msg->data[5] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 8);
-    msg->data[6] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 16);
-    msg->data[7] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 24);
-    
-    /* Release the FIFO */
-    if(handle == CAN_FIFO0) {
-        /* Release FIFO0 */
-        can->RF0R |= CAN_RF0R_RFOM0;
-    } else { /* FIFONumber == CAN_FIFO1 */
-      /* Release FIFO1 */
-      can->RF1R |= CAN_RF1R_RFOM1;
-    }
-
-    return 1;
-}
-
-void can_reset(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    can->MCR |= CAN_MCR_RESET;  
-    can->ESR = 0x0;
-}
-
-unsigned char can_rderror(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    return (can->ESR >> 24) & 0xFF;
-}
-
-unsigned char can_tderror(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    return (can->ESR >> 16) & 0xFF;
-}
-
-void can_monitor(can_t *obj, int silent) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);        
-    
-    can->MCR |= CAN_MCR_INRQ ;
-    while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-    }
-    if (silent) {
-        can->BTR |= ((uint32_t)1 << 31);
-    } else {
-        can->BTR &= ~((uint32_t)1 << 31);
-    }
-    can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-    while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-    }
-}
-
-int can_mode(can_t *obj, CanMode mode) 
-{
-    int success = 0;
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-    can->MCR |= CAN_MCR_INRQ ;
-    while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-    }
-    switch (mode) {
-        case MODE_NORMAL:
-            can->BTR &= ~(CAN_BTR_SILM | CAN_BTR_LBKM);
-            success = 1;
-            break;
-        case MODE_SILENT:
-            can->BTR |= CAN_BTR_SILM;
-            can->BTR &= ~CAN_BTR_LBKM;
-            success = 1;
-            break;
-        case MODE_TEST_GLOBAL:
-        case MODE_TEST_LOCAL:
-            can->BTR |= CAN_BTR_LBKM;
-            can->BTR &= ~CAN_BTR_SILM;
-            success = 1;
-            break;
-        case MODE_TEST_SILENT:
-            can->BTR |= (CAN_BTR_SILM | CAN_BTR_LBKM);
-            success = 1;
-            break;
-        default:
-            success = 0;
-            break;
-    }
-    can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-    while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-    }
-    return success;
-}
-
-int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) 
-{
-    CanHandle.Instance = (CAN_TypeDef *)(obj->can);
-    CAN_FilterConfTypeDef  sFilterConfig;
-    
-    sFilterConfig.FilterNumber = handle;
-    sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
-    sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
-    sFilterConfig.FilterIdHigh = (uint8_t) (id >> 8);
-    sFilterConfig.FilterIdLow = (uint8_t) id;
-    sFilterConfig.FilterMaskIdHigh = (uint8_t) (mask >> 8);
-    sFilterConfig.FilterMaskIdLow = (uint8_t) mask;
-    sFilterConfig.FilterFIFOAssignment = 0;
-    sFilterConfig.FilterActivation = ENABLE;
-    sFilterConfig.BankNumber = 14;
-  
-    HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig);
-      
-    return 0;
-}
-
-static void can_irq(CANName name, int id) 
-{
-    uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0;    
-    CanHandle.Instance = (CAN_TypeDef *)name;
-    
-    if(__HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_TME)) {
-        tmp1 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_0);
-        tmp2 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_1);
-        tmp3 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_2);
-        if(tmp1 || tmp2 || tmp3)  
-        {
-            irq_handler(can_irq_ids[id], IRQ_TX);
-        }
-    }
-  
-    tmp1 = __HAL_CAN_MSG_PENDING(&CanHandle, CAN_FIFO0);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_FMP0);
-  
-    if((tmp1 != 0) && tmp2) {
-         irq_handler(can_irq_ids[id], IRQ_RX);
-    }
-  
-    tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_EPV);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_EPV);
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR); 
-  
-    if(tmp1 && tmp2 && tmp3) {
-         irq_handler(can_irq_ids[id], IRQ_PASSIVE);
-    }
-  
-    tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_BOF);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_BOF);
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);  
-    if(tmp1 && tmp2 && tmp3) {
-        irq_handler(can_irq_ids[id], IRQ_BUS);
-    }
-  
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);  
-    if(tmp1 && tmp2 && tmp3) {
-        irq_handler(can_irq_ids[id], IRQ_ERROR);
-    }  
-}
-
-void CAN1_RX0_IRQHandler(void) 
-{
-    can_irq(CAN_1, 0);
-}
-
-void CAN1_TX_IRQHandler(void) 
-{
-    can_irq(CAN_1, 0);
-}
-
-void CAN1_SCE_IRQHandler(void) 
-{
-    can_irq(CAN_1, 0);
-}
-
-void CAN2_RX0_IRQHandler(void) 
-{
-    can_irq(CAN_2, 1);
-}
-
-void CAN2_TX_IRQHandler(void)
-{
-    can_irq(CAN_2, 1);
-}
-
-void CAN2_SCE_IRQHandler(void)
-{
-    can_irq(CAN_2, 1);
-}
-
-void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
-{
-
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-    IRQn_Type irq_n = (IRQn_Type)0;
-    uint32_t vector = 0;    
-    uint32_t ier;
-
-    if(obj->can == CAN_1) {
-        switch (type) {
-            case IRQ_RX:
-                ier = CAN_IT_FMP0;
-                irq_n = CAN1_RX0_IRQn;
-                vector = (uint32_t)&CAN1_RX0_IRQHandler;
-                break;
-            case IRQ_TX:      
-                ier = CAN_IT_TME;
-                irq_n = CAN1_TX_IRQn;
-                vector = (uint32_t)&CAN1_TX_IRQHandler;
-                break;
-            case IRQ_ERROR:   
-                ier = CAN_IT_ERR;
-                irq_n = CAN1_SCE_IRQn;
-                vector = (uint32_t)&CAN1_SCE_IRQHandler;
-                break;
-            case IRQ_PASSIVE:
-                ier = CAN_IT_EPV;
-                irq_n = CAN1_SCE_IRQn;
-                vector = (uint32_t)&CAN1_SCE_IRQHandler;
-                break;
-            case IRQ_BUS:
-                ier = CAN_IT_BOF;
-                irq_n = CAN1_SCE_IRQn;
-                vector = (uint32_t)&CAN1_SCE_IRQHandler;
-                break;
-            default: return;
-        }
-    } else {
-        switch (type) {
-              case IRQ_RX:
-                  ier = CAN_IT_FMP0;
-                  irq_n = CAN2_RX0_IRQn;
-                  vector = (uint32_t)&CAN2_RX0_IRQHandler;
-                  break;
-              case IRQ_TX:      
-                  ier = CAN_IT_TME;
-                  irq_n = CAN2_TX_IRQn;
-                  vector = (uint32_t)&CAN2_TX_IRQHandler;
-                  break;
-              case IRQ_ERROR:   
-                  ier = CAN_IT_ERR;
-                  irq_n = CAN2_SCE_IRQn;
-                  vector = (uint32_t)&CAN2_SCE_IRQHandler;
-                  break;
-              case IRQ_PASSIVE:
-                  ier = CAN_IT_EPV;
-                  irq_n = CAN2_SCE_IRQn;
-                  vector = (uint32_t)&CAN2_SCE_IRQHandler;
-                  break;
-              case IRQ_BUS:
-                  ier = CAN_IT_BOF;
-                  irq_n = CAN2_SCE_IRQn;
-                  vector = (uint32_t)&CAN2_SCE_IRQHandler;
-                  break;
-              default: return;
-        }
-    }
-
-    if(enable) {
-        can->IER |= ier;
-    } else {
-        can->IER &= ~ier;
-    }
-
-    NVIC_SetVector(irq_n, vector);
-    NVIC_EnableIRQ(irq_n);
-}
-
-#endif // DEVICE_CAN
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F7/can_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,53 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef MBED_CAN_DEVICE_H
+#define MBED_CAN_DEVICE_H
+
+#include "cmsis.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef DEVICE_CAN
+
+#define CAN_NUM 2 // Number of CAN peripherals present in the STM32 serie (1 or 2)
+
+#define CAN1_IRQ_RX_IRQN        CAN1_RX0_IRQn
+#define CAN1_IRQ_RX_VECT        CAN1_RX0_IRQHandler
+#define CAN1_IRQ_TX_IRQN        CAN1_TX_IRQn
+#define CAN1_IRQ_TX_VECT        CAN1_TX_IRQHandler
+#define CAN1_IRQ_ERROR_IRQN     CAN1_SCE_IRQn
+#define CAN1_IRQ_ERROR_VECT     CAN1_SCE_IRQHandler
+#define CAN1_IRQ_PASSIVE_IRQN   CAN1_SCE_IRQn
+#define CAN1_IRQ_PASSIVE_VECT   CAN1_SCE_IRQHandler
+#define CAN1_IRQ_BUS_IRQN       CAN1_SCE_IRQn
+#define CAN1_IRQ_BUS_VECT       CAN1_SCE_IRQHandler
+
+#define CAN2_IRQ_RX_IRQN        CAN2_RX0_IRQn
+#define CAN2_IRQ_RX_VECT        CAN2_RX0_IRQHandler
+#define CAN2_IRQ_TX_IRQN        CAN2_TX_IRQn
+#define CAN2_IRQ_TX_VECT        CAN2_TX_IRQHandler
+#define CAN2_IRQ_ERROR_IRQN     CAN2_SCE_IRQn
+#define CAN2_IRQ_ERROR_VECT     CAN2_SCE_IRQHandler
+#define CAN2_IRQ_PASSIVE_IRQN   CAN2_SCE_IRQn
+#define CAN2_IRQ_PASSIVE_VECT   CAN2_SCE_IRQHandler
+#define CAN2_IRQ_BUS_IRQN       CAN2_SCE_IRQn
+#define CAN2_IRQ_BUS_VECT       CAN2_SCE_IRQHandler
+
+#endif // DEVICE_CAN
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F7/gpio_irq_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,334 +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 <stddef.h>
-#include "cmsis.h"
-#include "gpio_irq_api.h"
-#include "pinmap.h"
-#include "mbed_error.h"
-
-#define EDGE_NONE (0)
-#define EDGE_RISE (1)
-#define EDGE_FALL (2)
-#define EDGE_BOTH (3)
-
-// Number of EXTI irq vectors (EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5_9, EXTI10_15)
-#define CHANNEL_NUM (7)
-
-// Max pins for one line (max with EXTI10_15)
-#define MAX_PIN_LINE (6)
-
-typedef struct gpio_channel {
-    uint32_t pin_mask;                   // bitmask representing which pins are configured for receiving interrupts
-    uint32_t channel_ids[MAX_PIN_LINE];  // mbed "gpio_irq_t gpio_irq" field of instance
-    uint32_t channel_gpio[MAX_PIN_LINE]; // base address of gpio port group
-    uint32_t channel_pin[MAX_PIN_LINE];  // pin number in port group
-} gpio_channel_t;
-
-static gpio_channel_t channels[CHANNEL_NUM] = {
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0}
-};
-
-// Used to return the index for channels array.
-static uint32_t pin_base_nr[16] = {
-    // EXTI0
-    0, // pin 0
-    // EXTI1
-    0, // pin 1
-    // EXTI2
-    0, // pin 2
-    // EXTI3
-    0, // pin 3
-    // EXTI4
-    0, // pin 4
-    // EXTI5_9
-    0, // pin 5
-    1, // pin 6
-    2, // pin 7
-    3, // pin 8
-    4, // pin 9
-    // EXTI10_15
-    0, // pin 10
-    1, // pin 11
-    2, // pin 12
-    3, // pin 13
-    4, // pin 14
-    5  // pin 15
-};
-
-static gpio_irq_handler irq_handler;
-
-static void handle_interrupt_in(uint32_t irq_index, uint32_t max_num_pin_line)
-{
-    gpio_channel_t *gpio_channel = &channels[irq_index];
-    uint32_t gpio_idx;
-
-    for (gpio_idx = 0; gpio_idx < max_num_pin_line; gpio_idx++) {
-        uint32_t current_mask = (1 << gpio_idx);
-
-        if (gpio_channel->pin_mask & current_mask) {
-            // Retrieve the gpio and pin that generate the irq
-            GPIO_TypeDef *gpio = (GPIO_TypeDef *)(gpio_channel->channel_gpio[gpio_idx]);
-            uint32_t pin = (uint32_t)(1 << (gpio_channel->channel_pin[gpio_idx]));
-
-            // Clear interrupt flag
-            if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) {
-                __HAL_GPIO_EXTI_CLEAR_FLAG(pin);
-
-                if (gpio_channel->channel_ids[gpio_idx] == 0) continue;
-
-                // Check which edge has generated the irq
-                if ((gpio->IDR & pin) == 0) {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_FALL);
-                } else  {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_RISE);
-                }
-            }
-        }
-    }
-}
-
-// EXTI line 0
-static void gpio_irq0(void)
-{
-    handle_interrupt_in(0, 1);
-}
-
-// EXTI line 1
-static void gpio_irq1(void)
-{
-    handle_interrupt_in(1, 1);
-}
-
-// EXTI line 2
-static void gpio_irq2(void)
-{
-    handle_interrupt_in(2, 1);
-}
-
-// EXTI line 3
-static void gpio_irq3(void)
-{
-    handle_interrupt_in(3, 1);
-}
-
-// EXTI line 4
-static void gpio_irq4(void)
-{
-    handle_interrupt_in(4, 1);
-}
-
-// EXTI lines 5 to 9
-static void gpio_irq5(void)
-{
-    handle_interrupt_in(5, 5);
-}
-
-// EXTI lines 10 to 15
-static void gpio_irq6(void)
-{
-    handle_interrupt_in(6, 6);
-}
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
-
-int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
-{
-    IRQn_Type irq_n = (IRQn_Type)0;
-    uint32_t vector = 0;
-    uint32_t irq_index;
-    gpio_channel_t *gpio_channel;
-    uint32_t gpio_idx;
-
-    if (pin == NC) return -1;
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Select irq number and interrupt routine
-    switch (pin_index) {
-        case 0:
-            irq_n = EXTI0_IRQn;
-            vector = (uint32_t)&gpio_irq0;
-            irq_index = 0;
-            break;
-        case 1:
-            irq_n = EXTI1_IRQn;
-            vector = (uint32_t)&gpio_irq1;
-            irq_index = 1;
-            break;
-        case 2:
-            irq_n = EXTI2_IRQn;
-            vector = (uint32_t)&gpio_irq2;
-            irq_index = 2;
-            break;
-        case 3:
-            irq_n = EXTI3_IRQn;
-            vector = (uint32_t)&gpio_irq3;
-            irq_index = 3;
-            break;
-        case 4:
-            irq_n = EXTI4_IRQn;
-            vector = (uint32_t)&gpio_irq4;
-            irq_index = 4;
-            break;
-        case 5:
-        case 6:
-        case 7:
-        case 8:
-        case 9:
-            irq_n = EXTI9_5_IRQn;
-            vector = (uint32_t)&gpio_irq5;
-            irq_index = 5;
-            break;
-        case 10:
-        case 11:
-        case 12:
-        case 13:
-        case 14:
-        case 15:
-            irq_n = EXTI15_10_IRQn;
-            vector = (uint32_t)&gpio_irq6;
-            irq_index = 6;
-            break;
-        default:
-            error("InterruptIn error: pin not supported.\n");
-            return -1;
-    }
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-
-    // Configure GPIO
-    pin_function(pin, STM_PIN_DATA(STM_MODE_IT_FALLING, GPIO_NOPULL, 0));
-
-    // Enable EXTI interrupt
-    NVIC_SetVector(irq_n, vector);
-    NVIC_EnableIRQ(irq_n);
-
-    // Save informations for future use
-    obj->irq_n = irq_n;
-    obj->irq_index = irq_index;
-    obj->event = EDGE_NONE;
-    obj->pin = pin;
-
-    gpio_channel = &channels[irq_index];
-    gpio_idx = pin_base_nr[pin_index];
-    gpio_channel->pin_mask |= (1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = id;
-    gpio_channel->channel_gpio[gpio_idx] = gpio_add;
-    gpio_channel->channel_pin[gpio_idx] = pin_index;
-
-    irq_handler = handler;
-
-    return 0;
-}
-
-void gpio_irq_free(gpio_irq_t *obj)
-{
-    gpio_channel_t *gpio_channel = &channels[obj->irq_index];
-    uint32_t pin_index  = STM_PIN(obj->pin);
-    uint32_t gpio_addr = GPIOA_BASE + (GPIOB_BASE-GPIOA_BASE) * STM_PORT(obj->pin);
-    uint32_t gpio_idx = pin_base_nr[pin_index];
-    
-    HAL_GPIO_DeInit((GPIO_TypeDef *)gpio_addr, (1<<pin_index));
-    gpio_channel->pin_mask &= ~(1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = 0;
-    gpio_channel->channel_gpio[gpio_idx] = 0;
-    gpio_channel->channel_pin[gpio_idx] = 0;
-
-    // Disable EXTI line, but don't change pull-up config
-    pin_function_gpiomode(obj->pin, STM_MODE_INPUT);
-    obj->event = EDGE_NONE;
-}
-
-void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
-{
-    uint32_t mode = STM_MODE_IT_EVT_RESET;
-
-    if (enable) {
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or RISE
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or FALL
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            }
-        }
-    } else { // Disable
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            } else { // NONE or RISE
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            } else { // NONE or FALL
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-    }
-
-    pin_function_gpiomode(obj->pin, mode);
-}
-
-void gpio_irq_enable(gpio_irq_t *obj)
-{
-    NVIC_EnableIRQ(obj->irq_n);
-}
-
-void gpio_irq_disable(gpio_irq_t *obj)
-{
-    NVIC_DisableIRQ(obj->irq_n);
-    obj->event = EDGE_NONE;
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F7/gpio_irq_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,59 @@
+/* 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 "gpio_irq_device.h"
+
+// Used to return the index for channels array.
+const exti_lines_t pin_lines_desc[16] = {
+    // EXTI0
+    {.gpio_idx = 0, .irq_index = 0, .irq_n = EXTI0_IRQn}, // pin 0
+    // EXTI1
+    {.gpio_idx = 0, .irq_index = 1, .irq_n = EXTI1_IRQn}, // pin 1
+    // EXTI2
+    {.gpio_idx = 0, .irq_index = 2, .irq_n = EXTI2_IRQn}, // pin 2
+    // EXTI3
+    {.gpio_idx = 0, .irq_index = 3, .irq_n = EXTI3_IRQn}, // pin 3
+    // EXTI4
+    {.gpio_idx = 0, .irq_index = 4, .irq_n = EXTI4_IRQn}, // pin 4
+    // EXTI5_9
+    {.gpio_idx = 0, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 5
+    {.gpio_idx = 1, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 6
+    {.gpio_idx = 2, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 7
+    {.gpio_idx = 3, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 8
+    {.gpio_idx = 4, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 9
+    // EXTI10_15
+    {.gpio_idx = 0, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 10
+    {.gpio_idx = 1, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 11
+    {.gpio_idx = 2, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 12
+    {.gpio_idx = 3, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 13
+    {.gpio_idx = 4, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 14
+    {.gpio_idx = 5, .irq_index = 6, .irq_n = EXTI15_10_IRQn}// pin 15
+};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F7/gpio_irq_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,94 @@
+/* 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_GPIO_IRQ_DEVICE_H
+#define MBED_GPIO_IRQ_DEVICE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// when LL is available, below include can be used
+// #include "stm32f0xx_f7_exti.h"
+// until then let's define locally the required functions
+__STATIC_INLINE void LL_EXTI_EnableRisingTrig_0_31(uint32_t ExtiLine)
+{
+  SET_BIT(EXTI->RTSR, ExtiLine);
+}
+__STATIC_INLINE void LL_EXTI_DisableRisingTrig_0_31(uint32_t ExtiLine)
+{
+  CLEAR_BIT(EXTI->RTSR, ExtiLine);
+}
+__STATIC_INLINE void LL_EXTI_EnableFallingTrig_0_31(uint32_t ExtiLine)
+{
+  SET_BIT(EXTI->FTSR, ExtiLine);
+}
+__STATIC_INLINE void LL_EXTI_DisableFallingTrig_0_31(uint32_t ExtiLine)
+{
+  CLEAR_BIT(EXTI->FTSR, ExtiLine);
+}
+__STATIC_INLINE void LL_EXTI_EnableIT_0_31(uint32_t ExtiLine)
+{
+  SET_BIT(EXTI->IMR, ExtiLine);
+}
+__STATIC_INLINE void LL_EXTI_DisableIT_0_31(uint32_t ExtiLine)
+{
+  CLEAR_BIT(EXTI->IMR, ExtiLine);
+}
+// Above lines shall be later defined in LL
+
+// Number of EXTI irq vectors (EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5_9, EXTI10_15)
+#define CHANNEL_NUM (7)
+
+#define EXTI_IRQ0_NUM_LINES 1
+#define EXTI_IRQ1_NUM_LINES 1
+#define EXTI_IRQ2_NUM_LINES 1
+#define EXTI_IRQ3_NUM_LINES 1
+#define EXTI_IRQ4_NUM_LINES 1
+#define EXTI_IRQ5_NUM_LINES 5
+#define EXTI_IRQ6_NUM_LINES 6
+
+// Max pins for one line (max with EXTI10_15)
+#define MAX_PIN_LINE (EXTI_IRQ6_NUM_LINES)
+
+/*  Structure to describe how the HW EXTI lines are defined in this HW */
+typedef struct exti_lines {
+    uint32_t gpio_idx;   // an index entry for each EXIT line
+    uint32_t irq_index;  // the IRQ index
+    IRQn_Type  irq_n;    // the corresponding EXTI IRQn
+} exti_lines_t;
+
+// Used to return the index for channels array.
+extern const exti_lines_t pin_lines_desc[];
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F7/pin_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,140 @@
+/* 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.
+ *******************************************************************************
+ */
+#ifndef MBED_PIN_DEVICE_H
+#define MBED_PIN_DEVICE_H
+
+#include "cmsis.h"
+
+// when LL is available, below include can be used
+// #include "stm32f7xx_ll_gpio.h"
+// until then let's define locally the required functions
+#define LL_GPIO_PIN_0                      GPIO_BSRR_BS_0 /*!< Select pin 0 */
+#define LL_GPIO_PIN_1                      GPIO_BSRR_BS_1 /*!< Select pin 1 */
+#define LL_GPIO_PIN_2                      GPIO_BSRR_BS_2 /*!< Select pin 2 */
+#define LL_GPIO_PIN_3                      GPIO_BSRR_BS_3 /*!< Select pin 3 */
+#define LL_GPIO_PIN_4                      GPIO_BSRR_BS_4 /*!< Select pin 4 */
+#define LL_GPIO_PIN_5                      GPIO_BSRR_BS_5 /*!< Select pin 5 */
+#define LL_GPIO_PIN_6                      GPIO_BSRR_BS_6 /*!< Select pin 6 */
+#define LL_GPIO_PIN_7                      GPIO_BSRR_BS_7 /*!< Select pin 7 */
+#define LL_GPIO_PIN_8                      GPIO_BSRR_BS_8 /*!< Select pin 8 */
+#define LL_GPIO_PIN_9                      GPIO_BSRR_BS_9 /*!< Select pin 9 */
+#define LL_GPIO_PIN_10                     GPIO_BSRR_BS_10 /*!< Select pin 10 */
+#define LL_GPIO_PIN_11                     GPIO_BSRR_BS_11 /*!< Select pin 11 */
+#define LL_GPIO_PIN_12                     GPIO_BSRR_BS_12 /*!< Select pin 12 */
+#define LL_GPIO_PIN_13                     GPIO_BSRR_BS_13 /*!< Select pin 13 */
+#define LL_GPIO_PIN_14                     GPIO_BSRR_BS_14 /*!< Select pin 14 */
+#define LL_GPIO_PIN_15                     GPIO_BSRR_BS_15 /*!< Select pin 15 */
+
+#define LL_GPIO_MODE_INPUT                 ((uint32_t)0x00000000U) /*!< Select input mode */
+#define LL_GPIO_MODE_OUTPUT                GPIO_MODER_MODER0_0  /*!< Select output mode */
+#define LL_GPIO_MODE_ALTERNATE             GPIO_MODER_MODER0_1  /*!< Select alternate function mode */
+#define LL_GPIO_MODE_ANALOG                GPIO_MODER_MODER0    /*!< Select analog mode */
+
+#define LL_GPIO_OUTPUT_PUSHPULL            ((uint32_t)0x00000000U) /*!< Select push-pull as output type */
+#define LL_GPIO_OUTPUT_OPENDRAIN           GPIO_OTYPER_OT_0 /*!< Select open-drain as output type */
+
+#define LL_GPIO_PULL_NO                    ((uint32_t)0x00000000U) /*!< Select I/O no pull */
+#define LL_GPIO_PULL_UP                    GPIO_PUPDR_PUPDR0_0 /*!< Select I/O pull up */
+#define LL_GPIO_PULL_DOWN                  GPIO_PUPDR_PUPDR0_1 /*!< Select I/O pull down */
+
+#define LL_GPIO_SPEED_FREQ_LOW             ((uint32_t)0x00000000U) /*!< Select I/O low output speed    */
+#define LL_GPIO_SPEED_FREQ_MEDIUM          GPIO_OSPEEDER_OSPEEDR0_0 /*!< Select I/O medium output speed */
+#define LL_GPIO_SPEED_FREQ_HIGH            GPIO_OSPEEDER_OSPEEDR0_1 /*!< Select I/O fast output speed   */
+#define LL_GPIO_SPEED_FREQ_VERY_HIGH       GPIO_OSPEEDER_OSPEEDR0   /*!< Select I/O high output speed   */
+
+
+__STATIC_INLINE void LL_GPIO_SetAFPin_0_7(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Alternate)
+{
+  MODIFY_REG(GPIOx->AFR[0], (0xFU << (POSITION_VAL(Pin) * 4U)),
+             (Alternate << (POSITION_VAL(Pin) * 4U)));
+}
+
+__STATIC_INLINE void LL_GPIO_SetAFPin_8_15(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Alternate)
+{
+  MODIFY_REG(GPIOx->AFR[1], (0xFU << (POSITION_VAL(Pin >> 8U) * 4U)),
+             (Alternate << (POSITION_VAL(Pin >> 8U) * 4U)));
+}
+__STATIC_INLINE void LL_GPIO_SetPinMode(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Mode)
+{
+  MODIFY_REG(GPIOx->MODER, (GPIO_MODER_MODER0 << (POSITION_VAL(Pin) * 2U)), (Mode << (POSITION_VAL(Pin) * 2U)));
+}
+__STATIC_INLINE uint32_t LL_GPIO_GetPinMode(GPIO_TypeDef *GPIOx, uint32_t Pin)
+{
+  return (uint32_t)(READ_BIT(GPIOx->MODER, ((Pin * Pin) * GPIO_MODER_MODER0)) / (Pin * Pin));
+}
+__STATIC_INLINE void LL_GPIO_SetPinPull(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Pull)
+{
+  MODIFY_REG(GPIOx->PUPDR, (GPIO_PUPDR_PUPDR0 << (POSITION_VAL(Pin) * 2U)), (Pull << (POSITION_VAL(Pin) * 2U)));
+}
+__STATIC_INLINE void LL_GPIO_SetPinOutputType(GPIO_TypeDef *GPIOx, uint32_t PinMask, uint32_t OutputType)
+{
+  MODIFY_REG(GPIOx->OTYPER, PinMask, (PinMask * OutputType));
+}
+__STATIC_INLINE void LL_GPIO_SetPinSpeed(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t  Speed)
+{
+  MODIFY_REG(GPIOx->OSPEEDR, (GPIO_OSPEEDER_OSPEEDR0 << (POSITION_VAL(Pin) * 2U)),
+             (Speed << (POSITION_VAL(Pin) * 2U)));
+}
+// Above lines shall be defined in LL when available
+
+extern const uint32_t ll_pin_defines[16];
+
+/* Family specific implementations */
+static inline void stm_pin_DisconnectDebug(PinName pin)
+{
+    /* empty for now */
+}
+
+static inline void stm_pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t pull_config)
+{
+    switch (pull_config) {
+        case GPIO_PULLUP:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_UP);
+            break;
+        case GPIO_PULLDOWN:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_DOWN);
+            break;
+        default:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_NO);
+            break;
+    }
+}
+
+static inline void stm_pin_SetAFPin( GPIO_TypeDef *gpio, PinName pin, uint32_t afnum)
+{
+    uint32_t ll_pin  = ll_pin_defines[STM_PIN(pin)];
+
+    if (STM_PIN(pin) > 7)
+        LL_GPIO_SetAFPin_8_15(gpio, ll_pin, afnum);
+    else
+        LL_GPIO_SetAFPin_0_7(gpio, ll_pin, afnum);
+}
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32F7/pinmap.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,196 +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 "mbed_assert.h"
-#include "pinmap.h"
-#include "PortNames.h"
-#include "mbed_error.h"
-
-// GPIO mode look-up table
-static const uint32_t gpio_mode[13] = {
-    0x00000000, //  0 = GPIO_MODE_INPUT
-    0x00000001, //  1 = GPIO_MODE_OUTPUT_PP
-    0x00000011, //  2 = GPIO_MODE_OUTPUT_OD
-    0x00000002, //  3 = GPIO_MODE_AF_PP
-    0x00000012, //  4 = GPIO_MODE_AF_OD
-    0x00000003, //  5 = GPIO_MODE_ANALOG
-    0x10110000, //  6 = GPIO_MODE_IT_RISING
-    0x10210000, //  7 = GPIO_MODE_IT_FALLING
-    0x10310000, //  8 = GPIO_MODE_IT_RISING_FALLING
-    0x10120000, //  9 = GPIO_MODE_EVT_RISING
-    0x10220000, // 10 = GPIO_MODE_EVT_FALLING
-    0x10320000, // 11 = GPIO_MODE_EVT_RISING_FALLING
-    0x10000000  // 12 = Reset GPIO_MODE_IT_EVT
-};
-
-// Enable GPIO clock and return GPIO base address
-uint32_t Set_GPIO_Clock(uint32_t port_idx)
-{
-    uint32_t gpio_add = 0;
-    switch (port_idx) {
-        case PortA:
-            gpio_add = GPIOA_BASE;
-            __GPIOA_CLK_ENABLE();
-            break;
-        case PortB:
-            gpio_add = GPIOB_BASE;
-            __GPIOB_CLK_ENABLE();
-            break;
-        case PortC:
-            gpio_add = GPIOC_BASE;
-            __GPIOC_CLK_ENABLE();
-            break;
-#if defined GPIOD_BASE
-        case PortD:
-            gpio_add = GPIOD_BASE;
-            __GPIOD_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOE_BASE
-        case PortE:
-            gpio_add = GPIOE_BASE;
-            __GPIOE_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOF_BASE
-        case PortF:
-            gpio_add = GPIOF_BASE;
-            __GPIOF_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOG_BASE
-        case PortG:
-            gpio_add = GPIOG_BASE;
-            __GPIOG_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOH_BASE
-        case PortH:
-            gpio_add = GPIOH_BASE;
-            __GPIOH_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOI_BASE
-        case PortI:
-            gpio_add = GPIOI_BASE;
-            __GPIOI_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOJ_BASE
-        case PortJ:
-            gpio_add = GPIOJ_BASE;
-            __GPIOJ_CLK_ENABLE();
-            break;
-#endif
-#if defined GPIOK_BASE
-        case PortK:
-            gpio_add = GPIOK_BASE;
-            __GPIOK_CLK_ENABLE();
-            break;
-#endif
-        default:
-            error("Pinmap error: wrong port number.");
-            break;
-    }
-    return gpio_add;
-}
-
-/**
- * Configure pin (mode, speed, output type and pull-up/pull-down)
- */
-void pin_function(PinName pin, int data)
-{
-    MBED_ASSERT(pin != (PinName)NC);
-    // Get the pin informations
-    uint32_t mode  = STM_PIN_MODE(data);
-    uint32_t pupd  = STM_PIN_PUPD(data);
-    uint32_t afnum = STM_PIN_AFNUM(data);
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Configure GPIO
-    GPIO_InitTypeDef GPIO_InitStructure;
-    GPIO_InitStructure.Pin       = (uint32_t)(1 << pin_index);
-    GPIO_InitStructure.Mode      = gpio_mode[mode];
-    GPIO_InitStructure.Pull      = pupd;
-    GPIO_InitStructure.Speed     = GPIO_SPEED_HIGH;
-    GPIO_InitStructure.Alternate = afnum;
-    HAL_GPIO_Init(gpio, &GPIO_InitStructure);
-
-    // [TODO] Disconnect JTAG-DP + SW-DP signals.
-    // Warning: Need to reconnect under reset
-    //if ((pin == PA_13) || (pin == PA_14)) {
-    //
-    //}
-    //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) {
-    //
-    //}
-}
-
-/**
- * Configure pin pull-up/pull-down
- */
-void pin_mode(PinName pin, PinMode mode)
-{
-    MBED_ASSERT(pin != (PinName)NC);
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Configure pull-up/pull-down resistors
-    uint32_t pupd = (uint32_t)mode;
-    if (pupd > 2)
-        pupd = 0; // Open-drain = No pull-up/No pull-down
-    gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2)));
-    gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
-}
-
-/*  Internal function for setting the gpiomode/function
- *  without changing Pull mode
- */
-void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
-
-    /* Read current pull state from HW to avoid over-write*/
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
-    uint32_t temp = gpio->PUPDR;
-    uint32_t pull = (temp  >> (pin_index * 2U)) & GPIO_PUPDR_PUPDR0;
-
-    /* Then re-use global function for updating the mode part*/
-    pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
-}
--- a/targets/TARGET_STM/TARGET_STM32F7/port_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,103 +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 "port_api.h"
-#include "pinmap.h"
-#include "gpio_api.h"
-#include "mbed_error.h"
-
-#if DEVICE_PORTIN || DEVICE_PORTOUT
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-
-// high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
-// low nibble  = pin number
-PinName port_pin(PortName port, int pin_n)
-{
-    return (PinName)(pin_n + (port << 4));
-}
-
-void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
-{
-    uint32_t port_index = (uint32_t)port;
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Fill PORT object structure for future use
-    obj->port      = port;
-    obj->mask      = mask;
-    obj->direction = dir;
-    obj->reg_in    = &gpio->IDR;
-    obj->reg_out   = &gpio->ODR;
-
-    port_dir(obj, dir);
-}
-
-void port_dir(port_t *obj, PinDirection dir)
-{
-    uint32_t i;
-    obj->direction = dir;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            if (dir == PIN_OUTPUT) {
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
-            } else { // PIN_INPUT
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
-            }
-        }
-    }
-}
-
-void port_mode(port_t *obj, PinMode mode)
-{
-    uint32_t i;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            pin_mode(port_pin(obj->port, i), mode);
-        }
-    }
-}
-
-void port_write(port_t *obj, int value)
-{
-    *obj->reg_out = (*obj->reg_out & ~obj->mask) | (value & obj->mask);
-}
-
-int port_read(port_t *obj)
-{
-    if (obj->direction == PIN_OUTPUT) {
-        return (*obj->reg_out & obj->mask);
-    } else { // PIN_INPUT
-        return (*obj->reg_in & obj->mask);
-    }
-}
-
-#endif
--- a/targets/TARGET_STM/TARGET_STM32F7/pwmout_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,57 @@
+/* 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;
+
+extern const pwm_apb_map_t pwm_apb_map_table[];
+
+#endif // DEVICE_PWMOUT
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32L0/analogin_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L0/analogin_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -30,7 +30,7 @@
 
 #if DEVICE_ANALOGIN
 
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "cmsis.h"
 #include "pinmap.h"
 #include "mbed_error.h"
--- a/targets/TARGET_STM/TARGET_STM32L0/gpio_irq_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,269 +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 <stddef.h>
-#include "cmsis.h"
-#include "gpio_irq_api.h"
-#include "pinmap.h"
-#include "mbed_error.h"
-
-#define EDGE_NONE (0)
-#define EDGE_RISE (1)
-#define EDGE_FALL (2)
-#define EDGE_BOTH (3)
-
-// Number of EXTI irq vectors (EXTI0_1, EXTI2_3, EXTI4_15)
-#define CHANNEL_NUM (3)
-
-// Max pins for one line (max with EXTI4_15)
-#define MAX_PIN_LINE (12)
-
-typedef struct gpio_channel {
-    uint32_t pin_mask;                   // bitmask representing which pins are configured for receiving interrupts
-    uint32_t channel_ids[MAX_PIN_LINE];  // mbed "gpio_irq_t gpio_irq" field of instance
-    uint32_t channel_gpio[MAX_PIN_LINE]; // base address of gpio port group
-    uint32_t channel_pin[MAX_PIN_LINE];  // pin number in port group
-} gpio_channel_t;
-
-static gpio_channel_t channels[CHANNEL_NUM] = {
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0}
-};
-
-// Used to return the index for channels array.
-static uint32_t pin_base_nr[16] = {
-    // EXTI0_1
-    0, // pin 0
-    1, // pin 1
-    // EXTI2_3
-    0, // pin 2
-    1, // pin 3
-    // EXTI4_15
-    0, // pin 4
-    1, // pin 5
-    2, // pin 6
-    3, // pin 7
-    4, // pin 8
-    5, // pin 9
-    6, // pin 10
-    7, // pin 11
-    8, // pin 12
-    9, // pin 13
-    10, // pin 14
-    11  // pin 15
-};
-
-static gpio_irq_handler irq_handler;
-
-static void handle_interrupt_in(uint32_t irq_index, uint32_t max_num_pin_line)
-{
-    gpio_channel_t *gpio_channel = &channels[irq_index];
-    uint32_t gpio_idx;
-
-    for (gpio_idx = 0; gpio_idx < max_num_pin_line; gpio_idx++) {
-        uint32_t current_mask = (1 << gpio_idx);
-
-        if (gpio_channel->pin_mask & current_mask) {
-            // Retrieve the gpio and pin that generate the irq
-            GPIO_TypeDef *gpio = (GPIO_TypeDef *)(gpio_channel->channel_gpio[gpio_idx]);
-            uint32_t pin = (uint32_t)(1 << (gpio_channel->channel_pin[gpio_idx]));
-
-            // Clear interrupt flag
-            if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) {
-                __HAL_GPIO_EXTI_CLEAR_FLAG(pin);
-
-                if (gpio_channel->channel_ids[gpio_idx] == 0) continue;
-
-                // Check which edge has generated the irq
-                if ((gpio->IDR & pin) == 0) {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_FALL);
-                } else  {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_RISE);
-                }
-            }
-        }
-    }
-}
-
-// EXTI lines 0 to 1
-static void gpio_irq0(void)
-{
-    handle_interrupt_in(0, 2);
-}
-
-// EXTI lines 2 to 3
-static void gpio_irq1(void)
-{
-    handle_interrupt_in(1, 2);
-}
-
-// EXTI lines 4 to 15
-static void gpio_irq2(void)
-{
-    handle_interrupt_in(2, 12);
-}
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
-
-int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
-{
-    IRQn_Type irq_n = (IRQn_Type)0;
-    uint32_t vector = 0;
-    uint32_t irq_index;
-    gpio_channel_t *gpio_channel;
-    uint32_t gpio_idx;
-
-    if (pin == NC) return -1;
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Select irq number and interrupt routine
-    if ((pin_index == 0) || (pin_index == 1)) {
-        irq_n = EXTI0_1_IRQn;
-        vector = (uint32_t)&gpio_irq0;
-        irq_index = 0;
-    } else if ((pin_index == 2) || (pin_index == 3)) {
-        irq_n = EXTI2_3_IRQn;
-        vector = (uint32_t)&gpio_irq1;
-        irq_index = 1;
-    } else if ((pin_index > 3) && (pin_index < 16)) {
-        irq_n = EXTI4_15_IRQn;
-        vector = (uint32_t)&gpio_irq2;
-        irq_index = 2;
-    } else {
-        error("InterruptIn error: pin not supported.\n");
-        return -1;
-    }
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-
-    // Configure GPIO
-    pin_function(pin, STM_PIN_DATA(STM_MODE_IT_FALLING, GPIO_NOPULL, 0));
-
-    // Enable EXTI interrupt
-    NVIC_SetVector(irq_n, vector);
-    NVIC_EnableIRQ(irq_n);
-
-    // Save informations for future use
-    obj->irq_n = irq_n;
-    obj->irq_index = irq_index;
-    obj->event = EDGE_NONE;
-    obj->pin = pin;
-
-    gpio_channel = &channels[irq_index];
-    gpio_idx = pin_base_nr[pin_index];
-    gpio_channel->pin_mask |= (1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = id;
-    gpio_channel->channel_gpio[gpio_idx] = gpio_add;
-    gpio_channel->channel_pin[gpio_idx] = pin_index;
-
-    irq_handler = handler;
-
-    return 0;
-}
-
-void gpio_irq_free(gpio_irq_t *obj)
-{
-    gpio_channel_t *gpio_channel = &channels[obj->irq_index];
-    uint32_t pin_index  = STM_PIN(obj->pin);
-    uint32_t gpio_addr = GPIOA_BASE + (GPIOB_BASE-GPIOA_BASE) * STM_PORT(obj->pin);
-    uint32_t gpio_idx = pin_base_nr[pin_index];
-    
-    HAL_GPIO_DeInit((GPIO_TypeDef *)gpio_addr, (1<<pin_index));
-    gpio_channel->pin_mask &= ~(1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = 0;
-    gpio_channel->channel_gpio[gpio_idx] = 0;
-    gpio_channel->channel_pin[gpio_idx] = 0;
-
-    // Disable EXTI line, but don't change pull-up config
-    pin_function_gpiomode(obj->pin, STM_MODE_INPUT);
-    obj->event = EDGE_NONE;
-}
-
-void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
-{
-    uint32_t mode = STM_MODE_IT_EVT_RESET;
-
-    if (enable) {
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or RISE
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or FALL
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            }
-        }
-    } else { // Disable
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            } else { // NONE or RISE
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            } else { // NONE or FALL
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-    }
-
-    pin_function_gpiomode(obj->pin, mode);
-}
-
-void gpio_irq_enable(gpio_irq_t *obj)
-{
-    NVIC_EnableIRQ(obj->irq_n);
-}
-
-void gpio_irq_disable(gpio_irq_t *obj)
-{
-    NVIC_DisableIRQ(obj->irq_n);
-    obj->event = EDGE_NONE;
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L0/gpio_irq_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,55 @@
+/* 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 "gpio_irq_device.h"
+
+// Used to return the index for channels array.
+const exti_lines_t pin_lines_desc[16] = {
+    // EXTI0_1
+    {.gpio_idx = 0, .irq_index = 0, .irq_n = EXTI0_1_IRQn}, // pin 0
+    {.gpio_idx = 1, .irq_index = 0, .irq_n = EXTI0_1_IRQn}, // pin 1
+    // EXTI2_3
+    {.gpio_idx = 0, .irq_index = 1, .irq_n = EXTI2_3_IRQn}, // pin 2
+    {.gpio_idx = 1, .irq_index = 1, .irq_n = EXTI2_3_IRQn}, // pin 3
+    // EXTI4_15
+    {.gpio_idx = 0, .irq_index = 2, .irq_n = EXTI4_15_IRQn}, // pin 4
+    {.gpio_idx = 1, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 5
+    {.gpio_idx = 2, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 6
+    {.gpio_idx = 3, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 7
+    {.gpio_idx = 4, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 8
+    {.gpio_idx = 5, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 9
+    {.gpio_idx = 6, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 10
+    {.gpio_idx = 7, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 11
+    {.gpio_idx = 8, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 12
+    {.gpio_idx = 9, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 13
+    {.gpio_idx = 10, .irq_index = 2, .irq_n = EXTI4_15_IRQn},// pin 14
+    {.gpio_idx = 11, .irq_index = 2, .irq_n = EXTI4_15_IRQn}// pin 15
+};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L0/gpio_irq_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,63 @@
+/* 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_GPIO_IRQ_DEVICE_H
+#define MBED_GPIO_IRQ_DEVICE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "stm32l0xx_ll_exti.h"
+
+// Number of EXTI irq vectors (EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5_9, EXTI10_15)
+#define CHANNEL_NUM (3)
+
+#define EXTI_IRQ0_NUM_LINES 2
+#define EXTI_IRQ1_NUM_LINES 2
+#define EXTI_IRQ2_NUM_LINES 12
+
+// Max pins for one line (max with EXTI10_15)
+#define MAX_PIN_LINE (EXTI_IRQ2_NUM_LINES)
+
+/*  Structure to describe how the HW EXTI lines are defined in this HW */
+typedef struct exti_lines {
+    uint32_t gpio_idx;   // an index entry for each EXIT line
+    uint32_t irq_index;  // the IRQ index
+    IRQn_Type  irq_n;    // the corresponding EXTI IRQn
+} exti_lines_t;
+
+// Used to return the index for channels array.
+extern const exti_lines_t pin_lines_desc[];
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L0/pin_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,69 @@
+/* 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.
+ *******************************************************************************
+ */
+#ifndef MBED_PIN_DEVICE_H
+#define MBED_PIN_DEVICE_H
+
+#include "cmsis.h"
+#include "stm32l0xx_ll_gpio.h"
+
+extern const uint32_t ll_pin_defines[16];
+
+/* Family specific implementations */
+static inline void stm_pin_DisconnectDebug(PinName pin)
+{
+    /* empty for now */
+}
+
+static inline void stm_pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t pull_config)
+{
+    switch (pull_config) {
+        case GPIO_PULLUP:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_UP);
+            break;
+        case GPIO_PULLDOWN:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_DOWN);
+            break;
+        default:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_NO);
+            break;
+    }
+}
+
+static inline void stm_pin_SetAFPin( GPIO_TypeDef *gpio, PinName pin, uint32_t afnum)
+{
+    uint32_t ll_pin  = ll_pin_defines[STM_PIN(pin)];
+
+    if (STM_PIN(pin) > 7)
+        LL_GPIO_SetAFPin_8_15(gpio, ll_pin, afnum);
+    else
+        LL_GPIO_SetAFPin_0_7(gpio, ll_pin, afnum);
+}
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32L0/pinmap.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,165 +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 "mbed_assert.h"
-#include "pinmap.h"
-#include "PortNames.h"
-#include "mbed_error.h"
-
-// GPIO mode look-up table
-// Warning: order must be the same as the one defined in PinNames.h !!!
-static const uint32_t gpio_mode[13] = {
-    0x00000000, //  0 = GPIO_MODE_INPUT
-    0x00000001, //  1 = GPIO_MODE_OUTPUT_PP
-    0x00000011, //  2 = GPIO_MODE_OUTPUT_OD
-    0x00000002, //  3 = GPIO_MODE_AF_PP
-    0x00000012, //  4 = GPIO_MODE_AF_OD
-    0x00000003, //  5 = GPIO_MODE_ANALOG
-    0x10110000, //  6 = GPIO_MODE_IT_RISING
-    0x10210000, //  7 = GPIO_MODE_IT_FALLING
-    0x10310000, //  8 = GPIO_MODE_IT_RISING_FALLING
-    0x10120000, //  9 = GPIO_MODE_EVT_RISING
-    0x10220000, // 10 = GPIO_MODE_EVT_FALLING
-    0x10320000, // 11 = GPIO_MODE_EVT_RISING_FALLING
-    0x10000000  // 12 = Reset IT and EVT (not in STM32Cube HAL)
-};
-
-// Enable GPIO clock and return GPIO base address
-uint32_t Set_GPIO_Clock(uint32_t port_idx)
-{
-    uint32_t gpio_add = 0;
-    switch (port_idx) {
-        case PortA:
-            gpio_add = GPIOA_BASE;
-            __GPIOA_CLK_ENABLE();
-            break;
-        case PortB:
-            gpio_add = GPIOB_BASE;
-            __GPIOB_CLK_ENABLE();
-            break;
-#if defined(GPIOC_BASE)
-        case PortC:
-            gpio_add = GPIOC_BASE;
-            __GPIOC_CLK_ENABLE();
-            break;
-#endif
-#if defined(GPIOD_BASE)
-        case PortD:
-            gpio_add = GPIOD_BASE;
-            __GPIOD_CLK_ENABLE();
-            break;
-#endif
-#if defined(GPIOH_BASE)
-        case PortH:
-            gpio_add = GPIOH_BASE;
-            __GPIOH_CLK_ENABLE();
-            break;
-#endif
-        default:
-            error("Pinmap error: wrong port number.");
-            break;
-    }
-    return gpio_add;
-}
-
-/**
- * Configure pin (mode, speed, output type and pull-up/pull-down)
- */
-void pin_function(PinName pin, int data)
-{
-    MBED_ASSERT(pin != (PinName)NC);
-    // Get the pin informations
-    uint32_t mode  = STM_PIN_MODE(data);
-    uint32_t pupd  = STM_PIN_PUPD(data);
-    uint32_t afnum = STM_PIN_AFNUM(data);
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Configure GPIO
-    GPIO_InitTypeDef GPIO_InitStructure;
-    GPIO_InitStructure.Pin       = (uint32_t)(1 << pin_index);
-    GPIO_InitStructure.Mode      = gpio_mode[mode];
-    GPIO_InitStructure.Pull      = pupd;
-    GPIO_InitStructure.Speed     = GPIO_SPEED_HIGH;
-    GPIO_InitStructure.Alternate = afnum;
-    HAL_GPIO_Init(gpio, &GPIO_InitStructure);
-
-    // [TODO] Disconnect JTAG-DP + SW-DP signals.
-    // Warning: Need to reconnect under reset
-    //if ((pin == PA_13) || (pin == PA_14)) {
-    //
-    //}
-    //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) {
-    //
-    //}
-}
-
-/**
- * Configure pin pull-up/pull-down
- */
-void pin_mode(PinName pin, PinMode mode)
-{
-    MBED_ASSERT(pin != (PinName)NC);
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Configure pull-up/pull-down resistors
-    uint32_t pupd = (uint32_t)mode;
-    if (pupd > 2)
-    {
-        pupd = 0; // Open-drain = No pull-up/No pull-down
-    }
-    gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPD0 << (pin_index * 2)));
-    gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
-}
-
-/*  Internal function for setting the gpiomode/function
- *  without changing Pull mode
- */
-void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
-
-    /* Read current pull state from HW to avoid over-write*/
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
-    uint32_t temp = gpio->PUPDR;
-    uint32_t pull = (temp  >> (pin_index * 2U)) & GPIO_PUPDR_PUPD0;
-
-    /* Then re-use global function for updating the mode part*/
-    pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
-}
--- a/targets/TARGET_STM/TARGET_STM32L0/port_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,103 +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 "port_api.h"
-#include "pinmap.h"
-#include "gpio_api.h"
-#include "mbed_error.h"
-
-#if DEVICE_PORTIN || DEVICE_PORTOUT
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-
-// high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
-// low nibble  = pin number
-PinName port_pin(PortName port, int pin_n)
-{
-    return (PinName)(pin_n + (port << 4));
-}
-
-void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
-{
-    uint32_t port_index = (uint32_t)port;
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Fill PORT object structure for future use
-    obj->port      = port;
-    obj->mask      = mask;
-    obj->direction = dir;
-    obj->reg_in    = &gpio->IDR;
-    obj->reg_out   = &gpio->ODR;
-
-    port_dir(obj, dir);
-}
-
-void port_dir(port_t *obj, PinDirection dir)
-{
-    uint32_t i;
-    obj->direction = dir;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            if (dir == PIN_OUTPUT) {
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
-            } else { // PIN_INPUT
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
-            }
-        }
-    }
-}
-
-void port_mode(port_t *obj, PinMode mode)
-{
-    uint32_t i;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            pin_mode(port_pin(obj->port, i), mode);
-        }
-    }
-}
-
-void port_write(port_t *obj, int value)
-{
-    *obj->reg_out = (*obj->reg_out & ~obj->mask) | (value & obj->mask);
-}
-
-int port_read(port_t *obj)
-{
-    if (obj->direction == PIN_OUTPUT) {
-        return (*obj->reg_out & obj->mask);
-    } else { // PIN_INPUT
-        return (*obj->reg_in & obj->mask);
-    }
-}
-
-#endif
--- a/targets/TARGET_STM/TARGET_STM32L0/pwmout_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,59 @@
+/* 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;
+
+extern const pwm_apb_map_t pwm_apb_map_table[];
+
+#endif // DEVICE_PWMOUT
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/PeripheralPins.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/PeripheralPins.c	Tue Mar 14 16:40:56 2017 +0000
@@ -92,40 +92,40 @@
 
 // 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}
 };
 
--- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/PeripheralPins.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/PeripheralPins.c	Tue Mar 14 16:40:56 2017 +0000
@@ -92,40 +92,40 @@
 
 // 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}
 };
 
--- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/PeripheralPins.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/PeripheralPins.c	Tue Mar 14 16:40:56 2017 +0000
@@ -92,40 +92,40 @@
 
 // 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}
 };
 
--- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c	Tue Mar 14 16:40:56 2017 +0000
@@ -79,13 +79,13 @@
 // 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}
 };
 
--- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/objects.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/objects.h	Tue Mar 14 16:40:56 2017 +0000
@@ -65,6 +65,7 @@
     PinName pin;
 };
 
+#define GPIO_IP_WITHOUT_BRR
 #include "common_objects.h"
 
 #ifdef __cplusplus
--- a/targets/TARGET_STM/TARGET_STM32L1/analogin_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L1/analogin_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -30,7 +30,7 @@
 
 #if DEVICE_ANALOGIN
 
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "cmsis.h"
 #include "pinmap.h"
 #include "mbed_error.h"
--- a/targets/TARGET_STM/TARGET_STM32L1/common_objects.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L1/common_objects.h	Tue Mar 14 16:40:56 2017 +0000
@@ -45,6 +45,8 @@
     uint32_t prescaler;
     uint32_t period;
     uint32_t pulse;
+    uint8_t channel;
+    uint8_t inverted; 
 };
 
 struct serial_s {
--- a/targets/TARGET_STM/TARGET_STM32L1/gpio_irq_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,334 +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 <stddef.h>
-#include "cmsis.h"
-#include "gpio_irq_api.h"
-#include "pinmap.h"
-#include "mbed_error.h"
-
-#define EDGE_NONE (0)
-#define EDGE_RISE (1)
-#define EDGE_FALL (2)
-#define EDGE_BOTH (3)
-
-// Number of EXTI irq vectors (EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5_9, EXTI10_15)
-#define CHANNEL_NUM (7)
-
-// Max pins for one line (max with EXTI10_15)
-#define MAX_PIN_LINE (6)
-
-typedef struct gpio_channel {
-    uint32_t pin_mask;                   // bitmask representing which pins are configured for receiving interrupts
-    uint32_t channel_ids[MAX_PIN_LINE];  // mbed "gpio_irq_t gpio_irq" field of instance
-    uint32_t channel_gpio[MAX_PIN_LINE]; // base address of gpio port group
-    uint32_t channel_pin[MAX_PIN_LINE];  // pin number in port group
-} gpio_channel_t;
-
-static gpio_channel_t channels[CHANNEL_NUM] = {
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0}
-};
-
-// Used to return the index for channels array.
-static uint32_t pin_base_nr[16] = {
-    // EXTI0
-    0, // pin 0
-    // EXTI1
-    0, // pin 1
-    // EXTI2
-    0, // pin 2
-    // EXTI3
-    0, // pin 3
-    // EXTI4
-    0, // pin 4
-    // EXTI5_9
-    0, // pin 5
-    1, // pin 6
-    2, // pin 7
-    3, // pin 8
-    4, // pin 9
-    // EXTI10_15
-    0, // pin 10
-    1, // pin 11
-    2, // pin 12
-    3, // pin 13
-    4, // pin 14
-    5  // pin 15
-};
-
-static gpio_irq_handler irq_handler;
-
-static void handle_interrupt_in(uint32_t irq_index, uint32_t max_num_pin_line)
-{
-    gpio_channel_t *gpio_channel = &channels[irq_index];
-    uint32_t gpio_idx;
-
-    for (gpio_idx = 0; gpio_idx < max_num_pin_line; gpio_idx++) {
-        uint32_t current_mask = (1 << gpio_idx);
-
-        if (gpio_channel->pin_mask & current_mask) {
-            // Retrieve the gpio and pin that generate the irq
-            GPIO_TypeDef *gpio = (GPIO_TypeDef *)(gpio_channel->channel_gpio[gpio_idx]);
-            uint32_t pin = (uint32_t)(1 << (gpio_channel->channel_pin[gpio_idx]));
-
-            // Clear interrupt flag
-            if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) {
-                __HAL_GPIO_EXTI_CLEAR_FLAG(pin);
-
-                if (gpio_channel->channel_ids[gpio_idx] == 0) continue;
-
-                // Check which edge has generated the irq
-                if ((gpio->IDR & pin) == 0) {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_FALL);
-                } else  {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_RISE);
-                }
-            }
-        }
-    }
-}
-
-// EXTI line 0
-static void gpio_irq0(void)
-{
-    handle_interrupt_in(0, 1);
-}
-
-// EXTI line 1
-static void gpio_irq1(void)
-{
-    handle_interrupt_in(1, 1);
-}
-
-// EXTI line 2
-static void gpio_irq2(void)
-{
-    handle_interrupt_in(2, 1);
-}
-
-// EXTI line 3
-static void gpio_irq3(void)
-{
-    handle_interrupt_in(3, 1);
-}
-
-// EXTI line 4
-static void gpio_irq4(void)
-{
-    handle_interrupt_in(4, 1);
-}
-
-// EXTI lines 5 to 9
-static void gpio_irq5(void)
-{
-    handle_interrupt_in(5, 5);
-}
-
-// EXTI lines 10 to 15
-static void gpio_irq6(void)
-{
-    handle_interrupt_in(6, 6);
-}
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
-
-int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
-{
-    IRQn_Type irq_n = (IRQn_Type)0;
-    uint32_t vector = 0;
-    uint32_t irq_index;
-    gpio_channel_t *gpio_channel;
-    uint32_t gpio_idx;
-
-    if (pin == NC) return -1;
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Select irq number and interrupt routine
-    switch (pin_index) {
-        case 0:
-            irq_n = EXTI0_IRQn;
-            vector = (uint32_t)&gpio_irq0;
-            irq_index = 0;
-            break;
-        case 1:
-            irq_n = EXTI1_IRQn;
-            vector = (uint32_t)&gpio_irq1;
-            irq_index = 1;
-            break;
-        case 2:
-            irq_n = EXTI2_IRQn;
-            vector = (uint32_t)&gpio_irq2;
-            irq_index = 2;
-            break;
-        case 3:
-            irq_n = EXTI3_IRQn;
-            vector = (uint32_t)&gpio_irq3;
-            irq_index = 3;
-            break;
-        case 4:
-            irq_n = EXTI4_IRQn;
-            vector = (uint32_t)&gpio_irq4;
-            irq_index = 4;
-            break;
-        case 5:
-        case 6:
-        case 7:
-        case 8:
-        case 9:
-            irq_n = EXTI9_5_IRQn;
-            vector = (uint32_t)&gpio_irq5;
-            irq_index = 5;
-            break;
-        case 10:
-        case 11:
-        case 12:
-        case 13:
-        case 14:
-        case 15:
-            irq_n = EXTI15_10_IRQn;
-            vector = (uint32_t)&gpio_irq6;
-            irq_index = 6;
-            break;
-        default:
-            error("InterruptIn error: pin not supported.\n");
-            return -1;
-    }
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-
-    // Configure GPIO
-    pin_function(pin, STM_PIN_DATA(STM_MODE_IT_FALLING, GPIO_NOPULL, 0));
-
-    // Enable EXTI interrupt
-    NVIC_SetVector(irq_n, vector);
-    NVIC_EnableIRQ(irq_n);
-
-    // Save informations for future use
-    obj->irq_n = irq_n;
-    obj->irq_index = irq_index;
-    obj->event = EDGE_NONE;
-    obj->pin = pin;
-
-    gpio_channel = &channels[irq_index];
-    gpio_idx = pin_base_nr[pin_index];
-    gpio_channel->pin_mask |= (1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = id;
-    gpio_channel->channel_gpio[gpio_idx] = gpio_add;
-    gpio_channel->channel_pin[gpio_idx] = pin_index;
-
-    irq_handler = handler;
-
-    return 0;
-}
-
-void gpio_irq_free(gpio_irq_t *obj)
-{
-    gpio_channel_t *gpio_channel = &channels[obj->irq_index];
-    uint32_t pin_index  = STM_PIN(obj->pin);
-    uint32_t gpio_addr = GPIOA_BASE + (GPIOB_BASE-GPIOA_BASE) * STM_PORT(obj->pin);
-    uint32_t gpio_idx = pin_base_nr[pin_index];
-    
-    HAL_GPIO_DeInit((GPIO_TypeDef *)gpio_addr, (1<<pin_index));
-    gpio_channel->pin_mask &= ~(1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = 0;
-    gpio_channel->channel_gpio[gpio_idx] = 0;
-    gpio_channel->channel_pin[gpio_idx] = 0;
-
-    // Disable EXTI line, but don't change pull-up config
-    pin_function_gpiomode(obj->pin, STM_MODE_INPUT);
-    obj->event = EDGE_NONE;
-}
-
-void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
-{
-    uint32_t mode = STM_MODE_IT_EVT_RESET;
-
-    if (enable) {
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or RISE
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or FALL
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            }
-        }
-    } else { // Disable
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            } else { // NONE or RISE
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            } else { // NONE or FALL
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-    }
-
-    pin_function_gpiomode(obj->pin, mode);
-}
-
-void gpio_irq_enable(gpio_irq_t *obj)
-{
-    NVIC_EnableIRQ(obj->irq_n);
-}
-
-void gpio_irq_disable(gpio_irq_t *obj)
-{
-    NVIC_DisableIRQ(obj->irq_n);
-    obj->event = EDGE_NONE;
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L1/gpio_irq_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,59 @@
+/* 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 "gpio_irq_device.h"
+
+// Used to return the index for channels array.
+const exti_lines_t pin_lines_desc[16] = {
+    // EXTI0
+    {.gpio_idx = 0, .irq_index = 0, .irq_n = EXTI0_IRQn}, // pin 0
+    // EXTI1
+    {.gpio_idx = 0, .irq_index = 1, .irq_n = EXTI1_IRQn}, // pin 1
+    // EXTI2
+    {.gpio_idx = 0, .irq_index = 2, .irq_n = EXTI2_IRQn}, // pin 2
+    // EXTI3
+    {.gpio_idx = 0, .irq_index = 3, .irq_n = EXTI3_IRQn}, // pin 3
+    // EXTI4
+    {.gpio_idx = 0, .irq_index = 4, .irq_n = EXTI4_IRQn}, // pin 4
+    // EXTI5_9
+    {.gpio_idx = 0, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 5
+    {.gpio_idx = 1, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 6
+    {.gpio_idx = 2, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 7
+    {.gpio_idx = 3, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 8
+    {.gpio_idx = 4, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 9
+    // EXTI10_15
+    {.gpio_idx = 0, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 10
+    {.gpio_idx = 1, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 11
+    {.gpio_idx = 2, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 12
+    {.gpio_idx = 3, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 13
+    {.gpio_idx = 4, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 14
+    {.gpio_idx = 5, .irq_index = 6, .irq_n = EXTI15_10_IRQn}// pin 15
+};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L1/gpio_irq_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,67 @@
+/* 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_GPIO_IRQ_DEVICE_H
+#define MBED_GPIO_IRQ_DEVICE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "stm32l1xx_ll_exti.h"
+
+// Number of EXTI irq vectors (EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5_9, EXTI10_15)
+#define CHANNEL_NUM (7)
+
+#define EXTI_IRQ0_NUM_LINES 1
+#define EXTI_IRQ1_NUM_LINES 1
+#define EXTI_IRQ2_NUM_LINES 1
+#define EXTI_IRQ3_NUM_LINES 1
+#define EXTI_IRQ4_NUM_LINES 1
+#define EXTI_IRQ5_NUM_LINES 5
+#define EXTI_IRQ6_NUM_LINES 6
+
+// Max pins for one line (max with EXTI10_15)
+#define MAX_PIN_LINE (EXTI_IRQ6_NUM_LINES)
+
+/*  Structure to describe how the HW EXTI lines are defined in this HW */
+typedef struct exti_lines {
+    uint32_t gpio_idx;   // an index entry for each EXIT line
+    uint32_t irq_index;  // the IRQ index
+    IRQn_Type  irq_n;    // the corresponding EXTI IRQn
+} exti_lines_t;
+
+// Used to return the index for channels array.
+extern const exti_lines_t pin_lines_desc[];
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L1/pin_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,69 @@
+/* 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.
+ *******************************************************************************
+ */
+#ifndef MBED_PIN_DEVICE_H
+#define MBED_PIN_DEVICE_H
+
+#include "cmsis.h"
+#include "stm32l1xx_ll_gpio.h"
+
+extern const uint32_t ll_pin_defines[16];
+
+/* Family specific implementations */
+static inline void stm_pin_DisconnectDebug(PinName pin)
+{
+    /* empty for now */
+}
+
+static inline void stm_pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t pull_config)
+{
+    switch (pull_config) {
+        case GPIO_PULLUP:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_UP);
+            break;
+        case GPIO_PULLDOWN:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_DOWN);
+            break;
+        default:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_NO);
+            break;
+    }
+}
+
+static inline void stm_pin_SetAFPin( GPIO_TypeDef *gpio, PinName pin, uint32_t afnum)
+{
+    uint32_t ll_pin  = ll_pin_defines[STM_PIN(pin)];
+
+    if (STM_PIN(pin) > 7)
+        LL_GPIO_SetAFPin_8_15(gpio, ll_pin, afnum);
+    else
+        LL_GPIO_SetAFPin_0_7(gpio, ll_pin, afnum);
+}
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32L1/pinmap.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,158 +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 "mbed_assert.h"
-#include "pinmap.h"
-#include "PortNames.h"
-#include "mbed_error.h"
-
-// GPIO mode look-up table
-static const uint32_t gpio_mode[13] = {
-    0x00000000, //  0 = GPIO_MODE_INPUT
-    0x00000001, //  1 = GPIO_MODE_OUTPUT_PP
-    0x00000011, //  2 = GPIO_MODE_OUTPUT_OD
-    0x00000002, //  3 = GPIO_MODE_AF_PP
-    0x00000012, //  4 = GPIO_MODE_AF_OD
-    0x00000003, //  5 = GPIO_MODE_ANALOG
-    0x10110000, //  6 = GPIO_MODE_IT_RISING
-    0x10210000, //  7 = GPIO_MODE_IT_FALLING
-    0x10310000, //  8 = GPIO_MODE_IT_RISING_FALLING
-    0x10120000, //  9 = GPIO_MODE_EVT_RISING
-    0x10220000, // 10 = GPIO_MODE_EVT_FALLING
-    0x10320000, // 11 = GPIO_MODE_EVT_RISING_FALLING
-    0x10000000  // 12 = Reset IT and EVT (not in STM32Cube HAL)
-};
-
-// Enable GPIO clock and return GPIO base address
-uint32_t Set_GPIO_Clock(uint32_t port_idx)
-{
-    uint32_t gpio_add = 0;
-    switch (port_idx) {
-        case PortA:
-            gpio_add = GPIOA_BASE;
-            __GPIOA_CLK_ENABLE();
-            break;
-        case PortB:
-            gpio_add = GPIOB_BASE;
-            __GPIOB_CLK_ENABLE();
-            break;
-        case PortC:
-            gpio_add = GPIOC_BASE;
-            __GPIOC_CLK_ENABLE();
-            break;
-        case PortD:
-            gpio_add = GPIOD_BASE;
-            __GPIOD_CLK_ENABLE();
-            break;
-        case PortH:
-            gpio_add = GPIOH_BASE;
-            __GPIOH_CLK_ENABLE();
-            break;
-        default:
-            error("Pinmap error: wrong port number.");
-            break;
-    }
-    return gpio_add;
-}
-
-/**
- * Configure pin (mode, speed, output type and pull-up/pull-down)
- */
-void pin_function(PinName pin, int data)
-{
-    MBED_ASSERT(pin != (PinName)NC);
-    // Get the pin informations
-    uint32_t mode  = STM_PIN_MODE(data);
-    uint32_t pupd  = STM_PIN_PUPD(data);
-    uint32_t afnum = STM_PIN_AFNUM(data);
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Configure GPIO
-    GPIO_InitTypeDef GPIO_InitStructure;
-    GPIO_InitStructure.Pin       = (uint32_t)(1 << pin_index);
-    GPIO_InitStructure.Mode      = gpio_mode[mode];
-    GPIO_InitStructure.Pull      = pupd;
-    GPIO_InitStructure.Speed     = GPIO_SPEED_HIGH;
-    GPIO_InitStructure.Alternate = afnum;
-    HAL_GPIO_Init(gpio, &GPIO_InitStructure);
-
-    // [TODO] Disconnect JTAG-DP + SW-DP signals.
-    // Warning: Need to reconnect under reset
-    //if ((pin == PA_13) || (pin == PA_14)) {
-    //
-    //}
-    //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) {
-    //
-    //}
-}
-
-/**
- * Configure pin pull-up/pull-down
- */
-void pin_mode(PinName pin, PinMode mode)
-{
-    MBED_ASSERT(pin != (PinName)NC);
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Configure pull-up/pull-down resistors
-    uint32_t pupd = (uint32_t)mode;
-    if (pupd > 2)
-    {
-        pupd = 0; // Open-drain = No pull-up/No pull-down
-    }
-    gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2)));
-    gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
-}
-
-/*  Internal function for setting the gpiomode/function
- *  without changing Pull mode
- */
-void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
-
-    /* Read current pull state from HW to avoid over-write*/
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
-    uint32_t temp = gpio->PUPDR;
-    uint32_t pull = (temp  >> (pin_index * 2U)) & GPIO_PUPDR_PUPDR0;
-
-    /* Then re-use global function for updating the mode part*/
-    pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
-}
--- a/targets/TARGET_STM/TARGET_STM32L1/port_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,103 +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 "port_api.h"
-#include "pinmap.h"
-#include "gpio_api.h"
-#include "mbed_error.h"
-
-#if DEVICE_PORTIN || DEVICE_PORTOUT
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-
-// high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
-// low nibble  = pin number
-PinName port_pin(PortName port, int pin_n)
-{
-    return (PinName)(pin_n + (port << 4));
-}
-
-void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
-{
-    uint32_t port_index = (uint32_t)port;
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Fill PORT object structure for future use
-    obj->port      = port;
-    obj->mask      = mask;
-    obj->direction = dir;
-    obj->reg_in    = &gpio->IDR;
-    obj->reg_out   = &gpio->ODR;
-
-    port_dir(obj, dir);
-}
-
-void port_dir(port_t *obj, PinDirection dir)
-{
-    uint32_t i;
-    obj->direction = dir;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            if (dir == PIN_OUTPUT) {
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
-            } else { // PIN_INPUT
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
-            }
-        }
-    }
-}
-
-void port_mode(port_t *obj, PinMode mode)
-{
-    uint32_t i;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            pin_mode(port_pin(obj->port, i), mode);
-        }
-    }
-}
-
-void port_write(port_t *obj, int value)
-{
-    *obj->reg_out = (*obj->reg_out & ~obj->mask) | (value & obj->mask);
-}
-
-int port_read(port_t *obj)
-{
-    if (obj->direction == PIN_OUTPUT) {
-        return (*obj->reg_out & obj->mask);
-    } else { // PIN_INPUT
-        return (*obj->reg_in & obj->mask);
-    }
-}
-
-#endif
--- a/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,221 +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.");
-    }
-
-    // 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_ENABLE;
-
-    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:
-            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:
-            channel = TIM_CHANNEL_2;
-            break;
-        // Channels 3
-        case PA_2:
-        case PB_0:
-        case PB_8:
-        case PB_10:
-        case PC_8:
-            channel = TIM_CHANNEL_3;
-            break;
-        // Channels 4
-        case PA_3:
-        case PB_1:
-        case PB_9:
-        case PB_11:
-        case PC_9:
-            channel = TIM_CHANNEL_4;
-            break;
-        default:
-            return;
-    }
-
-    HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel);
-    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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,60 @@
+/* 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;
+
+extern const pwm_apb_map_t pwm_apb_map_table[];
+
+#endif // DEVICE_PWMOUT
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_DISCO_L476VG/system_stm32l4xx.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_DISCO_L476VG/system_stm32l4xx.c	Tue Mar 14 16:40:56 2017 +0000
@@ -569,7 +569,7 @@
   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
--- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_NUCLEO_L476RG/system_stm32l4xx.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_NUCLEO_L476RG/system_stm32l4xx.c	Tue Mar 14 16:40:56 2017 +0000
@@ -569,7 +569,7 @@
   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
--- a/targets/TARGET_STM/TARGET_STM32L4/analogin_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L4/analogin_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -30,7 +30,7 @@
 
 #if DEVICE_ANALOGIN
 
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "cmsis.h"
 #include "pinmap.h"
 #include "mbed_error.h"
--- a/targets/TARGET_STM/TARGET_STM32L4/can_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,485 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2016 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "can_api.h"
-
-#if DEVICE_CAN
-
-#include "cmsis.h"
-#include "pinmap.h"
-#include "PeripheralPins.h"
-#include "mbed_error.h"
-#include <math.h>
-#include <string.h>
-
-#define CAN_NUM    1
-static CAN_HandleTypeDef CanHandle;
-static uint32_t can_irq_ids[CAN_NUM] = {0};
-static can_irq_handler irq_handler;
-
-void can_init(can_t *obj, PinName rd, PinName td) 
-{
-    CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
-    CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
-    obj->can = (CANName)pinmap_merge(can_rd, can_td);
-    MBED_ASSERT((int)obj->can != NC);    
-
-    if(obj->can == CAN_1) {
-        __CAN_CLK_ENABLE();
-        obj->index = 0;
-    }
-
-    // Configure the CAN pins
-    pinmap_pinout(rd, PinMap_CAN_RD);
-    pinmap_pinout(td, PinMap_CAN_TD);
-    if (rd != NC) {
-        pin_mode(rd, PullUp);
-    }
-    if (td != NC) {
-        pin_mode(td, PullUp);
-    }
-
-    CanHandle.Instance = (CAN_TypeDef *)(obj->can);
-
-    CanHandle.Init.TTCM = DISABLE;
-    CanHandle.Init.ABOM = DISABLE;
-    CanHandle.Init.AWUM = DISABLE;
-    CanHandle.Init.NART = DISABLE;
-    CanHandle.Init.RFLM = DISABLE;
-    CanHandle.Init.TXFP = DISABLE;
-    CanHandle.Init.Mode = CAN_MODE_NORMAL;
-    CanHandle.Init.SJW = CAN_SJW_1TQ;
-    CanHandle.Init.BS1 = CAN_BS1_6TQ;
-    CanHandle.Init.BS2 = CAN_BS2_8TQ;
-    CanHandle.Init.Prescaler = 2;
-
-    if (HAL_CAN_Init(&CanHandle) != HAL_OK) {
-       error("Cannot initialize CAN");
-    }
-    // Set initial CAN frequency to 100kb/s
-    can_frequency(obj, 100000);
-
-    can_filter(obj, 0, 0, CANStandard, 0);
-}
-
-void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id) 
-{
-    irq_handler = handler;
-    can_irq_ids[obj->index] = id;    
-}
-
-void can_irq_free(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-  
-    can->IER &= ~(CAN_IT_FMP0 | CAN_IT_FMP1 | CAN_IT_TME | \
-                  CAN_IT_ERR | CAN_IT_EPV | CAN_IT_BOF);
-    can_irq_ids[obj->can] = 0;
-}
-
-void can_free(can_t *obj) 
-{
-    // Reset CAN and disable clock
-    if (obj->can == CAN_1) {
-        __CAN_FORCE_RESET();
-        __CAN_RELEASE_RESET();
-        __CAN_CLK_DISABLE();
-    }
-}
-
-// The following table is used to program bit_timing. It is an adjustment of the sample
-// point by synchronizing on the start-bit edge and resynchronizing on the following edges.
-// This table has the sampling points as close to 75% as possible (most commonly used).
-// The first value is TSEG1, the second TSEG2.
-static const int timing_pts[23][2] = {
-    {0x0, 0x0},      // 2,  50%
-    {0x1, 0x0},      // 3,  67%
-    {0x2, 0x0},      // 4,  75%
-    {0x3, 0x0},      // 5,  80%
-    {0x3, 0x1},      // 6,  67%
-    {0x4, 0x1},      // 7,  71%
-    {0x5, 0x1},      // 8,  75%
-    {0x6, 0x1},      // 9,  78%
-    {0x6, 0x2},      // 10, 70%
-    {0x7, 0x2},      // 11, 73%
-    {0x8, 0x2},      // 12, 75%
-    {0x9, 0x2},      // 13, 77%
-    {0x9, 0x3},      // 14, 71%
-    {0xA, 0x3},      // 15, 73%
-    {0xB, 0x3},      // 16, 75%
-    {0xC, 0x3},      // 17, 76%
-    {0xD, 0x3},      // 18, 78%
-    {0xD, 0x4},      // 19, 74%
-    {0xE, 0x4},      // 20, 75%
-    {0xF, 0x4},      // 21, 76%
-    {0xF, 0x5},      // 22, 73%
-    {0xF, 0x6},      // 23, 70%
-    {0xF, 0x7},      // 24, 67%
-};
-
-static unsigned int can_speed(unsigned int pclk, unsigned int cclk, unsigned char psjw) 
-{
-    uint32_t    btr;
-    uint16_t    brp = 0;
-    uint32_t    calcbit;
-    uint32_t    bitwidth;
-    int         hit = 0;
-    int         bits;
-    
-    bitwidth = (pclk / cclk);
-    
-    brp = bitwidth / 0x18;
-    while ((!hit) && (brp < bitwidth / 4)) {
-        brp++;
-        for (bits = 22; bits > 0; bits--) {
-            calcbit = (bits + 3) * (brp + 1);
-            if (calcbit == bitwidth) {
-                hit = 1;
-                break;
-            }
-        }
-    }
-    
-    if (hit) {
-        btr = ((timing_pts[bits][1] << 20) & 0x00700000)
-            | ((timing_pts[bits][0] << 16) & 0x000F0000)
-            | ((psjw                << 24) & 0x0000C000)
-            | ((brp                 <<  0) & 0x000003FF);
-    } else {
-        btr = 0xFFFFFFFF;
-    }
-    
-    return btr;
-
-}
-
-int can_frequency(can_t *obj, int f) 
-{
-    int pclk = HAL_RCC_GetPCLK1Freq();
-    int btr = can_speed(pclk, (unsigned int)f, 1);
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    if (btr > 0) {
-        can->MCR |= CAN_MCR_INRQ ;
-        while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-        }
-        can->BTR = btr;
-        can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-        while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-        }
-        return 1;
-    } else {
-        return 0;
-    }
-}
-
-int can_write(can_t *obj, CAN_Message msg, int cc) 
-{
-    uint32_t  transmitmailbox = 5;
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    /* Select one empty transmit mailbox */
-    if ((can->TSR&CAN_TSR_TME0) == CAN_TSR_TME0) {
-        transmitmailbox = 0;
-    } else if ((can->TSR&CAN_TSR_TME1) == CAN_TSR_TME1) {
-        transmitmailbox = 1;
-    } else if ((can->TSR&CAN_TSR_TME2) == CAN_TSR_TME2) {
-        transmitmailbox = 2;
-    } else {
-        transmitmailbox = CAN_TXSTATUS_NOMAILBOX;
-    }
-
-    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) |
-                                             ((uint32_t)msg.data[4]));
-    /* Request transmission */
-    can->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ;
-  }
-
-  return 1;
-}
-
-int can_read(can_t *obj, CAN_Message *msg, int handle) 
-{
-    //handle is the FIFO number
-
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    
-    // check FPM0 which holds the pending message count in FIFO 0
-    // if no message is pending, return 0
-    if ((can->RF0R & CAN_RF0R_FMP0) == 0) {
-        return 0;
-    }
-
-    /* Get the Id */
-    msg->format = (CANFormat)((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR);
-    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);
-    /* Get the DLC */
-    msg->len = (uint8_t)0x0F & can->sFIFOMailBox[handle].RDTR;
-//  /* Get the FMI */
-//  msg->FMI = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDTR >> 8);
-    /* Get the data field */
-    msg->data[0] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDLR;
-    msg->data[1] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 8);
-    msg->data[2] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 16);
-    msg->data[3] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 24);
-    msg->data[4] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDHR;
-    msg->data[5] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 8);
-    msg->data[6] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 16);
-    msg->data[7] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 24);
-    
-    /* Release the FIFO */
-    if(handle == CAN_FIFO0) {
-        /* Release FIFO0 */
-        can->RF0R |= CAN_RF0R_RFOM0;
-    } else { /* FIFONumber == CAN_FIFO1 */
-      /* Release FIFO1 */
-      can->RF1R |= CAN_RF1R_RFOM1;
-    }
-
-    return 1;
-}
-
-void can_reset(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-
-    can->MCR |= CAN_MCR_RESET;  
-    can->ESR = 0x0;
-}
-
-unsigned char can_rderror(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    return (can->ESR >> 24) & 0xFF;
-}
-
-unsigned char can_tderror(can_t *obj) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);    
-    return (can->ESR >> 16) & 0xFF;
-}
-
-void can_monitor(can_t *obj, int silent) 
-{
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);        
-    
-    can->MCR |= CAN_MCR_INRQ ;
-    while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-    }
-    if (silent) {
-        can->BTR |= ((uint32_t)1 << 31);
-    } else {
-        can->BTR &= ~((uint32_t)1 << 31);
-    }
-    can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-    while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-    }
-}
-
-int can_mode(can_t *obj, CanMode mode) 
-{
-    int success = 0;
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-    can->MCR |= CAN_MCR_INRQ ;
-    while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
-    }
-    switch (mode) {
-        case MODE_NORMAL:
-            can->BTR &= ~(CAN_BTR_SILM | CAN_BTR_LBKM);
-            success = 1;
-            break;
-        case MODE_SILENT:
-            can->BTR |= CAN_BTR_SILM;
-            can->BTR &= ~CAN_BTR_LBKM;
-            success = 1;
-            break;
-        case MODE_TEST_GLOBAL:
-        case MODE_TEST_LOCAL:
-            can->BTR |= CAN_BTR_LBKM;
-            can->BTR &= ~CAN_BTR_SILM;
-            success = 1;
-            break;
-        case MODE_TEST_SILENT:
-            can->BTR |= (CAN_BTR_SILM | CAN_BTR_LBKM);
-            success = 1;
-            break;
-        default:
-            success = 0;
-            break;
-    }
-    can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
-    while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
-    }
-    return success;
-}
-
-int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) 
-{
-    CanHandle.Instance = (CAN_TypeDef *)(obj->can);
-    CAN_FilterConfTypeDef  sFilterConfig;
-    
-    sFilterConfig.FilterNumber = handle;
-    sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
-    sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
-    sFilterConfig.FilterIdHigh = (uint8_t) (id >> 8);
-    sFilterConfig.FilterIdLow = (uint8_t) id;
-    sFilterConfig.FilterMaskIdHigh = (uint8_t) (mask >> 8);
-    sFilterConfig.FilterMaskIdLow = (uint8_t) mask;
-    sFilterConfig.FilterFIFOAssignment = 0;
-    sFilterConfig.FilterActivation = ENABLE;
-    sFilterConfig.BankNumber = 14 + handle;
-  
-    HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig);
-      
-    return 0;
-}
-
-static void can_irq(CANName name, int id) 
-{
-    uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0;    
-    CanHandle.Instance = (CAN_TypeDef *)name;
-    
-    if(__HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_TME)) {
-        tmp1 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_0);
-        tmp2 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_1);
-        tmp3 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_2);
-        if(tmp1 || tmp2 || tmp3)  
-        {
-            irq_handler(can_irq_ids[id], IRQ_TX);
-        }
-    }
-  
-    tmp1 = __HAL_CAN_MSG_PENDING(&CanHandle, CAN_FIFO0);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_FMP0);
-  
-    if((tmp1 != 0) && tmp2) {
-         irq_handler(can_irq_ids[id], IRQ_RX);
-    }
-  
-    tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_EPV);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_EPV);
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR); 
-  
-    if(tmp1 && tmp2 && tmp3) {
-         irq_handler(can_irq_ids[id], IRQ_PASSIVE);
-    }
-  
-    tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_BOF);
-    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_BOF);
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);  
-    if(tmp1 && tmp2 && tmp3) {
-        irq_handler(can_irq_ids[id], IRQ_BUS);
-    }
-  
-    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);  
-    if(tmp1 && tmp2 && tmp3) {
-        irq_handler(can_irq_ids[id], IRQ_ERROR);
-    }  
-}
-
-void CAN1_RX0_IRQHandler(void) 
-{
-    can_irq(CAN_1, 0);
-}
-
-void CAN1_TX_IRQHandler(void) 
-{
-    can_irq(CAN_1, 0);
-}
-
-void CAN1_SCE_IRQHandler(void) 
-{
-    can_irq(CAN_1, 0);
-}
-
-void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
-{
-
-    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
-    IRQn_Type irq_n = (IRQn_Type)0;
-    uint32_t vector = 0;    
-    uint32_t ier;
-
-    if(obj->can == CAN_1) {
-        switch (type) {
-            case IRQ_RX:
-                ier = CAN_IT_FMP0;
-                irq_n = CAN1_RX0_IRQn;
-                vector = (uint32_t)&CAN1_RX0_IRQHandler;
-                break;
-            case IRQ_TX:      
-                ier = CAN_IT_TME;
-                irq_n = CAN1_TX_IRQn;
-                vector = (uint32_t)&CAN1_TX_IRQHandler;
-                break;
-            case IRQ_ERROR:   
-                ier = CAN_IT_ERR;
-                irq_n = CAN1_SCE_IRQn;
-                vector = (uint32_t)&CAN1_SCE_IRQHandler;
-                break;
-            case IRQ_PASSIVE:
-                ier = CAN_IT_EPV;
-                irq_n = CAN1_SCE_IRQn;
-                vector = (uint32_t)&CAN1_SCE_IRQHandler;
-                break;
-            case IRQ_BUS:
-                ier = CAN_IT_BOF;
-                irq_n = CAN1_SCE_IRQn;
-                vector = (uint32_t)&CAN1_SCE_IRQHandler;
-                break;
-            default: return;
-        }
-    } 
-
-    if(enable) {
-        can->IER |= ier;
-    } else {
-        can->IER &= ~ier;
-    }
-
-    NVIC_SetVector(irq_n, vector);
-    NVIC_EnableIRQ(irq_n);
-}
-
-#endif // DEVICE_CAN
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L4/can_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,42 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef MBED_CAN_DEVICE_H
+#define MBED_CAN_DEVICE_H
+
+#include "cmsis.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef DEVICE_CAN
+
+#define CAN_NUM    1 // Number of CAN peripherals present in the STM32 serie
+
+#define CAN1_IRQ_RX_IRQN        CAN1_RX0_IRQn
+#define CAN1_IRQ_RX_VECT        CAN1_RX0_IRQHandler
+#define CAN1_IRQ_TX_IRQN        CAN1_TX_IRQn
+#define CAN1_IRQ_TX_VECT        CAN1_TX_IRQHandler
+#define CAN1_IRQ_ERROR_IRQN     CAN1_SCE_IRQn
+#define CAN1_IRQ_ERROR_VECT     CAN1_SCE_IRQHandler
+#define CAN1_IRQ_PASSIVE_IRQN   CAN1_SCE_IRQn
+#define CAN1_IRQ_PASSIVE_VECT   CAN1_SCE_IRQHandler
+#define CAN1_IRQ_BUS_IRQN       CAN1_SCE_IRQn
+#define CAN1_IRQ_BUS_VECT       CAN1_SCE_IRQHandler
+
+#endif // DEVICE_CAN
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32L4/gpio_irq_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,334 +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 <stddef.h>
-#include "cmsis.h"
-#include "gpio_irq_api.h"
-#include "pinmap.h"
-#include "mbed_error.h"
-
-#define EDGE_NONE (0)
-#define EDGE_RISE (1)
-#define EDGE_FALL (2)
-#define EDGE_BOTH (3)
-
-// Number of EXTI irq vectors (EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5_9, EXTI10_15)
-#define CHANNEL_NUM (7)
-
-// Max pins for one line (max with EXTI10_15)
-#define MAX_PIN_LINE (6)
-
-typedef struct gpio_channel {
-    uint32_t pin_mask;                   // bitmask representing which pins are configured for receiving interrupts
-    uint32_t channel_ids[MAX_PIN_LINE];  // mbed "gpio_irq_t gpio_irq" field of instance
-    uint32_t channel_gpio[MAX_PIN_LINE]; // base address of gpio port group
-    uint32_t channel_pin[MAX_PIN_LINE];  // pin number in port group
-} gpio_channel_t;
-
-static gpio_channel_t channels[CHANNEL_NUM] = {
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0},
-    {.pin_mask = 0}
-};
-
-// Used to return the index for channels array.
-static uint32_t pin_base_nr[16] = {
-    // EXTI0
-    0, // pin 0
-    // EXTI1
-    0, // pin 1
-    // EXTI2
-    0, // pin 2
-    // EXTI3
-    0, // pin 3
-    // EXTI4
-    0, // pin 4
-    // EXTI5_9
-    0, // pin 5
-    1, // pin 6
-    2, // pin 7
-    3, // pin 8
-    4, // pin 9
-    // EXTI10_15
-    0, // pin 10
-    1, // pin 11
-    2, // pin 12
-    3, // pin 13
-    4, // pin 14
-    5  // pin 15
-};
-
-static gpio_irq_handler irq_handler;
-
-static void handle_interrupt_in(uint32_t irq_index, uint32_t max_num_pin_line)
-{
-    gpio_channel_t *gpio_channel = &channels[irq_index];
-    uint32_t gpio_idx;
-
-    for (gpio_idx = 0; gpio_idx < max_num_pin_line; gpio_idx++) {
-        uint32_t current_mask = (1 << gpio_idx);
-
-        if (gpio_channel->pin_mask & current_mask) {
-            // Retrieve the gpio and pin that generate the irq
-            GPIO_TypeDef *gpio = (GPIO_TypeDef *)(gpio_channel->channel_gpio[gpio_idx]);
-            uint32_t pin = (uint32_t)(1 << (gpio_channel->channel_pin[gpio_idx]));
-
-            // Clear interrupt flag
-            if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) {
-                __HAL_GPIO_EXTI_CLEAR_FLAG(pin);
-
-                if (gpio_channel->channel_ids[gpio_idx] == 0) continue;
-
-                // Check which edge has generated the irq
-                if ((gpio->IDR & pin) == 0) {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_FALL);
-                } else  {
-                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_RISE);
-                }
-            }
-        }
-    }
-}
-
-// EXTI line 0
-static void gpio_irq0(void)
-{
-    handle_interrupt_in(0, 1);
-}
-
-// EXTI line 1
-static void gpio_irq1(void)
-{
-    handle_interrupt_in(1, 1);
-}
-
-// EXTI line 2
-static void gpio_irq2(void)
-{
-    handle_interrupt_in(2, 1);
-}
-
-// EXTI line 3
-static void gpio_irq3(void)
-{
-    handle_interrupt_in(3, 1);
-}
-
-// EXTI line 4
-static void gpio_irq4(void)
-{
-    handle_interrupt_in(4, 1);
-}
-
-// EXTI lines 5 to 9
-static void gpio_irq5(void)
-{
-    handle_interrupt_in(5, 5);
-}
-
-// EXTI lines 10 to 15
-static void gpio_irq6(void)
-{
-    handle_interrupt_in(6, 6);
-}
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
-
-int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
-{
-    IRQn_Type irq_n = (IRQn_Type)0;
-    uint32_t vector = 0;
-    uint32_t irq_index;
-    gpio_channel_t *gpio_channel;
-    uint32_t gpio_idx;
-
-    if (pin == NC) return -1;
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Select irq number and interrupt routine
-    switch (pin_index) {
-        case 0:
-            irq_n = EXTI0_IRQn;
-            vector = (uint32_t)&gpio_irq0;
-            irq_index = 0;
-            break;
-        case 1:
-            irq_n = EXTI1_IRQn;
-            vector = (uint32_t)&gpio_irq1;
-            irq_index = 1;
-            break;
-        case 2:
-            irq_n = EXTI2_IRQn;
-            vector = (uint32_t)&gpio_irq2;
-            irq_index = 2;
-            break;
-        case 3:
-            irq_n = EXTI3_IRQn;
-            vector = (uint32_t)&gpio_irq3;
-            irq_index = 3;
-            break;
-        case 4:
-            irq_n = EXTI4_IRQn;
-            vector = (uint32_t)&gpio_irq4;
-            irq_index = 4;
-            break;
-        case 5:
-        case 6:
-        case 7:
-        case 8:
-        case 9:
-            irq_n = EXTI9_5_IRQn;
-            vector = (uint32_t)&gpio_irq5;
-            irq_index = 5;
-            break;
-        case 10:
-        case 11:
-        case 12:
-        case 13:
-        case 14:
-        case 15:
-            irq_n = EXTI15_10_IRQn;
-            vector = (uint32_t)&gpio_irq6;
-            irq_index = 6;
-            break;
-        default:
-            error("InterruptIn error: pin not supported.\n");
-            return -1;
-    }
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-
-    // Configure GPIO
-    pin_function(pin, STM_PIN_DATA(STM_MODE_IT_FALLING, GPIO_NOPULL, 0));
-
-    // Enable EXTI interrupt
-    NVIC_SetVector(irq_n, vector);
-    NVIC_EnableIRQ(irq_n);
-
-    // Save informations for future use
-    obj->irq_n = irq_n;
-    obj->irq_index = irq_index;
-    obj->event = EDGE_NONE;
-    obj->pin = pin;
-
-    gpio_channel = &channels[irq_index];
-    gpio_idx = pin_base_nr[pin_index];
-    gpio_channel->pin_mask |= (1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = id;
-    gpio_channel->channel_gpio[gpio_idx] = gpio_add;
-    gpio_channel->channel_pin[gpio_idx] = pin_index;
-
-    irq_handler = handler;
-
-    return 0;
-}
-
-void gpio_irq_free(gpio_irq_t *obj)
-{
-    gpio_channel_t *gpio_channel = &channels[obj->irq_index];
-    uint32_t pin_index  = STM_PIN(obj->pin);
-    uint32_t gpio_addr = GPIOA_BASE + (GPIOB_BASE-GPIOA_BASE) * STM_PORT(obj->pin);
-    uint32_t gpio_idx = pin_base_nr[pin_index];
-    
-    HAL_GPIO_DeInit((GPIO_TypeDef *)gpio_addr, (1<<pin_index));
-    gpio_channel->pin_mask &= ~(1 << gpio_idx);
-    gpio_channel->channel_ids[gpio_idx] = 0;
-    gpio_channel->channel_gpio[gpio_idx] = 0;
-    gpio_channel->channel_pin[gpio_idx] = 0;
-
-    // Disable EXTI line, but don't change pull-up config
-    pin_function_gpiomode(obj->pin, STM_MODE_INPUT);
-    obj->event = EDGE_NONE;
-}
-
-void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
-{
-    uint32_t mode = STM_MODE_IT_EVT_RESET;
-
-    if (enable) {
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or RISE
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING_FALLING;
-                obj->event = EDGE_BOTH;
-            } else { // NONE or FALL
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            }
-        }
-    } else { // Disable
-        if (event == IRQ_RISE) {
-            if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_FALLING;
-                obj->event = EDGE_FALL;
-            } else { // NONE or RISE
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-        if (event == IRQ_FALL) {
-            if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
-                mode = STM_MODE_IT_RISING;
-                obj->event = EDGE_RISE;
-            } else { // NONE or FALL
-                mode = STM_MODE_INPUT;
-                obj->event = EDGE_NONE;
-            }
-        }
-    }
-
-    pin_function_gpiomode(obj->pin, mode);
-}
-
-void gpio_irq_enable(gpio_irq_t *obj)
-{
-    NVIC_EnableIRQ(obj->irq_n);
-}
-
-void gpio_irq_disable(gpio_irq_t *obj)
-{
-    NVIC_DisableIRQ(obj->irq_n);
-    obj->event = EDGE_NONE;
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L4/gpio_irq_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,59 @@
+/* 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 "gpio_irq_device.h"
+
+// Used to return the index for channels array.
+const exti_lines_t pin_lines_desc[16] = {
+    // EXTI0
+    {.gpio_idx = 0, .irq_index = 0, .irq_n = EXTI0_IRQn}, // pin 0
+    // EXTI1
+    {.gpio_idx = 0, .irq_index = 1, .irq_n = EXTI1_IRQn}, // pin 1
+    // EXTI2
+    {.gpio_idx = 0, .irq_index = 2, .irq_n = EXTI2_IRQn}, // pin 2
+    // EXTI3
+    {.gpio_idx = 0, .irq_index = 3, .irq_n = EXTI3_IRQn}, // pin 3
+    // EXTI4
+    {.gpio_idx = 0, .irq_index = 4, .irq_n = EXTI4_IRQn}, // pin 4
+    // EXTI5_9
+    {.gpio_idx = 0, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 5
+    {.gpio_idx = 1, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 6
+    {.gpio_idx = 2, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 7
+    {.gpio_idx = 3, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 8
+    {.gpio_idx = 4, .irq_index = 5, .irq_n = EXTI9_5_IRQn},// pin 9
+    // EXTI10_15
+    {.gpio_idx = 0, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 10
+    {.gpio_idx = 1, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 11
+    {.gpio_idx = 2, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 12
+    {.gpio_idx = 3, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 13
+    {.gpio_idx = 4, .irq_index = 6, .irq_n = EXTI15_10_IRQn},// pin 14
+    {.gpio_idx = 5, .irq_index = 6, .irq_n = EXTI15_10_IRQn}// pin 15
+};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L4/gpio_irq_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,67 @@
+/* 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_GPIO_IRQ_DEVICE_H
+#define MBED_GPIO_IRQ_DEVICE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "stm32l4xx_ll_exti.h"
+
+// Number of EXTI irq vectors (EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5_9, EXTI10_15)
+#define CHANNEL_NUM (7)
+
+#define EXTI_IRQ0_NUM_LINES 1
+#define EXTI_IRQ1_NUM_LINES 1
+#define EXTI_IRQ2_NUM_LINES 1
+#define EXTI_IRQ3_NUM_LINES 1
+#define EXTI_IRQ4_NUM_LINES 1
+#define EXTI_IRQ5_NUM_LINES 5
+#define EXTI_IRQ6_NUM_LINES 6
+
+// Max pins for one line (max with EXTI10_15)
+#define MAX_PIN_LINE (EXTI_IRQ6_NUM_LINES)
+
+/*  Structure to describe how the HW EXTI lines are defined in this HW */
+typedef struct exti_lines {
+    uint32_t gpio_idx;   // an index entry for each EXIT line
+    uint32_t irq_index;  // the IRQ index
+    IRQn_Type  irq_n;    // the corresponding EXTI IRQn
+} exti_lines_t;
+
+// Used to return the index for channels array.
+extern const exti_lines_t pin_lines_desc[];
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L4/pin_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,69 @@
+/* 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.
+ *******************************************************************************
+ */
+#ifndef MBED_PIN_DEVICE_H
+#define MBED_PIN_DEVICE_H
+
+#include "cmsis.h"
+#include "stm32l4xx_ll_gpio.h"
+
+extern const uint32_t ll_pin_defines[16];
+
+/* Family specific implementations */
+static inline void stm_pin_DisconnectDebug(PinName pin)
+{
+    /* empty for now */
+}
+
+static inline void stm_pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t pull_config)
+{
+    switch (pull_config) {
+        case GPIO_PULLUP:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_UP);
+            break;
+        case GPIO_PULLDOWN:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_DOWN);
+            break;
+        default:
+            LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_NO);
+            break;
+    }
+}
+
+static inline void stm_pin_SetAFPin( GPIO_TypeDef *gpio, PinName pin, uint32_t afnum)
+{
+    uint32_t ll_pin  = ll_pin_defines[STM_PIN(pin)];
+
+    if (STM_PIN(pin) > 7)
+        LL_GPIO_SetAFPin_8_15(gpio, ll_pin, afnum);
+    else
+        LL_GPIO_SetAFPin_0_7(gpio, ll_pin, afnum);
+}
+
+#endif
--- a/targets/TARGET_STM/TARGET_STM32L4/pinmap.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,167 +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 "mbed_assert.h"
-#include "pinmap.h"
-#include "PortNames.h"
-#include "mbed_error.h"
-
-// GPIO mode look-up table
-// Warning: order must be the same as the one defined in PinNames.h !!!
-static const uint32_t gpio_mode[14] = {
-    0x00000000, //  0 = GPIO_MODE_INPUT
-    0x00000001, //  1 = GPIO_MODE_OUTPUT_PP
-    0x00000011, //  2 = GPIO_MODE_OUTPUT_OD
-    0x00000002, //  3 = GPIO_MODE_AF_PP
-    0x00000012, //  4 = GPIO_MODE_AF_OD
-    0x00000003, //  5 = GPIO_MODE_ANALOG
-    0x10110000, //  6 = GPIO_MODE_IT_RISING
-    0x10210000, //  7 = GPIO_MODE_IT_FALLING
-    0x10310000, //  8 = GPIO_MODE_IT_RISING_FALLING
-    0x10120000, //  9 = GPIO_MODE_EVT_RISING
-    0x10220000, // 10 = GPIO_MODE_EVT_FALLING
-    0x10320000, // 11 = GPIO_MODE_EVT_RISING_FALLING
-    0x10000000, // 12 = Reset IT and EVT (not in STM32Cube HAL)
-    0x0000000B  //13 = GPIO_MODE_ANALOG_ADC_CONTROL
-};
-
-// Enable GPIO clock and return GPIO base address
-uint32_t Set_GPIO_Clock(uint32_t port_idx)
-{
-    uint32_t gpio_add = 0;
-    switch (port_idx) {
-        case PortA:
-            gpio_add = GPIOA_BASE;
-            __HAL_RCC_GPIOA_CLK_ENABLE();
-            break;
-        case PortB:
-            gpio_add = GPIOB_BASE;
-            __HAL_RCC_GPIOB_CLK_ENABLE();
-            break;
-        case PortC:
-            gpio_add = GPIOC_BASE;
-            __HAL_RCC_GPIOC_CLK_ENABLE();
-            break;
-#if defined(GPIOD_BASE)
-        case PortD:
-            gpio_add = GPIOD_BASE;
-            __HAL_RCC_GPIOD_CLK_ENABLE();
-            break;
-#endif
-#if defined(GPIOE_BASE)
-        case PortE:
-            gpio_add = GPIOE_BASE;
-            __HAL_RCC_GPIOE_CLK_ENABLE();
-            break;
-#endif
-        case PortH:
-            gpio_add = GPIOH_BASE;
-            __HAL_RCC_GPIOH_CLK_ENABLE();
-            break;
-        default:
-            error("Pinmap error: wrong port number\n");
-            break;
-    }
-    return gpio_add;
-}
-
-/**
- * Configure pin (mode, speed, output type and pull-up/pull-down)
- */
-void pin_function(PinName pin, int data)
-{
-    MBED_ASSERT(pin != (PinName)NC);
-    // Get the pin informations
-    uint32_t mode  = STM_PIN_MODE(data);
-    uint32_t pupd  = STM_PIN_PUPD(data);
-    uint32_t afnum = STM_PIN_AFNUM(data);
-
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Configure GPIO
-    GPIO_InitTypeDef GPIO_InitStructure;
-    GPIO_InitStructure.Pin       = (uint32_t)(1 << pin_index);
-    GPIO_InitStructure.Mode      = gpio_mode[mode];
-    GPIO_InitStructure.Pull      = pupd;
-    GPIO_InitStructure.Speed     = GPIO_SPEED_HIGH;
-    GPIO_InitStructure.Alternate = afnum;
-    HAL_GPIO_Init(gpio, &GPIO_InitStructure);
-
-    // [TODO] Disconnect JTAG-DP + SW-DP signals.
-    // Warning: Need to reconnect under reset
-    //if ((pin == PA_13) || (pin == PA_14)) {
-    //
-    //}
-    //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) {
-    //
-    //}
-}
-
-/**
- * Configure pin pull-up/pull-down
- */
-void pin_mode(PinName pin, PinMode mode)
-{
-    MBED_ASSERT(pin != (PinName)NC);
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Configure pull-up/pull-down resistors
-    uint32_t pupd = (uint32_t)mode;
-    if (pupd > 2) {
-        pupd = 0; // Open-drain = No pull-up/No pull-down
-    }
-    gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2)));
-    gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
-}
-
-/*  Internal function for setting the gpiomode/function
- *  without changing Pull mode
- */
-void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
-
-    /* Read current pull state from HW to avoid over-write*/
-    uint32_t port_index = STM_PORT(pin);
-    uint32_t pin_index  = STM_PIN(pin);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
-    uint32_t temp = gpio->PUPDR;
-    uint32_t pull = (temp  >> (pin_index * 2U)) & GPIO_PUPDR_PUPDR0;
-
-    /* Then re-use global function for updating the mode part*/
-    pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
-}
--- a/targets/TARGET_STM/TARGET_STM32L4/port_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,103 +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 "port_api.h"
-#include "pinmap.h"
-#include "gpio_api.h"
-#include "mbed_error.h"
-
-#if DEVICE_PORTIN || DEVICE_PORTOUT
-
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
-
-// high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
-// low nibble  = pin number
-PinName port_pin(PortName port, int pin_n)
-{
-    return (PinName)(pin_n + (port << 4));
-}
-
-void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
-{
-    uint32_t port_index = (uint32_t)port;
-
-    // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
-
-    // Fill PORT object structure for future use
-    obj->port      = port;
-    obj->mask      = mask;
-    obj->direction = dir;
-    obj->reg_in    = &gpio->IDR;
-    obj->reg_out   = &gpio->ODR;
-
-    port_dir(obj, dir);
-}
-
-void port_dir(port_t *obj, PinDirection dir)
-{
-    uint32_t i;
-    obj->direction = dir;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            if (dir == PIN_OUTPUT) {
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
-            } else { // PIN_INPUT
-                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
-            }
-        }
-    }
-}
-
-void port_mode(port_t *obj, PinMode mode)
-{
-    uint32_t i;
-    for (i = 0; i < 16; i++) { // Process all pins
-        if (obj->mask & (1 << i)) { // If the pin is used
-            pin_mode(port_pin(obj->port, i), mode);
-        }
-    }
-}
-
-void port_write(port_t *obj, int value)
-{
-    *obj->reg_out = (*obj->reg_out & ~obj->mask) | (value & obj->mask);
-}
-
-int port_read(port_t *obj)
-{
-    if (obj->direction == PIN_OUTPUT) {
-        return (*obj->reg_out & obj->mask);
-    } else { // PIN_INPUT
-        return (*obj->reg_in & obj->mask);
-    }
-}
-
-#endif
--- a/targets/TARGET_STM/TARGET_STM32L4/pwmout_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.c	Tue Mar 14 16:40:56 2017 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,57 @@
+/* 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;
+
+extern const pwm_apb_map_t pwm_apb_map_table[];
+
+#endif // DEVICE_PWMOUT
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/can_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,595 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "can_api.h"
+
+#if DEVICE_CAN
+
+#include "cmsis.h"
+#include "pinmap.h"
+#include "PeripheralPins.h"
+#include "mbed_error.h"
+#include "can_device.h" // Specific to STM32 serie
+#include <math.h>
+#include <string.h>
+
+static CAN_HandleTypeDef CanHandle;
+static uint32_t can_irq_ids[CAN_NUM] = {0};
+static can_irq_handler irq_handler;
+
+void can_init(can_t *obj, PinName rd, PinName td)
+{
+    CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
+    CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
+
+    obj->can = (CANName)pinmap_merge(can_rd, can_td);
+    MBED_ASSERT((int)obj->can != NC);
+
+    if (obj->can == CAN_1) {
+        __HAL_RCC_CAN1_CLK_ENABLE();
+        obj->index = 0;
+    }
+#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;
+    }
+#endif
+    else {
+        return;
+    }
+
+    // Configure the CAN pins
+    pinmap_pinout(rd, PinMap_CAN_RD);
+    pinmap_pinout(td, PinMap_CAN_TD);
+    if (rd != NC) {
+        pin_mode(rd, PullUp);
+    }
+    if (td != NC) {
+        pin_mode(td, PullUp);
+    }
+
+    CanHandle.Instance = (CAN_TypeDef *)(obj->can);
+
+    CanHandle.Init.TTCM = DISABLE;
+    CanHandle.Init.ABOM = DISABLE;
+    CanHandle.Init.AWUM = DISABLE;
+    CanHandle.Init.NART = DISABLE;
+    CanHandle.Init.RFLM = DISABLE;
+    CanHandle.Init.TXFP = DISABLE;
+    CanHandle.Init.Mode = CAN_MODE_NORMAL;
+    CanHandle.Init.SJW = CAN_SJW_1TQ;
+    CanHandle.Init.BS1 = CAN_BS1_6TQ;
+    CanHandle.Init.BS2 = CAN_BS2_8TQ;
+    CanHandle.Init.Prescaler = 2;
+
+    if (HAL_CAN_Init(&CanHandle) != HAL_OK) {
+        error("Cannot initialize CAN");
+    }
+
+    // Set initial CAN frequency to 100 kb/s
+    can_frequency(obj, 100000);
+
+    uint32_t filter_number = (obj->can == CAN_1) ? 0 : 14;
+    can_filter(obj, 0, 0, CANStandard, filter_number);
+}
+
+void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id)
+{
+    irq_handler = handler;
+    can_irq_ids[obj->index] = id;
+}
+
+void can_irq_free(can_t *obj)
+{
+    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
+
+    can->IER &= ~(CAN_IT_FMP0 | CAN_IT_FMP1 | CAN_IT_TME | \
+                  CAN_IT_ERR | CAN_IT_EPV | CAN_IT_BOF);
+    can_irq_ids[obj->can] = 0;
+}
+
+void can_free(can_t *obj)
+{
+    // Reset CAN and disable clock
+    if (obj->can == CAN_1) {
+        __HAL_RCC_CAN1_FORCE_RESET();
+        __HAL_RCC_CAN1_RELEASE_RESET();
+        __HAL_RCC_CAN1_CLK_DISABLE();
+    }
+#if defined(CAN2_BASE) && (CAN_NUM == 2)
+    if (obj->can == CAN_2) {
+        __HAL_RCC_CAN2_FORCE_RESET();
+        __HAL_RCC_CAN2_RELEASE_RESET();
+        __HAL_RCC_CAN2_CLK_DISABLE();
+    }
+#endif
+}
+
+// The following table is used to program bit_timing. It is an adjustment of the sample
+// point by synchronizing on the start-bit edge and resynchronizing on the following edges.
+// This table has the sampling points as close to 75% as possible (most commonly used).
+// The first value is TSEG1, the second TSEG2.
+static const int timing_pts[23][2] = {
+    {0x0, 0x0},      // 2,  50%
+    {0x1, 0x0},      // 3,  67%
+    {0x2, 0x0},      // 4,  75%
+    {0x3, 0x0},      // 5,  80%
+    {0x3, 0x1},      // 6,  67%
+    {0x4, 0x1},      // 7,  71%
+    {0x5, 0x1},      // 8,  75%
+    {0x6, 0x1},      // 9,  78%
+    {0x6, 0x2},      // 10, 70%
+    {0x7, 0x2},      // 11, 73%
+    {0x8, 0x2},      // 12, 75%
+    {0x9, 0x2},      // 13, 77%
+    {0x9, 0x3},      // 14, 71%
+    {0xA, 0x3},      // 15, 73%
+    {0xB, 0x3},      // 16, 75%
+    {0xC, 0x3},      // 17, 76%
+    {0xD, 0x3},      // 18, 78%
+    {0xD, 0x4},      // 19, 74%
+    {0xE, 0x4},      // 20, 75%
+    {0xF, 0x4},      // 21, 76%
+    {0xF, 0x5},      // 22, 73%
+    {0xF, 0x6},      // 23, 70%
+    {0xF, 0x7},      // 24, 67%
+};
+
+static unsigned int can_speed(unsigned int pclk, unsigned int cclk, unsigned char psjw)
+{
+    uint32_t    btr;
+    uint16_t    brp = 0;
+    uint32_t    calcbit;
+    uint32_t    bitwidth;
+    int         hit = 0;
+    int         bits;
+
+    bitwidth = (pclk / cclk);
+
+    brp = bitwidth / 0x18;
+    while ((!hit) && (brp < bitwidth / 4)) {
+        brp++;
+        for (bits = 22; bits > 0; bits--) {
+            calcbit = (bits + 3) * (brp + 1);
+            if (calcbit == bitwidth) {
+                hit = 1;
+                break;
+            }
+        }
+    }
+
+    if (hit) {
+        btr = ((timing_pts[bits][1] << 20) & 0x00700000)
+              | ((timing_pts[bits][0] << 16) & 0x000F0000)
+              | ((psjw                << 24) & 0x0000C000)
+              | ((brp                 <<  0) & 0x000003FF);
+    } else {
+        btr = 0xFFFFFFFF;
+    }
+
+    return btr;
+
+}
+
+int can_frequency(can_t *obj, int f)
+{
+    int pclk = HAL_RCC_GetPCLK1Freq();
+    int btr = can_speed(pclk, (unsigned int)f, 1);
+    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
+
+    if (btr > 0) {
+        can->MCR |= CAN_MCR_INRQ ;
+        while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
+        }
+        can->BTR = btr;
+        can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
+        while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
+        }
+        return 1;
+    } else {
+        return 0;
+    }
+}
+
+int can_write(can_t *obj, CAN_Message msg, int cc)
+{
+    uint32_t  transmitmailbox = CAN_TXSTATUS_NOMAILBOX;
+    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
+
+    /* Select one empty transmit mailbox */
+    if ((can->TSR & CAN_TSR_TME0) == CAN_TSR_TME0) {
+        transmitmailbox = 0;
+    } else if ((can->TSR & CAN_TSR_TME1) == CAN_TSR_TME1) {
+        transmitmailbox = 1;
+    } else if ((can->TSR & CAN_TSR_TME2) == CAN_TSR_TME2) {
+        transmitmailbox = 2;
+    } else {
+      return 0;
+    }
+
+    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;
+
+    return 1;
+}
+
+int can_read(can_t *obj, CAN_Message *msg, int handle)
+{
+    //handle is the FIFO number
+
+    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
+
+    // check FPM0 which holds the pending message count in FIFO 0
+    // if no message is pending, return 0
+    if ((can->RF0R & CAN_RF0R_FMP0) == 0) {
+        return 0;
+    }
+
+    /* Get the Id */
+    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) >> 1);
+    /* Get the DLC */
+    msg->len = (uint8_t)0x0F & can->sFIFOMailBox[handle].RDTR;
+    /* Get the FMI */
+    // msg->FMI = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDTR >> 8);
+    /* Get the data field */
+    msg->data[0] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDLR;
+    msg->data[1] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 8);
+    msg->data[2] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 16);
+    msg->data[3] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 24);
+    msg->data[4] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDHR;
+    msg->data[5] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 8);
+    msg->data[6] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 16);
+    msg->data[7] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 24);
+
+    /* Release the FIFO */
+    if (handle == CAN_FIFO0) {
+        /* Release FIFO0 */
+        can->RF0R |= CAN_RF0R_RFOM0;
+    } else { /* FIFONumber == CAN_FIFO1 */
+        /* Release FIFO1 */
+        can->RF1R |= CAN_RF1R_RFOM1;
+    }
+
+    return 1;
+}
+
+void can_reset(can_t *obj)
+{
+    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
+    can->MCR |= CAN_MCR_RESET;
+    can->ESR = 0x0;
+}
+
+unsigned char can_rderror(can_t *obj)
+{
+    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
+    return (can->ESR >> 24) & 0xFF;
+}
+
+unsigned char can_tderror(can_t *obj)
+{
+    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
+    return (can->ESR >> 16) & 0xFF;
+}
+
+void can_monitor(can_t *obj, int silent)
+{
+    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
+
+    can->MCR |= CAN_MCR_INRQ ;
+    while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
+    }
+
+    if (silent) {
+        can->BTR |= ((uint32_t)1 << 31);
+    } else {
+        can->BTR &= ~((uint32_t)1 << 31);
+    }
+
+    can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
+    while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
+    }
+}
+
+int can_mode(can_t *obj, CanMode mode)
+{
+    int success = 0;
+    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
+
+    can->MCR |= CAN_MCR_INRQ ;
+    while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
+    }
+
+    switch (mode) {
+        case MODE_NORMAL:
+            can->BTR &= ~(CAN_BTR_SILM | CAN_BTR_LBKM);
+            success = 1;
+            break;
+        case MODE_SILENT:
+            can->BTR |= CAN_BTR_SILM;
+            can->BTR &= ~CAN_BTR_LBKM;
+            success = 1;
+            break;
+        case MODE_TEST_GLOBAL:
+        case MODE_TEST_LOCAL:
+            can->BTR |= CAN_BTR_LBKM;
+            can->BTR &= ~CAN_BTR_SILM;
+            success = 1;
+            break;
+        case MODE_TEST_SILENT:
+            can->BTR |= (CAN_BTR_SILM | CAN_BTR_LBKM);
+            success = 1;
+            break;
+        default:
+            success = 0;
+            break;
+    }
+
+    can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
+    while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
+    }
+
+    return success;
+}
+
+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);
+        CAN_FilterConfTypeDef  sFilterConfig;
+        sFilterConfig.FilterNumber = handle;
+        sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
+        sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
+
+        if (format == CANStandard) {
+            sFilterConfig.FilterIdHigh = id << 5;
+            sFilterConfig.FilterIdLow =  0x0;
+            sFilterConfig.FilterMaskIdHigh = mask << 5;
+            sFilterConfig.FilterMaskIdLow = 0x0; // allows both remote and data frames
+        } else if (format == CANExtended) {
+            sFilterConfig.FilterIdHigh = id >> 13; // EXTID[28:13]
+            sFilterConfig.FilterIdLow = (0x00FF & (id << 3)) | (1 << 2);  // EXTID[12:0]
+            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;
+    }
+    return retval;
+}
+
+static void can_irq(CANName name, int id)
+{
+    uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0;
+    CanHandle.Instance = (CAN_TypeDef *)name;
+
+    if (__HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_TME)) {
+        tmp1 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_0);
+        tmp2 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_1);
+        tmp3 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_2);
+        if (tmp1) {
+            __HAL_CAN_CLEAR_FLAG(&CanHandle, CAN_FLAG_RQCP0);
+        }
+        if (tmp2) {
+            __HAL_CAN_CLEAR_FLAG(&CanHandle, CAN_FLAG_RQCP1);
+        }
+        if (tmp3) {
+            __HAL_CAN_CLEAR_FLAG(&CanHandle, CAN_FLAG_RQCP2);
+        }
+        if (tmp1 || tmp2 || tmp3) {
+            irq_handler(can_irq_ids[id], IRQ_TX);
+        }
+    }
+
+    tmp1 = __HAL_CAN_MSG_PENDING(&CanHandle, CAN_FIFO0);
+    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_FMP0);
+
+    if ((tmp1 != 0) && tmp2) {
+        irq_handler(can_irq_ids[id], IRQ_RX);
+    }
+
+    tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_EPV);
+    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_EPV);
+    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);
+
+    if (tmp1 && tmp2 && tmp3) {
+        irq_handler(can_irq_ids[id], IRQ_PASSIVE);
+    }
+
+    tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_BOF);
+    tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_BOF);
+    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);
+    if (tmp1 && tmp2 && tmp3) {
+        irq_handler(can_irq_ids[id], IRQ_BUS);
+    }
+
+    tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR);
+    if (tmp1 && tmp2 && tmp3) {
+        irq_handler(can_irq_ids[id], IRQ_ERROR);
+    }
+}
+
+#if defined(TARGET_STM32F0)
+void CAN_IRQHandler(void)
+{
+    can_irq(CAN_1, 0);
+}
+#elif defined(TARGET_STM32F3)
+void CAN_RX0_IRQHandler(void)
+{
+    can_irq(CAN_1, 0);
+}
+void CAN_TX_IRQHandler(void)
+{
+    can_irq(CAN_1, 0);
+}
+void CAN_SCE_IRQHandler(void)
+{
+    can_irq(CAN_1, 0);
+}
+#else
+void CAN1_RX0_IRQHandler(void)
+{
+    can_irq(CAN_1, 0);
+}
+void CAN1_TX_IRQHandler(void)
+{
+    can_irq(CAN_1, 0);
+}
+void CAN1_SCE_IRQHandler(void)
+{
+    can_irq(CAN_1, 0);
+}
+#if defined(CAN2_BASE) && (CAN_NUM == 2)
+void CAN2_RX0_IRQHandler(void)
+{
+    can_irq(CAN_2, 1);
+}
+void CAN2_TX_IRQHandler(void)
+{
+    can_irq(CAN_2, 1);
+}
+void CAN2_SCE_IRQHandler(void)
+{
+    can_irq(CAN_2, 1);
+}
+#endif // defined(CAN2_BASE) && (CAN_NUM == 2)
+#endif // else
+
+void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
+{
+
+    CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
+    IRQn_Type irq_n = (IRQn_Type)0;
+    uint32_t vector = 0;
+    uint32_t ier;
+
+    if (obj->can == CAN_1) {
+        switch (type) {
+            case IRQ_RX:
+                ier = CAN_IT_FMP0;
+                irq_n = CAN1_IRQ_RX_IRQN;
+                vector = (uint32_t)&CAN1_IRQ_RX_VECT;
+                break;
+            case IRQ_TX:
+                ier = CAN_IT_TME;
+                irq_n = CAN1_IRQ_TX_IRQN;
+                vector = (uint32_t)&CAN1_IRQ_TX_VECT;
+                break;
+            case IRQ_ERROR:
+                ier = CAN_IT_ERR;
+                irq_n = CAN1_IRQ_ERROR_IRQN;
+                vector = (uint32_t)&CAN1_IRQ_ERROR_VECT;
+                break;
+            case IRQ_PASSIVE:
+                ier = CAN_IT_EPV;
+                irq_n = CAN1_IRQ_PASSIVE_IRQN;
+                vector = (uint32_t)&CAN1_IRQ_PASSIVE_VECT;
+                break;
+            case IRQ_BUS:
+                ier = CAN_IT_BOF;
+                irq_n = CAN1_IRQ_BUS_IRQN;
+                vector = (uint32_t)&CAN1_IRQ_BUS_VECT;
+                break;
+            default:
+                return;
+        }
+    }
+#if defined(CAN2_BASE) && (CAN_NUM == 2)
+    else if (obj->can == CAN_2) {
+        switch (type) {
+            case IRQ_RX:
+                ier = CAN_IT_FMP0;
+                irq_n = CAN2_IRQ_RX_IRQN;
+                vector = (uint32_t)&CAN2_IRQ_RX_VECT;
+                break;
+            case IRQ_TX:
+                ier = CAN_IT_TME;
+                irq_n = CAN2_IRQ_TX_IRQN;
+                vector = (uint32_t)&CAN2_IRQ_TX_VECT;
+                break;
+            case IRQ_ERROR:
+                ier = CAN_IT_ERR;
+                irq_n = CAN2_IRQ_ERROR_IRQN;
+                vector = (uint32_t)&CAN2_IRQ_ERROR_VECT;
+                break;
+            case IRQ_PASSIVE:
+                ier = CAN_IT_EPV;
+                irq_n = CAN2_IRQ_PASSIVE_IRQN;
+                vector = (uint32_t)&CAN2_IRQ_PASSIVE_VECT;
+                break;
+            case IRQ_BUS:
+                ier = CAN_IT_BOF;
+                irq_n = CAN2_IRQ_BUS_IRQN;
+                vector = (uint32_t)&CAN2_IRQ_BUS_VECT;
+                break;
+            default:
+                return;
+        }
+    }
+#endif
+    else {
+        return;
+    }
+
+    if (enable) {
+        can->IER |= ier;
+    } else {
+        can->IER &= ~ier;
+    }
+
+    NVIC_SetVector(irq_n, vector);
+    NVIC_EnableIRQ(irq_n);
+}
+
+#endif // DEVICE_CAN
+
--- a/targets/TARGET_STM/gpio_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/gpio_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -31,8 +31,86 @@
 #include "gpio_api.h"
 #include "pinmap.h"
 #include "mbed_error.h"
+#include "pin_device.h"
 
-extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
+extern const uint32_t ll_pin_defines[16];
+
+// Enable GPIO clock and return GPIO base address
+GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx) {
+    uint32_t gpio_add = 0;
+    switch (port_idx) {
+        case PortA:
+            gpio_add = GPIOA_BASE;
+            __GPIOA_CLK_ENABLE();
+            break;
+        case PortB:
+            gpio_add = GPIOB_BASE;
+            __GPIOB_CLK_ENABLE();
+            break;
+#if defined(GPIOC_BASE)
+        case PortC:
+            gpio_add = GPIOC_BASE;
+            __GPIOC_CLK_ENABLE();
+            break;
+#endif
+#if defined GPIOD_BASE
+        case PortD:
+            gpio_add = GPIOD_BASE;
+            __GPIOD_CLK_ENABLE();
+            break;
+#endif
+#if defined GPIOE_BASE
+        case PortE:
+            gpio_add = GPIOE_BASE;
+            __GPIOE_CLK_ENABLE();
+            break;
+#endif
+#if defined GPIOF_BASE
+        case PortF:
+            gpio_add = GPIOF_BASE;
+            __GPIOF_CLK_ENABLE();
+            break;
+#endif
+#if defined GPIOG_BASE
+        case PortG:
+#if defined TARGET_STM32L4
+            __HAL_RCC_PWR_CLK_ENABLE();
+            HAL_PWREx_EnableVddIO2();
+#endif
+            gpio_add = GPIOG_BASE;
+            __GPIOG_CLK_ENABLE();
+            break;
+#endif
+#if defined GPIOH_BASE
+        case PortH:
+            gpio_add = GPIOH_BASE;
+            __GPIOH_CLK_ENABLE();
+            break;
+#endif
+#if defined GPIOI_BASE
+        case PortI:
+            gpio_add = GPIOI_BASE;
+            __GPIOI_CLK_ENABLE();
+            break;
+#endif
+#if defined GPIOJ_BASE
+        case PortJ:
+            gpio_add = GPIOJ_BASE;
+            __GPIOJ_CLK_ENABLE();
+            break;
+#endif
+#if defined GPIOK_BASE
+        case PortK:
+            gpio_add = GPIOK_BASE;
+            __GPIOK_CLK_ENABLE();
+            break;
+#endif
+        default:
+            error("Pinmap error: wrong port number.");
+            break;
+    }
+    return (GPIO_TypeDef *) gpio_add;
+}
 
 uint32_t gpio_set(PinName pin) {
     MBED_ASSERT(pin != (PinName)NC);
@@ -42,6 +120,7 @@
     return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask
 }
 
+
 void gpio_init(gpio_t *obj, PinName pin) {
     obj->pin = pin;
     if (pin == (PinName)NC) {
@@ -51,11 +130,12 @@
     uint32_t port_index = STM_PORT(pin);
 
     // Enable GPIO clock
-    uint32_t gpio_add = Set_GPIO_Clock(port_index);
-    GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
+    GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index);
 
     // Fill GPIO object structure for future use
     obj->mask    = gpio_set(pin);
+    obj->gpio  = gpio;
+    obj->ll_pin  = ll_pin_defines[STM_PIN(obj->pin)];
     obj->reg_in  = &gpio->IDR;
     obj->reg_set = &gpio->BSRR;
 #ifdef GPIO_IP_WITHOUT_BRR
@@ -69,11 +149,11 @@
     pin_mode(obj->pin, mode);
 }
 
-void gpio_dir(gpio_t *obj, PinDirection direction) {
-    MBED_ASSERT(obj->pin != (PinName)NC);
-    if (direction == PIN_OUTPUT) {
-        pin_function(obj->pin, STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
-    } else { // PIN_INPUT
-        pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
+inline void gpio_dir(gpio_t *obj, PinDirection direction) {
+    if (direction == PIN_INPUT) {
+        LL_GPIO_SetPinMode(obj->gpio, obj->ll_pin, LL_GPIO_MODE_INPUT);
+    } else {
+        LL_GPIO_SetPinMode(obj->gpio, obj->ll_pin, LL_GPIO_MODE_OUTPUT);
     }
 }
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/gpio_irq_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,297 @@
+/* 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 <stddef.h>
+#include "cmsis.h"
+#include "gpio_irq_api.h"
+#include "pinmap.h"
+#include "mbed_error.h"
+#include "gpio_irq_device.h"
+
+#define EDGE_NONE (0)
+#define EDGE_RISE (1)
+#define EDGE_FALL (2)
+#define EDGE_BOTH (3)
+
+
+typedef struct gpio_channel {
+    uint32_t pin_mask;                   // bitmask representing which pins are configured for receiving interrupts
+    uint32_t channel_ids[MAX_PIN_LINE];  // mbed "gpio_irq_t gpio_irq" field of instance
+    GPIO_TypeDef* channel_gpio[MAX_PIN_LINE]; // base address of gpio port group
+    uint32_t channel_pin[MAX_PIN_LINE];  // pin number in port group
+} gpio_channel_t;
+
+static gpio_irq_handler irq_handler;
+
+static gpio_channel_t channels[CHANNEL_NUM] = {
+#ifdef EXTI_IRQ0_NUM_LINES
+    {.pin_mask = 0},
+#endif
+#ifdef EXTI_IRQ1_NUM_LINES
+    {.pin_mask = 0},
+#endif
+#ifdef EXTI_IRQ2_NUM_LINES
+    {.pin_mask = 0},
+#endif
+#ifdef EXTI_IRQ3_NUM_LINES
+    {.pin_mask = 0},
+#endif
+#ifdef EXTI_IRQ4_NUM_LINES
+    {.pin_mask = 0},
+#endif
+#ifdef EXTI_IRQ5_NUM_LINES
+    {.pin_mask = 0},
+#endif
+#ifdef EXTI_IRQ6_NUM_LINES
+    {.pin_mask = 0}
+#endif
+};
+
+static void handle_interrupt_in(uint32_t irq_index, uint32_t max_num_pin_line)
+{
+    gpio_channel_t *gpio_channel = &channels[irq_index];
+    uint32_t gpio_idx;
+
+    for (gpio_idx = 0; gpio_idx < max_num_pin_line; gpio_idx++) {
+        uint32_t current_mask = (1 << gpio_idx);
+
+        if (gpio_channel->pin_mask & current_mask) {
+            // Retrieve the gpio and pin that generate the irq
+            GPIO_TypeDef *gpio = (GPIO_TypeDef *)(gpio_channel->channel_gpio[gpio_idx]);
+            uint32_t pin = (uint32_t)(1 << (gpio_channel->channel_pin[gpio_idx]));
+
+            // Clear interrupt flag
+            if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) {
+                __HAL_GPIO_EXTI_CLEAR_FLAG(pin);
+
+                if (gpio_channel->channel_ids[gpio_idx] == 0)
+                    continue;
+
+                // Check which edge has generated the irq
+                if ((gpio->IDR & pin) == 0) {
+                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_FALL);
+                } else {
+                    irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_RISE);
+                }
+            }
+        }
+    }
+}
+
+
+#ifdef EXTI_IRQ0_NUM_LINES
+// EXTI line 0
+static void gpio_irq0(void)
+{
+    handle_interrupt_in(0, EXTI_IRQ0_NUM_LINES);
+}
+#endif
+#ifdef EXTI_IRQ1_NUM_LINES
+// EXTI line 1
+static void gpio_irq1(void)
+{
+    handle_interrupt_in(1, EXTI_IRQ1_NUM_LINES);
+}
+#endif
+#ifdef EXTI_IRQ2_NUM_LINES
+// EXTI line 2
+static void gpio_irq2(void)
+{
+    handle_interrupt_in(2, EXTI_IRQ2_NUM_LINES);
+}
+#endif
+#ifdef EXTI_IRQ3_NUM_LINES
+// EXTI line 3
+static void gpio_irq3(void)
+{
+    handle_interrupt_in(3, EXTI_IRQ3_NUM_LINES);
+}
+#endif
+#ifdef EXTI_IRQ4_NUM_LINES
+// EXTI line 4
+static void gpio_irq4(void)
+{
+    handle_interrupt_in(4, EXTI_IRQ4_NUM_LINES);
+}
+#endif
+#ifdef EXTI_IRQ5_NUM_LINES
+// EXTI lines 5 to 9
+static void gpio_irq5(void)
+{
+    handle_interrupt_in(5, EXTI_IRQ5_NUM_LINES);
+}
+#endif
+#ifdef EXTI_IRQ6_NUM_LINES
+// EXTI lines 10 to 15
+static void gpio_irq6(void)
+{
+    handle_interrupt_in(6, EXTI_IRQ6_NUM_LINES);
+}
+#endif
+
+extern GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx);
+extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
+
+int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
+{
+    uint32_t vector = 0;
+    uint32_t irq_index;
+    gpio_channel_t *gpio_channel;
+    uint32_t gpio_idx;
+
+    if (pin == NC) return -1;
+
+    /* Enable SYSCFG Clock */
+    __HAL_RCC_SYSCFG_CLK_ENABLE();
+
+    uint32_t port_index  = STM_PORT(pin);
+    uint32_t pin_index  = STM_PIN(pin);
+    irq_index =  pin_lines_desc[pin_index].irq_index;
+
+    switch (irq_index) {
+#ifdef EXTI_IRQ0_NUM_LINES
+        case 0:
+            vector = (uint32_t)&gpio_irq0;
+            break;
+#endif
+#ifdef EXTI_IRQ1_NUM_LINES
+        case 1:
+            vector = (uint32_t)&gpio_irq1;
+            break;
+#endif
+#ifdef EXTI_IRQ2_NUM_LINES
+        case 2:
+            vector = (uint32_t)&gpio_irq2;
+            break;
+#endif
+#ifdef EXTI_IRQ3_NUM_LINES
+        case 3:
+            vector = (uint32_t)&gpio_irq3;
+            break;
+#endif
+#ifdef EXTI_IRQ4_NUM_LINES
+        case 4:
+            vector = (uint32_t)&gpio_irq4;
+            break;
+#endif
+#ifdef EXTI_IRQ5_NUM_LINES
+        case 5:
+            vector = (uint32_t)&gpio_irq5;
+            break;
+#endif
+#ifdef EXTI_IRQ6_NUM_LINES
+        case 6:
+            vector = (uint32_t)&gpio_irq6;
+            break;
+#endif
+        default:
+            error("InterruptIn error: pin not supported.\n");
+            return -1;
+    }
+
+    // Enable GPIO clock
+    GPIO_TypeDef *gpio_add = Set_GPIO_Clock(port_index);
+
+    // Save informations for future use
+    obj->irq_n = pin_lines_desc[pin_index].irq_n;
+    obj->irq_index =  pin_lines_desc[pin_index].irq_index;
+    obj->event = EDGE_NONE;
+    obj->pin = pin;
+
+    gpio_channel = &channels[irq_index];
+    gpio_idx = pin_lines_desc[pin_index].gpio_idx;
+    gpio_channel->pin_mask |= (1 << gpio_idx);
+    gpio_channel->channel_ids[gpio_idx] = id;
+    gpio_channel->channel_gpio[gpio_idx] = gpio_add;
+    gpio_channel->channel_pin[gpio_idx] = pin_index;
+
+    irq_handler = handler;
+
+    // Enable EXTI interrupt
+    NVIC_SetVector(obj->irq_n, vector);
+    gpio_irq_enable(obj);
+
+    return 0;
+}
+
+void gpio_irq_free(gpio_irq_t *obj)
+{
+    uint32_t gpio_idx = pin_lines_desc[STM_PIN(obj->pin)].gpio_idx;
+    gpio_channel_t *gpio_channel = &channels[obj->irq_index];
+
+    gpio_irq_disable(obj);
+    gpio_channel->pin_mask &= ~(1 << gpio_idx);
+    gpio_channel->channel_ids[gpio_idx] = 0;
+    gpio_channel->channel_gpio[gpio_idx] = 0;
+    gpio_channel->channel_pin[gpio_idx] = 0;
+}
+
+void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
+{
+    if (event == IRQ_RISE) {
+        if (enable) {
+            LL_EXTI_EnableRisingTrig_0_31(1 << STM_PIN(obj->pin));
+        } else {
+            LL_EXTI_DisableRisingTrig_0_31(1 << STM_PIN(obj->pin));
+        }
+    }
+    if (event == IRQ_FALL) {
+        if (enable) {
+            LL_EXTI_EnableFallingTrig_0_31(1 << STM_PIN(obj->pin));
+        } else {
+            LL_EXTI_DisableFallingTrig_0_31(1 << STM_PIN(obj->pin));
+        }
+    }
+}
+
+void gpio_irq_enable(gpio_irq_t *obj)
+{
+    uint32_t temp = 0;
+    uint32_t port_index = STM_PORT(obj->pin);
+    uint32_t pin_index  = STM_PIN(obj->pin);
+
+    /*  Select Source  */
+    temp = SYSCFG->EXTICR[pin_index >> 2];
+    CLEAR_BIT(temp, (0x0FU) << (4U * (pin_index & 0x03U)));
+    SET_BIT(temp, port_index << (4U * (pin_index & 0x03U)));
+    SYSCFG->EXTICR[pin_index >> 2] = temp;
+
+    LL_EXTI_EnableIT_0_31(1 << pin_index);
+
+    NVIC_EnableIRQ(obj->irq_n);
+}
+
+void gpio_irq_disable(gpio_irq_t *obj)
+{
+    /* Clear EXTI line configuration */
+    LL_EXTI_DisableIT_0_31(1 << STM_PIN(obj->pin));
+    NVIC_DisableIRQ(obj->irq_n);
+    NVIC_ClearPendingIRQ(obj->irq_n);
+    obj->event = EDGE_NONE;
+}
--- a/targets/TARGET_STM/gpio_object.h	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/gpio_object.h	Tue Mar 14 16:40:56 2017 +0000
@@ -43,19 +43,20 @@
 /*
  * 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 {
-    PinName  pin;
     uint32_t mask;
     __IO uint32_t *reg_in;
     __IO uint32_t *reg_set;
     __IO uint32_t *reg_clr;
+    PinName  pin;
+    GPIO_TypeDef *gpio;
+    uint32_t ll_pin;
 } gpio_t;
 
 static inline void gpio_write(gpio_t *obj, int value)
 {
-    MBED_ASSERT(obj->pin != (PinName)NC);
     if (value) {
         *obj->reg_set = obj->mask;
     } else {
@@ -69,7 +70,6 @@
 
 static inline int gpio_read(gpio_t *obj)
 {
-    MBED_ASSERT(obj->pin != (PinName)NC);
     return ((*obj->reg_in & obj->mask) ? 1 : 0);
 }
 
@@ -78,6 +78,7 @@
     return obj->pin != (PinName)NC;
 }
 
+
 #ifdef __cplusplus
 }
 #endif
--- a/targets/TARGET_STM/hal_tick_32b.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/hal_tick_32b.c	Tue Mar 14 16:40:56 2017 +0000
@@ -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 @@
     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
     }
 
--- a/targets/TARGET_STM/i2c_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/i2c_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -31,7 +31,7 @@
 
 #include "mbed_assert.h"
 #include "i2c_api.h"
-#include "platform/wait_api.h"
+#include "platform/mbed_wait_api.h"
 
 #if DEVICE_I2C
 
@@ -276,8 +276,8 @@
         // Configure I2C pins
         pinmap_pinout(sda, PinMap_I2C_SDA);
         pinmap_pinout(scl, PinMap_I2C_SCL);
-        pin_mode(sda, PullUp);
-        pin_mode(scl, PullUp);
+        pin_mode(sda, OpenDrainPullUp);
+        pin_mode(scl, OpenDrainPullUp);
         obj_s->event_i2cIRQ = I2C1_EV_IRQn;
         obj_s->error_i2cIRQ = I2C1_ER_IRQn;
     }
@@ -290,8 +290,8 @@
         // Configure I2C pins
         pinmap_pinout(sda, PinMap_I2C_SDA);
         pinmap_pinout(scl, PinMap_I2C_SCL);
-        pin_mode(sda, PullUp);
-        pin_mode(scl, PullUp);
+        pin_mode(sda, OpenDrainPullUp);
+        pin_mode(scl, OpenDrainPullUp);
         obj_s->event_i2cIRQ = I2C2_EV_IRQn;
         obj_s->error_i2cIRQ = I2C2_ER_IRQn;
     }
@@ -304,8 +304,8 @@
         // Configure I2C pins
         pinmap_pinout(sda, PinMap_I2C_SDA);
         pinmap_pinout(scl, PinMap_I2C_SCL);
-        pin_mode(sda, PullUp);
-        pin_mode(scl, PullUp);
+        pin_mode(sda, OpenDrainPullUp);
+        pin_mode(scl, OpenDrainPullUp);
         obj_s->event_i2cIRQ = I2C3_EV_IRQn;
         obj_s->error_i2cIRQ = I2C3_ER_IRQn;
     }
@@ -318,8 +318,8 @@
         // Configure I2C pins
         pinmap_pinout(sda, PinMap_I2C_SDA);
         pinmap_pinout(scl, PinMap_I2C_SCL);
-        pin_mode(sda, PullUp);
-        pin_mode(scl, PullUp);
+        pin_mode(sda, OpenDrainPullUp);
+        pin_mode(scl, OpenDrainPullUp);
         obj_s->event_i2cIRQ = I2C4_EV_IRQn;
         obj_s->error_i2cIRQ = I2C4_ER_IRQn;
     }
@@ -332,8 +332,8 @@
         // Configure I2C pins
         pinmap_pinout(sda, PinMap_I2C_SDA);
         pinmap_pinout(scl, PinMap_I2C_SCL);
-        pin_mode(sda, PullUp);
-        pin_mode(scl, PullUp);
+        pin_mode(sda, OpenDrainPullUp);
+        pin_mode(scl, OpenDrainPullUp);
         obj_s->event_i2cIRQ = FMPI2C1_EV_IRQn;
         obj_s->error_i2cIRQ = FMPI2C1_ER_IRQn;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/pinmap.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,157 @@
+/* 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 "mbed_assert.h"
+#include "pinmap.h"
+#include "PortNames.h"
+#include "mbed_error.h"
+#include "pin_device.h"
+
+extern GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx);
+
+const uint32_t ll_pin_defines[16] = {
+    LL_GPIO_PIN_0,
+    LL_GPIO_PIN_1,
+    LL_GPIO_PIN_2,
+    LL_GPIO_PIN_3,
+    LL_GPIO_PIN_4,
+    LL_GPIO_PIN_5,
+    LL_GPIO_PIN_6,
+    LL_GPIO_PIN_7,
+    LL_GPIO_PIN_8,
+    LL_GPIO_PIN_9,
+    LL_GPIO_PIN_10,
+    LL_GPIO_PIN_11,
+    LL_GPIO_PIN_12,
+    LL_GPIO_PIN_13,
+    LL_GPIO_PIN_14,
+    LL_GPIO_PIN_15
+};
+
+/**
+ * Configure pin (mode, speed, output type and pull-up/pull-down)
+ */
+void pin_function(PinName pin, int data)
+{
+    MBED_ASSERT(pin != (PinName)NC);
+
+    // Get the pin informations
+    uint32_t mode  = STM_PIN_FUNCTION(data);
+    uint32_t afnum = STM_PIN_AFNUM(data);
+    uint32_t port = STM_PORT(pin);
+    uint32_t ll_pin  = ll_pin_defines[STM_PIN(pin)];
+    uint32_t ll_mode = 0;
+
+    // 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;
+            break;
+        case STM_PIN_OUTPUT:
+            ll_mode = LL_GPIO_MODE_OUTPUT;
+            break;
+        case STM_PIN_ALTERNATE:
+            ll_mode = LL_GPIO_MODE_ALTERNATE;
+            // In case of ALT function, also set he afnum
+           stm_pin_SetAFPin(gpio, pin, afnum);
+            break;
+        case STM_PIN_ANALOG:
+            ll_mode = LL_GPIO_MODE_ANALOG;
+            break;
+        default:
+            MBED_ASSERT(0);
+            break;
+    }
+    LL_GPIO_SetPinMode(gpio, ll_pin, ll_mode);
+
+#if defined(GPIO_ASCR_ASC0)
+    /*  For families where Analog Control ASC0 register is present */
+    if (STM_PIN_ANALOG_CONTROL(data)) {
+        LL_GPIO_EnablePinAnalogControl(gpio, ll_pin);
+    } else {
+        LL_GPIO_DisablePinAnalogControl(gpio, ll_pin);
+    }
+#endif
+
+    /*  For now by default use Speed HIGH for output or alt modes */
+    if ((mode == STM_PIN_OUTPUT) ||(mode == STM_PIN_ALTERNATE)) {
+    if (STM_PIN_OD(data)) {
+            LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_OPENDRAIN);
+    } else {
+            LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
+    }
+    }
+
+    stm_pin_PullConfig(gpio, ll_pin, STM_PIN_PUPD(data));
+
+    stm_pin_DisconnectDebug(pin);
+}
+
+/**
+ * Configure pin pull-up/pull-down
+ */
+void pin_mode(PinName pin, PinMode mode)
+{
+    MBED_ASSERT(pin != (PinName)NC);
+
+    uint32_t port_index = STM_PORT(pin);
+    uint32_t ll_pin  = ll_pin_defines[STM_PIN(pin)];
+    // Enable GPIO clock
+    GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index);
+    uint32_t function = LL_GPIO_GetPinMode(gpio, ll_pin);
+
+    if ((function == LL_GPIO_MODE_OUTPUT) || (function == LL_GPIO_MODE_ALTERNATE))
+    {
+        if ((mode == OpenDrainNoPull) || (mode == OpenDrainPullUp) || (mode == OpenDrainPullDown)) {
+            LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_OPENDRAIN);
+        } else {
+            LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
+        }
+    }
+
+    if ((mode == OpenDrainPullUp) || (mode == PullUp)) {
+        stm_pin_PullConfig(gpio, ll_pin, GPIO_PULLUP);
+    } else if ((mode == OpenDrainPullDown) || (mode == PullDown)) {
+        stm_pin_PullConfig(gpio, ll_pin, GPIO_PULLDOWN);
+    } else {
+        stm_pin_PullConfig(gpio, ll_pin, GPIO_NOPULL);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/port_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,102 @@
+/* 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 "port_api.h"
+#include "pinmap.h"
+#include "gpio_api.h"
+#include "mbed_error.h"
+
+#if DEVICE_PORTIN || DEVICE_PORTOUT
+
+extern GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx);
+
+// high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
+// low nibble  = pin number
+PinName port_pin(PortName port, int pin_n)
+{
+    return (PinName)(pin_n + (port << 4));
+}
+
+void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
+{
+    uint32_t port_index = (uint32_t)port;
+
+    // Enable GPIO clock
+    GPIO_TypeDef *gpio =  Set_GPIO_Clock(port_index);
+
+    // Fill PORT object structure for future use
+    obj->port      = port;
+    obj->mask      = mask;
+    obj->direction = dir;
+    obj->reg_in    = &gpio->IDR;
+    obj->reg_out   = &gpio->ODR;
+
+    port_dir(obj, dir);
+}
+
+void port_dir(port_t *obj, PinDirection dir)
+{
+    uint32_t i;
+    obj->direction = dir;
+    for (i = 0; i < 16; i++) { // Process all pins
+        if (obj->mask & (1 << i)) { // If the pin is used
+            if (dir == PIN_OUTPUT) {
+                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
+            } else { // PIN_INPUT
+                pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
+            }
+        }
+    }
+}
+
+void port_mode(port_t *obj, PinMode mode)
+{
+    uint32_t i;
+    for (i = 0; i < 16; i++) { // Process all pins
+        if (obj->mask & (1 << i)) { // If the pin is used
+            pin_mode(port_pin(obj->port, i), mode);
+        }
+    }
+}
+
+void port_write(port_t *obj, int value)
+{
+    *obj->reg_out = (*obj->reg_out & ~obj->mask) | (value & obj->mask);
+}
+
+int port_read(port_t *obj)
+{
+    if (obj->direction == PIN_OUTPUT) {
+        return (*obj->reg_out & obj->mask);
+    } else { // PIN_INPUT
+        return (*obj->reg_in & obj->mask);
+    }
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/TARGET_STM/pwmout_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -0,0 +1,343 @@
+/* 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"
+#include "pwmout_device.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
+#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);
+
+    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;
+#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:
+            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 !defined(PWMOUT_INVERTED_NOT_SUPPORTED)
+    if (obj->inverted) {
+        HAL_TIMEx_PWMN_Start(&TimHandle, channel);
+    } else
+#endif
+    {
+        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 = 0;
+    uint32_t APBxCLKDivider = RCC_HCLK_DIV1;
+    float dc = pwmout_read(obj);
+    uint8_t i = 0;
+
+    __HAL_TIM_DISABLE(&TimHandle);
+
+    // Get clock configuration
+    // Note: PclkFreq contains here the Latency (not used after)
+    HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq);
+
+    /*  Parse the pwm / apb mapping table to find the right entry */
+    while(pwm_apb_map_table[i].pwm != obj->pwm) {
+        i++;
+    }
+
+    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 {
+#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 = (((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;
+        /*  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;
+    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
--- a/targets/TARGET_STM/rtc_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/rtc_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -82,9 +82,7 @@
     if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
         error("PeriphClkInitStruct RTC failed with LSE\n");
     }
-
 #else /* !RTC_LSI */
-
     __PWR_CLK_ENABLE();
 
     // Reset Backup domain
@@ -108,7 +106,6 @@
     if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
         error("PeriphClkInitStruct RTC failed with LSI\n");
     }
-
 #endif /* !RTC_LSI */
 
     // Enable RTC
@@ -173,11 +170,23 @@
 }
 
 /*
- RTC Registers
-   RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday
-   RTC_Month   1=january, 2=february, ..., 12=december
-   RTC_Date    day of the month 1-31
-   RTC_Year    year 0-99
+ ST RTC_DateTypeDef structure
+   WeekDay 1=monday, 2=tuesday, ..., 7=sunday
+   Month   0x1=january, 0x2=february, ..., 0x12=december
+   Date    day of the month 1-31
+   Year    year 0-99
+
+ ST RTC_TimeTypeDef structure
+  Hours           0-12 if the RTC_HourFormat_12 is selected during init
+                  0-23 if the RTC_HourFormat_24 is selected during init
+  Minutes         0-59
+  Seconds         0-59
+  TimeFormat      RTC_HOURFORMAT12_AM/RTC_HOURFORMAT12_PM
+  SubSeconds      time unit range between [0-1] Second with [1 Sec / SecondFraction +1] granularity
+  SecondFraction  range or granularity of Sub Second register content corresponding to Synchronous pre-scaler factor value (PREDIV_S)
+  DayLightSaving  RTC_DAYLIGHTSAVING_SUB1H/RTC_DAYLIGHTSAVING_ADD1H/RTC_DAYLIGHTSAVING_NONE
+  StoreOperation  RTC_STOREOPERATION_RESET/RTC_STOREOPERATION_SET
+
  struct tm
    tm_sec      seconds after the minute 0-61
    tm_min      minutes after the hour 0-59
@@ -189,6 +198,22 @@
    tm_yday     days since January 1 0-365
    tm_isdst    Daylight Saving Time flag
 */
+
+/*
+Information about STM32F0, STM32F2, STM32F3, STM32F4, STM32F7, STM32L0, STM32L1, STM32L4:
+BCD format is used to store the date in the RTC. The year is store on 2 * 4 bits.
+Because the first year is reserved to see if the RTC is init, the supposed range is 01-99.
+1st point is to cover the standard range from 1970 to 2038 (limited by the 32 bits of time_t).
+2nd point is to keep the year 1970 and the leap years synchronized.
+
+So by moving it 68 years forward from 1970, it become 1969-2067 which include 1970-2038.
+68 is also a multiple of 4 so it let the leap year synchronized.
+
+Information about STM32F1:
+32bit register is used (no BCD format) for the seconds and a software structure to store dates.
+It is then not a problem to not use shifts.
+*/
+
 time_t rtc_read(void)
 {
     RTC_DateTypeDef dateStruct;
@@ -199,11 +224,11 @@
 
     // Read actual date and time
     // Warning: the time must be read first!
-    HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
-    HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN);
+    HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);
+    HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);
 
     // Setup a tm structure based on the RTC
-    timeinfo.tm_wday = dateStruct.WeekDay;
+    /* tm_wday information is ignored by mktime */
     timeinfo.tm_mon  = dateStruct.Month - 1;
     timeinfo.tm_mday = dateStruct.Date;
     timeinfo.tm_year = dateStruct.Year + 68;
@@ -230,7 +255,11 @@
     struct tm *timeinfo = localtime(&t);
 
     // Fill RTC structures
-    dateStruct.WeekDay        = timeinfo->tm_wday;
+    if (timeinfo->tm_wday == 0) {
+        dateStruct.WeekDay    = 7;
+    } else {
+        dateStruct.WeekDay    = timeinfo->tm_wday;
+    }
     dateStruct.Month          = timeinfo->tm_mon + 1;
     dateStruct.Date           = timeinfo->tm_mday;
     dateStruct.Year           = timeinfo->tm_year - 68;
@@ -245,21 +274,17 @@
 #endif /* TARGET_STM32F1 */
 
     // Change the RTC current date/time
-    HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN);
-    HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
+    HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);
+    HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);
 }
 
 int rtc_isenabled(void)
 {
-#if DEVICE_LOWPOWERTIMER
-    if ((RTC->ISR & RTC_ISR_INITS) ==  RTC_ISR_INITS) {
-        return 1;
-    } else {
-        return 0;
-    }
-#else /* DEVICE_LOWPOWERTIMER */
-    return 1;
-#endif /* DEVICE_LOWPOWERTIMER */
+#if !(TARGET_STM32F1)
+    return ( ((RTC->ISR & RTC_ISR_INITS) ==  RTC_ISR_INITS) && ((RTC->ISR & RTC_ISR_RSF) ==  RTC_ISR_RSF) );
+#else /* TARGET_STM32F1 */
+    return ((RTC->CRL & RTC_CRL_RSF) ==  RTC_CRL_RSF);
+#endif /* TARGET_STM32F1 */
 }
 
 #if DEVICE_LOWPOWERTIMER
@@ -308,5 +333,4 @@
 }
 #endif /* DEVICE_LOWPOWERTIMER */
 
-
 #endif /* DEVICE_RTC */
--- a/targets/TARGET_STM/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -27,52 +27,68 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *******************************************************************************
  */
-#include "sleep_api.h"
-#include "rtc_api_hal.h"
-
 #if DEVICE_SLEEP
 
 #include "cmsis.h"
-
+#include "us_ticker_api.h"
+#include "sleep_api.h"
+#include "rtc_api_hal.h"
+#include "hal_tick.h"
 
-void sleep(void)
+extern void HAL_SuspendTick(void);
+extern void HAL_ResumeTick(void);
+
+void hal_sleep(void)
 {
-    // Stop HAL systick
+    // Stop HAL tick to avoid to exit sleep in 1ms
     HAL_SuspendTick();
     // Request to enter SLEEP mode
     HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
-    // Restart HAL systick
+
+    // Restart HAL tick
     HAL_ResumeTick();
 }
 
-void deepsleep(void)
+void hal_deepsleep(void)
 {
-    // Stop HAL systick
+    // Stop HAL tick
     HAL_SuspendTick();
+    uint32_t EnterTimeUS = us_ticker_read();
 
     // Request to enter STOP mode with regulator in low power mode
 #if TARGET_STM32L4
-    if (__HAL_RCC_PWR_IS_CLK_ENABLED()) {
-        HAL_PWREx_EnableLowPowerRunMode();
-        HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);
+    int pwrClockEnabled = __HAL_RCC_PWR_IS_CLK_ENABLED();
+    int lowPowerModeEnabled = PWR->CR1 & PWR_CR1_LPR;
+    
+    if (!pwrClockEnabled) {
+        __HAL_RCC_PWR_CLK_ENABLE();
+    }
+    if (lowPowerModeEnabled) {
         HAL_PWREx_DisableLowPowerRunMode();
-    } else {
-        __HAL_RCC_PWR_CLK_ENABLE();
+    }
+    
+    HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);
+    
+    if (lowPowerModeEnabled) {
         HAL_PWREx_EnableLowPowerRunMode();
-        HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);
-        HAL_PWREx_DisableLowPowerRunMode();
+    }
+    if (!pwrClockEnabled) {
         __HAL_RCC_PWR_CLK_DISABLE();
     }
 #else /* TARGET_STM32L4 */
     HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
 #endif /* TARGET_STM32L4 */
 
-    // Restart HAL systick
+    // Restart HAL tick
     HAL_ResumeTick();
 
     // After wake-up from STOP reconfigure the PLL
     SetSysClock();
 
+    TIM_HandleTypeDef TimMasterHandle;
+    TimMasterHandle.Instance = TIM_MST;
+    __HAL_TIM_SET_COUNTER(&TimMasterHandle, EnterTimeUS);
+
 #if DEVICE_LOWPOWERTIMER
     rtc_synchronize();
 #endif
--- a/targets/TARGET_STM/stm_spi_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/stm_spi_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -308,7 +308,7 @@
 
     /*  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);
--- a/targets/TARGET_Silicon_Labs/TARGET_EFM32/lp_ticker.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_Silicon_Labs/TARGET_EFM32/lp_ticker.c	Tue Mar 14 16:40:56 2017 +0000
@@ -29,7 +29,7 @@
 #include "rtc_api_HAL.h"
 #include "lp_ticker_api.h"
 
-#include "critical.h"
+#include "mbed_critical.h"
 #if (defined RTCC_COUNT) && (RTCC_COUNT > 0)
 #include "em_rtcc.h"
 #endif
--- a/targets/TARGET_Silicon_Labs/TARGET_EFM32/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_Silicon_Labs/TARGET_EFM32/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -27,7 +27,7 @@
 #include "sleep_api.h"
 #include "sleepmodes.h"
 #include "em_emu.h"
-#include "critical.h"
+#include "mbed_critical.h"
 
 uint32_t sleep_block_counter[NUM_SLEEP_MODES] = {0};
 
@@ -35,7 +35,7 @@
  * Sleep mode.
  * Enter the lowest possible sleep mode that is not blocked by ongoing activity.
  */
-void sleep(void)
+void hal_sleep(void)
 {
     if (sleep_block_counter[0] > 0) {
         /* Blocked everything below EM0, so just return */
@@ -64,7 +64,7 @@
  * consumption as low as 1.1 μA with RTC enabled. Power-on Reset, Brown-out
  * Detection and full RAM and CPU retention is also included.
  */
-void deepsleep(void)
+void hal_deepsleep(void)
 {
     EMU_EnterEM2(true);
 }
--- a/targets/TARGET_WIZNET/TARGET_W7500x/analogin_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_WIZNET/TARGET_W7500x/analogin_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -33,7 +33,7 @@
 
 #if DEVICE_ANALOGIN
 
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "cmsis.h"
 #include "pinmap.h"
 #include "W7500x_adc.h"
--- a/targets/TARGET_WIZNET/TARGET_W7500x/i2c_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_WIZNET/TARGET_W7500x/i2c_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -38,7 +38,7 @@
 #include "pinmap.h"
 #include "PeripheralPins.h"
 
-#include "wait_api.h"
+#include "mbed_wait_api.h"
 #include "us_ticker_api.h"
 #include "W7500x_i2c.h"
 
--- a/targets/TARGET_WIZNET/TARGET_W7500x/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_WIZNET/TARGET_W7500x/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -31,12 +31,12 @@
 #include "cmsis.h"
 #include "mbed_interface.h"
 
-void sleep(void)
+void hal_sleep(void)
 {
     // To Do
 }
 
-void deepsleep(void)
+void hal_deepsleep(void)
 {
     // To Do
 }
--- a/targets/TARGET_ublox/TARGET_HI2110/lp_ticker.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ublox/TARGET_HI2110/lp_ticker.c	Tue Mar 14 16:40:56 2017 +0000
@@ -38,7 +38,7 @@
 
 #include "lp_ticker_api.h"
 #include "sleep_api.h"
-#include "critical.h"
+#include "mbed_critical.h"
 
 /* ----------------------------------------------------------------
  * MACROS
--- a/targets/TARGET_ublox/TARGET_HI2110/sleep.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ublox/TARGET_HI2110/sleep.c	Tue Mar 14 16:40:56 2017 +0000
@@ -40,14 +40,14 @@
  * MBED API CALLS
  * ----------------------------------------------------------------*/
 
-void sleep(void)
+void hal_sleep(void)
 {
     __DSB();
     __WFI();
     __ISB();
 }
 
-void deepsleep()
+void hal_deepsleep()
 {
-    sleep();
+    hal_sleep();
 }
--- a/targets/TARGET_ublox/TARGET_HI2110/us_ticker.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_ublox/TARGET_HI2110/us_ticker.c	Tue Mar 14 16:40:56 2017 +0000
@@ -25,7 +25,7 @@
  */
 
 #include "us_ticker_api.h"
-#include "critical.h"
+#include "mbed_critical.h"
 
 /* ----------------------------------------------------------------
  * MACROS
--- a/targets/targets.json	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/targets.json	Tue Mar 14 16:40:56 2017 +0000
@@ -10,7 +10,8 @@
         "features": [],
         "detect_code": [],
         "public": false,
-        "default_lib": "std"
+        "default_lib": "std",
+        "bootloader_supported": false
     },
     "Super_Target": {
         "inherits": ["Target"],
@@ -448,14 +449,15 @@
     "KL46Z": {
         "supported_form_factors": ["ARDUINO"],
         "core": "Cortex-M0+",
-        "extra_labels": ["Freescale", "KLXX"],
+        "extra_labels": ["Freescale", "KLXX", "FLASH_CMSIS_ALGO"],
         "is_disk_virtual": true,
         "supported_toolchains": ["GCC_ARM", "ARM", "IAR"],
         "inherits": ["Target"],
         "detect_code": ["0220"],
-        "device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SEMIHOST", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
+        "device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SEMIHOST", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "FLASH"],
         "release_versions": ["2", "5"],
-        "device_name": "MKL46Z256xxx4"
+        "device_name": "MKL46Z256xxx4",
+        "bootloader_supported": true
     },
     "K20D50M": {
         "inherits": ["Target"],
@@ -573,36 +575,37 @@
         "supported_form_factors": ["ARDUINO"],
         "core": "Cortex-M4F",
         "supported_toolchains": ["ARM", "GCC_ARM", "IAR"],
-        "extra_labels": ["Freescale", "MCUXpresso_MCUS", "KSDK2_MCUS", "FRDM", "KPSDK_MCUS", "KPSDK_CODE", "MCU_K64F"],
+        "extra_labels": ["Freescale", "MCUXpresso_MCUS", "KSDK2_MCUS", "FRDM", "KPSDK_MCUS", "KPSDK_CODE", "MCU_K64F", "FLASH_CMSIS_ALGO"],
         "is_disk_virtual": true,
         "macros": ["CPU_MK64FN1M0VMD12", "FSL_RTOS_MBED"],
         "inherits": ["Target"],
         "detect_code": ["0240"],
-        "device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE", "STDIO_MESSAGES", "STORAGE", "TRNG"],
+        "device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE", "STDIO_MESSAGES", "STORAGE", "TRNG", "FLASH"],
         "features": ["LWIP", "STORAGE"],
         "release_versions": ["2", "5"],
-        "device_name": "MK64FN1M0xxx12"
+        "device_name": "MK64FN1M0xxx12",
+        "bootloader_supported": true
     },
     "MTS_GAMBIT": {
         "inherits": ["Target"],
         "core": "Cortex-M4F",
         "supported_toolchains": ["ARM", "GCC_ARM"],
-        "extra_labels": ["Freescale", "MCUXpresso_MCUS", "KSDK2_MCUS", "KPSDK_MCUS", "KPSDK_CODE", "MCU_K64F"],
+        "extra_labels": ["Freescale", "MCUXpresso_MCUS", "KSDK2_MCUS", "KPSDK_MCUS", "KPSDK_CODE", "MCU_K64F", "FLASH_CMSIS_ALGO"],
         "is_disk_virtual": true,
         "macros": ["CPU_MK64FN1M0VMD12", "FSL_RTOS_MBED", "TARGET_K64F"],
-        "device_has": ["I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE", "STDIO_MESSAGES"],
+        "device_has": ["I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE", "STDIO_MESSAGES", "FLASH"],
         "device_name": "MK64FN1M0xxx12"
     },
     "HEXIWEAR": {
         "inherits": ["Target"],
         "core": "Cortex-M4F",
-        "extra_labels": ["Freescale", "MCUXpresso_MCUS", "KSDK2_MCUS", "MCU_K64F"],
+        "extra_labels": ["Freescale", "MCUXpresso_MCUS", "KSDK2_MCUS", "MCU_K64F", "FLASH_CMSIS_ALGO"],
         "supported_toolchains": ["ARM", "GCC_ARM", "IAR"],
         "macros": ["CPU_MK64FN1M0VMD12", "FSL_RTOS_MBED", "TARGET_K64F"],
         "is_disk_virtual": true,
         "default_toolchain": "ARM",
         "detect_code": ["0214"],
-        "device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
+        "device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE", "STDIO_MESSAGES", "TRNG", "FLASH"],
         "default_lib": "std",
         "release_versions": ["2", "5"],
         "device_name": "MK64FN1M0xxx12"
@@ -881,26 +884,27 @@
         "inherits": ["Target"],
         "core": "Cortex-M4F",
         "default_toolchain": "ARM",
-        "extra_labels": ["STM", "STM32F4", "STM32F429", "STM32F429ZI", "STM32F429xx", "STM32F429xI"],
+        "extra_labels": ["STM", "STM32F4", "STM32F429", "STM32F429ZI", "STM32F429xx", "STM32F429xI", "FLASH_CMSIS_ALGO"],
         "supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
         "progen": {"target": "nucleo-f429zi"},
         "macros": ["TRANSACTION_QUEUE_SIZE_SPI=2", "USB_STM_HAL", "USBHOST_OTHER"],
-        "device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG"],
+        "device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG", "FLASH"],
         "detect_code": ["0796"],
         "features": ["LWIP"],
         "release_versions": ["2", "5"],
-        "device_name" : "STM32F429ZI"
+        "device_name" : "STM32F429ZI",
+        "bootloader_supported": true
     },
     "NUCLEO_F439ZI": {
         "supported_form_factors": ["ARDUINO"],
         "inherits": ["Target"],
         "core": "Cortex-M4F",
         "default_toolchain": "ARM",
-        "extra_labels": ["STM", "STM32F4", "STM32F439", "STM32F439ZI", "STM32F439xx", "STM32F439xI"],
+        "extra_labels": ["STM", "STM32F4", "STM32F439", "STM32F439ZI", "STM32F439xx", "STM32F439xI", "FLASH_CMSIS_ALGO"],
         "supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
         "progen": {"target": "nucleo-f439zi"},
         "macros": ["TRANSACTION_QUEUE_SIZE_SPI=2"],
-        "device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG"],
+        "device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG", "FLASH"],
         "detect_code": ["0797"],
         "features": ["LWIP"],
         "release_versions": ["2", "5"],
@@ -1170,10 +1174,10 @@
         "inherits": ["Target"],
         "core": "Cortex-M4F",
         "default_toolchain": "ARM",
-        "extra_labels": ["STM", "STM32F4", "STM32F429", "STM32F429ZI", "STM32F429xI", "STM32F429xx"],
+        "extra_labels": ["STM", "STM32F4", "STM32F429", "STM32F429ZI", "STM32F429xI", "STM32F429xx", "FLASH_CMSIS_ALGO"],
         "macros": ["RTC_LSI=1","TRANSACTION_QUEUE_SIZE_SPI=2", "USBHOST_OTHER"],
         "supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
-        "device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG"],
+        "device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG", "FLASH"],
         "release_versions": ["2", "5"],
         "device_name": "STM32F429ZI"
     },
@@ -1321,13 +1325,14 @@
         "core": "Cortex-M4F",
         "default_toolchain": "ARM",
         "supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
-        "extra_labels": ["STM", "STM32F4", "STM32F439", "STM32F439ZI","STM32F439xx"],
+        "extra_labels": ["STM", "STM32F4", "STM32F439", "STM32F439ZI","STM32F439xx", "FLASH_CMSIS_ALGO"],
         "macros": ["HSE_VALUE=24000000", "HSE_STARTUP_TIMEOUT=5000", "CB_INTERFACE_SDIO","CB_CHIP_WL18XX","SUPPORT_80211D_ALWAYS","WLAN_ENABLED","MBEDTLS_ARC4_C","MBEDTLS_DES_C","MBEDTLS_MD4_C","MBEDTLS_MD5_C","MBEDTLS_SHA1_C"],
         "inherits": ["Target"],
-        "device_has": ["ANALOGIN", "CAN", "EMAC", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
+        "device_has": ["ANALOGIN", "CAN", "EMAC", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG", "FLASH"],
         "features": ["LWIP"],
         "release_versions": ["5"],
-        "device_name": "STM32F439ZI"
+        "device_name": "STM32F439ZI",
+        "bootloader_supported": true
     },
     "NZ32_SC151": {
         "inherits": ["Target"],
@@ -2553,7 +2558,7 @@
     "NUMAKER_PFM_NUC472": {
         "core": "Cortex-M4F",
         "default_toolchain": "ARM",
-        "extra_labels": ["NUVOTON", "NUC472", "NUMAKER_PFM_NUC472"],
+        "extra_labels": ["NUVOTON", "NUC472", "NU_XRAM_SUPPORTED"],
         "is_disk_virtual": true,
         "supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
         "inherits": ["Target"],
@@ -2595,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": {