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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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_HEATED_BED
  59. #define GET_BED_ADC() PIN_TO_ADC(TEMP_BED_PIN)
  60. #else
  61. #define GET_BED_ADC() -1
  62. #endif
  63. #if HAS_HEATED_CHAMBER
  64. #define GET_CHAMBER_ADC() PIN_TO_ADC(TEMP_CHAMBER_PIN)
  65. #else
  66. #define GET_CHAMBER_ADC() -1
  67. #endif
  68. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  69. #define GET_FILAMENT_WIDTH_ADC() PIN_TO_ADC(FILWIDTH_PIN)
  70. #else
  71. #define GET_FILAMENT_WIDTH_ADC() -1
  72. #endif
  73. #if HAS_ADC_BUTTONS
  74. #define GET_BUTTONS_ADC() PIN_TO_ADC(ADC_KEYPAD_PIN)
  75. #else
  76. #define GET_BUTTONS_ADC() -1
  77. #endif
  78. #define IS_ADC_REQUIRED(n) (GET_TEMP_0_ADC() == n || GET_TEMP_1_ADC() == n || GET_TEMP_2_ADC() == n \
  79. || GET_TEMP_3_ADC() == n || GET_TEMP_4_ADC() == n || GET_TEMP_5_ADC() == n \
  80. || GET_BED_ADC() == n \
  81. || GET_CHAMBER_ADC() == n \
  82. || GET_FILAMENT_WIDTH_ADC() == n \
  83. || GET_BUTTONS_ADC() == n)
  84. #define ADC0_IS_REQUIRED IS_ADC_REQUIRED(0)
  85. #define ADC1_IS_REQUIRED IS_ADC_REQUIRED(1)
  86. #define ADC_IS_REQUIRED (ADC0_IS_REQUIRED || ADC1_IS_REQUIRED)
  87. #if ADC0_IS_REQUIRED
  88. #define FIRST_ADC 0
  89. #else
  90. #define FIRST_ADC 1
  91. #endif
  92. #if ADC1_IS_REQUIRED
  93. #define LAST_ADC 1
  94. #else
  95. #define LAST_ADC 0
  96. #endif
  97. #define DMA_IS_REQUIRED ADC_IS_REQUIRED
  98. // ------------------------
  99. // Types
  100. // ------------------------
  101. #if DMA_IS_REQUIRED
  102. // Struct must be 32 bits aligned because of DMA accesses but fields needs to be 8 bits packed
  103. typedef struct __attribute__((aligned(4), packed)) {
  104. ADC_INPUTCTRL_Type INPUTCTRL;
  105. } HAL_DMA_DAC_Registers; // DMA transfered registers
  106. #endif
  107. // ------------------------
  108. // Private Variables
  109. // ------------------------
  110. uint16_t HAL_adc_result;
  111. #if ADC_IS_REQUIRED
  112. // Pins used by ADC inputs. Order must be ADC0 inputs first then ADC1
  113. const uint8_t adc_pins[] = {
  114. // ADC0 pins
  115. #if GET_TEMP_0_ADC() == 0
  116. TEMP_0_PIN,
  117. #endif
  118. #if GET_TEMP_1_ADC() == 0
  119. TEMP_1_PIN,
  120. #endif
  121. #if GET_TEMP_2_ADC() == 0
  122. TEMP_2_PIN,
  123. #endif
  124. #if GET_TEMP_3_ADC() == 0
  125. TEMP_3_PIN,
  126. #endif
  127. #if GET_TEMP_4_ADC() == 0
  128. TEMP_4_PIN,
  129. #endif
  130. #if GET_TEMP_5_ADC() == 0
  131. TEMP_5_PIN,
  132. #endif
  133. #if GET_BED_ADC() == 0
  134. TEMP_BED_PIN,
  135. #endif
  136. #if GET_CHAMBER_ADC() == 0
  137. TEMP_CHAMBER_PIN,
  138. #endif
  139. #if GET_FILAMENT_WIDTH_ADC() == 0
  140. FILWIDTH_PIN,
  141. #endif
  142. #if GET_BUTTONS_ADC() == 0
  143. ADC_KEYPAD_PIN,
  144. #endif
  145. // ADC1 pins
  146. #if GET_TEMP_0_ADC() == 1
  147. TEMP_0_PIN,
  148. #endif
  149. #if GET_TEMP_1_ADC() == 1
  150. TEMP_1_PIN,
  151. #endif
  152. #if GET_TEMP_2_ADC() == 1
  153. TEMP_2_PIN,
  154. #endif
  155. #if GET_TEMP_3_ADC() == 1
  156. TEMP_3_PIN,
  157. #endif
  158. #if GET_TEMP_4_ADC() == 1
  159. TEMP_4_PIN,
  160. #endif
  161. #if GET_TEMP_5_ADC() == 1
  162. TEMP_5_PIN,
  163. #endif
  164. #if GET_BED_ADC() == 1
  165. TEMP_BED_PIN,
  166. #endif
  167. #if GET_CHAMBER_ADC() == 1
  168. TEMP_CHAMBER_PIN,
  169. #endif
  170. #if GET_FILAMENT_WIDTH_ADC() == 1
  171. FILWIDTH_PIN,
  172. #endif
  173. #if GET_BUTTONS_ADC() == 1
  174. ADC_KEYPAD_PIN,
  175. #endif
  176. };
  177. uint16_t HAL_adc_results[COUNT(adc_pins)];
  178. #if ADC0_IS_REQUIRED
  179. Adafruit_ZeroDMA adc0ProgramDMA,
  180. adc0ReadDMA;
  181. const HAL_DMA_DAC_Registers adc0_dma_regs_list[] = {
  182. #if GET_TEMP_0_ADC() == 0
  183. { PIN_TO_INPUTCTRL(TEMP_0_PIN) },
  184. #endif
  185. #if GET_TEMP_1_ADC() == 0
  186. { PIN_TO_INPUTCTRL(TEMP_1_PIN) },
  187. #endif
  188. #if GET_TEMP_2_ADC() == 0
  189. { PIN_TO_INPUTCTRL(TEMP_2_PIN) },
  190. #endif
  191. #if GET_TEMP_3_ADC() == 0
  192. { PIN_TO_INPUTCTRL(TEMP_3_PIN) },
  193. #endif
  194. #if GET_TEMP_4_ADC() == 0
  195. { PIN_TO_INPUTCTRL(TEMP_4_PIN) },
  196. #endif
  197. #if GET_TEMP_5_ADC() == 0
  198. { PIN_TO_INPUTCTRL(TEMP_5_PIN) },
  199. #endif
  200. #if GET_BED_ADC() == 0
  201. { PIN_TO_INPUTCTRL(TEMP_BED_PIN) },
  202. #endif
  203. #if GET_CHAMBER_ADC() == 0
  204. { PIN_TO_INPUTCTRL(TEMP_CHAMBER_PIN) },
  205. #endif
  206. #if GET_FILAMENT_WIDTH_ADC() == 0
  207. { PIN_TO_INPUTCTRL(FILWIDTH_PIN) },
  208. #endif
  209. #if GET_BUTTONS_ADC() == 0
  210. { PIN_TO_INPUTCTRL(ADC_KEYPAD_PIN) },
  211. #endif
  212. };
  213. #define ADC0_AINCOUNT COUNT(adc0_dma_regs_list)
  214. #endif // ADC0_IS_REQUIRED
  215. #if ADC1_IS_REQUIRED
  216. Adafruit_ZeroDMA adc1ProgramDMA,
  217. adc1ReadDMA;
  218. const HAL_DMA_DAC_Registers adc1_dma_regs_list[] = {
  219. #if GET_TEMP_0_ADC() == 1
  220. { PIN_TO_INPUTCTRL(TEMP_0_PIN) },
  221. #endif
  222. #if GET_TEMP_1_ADC() == 1
  223. { PIN_TO_INPUTCTRL(TEMP_1_PIN) },
  224. #endif
  225. #if GET_TEMP_2_ADC() == 1
  226. { PIN_TO_INPUTCTRL(TEMP_2_PIN) },
  227. #endif
  228. #if GET_TEMP_3_ADC() == 1
  229. { PIN_TO_INPUTCTRL(TEMP_3_PIN) },
  230. #endif
  231. #if GET_TEMP_4_ADC() == 1
  232. { PIN_TO_INPUTCTRL(TEMP_4_PIN) },
  233. #endif
  234. #if GET_TEMP_5_ADC() == 1
  235. { PIN_TO_INPUTCTRL(TEMP_5_PIN) },
  236. #endif
  237. #if GET_BED_ADC() == 1
  238. { PIN_TO_INPUTCTRL(TEMP_BED_PIN) },
  239. #endif
  240. #if GET_CHAMBER_ADC() == 1
  241. { PIN_TO_INPUTCTRL(TEMP_CHAMBER_PIN) },
  242. #endif
  243. #if GET_FILAMENT_WIDTH_ADC() == 1
  244. { PIN_TO_INPUTCTRL(FILWIDTH_PIN) },
  245. #endif
  246. #if GET_BUTTONS_ADC() == 1
  247. { PIN_TO_INPUTCTRL(ADC_KEYPAD_PIN) },
  248. #endif
  249. };
  250. #define ADC1_AINCOUNT COUNT(adc1_dma_regs_list)
  251. #endif // ADC1_IS_REQUIRED
  252. #endif // ADC_IS_REQUIRED
  253. // ------------------------
  254. // Private functions
  255. // ------------------------
  256. #if DMA_IS_REQUIRED
  257. void dma_init() {
  258. DmacDescriptor *descriptor;
  259. #if ADC0_IS_REQUIRED
  260. adc0ProgramDMA.setTrigger(ADC0_DMAC_ID_SEQ);
  261. adc0ProgramDMA.setAction(DMA_TRIGGER_ACTON_BEAT);
  262. adc0ProgramDMA.loop(true);
  263. if (adc0ProgramDMA.allocate() == DMA_STATUS_OK) {
  264. descriptor = adc0ProgramDMA.addDescriptor(
  265. (void *)adc0_dma_regs_list, // SRC
  266. (void *)&ADC0->DSEQDATA.reg, // DEST
  267. sizeof(adc0_dma_regs_list) / 4, // CNT
  268. DMA_BEAT_SIZE_WORD,
  269. true, // SRCINC
  270. false, // DSTINC
  271. DMA_ADDRESS_INCREMENT_STEP_SIZE_1, // STEPSIZE
  272. DMA_STEPSEL_SRC // STEPSEL
  273. );
  274. if (descriptor != nullptr)
  275. descriptor->BTCTRL.bit.EVOSEL = DMA_EVENT_OUTPUT_BEAT;
  276. adc0ProgramDMA.startJob();
  277. }
  278. adc0ReadDMA.setTrigger(ADC0_DMAC_ID_RESRDY);
  279. adc0ReadDMA.setAction(DMA_TRIGGER_ACTON_BEAT);
  280. adc0ReadDMA.loop(true);
  281. if (adc0ReadDMA.allocate() == DMA_STATUS_OK) {
  282. adc0ReadDMA.addDescriptor(
  283. (void *)&ADC0->RESULT.reg, // SRC
  284. &HAL_adc_results, // DEST
  285. ADC0_AINCOUNT, // CNT
  286. DMA_BEAT_SIZE_HWORD,
  287. false, // SRCINC
  288. true, // DSTINC
  289. DMA_ADDRESS_INCREMENT_STEP_SIZE_1, // STEPSIZE
  290. DMA_STEPSEL_DST // STEPSEL
  291. );
  292. adc0ReadDMA.startJob();
  293. }
  294. #endif
  295. #if ADC1_IS_REQUIRED
  296. adc1ProgramDMA.setTrigger(ADC1_DMAC_ID_SEQ);
  297. adc1ProgramDMA.setAction(DMA_TRIGGER_ACTON_BEAT);
  298. adc1ProgramDMA.loop(true);
  299. if (adc1ProgramDMA.allocate() == DMA_STATUS_OK) {
  300. descriptor = adc1ProgramDMA.addDescriptor(
  301. (void *)adc1_dma_regs_list, // SRC
  302. (void *)&ADC1->DSEQDATA.reg, // DEST
  303. sizeof(adc1_dma_regs_list) / 4, // CNT
  304. DMA_BEAT_SIZE_WORD,
  305. true, // SRCINC
  306. false, // DSTINC
  307. DMA_ADDRESS_INCREMENT_STEP_SIZE_1, // STEPSIZE
  308. DMA_STEPSEL_SRC // STEPSEL
  309. );
  310. if (descriptor != nullptr)
  311. descriptor->BTCTRL.bit.EVOSEL = DMA_EVENT_OUTPUT_BEAT;
  312. adc1ProgramDMA.startJob();
  313. }
  314. adc1ReadDMA.setTrigger(ADC1_DMAC_ID_RESRDY);
  315. adc1ReadDMA.setAction(DMA_TRIGGER_ACTON_BEAT);
  316. adc1ReadDMA.loop(true);
  317. if (adc1ReadDMA.allocate() == DMA_STATUS_OK) {
  318. adc1ReadDMA.addDescriptor(
  319. (void *)&ADC1->RESULT.reg, // SRC
  320. &HAL_adc_results[ADC0_AINCOUNT], // DEST
  321. ADC1_AINCOUNT, // CNT
  322. DMA_BEAT_SIZE_HWORD,
  323. false, // SRCINC
  324. true, // DSTINC
  325. DMA_ADDRESS_INCREMENT_STEP_SIZE_1, // STEPSIZE
  326. DMA_STEPSEL_DST // STEPSEL
  327. );
  328. adc1ReadDMA.startJob();
  329. }
  330. #endif
  331. DMAC->PRICTRL0.bit.RRLVLEN0 = true; // Activate round robin for DMA channels used by ADCs
  332. }
  333. #endif // DMA_IS_REQUIRED
  334. // ------------------------
  335. // Public functions
  336. // ------------------------
  337. // HAL initialization task
  338. void HAL_init(void) {
  339. #if DMA_IS_REQUIRED
  340. dma_init();
  341. #endif
  342. #if ENABLED(SDSUPPORT)
  343. #if SD_CONNECTION_IS(ONBOARD) && PIN_EXISTS(SD_DETECT) // SD_DETECT_PIN may be remove when NO_SD_HOST_DRIVE is not defined in configuration_adv
  344. SET_INPUT_PULLUP(SD_DETECT_PIN);
  345. #endif
  346. OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
  347. #endif
  348. }
  349. // HAL idle task
  350. /*
  351. void HAL_idletask(void) {
  352. }
  353. */
  354. void HAL_clear_reset_source(void) { }
  355. #pragma push_macro("WDT")
  356. #undef WDT // Required to be able to use '.bit.WDT'. Compiler wrongly replace struct field with WDT define
  357. uint8_t HAL_get_reset_source(void) {
  358. RSTC_RCAUSE_Type resetCause;
  359. resetCause.reg = REG_RSTC_RCAUSE;
  360. if (resetCause.bit.POR) return RST_POWER_ON;
  361. else if (resetCause.bit.EXT) return RST_EXTERNAL;
  362. else if (resetCause.bit.BODCORE || resetCause.bit.BODVDD) return RST_BROWN_OUT;
  363. else if (resetCause.bit.WDT) return RST_WATCHDOG;
  364. else if (resetCause.bit.SYST || resetCause.bit.NVM) return RST_SOFTWARE;
  365. else if (resetCause.bit.BACKUP) return RST_BACKUP;
  366. return 0;
  367. }
  368. #pragma pop_macro("WDT")
  369. extern "C" {
  370. void * _sbrk(int incr);
  371. extern unsigned int __bss_end__; // end of bss section
  372. }
  373. // Return free memory between end of heap (or end bss) and whatever is current
  374. int freeMemory() {
  375. int free_memory, heap_end = (int)_sbrk(0);
  376. return (int)&free_memory - (heap_end ? heap_end : (int)&__bss_end__);
  377. }
  378. // ------------------------
  379. // ADC
  380. // ------------------------
  381. void HAL_adc_init(void) {
  382. #if ADC_IS_REQUIRED
  383. memset(HAL_adc_results, 0xFF, sizeof(HAL_adc_results)); // Fill result with invalid values
  384. for (uint8_t pi = 0; pi < COUNT(adc_pins); ++pi)
  385. pinPeripheral(adc_pins[pi], PIO_ANALOG);
  386. for (uint8_t ai = FIRST_ADC; ai <= LAST_ADC; ++ai) {
  387. Adc* adc = ((Adc*[])ADC_INSTS)[ai];
  388. // ADC clock setup
  389. GCLK->PCHCTRL[ADC0_GCLK_ID + ai].bit.CHEN = false;
  390. SYNC(GCLK->PCHCTRL[ADC0_GCLK_ID + ai].bit.CHEN);
  391. GCLK->PCHCTRL[ADC0_GCLK_ID + ai].reg = GCLK_PCHCTRL_GEN_GCLK1 | GCLK_PCHCTRL_CHEN; // 48MHz startup code programmed
  392. SYNC(!GCLK->PCHCTRL[ADC0_GCLK_ID + ai].bit.CHEN);
  393. adc->CTRLA.bit.PRESCALER = ADC_CTRLA_PRESCALER_DIV32_Val; // 1.5MHZ adc clock
  394. // ADC setup
  395. // Preloaded data (fixed for all ADC instances hence not loaded by DMA)
  396. adc->REFCTRL.bit.REFSEL = ADC_REFCTRL_REFSEL_AREFA_Val; // VRefA pin
  397. SYNC(adc->SYNCBUSY.bit.REFCTRL);
  398. adc->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_10BIT_Val;
  399. SYNC(adc->SYNCBUSY.bit.CTRLB);
  400. adc->SAMPCTRL.bit.SAMPLEN = (6 - 1); // Sampling clocks
  401. // Registers loaded by DMA
  402. adc->DSEQCTRL.bit.INPUTCTRL = true;
  403. adc->DSEQCTRL.bit.AUTOSTART = true; // Start conversion after DMA sequence
  404. adc->CTRLA.bit.ENABLE = true; // Enable ADC
  405. SYNC(adc->SYNCBUSY.bit.ENABLE);
  406. }
  407. #endif // ADC_IS_REQUIRED
  408. }
  409. void HAL_adc_start_conversion(const uint8_t adc_pin) {
  410. #if ADC_IS_REQUIRED
  411. for (uint8_t pi = 0; pi < COUNT(adc_pins); ++pi) {
  412. if (adc_pin == adc_pins[pi]) {
  413. HAL_adc_result = HAL_adc_results[pi];
  414. return;
  415. }
  416. }
  417. #endif
  418. HAL_adc_result = 0xFFFF;
  419. }
  420. uint16_t HAL_adc_get_result(void) {
  421. return HAL_adc_result;
  422. }
  423. #endif // __SAMD51__