SET(CMAKE_C_FLAGS "-mthumb -fno-builtin -mcpu=cortex-m0plus -Wall -std=gnu99 -ffunction-sections -fdata-sections -fomit-frame-pointer -mabi=aapcs -fno-unroll-loops -ffast-math -ftree-vectorize" CACHE INTERNAL "c compiler flags")
SET(CMAKE_CXX_FLAGS "-mthumb -fno-builtin -mcpu=cortex-m0plus -Wall -std=c++11 -ffunction-sections -fdata-sections -fomit-frame-pointer -mabi=aapcs -fno-unroll-loops -ffast-math -ftree-vectorize" CACHE INTERNAL "cxx compiler flags")
SET(CMAKE_ASM_FLAGS "-mthumb -mcpu=cortex-m0plus -x assembler-with-cpp" CACHE INTERNAL "asm compiler flags")


SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--gc-sections -mthumb -mcpu=cortex-m0 -mabi=aapcs --specs=nano.specs" CACHE INTERNAL "executable linker flags")
SET(CMAKE_MODULE_LINKER_FLAGS "-mthumb -mcpu=cortex-m0plus -mabi=aapcs" CACHE INTERNAL "module linker flags")
SET(CMAKE_SHARED_LINKER_FLAGS "-mthumb -mcpu=cortex-m0plus -mabi=aapcs" CACHE INTERNAL "shared linker flags")

SET(STM32_CHIP_TYPES 070xB 071x8 071xB 081xB 041x8 031x8 041x6 031x6 031x4  CACHE INTERNAL "stm32g0 chip types")
SET(STM32_CODES     "070.B" "071.8" "071.B" "081.B" "041.8" "031.8" "041.6" "031.6" "031.4")

MACRO(STM32_GET_CHIP_TYPE CHIP CHIP_TYPE)
    STRING(SUBSTRING ${CHIP} 6 5 STM32_CODE)
    SET(INDEX 0)
    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})
ENDMACRO()

MACRO(STM32_GET_CHIP_PARAMETERS CHIP FLASH_SIZE RAM_SIZE)
    STRING(REGEX REPLACE "^[sS][tT][mM]32[gG](0[3478][1]).([468BJ]$|[68BCDE][AX]$)" "\\1" STM32_CODE ${CHIP})
    STRING(REGEX REPLACE "^[sS][tT][mM]32[gG](0[3478][1]).([468BJ]$|[68BCDE][AX]$)" "\\2" STM32_SIZE_CODE ${CHIP})

    STM32_GET_CHIP_TYPE(${CHIP} TYPE)

    IF(${TYPE} STREQUAL 071x8)
        SET(RAM "36K")
        SET(FLASH "64K")
    ELSEIF(${TYPE} STREQUAL 070xB)
        SET(RAM "36K")
        SET(FLASH "128K")
    ELSEIF(${TYPE} STREQUAL 071xB)
        SET(RAM "36K")
        SET(FLASH "128K")
    ELSEIF(${TYPE} STREQUAL 081xB)
        SET(RAM "36K")
        SET(FLASH "128K")
    ELSEIF(${TYPE} STREQUAL 041x8)
        SET(RAM "8K")
        SET(FLASH "64K")
    ELSEIF(${TYPE} STREQUAL 031x8)
        SET(RAM "8K")
        SET(FLASH "64K")
    ELSEIF(${TYPE} STREQUAL 041x6)
        SET(RAM "8K")
        SET(FLASH "32K")
    ELSEIF(${TYPE} STREQUAL 031x6)
        SET(RAM "8K")
        SET(FLASH "32K")
    ELSEIF(${TYPE} STREQUAL 031x4)
        SET(RAM "8K")
        SET(FLASH "16K")
    ENDIF()

    SET(${FLASH_SIZE} ${FLASH})
    SET(${RAM_SIZE} ${RAM})
ENDMACRO()

FUNCTION(STM32_SET_CHIP_DEFINITIONS TARGET CHIP_TYPE)
    LIST(FIND STM32_CHIP_TYPES ${CHIP_TYPE} TYPE_INDEX)
    IF(TYPE_INDEX EQUAL -1)
        MESSAGE(FATAL_ERROR "Invalid/unsupported STM32G0 chip type: ${CHIP_TYPE}")
    ENDIF()
    GET_TARGET_PROPERTY(TARGET_DEFS ${TARGET} COMPILE_DEFINITIONS)
    STRING(REGEX REPLACE "^(0[3478][01]).[BCDEFGJ468]" "\\1" STM32_DEVICE_NUM ${STM32_CHIP_TYPE})
    IF(TARGET_DEFS)
        SET(TARGET_DEFS "STM32${STM32_FAMILY};STM32G${STM32_DEVICE_NUM}xx;${TARGET_DEFS}")
    ELSE()
        SET(TARGET_DEFS "STM32${STM32_FAMILY};STM32G${STM32_DEVICE_NUM}xx")
    ENDIF()
    SET_TARGET_PROPERTIES(${TARGET} PROPERTIES COMPILE_DEFINITIONS "${TARGET_DEFS}")
ENDFUNCTION()
