Add programs for lpcan and lpcanvca testing on spejblarm board
[LPC2xxx_and_RobotSpejbl.git] / app / smotor / pwm.c
blobdbce87e13d48504c5a26e8fda0c3f19ec29cf254
1 #include "pwm.h"
3 int PWM_PINSEL[] = {
4 /*nothing*/ 1, /*PWM1*/ 1, 15, 3, 17, /*PWM5*/ 11, /*PWM6*/ 19
5 };
7 uint32_t *PWM_MR[] = {
8 (uint32_t*)&(PWMMR0),
9 (uint32_t*)&(PWMMR1),
10 (uint32_t*)&(PWMMR2),
11 (uint32_t*)&(PWMMR3),
12 (uint32_t*)&(PWMMR4),
13 (uint32_t*)&(PWMMR5),
14 (uint32_t*)&(PWMMR6)
17 void pwm_channel(int n, int double_edge) {
18 uint32_t bit;
20 PWMPCR |= (0x100 | (double_edge && n)) << n;
21 if (n == 5) {
22 PINSEL1 |= 0x00000400;
23 PINSEL1 &= 0xfffff7ff;
25 else {
26 bit = 1 << PWM_PINSEL[n];
27 PINSEL0 |= bit;
28 bit = ~(bit >> 1);
29 PINSEL0 &= bit;
33 void pwm_set(int n, uint32_t when) {
34 *PWM_MR[n] = when;
35 PWMLER |= 1 << n;
38 void pwm_set_double(int n, uint32_t from, uint32_t to) {
39 *PWM_MR[n-1] = from;
40 *PWM_MR[n] = to;
41 PWMLER |= 0x3 << (n-1);
44 void pwm_init(uint32_t prescale, uint32_t period) {
45 PWMPR = prescale - 1;
46 PWMMR0 = period - 1;
47 PWMLER |= 0x1;
48 PWMMCR |= 0x00000002;
49 PWMTCR &= ~0x2;
50 PWMTCR |= 0x9;