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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #ifndef vsnprintf_P
  29. #define vsnprintf_P vsnprintf
  30. #endif
  31. // --------------------------------------------------------------------------
  32. // Includes
  33. // --------------------------------------------------------------------------
  34. #include "../../core/macros.h"
  35. #include "../shared/Marduino.h"
  36. #include "../shared/math_32bit.h"
  37. #include "../shared/HAL_SPI.h"
  38. #include "fastio_STM32F1.h"
  39. #include "watchdog_STM32F1.h"
  40. #include "HAL_timers_STM32F1.h"
  41. #include <stdint.h>
  42. #include <util/atomic.h>
  43. #include "../../inc/MarlinConfigPre.h"
  44. // --------------------------------------------------------------------------
  45. // Defines
  46. // --------------------------------------------------------------------------
  47. #ifdef SERIAL_USB
  48. #define UsbSerial Serial
  49. #define MSerial1 Serial1
  50. #define MSerial2 Serial2
  51. #define MSerial3 Serial3
  52. #define MSerial4 Serial4
  53. #define MSerial5 Serial5
  54. #else
  55. extern USBSerial SerialUSB;
  56. #define UsbSerial SerialUSB
  57. #define MSerial1 Serial
  58. #define MSerial2 Serial1
  59. #define MSerial3 Serial2
  60. #define MSerial4 Serial3
  61. #define MSerial5 Serial4
  62. #endif
  63. #if !WITHIN(SERIAL_PORT, -1, 5)
  64. #error "SERIAL_PORT must be from -1 to 5"
  65. #endif
  66. #if SERIAL_PORT == -1
  67. #define MYSERIAL0 UsbSerial
  68. #elif SERIAL_PORT == 0
  69. #error "Serial port 0 does not exist"
  70. #elif SERIAL_PORT == 1
  71. #define MYSERIAL0 MSerial1
  72. #elif SERIAL_PORT == 2
  73. #define MYSERIAL0 MSerial2
  74. #elif SERIAL_PORT == 3
  75. #define MYSERIAL0 MSerial3
  76. #elif SERIAL_PORT == 4
  77. #define MYSERIAL0 MSerial4
  78. #elif SERIAL_PORT == 5
  79. #define MYSERIAL0 MSerial5
  80. #endif
  81. #ifdef SERIAL_PORT_2
  82. #if !WITHIN(SERIAL_PORT_2, -1, 5)
  83. #error "SERIAL_PORT_2 must be from -1 to 5"
  84. #elif SERIAL_PORT_2 == SERIAL_PORT
  85. #error "SERIAL_PORT_2 must be different than SERIAL_PORT"
  86. #endif
  87. #define NUM_SERIAL 2
  88. #if SERIAL_PORT_2 == -1
  89. #define MYSERIAL1 UsbSerial
  90. #elif SERIAL_PORT_2 == 0
  91. #error "Serial port 0 does not exist"
  92. #elif SERIAL_PORT_2 == 1
  93. #define MYSERIAL1 MSerial1
  94. #elif SERIAL_PORT_2 == 2
  95. #define MYSERIAL1 MSerial2
  96. #elif SERIAL_PORT_2 == 3
  97. #define MYSERIAL1 MSerial3
  98. #elif SERIAL_PORT_2 == 4
  99. #define MYSERIAL1 MSerial4
  100. #elif SERIAL_PORT_2 == 5
  101. #define MYSERIAL1 MSerial5
  102. #endif
  103. #else
  104. #define NUM_SERIAL 1
  105. #endif
  106. // Use HAL_init() to set interrupt grouping.
  107. #define HAL_INIT
  108. void HAL_init();
  109. /**
  110. * TODO: review this to return 1 for pins that are not analog input
  111. */
  112. #ifndef analogInputToDigitalPin
  113. #define analogInputToDigitalPin(p) (p)
  114. #endif
  115. #ifndef digitalPinHasPWM
  116. #define digitalPinHasPWM(P) (PIN_MAP[P].timer_device != nullptr)
  117. #endif
  118. #define CRITICAL_SECTION_START uint32_t primask = __get_primask(); (void)__iCliRetVal()
  119. #define CRITICAL_SECTION_END if (!primask) (void)__iSeiRetVal()
  120. #define ISRS_ENABLED() (!__get_primask())
  121. #define ENABLE_ISRS() ((void)__iSeiRetVal())
  122. #define DISABLE_ISRS() ((void)__iCliRetVal())
  123. // On AVR this is in math.h?
  124. #define square(x) ((x)*(x))
  125. #ifndef strncpy_P
  126. #define strncpy_P(dest, src, num) strncpy((dest), (src), (num))
  127. #endif
  128. // Fix bug in pgm_read_ptr
  129. #undef pgm_read_ptr
  130. #define pgm_read_ptr(addr) (*(addr))
  131. #define RST_POWER_ON 1
  132. #define RST_EXTERNAL 2
  133. #define RST_BROWN_OUT 4
  134. #define RST_WATCHDOG 8
  135. #define RST_JTAG 16
  136. #define RST_SOFTWARE 32
  137. #define RST_BACKUP 64
  138. // --------------------------------------------------------------------------
  139. // Types
  140. // --------------------------------------------------------------------------
  141. typedef int8_t pin_t;
  142. // --------------------------------------------------------------------------
  143. // Public Variables
  144. // --------------------------------------------------------------------------
  145. /** result of last ADC conversion */
  146. extern uint16_t HAL_adc_result;
  147. // --------------------------------------------------------------------------
  148. // Public functions
  149. // --------------------------------------------------------------------------
  150. // Disable interrupts
  151. #define cli() noInterrupts()
  152. // Enable interrupts
  153. #define sei() interrupts()
  154. // Memory related
  155. #define __bss_end __bss_end__
  156. /** clear reset reason */
  157. void HAL_clear_reset_source(void);
  158. /** reset reason */
  159. uint8_t HAL_get_reset_source(void);
  160. void _delay_ms(const int delay);
  161. /*
  162. extern "C" {
  163. int freeMemory(void);
  164. }
  165. */
  166. extern "C" char* _sbrk(int incr);
  167. /*
  168. static int freeMemory() {
  169. volatile int top;
  170. top = (int)((char*)&top - reinterpret_cast<char*>(_sbrk(0)));
  171. return top;
  172. }
  173. */
  174. #pragma GCC diagnostic push
  175. #pragma GCC diagnostic ignored "-Wunused-function"
  176. static int freeMemory() {
  177. volatile char top;
  178. return &top - reinterpret_cast<char*>(_sbrk(0));
  179. }
  180. #pragma GCC diagnostic pop
  181. //
  182. // SPI: Extended functions which take a channel number (hardware SPI only)
  183. //
  184. /** Write single byte to specified SPI channel */
  185. void spiSend(uint32_t chan, byte b);
  186. /** Write buffer to specified SPI channel */
  187. void spiSend(uint32_t chan, const uint8_t* buf, size_t n);
  188. /** Read single byte from specified SPI channel */
  189. uint8_t spiRec(uint32_t chan);
  190. //
  191. // EEPROM
  192. //
  193. /**
  194. * TODO: Write all this EEPROM stuff. Can emulate EEPROM in flash as last resort.
  195. * Wire library should work for i2c EEPROMs.
  196. */
  197. void eeprom_write_byte(uint8_t *pos, unsigned char value);
  198. uint8_t eeprom_read_byte(uint8_t *pos);
  199. void eeprom_read_block(void *__dst, const void *__src, size_t __n);
  200. void eeprom_update_block(const void *__src, void *__dst, size_t __n);
  201. //
  202. // ADC
  203. //
  204. #define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT_ANALOG);
  205. void HAL_adc_init(void);
  206. #define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
  207. #define HAL_READ_ADC() HAL_adc_result
  208. #define HAL_ADC_READY() true
  209. void HAL_adc_start_conversion(const uint8_t adc_pin);
  210. uint16_t HAL_adc_get_result(void);
  211. /* Todo: Confirm none of this is needed.
  212. uint16_t HAL_getAdcReading(uint8_t chan);
  213. void HAL_startAdcConversion(uint8_t chan);
  214. uint8_t HAL_pinToAdcChannel(int pin);
  215. uint16_t HAL_getAdcFreerun(uint8_t chan, bool wait_for_conversion = false);
  216. //uint16_t HAL_getAdcSuperSample(uint8_t chan);
  217. void HAL_enable_AdcFreerun(void);
  218. //void HAL_disable_AdcFreerun(uint8_t chan);
  219. */
  220. #define GET_PIN_MAP_PIN(index) index
  221. #define GET_PIN_MAP_INDEX(pin) pin
  222. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  223. #define JTAG_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY)
  224. #define JTAGSWD_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_NONE)