-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Bcf: Fix sensor shell example #64693
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
carlescufi
merged 2 commits into
zephyrproject-rtos:main
from
Ayush1325:bcf-sensor-shell
Nov 9, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * Copyright (c) 2023 Ayush Singh <[email protected]> | ||
| * Copyright (c) 2021 Jason Kridner, BeagleBoard.org Foundation | ||
Ayush1325 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * Copyright (c) 2020 Innoseis BV | ||
| * | ||
| * Based on i2c_tca9656a.c | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #define DT_DRV_COMPAT gpio_i2c_switch | ||
|
|
||
| #define GPIO_I2C_TOGGLE_DELAY_US 1 | ||
| #define GPIO_I2C_LOCK_TIMEOUT_US (GPIO_I2C_TOGGLE_DELAY_US * 2 + 100) | ||
|
|
||
| #include <zephyr/kernel.h> | ||
| #include <zephyr/device.h> | ||
| #include <zephyr/devicetree.h> | ||
| #include <zephyr/drivers/i2c.h> | ||
| #include <zephyr/drivers/gpio.h> | ||
| #include <zephyr/logging/log.h> | ||
|
|
||
| LOG_MODULE_REGISTER(gpio_i2c_switch, CONFIG_I2C_LOG_LEVEL); | ||
|
|
||
| struct gpio_i2c_switch_config { | ||
| const struct device *bus; | ||
| const struct gpio_dt_spec gpio; | ||
| }; | ||
|
|
||
| struct gpio_i2c_switch_data { | ||
| struct k_mutex lock; | ||
| }; | ||
|
|
||
| static int gpio_i2c_switch_configure(const struct device *dev, uint32_t dev_config) | ||
| { | ||
| const struct gpio_i2c_switch_config *config = dev->config; | ||
|
|
||
| return i2c_configure(config->bus, dev_config); | ||
| } | ||
|
|
||
| static int gpio_i2c_switch_transfer(const struct device *dev, struct i2c_msg *msgs, | ||
| uint8_t num_msgs, uint16_t addr) | ||
| { | ||
| int res; | ||
| struct gpio_i2c_switch_data *data = dev->data; | ||
| const struct gpio_i2c_switch_config *config = dev->config; | ||
|
|
||
| res = k_mutex_lock(&data->lock, K_USEC(GPIO_I2C_LOCK_TIMEOUT_US)); | ||
| if (res != 0) { | ||
| return res; | ||
fabiobaltieri marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /* enable switch */ | ||
| gpio_pin_configure_dt(&config->gpio, GPIO_OUTPUT_HIGH); | ||
| k_busy_wait(GPIO_I2C_TOGGLE_DELAY_US); | ||
|
|
||
| res = i2c_transfer(config->bus, msgs, num_msgs, addr); | ||
|
|
||
| /* disable switch */ | ||
| gpio_pin_configure_dt(&config->gpio, GPIO_OUTPUT_LOW); | ||
| k_busy_wait(GPIO_I2C_TOGGLE_DELAY_US); | ||
| k_mutex_unlock(&data->lock); | ||
|
|
||
| return res; | ||
fabiobaltieri marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| const struct i2c_driver_api gpio_i2c_switch_api_funcs = { | ||
| .configure = gpio_i2c_switch_configure, | ||
| .transfer = gpio_i2c_switch_transfer, | ||
| }; | ||
|
|
||
| static int gpio_i2c_switch_init(const struct device *dev) | ||
| { | ||
| const struct gpio_i2c_switch_config *config = dev->config; | ||
| struct gpio_i2c_switch_data *data = dev->data; | ||
|
|
||
| k_mutex_init(&data->lock); | ||
|
|
||
Ayush1325 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return gpio_pin_configure_dt(&config->gpio, GPIO_OUTPUT_LOW); | ||
| } | ||
|
|
||
| #define DEFINE_GPIO_I2C_SWITCH(inst) \ | ||
| \ | ||
| static struct gpio_i2c_switch_data gpio_i2c_switch_dev_data_##inst; \ | ||
| \ | ||
| static const struct gpio_i2c_switch_config gpio_i2c_switch_dev_cfg_##inst = { \ | ||
| .bus = DEVICE_DT_GET(DT_PHANDLE(DT_DRV_INST(inst), controller)), \ | ||
| .gpio = GPIO_DT_SPEC_GET(DT_DRV_INST(inst), gpios), \ | ||
| }; \ | ||
| \ | ||
| DEVICE_DT_INST_DEFINE(inst, gpio_i2c_switch_init, device_pm_control_nop, \ | ||
| &gpio_i2c_switch_dev_data_##inst, &gpio_i2c_switch_dev_cfg_##inst, \ | ||
| POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, &gpio_i2c_switch_api_funcs); | ||
|
|
||
| DT_INST_FOREACH_STATUS_OKAY(DEFINE_GPIO_I2C_SWITCH) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Copyright (c) 2023, Ayush Singh <[email protected]> | ||
| # Copyright (c) 2021, Jason Kridner, BeagleBoard.org Foundation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| description: | | ||
| GPIO enabled analog switch to isolate devices from an I2C bus | ||
|
|
||
| compatible: "gpio-i2c-switch" | ||
|
|
||
| include: i2c-controller.yaml | ||
|
|
||
| properties: | ||
| "#address-cells": | ||
| required: true | ||
| const: 1 | ||
| "#size-cells": | ||
| required: true | ||
| const: 0 | ||
| controller: | ||
| type: phandle | ||
| required: true | ||
| gpios: | ||
| type: phandle-array | ||
| required: true |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.