1b9813fe70f77d5a8591d250992bdf91dbdc1c12
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19 #include <ao_exti.h>
20 #include <ao_quadrature.h>
21
22 struct ao_task demo_task;
23
24 static inline int min(int a, int b) { return a < b ? a : b; }
25
26 void
27 ao_demo(void)
28 {
29         char    message[] = "Hello, Mike & Bdale --- ";
30         char    part[7];
31         int     i = 0;
32         int     len = sizeof(message) - 1;
33         int     first, second;
34
35         part[6] = '\0';
36         for (;;) {
37                 ao_delay(AO_MS_TO_TICKS(150));
38                 first = min(6, len - i);
39                 second = 6 - first;
40                 memcpy(part, message + i, first);
41                 memcpy(part + first, message, second);
42                 ao_lcd_font_string(part);
43                 if (++i >= len)
44                         i = 0;
45         }
46 }
47
48 void _close() { }
49 void _sbrk() { }
50 void _isatty() { }
51 void _lseek() { }
52 void _exit () { }
53 void _read () { }
54 void _fstat() { }
55
56 #define AO_DMA_TEST_INDEX       STM_DMA_INDEX(4)
57
58 static void
59 ao_dma_test(void) {
60         static char     src[20] = "hello, world";
61         static char     dst[20];
62         
63         dst[0] = '\0';
64         ao_dma_set_transfer(AO_DMA_TEST_INDEX, dst, src, 13,
65                             (1 << STM_DMA_CCR_MEM2MEM) |
66                             (STM_DMA_CCR_PL_LOW << STM_DMA_CCR_PL) |
67                             (STM_DMA_CCR_MSIZE_8 << STM_DMA_CCR_MSIZE) |
68                             (STM_DMA_CCR_PSIZE_8 << STM_DMA_CCR_PSIZE) |
69                             (1 << STM_DMA_CCR_MINC) |
70                             (1 << STM_DMA_CCR_PINC) |
71                             (0 << STM_DMA_CCR_CIRC) |
72                             (STM_DMA_CCR_DIR_MEM_TO_PER << STM_DMA_CCR_DIR));
73         ao_dma_start(AO_DMA_TEST_INDEX);
74         ao_arch_critical(
75                 while (!ao_dma_done[AO_DMA_TEST_INDEX])
76                         ao_sleep(&ao_dma_done[AO_DMA_TEST_INDEX]);
77                 );
78         ao_dma_done_transfer(AO_DMA_TEST_INDEX);
79         printf ("copied %s\n", dst);
80 }
81
82 static void
83 ao_spi_write(void) {
84         unsigned char   data[] = { 0x55, 0xaa, 0xff, 0x00 };
85         int i;
86
87         for (i = 0; i < 10; i++) {
88                 ao_spi_get(0, AO_SPI_SPEED_FAST);
89                 stm_gpio_set(&stm_gpioc, 12, 0);
90                 ao_spi_send(data, 4, 0);
91                 stm_gpio_set(&stm_gpioc, 12, 1);
92                 ao_spi_put(0);
93                 printf(".");
94                 flush();
95                 ao_delay(100);
96         }
97 }
98
99 static void
100 ao_spi_read(void) {
101         unsigned char   data[4];
102         int i;
103
104         for (i = 0; i < 10; i++) {
105                 ao_spi_get(0, AO_SPI_SPEED_FAST);
106                 stm_gpio_set(&stm_gpioc, 12, 0);
107                 ao_spi_recv(data, 4, 0);
108                 stm_gpio_set(&stm_gpioc, 12, 1);
109                 ao_spi_put(0);
110                 printf(".");
111                 flush();
112                 ao_delay(100);
113         }
114 }
115
116 static void
117 ao_i2c_write(void) {
118         unsigned char   data[] = { 0x55, 0xaa, 0xff, 0x00 };
119         int i;
120
121         for (i = 0; i < 10; i++) {
122                 ao_i2c_get(0);
123                 if (ao_i2c_start(0, 0x55))
124                         ao_i2c_send(data, 4, 0, TRUE);
125                 else {
126                         printf ("i2c start failed\n");
127                         ao_i2c_put(0);
128                         break;
129                 }
130                 ao_i2c_put(0);
131                 printf(".");
132                 flush();
133                 ao_delay(100);
134         }
135 }
136
137 static void
138 ao_temp (void)
139 {
140         struct ao_data  packet;
141         int temp;
142
143         ao_data_get(&packet);
144
145         /*
146          * r = (110 - 25) / (ts_cal_hot - ts_cal_cold)
147          * 25 + (110 - 25) * (temp - ts_cal_cold) / (ts_cal_hot - ts_cal_cold)
148          */
149         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);
150         printf ("temp: %d\n", temp);
151 }
152
153 __code struct ao_cmds ao_demo_cmds[] = {
154         { ao_dma_test,  "D\0DMA test" },
155         { ao_spi_write, "W\0SPI write" },
156         { ao_spi_read, "R\0SPI read" },
157         { ao_i2c_write, "i\0I2C write" },
158         { ao_temp, "t\0Show temp" },
159         { 0, NULL }
160 };
161
162 int
163 main(void)
164 {
165         ao_clock_init();
166         
167         ao_serial_init();
168         ao_timer_init();
169         ao_dma_init();
170         ao_cmd_init();
171         ao_lcd_stm_init();
172 //      ao_lcd_font_init();
173         ao_spi_init();
174         ao_i2c_init();
175         ao_exti_init();
176         ao_quadrature_init();
177
178         ao_timer_set_adc_interval(100);
179
180         ao_adc_init();
181         ao_usb_init();
182
183         ao_cmd_register(&ao_demo_cmds[0]);
184         
185         ao_start_scheduler();
186         return 0;
187 }