i2c_cxx.cpp 7.4 KB

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