@@ -1781,29 +1781,99 @@ uint32_t i2cGetStatus(i2c_t * i2c){
1781
1781
else return 0 ;
1782
1782
}
1783
1783
#else
1784
+ #include "driver/i2c.h"
1785
+
1786
+ #define ACK_CHECK_EN 1 /*!< I2C master will check ack from slave*/
1787
+ #define ACK_CHECK_DIS 0 /*!< I2C master will not check ack from slave */
1788
+ #define ACK_VAL 0x0 /*!< I2C ack value */
1789
+ #define NACK_VAL 0x1 /*!< I2C nack value */
1790
+
1791
+ struct i2c_struct_t {
1792
+ i2c_port_t num ;
1793
+ };
1784
1794
1785
1795
i2c_t * i2cInit (uint8_t i2c_num , int8_t sda , int8_t scl , uint32_t clk_speed ){
1786
- return NULL ;
1787
- }
1788
- void i2cRelease (i2c_t * i2c ){
1789
- return ;
1796
+ i2c_t * out = (i2c_t * )malloc (sizeof (i2c_t ));
1797
+ if (out == NULL ){
1798
+ log_e ("malloc failed" );
1799
+ return NULL ;
1800
+ }
1801
+ out -> num = (i2c_port_t )i2c_num ;
1802
+
1803
+ i2c_config_t conf ;
1804
+ conf .mode = I2C_MODE_MASTER ;
1805
+ conf .scl_io_num = (gpio_num_t )scl ;
1806
+ conf .sda_io_num = (gpio_num_t )sda ;
1807
+ conf .scl_pullup_en = GPIO_PULLUP_ENABLE ;
1808
+ conf .sda_pullup_en = GPIO_PULLUP_ENABLE ;
1809
+ conf .master .clk_speed = clk_speed ;
1810
+ esp_err_t ret = i2c_param_config (out -> num , & conf );
1811
+ if (ret != ESP_OK ) {
1812
+ log_e ("i2c_param_config failed" );
1813
+ free (out );
1814
+ return NULL ;
1815
+ }
1816
+ ret = i2c_driver_install (out -> num , conf .mode , 0 , 0 , 0 );
1817
+ if (ret != ESP_OK ) {
1818
+ log_e ("i2c_driver_install failed" );
1819
+ free (out );
1820
+ return NULL ;
1821
+ }
1822
+ return out ;
1790
1823
}
1824
+
1791
1825
i2c_err_t i2cWrite (i2c_t * i2c , uint16_t address , uint8_t * buff , uint16_t size , bool sendStop , uint16_t timeOutMillis ){
1792
- return ESP_FAIL ;
1826
+ esp_err_t ret = ESP_OK ;
1827
+ i2c_cmd_handle_t cmd = i2c_cmd_link_create ();
1828
+ i2c_master_start (cmd );
1829
+ i2c_master_write_byte (cmd , (address << 1 ) | I2C_MASTER_WRITE , ACK_CHECK_EN );
1830
+ i2c_master_write (cmd , buff , size , ACK_CHECK_EN );
1831
+ //if send stop?
1832
+ i2c_master_stop (cmd );
1833
+ ret = i2c_master_cmd_begin (i2c -> num , cmd , timeOutMillis / portTICK_RATE_MS );
1834
+ i2c_cmd_link_delete (cmd );
1835
+ return ret ;
1793
1836
}
1837
+
1794
1838
i2c_err_t i2cRead (i2c_t * i2c , uint16_t address , uint8_t * buff , uint16_t size , bool sendStop , uint16_t timeOutMillis , uint32_t * readCount ){
1795
- return ESP_FAIL ;
1839
+ esp_err_t ret = ESP_OK ;
1840
+ i2c_cmd_handle_t cmd = i2c_cmd_link_create ();
1841
+ i2c_master_start (cmd );
1842
+ i2c_master_write_byte (cmd , (address << 1 ) | I2C_MASTER_READ , ACK_CHECK_EN );
1843
+ if (size > 1 ) {
1844
+ i2c_master_read (cmd , buff , size - 1 , ACK_VAL );
1845
+ }
1846
+ i2c_master_read_byte (cmd , buff + size - 1 , NACK_VAL );
1847
+ i2c_master_stop (cmd );
1848
+ ret = i2c_master_cmd_begin (i2c -> num , cmd , timeOutMillis / portTICK_RATE_MS );
1849
+ i2c_cmd_link_delete (cmd );
1850
+ if (ret == ESP_OK ){
1851
+ * readCount = size ;
1852
+ }
1853
+ return ret ;
1854
+ }
1855
+
1856
+ void i2cRelease (i2c_t * i2c ){
1857
+ log_w ("" );
1858
+ return ;
1796
1859
}
1797
1860
i2c_err_t i2cFlush (i2c_t * i2c ){
1798
- return ESP_FAIL ;
1861
+ esp_err_t ret = i2c_reset_tx_fifo (i2c -> num );
1862
+ if (ret != ESP_OK ){
1863
+ return ret ;
1864
+ }
1865
+ return i2c_reset_rx_fifo (i2c -> num );
1799
1866
}
1800
1867
i2c_err_t i2cSetFrequency (i2c_t * i2c , uint32_t clk_speed ){
1801
- return ESP_FAIL ;
1868
+ log_w ("" );
1869
+ return ESP_OK ;
1802
1870
}
1803
1871
uint32_t i2cGetFrequency (i2c_t * i2c ){
1872
+ log_w ("" );
1804
1873
return 0 ;
1805
1874
}
1806
1875
uint32_t i2cGetStatus (i2c_t * i2c ){
1876
+ log_w ("" );
1807
1877
return 0 ;
1808
1878
}
1809
1879
0 commit comments