From 7c36237c824ae8c5fdfec3f9e48e26ff3f46e311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucher?= <cfoucher@laas.fr> Date: Wed, 2 Nov 2022 10:11:02 +0000 Subject: [PATCH] Update method to acquire Zephyr device structures This MR updates the code used to acquire Zephyr device structures, in order to get rid of the `device_get_binding()` calls. This allows to ensure statically (at compile time) that the drivers used by the code are correctly referenced wrt. drivers names in device tree, and to prevent code from using devices that are not enabled in device tree. --- .../dts/bindings/gpio-device.yaml | 6 --- .../owntech_board/dts/can-standby-switch.dtsi | 1 - zephyr/boards/arm/owntech_board/dts/ngnd.dtsi | 1 - .../zephyr/src/adc_channels.c | 2 +- .../owntech_communication/zephyr/src/can.h | 1 - .../zephyr/src/thingset_isotp.cpp | 5 +- .../zephyr/src/thingset_pub_sub.cpp | 7 ++- .../zephyr/public_api/dac.h | 6 +-- .../owntech_data_acquisition/zephyr/dma/dma.c | 20 ++++--- .../zephyr/src/dac_configuration.cpp | 52 ++++++++++--------- .../src/hardware_auto_configuration.cpp | 12 +++-- .../zephyr/src/ngnd_configuration.cpp | 14 ++--- .../zephyr/src/timer_configuration.cpp | 29 ++++++----- .../zephyr/src/uart_configuration.cpp | 28 ++++++---- .../zephyr/public_api/ngnd.h | 2 +- .../zephyr/public_api/Scheduling.cpp | 7 ++- .../zephyr/public_api/timer.h | 6 +-- 17 files changed, 102 insertions(+), 97 deletions(-) diff --git a/zephyr/boards/arm/owntech_board/dts/bindings/gpio-device.yaml b/zephyr/boards/arm/owntech_board/dts/bindings/gpio-device.yaml index c5f3c1a..4151161 100644 --- a/zephyr/boards/arm/owntech_board/dts/bindings/gpio-device.yaml +++ b/zephyr/boards/arm/owntech_board/dts/bindings/gpio-device.yaml @@ -2,12 +2,6 @@ description: GPIO-controllable device compatible: "gpio-device" -properties: - label: - required: true - type: string - description: Name of the peripheral (used as device_get_binding() argument) - child-binding: description: GPIO node properties: diff --git a/zephyr/boards/arm/owntech_board/dts/can-standby-switch.dtsi b/zephyr/boards/arm/owntech_board/dts/can-standby-switch.dtsi index 5185624..020cf99 100644 --- a/zephyr/boards/arm/owntech_board/dts/can-standby-switch.dtsi +++ b/zephyr/boards/arm/owntech_board/dts/can-standby-switch.dtsi @@ -7,7 +7,6 @@ / { can-standby-switch { compatible = "gpio-device"; - label = "CAN_STANDBY"; can-standby-gpio-pin { gpios = <&gpiob 10 GPIO_ACTIVE_HIGH>; label = "CAN Standby output"; diff --git a/zephyr/boards/arm/owntech_board/dts/ngnd.dtsi b/zephyr/boards/arm/owntech_board/dts/ngnd.dtsi index 09a0e48..cde9c3f 100644 --- a/zephyr/boards/arm/owntech_board/dts/ngnd.dtsi +++ b/zephyr/boards/arm/owntech_board/dts/ngnd.dtsi @@ -7,7 +7,6 @@ / { ngnd: ngnd-switch { compatible = "gpio-device"; - label = "NGND"; ngnd-gpio-pin { gpios = <&gpiob 11 GPIO_ACTIVE_HIGH>; label = "Neutral to GND shunt"; diff --git a/zephyr/modules/owntech_adc_driver/zephyr/src/adc_channels.c b/zephyr/modules/owntech_adc_driver/zephyr/src/adc_channels.c index 7e3ab48..7091a92 100644 --- a/zephyr/modules/owntech_adc_driver/zephyr/src/adc_channels.c +++ b/zephyr/modules/owntech_adc_driver/zephyr/src/adc_channels.c @@ -57,7 +57,7 @@ typedef struct #define ADC_INPUTS_NODELABEL DT_NODELABEL(mychannels) // Channel properties -#define CHANNEL_NAME(node_id) DT_LABEL(node_id) +#define CHANNEL_NAME(node_id) DT_PROP(node_id, label) #define CHANNEL_IS_DIFF(node_id) DT_PROP(node_id, differential) #define CHANNEL_NUMBER(node_id) DT_PHA_BY_IDX(node_id, io_channels, 0, input) #define CHANNEL_ADC(node_id) DT_PROP_BY_PHANDLE_IDX(node_id, io_channels, 0, label) diff --git a/zephyr/modules/owntech_communication/zephyr/src/can.h b/zephyr/modules/owntech_communication/zephyr/src/can.h index ddaec8c..84efe2f 100644 --- a/zephyr/modules/owntech_communication/zephyr/src/can.h +++ b/zephyr/modules/owntech_communication/zephyr/src/can.h @@ -4,7 +4,6 @@ #include <zephyr.h> -#define CAN_DEVICE DT_LABEL(DT_NODELABEL(can1)) #define CAN_STANDBY_DEVICE DT_CHILD(DT_PATH(can_standby_switch), can_standby_gpio_pin) diff --git a/zephyr/modules/owntech_communication/zephyr/src/thingset_isotp.cpp b/zephyr/modules/owntech_communication/zephyr/src/thingset_isotp.cpp index 937eacd..8977d6f 100644 --- a/zephyr/modules/owntech_communication/zephyr/src/thingset_isotp.cpp +++ b/zephyr/modules/owntech_communication/zephyr/src/thingset_isotp.cpp @@ -5,9 +5,11 @@ */ /** + * @date 2022 * @author Martin Jäger <martin@libre.solar> * @author Jean Alinei <jean.alinei@laas.fr> * @author Luiz Villa <luiz.villa@laas.fr> + * @author Clément Foucher <clement.foucher@laas.fr> */ #include <canbus/isotp.h> @@ -16,6 +18,7 @@ extern ThingSet ts; extern uint16_t can_node_addr; +static const struct device* can_dev = DEVICE_DT_GET(DT_NODELABEL(can1)); #define RX_THREAD_STACK_SIZE 1024 #define RX_THREAD_PRIORITY 2 @@ -53,8 +56,6 @@ void can_isotp_thread() static uint8_t rx_buffer[600]; // large enough to receive a 512k flash page for DFU static uint8_t tx_buffer[1000]; - const struct device* can_dev = device_get_binding(CAN_DEVICE); - enable_can(); if (!device_is_ready(can_dev)) { diff --git a/zephyr/modules/owntech_communication/zephyr/src/thingset_pub_sub.cpp b/zephyr/modules/owntech_communication/zephyr/src/thingset_pub_sub.cpp index 5b25ffb..0116157 100644 --- a/zephyr/modules/owntech_communication/zephyr/src/thingset_pub_sub.cpp +++ b/zephyr/modules/owntech_communication/zephyr/src/thingset_pub_sub.cpp @@ -5,9 +5,11 @@ */ /** + * @date 2022 * @author Martin Jäger <martin@libre.solar> * @author Jean Alinei <jean.alinei@laas.fr> * @author Luiz Villa <luiz.villa@laas.fr> + * @author Clément Foucher <clement.foucher@laas.fr> */ @@ -29,6 +31,7 @@ extern ThingSet ts; extern uint16_t can_node_addr; uint16_t broadcast_time = 10; //the time of the measurement variables broadcast (multiples of 100ms) uint16_t control_time = 10; //the time of the control variables broadcast/receive (multiples of 100ms) +static const struct device* can_dev = DEVICE_DT_GET(DT_NODELABEL(can1)); // below defines should go into the ThingSet library #define TS_CAN_SOURCE_GET(id) (((uint32_t)id & TS_CAN_SOURCE_MASK) >> TS_CAN_SOURCE_POS) @@ -51,8 +54,6 @@ void can_pub_isr(uint32_t err_flags, void *arg) void can_pub_send(uint32_t can_id, uint8_t can_data[8], uint8_t data_len) { - const struct device* can_dev = device_get_binding(CAN_DEVICE); - if (!device_is_ready(can_dev)) { return; @@ -119,8 +120,6 @@ void can_pubsub_thread() { enable_can(); - const struct device* can_dev = device_get_binding(CAN_DEVICE); - if (!device_is_ready(can_dev)) { return; diff --git a/zephyr/modules/owntech_dac_driver/zephyr/public_api/dac.h b/zephyr/modules/owntech_dac_driver/zephyr/public_api/dac.h index bd35c6d..158c19c 100644 --- a/zephyr/modules/owntech_dac_driver/zephyr/public_api/dac.h +++ b/zephyr/modules/owntech_dac_driver/zephyr/public_api/dac.h @@ -38,9 +38,9 @@ extern "C" { ///// // Public devices names -#define DAC1_DEVICE DT_LABEL(DT_NODELABEL(dac1)) -#define DAC2_DEVICE DT_LABEL(DT_NODELABEL(dac2)) -#define DAC3_DEVICE DT_LABEL(DT_NODELABEL(dac3)) +#define DAC1_DEVICE DT_NODELABEL(dac1) +#define DAC2_DEVICE DT_NODELABEL(dac2) +#define DAC3_DEVICE DT_NODELABEL(dac3) ///// diff --git a/zephyr/modules/owntech_data_acquisition/zephyr/dma/dma.c b/zephyr/modules/owntech_data_acquisition/zephyr/dma/dma.c index 20538a6..5faebac 100644 --- a/zephyr/modules/owntech_data_acquisition/zephyr/dma/dma.c +++ b/zephyr/modules/owntech_data_acquisition/zephyr/dma/dma.c @@ -51,10 +51,7 @@ ///// // DT definitions -#define DMA1_NODELABEL DT_NODELABEL(dma1) -#define DMA1_LABEL DT_PROP(DMA1_NODELABEL, label) - -static const struct device* dma1; +static const struct device* dma1 = DEVICE_DT_GET(DT_NODELABEL(dma1)); ///// @@ -154,18 +151,19 @@ static void _dma_channel_init(uint8_t adc_num, uint32_t source_address, uint32_t void dma_configure_and_start(uint8_t adc_count) { - dma1 = device_get_binding(DMA1_LABEL); - half_buffer_1 = k_malloc(adc_count * sizeof(uint16_t*)); half_buffer_2 = k_malloc(adc_count * sizeof(uint16_t*)); - for (uint8_t adc_index = 0 ; adc_index < adc_count ; adc_index++) + if (device_is_ready(dma1) == true) { - uint8_t adc_num = adc_index +1; - if (adc_get_enabled_channels_count(adc_num) > 0) + for (uint8_t adc_index = 0 ; adc_index < adc_count ; adc_index++) { - _dma_channel_init(adc_num, source_registers[adc_index], source_triggers[adc_index]); - dma_start(dma1, adc_num); + uint8_t adc_num = adc_index +1; + if (adc_get_enabled_channels_count(adc_num) > 0) + { + _dma_channel_init(adc_num, source_registers[adc_index], source_triggers[adc_index]); + dma_start(dma1, adc_num); + } } } } diff --git a/zephyr/modules/owntech_hardware_configuration/zephyr/src/dac_configuration.cpp b/zephyr/modules/owntech_hardware_configuration/zephyr/src/dac_configuration.cpp index 2b15ba9..b213ff5 100644 --- a/zephyr/modules/owntech_hardware_configuration/zephyr/src/dac_configuration.cpp +++ b/zephyr/modules/owntech_hardware_configuration/zephyr/src/dac_configuration.cpp @@ -31,31 +31,35 @@ #include "dac.h" +static const struct device* dac1 = DEVICE_DT_GET(DAC1_DEVICE); +static const struct device* dac3 = DEVICE_DT_GET(DAC3_DEVICE); + + void dac_config_dac1_dac3_current_mode_init() { - const struct device* dac1 = device_get_binding(DAC1_DEVICE); - const struct device* dac3 = device_get_binding(DAC3_DEVICE); - - // DAC 1 - dac_function_config_t function_config = + if ( (device_is_ready(dac1) == true) && (device_is_ready(dac3) == true) ) { - .dac_function = dac_function_sawtooth, - .reset_trigger_source = hrtim_trig1, - .step_trigger_source = hrtim_trig1, - .polarity = dac_polarity_decrement, - .reset_data = 4000, - .step_data = 200 - }; - - dac_set_function(dac1, 1, &function_config); - dac_pin_configure(dac1, 1, dac_pin_internal); - dac_start(dac1, 1); - - // DAC 3 - function_config.reset_trigger_source = hrtim_trig2; - function_config.step_trigger_source = hrtim_trig2; - - dac_set_function(dac3, 1, &function_config); - dac_pin_configure(dac3, 1, dac_pin_internal); - dac_start(dac3, 1); + // DAC 1 + dac_function_config_t function_config = + { + .dac_function = dac_function_sawtooth, + .reset_trigger_source = hrtim_trig1, + .step_trigger_source = hrtim_trig1, + .polarity = dac_polarity_decrement, + .reset_data = 4000, + .step_data = 200 + }; + + dac_set_function(dac1, 1, &function_config); + dac_pin_configure(dac1, 1, dac_pin_internal); + dac_start(dac1, 1); + + // DAC 3 + function_config.reset_trigger_source = hrtim_trig2; + function_config.step_trigger_source = hrtim_trig2; + + dac_set_function(dac3, 1, &function_config); + dac_pin_configure(dac3, 1, dac_pin_internal); + dac_start(dac3, 1); + } } diff --git a/zephyr/modules/owntech_hardware_configuration/zephyr/src/hardware_auto_configuration.cpp b/zephyr/modules/owntech_hardware_configuration/zephyr/src/hardware_auto_configuration.cpp index d9167c8..d227309 100644 --- a/zephyr/modules/owntech_hardware_configuration/zephyr/src/hardware_auto_configuration.cpp +++ b/zephyr/modules/owntech_hardware_configuration/zephyr/src/hardware_auto_configuration.cpp @@ -41,6 +41,7 @@ // Owntech driver #include "dac.h" +static const struct device* dac2 = DEVICE_DT_GET(DAC2_DEVICE); ///// // Functions to be run @@ -57,11 +58,12 @@ static int _vrefbuf_init(const struct device* dev) static int _dac2_init(const struct device* dev) { - const struct device* dac2 = device_get_binding(DAC2_DEVICE); - - dac_set_const_value(dac2, 1, 2048U); - dac_pin_configure(dac2, 1, dac_pin_external); - dac_start(dac2, 1); + if (device_is_ready(dac2) == true) + { + dac_set_const_value(dac2, 1, 2048U); + dac_pin_configure(dac2, 1, dac_pin_external); + dac_start(dac2, 1); + } return 0; } diff --git a/zephyr/modules/owntech_hardware_configuration/zephyr/src/ngnd_configuration.cpp b/zephyr/modules/owntech_hardware_configuration/zephyr/src/ngnd_configuration.cpp index 0d6efc6..3012880 100644 --- a/zephyr/modules/owntech_hardware_configuration/zephyr/src/ngnd_configuration.cpp +++ b/zephyr/modules/owntech_hardware_configuration/zephyr/src/ngnd_configuration.cpp @@ -25,25 +25,21 @@ #include "ngnd.h" -static const struct device* ngnd_switch = nullptr; +static const struct device* ngnd_switch = DEVICE_DT_GET(NGND_DEVICE); void ngnd_config_on() { - if (ngnd_switch == NULL) + if (device_is_ready(ngnd_switch) == true) { - ngnd_switch = device_get_binding(NGND_DEVICE); + ngnd_set(ngnd_switch, 1); } - - ngnd_set(ngnd_switch, 1); } void ngnd_config_off() { - if (ngnd_switch == NULL) + if (device_is_ready(ngnd_switch) == true) { - ngnd_switch = device_get_binding(NGND_DEVICE); + ngnd_set(ngnd_switch, 0); } - - ngnd_set(ngnd_switch, 0); } diff --git a/zephyr/modules/owntech_hardware_configuration/zephyr/src/timer_configuration.cpp b/zephyr/modules/owntech_hardware_configuration/zephyr/src/timer_configuration.cpp index 3fa04a6..e0cbd8a 100644 --- a/zephyr/modules/owntech_hardware_configuration/zephyr/src/timer_configuration.cpp +++ b/zephyr/modules/owntech_hardware_configuration/zephyr/src/timer_configuration.cpp @@ -25,7 +25,7 @@ #include "timer.h" -static const struct device* timer4 = NULL; +static const struct device* timer4 = DEVICE_DT_GET(TIMER4_DEVICE); static bool timer4init = false; static bool timer4started = false; @@ -33,17 +33,19 @@ static bool timer4started = false; static void _timer_incremental_encoder_tim4_init() { - // Configure timer - timer4 = device_get_binding(TIMER4_DEVICE); - struct timer_config_t timer_cfg = + if (device_is_ready(timer4) == true) { - .timer_enable_irq = 0, - .timer_enable_encoder = 1, - .timer_enc_pin_mode = pull_up + // Configure timer + struct timer_config_t timer_cfg = + { + .timer_enable_irq = 0, + .timer_enable_encoder = 1, + .timer_enc_pin_mode = pull_up - }; - timer_config(timer4, &timer_cfg); - timer4init = true; + }; + timer_config(timer4, &timer_cfg); + timer4init = true; + } } void timer_incremental_encoder_tim4_start() @@ -55,8 +57,11 @@ void timer_incremental_encoder_tim4_start() if (timer4started == false) { - timer_start(timer4); - timer4started = true; + if (device_is_ready(timer4) == true) + { + timer_start(timer4); + timer4started = true; + } } } diff --git a/zephyr/modules/owntech_hardware_configuration/zephyr/src/uart_configuration.cpp b/zephyr/modules/owntech_hardware_configuration/zephyr/src/uart_configuration.cpp index 58f5462..2c0fe7f 100644 --- a/zephyr/modules/owntech_hardware_configuration/zephyr/src/uart_configuration.cpp +++ b/zephyr/modules/owntech_hardware_configuration/zephyr/src/uart_configuration.cpp @@ -32,18 +32,20 @@ ///// -//USART 1 defines +// USART 1 defines + #define CONFIG_OWNTECH_SERIAL_TX_BUF_SIZE 5 #define CONFIG_OWNTECH_SERIAL_RX_BUF_SIZE 5 #define USART1_DEVICE DT_PROP(DT_NODELABEL(usart1), label) -static const struct device* uart_dev = NULL; +static const struct device* uart_dev = DEVICE_DT_GET(DT_NODELABEL(usart1)); static char buf_req[CONFIG_OWNTECH_SERIAL_RX_BUF_SIZE]; static bool command_flag = false; +///// +// USART 1 private functions - -void _uart_usart1_process_input(const struct device *dev, void* user_data) +static void _uart_usart1_process_input(const struct device *dev, void* user_data) { uint8_t c; @@ -60,6 +62,9 @@ void _uart_usart1_process_input(const struct device *dev, void* user_data) } } +///// +// USART 1 public functions + void uart_usart1_init() { const struct uart_config usart1_config = @@ -71,10 +76,12 @@ void uart_usart1_init() .flow_ctrl = UART_CFG_FLOW_CTRL_NONE }; - uart_dev = device_get_binding(USART1_DEVICE); - uart_configure(uart_dev, &usart1_config); - uart_irq_callback_user_data_set(uart_dev, _uart_usart1_process_input, NULL); - uart_irq_rx_enable(uart_dev); + if (device_is_ready(uart_dev) == true) + { + uart_configure(uart_dev, &usart1_config); + uart_irq_callback_user_data_set(uart_dev, _uart_usart1_process_input, NULL); + uart_irq_rx_enable(uart_dev); + } } char uart_usart1_get_data() @@ -89,7 +96,10 @@ char uart_usart1_get_data() void uart_usart1_write_single(char data) { - uart_poll_out(uart_dev,data); + if (device_is_ready(uart_dev) == true) + { + uart_poll_out(uart_dev,data); + } } void uart_lpuart1_swap_rx_tx() diff --git a/zephyr/modules/owntech_ngnd_driver/zephyr/public_api/ngnd.h b/zephyr/modules/owntech_ngnd_driver/zephyr/public_api/ngnd.h index 5a5e899..3f230c2 100644 --- a/zephyr/modules/owntech_ngnd_driver/zephyr/public_api/ngnd.h +++ b/zephyr/modules/owntech_ngnd_driver/zephyr/public_api/ngnd.h @@ -40,7 +40,7 @@ extern "C" { ///// // Public device name -#define NGND_DEVICE DT_LABEL(DT_NODELABEL(ngnd)) +#define NGND_DEVICE DT_NODELABEL(ngnd) ///// diff --git a/zephyr/modules/owntech_scheduling/zephyr/public_api/Scheduling.cpp b/zephyr/modules/owntech_scheduling/zephyr/public_api/Scheduling.cpp index ed6186c..cc5777c 100644 --- a/zephyr/modules/owntech_scheduling/zephyr/public_api/Scheduling.cpp +++ b/zephyr/modules/owntech_scheduling/zephyr/public_api/Scheduling.cpp @@ -33,7 +33,7 @@ ///// // Static variables -const struct device* Scheduling::timer6 = NULL; +const struct device* Scheduling::timer6 = DEVICE_DT_GET(TIMER6_DEVICE); const int Scheduling::DEFAULT_PRIORITY = 5; @@ -68,10 +68,9 @@ void Scheduling::threadEntryPoint(void* thread_function_p, void*, void*) void Scheduling::startControlTask(void (*periodic_task)(), uint32_t task_period_us) { - if (periodic_task != NULL) + if ( (periodic_task != NULL) && (device_is_ready(timer6) == true) ) { - // Configure timer - timer6 = device_get_binding(TIMER6_DEVICE); + // Configure and start timer struct timer_config_t timer_cfg = {0}; timer_cfg.timer_enable_irq = 1; diff --git a/zephyr/modules/owntech_timer_driver/zephyr/public_api/timer.h b/zephyr/modules/owntech_timer_driver/zephyr/public_api/timer.h index 3957ceb..f65f7a4 100644 --- a/zephyr/modules/owntech_timer_driver/zephyr/public_api/timer.h +++ b/zephyr/modules/owntech_timer_driver/zephyr/public_api/timer.h @@ -51,9 +51,9 @@ extern "C" { ///// // Public devices names -#define TIMER4_DEVICE DT_LABEL(DT_NODELABEL(timers4)) -#define TIMER6_DEVICE DT_LABEL(DT_NODELABEL(timers6)) -#define TIMER7_DEVICE DT_LABEL(DT_NODELABEL(timers7)) +#define TIMER4_DEVICE DT_NODELABEL(timers4) +#define TIMER6_DEVICE DT_NODELABEL(timers6) +#define TIMER7_DEVICE DT_NODELABEL(timers7) ///// -- GitLab