Mercurial > ~darius > hgwebdir.cgi > stm32test
comparison main.c @ 2:274e01fa5a4c
- Do console IO with RX IRQs.
- Init tick counter (unused anyway).
- Handle C-u and C-w.
- Rejig NVIC setup for less duplication.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Sat, 08 Oct 2011 20:35:34 +1030 |
parents | c59513fd84fb |
children | 74e9b3baac1e |
comparison
equal
deleted
inserted
replaced
1:7a08db98ae8b | 2:274e01fa5a4c |
---|---|
3 #include <stdint.h> | 3 #include <stdint.h> |
4 #include "stm32f10x.h" | 4 #include "stm32f10x.h" |
5 #include "main.h" | 5 #include "main.h" |
6 #include "comm.h" | 6 #include "comm.h" |
7 | 7 |
8 USART_InitTypeDef USART_InitStructure; | 8 typedef volatile struct { |
9 char buf[40]; | |
10 uint8_t state; | |
11 uint8_t len; | |
12 } consbuf_t; | |
9 | 13 |
10 void Setup_HW(void); | 14 void Setup_HW(void); |
11 void NVIC_Configuration(void); | 15 void NVIC_Configuration(void); |
12 | 16 |
13 /* Called every millisecond */ | 17 /* Called every millisecond */ |
14 RAMFUNC void | 18 RAMFUNC void |
15 SysTick_Handler(void) { | 19 SysTick_Handler(void) { |
16 static uint32_t tick; | 20 static uint32_t tick = 0; |
17 | 21 |
18 tick++; | 22 tick++; |
19 } | 23 } |
20 | 24 |
25 consbuf_t cmd; | |
26 | |
21 RAMFUNC void | 27 RAMFUNC void |
22 USART1_IRQHandler(void) { | 28 USART1_IRQHandler(void) { |
29 char c; | |
30 int i; | |
23 | 31 |
32 /* Recieved data */ | |
33 while (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { | |
34 c = USART_ReceiveData(USART1); | |
35 | |
36 /* End of line? */ | |
37 if (c == '\n' || c == '\r') { | |
38 cmd.buf[cmd.state] = '\0'; | |
39 fputs("\r\n", stdout); | |
40 cmd.len = cmd.state; | |
41 cmd.state = 255; | |
42 continue; | |
43 } | |
44 | |
45 /* Ctrl-w / Ctrl-u */ | |
46 if (c == 0x17 || c == 0x15) { | |
47 for (i = 0; i < cmd.state; i++) | |
48 fputs("\010\040\010", stdout); | |
49 cmd.state = 0; | |
50 continue; | |
51 } | |
52 | |
53 /* Backspace/delete */ | |
54 if (c == 0x08 || c == 0x7f) { | |
55 if (cmd.state > 0) { | |
56 cmd.state--; | |
57 fputs("\010\040\010", stdout); | |
58 } | |
59 continue; | |
60 } | |
61 | |
62 /* Anything unprintable just ignore it */ | |
63 if (!isprint(c)) | |
64 continue; | |
65 | |
66 cmd.buf[cmd.state] = tolower(c); | |
67 | |
68 /* Echo back to the user */ | |
69 comm_put(cmd.buf[cmd.state]); | |
70 | |
71 cmd.state++; | |
72 /* Over flow? */ | |
73 if (cmd.state == ((sizeof(cmd.buf) / sizeof(cmd.buf[0])) - 1)) { | |
74 fputs("\r\nLine too long", stdout); | |
75 cmd.state = 0; | |
76 continue; | |
77 } | |
78 } | |
24 } | 79 } |
25 | 80 |
26 typedef volatile struct { | |
27 char buf[40]; | |
28 uint8_t state; | |
29 uint8_t len; | |
30 } consbuf_t; | |
31 | 81 |
32 int | 82 int |
33 main(void) { | 83 main(void) { |
34 consbuf_t cmd; | |
35 char c; | |
36 int idx; | |
37 | |
38 cmd.state = cmd.len = 0; | 84 cmd.state = cmd.len = 0; |
39 | 85 |
40 /* Setup USART etc */ | 86 /* Setup USART etc */ |
41 Setup_HW(); | 87 Setup_HW(); |
42 | 88 |
49 comm_puts("Can't setup SysTick\r\n"); | 95 comm_puts("Can't setup SysTick\r\n"); |
50 while (1) | 96 while (1) |
51 ; | 97 ; |
52 } | 98 } |
53 | 99 |
100 setvbuf(stdout, NULL, _IONBF, 0); | |
101 | |
54 /* Say hello */ | 102 /* Say hello */ |
55 comm_puts("\r\nHello world\r\n"); | 103 fputs("\r\nHello world\r\n", stdout); |
56 idx = 0; | |
57 | 104 |
58 while (1) { | 105 while (1) { |
59 comm_puts("> "); | 106 fputs("> ", stdout); |
60 while (1) { | |
61 c = comm_get(); | |
62 | 107 |
63 /* End of line? */ | 108 while (cmd.state != 255) |
64 if (c == '\n' || c == '\r') { | 109 ; |
65 cmd.buf[cmd.state] = '\0'; | |
66 printf("\r\n"); | |
67 cmd.len = cmd.state; | |
68 cmd.state = 255; | |
69 break; | |
70 } | |
71 | 110 |
72 /* Backspace/delete */ | |
73 if (c == 0x08 || c == 0x7f) { | |
74 if (cmd.state > 0) { | |
75 cmd.state--; | |
76 printf("\177\040\177"); | |
77 } | |
78 continue; | |
79 } | |
80 | |
81 /* Anything unprintable just ignore it */ | |
82 if (!isprint(c)) | |
83 continue; | |
84 cmd.buf[cmd.state] = tolower(c); | |
85 | |
86 /* Echo back to the user */ | |
87 comm_put(cmd.buf[cmd.state]); | |
88 | |
89 cmd.state++; | |
90 /* Over flow? */ | |
91 if (cmd.state == ((sizeof(cmd.buf) / sizeof(cmd.buf[0])) - 1)) { | |
92 printf("\r\nLine too long"); | |
93 cmd.state = 0; | |
94 continue; | |
95 } | |
96 } | |
97 if (cmd.len > 0) | 111 if (cmd.len > 0) |
98 printf("Got command '%s'\r\n", cmd.buf); | 112 printf("Got command '%s'\r\n", cmd.buf); |
99 cmd.state = 0; | 113 cmd.state = 0; |
100 } | 114 } |
101 } | 115 } |
102 | 116 |
103 /* Setup hardware (USART etc) */ | 117 /* Setup hardware (USART etc) */ |
104 void | 118 void |
105 Setup_HW(void) { | 119 Setup_HW(void) { |
106 GPIO_InitTypeDef GPIO_InitStructure; | 120 GPIO_InitTypeDef GPIO_InitStructure; |
121 USART_InitTypeDef USART_InitStructure; | |
107 | 122 |
108 /* Enable USART1, GPIOA, GPIOD and AFIO clocks */ | 123 /* Enable USART1, GPIOA, GPIOD and AFIO clocks */ |
109 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOD | 124 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOD |
110 | RCC_APB2Periph_AFIO, ENABLE); | 125 | RCC_APB2Periph_AFIO, ENABLE); |
111 /* Enable USART2 clock */ | 126 /* Enable USART2 clock */ |
134 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; | 149 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; |
135 | 150 |
136 /* Configure USART1 */ | 151 /* Configure USART1 */ |
137 USART_Init(USART1, &USART_InitStructure); | 152 USART_Init(USART1, &USART_InitStructure); |
138 | 153 |
154 /* Enable interrupts on receive data */ | |
155 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); | |
156 | |
139 /* Enable the USART1 */ | 157 /* Enable the USART1 */ |
140 USART_Cmd(USART1, ENABLE); | 158 USART_Cmd(USART1, ENABLE); |
141 } | 159 } |
142 | 160 |
143 /* Configure interrupt controller */ | 161 /* Configure interrupt controller */ |
144 #ifdef VECT_TAB_RAM | 162 #ifdef VECT_TAB_RAM |
145 /* vector-offset (TBLOFF) from bottom of SRAM. defined in linker script */ | 163 /* vector-offset (TBLOFF) from bottom of SRAM. defined in linker script */ |
146 extern uint32_t _isr_vectorsram_offs; | 164 extern uint32_t _isr_vectorsram_offs; |
165 #else | |
166 extern uint32_t _isr_vectorsflash_offs; | |
167 #endif | |
168 | |
147 void | 169 void |
148 NVIC_Configuration(void) { | 170 NVIC_Configuration(void) { |
171 NVIC_InitTypeDef NVIC_InitStructure; | |
172 | |
173 #ifdef VECT_TAB_RAM | |
149 /* Set the Vector Table base location at 0x20000000+_isr_vectorsram_offs */ | 174 /* Set the Vector Table base location at 0x20000000+_isr_vectorsram_offs */ |
150 NVIC_SetVectorTable(NVIC_VectTab_RAM, (uint32_t)&_isr_vectorsram_offs); | 175 NVIC_SetVectorTable(NVIC_VectTab_RAM, (uint32_t)&_isr_vectorsram_offs); |
151 } | |
152 #else | 176 #else |
153 extern uint32_t _isr_vectorsflash_offs; | |
154 void | |
155 NVIC_Configuration(void) { | |
156 /* Set the Vector Table base location at 0x08000000+_isr_vectorsflash_offs */ | 177 /* Set the Vector Table base location at 0x08000000+_isr_vectorsflash_offs */ |
157 NVIC_SetVectorTable(NVIC_VectTab_FLASH, (uint32_t)&_isr_vectorsflash_offs); | 178 NVIC_SetVectorTable(NVIC_VectTab_FLASH, (uint32_t)&_isr_vectorsflash_offs); |
179 #endif | |
180 | |
181 /* Enable the USART1 Interrupt */ | |
182 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; | |
183 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; | |
184 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; | |
185 NVIC_Init(&NVIC_InitStructure); | |
158 } | 186 } |
159 #endif /* VECT_TAB_RAM */ | 187 |