1 /* AT91SAM7 PWM routines for OpenPCD / OpenPICC
2 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <lib_AT91SAM7.h>
22 #include <sys/types.h>
25 #include <os/usb_handler.h>
26 #include <os/pcd_enumerate.h>
27 #include <os/req_ctx.h>
29 #include "../openpcd.h"
37 #define DEBUGPWM DEBUGPCRF
39 #define DEBUGPWM(x, args...)
42 static AT91PS_PWMC pwm = AT91C_BASE_PWMC;
44 /* find highest bit set. returns bit (32..1) or 0 in case no bit set */
45 static int fhs(u_int32_t val)
49 for (i = 32; i > 0; i--) {
50 if (val & (1 << (i-1)))
57 /* set frequency of PWM signal to freq */
58 int pwm_freq_set(int channel, u_int32_t freq)
60 /* in order to get maximum resolution, the pre-scaler must be set to
61 * something like freq << 16. However, the mimimum pre-scaled frequency
62 * we can get is MCLK (48MHz), the minimum is MCLK/(1024*255) =
63 * 48MHz/261120 = 183Hz */
64 u_int32_t overall_div;
65 u_int32_t presc_total;
72 overall_div = MCLK / freq;
73 DEBUGPCRF("mclk=%u, freq=%u, overall_div=%u", MCLK, freq, overall_div);
75 if (overall_div > 0x7fff) {
76 /* divisor is larger than half the maximum CPRD register, we
77 * have to configure prescalers */
78 presc_total = overall_div >> 15;
80 /* find highest 2^n fitting in prescaler (highest bit set) */
81 cpre = fhs(presc_total);
83 /* subtract one, because of fhs semantics */
86 cprd = overall_div / (1 << cpre);
90 DEBUGPCRF("cpre=%u, cprd=%u", cpre, cprd);
91 AT91F_PWMC_CfgChannel(AT91C_BASE_PWMC, channel,
92 cpre|AT91C_PWMC_CPOL, cprd, 1);
97 void pwm_start(int channel)
99 AT91F_PWMC_StartChannel(AT91C_BASE_PWMC, (1 << channel));
102 void pwm_stop(int channel)
104 AT91F_PWMC_StopChannel(AT91C_BASE_PWMC, (1 << channel));
107 void pwm_duty_set_percent(int channel, u_int16_t duty)
109 u_int32_t tmp = pwm->PWMC_CH[channel].PWMC_CPRDR & 0xffff;
111 tmp = tmp << 16; /* extend value by 2^16 */
112 tmp = tmp / 100; /* tmp = 1 % of extended cprd */
113 tmp = duty * tmp; /* tmp = 'duty' % of extended cprd */
114 tmp = tmp >> 16; /* un-extend tmp (divide by 2^16) */
116 DEBUGPWM("Writing %u to Update register\n", tmp);
117 AT91F_PWMC_UpdateChannel(AT91C_BASE_PWMC, channel, tmp);
120 static int pwm_usb_in(struct req_ctx *rctx)
122 struct openpcd_hdr *poh = (struct openpcd_hdr *) rctx->data;
126 case OPENPCD_CMD_PWM_ENABLE:
132 case OPENPCD_CMD_PWM_DUTY_SET:
133 pwm_duty_set_percent(0, poh->val);
135 case OPENPCD_CMD_PWM_DUTY_GET:
138 case OPENPCD_CMD_PWM_FREQ_SET:
139 if (rctx->tot_len < sizeof(*poh)+4)
141 freq = (u_int32_t *) ((unsigned char *) poh) + sizeof(*poh);
142 pwm_freq_set(0, *freq);
144 case OPENPCD_CMD_PWM_FREQ_GET:
154 req_ctx_set_state(rctx, RCTX_STATE_UDP_EP2_PENDING);
161 /* IMPORTANT: Disable PA17 (SSC TD) output */
162 AT91F_PIO_CfgInput(AT91C_BASE_PIOA, AT91C_PIO_PA17);
164 /* Set PA23 to Peripheral A (PWM0) */
165 AT91F_PIO_CfgPeriph(AT91C_BASE_PIOA, 0, OPENPCD_PIO_MFIN_PWM);
167 /* Enable Clock for PWM controller */
170 usb_hdlr_register(&pwm_usb_in, OPENPCD_CMD_CLS_PWM);
175 usb_hdlr_unregister(OPENPCD_CMD_CLS_PWM);
176 AT91F_PMC_DisablePeriphClock(AT91C_BASE_PMC, (1 << AT91C_ID_PWMC));