My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

boards.cpp 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /******************************************************************************
  2. * The MIT License
  3. *
  4. * Copyright (c) 2010 Perry Hung.
  5. * Copyright (c) 2011, 2012 LeafLabs, LLC.
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use, copy,
  11. * modify, merge, publish, distribute, sublicense, and/or sell copies
  12. * of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  22. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  23. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. *****************************************************************************/
  27. /**
  28. * @file wirish/boards.cpp
  29. * @brief init() and board routines.
  30. *
  31. * This file is mostly interesting for the init() function, which
  32. * configures Flash, the core clocks, and a variety of other available
  33. * peripherals on the board so the rest of Wirish doesn't have to turn
  34. * things on before using them.
  35. *
  36. * Prior to returning, init() calls boardInit(), which allows boards
  37. * to perform any initialization they need to. This file includes a
  38. * weak no-op definition of boardInit(), so boards that don't need any
  39. * special initialization don't have to define their own.
  40. *
  41. * How init() works is chip-specific. See the boards_setup.cpp files
  42. * under e.g. wirish/stm32f1/, wirish/stmf32f2 for the details, but be
  43. * advised: their contents are unstable, and can/will change without
  44. * notice.
  45. */
  46. #include <boards.h>
  47. #include <libmaple/libmaple_types.h>
  48. #include <libmaple/flash.h>
  49. #include <libmaple/nvic.h>
  50. #include <libmaple/systick.h>
  51. #include "boards_private.h"
  52. static void setup_flash(void);
  53. static void setup_clocks(void);
  54. static void setup_nvic(void);
  55. static void setup_adcs(void);
  56. static void setup_timers(void);
  57. /*
  58. * Exported functions
  59. */
  60. void init(void) {
  61. setup_flash();
  62. setup_clocks();
  63. setup_nvic();
  64. systick_init(SYSTICK_RELOAD_VAL);
  65. wirish::priv::board_setup_gpio();
  66. setup_adcs();
  67. setup_timers();
  68. wirish::priv::board_setup_usb();
  69. wirish::priv::series_init();
  70. boardInit();
  71. }
  72. /* Provide a default no-op boardInit(). */
  73. __weak void boardInit(void) {
  74. }
  75. /* You could farm this out to the files in boards/ if e.g. it takes
  76. * too long to test on boards with lots of pins. */
  77. bool boardUsesPin(uint8 pin) {
  78. for (int i = 0; i < BOARD_NR_USED_PINS; i++) {
  79. if (pin == boardUsedPins[i]) {
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. /*
  86. * Auxiliary routines
  87. */
  88. static void setup_flash(void) {
  89. // Turn on as many Flash "go faster" features as
  90. // possible. flash_enable_features() just ignores any flags it
  91. // can't support.
  92. flash_enable_features(FLASH_PREFETCH | FLASH_ICACHE | FLASH_DCACHE);
  93. // Configure the wait states, assuming we're operating at "close
  94. // enough" to 3.3V.
  95. flash_set_latency(FLASH_SAFE_WAIT_STATES);
  96. }
  97. static void setup_clocks(void) {
  98. // Turn on HSI. We'll switch to and run off of this while we're
  99. // setting up the main PLL.
  100. rcc_turn_on_clk(RCC_CLK_HSI);
  101. // Turn off and reset the clock subsystems we'll be using, as well
  102. // as the clock security subsystem (CSS). Note that resetting CFGR
  103. // to its default value of 0 implies a switch to HSI for SYSCLK.
  104. RCC_BASE->CFGR = 0x00000000;
  105. rcc_disable_css();
  106. rcc_turn_off_clk(RCC_CLK_PLL);
  107. rcc_turn_off_clk(RCC_CLK_HSE);
  108. wirish::priv::board_reset_pll();
  109. // Clear clock readiness interrupt flags and turn off clock
  110. // readiness interrupts.
  111. RCC_BASE->CIR = 0x00000000;
  112. #if !USE_HSI_CLOCK
  113. // Enable HSE, and wait until it's ready.
  114. rcc_turn_on_clk(RCC_CLK_HSE);
  115. while (!rcc_is_clk_ready(RCC_CLK_HSE))
  116. ;
  117. #endif
  118. // Configure AHBx, APBx, etc. prescalers and the main PLL.
  119. wirish::priv::board_setup_clock_prescalers();
  120. rcc_configure_pll(&wirish::priv::w_board_pll_cfg);
  121. // Enable the PLL, and wait until it's ready.
  122. rcc_turn_on_clk(RCC_CLK_PLL);
  123. while(!rcc_is_clk_ready(RCC_CLK_PLL))
  124. ;
  125. // Finally, switch to the now-ready PLL as the main clock source.
  126. rcc_switch_sysclk(RCC_CLKSRC_PLL);
  127. }
  128. /*
  129. * These addresses are where usercode starts when a bootloader is
  130. * present. If no bootloader is present, the user NVIC usually starts
  131. * at the Flash base address, 0x08000000.
  132. */
  133. #if defined(BOOTLOADER_maple)
  134. #define USER_ADDR_ROM 0x08002000
  135. #else
  136. #define USER_ADDR_ROM 0x08000000
  137. #endif
  138. #define USER_ADDR_RAM 0x20000C00
  139. extern char __text_start__;
  140. static void setup_nvic(void) {
  141. nvic_init((uint32)VECT_TAB_ADDR, 0);
  142. /* Roger Clark. We now control nvic vector table in boards.txt using the build.vect paramater
  143. #ifdef VECT_TAB_FLASH
  144. nvic_init(USER_ADDR_ROM, 0);
  145. #elif defined VECT_TAB_RAM
  146. nvic_init(USER_ADDR_RAM, 0);
  147. #elif defined VECT_TAB_BASE
  148. nvic_init((uint32)0x08000000, 0);
  149. #elif defined VECT_TAB_ADDR
  150. // A numerically supplied value
  151. nvic_init((uint32)VECT_TAB_ADDR, 0);
  152. #else
  153. // Use the __text_start__ value from the linker script; this
  154. // should be the start of the vector table.
  155. nvic_init((uint32)&__text_start__, 0);
  156. #endif
  157. */
  158. }
  159. static void adc_default_config(adc_dev *dev) {
  160. adc_enable_single_swstart(dev);
  161. adc_set_sample_rate(dev, wirish::priv::w_adc_smp);
  162. }
  163. static void setup_adcs(void) {
  164. adc_set_prescaler(wirish::priv::w_adc_pre);
  165. adc_foreach(adc_default_config);
  166. }
  167. static void timer_default_config(timer_dev *dev) {
  168. timer_adv_reg_map *regs = (dev->regs).adv;
  169. const uint16 full_overflow = 0xFFFF;
  170. const uint16 half_duty = 0x8FFF;
  171. timer_init(dev);
  172. timer_pause(dev);
  173. regs->CR1 = TIMER_CR1_ARPE;
  174. regs->PSC = 1;
  175. regs->SR = 0;
  176. regs->DIER = 0;
  177. regs->EGR = TIMER_EGR_UG;
  178. switch (dev->type) {
  179. case TIMER_ADVANCED:
  180. regs->BDTR = TIMER_BDTR_MOE | TIMER_BDTR_LOCK_OFF;
  181. // fall-through
  182. case TIMER_GENERAL:
  183. timer_set_reload(dev, full_overflow);
  184. for (uint8 channel = 1; channel <= 4; channel++) {
  185. if (timer_has_cc_channel(dev, channel)) {
  186. timer_set_compare(dev, channel, half_duty);
  187. timer_oc_set_mode(dev, channel, TIMER_OC_MODE_PWM_1,
  188. TIMER_OC_PE);
  189. }
  190. }
  191. // fall-through
  192. case TIMER_BASIC:
  193. break;
  194. }
  195. timer_generate_update(dev);
  196. timer_resume(dev);
  197. }
  198. static void setup_timers(void) {
  199. timer_foreach(timer_default_config);
  200. }