block_am335x_pwm.c 6.36 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
/*
	Copyright Jeroen Vreeken (jeroen@vreeken.net), 2014

	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 "am335x.h"

#include <controller/controller_block.h>
#include <log/log.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <inttypes.h>
#include <unistd.h>
Jeroen Vreeken's avatar
Add qed    
Jeroen Vreeken committed
29
#include <math.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
30
31
32
33


struct controller_block_private {
	float *in;
Jeroen Vreeken's avatar
Add qed    
Jeroen Vreeken committed
34
35
36
37

	uint16_t tbprd;	// period
	double freq;
	bool sym;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
38
39
40
41
42
43
	
	void *base;
};

static void pwm_calculate(struct controller_block *pwm)
{
Jeroen Vreeken's avatar
Add qed    
Jeroen Vreeken committed
44
45
46
	struct controller_block_private *priv = pwm->private;
	float in;
	uint16_t cmp;
root's avatar
root committed
47
	float tbprd;
Jeroen Vreeken's avatar
Add qed    
Jeroen Vreeken committed
48
49
	bool sign;
	void *base = priv->base;
root's avatar
root committed
50

Jeroen Vreeken's avatar
Add qed    
Jeroen Vreeken committed
51
52
53
	tbprd = priv->tbprd;
	in = *priv->in;
	sign = signbit(in);
root's avatar
root committed
54
	cmp = fminf(fabsf(in) * tbprd, tbprd);
Jeroen Vreeken's avatar
Add qed    
Jeroen Vreeken committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

	if (sign) {
		am335x_write16(base, AM335X_PWMSS_REG_EPWM_CMPA, 0);
		am335x_write16(base, AM335X_PWMSS_REG_EPWM_CMPB, cmp);
	} else {
		am335x_write16(base, AM335X_PWMSS_REG_EPWM_CMPA, cmp);
		am335x_write16(base, AM335X_PWMSS_REG_EPWM_CMPB, 0);
	}
}

static void set_freq(struct controller_block *pwm)
{
	struct controller_block_private *priv = pwm->private;
	void *base = priv->base;
	int clkdiv = 0;
	int div = 1;
	double period, freq;
	uint16_t tbprd;
root's avatar
root committed
73
	uint16_t aqctl;
Jeroen Vreeken's avatar
Add qed    
Jeroen Vreeken committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
	
	/* double divider for symmetric use */
	period = AM335X_SYSCLK / (priv->freq * ( priv->sym ? 2.0 : 1.0));

	while (period > AM335X_PWMSS_EPWM_TBPRD_MAX) {
		clkdiv++;
		div *= 2;
		period /= 2.0;
		if (clkdiv == AM335X_PWMSS_EPWM_TBCTL_CLKDIV_MAX)
			break;
	}
	
	tbprd = period;
	
	am335x_write16(base, AM335X_PWMSS_REG_EPWM_TBCTL,
	    AM335X_PWMSS_EPWM_TBCTL_CLKDIV_SET(clkdiv) |
	    AM335X_PWMSS_EPWM_TBCTL_SYNC0SEL_DIS |
root's avatar
root committed
91
92
93
	    (priv->sym ? 
	        AM335X_PWMSS_EPWM_TBCTL_CTRMODE_UD : 
		AM335X_PWMSS_EPWM_TBCTL_CTRMODE_DN));
Jeroen Vreeken's avatar
Add qed    
Jeroen Vreeken committed
94
95
	am335x_write16(base, AM335X_PWMSS_REG_EPWM_CMPCTL, 0);

root's avatar
root committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
	if (priv->sym) {
		aqctl =
		    AM335X_PWMSS_EPWM_AQCTLA_CBD_NOP |
		    AM335X_PWMSS_EPWM_AQCTLA_CBU_NOP |
		    AM335X_PWMSS_EPWM_AQCTLA_CAD_SET |
		    AM335X_PWMSS_EPWM_AQCTLA_CAU_CLR |
		    AM335X_PWMSS_EPWM_AQCTLA_PRD_NOP;
	} else {
		aqctl =
		    AM335X_PWMSS_EPWM_AQCTLA_CBD_NOP |
		    AM335X_PWMSS_EPWM_AQCTLA_CBU_NOP |
		    AM335X_PWMSS_EPWM_AQCTLA_CAD_SET |
		    AM335X_PWMSS_EPWM_AQCTLA_CAU_CLR |
		    AM335X_PWMSS_EPWM_AQCTLA_PRD_CLR;
	}
	am335x_write16(base, AM335X_PWMSS_REG_EPWM_AQCTLA, aqctl);
	am335x_write16(base, AM335X_PWMSS_REG_EPWM_AQCTLB, aqctl);
   
Jeroen Vreeken's avatar
Add qed    
Jeroen Vreeken committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	am335x_write16(base, AM335X_PWMSS_REG_EPWM_AQSFRC, 0);
	
	am335x_write16(base, AM335X_PWMSS_REG_EPWM_TBPRD, tbprd);
	am335x_write16(base, AM335X_PWMSS_REG_EPWM_CMPA, 0);
	am335x_write16(base, AM335X_PWMSS_REG_EPWM_CMPB, 0);

	priv->tbprd = tbprd;
	
	freq = AM335X_SYSCLK / (tbprd * div * ( priv->sym ? 2.0 : 1.0));
	
	log_send(LOG_T_DEBUG, 
	    "%s: Frequency: %gHz, period: %d ticks, divider: %d",
	    pwm->name, freq, tbprd, div);
	priv->freq = freq;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
128
129
130
}

static struct controller_block_param_list params[] = {
Jeroen Vreeken's avatar
Add qed    
Jeroen Vreeken committed
131
132
	{ "frequency",   false },
	{ "symmetrical", false },
Jeroen Vreeken's avatar
Jeroen Vreeken committed
133
134
135
136
137
	{ NULL },
};

static void param_set(struct controller_block *pwm, int param, va_list val)
{
Jeroen Vreeken's avatar
Add qed    
Jeroen Vreeken committed
138
139
140
141
142
143
144
145
146
147
	switch (param) {
		case 0:
			pwm->private->freq = va_arg(val, double);
			set_freq(pwm);
			break;
		case 1:
			pwm->private->sym = va_arg(val, int);
			set_freq(pwm);
			break;
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
}

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


struct controller_block * block_am335x_pwm_create(char *name, va_list ap)
{
	struct controller_block *pwm;
	int fd;
	void *base;
	uint32_t reg;
	int pwm_nr;
	size_t offset;
	
	pwm_nr = va_arg(ap, int);
	if (pwm_nr < 0 || pwm_nr > 2) {
		log_send(LOG_T_ERROR, "%s: pwm%d is not valid. (valid: 0-2)",
		    name, pwm_nr);
		return NULL;
	}
	switch (pwm_nr) {
		case 0:
			offset = AM335X_PWMSS0_BASE;
			break;
		case 1:
			offset = AM335X_PWMSS1_BASE;
			break;
		case 2:
			offset = AM335X_PWMSS2_BASE;
			break;
	}
	
	if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0 ) {
		log_send(LOG_T_ERROR, "%s: Error opening /dev/mem", name);
		close(fd);
		return NULL;
	}

	base = mmap(NULL, AM335X_PWMSS_SIZE, PROT_READ | PROT_WRITE, 
	    MAP_SHARED, fd, offset);
	close(fd);
	if (!base) {
		log_send(LOG_T_ERROR, "Mapping PWM failed");
		return NULL;
	}
	log_send(LOG_T_DEBUG, "%s: PWM mapped @ %p", name, base);
	
	reg = am335x_read32(base, AM335X_PWMSS_REG_IDVER);
	log_send(LOG_T_DEBUG, 
	    "%s: IDVER: 0x%" PRIx32 ": func: 0x%03x rev: %d.%d",
	    name, reg, AM335X_PWMSS_IDVER_FUNC_GET(reg),
	    AM335X_PWMSS_IDVER_X_MAJOR_GET(reg),
	    AM335X_PWMSS_IDVER_Y_MINOR_GET(reg));
	if (AM335X_PWMSS_IDVER_FUNC_GET(reg) != AM335X_PWMSS_IDVER_FUNC) {
		log_send(LOG_T_ERROR, "Unexpected functional number");
		goto err_rev;
	}

Jeroen Vreeken's avatar
Add qed    
Jeroen Vreeken committed
209
210
211
212
213
214
215
216
217
218
219
	am335x_write16(base, AM335X_PWMSS_REG_EPWM_PCCTL, 0);

	am335x_write16(base, AM335X_PWMSS_REG_EPWM_DBCTL, 0);
	am335x_write16(base, AM335X_PWMSS_REG_EPWM_TZCTL,
	    AM335X_PWMSS_EPWM_TZCTL_TZA_NOP |
	    AM335X_PWMSS_EPWM_TZCTL_TZB_NOP );

	am335x_write16(base, AM335X_PWMSS_REG_EPWM_TZEINT, 0);
	am335x_write16(base, AM335X_PWMSS_REG_EPWM_ETSEL, 0);
	am335x_write16(base, AM335X_PWMSS_REG_EPWM_HRCNFG, 0);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
220
221
222
223
224
225
226
227
	if (!(pwm = controller_block_alloc("am355x_pwm", name, sizeof(struct controller_block_private))))
		goto err_alloc;

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

	pwm->private->base = base;

Jeroen Vreeken's avatar
Add qed    
Jeroen Vreeken committed
228
229
230
231
232
233
234
235
236
	pwm->private->sym = true;
	pwm->private->freq = 10000;
	set_freq(pwm);

	am335x_write32(base, AM335X_PWMSS_REG_CLKCONFIG,
	    (am335x_read32(base, AM335X_PWMSS_REG_CLKCONFIG) &
	    ~AM335X_PWMSS_CLKCONFIG_EPWMCLKSTOP_REQ) |
	    AM335X_PWMSS_CLKCONFIG_EPWMCLK_EN);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
	pwm->calculate = pwm_calculate;

	if (controller_block_param_list_init(pwm, params))
		goto err_param;

	pwm->param_set = param_set;

	controller_block_add(pwm);
	return pwm;

err_param:
err_interm:
	controller_block_free(pwm);
err_alloc:
err_rev:
	munmap(base, AM335X_PWMSS_SIZE);
	return NULL;
}