1 /* main_pwm - OpenPCD firmware for generating a PWM-modulated 13.56MHz
4 * To use this, you need to connect PIOA P0 with MFIN of the reader.
6 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <lib_AT91SAM7.h>
27 #include "../openpcd.h"
28 #include <pcd/rc632.h>
32 #include <os/tc_cdiv.h>
33 #include <os/pcd_enumerate.h>
34 #include <os/usb_handler.h>
42 static u_int8_t force_100ask = 1;
43 static u_int8_t mod_conductance = 0x3f;
44 static u_int8_t cw_conductance = 0x3f;
45 static u_int16_t duty_percent = 22;
47 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
48 static u_int32_t pwm_freq[] = { 105937, 211875, 423750, 847500 };
49 static u_int8_t pwm_freq_idx = 0;
51 static void rc632_modulate_mfin()
53 opcd_rc632_reg_write(RAH, RC632_REG_TX_CONTROL,
54 RC632_TXCTRL_MOD_SRC_MFIN|RC632_TXCTRL_TX2_INV|
55 RC632_TXCTRL_TX1_RF_EN|RC632_TXCTRL_TX2_RF_EN);
58 /* (77/40) ^ EXPcsCfgCW */
59 /* 1, 1.925, 3.705625, 7.13332812 */
61 #define COND_MANT(x) (x & 0x0f)
62 #define COND_EXP(x) ((x & 0x30) >> 4)
64 static const u_int16_t rsrel_expfact[] = { 1000, 1925, 3706, 7133 };
66 static u_int32_t calc_conduct_rel(u_int8_t inp)
70 cond_rel = COND_MANT(inp) * rsrel_expfact[COND_EXP(inp)];
71 cond_rel = cond_rel / 1000;
76 static const u_int8_t rsrel_table[] = {
77 0, 16, 32, 48, 1, 17, 2, 3, 33, 18, 4, 5, 19, 6, 7, 49, 34, 20,
78 8, 9, 21, 10, 11, 35, 22, 12, 13, 23, 14, 50, 36, 15, 24, 25,
79 37, 26, 27, 51, 38, 28, 29, 39, 30, 52, 31, 40, 41, 53, 42, 43,
80 54, 44, 45, 55, 46, 47, 56, 57, 58, 59, 60, 61, 62, 63 };
83 static const u_int16_t cdivs[] = { 128, 64, 32, 16 };
84 static int cdiv_idx = 0;
86 static void help(void)
88 DEBUGPCR("o: decrease duty p: increase duty\r\n"
89 "k: stop pwm l: start pwn\r\n"
90 "n: decrease freq m: incresae freq\r\n"
91 "v: decrease mod_cond b: increase mod_cond\r\n"
92 "g: decrease cw_cond h: increase cw_cond");
93 DEBUGPCR("u: PA23 const 1 y: PA23 const 0\r\n"
94 "t: PA23 PWM0 f: toggle Force100ASK\r\n"
95 "{: decrease cdiv_idx }: increse cdiv idx");
100 DEBUGPCR("\r\n===> main_pwm <===\r\n");
104 DEBUGPCRF("turning on RF");
105 rc632_turn_on_rf(RAH);
107 /* switch PA17 (connected to MFIN on board) to input */
108 AT91F_PIO_CfgInput(AT91C_BASE_PIOA, AT91C_PIO_PA17);
110 DEBUGPCRF("Initializing carrier divider");
113 DEBUGPCRF("Initializing PWM");
115 pwm_freq_set(0, 105937);
117 pwm_duty_set_percent(0, 22); /* 22% of 9.43uS = 2.07uS */
118 rc632_modulate_mfin();
121 DEBUGPCRF("Initializing SSC RX");
126 int _main_dbgu(char key)
130 if (duty_percent >= 1)
132 pwm_duty_set_percent(0, duty_percent);
135 if (duty_percent <= 99)
137 pwm_duty_set_percent(0, duty_percent);
146 if (pwm_freq_idx > 0) {
149 pwm_freq_set(0, pwm_freq[pwm_freq_idx]);
151 pwm_duty_set_percent(0, 22); /* 22% of 9.43uS = 2.07uS */
155 if (pwm_freq_idx < ARRAY_SIZE(pwm_freq)-1) {
158 pwm_freq_set(0, pwm_freq[pwm_freq_idx]);
160 pwm_duty_set_percent(0, 22); /* 22% of 9.43uS = 2.07uS */
164 DEBUGPCRF("PA23 output high");
165 AT91F_PIO_CfgOutput(AT91C_BASE_PIOA, AT91C_PIO_PA23);
166 AT91F_PIO_SetOutput(AT91C_BASE_PIOA, AT91C_PIO_PA23);
169 DEBUGPCRF("PA23 output low");
170 AT91F_PIO_CfgOutput(AT91C_BASE_PIOA, AT91C_PIO_PA23);
171 AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, AT91C_PIO_PA23);
175 DEBUGPCRF("PA23 PeriphA (PWM)");
176 AT91F_PIO_CfgPeriph(AT91C_BASE_PIOA, 0, AT91C_PA23_PWM0);
180 DEBUGPCRF("%sabling Force100ASK", force_100ask ? "Dis":"En");
183 opcd_rc632_clear_bits(RAH, RC632_REG_TX_CONTROL,
184 RC632_TXCTRL_FORCE_100_ASK);
187 opcd_rc632_set_bits(RAH, RC632_REG_TX_CONTROL,
188 RC632_TXCTRL_FORCE_100_ASK);
193 if (mod_conductance > 0) {
195 opcd_rc632_reg_write(RAH, RC632_REG_MOD_CONDUCTANCE,
196 rsrel_table[mod_conductance]);
200 if (mod_conductance < 0x3f) {
202 opcd_rc632_reg_write(RAH, RC632_REG_MOD_CONDUCTANCE,
203 rsrel_table[mod_conductance]);
207 if (cw_conductance > 0) {
209 opcd_rc632_reg_write(RAH, RC632_REG_CW_CONDUCTANCE,
210 rsrel_table[cw_conductance]);
214 if (cw_conductance < 0x3f) {
216 opcd_rc632_reg_write(RAH, RC632_REG_CW_CONDUCTANCE,
217 rsrel_table[cw_conductance]);
233 tc_cdiv_set_divider(cdivs[cdiv_idx]);
236 if (cdiv_idx < ARRAY_SIZE(cdivs)-1)
238 tc_cdiv_set_divider(cdivs[cdiv_idx]);
252 DEBUGPCR("pwm_freq=%u, duty_percent=%u, mod_cond=%u, cw_cond=%u tc_cdiv=%u",
253 pwm_freq[pwm_freq_idx], duty_percent, mod_conductance, cw_conductance,
260 void _main_func(void)
262 /* first we try to get rid of pending to-be-sent stuff */
265 /* next we deal with incoming reqyests from USB EP1 (OUT) */
268 /* try unthrottling sources since we now are [more] likely to
269 * have empty request contexts */