-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.c
192 lines (156 loc) · 3.83 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
#define HSE_VALUE ((uint32_t)8000000) /* STM32 discovery uses a 8Mhz external crystal */
#include "stm32f4xx_conf.h"
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_exti.h"
#include "usbd_cdc_core.h"
#include "usbd_usr.h"
#include "usbd_desc.h"
#include "usbd_cdc_vcp.h"
#include "usb_dcd_int.h"
volatile uint32_t ticker, downTicker;
/*
* The USB data must be 4 byte aligned if DMA is enabled. This macro handles
* the alignment, if necessary (it's actually magic, but don't tell anyone).
*/
__ALIGN_BEGIN USB_OTG_CORE_HANDLE USB_OTG_dev __ALIGN_END;
void init();
void ColorfulRingOfDeath(void);
/*
* Define prototypes for interrupt handlers here. The conditional "extern"
* ensures the weak declarations from startup_stm32f4xx.c are overridden.
*/
#ifdef __cplusplus
extern "C" {
#endif
void SysTick_Handler(void);
void NMI_Handler(void);
void HardFault_Handler(void);
void MemManage_Handler(void);
void BusFault_Handler(void);
void UsageFault_Handler(void);
void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void OTG_FS_IRQHandler(void);
void OTG_FS_WKUP_IRQHandler(void);
#ifdef __cplusplus
}
#endif
int main(void)
{
/* Set up the system clocks */
SystemInit();
/* Initialize USB, IO, SysTick, and all those other things you do in the morning */
init();
while (1)
{
/* Blink the orange LED at 1Hz */
if (500 == ticker)
{
GPIOD->BSRRH = GPIO_Pin_13;
}
else if (1000 == ticker)
{
ticker = 0;
GPIOD->BSRRL = GPIO_Pin_13;
}
/* If there's data on the virtual serial port:
* - Echo it back
* - Turn the green LED on for 10ms
*/
uint8_t theByte;
if (VCP_get_char(&theByte))
{
VCP_put_char(theByte);
GPIOD->BSRRL = GPIO_Pin_12;
downTicker = 10;
}
if (0 == downTicker)
{
GPIOD->BSRRH = GPIO_Pin_12;
}
}
return 0;
}
void init()
{
/* STM32F4 discovery LEDs */
GPIO_InitTypeDef LED_Config;
/* Always remember to turn on the peripheral clock... If not, you may be up till 3am debugging... */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
LED_Config.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
LED_Config.GPIO_Mode = GPIO_Mode_OUT;
LED_Config.GPIO_OType = GPIO_OType_PP;
LED_Config.GPIO_Speed = GPIO_Speed_25MHz;
LED_Config.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &LED_Config);
/* Setup SysTick or CROD! */
if (SysTick_Config(SystemCoreClock / 1000))
{
ColorfulRingOfDeath();
}
/* Setup USB */
USBD_Init(&USB_OTG_dev,
USB_OTG_FS_CORE_ID,
&USR_desc,
&USBD_CDC_cb,
&USR_cb);
return;
}
/*
* Call this to indicate a failure. Blinks the STM32F4 discovery LEDs
* in sequence. At 168Mhz, the blinking will be very fast - about 5 Hz.
* Keep that in mind when debugging, knowing the clock speed might help
* with debugging.
*/
void ColorfulRingOfDeath(void)
{
uint16_t ring = 1;
while (1)
{
uint32_t count = 0;
while (count++ < 500000);
GPIOD->BSRRH = (ring << 12);
ring = ring << 1;
if (ring >= 1<<4)
{
ring = 1;
}
GPIOD->BSRRL = (ring << 12);
}
}
/*
* Interrupt Handlers
*/
void SysTick_Handler(void)
{
ticker++;
if (downTicker > 0)
{
downTicker--;
}
}
void NMI_Handler(void) {}
void HardFault_Handler(void) { ColorfulRingOfDeath(); }
void MemManage_Handler(void) { ColorfulRingOfDeath(); }
void BusFault_Handler(void) { ColorfulRingOfDeath(); }
void UsageFault_Handler(void){ ColorfulRingOfDeath(); }
void SVC_Handler(void) {}
void DebugMon_Handler(void) {}
void PendSV_Handler(void) {}
void OTG_FS_IRQHandler(void)
{
USBD_OTG_ISR_Handler (&USB_OTG_dev);
}
void OTG_FS_WKUP_IRQHandler(void)
{
if(USB_OTG_dev.cfg.low_power)
{
*(uint32_t *)(0xE000ED10) &= 0xFFFFFFF9 ;
SystemInit();
USB_OTG_UngateClock(&USB_OTG_dev);
}
EXTI_ClearITPendingBit(EXTI_Line18);
}