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