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.

WireKinetis.h 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. TwoWire.h - TWI/I2C library for Arduino & Wiring
  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. #ifndef TwoWireKinetis_h
  18. #define TwoWireKinetis_h
  19. #if defined(__arm__) && defined(TEENSYDUINO)
  20. extern "C" void i2c0_isr(void);
  21. #include <inttypes.h>
  22. #include "Arduino.h"
  23. #define BUFFER_LENGTH 32
  24. #define WIRE_HAS_END 1
  25. #if defined(__MKL26Z64__)
  26. #define WIRE_HAS_STOP_INTERRUPT 1
  27. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  28. #define WIRE_HAS_START_INTERRUPT 1
  29. #define WIRE_HAS_STOP_INTERRUPT 1
  30. #endif
  31. class TwoWire : public Stream
  32. {
  33. public:
  34. // Hardware description struct
  35. typedef struct {
  36. volatile uint32_t &clock_gate_register;
  37. uint32_t clock_gate_mask;
  38. uint8_t sda_pin[5];
  39. uint8_t sda_mux[5];
  40. uint8_t scl_pin[5];
  41. uint8_t scl_mux[5];
  42. } I2C_Hardware_t;
  43. static const I2C_Hardware_t i2c0_hardware;
  44. static const I2C_Hardware_t i2c1_hardware;
  45. static const I2C_Hardware_t i2c2_hardware;
  46. static const I2C_Hardware_t i2c3_hardware;
  47. public:
  48. TwoWire(KINETIS_I2C_t &myport, const I2C_Hardware_t &myhardware)
  49. : port(myport), hardware(myhardware), sda_pin_index(0), scl_pin_index(0) {
  50. }
  51. void begin();
  52. void begin(uint8_t address);
  53. void begin(int address) {
  54. begin((uint8_t)address);
  55. }
  56. void end();
  57. void setClock(uint32_t frequency);
  58. void setSDA(uint8_t pin);
  59. void setSCL(uint8_t pin);
  60. void beginTransmission(uint8_t address) {
  61. txBuffer[0] = (address << 1);
  62. transmitting = 1;
  63. txBufferLength = 1;
  64. }
  65. void beginTransmission(int address) {
  66. beginTransmission((uint8_t)address);
  67. }
  68. uint8_t endTransmission(uint8_t sendStop);
  69. uint8_t endTransmission(void) {
  70. return endTransmission(1);
  71. }
  72. uint8_t requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop);
  73. uint8_t requestFrom(uint8_t address, uint8_t quantity) {
  74. return requestFrom(address, quantity, (uint8_t)1);
  75. }
  76. uint8_t requestFrom(int address, int quantity, int sendStop) {
  77. return requestFrom((uint8_t)address, (uint8_t)quantity,
  78. (uint8_t)(sendStop ? 1 : 0));
  79. }
  80. uint8_t requestFrom(int address, int quantity) {
  81. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)1);
  82. }
  83. virtual size_t write(uint8_t data);
  84. virtual size_t write(const uint8_t *data, size_t quantity);
  85. virtual int available(void) {
  86. return rxBufferLength - rxBufferIndex;
  87. }
  88. virtual int read(void) {
  89. if (rxBufferIndex >= rxBufferLength) return -1;
  90. return rxBuffer[rxBufferIndex++];
  91. }
  92. virtual int peek(void) {
  93. if (rxBufferIndex >= rxBufferLength) return -1;
  94. return rxBuffer[rxBufferIndex];
  95. }
  96. virtual void flush(void) {
  97. }
  98. void onReceive(void (*function)(int numBytes)) {
  99. user_onReceive = function;
  100. }
  101. void onRequest(void (*function)(void)) {
  102. user_onRequest = function;
  103. }
  104. // send() for compatibility with very old sketches and libraries
  105. void send(uint8_t b) {
  106. write(b);
  107. }
  108. void send(uint8_t *s, uint8_t n) {
  109. write(s, n);
  110. }
  111. void send(int n) {
  112. write((uint8_t)n);
  113. }
  114. void send(char *s) {
  115. write(s);
  116. }
  117. uint8_t receive(void) {
  118. int c = read();
  119. if (c < 0) return 0;
  120. return c;
  121. }
  122. size_t write(unsigned long n) {
  123. return write((uint8_t)n);
  124. }
  125. size_t write(long n) {
  126. return write((uint8_t)n);
  127. }
  128. size_t write(unsigned int n) {
  129. return write((uint8_t)n);
  130. }
  131. size_t write(int n) {
  132. return write((uint8_t)n);
  133. }
  134. using Print::write;
  135. private:
  136. uint8_t i2c_status(void) {
  137. return port.S;
  138. }
  139. void i2c_wait(void) {
  140. while (!(port.S & I2C_S_IICIF)) ; // wait (TODO: timeout)
  141. port.S = I2C_S_IICIF;
  142. }
  143. void isr(void);
  144. KINETIS_I2C_t &port;
  145. const I2C_Hardware_t &hardware;
  146. uint8_t rxBuffer[BUFFER_LENGTH];
  147. uint8_t rxBufferIndex;
  148. uint8_t rxBufferLength;
  149. uint8_t txAddress;
  150. uint8_t txBuffer[BUFFER_LENGTH+1];
  151. uint8_t txBufferIndex;
  152. uint8_t txBufferLength;
  153. uint8_t transmitting;
  154. uint8_t slave_mode;
  155. uint8_t irqcount;
  156. uint8_t sda_pin_index;
  157. uint8_t scl_pin_index;
  158. void onRequestService(void);
  159. void onReceiveService(uint8_t*, int);
  160. void (*user_onRequest)(void);
  161. void (*user_onReceive)(int);
  162. void sda_rising_isr(void);
  163. friend void i2c0_isr(void);
  164. friend void sda_rising_isr(void);
  165. };
  166. extern TwoWire Wire;
  167. class TWBRemulation
  168. {
  169. public:
  170. inline TWBRemulation & operator = (int val) __attribute__((always_inline)) {
  171. if (val == 12 || val == ((F_CPU / 400000) - 16) / 2) { // 22, 52, 112
  172. I2C0_C1 = 0;
  173. #if F_BUS == 120000000
  174. I2C0_F = I2C_F_DIV288; // 416 kHz
  175. #elif F_BUS == 108000000
  176. I2C0_F = I2C_F_DIV256; // 422 kHz
  177. #elif F_BUS == 96000000
  178. I2C0_F = I2C_F_DIV240; // 400 kHz
  179. #elif F_BUS == 90000000
  180. I2C0_F = I2C_F_DIV224; // 402 kHz
  181. #elif F_BUS == 80000000
  182. I2C0_F = I2C_F_DIV192; // 416 kHz
  183. #elif F_BUS == 72000000
  184. I2C0_F = I2C_F_DIV192; // 375 kHz
  185. #elif F_BUS == 64000000
  186. I2C0_F = I2C_F_DIV160; // 400 kHz
  187. #elif F_BUS == 60000000
  188. I2C0_F = I2C_F_DIV144; // 416 kHz
  189. #elif F_BUS == 56000000
  190. I2C0_F = I2C_F_DIV144; // 389 kHz
  191. #elif F_BUS == 54000000
  192. I2C0_F = I2C_F_DIV128; // 422 kHz
  193. #elif F_BUS == 48000000
  194. I2C0_F = I2C_F_DIV112; // 400 kHz
  195. #elif F_BUS == 40000000
  196. I2C0_F = I2C_F_DIV96; // 416 kHz
  197. #elif F_BUS == 36000000
  198. I2C0_F = I2C_F_DIV96; // 375 kHz
  199. #elif F_BUS == 24000000
  200. I2C0_F = I2C_F_DIV64; // 375 kHz
  201. #elif F_BUS == 16000000
  202. I2C0_F = I2C_F_DIV40; // 400 kHz
  203. #elif F_BUS == 8000000
  204. I2C0_F = I2C_F_DIV20; // 400 kHz
  205. #elif F_BUS == 4000000
  206. I2C0_F = I2C_F_DIV20; // 200 kHz
  207. #elif F_BUS == 2000000
  208. I2C0_F = I2C_F_DIV20; // 100 kHz
  209. #endif
  210. I2C0_C1 = I2C_C1_IICEN;
  211. } else if (val == 72 || val == ((F_CPU / 100000) - 16) / 2) { // 112, 232, 472
  212. I2C0_C1 = 0;
  213. #if F_BUS == 120000000
  214. I2C0_F = I2C_F_DIV1152; // 104 kHz
  215. #elif F_BUS == 108000000
  216. I2C0_F = I2C_F_DIV1024; // 105 kHz
  217. #elif F_BUS == 96000000
  218. I2C0_F = I2C_F_DIV960; // 100 kHz
  219. #elif F_BUS == 90000000
  220. I2C0_F = I2C_F_DIV896; // 100 kHz
  221. #elif F_BUS == 80000000
  222. I2C0_F = I2C_F_DIV768; // 104 kHz
  223. #elif F_BUS == 72000000
  224. I2C0_F = I2C_F_DIV640; // 112 kHz
  225. #elif F_BUS == 64000000
  226. I2C0_F = I2C_F_DIV640; // 100 kHz
  227. #elif F_BUS == 60000000
  228. I2C0_F = I2C_F_DIV576; // 104 kHz
  229. #elif F_BUS == 56000000
  230. I2C0_F = I2C_F_DIV512; // 109 kHz
  231. #elif F_BUS == 54000000
  232. I2C0_F = I2C_F_DIV512; // 105 kHz
  233. #elif F_BUS == 48000000
  234. I2C0_F = I2C_F_DIV480; // 100 kHz
  235. #elif F_BUS == 40000000
  236. I2C0_F = I2C_F_DIV384; // 104 kHz
  237. #elif F_BUS == 36000000
  238. I2C0_F = I2C_F_DIV320; // 113 kHz
  239. #elif F_BUS == 24000000
  240. I2C0_F = I2C_F_DIV240; // 100 kHz
  241. #elif F_BUS == 16000000
  242. I2C0_F = I2C_F_DIV160; // 100 kHz
  243. #elif F_BUS == 8000000
  244. I2C0_F = I2C_F_DIV80; // 100 kHz
  245. #elif F_BUS == 4000000
  246. I2C0_F = I2C_F_DIV40; // 100 kHz
  247. #elif F_BUS == 2000000
  248. I2C0_F = I2C_F_DIV20; // 100 kHz
  249. #endif
  250. I2C0_C1 = I2C_C1_IICEN;
  251. }
  252. return *this;
  253. }
  254. inline operator int () const __attribute__((always_inline)) {
  255. #if F_BUS == 120000000
  256. if (I2C0_F == I2C_F_DIV288) return 12;
  257. #elif F_BUS == 108000000
  258. if (I2C0_F == I2C_F_DIV256) return 12;
  259. #elif F_BUS == 96000000
  260. if (I2C0_F == I2C_F_DIV240) return 12;
  261. #elif F_BUS == 90000000
  262. if (I2C0_F == I2C_F_DIV224) return 12;
  263. #elif F_BUS == 80000000
  264. if (I2C0_F == I2C_F_DIV192) return 12;
  265. #elif F_BUS == 72000000
  266. if (I2C0_F == I2C_F_DIV192) return 12;
  267. #elif F_BUS == 64000000
  268. if (I2C0_F == I2C_F_DIV160) return 12;
  269. #elif F_BUS == 60000000
  270. if (I2C0_F == I2C_F_DIV144) return 12;
  271. #elif F_BUS == 56000000
  272. if (I2C0_F == I2C_F_DIV144) return 12;
  273. #elif F_BUS == 54000000
  274. if (I2C0_F == I2C_F_DIV128) return 12;
  275. #elif F_BUS == 48000000
  276. if (I2C0_F == I2C_F_DIV112) return 12;
  277. #elif F_BUS == 40000000
  278. if (I2C0_F == I2C_F_DIV96) return 12;
  279. #elif F_BUS == 36000000
  280. if (I2C0_F == I2C_F_DIV96) return 12;
  281. #elif F_BUS == 24000000
  282. if (I2C0_F == I2C_F_DIV64) return 12;
  283. #elif F_BUS == 16000000
  284. if (I2C0_F == I2C_F_DIV40) return 12;
  285. #elif F_BUS == 8000000
  286. if (I2C0_F == I2C_F_DIV20) return 12;
  287. #elif F_BUS == 4000000
  288. if (I2C0_F == I2C_F_DIV20) return 12;
  289. #endif
  290. return 72;
  291. }
  292. };
  293. extern TWBRemulation TWBR;
  294. #endif
  295. #endif