SET(CHIP_C_OPTIONS -g3 -mthumb -fno-builtin -mcpu=cortex-m0 -std=gnu99 -ffunction-sections -fdata-sections -fomit-frame-pointer -mabi=aapcs -fno-unroll-loops -ffast-math -ftree-vectorize)
SET(CHIP_CXX_OPTIONS -g3 -mthumb -fno-builtin -mcpu=cortex-m0 -ffunction-sections -fdata-sections -fomit-frame-pointer -mabi=aapcs -fno-unroll-loops -ffast-math -ftree-vectorize)
SET(CHIP_ASM_OPTIONS -g3 -mthumb -mcpu=cortex-m0 -x assembler-with-cpp)
SET(CHIP_EXE_LINKER_OPTIONS -Wl,--gc-sections -mthumb -mcpu=cortex-m0 -mabi=aapcs)
SET(CHIP_MODULE_LINKER_OPTIONS -mthumb -mcpu=cortex-m0 -mabi=aapcs)
SET(CHIP_SHARED_LINKER_OPTIONS -mthumb -mcpu=cortex-m0 -mabi=aapcs)

FUNCTION(CHIP_GET_OPTIONS_ STM32_CHIP_C_OPTIONS STM32_CHIP_CXX_OPTIONS STM32_CHIP_ASM_OPTIONS STM32_CHIP_EXE_LINKER_OPTIONS STM32_CHIP_MODULE_LINKER_OPTIONS STM32_CHIP_SHARED_LINKER_OPTIONS)
SET(${STM32_CHIP_C_OPTIONS} ${CHIP_C_OPTIONS} PARENT_SCOPE)
SET(${STM32_CHIP_CXX_OPTIONS} ${CHIP_CXX_OPTIONS} PARENT_SCOPE)
SET(${STM32_CHIP_ASM_OPTIONS} ${CHIP_ASM_OPTIONS} PARENT_SCOPE)
SET(${STM32_CHIP_EXE_LINKER_OPTIONS} ${CHIP_EXE_LINKER_OPTIONS} PARENT_SCOPE)
SET(${STM32_CHIP_MODULE_LINKER_OPTIONS} ${CHIP_MODULE_LINKER_OPTIONS} PARENT_SCOPE)
SET(${STM32_CHIP_SHARED_LINKER_OPTIONS} ${CHIP_SHARED_LINKER_OPTIONS} PARENT_SCOPE)
ENDFUNCTION()

FUNCTION(CHIP_GET_TYPES_ STM32_CHIP_TYPES)
    SET(${STM32_CHIP_TYPES} 030x6 030x8 031x6 038xx 042x6 048x6 051x8 058xx 070x6 070xB 071xB 072xB 078xx 091xC 098xx 030xC PARENT_SCOPE)
ENDFUNCTION()

FUNCTION(CHIP_GET_CODES_ CODES)
    SET(${CODES} "030.[46]" "030.8" "031.[46]" "038.6" "042.[46]" "048.6" "051.[468]" "058.8" "070.6" "070.B" "071.[8B]" "072.[8B]" "078.B" "091.[BC]" "098.C" "030.C" PARENT_SCOPE)
ENDFUNCTION()

#OUT -> STM32_CODE
FUNCTION(CHIP_GET_CURRENT_CODE_ MCU STM32_CODE)
    STRING(REGEX REPLACE "^[sS][tT][mM]32[fF]((03[018].[468C])|(04[28].[46])|(05[18].[468])|(07[0128].[68B])|(09[18].[BC])).*$" "\\1" STM32_CODE_ ${MCU})
    SET(${STM32_CODE} ${STM32_CODE_} PARENT_SCOPE)
ENDFUNCTION()

FUNCTION(CHIP_GET_TYPE_ MCU CHIP_TYPE)
    CHIP_GET_CODES_(STM32_CODES)
    CHIP_GET_CURRENT_CODE_(${MCU} STM32_CODE)
    SET(INDEX 0)
    CHIP_GET_TYPES_(STM32_CHIP_TYPES)
    FOREACH(C_TYPE ${STM32_CHIP_TYPES})
        LIST(GET STM32_CODES ${INDEX} CHIP_TYPE_REGEXP)
        IF(STM32_CODE MATCHES ${CHIP_TYPE_REGEXP})
            SET(RESULT_TYPE ${C_TYPE})
        ENDIF()
        MATH(EXPR INDEX "${INDEX}+1")
    ENDFOREACH()
    SET(${CHIP_TYPE} ${RESULT_TYPE} PARENT_SCOPE)
ENDFUNCTION()



FUNCTION(CHIP_GET_PARAMETERS_ MCU FLASH_SIZE RAM_SIZE CCRAM_SIZE)
    STRING(REGEX REPLACE "^[sS][tT][mM]32[fF]0[34579][0128].([468BC]).*$" "\\1" STM32_SIZE_CODE ${MCU})
    IF(STM32_SIZE_CODE STREQUAL "4")
        SET(FLASH "16K")
    ELSEIF(STM32_SIZE_CODE STREQUAL "6")
        SET(FLASH "32K")
    ELSEIF(STM32_SIZE_CODE STREQUAL "8")
        SET(FLASH "64K")
    ELSEIF(STM32_SIZE_CODE STREQUAL "B")
        SET(FLASH "128K")
    ELSEIF(STM32_SIZE_CODE STREQUAL "C")
        SET(FLASH "256K")
    ENDIF()

    CHIP_GET_TYPE_(${MCU} TYPE)

    IF(${TYPE} STREQUAL 030x6)
        SET(RAM "4K")
    ELSEIF(${TYPE} STREQUAL 030x8)
        SET(RAM "8K")
    ELSEIF(${TYPE} STREQUAL 030xC)
        SET(RAM "32K")
    ELSEIF(${TYPE} STREQUAL 031x6)
        SET(RAM "4K")
    ELSEIF(${TYPE} STREQUAL 038xx)
        SET(RAM "4K")
    ELSEIF(${TYPE} STREQUAL 042x6)
        SET(RAM "6K")
    ELSEIF(${TYPE} STREQUAL 048x6)
        SET(RAM "6K")
    ELSEIF(${TYPE} STREQUAL 051x8)
        SET(RAM "8K")
    ELSEIF(${TYPE} STREQUAL 058xx)
        SET(RAM "8K")
    ELSEIF(${TYPE} STREQUAL 070x6)
        SET(RAM "6K")
    ELSEIF(${TYPE} STREQUAL 070xB)
        SET(RAM "16K")
    ELSEIF(${TYPE} STREQUAL 071xB)
        SET(RAM "16K")
    ELSEIF(${TYPE} STREQUAL 072xB)
        SET(RAM "16K")
    ELSEIF(${TYPE} STREQUAL 078xx)
        SET(RAM "16K")
    ELSEIF(${TYPE} STREQUAL 091xC)
        SET(RAM "32K")
    ELSEIF(${TYPE} STREQUAL 098xx)
        SET(RAM "32K")
    ENDIF()

    SET(${FLASH_SIZE} ${FLASH} PARENT_SCOPE)
    SET(${RAM_SIZE} ${RAM} PARENT_SCOPE)
    SET(${CCRAM_SIZE} "0K" PARENT_SCOPE)
ENDFUNCTION()

FUNCTION(CHIP_SET_DEFINITIONS_ TARGET MCU)
    CHIP_GET_TYPE_(${MCU} CHIP_TYPE)
    CHIP_GET_TYPES_(STM32_CHIP_TYPES)
    LIST(FIND STM32_CHIP_TYPES ${CHIP_TYPE} TYPE_INDEX)
    IF(TYPE_INDEX EQUAL -1)
        MESSAGE(FATAL_ERROR "Invalid/unsupported STM32F0 chip type: ${CHIP_TYPE}")
    ENDIF()
    GET_TARGET_PROPERTY(TARGET_DEFS ${TARGET} COMPILE_DEFINITIONS)
    IF(TARGET_DEFS)
        SET(TARGET_DEFS "STM32F0;STM32F${CHIP_TYPE};${TARGET_DEFS}")
    ELSE()
        SET(TARGET_DEFS "STM32F0;STM32F${CHIP_TYPE}")
    ENDIF()
    SET_TARGET_PROPERTIES(${TARGET} PROPERTIES COMPILE_DEFINITIONS "${TARGET_DEFS}")
ENDFUNCTION()
