altos/stm-demo: Make this build again after various API and CFLAGS changes
[fw/altos] / src / stm-demo / ao_demo.c
1 /*
2  * Copyright © 2011 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 #include "ao.h"
20 #include <ao_exti.h>
21 #include <ao_event.h>
22 #include <ao_quadrature.h>
23 #include <ao_button.h>
24 #include <ao_boot.h>
25
26 struct ao_task demo_task;
27
28 static inline int min(int a, int b) { return a < b ? a : b; }
29
30
31 #define AO_DMA_TEST_INDEX       STM_DMA_INDEX(4)
32
33 static void
34 ao_dma_test(void) {
35         static char     src[20] = "hello, world";
36         static char     dst[20];
37         
38         dst[0] = '\0';
39         ao_dma_set_transfer(AO_DMA_TEST_INDEX, dst, src, 13,
40                             (1 << STM_DMA_CCR_MEM2MEM) |
41                             (STM_DMA_CCR_PL_LOW << STM_DMA_CCR_PL) |
42                             (STM_DMA_CCR_MSIZE_8 << STM_DMA_CCR_MSIZE) |
43                             (STM_DMA_CCR_PSIZE_8 << STM_DMA_CCR_PSIZE) |
44                             (1 << STM_DMA_CCR_MINC) |
45                             (1 << STM_DMA_CCR_PINC) |
46                             (0 << STM_DMA_CCR_CIRC) |
47                             (STM_DMA_CCR_DIR_MEM_TO_PER << STM_DMA_CCR_DIR));
48         ao_dma_start(AO_DMA_TEST_INDEX);
49         ao_arch_critical(
50                 while (!ao_dma_done[AO_DMA_TEST_INDEX])
51                         ao_sleep(&ao_dma_done[AO_DMA_TEST_INDEX]);
52                 );
53         ao_dma_done_transfer(AO_DMA_TEST_INDEX);
54         printf ("copied %s\n", dst);
55 }
56
57 static void
58 ao_spi_write(void) {
59         unsigned char   data[] = { 0x55, 0xaa, 0xff, 0x00 };
60         int i;
61
62         for (i = 0; i < 10; i++) {
63                 ao_spi_get(0, AO_SPI_SPEED_FAST);
64                 stm_gpio_set(&stm_gpioc, 12, 0);
65                 ao_spi_send(data, 4, 0);
66                 stm_gpio_set(&stm_gpioc, 12, 1);
67                 ao_spi_put(0);
68                 printf(".");
69                 flush();
70                 ao_delay(100);
71         }
72 }
73
74 static void
75 ao_spi_read(void) {
76         unsigned char   data[4];
77         int i;
78
79         for (i = 0; i < 10; i++) {
80                 ao_spi_get(0, AO_SPI_SPEED_FAST);
81                 stm_gpio_set(&stm_gpioc, 12, 0);
82                 ao_spi_recv(data, 4, 0);
83                 stm_gpio_set(&stm_gpioc, 12, 1);
84                 ao_spi_put(0);
85                 printf(".");
86                 flush();
87                 ao_delay(100);
88         }
89 }
90
91 static void
92 ao_i2c_write(void) {
93         unsigned char   data[] = { 0x55, 0xaa, 0xff, 0x00 };
94         int i;
95
96         for (i = 0; i < 10; i++) {
97                 ao_i2c_get(0);
98                 if (ao_i2c_start(0, 0x55))
99                         ao_i2c_send(data, 4, 0, true);
100                 else {
101                         printf ("i2c start failed\n");
102                         ao_i2c_put(0);
103                         break;
104                 }
105                 ao_i2c_put(0);
106                 printf(".");
107                 flush();
108                 ao_delay(100);
109         }
110 }
111
112 static void
113 ao_temp (void)
114 {
115         struct ao_data  packet;
116         int temp;
117
118         ao_data_get(&packet);
119
120         /*
121          * r = (110 - 25) / (ts_cal_hot - ts_cal_cold)
122          * 25 + (110 - 25) * (temp - ts_cal_cold) / (ts_cal_hot - ts_cal_cold)
123          */
124         temp = 25 + (110 - 25) * (packet.adc.temp - stm_temp_cal.ts_cal_cold) / (stm_temp_cal.ts_cal_hot - stm_temp_cal.ts_cal_cold);
125         printf ("temp: %d\n", temp);
126 }
127
128 #if 0
129 static void
130 ao_event(void)
131 {
132         struct ao_event event;
133
134         for (;;) {
135                 flush();
136                 ao_event_get(&event);
137                 printf ("type %1d unit %1d tick %5u value %ld\n",
138                         event.type, event.unit, event.tick, event.value);
139                 if (event.value == 100)
140                         break;
141         }
142
143 }
144 #endif
145
146 static uint8_t ao_blinking = 0;
147
148 static void
149 ao_blink(void)
150 {
151         for (;;) {
152                 while (!ao_blinking)
153                         ao_sleep(&ao_blinking);
154                 while (ao_blinking) {
155                         ao_led_toggle(AO_LED_BLUE|AO_LED_GREEN);
156                         ao_delay(AO_MS_TO_TICKS(500));
157                 }
158         }
159 }
160
161 static struct ao_task ao_blink_task;
162
163 static void
164 ao_blink_toggle(void)
165 {
166         ao_blinking = !ao_blinking;
167         ao_wakeup(&ao_blinking);
168 }
169
170
171 const struct ao_cmds ao_demo_cmds[] = {
172         { ao_dma_test,  "D\0DMA test" },
173         { ao_spi_write, "W\0SPI write" },
174         { ao_spi_read, "R\0SPI read" },
175         { ao_i2c_write, "i\0I2C write" },
176         { ao_temp, "t\0Show temp" },
177         { ao_blink_toggle, "b\0Toggle LED blinking" },
178 /*      { ao_event, "e\0Monitor event queue" }, */
179         { 0, NULL }
180 };
181
182 int
183 main(void)
184 {
185         ao_clock_init();
186         
187         ao_task_init();
188
189         ao_led_init();
190         ao_led_on(AO_LED_GREEN);
191         ao_led_off(AO_LED_BLUE);
192         ao_timer_init();
193         ao_dma_init();
194         ao_cmd_init();
195 //      ao_lcd_stm_init();
196 //      ao_lcd_font_init();
197         ao_spi_init();
198 //      ao_i2c_init();
199 //      ao_exti_init();
200 //      ao_quadrature_init();
201 //      ao_button_init();
202         ao_storage_init();
203
204 //      ao_timer_set_adc_interval(100);
205
206 //      ao_adc_init();
207         ao_usb_init();
208
209         ao_add_task(&ao_blink_task, ao_blink, "blink");
210         ao_cmd_register(&ao_demo_cmds[0]);
211         
212         ao_start_scheduler();
213         return 0;
214 }