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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #pragma once
  24. #define CPU_32_BIT
  25. #include "../../inc/MarlinConfigPre.h"
  26. #include "../shared/Marduino.h"
  27. #include "../shared/math_32bit.h"
  28. #include "../shared/HAL_SPI.h"
  29. #include "fastio.h"
  30. #include "timers.h"
  31. #include "watchdog.h"
  32. #include <stdint.h>
  33. #if defined(STM32F4) && USBCON
  34. #include <USBSerial.h>
  35. #endif
  36. // ------------------------
  37. // Defines
  38. // ------------------------
  39. // Serial override
  40. //extern HalSerial usb_serial;
  41. #if defined(STM32F4) && SERIAL_PORT == 0
  42. #error "SERIAL_PORT cannot be 0. (Port 0 does not exist.) Please update your configuration."
  43. #elif SERIAL_PORT == -1
  44. #define MYSERIAL0 SerialUSB
  45. #elif SERIAL_PORT == 1
  46. #define MYSERIAL0 SerialUART1
  47. #elif SERIAL_PORT == 2
  48. #define MYSERIAL0 SerialUART2
  49. #elif SERIAL_PORT == 3
  50. #define MYSERIAL0 SerialUART3
  51. #elif SERIAL_PORT == 4
  52. #define MYSERIAL0 SerialUART4
  53. #elif SERIAL_PORT == 5
  54. #define MYSERIAL0 SerialUART5
  55. #elif SERIAL_PORT == 6
  56. #define MYSERIAL0 SerialUART6
  57. #else
  58. #error "SERIAL_PORT must be from -1 to 6. Please update your configuration."
  59. #endif
  60. #ifdef SERIAL_PORT_2
  61. #if defined(STM32F4) && SERIAL_PORT_2 == 0
  62. #error "SERIAL_PORT_2 cannot be 0. (Port 0 does not exist.) Please update your configuration."
  63. #elif SERIAL_PORT_2 == SERIAL_PORT
  64. #error "SERIAL_PORT_2 must be different than SERIAL_PORT. Please update your configuration."
  65. #elif SERIAL_PORT_2 == -1
  66. #define MYSERIAL1 SerialUSB
  67. #elif SERIAL_PORT_2 == 1
  68. #define MYSERIAL1 SerialUART1
  69. #elif SERIAL_PORT_2 == 2
  70. #define MYSERIAL1 SerialUART2
  71. #elif SERIAL_PORT_2 == 3
  72. #define MYSERIAL1 SerialUART3
  73. #elif SERIAL_PORT_2 == 4
  74. #define MYSERIAL1 SerialUART4
  75. #elif SERIAL_PORT_2 == 5
  76. #define MYSERIAL1 SerialUART5
  77. #elif SERIAL_PORT_2 == 6
  78. #define MYSERIAL1 SerialUART6
  79. #else
  80. #error "SERIAL_PORT_2 must be from -1 to 6. Please update your configuration."
  81. #endif
  82. #define NUM_SERIAL 2
  83. #else
  84. #define NUM_SERIAL 1
  85. #endif
  86. #ifdef DGUS_SERIAL_PORT
  87. #if defined(STM32F4) && DGUS_SERIAL_PORT == 0
  88. #error "DGUS_SERIAL_PORT cannot be 0. (Port 0 does not exist.) Please update your configuration."
  89. #elif DGUS_SERIAL_PORT == SERIAL_PORT
  90. #error "DGUS_SERIAL_PORT must be different than SERIAL_PORT. Please update your configuration."
  91. #elif defined(SERIAL_PORT_2) && DGUS_SERIAL_PORT == SERIAL_PORT_2
  92. #error "DGUS_SERIAL_PORT must be different than SERIAL_PORT_2. Please update your configuration."
  93. #elif DGUS_SERIAL_PORT == -1
  94. #define DGUS_SERIAL SerialUSB
  95. #elif DGUS_SERIAL_PORT == 1
  96. #define DGUS_SERIAL SerialUART1
  97. #elif DGUS_SERIAL_PORT == 2
  98. #define DGUS_SERIAL SerialUART2
  99. #elif DGUS_SERIAL_PORT == 3
  100. #define DGUS_SERIAL SerialUART3
  101. #elif DGUS_SERIAL_PORT == 4
  102. #define DGUS_SERIAL SerialUART4
  103. #elif DGUS_SERIAL_PORT == 5
  104. #define DGUS_SERIAL SerialUART5
  105. #elif DGUS_SERIAL_PORT == 6
  106. #define DGUS_SERIAL SerialUART6
  107. #else
  108. #error "DGUS_SERIAL_PORT must be from -1 to 6. Please update your configuration."
  109. #endif
  110. #endif
  111. /**
  112. * TODO: review this to return 1 for pins that are not analog input
  113. */
  114. #ifndef analogInputToDigitalPin
  115. #define analogInputToDigitalPin(p) (p)
  116. #endif
  117. #define CRITICAL_SECTION_START uint32_t primask = __get_PRIMASK(); __disable_irq()
  118. #define CRITICAL_SECTION_END if (!primask) __enable_irq()
  119. #define ISRS_ENABLED() (!__get_PRIMASK())
  120. #define ENABLE_ISRS() __enable_irq()
  121. #define DISABLE_ISRS() __disable_irq()
  122. #define cli() __disable_irq()
  123. #define sei() __enable_irq()
  124. // On AVR this is in math.h?
  125. #define square(x) ((x)*(x))
  126. #ifndef strncpy_P
  127. #define strncpy_P(dest, src, num) strncpy((dest), (src), (num))
  128. #endif
  129. // Fix bug in pgm_read_ptr
  130. #undef pgm_read_ptr
  131. #define pgm_read_ptr(addr) (*(addr))
  132. // ------------------------
  133. // Types
  134. // ------------------------
  135. typedef int8_t pin_t;
  136. #ifdef STM32F4
  137. #define HAL_SERVO_LIB libServo
  138. #endif
  139. // ------------------------
  140. // Public Variables
  141. // ------------------------
  142. // Result of last ADC conversion
  143. extern uint16_t HAL_adc_result;
  144. // ------------------------
  145. // Public functions
  146. // ------------------------
  147. // Memory related
  148. #define __bss_end __bss_end__
  149. inline void HAL_init() {}
  150. // Clear reset reason
  151. void HAL_clear_reset_source();
  152. // Reset reason
  153. uint8_t HAL_get_reset_source();
  154. void _delay_ms(const int delay);
  155. /*
  156. extern "C" {
  157. int freeMemory();
  158. }
  159. */
  160. extern "C" char* _sbrk(int incr);
  161. /*
  162. int freeMemory() {
  163. volatile int top;
  164. top = (int)((char*)&top - reinterpret_cast<char*>(_sbrk(0)));
  165. return top;
  166. }
  167. */
  168. #pragma GCC diagnostic push
  169. #pragma GCC diagnostic ignored "-Wunused-function"
  170. static inline int freeMemory() {
  171. volatile char top;
  172. return &top - reinterpret_cast<char*>(_sbrk(0));
  173. }
  174. #pragma GCC diagnostic pop
  175. //
  176. // EEPROM
  177. //
  178. /**
  179. * TODO: Write all this EEPROM stuff. Can emulate EEPROM in flash as last resort.
  180. * Wire library should work for i2c EEPROMs.
  181. */
  182. void eeprom_write_byte(uint8_t *pos, unsigned char value);
  183. uint8_t eeprom_read_byte(uint8_t *pos);
  184. void eeprom_read_block (void *__dst, const void *__src, size_t __n);
  185. void eeprom_update_block (const void *__src, void *__dst, size_t __n);
  186. //
  187. // ADC
  188. //
  189. #define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT)
  190. inline void HAL_adc_init() {}
  191. #define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
  192. #define HAL_ADC_RESOLUTION 10
  193. #define HAL_READ_ADC() HAL_adc_result
  194. #define HAL_ADC_READY() true
  195. void HAL_adc_start_conversion(const uint8_t adc_pin);
  196. uint16_t HAL_adc_get_result();
  197. #define GET_PIN_MAP_PIN(index) index
  198. #define GET_PIN_MAP_INDEX(pin) pin
  199. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  200. #ifdef STM32F4
  201. #define JTAG_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY)
  202. #define JTAGSWD_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_NONE)
  203. #endif