Skip to content

Commit

Permalink
Add a cmake function for configuring IP addresses (#1424)
Browse files Browse the repository at this point in the history
The fix for the following issue adds some macros to configure default
ip addresses. These are expressed in hex which is a bit non-obvious to
set. So add a macro to convert from a string to the hex representation.

georgerobotics/cyw43-driver#41
  • Loading branch information
peterharperuk authored Jan 5, 2024
1 parent 9f45e3c commit d7bbadb
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/rp2_common/pico_cyw43_driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,32 @@ if (EXISTS ${PICO_CYW43_DRIVER_PATH}/${CYW43_DRIVER_TEST_FILE})
)
endif()

# Set an ip address in a compile definition
# target name, target type, compile definition name to set then address in a string
# This can be used to set the following compile definitions
# CYW43_DEFAULT_IP_ADDRESS
# CYW43_DEFAULT_IP_MASK
# CYW43_DEFAULT_IP_GATEWAY
# CYW43_DEFAULT_IP_DNS
# CYW43_DEFAULT_AP_IP_ADDRESS
# e.g. pico_configure_ip4_address(picow_tcpip_server_background PRIVATE CYW43_DEFAULT_IP_ADDRESS "10.3.15.204")
function(pico_configure_ip4_address TARGET_LIB TARGET_TYPE DEF_NAME IP_ADDRESS_STR)
string(REGEX MATCHALL "[0-9]+" IP_ADDRESS_LIST ${IP_ADDRESS_STR})
list(LENGTH IP_ADDRESS_LIST IP_ADDRESS_COMPONENT_COUNT)
if (NOT ${IP_ADDRESS_COMPONENT_COUNT} EQUAL 4)
message(FATAL_ERROR "wrong number of components in ip address 4 != ${IP_ADDRESS_COMPONENT_COUNT}")
endif()
set(IP_ADDRESS_HEX "0x0")
foreach(IP_COMPONENT ${IP_ADDRESS_LIST})
if (${IP_COMPONENT} GREATER 255)
message(FATAL_ERROR "ip address component too big ${IP_COMPONENT} > 255")
endif()
math(EXPR IP_ADDRESS_HEX "(${IP_ADDRESS_HEX} << 8 ) | ${IP_COMPONENT}" OUTPUT_FORMAT HEXADECIMAL)
endforeach()
target_compile_definitions(${TARGET_LIB} ${TARGET_TYPE}
${DEF_NAME}=${IP_ADDRESS_HEX}
)
endfunction()

pico_promote_common_scope_vars()
endif()

0 comments on commit d7bbadb

Please sign in to comment.