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.h 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. void HAL_init();
  29. #include <stdint.h>
  30. #include <stdarg.h>
  31. #include <algorithm>
  32. extern "C" volatile uint32_t _millis;
  33. #include "../shared/Marduino.h"
  34. #include "../shared/math_32bit.h"
  35. #include "../shared/HAL_SPI.h"
  36. #include "fastio.h"
  37. #include "watchdog.h"
  38. #include "MarlinSerial.h"
  39. #include <adc.h>
  40. #include <pinmapping.h>
  41. #include <CDCSerial.h>
  42. //
  43. // Default graphical display delays
  44. //
  45. #define CPU_ST7920_DELAY_1 DELAY_NS(600)
  46. #define CPU_ST7920_DELAY_2 DELAY_NS(750)
  47. #define CPU_ST7920_DELAY_3 DELAY_NS(750)
  48. typedef ForwardSerial1Class< decltype(UsbSerial) > DefaultSerial1;
  49. extern DefaultSerial1 USBSerial;
  50. #define _MSERIAL(X) MSerial##X
  51. #define MSERIAL(X) _MSERIAL(X)
  52. #if SERIAL_PORT == -1
  53. #define MYSERIAL1 USBSerial
  54. #elif WITHIN(SERIAL_PORT, 0, 3)
  55. #define MYSERIAL1 MSERIAL(SERIAL_PORT)
  56. #else
  57. #error "SERIAL_PORT must be from 0 to 3. You can also use -1 if the board supports Native USB."
  58. #endif
  59. #ifdef SERIAL_PORT_2
  60. #if SERIAL_PORT_2 == -1
  61. #define MYSERIAL2 USBSerial
  62. #elif WITHIN(SERIAL_PORT_2, 0, 3)
  63. #define MYSERIAL2 MSERIAL(SERIAL_PORT_2)
  64. #else
  65. #error "SERIAL_PORT_2 must be from 0 to 3. You can also use -1 if the board supports Native USB."
  66. #endif
  67. #endif
  68. #ifdef SERIAL_PORT_3
  69. #if SERIAL_PORT_3 == -1
  70. #define MYSERIAL3 USBSerial
  71. #elif WITHIN(SERIAL_PORT_3, 0, 3)
  72. #define MYSERIAL3 MSERIAL(SERIAL_PORT_3)
  73. #else
  74. #error "SERIAL_PORT_3 must be from 0 to 3. You can also use -1 if the board supports Native USB."
  75. #endif
  76. #endif
  77. #ifdef MMU2_SERIAL_PORT
  78. #if MMU2_SERIAL_PORT == -1
  79. #define MMU2_SERIAL USBSerial
  80. #elif WITHIN(MMU2_SERIAL_PORT, 0, 3)
  81. #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT)
  82. #else
  83. #error "MMU2_SERIAL_PORT must be from 0 to 3. You can also use -1 if the board supports Native USB."
  84. #endif
  85. #endif
  86. #ifdef LCD_SERIAL_PORT
  87. #if LCD_SERIAL_PORT == -1
  88. #define LCD_SERIAL USBSerial
  89. #elif WITHIN(LCD_SERIAL_PORT, 0, 3)
  90. #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
  91. #else
  92. #error "LCD_SERIAL_PORT must be from 0 to 3. You can also use -1 if the board supports Native USB."
  93. #endif
  94. #if HAS_DGUS_LCD
  95. #define SERIAL_GET_TX_BUFFER_FREE() MSerial0.available()
  96. #endif
  97. #endif
  98. //
  99. // Interrupts
  100. //
  101. #define CRITICAL_SECTION_START() uint32_t primask = __get_PRIMASK(); __disable_irq()
  102. #define CRITICAL_SECTION_END() if (!primask) __enable_irq()
  103. #define ISRS_ENABLED() (!__get_PRIMASK())
  104. #define ENABLE_ISRS() __enable_irq()
  105. #define DISABLE_ISRS() __disable_irq()
  106. //
  107. // Utility functions
  108. //
  109. #if GCC_VERSION <= 50000
  110. #pragma GCC diagnostic push
  111. #pragma GCC diagnostic ignored "-Wunused-function"
  112. #endif
  113. int freeMemory();
  114. #if GCC_VERSION <= 50000
  115. #pragma GCC diagnostic pop
  116. #endif
  117. //
  118. // ADC API
  119. //
  120. #define ADC_MEDIAN_FILTER_SIZE (23) // Higher values increase step delay (phase shift),
  121. // (ADC_MEDIAN_FILTER_SIZE + 1) / 2 sample step delay (12 samples @ 500Hz: 24ms phase shift)
  122. // Memory usage per ADC channel (bytes): (6 * ADC_MEDIAN_FILTER_SIZE) + 16
  123. // 8 * ((6 * 23) + 16 ) = 1232 Bytes for 8 channels
  124. #define ADC_LOWPASS_K_VALUE (2) // Higher values increase rise time
  125. // Rise time sample delays for 100% signal convergence on full range step
  126. // (1 : 13, 2 : 32, 3 : 67, 4 : 139, 5 : 281, 6 : 565, 7 : 1135, 8 : 2273)
  127. // K = 6, 565 samples, 500Hz sample rate, 1.13s convergence on full range step
  128. // Memory usage per ADC channel (bytes): 4 (32 Bytes for 8 channels)
  129. #define HAL_ADC_VREF 3.3 // ADC voltage reference
  130. #define HAL_ADC_RESOLUTION 12 // 15 bit maximum, raw temperature is stored as int16_t
  131. #define HAL_ADC_FILTERED // Disable oversampling done in Marlin as ADC values already filtered in HAL
  132. using FilteredADC = LPC176x::ADC<ADC_LOWPASS_K_VALUE, ADC_MEDIAN_FILTER_SIZE>;
  133. extern uint32_t HAL_adc_reading;
  134. [[gnu::always_inline]] inline void HAL_start_adc(const pin_t pin) {
  135. HAL_adc_reading = FilteredADC::read(pin) >> (16 - HAL_ADC_RESOLUTION); // returns 16bit value, reduce to required bits
  136. }
  137. [[gnu::always_inline]] inline uint16_t HAL_read_adc() {
  138. return HAL_adc_reading;
  139. }
  140. #define HAL_adc_init()
  141. #define HAL_ANALOG_SELECT(pin) FilteredADC::enable_channel(pin)
  142. #define HAL_START_ADC(pin) HAL_start_adc(pin)
  143. #define HAL_READ_ADC() HAL_read_adc()
  144. #define HAL_ADC_READY() (true)
  145. // Test whether the pin is valid
  146. constexpr bool VALID_PIN(const pin_t pin) {
  147. return LPC176x::pin_is_valid(pin);
  148. }
  149. // Get the analog index for a digital pin
  150. constexpr int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t pin) {
  151. return (LPC176x::pin_is_valid(pin) && LPC176x::pin_has_adc(pin)) ? pin : -1;
  152. }
  153. // Return the index of a pin number
  154. constexpr int16_t GET_PIN_MAP_INDEX(const pin_t pin) {
  155. return LPC176x::pin_index(pin);
  156. }
  157. // Get the pin number at the given index
  158. constexpr pin_t GET_PIN_MAP_PIN(const int16_t index) {
  159. return LPC176x::pin_index(index);
  160. }
  161. // Parse a G-code word into a pin index
  162. int16_t PARSED_PIN_INDEX(const char code, const int16_t dval);
  163. // P0.6 thru P0.9 are for the onboard SD card
  164. #define HAL_SENSITIVE_PINS P0_06, P0_07, P0_08, P0_09,
  165. #define HAL_IDLETASK 1
  166. void HAL_idletask();
  167. #define PLATFORM_M997_SUPPORT
  168. void flashFirmware(const int16_t);
  169. #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
  170. /**
  171. * set_pwm_frequency
  172. * Set the frequency of the timer corresponding to the provided pin
  173. * All Hardware PWM pins run at the same frequency and all
  174. * Software PWM pins run at the same frequency
  175. */
  176. void set_pwm_frequency(const pin_t pin, int f_desired);
  177. /**
  178. * set_pwm_duty
  179. * Set the PWM duty cycle of the provided pin to the provided value
  180. * Optionally allows inverting the duty cycle [default = false]
  181. * Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
  182. */
  183. void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
  184. // Reset source
  185. void HAL_clear_reset_source(void);
  186. uint8_t HAL_get_reset_source(void);
  187. void HAL_reboot();