/src/main.rs

https://gitlab.com/motorica-org/telemetry-hardware-teensy-3-rust · Rust · 167 lines · 142 code · 22 blank · 3 comment · 3 complexity · 49a69440492e0ce55a8ea5c8c7325925 MD5 · raw file

  1. #![feature(lang_items,core_intrinsics,asm,start)]
  2. #![no_std]
  3. #![crate_type="staticlib"]
  4. use core::intrinsics::volatile_store;
  5. #[lang="eh_personality"]
  6. extern "C" fn eh_personality() {}
  7. #[lang="panic_fmt"]
  8. #[no_mangle]
  9. pub extern "C" fn rust_begin_unwind(_fmt: &core::fmt::Arguments,
  10. _file_line: &(&'static str, usize))
  11. -> ! {
  12. loop {}
  13. }
  14. #[no_mangle]
  15. pub extern "C" fn __aeabi_unwind_cpp_pr0() -> () {
  16. loop {}
  17. }
  18. #[no_mangle]
  19. pub extern "C" fn __aeabi_unwind_cpp_pr1() -> () {
  20. loop {}
  21. }
  22. macro_rules! GPIOC_PDOR {() => (0x400FF080 as *mut u32);} // GPIOC_PDOR - page 1334,1335
  23. macro_rules! WDOG_UNLOCK {() => (0x4005200E as *mut u16);} // Watchdog Unlock register
  24. macro_rules! WDOG_STCTRLH {() => (0x40052000 as *mut u16);} // Watchdog Status and Control Register High
  25. macro_rules! GPIO_CONFIG {() => (0x40048038 as *mut u32);}
  26. macro_rules! PORTC_PCR5 {() => (0x4004B014 as *mut u32);} // PORTC_PCR5 - page 223/227
  27. macro_rules! GPIOC_PDDR {() => (0x400FF094 as *mut u32);} // GPIOC_PDDR - page 1334,1337
  28. macro_rules! GPIOC_PDOR {() => (0x400FF080 as *mut u32);} // GPIOC_PDOR - page 1334,1335
  29. extern "C" {
  30. static mut _sflashdata: u32;
  31. static mut _sdata: u32;
  32. static mut _edata: u32;
  33. static mut _sbss: u32;
  34. static mut _ebss: u32;
  35. fn _estack();
  36. }
  37. #[link_section=".vectors"]
  38. #[allow(non_upper_case_globals)]
  39. #[no_mangle]
  40. pub static ISRVectors: [Option<unsafe extern "C" fn()>; 16] = [Some(_estack), // Stack pointer
  41. Some(startup), // Reset
  42. Some(isr_nmi), // NMI
  43. Some(isr_hardfault), // Hard Fault
  44. Some(isr_mmfault), /* CM3 Memory Management Fault */
  45. Some(isr_busfault), /* CM3 Bus Fault */
  46. Some(isr_usagefault), /* CM3 Usage Fault */
  47. Some(isr_reserved_1), /* Reserved - Used as NXP Checksum */
  48. None, // Reserved
  49. None, // Reserved
  50. None, // Reserved
  51. Some(isr_svcall), // SVCall
  52. Some(isr_debugmon), /* Reserved for debug */
  53. None, // Reserved
  54. Some(isr_pendsv), // PendSV
  55. Some(isr_systick) /* SysTick */];
  56. #[link_section=".flashconfig"]
  57. #[allow(non_upper_case_globals)]
  58. #[no_mangle]
  59. pub static flashconfigbytes: [usize; 4] = [0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE];
  60. pub unsafe extern "C" fn startup() {
  61. let mut src: *mut u32 = &mut _sflashdata;
  62. let mut dest: *mut u32 = &mut _sdata;
  63. volatile_store(WDOG_UNLOCK!(), 0xC520);
  64. volatile_store(WDOG_UNLOCK!(), 0xD928);
  65. volatile_store(WDOG_STCTRLH!(), 0x01D2);
  66. while dest < &mut _edata as *mut u32 {
  67. *dest = *src;
  68. dest = ((dest as u32) + 4) as *mut u32;
  69. src = ((src as u32) + 4) as *mut u32;
  70. }
  71. dest = &mut _sbss as *mut u32;
  72. while dest < &mut _edata as *mut u32 {
  73. *dest = 0;
  74. dest = ((dest as u32) + 4) as *mut u32;
  75. }
  76. // Enable system clock on all GPIO ports - page 254
  77. *GPIO_CONFIG!() = 0x00043F82; // 0b1000011111110000010
  78. // Configure the led pin
  79. *PORTC_PCR5!() = 0x00000143; // Enables GPIO | DSE | PULL_ENABLE | PULL_SELECT - page 227
  80. // Set the led pin to output
  81. *GPIOC_PDDR!() = 0x20; // pin 5 on port c
  82. rust_loop();
  83. }
  84. pub fn led_on() {
  85. unsafe {
  86. volatile_store(GPIOC_PDOR!(), 0x20);
  87. }
  88. }
  89. pub fn led_off() {
  90. unsafe {
  91. volatile_store(GPIOC_PDOR!(), 0x0);
  92. }
  93. }
  94. pub fn delay(ms: i32) {
  95. for _ in 0..ms * 250 {
  96. unsafe {
  97. asm!("NOP");
  98. }
  99. }
  100. }
  101. pub fn rust_loop() {
  102. loop {
  103. led_on();
  104. delay(1000);
  105. led_off();
  106. delay(1000);
  107. }
  108. }
  109. #[start]
  110. fn lang_start(_: isize, _: *const *const u8) -> isize {
  111. unsafe {
  112. startup();
  113. }
  114. 0
  115. }
  116. pub unsafe extern "C" fn isr_nmi() {
  117. loop {}
  118. }
  119. pub unsafe extern "C" fn isr_hardfault() {
  120. loop {}
  121. }
  122. pub unsafe extern "C" fn isr_mmfault() {
  123. loop {}
  124. }
  125. pub unsafe extern "C" fn isr_busfault() {
  126. loop {}
  127. }
  128. pub unsafe extern "C" fn isr_usagefault() {
  129. loop {}
  130. }
  131. pub unsafe extern "C" fn isr_reserved_1() {
  132. loop {}
  133. }
  134. pub unsafe extern "C" fn isr_svcall() {
  135. loop {}
  136. }
  137. pub unsafe extern "C" fn isr_debugmon() {
  138. loop {}
  139. }
  140. pub unsafe extern "C" fn isr_pendsv() {
  141. loop {}
  142. }
  143. pub unsafe extern "C" fn isr_systick() {
  144. loop {}
  145. }