element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Blog CMake and Raspberry Pico: generate multiple firmware binaries for the same code base
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 5 Aug 2024 5:19 PM Date Created
  • Views 241 views
  • Likes 5 likes
  • Comments 0 comments
  • nmake
  • vscode
  • teseo_c++
Related
Recommended

CMake and Raspberry Pico: generate multiple firmware binaries for the same code base

Jan Cumps
Jan Cumps
5 Aug 2024

I have a Raspberry Pico GPS library project. It comes with several examples. And each example can communicate to the GPS over I2C or UART.

image

Initially, it was a single-example project. I used a definition in the CMake config, that allowed you to build the project for either I2C or UART. Based on that parameter, it would generate a RP2040 .uf2 firmware that matched your choice.
I wasn't too happy with that approach, because a user was required to change the build file. And I had to run several builds when I was about to generate the assets for a new release.

Multiple Firmwares

If you analyse my challenge, it comes down to this:

  • I have multiple examples that use my Teseo communication code
  • The Teseo communication code can work on I2C or UART

I split the work up. First, I learned CMake to build two libraries for my communication code. One for I2C, and one for UART:

# sources shared by all examples with uart
add_library(${CMAKE_PROJECT_NAME}_shared_uart
        ${CMAKE_CURRENT_SOURCE_DIR}/gps_teseo_lib/teseo/teseo.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/port/pico/reset.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/port/pico/uart/teseo_communicate.cpp
)

target_include_directories(${CMAKE_PROJECT_NAME}_shared_uart PUBLIC 
        ${CMAKE_CURRENT_SOURCE_DIR}/gps_teseo_lib/teseo
        ${CMAKE_CURRENT_SOURCE_DIR}/gps_teseo_lib/callbackmanager
        ${CMAKE_CURRENT_SOURCE_DIR}/port/pico
        ${CMAKE_CURRENT_SOURCE_DIR}/port/pico/uart
)

target_link_libraries(${CMAKE_PROJECT_NAME}_shared_uart  PUBLIC
        pico_stdlib hardware_gpio hardware_uart
)

# sources shared by all examples with i2c
add_library(${CMAKE_PROJECT_NAME}_shared_i2c
        ${CMAKE_CURRENT_SOURCE_DIR}/gps_teseo_lib/teseo/teseo.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/port/pico/reset.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/port/pico/i2c/teseo_communicate.cpp
)

target_include_directories(${CMAKE_PROJECT_NAME}_shared_i2c PUBLIC 
        ${CMAKE_CURRENT_SOURCE_DIR}/gps_teseo_lib/teseo
        ${CMAKE_CURRENT_SOURCE_DIR}/gps_teseo_lib/callbackmanager
        ${CMAKE_CURRENT_SOURCE_DIR}/port/pico
        ${CMAKE_CURRENT_SOURCE_DIR}/port/pico/i2c
)

target_link_libraries(${CMAKE_PROJECT_NAME}_shared_i2c 
        pico_stdlib hardware_gpio hardware_i2c
)

Then, for each of my examples, I generate one binary that links to the I2C version. And one that links to the UART lib. Because the communication part is fully abstracted, I don't need conditional compilation. The linker can resolve it all.

# teseo reply response example --------------------------------------

add_executable(${CMAKE_PROJECT_NAME}_reply_response_uart
        ${CMAKE_CURRENT_SOURCE_DIR}/teseo_reply_response.cpp
)

target_link_libraries(${CMAKE_PROJECT_NAME}_reply_response_uart 
        ${CMAKE_PROJECT_NAME}_shared_uart
)

target_include_directories(${CMAKE_PROJECT_NAME}_reply_response_uart PUBLIC 
)

# select the debug output (not used by the GPS interface)
pico_enable_stdio_uart(${CMAKE_PROJECT_NAME}_reply_response_uart 1)
pico_enable_stdio_usb(${CMAKE_PROJECT_NAME}_reply_response_uart 0)

pico_add_extra_outputs(${CMAKE_PROJECT_NAME}_reply_response_uart )

add_executable(${CMAKE_PROJECT_NAME}_reply_response_i2c
        ${CMAKE_CURRENT_SOURCE_DIR}/teseo_reply_response.cpp
)

target_link_libraries(${CMAKE_PROJECT_NAME}_reply_response_i2c 
        ${CMAKE_PROJECT_NAME}_shared_i2c
)

target_include_directories(${CMAKE_PROJECT_NAME}_reply_response_i2c PUBLIC 
)

# select the debug output (not used by the GPS interface)
pico_enable_stdio_uart(${CMAKE_PROJECT_NAME}_reply_response_i2c 1)
pico_enable_stdio_usb(${CMAKE_PROJECT_NAME}_reply_response_i2c 0)

pico_add_extra_outputs(${CMAKE_PROJECT_NAME}_reply_response_i2c )

# nmea parse example --------------------------------------

add_executable(${CMAKE_PROJECT_NAME}_nmea_parse_uart
        ${CMAKE_CURRENT_SOURCE_DIR}/teseo_with_nmea_parse.cpp
)

target_link_libraries(${CMAKE_PROJECT_NAME}_nmea_parse_uart 
        ${CMAKE_PROJECT_NAME}_shared_uart
)

target_include_directories(${CMAKE_PROJECT_NAME}_nmea_parse_uart PUBLIC 
        ${CMAKE_CURRENT_SOURCE_DIR}/gps_nmea_lib/nmea
)

# select the debug output (not used by the GPS interface)
pico_enable_stdio_uart(${CMAKE_PROJECT_NAME}_nmea_parse_uart 1)
pico_enable_stdio_usb(${CMAKE_PROJECT_NAME}_nmea_parse_uart 0)

pico_add_extra_outputs(${CMAKE_PROJECT_NAME}_nmea_parse_uart )

add_executable(${CMAKE_PROJECT_NAME}_nmea_parse_i2c
        ${CMAKE_CURRENT_SOURCE_DIR}/teseo_with_nmea_parse.cpp
)

target_link_libraries(${CMAKE_PROJECT_NAME}_nmea_parse_i2c 
        ${CMAKE_PROJECT_NAME}_shared_i2c
)

target_include_directories(${CMAKE_PROJECT_NAME}_nmea_parse_i2c PUBLIC 
        ${CMAKE_CURRENT_SOURCE_DIR}/gps_nmea_lib/nmea
)

# select the debug output (not used by the GPS interface)
pico_enable_stdio_uart(${CMAKE_PROJECT_NAME}_nmea_parse_i2c 1)
pico_enable_stdio_usb(${CMAKE_PROJECT_NAME}_nmea_parse_i2c 0)

pico_add_extra_outputs(${CMAKE_PROJECT_NAME}_nmea_parse_i2c )

That's it. If you run CMake without target, it 'll build all permutations. If you only want a single firmware, you can define that as a target during build.
VSCode, and likely all of the other IDEs, have an option for that. For CLI users: you can also pass your (or no) target on the command line.


Link to 
all posts.

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube