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

Native testing with FreeRTOS #144

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ lib_deps =
extends = base
platform = native@~1.2.1
test_framework = googletest
lib_extra_dirs =
test-lib
lib_archive = no
build_flags =
${base.build_flags}
-I test-include
debug_test = *

[esp32base]
extends = base
Expand Down
43 changes: 43 additions & 0 deletions src/kernel/Log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#ifdef ARDUINO
#include <ArduinoLog.h>

#define LOG_DEBUG(...) Log.verboseln(__VA_ARGS__)
#define LOG_TRACE(...) Log.traceln(__VA_ARGS__)
#define LOG_INFO(...) Log.infoln(__VA_ARGS__)
#define LOG_WARN(...) Log.warningln(__VA_ARGS__)
#define LOG_ERROR(...) Log.errorln(__VA_ARGS__)

#define LOG_IMMEDIATE(...) Serial.printf(__VA_ARGS__)
#else
#include <cstdarg>
#include <string>

typedef std::string String;

std::string __replaceAllOccurrences(std::string str, const std::string& from, const std::string& to) {
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
return str;
}

void __log(const std::string& prefix, const std::string& _fmt, ...) {
std::string fmt = prefix + __replaceAllOccurrences(_fmt, "%F", "%f") + std::string("\n");
va_list args;
va_start(args, fmt.c_str());
vfprintf(stderr, fmt.c_str(), args);
va_end(args);
}

#define LOG_DEBUG(...) __log("[VERBOSE] ", __VA_ARGS__)
#define LOG_TRACE(...) __log(" [TRACE] ", __VA_ARGS__)
#define LOG_INFO(...) __log(" [INFO] ", __VA_ARGS__)
#define LOG_WARN(...) __log("[WARNING] ", __VA_ARGS__)
#define LOG_ERROR(...) __log(" [ERROR] ", __VA_ARGS__)

#define LOG_IMMEDIATE(...) __log("", __VA_ARGS__)
#endif
21 changes: 9 additions & 12 deletions src/kernel/Task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@
#include <chrono>
#include <functional>

#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

#include <Arduino.h>

#include <ArduinoLog.h>
#include <FreeRTOS.h>
#include <task.h>

#include <kernel/Log.hpp>
#include <kernel/Time.hpp>

using namespace std::chrono;

namespace farmhub::kernel {

static const uint32_t DEFAULT_STACK_SIZE = 2048;
static const uint32_t DEFAULT_STACK_SIZE = configMINIMAL_STACK_SIZE;
static const unsigned int DEFAULT_PRIORITY = 1;

class Task;
Expand Down Expand Up @@ -79,12 +76,12 @@ class Task {
}
static TaskHandle run(const String& name, uint32_t stackSize, UBaseType_t priority, const TaskFunction runFunction) {
TaskFunction* taskFunction = new TaskFunction(runFunction);
Log.traceln("Creating task %s with priority %d and stack size %d",
LOG_TRACE("Creating task %s with priority %d and stack size %d",
name.c_str(), priority, stackSize);
TaskHandle_t handle = nullptr;
auto result = xTaskCreate(executeTask, name.c_str(), stackSize, taskFunction, priority, &handle);
if (result != pdPASS) {
Log.errorln("Failed to create task %s: %d", name.c_str(), result);
LOG_ERROR("Failed to create task %s: %d", name.c_str(), result);
delete taskFunction;
return TaskHandle();
}
Expand Down Expand Up @@ -114,7 +111,7 @@ class Task {
return true;
}
auto newWakeTime = xTaskGetTickCount();
Serial.printf("Task '%s' missed deadline by %lld ms\n",
LOG_IMMEDIATE("Task '%s' missed deadline by %lld ms\n",
pcTaskGetName(nullptr), duration_cast<milliseconds>(ticks(newWakeTime - lastWakeTime)).count());
lastWakeTime = newWakeTime;
return false;
Expand All @@ -136,7 +133,7 @@ class Task {
return time - (currentTime - ticks(lastWakeTime));
} else {
// 'currentTime' has surpassed our target time, indicating the delay has expired.
Serial.printf("Task '%s' missed deadline by %lld ms\n",
LOG_IMMEDIATE("Task '%s' missed deadline by %lld ms\n",
pcTaskGetName(nullptr), duration_cast<milliseconds>(currentTime - ticks(lastWakeTime)).count());
return ticks::zero();
}
Expand Down Expand Up @@ -165,7 +162,7 @@ class Task {

private:
~Task() {
Log.verboseln("Finished task %s\n",
LOG_IMMEDIATE("Finished task '%s'",
pcTaskGetName(nullptr));
#ifdef FARMHUB_DEBUG
String* buffer = static_cast<String*>(pvTaskGetThreadLocalStoragePointer(nullptr, CONSOLE_BUFFER_INDEX));
Expand Down
111 changes: 111 additions & 0 deletions test-include/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* FreeRTOS V202212.00
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* https://www.FreeRTOS.org
* https://github.com/FreeRTOS
*
*/
#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H

/*-----------------------------------------------------------
* Application specific definitions.
*
* These definitions should be adjusted for your particular hardware and
* application requirements.
*
* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. See
* https://www.FreeRTOS.org/a00110.html
*----------------------------------------------------------*/

#define configUSE_PREEMPTION 1
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 0
#define configUSE_16_BIT_TICKS 0
#define configTICK_RATE_HZ (1000)
#define configMINIMAL_STACK_SIZE (PTHREAD_STACK_MIN)
#define configTOTAL_HEAP_SIZE ((size_t) (16 * 1024 * 1024))
#define configMAX_TASK_NAME_LEN (32)
#define configCHECK_FOR_STACK_OVERFLOW 1
#define configIDLE_SHOULD_YIELD 1

/* The following 2 memory allocation schemes are possible for this demo:
*
* 1. Dynamic Only.
* #define configSUPPORT_STATIC_ALLOCATION 0
* #define configSUPPORT_DYNAMIC_ALLOCATION 1
*
* 2. Static and Dynamic.
* #define configSUPPORT_STATIC_ALLOCATION 1
* #define configSUPPORT_DYNAMIC_ALLOCATION 1
*
* Static only configuration is not possible for this demo as it utilizes
* dynamic allocation.
*/
#define configSUPPORT_STATIC_ALLOCATION 0
#define configSUPPORT_DYNAMIC_ALLOCATION 1

#define configRECORD_STACK_HIGH_ADDRESS 1

#define configMAX_PRIORITIES (7)

#define INCLUDE_vTaskDelete 1
#define INCLUDE_xTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_xTaskAbortDelay 1
#define INCLUDE_vTaskSuspend 1

extern void vAssertCalled(const char* const pcFileName,
unsigned long ulLine);

/* It is a good idea to define configASSERT() while developing. configASSERT()
* uses the same semantics as the standard C assert() macro. Don't define
* configASSERT() when performing code coverage tests though, as it is not
* intended to asserts() to fail, some some code is intended not to run if no
* errors are present. */
#define configASSERT(x) \
if ((x) == 0) \
vAssertCalled(__FILE__, __LINE__)

/* Prototype for the function used to print out. In this case it prints to the
* console before the network is connected then a UDP port after the network has
* connected. */
extern void vLoggingPrintf(const char* pcFormatString,
...);

/* Set to 1 to print out debug messages. If ipconfigHAS_DEBUG_PRINTF is set to
* 1 then FreeRTOS_debug_printf should be defined to the function used to print
* out the debugging messages. */
#define ipconfigHAS_DEBUG_PRINTF 1
#if (ipconfigHAS_DEBUG_PRINTF == 1)
#define FreeRTOS_debug_printf(X) vLoggingPrintf X
#endif

/* Set to 1 to print out non debugging messages, for example the output of the
* FreeRTOS_netstat() command, and ping replies. If ipconfigHAS_PRINTF is set to 1
* then FreeRTOS_printf should be set to the function used to print out the
* messages. */
#define ipconfigHAS_PRINTF 1
#if (ipconfigHAS_PRINTF == 1)
#define FreeRTOS_printf(X) vLoggingPrintf X
#endif
#endif /* FREERTOS_CONFIG_H */
1 change: 1 addition & 0 deletions test-lib/freertos/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
37 changes: 37 additions & 0 deletions test-lib/freertos/.github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Each line is a file pattern followed by one or more owners.

# These owners will be the default owners for everything in
# the repo. Unless a later match takes precedence,
# @global-owner1 and @global-owner2 will be requested for
# review when someone opens a pull request.
* @FreeRTOS/pr-bar-raiser

# Order is important; the last matching pattern takes the most
# precedence. When someone opens a pull request that only
# modifies JS files, only @js-owner and not the global
# owner(s) will be requested for a review.
# *.c FreeRTOS/pr-bar-raiser

# You can also use email addresses if you prefer. They'll be
# used to look up users just like we do for commit author
# emails.
# *.go [email protected]

# In this example, @doctocat owns any files in the build/logs
# directory at the root of the repository and any of its
# subdirectories.
# /build/logs/ @doctocat

# The `docs/*` pattern will match files like
# `docs/getting-started.md` but not further nested files like
# `docs/build-app/troubleshooting.md`.
# docs/* [email protected]

# In this example, @octocat owns any file in an apps directory
# anywhere in your repository.
# apps/ @octocat

# In this example, @doctocat owns any file in the `/docs`
# directory in the root of your repository and any of its
# subdirectories.
# /docs/ @doctocat
70 changes: 70 additions & 0 deletions test-lib/freertos/.github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Contribution guidelines

Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, code, or
documentation, we welcome our community to be involved in this project.

Please read through this document before submitting any issues or pull requests to ensure we are able to help you and all members of the community as effectively as possible.

## Code of conduct
This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct).
For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact
[email protected] with any additional questions or comments.


## Security issue notifications
If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](https://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue.


## Submitting a bugs/feature request
Have a bug to report or feature to request? Follow these steps:
1. Search on the [FreeRTOS Community Support Forums](https://forums.freertos.org/) and [GitHub issue tracker](https://github.com/FreeRTOS/FreeRTOS/issues?utf8=%E2%9C%93&q=is%3Aissue) to be sure this hasn't been already reported or discussed.
2. If your search turns up empty, create a new topic in the [forums](https://forums.freertos.org/) and work with the community to help clarify issues or refine the idea. Include as many of the details listed below.
3. Once the community has had time to discuss and digest, we welcome you to create an [issue](https://github.com/FreeRTOS/FreeRTOS/issues) to report bugs or suggest features.

When creating a new topic on the forums or filing an issue, please include as many relevant details as possible. Examples include:

* A clear description of the situation - what you observe, what you expect, and your view on how the two differ.
* A reproducible test case or sequence of steps.
* The version of our code being used.
* Any modifications you've made relevant to the bug.
* Details of your environment or deployment. Highlight anything unusual.


## Contributing via pull request
Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that:

1. You are working against the latest source on the *main* branch.
2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already.
3. You open an issue to discuss any significant work - we would hate for your time to be wasted.

To send us a pull request, please:

1. Fork the repository.
2. Modify the source; focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change.
3. Follow the [coding style guide](https://www.FreeRTOS.org/FreeRTOS-Coding-Standard-and-Style-Guide.html).
4. Commit to your fork using clear commit messages.
5. Send us a pull request, answering any default questions in the pull request interface.
NOTE: Please make sure the default option (Allow edits from maintainers) is left checked.
6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation.

GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and
[creating a pull request](https://help.github.com/articles/creating-a-pull-request/).

## Coding style
* Please ensure that your code complies to the [FreeRTOS coding style guidelines](https://www.FreeRTOS.org/FreeRTOS-Coding-Standard-and-Style-Guide.html).


## Getting your pull request merged
All pull requests must be approved by our review team before it can be merged in. We appreciate your patience while pull requests are reviewed. The time it takes to review will depend on complexity and consideration of wider implications.


## Finding contributions to work on
Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), tackling open 'help wanted' issues is a great place to start.


## Licensing
The FreeRTOS kernel is released under the MIT open source license, the text of which can be found [here](https://github.com/FreeRTOS/FreeRTOS/blob/main/FreeRTOS/License/license.txt)

Additional license files can be found in the folders containing any supplementary libraries licensed by their respective copyright owners where applicable.

We may ask you to sign a [Contributor License Agreement (CLA)](https://en.wikipedia.org/wiki/Contributor_License_Agreement) for larger changes.
40 changes: 40 additions & 0 deletions test-lib/freertos/.github/ISSUE_TEMPLATE/bug-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
name: Bug report
about: Create a report to help us improve FreeRTOS. This should only be used for confirmed
bugs. If you suspect something it is best to first discuss it on the FreeRTOS community
support forums linked below.
title: "[BUG]"
labels: bug
assignees: ''

---

**Describe the bug**
A concise description of what the bug is.

**Target**
- Development board: [e.g. HiFive11 RevB]
- Instruction Set Architecture: [e.g. RV32IMAC]
- IDE and version: [e.g. Freedom Studio 4.12.0.2019-08-2]
- Toolchain and version: [e.g. riscv64-unknown-elf-gcc-8.3.0-2019.08.0]

**Host**
- Host OS: [e.g. MacOS]
- Version: [e.g. Mojave 10.14.6]

**To Reproduce**
- Use project ... and configure with ...
- Run on ... and could observe ...

**Expected behavior**
A concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Additional context**
Add any other context about the problem here.
e.g. code snippet to reproduce the issue.
e.g. stack trace, memory dump, debugger log, and many etc.

<!-- For general inquiries, please post in [FreeRTOS forum](https://forums.FreeRTOS.org) for community support. -->
5 changes: 5 additions & 0 deletions test-lib/freertos/.github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
blank_issues_enabled: false
contact_links:
- name: FreeRTOS Community Support Forum
url: https://forums.freertos.org/
about: Please ask and answer questions about FreeRTOS here.
Loading
Loading