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

Shivam: Timers Wrapper Code Review #49

Closed
wants to merge 84 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
4a3299b
Initial Implementation of Core Objects
cjchanx Jul 30, 2022
b81b769
Pull README.md changes from Chris/AndromedaC++
cjchanx Jul 30, 2022
e6a86c6
Attempt to Implement FlightTask
cjchanx Aug 6, 2022
5859a4a
Add VS2022 .gitignore
cjchanx Aug 6, 2022
f8e6031
Update Project Compilation and Refactoring
cjchanx Aug 9, 2022
8ebf752
Implement wrapper for RTOS Mutex
cjchanx Aug 13, 2022
67f7b7f
Implement wrapper for RTOS Mutex (#46)
cjchanx Aug 13, 2022
f8c99cd
Merge branch 'StudentOrganisationForAerospaceResearch:Chris/Ouroboros…
cjchanx Aug 13, 2022
a86f9b7
Start UARTTask
cjchanx Aug 15, 2022
d4fffcd
Merge branch 'Chris/OuroborosC++' of https://github.com/StudentOrgani…
cjchanx Aug 15, 2022
6f06ea6
Add clarification on _IDE README.md
cjchanx Aug 15, 2022
fd89a05
Begin Implementing RTOS Timer Wrapper
cjchanx Aug 18, 2022
cfe977b
Add UARTTask to VS2022
cjchanx Aug 18, 2022
f45eb7b
Start File for UARTTask
cjchanx Aug 18, 2022
13e820d
Add Notes
cjchanx Aug 18, 2022
5e851fd
Update README.md
cjchanx Aug 18, 2022
6ae14a0
Update README.md
cjchanx Aug 18, 2022
b983ffa
Begin implementation of UARTTask::Init
cjchanx Aug 20, 2022
08b1593
Merge branch 'Chris/OuroborosC++' of https://github.com/cjchanx/Avion…
cjchanx Aug 20, 2022
9f29238
Implement basic single-task activity on FlightBoard
cjchanx Sep 3, 2022
8846a44
Merge with remote
cjchanx Sep 3, 2022
d7f69da
Implement VA List mutex for SOAR_ASSERT
cjchanx Sep 10, 2022
efa70a5
Add process for changing .ioc file
cjchanx Sep 10, 2022
739417f
Initial debug and soar assert implementation
cjchanx Sep 10, 2022
8a9b687
Try to add NVIC_SystemReset() to the HF Handler
cjchanx Sep 10, 2022
44ffa32
Update README.md
cjchanx Sep 10, 2022
b4061d3
Update README.md
cjchanx Sep 10, 2022
c365c39
Create README.md
cjchanx Sep 10, 2022
0fa797a
Merge branch 'Chris/OuroborosC++' of https://github.com/cjchanx/Avion…
cjchanx Sep 11, 2022
782de39
Initial Implementation of SOAR_PRINT, Redesign Task Inst Dependency
cjchanx Sep 13, 2022
86d18c1
Cleanup UARTTask Object Routing, Use SOAR_PRINT
cjchanx Sep 16, 2022
84df2dd
Remove unused mutex
cjchanx Sep 16, 2022
6fa8678
Add reset of packet after Handling command
cjchanx Sep 16, 2022
d343b71
Code Review Fixes
cjchanx Sep 16, 2022
5740f3c
Get SOAR_PRINT working
cjchanx Sep 17, 2022
c56ea4d
Documentation and Debug Print Changes
cjchanx Sep 18, 2022
62656cd
Add condition to SOAR_ASSERT so we don't need to wait if the assert h…
cjchanx Sep 18, 2022
d26c170
Format Improvements and Note
cjchanx Sep 18, 2022
be21745
Revert: Add condition to SOAR_ASSERT
cjchanx Sep 18, 2022
fe872ee
Add extra endline
cjchanx Sep 18, 2022
1ec1f96
Add comments for clarification
cjchanx Sep 18, 2022
2d3ea29
Add double instantiation guard to FlightTask
cjchanx Sep 18, 2022
0421815
Add IDE Instructions
cjchanx Sep 21, 2022
4ba5eea
Add new Constructor to Command
cjchanx Sep 21, 2022
f632126
Command:: Object Improvements
cjchanx Sep 22, 2022
57267b7
Change FlightTask:: to Instance Run Style
cjchanx Sep 22, 2022
226dc10
Add defines for ASSERT constants
cjchanx Sep 23, 2022
e8fd4cf
Codegen for UART 5 DMA
cjchanx Sep 23, 2022
ad53020
Fix DMA Handle Ref
cjchanx Sep 23, 2022
0d1474d
Initial Definition for DebugTask::
cjchanx Sep 23, 2022
c7926f8
Init DebugTask
cjchanx Sep 23, 2022
4dc9ac0
Rename Malloc and Free to soar_malloc and soar_free for consistency
cjchanx Sep 23, 2022
342433b
Refactor to nullptr
cjchanx Sep 23, 2022
b13c98c
Update .ioc to Andromeda V3.3
cjchanx Sep 24, 2022
31f6cfa
Remove unused CMake files
cjchanx Sep 24, 2022
62e4b36
Code review fixes for SystemDefines.hpp
cjchanx Sep 24, 2022
95b702f
Code review fixes for Mutex.hpp
cjchanx Sep 24, 2022
701913d
Specify CRC Comments
cjchanx Oct 6, 2022
2169f30
Merge branch 'Chris/OuroborosC++' of https://github.com/StudentOrgani…
cjchanx Oct 6, 2022
c975b74
Refactoring, Utils, Conversions
cjchanx Oct 8, 2022
16ec0ca
Change statAllocationCounter to std::atomic (TODO: TEST THIS)
cjchanx Oct 8, 2022
85a9e3e
Refactoring, DMA Stub
cjchanx Oct 8, 2022
38fa2cb
Move Utils to root
cjchanx Oct 8, 2022
19e07c1
Project Linker, Release, Settings Improvements
cjchanx Oct 8, 2022
2a3d930
Update README.md
cjchanx Oct 8, 2022
739230a
Increase FreeRTOS Heap size to 24KiB
cjchanx Oct 8, 2022
ff1abe3
added timer states and functions to get state
shivamdesai04 Oct 14, 2022
3bbae48
completed funstion removed and other functions have been updated
shivamdesai04 Oct 14, 2022
c0ef82f
added ChangePeriodAndStart function
shivamdesai04 Oct 15, 2022
c5f025f
added new functions and implemented changes stated in code review
shivamdesai04 Oct 15, 2022
04fae0d
added print statement to deconstructor
shivamdesai04 Oct 15, 2022
080bceb
Enabled software timers in ioc file
shivamdesai04 Oct 22, 2022
6c170c9
GetState test code
shivamdesai04 Oct 22, 2022
7a2032d
getstate and destructor testing functions added
shivamdesai04 Oct 22, 2022
acd98d5
added timer test functions
shivamdesai04 Oct 22, 2022
ff2e80c
finished testing functions
shivamdesai04 Oct 22, 2022
7ac5697
Timer test moved to filghttask.cpp
shivamdesai04 Oct 22, 2022
5ba92b6
Test code partly corrected
shivamdesai04 Oct 27, 2022
dd36e61
Merge branch 'master' of https://github.com/StudentOrganisationForAer…
cjchanx Oct 29, 2022
c8145bb
implemeted completed state
shivamdesai04 Nov 10, 2022
9916469
Code Dubugged and Comments added
shivamdesai04 Jan 7, 2023
b0e5e6d
Documentation added
shivamdesai04 Jan 7, 2023
d726bb6
TEST CODE
shivamdesai04 Jan 7, 2023
2498acb
merge conflict resolved
shivamdesai04 Jan 7, 2023
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
6 changes: 5 additions & 1 deletion .cproject
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,14 @@
<listOptionValue builtIn="false" value="USE_HAL_DRIVER"/>
<listOptionValue builtIn="false" value="STM32F405xx"/>
</option>
<<<<<<< HEAD
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.option.languagestandard.53194756" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.option.languagestandard" useByScannerDiscovery="true" value="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.option.languagestandard.value.gnupp20" valueType="enumerated"/>
=======
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.option.languagestandard.53194756" name="Language standard" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.option.languagestandard" useByScannerDiscovery="true" value="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.option.languagestandard.value.gnupp20" valueType="enumerated"/>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.option.otherflags.1098094453" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.option.otherflags" useByScannerDiscovery="true" valueType="stringList">
<listOptionValue builtIn="false" value="-Wno-volatile"/>
</option>
>>>>>>> ef0277edecfdc6058bab831e34734f3d064ecbe3
<inputType id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.input.cpp.1401909830" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.input.cpp"/>
</tool>
<tool id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.1770583617" name="MCU GCC Linker" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker">
Expand Down Expand Up @@ -299,4 +303,4 @@
<autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
</storageModule>
</cproject>
</cproject>
3 changes: 2 additions & 1 deletion AvionicsSoftware.ioc
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ Dma.UART5_TX.2.Priority=DMA_PRIORITY_LOW
Dma.UART5_TX.2.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode
FREERTOS.FootprintOK=true
FREERTOS.INCLUDE_vTaskDelayUntil=1
FREERTOS.IPParameters=Tasks01,INCLUDE_vTaskDelayUntil,FootprintOK,configMAX_TASK_NAME_LEN,configUSE_NEWLIB_REENTRANT,configTOTAL_HEAP_SIZE
FREERTOS.IPParameters=Tasks01,INCLUDE_vTaskDelayUntil,FootprintOK,configMAX_TASK_NAME_LEN,configUSE_NEWLIB_REENTRANT,configTOTAL_HEAP_SIZE,configUSE_TIMERS
FREERTOS.Tasks01=defaultTask,0,128,StartDefaultTask,Default,NULL
FREERTOS.configMAX_TASK_NAME_LEN=64
FREERTOS.configTOTAL_HEAP_SIZE=24576
FREERTOS.configUSE_NEWLIB_REENTRANT=1
FREERTOS.configUSE_TIMERS=1
File.Version=6
GPIO.groupedBy=Group By Peripherals
KeepUserPlacement=false
Expand Down
2 changes: 1 addition & 1 deletion Components/Core/Inc/DMAController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ class DMAController

};

#endif /* AVIONICS_INCLUDE_SOAR_CORE_DMA_CONTROLLER_BASE_H */
#endif /* AVIONICS_INCLUDE_SOAR_CORE_DMA_CONTROLLER_BASE_H */
2 changes: 1 addition & 1 deletion Components/Core/Inc/Queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ class Queue{
uint16_t queueDepth; // Max queue depth
};

#endif /* AVIONICS_INCLUDE_SOAR_CORE_QUEUE_H */
#endif /* AVIONICS_INCLUDE_SOAR_CORE_QUEUE_H */
43 changes: 32 additions & 11 deletions Components/Core/Inc/Timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@
/* Includes ------------------------------------------------------------------*/
#include "cmsis_os.h"
#include "Utils.hpp"
#include "FreeRTOS.h"

/* Macros --------------------------------------------------------------------*/
constexpr uint32_t DEFAULT_TIMER_COMMAND_WAIT_PERIOD = MS_TO_TICKS(15); // Default time to block a task if a command cannot be issued to the timer

#define DEFAULT_TIMER_PERIOD (MS_TO_TICKS(1000)) // 1s

// Enumeration representing the 4 timer states
enum TimerState {
UNINITIALIZED=0,
COUNTING,
PAUSED,
COMPLETE
};


/* Class -----------------------------------------------------------------*/

/**
Expand All @@ -25,21 +35,32 @@ constexpr uint32_t DEFAULT_TIMER_COMMAND_WAIT_PERIOD = MS_TO_TICKS(15); // Defau
class Timer
{
public:
Timer();

bool ChangePeriod(const uint32_t period);

// WORK-IN-PROGRESS
// NOTES:
// - I can think of several timer types
// 1) Default Ctor Timer (1 second polling timer that requires polling to acquire state with no callback)
// 2) Callback Enabled Timer (user-provided callback)
Timer(); // Default Constructor (Polling Timer)
Timer(void (*TimerCallbackFunction_t)( TimerHandle_t xTimer )); // Constructor for Callback Enabled Timer
~Timer();
bool ChangePeriod(const uint32_t period_ms);
bool ChangePeriodAndStart(const uint32_t period_ms);
bool Start();
bool Stop();
bool ResetTimer();
bool ResetTimerAndStart();
void SetAutoReload(bool setReloadOn);

bool GetAutoReload();
TimerState GetState();
const uint32_t GetPeriod();
const uint32_t GetRemainingTime();

static void CallbackFunction( TimerHandle_t xTimer );

protected:
TimerHandle_t rtTimerHandle;

TimerState timerState;
uint32_t remainingTime;
uint32_t remainingTimeBetweenPauses;
uint32_t rtosTimeRemaning();
};

#endif /* AVIONICS_INCLUDE_SOAR_CORE_COMMAND_H */


#endif /* AVIONICS_INCLUDE_SOAR_CORE_TIMER_H*/
214 changes: 203 additions & 11 deletions Components/Core/Timer.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,226 @@
#include "SystemDefines.hpp"
#include "Timer.hpp"

/**
******************************************************************************
* File Name : Timer.cpp
* Description : FreeRTOS Timer Wrapper
******************************************************************************
*/
#include "SystemDefines.hpp"
#include "Timer.hpp"

/**
* @brief Empty callback function, used internally for default polling timers
*/
void empty_callback(TimerHandle_t rtTimerHandle) {};

/**
* @brief Default constructor makes a timer that can only be polled for state
* Default constructor makes a timer that can only be polled for state
* Default behaviour : ->Autoreload is set to false (One shot Timer)
* ->Timer Period is 1000ms
* ->The Callback function simply changes state to COMPLETE and has no other functionality
*/
Timer::Timer()
{
// We make a timer named "Timer" with a callback function that does nothing, Autoreload false, and the default period of 1s.
// The timer ID is specified as (void *)this to provide a unique ID for each timer object - however this is not necessary for polling timers.
// The timer is created in the dormant state.
rtTimerHandle = xTimerCreate("Timer", DEFAULT_TIMER_PERIOD, pdFALSE, (void *)this, empty_callback);
rtTimerHandle = xTimerCreate("Timer", DEFAULT_TIMER_PERIOD, pdFALSE, (void *)this, CallbackFunction);
SOAR_ASSERT(rtTimerHandle, "Error Occurred, Timer not created");
timerState = UNINITIALIZED;
}

/**
* Constructor for callback enabled timer
* ! User has to add "Timer::CallbackFunction(rtTimerHandle);" in the callback function for accurate functioning of Timer States
* Default behaviour : ->Autoreload is set to false (One shot Timer)
* ->Timer Period is 1000ms
* ->The Callback function will be provided by the user while making sure to follow instruction above
*/
Timer::Timer(void (*TimerCallbackFunction_t)( TimerHandle_t xTimer ))
{
rtTimerHandle = xTimerCreate("Timer", DEFAULT_TIMER_PERIOD, pdFALSE, (void *)this, TimerCallbackFunction_t);
SOAR_ASSERT(rtTimerHandle, "Error Occurred, Timer not created");
timerState = UNINITIALIZED;
}

/**
* @brief Default de-constructor makes a timer that can only be polled for state
*/
Timer::~Timer()
{
if (xTimerDelete(rtTimerHandle, DEFAULT_TIMER_COMMAND_WAIT_PERIOD*2) == pdPASS) {
SOAR_PRINT("Timer has been deleted \n\n");
}
}

/**
* @brief Callback function for polling timers
*/
void Timer::CallbackFunction(TimerHandle_t xTimer){
Timer* ptrTimer = (Timer*)pvTimerGetTimerID(xTimer);
ptrTimer->timerState = COMPLETE;
}

/**
* @brief Changes this timer object's RTOS timer period, returns true on success, returns false on failure (timer command queue full)
* @brief Changes timer period, Sets timer state back to uninitialized and stops timer
*/
bool Timer::ChangePeriod(const uint32_t period_ms)
{
if (xTimerChangePeriod(rtTimerHandle, MS_TO_TICKS(period_ms), DEFAULT_TIMER_COMMAND_WAIT_PERIOD) == pdTRUE)
if (xTimerChangePeriod(rtTimerHandle, MS_TO_TICKS(period_ms), DEFAULT_TIMER_COMMAND_WAIT_PERIOD) == pdTRUE) {
if (xTimerStop(rtTimerHandle, DEFAULT_TIMER_COMMAND_WAIT_PERIOD) == pdPASS) {
timerState = UNINITIALIZED;
return true;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an else here to account for if we failed xTimerStop (for whatever reason)

}
return false;
}

/**
* @brief Changes timer period, Sets timer state back to counting and starts timer
*/
bool Timer::ChangePeriodAndStart(const uint32_t period_ms)
{
if (xTimerChangePeriod(rtTimerHandle, MS_TO_TICKS(period_ms), DEFAULT_TIMER_COMMAND_WAIT_PERIOD) == pdTRUE) {
timerState = COUNTING;
return true;
}
return false;
}

/**
* @brief Starts the timer
*/
bool Timer::Start()
{
if ((timerState == COMPLETE) || (timerState == COUNTING)) {
cjchanx marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
else if (timerState == PAUSED) {
ChangePeriod(remainingTimeBetweenPauses);
}
if (xTimerStart(rtTimerHandle, DEFAULT_TIMER_COMMAND_WAIT_PERIOD) == pdPASS) {
timerState = COUNTING;
return true;
}
return false;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs

/**
* @brief Stops the timer
*/
bool Timer::Stop()
{
// Checks if timer is in counting state because it cannot be stopped in any other state
if (timerState != COUNTING) {
return false;
}
// Calculates the time left on the timer before it is paused
remainingTimeBetweenPauses = rtosTimeRemaning();
if (xTimerStop(rtTimerHandle, DEFAULT_TIMER_COMMAND_WAIT_PERIOD) == pdPASS) {
timerState = PAUSED;
return true;
}
return false;
}

/**
* @brief Restarts timer without starting to count
*/
bool Timer::ResetTimer()
{
if (timerState == UNINITIALIZED) {
return false;
}
if (xTimerReset(rtTimerHandle, DEFAULT_TIMER_COMMAND_WAIT_PERIOD) == pdPASS) {
if (xTimerStop(rtTimerHandle, DEFAULT_TIMER_COMMAND_WAIT_PERIOD) == pdPASS) {
timerState = UNINITIALIZED;
return true;
}
}
return false;
}

/**
* @brief Restarts Timer and starts counting
*/
bool Timer::ResetTimerAndStart()
{
if (timerState == UNINITIALIZED) {
return false;
}
if (xTimerReset(rtTimerHandle, DEFAULT_TIMER_COMMAND_WAIT_PERIOD) == pdPASS) {
timerState = COUNTING;
return true;
}
return false;
}

/**
* @brief Sets timer to auto-reload if parameter is set to true, Sets timer to one shot if parameter is set to false
*/
void Timer::SetAutoReload(bool setReloadOn)
{
if (setReloadOn == true){
vTimerSetReloadMode(rtTimerHandle, pdTRUE);
//Testing purposes
SOAR_PRINT("Set to Auto Reload\n\n");
}
if (setReloadOn == false){
vTimerSetReloadMode(rtTimerHandle, pdFALSE);
//Testing purposes
SOAR_PRINT("Set to One Shot\n\n");
}
}

/**
* @brief Returns true if the timer is set to autoreload and false if it is set to one-shot
*/
bool Timer::GetAutoReload()
{
if ((uxTimerGetReloadMode(rtTimerHandle)) == (( UBaseType_t ) pdTRUE)) {
return true;
}
if ((uxTimerGetReloadMode(rtTimerHandle)) == (( UBaseType_t ) pdFALSE)) {
return false;
}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs

/**
* @brief Returns timer state enum
*/
TimerState Timer::GetState()
{
return timerState;
}

/**
* @brief Returns the timers' period
*/
const uint32_t Timer::GetPeriod()
{
return (TICKS_TO_MS(xTimerGetPeriod(rtTimerHandle)));
}

/**
* @brief Returns remaining time on timer based on current state
*/
const uint32_t Timer::GetRemainingTime()
{
if (timerState == UNINITIALIZED){
return (GetPeriod());
}
else if (timerState == COUNTING){
return rtosTimeRemaning();
}
else if (timerState == PAUSED){
return remainingTimeBetweenPauses;
}
else {
return 0;
}
}

cjchanx marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief Calculates remaining time on timer in counting state
*/
uint32_t Timer::rtosTimeRemaning()
{
remainingTime = (TICKS_TO_MS(xTimerGetExpiryTime(rtTimerHandle) - xTaskGetTickCount()));
return remainingTime;
}
Loading