/
main.c
355 lines (284 loc) · 7.23 KB
/
main.c
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <sys/types.h>
#include <stdint.h>
#include <stdio.h>
#include <debug.h>
#include <stm32.h>
#include <stm32f10x_usart.h>
#include <stm32f10x_rcc.h>
#include <stm32f10x_gpio.h>
#include <stm32f10x_flash.h>
#include <stm32f10x_dbgmcu.h>
#include <misc.h>
#include <core_cm3.h>
/* externals */
extern unsigned int __data_start_rom, __data_start, __data_end;
extern unsigned int __bss_start, __bss_end;
void _nmi(void)
{
printf("nmi\n");
halt();
}
void _hardfault(void)
{
printf("hardfault\n");
halt();
}
void _memmanage(void)
{
printf("memmanage\n");
halt();
}
void _busfault(void)
{
printf("busfault\n");
halt();
}
void _usagefault(void)
{
printf("usagefault\n");
halt();
}
void _svc(void)
{
printf("svc\n");
halt();
}
void _pendsv(void)
{
printf("pendsv\n");
halt();
}
volatile uint32_t tick_counter = 0;
const uint32_t systick_counter = 10000000;
const uint32_t systick_rate = HSE_VALUE;
void _systick(void)
{
tick_counter++;
}
uint32_t current_time(void)
{
do {
uint32_t t = tick_counter;
uint32_t delta = SysTick->VAL;
uint32_t t2 = tick_counter;
if (t2 != t)
continue;
uint32_t time = (t2 * systick_counter + (systick_counter - delta)) / (systick_rate / 1000000);
return time;
} while (true);
}
void spin(uint32_t usecs)
{
uint32_t t = current_time();
while ((current_time() - t) < usecs)
;
}
void stm32_USART1_IRQ(void)
{
printf("USART1_IRQ\n");
}
static void usart_init(USART_TypeDef *usart)
{
if (usart == USART1) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
} else if (usart == USART2) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
} else if (usart == USART3) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
}
USART_InitTypeDef init;
init.USART_BaudRate = 115200;
init.USART_WordLength = USART_WordLength_8b;
init.USART_StopBits = USART_StopBits_1;
init.USART_Parity = USART_Parity_No;
init.USART_Mode = USART_Mode_Tx;
init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(usart, &init);
USART_ITConfig(usart, USART_IT_RXNE, DISABLE);
USART_Cmd(usart, ENABLE);
}
static void init_leds(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
GPIO_InitTypeDef init;
init.GPIO_Pin = GPIO_Pin_6;
init.GPIO_Speed = GPIO_Speed_50MHz;
init.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOF, &init);
init.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOF, &init);
init.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOF, &init);
init.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOF, &init);
}
static void set_led(uint led, bool en)
{
uint16_t pin;
switch (led) {
default:
case 0: pin = GPIO_Pin_6; break;
case 1: pin = GPIO_Pin_7; break;
case 2: pin = GPIO_Pin_8; break;
case 3: pin = GPIO_Pin_9; break;
}
GPIO_WriteBit(GPIOF, pin, en ? Bit_SET : Bit_RESET);
}
#define LED_CS GPIO_Pin_6
#define LED_WR GPIO_Pin_7
#define LED_DATA GPIO_Pin_8
void led_panel_write(uint32_t dat, size_t len)
{
uint i;
for (i = len; i > 0; i--) {
GPIO_ResetBits(GPIOF, LED_WR);
GPIO_WriteBit(GPIOF, LED_DATA, (dat & (1<<(i-1))) ? SET : RESET);
GPIO_SetBits(GPIOF, LED_WR);
}
}
void led_panel_command_write(uint32_t cmd, size_t len)
{
GPIO_ResetBits(GPIOF, LED_CS);
led_panel_write(cmd, len);
GPIO_SetBits(GPIOF, LED_CS);
}
static void dump_clocks(void)
{
RCC_ClocksTypeDef clocks;
RCC_GetClocksFreq(&clocks);
printf("SYSCLKFrequency %u\n", clocks.SYSCLK_Frequency);
printf("HCLKFrequency %u\n", clocks.HCLK_Frequency);
printf("PCLK1Frequency %u\n", clocks.PCLK1_Frequency);
printf("PCLK2Frequency %u\n", clocks.PCLK2_Frequency);
printf("ADCCLKFrequency %u\n", clocks.ADCCLK_Frequency);
}
void _start(void)
{
/* copy data from rom */
if (&__data_start != &__data_start_rom) {
unsigned int *src = &__data_start_rom;
unsigned int *dest = &__data_start;
while (dest != &__data_end)
*dest++ = *src++;
}
/* zero out bss */
unsigned int *bss = &__bss_start;
while (bss != &__bss_end)
*bss++ = 0;
USART_TypeDef *debug_usart;
#if TARGET_STM3210E
/* configure the usart1 pins */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART1, DISABLE);
GPIO_InitTypeDef init;
init.GPIO_Pin = GPIO_Pin_9;
init.GPIO_Speed = GPIO_Speed_50MHz;
init.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &init);
init.GPIO_Pin = GPIO_Pin_10;
init.GPIO_Speed = GPIO_Speed_50MHz;
init.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &init);
debug_usart = USART1;
#endif
#if TARGET_STM32_P107
/* configure the usart3 pins */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_FullRemap_USART3, ENABLE);
GPIO_InitTypeDef init;
init.GPIO_Pin = GPIO_Pin_8;
init.GPIO_Speed = GPIO_Speed_50MHz;
init.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &init);
init.GPIO_Pin = GPIO_Pin_9;
init.GPIO_Speed = GPIO_Speed_50MHz;
init.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &init);
debug_usart = USART3;
#endif
init_leds();
usart_init(debug_usart);
printf("how are you gentlemen\n");
printf("devid 0x%x\n", DBGMCU_GetDEVID());
dump_clocks();
// bring up te HSE
printf("enabling external crystal\n");
RCC_HSEConfig(RCC_HSE_ON);
RCC_WaitForHSEStartUp();
printf("external crystal up\n");
// try to program up the pll
printf("enabling pll\n");
#if STM32F10X_CL
RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_4);
#else
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
#endif
RCC_PLLCmd(ENABLE);
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
;
printf("pll latched\n");
printf("setting up clocks\n");
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PCLK2Config(RCC_HCLK_Div1);
#if STM32F10X_CL
RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
#else
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
#endif
usart_init(debug_usart);
set_led(3, 0);
set_led(3, 1);
printf("after new sysclk\n");
dump_clocks();
printf("done!\n");
/* try to fire the systick */
// __set_BASEPRI(8 << __NVIC_PRIO_BITS);
/* start the systick timer */
NVIC_SetVectorTable(0, 0);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
uint32_t pri = NVIC_EncodePriority(3, 0, 0);
NVIC_SetPriority(SysTick_IRQn, pri);
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
SysTick_Config(systick_counter);
#if 0
uint32_t last = 0;
for (;;) {
uint32_t now = current_time();
if (now - last >= 1000000) {
printf("time %d\n", now);
last = now;
}
}
#endif
#if 0
uint32_t val;
for (val = 0; ; val++) {
set_led(0, val & 0x1);
set_led(1, val & 0x2);
set_led(2, val & 0x4);
set_led(3, val & 0x8);
}
#endif
/* write the boot sequence */
led_panel_command_write(0b100000000010, 12); // SYS_EN
led_panel_command_write(0b100000000110, 12); // LED_ON
led_panel_command_write(0b100000010000, 12); // BLINK_OFF
led_panel_command_write(0b100000110000, 12); // INT_RC
led_panel_command_write(0b100001001000, 12); // n-mos open drain, 16 com
led_panel_command_write(0b100101011110, 12); // PWM_CTRL | 0xf
for(uint j = 0; ; j++) {
GPIO_ResetBits(GPIOF, LED_CS);
led_panel_write(0b1010000000, 10); // start write at address 0
for (int i = 0; i < 96; i++) {
led_panel_write(((j % 96) > i) ? 0b1111: 0, 4);
}
GPIO_SetBits(GPIOF, LED_CS);
spin(10000);
}
for(;;)
;
}