twi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. si2c.c - Software I2C library for ESP31B
  3. Copyright (c) 2015 Hristo Gochkov. All rights reserved.
  4. This file is part of the ESP31B core for Arduino environment.
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include "twi.h"
  20. #include "soc/gpio_reg.h"
  21. #include "soc/gpio_struct.h"
  22. #include "soc/io_mux_reg.h"
  23. #include "driver/rtc_io.h"
  24. #include <stdio.h>
  25. #define LOW 0x0
  26. #define HIGH 0x1
  27. //GPIO FUNCTIONS
  28. #define INPUT 0x01
  29. #define OUTPUT 0x02
  30. #define PULLUP 0x04
  31. #define INPUT_PULLUP 0x05
  32. #define PULLDOWN 0x08
  33. #define INPUT_PULLDOWN 0x09
  34. #define OPEN_DRAIN 0x10
  35. #define OUTPUT_OPEN_DRAIN 0x12
  36. #define SPECIAL 0xF0
  37. #define FUNCTION_1 0x00
  38. #define FUNCTION_2 0x20
  39. #define FUNCTION_3 0x40
  40. #define FUNCTION_4 0x60
  41. #define FUNCTION_5 0x80
  42. #define FUNCTION_6 0xA0
  43. #define ESP_REG(addr) *((volatile uint32_t *)(addr))
  44. const uint8_t pin_to_mux[40] = { 0x44, 0x88, 0x40, 0x84, 0x48, 0x6c, 0x60, 0x64, 0x68, 0x54, 0x58, 0x5c, 0x34, 0x38, 0x30, 0x3c, 0x4c, 0x50, 0x70, 0x74, 0x78, 0x7c, 0x80, 0x8c, 0, 0x24, 0x28, 0x2c, 0, 0, 0, 0, 0x1c, 0x20, 0x14, 0x18, 0x04, 0x08, 0x0c, 0x10};
  45. static void pinMode(uint8_t pin, uint8_t mode)
  46. {
  47. if(pin >= 40) {
  48. return;
  49. }
  50. uint32_t rtc_reg = rtc_gpio_desc[pin].reg;
  51. //RTC pins PULL settings
  52. if(rtc_reg) {
  53. //lock rtc
  54. ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_gpio_desc[pin].mux);
  55. if(mode & PULLUP) {
  56. ESP_REG(rtc_reg) = (ESP_REG(rtc_reg) | rtc_gpio_desc[pin].pullup) & ~(rtc_gpio_desc[pin].pulldown);
  57. } else if(mode & PULLDOWN) {
  58. ESP_REG(rtc_reg) = (ESP_REG(rtc_reg) | rtc_gpio_desc[pin].pulldown) & ~(rtc_gpio_desc[pin].pullup);
  59. } else {
  60. ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_gpio_desc[pin].pullup | rtc_gpio_desc[pin].pulldown);
  61. }
  62. //unlock rtc
  63. }
  64. uint32_t pinFunction = 0, pinControl = 0;
  65. //lock gpio
  66. if(mode & INPUT) {
  67. if(pin < 32) {
  68. GPIO.enable_w1tc = BIT(pin);
  69. } else {
  70. GPIO.enable1_w1tc.val = BIT(pin - 32);
  71. }
  72. } else if(mode & OUTPUT) {
  73. if(pin > 33) {
  74. //unlock gpio
  75. return;//pins above 33 can be only inputs
  76. } else if(pin < 32) {
  77. GPIO.enable_w1ts = BIT(pin);
  78. } else {
  79. GPIO.enable1_w1ts.val = BIT(pin - 32);
  80. }
  81. }
  82. if(mode & PULLUP) {
  83. pinFunction |= FUN_PU;
  84. } else if(mode & PULLDOWN) {
  85. pinFunction |= FUN_PD;
  86. }
  87. pinFunction |= ((uint32_t)2 << FUN_DRV_S);//what are the drivers?
  88. pinFunction |= FUN_IE;//input enable but required for output as well?
  89. if(mode & (INPUT | OUTPUT)) {
  90. pinFunction |= ((uint32_t)2 << MCU_SEL_S);
  91. } else if(mode == SPECIAL) {
  92. pinFunction |= ((uint32_t)(((pin)==1||(pin)==3)?0:1) << MCU_SEL_S);
  93. } else {
  94. pinFunction |= ((uint32_t)(mode >> 5) << MCU_SEL_S);
  95. }
  96. ESP_REG(DR_REG_IO_MUX_BASE + pin_to_mux[pin]) = pinFunction;
  97. if(mode & OPEN_DRAIN) {
  98. pinControl = (1 << GPIO_PIN0_PAD_DRIVER_S);
  99. }
  100. GPIO.pin[pin].val = pinControl;
  101. //unlock gpio
  102. }
  103. static void digitalWrite(uint8_t pin, uint8_t val)
  104. {
  105. if(val) {
  106. if(pin < 32) {
  107. GPIO.out_w1ts = BIT(pin);
  108. } else if(pin < 34) {
  109. GPIO.out1_w1ts.val = BIT(pin - 32);
  110. }
  111. } else {
  112. if(pin < 32) {
  113. GPIO.out_w1tc = BIT(pin);
  114. } else if(pin < 34) {
  115. GPIO.out1_w1tc.val = BIT(pin - 32);
  116. }
  117. }
  118. }
  119. unsigned char twi_dcount = 18;
  120. static unsigned char twi_sda, twi_scl;
  121. static inline void SDA_LOW()
  122. {
  123. //Enable SDA (becomes output and since GPO is 0 for the pin,
  124. // it will pull the line low)
  125. if (twi_sda < 32) {
  126. GPIO.enable_w1ts = BIT(twi_sda);
  127. } else {
  128. GPIO.enable1_w1ts.val = BIT(twi_sda - 32);
  129. }
  130. }
  131. static inline void SDA_HIGH()
  132. {
  133. //Disable SDA (becomes input and since it has pullup it will go high)
  134. if (twi_sda < 32) {
  135. GPIO.enable_w1tc = BIT(twi_sda);
  136. } else {
  137. GPIO.enable1_w1tc.val = BIT(twi_sda - 32);
  138. }
  139. }
  140. static inline uint32_t SDA_READ()
  141. {
  142. if (twi_sda < 32) {
  143. return (GPIO.in & BIT(twi_sda)) != 0;
  144. } else {
  145. return (GPIO.in1.val & BIT(twi_sda - 32)) != 0;
  146. }
  147. }
  148. static void SCL_LOW()
  149. {
  150. if (twi_scl < 32) {
  151. GPIO.enable_w1ts = BIT(twi_scl);
  152. } else {
  153. GPIO.enable1_w1ts.val = BIT(twi_scl - 32);
  154. }
  155. }
  156. static void SCL_HIGH()
  157. {
  158. if (twi_scl < 32) {
  159. GPIO.enable_w1tc = BIT(twi_scl);
  160. } else {
  161. GPIO.enable1_w1tc.val = BIT(twi_scl - 32);
  162. }
  163. }
  164. static uint32_t SCL_READ()
  165. {
  166. if (twi_scl < 32) {
  167. return (GPIO.in & BIT(twi_scl)) != 0;
  168. } else {
  169. return (GPIO.in1.val & BIT(twi_scl - 32)) != 0;
  170. }
  171. }
  172. #ifndef FCPU80
  173. #define FCPU80 80000000L
  174. #endif
  175. #if F_CPU == FCPU80
  176. #define TWI_CLOCK_STRETCH 800
  177. #else
  178. #define TWI_CLOCK_STRETCH 1600
  179. #endif
  180. void twi_setClock(unsigned int freq)
  181. {
  182. #if F_CPU == FCPU80
  183. if(freq <= 100000) {
  184. twi_dcount = 19; //about 100KHz
  185. } else if(freq <= 200000) {
  186. twi_dcount = 8; //about 200KHz
  187. } else if(freq <= 300000) {
  188. twi_dcount = 3; //about 300KHz
  189. } else if(freq <= 400000) {
  190. twi_dcount = 1; //about 400KHz
  191. } else {
  192. twi_dcount = 1; //about 400KHz
  193. }
  194. #else
  195. if(freq <= 100000) {
  196. twi_dcount = 32; //about 100KHz
  197. } else if(freq <= 200000) {
  198. twi_dcount = 14; //about 200KHz
  199. } else if(freq <= 300000) {
  200. twi_dcount = 8; //about 300KHz
  201. } else if(freq <= 400000) {
  202. twi_dcount = 5; //about 400KHz
  203. } else if(freq <= 500000) {
  204. twi_dcount = 3; //about 500KHz
  205. } else if(freq <= 600000) {
  206. twi_dcount = 2; //about 600KHz
  207. } else {
  208. twi_dcount = 1; //about 700KHz
  209. }
  210. #endif
  211. }
  212. void twi_init(unsigned char sda, unsigned char scl)
  213. {
  214. twi_sda = sda;
  215. twi_scl = scl;
  216. pinMode(twi_sda, OUTPUT);
  217. pinMode(twi_scl, OUTPUT);
  218. digitalWrite(twi_sda, 0);
  219. digitalWrite(twi_scl, 0);
  220. pinMode(twi_sda, INPUT_PULLUP);
  221. pinMode(twi_scl, INPUT_PULLUP);
  222. twi_setClock(100000);
  223. }
  224. void twi_stop(void)
  225. {
  226. pinMode(twi_sda, INPUT);
  227. pinMode(twi_scl, INPUT);
  228. }
  229. static void twi_delay(unsigned char v)
  230. {
  231. unsigned int i;
  232. #pragma GCC diagnostic push
  233. #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
  234. unsigned int reg;
  235. for(i=0; i<v; i++) {
  236. reg = REG_READ(GPIO_IN_REG);
  237. }
  238. #pragma GCC diagnostic pop
  239. }
  240. static bool twi_write_start(void)
  241. {
  242. SCL_HIGH();
  243. SDA_HIGH();
  244. if (SDA_READ() == 0) {
  245. return false;
  246. }
  247. twi_delay(twi_dcount);
  248. SDA_LOW();
  249. twi_delay(twi_dcount);
  250. return true;
  251. }
  252. static bool twi_write_stop(void)
  253. {
  254. unsigned int i = 0;
  255. SCL_LOW();
  256. SDA_LOW();
  257. twi_delay(twi_dcount);
  258. SCL_HIGH();
  259. while (SCL_READ() == 0 && (i++) < TWI_CLOCK_STRETCH);// Clock stretching (up to 100us)
  260. twi_delay(twi_dcount);
  261. SDA_HIGH();
  262. twi_delay(twi_dcount);
  263. return true;
  264. }
  265. bool do_log = false;
  266. static bool twi_write_bit(bool bit)
  267. {
  268. unsigned int i = 0;
  269. SCL_LOW();
  270. if (bit) {
  271. SDA_HIGH();
  272. if (do_log) {
  273. twi_delay(twi_dcount+1);
  274. }
  275. } else {
  276. SDA_LOW();
  277. if (do_log) {}
  278. }
  279. twi_delay(twi_dcount+1);
  280. SCL_HIGH();
  281. while (SCL_READ() == 0 && (i++) < TWI_CLOCK_STRETCH);// Clock stretching (up to 100us)
  282. twi_delay(twi_dcount);
  283. return true;
  284. }
  285. static bool twi_read_bit(void)
  286. {
  287. unsigned int i = 0;
  288. SCL_LOW();
  289. SDA_HIGH();
  290. twi_delay(twi_dcount+2);
  291. SCL_HIGH();
  292. while (SCL_READ() == 0 && (i++) < TWI_CLOCK_STRETCH);// Clock stretching (up to 100us)
  293. bool bit = SDA_READ();
  294. twi_delay(twi_dcount);
  295. return bit;
  296. }
  297. static bool twi_write_byte(unsigned char byte)
  298. {
  299. if (byte == 0x43) {
  300. // printf("TWB %02x ", (uint32_t) byte);
  301. // do_log = true;
  302. }
  303. unsigned char bit;
  304. for (bit = 0; bit < 8; bit++) {
  305. twi_write_bit((byte & 0x80) != 0);
  306. byte <<= 1;
  307. }
  308. if (do_log) {
  309. printf("\n");
  310. do_log = false;
  311. }
  312. return !twi_read_bit();//NACK/ACK
  313. }
  314. static unsigned char twi_read_byte(bool nack)
  315. {
  316. unsigned char byte = 0;
  317. unsigned char bit;
  318. for (bit = 0; bit < 8; bit++) {
  319. byte = (byte << 1) | twi_read_bit();
  320. }
  321. twi_write_bit(nack);
  322. return byte;
  323. }
  324. unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop)
  325. {
  326. unsigned int i;
  327. if(!twi_write_start()) {
  328. return 4; //line busy
  329. }
  330. if(!twi_write_byte(((address << 1) | 0) & 0xFF)) {
  331. if (sendStop) {
  332. twi_write_stop();
  333. }
  334. return 2; //received NACK on transmit of address
  335. }
  336. for(i=0; i<len; i++) {
  337. if(!twi_write_byte(buf[i])) {
  338. if (sendStop) {
  339. twi_write_stop();
  340. }
  341. return 3;//received NACK on transmit of data
  342. }
  343. }
  344. if(sendStop) {
  345. twi_write_stop();
  346. }
  347. i = 0;
  348. while(SDA_READ() == 0 && (i++) < 10) {
  349. SCL_LOW();
  350. twi_delay(twi_dcount);
  351. SCL_HIGH();
  352. twi_delay(twi_dcount);
  353. }
  354. return 0;
  355. }
  356. unsigned char twi_readFrom(unsigned char address, unsigned char* buf, unsigned int len, unsigned char sendStop)
  357. {
  358. unsigned int i;
  359. if(!twi_write_start()) {
  360. return 4; //line busy
  361. }
  362. if(!twi_write_byte(((address << 1) | 1) & 0xFF)) {
  363. if (sendStop) {
  364. twi_write_stop();
  365. }
  366. return 2;//received NACK on transmit of address
  367. }
  368. for(i=0; i<(len-1); i++) {
  369. buf[i] = twi_read_byte(false);
  370. }
  371. buf[len-1] = twi_read_byte(true);
  372. if(sendStop) {
  373. twi_write_stop();
  374. }
  375. i = 0;
  376. while(SDA_READ() == 0 && (i++) < 10) {
  377. SCL_LOW();
  378. twi_delay(twi_dcount);
  379. SCL_HIGH();
  380. twi_delay(twi_dcount);
  381. }
  382. return 0;
  383. }