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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. /**
  25. * HAL for stm32duino.com based on Libmaple and compatible (STM32F1)
  26. */
  27. #define CPU_32_BIT
  28. #include "../../core/macros.h"
  29. #include "../shared/Marduino.h"
  30. #include "../shared/math_32bit.h"
  31. #include "../shared/HAL_SPI.h"
  32. #include "fastio.h"
  33. #include "watchdog.h"
  34. #include "timers.h"
  35. #include <stdint.h>
  36. #include <util/atomic.h>
  37. #include "../../inc/MarlinConfigPre.h"
  38. // ------------------------
  39. // Defines
  40. // ------------------------
  41. #ifdef SERIAL_USB
  42. #define UsbSerial Serial
  43. #define MSerial1 Serial1
  44. #define MSerial2 Serial2
  45. #define MSerial3 Serial3
  46. #define MSerial4 Serial4
  47. #define MSerial5 Serial5
  48. #else
  49. extern USBSerial SerialUSB;
  50. #define UsbSerial SerialUSB
  51. #define MSerial1 Serial
  52. #define MSerial2 Serial1
  53. #define MSerial3 Serial2
  54. #define MSerial4 Serial3
  55. #define MSerial5 Serial4
  56. #endif
  57. #if !WITHIN(SERIAL_PORT, -1, 5)
  58. #error "SERIAL_PORT must be from -1 to 5"
  59. #endif
  60. #if SERIAL_PORT == -1
  61. #define MYSERIAL0 UsbSerial
  62. #elif SERIAL_PORT == 0
  63. #error "Serial port 0 does not exist"
  64. #elif SERIAL_PORT == 1
  65. #define MYSERIAL0 MSerial1
  66. #elif SERIAL_PORT == 2
  67. #define MYSERIAL0 MSerial2
  68. #elif SERIAL_PORT == 3
  69. #define MYSERIAL0 MSerial3
  70. #elif SERIAL_PORT == 4
  71. #define MYSERIAL0 MSerial4
  72. #elif SERIAL_PORT == 5
  73. #define MYSERIAL0 MSerial5
  74. #endif
  75. #ifdef SERIAL_PORT_2
  76. #if !WITHIN(SERIAL_PORT_2, -1, 5)
  77. #error "SERIAL_PORT_2 must be from -1 to 5"
  78. #elif SERIAL_PORT_2 == SERIAL_PORT
  79. #error "SERIAL_PORT_2 must be different than SERIAL_PORT"
  80. #endif
  81. #define NUM_SERIAL 2
  82. #if SERIAL_PORT_2 == -1
  83. #define MYSERIAL1 UsbSerial
  84. #elif SERIAL_PORT_2 == 0
  85. #error "Serial port 0 does not exist"
  86. #elif SERIAL_PORT_2 == 1
  87. #define MYSERIAL1 MSerial1
  88. #elif SERIAL_PORT_2 == 2
  89. #define MYSERIAL1 MSerial2
  90. #elif SERIAL_PORT_2 == 3
  91. #define MYSERIAL1 MSerial3
  92. #elif SERIAL_PORT_2 == 4
  93. #define MYSERIAL1 MSerial4
  94. #elif SERIAL_PORT_2 == 5
  95. #define MYSERIAL1 MSerial5
  96. #endif
  97. #else
  98. #define NUM_SERIAL 1
  99. #endif
  100. // Set interrupt grouping for this MCU
  101. void HAL_init(void);
  102. /**
  103. * TODO: review this to return 1 for pins that are not analog input
  104. */
  105. #ifndef analogInputToDigitalPin
  106. #define analogInputToDigitalPin(p) (p)
  107. #endif
  108. #ifndef digitalPinHasPWM
  109. #define digitalPinHasPWM(P) (PIN_MAP[P].timer_device != nullptr)
  110. #endif
  111. #define CRITICAL_SECTION_START uint32_t primask = __get_primask(); (void)__iCliRetVal()
  112. #define CRITICAL_SECTION_END if (!primask) (void)__iSeiRetVal()
  113. #define ISRS_ENABLED() (!__get_primask())
  114. #define ENABLE_ISRS() ((void)__iSeiRetVal())
  115. #define DISABLE_ISRS() ((void)__iCliRetVal())
  116. // On AVR this is in math.h?
  117. #define square(x) ((x)*(x))
  118. #ifndef strncpy_P
  119. #define strncpy_P(dest, src, num) strncpy((dest), (src), (num))
  120. #endif
  121. // Fix bug in pgm_read_ptr
  122. #undef pgm_read_ptr
  123. #define pgm_read_ptr(addr) (*(addr))
  124. #define RST_POWER_ON 1
  125. #define RST_EXTERNAL 2
  126. #define RST_BROWN_OUT 4
  127. #define RST_WATCHDOG 8
  128. #define RST_JTAG 16
  129. #define RST_SOFTWARE 32
  130. #define RST_BACKUP 64
  131. // ------------------------
  132. // Types
  133. // ------------------------
  134. typedef int8_t pin_t;
  135. // ------------------------
  136. // Public Variables
  137. // ------------------------
  138. // Result of last ADC conversion
  139. extern uint16_t HAL_adc_result;
  140. // ------------------------
  141. // Public functions
  142. // ------------------------
  143. // Disable interrupts
  144. #define cli() noInterrupts()
  145. // Enable interrupts
  146. #define sei() interrupts()
  147. // Memory related
  148. #define __bss_end __bss_end__
  149. // Clear reset reason
  150. void HAL_clear_reset_source(void);
  151. // Reset reason
  152. uint8_t HAL_get_reset_source(void);
  153. void _delay_ms(const int delay);
  154. #pragma GCC diagnostic push
  155. #pragma GCC diagnostic ignored "-Wunused-function"
  156. /*
  157. extern "C" {
  158. int freeMemory(void);
  159. }
  160. */
  161. extern "C" char* _sbrk(int incr);
  162. /*
  163. static int freeMemory() {
  164. volatile int top;
  165. top = (int)((char*)&top - reinterpret_cast<char*>(_sbrk(0)));
  166. return top;
  167. }
  168. */
  169. static int freeMemory() {
  170. volatile char top;
  171. return &top - reinterpret_cast<char*>(_sbrk(0));
  172. }
  173. #pragma GCC diagnostic pop
  174. //
  175. // EEPROM
  176. //
  177. /**
  178. * TODO: Write all this EEPROM stuff. Can emulate EEPROM in flash as last resort.
  179. * Wire library should work for i2c EEPROMs.
  180. */
  181. void eeprom_write_byte(uint8_t *pos, unsigned char value);
  182. uint8_t eeprom_read_byte(uint8_t *pos);
  183. void eeprom_read_block(void *__dst, const void *__src, size_t __n);
  184. void eeprom_update_block(const void *__src, void *__dst, size_t __n);
  185. //
  186. // ADC
  187. //
  188. #define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT_ANALOG);
  189. void HAL_adc_init(void);
  190. #define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
  191. #define HAL_READ_ADC() HAL_adc_result
  192. #define HAL_ADC_READY() true
  193. void HAL_adc_start_conversion(const uint8_t adc_pin);
  194. uint16_t HAL_adc_get_result(void);
  195. uint16_t analogRead(pin_t pin); // need HAL_ANALOG_SELECT() first
  196. void analogWrite(pin_t pin, int pwm_val8); // PWM only! mul by 257 in maple!?
  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. #define JTAG_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY)
  201. #define JTAGSWD_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_NONE)