Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

323 linhas
7.7KB

  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. Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
  16. */
  17. #if defined(__AVR__)
  18. #include "Wire.h"
  19. extern "C" {
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <inttypes.h>
  23. #include "twi.h"
  24. }
  25. // Initialize Class Variables //////////////////////////////////////////////////
  26. uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
  27. uint8_t TwoWire::rxBufferIndex = 0;
  28. uint8_t TwoWire::rxBufferLength = 0;
  29. uint8_t TwoWire::txAddress = 0;
  30. uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
  31. uint8_t TwoWire::txBufferIndex = 0;
  32. uint8_t TwoWire::txBufferLength = 0;
  33. uint8_t TwoWire::transmitting = 0;
  34. void (*TwoWire::user_onRequest)(void);
  35. void (*TwoWire::user_onReceive)(int);
  36. // Constructors ////////////////////////////////////////////////////////////////
  37. TwoWire::TwoWire()
  38. {
  39. }
  40. // Public Methods //////////////////////////////////////////////////////////////
  41. void TwoWire::begin(void)
  42. {
  43. rxBufferIndex = 0;
  44. rxBufferLength = 0;
  45. txBufferIndex = 0;
  46. txBufferLength = 0;
  47. twi_init();
  48. }
  49. void TwoWire::begin(uint8_t address)
  50. {
  51. twi_setAddress(address);
  52. twi_attachSlaveTxEvent(onRequestService);
  53. twi_attachSlaveRxEvent(onReceiveService);
  54. begin();
  55. }
  56. void TwoWire::begin(int address)
  57. {
  58. begin((uint8_t)address);
  59. }
  60. void TwoWire::end()
  61. {
  62. TWCR &= ~(_BV(TWEN) | _BV(TWIE) | _BV(TWEA));
  63. digitalWrite(SDA, 0);
  64. digitalWrite(SCL, 0);
  65. }
  66. void TwoWire::setClock(uint32_t frequency)
  67. {
  68. TWBR = ((F_CPU / frequency) - 16) / 2;
  69. }
  70. void TwoWire::setSDA(uint8_t pin)
  71. {
  72. }
  73. void TwoWire::setSCL(uint8_t pin)
  74. {
  75. }
  76. uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
  77. {
  78. // clamp to buffer length
  79. if(quantity > BUFFER_LENGTH){
  80. quantity = BUFFER_LENGTH;
  81. }
  82. // perform blocking read into buffer
  83. uint8_t read = twi_readFrom(address, rxBuffer, quantity, sendStop);
  84. // set rx buffer iterator vars
  85. rxBufferIndex = 0;
  86. rxBufferLength = read;
  87. return read;
  88. }
  89. uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
  90. {
  91. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
  92. }
  93. uint8_t TwoWire::requestFrom(int address, int quantity)
  94. {
  95. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
  96. }
  97. uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
  98. {
  99. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
  100. }
  101. void TwoWire::beginTransmission(uint8_t address)
  102. {
  103. // indicate that we are transmitting
  104. transmitting = 1;
  105. // set address of targeted slave
  106. txAddress = address;
  107. // reset tx buffer iterator vars
  108. txBufferIndex = 0;
  109. txBufferLength = 0;
  110. }
  111. void TwoWire::beginTransmission(int address)
  112. {
  113. beginTransmission((uint8_t)address);
  114. }
  115. //
  116. // Originally, 'endTransmission' was an f(void) function.
  117. // It has been modified to take one parameter indicating
  118. // whether or not a STOP should be performed on the bus.
  119. // Calling endTransmission(false) allows a sketch to
  120. // perform a repeated start.
  121. //
  122. // WARNING: Nothing in the library keeps track of whether
  123. // the bus tenure has been properly ended with a STOP. It
  124. // is very possible to leave the bus in a hung state if
  125. // no call to endTransmission(true) is made. Some I2C
  126. // devices will behave oddly if they do not see a STOP.
  127. //
  128. uint8_t TwoWire::endTransmission(uint8_t sendStop)
  129. {
  130. // transmit buffer (blocking)
  131. int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1, sendStop);
  132. // reset tx buffer iterator vars
  133. txBufferIndex = 0;
  134. txBufferLength = 0;
  135. // indicate that we are done transmitting
  136. transmitting = 0;
  137. return ret;
  138. }
  139. // This provides backwards compatibility with the original
  140. // definition, and expected behaviour, of endTransmission
  141. //
  142. uint8_t TwoWire::endTransmission(void)
  143. {
  144. return endTransmission(true);
  145. }
  146. // must be called in:
  147. // slave tx event callback
  148. // or after beginTransmission(address)
  149. size_t TwoWire::write(uint8_t data)
  150. {
  151. if(transmitting){
  152. // in master transmitter mode
  153. // don't bother if buffer is full
  154. if(txBufferLength >= BUFFER_LENGTH){
  155. setWriteError();
  156. return 0;
  157. }
  158. // put byte in tx buffer
  159. txBuffer[txBufferIndex] = data;
  160. ++txBufferIndex;
  161. // update amount in buffer
  162. txBufferLength = txBufferIndex;
  163. }else{
  164. // in slave send mode
  165. // reply to master
  166. twi_transmit(&data, 1);
  167. }
  168. return 1;
  169. }
  170. // must be called in:
  171. // slave tx event callback
  172. // or after beginTransmission(address)
  173. size_t TwoWire::write(const uint8_t *data, size_t quantity)
  174. {
  175. if(transmitting){
  176. // in master transmitter mode
  177. for(size_t i = 0; i < quantity; ++i){
  178. write(data[i]);
  179. }
  180. }else{
  181. // in slave send mode
  182. // reply to master
  183. twi_transmit(data, quantity);
  184. }
  185. return quantity;
  186. }
  187. // must be called in:
  188. // slave rx event callback
  189. // or after requestFrom(address, numBytes)
  190. int TwoWire::available(void)
  191. {
  192. return rxBufferLength - rxBufferIndex;
  193. }
  194. // must be called in:
  195. // slave rx event callback
  196. // or after requestFrom(address, numBytes)
  197. int TwoWire::read(void)
  198. {
  199. int value = -1;
  200. // get each successive byte on each call
  201. if(rxBufferIndex < rxBufferLength){
  202. value = rxBuffer[rxBufferIndex];
  203. ++rxBufferIndex;
  204. }
  205. return value;
  206. }
  207. // must be called in:
  208. // slave rx event callback
  209. // or after requestFrom(address, numBytes)
  210. int TwoWire::peek(void)
  211. {
  212. int value = -1;
  213. if(rxBufferIndex < rxBufferLength){
  214. value = rxBuffer[rxBufferIndex];
  215. }
  216. return value;
  217. }
  218. void TwoWire::flush(void)
  219. {
  220. // XXX: to be implemented.
  221. }
  222. // behind the scenes function that is called when data is received
  223. void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
  224. {
  225. // don't bother if user hasn't registered a callback
  226. if(!user_onReceive){
  227. return;
  228. }
  229. // don't bother if rx buffer is in use by a master requestFrom() op
  230. // i know this drops data, but it allows for slight stupidity
  231. // meaning, they may not have read all the master requestFrom() data yet
  232. if(rxBufferIndex < rxBufferLength){
  233. return;
  234. }
  235. // copy twi rx buffer into local read buffer
  236. // this enables new reads to happen in parallel
  237. for(uint8_t i = 0; i < numBytes; ++i){
  238. rxBuffer[i] = inBytes[i];
  239. }
  240. // set rx iterator vars
  241. rxBufferIndex = 0;
  242. rxBufferLength = numBytes;
  243. // alert user program
  244. user_onReceive(numBytes);
  245. }
  246. // behind the scenes function that is called when data is requested
  247. void TwoWire::onRequestService(void)
  248. {
  249. // don't bother if user hasn't registered a callback
  250. if(!user_onRequest){
  251. return;
  252. }
  253. // reset tx buffer iterator vars
  254. // !!! this will kill any pending pre-master sendTo() activity
  255. txBufferIndex = 0;
  256. txBufferLength = 0;
  257. // alert user program
  258. user_onRequest();
  259. }
  260. // sets function called on slave write
  261. void TwoWire::onReceive( void (*function)(int) )
  262. {
  263. user_onReceive = function;
  264. }
  265. // sets function called on slave read
  266. void TwoWire::onRequest( void (*function)(void) )
  267. {
  268. user_onRequest = function;
  269. }
  270. // Preinstantiate Objects //////////////////////////////////////////////////////
  271. TwoWire Wire = TwoWire();
  272. #endif // __AVR__