timer.cpp 2.31 KB
Newer Older
1 2 3 4 5 6
/** Timer related helper functions
 *
 * These functions present a simpler interface to Xilinx' Timer Counter driver (XTmrCtr_*)
 *
 * @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
 * @author Daniel Krebs <github@daniel-krebs.net>
Steffen Vogel's avatar
Steffen Vogel committed
7
 * @copyright 2017-2018, Steffen Vogel
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * @license GNU General Public License (version 3)
 *
 * VILLASfpga
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *********************************************************************************/

26 27
#include <cstdint>

28
#include <xilinx/xtmrctr.h>
29

30 31
#include <villas/fpga/ips/timer.hpp>
#include <villas/fpga/ips/intc.hpp>
32 33 34 35 36 37 38 39 40

namespace villas {
namespace fpga {
namespace ip {


// instantiate factory to make available to plugin infrastructure
static TimerFactory factory;

41
bool Timer::init()
42 43
{
	XTmrCtr_Config xtmr_cfg;
44
	xtmr_cfg.SysClockFreqHz = getFrequency();
45

46
	XTmrCtr_CfgInitialize(&xTmr, &xtmr_cfg, getBaseAddr(registerMemory));
Daniel Krebs's avatar
Daniel Krebs committed
47
	XTmrCtr_InitHw(&xTmr);
48

49 50
	if(irqs.find(irqName) == irqs.end()) {
		logger->error("IRQ '{}' not found but required", irqName);
51 52 53
		return false;
	}

54 55
	// disable so we don't receive any stray interrupts
	irqs[irqName].irqController->disableInterrupt(irqs[irqName]);
56 57 58 59 60 61

	return true;
}

bool Timer::start(uint32_t ticks)
{
62
	irqs[irqName].irqController->enableInterrupt(irqs[irqName], false);
63 64 65 66 67

	XTmrCtr_SetOptions(&xTmr, 0, XTC_EXT_COMPARE_OPTION | XTC_DOWN_COUNT_OPTION);
	XTmrCtr_SetResetValue(&xTmr, 0, ticks);
	XTmrCtr_Start(&xTmr, 0);

68 69 70
	return true;
}

71 72
bool Timer::wait()
{
73 74
	int count = irqs[irqName].irqController->waitForInterrupt(irqs[irqName]);
	irqs[irqName].irqController->disableInterrupt(irqs[irqName]);
75 76 77 78 79 80 81 82 83

	return (count == 1);
}

uint32_t Timer::remaining()
{
	return XTmrCtr_GetValue(&xTmr, 0);
}

84 85 86 87

} // namespace ip
} // namespace fpga
} // namespace villas