選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

300 行
8.3KB

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