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.

HAL.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
  6. * Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
  7. * Copyright (c) 2017 Victor Perez
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. /**
  24. * HAL for stm32duino.com based on Libmaple and compatible (STM32F1)
  25. */
  26. #ifdef __STM32F1__
  27. // --------------------------------------------------------------------------
  28. // Includes
  29. // --------------------------------------------------------------------------
  30. #include "HAL.h"
  31. #include <STM32ADC.h>
  32. #include "../../inc/MarlinConfig.h"
  33. // --------------------------------------------------------------------------
  34. // Externals
  35. // --------------------------------------------------------------------------
  36. // --------------------------------------------------------------------------
  37. // Types
  38. // --------------------------------------------------------------------------
  39. #define __I
  40. #define __IO volatile
  41. typedef struct
  42. {
  43. __I uint32_t CPUID; /*!< Offset: 0x000 (R/ ) CPUID Base Register */
  44. __IO uint32_t ICSR; /*!< Offset: 0x004 (R/W) Interrupt Control and State Register */
  45. __IO uint32_t VTOR; /*!< Offset: 0x008 (R/W) Vector Table Offset Register */
  46. __IO uint32_t AIRCR; /*!< Offset: 0x00C (R/W) Application Interrupt and Reset Control Register */
  47. __IO uint32_t SCR; /*!< Offset: 0x010 (R/W) System Control Register */
  48. __IO uint32_t CCR; /*!< Offset: 0x014 (R/W) Configuration Control Register */
  49. __IO uint8_t SHP[12]; /*!< Offset: 0x018 (R/W) System Handlers Priority Registers (4-7, 8-11, 12-15) */
  50. __IO uint32_t SHCSR; /*!< Offset: 0x024 (R/W) System Handler Control and State Register */
  51. __IO uint32_t CFSR; /*!< Offset: 0x028 (R/W) Configurable Fault Status Register */
  52. __IO uint32_t HFSR; /*!< Offset: 0x02C (R/W) HardFault Status Register */
  53. __IO uint32_t DFSR; /*!< Offset: 0x030 (R/W) Debug Fault Status Register */
  54. __IO uint32_t MMFAR; /*!< Offset: 0x034 (R/W) MemManage Fault Address Register */
  55. __IO uint32_t BFAR; /*!< Offset: 0x038 (R/W) BusFault Address Register */
  56. __IO uint32_t AFSR; /*!< Offset: 0x03C (R/W) Auxiliary Fault Status Register */
  57. __I uint32_t PFR[2]; /*!< Offset: 0x040 (R/ ) Processor Feature Register */
  58. __I uint32_t DFR; /*!< Offset: 0x048 (R/ ) Debug Feature Register */
  59. __I uint32_t ADR; /*!< Offset: 0x04C (R/ ) Auxiliary Feature Register */
  60. __I uint32_t MMFR[4]; /*!< Offset: 0x050 (R/ ) Memory Model Feature Register */
  61. __I uint32_t ISAR[5]; /*!< Offset: 0x060 (R/ ) Instruction Set Attributes Register */
  62. uint32_t RESERVED0[5];
  63. __IO uint32_t CPACR; /*!< Offset: 0x088 (R/W) Coprocessor Access Control Register */
  64. } SCB_Type;
  65. // --------------------------------------------------------------------------
  66. // Variables
  67. // --------------------------------------------------------------------------
  68. // --------------------------------------------------------------------------
  69. // Local defines
  70. // --------------------------------------------------------------------------
  71. #define SCS_BASE (0xE000E000UL) /*!< System Control Space Base Address */
  72. #define SCB_BASE (SCS_BASE + 0x0D00UL) /*!< System Control Block Base Address */
  73. #define SCB ((SCB_Type *) SCB_BASE ) /*!< SCB configuration struct */
  74. /* SCB Application Interrupt and Reset Control Register Definitions */
  75. #define SCB_AIRCR_VECTKEY_Pos 16 /*!< SCB AIRCR: VECTKEY Position */
  76. #define SCB_AIRCR_VECTKEY_Msk (0xFFFFUL << SCB_AIRCR_VECTKEY_Pos) /*!< SCB AIRCR: VECTKEY Mask */
  77. #define SCB_AIRCR_PRIGROUP_Pos 8 /*!< SCB AIRCR: PRIGROUP Position */
  78. #define SCB_AIRCR_PRIGROUP_Msk (7UL << SCB_AIRCR_PRIGROUP_Pos) /*!< SCB AIRCR: PRIGROUP Mask */
  79. // --------------------------------------------------------------------------
  80. // Public Variables
  81. // --------------------------------------------------------------------------
  82. #ifdef SERIAL_USB
  83. USBSerial SerialUSB;
  84. #endif
  85. uint16_t HAL_adc_result;
  86. // --------------------------------------------------------------------------
  87. // Private Variables
  88. // --------------------------------------------------------------------------
  89. STM32ADC adc(ADC1);
  90. uint8_t adc_pins[] = {
  91. #if HAS_TEMP_ADC_0
  92. TEMP_0_PIN,
  93. #endif
  94. #if HAS_HEATED_BED
  95. TEMP_BED_PIN,
  96. #endif
  97. #if HAS_HEATED_CHAMBER
  98. TEMP_CHAMBER_PIN,
  99. #endif
  100. #if HAS_TEMP_ADC_1
  101. TEMP_1_PIN,
  102. #endif
  103. #if HAS_TEMP_ADC_2
  104. TEMP_2_PIN,
  105. #endif
  106. #if HAS_TEMP_ADC_3
  107. TEMP_3_PIN,
  108. #endif
  109. #if HAS_TEMP_ADC_4
  110. TEMP_4_PIN,
  111. #endif
  112. #if HAS_TEMP_ADC_5
  113. TEMP_5_PIN,
  114. #endif
  115. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  116. FILWIDTH_PIN,
  117. #endif
  118. };
  119. enum TEMP_PINS : char {
  120. #if HAS_TEMP_ADC_0
  121. TEMP_0,
  122. #endif
  123. #if HAS_HEATED_BED
  124. TEMP_BED,
  125. #endif
  126. #if HAS_HEATED_CHAMBER
  127. TEMP_CHAMBER,
  128. #endif
  129. #if HAS_TEMP_ADC_1
  130. TEMP_1,
  131. #endif
  132. #if HAS_TEMP_ADC_2
  133. TEMP_2,
  134. #endif
  135. #if HAS_TEMP_ADC_3
  136. TEMP_3,
  137. #endif
  138. #if HAS_TEMP_ADC_4
  139. TEMP_4,
  140. #endif
  141. #if HAS_TEMP_ADC_5
  142. TEMP_5,
  143. #endif
  144. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  145. FILWIDTH,
  146. #endif
  147. ADC_PIN_COUNT
  148. };
  149. uint16_t HAL_adc_results[ADC_PIN_COUNT];
  150. // --------------------------------------------------------------------------
  151. // Function prototypes
  152. // --------------------------------------------------------------------------
  153. // --------------------------------------------------------------------------
  154. // Private functions
  155. // --------------------------------------------------------------------------
  156. static void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) {
  157. uint32_t reg_value;
  158. uint32_t PriorityGroupTmp = (PriorityGroup & (uint32_t)0x07); /* only values 0..7 are used */
  159. reg_value = SCB->AIRCR; /* read old register configuration */
  160. reg_value &= ~(SCB_AIRCR_VECTKEY_Msk | SCB_AIRCR_PRIGROUP_Msk); /* clear bits to change */
  161. reg_value = (reg_value |
  162. ((uint32_t)0x5FA << SCB_AIRCR_VECTKEY_Pos) |
  163. (PriorityGroupTmp << 8)); /* Insert write key and priorty group */
  164. SCB->AIRCR = reg_value;
  165. }
  166. // --------------------------------------------------------------------------
  167. // Public functions
  168. // --------------------------------------------------------------------------
  169. //
  170. // Leave PA11/PA12 intact if USBSerial is not used
  171. //
  172. #if SERIAL_USB
  173. namespace wirish { namespace priv {
  174. #if SERIAL_PORT > 0
  175. #if SERIAL_PORT2
  176. #if SERIAL_PORT2 > 0
  177. void board_setup_usb(void) {}
  178. #endif
  179. #else
  180. void board_setup_usb(void) {}
  181. #endif
  182. #endif
  183. } }
  184. #endif
  185. void HAL_init(void) {
  186. NVIC_SetPriorityGrouping(0x3);
  187. }
  188. /* VGPV Done with defines
  189. // disable interrupts
  190. void cli(void) { noInterrupts(); }
  191. // enable interrupts
  192. void sei(void) { interrupts(); }
  193. */
  194. void HAL_clear_reset_source(void) { }
  195. /**
  196. * TODO: Check this and change or remove.
  197. * currently returns 1 that's equal to poweron reset.
  198. */
  199. uint8_t HAL_get_reset_source(void) { return 1; }
  200. void _delay_ms(const int delay_ms) { delay(delay_ms); }
  201. extern "C" {
  202. extern unsigned int _ebss; // end of bss section
  203. }
  204. /**
  205. * TODO: Change this to correct it for libmaple
  206. */
  207. // return free memory between end of heap (or end bss) and whatever is current
  208. /*
  209. #include "wirish/syscalls.c"
  210. //extern caddr_t _sbrk(int incr);
  211. #ifndef CONFIG_HEAP_END
  212. extern char _lm_heap_end;
  213. #define CONFIG_HEAP_END ((caddr_t)&_lm_heap_end)
  214. #endif
  215. extern "C" {
  216. static int freeMemory() {
  217. char top = 't';
  218. return &top - reinterpret_cast<char*>(sbrk(0));
  219. }
  220. int freeMemory() {
  221. int free_memory;
  222. int heap_end = (int)_sbrk(0);
  223. free_memory = ((int)&free_memory) - ((int)heap_end);
  224. return free_memory;
  225. }
  226. }
  227. */
  228. // --------------------------------------------------------------------------
  229. // ADC
  230. // --------------------------------------------------------------------------
  231. // Init the AD in continuous capture mode
  232. void HAL_adc_init(void) {
  233. // configure the ADC
  234. adc.calibrate();
  235. adc.setSampleRate(ADC_SMPR_41_5); // ?
  236. adc.setPins(adc_pins, ADC_PIN_COUNT);
  237. adc.setDMA(HAL_adc_results, (uint16_t)ADC_PIN_COUNT, (uint32_t)(DMA_MINC_MODE | DMA_CIRC_MODE), (void (*)())NULL);
  238. adc.setScanMode();
  239. adc.setContinuous();
  240. adc.startConversion();
  241. }
  242. void HAL_adc_start_conversion(const uint8_t adc_pin) {
  243. TEMP_PINS pin_index;
  244. switch (adc_pin) {
  245. #if HAS_TEMP_ADC_0
  246. case TEMP_0_PIN: pin_index = TEMP_0; break;
  247. #endif
  248. #if HAS_HEATED_BED
  249. case TEMP_BED_PIN: pin_index = TEMP_BED; break;
  250. #endif
  251. #if HAS_HEATED_CHAMBER
  252. case TEMP_CHAMBER_PIN: pin_index = TEMP_CHAMBER; break;
  253. #endif
  254. #if HAS_TEMP_ADC_1
  255. case TEMP_1_PIN: pin_index = TEMP_1; break;
  256. #endif
  257. #if HAS_TEMP_ADC_2
  258. case TEMP_2_PIN: pin_index = TEMP_2; break;
  259. #endif
  260. #if HAS_TEMP_ADC_3
  261. case TEMP_3_PIN: pin_index = TEMP_3; break;
  262. #endif
  263. #if HAS_TEMP_ADC_4
  264. case TEMP_4_PIN: pin_index = TEMP_4; break;
  265. #endif
  266. #if HAS_TEMP_ADC_5
  267. case TEMP_5_PIN: pin_index = TEMP_5; break;
  268. #endif
  269. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  270. case FILWIDTH_PIN: pin_index = FILWIDTH; break;
  271. #endif
  272. }
  273. HAL_adc_result = (HAL_adc_results[(int)pin_index] >> 2) & 0x3FF; // shift to get 10 bits only.
  274. }
  275. uint16_t HAL_adc_get_result(void) {
  276. return HAL_adc_result;
  277. }
  278. #endif // __STM32F1__