block_atsamx70_pwm.c 5.03 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken 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
/*
	Copyright Jeroen Vreeken (jeroen@vreeken.net), 2018

	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
	(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 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/>.

 */

#include <samx70.h>
#include <pwm.h>
#include <pio/samx70.h>
#include <ioport.h>

#include <controller/controller_block.h>
#include <log/log.h>


struct controller_block_private {
	float *in;
	
	int period;
	uint32_t pwm;
	int ch_nr;
	uint32_t outputh;
	uint32_t outputl;
};

static void pwm_calculate(struct controller_block *pwm)
{
	struct controller_block_private *priv = pwm->private;
	uint32_t pw = priv->pwm;
	float in = *priv->in;
	int period = priv->period;
	uint32_t outputl = priv->outputl;
	uint32_t outputh = priv->outputh;
	int ch_nr = priv->ch_nr;
	
	int duty = in * period;

	uint32_t dty = abs(duty);
	/* Make sure we generate negative pulses for capacitor loading */
Jeroen Vreeken's avatar
Jeroen Vreeken committed
52
	// TODO make this a parameter (in seconds off)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
53
	if (dty >= period) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
54
		dty-=1 + period / 1000;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
	}
	
	if (duty > 0) {
		pwm_output_selection_set(pw, outputl);
		pwm_channel_duty_cycle_update_set(pw, ch_nr, dty);
		pwm_output_selection_clear_update(pw, outputh);
	} else if (duty < 0) {
		pwm_output_selection_set(pw, outputh);
		pwm_channel_duty_cycle_update_set(pw, ch_nr, dty);
		pwm_output_selection_clear_update(pw, outputl);
	} else {
		pwm_output_selection_set(pw, outputh | outputl);
		pwm_channel_duty_cycle_update_set(pw, ch_nr, dty);
	}
	
	pwm_sync_update(pw);
}


static struct controller_block_interm_list interms[] = {
	{ "in", CONTROLLER_BLOCK_TERM_FLOAT, offsetof(struct controller_block_private, in) },
	{ NULL },
};



static struct controller_block * block_atsamx70_pwm_create(char *name, int argc, va_list ap)
{
	struct controller_block *pwm;
	int pwm_nr;
	int ch_nr;

	double frequency;
	
	pwm_nr = va_arg(ap, int);
	if (pwm_nr < 0 || pwm_nr > 1) {
		log_send(LOG_T_ERROR, "%s: pwm%d is not valid. (valid: 0-1)",
		    name, pwm_nr);
		return NULL;
	}

	ch_nr = va_arg(ap, int);
	if (ch_nr < 0 || ch_nr > 1) {
		log_send(LOG_T_ERROR, "%s: channel %d is not valid. (valid: 0-3)",
		    name, ch_nr);
		return NULL;
	}
	
	double fclock = sysclk_get_peripheral_hz();
	
	frequency = va_arg(ap, double);
	int period = fclock / frequency;
	
	log_send(LOG_T_DEBUG, "%s: Frequency: %fHz, period: %d (%fHz)", name, 
	    frequency, period, fclock / period);

	if (!(pwm = controller_block_alloc("atsamx70_pwm", name, sizeof(struct controller_block_private))))
		goto err_alloc;

	if (controller_block_interm_list_init(pwm, interms))
		goto err_interm;

	pwm->calculate = pwm_calculate;
	
	pwm->private->ch_nr = ch_nr;
	pwm->private->period = period;
	
	switch (pwm_nr) {
		case 0:
			pwm->private->pwm = PWM0;
			break;
		case 1:
			pwm->private->pwm = PWM1;
			break;
	}
	
	pwm_init(pwm->private->pwm);
	pwm_disable(pwm->private->pwm, ch_nr);

	uint32_t oov = pwm_output_override_get(pwm->private->pwm);
	switch (ch_nr) {
		case 0:
			oov &= ~(PWM_OOV_OOVL0 | PWM_OOV_OOVH0);
			pwm->private->outputh = PWM_OS_OSL0;
			pwm->private->outputl = PWM_OS_OSH0;
			ioport_disable_pin(PIO_PD20_IDX);
			ioport_set_pin_mode(PIO_PD20_IDX, IOPORT_MODE_MUX_A);
			ioport_set_pin_dir(PIO_PD20_IDX, IOPORT_DIR_OUTPUT);
			ioport_disable_pin(PIO_PA19_IDX);
			ioport_set_pin_mode(PIO_PA19_IDX, IOPORT_MODE_MUX_B);
			ioport_set_pin_dir(PIO_PA19_IDX, IOPORT_DIR_OUTPUT);
			break;
		case 1:
			oov &= ~(PWM_OOV_OOVL1 | PWM_OOV_OOVH1);
			pwm->private->outputh = PWM_OS_OSL1;
			pwm->private->outputl = PWM_OS_OSH1;
			break;
		case 2:
			oov &= ~(PWM_OOV_OOVL2 | PWM_OOV_OOVH2);
			pwm->private->outputh = PWM_OS_OSL0;
			pwm->private->outputl = PWM_OS_OSH2;
			break;
		case 3:
			oov &= ~(PWM_OOV_OOVL3 | PWM_OOV_OOVH3);
			pwm->private->outputh = PWM_OS_OSL3;
			pwm->private->outputl = PWM_OS_OSH3;
			break;
	}
	
	pwm_output_override_set(pwm->private->pwm, oov);
	pwm_output_selection_set_update(pwm->private->pwm, pwm->private->outputh | pwm->private->outputl);

	pwm_sync_mode_set(pwm->private->pwm, 
	    PWM_SCM_SYNC0 | PWM_SCM_SYNC1 | PWM_SCM_SYNC2 | PWM_SCM_SYNC3);

	pwm_channel_mode_set(pwm->private->pwm, ch_nr, PWM_CMR_CPOL | PWM_CMR_DPOLI | PWM_CMR_DTLI);

	pwm_channel_period_set(pwm->private->pwm, ch_nr, period);
	pwm_channel_duty_cycle_update_set(pwm->private->pwm, ch_nr, 0);
	pwm_channel_dead_time_set(pwm->private->pwm, ch_nr, 0);
	
	pwm_sync_update(pwm->private->pwm);
	pwm_enable(pwm->private->pwm, ch_nr);

	if (controller_block_add(pwm))
		goto err_add;
	return pwm;

err_add:
err_interm:
	controller_block_free(pwm);
err_alloc:
	return NULL;
}

BLOCK_CREATE(atsamx70_pwm) = {
	.create = block_atsamx70_pwm_create,
	.args = { "int,int,double", NULL },
};