i2c_cxx.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifdef __cpp_exceptions
  7. #include "driver/i2c.h"
  8. #include "i2c_cxx.hpp"
  9. using namespace std;
  10. namespace idf {
  11. #define I2C_CHECK_THROW(err) CHECK_THROW_SPECIFIC((err), I2CException)
  12. /**
  13. * I2C bus are defined in the header files, let's check that the values are correct
  14. */
  15. #if SOC_I2C_NUM >= 2
  16. static_assert(I2C_NUM_1 == 1, "I2C_NUM_1 must be equal to 1");
  17. #endif // SOC_I2C_NUM >= 2
  18. static_assert(I2C_NUM_MAX == SOC_I2C_NUM, "I2C_NUM_MAX must be equal to SOC_I2C_NUM");
  19. namespace {
  20. i2c_port_t i2c_num_to_driver_type(I2CNumber num) {
  21. return static_cast<i2c_port_t>(num.get_num());
  22. }
  23. }
  24. esp_err_t check_i2c_num(uint32_t i2c_num) noexcept
  25. {
  26. if (i2c_num >= I2C_NUM_MAX) {
  27. return ESP_ERR_INVALID_ARG;
  28. }
  29. return ESP_OK;
  30. }
  31. esp_err_t check_i2c_addr(uint32_t addr) noexcept
  32. {
  33. // maximum I2C address currently supported in the C++ classes is 127
  34. if (addr > 0x7f) {
  35. return ESP_ERR_INVALID_ARG;
  36. }
  37. return ESP_OK;
  38. }
  39. I2CException::I2CException(esp_err_t error) : ESPException(error) { }
  40. I2CTransferException::I2CTransferException(esp_err_t error) : I2CException(error) { }
  41. uint32_t I2CNumber::get_num()
  42. {
  43. return get_value();
  44. }
  45. I2CAddress::I2CAddress(uint8_t addr) : StrongValueComparable<uint8_t> (addr)
  46. {
  47. esp_err_t error = check_i2c_addr(addr);
  48. if (error != ESP_OK) {
  49. throw I2CException(error);
  50. }
  51. }
  52. uint8_t I2CAddress::get_addr()
  53. {
  54. return get_value();
  55. }
  56. I2CCommandLink::I2CCommandLink()
  57. {
  58. handle = i2c_cmd_link_create();
  59. if (!handle) {
  60. throw I2CException(ESP_ERR_NO_MEM);
  61. }
  62. }
  63. I2CCommandLink::~I2CCommandLink()
  64. {
  65. i2c_cmd_link_delete(handle);
  66. }
  67. void I2CCommandLink::start()
  68. {
  69. I2C_CHECK_THROW(i2c_master_start(handle));
  70. }
  71. void I2CCommandLink::write(const std::vector<uint8_t> &bytes, bool expect_ack)
  72. {
  73. I2C_CHECK_THROW(i2c_master_write(handle, bytes.data(), bytes.size(), expect_ack));
  74. }
  75. void I2CCommandLink::write_byte(uint8_t byte, bool expect_ack)
  76. {
  77. I2C_CHECK_THROW(i2c_master_write_byte(handle, byte, expect_ack));
  78. }
  79. void I2CCommandLink::read(std::vector<uint8_t> &bytes)
  80. {
  81. I2C_CHECK_THROW(i2c_master_read(handle, bytes.data(), bytes.size(), I2C_MASTER_LAST_NACK));
  82. }
  83. void I2CCommandLink::stop()
  84. {
  85. I2C_CHECK_THROW(i2c_master_stop(handle));
  86. }
  87. void I2CCommandLink::execute_transfer(I2CNumber i2c_num, chrono::milliseconds driver_timeout)
  88. {
  89. esp_err_t err = i2c_master_cmd_begin(i2c_num_to_driver_type(i2c_num), handle, driver_timeout.count() / portTICK_PERIOD_MS);
  90. if (err != ESP_OK) {
  91. throw I2CTransferException(err);
  92. }
  93. }
  94. I2CBus::I2CBus(I2CNumber i2c_number) : i2c_num(std::move(i2c_number)) { }
  95. I2CBus::~I2CBus() { }
  96. I2CMaster::I2CMaster(I2CNumber i2c_number,
  97. SCL_GPIO scl_gpio,
  98. SDA_GPIO sda_gpio,
  99. Frequency clock_speed,
  100. bool scl_pullup,
  101. bool sda_pullup)
  102. : I2CBus(std::move(i2c_number))
  103. {
  104. i2c_config_t conf = {};
  105. conf.mode = I2C_MODE_MASTER;
  106. conf.scl_io_num = scl_gpio.get_num();
  107. conf.scl_pullup_en = scl_pullup;
  108. conf.sda_io_num = sda_gpio.get_num();
  109. conf.sda_pullup_en = sda_pullup;
  110. conf.master.clk_speed = clock_speed.get_value();
  111. I2C_CHECK_THROW(i2c_param_config(i2c_num_to_driver_type(i2c_num), &conf));
  112. I2C_CHECK_THROW(i2c_driver_install(i2c_num_to_driver_type(i2c_num), conf.mode, 0, 0, 0));
  113. }
  114. I2CMaster::~I2CMaster()
  115. {
  116. i2c_driver_delete(i2c_num_to_driver_type(i2c_num));
  117. }
  118. void I2CMaster::sync_write(I2CAddress i2c_addr, const vector<uint8_t> &data)
  119. {
  120. I2CWrite writer(data);
  121. writer.do_transfer(i2c_num, i2c_addr);
  122. }
  123. std::vector<uint8_t> I2CMaster::sync_read(I2CAddress i2c_addr, size_t n_bytes)
  124. {
  125. I2CRead reader(n_bytes);
  126. return reader.do_transfer(i2c_num, i2c_addr);
  127. }
  128. vector<uint8_t> I2CMaster::sync_transfer(I2CAddress i2c_addr,
  129. const std::vector<uint8_t> &write_data,
  130. size_t read_n_bytes)
  131. {
  132. I2CComposed composed_transfer;
  133. composed_transfer.add_write(write_data);
  134. composed_transfer.add_read(read_n_bytes);
  135. return composed_transfer.do_transfer(i2c_num, i2c_addr)[0];
  136. }
  137. #if CONFIG_SOC_I2C_SUPPORT_SLAVE
  138. I2CSlave::I2CSlave(I2CNumber i2c_number,
  139. SCL_GPIO scl_gpio,
  140. SDA_GPIO sda_gpio,
  141. I2CAddress slave_addr,
  142. size_t rx_buf_len,
  143. size_t tx_buf_len,
  144. bool scl_pullup,
  145. bool sda_pullup)
  146. : I2CBus(std::move(i2c_number))
  147. {
  148. i2c_config_t conf = {};
  149. conf.mode = I2C_MODE_SLAVE;
  150. conf.scl_io_num = scl_gpio.get_value();
  151. conf.scl_pullup_en = scl_pullup;
  152. conf.sda_io_num = sda_gpio.get_value();
  153. conf.sda_pullup_en = sda_pullup;
  154. conf.slave.addr_10bit_en = 0;
  155. conf.slave.slave_addr = slave_addr.get_addr();
  156. I2C_CHECK_THROW(i2c_param_config(i2c_num_to_driver_type(i2c_num), &conf));
  157. I2C_CHECK_THROW(i2c_driver_install(i2c_num_to_driver_type(i2c_num), conf.mode, rx_buf_len, tx_buf_len, 0));
  158. }
  159. I2CSlave::~I2CSlave()
  160. {
  161. i2c_driver_delete(i2c_num_to_driver_type(i2c_num));
  162. }
  163. int I2CSlave::write_raw(const uint8_t *data, size_t data_len, chrono::milliseconds timeout)
  164. {
  165. return i2c_slave_write_buffer(i2c_num_to_driver_type(i2c_num), data, data_len, (TickType_t) timeout.count() / portTICK_PERIOD_MS);
  166. }
  167. int I2CSlave::read_raw(uint8_t *buffer, size_t buffer_len, chrono::milliseconds timeout)
  168. {
  169. return i2c_slave_read_buffer(i2c_num_to_driver_type(i2c_num), buffer, buffer_len, (TickType_t) timeout.count() / portTICK_PERIOD_MS);
  170. }
  171. #endif // CONFIG_SOC_I2C_SUPPORT_SLAVE
  172. I2CWrite::I2CWrite(const vector<uint8_t> &bytes, chrono::milliseconds driver_timeout)
  173. : I2CTransfer<void>(driver_timeout), bytes(bytes)
  174. {
  175. if (bytes.empty()) {
  176. throw I2CException(ESP_ERR_INVALID_ARG);
  177. }
  178. }
  179. void I2CWrite::queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr)
  180. {
  181. handle.start();
  182. handle.write_byte(i2c_addr.get_value() << 1 | I2C_MASTER_WRITE);
  183. handle.write(bytes);
  184. }
  185. void I2CWrite::process_result() { }
  186. I2CRead::I2CRead(size_t size, chrono::milliseconds driver_timeout)
  187. : I2CTransfer<vector<uint8_t> >(driver_timeout), bytes(size)
  188. {
  189. if (size == 0) {
  190. throw I2CException(ESP_ERR_INVALID_ARG);
  191. }
  192. }
  193. void I2CRead::queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr)
  194. {
  195. handle.start();
  196. handle.write_byte(i2c_addr.get_value() << 1 | I2C_MASTER_READ);
  197. handle.read(bytes);
  198. }
  199. vector<uint8_t> I2CRead::process_result()
  200. {
  201. return bytes;
  202. }
  203. I2CComposed::I2CComposed(chrono::milliseconds driver_timeout)
  204. : I2CTransfer<vector<vector<uint8_t> > >(driver_timeout), transfer_list() { }
  205. void I2CComposed::CompTransferNodeRead::queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr)
  206. {
  207. handle.write_byte(i2c_addr.get_value() << 1 | I2C_MASTER_READ);
  208. handle.read(bytes);
  209. }
  210. void I2CComposed::CompTransferNodeRead::process_result(std::vector<std::vector<uint8_t> > &read_results)
  211. {
  212. read_results.push_back(bytes);
  213. }
  214. void I2CComposed::CompTransferNodeWrite::queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr)
  215. {
  216. handle.write_byte(i2c_addr.get_value() << 1 | I2C_MASTER_WRITE);
  217. handle.write(bytes);
  218. }
  219. void I2CComposed::add_read(size_t size)
  220. {
  221. if (!size) {
  222. throw I2CException(ESP_ERR_INVALID_ARG);
  223. }
  224. transfer_list.push_back(make_shared<CompTransferNodeRead>(size));
  225. }
  226. void I2CComposed::add_write(std::vector<uint8_t> bytes)
  227. {
  228. if (bytes.empty()) {
  229. throw I2CException(ESP_ERR_INVALID_ARG);
  230. }
  231. transfer_list.push_back(make_shared<CompTransferNodeWrite>(bytes));
  232. }
  233. void I2CComposed::queue_cmd(I2CCommandLink &handle, I2CAddress i2c_addr)
  234. {
  235. for (auto it = transfer_list.begin(); it != transfer_list.end(); it++) {
  236. handle.start();
  237. (*it)->queue_cmd(handle, i2c_addr);
  238. }
  239. }
  240. std::vector<std::vector<uint8_t> > I2CComposed::process_result()
  241. {
  242. std::vector<std::vector<uint8_t> > results;
  243. for (auto it = transfer_list.begin(); it != transfer_list.end(); it++) {
  244. (*it)->process_result(results);
  245. }
  246. return results;
  247. }
  248. } // idf
  249. #endif // __cpp_exceptions