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

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