123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- #include "ets_sys.h"
- #include "osapi.h"
- #include "gpio.h"
- #include "driver/i2c_master.h"
- LOCAL uint8 m_nLastSDA;
- LOCAL uint8 m_nLastSCL;
- LOCAL void ICACHE_FLASH_ATTR
- i2c_master_setDC(uint8 SDA, uint8 SCL)
- {
- SDA &= 0x01;
- SCL &= 0x01;
- m_nLastSDA = SDA;
- m_nLastSCL = SCL;
- if ((0 == SDA) && (0 == SCL)) {
- I2C_MASTER_SDA_LOW_SCL_LOW();
- } else if ((0 == SDA) && (1 == SCL)) {
- I2C_MASTER_SDA_LOW_SCL_HIGH();
- } else if ((1 == SDA) && (0 == SCL)) {
- I2C_MASTER_SDA_HIGH_SCL_LOW();
- } else {
- I2C_MASTER_SDA_HIGH_SCL_HIGH();
- }
- }
- LOCAL uint8 ICACHE_FLASH_ATTR
- i2c_master_getDC(void)
- {
- uint8 sda_out;
- sda_out = GPIO_INPUT_GET(GPIO_ID_PIN(I2C_MASTER_SDA_GPIO));
- return sda_out;
- }
- void ICACHE_FLASH_ATTR
- i2c_master_init(void)
- {
- uint8 i;
- i2c_master_setDC(1, 0);
- i2c_master_wait(5);
-
- i2c_master_setDC(0, 0) ;
- i2c_master_wait(5);
- i2c_master_setDC(1, 0) ;
- i2c_master_wait(5);
-
- for (i = 0; i < 28; i++) {
- i2c_master_setDC(1, 0);
- i2c_master_wait(5);
- i2c_master_setDC(1, 1);
- i2c_master_wait(5);
- }
-
- i2c_master_stop();
- return;
- }
- void ICACHE_FLASH_ATTR
- i2c_master_gpio_init(void)
- {
- ETS_GPIO_INTR_DISABLE() ;
- PIN_FUNC_SELECT(I2C_MASTER_SDA_MUX, I2C_MASTER_SDA_FUNC);
- PIN_FUNC_SELECT(I2C_MASTER_SCL_MUX, I2C_MASTER_SCL_FUNC);
- GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(I2C_MASTER_SDA_GPIO)), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(I2C_MASTER_SDA_GPIO))) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE));
- GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << I2C_MASTER_SDA_GPIO));
- GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(I2C_MASTER_SCL_GPIO)), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(I2C_MASTER_SCL_GPIO))) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE));
- GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << I2C_MASTER_SCL_GPIO));
- I2C_MASTER_SDA_HIGH_SCL_HIGH();
- ETS_GPIO_INTR_ENABLE() ;
- i2c_master_init();
- }
- void ICACHE_FLASH_ATTR
- i2c_master_start(void)
- {
- i2c_master_setDC(1, m_nLastSCL);
- i2c_master_wait(5);
- i2c_master_setDC(1, 1);
- i2c_master_wait(5);
- i2c_master_setDC(0, 1);
- i2c_master_wait(5);
- }
- void ICACHE_FLASH_ATTR
- i2c_master_stop(void)
- {
- i2c_master_wait(5);
- i2c_master_setDC(0, m_nLastSCL);
- i2c_master_wait(5);
- i2c_master_setDC(0, 1);
- i2c_master_wait(5);
- i2c_master_setDC(1, 1);
- i2c_master_wait(5);
- }
- void ICACHE_FLASH_ATTR
- i2c_master_setAck(uint8 level)
- {
- i2c_master_setDC(m_nLastSDA, 0);
- i2c_master_wait(5);
- i2c_master_setDC(level, 0);
- i2c_master_wait(5);
- i2c_master_setDC(level, 1);
- i2c_master_wait(8);
- i2c_master_setDC(level, 0);
- i2c_master_wait(5);
- i2c_master_setDC(1, 0);
- i2c_master_wait(5);
- }
- uint8 ICACHE_FLASH_ATTR
- i2c_master_getAck(void)
- {
- uint8 retVal;
- i2c_master_setDC(m_nLastSDA, 0);
- i2c_master_wait(5);
- i2c_master_setDC(1, 0);
- i2c_master_wait(5);
- i2c_master_setDC(1, 1);
- i2c_master_wait(5);
- retVal = i2c_master_getDC();
- i2c_master_wait(5);
- i2c_master_setDC(1, 0);
- i2c_master_wait(5);
- return retVal;
- }
- bool ICACHE_FLASH_ATTR
- i2c_master_checkAck(void)
- {
- if(i2c_master_getAck()){
- return FALSE;
- }else{
- return TRUE;
- }
- }
- void ICACHE_FLASH_ATTR
- i2c_master_send_ack(void)
- {
- i2c_master_setAck(0x0);
- }
- void ICACHE_FLASH_ATTR
- i2c_master_send_nack(void)
- {
- i2c_master_setAck(0x1);
- }
- uint8 ICACHE_FLASH_ATTR
- i2c_master_readByte(void)
- {
- uint8 retVal = 0;
- uint8 k, i;
- i2c_master_wait(5);
- i2c_master_setDC(m_nLastSDA, 0);
- i2c_master_wait(5);
- for (i = 0; i < 8; i++) {
- i2c_master_wait(5);
- i2c_master_setDC(1, 0);
- i2c_master_wait(5);
- i2c_master_setDC(1, 1);
- i2c_master_wait(5);
- k = i2c_master_getDC();
- i2c_master_wait(5);
- if (i == 7) {
- i2c_master_wait(3);
- }
- k <<= (7 - i);
- retVal |= k;
- }
- i2c_master_setDC(1, 0);
- i2c_master_wait(5);
- return retVal;
- }
- void ICACHE_FLASH_ATTR
- i2c_master_writeByte(uint8 wrdata)
- {
- uint8 dat;
- sint8 i;
- i2c_master_wait(5);
- i2c_master_setDC(m_nLastSDA, 0);
- i2c_master_wait(5);
- for (i = 7; i >= 0; i--) {
- dat = wrdata >> i;
- i2c_master_setDC(dat, 0);
- i2c_master_wait(5);
- i2c_master_setDC(dat, 1);
- i2c_master_wait(5);
- if (i == 0) {
- i2c_master_wait(3);
- }
- i2c_master_setDC(dat, 0);
- i2c_master_wait(5);
- }
- }
|