Software APIs
teacup_leds_demo.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #include "sw/device/lib/boards/teacup_v1_3_0/leds.h"
9 #include "sw/device/lib/testing/i2c_testutils.h"
10 #include "sw/device/lib/testing/test_framework/check.h"
12 
14 
15 OTTF_DEFINE_TEST_CONFIG();
16 
17 /**
18  * OT Peripheral Handles.
19  */
20 static dif_i2c_t i2c;
21 static dif_pinmux_t pinmux;
22 
23 /**
24  * Defined Constants.
25  */
26 enum {
27  kLedNumCycles = 100,
28  kLedNumColorsInCycle = 4,
29  kLedCyclePauseMilliseconds = 200,
30  kLedBrightnessLowPercent = 5,
31  kLedBrightnessHighPercent = 40,
32  kLedBrightnessStepPercent = 5,
33 };
34 
35 static const led_rgb_color_t kLedColorBlue = {
36  .r = 0x33,
37  .g = 0x69,
38  .b = 0xE8,
39 };
40 
41 static const led_rgb_color_t kLedColorRed = {
42  .r = 0xD5,
43  .g = 0x0F,
44  .b = 0x25,
45 };
46 
47 static const led_rgb_color_t kLedColorYellow = {
48  .r = 0xEE,
49  .g = 0xB2,
50  .b = 0x11,
51 };
52 
53 static const led_rgb_color_t kLedColorGreen = {
54  .r = 0x00,
55  .g = 0x99,
56  .b = 0x25,
57 };
58 
59 static status_t peripheral_init(void) {
60  // Initialize DIFs.
61  TRY(dif_i2c_init(mmio_region_from_addr(TOP_EARLGREY_I2C0_BASE_ADDR), &i2c));
63  &pinmux));
64 
65  // Initialize pinmux.
74 
75  return OK_STATUS();
76 }
77 
78 static status_t configure_led_i2c_controller(void) {
80  TRY(i2c_testutils_set_speed(&i2c, kDifI2cSpeedFastPlus));
81  TRY(leds_i2c_controller_configure(&i2c));
82  return OK_STATUS();
83 }
84 
85 bool test_main(void) {
86  CHECK_STATUS_OK(peripheral_init());
87  CHECK_STATUS_OK(configure_led_i2c_controller());
88  CHECK_STATUS_OK(leds_turn_all_on(&i2c));
89 
90  // Brightness levels and colors.
91  uint8_t brightness_start =
92  (uint8_t)((float)0xFF * (float)kLedBrightnessLowPercent / 100.0);
93  uint8_t brightness_end =
94  (uint8_t)((float)0xFF * (float)kLedBrightnessHighPercent / 100.0);
95  uint8_t brightness_step =
96  (uint8_t)((float)0xFF * (float)kLedBrightnessStepPercent / 100.0);
97  uint8_t curr_brightness = brightness_start;
98  const led_rgb_color_t kColorCycle[kLedNumColorsInCycle] = {
99  kLedColorBlue,
100  kLedColorRed,
101  kLedColorYellow,
102  kLedColorGreen,
103  };
104 
105  // Cycle through brightness levels and colors.
106  for (size_t i = 0; i < kLedNumCycles; ++i) {
107  for (size_t j = 0; j < kLedNumColorsInCycle; ++j) {
108  CHECK_STATUS_OK(
109  leds_set_color(&i2c, (i + j) % kNumTeacupLeds, kColorCycle[j]));
110  }
111  CHECK_STATUS_OK(leds_set_all_brightness(&i2c, curr_brightness));
112  curr_brightness += brightness_step;
113  if (curr_brightness >= brightness_end ||
114  curr_brightness <= brightness_start) {
115  brightness_step *= -1;
116  }
117  busy_spin_micros(kLedCyclePauseMilliseconds * 1000);
118  }
119 
120  CHECK_STATUS_OK(leds_turn_all_off(&i2c));
121 
122  return true;
123 }