owntech_dac_configure.c 2.33 KB
Newer Older
Clément Foucher's avatar
Clément Foucher committed
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * 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>
 */


// Zephyr
#include <zephyr.h>

// STM32LL
#include <stm32_ll_dac.h>

// Owntech driver
#include "dac.h"


// TODO: this function must *always* be run.
// Write a driver to load on startup without user action?
void owntech_dac_dac2_constant_init()
{
	const struct device* dac2 = device_get_binding(DAC2_LABEL);

	dac_pin_config_t pin_config =
	{
		.pin_connect       = LL_DAC_OUTPUT_CONNECT_GPIO,
		.pin_buffer_enable = LL_DAC_OUTPUT_BUFFER_ENABLE
	};

	dac_set_const_value(dac2, 1, 2048U);
	dac_pin_configure(dac2, 1, &pin_config);
	dac_start(dac2, 1);
}

void owntech_dac_dac1_dac3_current_mode_init()
{
	const struct device* dac1 = device_get_binding(DAC1_LABEL);
	const struct device* dac3 = device_get_binding(DAC3_LABEL);

	dac_function_config_t function_config =
	{
		.dac_function = dac_function_sawtooth,
		.trigger_source = LL_DAC_TRIG_EXT_HRTIM_RST_TRG1,
		.step_trigger_source = LL_DAC_TRIG_EXT_HRTIM_STEP_TRG1,
		.polarity = LL_DAC_SAWTOOTH_POLARITY_DECREMENT,
		.reset_data = 4000,
		.step_data = 200
	};

	dac_pin_config_t pin_config =
	{
		.pin_connect       = LL_DAC_OUTPUT_CONNECT_INTERNAL,
		.pin_buffer_enable = LL_DAC_OUTPUT_BUFFER_DISABLE
	};

	// DAC 1
	dac_set_function(dac1, 1, &function_config);
	dac_pin_configure(dac1, 1, &pin_config);
	dac_start(dac1, 1);

	// DAC 3
	function_config.trigger_source = LL_DAC_TRIG_EXT_HRTIM_RST_TRG2;
	function_config.step_trigger_source = LL_DAC_TRIG_EXT_HRTIM_STEP_TRG2;

	dac_set_function(dac3, 1, &function_config);
	dac_pin_configure(dac3, 1, &pin_config);
	dac_start(dac3, 1);
}