From 63de5735dd94d95b830e54233218ffe0975e08a7 Mon Sep 17 00:00:00 2001 From: Karsten Koenig Date: Thu, 7 Nov 2019 20:16:06 +0100 Subject: [PATCH 1/2] drivers: can: mcp2515: Switch to new GPIO API Mark the INT signal to be active low and use the new functions to get gpio state and configure the gpio interrupt flanks. Signed-off-by: Karsten Koenig --- .../dfrobot_can_bus_v2_0.overlay | 2 +- .../link_board_can/link_board_can.overlay | 2 +- drivers/can/can_mcp2515.c | 18 +++++++++--------- dts/bindings/can/microchip,mcp2515.yaml | 6 ++++++ 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/boards/shields/dfrobot_can_bus_v2_0/dfrobot_can_bus_v2_0.overlay b/boards/shields/dfrobot_can_bus_v2_0/dfrobot_can_bus_v2_0.overlay index 5acf843b3972f..c556584492b89 100644 --- a/boards/shields/dfrobot_can_bus_v2_0/dfrobot_can_bus_v2_0.overlay +++ b/boards/shields/dfrobot_can_bus_v2_0/dfrobot_can_bus_v2_0.overlay @@ -11,7 +11,7 @@ mcp2515@0 { compatible = "microchip,mcp2515"; spi-max-frequency = <1000000>; - int-gpios = <&arduino_header 8 0>; /* D2 */ + int-gpios = <&arduino_header 8 GPIO_ACTIVE_LOW>; /* D2 */ status = "okay"; label = "CAN_1"; reg = <0x0>; diff --git a/boards/shields/link_board_can/link_board_can.overlay b/boards/shields/link_board_can/link_board_can.overlay index 66ee34c4090b3..72805cd280f21 100644 --- a/boards/shields/link_board_can/link_board_can.overlay +++ b/boards/shields/link_board_can/link_board_can.overlay @@ -14,7 +14,7 @@ mcp2515@0 { compatible = "microchip,mcp2515"; spi-max-frequency = <1000000>; - int-gpios = <&gpio1 7 GPIO_INT_ACTIVE_LOW>; + int-gpios = <&gpio1 7 GPIO_ACTIVE_LOW>; status = "okay"; label = "CAN_1"; reg = <0>; diff --git a/drivers/can/can_mcp2515.c b/drivers/can/can_mcp2515.c index 155be404ea07d..12dd2de3e79b0 100644 --- a/drivers/can/can_mcp2515.c +++ b/drivers/can/can_mcp2515.c @@ -639,11 +639,10 @@ static void mcp2515_handle_interrupts(struct device *dev) { const struct mcp2515_config *dev_cfg = DEV_CFG(dev); struct mcp2515_data *dev_data = DEV_DATA(dev); - u32_t pin; int ret; u8_t canintf; - /* Loop until INT pin is high (all interrupt flags handled) */ + /* Loop until INT pin is inactive (all interrupt flags handled) */ while (1) { ret = mcp2515_cmd_read_reg(dev, MCP2515_ADDR_CANINTF, &canintf, 1); @@ -693,11 +692,11 @@ static void mcp2515_handle_interrupts(struct device *dev) canintf, ~canintf); } - /* Break from loop if INT pin is no longer low */ - ret = gpio_pin_read(dev_data->int_gpio, dev_cfg->int_pin, &pin); - if (ret != 0) { + /* Break from loop if INT pin is inactive */ + ret = gpio_pin_get(dev_data->int_gpio, dev_cfg->int_pin); + if (ret < 0) { LOG_ERR("Couldn't read INT pin"); - } else if (pin != 0) { + } else if (ret == 0) { /* All interrupt flags handled */ break; } @@ -790,8 +789,8 @@ static int mcp2515_init(struct device *dev) } if (gpio_pin_configure(dev_data->int_gpio, dev_cfg->int_pin, - (GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE - | GPIO_INT_ACTIVE_LOW | GPIO_INT_DEBOUNCE))) { + (GPIO_INPUT | + DT_INST_0_MICROCHIP_MCP2515_INT_GPIOS_FLAGS))) { LOG_ERR("Unable to configure GPIO pin %u", dev_cfg->int_pin); return -EINVAL; } @@ -803,7 +802,8 @@ static int mcp2515_init(struct device *dev) return -EINVAL; } - if (gpio_pin_enable_callback(dev_data->int_gpio, dev_cfg->int_pin)) { + if (gpio_pin_interrupt_configure(dev_data->int_gpio, dev_cfg->int_pin, + GPIO_INT_EDGE_TO_ACTIVE)) { return -EINVAL; } diff --git a/dts/bindings/can/microchip,mcp2515.yaml b/dts/bindings/can/microchip,mcp2515.yaml index 531967d9a9270..85dbe4df71050 100644 --- a/dts/bindings/can/microchip,mcp2515.yaml +++ b/dts/bindings/can/microchip,mcp2515.yaml @@ -18,6 +18,12 @@ properties: int-gpios: type: phandle-array required: true + description: > + Interrupt pin. + + This pin signals active low when produced by the controller. The + property value should ensure the flags properly describe the signal + that is presented to the driver. reg: type: array required: true From b6e136d5bc324464a35063f21b796375c7f9cf40 Mon Sep 17 00:00:00 2001 From: Karsten Koenig Date: Thu, 21 Nov 2019 23:27:34 +0100 Subject: [PATCH 2/2] samples: drivers: CAN: Switch to new GPIO API Switched from deprecated gpio_pin_write to gpio_pin_set and also add the LED GPIO flags to the gpio configuration. Signed-off-by: Karsten Koenig --- samples/drivers/CAN/src/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/drivers/CAN/src/main.c b/samples/drivers/CAN/src/main.c index b315c1bd13700..77d4a1572da7d 100644 --- a/samples/drivers/CAN/src/main.c +++ b/samples/drivers/CAN/src/main.c @@ -90,10 +90,10 @@ void change_led(struct zcan_frame *msg, void *led_dev_param) switch (msg->data[0]) { case SET_LED: - gpio_pin_write(led_dev, DT_ALIAS_LED0_GPIOS_PIN, 1); + gpio_pin_set(led_dev, DT_ALIAS_LED0_GPIOS_PIN, 1); break; case RESET_LED: - gpio_pin_write(led_dev, DT_ALIAS_LED0_GPIOS_PIN, 0); + gpio_pin_set(led_dev, DT_ALIAS_LED0_GPIOS_PIN, 0); break; } #else @@ -220,7 +220,7 @@ void main(void) } ret = gpio_pin_configure(led_gpio_dev, DT_ALIAS_LED0_GPIOS_PIN, - GPIO_DIR_OUT); + GPIO_OUTPUT_HIGH | DT_ALIAS_LED0_GPIOS_FLAGS); if (ret < 0) { printk("Error setting LED pin to output mode [%d]", ret); }