From 773f6a91053e99de8afaa6e4b450e13f416ba98c Mon Sep 17 00:00:00 2001 From: hackbard Date: Mon, 24 Sep 2007 21:50:42 +0200 Subject: [PATCH] forgot to add the pwm code --- betty/pwm.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ betty/pwm.h | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 betty/pwm.c create mode 100644 betty/pwm.h diff --git a/betty/pwm.c b/betty/pwm.c new file mode 100644 index 0000000..ec1cbf4 --- /dev/null +++ b/betty/pwm.c @@ -0,0 +1,70 @@ +/* + * pwm.c - arm pwm interface + * + * author: hackbard@hackdaworld.org + * + */ + +#include "pwm.h" + +/* + * functions + */ + +void pwm_set_rate_and_prescaler(u32 rate,u32 prescaler) { + + PWMMR0=rate; + PWMPR=prescaler; +} + +int pwm_config(u8 pwmchan,u8 mode,u32 val1,u32 val2) { + + u32 *addr; + + addr=(u32 *)&PWMMR0; + + if((pwmchan>6)|(pwmchan<1)) + return PWM_INVALID_CHAN; + + if(mode==PWM_DOUBLE_EDGE) { + if(pwmchan&0x01) + return PWM_STUPID; + *((volatile u32 *)addr+pwmchan)=val2; + *((volatile u32 *)addr+pwmchan-1)=val1; + } + + if(mode==PWM_SINGLE_EDGE) + *((volatile u32 *)addr+pwmchan)=val1; + + PWMPCR=(PWMPCR&0x7e7c)|(mode<<(2+pwmchan))|(1<<(9+pwmchan)); + + return PWM_SET; +} + +int pwm_update_match(u8 pwmchan,u8 mode,u32 val1,u32 val2) { + + u32 *addr; + + addr=(u32 *)&PWMMR0; + + if((pwmchan>6)|(pwmchan<1)) + return PWM_INVALID_CHAN; + + if(mode==PWM_DOUBLE_EDGE) { + if(pwmchan&0x01) + return PWM_STUPID; + *((volatile u32 *)addr+pwmchan)=val2; + *((volatile u32 *)addr+pwmchan-1)=val1; + } + + if(mode==PWM_SINGLE_EDGE) + *((volatile u32 *)addr+pwmchan)=val1; + + return PWM_SET; +} + +void pwm_match_ctrl_config(u8 matchreg,u8 ctrl) { + + PWMMCR=(PWMMCR&0x1fffff)|(ctrl<<(3*matchreg)); +} + diff --git a/betty/pwm.h b/betty/pwm.h new file mode 100644 index 0000000..9d1903f --- /dev/null +++ b/betty/pwm.h @@ -0,0 +1,44 @@ +/* + * pwm.h - arm pwm interface header file + * + * author: hackbard@hackdaworld.org + * + */ + +#ifndef PWM_H +#define PWM_H + +#include "lpc2xxx.h" +#include "types.h" + +/* macros */ +#define pwm_enable PWMTCR=0x0b + +/* defines */ +#define PWM_SINGLE_EDGE 0x00 +#define PWM_DOUBLE_EDGE 0x01 + +#define PWM_MATCH_INTERRUPT 0x01 +#define PWM_MATCH_RESET 0x02 +#define PWM_MATCH_STOP 0x04 + +#define PWM_SET 0x00 +#define PWM_INVALID_CHAN 0x01 +#define PWM_STUPID 0x02 + +#define PWM_CHAN0 0 +#define PWM_CHAN1 1 +#define PWM_CHAN2 2 +#define PWM_CHAN3 3 +#define PWM_CHAN4 4 +#define PWM_CHAN5 5 +#define PWM_CHAN6 6 + +/* function prototypes */ + +void pwm_set_rate_and_prescaler(u32 rate,u32 prescaler); +int pwm_config(u8 pwmchan,u8 mode,u32 val1,u32 val2); +int pwm_update_match(u8 pwmchan,u8 mode,u32 val1,u32 val2); +void pwm_match_ctrl_config(u8 matchreg,u8 ctrl); + +#endif -- 2.39.2