hpm_smbus.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (c) 2023 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include "hpm_smbus.h"
  8. static uint8_t hpm_smbus_pec_crc8(uint8_t *data, uint32_t len);
  9. hpm_stat_t hpm_smbus_master_write_byte(I2C_Type *ptr, uint8_t slave_address, uint8_t data)
  10. {
  11. hpm_stat_t stat;
  12. uint8_t buf[3];
  13. /* addr + rw bit*/
  14. buf[0] = slave_address << 1;
  15. buf[1] = data;
  16. buf[2] = hpm_smbus_pec_crc8(buf, 2);
  17. stat = i2c_master_write(ptr, (const uint16_t)slave_address, &buf[1], sizeof(buf) - 1);
  18. return stat;
  19. }
  20. hpm_stat_t hpm_smbus_master_read_byte(I2C_Type *ptr, uint8_t slave_address, uint8_t *data)
  21. {
  22. uint8_t buf[3];
  23. hpm_stat_t stat;
  24. uint8_t pec;
  25. /* addr + rw bit*/
  26. buf[0] = (slave_address << 1);
  27. stat = i2c_master_read(ptr, (const uint16_t)slave_address, &buf[1], sizeof(buf) - 1);
  28. if (stat == status_success) {
  29. pec = hpm_smbus_pec_crc8(buf, sizeof(buf) - 1);
  30. if (pec == buf[2]) {
  31. *data = buf[1];
  32. } else {
  33. stat = status_fail;
  34. }
  35. }
  36. return stat;
  37. }
  38. hpm_stat_t hpm_smbus_master_write_byte_in_command(I2C_Type *ptr, uint8_t slave_address, uint8_t command, uint8_t data)
  39. {
  40. hpm_stat_t stat;
  41. uint8_t buf[4];
  42. /* addr + rw bit*/
  43. buf[0] = slave_address << 1;
  44. buf[1] = command;
  45. buf[2] = data;
  46. buf[3] = hpm_smbus_pec_crc8(buf, sizeof(buf) - 1);
  47. stat = i2c_master_write(ptr, (const uint16_t)slave_address, &buf[1], sizeof(buf) - 1);
  48. return stat;
  49. }
  50. hpm_stat_t hpm_smbus_master_write_word_in_command(I2C_Type *ptr, uint8_t slave_address, uint8_t command, uint16_t data)
  51. {
  52. hpm_stat_t stat;
  53. uint8_t pec;
  54. uint8_t buf[5];
  55. /* addr + rw bit*/
  56. buf[0] = slave_address << 1;
  57. buf[1] = command;
  58. *(uint16_t *)(&buf[2]) = data;
  59. buf[4] = hpm_smbus_pec_crc8(buf, sizeof(buf) - 1);
  60. stat = i2c_master_write(ptr, (const uint16_t)slave_address, &buf[1], sizeof(buf) - 1);
  61. return stat;
  62. }
  63. hpm_stat_t hpm_smbus_master_read_byte_in_command(I2C_Type *ptr, uint8_t slave_address, uint8_t command, uint8_t *data)
  64. {
  65. hpm_stat_t stat;
  66. uint8_t pec;
  67. uint8_t buf[5];
  68. /* addr + rw bit*/
  69. buf[0] = (slave_address << 1);
  70. buf[1] = command;
  71. /* write command code in smbus spec*/
  72. stat = i2c_master_seq_transmit(ptr, (const uint16_t)slave_address, &command, sizeof(uint8_t), i2c_frist_frame);
  73. if (stat == status_success) {
  74. /* read */
  75. buf[2] = (slave_address << 1) | 0x01;
  76. /* now change dir,restart, read the byte */
  77. stat = i2c_master_seq_receive(ptr, (const uint16_t)slave_address, &buf[3], 1, i2c_frist_frame);
  78. /* read the pec */
  79. stat = i2c_master_seq_receive(ptr, (const uint16_t)slave_address, &buf[4], 1, i2c_last_frame);
  80. if (stat == status_success) {
  81. pec = hpm_smbus_pec_crc8(buf, sizeof(buf) - 1);
  82. if (pec == buf[4]) {
  83. *data = buf[3];
  84. } else {
  85. stat = status_fail;
  86. }
  87. }
  88. }
  89. return stat;
  90. }
  91. hpm_stat_t hpm_smbus_master_read_word_in_command(I2C_Type *ptr, uint8_t slave_address, uint8_t command, uint16_t *data)
  92. {
  93. hpm_stat_t stat;
  94. uint8_t pec;
  95. uint8_t buf[6];
  96. /* addr + rw bit*/
  97. buf[0] = (slave_address << 1);
  98. buf[1] = command;
  99. /* write command code in smbus spec*/
  100. stat = i2c_master_seq_transmit(ptr, (const uint16_t)slave_address, &command, sizeof(uint8_t), i2c_frist_frame);
  101. if (stat == status_success) {
  102. /* read */
  103. buf[2] = (slave_address << 1) | 0x01;
  104. /* now change dir,restart, read the word (16 bits)*/
  105. stat = i2c_master_seq_receive(ptr, (const uint16_t)slave_address, &buf[3], 2, i2c_frist_frame);
  106. /* read the pec */
  107. stat = i2c_master_seq_receive(ptr, (const uint16_t)slave_address, &buf[5], 1, i2c_last_frame);
  108. if (stat == status_success) {
  109. pec = hpm_smbus_pec_crc8(buf, sizeof(buf) - 1);
  110. if (pec == buf[5]) {
  111. *data = *(uint16_t *)(&buf[3]);
  112. } else {
  113. stat = status_fail;
  114. }
  115. }
  116. }
  117. return stat;
  118. }
  119. hpm_stat_t hpm_smbus_master_write_block_in_command(I2C_Type *ptr, uint8_t slave_address, uint8_t command, uint8_t *data, uint32_t size)
  120. {
  121. hpm_stat_t stat;
  122. uint8_t pec;
  123. uint8_t buf[I2C_SOC_TRANSFER_COUNT_MAX];
  124. uint16_t buf_size;
  125. /* frame included addr, command, data, and pec */
  126. assert(size > 0 && size <= (I2C_SOC_TRANSFER_COUNT_MAX - 3));
  127. /* addr + rw bit*/
  128. buf[0] = slave_address << 1;
  129. buf[1] = command;
  130. buf[2] = size;
  131. memcpy(&buf[3], data, size);
  132. buf[size + 3] = hpm_smbus_pec_crc8(buf, size + 3);
  133. buf_size = size + 4;
  134. stat = i2c_master_write(ptr, (const uint16_t)slave_address, buf, buf_size);
  135. return stat;
  136. }
  137. hpm_stat_t hpm_smbus_master_read_block_in_command(I2C_Type *ptr, uint8_t slave_address, uint8_t command, uint8_t *data, uint32_t size)
  138. {
  139. hpm_stat_t stat;
  140. uint8_t pec;
  141. uint8_t buf[I2C_SOC_TRANSFER_COUNT_MAX];
  142. uint16_t buf_size;
  143. /* frame included addr, command, data, and pec */
  144. assert(size > 0 && size <= (I2C_SOC_TRANSFER_COUNT_MAX - 3));
  145. /* addr + rw bit*/
  146. buf[0] = (slave_address << 1);
  147. buf[1] = command;
  148. /* write command code in smbus spec*/
  149. stat = i2c_master_seq_transmit(ptr, (const uint16_t)slave_address, &command, sizeof(uint8_t), i2c_frist_frame);
  150. /* read */
  151. buf[2] = (slave_address << 1) | 0x01;
  152. if (stat == status_success) {
  153. /* now change dir,restart, read the block count*/
  154. stat = i2c_master_seq_receive(ptr, (const uint16_t)slave_address, &buf[3], 1, i2c_frist_frame);
  155. /* read data*/
  156. stat = i2c_master_seq_receive(ptr, (const uint16_t)slave_address, &buf[4], size, i2c_next_frame);
  157. /* read pec */
  158. stat = i2c_master_seq_receive(ptr, (const uint16_t)slave_address, &buf[size + 4], 1, i2c_last_frame);
  159. if (stat == status_success) {
  160. pec = hpm_smbus_pec_crc8(buf, sizeof(buf) - 1);
  161. if (pec == buf[size + 4]) {
  162. memcpy(data, &buf[4], size);
  163. } else {
  164. stat = status_fail;
  165. }
  166. }
  167. }
  168. return stat;
  169. }
  170. hpm_stat_t hpm_smbus_master_write(I2C_Type *ptr, uint8_t slave_address, uint8_t *data, uint32_t size)
  171. {
  172. hpm_stat_t stat;
  173. uint8_t pec;
  174. uint8_t buf[I2C_SOC_TRANSFER_COUNT_MAX];
  175. uint16_t buf_size;
  176. /* frame included addr, data, and pec */
  177. assert(size > 0 && size <= (I2C_SOC_TRANSFER_COUNT_MAX - 1));
  178. /* addr + rw bit*/
  179. buf[0] = (slave_address << 1) | 0x01;
  180. memcpy(&buf[1], data, size);
  181. buf[size + 1] = hpm_smbus_pec_crc8(buf, size + 1);
  182. buf_size = size + 1;
  183. stat = i2c_master_write(ptr, (const uint16_t)slave_address, &buf[1], buf_size);
  184. return stat;
  185. }
  186. hpm_stat_t hpm_smbus_master_read(I2C_Type *ptr, uint8_t slave_address, uint8_t *data, uint32_t size)
  187. {
  188. hpm_stat_t stat;
  189. uint8_t pec;
  190. uint8_t buf[I2C_SOC_TRANSFER_COUNT_MAX];
  191. uint16_t buf_size;
  192. /* frame included addr, data, and pec */
  193. assert(size > 0 && size <= (I2C_SOC_TRANSFER_COUNT_MAX - 2));
  194. buf[0] = (slave_address << 1);
  195. stat = i2c_master_read(ptr, slave_address, &buf[1], size + 1);
  196. if (stat == status_success) {
  197. pec = hpm_smbus_pec_crc8(buf, size + 1);
  198. if (pec == buf[size + 1]) {
  199. memcpy(data, &buf[1], size);
  200. } else {
  201. stat = status_fail;
  202. }
  203. }
  204. return stat;
  205. }
  206. hpm_stat_t hpm_smbus_slave_write(I2C_Type *ptr, uint8_t *data, uint32_t size)
  207. {
  208. hpm_stat_t stat;
  209. uint8_t pec;
  210. uint8_t buf[I2C_SOC_TRANSFER_COUNT_MAX];
  211. uint16_t buf_size;
  212. uint8_t slave_address;
  213. /* frame included addr, data, and pec */
  214. assert(size > 0 && size <= (I2C_SOC_TRANSFER_COUNT_MAX - 1));
  215. slave_address = ptr->ADDR;
  216. /* addr + rw bit*/
  217. buf[0] = (slave_address << 1);
  218. memcpy(&buf[1], data, size);
  219. buf[size + 1] = hpm_smbus_pec_crc8(buf, size + 1);
  220. buf_size = size + 1;
  221. stat = i2c_slave_write(ptr, &buf[1], buf_size);
  222. return stat;
  223. }
  224. hpm_stat_t hpm_smbus_slave_read(I2C_Type *ptr, uint8_t *data, uint32_t size)
  225. {
  226. hpm_stat_t stat;
  227. uint8_t pec;
  228. uint8_t buf[I2C_SOC_TRANSFER_COUNT_MAX];
  229. uint16_t buf_size;
  230. uint8_t slave_address;
  231. /* frame included addr, data, and pec */
  232. assert(size > 0 && size <= (I2C_SOC_TRANSFER_COUNT_MAX - 2));
  233. /* addr + rw bit*/
  234. slave_address = ptr->ADDR;
  235. buf[0] = (slave_address << 1) | 0x01;
  236. stat = i2c_slave_read(ptr, &buf[1], size + 1);
  237. if (stat == status_success) {
  238. pec = hpm_smbus_pec_crc8(buf, size + 1);
  239. if (pec == buf[size + 1]) {
  240. memcpy(data, &buf[1], size);
  241. } else {
  242. stat = status_fail;
  243. }
  244. }
  245. return stat;
  246. }
  247. static uint8_t hpm_smbus_pec_crc8(uint8_t *data, uint32_t len)
  248. {
  249. /* The PEC is a CRC-8 error-checking byte, calculated on all the message bytes (including addresses and read/write bits) */
  250. uint32_t i;
  251. uint8_t crc = 0x00;
  252. while (len--) {
  253. crc ^= *data++;
  254. for (i = 0; i < 8; i++) {
  255. if (crc & 0x80) {
  256. crc = (crc << 1) ^ 0x07;
  257. } else {
  258. crc <<= 1;
  259. }
  260. }
  261. }
  262. return crc;
  263. }