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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (C) 2016 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. #ifndef _HAL_STM32F4_H
  24. #define _HAL_STM32F4_H
  25. #define CPU_32_BIT
  26. #undef DEBUG_NONE
  27. #ifndef vsnprintf_P
  28. #define vsnprintf_P vsnprintf
  29. #endif
  30. // --------------------------------------------------------------------------
  31. // Includes
  32. // --------------------------------------------------------------------------
  33. #include <stdint.h>
  34. #include "Arduino.h"
  35. #ifdef USBCON
  36. #include <USBSerial.h>
  37. #endif
  38. #include "../shared/math_32bit.h"
  39. #include "../shared/HAL_SPI.h"
  40. #include "fastio_STM32F4.h"
  41. #include "watchdog_STM32F4.h"
  42. #include "HAL_timers_STM32F4.h"
  43. // --------------------------------------------------------------------------
  44. // Defines
  45. // --------------------------------------------------------------------------
  46. //Serial override
  47. //extern HalSerial usb_serial;
  48. #if SERIAL_PORT == 0
  49. #error "Serial port 0 does not exist"
  50. #endif
  51. #if !WITHIN(SERIAL_PORT, -1, 6)
  52. #error "SERIAL_PORT must be from -1 to 6"
  53. #endif
  54. #if SERIAL_PORT == -1
  55. #define MYSERIAL0 SerialUSB
  56. #elif SERIAL_PORT == 1
  57. #define MYSERIAL0 SerialUART1
  58. #elif SERIAL_PORT == 2
  59. #define MYSERIAL0 SerialUART2
  60. #elif SERIAL_PORT == 3
  61. #define MYSERIAL0 SerialUART3
  62. #elif SERIAL_PORT == 4
  63. #define MYSERIAL0 SerialUART4
  64. #elif SERIAL_PORT == 5
  65. #define MYSERIAL0 SerialUART5
  66. #elif SERIAL_PORT == 6
  67. #define MYSERIAL0 SerialUART6
  68. #endif
  69. #ifdef SERIAL_PORT_2
  70. #if SERIAL_PORT_2 == 0
  71. #error "Serial port 0 does not exist"
  72. #endif
  73. #if !WITHIN(SERIAL_PORT_2, -1, 6)
  74. #error "SERIAL_PORT_2 must be from -1 to 6"
  75. #elif SERIAL_PORT_2 == SERIAL_PORT
  76. #error "SERIAL_PORT_2 must be different than SERIAL_PORT"
  77. #endif
  78. #define NUM_SERIAL 2
  79. #if SERIAL_PORT_2 == -1
  80. #define MYSERIAL1 SerialUSB
  81. #elif SERIAL_PORT_2 == 1
  82. #define MYSERIAL1 SerialUART1
  83. #elif SERIAL_PORT_2 == 2
  84. #define MYSERIAL1 SerialUART2
  85. #elif SERIAL_PORT_2 == 3
  86. #define MYSERIAL1 SerialUART3
  87. #elif SERIAL_PORT_2 == 4
  88. #define MYSERIAL1 SerialUART4
  89. #elif SERIAL_PORT_2 == 5
  90. #define MYSERIAL1 SerialUART5
  91. #elif SERIAL_PORT_2 == 6
  92. #define MYSERIAL1 SerialUART6
  93. #endif
  94. #else
  95. #define NUM_SERIAL 1
  96. #endif
  97. #define _BV(b) (1 << (b))
  98. /**
  99. * TODO: review this to return 1 for pins that are not analog input
  100. */
  101. #ifndef analogInputToDigitalPin
  102. #define analogInputToDigitalPin(p) (p)
  103. #endif
  104. #define CRITICAL_SECTION_START uint32_t primask = __get_PRIMASK(); __disable_irq()
  105. #define CRITICAL_SECTION_END if (!primask) __enable_irq()
  106. #define ISRS_ENABLED() (!__get_PRIMASK())
  107. #define ENABLE_ISRS() __enable_irq()
  108. #define DISABLE_ISRS() __disable_irq()
  109. #define cli() __disable_irq()
  110. #define sei() __enable_irq()
  111. // On AVR this is in math.h?
  112. #define square(x) ((x)*(x))
  113. #ifndef strncpy_P
  114. #define strncpy_P(dest, src, num) strncpy((dest), (src), (num))
  115. #endif
  116. // Fix bug in pgm_read_ptr
  117. #undef pgm_read_ptr
  118. #define pgm_read_ptr(addr) (*(addr))
  119. #define RST_POWER_ON 1
  120. #define RST_EXTERNAL 2
  121. #define RST_BROWN_OUT 4
  122. #define RST_WATCHDOG 8
  123. #define RST_JTAG 16
  124. #define RST_SOFTWARE 32
  125. #define RST_BACKUP 64
  126. // --------------------------------------------------------------------------
  127. // Types
  128. // --------------------------------------------------------------------------
  129. typedef int8_t pin_t;
  130. #define HAL_SERVO_LIB libServo
  131. // --------------------------------------------------------------------------
  132. // Public Variables
  133. // --------------------------------------------------------------------------
  134. /** result of last ADC conversion */
  135. extern uint16_t HAL_adc_result;
  136. // --------------------------------------------------------------------------
  137. // Public functions
  138. // --------------------------------------------------------------------------
  139. // Memory related
  140. #define __bss_end __bss_end__
  141. /** clear reset reason */
  142. void HAL_clear_reset_source (void);
  143. /** reset reason */
  144. uint8_t HAL_get_reset_source (void);
  145. void _delay_ms(const int delay);
  146. /*
  147. extern "C" {
  148. int freeMemory(void);
  149. }
  150. */
  151. extern "C" char* _sbrk(int incr);
  152. /*
  153. static int freeMemory() {
  154. volatile int top;
  155. top = (int)((char*)&top - reinterpret_cast<char*>(_sbrk(0)));
  156. return top;
  157. }
  158. */
  159. static int freeMemory() {
  160. volatile char top;
  161. return &top - reinterpret_cast<char*>(_sbrk(0));
  162. }
  163. // SPI: Extended functions which take a channel number (hardware SPI only)
  164. /** Write single byte to specified SPI channel */
  165. void spiSend(uint32_t chan, byte b);
  166. /** Write buffer to specified SPI channel */
  167. void spiSend(uint32_t chan, const uint8_t* buf, size_t n);
  168. /** Read single byte from specified SPI channel */
  169. uint8_t spiRec(uint32_t chan);
  170. // EEPROM
  171. /**
  172. * TODO: Write all this eeprom stuff. Can emulate eeprom in flash as last resort.
  173. * Wire library should work for i2c eeproms.
  174. */
  175. void eeprom_write_byte(unsigned char *pos, unsigned char value);
  176. unsigned char eeprom_read_byte(unsigned char *pos);
  177. void eeprom_read_block (void *__dst, const void *__src, size_t __n);
  178. void eeprom_update_block (const void *__src, void *__dst, size_t __n);
  179. // ADC
  180. #define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT)
  181. inline void HAL_adc_init(void) {}
  182. #define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
  183. #define HAL_READ_ADC() HAL_adc_result
  184. #define HAL_ADC_READY() true
  185. void HAL_adc_start_conversion(const uint8_t adc_pin);
  186. uint16_t HAL_adc_get_result(void);
  187. /* Todo: Confirm none of this is needed.
  188. uint16_t HAL_getAdcReading(uint8_t chan);
  189. void HAL_startAdcConversion(uint8_t chan);
  190. uint8_t HAL_pinToAdcChannel(int pin);
  191. uint16_t HAL_getAdcFreerun(uint8_t chan, bool wait_for_conversion = false);
  192. //uint16_t HAL_getAdcSuperSample(uint8_t chan);
  193. void HAL_enable_AdcFreerun(void);
  194. //void HAL_disable_AdcFreerun(uint8_t chan);
  195. */
  196. #define GET_PIN_MAP_PIN(index) index
  197. #define GET_PIN_MAP_INDEX(pin) pin
  198. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  199. #define JTAG_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY)
  200. #define JTAGSWD_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_NONE)
  201. #endif // _HAL_STM32F4_H