blob: 144fc1ec8e2626173795b31551fb2c6cc85b8c7c [file] [log] [blame]
Brian Silverman8b638692017-06-26 23:10:26 -07001#include "motors/core/kinetis.h"
2
3#include <stdio.h>
4
Brian Silverman8d3816a2017-07-03 18:52:15 -07005#include <atomic>
6
Brian Silverman8b638692017-06-26 23:10:26 -07007#include "motors/core/time.h"
Brian Silverman8d3816a2017-07-03 18:52:15 -07008#include "motors/motor.h"
9#include "motors/motor_controls.h"
10#include "motors/peripheral/adc.h"
11#include "motors/peripheral/can.h"
Brian Silverman8b638692017-06-26 23:10:26 -070012#include "motors/usb/usb_serial.h"
13#include "motors/util.h"
14
15namespace frc971 {
16namespace salsa {
Brian Silverman8d3816a2017-07-03 18:52:15 -070017namespace {
18
19::std::atomic<Motor *> global_motor{nullptr};
Brian Silverman8b638692017-06-26 23:10:26 -070020
21extern "C" {
Brian Silverman8d3816a2017-07-03 18:52:15 -070022
Brian Silverman8b638692017-06-26 23:10:26 -070023void *__stack_chk_guard = (void *)0x67111971;
Brian Silverman8d3816a2017-07-03 18:52:15 -070024void __stack_chk_fail(void) {
25 while (true) {
26 GPIOC_PSOR = (1 << 5);
27 printf("Stack corruption detected\n");
28 delay(1000);
29 GPIOC_PCOR = (1 << 5);
30 delay(1000);
31 }
32}
33
Brian Silverman8b638692017-06-26 23:10:26 -070034extern void usb_init();
35int _write(int file, char *ptr, int len) {
36 (void)file;
37 return usb_serial_write(0, ptr, len);
38}
39
40void __stack_chk_fail(void);
41
42extern char *__brkval;
Brian Silvermanf1ad1bc2017-09-23 13:08:36 -040043extern uint32_t __bss_ram_start__[];
44extern uint32_t __heap_start__[];
45extern uint32_t __stack_end__[];
Brian Silverman8b638692017-06-26 23:10:26 -070046
Brian Silverman8d3816a2017-07-03 18:52:15 -070047void ftm0_isr(void) {
48 global_motor.load(::std::memory_order_relaxed)->HandleInterrupt();
49}
50
Brian Silverman8b638692017-06-26 23:10:26 -070051} // extern "C"
Brian Silverman8d3816a2017-07-03 18:52:15 -070052} // namespace
Brian Silverman8b638692017-06-26 23:10:26 -070053
54extern "C" int main(void) {
55 // for background about this startup delay, please see these conversations
56 // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980
57 // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273
58 delay(400);
59
60 // Set all interrupts to the second-lowest priority to start with.
61 for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD);
62
63 // Now set priorities for all the ones we care about. They only have meaning
64 // relative to each other, which means centralizing them here makes it a lot
65 // more manageable.
66 NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7);
Brian Silverman8d3816a2017-07-03 18:52:15 -070067 NVIC_SET_SANE_PRIORITY(IRQ_FTM0, 0x3);
Brian Silverman8b638692017-06-26 23:10:26 -070068
69 // Set the LED's pin to output mode.
70 GPIO_BITBAND(GPIOC_PDDR, 5) = 1;
71 PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1);
72
Brian Silverman8d3816a2017-07-03 18:52:15 -070073 GPIO_BITBAND(GPIOA_PDDR, 15) = 1;
74 PORTA_PCR15 = PORT_PCR_DSE | PORT_PCR_MUX(1);
75
76 DMA_CR = DMA_CR_EMLM;
Brian Silverman8b638692017-06-26 23:10:26 -070077 usb_serial_init();
78 usb_descriptor_set_product_id(0x0490);
79 usb_init();
Brian Silverman8d3816a2017-07-03 18:52:15 -070080 AdcInit();
81 MathInit();
82 delay(1000);
83 can_init();
84
85 MotorControlsImplementation controls;
86
87 delay(1000);
88 Motor motor(FTM0, FTM1, &controls);
89 motor.Init();
90 global_motor.store(&motor, ::std::memory_order_relaxed);
91 // Output triggers to things like the PDBs on initialization.
92 FTM0_EXTTRIG = FTM_EXTTRIG_INITTRIGEN;
93 // Don't let any memory accesses sneak past here, because we actually
94 // need everything to be starting up.
95 __asm__("" :: : "memory");
Brian Silverman8b638692017-06-26 23:10:26 -070096
97 // Give everything a chance to get going.
98 delay(100);
99
Brian Silverman8d3816a2017-07-03 18:52:15 -0700100#if 0
Brian Silvermanf1ad1bc2017-09-23 13:08:36 -0400101 printf("Ram start: %p\n", __bss_ram_start__);
102 printf("Heap start: %p\n", __heap_start__);
Brian Silverman8b638692017-06-26 23:10:26 -0700103 printf("Heap end: %p\n", __brkval);
Brian Silvermanf1ad1bc2017-09-23 13:08:36 -0400104 printf("Stack start: %p\n", __stack_end__);
Brian Silverman8d3816a2017-07-03 18:52:15 -0700105#endif
Brian Silverman8b638692017-06-26 23:10:26 -0700106
Brian Silverman8d3816a2017-07-03 18:52:15 -0700107 printf("Going silent to zero motors...\n");
108 // Give the print a chance to make it out.
109 delay(1000);
110 motor.Zero();
111
112 printf("Zeroed motor!\n");
113 // Give stuff a chance to recover from interrupts-disabled.
114 delay(100);
115 motor.Start();
116
117 GPIOA_PCOR = 1 << 15;
118
119 // TODO(Brian): Use SLEEPONEXIT to reduce interrupt latency?
Brian Silverman8b638692017-06-26 23:10:26 -0700120 while (true) {}
Brian Silvermanf1ad1bc2017-09-23 13:08:36 -0400121
Brian Silverman8b638692017-06-26 23:10:26 -0700122 return 0;
123}
124
Brian Silverman8b638692017-06-26 23:10:26 -0700125} // namespace salsa
126} // namespace frc971