Mercurial > ~darius > hgwebdir.cgi > stm32test
comparison main.c @ 8:58d76cf522ff
Split out code into separate files.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Sat, 04 Feb 2012 13:29:31 +1030 |
parents | 9404b9869c27 |
children | 093bc0c3b1cc |
comparison
equal
deleted
inserted
replaced
7:9404b9869c27 | 8:58d76cf522ff |
---|---|
6 #include <string.h> | 6 #include <string.h> |
7 #include <sys/time.h> | 7 #include <sys/time.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 | 9 |
10 #include "stm32f10x.h" | 10 #include "stm32f10x.h" |
11 | |
12 #include "comm.h" | |
13 #include "delay.h" | |
14 #include "flash.h" | |
15 #include "hw.h" | |
11 #include "lcd.h" | 16 #include "lcd.h" |
12 #include "main.h" | 17 #include "main.h" |
13 #include "comm.h" | 18 #include "touch.h" |
14 | 19 |
15 typedef struct { | 20 typedef struct { |
16 char buf[40]; | 21 char buf[40]; |
17 volatile uint8_t state; | 22 volatile uint8_t state; |
18 uint8_t len; | 23 uint8_t len; |
19 } consbuf_t; | 24 } consbuf_t; |
20 | 25 |
21 void Setup_HW(void); | |
22 void NVIC_Configuration(void); | 26 void NVIC_Configuration(void); |
23 | 27 |
24 | 28 |
25 /* Called every 1 / TICK_FREQ */ | 29 /* Called every 1 / TICK_FREQ */ |
26 #define TICK_FREQ 10000 | 30 #define TICK_FREQ 10000 |
103 struct tm nowtm; | 107 struct tm nowtm; |
104 time_t now; | 108 time_t now; |
105 | 109 |
106 cmd.state = cmd.len = 0; | 110 cmd.state = cmd.len = 0; |
107 | 111 |
108 /* Setup USART etc */ | 112 /* Init hardware - configure IO ports and external peripherals */ |
109 Setup_HW(); | 113 hw_init(); |
110 | 114 |
111 /* NVIC configuration */ | 115 /* NVIC configuration */ |
112 NVIC_Configuration(); | 116 NVIC_Configuration(); |
113 | 117 |
114 /* Setup SysTick Timer rate, also enables Systick and Systick-Interrupt */ | 118 /* Setup SysTick Timer rate, also enables Systick and Systick-Interrupt */ |
116 /* Capture error */ | 120 /* Capture error */ |
117 comm_puts("Can't setup SysTick\r\n"); | 121 comm_puts("Can't setup SysTick\r\n"); |
118 while (1) | 122 while (1) |
119 ; | 123 ; |
120 } | 124 } |
121 | |
122 /* Init LCD panel */ | |
123 LCD_init(); | |
124 | 125 |
125 /* Set stdout to unbuffered */ | 126 /* Set stdout to unbuffered */ |
126 setvbuf(stdout, NULL, _IONBF, 0); | 127 setvbuf(stdout, NULL, _IONBF, 0); |
127 | 128 |
128 /* Say hello */ | 129 /* Say hello */ |
146 tv.tv_usec = 0; | 147 tv.tv_usec = 0; |
147 settimeofday(&tv, NULL); | 148 settimeofday(&tv, NULL); |
148 } else if (!strncmp("read", cmd.buf, 4)) { | 149 } else if (!strncmp("read", cmd.buf, 4)) { |
149 printf("PB5 = %d\r\n", GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)); | 150 printf("PB5 = %d\r\n", GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)); |
150 } else if (!strncmp("touch", cmd.buf, 5)) { | 151 } else if (!strncmp("touch", cmd.buf, 5)) { |
152 uint16_t x, y, z1, z2; | |
153 float t, t2; | |
151 for (int i = 0; i < 10; i++) { | 154 for (int i = 0; i < 10; i++) { |
152 uint16_t x, y, z1, z2; | 155 tp_getcoords(&x, &y, &z1, &z2, &t, &t2); |
153 float t, t2; | |
154 | |
155 x = TPRead(TP_READ_SEL(TP_CHAN_X, TP_MODE_12, TP_REF_DIFF, TP_PD_ON)); | |
156 y = TPRead(TP_READ_SEL(TP_CHAN_Y, TP_MODE_12, TP_REF_DIFF, TP_PD_ON)); | |
157 z1 = TPRead(TP_READ_SEL(TP_CHAN_Z1, TP_MODE_12, TP_REF_DIFF, TP_PD_ON)); | |
158 z2 = TPRead(TP_READ_SEL(TP_CHAN_Z2, TP_MODE_12, TP_REF_DIFF, TP_PD_ON)); | |
159 t = ((float)x / 4096.0) * (((float)z2 / (float)z1) - 1); | |
160 t2 = (((float)x / 4096) * ((4096.0 / (float)z1) - 1)) - (1 - ((float)y / (float)4096.0)); | |
161 printf("X = %5d Y = %5d Z1 = %5d Z2 = %5d T = %7.2f T2 = %7.2f\r\n", x, y, z1, z2, t, t2); | 156 printf("X = %5d Y = %5d Z1 = %5d Z2 = %5d T = %7.2f T2 = %7.2f\r\n", x, y, z1, z2, t, t2); |
162 } | 157 } |
163 } else if (!strncmp("fl", cmd.buf, 2)) { | 158 } else if (!strncmp("fl", cmd.buf, 2)) { |
164 uint8_t status; | 159 uint8_t status; |
165 char *flstattbl[] = { | 160 char *flstattbl[] = { |
171 "BP3", | 166 "BP3", |
172 "AAI", | 167 "AAI", |
173 "BPL" | 168 "BPL" |
174 }; | 169 }; |
175 | 170 |
176 printf("Flash ID = 0x%04hx (expect 0xbf41)\r\n", FlashReadID()); | 171 printf("Flash ID = 0x%04hx (expect 0xbf41)\r\n", flashreadid()); |
177 status = FlashReadStatus(); | 172 status = flashreadstatus(); |
178 | 173 |
179 fputs("Status = ", stdout); | 174 fputs("Status = ", stdout); |
180 for (unsigned int i = 0; i < sizeof(flstattbl) / sizeof(flstattbl[0]); i++) | 175 for (unsigned int i = 0; i < sizeof(flstattbl) / sizeof(flstattbl[0]); i++) |
181 if (status & 1 << i) { | 176 if (status & 1 << i) { |
182 fputs(flstattbl[i], stdout); | 177 fputs(flstattbl[i], stdout); |
183 fputs(" ", stdout); | 178 fputs(" ", stdout); |
184 } | 179 } |
185 printf("(0x%02x)\r\n", status); | 180 printf("(0x%02x)\r\n", status); |
186 | 181 |
187 FlashWriteStatus(0x00); | 182 flashwritestatus(0x00); |
188 | 183 |
189 status = FlashReadStatus(); | 184 status = flashreadstatus(); |
190 | 185 |
191 fputs("Status = ", stdout); | 186 fputs("Status = ", stdout); |
192 for (unsigned int i = 0; i < sizeof(flstattbl) / sizeof(flstattbl[0]); i++) | 187 for (unsigned int i = 0; i < sizeof(flstattbl) / sizeof(flstattbl[0]); i++) |
193 if (status & 1 << i) { | 188 if (status & 1 << i) { |
194 fputs(flstattbl[i], stdout); | 189 fputs(flstattbl[i], stdout); |
197 printf("(0x%02x)\r\n", status); | 192 printf("(0x%02x)\r\n", status); |
198 } else if (!strncmp("pwm ", cmd.buf, 4)) { | 193 } else if (!strncmp("pwm ", cmd.buf, 4)) { |
199 lcd_setpwm(atoi(cmd.buf + 4)); | 194 lcd_setpwm(atoi(cmd.buf + 4)); |
200 } else if (!strncmp("timing", cmd.buf, 6)) { | 195 } else if (!strncmp("timing", cmd.buf, 6)) { |
201 fputs("Timing..\r\n", stdout); | 196 fputs("Timing..\r\n", stdout); |
202 Delay(10000); | 197 delay(10000); |
203 fputs("Done\r\n", stdout); | 198 fputs("Done\r\n", stdout); |
199 } else if (!strncmp("circ ", cmd.buf, 5)) { | |
200 uint16_t x, y, r, c; | |
201 char col; | |
202 | |
203 if (sscanf(cmd.buf, "circ %hu %hu %hu %c", &x, &y, &r, &col) != 4) { | |
204 printf("Unable to parse circ args\r\n"); | |
205 goto out; | |
206 } | |
207 | |
208 col = toupper(col); | |
209 if (col == 'R') | |
210 c = LCD_RED; | |
211 else if (col == 'G') | |
212 c = LCD_GREEN; | |
213 else if (col == 'B') | |
214 c = LCD_BLUE; | |
215 else if (col == 'L') | |
216 c = LCD_BLACK; | |
217 else | |
218 c = LCD_WHITE; | |
219 lcd_circle(x, y, r, c); | |
204 } else if (!strncmp("zz", cmd.buf, 2)) { | 220 } else if (!strncmp("zz", cmd.buf, 2)) { |
205 NVIC_SystemReset(); | 221 NVIC_SystemReset(); |
206 } else { | 222 } else { |
207 printf("Unknown command\r\n"); | 223 printf("Unknown command\r\n"); |
208 } | 224 } |
209 } | 225 } |
226 out: | |
210 cmd.state = 0; | 227 cmd.state = 0; |
211 } | 228 } |
212 } | |
213 | |
214 /* Setup hardware (USART etc) */ | |
215 void | |
216 Setup_HW(void) { | |
217 GPIO_InitTypeDef GPIO_InitStructure; | |
218 USART_InitTypeDef USART_InitStructure; | |
219 | |
220 /* RTC stuff */ | |
221 /* Enable PWR and BKP clocks */ | |
222 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); | |
223 | |
224 /* Allow access to BKP Domain */ | |
225 PWR_BackupAccessCmd(ENABLE); | |
226 | |
227 /* Reset Backup Domain | |
228 * | |
229 * This resets the RTC etc back to 0 so probably only useful under user command | |
230 BKP_DeInit(); | |
231 */ | |
232 | |
233 /* Enable Low Speed External clock */ | |
234 RCC_LSEConfig(RCC_LSE_ON); | |
235 | |
236 /* Wait till LSE is ready */ | |
237 while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) | |
238 ; | |
239 | |
240 /* Select LSE as RTC Clock Source */ | |
241 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); | |
242 | |
243 /* Enable RTC Clock */ | |
244 RCC_RTCCLKCmd(ENABLE); | |
245 | |
246 /* Wait for RTC registers synchronization */ | |
247 RTC_WaitForSynchro(); | |
248 | |
249 /* Wait until last write operation on RTC registers has finished */ | |
250 RTC_WaitForLastTask(); | |
251 | |
252 /* Wait until last write operation on RTC registers has finished */ | |
253 RTC_WaitForLastTask(); | |
254 | |
255 /* Set RTC prescaler: set RTC period to 1sec */ | |
256 RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */ | |
257 | |
258 /* Wait until last write operation on RTC registers has finished */ | |
259 RTC_WaitForLastTask(); | |
260 | |
261 /* Clock setup */ | |
262 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); | |
263 | |
264 /* Port configuration */ | |
265 /* Configure USART1 TX (PA.09) as alternate function push-pull */ | |
266 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; | |
267 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; | |
268 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; | |
269 GPIO_Init(GPIOA, &GPIO_InitStructure); | |
270 | |
271 /* Configure USART1 RX (PA.10) as input floating */ | |
272 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; | |
273 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; | |
274 GPIO_Init(GPIOA, &GPIO_InitStructure); | |
275 | |
276 /* Enable GPIOB clock */ | |
277 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); | |
278 | |
279 /* Configure PB5 as output push-pull for LED */ | |
280 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; | |
281 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; | |
282 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; | |
283 GPIO_Init(GPIOB, &GPIO_InitStructure); | |
284 | |
285 /* Configure PB15 as input pull-up push-pull for push button */ | |
286 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; | |
287 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; | |
288 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; | |
289 GPIO_Init(GPIOB, &GPIO_InitStructure); | |
290 | |
291 /* USART configuration */ | |
292 /* USART1 - 115200 8n1, no flow control TX & RX enabled */ | |
293 USART_InitStructure.USART_BaudRate = 115200; | |
294 USART_InitStructure.USART_WordLength = USART_WordLength_8b; | |
295 USART_InitStructure.USART_StopBits = USART_StopBits_1; | |
296 USART_InitStructure.USART_Parity = USART_Parity_No; | |
297 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; | |
298 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; | |
299 USART_Init(USART1, &USART_InitStructure); | |
300 | |
301 /* Enable interrupts on receive data */ | |
302 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); | |
303 | |
304 /* Enable USART */ | |
305 USART_Cmd(USART1, ENABLE); | |
306 } | 229 } |
307 | 230 |
308 /* Configure interrupt controller */ | 231 /* Configure interrupt controller */ |
309 #ifdef VECT_TAB_RAM | 232 #ifdef VECT_TAB_RAM |
310 /* vector-offset (TBLOFF) from bottom of SRAM. defined in linker script */ | 233 /* vector-offset (TBLOFF) from bottom of SRAM. defined in linker script */ |