i2c.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * (c) danielinux 2019
  3. * GPLv.2
  4. *
  5. * See LICENSE for details
  6. */
  7. #include <stdint.h>
  8. #include <stdlib.h>
  9. #include "system.h"
  10. #include "i2c.h"
  11. #include "display.h"
  12. #define DISPLAY_I2C_ADDR 0x3C
  13. #define DJHERO_I2C_ADDR 0x52
  14. //#define DJHERO_I2C_ADDR 0x29
  15. #define I2C1 (0x40005400)
  16. #define APB1_SPEED_IN_MHZ (42)
  17. #define I2C1_CR1 (*(volatile uint32_t *)(I2C1))
  18. #define I2C1_CR2 (*(volatile uint32_t *)(I2C1 + 0x04))
  19. #define I2C1_OAR1 (*(volatile uint32_t *)(I2C1 + 0x08))
  20. #define I2C1_OAR2 (*(volatile uint32_t *)(I2C1 + 0x0c))
  21. #define I2C1_DR (*(volatile uint32_t *)(I2C1 + 0x10))
  22. #define I2C1_SR1 (*(volatile uint32_t *)(I2C1 + 0x14))
  23. #define I2C1_SR2 (*(volatile uint32_t *)(I2C1 + 0x18))
  24. #define I2C1_CCR (*(volatile uint32_t *)(I2C1 + 0x1c))
  25. #define I2C1_TRISE (*(volatile uint32_t *)(I2C1 + 0x20))
  26. #define I2C_CR1_ENABLE (1 << 0)
  27. #define I2C_CR1_START (1 << 8)
  28. #define I2C_CR1_STOP (1 << 9)
  29. #define I2C_CR1_ACK (1 << 10)
  30. #define I2C_CR2_FREQ_MASK (0x3ff)
  31. #define I2C_CCR_MASK (0xfff)
  32. #define I2C_TRISE_MASK (0x3f)
  33. #define I2C_SR1_START (1 << 0)
  34. #define I2C_SR1_TX_BTF (1 << 2)
  35. #define I2C_SR1_ADDR_SENT (1 << 1)
  36. #define I2C_SR1_RX_NOTEMPTY (1 << 6)
  37. #define I2C_SR1_TX_EMPTY (1 << 7)
  38. #define I2C_SR2_MASTER (1 << 0)
  39. #define I2C_SR2_BUSY (1 << 1)
  40. #define I2C_SR2_XMIT (1 << 2)
  41. #define APB1_CLOCK_ER (*(volatile uint32_t *)(0x40023840))
  42. #define APB1_CLOCK_RST (*(volatile uint32_t *)(0x40023820))
  43. #define I2C1_APB1_CLOCK_ER_VAL (1 << 21)
  44. static void i2c1_pins_setup(void)
  45. {
  46. uint32_t reg;
  47. AHB1_CLOCK_ER |= GPIOB_AHB1_CLOCK_ER;
  48. /* Set mode = AF */
  49. reg = GPIOB_MODE & ~ (0x03 << (I2C1_SCL * 2));
  50. GPIOB_MODE = reg | (2 << (I2C1_SCL * 2));
  51. reg = GPIOB_MODE & ~ (0x03 << (I2C1_SDA * 2));
  52. GPIOB_MODE = reg | (2 << (I2C1_SDA * 2));
  53. /* Set pull-up */
  54. #if 0
  55. reg = GPIOB_PUPD & (0x03 << (I2C1_SCL * 2));
  56. GPIOB_PUPD = reg | (0x01 << (I2C1_SCL * 2));
  57. reg = GPIOB_PUPD & (0x03 << (I2C1_SDA * 2));
  58. GPIOB_PUPD = reg | (0x01 << (I2C1_SDA * 2));
  59. #endif
  60. /* Alternate function: */
  61. reg = GPIOB_AFL & ~(0xf << ((I2C1_SCL) * 4));
  62. GPIOB_AFL = reg | (I2C1_PIN_AF << ((I2C1_SCL) * 4));
  63. reg = GPIOB_AFL & ~(0xf << ((I2C1_SDA) * 4));
  64. GPIOB_AFL = reg | (I2C1_PIN_AF << ((I2C1_SDA) * 4));
  65. }
  66. static void i2c1_reset(void)
  67. {
  68. APB1_CLOCK_RST |= I2C1_APB1_CLOCK_ER_VAL;
  69. APB1_CLOCK_RST &= ~I2C1_APB1_CLOCK_ER_VAL;
  70. }
  71. static void i2c1_send_start(void)
  72. {
  73. volatile uint32_t sr1;
  74. I2C1_CR1 |= I2C_CR1_START;
  75. do {
  76. sr1 = I2C1_SR1;
  77. } while ((sr1 & I2C_SR1_START) == 0);;
  78. }
  79. static void i2c1_send_stop(void)
  80. {
  81. I2C1_CR1 |= I2C_CR1_STOP;
  82. }
  83. void display_send_data(void *priv, const uint8_t *buf, int len)
  84. {
  85. volatile uint32_t sr1, sr2;
  86. int i;
  87. volatile uint8_t drval;
  88. uint32_t start_data = 0x00000040;
  89. uint8_t address = DISPLAY_I2C_ADDR;
  90. I2C1_CR1 &= ~I2C_CR1_ENABLE;
  91. I2C1_CR1 &= ~I2C_CR1_STOP;
  92. I2C1_CR1 &= ~I2C_CR1_ACK;
  93. I2C1_CR1 |= I2C_CR1_ENABLE;
  94. /* Wait if the bus is busy */
  95. do {
  96. sr2 = I2C1_SR2;
  97. } while ((sr2 & I2C_SR2_BUSY) != 0);;
  98. /* Send a start condition */
  99. i2c1_send_start();
  100. /* Send address + R/W = 0 */
  101. I2C1_DR = (address << 1);
  102. do {
  103. sr1 = I2C1_SR1;
  104. } while ((sr1 & I2C_SR1_ADDR_SENT) != I2C_SR1_ADDR_SENT);
  105. do {
  106. sr2 = I2C1_SR2;
  107. } while ((sr2 & (I2C_SR2_BUSY | I2C_SR2_MASTER)) != (I2C_SR2_BUSY | I2C_SR2_MASTER));;
  108. I2C1_DR = start_data;
  109. do {
  110. sr1 = I2C1_SR1;
  111. } while ((sr1 & (I2C_SR1_TX_EMPTY)) == 0);
  112. for (i = 0; i < len; i++) {
  113. I2C1_DR = buf[i];
  114. do {
  115. sr1 = I2C1_SR1;
  116. if ((sr1 & I2C_SR1_RX_NOTEMPTY) == I2C_SR1_RX_NOTEMPTY) {
  117. drval = I2C1_DR;
  118. }
  119. } while ((sr1 & (I2C_SR1_TX_EMPTY)) == 0);
  120. }
  121. while ((sr1 & (I2C_SR1_TX_BTF)) == 0) {
  122. sr1 = I2C1_SR1;
  123. }
  124. i2c1_send_stop();
  125. }
  126. int i2c1_sendrecv(uint8_t address, const uint8_t *wbuf, int wlen, uint8_t *rbuf, int rlen)
  127. {
  128. volatile uint32_t sr1, sr2;
  129. int i;
  130. volatile uint8_t drval;
  131. I2C1_CR1 &= ~I2C_CR1_ENABLE;
  132. I2C1_CR1 &= ~I2C_CR1_STOP;
  133. I2C1_CR1 &= ~I2C_CR1_ACK;
  134. I2C1_CR1 |= I2C_CR1_ENABLE;
  135. /* Wait if the bus is busy */
  136. do {
  137. sr2 = I2C1_SR2;
  138. } while ((sr2 & I2C_SR2_BUSY) != 0);;
  139. /* Send a start condition */
  140. i2c1_send_start();
  141. /* Send address + R/W = 0 */
  142. I2C1_DR = (address << 1);
  143. do {
  144. sr1 = I2C1_SR1;
  145. } while ((sr1 & I2C_SR1_ADDR_SENT) != I2C_SR1_ADDR_SENT);
  146. do {
  147. sr2 = I2C1_SR2;
  148. } while ((sr2 & (I2C_SR2_BUSY | I2C_SR2_MASTER)) != (I2C_SR2_BUSY | I2C_SR2_MASTER));;
  149. for (i = 0; i < wlen; i++) {
  150. I2C1_DR = wbuf[i];
  151. do {
  152. sr1 = I2C1_SR1;
  153. if ((sr1 & I2C_SR1_RX_NOTEMPTY) == I2C_SR1_RX_NOTEMPTY) {
  154. drval = I2C1_DR;
  155. }
  156. } while ((sr1 & (I2C_SR1_TX_EMPTY)) == 0);
  157. }
  158. for (i = 0; i < rlen; i++) {
  159. I2C1_DR = 0xFF;
  160. do {
  161. sr1 = I2C1_SR1;
  162. if ((sr1 & I2C_SR1_RX_NOTEMPTY) == I2C_SR1_RX_NOTEMPTY) {
  163. rbuf[i] = I2C1_DR;
  164. }
  165. } while ((sr1 & (I2C_SR1_TX_EMPTY)) == 0);
  166. }
  167. while ((sr1 & (I2C_SR1_TX_BTF)) == 0) {
  168. sr1 = I2C1_SR1;
  169. }
  170. i2c1_send_stop();
  171. return i;
  172. }
  173. int i2c1_send(uint8_t address, const uint8_t *buf, int len)
  174. {
  175. volatile uint32_t sr1, sr2;
  176. int i;
  177. volatile uint8_t drval;
  178. I2C1_CR1 &= ~I2C_CR1_ENABLE;
  179. I2C1_CR1 &= ~I2C_CR1_STOP;
  180. I2C1_CR1 &= ~I2C_CR1_ACK;
  181. I2C1_CR1 |= I2C_CR1_ENABLE;
  182. /* Wait if the bus is busy */
  183. do {
  184. sr2 = I2C1_SR2;
  185. } while ((sr2 & I2C_SR2_BUSY) != 0);;
  186. /* Send a start condition */
  187. i2c1_send_start();
  188. /* Send address + R/W = 0 */
  189. I2C1_DR = (address << 1);
  190. do {
  191. sr1 = I2C1_SR1;
  192. } while ((sr1 & I2C_SR1_ADDR_SENT) != I2C_SR1_ADDR_SENT);
  193. do {
  194. sr2 = I2C1_SR2;
  195. } while ((sr2 & (I2C_SR2_BUSY | I2C_SR2_MASTER)) != (I2C_SR2_BUSY | I2C_SR2_MASTER));;
  196. for (i = 0; i < len; i++) {
  197. I2C1_DR = buf[i];
  198. do {
  199. sr1 = I2C1_SR1;
  200. if ((sr1 & I2C_SR1_RX_NOTEMPTY) == I2C_SR1_RX_NOTEMPTY) {
  201. drval = I2C1_DR;
  202. }
  203. } while ((sr1 & (I2C_SR1_TX_EMPTY)) == 0);
  204. }
  205. while ((sr1 & (I2C_SR1_TX_BTF)) == 0) {
  206. sr1 = I2C1_SR1;
  207. }
  208. i2c1_send_stop();
  209. return i;
  210. }
  211. void i2c1_setup(void)
  212. {
  213. uint32_t reg;
  214. i2c1_pins_setup();
  215. APB1_CLOCK_ER |= I2C1_APB1_CLOCK_ER_VAL;
  216. I2C1_CR1 &= ~I2C_CR1_ENABLE;
  217. i2c1_reset();
  218. reg = I2C1_CR2 & ~(I2C_CR2_FREQ_MASK);
  219. I2C1_CR2 = reg | APB1_SPEED_IN_MHZ;
  220. reg = I2C1_CCR & ~(I2C_CCR_MASK);
  221. I2C1_CCR = reg | (APB1_SPEED_IN_MHZ * 5); /* 400 Khz */
  222. // I2C1_CCR = reg | (APB1_SPEED_IN_MHZ * 20); /* 100 Khz */
  223. // I2C1_CCR = reg | (APB1_SPEED_IN_MHZ * 40); /* 50 Khz */
  224. reg = I2C1_TRISE & ~(I2C_TRISE_MASK);
  225. I2C1_TRISE = reg | (APB1_SPEED_IN_MHZ + 1);
  226. I2C1_CR1 |= I2C_CR1_ENABLE;
  227. }
  228. void i2c_display_init(void)
  229. {
  230. display_init(NULL);
  231. }
  232. void display_send_cmd(void *priv, uint8_t cmd)
  233. {
  234. uint8_t buf[2] = {0x00, cmd};
  235. volatile int j;
  236. i2c1_send(DISPLAY_I2C_ADDR, buf, 2);
  237. }
  238. void display_send_cmd1(void *priv, uint8_t cmd, uint8_t arg1)
  239. {
  240. uint8_t buf[3] = {0x00, cmd, arg1};
  241. volatile int j;
  242. i2c1_send(DISPLAY_I2C_ADDR, buf, 3);
  243. }
  244. void display_send_cmd2(void *priv, uint8_t cmd, uint8_t arg1, uint8_t arg2)
  245. {
  246. uint8_t buf[4] = {0x00, cmd, arg1, arg2};
  247. volatile int j;
  248. i2c1_send(DISPLAY_I2C_ADDR, buf, 3);
  249. }
  250. void djhero_init(void)
  251. {
  252. const uint8_t cmd1[2] = { 0xf0, 0x55 };
  253. const uint8_t cmd2[2] = { 0xfb, 0x00 };
  254. const uint8_t cmdquery[1] = { 0xFA };
  255. uint8_t serial[6];
  256. i2c1_send(DJHERO_I2C_ADDR, cmd1, 2);
  257. i2c1_send(DJHERO_I2C_ADDR, cmd2, 2);
  258. i2c1_sendrecv(DJHERO_I2C_ADDR, cmdquery, 1, serial, 6);
  259. }