ADD test for multi-tasking
This commit is contained in:
@ -27,12 +27,12 @@
|
||||
#include "bike_system.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
|
||||
#include "Callback.h"
|
||||
#include "cmsis_os2.h"
|
||||
#include "constants.hpp"
|
||||
#include "cmsis_os.h"
|
||||
#include "common/constants.hpp"
|
||||
#include "mbed_trace.h"
|
||||
#include "pedal_device.hpp"
|
||||
|
||||
#if MBED_CONF_MBED_TRACE_ENABLE
|
||||
#define TRACE_GROUP "BikeSystem"
|
||||
#endif // MBED_CONF_MBED_TRACE_ENABLE
|
||||
@ -75,7 +75,7 @@ BikeSystem::BikeSystem()
|
||||
_isrEventThread(osPriorityAboveNormal, OS_STACK_SIZE, nullptr, "ISR_Event"),
|
||||
_speedDistanceThread(
|
||||
osPriorityNormal, OS_STACK_SIZE, nullptr, "Speed_distance_Task"),
|
||||
_gearTaskThread(osPriorityNormal, OS_STACK_SIZE, nullptr, "Gear_Task"),
|
||||
_gearTaskThread(osPriorityAboveNormal, OS_STACK_SIZE, nullptr, "Gear_Task"),
|
||||
_gearDevice(&_mailGearDevice, _timer),
|
||||
_pedalDevice(&_mailPedalDevice, _timer),
|
||||
_resetDevice(callback(this, &BikeSystem::onReset)),
|
||||
@ -88,13 +88,22 @@ BikeSystem::BikeSystem()
|
||||
#if defined(MBED_TEST_MODE)
|
||||
const advembsof::TaskLogger& BikeSystem::getTaskLogger() { return _taskLogger; }
|
||||
|
||||
bike_computer::Speedometer& Bike_system::getSpeedometer() {
|
||||
bike_computer::Speedometer& BikeSystem::getSpeedometer() {
|
||||
// ENTER CRITICAL SECTION
|
||||
_mutexSpeedometer.lock();
|
||||
bike_computer::Speedometer& speedometer = _speedometer;
|
||||
_mutexSpeedometer.unlock();
|
||||
// END CRITICAL SECTION
|
||||
|
||||
return speedometer;
|
||||
}
|
||||
|
||||
GearDevice& BikeSystem::getGearDevice() { return _gearDevice; }
|
||||
|
||||
void BikeSystem::setCallbackGearChage(Callback<void()> cbGearChange) {
|
||||
_cbGearChange = cbGearChange;
|
||||
}
|
||||
|
||||
#endif // defined(MBED_TEST_MODE)
|
||||
|
||||
void BikeSystem::init() {
|
||||
@ -104,7 +113,7 @@ void BikeSystem::init() {
|
||||
// initialize the lcd display
|
||||
disco::ReturnCode rc = _displayDevice.init();
|
||||
if (rc != disco::ReturnCode::Ok) {
|
||||
tr_error("Ffalseailed to initialized the lcd display: %ld", static_cast<int>(rc));
|
||||
tr_error("Ffalseailed to initialized the lcd display: %d", static_cast<int>(rc));
|
||||
}
|
||||
|
||||
// initialize the sensor device
|
||||
@ -114,7 +123,13 @@ void BikeSystem::init() {
|
||||
}
|
||||
|
||||
// enable/disable task logging
|
||||
_taskLogger.enable(false);
|
||||
bool runTaskLogger = false;
|
||||
|
||||
#if defined(MBED_TEST_MODE)
|
||||
runTaskLogger = true;
|
||||
#endif
|
||||
|
||||
_taskLogger.enable(runTaskLogger);
|
||||
}
|
||||
|
||||
void BikeSystem::start() {
|
||||
@ -134,18 +149,24 @@ void BikeSystem::start() {
|
||||
osStatus status =
|
||||
_isrEventThread.start(callback(this, &BikeSystem::dispatch_isr_events));
|
||||
if (status != osOK) {
|
||||
tr_error("Thread %s started with status %d", _isrEventThread.get_name(), status);
|
||||
tr_error("Thread %s started with status %ld",
|
||||
_isrEventThread.get_name(),
|
||||
static_cast<int32_t>(status));
|
||||
}
|
||||
|
||||
status =
|
||||
_speedDistanceThread.start(callback(this, &BikeSystem::loop_speed_distance_task));
|
||||
if (status != osOK) {
|
||||
tr_error("Thread %s started with status %d", _isrEventThread.get_name(), status);
|
||||
tr_error("Thread %s started with status %ld",
|
||||
_isrEventThread.get_name(),
|
||||
static_cast<int32_t>(status));
|
||||
}
|
||||
|
||||
status = _gearTaskThread.start(callback(this, &BikeSystem::loop_gear_task));
|
||||
if (status != osOK) {
|
||||
tr_error("Thread %s started with status %d", _gearTaskThread.get_name(), status);
|
||||
tr_error("Thread %s started with status %ld",
|
||||
_gearTaskThread.get_name(),
|
||||
static_cast<int32_t>(status));
|
||||
}
|
||||
|
||||
#if !defined(MBED_TEST_MODE)
|
||||
@ -159,6 +180,16 @@ void BikeSystem::start() {
|
||||
dispatch_events();
|
||||
}
|
||||
|
||||
void BikeSystem::stop() {
|
||||
osStatus status = _isrEventThread.terminate();
|
||||
status += _speedDistanceThread.terminate();
|
||||
status += _gearTaskThread.terminate();
|
||||
if (status != 0) {
|
||||
tr_error("Stop thread error");
|
||||
}
|
||||
tr_info("Bike system has stopped !");
|
||||
}
|
||||
|
||||
/* Callback from isr */
|
||||
|
||||
void BikeSystem::onReset() {
|
||||
@ -170,20 +201,19 @@ void BikeSystem::onReset() {
|
||||
// ISR thread functions
|
||||
|
||||
void BikeSystem::resetTask() {
|
||||
#ifndef(MBED_TEST_MODE)
|
||||
#if !defined(MBED_TEST_MODE)
|
||||
auto taskStartTime = _timer.elapsed_time();
|
||||
|
||||
std::chrono::microseconds responseTime = _timer.elapsed_time() - _resetTime;
|
||||
tr_info("Reset task: response time is %" PRIu64 " usecs", responseTime.count());
|
||||
#endif
|
||||
|
||||
// ENTER CRITICAL SECTION
|
||||
_mutexSpeedometer.lock();
|
||||
_speedometer.reset();
|
||||
_mutexSpeedometer.unlock();
|
||||
// END CRITICAL SECTION
|
||||
|
||||
#ifndef(MBED_TEST_MODE)
|
||||
#if !defined(MBED_TEST_MODE)
|
||||
_taskLogger.logPeriodAndExecutionTime(
|
||||
_timer, advembsof::TaskLogger::kResetTaskIndex, taskStartTime);
|
||||
#endif
|
||||
@ -208,7 +238,8 @@ void BikeSystem::speedDistanceTask() {
|
||||
|
||||
std::chrono::microseconds responseTime =
|
||||
_timer.elapsed_time() - currentStep->callTime;
|
||||
tr_info("Reset task: response time is %" PRIu64 " usecs", responseTime.count());
|
||||
tr_info("Speed distance task: response time is %" PRIu64 " usecs",
|
||||
responseTime.count());
|
||||
|
||||
osStatus status = _mailPedalDevice.free(currentStep);
|
||||
if (status != osOK) {
|
||||
@ -228,6 +259,9 @@ void BikeSystem::speedDistanceTask() {
|
||||
_mutexSpeedometer.unlock();
|
||||
// END CRITICAL SECTION
|
||||
|
||||
ThisThread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
kSpeedDistanceTaskComputationTime - (_timer.elapsed_time() - taskStartTime)));
|
||||
|
||||
_taskLogger.logPeriodAndExecutionTime(
|
||||
_timer, advembsof::TaskLogger::kSpeedTaskIndex, taskStartTime);
|
||||
}
|
||||
@ -239,6 +273,16 @@ void BikeSystem::gearTask() {
|
||||
gearMail_t* currentGear = _mailGearDevice.try_get();
|
||||
|
||||
if (currentGear != nullptr) {
|
||||
#if !defined(MBED_TEST_MODE)
|
||||
std::chrono::microseconds responseTime =
|
||||
_timer.elapsed_time() - currentGear->callTime;
|
||||
tr_info("Gear task: response time is %" PRIu64 " usecs", responseTime.count());
|
||||
#endif
|
||||
|
||||
#if defined(MBED_TEST_MODE)
|
||||
_cbGearChange();
|
||||
#endif
|
||||
|
||||
// ENTER CRITICAL SECTION
|
||||
_mutexGear.lock();
|
||||
_currentGear = currentGear->gear;
|
||||
@ -248,17 +292,17 @@ void BikeSystem::gearTask() {
|
||||
_mutexGearSize.unlock();
|
||||
// END CRITICAL SECTION
|
||||
|
||||
std::chrono::microseconds responseTime =
|
||||
_timer.elapsed_time() - currentGear->callTime;
|
||||
tr_info("Reset task: response time is %" PRIu64 " usecs", responseTime.count());
|
||||
|
||||
osStatus status = _mailGearDevice.free(currentGear);
|
||||
if (status != osOK) {
|
||||
tr_error("free current gear in the gear tasks doesn't work !");
|
||||
}
|
||||
}
|
||||
|
||||
ThisThread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
kGearTaskComputationTime - (_timer.elapsed_time() - taskStartTime)));
|
||||
|
||||
_taskLogger.logPeriodAndExecutionTime(
|
||||
_timer, advembsof::TaskLogger::kSpeedTaskIndex, taskStartTime);
|
||||
_timer, advembsof::TaskLogger::kGearTaskIndex, taskStartTime);
|
||||
}
|
||||
|
||||
/* Main thread functions */
|
||||
|
@ -59,6 +59,9 @@ class BikeSystem {
|
||||
#if defined(MBED_TEST_MODE)
|
||||
const advembsof::TaskLogger& getTaskLogger();
|
||||
bike_computer::Speedometer& getSpeedometer();
|
||||
GearDevice& getGearDevice();
|
||||
void setCallbackGearChage(Callback<void()> cbGearChange);
|
||||
Callback<void()> _cbGearChange;
|
||||
#endif // defined(MBED_TEST_MODE)
|
||||
|
||||
private:
|
||||
|
Reference in New Issue
Block a user