280 lines
7.7 KiB
C
Raw Normal View History

2022-01-18 00:58:54 +08:00
/*********************************************************************************************************************
* COPYRIGHT NOTICE
* Copyright (c) 2020,<EFBFBD><EFBFBD><EFBFBD>ɿƼ<EFBFBD>
* All rights reserved.
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>QQȺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱥ<EFBFBD><EFBFBD>824575535
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD>Ȩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿƼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD>;<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD>ӭ<EFBFBD><EFBFBD>λʹ<EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿƼ<EFBFBD><EFBFBD>İ<EFBFBD>Ȩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @file ģ<EFBFBD><EFBFBD>IIC
* @company <EFBFBD>ɶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿƼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>޹<EFBFBD>˾
* @author <EFBFBD><EFBFBD><EFBFBD>ɿƼ<EFBFBD>(QQ3184284598)
* @version <EFBFBD>doc<EFBFBD><EFBFBD>version<EFBFBD>ļ<EFBFBD> <EFBFBD>˵<EFBFBD><EFBFBD>
* @Software ADS v1.2.2
* @Target core TC264D
* @Taobao https://seekfree.taobao.com/
* @date 2020-3-23
* @note
<EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD><EFBFBD>
------------------------------------
SCL <EFBFBD>SEEKFREE_IIC<EFBFBD>ļ<EFBFBD><EFBFBD>ڵ<EFBFBD>SEEKFREE_SCL<EFBFBD><EFBFBD><EFBFBD>
SDA <EFBFBD>SEEKFREE_IIC<EFBFBD>ļ<EFBFBD><EFBFBD>ڵ<EFBFBD>SEEKFREE_SDA<EFBFBD><EFBFBD><EFBFBD>
------------------------------------
********************************************************************************************************************/
#include "zf_gpio.h"
#include "zf_stm_systick.h"
#include "SEEKFREE_IIC.h"
#define SDA gpio_get (SEEKFREE_SDA)
#define SDA0() gpio_set (SEEKFREE_SDA, 0) //IO<49><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>ƽ
#define SDA1() gpio_set (SEEKFREE_SDA, 1) //IO<49><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD>ƽ
#define SCL0() gpio_set (SEEKFREE_SCL, 0) //IO<49><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>ƽ
#define SCL1() gpio_set (SEEKFREE_SCL, 1) //IO<49><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD>ƽ
#define DIR_OUT() gpio_dir (SEEKFREE_SDA, GPO, PUSHPULL) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#define DIR_IN() gpio_dir (SEEKFREE_SDA, GPI, NO_PULL) //<2F><><EFBFBD><EFBFBD><EBB7BD>
//<2F>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD>ݶ<EFBFBD><DDB6><EFBFBD>
uint8 IIC_ad_main; //<2F><><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>ַ
uint8 IIC_ad_sub; //<2F><><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>ַ
uint8 *IIC_buf; //<2F><><EFBFBD><EFBFBD>|<7C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD><EFBFBD><EFBFBD>
uint8 IIC_num; //<2F><><EFBFBD><EFBFBD>|<7C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD><DDB8><EFBFBD>
#define ack 1 //<2F><>Ӧ<EFBFBD><D3A6>
#define no_ack 0 //<2F><>Ӧ<EFBFBD><D3A6>
uint16 simiic_delay_time = SIMIIC_DELAY_TIME; //ICM<43>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>Ϊ20
//-------------------------------------------------------------------------------------------------------------------
// @brief ģ<><C4A3>IIC<49><43>ʱ ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// @return void
// @since v1.0
// Sample usage: <09><><EFBFBD><EFBFBD>IICͨѶʧ<D1B6>ܿ<EFBFBD><DCBF>Գ<EFBFBD><D4B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>simiic_delay_time<6D><65>ֵ
//-------------------------------------------------------------------------------------------------------------------
void simiic_delay_set(uint16 time)
{
simiic_delay_time = time; //ICM<43>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>Ϊ100
}
//-------------------------------------------------------------------------------------------------------------------
// @brief ģ<><C4A3>IIC<49><43>ʱ
// @return void
// @since v1.0
// Sample usage: <09><><EFBFBD><EFBFBD>IICͨѶʧ<D1B6>ܿ<EFBFBD><DCBF>Գ<EFBFBD><D4B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>simiic_delay_time<6D><65>ֵ
//-------------------------------------------------------------------------------------------------------------------
void simiic_delay(void)
{
uint16 delay_time;
delay_time = simiic_delay_time;
while(delay_time--);
}
//<2F>ڲ<EFBFBD>ʹ<EFBFBD>ã<EFBFBD><C3A3>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void simiic_start(void)
{
SDA1();
SCL1();
simiic_delay();
SDA0();
simiic_delay();
SCL0();
}
//<2F>ڲ<EFBFBD>ʹ<EFBFBD>ã<EFBFBD><C3A3>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void simiic_stop(void)
{
SDA0();
SCL0();
simiic_delay();
SCL1();
simiic_delay();
SDA1();
simiic_delay();
}
//<2F><>Ӧ<EFBFBD><D3A6>(<28><><EFBFBD><EFBFBD>ack:SDA=0<><30>no_ack:SDA=0)
//<2F>ڲ<EFBFBD>ʹ<EFBFBD>ã<EFBFBD><C3A3>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void simiic_sendack(unsigned char ack_dat)
{
SCL0();
simiic_delay();
if(ack_dat) SDA0();
else SDA1();
SCL1();
simiic_delay();
SCL0();
simiic_delay();
}
static int sccb_waitack(void)
{
SCL0();
DIR_IN();
simiic_delay();
SCL1();
simiic_delay();
if(SDA) //Ӧ<><D3A6>Ϊ<EFBFBD>ߵ<EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><ECB3A3>ͨ<EFBFBD><CDA8>ʧ<EFBFBD><CAA7>
{
DIR_OUT();
SCL0();
return 0;
}
DIR_OUT();
SCL0();
simiic_delay();
return 1;
}
//<2F>ֽڷ<D6BD><DAB7>ͳ<EFBFBD><CDB3><EFBFBD>
//<2F><><EFBFBD><EFBFBD>c(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD>ǵ<EFBFBD>ַ)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD>Ӧ<EFBFBD><D3A6>
//<2F><><EFBFBD><EFBFBD><EFBFBD>Ǵ<EFBFBD>Ӧ<EFBFBD><D3A6>λ
//<2F>ڲ<EFBFBD>ʹ<EFBFBD>ã<EFBFBD><C3A3>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void send_ch(uint8 c)
{
uint8 i = 8;
while(i--)
{
if(c & 0x80) SDA1();//SDA <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
else SDA0();
c <<= 1;
simiic_delay();
SCL1(); //SCL <20><><EFBFBD>ߣ<EFBFBD><DFA3>ɼ<EFBFBD><C9BC>ź<EFBFBD>
simiic_delay();
SCL0(); //SCL ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
sccb_waitack();
}
//<2F>ֽڽ<D6BD><DABD>ճ<EFBFBD><D5B3><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3>˳<EFBFBD><CBB3><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>|<7C><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>|ʹ<><CAB9>
//<2F>ڲ<EFBFBD>ʹ<EFBFBD>ã<EFBFBD><C3A3>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
uint8 read_ch(uint8 ack_x)
{
uint8 i;
uint8 c;
c=0;
SCL0();
simiic_delay();
SDA1();
DIR_IN(); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>ʽ
for(i=0;i<8;i++)
{
simiic_delay();
SCL0(); //<2F><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ϊ<EFBFBD>ͣ<EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
simiic_delay();
SCL1(); //<2F><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ϊ<EFBFBD>ߣ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
simiic_delay();
c<<=1;
if(SDA)
{
c+=1; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD>ݴ<EFBFBD>c
}
}
DIR_OUT();
SCL0();
simiic_delay();
simiic_sendack(ack_x);
return c;
}
//-------------------------------------------------------------------------------------------------------------------
// @brief ģ<><C4A3>IICд<43><D0B4><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// @param dev_add <09><EFBFBD><E8B1B8>ַ(<28><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>ַ)
// @param reg <09>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ
// @param dat д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// @return void
// @since v1.0
// Sample usage:
//-------------------------------------------------------------------------------------------------------------------
void simiic_write_reg(uint8 dev_add, uint8 reg, uint8 dat)
{
simiic_start();
send_ch( (dev_add<<1) | 0x00); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>дλ
send_ch( reg ); //<2F><><EFBFBD>ʹӻ<CDB4><D3BB>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ
send_ch( dat ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫд<D2AA><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
simiic_stop();
}
//-------------------------------------------------------------------------------------------------------------------
// @brief ģ<><C4A3>IIC<49><43><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
// @param dev_add <09><EFBFBD><E8B1B8>ַ(<28><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>ַ)
// @param reg <09>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ
// @param type ѡ<><D1A1>ͨ<EFBFBD>ŷ<EFBFBD>ʽ<EFBFBD><CABD>IIC <20><><EFBFBD><EFBFBD> SCCB
// @return uint8 <09><><EFBFBD>ؼĴ<D8BC><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// @since v1.0
// Sample usage:
//-------------------------------------------------------------------------------------------------------------------
uint8 simiic_read_reg(uint8 dev_add, uint8 reg, IIC_type type)
{
uint8 dat;
simiic_start();
send_ch( (dev_add<<1) | 0x00); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>дλ
send_ch( reg ); //<2F><><EFBFBD>ʹӻ<CDB4><D3BB>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ
if(type == SCCB)simiic_stop();
simiic_start();
send_ch( (dev_add<<1) | 0x01); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>Ӷ<EFBFBD>λ
dat = read_ch(no_ack); //<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
simiic_stop();
return dat;
}
//-------------------------------------------------------------------------------------------------------------------
// @brief ģ<><C4A3>IIC<49><43>ȡ<EFBFBD><C8A1><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
// @param dev_add <09><EFBFBD><E8B1B8>ַ(<28><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>ַ)
// @param reg <09>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ
// @param dat_add <09><><EFBFBD>ݱ<EFBFBD><DDB1><EFBFBD><EFBFBD>ĵ<EFBFBD>ַָ<D6B7><D6B8>
// @param num <09><>ȡ<EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
// @param type ѡ<><D1A1>ͨ<EFBFBD>ŷ<EFBFBD>ʽ<EFBFBD><CABD>IIC <20><><EFBFBD><EFBFBD> SCCB
// @return uint8 <09><><EFBFBD>ؼĴ<D8BC><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// @since v1.0
// Sample usage:
//-------------------------------------------------------------------------------------------------------------------
void simiic_read_regs(uint8 dev_add, uint8 reg, uint8 *dat_add, uint8 num, IIC_type type)
{
simiic_start();
send_ch( (dev_add<<1) | 0x00); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>дλ
send_ch( reg ); //<2F><><EFBFBD>ʹӻ<CDB4><D3BB>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ
if(type == SCCB)simiic_stop();
simiic_start();
send_ch( (dev_add<<1) | 0x01); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>Ӷ<EFBFBD>λ
while(--num)
{
*dat_add = read_ch(ack); //<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
dat_add++;
}
*dat_add = read_ch(no_ack); //<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
simiic_stop();
}
//-------------------------------------------------------------------------------------------------------------------
// @brief ģ<><C4A3>IIC<49>˿ڳ<CBBF>ʼ<EFBFBD><CABC>
// @param NULL
// @return void
// @since v1.0
// Sample usage:
//-------------------------------------------------------------------------------------------------------------------
void simiic_init(void)
{
gpio_init (SEEKFREE_SCL, GPO, 1, PUSHPULL);
gpio_init (SEEKFREE_SDA, GPO, 1, PUSHPULL);
}