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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * SAMD51 HAL developed by Giuliano Zaro (AKA GMagician)
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #ifdef __SAMD51__
  22. #include "../../inc/MarlinConfig.h"
  23. #include <Adafruit_ZeroDMA.h>
  24. #include <wiring_private.h>
  25. // ------------------------
  26. // Local defines
  27. // ------------------------
  28. #if HAS_TEMP_ADC_0
  29. #define GET_TEMP_0_ADC() PIN_TO_ADC(TEMP_0_PIN)
  30. #else
  31. #define GET_TEMP_0_ADC() -1
  32. #endif
  33. #if HAS_TEMP_ADC_1
  34. #define GET_TEMP_1_ADC() PIN_TO_ADC(TEMP_1_PIN)
  35. #else
  36. #define GET_TEMP_1_ADC() -1
  37. #endif
  38. #if HAS_TEMP_ADC_2
  39. #define GET_TEMP_2_ADC() PIN_TO_ADC(TEMP_2_PIN)
  40. #else
  41. #define GET_TEMP_2_ADC() -1
  42. #endif
  43. #if HAS_TEMP_ADC_3
  44. #define GET_TEMP_3_ADC() PIN_TO_ADC(TEMP_3_PIN)
  45. #else
  46. #define GET_TEMP_3_ADC() -1
  47. #endif
  48. #if HAS_TEMP_ADC_4
  49. #define GET_TEMP_4_ADC() PIN_TO_ADC(TEMP_4_PIN)
  50. #else
  51. #define GET_TEMP_4_ADC() -1
  52. #endif
  53. #if HAS_TEMP_ADC_5
  54. #define GET_TEMP_5_ADC() PIN_TO_ADC(TEMP_5_PIN)
  55. #else
  56. #define GET_TEMP_5_ADC() -1
  57. #endif
  58. #if HAS_TEMP_PROBE
  59. #define GET_PROBE_ADC() PIN_TO_ADC(TEMP_PROBE_PIN)
  60. #else
  61. #define GET_PROBE_ADC() -1
  62. #endif
  63. #if HAS_TEMP_ADC_BED
  64. #define GET_BED_ADC() PIN_TO_ADC(TEMP_BED_PIN)
  65. #else
  66. #define GET_BED_ADC() -1
  67. #endif
  68. #if HAS_TEMP_ADC_CHAMBER
  69. #define GET_CHAMBER_ADC() PIN_TO_ADC(TEMP_CHAMBER_PIN)
  70. #else
  71. #define GET_CHAMBER_ADC() -1
  72. #endif
  73. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  74. #define GET_FILAMENT_WIDTH_ADC() PIN_TO_ADC(FILWIDTH_PIN)
  75. #else
  76. #define GET_FILAMENT_WIDTH_ADC() -1
  77. #endif
  78. #if HAS_ADC_BUTTONS
  79. #define GET_BUTTONS_ADC() PIN_TO_ADC(ADC_KEYPAD_PIN)
  80. #else
  81. #define GET_BUTTONS_ADC() -1
  82. #endif
  83. #define IS_ADC_REQUIRED(n) (GET_TEMP_0_ADC() == n || GET_TEMP_1_ADC() == n || GET_TEMP_2_ADC() == n \
  84. || GET_TEMP_3_ADC() == n || GET_TEMP_4_ADC() == n || GET_TEMP_5_ADC() == n \
  85. || GET_PROBE_ADC() == n \
  86. || GET_BED_ADC() == n \
  87. || GET_CHAMBER_ADC() == n \
  88. || GET_FILAMENT_WIDTH_ADC() == n \
  89. || GET_BUTTONS_ADC() == n)
  90. #define ADC0_IS_REQUIRED IS_ADC_REQUIRED(0)
  91. #define ADC1_IS_REQUIRED IS_ADC_REQUIRED(1)
  92. #define ADC_IS_REQUIRED (ADC0_IS_REQUIRED || ADC1_IS_REQUIRED)
  93. #if ADC0_IS_REQUIRED
  94. #define FIRST_ADC 0
  95. #else
  96. #define FIRST_ADC 1
  97. #endif
  98. #if ADC1_IS_REQUIRED
  99. #define LAST_ADC 1
  100. #else
  101. #define LAST_ADC 0
  102. #endif
  103. #define DMA_IS_REQUIRED ADC_IS_REQUIRED
  104. // ------------------------
  105. // Types
  106. // ------------------------
  107. #if DMA_IS_REQUIRED
  108. // Struct must be 32 bits aligned because of DMA accesses but fields needs to be 8 bits packed
  109. typedef struct __attribute__((aligned(4), packed)) {
  110. ADC_INPUTCTRL_Type INPUTCTRL;
  111. } HAL_DMA_DAC_Registers; // DMA transfered registers
  112. #endif
  113. // ------------------------
  114. // Private Variables
  115. // ------------------------
  116. uint16_t HAL_adc_result;
  117. #if ADC_IS_REQUIRED
  118. // Pins used by ADC inputs. Order must be ADC0 inputs first then ADC1
  119. const uint8_t adc_pins[] = {
  120. // ADC0 pins
  121. #if GET_TEMP_0_ADC() == 0
  122. TEMP_0_PIN,
  123. #endif
  124. #if GET_TEMP_1_ADC() == 0
  125. TEMP_1_PIN,
  126. #endif
  127. #if GET_TEMP_2_ADC() == 0
  128. TEMP_2_PIN,
  129. #endif
  130. #if GET_TEMP_3_ADC() == 0
  131. TEMP_3_PIN,
  132. #endif
  133. #if GET_TEMP_4_ADC() == 0
  134. TEMP_4_PIN,
  135. #endif
  136. #if GET_TEMP_5_ADC() == 0
  137. TEMP_5_PIN,
  138. #endif
  139. #if GET_PROBE_ADC() == 0
  140. TEMP_PROBE_PIN,
  141. #endif
  142. #if GET_BED_ADC() == 0
  143. TEMP_BED_PIN,
  144. #endif
  145. #if GET_CHAMBER_ADC() == 0
  146. TEMP_CHAMBER_PIN,
  147. #endif
  148. #if GET_FILAMENT_WIDTH_ADC() == 0
  149. FILWIDTH_PIN,
  150. #endif
  151. #if GET_BUTTONS_ADC() == 0
  152. ADC_KEYPAD_PIN,
  153. #endif
  154. // ADC1 pins
  155. #if GET_TEMP_0_ADC() == 1
  156. TEMP_0_PIN,
  157. #endif
  158. #if GET_TEMP_1_ADC() == 1
  159. TEMP_1_PIN,
  160. #endif
  161. #if GET_TEMP_2_ADC() == 1
  162. TEMP_2_PIN,
  163. #endif
  164. #if GET_TEMP_3_ADC() == 1
  165. TEMP_3_PIN,
  166. #endif
  167. #if GET_TEMP_4_ADC() == 1
  168. TEMP_4_PIN,
  169. #endif
  170. #if GET_TEMP_5_ADC() == 1
  171. TEMP_5_PIN,
  172. #endif
  173. #if GET_PROBE_ADC() == 1
  174. TEMP_PROBE_PIN,
  175. #endif
  176. #if GET_BED_ADC() == 1
  177. TEMP_BED_PIN,
  178. #endif
  179. #if GET_CHAMBER_ADC() == 1
  180. TEMP_CHAMBER_PIN,
  181. #endif
  182. #if GET_FILAMENT_WIDTH_ADC() == 1
  183. FILWIDTH_PIN,
  184. #endif
  185. #if GET_BUTTONS_ADC() == 1
  186. ADC_KEYPAD_PIN,
  187. #endif
  188. };
  189. uint16_t HAL_adc_results[COUNT(adc_pins)];
  190. #if ADC0_IS_REQUIRED
  191. Adafruit_ZeroDMA adc0DMAProgram,
  192. adc0DMARead;
  193. const HAL_DMA_DAC_Registers adc0_dma_regs_list[] = {
  194. #if GET_TEMP_0_ADC() == 0
  195. { PIN_TO_INPUTCTRL(TEMP_0_PIN) },
  196. #endif
  197. #if GET_TEMP_1_ADC() == 0
  198. { PIN_TO_INPUTCTRL(TEMP_1_PIN) },
  199. #endif
  200. #if GET_TEMP_2_ADC() == 0
  201. { PIN_TO_INPUTCTRL(TEMP_2_PIN) },
  202. #endif
  203. #if GET_TEMP_3_ADC() == 0
  204. { PIN_TO_INPUTCTRL(TEMP_3_PIN) },
  205. #endif
  206. #if GET_TEMP_4_ADC() == 0
  207. { PIN_TO_INPUTCTRL(TEMP_4_PIN) },
  208. #endif
  209. #if GET_TEMP_5_ADC() == 0
  210. { PIN_TO_INPUTCTRL(TEMP_5_PIN) },
  211. #endif
  212. #if GET_PROBE_ADC() == 0
  213. { PIN_TO_INPUTCTRL(TEMP_PROBE_PIN) },
  214. #endif
  215. #if GET_BED_ADC() == 0
  216. { PIN_TO_INPUTCTRL(TEMP_BED_PIN) },
  217. #endif
  218. #if GET_CHAMBER_ADC() == 0
  219. { PIN_TO_INPUTCTRL(TEMP_CHAMBER_PIN) },
  220. #endif
  221. #if GET_FILAMENT_WIDTH_ADC() == 0
  222. { PIN_TO_INPUTCTRL(FILWIDTH_PIN) },
  223. #endif
  224. #if GET_BUTTONS_ADC() == 0
  225. { PIN_TO_INPUTCTRL(ADC_KEYPAD_PIN) },
  226. #endif
  227. };
  228. #define ADC0_AINCOUNT COUNT(adc0_dma_regs_list)
  229. #endif // ADC0_IS_REQUIRED
  230. #if ADC1_IS_REQUIRED
  231. Adafruit_ZeroDMA adc1DMAProgram,
  232. adc1DMARead;
  233. const HAL_DMA_DAC_Registers adc1_dma_regs_list[] = {
  234. #if GET_TEMP_0_ADC() == 1
  235. { PIN_TO_INPUTCTRL(TEMP_0_PIN) },
  236. #endif
  237. #if GET_TEMP_1_ADC() == 1
  238. { PIN_TO_INPUTCTRL(TEMP_1_PIN) },
  239. #endif
  240. #if GET_TEMP_2_ADC() == 1
  241. { PIN_TO_INPUTCTRL(TEMP_2_PIN) },
  242. #endif
  243. #if GET_TEMP_3_ADC() == 1
  244. { PIN_TO_INPUTCTRL(TEMP_3_PIN) },
  245. #endif
  246. #if GET_TEMP_4_ADC() == 1
  247. { PIN_TO_INPUTCTRL(TEMP_4_PIN) },
  248. #endif
  249. #if GET_TEMP_5_ADC() == 1
  250. { PIN_TO_INPUTCTRL(TEMP_5_PIN) },
  251. #endif
  252. #if GET_PROBE_ADC() == 1
  253. { PIN_TO_INPUTCTRL(TEMP_PROBE_PIN) },
  254. #endif
  255. #if GET_BED_ADC() == 1
  256. { PIN_TO_INPUTCTRL(TEMP_BED_PIN) },
  257. #endif
  258. #if GET_CHAMBER_ADC() == 1
  259. { PIN_TO_INPUTCTRL(TEMP_CHAMBER_PIN) },
  260. #endif
  261. #if GET_FILAMENT_WIDTH_ADC() == 1
  262. { PIN_TO_INPUTCTRL(FILWIDTH_PIN) },
  263. #endif
  264. #if GET_BUTTONS_ADC() == 1
  265. { PIN_TO_INPUTCTRL(ADC_KEYPAD_PIN) },
  266. #endif
  267. };
  268. #define ADC1_AINCOUNT COUNT(adc1_dma_regs_list)
  269. #endif // ADC1_IS_REQUIRED
  270. #endif // ADC_IS_REQUIRED
  271. // ------------------------
  272. // Private functions
  273. // ------------------------
  274. #if DMA_IS_REQUIRED
  275. void dma_init() {
  276. DmacDescriptor *descriptor;
  277. #if ADC0_IS_REQUIRED
  278. adc0DMAProgram.setTrigger(ADC0_DMAC_ID_SEQ);
  279. adc0DMAProgram.setAction(DMA_TRIGGER_ACTON_BEAT);
  280. adc0DMAProgram.loop(true);
  281. if (adc0DMAProgram.allocate() == DMA_STATUS_OK) {
  282. descriptor = adc0DMAProgram.addDescriptor(
  283. (void *)adc0_dma_regs_list, // SRC
  284. (void *)&ADC0->DSEQDATA.reg, // DEST
  285. sizeof(adc0_dma_regs_list) / 4, // CNT
  286. DMA_BEAT_SIZE_WORD,
  287. true, // SRCINC
  288. false, // DSTINC
  289. DMA_ADDRESS_INCREMENT_STEP_SIZE_1, // STEPSIZE
  290. DMA_STEPSEL_SRC // STEPSEL
  291. );
  292. if (descriptor != nullptr)
  293. descriptor->BTCTRL.bit.EVOSEL = DMA_EVENT_OUTPUT_BEAT;
  294. adc0DMAProgram.startJob();
  295. }
  296. adc0DMARead.setTrigger(ADC0_DMAC_ID_RESRDY);
  297. adc0DMARead.setAction(DMA_TRIGGER_ACTON_BEAT);
  298. adc0DMARead.loop(true);
  299. if (adc0DMARead.allocate() == DMA_STATUS_OK) {
  300. adc0DMARead.addDescriptor(
  301. (void *)&ADC0->RESULT.reg, // SRC
  302. &HAL_adc_results, // DEST
  303. ADC0_AINCOUNT, // CNT
  304. DMA_BEAT_SIZE_HWORD,
  305. false, // SRCINC
  306. true, // DSTINC
  307. DMA_ADDRESS_INCREMENT_STEP_SIZE_1, // STEPSIZE
  308. DMA_STEPSEL_DST // STEPSEL
  309. );
  310. adc0DMARead.startJob();
  311. }
  312. #endif
  313. #if ADC1_IS_REQUIRED
  314. adc1DMAProgram.setTrigger(ADC1_DMAC_ID_SEQ);
  315. adc1DMAProgram.setAction(DMA_TRIGGER_ACTON_BEAT);
  316. adc1DMAProgram.loop(true);
  317. if (adc1DMAProgram.allocate() == DMA_STATUS_OK) {
  318. descriptor = adc1DMAProgram.addDescriptor(
  319. (void *)adc1_dma_regs_list, // SRC
  320. (void *)&ADC1->DSEQDATA.reg, // DEST
  321. sizeof(adc1_dma_regs_list) / 4, // CNT
  322. DMA_BEAT_SIZE_WORD,
  323. true, // SRCINC
  324. false, // DSTINC
  325. DMA_ADDRESS_INCREMENT_STEP_SIZE_1, // STEPSIZE
  326. DMA_STEPSEL_SRC // STEPSEL
  327. );
  328. if (descriptor != nullptr)
  329. descriptor->BTCTRL.bit.EVOSEL = DMA_EVENT_OUTPUT_BEAT;
  330. adc1DMAProgram.startJob();
  331. }
  332. adc1DMARead.setTrigger(ADC1_DMAC_ID_RESRDY);
  333. adc1DMARead.setAction(DMA_TRIGGER_ACTON_BEAT);
  334. adc1DMARead.loop(true);
  335. if (adc1DMARead.allocate() == DMA_STATUS_OK) {
  336. adc1DMARead.addDescriptor(
  337. (void *)&ADC1->RESULT.reg, // SRC
  338. &HAL_adc_results[ADC0_AINCOUNT], // DEST
  339. ADC1_AINCOUNT, // CNT
  340. DMA_BEAT_SIZE_HWORD,
  341. false, // SRCINC
  342. true, // DSTINC
  343. DMA_ADDRESS_INCREMENT_STEP_SIZE_1, // STEPSIZE
  344. DMA_STEPSEL_DST // STEPSEL
  345. );
  346. adc1DMARead.startJob();
  347. }
  348. #endif
  349. DMAC->PRICTRL0.bit.RRLVLEN0 = true; // Activate round robin for DMA channels required by ADCs
  350. }
  351. #endif // DMA_IS_REQUIRED
  352. // ------------------------
  353. // Public functions
  354. // ------------------------
  355. // HAL initialization task
  356. void HAL_init() {
  357. #if DMA_IS_REQUIRED
  358. dma_init();
  359. #endif
  360. #if ENABLED(SDSUPPORT)
  361. // SD_DETECT_PIN may be removed if NO_SD_HOST_DRIVE is not defined in Configuration_adv.h
  362. #if SD_CONNECTION_IS(ONBOARD) && PIN_EXISTS(SD_DETECT)
  363. SET_INPUT_PULLUP(SD_DETECT_PIN);
  364. #endif
  365. OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
  366. #endif
  367. }
  368. // HAL idle task
  369. /*
  370. void HAL_idletask() {
  371. }
  372. */
  373. void HAL_clear_reset_source() { }
  374. #pragma push_macro("WDT")
  375. #undef WDT // Required to be able to use '.bit.WDT'. Compiler wrongly replace struct field with WDT define
  376. uint8_t HAL_get_reset_source() {
  377. RSTC_RCAUSE_Type resetCause;
  378. resetCause.reg = REG_RSTC_RCAUSE;
  379. if (resetCause.bit.POR) return RST_POWER_ON;
  380. else if (resetCause.bit.EXT) return RST_EXTERNAL;
  381. else if (resetCause.bit.BODCORE || resetCause.bit.BODVDD) return RST_BROWN_OUT;
  382. else if (resetCause.bit.WDT) return RST_WATCHDOG;
  383. else if (resetCause.bit.SYST || resetCause.bit.NVM) return RST_SOFTWARE;
  384. else if (resetCause.bit.BACKUP) return RST_BACKUP;
  385. return 0;
  386. }
  387. #pragma pop_macro("WDT")
  388. extern "C" {
  389. void * _sbrk(int incr);
  390. extern unsigned int __bss_end__; // end of bss section
  391. }
  392. // Return free memory between end of heap (or end bss) and whatever is current
  393. int freeMemory() {
  394. int free_memory, heap_end = (int)_sbrk(0);
  395. return (int)&free_memory - (heap_end ?: (int)&__bss_end__);
  396. }
  397. // ------------------------
  398. // ADC
  399. // ------------------------
  400. void HAL_adc_init() {
  401. #if ADC_IS_REQUIRED
  402. memset(HAL_adc_results, 0xFF, sizeof(HAL_adc_results)); // Fill result with invalid values
  403. for (uint8_t pi = 0; pi < COUNT(adc_pins); ++pi)
  404. pinPeripheral(adc_pins[pi], PIO_ANALOG);
  405. for (uint8_t ai = FIRST_ADC; ai <= LAST_ADC; ++ai) {
  406. Adc* adc = ((Adc*[])ADC_INSTS)[ai];
  407. // ADC clock setup
  408. GCLK->PCHCTRL[ADC0_GCLK_ID + ai].bit.CHEN = false;
  409. SYNC(GCLK->PCHCTRL[ADC0_GCLK_ID + ai].bit.CHEN);
  410. GCLK->PCHCTRL[ADC0_GCLK_ID + ai].reg = GCLK_PCHCTRL_GEN_GCLK1 | GCLK_PCHCTRL_CHEN; // 48MHz startup code programmed
  411. SYNC(!GCLK->PCHCTRL[ADC0_GCLK_ID + ai].bit.CHEN);
  412. adc->CTRLA.bit.PRESCALER = ADC_CTRLA_PRESCALER_DIV32_Val; // 1.5MHZ adc clock
  413. // ADC setup
  414. // Preloaded data (fixed for all ADC instances hence not loaded by DMA)
  415. adc->REFCTRL.bit.REFSEL = ADC_REFCTRL_REFSEL_AREFA_Val; // VRefA pin
  416. SYNC(adc->SYNCBUSY.bit.REFCTRL);
  417. adc->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_12BIT_Val;
  418. SYNC(adc->SYNCBUSY.bit.CTRLB);
  419. adc->SAMPCTRL.bit.SAMPLEN = (6 - 1); // Sampling clocks
  420. adc->AVGCTRL.reg = ADC_AVGCTRL_SAMPLENUM_16 | ADC_AVGCTRL_ADJRES(4); // 16 Accumulated conversions and shift 4 to get oversampled 12 bits result
  421. SYNC(adc->SYNCBUSY.bit.AVGCTRL);
  422. // Registers loaded by DMA
  423. adc->DSEQCTRL.bit.INPUTCTRL = true;
  424. adc->DSEQCTRL.bit.AUTOSTART = true; // Start conversion after DMA sequence
  425. adc->CTRLA.bit.ENABLE = true; // Enable ADC
  426. SYNC(adc->SYNCBUSY.bit.ENABLE);
  427. }
  428. #endif // ADC_IS_REQUIRED
  429. }
  430. void HAL_adc_start_conversion(const uint8_t adc_pin) {
  431. #if ADC_IS_REQUIRED
  432. for (uint8_t pi = 0; pi < COUNT(adc_pins); ++pi) {
  433. if (adc_pin == adc_pins[pi]) {
  434. HAL_adc_result = HAL_adc_results[pi];
  435. return;
  436. }
  437. }
  438. #endif
  439. HAL_adc_result = 0xFFFF;
  440. }
  441. #endif // __SAMD51__