Mercurial > ~darius > hgwebdir.cgi > stm32temp
comparison main.c @ 3:74e9b3baac1e
Jumbo commit to make things work.
Note I have t
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Sun, 01 Jan 2012 11:01:13 +1030 |
parents | 274e01fa5a4c |
children | 2c87e30c982d |
comparison
equal
deleted
inserted
replaced
2:274e01fa5a4c | 3:74e9b3baac1e |
---|---|
1 #include <ctype.h> | 1 #include <ctype.h> |
2 #include <malloc.h> | |
2 #include <stdio.h> | 3 #include <stdio.h> |
3 #include <stdint.h> | 4 #include <stdint.h> |
5 #include <time.h> | |
6 #include <string.h> | |
7 #include <sys/time.h> | |
8 #include <stdlib.h> | |
9 | |
4 #include "stm32f10x.h" | 10 #include "stm32f10x.h" |
11 #include "lcd.h" | |
5 #include "main.h" | 12 #include "main.h" |
6 #include "comm.h" | 13 #include "comm.h" |
7 | 14 |
8 typedef volatile struct { | 15 typedef struct { |
9 char buf[40]; | 16 char buf[40]; |
10 uint8_t state; | 17 volatile uint8_t state; |
11 uint8_t len; | 18 uint8_t len; |
12 } consbuf_t; | 19 } consbuf_t; |
13 | 20 |
14 void Setup_HW(void); | 21 void Setup_HW(void); |
15 void NVIC_Configuration(void); | 22 void NVIC_Configuration(void); |
16 | 23 |
17 /* Called every millisecond */ | 24 |
25 /* Called every 1 / TICK_FREQ */ | |
26 #define TICK_FREQ 10000 | |
18 RAMFUNC void | 27 RAMFUNC void |
19 SysTick_Handler(void) { | 28 SysTick_Handler(void) { |
20 static uint32_t tick = 0; | 29 static uint32_t tick = 0; |
21 | 30 static int led = 0; |
31 | |
22 tick++; | 32 tick++; |
33 if (tick % 10000 == 0) { | |
34 led = !led; | |
35 if (led) | |
36 GPIO_SetBits(GPIOB, GPIO_Pin_5); | |
37 else | |
38 GPIO_ResetBits(GPIOB, GPIO_Pin_5); | |
39 } | |
40 | |
23 } | 41 } |
24 | 42 |
25 consbuf_t cmd; | 43 consbuf_t cmd; |
26 | 44 |
27 RAMFUNC void | 45 RAMFUNC void |
79 } | 97 } |
80 | 98 |
81 | 99 |
82 int | 100 int |
83 main(void) { | 101 main(void) { |
102 char buf[40]; | |
103 struct tm nowtm; | |
104 time_t now; | |
105 | |
84 cmd.state = cmd.len = 0; | 106 cmd.state = cmd.len = 0; |
85 | 107 |
86 /* Setup USART etc */ | 108 /* Setup USART etc */ |
87 Setup_HW(); | 109 Setup_HW(); |
88 | 110 |
89 /* NVIC configuration */ | 111 /* NVIC configuration */ |
90 NVIC_Configuration(); | 112 NVIC_Configuration(); |
91 | 113 |
92 /* Setup SysTick Timer for 1 millisecond interrupts, also enables Systick and Systick-Interrupt */ | 114 /* Setup SysTick Timer rate, also enables Systick and Systick-Interrupt */ |
93 if (SysTick_Config(SystemCoreClock / 1000)) { | 115 if (SysTick_Config(SystemCoreClock / TICK_FREQ)) { |
94 /* Capture error */ | 116 /* Capture error */ |
95 comm_puts("Can't setup SysTick\r\n"); | 117 comm_puts("Can't setup SysTick\r\n"); |
96 while (1) | 118 while (1) |
97 ; | 119 ; |
98 } | 120 } |
99 | 121 |
122 /* Init LCD interface */ | |
123 LCD_init(); | |
124 | |
125 /* Set stdout to unbuffered */ | |
100 setvbuf(stdout, NULL, _IONBF, 0); | 126 setvbuf(stdout, NULL, _IONBF, 0); |
101 | 127 |
102 /* Say hello */ | 128 /* Say hello */ |
103 fputs("\r\nHello world\r\n", stdout); | 129 fputs("\r\nHello world\r\n", stdout); |
104 | 130 |
131 #define Bank1_LCD_D ((uint32_t)0x60020000) //disp Data ADDR | |
132 #define Bank1_LCD_C ((uint32_t)0x60000000) //disp Reg ADDR | |
133 | |
105 while (1) { | 134 while (1) { |
106 fputs("> ", stdout); | 135 fputs("> ", stdout); |
107 | 136 |
108 while (cmd.state != 255) | 137 while (cmd.state != 255) |
109 ; | 138 ; |
110 | 139 |
111 if (cmd.len > 0) | 140 if (cmd.len > 0) { |
112 printf("Got command '%s'\r\n", cmd.buf); | 141 if (!strncmp("gc", cmd.buf, 2)) { |
142 now = time(NULL); | |
143 gmtime_r(&now, &nowtm); | |
144 strftime(buf, sizeof(buf) - 1, "Time is %Y/%m/%d %H:%M:%S UTC", &nowtm); | |
145 printf("Time is %s (%d)\r\n", buf, (int)now); | |
146 } else if (!strncmp("sc ", cmd.buf, 3)) { | |
147 struct timeval tv; | |
148 tv.tv_sec = atoi(cmd.buf + 3); | |
149 tv.tv_usec = 0; | |
150 settimeofday(&tv, NULL); | |
151 } else if (!strncmp("lcd", cmd.buf, 3)) { | |
152 *(__IO uint16_t *) (Bank1_LCD_C) = 0x00; | |
153 printf("LCD ID = %hx\r\n", *(__IO uint16_t *) (Bank1_LCD_D)); | |
154 } else if (!strncmp("read", cmd.buf, 4)) { | |
155 printf("PB5 = %d\r\n", GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)); | |
156 } else if (!strncmp("zz", cmd.buf, 2)) { | |
157 NVIC_SystemReset(); | |
158 } else { | |
159 printf("Unknown command\r\n"); | |
160 } | |
161 } | |
113 cmd.state = 0; | 162 cmd.state = 0; |
114 } | 163 } |
115 } | 164 } |
116 | 165 |
117 /* Setup hardware (USART etc) */ | 166 /* Setup hardware (USART etc) */ |
118 void | 167 void |
119 Setup_HW(void) { | 168 Setup_HW(void) { |
120 GPIO_InitTypeDef GPIO_InitStructure; | 169 GPIO_InitTypeDef GPIO_InitStructure; |
121 USART_InitTypeDef USART_InitStructure; | 170 USART_InitTypeDef USART_InitStructure; |
122 | 171 |
123 /* Enable USART1, GPIOA, GPIOD and AFIO clocks */ | 172 /* RTC stuff */ |
124 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOD | 173 /* Enable PWR and BKP clocks */ |
125 | RCC_APB2Periph_AFIO, ENABLE); | 174 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); |
126 /* Enable USART2 clock */ | 175 |
127 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); | 176 /* Allow access to BKP Domain */ |
128 | 177 PWR_BackupAccessCmd(ENABLE); |
129 /* DMA1 clock enable */ | 178 |
130 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); | 179 /* Reset Backup Domain |
131 | 180 * |
181 * This resets the RTC etc back to 0 so probably only useful under user command | |
182 BKP_DeInit(); | |
183 */ | |
184 | |
185 /* Enable LSE */ | |
186 RCC_LSEConfig(RCC_LSE_ON); | |
187 | |
188 /* Wait till LSE is ready */ | |
189 while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) | |
190 ; | |
191 | |
192 /* Select LSE as RTC Clock Source */ | |
193 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); | |
194 | |
195 /* Enable RTC Clock */ | |
196 RCC_RTCCLKCmd(ENABLE); | |
197 | |
198 /* Wait for RTC registers synchronization */ | |
199 RTC_WaitForSynchro(); | |
200 | |
201 /* Wait until last write operation on RTC registers has finished */ | |
202 RTC_WaitForLastTask(); | |
203 | |
204 /* Wait until last write operation on RTC registers has finished */ | |
205 RTC_WaitForLastTask(); | |
206 | |
207 /* Set RTC prescaler: set RTC period to 1sec */ | |
208 RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */ | |
209 | |
210 /* Wait until last write operation on RTC registers has finished */ | |
211 RTC_WaitForLastTask(); | |
212 | |
213 /* Clock setup */ | |
214 /* Enable clocks we need */ | |
215 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); | |
216 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | | |
217 RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE); | |
218 | |
219 /* Port configuration */ | |
132 /* Configure USART1 TX (PA.09) as alternate function push-pull */ | 220 /* Configure USART1 TX (PA.09) as alternate function push-pull */ |
133 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; | 221 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; |
134 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; | 222 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
135 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; | 223 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; |
136 GPIO_Init(GPIOA, &GPIO_InitStructure); | 224 GPIO_Init(GPIOA, &GPIO_InitStructure); |
138 /* Configure USART1 RX (PA.10) as input floating */ | 226 /* Configure USART1 RX (PA.10) as input floating */ |
139 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; | 227 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; |
140 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; | 228 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; |
141 GPIO_Init(GPIOA, &GPIO_InitStructure); | 229 GPIO_Init(GPIOA, &GPIO_InitStructure); |
142 | 230 |
143 /* Init UART1 - 115200 8n1, no flow control TX & RX enabled */ | 231 /* Configure PB5 as output push-pull */ |
232 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; | |
233 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; | |
234 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; | |
235 GPIO_Init(GPIOB, &GPIO_InitStructure); | |
236 | |
237 /* Configure PB15 as input pull-up push-pull */ | |
238 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; | |
239 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; | |
240 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; | |
241 GPIO_Init(GPIOB, &GPIO_InitStructure); | |
242 | |
243 /* USART configuration */ | |
244 /* USART1 - 115200 8n1, no flow control TX & RX enabled */ | |
144 USART_InitStructure.USART_BaudRate = 115200; | 245 USART_InitStructure.USART_BaudRate = 115200; |
145 USART_InitStructure.USART_WordLength = USART_WordLength_8b; | 246 USART_InitStructure.USART_WordLength = USART_WordLength_8b; |
146 USART_InitStructure.USART_StopBits = USART_StopBits_1; | 247 USART_InitStructure.USART_StopBits = USART_StopBits_1; |
147 USART_InitStructure.USART_Parity = USART_Parity_No; | 248 USART_InitStructure.USART_Parity = USART_Parity_No; |
148 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; | 249 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; |
149 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; | 250 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; |
150 | |
151 /* Configure USART1 */ | |
152 USART_Init(USART1, &USART_InitStructure); | 251 USART_Init(USART1, &USART_InitStructure); |
153 | 252 |
154 /* Enable interrupts on receive data */ | 253 /* Enable interrupts on receive data */ |
155 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); | 254 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); |
156 | 255 |
157 /* Enable the USART1 */ | 256 /* Enable USART */ |
158 USART_Cmd(USART1, ENABLE); | 257 USART_Cmd(USART1, ENABLE); |
159 } | 258 } |
160 | 259 |
161 /* Configure interrupt controller */ | 260 /* Configure interrupt controller */ |
162 #ifdef VECT_TAB_RAM | 261 #ifdef VECT_TAB_RAM |