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_Stm32f1.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /**
  24. * HAL for stm32duino.com based on Libmaple and compatible (STM32F1)
  25. */
  26. #ifdef __STM32F1__
  27. // --------------------------------------------------------------------------
  28. // Includes
  29. // --------------------------------------------------------------------------
  30. #include "../HAL.h"
  31. #include <STM32ADC.h>
  32. //#include <Wire.h>
  33. // --------------------------------------------------------------------------
  34. // Externals
  35. // --------------------------------------------------------------------------
  36. // --------------------------------------------------------------------------
  37. // Local defines
  38. // --------------------------------------------------------------------------
  39. // --------------------------------------------------------------------------
  40. // Types
  41. // --------------------------------------------------------------------------
  42. // --------------------------------------------------------------------------
  43. // Variables
  44. // --------------------------------------------------------------------------
  45. // --------------------------------------------------------------------------
  46. // Public Variables
  47. // --------------------------------------------------------------------------
  48. uint16_t HAL_adc_result;
  49. // --------------------------------------------------------------------------
  50. // Private Variables
  51. // --------------------------------------------------------------------------
  52. STM32ADC adc(ADC1);
  53. uint8 adc_pins[] = {
  54. #if HAS_TEMP_0
  55. TEMP_0_PIN,
  56. #endif
  57. #if HAS_TEMP_1
  58. TEMP_1_PIN
  59. #endif
  60. #if HAS_TEMP_2
  61. TEMP_2_PIN,
  62. #endif
  63. #if HAS_TEMP_3
  64. TEMP_3_PIN,
  65. #endif
  66. #if HAS_TEMP_4
  67. TEMP_4_PIN,
  68. #endif
  69. #if HAS_TEMP_BED
  70. TEMP_BED_PIN,
  71. #endif
  72. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  73. FILWIDTH_PIN,
  74. #endif
  75. };
  76. enum TEMP_PINS : char {
  77. #if HAS_TEMP_0
  78. TEMP_0,
  79. #endif
  80. #if HAS_TEMP_1
  81. TEMP_1,
  82. #endif
  83. #if HAS_TEMP_2
  84. TEMP_2,
  85. #endif
  86. #if HAS_TEMP_3
  87. TEMP_3,
  88. #endif
  89. #if HAS_TEMP_4
  90. TEMP_4,
  91. #endif
  92. #if HAS_TEMP_BED
  93. TEMP_BED,
  94. #endif
  95. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  96. FILWIDTH,
  97. #endif
  98. ADC_PIN_COUNT
  99. };
  100. uint16_t HAL_adc_results[ADC_PIN_COUNT];
  101. // --------------------------------------------------------------------------
  102. // Function prototypes
  103. // --------------------------------------------------------------------------
  104. // --------------------------------------------------------------------------
  105. // Private functions
  106. // --------------------------------------------------------------------------
  107. // --------------------------------------------------------------------------
  108. // Public functions
  109. // --------------------------------------------------------------------------
  110. /* VGPV Done with defines
  111. // disable interrupts
  112. void cli(void) { noInterrupts(); }
  113. // enable interrupts
  114. void sei(void) { interrupts(); }
  115. */
  116. void HAL_clear_reset_source(void) { }
  117. /**
  118. * TODO: Check this and change or remove.
  119. * currently returns 1 that's equal to poweron reset.
  120. */
  121. uint8_t HAL_get_reset_source(void) { return 1; }
  122. void _delay_ms(const int delay_ms) { delay(delay_ms); }
  123. extern "C" {
  124. extern unsigned int _ebss; // end of bss section
  125. }
  126. /**
  127. * TODO: Change this to correct it for libmaple
  128. */
  129. // return free memory between end of heap (or end bss) and whatever is current
  130. /*
  131. #include "wirish/syscalls.c"
  132. //extern caddr_t _sbrk(int incr);
  133. #ifndef CONFIG_HEAP_END
  134. extern char _lm_heap_end;
  135. #define CONFIG_HEAP_END ((caddr_t)&_lm_heap_end)
  136. #endif
  137. extern "C" {
  138. static int freeMemory() {
  139. char top = 't';
  140. return &top - reinterpret_cast<char*>(sbrk(0));
  141. }
  142. int freeMemory() {
  143. int free_memory;
  144. int heap_end = (int)_sbrk(0);
  145. free_memory = ((int)&free_memory) - ((int)heap_end);
  146. return free_memory;
  147. }
  148. }
  149. */
  150. // --------------------------------------------------------------------------
  151. // ADC
  152. // --------------------------------------------------------------------------
  153. // Init the AD in continuous capture mode
  154. void HAL_adc_init(void) {
  155. // configure the ADC
  156. adc.calibrate();
  157. adc.setSampleRate(ADC_SMPR_41_5); // ?
  158. adc.setPins(adc_pins, ADC_PIN_COUNT);
  159. adc.setDMA(HAL_adc_results, (uint16_t)ADC_PIN_COUNT, (uint32_t)(DMA_MINC_MODE | DMA_CIRC_MODE), (void (*)())NULL);
  160. adc.setScanMode();
  161. adc.setContinuous();
  162. adc.startConversion();
  163. }
  164. void HAL_adc_start_conversion(const uint8_t adc_pin) {
  165. TEMP_PINS pin_index;
  166. switch (adc_pin) {
  167. #if HAS_TEMP_0
  168. case TEMP_0_PIN: pin_index = TEMP_0; break;
  169. #endif
  170. #if HAS_TEMP_1
  171. case TEMP_1_PIN: pin_index = TEMP_1; break;
  172. #endif
  173. #if HAS_TEMP_2
  174. case TEMP_2_PIN: pin_index = TEMP_2; break;
  175. #endif
  176. #if HAS_TEMP_3
  177. case TEMP_3_PIN: pin_index = TEMP_3; break;
  178. #endif
  179. #if HAS_TEMP_4
  180. case TEMP_4_PIN: pin_index = TEMP_4; break;
  181. #endif
  182. #if HAS_TEMP_BED
  183. case TEMP_BED_PIN: pin_index = TEMP_BED; break;
  184. #endif
  185. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  186. case FILWIDTH_PIN: pin_index = FILWIDTH; break;
  187. #endif
  188. }
  189. HAL_adc_result = (HAL_adc_results[(int)pin_index] >> 2) & 0x3FF; // shift to get 10 bits only.
  190. }
  191. uint16_t HAL_adc_get_result(void) {
  192. return HAL_adc_result;
  193. }
  194. #endif // __STM32F1__