2016-01-02 03:02:34 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2016-05-22 21:41:22 -07:00
|
|
|
#include "HAL/Notifier.h"
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2016-06-05 07:33:37 -07:00
|
|
|
// For std::atexit()
|
|
|
|
|
#include <cstdlib>
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2015-11-21 22:17:11 -08:00
|
|
|
#include <atomic>
|
2016-06-05 07:29:47 -07:00
|
|
|
#include <memory>
|
2015-11-21 22:17:11 -08:00
|
|
|
#include <mutex>
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "ChipObject.h"
|
2016-05-22 21:41:22 -07:00
|
|
|
#include "HAL/HAL.h"
|
2016-07-20 22:05:17 -07:00
|
|
|
#include "HAL/cpp/make_unique.h"
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "HAL/cpp/priority_mutex.h"
|
2016-07-13 20:29:28 -07:00
|
|
|
#include "HAL/handles/UnlimitedHandleResource.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
static const uint32_t kTimerInterruptNumber = 28;
|
|
|
|
|
|
2015-11-21 22:17:11 -08:00
|
|
|
static priority_mutex notifierInterruptMutex;
|
|
|
|
|
static priority_recursive_mutex notifierMutex;
|
2016-07-20 22:05:17 -07:00
|
|
|
static std::unique_ptr<tAlarm> notifierAlarm;
|
|
|
|
|
static std::unique_ptr<tInterruptManager> notifierManager;
|
2015-12-30 19:06:47 -08:00
|
|
|
static uint64_t closestTrigger = UINT64_MAX;
|
2016-06-05 07:29:47 -07:00
|
|
|
|
|
|
|
|
namespace {
|
2015-11-21 22:17:11 -08:00
|
|
|
struct Notifier {
|
2016-06-05 07:29:47 -07:00
|
|
|
std::shared_ptr<Notifier> prev, next;
|
2016-05-20 17:30:37 -07:00
|
|
|
void* param;
|
2016-09-05 07:31:51 -07:00
|
|
|
HAL_NotifierProcessFunction process;
|
2016-05-20 17:30:37 -07:00
|
|
|
uint64_t triggerTime = UINT64_MAX;
|
2016-08-15 19:56:32 -07:00
|
|
|
HAL_NotifierHandle handle;
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|
2016-06-05 07:29:47 -07:00
|
|
|
}
|
2016-07-20 22:05:17 -07:00
|
|
|
static std::shared_ptr<Notifier> notifiers;
|
2015-11-21 22:17:11 -08:00
|
|
|
static std::atomic_flag notifierAtexitRegistered = ATOMIC_FLAG_INIT;
|
2015-11-21 22:17:11 -08:00
|
|
|
static std::atomic_int notifierRefCount{0};
|
2015-11-21 22:17:11 -08:00
|
|
|
|
2016-06-05 07:29:47 -07:00
|
|
|
using namespace hal;
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
static UnlimitedHandleResource<HAL_NotifierHandle, Notifier,
|
|
|
|
|
HAL_HandleEnum::Notifier>
|
2016-06-05 07:29:47 -07:00
|
|
|
notifierHandles;
|
|
|
|
|
|
|
|
|
|
// internal version of updateAlarm used during the alarmCallback when we know
|
|
|
|
|
// that the pointer is a valid pointer.
|
2016-08-12 13:45:28 -07:00
|
|
|
void updateNotifierAlarmInternal(std::shared_ptr<Notifier> notifierPointer,
|
2016-06-05 07:29:47 -07:00
|
|
|
uint64_t triggerTime, int32_t* status) {
|
|
|
|
|
std::lock_guard<priority_recursive_mutex> sync(notifierMutex);
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
auto notifier = notifierPointer;
|
2016-06-05 07:29:47 -07:00
|
|
|
// no need for a null check, as this must always be a valid pointer.
|
|
|
|
|
notifier->triggerTime = triggerTime;
|
|
|
|
|
bool wasActive = (closestTrigger != UINT64_MAX);
|
|
|
|
|
|
|
|
|
|
if (!notifierInterruptMutex.try_lock() || notifierRefCount == 0 ||
|
|
|
|
|
!notifierAlarm)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Update alarm time if closer than current.
|
|
|
|
|
if (triggerTime < closestTrigger) {
|
|
|
|
|
closestTrigger = triggerTime;
|
|
|
|
|
// Simply truncate the hardware trigger time to 32-bit.
|
2016-08-11 23:38:45 -07:00
|
|
|
notifierAlarm->writeTriggerTime(static_cast<uint32_t>(triggerTime), status);
|
2016-06-05 07:29:47 -07:00
|
|
|
}
|
|
|
|
|
// Enable the alarm. The hardware disables itself after each alarm.
|
|
|
|
|
if (!wasActive) notifierAlarm->writeEnable(true, status);
|
|
|
|
|
|
|
|
|
|
notifierInterruptMutex.unlock();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
static void alarmCallback(uint32_t, void*) {
|
|
|
|
|
std::unique_lock<priority_recursive_mutex> sync(notifierMutex);
|
|
|
|
|
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
uint64_t currentTime = 0;
|
|
|
|
|
|
|
|
|
|
// the hardware disables itself after each alarm
|
|
|
|
|
closestTrigger = UINT64_MAX;
|
|
|
|
|
|
|
|
|
|
// process all notifiers
|
2016-06-05 07:29:47 -07:00
|
|
|
std::shared_ptr<Notifier> notifier = notifiers;
|
2016-05-20 17:30:37 -07:00
|
|
|
while (notifier) {
|
2016-06-05 07:29:47 -07:00
|
|
|
if (notifier->triggerTime != UINT64_MAX) {
|
2016-07-09 00:24:26 -07:00
|
|
|
if (currentTime == 0) currentTime = HAL_GetFPGATime(&status);
|
2016-05-20 17:30:37 -07:00
|
|
|
if (notifier->triggerTime < currentTime) {
|
|
|
|
|
notifier->triggerTime = UINT64_MAX;
|
|
|
|
|
auto process = notifier->process;
|
2016-08-15 19:56:32 -07:00
|
|
|
auto handle = notifier->handle;
|
2016-05-20 17:30:37 -07:00
|
|
|
sync.unlock();
|
2016-08-15 19:56:32 -07:00
|
|
|
process(currentTime, handle);
|
2016-05-20 17:30:37 -07:00
|
|
|
sync.lock();
|
|
|
|
|
} else if (notifier->triggerTime < closestTrigger) {
|
2016-06-05 07:29:47 -07:00
|
|
|
updateNotifierAlarmInternal(notifier, notifier->triggerTime, &status);
|
2016-05-20 17:30:37 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
notifier = notifier->next;
|
|
|
|
|
}
|
2015-11-21 22:17:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void cleanupNotifierAtExit() {
|
2016-05-20 17:30:37 -07:00
|
|
|
notifierAlarm = nullptr;
|
|
|
|
|
notifierManager = nullptr;
|
2015-11-21 22:17:11 -08:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-11-26 00:08:32 -08:00
|
|
|
extern "C" {
|
|
|
|
|
|
2016-09-05 07:31:51 -07:00
|
|
|
HAL_NotifierHandle HAL_InitializeNotifier(HAL_NotifierProcessFunction process,
|
2016-07-09 00:24:26 -07:00
|
|
|
void* param, int32_t* status) {
|
2016-05-20 17:30:37 -07:00
|
|
|
if (!process) {
|
|
|
|
|
*status = NULL_PARAMETER;
|
2016-06-05 07:29:47 -07:00
|
|
|
return 0;
|
2016-05-20 17:30:37 -07:00
|
|
|
}
|
|
|
|
|
if (!notifierAtexitRegistered.test_and_set())
|
|
|
|
|
std::atexit(cleanupNotifierAtExit);
|
|
|
|
|
if (notifierRefCount.fetch_add(1) == 0) {
|
|
|
|
|
std::lock_guard<priority_mutex> sync(notifierInterruptMutex);
|
|
|
|
|
// create manager and alarm if not already created
|
|
|
|
|
if (!notifierManager) {
|
2016-07-20 22:05:17 -07:00
|
|
|
notifierManager = std::make_unique<tInterruptManager>(
|
|
|
|
|
1 << kTimerInterruptNumber, false, status);
|
2016-05-25 23:07:45 -07:00
|
|
|
notifierManager->registerHandler(alarmCallback, nullptr, status);
|
2016-05-20 17:30:37 -07:00
|
|
|
notifierManager->enable(status);
|
|
|
|
|
}
|
2016-07-20 22:05:17 -07:00
|
|
|
if (!notifierAlarm) notifierAlarm.reset(tAlarm::create(status));
|
2016-05-20 17:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::lock_guard<priority_recursive_mutex> sync(notifierMutex);
|
2016-06-05 07:29:47 -07:00
|
|
|
std::shared_ptr<Notifier> notifier = std::make_shared<Notifier>();
|
2016-08-15 19:56:32 -07:00
|
|
|
HAL_NotifierHandle handle = notifierHandles.Allocate(notifier);
|
|
|
|
|
if (handle == HAL_kInvalidHandle) {
|
2016-09-05 07:31:51 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return HAL_kInvalidHandle;
|
2016-08-15 19:56:32 -07:00
|
|
|
}
|
|
|
|
|
// create notifier structure and add to list
|
2016-05-20 17:30:37 -07:00
|
|
|
notifier->next = notifiers;
|
|
|
|
|
if (notifier->next) notifier->next->prev = notifier;
|
|
|
|
|
notifier->param = param;
|
|
|
|
|
notifier->process = process;
|
2016-08-15 19:56:32 -07:00
|
|
|
notifier->handle = handle;
|
2016-05-20 17:30:37 -07:00
|
|
|
notifiers = notifier;
|
2016-08-15 19:56:32 -07:00
|
|
|
return handle;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) {
|
2016-05-20 17:30:37 -07:00
|
|
|
{
|
|
|
|
|
std::lock_guard<priority_recursive_mutex> sync(notifierMutex);
|
2016-08-12 13:45:28 -07:00
|
|
|
auto notifier = notifierHandles.Get(notifierHandle);
|
2016-06-05 07:29:47 -07:00
|
|
|
if (!notifier) return;
|
2016-05-22 23:21:45 -07:00
|
|
|
|
|
|
|
|
// remove from list
|
2016-05-20 17:30:37 -07:00
|
|
|
if (notifier->prev) notifier->prev->next = notifier->next;
|
|
|
|
|
if (notifier->next) notifier->next->prev = notifier->prev;
|
|
|
|
|
if (notifiers == notifier) notifiers = notifier->next;
|
2016-08-12 13:45:28 -07:00
|
|
|
notifierHandles.Free(notifierHandle);
|
2016-05-20 17:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (notifierRefCount.fetch_sub(1) == 1) {
|
|
|
|
|
std::lock_guard<priority_mutex> sync(notifierInterruptMutex);
|
|
|
|
|
// if this was the last notifier, clean up alarm and manager
|
|
|
|
|
if (notifierAlarm) {
|
|
|
|
|
notifierAlarm->writeEnable(false, status);
|
|
|
|
|
notifierAlarm = nullptr;
|
|
|
|
|
}
|
|
|
|
|
if (notifierManager) {
|
|
|
|
|
notifierManager->disable(status);
|
|
|
|
|
notifierManager = nullptr;
|
|
|
|
|
}
|
|
|
|
|
closestTrigger = UINT64_MAX;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
void* HAL_GetNotifierParam(HAL_NotifierHandle notifierHandle, int32_t* status) {
|
|
|
|
|
auto notifier = notifierHandles.Get(notifierHandle);
|
2016-06-05 07:29:47 -07:00
|
|
|
if (!notifier) return nullptr;
|
|
|
|
|
return notifier->param;
|
2015-12-17 16:19:44 -08:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
void HAL_UpdateNotifierAlarm(HAL_NotifierHandle notifierHandle,
|
2016-07-09 00:24:26 -07:00
|
|
|
uint64_t triggerTime, int32_t* status) {
|
2016-05-20 17:30:37 -07:00
|
|
|
std::lock_guard<priority_recursive_mutex> sync(notifierMutex);
|
2015-11-21 22:17:11 -08:00
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
auto notifier = notifierHandles.Get(notifierHandle);
|
2016-06-05 07:29:47 -07:00
|
|
|
if (!notifier) return;
|
|
|
|
|
updateNotifierAlarmInternal(notifier, triggerTime, status);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2015-12-17 16:19:44 -08:00
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
void HAL_StopNotifierAlarm(HAL_NotifierHandle notifierHandle, int32_t* status) {
|
2016-05-20 17:30:37 -07:00
|
|
|
std::lock_guard<priority_recursive_mutex> sync(notifierMutex);
|
2016-08-12 13:45:28 -07:00
|
|
|
auto notifier = notifierHandles.Get(notifierHandle);
|
2016-06-05 07:29:47 -07:00
|
|
|
if (!notifier) return;
|
2016-05-20 17:30:37 -07:00
|
|
|
notifier->triggerTime = UINT64_MAX;
|
2015-12-17 16:19:44 -08:00
|
|
|
}
|
2015-11-26 00:08:32 -08:00
|
|
|
|
|
|
|
|
} // extern "C"
|