mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
280 lines
7.7 KiB
C
280 lines
7.7 KiB
C
/*********************************************************************************************************************
|
||
* COPYRIGHT NOTICE
|
||
* Copyright (c) 2020,<2C><><EFBFBD>ɿƼ<C9BF>
|
||
* All rights reserved.
|
||
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>QQȺ<51><C8BA><EFBFBD><EFBFBD>Ⱥ<EFBFBD><C8BA>824575535
|
||
*
|
||
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD>Ȩ<EFBFBD><C8A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿƼ<C9BF><C6BC><EFBFBD><EFBFBD>У<EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5>;<EFBFBD><CDBE>
|
||
* <20><>ӭ<EFBFBD><D3AD>λʹ<CEBB>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DEB8><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>뱣<EFBFBD><EBB1A3><EFBFBD><EFBFBD><EFBFBD>ɿƼ<C9BF><C6BC>İ<EFBFBD>Ȩ<EFBFBD><C8A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*
|
||
* @file ģ<><C4A3>IIC
|
||
* @company <09>ɶ<EFBFBD><C9B6><EFBFBD><EFBFBD>ɿƼ<C9BF><C6BC><EFBFBD><EFBFBD><EFBFBD>˾
|
||
* @author <09><><EFBFBD>ɿƼ<C9BF>(QQ3184284598)
|
||
* @version <09>鿴doc<6F><63>version<6F>ļ<EFBFBD> <20>汾˵<E6B1BE><CBB5>
|
||
* @Software ADS v1.2.2
|
||
* @Target core TC264D
|
||
* @Taobao https://seekfree.taobao.com/
|
||
* @date 2020-3-23
|
||
* @note
|
||
<09><><EFBFBD>߶<EFBFBD><DFB6>壺
|
||
------------------------------------
|
||
SCL <20>鿴SEEKFREE_IIC<49>ļ<EFBFBD><C4BC>ڵ<EFBFBD>SEEKFREE_SCL<43>궨<EFBFBD><EAB6A8>
|
||
SDA <20>鿴SEEKFREE_IIC<49>ļ<EFBFBD><C4BC>ڵ<EFBFBD>SEEKFREE_SDA<44>궨<EFBFBD><EAB6A8>
|
||
------------------------------------
|
||
********************************************************************************************************************/
|
||
|
||
|
||
|
||
#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);
|
||
}
|
||
|