My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

arduino.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifdef TARGET_LPC1768
  23. #include "../../inc/MarlinConfig.h"
  24. #include "LPC1768_PWM.h"
  25. #include <lpc17xx_pinsel.h>
  26. // Interrupts
  27. void cli(void) { __disable_irq(); } // Disable
  28. void sei(void) { __enable_irq(); } // Enable
  29. // Time functions
  30. void _delay_ms(const int delay_ms) {
  31. delay(delay_ms);
  32. }
  33. uint32_t millis() {
  34. return _millis;
  35. }
  36. void delayMicroseconds(uint32_t us) {
  37. static const int nop_factor = (SystemCoreClock / 11000000);
  38. static volatile int loops = 0;
  39. //previous ops already burned most of 1us, burn the rest
  40. loops = nop_factor / 4; //measured at 1us
  41. while (loops > 0) --loops;
  42. if (us < 2) return;
  43. us--;
  44. //redirect to delay for large values, then set new delay to remainder
  45. if (us > 1000) {
  46. delay(us / 1000);
  47. us = us % 1000;
  48. }
  49. // burn cycles, time in interrupts will not be taken into account
  50. loops = us * nop_factor;
  51. while (loops > 0) --loops;
  52. }
  53. extern "C" void delay(const int msec) {
  54. volatile millis_t end = _millis + msec;
  55. SysTick->VAL = SysTick->LOAD; // reset systick counter so next systick is in exactly 1ms
  56. // this could extend the time between systicks by upto 1ms
  57. while PENDING(_millis, end) __WFE();
  58. }
  59. // IO functions
  60. // As defined by Arduino INPUT(0x0), OUPUT(0x1), INPUT_PULLUP(0x2)
  61. void pinMode(pin_t pin, uint8_t mode) {
  62. if (!VALID_PIN(pin))
  63. return;
  64. PINSEL_CFG_Type config = { LPC1768_PIN_PORT(pin),
  65. LPC1768_PIN_PIN(pin),
  66. PINSEL_FUNC_0,
  67. PINSEL_PINMODE_TRISTATE,
  68. PINSEL_PINMODE_NORMAL };
  69. switch(mode) {
  70. case INPUT:
  71. LPC_GPIO(LPC1768_PIN_PORT(pin))->FIODIR &= ~LPC_PIN(LPC1768_PIN_PIN(pin));
  72. PINSEL_ConfigPin(&config);
  73. break;
  74. case OUTPUT:
  75. LPC_GPIO(LPC1768_PIN_PORT(pin))->FIODIR |= LPC_PIN(LPC1768_PIN_PIN(pin));
  76. PINSEL_ConfigPin(&config);
  77. break;
  78. case INPUT_PULLUP:
  79. LPC_GPIO(LPC1768_PIN_PORT(pin))->FIODIR &= ~LPC_PIN(LPC1768_PIN_PIN(pin));
  80. config.Pinmode = PINSEL_PINMODE_PULLUP;
  81. PINSEL_ConfigPin(&config);
  82. break;
  83. default:
  84. break;
  85. }
  86. }
  87. void digitalWrite(pin_t pin, uint8_t pin_status) {
  88. if (!VALID_PIN(pin))
  89. return;
  90. if (pin_status)
  91. LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOSET = LPC_PIN(LPC1768_PIN_PIN(pin));
  92. else
  93. LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOCLR = LPC_PIN(LPC1768_PIN_PIN(pin));
  94. pinMode(pin, OUTPUT); // Set pin mode on every write (Arduino version does this)
  95. /**
  96. * Must be done AFTER the output state is set. Doing this before will cause a
  97. * 2uS glitch if writing a "1".
  98. *
  99. * When the Port Direction bit is written to a "1" the output is immediately set
  100. * to the value of the FIOPIN bit which is "0" because of power up defaults.
  101. */
  102. }
  103. bool digitalRead(pin_t pin) {
  104. if (!VALID_PIN(pin)) {
  105. return false;
  106. }
  107. return LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOPIN & LPC_PIN(LPC1768_PIN_PIN(pin)) ? 1 : 0;
  108. }
  109. void analogWrite(pin_t pin, int pwm_value) { // 1 - 254: pwm_value, 0: LOW, 255: HIGH
  110. #define MR0_MARGIN 200 // if channel value too close to MR0 the system locks up
  111. static bool out_of_PWM_slots = false;
  112. if (!VALID_PIN(pin))
  113. return;
  114. uint value = MAX(MIN(pwm_value, 255), 0);
  115. if (value == 0 || value == 255) { // treat as digital pin
  116. LPC1768_PWM_detach_pin(pin); // turn off PWM
  117. digitalWrite(pin, value);
  118. }
  119. else {
  120. if (LPC1768_PWM_attach_pin(pin, 1, (LPC_PWM1->MR0 - MR0_MARGIN), 0xff)) // locks up if get too close to MR0 value
  121. LPC1768_PWM_write(pin, map(value, 1, 254, 1, (LPC_PWM1->MR0 - MR0_MARGIN))); // map 1-254 onto PWM range
  122. else { // out of PWM channels
  123. if (!out_of_PWM_slots) MYSERIAL.printf(".\nWARNING - OUT OF PWM CHANNELS\n.\n"); //only warn once
  124. out_of_PWM_slots = true;
  125. digitalWrite(pin, value); // treat as a digital pin if out of channels
  126. }
  127. }
  128. }
  129. extern bool HAL_adc_finished();
  130. uint16_t analogRead(pin_t adc_pin) {
  131. HAL_adc_start_conversion(adc_pin);
  132. while (!HAL_adc_finished()); // Wait for conversion to finish
  133. return HAL_adc_get_result();
  134. }
  135. // **************************
  136. // Persistent Config Storage
  137. // **************************
  138. void eeprom_write_byte(unsigned char *pos, unsigned char value) {
  139. }
  140. unsigned char eeprom_read_byte(uint8_t * pos) { return '\0'; }
  141. void eeprom_read_block (void *__dst, const void *__src, size_t __n) { }
  142. void eeprom_update_block (const void *__src, void *__dst, size_t __n) { }
  143. char *dtostrf (double __val, signed char __width, unsigned char __prec, char *__s) {
  144. char format_string[20];
  145. snprintf(format_string, 20, "%%%d.%df", __width, __prec);
  146. sprintf(__s, format_string, __val);
  147. return __s;
  148. }
  149. int32_t random(int32_t max) {
  150. return rand() % max;
  151. }
  152. int32_t random(int32_t min, int32_t max) {
  153. return min + rand() % (max - min);
  154. }
  155. void randomSeed(uint32_t value) {
  156. srand(value);
  157. }
  158. int map(uint16_t x, uint16_t in_min, uint16_t in_max, uint16_t out_min, uint16_t out_max) {
  159. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  160. }
  161. #endif // TARGET_LPC1768