esp8266_i2c_BH1750_基於安信科編譯器

i2c_master.h

/*
 * ESPRESSIF MIT License
 *
 * Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
 *
 * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
 * it is free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
 * to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#ifndef __I2C_MASTER_H__
#define __I2C_MASTER_H__

#define I2C_MASTER_SDA_MUX PERIPHS_IO_MUX_MTCK_U
#define I2C_MASTER_SCL_MUX PERIPHS_IO_MUX_MTDI_U
#define I2C_MASTER_SDA_GPIO 13
#define I2C_MASTER_SCL_GPIO 12
#define I2C_MASTER_SDA_FUNC FUNC_GPIO13
#define I2C_MASTER_SCL_FUNC FUNC_GPIO12

//#define I2C_MASTER_SDA_MUX PERIPHS_IO_MUX_GPIO2_U
//#define I2C_MASTER_SCL_MUX PERIPHS_IO_MUX_GPIO0_U
//#define I2C_MASTER_SDA_GPIO 2
//#define I2C_MASTER_SCL_GPIO 0
//#define I2C_MASTER_SDA_FUNC FUNC_GPIO2
//#define I2C_MASTER_SCL_FUNC FUNC_GPIO0

#if 0
#define I2C_MASTER_GPIO_SET(pin)  \
    gpio_output_set(1<<pin,0,1<<pin,0)

#define I2C_MASTER_GPIO_CLR(pin) \
    gpio_output_set(0,1<<pin,1<<pin,0)

#define I2C_MASTER_GPIO_OUT(pin,val) \
    if(val) I2C_MASTER_GPIO_SET(pin);\
    else I2C_MASTER_GPIO_CLR(pin)
#endif

i2c_master.c

/*
 * ESPRESSIF MIT License
 *
 * Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
 *
 * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
 * it is free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
 * to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include "ets_sys.h"
#include "osapi.h"
#include "gpio.h"

#include "driver/i2c_master.h"


/******************************************************************************
 * FunctionName : i2c_master_setDC
 * Description  : Internal used function -
 *                    set i2c SDA and SCL bit value for half clk cycle
 * Parameters   : uint8 SDA
 *                uint8 SCL
 * Returns      : NONE
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR
i2c_master_setDC(uint8 SDA, uint8 SCL)
{
    SDA	&= 0x01;
    SCL	&= 0x01;

    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();
    }
}

/******************************************************************************
 * FunctionName : i2c_master_getDC
 * Description  : Internal used function -
 *                    get i2c SDA bit value
 * Parameters   : NONE
 * Returns      : uint8 - SDA bit value
*******************************************************************************/
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;
}

/******************************************************************************
 * FunctionName : i2c_master_init
 * Description  : initilize I2C bus to enable i2c operations
 * Parameters   : NONE
 * Returns      : NONE
*******************************************************************************/
void ICACHE_FLASH_ATTR
i2c_master_init(void) //復位i2c從機狀態
{

    while(!i2c_master_getDC()){//檢測i2c從機是否掛死,fSCL約等於333.3kHz
    	system_soft_wdt_feed();//喂軟件看門狗
        i2c_master_setDC(1, 0);
        i2c_master_wait(2);	// sda 1, scl 0
        i2c_master_setDC(1, 1);
        i2c_master_wait(1);	// sda 1, scl 1
    }

    // reset all
    i2c_master_stop();
    return;
}

/******************************************************************************
 * FunctionName : i2c_master_gpio_init
 * Description  : config SDA and SCL gpio to open-drain output mode,
 *                mux and gpio num defined in i2c_master.h
 * Parameters   : NONE
 * Returns      : NONE
*******************************************************************************/
void ICACHE_FLASH_ATTR
i2c_master_gpio_init(void)
{
    ETS_GPIO_INTR_DISABLE() ;
//    ETS_INTR_LOCK();

    PIN_FUNC_SELECT(I2C_MASTER_SDA_MUX, I2C_MASTER_SDA_FUNC);
    PIN_FUNC_SELECT(I2C_MASTER_SCL_MUX, I2C_MASTER_SCL_FUNC);
 /*設置SDA_GPIO口爲輸入模式
    gpio_output_set(0, 0, 0, GPIO_ID_PIN(I2C_MASTER_SDA_GPIO));*/

    //設置SDA_GPIO口爲開漏模式
   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)); //open drain;
    GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << I2C_MASTER_SDA_GPIO));
    //設置CLK_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)); //open drain;
    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() ;
//    ETS_INTR_UNLOCK();

    i2c_master_init();
}

/******************************************************************************
 * FunctionName : i2c_master_start
 * Description  : set i2c to send state
 * Parameters   : NONE
 * Returns      : NONE
*******************************************************************************/
void ICACHE_FLASH_ATTR
i2c_master_start(void)
{
    i2c_master_setDC(1, 0);
    i2c_master_wait(1);
	i2c_master_setDC(1, 1);
    i2c_master_wait(1);	// 一個重複起始信號的建立時間0.6us
    i2c_master_setDC(0, 1);
    i2c_master_wait(1);	// 起始信號的保持時間0.6us,在這段時間過後可產生第一個時鐘脈衝
    i2c_master_setDC(0, 0);
    i2c_master_wait(1);	// 起始信號的保持時間0.6us,在這段時間過後可產生第一個時鐘脈衝
}
/******************************************************************************
 * FunctionName : i2c_master_writeByte
 * Description  : write wrdata value(one byte) into i2c
 * Parameters   : uint8 wrdata - write value
 * Returns      : NONE
*******************************************************************************/
void ICACHE_FLASH_ATTR
i2c_master_writeByte(uint8 wrdata)//向i2c寫數據
{
    uint8 dat;
    sint8 i;

    for (i = 7; i >= 0; i--) {
        dat = wrdata >> i;
        i2c_master_setDC(dat, 0);
        i2c_master_wait(1);//數據建立時間100ns
        i2c_master_setDC(dat, 1);
        i2c_master_wait(1);//SCL時鐘高電平週期0.6us
        i2c_master_setDC(dat, 0);
        i2c_master_wait(1);//SCL時鐘低電平週期1.3us+數據保持時間max0.9us
    }
}
/******************************************************************************
* FunctionName : i2c_master_checkAck
* Description  : get dev response
* Parameters   : NONE
* Returns      : true : get ack ; false : get nack
*******************************************************************************/
bool ICACHE_FLASH_ATTR
i2c_master_checkAck(void)//檢查從機應答狀態
{
    if(i2c_master_getAck()){
        return FALSE;
    }else{
        return TRUE;
    }
}
/******************************************************************************
 * FunctionName : i2c_master_getAck
 * Description  : confirm if peer send ack
 * Parameters   : NONE
 * Returns      : uint8 - ack value, 0 or 1
*******************************************************************************/
uint8 ICACHE_FLASH_ATTR
i2c_master_getAck(void)//第九個時鐘獲取應答狀態
{
    uint8 retVal;
    i2c_master_setDC(0, 0);
    i2c_master_wait(1);
    i2c_master_setDC(1, 1);
    i2c_master_wait(1);
    retVal = i2c_master_getDC();
    i2c_master_setDC(0, 0);
    i2c_master_wait(1);
    return retVal;
}
/******************************************************************************
 * FunctionName : i2c_master_stop
 * Description  : set i2c to stop sending state
 * Parameters   : NONE
 * Returns      : NONE
*******************************************************************************/
void ICACHE_FLASH_ATTR
i2c_master_stop(void)
{
    i2c_master_setDC(0, 0);
    i2c_master_wait(1);
    i2c_master_setDC(0, 1);
    i2c_master_wait(1);	// 終止信號的建立時間0.6us
    i2c_master_setDC(1, 1);
    i2c_master_wait(1);	// 在一個終止信號和起始信號之間總線必須空閒的時間1.6us

}

/******************************************************************************
 * FunctionName : i2c_master_setAck
 * Description  : set ack to i2c bus as level value
 * Parameters   : uint8 level - 0 or 1
 * Returns      : NONE
*******************************************************************************/
void ICACHE_FLASH_ATTR
i2c_master_setAck(uint8 level)//設置主機應答方式
{
	i2c_master_setDC(level, 0);
	i2c_master_wait(1);	// 數據建立時間100ns
	i2c_master_setDC(level, 1);
    i2c_master_wait(1);	//SCL時鐘信號高電平週期
    i2c_master_setDC(level, 0);
    i2c_master_wait(1);	//數據保持時間max0.9us
    i2c_master_setDC(1, 0);
}


/******************************************************************************
* FunctionName : i2c_master_send_ack
* Description  : response ack
* Parameters   : NONE
* Returns      : NONE
*******************************************************************************/
void ICACHE_FLASH_ATTR
i2c_master_send_ack(void)//i2c主機應答
{
    i2c_master_setAck(0x0);
}
/******************************************************************************
* FunctionName : i2c_master_send_nack
* Description  : response nack
* Parameters   : NONE
* Returns      : NONE
*******************************************************************************/
void ICACHE_FLASH_ATTR
i2c_master_send_nack(void)//i2c主機非應答
{
    i2c_master_setAck(0x1);
}

/******************************************************************************
 * FunctionName : i2c_master_readByte
 * Description  : read Byte from i2c bus
 * Parameters   : NONE
 * Returns      : uint8 - readed value
*******************************************************************************/
uint8 ICACHE_FLASH_ATTR
i2c_master_readByte(void)//向i2c總線讀數據
{
    uint8 retVal = 0;
    uint8 k, i;
    for (i = 0; i < 8; i++) {
        i2c_master_setDC(1, 1);
        i2c_master_wait(1);	//SCL時鐘信號高電平週期
        k = i2c_master_getDC();
        i2c_master_setDC(1, 0);
        i2c_master_wait(1);	//SCL時鐘信號低電平週期
        k <<= (7 - i);
        retVal |= k;
    }
    return retVal;
}

i2c_BH1750.h

/*
 * i2c_BH1750.h
 *
 *  Created on: 2018年12月29日
 *      Author: 王傑龍
 */

#ifndef APP_INCLUDE_USER_DRIVER_I2C_BH1750_H_
#define APP_INCLUDE_USER_DRIVER_I2C_BH1750_H_

#define BHT1750_address 0x46

void BH1750_write(unsigned char Command);
unsigned int BH1750_read(void);

#endif /* APP_INCLUDE_USER_DRIVER_I2C_BH1750_H_ */

i2c_BH1750.c

#include "driver/i2c_master.h"
#include "user_driver/i2c_BH1750.h"
#include "osapi.h"
void ICACHE_FLASH_ATTR
BH1750_write(unsigned char Command){//寫命令
	i2c_master_start();
	i2c_master_writeByte(BHT1750_address&0xfe);//寫寄存器數據
		if(!i2c_master_checkAck()) {//檢測應答
			i2c_master_stop();
			return;
		}
		i2c_master_writeByte(Command);//寫命令
		if(!i2c_master_checkAck()) {//檢測應答
			i2c_master_stop();
			return;
		}
		i2c_master_stop();
}
unsigned int ICACHE_FLASH_ATTR
BH1750_read(void){//寫命令
	unsigned int temp;
	i2c_master_start();
	i2c_master_writeByte(BHT1750_address|0x01);//讀寄存器數據
		if(!i2c_master_checkAck()) {//檢測應答
			i2c_master_stop();
		}
		temp=i2c_master_readByte();
		temp<<=8;
		i2c_master_send_ack();
		temp|=i2c_master_readByte();
		i2c_master_send_nack();
		i2c_master_stop();
		return temp;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章