Skip to content

rpi_pico: Add hwinfo support #42558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions boards/arm/rpi_pico/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ hardware features:
* - I2C
- :kconfig:option:`CONFIG_I2C`
- :dtcompatible:`rpi,pico-i2c`
* - HWINFO
- :kconfig:option:`CONFIG_HWINFO`
- N/A

Programming and Debugging
*************************
Expand Down
1 change: 1 addition & 0 deletions boards/arm/rpi_pico/rpi_pico.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ supported:
- uart
- gpio
- i2c
- hwinfo
3 changes: 3 additions & 0 deletions cmake/compiler/compiler_flags_template.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ set_property(TARGET compiler-cpp PROPERTY no_threadsafe_statics)

# Required ASM flags when compiling
set_property(TARGET asm PROPERTY required)

# Compiler flag for disabling pointer arithmetic warnings
set_compiler_property(PROPERTY warning_no_pointer_arithmetic)
3 changes: 3 additions & 0 deletions cmake/compiler/gcc/compiler_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,6 @@ set_property(TARGET asm PROPERTY required "-xassembler-with-cpp")
if (NOT COMPILER STREQUAL "xcc")
set_compiler_property(PROPERTY diagnostic -fdiagnostics-color=always)
endif()

# Compiler flag for disabling pointer arithmetic warnings
set_compiler_property(PROPERTY warning_no_pointer_arithmetic "-Wno-pointer-arith")
1 change: 1 addition & 0 deletions drivers/hwinfo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ zephyr_library_sources_ifdef(CONFIG_HWINFO_MCUX_SRC hwinfo_mcux_src.c)
zephyr_library_sources_ifdef(CONFIG_HWINFO_MCUX_SYSCON hwinfo_mcux_syscon.c)
zephyr_library_sources_ifdef(CONFIG_HWINFO_NRF hwinfo_nrf.c)
zephyr_library_sources_ifdef(CONFIG_HWINFO_PSOC6 hwinfo_psoc6.c)
zephyr_library_sources_ifdef(CONFIG_HWINFO_RPI_PICO hwinfo_rpi_pico.c)
zephyr_library_sources_ifdef(CONFIG_HWINFO_SAM_RSTC hwinfo_sam_rstc.c)
zephyr_library_sources_ifdef(CONFIG_HWINFO_SAM hwinfo_sam.c)
zephyr_library_sources_ifdef(CONFIG_HWINFO_SAM0 hwinfo_sam0.c)
Expand Down
8 changes: 8 additions & 0 deletions drivers/hwinfo/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ config HWINFO_IMXRT
help
Enable NXP i.mx RT hwinfo driver.

config HWINFO_RPI_PICO
bool "Raspberry Pi Pico hwinfo driver"
default y
depends on SOC_SERIES_RP2XXX
select PICOSDK_USE_FLASH
help
Enable Raspberry Pi Pico hwinfo driver.

config HWINFO_SAM_RSTC
bool "Atmel SAM reset cause"
default y
Expand Down
74 changes: 74 additions & 0 deletions drivers/hwinfo/hwinfo_rpi_pico.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2022 Yonatan Schachter
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <string.h>
#include <drivers/hwinfo.h>
#include <hardware/flash.h>
#include <hardware/structs/vreg_and_chip_reset.h>

#define FLASH_RUID_DATA_BYTES 8

#define HAD_RUN_BIT BIT(VREG_AND_CHIP_RESET_CHIP_RESET_HAD_RUN_LSB)
#define HAD_PSM_RESTART_BIT BIT(VREG_AND_CHIP_RESET_CHIP_RESET_HAD_PSM_RESTART_LSB)
#define HAD_POR_BIT BIT(VREG_AND_CHIP_RESET_CHIP_RESET_HAD_POR_LSB)

ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length)
{
uint8_t id[FLASH_RUID_DATA_BYTES];
uint32_t key;

/*
* flash_get_unique_id temporarily disables XIP to query the
* flash for its ID. If the CPU is interrupted while XIP is
* disabled, it will halt. Therefore, interrupts must be disabled
* before fetching the ID.
*/
key = irq_lock();
flash_get_unique_id(id);
irq_unlock(key);

if (length > sizeof(id)) {
length = sizeof(id);
}
memcpy(buffer, id, length);

return length;
}

int z_impl_hwinfo_get_reset_cause(uint32_t *cause)
{
uint32_t flags = 0;
uint32_t reset_register = vreg_and_chip_reset_hw->chip_reset;

if (reset_register & HAD_POR_BIT) {
flags |= RESET_POR;
}

if (reset_register & HAD_RUN_BIT) {
flags |= RESET_PIN;
}

if (reset_register & HAD_PSM_RESTART_BIT) {
flags |= RESET_DEBUG;
}

*cause = flags;
return 0;
}

int z_impl_hwinfo_clear_reset_cause(void)
{
/* The reset register can't be modified, nothing to do. */

return -ENOSYS;
}

int z_impl_hwinfo_get_supported_reset_cause(uint32_t *supported)
{
*supported = RESET_PIN | RESET_DEBUG | RESET_POR;

return 0;
}
8 changes: 4 additions & 4 deletions include/drivers/hwinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extern "C" {
* @param length Max length of the buffer.
*
* @retval size of the device ID copied.
* @retval -ENOTSUP if there is no implementation for the particular device.
* @retval -ENOSYS if there is no implementation for the particular device.
* @retval any negative value on driver specific errors.
*/
__syscall ssize_t hwinfo_get_device_id(uint8_t *buffer, size_t length);
Expand All @@ -87,7 +87,7 @@ ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length);
* `hwinfo_clear_reset_cause` has been called.
*
* @retval zero if successful.
* @retval -ENOTSUP if there is no implementation for the particular device.
* @retval -ENOSYS if there is no implementation for the particular device.
* @retval any negative value on driver specific errors.
*/
__syscall int hwinfo_get_reset_cause(uint32_t *cause);
Expand All @@ -100,7 +100,7 @@ int z_impl_hwinfo_get_reset_cause(uint32_t *cause);
* Clears reset cause flags.
*
* @retval zero if successful.
* @retval -ENOTSUP if there is no implementation for the particular device.
* @retval -ENOSYS if there is no implementation for the particular device.
* @retval any negative value on driver specific errors.
*/
__syscall int hwinfo_clear_reset_cause(void);
Expand All @@ -115,7 +115,7 @@ int z_impl_hwinfo_clear_reset_cause(void);
* Retrieves all `reset_cause` flags that are supported by this device.
*
* @retval zero if successful.
* @retval -ENOTSUP if there is no implementation for the particular device.
* @retval -ENOSYS if there is no implementation for the particular device.
* @retval any negative value on driver specific errors.
*/
__syscall int hwinfo_get_supported_reset_cause(uint32_t *supported);
Expand Down
19 changes: 19 additions & 0 deletions modules/hal_rpi_pico/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ if(CONFIG_HAS_RPI_PICO)
${rp2_common_dir}/hardware_xosc/xosc.c
${rp2_common_dir}/hardware_watchdog/watchdog.c
${rp2_common_dir}/pico_platform/platform.c
${rp2_common_dir}/pico_bootrom/bootrom.c
)

zephyr_include_directories(
Expand All @@ -62,6 +63,7 @@ if(CONFIG_HAS_RPI_PICO)
${rp2_common_dir}/hardware_sync/include
${rp2_common_dir}/hardware_timer/include
${rp2_common_dir}/hardware_resets/include
${rp2_common_dir}/pico_bootrom/include
${rp2040_dir}/hardware_regs/include
${rp2040_dir}/hardware_structs/include
${common_dir}/pico_base/include
Expand All @@ -78,4 +80,21 @@ if(CONFIG_HAS_RPI_PICO)
${rp2_common_dir}/hardware_uart/uart.c)
zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_UART
${rp2_common_dir}/hardware_uart/include)

zephyr_library_sources_ifdef(CONFIG_PICOSDK_USE_FLASH
${rp2_common_dir}/hardware_flash/flash.c)
zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_FLASH
${rp2_common_dir}/hardware_flash/include)

# Some flash driver functions must be executed from the RAM.
# Originally pico-sdk places them in the RW data section, so this
# implementation does the same.
zephyr_linker_sources_ifdef(CONFIG_PICOSDK_USE_FLASH RWDATA timecritical.ld)

# A function in flash.c adds 1 to a function pointer, causing a warning
set_source_files_properties(
${rp2_common_dir}/hardware_flash/flash.c
PROPERTIES
COMPILE_FLAGS $<TARGET_PROPERTY:compiler,warning_no_pointer_arithmetic>
)
endif()
5 changes: 5 additions & 0 deletions modules/hal_rpi_pico/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ config PICOSDK_USE_GPIO
bool
help
Use the GPIO driver from pico-sdk

config PICOSDK_USE_FLASH
bool
help
Use the flash driver from pico-sdk
7 changes: 7 additions & 0 deletions modules/hal_rpi_pico/pico/config_autogen.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@
#define __always_inline ALWAYS_INLINE
#endif /* __always_inline */

/* Two definitions required for the flash driver */
#define __STRING(x) #x

#ifndef __noinline
#define __noinline __attribute__((noinline))
#endif

#endif
1 change: 1 addition & 0 deletions modules/hal_rpi_pico/timecritical.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*(.time_critical*)