Skip to content
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

bloodlight_based.c: Apply ztest #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ This is a work in progress section. Currently, in order to be able to use the bl
```

Example of the Zephyr environment variable settings:
```

```sh
export ZEPHYR_TOOLCHAIN_VARIANT=zephyr
export ZEPHYR_SDK_INSTALL_DIR="/opt/zephyr-sdk/"
export ZEPHYR_BASE=/home/rdale/projects/co054/src/zephyrproject/zephyr
Expand Down Expand Up @@ -54,3 +55,19 @@ Now you can run some basic tests to make sure the LEDs and USB work:
The idea is to have a `Zephyr` out of tree app. To achieve this we will first need to establish a connection between our board support files and zephyr and then between our app and zephyr.

The second task won't be hard, but the first one it might be a bit more tricky, since so far I haven't found an example of a running app that uses a board that is not supported by zephyr.

## Testing

In order to test all the available test for this project execute the next:

```sh
cd $ZEPHYR_BASE
./scripts/twister --list-tests -T <bloodlight-zephr directory path>/tests
```

To run all tests:

```sh
cd $ZEPHYR_BASE
./scripts/twister --device-testing --device-serial /dev/ttyACM2 -p bloodlight_rev2 -T <bloodlight-zephr directory path>/tests --board-root=~/<bloodlight-zephr directory path>/boards
```
2 changes: 2 additions & 0 deletions boards/arm/bloodlight_rev2/bloodlight_rev2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ supported:
- spi
- watchdog
- adc
testing:
default: true
156 changes: 80 additions & 76 deletions tests/led/bloodlight_based.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,12 @@
#include <usb/usb_device.h>
#include "../../common/led.h"
#include "../../src/led.h"
#include <ztest.h>

#define SLEEP_TIME_MS 1000
#define LOOPS 10
/* The devicetree node identifier for the "led0" alias. */
#define STATUS_NODE DT_ALIAS(statusled)

#if DT_NODE_HAS_STATUS(STATUS_NODE, okay)
#define STATUS_LED DT_GPIO_LABEL(STATUS_NODE, gpios)
#define STATUS_PIN DT_GPIO_PIN(STATUS_NODE, gpios)
#define STATUS_FLAGS DT_GPIO_FLAGS(STATUS_NODE, gpios)
#else
/* A build error here means your board isn't set up to blink an LED. */
#error "Unsupported board: led0 devicetree alias is not defined"
#define STATUS_LED ""
#define STATUS_PIN 0
#define STATUS_FLAGS 0
#endif

void main (void) {
const struct device *dev = device_get_binding(
CONFIG_UART_CONSOLE_ON_DEV_NAME);
uint32_t dtr = 0;

if (usb_enable(NULL)) {
return;
}

while (!dtr) {
uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
}

if (strlen(CONFIG_UART_CONSOLE_ON_DEV_NAME) !=
strlen("CDC_ACM_0") ||
strncmp(CONFIG_UART_CONSOLE_ON_DEV_NAME, "CDC_ACM_0",
strlen(CONFIG_UART_CONSOLE_ON_DEV_NAME))) {
printk("Error: Console device name is not USB ACM\n");

return;
}

printf("\n*****Starting LED APIs tests****\n");
static void test_get_port(void) {

printf("\nTesting bl_led_get_port\n");
for (int led = 0; led < BL_LED_COUNT; led++){
Expand All @@ -63,7 +28,7 @@ void main (void) {
case 5:
case 6:
case 7:
__ASSERT(((GPIO_TypeDef *) GPIOA_BASE) == bl_led_get_port(led), "Invalid port value in led %i\n", led);
zassert_equal(((GPIO_TypeDef *) GPIOA_BASE), bl_led_get_port(led), "Invalid port value in led %i\n", led);
break;
case 0:
case 1:
Expand All @@ -74,12 +39,12 @@ void main (void) {
case 13:
case 14:
case 15:
__ASSERT(((GPIO_TypeDef *) GPIOB_BASE) == bl_led_get_port(led), "Invalid port value in led %i\n", led);
zassert_equal(((GPIO_TypeDef *) GPIOB_BASE), bl_led_get_port(led), "Invalid port value in led %i\n", led);
break;
case 8:
case 9:
case 10:
__ASSERT(((GPIO_TypeDef *) GPIOC_BASE) == bl_led_get_port(led), "Invalid port value in led %i\n", led);
zassert_equal(((GPIO_TypeDef *) GPIOC_BASE), bl_led_get_port(led), "Invalid port value in led %i\n", led);
break;
#else
case 1:
Expand All @@ -91,140 +56,145 @@ void main (void) {
case 13:
case 14:
case 15:
__ASSERT(((GPIO_TypeDef *) GPIOA_BASE) == bl_led_get_port(led), "Invalid port value in led %i\n", led);
zassert_equal(((GPIO_TypeDef *) GPIOA_BASE), bl_led_get_port(led), "Invalid port value in led %i\n", led);
break;
case 2:
case 9:
case 10:
case 12:
__ASSERT(((GPIO_TypeDef *) GPIOB_BASE) == bl_led_get_port(led), "Invalid port value in led %i\n", led);
zassert_equal(((GPIO_TypeDef *) GPIOB_BASE), bl_led_get_port(led), "Invalid port value in led %i\n", led);
break;
case 0:
case 3:
case 8:
__ASSERT(((GPIO_TypeDef *) GPIOC_BASE) == bl_led_get_port(led), "Invalid port value in led %i\n", led);
zassert_equal(((GPIO_TypeDef *) GPIOC_BASE), bl_led_get_port(led), "Invalid port value in led %i\n", led);
break;
#endif
}
}
printf("\nSuccess testing bl_led_get_port\n");
}


static void test_get_gpio(void) {
printf("\nTesting bl_led_get_gpio\n");
for (int led = 0; led < BL_LED_COUNT; led++){
switch (led)
{
#if (BL_REVISION == 1)
case 0:
__ASSERT((1U << 1) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 1), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 1:
__ASSERT((1U << 2) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 2), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 2:
__ASSERT((1U << 10) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 10), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 3:
__ASSERT((1U << 11) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 11), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 4:
__ASSERT((1U << 8) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 8), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 5:
__ASSERT((1U << 9) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 9), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 6:
__ASSERT((1U << 10) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 10), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 7:
__ASSERT((1U << 15) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 15), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 8:
__ASSERT((1U << 13) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 13), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 9:
__ASSERT((1U << 14) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 14), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 10:
__ASSERT((1U << 15) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 15), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 11:
__ASSERT((1U << 9) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 9), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 12:
__ASSERT((1U << 8) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 8), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 13:
__ASSERT((1U << 7) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 7), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 14:
__ASSERT((1U << 6) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 6), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 15:
__ASSERT((1U << 5) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 5), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
#else
case 0:
__ASSERT((1U << 14) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 14), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 1:
__ASSERT((1U << 10) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 10), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 2:
__ASSERT((1U << 11) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 11), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 3:
__ASSERT((1U << 13) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 13), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 4:
__ASSERT((1U << 6) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 6), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 5:
__ASSERT((1U << 8) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 8), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 6:
__ASSERT((1U << 9) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 9), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 7:
__ASSERT((1U << 5) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 5), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 8:
__ASSERT((1U << 15) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 15), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 9:
__ASSERT((1U << 6) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 6), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 10:
__ASSERT((1U << 5) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 5), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 11:
__ASSERT((1U << 0) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 0), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 12:
__ASSERT((1U << 4) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 4), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 13:
__ASSERT((1U << 15) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 15), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 14:
__ASSERT((1U << 1) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 1), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
case 15:
__ASSERT((1U << 2) == bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
zassert_equal((1U << 2), bl_led_get_gpio(led), "Invalid pin value in led %i\n", led);
break;
#endif
}
}
printf("\nSuccess testing bl_led_get_gpio\n");
}

static void test_status(void) {
printf("\nTesting bl_led_status_set(), please check for a red led blinking\n");
for (int i = 0 ; i < LOOPS ; i++){
bl_led_status_set(true);
k_msleep(SLEEP_TIME_MS);
bl_led_status_set(false);
k_msleep(SLEEP_TIME_MS);
}
}

static void test_set(void) {
bl_led_init();

printf("\nTesting bl_led_set(), please check for all leds blinking\n");
Expand All @@ -234,4 +204,38 @@ void main (void) {
bl_led_set(0);
k_msleep(SLEEP_TIME_MS);
}
}

void test_main (void) {
const struct device *dev = device_get_binding(
CONFIG_UART_CONSOLE_ON_DEV_NAME);
uint32_t dtr = 0;

if (usb_enable(NULL)) {
return;
}

while (!dtr) {
uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
}

if (strlen(CONFIG_UART_CONSOLE_ON_DEV_NAME) !=
strlen("CDC_ACM_0") ||
strncmp(CONFIG_UART_CONSOLE_ON_DEV_NAME, "CDC_ACM_0",
strlen(CONFIG_UART_CONSOLE_ON_DEV_NAME))) {
printk("Error: Console device name is not USB ACM\n");

return;
}

printf("\n*****Starting LED APIs tests****\n");

ztest_test_suite(framework_tests,
ztest_unit_test(test_get_port),
ztest_unit_test(test_get_gpio),
ztest_unit_test(test_set)
);

ztest_run_test_suite(framework_tests);

};
2 changes: 1 addition & 1 deletion tests/led/prj.conf
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CONFIG_ASSERT=y
CONFIG_ZTEST=y
5 changes: 5 additions & 0 deletions tests/led/testcase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tests:
testing.ztest:
build_only: true
platform_allow: bloodlight_rev2
tags: testing