Rename tools to ao-<foo>
[fw/altos] / ao-tools / target / beep-timer / beep_timer.c
1 /*
2  * Copyright © 2008 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 sfr at 0x80 P0;
20 sfr at 0x90 P1;
21 sfr at 0xA0 P2;
22 sfr at 0xC6 CLKCON;
23 sfr at 0xbe SLEEP;
24
25 # define SLEEP_USB_EN           (1 << 7)
26 # define SLEEP_XOSC_STB         (1 << 6)
27
28 sbit at 0x90 P1_0;
29 sbit at 0x91 P1_1;
30 sbit at 0x92 P1_2;
31 sbit at 0x93 P1_3;
32 sbit at 0x94 P1_4;
33 sbit at 0x95 P1_5;
34 sbit at 0x96 P1_6;
35 sbit at 0x97 P1_7;
36
37 sfr at 0xF1 PERCFG;
38 sfr at 0xF2 ADCCFG;
39 sfr at 0xF3 P0SEL;
40 sfr at 0xF4 P1SEL;
41 sfr at 0xF5 P2SEL;
42
43 #define P2SEL_PRI3P1_USART0             (0 << 6)
44 #define P2SEL_PRI3P1_USART1             (1 << 6)
45 #define P2SEL_PRI2P1_USART1             (0 << 5)
46 #define P2SEL_PRI2P1_TIMER3             (1 << 5)
47 #define P2SEL_PRI1P1_TIMER1             (0 << 4)
48 #define P2SEL_PRI1P1_TIMER4             (1 << 4)
49 #define P2SEL_PRI0P1_USART0             (0 << 3)
50 #define P2SEL_PRI0P1_TIMER1             (1 << 3)
51 #define P2SEL_SELP2_4_GPIO              (0 << 2)
52 #define P2SEL_SELP2_4_PERIPHERAL        (1 << 2)
53 #define P2SEL_SELP2_3_GPIO              (0 << 1)
54 #define P2SEL_SELP2_3_PERIPHERAL        (1 << 1)
55 #define P2SEL_SELP2_0_GPIO              (0 << 0)
56 #define P2SEL_SELP2_0_PERIPHERAL        (1 << 0)
57
58 sfr at 0xFD P0DIR;
59 sfr at 0xFE P1DIR;
60 sfr at 0xFF P2DIR;
61 sfr at 0x8F P0INP;
62 sfr at 0xF6 P1INP;
63 sfr at 0xF7 P2INP;
64
65 sfr at 0x89 P0IFG;
66 sfr at 0x8A P1IFG;
67 sfr at 0x8B P2IFG;
68
69 sfr at 0xF1 PERCFG;
70 #define PERCFG_T1CFG_ALT_1      (0 << 6)
71 #define PERCFG_T1CFG_ALT_2      (1 << 6)
72
73 #define PERCFG_T3CFG_ALT_1      (0 << 5)
74 #define PERCFG_T3CFG_ALT_2      (1 << 5)
75
76 #define PERCFG_T4CFG_ALT_1      (0 << 4)
77 #define PERCFG_T4CFG_ALT_2      (1 << 4)
78
79 #define PERCFG_U1CFG_ALT_1      (0 << 1)
80 #define PERCFG_U1CFG_ALT_2      (1 << 1)
81
82 #define PERCFG_U0CFG_ALT_1      (0 << 0)
83 #define PERCFG_U0CFG_ALT_2      (1 << 0)
84
85 /* Timer count */
86 sfr at 0xCA T3CNT;
87 sfr at 0xEA T4CNT;
88
89 /* Timer control */
90
91 sfr at 0xCB T3CTL;
92 sfr at 0xEB T4CTL;
93
94 #define TxCTL_DIV_1             (0 << 5)
95 #define TxCTL_DIV_2             (1 << 5)
96 #define TxCTL_DIV_4             (2 << 5)
97 #define TxCTL_DIV_8             (3 << 5)
98 #define TxCTL_DIV_16            (4 << 5)
99 #define TxCTL_DIV_32            (5 << 5)
100 #define TxCTL_DIV_64            (6 << 5)
101 #define TxCTL_DIV_128           (7 << 5)
102 #define TxCTL_START             (1 << 4)
103 #define TxCTL_OVFIM             (1 << 3)
104 #define TxCTL_CLR               (1 << 2)
105 #define TxCTL_MODE_FREE         (0 << 0)
106 #define TxCTL_MODE_DOWN         (1 << 0)
107 #define TxCTL_MODE_MODULO       (2 << 0)
108 #define TxCTL_MODE_UP_DOWN      (3 << 0)
109
110 /* Timer 4 channel 0 compare control */
111
112 sfr at 0xCC T3CCTL0;
113 sfr at 0xCE T3CCTL1;
114 sfr at 0xEC T4CCTL0;
115 sfr at 0xEE T4CCTL1;
116
117 #define TxCCTLy_IM                      (1 << 6)
118 #define TxCCTLy_CMP_SET                 (0 << 3)
119 #define TxCCTLy_CMP_CLEAR               (1 << 3)
120 #define TxCCTLy_CMP_TOGGLE              (2 << 3)
121 #define TxCCTLy_CMP_SET_UP_CLEAR_DOWN   (3 << 3)
122 #define TxCCTLy_CMP_CLEAR_UP_SET_DOWN   (4 << 3)
123 #define TxCCTLy_CMP_SET_CLEAR_FF        (5 << 3)
124 #define TxCCTLy_CMP_CLEAR_SET_00        (6 << 3)
125 #define TxCCTLy_CMP_MODE_ENABLE         (1 << 2)
126
127 /* Timer compare value */
128 sfr at 0xCD T3CC0;
129 sfr at 0xCF T3CC1;
130 sfr at 0xED T4CC0;
131 sfr at 0xEF T4CC1;
132
133 #define nop()   _asm nop _endasm;
134
135 void
136 delay (unsigned char n)
137 {
138         unsigned char i = 0, j = 0;
139
140         n <<= 1;
141         while (--n != 0)
142                 while (--j != 0)
143                         while (--i != 0)
144                                 nop();
145 }
146
147 void
148 dit() {
149         T4CTL |= TxCTL_START;
150         delay(1);
151         T4CTL &= ~TxCTL_START;
152         delay(1);
153 }
154
155 void
156 dah () {
157         T4CTL |= TxCTL_START;
158         delay(3);
159         T4CTL &= ~TxCTL_START;
160         delay(1);
161 }
162
163 void
164 charspace () {
165         delay(2);
166 }
167
168 void
169 wordspace () {
170         delay(8);
171 }
172
173 #define _ dit();
174 #define ___ dah();
175 #define C charspace();
176 #define W wordspace();
177
178 main ()
179 {
180         CLKCON = 0;
181         while (!(SLEEP & SLEEP_XOSC_STB))
182                 ;
183
184         /* Use timer 4 alternate config 2 */
185         PERCFG = PERCFG_T4CFG_ALT_2;
186         /* Use P2_4 for timer 4 output */
187         P2SEL = P2SEL_SELP2_0_PERIPHERAL;
188
189         T4CCTL0 = TxCCTLy_CMP_TOGGLE|TxCCTLy_CMP_MODE_ENABLE;
190         T4CC0 = 125;
191         T4CTL = TxCTL_DIV_32 | TxCTL_MODE_MODULO;
192
193         for (;;) {
194                 ___ _ ___ _ C ___ ___ _ ___ W   /* cq */
195                 ___ _ _ C _ W                   /* de */
196                 ___ _ ___ C ___ _ _ C           /* kd */
197                 ___ ___ _ _ _ C _ _ _ C         /* 7s */
198                 ___ ___ _ ___ C ___ ___ _ W     /* qg */
199                 if (T4CC0 == 94)
200                         T4CC0 = 125;
201                 else
202                         T4CC0 = 94;
203         }
204 }