diff options
| author | Keith Packard <keithp@keithp.com> | 2009-04-12 23:53:55 -0700 | 
|---|---|---|
| committer | Keith Packard <keithp@keithp.com> | 2009-04-12 23:53:55 -0700 | 
| commit | f3f25a1cec7d2a034aa544569cfd23bea1a996c5 (patch) | |
| tree | 4b39836787404bdd331f9192c5256fc8031f67cf | |
| parent | e14f07bfdb8824fc7ed6df1129c66ee39ffd6d54 (diff) | |
Add beep/led support.
Support our P2_0 connected buzzer, and formalize LED output support.
Signed-off-by: Keith Packard <keithp@keithp.com>
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | Makefile | 13 | ||||
| -rw-r--r-- | ao.h | 44 | ||||
| -rw-r--r-- | ao_adc.c | 4 | ||||
| -rw-r--r-- | ao_beep.c | 53 | ||||
| -rw-r--r-- | ao_led.c | 54 | ||||
| -rw-r--r-- | ao_task.c | 5 | ||||
| -rw-r--r-- | ao_test.c | 34 | 
8 files changed, 187 insertions, 23 deletions
@@ -9,5 +9,4 @@  *.lnk  *.map  *.mem -altos-flash -altos-ram +altos @@ -21,6 +21,8 @@ INC = \  SRC = \  	ao_adc.c \ +	ao_beep.c \ +	ao_led.c \  	ao_task.c \  	ao_timer.c \  	ao_panic.c \ @@ -35,7 +37,7 @@ REL=$(SRC:.c=.rel)  RST=$(SRC:.c=.rst)  SYM=$(SRC:.c=.sym) -PROGS=$(PROG)-flash.ihx $(PROG)-ram.ihx +PROGS=$(PROG).ihx  PCDB=$(PROGS:.ihx=.cdb)  PLNK=$(PROGS:.ihx=.lnk)  PMAP=$(PROGS:.ihx=.map) @@ -47,12 +49,9 @@ PAOM=$(PROGS:.ihx=)  all: $(PROGS) -$(PROG)-ram.ihx: $(REL) Makefile -	$(CC) $(LDFLAGS_RAM) $(CFLAGS) -o $(PROG)-ram.ihx $(REL) -	$(CC) $(LDFLAGS_FLASH) $(CFLAGS) -o $(PROG)-flash.ihx $(REL) -	sh check-stack ao.h $(PROG)-flash.mem - -$(PROG)-flash.ihx: $(PROG)-ram.ihx +$(PROG).ihx: $(REL) Makefile +	$(CC) $(LDFLAGS_FLASH) $(CFLAGS) -o $(PROG).ihx $(REL) +	sh check-stack ao.h $(PROG).mem  clean:  	rm -f $(ADB) $(ASM) $(LNK) $(LST) $(REL) $(RST) $(SYM) @@ -54,6 +54,9 @@ void ao_panic(uint8_t reason);  volatile __data uint16_t ao_time; +#define AO_MS_TO_TICKS(ms)	((ms) / 10) +#define AO_SEC_TO_TICKS(s)	((s) * 100) +  void ao_timer_isr(void) interrupt 9;  void ao_timer_init(void);  uint16_t ao_time_atomic(void); @@ -73,12 +76,49 @@ struct ao_adc {  	int16_t		sense_m;  }; -extern __xdata struct ao_adc	ao_adc_ring[ADC_RING]; -extern __data uint8_t		ao_adc_head; +extern volatile __xdata struct ao_adc	ao_adc_ring[ADC_RING]; +extern volatile __data uint8_t		ao_adc_head;  void ao_adc_isr(void) interrupt 1;  void ao_adc_init(void);  void ao_adc_poll(void);  void ao_adc_get(__xdata struct ao_adc *packet); +/* ao_beep.c */ + +#define AO_BEEP_LOW	150 +#define AO_BEEP_MID	94 +#define AO_BEEP_HIGH	75 +#define AO_BEEP_OFF	0 + +void +ao_beep_init(void); + +void +ao_beep(uint8_t beep); + +void +ao_beep_for(uint8_t beep, uint16_t ticks); + +/* ao_led.c */ + +#define AO_LED_NONE	0 +#define AO_LED_GREEN	1 +#define AO_LED_RED	2 + +void +ao_led_init(void); + +void +ao_led_on(uint8_t colors); + +void +ao_led_off(uint8_t colors); + +void +ao_led_set(uint8_t colors); + +void +ao_led_for(uint8_t colors, uint16_t ticks); +  #endif /* _AO_H_ */ @@ -18,8 +18,8 @@  #include "ao.h" -__xdata struct ao_adc	ao_adc_ring[ADC_RING]; -__data uint8_t		ao_adc_head; +volatile __xdata struct ao_adc	ao_adc_ring[ADC_RING]; +volatile __data uint8_t		ao_adc_head;  void ao_adc_isr(void) interrupt 1  { diff --git a/ao_beep.c b/ao_beep.c new file mode 100644 index 00000000..ae5e36a6 --- /dev/null +++ b/ao_beep.c @@ -0,0 +1,53 @@ +/* + * Copyright © 2009 Keith Packard <keithp@keithp.com> + * + * 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 "ao.h" + +void +ao_beep_init(void) +{ +	/* Our beeper is on P2_0, which is hooked to timer 4 using +	 * configuration alternative 2 +	 */ +	P2_0 = 0; +	P2SEL = (P2SEL & ~P2SEL_SELP2_0_MASK) | P2SEL_SELP2_0_GPIO; +	PERCFG = (PERCFG & ~PERCFG_T4CFG_ALT_MASK) | PERCFG_T4CFG_ALT_2; +	T4CCTL0 = TxCCTLy_CMP_TOGGLE|TxCCTLy_CMP_MODE_ENABLE; +} + +void +ao_beep(uint8_t beep) +{ +	if (beep == 0) { +		P2_0 = 0; +		P2SEL = (P2SEL & ~P2SEL_SELP2_0_MASK) | P2SEL_SELP2_0_GPIO; +		T4CTL = 0; +	} else { +		P2SEL = (P2SEL & ~P2SEL_SELP2_0_MASK) | P2SEL_SELP2_0_PERIPHERAL; +		T4CC0 = beep; +		T4CTL = TxCTL_DIV_32 | TxCTL_MODE_MODULO | TxCTL_START; +	} +} + +void +ao_beep_for(uint8_t beep, uint16_t ticks) +{ +	ao_beep(beep); +	ao_delay(ticks); +	ao_beep(0); +} diff --git a/ao_led.c b/ao_led.c new file mode 100644 index 00000000..c46e2eba --- /dev/null +++ b/ao_led.c @@ -0,0 +1,54 @@ +/* + * Copyright © 2009 Keith Packard <keithp@keithp.com> + * + * 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 "ao.h" + +void +ao_led_init(void) +{ +	P1SEL &= ~3; +	P1 &= ~3; +	P1DIR |= 3; +} + +void +ao_led_on(uint8_t colors) +{ +	P1 |= colors; +} + +void +ao_led_off(uint8_t colors) +{ +	P1 &= ~colors; +} + +void +ao_led_set(uint8_t colors) +{ +	P1 = (P1 & ~3) | colors; +} + +void +ao_led_for(uint8_t colors, uint16_t ticks) +{ +	ao_led_on(colors); +	ao_delay(ticks); +	ao_led_off(colors); +} + @@ -179,6 +179,11 @@ ao_wakeup(__xdata void *wchan)  void  ao_start_scheduler(void)  { +	ao_timer_init(); +	ao_adc_init(); +	ao_beep_init(); +	ao_led_init(); +  	ao_cur_task_id = AO_NO_TASK;  	ao_cur_task = NULL;  	ao_yield(); @@ -20,6 +20,7 @@  struct ao_task __xdata blink_0_task;  struct ao_task __xdata blink_1_task;  struct ao_task __xdata wakeup_task; +struct ao_task __xdata beep_task;  void delay(int n) __reentrant  { @@ -34,8 +35,13 @@ static __xdata uint8_t blink_chan;  void  blink_0(void)  { +	uint8_t	b = 0;  	for (;;) { -		P1 ^= 1; +		b = 1 - b; +		if (b) +			ao_led_on(AO_LED_GREEN); +		else +			ao_led_off(AO_LED_GREEN);  		ao_sleep(&blink_chan);  	}  } @@ -49,9 +55,9 @@ blink_1(void)  		ao_sleep(&ao_adc_ring);  		ao_adc_get(&adc);  		if (adc.accel < 15900) -			P1_1 = 1; +			ao_led_on(AO_LED_RED);  		else -			P1_1 = 0; +			ao_led_off(AO_LED_RED);  	}  } @@ -59,26 +65,34 @@ void  wakeup(void)  {  	for (;;) { -		ao_delay(10); +		ao_delay(AO_MS_TO_TICKS(100));  		ao_wakeup(&blink_chan);  	}  }  void +beep(void) +{ +	static struct ao_adc adc; + +	for (;;) { +		ao_delay(AO_SEC_TO_TICKS(1)); +		ao_adc_get(&adc); +		if (adc.temp > 7400) +			ao_beep_for(AO_BEEP_LOW, AO_MS_TO_TICKS(50)); +	} +} + +void  main(void)  {  	CLKCON = 0;  	while (!(SLEEP & SLEEP_XOSC_STB))  		; -	/* Set p1_1 and p1_0 to output */ -	P1DIR = 0x03; -	 -	ao_adc_init(); -	ao_timer_init(); -  	ao_add_task(&blink_0_task, blink_0);  	ao_add_task(&blink_1_task, blink_1);  	ao_add_task(&wakeup_task, wakeup); +	ao_add_task(&beep_task, beep);  	ao_start_scheduler();  }  | 
