Skip to content

Commit

Permalink
samples: boards: firmware for Twinkie V2
Browse files Browse the repository at this point in the history
sample firmware for Twinkie V2 that toggles the LED based on charging
status.

Signed-off-by: Jason Yuan <jasonyuan@google.com>
  • Loading branch information
ualbertagreen committed Jul 6, 2023
1 parent f60d8f5 commit fb10b29
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 17 deletions.
8 changes: 2 additions & 6 deletions boards/arm/google_twinkie_v2/board.cmake
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
# SPDX-License-Identifier: Apache-2.0

board_runner_args(dfu-util "--pid=0483:df11" "--alt=0" "--dfuse")

include(${ZEPHYR_BASE}/boards/common/dfu-util.board.cmake)

board_runner_args(stm32cubeprogrammer "--port=swd" "--reset=hw")

include(${ZEPHYR_BASE}/boards/common/stm32cubeprogrammer.board.cmake)

board_runner_args(jlink "--device=STM32G0B1RE" "--speed=4000")

include(${ZEPHYR_BASE}/boards/common/dfu-util.board.cmake)
include(${ZEPHYR_BASE}/boards/common/stm32cubeprogrammer.board.cmake)
include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake)
2 changes: 0 additions & 2 deletions boards/arm/google_twinkie_v2/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ Overview
Google Twinkie V2 is a reference board for the google power delivery analyzer
(PDA) Twinkie V2.

Zephyr has support for the STM32-based embedded controller (EC) on-board.

Hardware
********

Expand Down
17 changes: 8 additions & 9 deletions boards/arm/google_twinkie_v2/google_twinkie_v2.dts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@

gpio_keys {
compatible = "gpio-keys";

/* does not go to an actual button in current hardware.
* short TP5 to TP6 to activate.
*/
dfu_detect: dfudetect {
gpios = <&gpioa 14 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
};
Expand Down Expand Up @@ -70,11 +74,7 @@
sense-gain-mult = <25>;
};


aliases {
led0 = &red_led_0;
led1 = &green_led_1;
led2 = &blue_led_2;
bootloader-led0 = &blue_led_2;
vcc1 = &cc1_buf;
vcc2 = &cc2_buf;
Expand All @@ -84,7 +84,6 @@
};
};


&adc1 {
#address-cells = <1>;
#size-cells = <0>;
Expand All @@ -96,6 +95,9 @@
&adc1_in18_pc5 /* CSA_CC2 */
>;

pinctrl-names = "default";
status = "okay";

channel@1 {
reg = <1>;
zephyr,gain = "ADC_GAIN_1";
Expand Down Expand Up @@ -140,9 +142,6 @@
zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
zephyr,resolution = <12>;
};

pinctrl-names = "default";
status = "okay";
};

&clk_hsi {
Expand Down Expand Up @@ -175,6 +174,6 @@

psc-ucpdclk = <1>;
hbitclkdiv = <27>;
pinctrl-names = "default";
pinctrl-0 = <&ucpd1_cc1_pa8 &ucpd1_cc2_pb15>;
pinctrl-names = "default";
};
9 changes: 9 additions & 0 deletions samples/boards/google_twinkie_v2_pda/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(google_twinkie_v2)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
25 changes: 25 additions & 0 deletions samples/boards/google_twinkie_v2_pda/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.. _google_twinkie_v2_pda:

Twinkie Power Delivery
######################

Overview
********

This provides access to :ref:`Twinkie <google_twinkie_v2_board>` so you can try out
the supported features.

Building and Running
********************

Build and flash Twinkie as follows:

.. zephyr-app-commands::
:zephyr-app: samples/boards/google_pda
:board: google_twinkie_v2
:goals: build flash
:compact:

After flashing, the LED will start red. Putting the Twinkie in between any
usbc connection will cause the LED to turn blue. The LED will turn green instead
if the device is currently charging.
4 changes: 4 additions & 0 deletions samples/boards/google_twinkie_v2_pda/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONFIG_ADC=y
CONFIG_ADC_STM32=y
CONFIG_GPIO=y
CONFIG_LED=y
6 changes: 6 additions & 0 deletions samples/boards/google_twinkie_v2_pda/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sample:
name: Twinkie V2 usb Vbus snooper
tests:
sample.board.google_twinkie_v2:
platform_allow: google_twinkie_v2
tags: usb
102 changes: 102 additions & 0 deletions samples/boards/google_twinkie_v2_pda/src/meas.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2023 The ChromiumOS Authors
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdio.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/adc.h>
#include <zephyr/drivers/adc/voltage_divider.h>
#include <zephyr/drivers/adc/current_sense_amplifier.h>

/* The devicetree node identifier for the adc aliases. */
#define VBUS_V_MEAS_NODE DT_ALIAS(vbus)
#define VBUS_C_MEAS_NODE DT_ALIAS(cbus)

static const struct voltage_divider_dt_spec adc_vbus_v =
VOLTAGE_DIVIDER_DT_SPEC_GET(VBUS_V_MEAS_NODE);
static const struct current_sense_amplifier_dt_spec adc_vbus_c =
CURRENT_SENSE_AMPLIFIER_DT_SPEC_GET(VBUS_C_MEAS_NODE);

int meas_vbus_v(int32_t *v)
{
int ret;
int32_t sample_buffer = 0;

/* Structure defining an ADC sampling sequence */
struct adc_sequence sequence = {
.buffer = &sample_buffer,
/* buffer size in bytes, not number of samples */
.buffer_size = sizeof(sample_buffer),
.calibrate = true,
};
adc_sequence_init_dt(&adc_vbus_v.port, &sequence);

ret = adc_read(adc_vbus_v.port.dev, &sequence);
if (ret != 0) {
return ret;
}

*v = sample_buffer;
ret = adc_raw_to_millivolts_dt(&adc_vbus_v.port, v);
if (ret != 0) {
return ret;
}

ret = voltage_divider_scale_dt(&adc_vbus_v, v);
if (ret != 0) {
return ret;
}

return 0;
}

int meas_vbus_c(int32_t *c)
{
int ret;
int32_t sample_buffer = 0;

/* Structure defining an ADC sampling sequence */
struct adc_sequence sequence = {
.buffer = &sample_buffer,
/* buffer size in bytes, not number of samples */
.buffer_size = sizeof(sample_buffer),
.calibrate = true,
};
adc_sequence_init_dt(&adc_vbus_c.port, &sequence);

ret = adc_read(adc_vbus_c.port.dev, &sequence);
if (ret != 0) {
return ret;
}

*c = sample_buffer;
ret = adc_raw_to_millivolts_dt(&adc_vbus_c.port, c);
if (ret != 0) {
return ret;
}

/* prescaling the voltage offset */
*c -= adc_vbus_c.port.vref_mv / 2;
current_sense_amplifier_scale_dt(&adc_vbus_c, c);

return 0;
}

int meas_init(void)
{
int ret;

ret = adc_channel_setup_dt(&adc_vbus_v.port);
if (ret != 0) {
return ret;
}

ret = adc_channel_setup_dt(&adc_vbus_c.port);
if (ret != 0) {
return ret;
}

return 0;
}
36 changes: 36 additions & 0 deletions samples/boards/google_twinkie_v2_pda/src/meas.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2023 The ChromiumOS Authors
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef __MEAS_H__
#define __MEAS_H__

/**
* @brief Initializes the measurement module, sets up all the adc channels through the device tree
* binding
*
* @return 0 on success
*/
int meas_init(void);

/**
* @brief Measure the voltage on VBUS
*
* @param v pointer where VBUS voltage, in millivolts, is stored
*
* @return 0 on success
*/
int meas_vbus_v(int32_t *v);

/**
* @brief Measure the current on VBUS
*
* @param c pointer where VBUS current, in milliamperes, is stored
*
* @return 0 on success
*/
int meas_vbus_c(int32_t *c);

#endif
58 changes: 58 additions & 0 deletions samples/boards/google_twinkie_v2_pda/src/view.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2023 The ChromiumOS Authors
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/device.h>
#include <zephyr/drivers/led.h>
#include <zephyr/kernel.h>

#include "meas.h"

enum led_color_t {
LED_RED,
LED_GREEN,
LED_BLUE,
};

static void set_led(const struct device *const led, enum led_color_t led_color)
{
if (led_color != LED_RED) led_off(led, LED_RED);

Check failure on line 21 in samples/boards/google_twinkie_v2_pda/src/view.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

TRAILING_STATEMENTS

samples/boards/google_twinkie_v2_pda/src/view.c:21 trailing statements should be on next line
if (led_color != LED_GREEN) led_off(led, LED_GREEN);

Check failure on line 22 in samples/boards/google_twinkie_v2_pda/src/view.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

TRAILING_STATEMENTS

samples/boards/google_twinkie_v2_pda/src/view.c:22 trailing statements should be on next line
if (led_color != LED_BLUE) led_off(led, LED_BLUE);

Check failure on line 23 in samples/boards/google_twinkie_v2_pda/src/view.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

TRAILING_STATEMENTS

samples/boards/google_twinkie_v2_pda/src/view.c:23 trailing statements should be on next line
led_on(led, led_color);
}

#define CHARGING_VOLTAGE 5000
#define CHARGING_CURRENT 1000

void main(void)
{
meas_init();

const struct device *const led = DEVICE_DT_GET_ONE(gpio_leds);
int32_t vbus_v = 0;
int32_t vbus_c = 0;

if (!device_is_ready(led)) {
return;
}

while (1) {
meas_vbus_v(&vbus_v);
meas_vbus_c(&vbus_c);

if (vbus_v > CHARGING_VOLTAGE) {
if (vbus_c > CHARGING_CURRENT) {
set_led(led, LED_GREEN);
} else {
set_led(led, LED_BLUE);
}
} else {
set_led(led, LED_RED);
}

k_usleep(500);
}
}

0 comments on commit fb10b29

Please sign in to comment.