opalib_quick_start.c 2.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * Copyright (c) 2021 LAAS-CNRS
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 2.1 of the License, or
 *   (at your option) 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 Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGLPV2.1
 */

/**
 * @author Clément Foucher <clement.foucher@laas.fr>
 */


/////
// OwnTech Power API includes
#include "timer.h"
#include "hrtim.h"
#include "leg.h"
#include "data_acquisition.h"


/////
// Timer defines
#define TIMER6_NODELABEL DT_NODELABEL(timers6)
#define TIMER6_LABEL     DT_PROP(TIMER6_NODELABEL, label)
static const struct device* timer6;


/////
// Pointer to the user-defined periodic task
static void (*periodic_task_ptr)() = NULL;


/////
// Functions

static void _internal_task()
{
	// Dispatch data from the DMA buffers to per-channel buffers
	data_dispatch_do_dispatch();

	// Call user-defined peroidic task
	if (periodic_task_ptr != NULL)
		periodic_task_ptr();
}

void opalib_quick_start_init(void (*periodic_task)(), uint32_t task_period_us)
{
	/////
	// Store user-defined task
	periodic_task_ptr = periodic_task;

	/////
	// Initialize peripherals

	// ADC
	data_acquisition_init();

	// Leg
71
	leg_init(true);
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

	// HRTIM
	hrtim_adc_trigger_en(0, ADC1R, AD13_TAC3);
	hrtim_cmp_set(0, TIMA, CMP3xR, 1);

	// Timer
	timer6 = device_get_binding(TIMER6_LABEL);
	struct timer_config_t timer_cfg =
	{
		.timer_enable_irq = 1,
		.timer_callback   = _internal_task
	};
	timer_config(timer6, &timer_cfg);

	/////
	// Initialize data dispatch
	data_dispatch_init();

	/////
	// Launch task by starting timer
	timer_start(timer6, task_period_us);
}