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.

SoftwareSerial.cpp 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * SoftwareSerial.cpp (formerly NewSoftSerial.cpp)
  3. *
  4. * Multi-instance software serial library for Arduino/Wiring
  5. * -- Interrupt-driven receive and other improvements by ladyada
  6. * (http://ladyada.net)
  7. * -- Tuning, circular buffer, derivation from class Print/Stream,
  8. * multi-instance support, porting to 8MHz processors,
  9. * various optimizations, PROGMEM delay tables, inverse logic and
  10. * direct port writing by Mikal Hart (http://www.arduiniana.org)
  11. * -- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com)
  12. * -- 20MHz processor support by Garrett Mace (http://www.macetech.com)
  13. * -- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.com/)
  14. *
  15. * This library is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU Lesser General Public
  17. * License as published by the Free Software Foundation; either
  18. * version 2.1 of the License, or (at your option) any later version.
  19. *
  20. * This library is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * Lesser General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Lesser General Public
  26. * License along with this library; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  28. *
  29. * The latest version of this library can always be found at
  30. * http://arduiniana.org.
  31. */
  32. #ifdef TARGET_LPC1768
  33. //
  34. // Includes
  35. //
  36. //#include <WInterrupts.h>
  37. #include "../../inc/MarlinConfig.h"
  38. #include <stdint.h>
  39. #include <stdarg.h>
  40. #include <Arduino.h>
  41. #include <pinmapping.h>
  42. #include "fastio.h"
  43. #include "SoftwareSerial.h"
  44. void GpioEnableInt(uint32_t port, uint32_t pin, uint32_t mode);
  45. void GpioDisableInt(uint32_t port, uint32_t pin);
  46. //
  47. // Statics
  48. //
  49. SoftwareSerial *SoftwareSerial::active_object = 0;
  50. unsigned char SoftwareSerial::_receive_buffer[_SS_MAX_RX_BUFF];
  51. volatile uint8_t SoftwareSerial::_receive_buffer_tail = 0;
  52. volatile uint8_t SoftwareSerial::_receive_buffer_head = 0;
  53. typedef struct _DELAY_TABLE {
  54. long baud;
  55. uint16_t rx_delay_centering;
  56. uint16_t rx_delay_intrabit;
  57. uint16_t rx_delay_stopbit;
  58. uint16_t tx_delay;
  59. } DELAY_TABLE;
  60. // rough delay estimation
  61. static const DELAY_TABLE table[] = {
  62. //baud |rxcenter|rxintra |rxstop |tx { 250000, 2, 4, 4, 4, }, //Done but not good due to instruction cycle error { 115200, 4, 8, 8, 8, }, //Done but not good due to instruction cycle error
  63. //{ 74880, 69, 139, 62, 162, }, // estimation
  64. //{ 57600, 100, 185, 1, 208, }, // Done but not good due to instruction cycle error
  65. //{ 38400, 13, 26, 26, 26, }, // Done
  66. //{ 19200, 26, 52, 52, 52, }, // Done { 9600, 52, 104, 104, 104, }, // Done
  67. //{ 4800, 104, 208, 208, 208, },
  68. //{ 2400, 208, 417, 417, 417, },
  69. //{ 1200, 416, 833, 833, 833,},
  70. };
  71. //
  72. // Private methods
  73. //
  74. #if 0
  75. /* static */
  76. inline void SoftwareSerial::tunedDelay(const uint32_t count) {
  77. asm volatile(
  78. "mov r3, %[loopsPerMicrosecond] \n\t" //load the initial loop counter
  79. "1: \n\t"
  80. "sub r3, r3, #1 \n\t"
  81. "bne 1b \n\t"
  82. ://empty output list
  83. :[loopsPerMicrosecond] "r" (count)
  84. :"r3", "cc" //clobber list
  85. );
  86. }
  87. #else
  88. inline void SoftwareSerial::tunedDelay(const uint32_t count) {
  89. delayMicroseconds(count);
  90. }
  91. #endif
  92. // This function sets the current object as the "listening"
  93. // one and returns true if it replaces another
  94. bool SoftwareSerial::listen() {
  95. if (!_rx_delay_stopbit)
  96. return false;
  97. if (active_object != this) {
  98. if (active_object)
  99. active_object->stopListening();
  100. _buffer_overflow = false;
  101. _receive_buffer_head = _receive_buffer_tail = 0;
  102. active_object = this;
  103. setRxIntMsk(true);
  104. return true;
  105. }
  106. return false;
  107. }
  108. // Stop listening. Returns true if we were actually listening.
  109. bool SoftwareSerial::stopListening() {
  110. if (active_object == this) {
  111. setRxIntMsk(false);
  112. active_object = NULL;
  113. return true;
  114. }
  115. return false;
  116. }
  117. //
  118. // The receive routine called by the interrupt handler
  119. //
  120. void SoftwareSerial::recv() {
  121. uint8_t d = 0;
  122. // If RX line is high, then we don't see any start bit
  123. // so interrupt is probably not for us
  124. if (_inverse_logic ? rx_pin_read() : !rx_pin_read()) {
  125. // Disable further interrupts during reception, this prevents
  126. // triggering another interrupt directly after we return, which can
  127. // cause problems at higher baudrates.
  128. setRxIntMsk(false);//__disable_irq();//
  129. // Wait approximately 1/2 of a bit width to "center" the sample
  130. tunedDelay(_rx_delay_centering);
  131. // Read each of the 8 bits
  132. for (uint8_t i=8; i > 0; --i) {
  133. tunedDelay(_rx_delay_intrabit);
  134. d >>= 1;
  135. if (rx_pin_read()) d |= 0x80;
  136. }
  137. if (_inverse_logic) d = ~d;
  138. // if buffer full, set the overflow flag and return
  139. uint8_t next = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF;
  140. if (next != _receive_buffer_head) {
  141. // save new data in buffer: tail points to where byte goes
  142. _receive_buffer[_receive_buffer_tail] = d; // save new byte
  143. _receive_buffer_tail = next;
  144. }
  145. else {
  146. _buffer_overflow = true;
  147. }
  148. tunedDelay(_rx_delay_stopbit);
  149. // Re-enable interrupts when we're sure to be inside the stop bit
  150. setRxIntMsk(true); //__enable_irq();//
  151. }
  152. }
  153. uint32_t SoftwareSerial::rx_pin_read() {
  154. return digitalRead(_receivePin);
  155. }
  156. //
  157. // Interrupt handling
  158. //
  159. /* static */
  160. inline void SoftwareSerial::handle_interrupt() {
  161. if (active_object)
  162. active_object->recv();
  163. }
  164. extern "C" void intWrapper() {
  165. SoftwareSerial::handle_interrupt();
  166. }
  167. //
  168. // Constructor
  169. //
  170. SoftwareSerial::SoftwareSerial(pin_t receivePin, pin_t transmitPin, bool inverse_logic /* = false */) :
  171. _rx_delay_centering(0),
  172. _rx_delay_intrabit(0),
  173. _rx_delay_stopbit(0),
  174. _tx_delay(0),
  175. _buffer_overflow(false),
  176. _inverse_logic(inverse_logic) {
  177. setTX(transmitPin);
  178. setRX(receivePin);
  179. }
  180. //
  181. // Destructor
  182. //
  183. SoftwareSerial::~SoftwareSerial() {
  184. end();
  185. }
  186. void SoftwareSerial::setTX(pin_t tx) {
  187. // First write, then set output. If we do this the other way around,
  188. // the pin would be output low for a short while before switching to
  189. // output hihg. Now, it is input with pullup for a short while, which
  190. // is fine. With inverse logic, either order is fine.
  191. digitalWrite(tx, _inverse_logic ? LOW : HIGH);
  192. pinMode(tx,OUTPUT);
  193. _transmitPin = tx;
  194. }
  195. void SoftwareSerial::setRX(pin_t rx) {
  196. pinMode(rx, INPUT_PULLUP); // pullup for normal logic!
  197. //if (!_inverse_logic)
  198. // digitalWrite(rx, HIGH);
  199. _receivePin = rx;
  200. _receivePort = LPC1768_PIN_PORT(rx);
  201. _receivePortPin = LPC1768_PIN_PIN(rx);
  202. /* GPIO_T * rxPort = digitalPinToPort(rx);
  203. _receivePortRegister = portInputRegister(rxPort);
  204. _receiveBitMask = digitalPinToBitMask(rx);*/
  205. }
  206. //
  207. // Public methods
  208. //
  209. void SoftwareSerial::begin(long speed) {
  210. _rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0;
  211. for(uint8_t i = 0; i < sizeof(table)/sizeof(table[0]); ++i) {
  212. long baud = table[i].baud;
  213. if (baud == speed) {
  214. _rx_delay_centering = table[i].rx_delay_centering;
  215. _rx_delay_intrabit = table[i].rx_delay_intrabit;
  216. _rx_delay_stopbit = table[i].rx_delay_stopbit;
  217. _tx_delay = table[i].tx_delay;
  218. break;
  219. }
  220. }
  221. attachInterrupt(_receivePin, intWrapper, CHANGE); //this->handle_interrupt, CHANGE);
  222. listen();
  223. tunedDelay(_tx_delay);
  224. }
  225. void SoftwareSerial::setRxIntMsk(bool enable) {
  226. if (enable)
  227. GpioEnableInt(_receivePort,_receivePin,CHANGE);
  228. else
  229. GpioDisableInt(_receivePort,_receivePin);
  230. }
  231. void SoftwareSerial::end() {
  232. stopListening();
  233. }
  234. // Read data from buffer
  235. int SoftwareSerial::read() {
  236. if (!isListening()) return -1;
  237. // Empty buffer?
  238. if (_receive_buffer_head == _receive_buffer_tail) return -1;
  239. // Read from "head"
  240. uint8_t d = _receive_buffer[_receive_buffer_head]; // grab next byte
  241. _receive_buffer_head = (_receive_buffer_head + 1) % _SS_MAX_RX_BUFF;
  242. return d;
  243. }
  244. int SoftwareSerial::available() {
  245. if (!isListening()) return 0;
  246. return (_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head) % _SS_MAX_RX_BUFF;
  247. }
  248. size_t SoftwareSerial::write(uint8_t b) {
  249. // By declaring these as local variables, the compiler will put them
  250. // in registers _before_ disabling interrupts and entering the
  251. // critical timing sections below, which makes it a lot easier to
  252. // verify the cycle timings
  253. bool inv = _inverse_logic;
  254. uint16_t delay = _tx_delay;
  255. if (inv) b = ~b;
  256. cli(); // turn off interrupts for a clean txmit
  257. // Write the start bit
  258. digitalWrite(_transmitPin, !!inv);
  259. tunedDelay(delay);
  260. // Write each of the 8 bits
  261. for (uint8_t i = 8; i > 0; --i) {
  262. digitalWrite(_transmitPin, b & 1); // send 1 //(GPIO_Desc[_transmitPin].P)->DOUT |= GPIO_Desc[_transmitPin].bit;
  263. // send 0 //(GPIO_Desc[_transmitPin].P)->DOUT &= ~GPIO_Desc[_transmitPin].bit;
  264. tunedDelay(delay);
  265. b >>= 1;
  266. }
  267. // restore pin to natural state
  268. digitalWrite(_transmitPin, !inv);
  269. sei(); // turn interrupts back on
  270. tunedDelay(delay);
  271. return 1;
  272. }
  273. void SoftwareSerial::flush() {
  274. if (!isListening()) return;
  275. cli();
  276. _receive_buffer_head = _receive_buffer_tail = 0;
  277. sei();
  278. }
  279. int SoftwareSerial::peek() {
  280. if (!isListening())
  281. return -1;
  282. // Empty buffer?
  283. if (_receive_buffer_head == _receive_buffer_tail)
  284. return -1;
  285. // Read from "head"
  286. return _receive_buffer[_receive_buffer_head];
  287. }
  288. #endif // TARGET_LPC1768