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.

Wire.cpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. TwoWire.cpp - TWI/I2C library for Wiring & Arduino
  3. Copyright (c) 2006 Nicholas Zambetti. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. #ifdef TARGET_LPC1768
  17. extern "C" {
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <inttypes.h>
  21. #include <lpc17xx_i2c.h>
  22. #include <lpc17xx_pinsel.h>
  23. #include <lpc17xx_libcfg_default.h>
  24. }
  25. #include <Wire.h>
  26. #define USEDI2CDEV_M 1
  27. #if (USEDI2CDEV_M == 0)
  28. #define I2CDEV_M LPC_I2C0
  29. #elif (USEDI2CDEV_M == 1)
  30. #define I2CDEV_M LPC_I2C1
  31. #elif (USEDI2CDEV_M == 2)
  32. #define I2CDEV_M LPC_I2C2
  33. #else
  34. #error "Master I2C device not defined!"
  35. #endif
  36. // Initialize Class Variables //////////////////////////////////////////////////
  37. uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
  38. uint8_t TwoWire::rxBufferIndex = 0;
  39. uint8_t TwoWire::rxBufferLength = 0;
  40. uint8_t TwoWire::txAddress = 0;
  41. uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
  42. uint8_t TwoWire::txBufferIndex = 0;
  43. uint8_t TwoWire::txBufferLength = 0;
  44. uint8_t TwoWire::transmitting = 0;
  45. // Constructors ////////////////////////////////////////////////////////////////
  46. TwoWire::TwoWire() {
  47. }
  48. // Public Methods //////////////////////////////////////////////////////////////
  49. void TwoWire::begin(void) {
  50. rxBufferIndex = 0;
  51. rxBufferLength = 0;
  52. txBufferIndex = 0;
  53. txBufferLength = 0;
  54. /*
  55. * Init I2C pin connect
  56. */
  57. PINSEL_CFG_Type PinCfg;
  58. PinCfg.OpenDrain = 0;
  59. PinCfg.Pinmode = 0;
  60. #if USEDI2CDEV_M == 0
  61. PinCfg.Funcnum = 1;
  62. PinCfg.Pinnum = 27;
  63. PinCfg.Portnum = 0;
  64. PINSEL_ConfigPin(&PinCfg); // SDA0 / D57 AUX-1
  65. PinCfg.Pinnum = 28;
  66. PINSEL_ConfigPin(&PinCfg); // SCL0 / D58 AUX-1
  67. #endif
  68. #if USEDI2CDEV_M == 1
  69. PinCfg.Funcnum = 3;
  70. PinCfg.Pinnum = 0;
  71. PinCfg.Portnum = 0;
  72. PINSEL_ConfigPin(&PinCfg); // SDA1 / D20 SCA
  73. PinCfg.Pinnum = 1;
  74. PINSEL_ConfigPin(&PinCfg); // SCL1 / D21 SCL
  75. #endif
  76. #if USEDI2CDEV_M == 2
  77. PinCfg.Funcnum = 2;
  78. PinCfg.Pinnum = 10;
  79. PinCfg.Portnum = 0;
  80. PINSEL_ConfigPin(&PinCfg); // SDA2 / D38 X_ENABLE_PIN
  81. PinCfg.Pinnum = 11;
  82. PINSEL_ConfigPin(&PinCfg); // SCL2 / D55 X_DIR_PIN
  83. #endif
  84. // Initialize I2C peripheral
  85. I2C_Init(I2CDEV_M, 100000);
  86. // Enable Master I2C operation
  87. I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
  88. }
  89. uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) {
  90. // clamp to buffer length
  91. if (quantity > BUFFER_LENGTH)
  92. quantity = BUFFER_LENGTH;
  93. // perform blocking read into buffer
  94. I2C_M_SETUP_Type transferMCfg;
  95. transferMCfg.sl_addr7bit = address >> 1; // not sure about the right shift
  96. transferMCfg.tx_data = NULL;
  97. transferMCfg.tx_length = 0;
  98. transferMCfg.rx_data = rxBuffer;
  99. transferMCfg.rx_length = quantity;
  100. transferMCfg.retransmissions_max = 3;
  101. I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
  102. // set rx buffer iterator vars
  103. rxBufferIndex = 0;
  104. rxBufferLength = transferMCfg.rx_count;
  105. return transferMCfg.rx_count;
  106. }
  107. uint8_t TwoWire::requestFrom(int address, int quantity) {
  108. return requestFrom((uint8_t)address, (uint8_t)quantity);
  109. }
  110. void TwoWire::beginTransmission(uint8_t address) {
  111. // indicate that we are transmitting
  112. transmitting = 1;
  113. // set address of targeted slave
  114. txAddress = address;
  115. // reset tx buffer iterator vars
  116. txBufferIndex = 0;
  117. txBufferLength = 0;
  118. }
  119. void TwoWire::beginTransmission(int address) {
  120. beginTransmission((uint8_t)address);
  121. }
  122. uint8_t TwoWire::endTransmission(void) {
  123. // transmit buffer (blocking)
  124. I2C_M_SETUP_Type transferMCfg;
  125. transferMCfg.sl_addr7bit = txAddress >> 1; // not sure about the right shift
  126. transferMCfg.tx_data = txBuffer;
  127. transferMCfg.tx_length = txBufferLength;
  128. transferMCfg.rx_data = NULL;
  129. transferMCfg.rx_length = 0;
  130. transferMCfg.retransmissions_max = 3;
  131. Status status = I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
  132. // reset tx buffer iterator vars
  133. txBufferIndex = 0;
  134. txBufferLength = 0;
  135. // indicate that we are done transmitting
  136. transmitting = 0;
  137. return status == SUCCESS ? 0 : 4;
  138. }
  139. // must be called after beginTransmission(address)
  140. size_t TwoWire::write(uint8_t data) {
  141. if (transmitting) {
  142. // don't bother if buffer is full
  143. if (txBufferLength >= BUFFER_LENGTH) return 0;
  144. // put byte in tx buffer
  145. txBuffer[txBufferIndex++] = data;
  146. // update amount in buffer
  147. txBufferLength = txBufferIndex;
  148. }
  149. return 1;
  150. }
  151. // must be called after beginTransmission(address)
  152. size_t TwoWire::write(const uint8_t *data, size_t quantity) {
  153. size_t sent = 0;
  154. if (transmitting)
  155. for (sent = 0; sent < quantity; ++sent)
  156. if (!write(data[sent])) break;
  157. return sent;
  158. }
  159. // Must be called after requestFrom(address, numBytes)
  160. int TwoWire::available(void) {
  161. return rxBufferLength - rxBufferIndex;
  162. }
  163. // Must be called after requestFrom(address, numBytes)
  164. int TwoWire::read(void) {
  165. return rxBufferIndex < rxBufferLength ? rxBuffer[rxBufferIndex++] : -1;
  166. }
  167. // Must be called after requestFrom(address, numBytes)
  168. int TwoWire::peek(void) {
  169. return rxBufferIndex < rxBufferLength ? rxBuffer[rxBufferIndex] : -1;
  170. }
  171. // Preinstantiate Objects //////////////////////////////////////////////////////
  172. TwoWire Wire = TwoWire();
  173. #endif // TARGET_LPC1768