blob: 1a899b16e27343183df452ea44addd6b4ef2821e [file] [log] [blame]
Brian Silverman8b638692017-06-26 23:10:26 -07001#include "motors/core/kinetis.h"
2
3#include <stdio.h>
4
5#include "motors/core/time.h"
6#include "motors/usb/usb_serial.h"
7#include "motors/util.h"
8
9namespace frc971 {
10namespace salsa {
11
12extern "C" {
13void *__stack_chk_guard = (void *)0x67111971;
14extern void usb_init();
15int _write(int file, char *ptr, int len) {
16 (void)file;
17 return usb_serial_write(0, ptr, len);
18}
19
20void __stack_chk_fail(void);
21
22extern char *__brkval;
23extern unsigned long _ebss;
24extern unsigned long _sram;
25extern unsigned long _estack;
26
27} // extern "C"
28
29extern "C" int main(void) {
30 // for background about this startup delay, please see these conversations
31 // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980
32 // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273
33 delay(400);
34
35 // Set all interrupts to the second-lowest priority to start with.
36 for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD);
37
38 // Now set priorities for all the ones we care about. They only have meaning
39 // relative to each other, which means centralizing them here makes it a lot
40 // more manageable.
41 NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7);
42
43 // Set the LED's pin to output mode.
44 GPIO_BITBAND(GPIOC_PDDR, 5) = 1;
45 PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1);
46
47 usb_serial_init();
48 usb_descriptor_set_product_id(0x0490);
49 usb_init();
50
51 // Give everything a chance to get going.
52 delay(100);
53
54 printf("Ram start: %p\n", &_sram);
55 printf("Heap start: %p\n", &_ebss);
56 printf("Heap end: %p\n", __brkval);
57 printf("Stack start: %p\n", &_estack);
58
59 GPIOC_PSOR = 1 << 5;
60
61 while (true) {}
62 return 0;
63}
64
65void __stack_chk_fail(void) {
66 while (true) {
67 GPIOC_PSOR = (1 << 5);
68 printf("Stack corruption detected\n");
69 delay(1000);
70 GPIOC_PCOR = (1 << 5);
71 delay(1000);
72 }
73}
74
75} // namespace salsa
76} // namespace frc971