diff --git a/tests/freertos/Makefile b/tests/freertos/Makefile new file mode 100644 index 0000000000..4f69e7f7af --- /dev/null +++ b/tests/freertos/Makefile @@ -0,0 +1,8 @@ +include ../Makefile.tests_common + +USEMODULE += riot_freertos +USEMODULE += shell +BOARD_WHITELIST := esp32-wroom-32 + + +include $(RIOTBASE)/Makefile.include diff --git a/tests/freertos/main.c b/tests/freertos/main.c new file mode 100644 index 0000000000..67acb3e61a --- /dev/null +++ b/tests/freertos/main.c @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2019 Freie Universitaet Berlin, + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief freertos testing tool + * + * More detailed information about the file and the functionality implemented. + * + * @author Julian Holzwarth + * + */ + +#include +#include + +#include "shell.h" + +#include "freertos/FreeRTOS.h" +#include "semaphore_test.h" + +/** + * Foward declarations + */ +static int cmd_test_mutex(int argc, char **argv); +static int cmd_test_recursive_mutex(int argc, char **argv); + + +/** + * @brief List of command for this application. + */ +static const shell_command_t shell_commands[] = { + { "mutex_semaphore", "tests freertos mutex semaphor", cmd_test_mutex, }, + { "recursive_mutex_semaphore", "tests freertos recursive mutex semaphor", cmd_test_recursive_mutex }, + { NULL, NULL, NULL } +}; + +/** + * @brief shell command to test freertos mutex semaphore + * + * @param[in] argc Number of arguments + * @param[in] argv Array of arguments + * + * @return 0 on success + */ +static int cmd_test_mutex(int argc, char **argv) +{ + (void)argc; + (void)argv; + puts("starting test: mutex semaphore"); + if (semaphore_test_mutex_take() == pdPASS) { + puts("OK"); + } + else { + puts("mutex semaphore test failed"); + } + + return 0; +} + +/** + * @brief shell command to test freertos recursive mutex semaphore + * + * @param[in] argc Number of arguments + * @param[in] argv Array of arguments + * + * @return 0 on success + */ +static int cmd_test_recursive_mutex(int argc, char **argv) +{ + (void)argc; + (void)argv; + puts("starting test: recursive mutex semaphore"); + if (semaphore_test_recursive_mutex_take() == pdPASS) { + puts("OK"); + } + else { + puts("recursive mutex semaphore test failed"); + } + return 0; +} + +/** + * @brief main function starting shell + * + * @return 0 on success + */ +int main(void) +{ + puts("Starting shell..."); + char line_buf[SHELL_DEFAULT_BUFSIZE]; + shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); + + return 0; +} diff --git a/tests/freertos/semaphore_test.c b/tests/freertos/semaphore_test.c new file mode 100644 index 0000000000..e9a1a017fa --- /dev/null +++ b/tests/freertos/semaphore_test.c @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2019 Freie Universitaet Berlin, + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +#include +#include + +#include "freertos/semphr.h" +#include "freertos/queue.h" +#include "freertos/FreeRTOS.h" + +#include "semaphore_test.h" + +/** + * @brief tests the take function for a freertos mutex semaphore + * + * @return pdPASS when the test is passed, pdFail otherwise + */ +int semaphore_test_mutex_take(void) +{ + SemaphoreHandle_t testing_semaphore = xSemaphoreCreateMutex(); + + if (testing_semaphore == NULL) { + puts("test failed: mutex semaphore not created"); + return pdFAIL; + } + /* + * Freertos Documentation: + * "pdPASS: Returned only if the call to xSemaphoreTake() + * was successful in obtaining the semaphore." + */ + /* first call should be successful */ + if (xSemaphoreTake(testing_semaphore, 0) == pdFAIL) { + puts("error in Take"); + vSemaphoreDelete(testing_semaphore); + return pdFAIL; + } + /* after the fist call every call should fail */ + if (xSemaphoreTake(testing_semaphore, 0) == pdPASS) { + puts("error in Take"); + vSemaphoreDelete(testing_semaphore); + return pdFAIL; + } + vSemaphoreDelete(testing_semaphore); + return pdPASS; +} + +/** + * @brief tests the take function for a freertos recursive mutex semaphore + * + * @return pdPASS when the test is passed, pdFAIL otherwise + */ +int semaphore_test_recursive_mutex_take(void) +{ + SemaphoreHandle_t testing_semaphore = xSemaphoreCreateRecursiveMutex(); + + if (testing_semaphore == NULL) { + puts("test failed: recursive mutex semaphore not created"); + return pdFAIL; + } + /* + * Freertos Documentation: + * "pdPASS: Returned only if the call to xSemaphoreTakeRecursive() + * was successful in obtaining the semaphore.", + * "once a recursive mutex has been successfully ‘taken’ by a task, + * further calls to xSemaphoreTakeRecursive() made by the + * same task will also be successful." + */ + for (size_t i = 0; i < 3; i++) { + if (xSemaphoreTakeRecursive(testing_semaphore, 0) == pdFAIL) { + puts("error in Take"); + vSemaphoreDelete(testing_semaphore); + return pdFAIL; + } + } + vSemaphoreDelete(testing_semaphore); + return pdPASS; +} diff --git a/tests/freertos/semaphore_test.h b/tests/freertos/semaphore_test.h new file mode 100644 index 0000000000..ff28059837 --- /dev/null +++ b/tests/freertos/semaphore_test.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2019 Freie Universitaet Berlin, + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ +#ifndef SEMAPHORE_TEST_H +#define SEMAPHORE_TEST_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @ingroup tests + * @{ + * + * @file + * @brief freertos testing tool + * + * More detailed information about the file and the functionality implemented. + * + * @author Julian Holzwarth + * + */ + + +/** + * @brief tests the take function for a freertos mutex semaphore + * + * @return pdPASS when the test is passed, pdFail otherwise + */ +int semaphore_test_mutex_take(void); + +/** + * @brief tests the take function for a freertos recursive mutex semaphore + * + * @return pdPASS when the test is passed, pdFAIL otherwise + */ +int semaphore_test_recursive_mutex_take(void); + +#ifdef __cplusplus +} +#endif + +#endif /* SEMAPHORE_TEST_H */ diff --git a/tests/freertos/tests/01-run.py b/tests/freertos/tests/01-run.py new file mode 100755 index 0000000000..d7a1bef28f --- /dev/null +++ b/tests/freertos/tests/01-run.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2019 Freie Universitaet Berlin, +# +# This file is subject to the terms and conditions of the GNU Lesser +# General Public License v2.1. See the file LICENSE in the top level +# directory for more details. + +# @author Julian Holzwarth + +import sys +import pexpect +from testrunner import run + + +def testfunc(child): + # Try to wait for the shell + for _ in range(0, 10): + child.sendline("help") + if child.expect_exact(["> ", pexpect.TIMEOUT], timeout=1) == 0: + break + child.sendline("mutex_semaphore") + child.expect("starting test: mutex semaphore") + child.expect("OK") + child.expect_exact("> ") + + child.sendline("recursive_mutex_semaphore") + child.expect_exact("starting test: recursive mutex semaphore") + child.expect("OK") + child.expect_exact("> ") + + +if __name__ == "__main__": + sys.exit(run(testfunc))