Skip to content

Commit

Permalink
[sw] Add test for RGB LED controller
Browse files Browse the repository at this point in the history
  • Loading branch information
GregAC authored and marnovandermaas committed May 28, 2024
1 parent cb14229 commit 2d5aa1f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions sw/cheri/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(TESTS
uart_check.cc
spi_test.cc
revocation_test.cc
rgbled_test.cc
)

foreach(TEST ${TESTS})
Expand Down
98 changes: 98 additions & 0 deletions sw/cheri/tests/rgbled_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright lowRISC contributors.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

#define CHERIOT_NO_AMBIENT_MALLOC
#define CHERIOT_NO_NEW_DELETE

#include "../../common/defs.h"

#include <cheri.hh>
#include <riscvreg.h>
#include <stdint.h>

using namespace CHERI;

struct SonataRGBLEDCtrl
{
uint32_t rgbled0;
uint32_t rgbled1;
uint32_t ctrl;
uint32_t status;

void wait_idle() volatile
{
while ((status & 0x1) == 0);
}

void set_rgb(uint8_t r, uint8_t g, uint8_t b, uint32_t led) volatile
{
uint32_t rgb;

rgb = (uint32_t)r | ((uint32_t)g << 8) | ((uint32_t)b << 16);

wait_idle();

if (led == 0) {
rgbled0 = rgb;
} else if (led == 1) {
rgbled1 = rgb;
}
}

void update() volatile
{
wait_idle();
ctrl = 0x1;
}

void clear() volatile
{
wait_idle();
ctrl = 0x2;
}
};

void busy_wait(uint64_t cycles)
{
uint64_t end = rdcycle64() + cycles;
while(rdcycle64() < end);
}

/**
* C++ entry point for the loader. This is called from assembly, with the
* read-write root in the first argument.
*/
[[noreturn]] extern "C" void rom_loader_entry(void *rwRoot)
{
Capability<void> root{rwRoot};

Capability<volatile SonataRGBLEDCtrl> rgbled_ctrl =
root.cast<volatile SonataRGBLEDCtrl>();
rgbled_ctrl.address() = RGBLED_CTRL_ADDRESS;
rgbled_ctrl.bounds() = RGBLED_CTRL_BOUNDS;

while (1) {
rgbled_ctrl->set_rgb(32, 0, 0, 1);
rgbled_ctrl->set_rgb(0, 32, 0, 0);
rgbled_ctrl->update();

busy_wait(20000000);

rgbled_ctrl->set_rgb(0, 32, 0, 1);
rgbled_ctrl->set_rgb(0, 0, 32, 0);
rgbled_ctrl->update();

busy_wait(20000000);

rgbled_ctrl->set_rgb(0, 0, 32, 1);
rgbled_ctrl->set_rgb(32, 0, 0, 0);
rgbled_ctrl->update();

busy_wait(20000000);

rgbled_ctrl->clear();

busy_wait(20000000);
}
}

0 comments on commit 2d5aa1f

Please sign in to comment.