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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2020 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. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * HAL_LPC1768/HAL.h
  25. * Hardware Abstraction Layer for NXP LPC1768
  26. */
  27. #define CPU_32_BIT
  28. #include <stdint.h>
  29. #include <stdarg.h>
  30. #include <algorithm>
  31. extern "C" volatile uint32_t _millis;
  32. #include "../shared/Marduino.h"
  33. #include "../shared/math_32bit.h"
  34. #include "../shared/HAL_SPI.h"
  35. #include "fastio.h"
  36. #include "MarlinSerial.h"
  37. #include <adc.h>
  38. #include <pinmapping.h>
  39. #include <CDCSerial.h>
  40. // ------------------------
  41. // Serial ports
  42. // ------------------------
  43. typedef ForwardSerial1Class< decltype(UsbSerial) > DefaultSerial1;
  44. extern DefaultSerial1 USBSerial;
  45. #define _MSERIAL(X) MSerial##X
  46. #define MSERIAL(X) _MSERIAL(X)
  47. #if SERIAL_PORT == -1
  48. #define MYSERIAL1 USBSerial
  49. #elif WITHIN(SERIAL_PORT, 0, 3)
  50. #define MYSERIAL1 MSERIAL(SERIAL_PORT)
  51. #else
  52. #error "SERIAL_PORT must be from 0 to 3. You can also use -1 if the board supports Native USB."
  53. #endif
  54. #ifdef SERIAL_PORT_2
  55. #if SERIAL_PORT_2 == -1
  56. #define MYSERIAL2 USBSerial
  57. #elif WITHIN(SERIAL_PORT_2, 0, 3)
  58. #define MYSERIAL2 MSERIAL(SERIAL_PORT_2)
  59. #else
  60. #error "SERIAL_PORT_2 must be from 0 to 3. You can also use -1 if the board supports Native USB."
  61. #endif
  62. #endif
  63. #ifdef SERIAL_PORT_3
  64. #if SERIAL_PORT_3 == -1
  65. #define MYSERIAL3 USBSerial
  66. #elif WITHIN(SERIAL_PORT_3, 0, 3)
  67. #define MYSERIAL3 MSERIAL(SERIAL_PORT_3)
  68. #else
  69. #error "SERIAL_PORT_3 must be from 0 to 3. You can also use -1 if the board supports Native USB."
  70. #endif
  71. #endif
  72. #ifdef MMU2_SERIAL_PORT
  73. #if MMU2_SERIAL_PORT == -1
  74. #define MMU2_SERIAL USBSerial
  75. #elif WITHIN(MMU2_SERIAL_PORT, 0, 3)
  76. #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT)
  77. #else
  78. #error "MMU2_SERIAL_PORT must be from 0 to 3. You can also use -1 if the board supports Native USB."
  79. #endif
  80. #endif
  81. #ifdef LCD_SERIAL_PORT
  82. #if LCD_SERIAL_PORT == -1
  83. #define LCD_SERIAL USBSerial
  84. #elif WITHIN(LCD_SERIAL_PORT, 0, 3)
  85. #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
  86. #else
  87. #error "LCD_SERIAL_PORT must be from 0 to 3. You can also use -1 if the board supports Native USB."
  88. #endif
  89. #if HAS_DGUS_LCD
  90. #define SERIAL_GET_TX_BUFFER_FREE() LCD_SERIAL.available()
  91. #endif
  92. #endif
  93. //
  94. // Interrupts
  95. //
  96. #define CRITICAL_SECTION_START() const bool irqon = !__get_PRIMASK(); __disable_irq()
  97. #define CRITICAL_SECTION_END() if (irqon) __enable_irq()
  98. //
  99. // ADC
  100. //
  101. #define ADC_MEDIAN_FILTER_SIZE (23) // Higher values increase step delay (phase shift),
  102. // (ADC_MEDIAN_FILTER_SIZE + 1) / 2 sample step delay (12 samples @ 500Hz: 24ms phase shift)
  103. // Memory usage per ADC channel (bytes): (6 * ADC_MEDIAN_FILTER_SIZE) + 16
  104. // 8 * ((6 * 23) + 16 ) = 1232 Bytes for 8 channels
  105. #define ADC_LOWPASS_K_VALUE (2) // Higher values increase rise time
  106. // Rise time sample delays for 100% signal convergence on full range step
  107. // (1 : 13, 2 : 32, 3 : 67, 4 : 139, 5 : 281, 6 : 565, 7 : 1135, 8 : 2273)
  108. // K = 6, 565 samples, 500Hz sample rate, 1.13s convergence on full range step
  109. // Memory usage per ADC channel (bytes): 4 (32 Bytes for 8 channels)
  110. #define HAL_ADC_VREF 3.3 // ADC voltage reference
  111. #define HAL_ADC_RESOLUTION 12 // 15 bit maximum, raw temperature is stored as int16_t
  112. #define HAL_ADC_FILTERED // Disable oversampling done in Marlin as ADC values already filtered in HAL
  113. //
  114. // Pin Mapping for M42, M43, M226
  115. //
  116. // Test whether the pin is valid
  117. constexpr bool VALID_PIN(const pin_t pin) {
  118. return LPC176x::pin_is_valid(pin);
  119. }
  120. // Get the analog index for a digital pin
  121. constexpr int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t pin) {
  122. return (LPC176x::pin_is_valid(pin) && LPC176x::pin_has_adc(pin)) ? pin : -1;
  123. }
  124. // Return the index of a pin number
  125. constexpr int16_t GET_PIN_MAP_INDEX(const pin_t pin) {
  126. return LPC176x::pin_index(pin);
  127. }
  128. // Get the pin number at the given index
  129. constexpr pin_t GET_PIN_MAP_PIN(const int16_t index) {
  130. return LPC176x::pin_index(index);
  131. }
  132. // Parse a G-code word into a pin index
  133. int16_t PARSED_PIN_INDEX(const char code, const int16_t dval);
  134. // P0.6 thru P0.9 are for the onboard SD card
  135. #define HAL_SENSITIVE_PINS P0_06, P0_07, P0_08, P0_09,
  136. // ------------------------
  137. // Defines
  138. // ------------------------
  139. #define PLATFORM_M997_SUPPORT
  140. void flashFirmware(const int16_t);
  141. #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
  142. // Default graphical display delays
  143. #define CPU_ST7920_DELAY_1 600
  144. #define CPU_ST7920_DELAY_2 750
  145. #define CPU_ST7920_DELAY_3 750
  146. // ------------------------
  147. // Free Memory Accessor
  148. // ------------------------
  149. #pragma GCC diagnostic push
  150. #if GCC_VERSION <= 50000
  151. #pragma GCC diagnostic ignored "-Wunused-function"
  152. #endif
  153. int freeMemory();
  154. #pragma GCC diagnostic pop
  155. // ------------------------
  156. // MarlinHAL Class
  157. // ------------------------
  158. class MarlinHAL {
  159. public:
  160. // Earliest possible init, before setup()
  161. MarlinHAL() {}
  162. static void init(); // Called early in setup()
  163. static void init_board() {} // Called less early in setup()
  164. static void reboot(); // Restart the firmware from 0x0
  165. // Interrupts
  166. static bool isr_state() { return !__get_PRIMASK(); }
  167. static void isr_on() { __enable_irq(); }
  168. static void isr_off() { __disable_irq(); }
  169. static void delay_ms(const int ms) { _delay_ms(ms); }
  170. // Watchdog
  171. static void watchdog_init() IF_DISABLED(USE_WATCHDOG, {});
  172. static void watchdog_refresh() IF_DISABLED(USE_WATCHDOG, {});
  173. static bool watchdog_timed_out() IF_DISABLED(USE_WATCHDOG, { return false; });
  174. static void watchdog_clear_timeout_flag() IF_DISABLED(USE_WATCHDOG, {});
  175. // Tasks, called from idle()
  176. static void idletask();
  177. // Reset
  178. static uint8_t get_reset_source();
  179. static void clear_reset_source();
  180. // Free SRAM
  181. static int freeMemory() { return ::freeMemory(); }
  182. //
  183. // ADC Methods
  184. //
  185. using FilteredADC = LPC176x::ADC<ADC_LOWPASS_K_VALUE, ADC_MEDIAN_FILTER_SIZE>;
  186. // Called by Temperature::init once at startup
  187. static void adc_init() {}
  188. // Called by Temperature::init for each sensor at startup
  189. static void adc_enable(const pin_t pin) {
  190. FilteredADC::enable_channel(pin);
  191. }
  192. // Begin ADC sampling on the given pin. Called from Temperature::isr!
  193. static uint32_t adc_result;
  194. static void adc_start(const pin_t pin) {
  195. adc_result = FilteredADC::read(pin) >> (16 - HAL_ADC_RESOLUTION); // returns 16bit value, reduce to required bits
  196. }
  197. // Is the ADC ready for reading?
  198. static bool adc_ready() { return true; }
  199. // The current value of the ADC register
  200. static uint16_t adc_value() { return uint16_t(adc_result); }
  201. /**
  202. * Set the PWM duty cycle for the pin to the given value.
  203. * Optionally invert the duty cycle [default = false]
  204. * Optionally change the scale of the provided value to enable finer PWM duty control [default = 255]
  205. */
  206. static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
  207. /**
  208. * Set the frequency of the timer corresponding to the provided pin
  209. * All Hardware PWM pins will run at the same frequency and
  210. * All Software PWM pins will run at the same frequency
  211. */
  212. static void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
  213. };