blob: 1fbe9a6996be9de09070c87dfb2285ffacacff0d [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;
Brian Silvermanf1ad1bc2017-09-23 13:08:36 -040023extern uint32_t __bss_ram_start__[];
24extern uint32_t __heap_start__[];
25extern uint32_t __stack_end__[];
Brian Silverman8b638692017-06-26 23:10:26 -070026
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
Brian Silvermanf1ad1bc2017-09-23 13:08:36 -040054 printf("Ram start: %p\n", __bss_ram_start__);
55 printf("Heap start: %p\n", __heap_start__);
Brian Silverman8b638692017-06-26 23:10:26 -070056 printf("Heap end: %p\n", __brkval);
Brian Silvermanf1ad1bc2017-09-23 13:08:36 -040057 printf("Stack start: %p\n", __stack_end__);
Brian Silverman8b638692017-06-26 23:10:26 -070058
59 GPIOC_PSOR = 1 << 5;
Brian Silverman8b638692017-06-26 23:10:26 -070060 while (true) {}
Brian Silvermanf1ad1bc2017-09-23 13:08:36 -040061
Brian Silverman8b638692017-06-26 23:10:26 -070062 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