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 10KB

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