Skip to content

Commit

Permalink
samples: cp9314: Adds a basic sample app for CP9314
Browse files Browse the repository at this point in the history
Adds a sample app demonstrating CP9314 operation

Signed-off-by: Ricardo Rivera-Matos <rriveram@opensource.cirrus.com>
  • Loading branch information
rriveramcrus committed Sep 13, 2023
1 parent 53f8178 commit a38cb6d
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
8 changes: 8 additions & 0 deletions samples/drivers/regulator_cp9314/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(cp9314)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
38 changes: 38 additions & 0 deletions samples/drivers/regulator_cp9314/boards/nucleo_f401re.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2023 Cirrus Logic, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/dt-bindings/gpio/gpio.h>
#include <zephyr/dt-bindings/i2c/i2c.h>
#include <zephyr/dt-bindings/regulator/cp9314.h>

&arduino_i2c {
status = "okay";
clock-frequency = <I2C_BITRATE_STANDARD>;

cp9314: cp9314@72 {
compatible = "cirrus,cp9314";
reg = <0x72>;
status = "okay";

regulator-initial-mode = <CP9314_MODE_2TO1>;
regulator-allowed-modes = <CP9314_MODE_2TO1 CP9314_MODE_3TO1>;

cirrus,en-gpios = <&arduino_header 8 GPIO_ACTIVE_HIGH>;
cirrus,sync-role = "host";
};

cp9314_dev2: cp9314@73 {
compatible = "cirrus,cp9314";
reg = <0x73>;
status = "okay";

regulator-initial-mode = <CP9314_MODE_2TO1>;
regulator-allowed-modes = <CP9314_MODE_2TO1 CP9314_MODE_3TO1>;

cirrus,en-gpios = <&arduino_header 8 GPIO_ACTIVE_HIGH>;
cirrus,sync-role = "device-2";
};
};
5 changes: 5 additions & 0 deletions samples/drivers/regulator_cp9314/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CONFIG_I2C=y
CONFIG_LOG=y
CONFIG_REGULATOR=y
CONFIG_REGULATOR_CP9314=y
CONFIG_CBPRINTF_FP_SUPPORT=y
107 changes: 107 additions & 0 deletions samples/drivers/regulator_cp9314/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2023 Cirrus Logic, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "zephyr/sys/printk.h"
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/regulator.h>

int main(void)
{
const struct device *dev2 = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(cp9314_dev2));
const struct device *host = DEVICE_DT_GET(DT_NODELABEL(cp9314));
uint8_t mode;
int ret;

if (host == NULL) {
printk("No host CP9314 found...\n");
return -EIO;
} else if (dev2 == NULL) {
printk("No secondary CP9314 found, assuming standalone operation\n");
}

if (!device_is_ready(host)) {
printk("\nError: Device \"%s\" is not ready; "
"check the driver initialization logs for errors.\n",
host->name);
return -EIO;
}

printk("Found device \"%s\", getting regulator data\n", host->name);

ret = regulator_get_mode(host, &mode);
if (ret < 0) {
printk("Failed to get \"%s\" regulator mode:%d\n", host->name, ret);
return -EIO;
}

printk("\"%s\" op mode is:%d\n", host->name, mode);

if (dev2) {
if (!device_is_ready(dev2)) {
printk("\nError: Device \"%s\" is not ready; "
"check the driver initialization logs for errors.\n",
dev2->name);
return -EIO;
}

printk("Found device \"%s\", getting regulator data\n", dev2->name);

ret = regulator_get_mode(dev2, &mode);
if (ret < 0) {
printk("Failed to get \"%s\" regulator mode:%d\n", dev2->name, ret);
return -EIO;
}

printk("\"%s\" op mode is:%d\n", dev2->name, mode);
}

while (1) {
printk("Found device %s\n", host->name);
if (dev2) {
printk("Found device %s\n", dev2->name);
}

ret = regulator_enable(host);
if (ret < 0) {
printk("Error enabling regulator: %d\n", ret);
return ret;
}

if (dev2) {
ret = regulator_enable(dev2);
if (ret < 0) {
printk("Error enabling regulator: %d\n", ret);
return ret;
}
}

printk("Converter(s) enabled\n");

k_sleep(K_SECONDS(10));

if (dev2) {
ret = regulator_disable(dev2);
if (ret < 0) {
printk("Error disabling regulator: %d\n", ret);
return ret;
}
}

ret = regulator_disable(host);
if (ret < 0) {
printk("Error disabling regulator: %d\n", ret);
return ret;
}

printk("Converter(s) disabled\n");

k_sleep(K_SECONDS(10));
}

return 0;
}

0 comments on commit a38cb6d

Please sign in to comment.