summaryrefslogtreecommitdiff
path: root/firmware/src/pcd/main_pwm.c
blob: 271daaf9609f9812f67fdf4230dbe62700b9669f (plain)
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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* main_pwm - OpenPCD firmware for generating a PWM-modulated 13.56MHz
 *	      carrier
 *
 * To use this, you need to connect PIOA P0 with MFIN of the reader.
 *
 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
 *
 *  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 2 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <errno.h>
#include <string.h>
#include <lib_AT91SAM7.h>
#include "../openpcd.h"
#include <pcd/rc632.h>
#include <os/dbgu.h>
#include <os/led.h>
#include <os/pwm.h>
#include <os/tc_cdiv.h>
#include <os/pcd_enumerate.h>
#include <os/usb_handler.h>

#ifdef SSC
#include <pcd/ssc.h>
#endif

#define RAH NULL

static uint8_t force_100ask = 1;
static uint8_t mod_conductance = 0x3f;
static uint8_t cw_conductance = 0x3f;
static uint16_t duty_percent = 22;

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
static uint32_t pwm_freq[] = { 105937, 211875, 423750, 847500 };
static uint8_t pwm_freq_idx = 0;

static void rc632_modulate_mfin()
{
	opcd_rc632_reg_write(RAH, RC632_REG_TX_CONTROL, 
			RC632_TXCTRL_MOD_SRC_MFIN|RC632_TXCTRL_TX2_INV|
			RC632_TXCTRL_TX1_RF_EN|RC632_TXCTRL_TX2_RF_EN);
}

/* (77/40) ^ EXPcsCfgCW */
/* 1, 1.925, 3.705625, 7.13332812 */

#define COND_MANT(x)	(x & 0x0f)
#define COND_EXP(x)	((x & 0x30) >> 4)

static const uint16_t rsrel_expfact[] = { 1000, 1925, 3706, 7133 };

static uint32_t calc_conduct_rel(uint8_t inp)
{
	uint32_t cond_rel;

	cond_rel = COND_MANT(inp) * rsrel_expfact[COND_EXP(inp)];
	cond_rel = cond_rel / 1000;

	return cond_rel;
}

static const uint8_t rsrel_table[] = {
	0, 16, 32, 48, 1, 17, 2, 3, 33, 18, 4, 5, 19, 6, 7, 49, 34, 20,
	8, 9, 21, 10, 11, 35, 22, 12, 13, 23, 14, 50, 36, 15, 24, 25,
	37, 26, 27, 51, 38, 28, 29, 39, 30, 52, 31, 40, 41, 53, 42, 43,
	54, 44, 45, 55, 46, 47, 56, 57, 58, 59, 60, 61, 62, 63 };


static const uint16_t cdivs[] = { 128, 64, 32, 16 };
static int cdiv_idx = 0;

static void help(void)
{
	DEBUGPCR("o: decrease duty     p: increase duty\r\n"
		 "k: stop pwm          l: start pwn\r\n"
		 "n: decrease freq     m: incresae freq\r\n"
		 "v: decrease mod_cond b: increase mod_cond\r\n"
		 "g: decrease cw_cond  h: increase cw_cond");
	DEBUGPCR("u: PA23 const 1      y: PA23 const 0\r\n"
		 "t: PA23 PWM0         f: toggle Force100ASK\r\n"
		 "{: decrease cdiv_idx }: increse cdiv idx");
}

void _init_func(void)
{
	DEBUGPCR("\r\n===> main_pwm <===\r\n");
	help();

	rc632_init();
	DEBUGPCRF("turning on RF");
	rc632_turn_on_rf(RAH);

	/* switch PA17 (connected to MFIN on board) to input */
	AT91F_PIO_CfgInput(AT91C_BASE_PIOA, AT91C_PIO_PA17);

	DEBUGPCRF("Initializing carrier divider");
	tc_cdiv_init();

	DEBUGPCRF("Initializing PWM");
	pwm_init();
	pwm_freq_set(0, 105937);
	pwm_start(0);
	pwm_duty_set_percent(0, 22);	/* 22% of 9.43uS = 2.07uS */
	rc632_modulate_mfin();

#ifdef SSC
	DEBUGPCRF("Initializing SSC RX");
	ssc_rx_init();
#endif
}

int _main_dbgu(char key)
{
	switch (key) {
	case 'o':
		if (duty_percent >= 1)
			duty_percent--;
		pwm_duty_set_percent(0, duty_percent);
		break;
	case 'p':
		if (duty_percent <= 99)
			duty_percent++;
		pwm_duty_set_percent(0, duty_percent);
		break;
	case 'k':
		pwm_stop(0);
		break;
	case 'l':
		pwm_start(0);
		break;
	case 'n':
		if (pwm_freq_idx > 0) {
			pwm_freq_idx--;
			pwm_stop(0);
			pwm_freq_set(0, pwm_freq[pwm_freq_idx]);
			pwm_start(0);
			pwm_duty_set_percent(0, 22);	/* 22% of 9.43uS = 2.07uS */
		}
		break;
	case 'm':
		if (pwm_freq_idx < ARRAY_SIZE(pwm_freq)-1) {
			pwm_freq_idx++;
			pwm_stop(0);
			pwm_freq_set(0, pwm_freq[pwm_freq_idx]);
			pwm_start(0);
			pwm_duty_set_percent(0, 22);	/* 22% of 9.43uS = 2.07uS */
		}
		break;
	case 'u':
		DEBUGPCRF("PA23 output high");
		AT91F_PIO_CfgOutput(AT91C_BASE_PIOA, AT91C_PIO_PA23);
		AT91F_PIO_SetOutput(AT91C_BASE_PIOA, AT91C_PIO_PA23);
		break;
	case 'y':
		DEBUGPCRF("PA23 output low");
		AT91F_PIO_CfgOutput(AT91C_BASE_PIOA, AT91C_PIO_PA23);
		AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, AT91C_PIO_PA23);
		return 0;
		break;
	case 't':
		DEBUGPCRF("PA23 PeriphA (PWM)");
		AT91F_PIO_CfgPeriph(AT91C_BASE_PIOA, 0, AT91C_PA23_PWM0);
		return 0;
		break;
	case 'f':
		DEBUGPCRF("%sabling Force100ASK", force_100ask ? "Dis":"En");
		if (force_100ask) {
			force_100ask = 0;
			opcd_rc632_clear_bits(RAH, RC632_REG_TX_CONTROL,
					      RC632_TXCTRL_FORCE_100_ASK);
		} else {
			force_100ask = 1;
			opcd_rc632_set_bits(RAH, RC632_REG_TX_CONTROL,
					    RC632_TXCTRL_FORCE_100_ASK);
		}
		return 0;
		break;
	case 'v':
		if (mod_conductance > 0) {
			mod_conductance--;
			opcd_rc632_reg_write(RAH, RC632_REG_MOD_CONDUCTANCE,
					     rsrel_table[mod_conductance]);
		}
		break;
	case 'b':
		if (mod_conductance < 0x3f) {
			mod_conductance++;
			opcd_rc632_reg_write(RAH, RC632_REG_MOD_CONDUCTANCE,
					     rsrel_table[mod_conductance]);
		}
		break;
	case 'g':
		if (cw_conductance > 0) {
			cw_conductance--;
			opcd_rc632_reg_write(RAH, RC632_REG_CW_CONDUCTANCE, 
					     rsrel_table[cw_conductance]);
		}
		break;
	case 'h':
		if (cw_conductance < 0x3f) {
			cw_conductance++;
			opcd_rc632_reg_write(RAH, RC632_REG_CW_CONDUCTANCE, 
					     rsrel_table[cw_conductance]);
		}
		break;
	case '?':
		help();
		return 0;
		break;
	case '<':
		tc_cdiv_phase_inc();
		break;
	case '>':
		tc_cdiv_phase_dec();
		break;
	case '{':
		if (cdiv_idx > 0)
			cdiv_idx--;
		tc_cdiv_set_divider(cdivs[cdiv_idx]);
		break;
	case '}':
		if (cdiv_idx < ARRAY_SIZE(cdivs)-1)
			cdiv_idx++;
		tc_cdiv_set_divider(cdivs[cdiv_idx]);
		break;
#ifdef SSC
	case 's':
		ssc_rx_start();
		break;
	case 'S':
		ssc_rx_stop();
		break;
#endif
	default:
		return -EINVAL;
	}

	DEBUGPCR("pwm_freq=%u, duty_percent=%u, mod_cond=%u, cw_cond=%u tc_cdiv=%u", 
		 pwm_freq[pwm_freq_idx], duty_percent, mod_conductance, cw_conductance,
		 cdivs[cdiv_idx]);
	
	return 0;
}


void _main_func(void)
{
	/* first we try to get rid of pending to-be-sent stuff */
	usb_out_process();

	/* next we deal with incoming requests from USB EP1 (OUT) */
	usb_in_process();

	/* try unthrottling sources since we now are [more] likely to
	 * have empty request contexts */
	rc632_unthrottle();
#ifdef SSC
	ssc_rx_unthrottle();
#endif

	led_toggle(2);
}
personal git repositories of Harald Welte. Your mileage may vary