Commit 7a571ddf authored by 时昊's avatar 时昊

🎉 init:增加光感功能,增加模拟iic

parent 46899d54
......@@ -51,6 +51,7 @@ void Power_KL30_Init ( void )
BU98R10_Init();
Data_User_Mileage_KL30Init();
BU2703_Senser_Init();
}
......@@ -200,10 +201,11 @@ void Power_Wakeup_Init ( void )
Common_DataInit();
BU98R10_Init();
APP_CAN_Init();
Data_User_Mileage_WakeupInit();
BU2703_Senser_Init();
}
......@@ -238,7 +240,7 @@ void Power_IG_ON_Init(void)
Common_Set_IG_Sts(COMMON_POWER_ON);
// UART2_Init(115200);
BU2703_Senser_Init();
......
#ifndef EMULATED_EEPROM_H__
#define EMULATED_EEPROM_H__
typedef enum
{
EEPROM_STAT_BLANK = 0U,
EEPROM_STAT_ACTIVE,
EEPROM_STAT_ERROR,
}EEPROM_Block_Status_en_t;
typedef enum
{
EEPROM_RW_PASS = 0,
EEPROM_RW_FAIL,
}EEPROM_RW_Result_en_t;
typedef enum
{
EEPROM_STAT_IDLE = 0,
EEPROM_STAT_BUSY,
}EEPROM_Status_en_t;
/****************************************************************************
* !!! The following code should be generated by configuration tool !!! *
****************************************************************************/
enum g_enEEPROMBlockName
{
EEPROM_BLOCK_UDS_FLAG = 0U,
EEPROM_BLOCK_UDS_S27 = 1U,
EEPROM_BLOCK_UDS_S2E0 = 2U,
EEPROM_BLOCK_UDS_S2E = 3U,
EEPROM_BLOCK_UDS_CONFIG = 4U,
EEPROM_BLOCK_UDS_DTC = 5U,
EEPROM_BLOCK_ODO = 6U,
EEPROM_BLOCK_ODO_STAMP = 7U,
EEPROM_BLOCK_TRIP_STAMP = 8U,
EEPROM_BLOCK_SERVICE_INFO = 9U,
EEPROM_BLOCK_UE_INFO = 10U,
};
#define EEPROM_BLOCK_UDS_FLAG_SIZE (16U)/*Life:20.0W*/
#define EEPROM_BLOCK_UDS_S27_SIZE (16U)/*Life:20.0W*/
#define EEPROM_BLOCK_UDS_S2E0_SIZE (216U)/*Life:20.0W*/
#define EEPROM_BLOCK_UDS_S2E_SIZE (216U)/*Life:20.0W*/
#define EEPROM_BLOCK_UDS_CONFIG_SIZE (40U)/*Life:20.0W*/
#define EEPROM_BLOCK_UDS_DTC_SIZE (76U)/*Life:20.0W*/
#define EEPROM_BLOCK_ODO_SIZE (4U)/*Life:1000.0W*/
#define EEPROM_BLOCK_ODO_STAMP_SIZE (8U)/*Life:1000.0W*/
#define EEPROM_BLOCK_TRIP_STAMP_SIZE (16U)/*Life:1000.0W*/
#define EEPROM_BLOCK_SERVICE_INFO_SIZE (56U)/*Life:20.0W*/
#define EEPROM_BLOCK_UE_INFO_SIZE (124U)/*Life:100.0W*/
#define EEPROM_TOTAL_BLOCK_NUM (11U)
/****************************************************************************
* !!! End of code generation !!! *
****************************************************************************/
extern void EEPROM_Init(void);
extern EEPROM_Status_en_t EEPROM_Get_Status(void);
extern EEPROM_Block_Status_en_t EEPROM_Get_Block_Status(uint16_t u16BlockID);
extern EEPROM_RW_Result_en_t EEPROM_Read_Data(uint16_t u16BlockID, uint32_t u32Data[], uint16_t u16Len);
extern EEPROM_RW_Result_en_t EEPROM_Write_Data(uint16_t u16BlockID, uint32_t u32Data[], uint16_t u16Len);
#endif
#ifndef EMULATED_EEPROM_ACCESS_H__
#define EMULATED_EEPROM_ACCESS_H__
typedef struct
{
uint32_t u32StartAddr;
uint32_t u32EndAddr;
uint32_t u32DataSize;
uint32_t* pu32DataBuffer;
}EEPROM_Block_st_t;
typedef struct
{
uint8_t u8Status;
uint8_t u8SectorOffset;
uint16_t u16SectorSize;
uint32_t u32BaseAddr;
uint16_t u16Index;
uint16_t u16RecordNum;
uint16_t u16MaxRecordNum;
uint16_t u16BlockSize;
uint32_t* pu32Data;
}EEPROM_Block_Access_st_t;
typedef void (*EEPROM_Mem_Erase_Func_ptr_t) (uint32_t, uint32_t);
typedef uint32_t (*EEPROM_Mem_Blank_Chk_Func_ptr_t) (uint32_t, uint32_t);
typedef void (*EEPROM_Mem_Read_Func_ptr_t) (uint32_t, uint32_t*, uint32_t);
typedef void (*EEPROM_Mem_Write_Func_ptr_t) (uint32_t, uint32_t*, uint32_t);
typedef struct
{
EEPROM_Mem_Erase_Func_ptr_t pfnMemErase;
EEPROM_Mem_Blank_Chk_Func_ptr_t pfnMemBlankChk;
EEPROM_Mem_Read_Func_ptr_t pfnMemRead;
EEPROM_Mem_Write_Func_ptr_t pfnMemWrite;
}EEPROM_Media_Access_st_t;
#define EEPROM_BLOCK_BLANK (0U)
#define EEPROM_BLOCK_ACTIVE (1U)
#define EEPROM_BLOCK_ERROR (2U)
#define EEPROM_BLOCK_CTRL_BYTE_SIZE (8U)
extern void EEPROM_Access_Init(const EEPROM_Block_st_t * pstBlockTable,
EEPROM_Block_Access_st_t * pstBlockAccess,
EEPROM_Media_Access_st_t * pstMediaAccess,
uint16_t u16BlockNum);
extern uint8_t EEPROM_Access_Busy(void);
extern uint8_t EEPROM_Block_Access_Status(uint16_t u16BlockID);
extern uint8_t EEPROM_Read_Block_Data(uint16_t u16BlockID, uint32_t u32Data[], uint16_t u16Len);
extern uint8_t EEPROM_Write_Block_Data(uint16_t u16BlockID, uint32_t u32Data[], uint16_t u16Len);
extern void EEPROM_Mem_Access_Complete_Callback(void);
#endif
#include "common.h"
#define IIC_WaitOverTime 50
/******************************************************************************
引脚定义
******************************************************************************/
#define SCL_LED_DRIVER_PORT PORT6
#define SCL_LED_DRIVER_PIN PIN2
#define SDA_LED_DRIVER_PORT PORT6
#define SDA_LED_DRIVER_PIN PIN3
/******************************************************************************
函数声明
******************************************************************************/
extern void Simulated_IIC_2_Init(void);
extern void bsp_IIC_delay(uint32_t time);
extern void bsp_IIC_Start(void);
extern void bsp_IIC_Stop(void);
extern void bsp_IIC_NACK(void);
extern void bsp_IIC_ACK(void);
extern void bsp_IIC_SendByte(uint8_t Data);
extern uint8_t bsp_IIC_ReadByte(void);
extern uint8_t bsp_IIC_WaitAck(void);
extern void Simulated_Release_SCL(void);
extern void bsp_IIC_nop( void );
/******************************************************************************
文 件 名:Simulated_IIC_Master.h
功能描述:IO端口模拟的IIC(I2C)主机函数库头文件
作 者:张暄
版 本:V1.0
日 期:2016.12.21
******************************************************************************/
#ifndef _SIMULATED_IIC_MASTER_H_
#define _SIMULATED_IIC_MASTER_H_
#include "common.h"
/******************************************************************************
1us延时校准
******************************************************************************/
#define IIC_FREQ_CALIBRATION 4
/******************************************************************************
确认信号
******************************************************************************/
#define IIC_ACK 0 //肯定应答
#define IIC_NAK 1 //否定应答
#define IIC_INVALID_ACK 2 //无效的应答信号(应答信号还未收到)
/******************************************************************************
函数声明
******************************************************************************/
void LtDet_IIC_Init(void);
void LtDet_IIC_Start(void);
void LtDet_IIC_Stop(void);
void LtDet_IIC_Transmit_Data(uint8_t Data);
uint8_t LtDet_IIC_Receive_Data(void);
void LtDet_IIC_Transmit_ACK(uint8_t ACK);
uint8_t LtDet_IIC_Receive_ACK(void);
void LtDet_IIC_Delay(void);
#endif
#ifndef __BSP_BU27030_H_
#define __BSP_BU27030_H_
#include "common.h"
void BU2703_Read_Datas(void );
uint16_t BU2703_Get_Data1(void );
void BU2703_Senser_Init(void );
void LIGHT_IIC_Write_Data(uint8_t devAddr, uint8_t len, uint8_t *Data);
uint8_t LIGHT_IIC_Read_Byte(uint8_t devAddr, uint8_t addr2, uint8_t *pData);
void App_SortNWord_u16(uint16_t *SortData,uint8_t len);
void App_SwapWord_u16(uint16_t *pData1,uint16_t *pData2);
#endif
#include "r_typedefs.h"
#include "Internal_Flash.h"
#include "Emulated_EEPROM.h"
#include "Emulated_EEPROM_Access.h"
/****************************************************************************
* !!! The following code should be generated by configuration tool !!! *
****************************************************************************/
#define EEPROM_BLOCK0_BUFFER_SIZE (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_UDS_FLAG_SIZE)
uint32_t g_u32EEPROMBlock0Buffer[EEPROM_BLOCK0_BUFFER_SIZE / 4U];
#define EEPROM_BLOCK1_BUFFER_SIZE (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_UDS_S27_SIZE)
uint32_t g_u32EEPROMBlock1Buffer[EEPROM_BLOCK1_BUFFER_SIZE / 4U];
#define EEPROM_BLOCK2_BUFFER_SIZE (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_UDS_S2E0_SIZE)
uint32_t g_u32EEPROMBlock2Buffer[EEPROM_BLOCK2_BUFFER_SIZE / 4U];
#define EEPROM_BLOCK3_BUFFER_SIZE (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_UDS_S2E_SIZE)
uint32_t g_u32EEPROMBlock3Buffer[EEPROM_BLOCK3_BUFFER_SIZE / 4U];
#define EEPROM_BLOCK4_BUFFER_SIZE (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_UDS_CONFIG_SIZE)
uint32_t g_u32EEPROMBlock4Buffer[EEPROM_BLOCK4_BUFFER_SIZE / 4U];
#define EEPROM_BLOCK5_BUFFER_SIZE (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_UDS_DTC_SIZE)
uint32_t g_u32EEPROMBlock5Buffer[EEPROM_BLOCK5_BUFFER_SIZE / 4U];
#define EEPROM_BLOCK6_BUFFER_SIZE (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_ODO_SIZE)
uint32_t g_u32EEPROMBlock6Buffer[EEPROM_BLOCK6_BUFFER_SIZE / 4U];
#define EEPROM_BLOCK7_BUFFER_SIZE (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_ODO_STAMP_SIZE)
uint32_t g_u32EEPROMBlock7Buffer[EEPROM_BLOCK7_BUFFER_SIZE / 4U];
#define EEPROM_BLOCK8_BUFFER_SIZE (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_TRIP_STAMP_SIZE)
uint32_t g_u32EEPROMBlock8Buffer[EEPROM_BLOCK8_BUFFER_SIZE / 4U];
#define EEPROM_BLOCK9_BUFFER_SIZE (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_SERVICE_INFO_SIZE)
uint32_t g_u32EEPROMBlock9Buffer[EEPROM_BLOCK9_BUFFER_SIZE / 4U];
#define EEPROM_BLOCK10_BUFFER_SIZE (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_UE_INFO_SIZE)
uint32_t g_u32EEPROMBlock10Buffer[EEPROM_BLOCK10_BUFFER_SIZE / 4U];
static const EEPROM_Block_st_t g_stEEPROMBlockTable[] =
{
/* u32StartAddr u32EndAddr u32DataSize pu32DataBuffer */
{0xFF200000UL, 0xFF20007FUL, EEPROM_BLOCK0_BUFFER_SIZE, g_u32EEPROMBlock0Buffer,},
{0xFF200080UL, 0xFF2000FFUL, EEPROM_BLOCK1_BUFFER_SIZE, g_u32EEPROMBlock1Buffer,},
{0xFF200100UL, 0xFF2002FFUL, EEPROM_BLOCK2_BUFFER_SIZE, g_u32EEPROMBlock2Buffer,},
{0xFF200300UL, 0xFF2004FFUL, EEPROM_BLOCK3_BUFFER_SIZE, g_u32EEPROMBlock3Buffer,},
{0xFF200500UL, 0xFF20057FUL, EEPROM_BLOCK4_BUFFER_SIZE, g_u32EEPROMBlock4Buffer,},
{0xFF200580UL, 0xFF20067FUL, EEPROM_BLOCK5_BUFFER_SIZE, g_u32EEPROMBlock5Buffer,},
{0xFF200680UL, 0xFF200A7FUL, EEPROM_BLOCK6_BUFFER_SIZE, g_u32EEPROMBlock6Buffer,},
{0xFF200A80UL, 0xFF200F7FUL, EEPROM_BLOCK7_BUFFER_SIZE, g_u32EEPROMBlock7Buffer,},
{0xFF200F80UL, 0xFF20197FUL, EEPROM_BLOCK8_BUFFER_SIZE, g_u32EEPROMBlock8Buffer,},
{0xFF201980UL, 0xFF2019FFUL, EEPROM_BLOCK9_BUFFER_SIZE, g_u32EEPROMBlock9Buffer,},
{0xFF201A00UL, 0xFF201FFFUL, EEPROM_BLOCK10_BUFFER_SIZE, g_u32EEPROMBlock10Buffer,},
};
/****************************************************************************
* !!! End of code generation !!! *
****************************************************************************/
EEPROM_Block_Access_st_t g_stEEPROMBlockAccess[EEPROM_TOTAL_BLOCK_NUM];
void EEPROM_Init(void)
{
EEPROM_Media_Access_st_t stFlashAccessFunc;
stFlashAccessFunc.pfnMemErase = Int_Data_Flash_Erase;
stFlashAccessFunc.pfnMemBlankChk = Int_Data_Flash_Blank_Check;
stFlashAccessFunc.pfnMemRead = Int_Data_Flash_Read;
stFlashAccessFunc.pfnMemWrite = Int_Data_Flash_Write;
Int_Data_Flash_Register_P_E_Complete_Cb(EEPROM_Mem_Access_Complete_Callback);
EEPROM_Access_Init( g_stEEPROMBlockTable,
g_stEEPROMBlockAccess,
&stFlashAccessFunc,
EEPROM_TOTAL_BLOCK_NUM);
}
EEPROM_Status_en_t EEPROM_Get_Status(void)
{
EEPROM_Status_en_t enStatus;
if(EEPROM_Access_Busy())
{
enStatus = EEPROM_STAT_BUSY;
}
else
{
enStatus = EEPROM_STAT_IDLE;
}
return enStatus;
}
EEPROM_Block_Status_en_t EEPROM_Get_Block_Status(uint16_t u16BlockID)
{
uint8_t u8BlockStatus;
EEPROM_Block_Status_en_t enStatus;
u8BlockStatus = EEPROM_Block_Access_Status(u16BlockID);
if (u8BlockStatus == EEPROM_BLOCK_BLANK)
{
enStatus = EEPROM_STAT_BLANK;
}
else if (u8BlockStatus == EEPROM_BLOCK_ACTIVE)
{
enStatus = EEPROM_STAT_ACTIVE;
}
else
{
enStatus = EEPROM_STAT_ERROR;
}
return enStatus;
}
EEPROM_RW_Result_en_t EEPROM_Read_Data(uint16_t u16BlockID, uint32_t u32Data[], uint16_t u16Len)
{
uint8_t u8Error;
EEPROM_RW_Result_en_t enResult;
u8Error = EEPROM_Read_Block_Data(u16BlockID, u32Data, u16Len);
if (u8Error)
{
enResult = EEPROM_RW_FAIL;
}
else
{
enResult = EEPROM_RW_PASS;
}
return enResult;
}
EEPROM_RW_Result_en_t EEPROM_Write_Data(uint16_t u16BlockID, uint32_t u32Data[], uint16_t u16Len)
{
uint8_t u8Error;
EEPROM_RW_Result_en_t enResult;
u8Error = EEPROM_Write_Block_Data(u16BlockID, u32Data, u16Len);
if (u8Error)
{
enResult = EEPROM_RW_FAIL;
}
else
{
enResult = EEPROM_RW_PASS;
}
return enResult;
}
/******************************************************************************
�?�?名:Simulated_IIC_Master.c
功能描述:IO端口模拟的IIC(I2C)主机函数库文�?
�? 者:张暄
�? 本:V1.0
�? 期:2016.12.21
******************************************************************************/
#include "IIC.h"
#include "gpio.h"
uint8_t IIC_addr = 0u;
__inline void bsp_IIC_nop( void )
{
__NOP();
}
void bsp_IIC_SCL_inConfig( void )
{
*((volatile uint8_t*)(&PORT->PMC0+SCL_LED_DRIVER_PORT)) &= ~( 1 << SCL_LED_DRIVER_PIN );
*((volatile uint8_t*)(&PORT->PM0+SCL_LED_DRIVER_PORT)) |= ( 1 << SCL_LED_DRIVER_PIN );
*((volatile uint8_t*)(&PORT->PIM0+SCL_LED_DRIVER_PORT)) &= ~( 1 << SCL_LED_DRIVER_PIN );
*((volatile uint8_t*)(&PORT->POM0+SCL_LED_DRIVER_PORT)) &= ~( 1 << SCL_LED_DRIVER_PIN );
*((volatile uint8_t*)(&PORT->PU0+SCL_LED_DRIVER_PORT)) &= ~( 1 << SCL_LED_DRIVER_PIN );
}
void bsp_IIC_SCL_outConfig( void )
{
*((volatile uint8_t*)(&PORT->PMC0+SCL_LED_DRIVER_PORT)) &= ~( 1 << SCL_LED_DRIVER_PIN );
*((volatile uint8_t*)(&PORT->PM0+SCL_LED_DRIVER_PORT)) &= ~( 1 << SCL_LED_DRIVER_PIN );
*((volatile uint8_t*)(&PORT->PIM0+SCL_LED_DRIVER_PORT)) &= ~( 1 << SCL_LED_DRIVER_PIN );
*((volatile uint8_t*)(&PORT->POM0+SCL_LED_DRIVER_PORT)) &= ~( 1 << SCL_LED_DRIVER_PIN );
}
void bsp_IIC_SCL_out( uint8_t dat )
{
if ( dat )
{
*( ( volatile uint8_t* )( &PORT->P0 + 6 ) ) |= ( 1 << PIN2 );
}
else
{
*( ( volatile uint8_t * )( &PORT->P0 + 6 ) ) &= ( ~( 1 << PIN2 ) );
}
}
void bsp_IIC_SDA_inConfig( void )
{
PORT_Init( IIC_SDA_27030, INPUT );
}
void bsp_IIC_SDA_outConfig( void )
{
PORT_Init( IIC_SDA_27030, OUTPUT );
}
void bsp_IIC_SDA_out( uint8_t dat )
{
if ( dat )
{
*( ( volatile uint8_t* )( &PORT->P0 + 6 ) ) |= ( 1 << PIN3 );
}
else
{
*( ( volatile uint8_t * )( &PORT->P0 + 6 ) ) &= ( ~( 1 << PIN3 ) );
}
}
uint8_t bsp_IIC_SDA_getVal( void )
{
if ( PORT_GetBit( IIC_SDA_27030 ) )
{ return 1; }
else
{ return 0; }
}
/******************************************************************************
函数名:IIC_Init
�? 能:初始化IIC总线
�? 数:�?
返回值:�?
******************************************************************************/
void Simulated_IIC_2_Init( void )
{
*((volatile uint8_t*)(&PORT->PMC0+SCL_LED_DRIVER_PORT)) &= ~( 1 << SCL_LED_DRIVER_PIN );
*((volatile uint8_t*)(&PORT->PM0+SCL_LED_DRIVER_PORT)) &= ~( 1 << SCL_LED_DRIVER_PIN );
*((volatile uint8_t*)(&PORT->PIM0+SCL_LED_DRIVER_PORT)) &= ~( 1 << SCL_LED_DRIVER_PIN );
*((volatile uint8_t*)(&PORT->POM0+SCL_LED_DRIVER_PORT)) &= ~( 1 << SCL_LED_DRIVER_PIN );
*((volatile uint8_t*)(&PORT->PMC0+SDA_LED_DRIVER_PORT)) &= ~( 1 << SDA_LED_DRIVER_PIN );
*((volatile uint8_t*)(&PORT->PM0+SDA_LED_DRIVER_PORT)) &= ~( 1 << SDA_LED_DRIVER_PIN );
*((volatile uint8_t*)(&PORT->PIM0+SDA_LED_DRIVER_PORT)) &= ~( 1 << SDA_LED_DRIVER_PIN );
*((volatile uint8_t*)(&PORT->POM0+SDA_LED_DRIVER_PORT)) &= ~( 1 << SDA_LED_DRIVER_PIN );
bsp_IIC_Stop();
}
void Simulated_Release_SCL( void )
{
bsp_IIC_SCL_out( 0 );
bsp_IIC_delay( 10 );
}
/*-------------------------------------------------------------------------
* Function Name : bsp_delay
* Description :
* Input : None
* Output : None
* Return : None
* onther :
--------------------------------------------------------------------------*/
void bsp_IIC_delay( uint32_t time )
{
uint32_t i;
for ( i = 0; i <= time; i++ )
{
bsp_IIC_nop();
}
}
/******************************************************************************
函数名:IIC_Start
�? 能:在IIC总线上发送起始信�?发起一次IIC通信
�? 数:�?
返回值:�?
******************************************************************************/
void bsp_IIC_Start(void)
{
bsp_IIC_SDA_outConfig();
bsp_IIC_SDA_out( 1 );
bsp_IIC_nop();
bsp_IIC_SCL_out( 1 );
bsp_IIC_nop();
bsp_IIC_SDA_out( 0 );
bsp_IIC_nop();
bsp_IIC_SCL_out( 0 );
bsp_IIC_nop();
}
/******************************************************************************
函数名:IIC_Stop
�? 能:在IIC总线上发送停止信�?终止当前IIC通信
�? 数:�?
返回值:�?
******************************************************************************/
void bsp_IIC_Stop( void )
{
bsp_IIC_SDA_outConfig();
bsp_IIC_SCL_out( 0 );
bsp_IIC_nop();
bsp_IIC_SDA_out( 0 );
bsp_IIC_nop();
bsp_IIC_SCL_out( 1 );
bsp_IIC_nop();
bsp_IIC_SDA_out( 1 );
bsp_IIC_nop();
}
/******************************************************************************
函数名:IIC_Transmit_Data
�? 能:向IIC总线上发�?字节数据
�? 数:Data:发送的数据
返回值:总线上器件的确认(ACK) 0 - ACK 1 - NAK
******************************************************************************/
void bsp_IIC_SendByte( uint8_t Data )
{
uint8_t temp;
uint8_t d = Data;
bsp_IIC_SDA_outConfig();
for ( temp = 8; temp != 0; temp-- )
{
if ( d & 0x80 )
{
bsp_IIC_SDA_out( 1 );
}
else
{
bsp_IIC_SDA_out( 0 );
}
bsp_IIC_nop();
bsp_IIC_SCL_out( 1 );
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_SCL_out( 0 );
bsp_IIC_nop();
d = d << 1;
bsp_IIC_nop();
}
}
/*-------------------------------------------------------------------------
* Function Name : bsp_IIC_ReadByte
* Description :
* Input : None
* Output : None
* Return : None
* onther :
--------------------------------------------------------------------------*/
uint8_t bsp_IIC_ReadByte( void )
{
uint8_t temp;
uint8_t rbyte = 0;
bsp_IIC_SDA_inConfig();
bsp_IIC_nop();
for ( temp = 8; temp != 0; temp-- )
{
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_SCL_out( 1 );
bsp_IIC_nop();
rbyte = rbyte << 1;
bsp_IIC_nop();
rbyte = rbyte | ( ( uint8_t )( bsp_IIC_SDA_getVal() ) );
bsp_IIC_SCL_out( 0 );
bsp_IIC_nop();
}
return ( rbyte );
}
/*-------------------------------------------------------------------------
* Function Name : bsp_IIC_ACK
* Description : ack
* Input : None
* Output : None
* Return : None
* onther :
--------------------------------------------------------------------------*/
void bsp_IIC_ACK( void )
{
bsp_IIC_SDA_outConfig();
bsp_IIC_SDA_out( 0 );
bsp_IIC_nop();
bsp_IIC_SCL_out( 1 );
bsp_IIC_nop();
bsp_IIC_SCL_out( 0 );
bsp_IIC_nop();
bsp_IIC_SDA_out( 1 );
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
}
/*-------------------------------------------------------------------------
* Function Name : bsp_IIC_NACK
* Description : nack
* Input : None
* Output : None
* Return : None
* onther :
--------------------------------------------------------------------------*/
void bsp_IIC_NACK( void )
{
bsp_IIC_SDA_outConfig();
bsp_IIC_nop();
bsp_IIC_SDA_out( 1 );
bsp_IIC_nop();
bsp_IIC_SCL_out( 1 );
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_SCL_out( 0 );
bsp_IIC_nop();
}
/******************************************************************************
函数名:bsp_IIC_WaitAck
�? 能:获取从总线上接收到的确认信�?
�? 数:�?
返回值:
******************************************************************************/
uint8_t bsp_IIC_WaitAck( void )
{
uint8_t ack = 1;
uint16_t i = IIC_WaitOverTime;
bsp_IIC_SDA_inConfig();
bsp_IIC_SCL_out( 1 );
while ( ( ack ) && ( --i != 0 ) )
{
ack = bsp_IIC_SDA_getVal();
}
bsp_IIC_SCL_out( 0 );
if ( IIC_addr == 0x38 )
{
}
else if ( IIC_addr == 0x78 )
{
// if ( ack )
// {
// if ( IIC_3236_NACK_Time < 0xFFFFFFFF )
// { IIC_3236_NACK_Time ++; }
// }
// else
// { IIC_3236_NACK_Time = 0; }
}
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
bsp_IIC_nop();
return ( ack );
}
/******************************************************************************
文 件 名:Simulated_IIC_Master.c
功能描述:IO端口模拟的IIC(I2C)主机函数库文件
作 者:张暄
版 本:V1.0
日 期:2016.12.21
******************************************************************************/
#include "LtDet_Simulated_IIC_Master.h"
#include "gpio.h"
uint8_t LtDet_IICReceivedACK;
/******************************************************************************
函数名:IIC_Init
功 能:初始化IIC总线
参 数:无
返回值:无
******************************************************************************/
void LtDet_IIC_Init(void)
{
// PORT_Init(IIC_SCL_LtDet, OPENDRAIN_OUTPUT); //SCL被设置为输入,IO口为高阻状态,总线上SCL信号由上拉电阻拉高,此时SCL = 1
LtDet_IIC_Delay();
LtDet_IIC_Delay();
//PORT_Init(IIC_SDA_LtDet, INPUT); //SDA被设置为输入,IO口为高阻状态,总线上SDA信号由上拉电阻拉高,此时SDA = 1
LtDet_IIC_Delay();
LtDet_IIC_Delay();
LtDet_IICReceivedACK = IIC_INVALID_ACK;
}
/******************************************************************************
函数名:IIC_Start
功 能:在IIC总线上发送起始信号,发起一次IIC通信
参 数:无
返回值:无
******************************************************************************/
void LtDet_IIC_Start(void)
{
LtDet_IICReceivedACK = IIC_INVALID_ACK;
// PORT_SetBit(IIC_SDA_LtDet); //SDA被设置为输入,IO口为高阻状态,总线上SDA信号由上拉电阻拉高,此时SDA = 1
LtDet_IIC_Delay();
LtDet_IIC_Delay();
// PORT_SetBit(IIC_SCL_LtDet); //SCL被设置为输入,IO口为高阻状态,总线上SCL信号由上拉电阻拉高,此时SCL = 1
LtDet_IIC_Delay();
LtDet_IIC_Delay();
// PORT_Init(IIC_SDA_LtDet, OPENDRAIN_OUTPUT); //SDA输出0,此时SDA = 0
// PORT_ClrBit(IIC_SDA_LtDet);
LtDet_IIC_Delay();
LtDet_IIC_Delay();
// PORT_ClrBit(IIC_SCL_LtDet); //SCL输出0,此时SCL = 0
LtDet_IIC_Delay();
}
/******************************************************************************
函数名:IIC_Stop
功 能:在IIC总线上发送停止信号,终止当前IIC通信
参 数:无
返回值:无
******************************************************************************/
void LtDet_IIC_Stop(void)
{
LtDet_IICReceivedACK = IIC_INVALID_ACK;
// PORT_Init(IIC_SDA_LtDet, OPENDRAIN_OUTPUT); //SDA输出0,此时SDA = 0
// PORT_ClrBit(IIC_SDA_LtDet);
LtDet_IIC_Delay();
LtDet_IIC_Delay();
// PORT_SetBit(IIC_SCL_LtDet); //SCL被设置为输入,IO口为高阻状态,总线上SCL信号由上拉电阻拉高,此时SCL = 1
LtDet_IIC_Delay();
LtDet_IIC_Delay();
// PORT_SetBit(IIC_SDA_LtDet); //SDA被设置为输入,IO口为高阻状态,总线上SDA信号由上拉电阻拉高,此时SDA = 1
LtDet_IIC_Delay();
LtDet_IIC_Delay();
}
/******************************************************************************
函数名:IIC_Transmit_Data
功 能:向IIC总线上发送1字节数据
参 数:Data:发送的数据
返回值:总线上器件的确认(ACK) 0 - ACK 1 - NAK
******************************************************************************/
void LtDet_IIC_Transmit_Data(uint8_t Data)
{
uint8_t i;
LtDet_IICReceivedACK = IIC_INVALID_ACK;
for (i = 0; i < 8; i++)
{
// PORT_ClrBit(IIC_SCL_LtDet);
LtDet_IIC_Delay();
if (Data & 0x80){
// PORT_Init(IIC_SDA_LtDet, OPENDRAIN_OUTPUT);
// PORT_SetBit(IIC_SDA_LtDet);
// IIC_SDA_DIR = 0; //SDA被设置为输入,IO口为高阻状态,总线上SDA信号由上拉电阻拉高,此时SDA = 1
;
}
else
{
// PORT_Init(IIC_SDA_LtDet, OPENDRAIN_OUTPUT);
// PORT_ClrBit(IIC_SDA_LtDet);
// IIC_SDA_DIR = 1; //SDA输出0,此时SDA = 0
// IIC_SDA = 0;
}
LtDet_IIC_Delay();
LtDet_IIC_Delay();
// PORT_SetBit(IIC_SCL_LtDet);
// IIC_SCL_DIR = 0; //SCL被设置为输入,IO口为高阻状态,总线上SCL信号由上拉电阻拉高,此时SCL = 1
LtDet_IIC_Delay();
LtDet_IIC_Delay();
// IIC_SCL_DIR = 1; //SCL输出0,此时SCL = 0
// IIC_SCL = 0;
Data <<= 1;
}
// PORT_ClrBit(IIC_SCL_LtDet);
// PORT_Init(IIC_SDA_LtDet, INPUT);
LtDet_IIC_Delay();
LtDet_IIC_Delay();
// PORT_SetBit(IIC_SCL_LtDet);
// IIC_SCL_DIR = 0; //SCL被设置为输入,IO口为高阻状态,总线上SCL信号由上拉电阻拉高,此时SCL = 1
LtDet_IIC_Delay();
LtDet_IIC_Delay();
// if(PORT_GetBit(IIC_SDA_LtDet)){
// LtDet_IICReceivedACK = 1;
// }
// else{
// LtDet_IICReceivedACK = 0;
// }
// IICReceivedACK = IIC_SDA;
// PORT_ClrBit(IIC_SCL_LtDet);
// IIC_SCL_DIR = 1; //SCL输出0,此时SCL = 0
// IIC_SCL = 0;
LtDet_IIC_Delay();
}
/******************************************************************************
函数名:IIC_Receive_Data
功 能:从IIC总线上接收1字节数据
参 数:无
返回值:接收到的数据
******************************************************************************/
uint8_t LtDet_IIC_Receive_Data(void)
{
uint8_t i;
uint8_t Data;
Data = 0;
// PORT_Init(IIC_SDA_LtDet, INPUT);
//IIC_SDA_DIR = 0; //设置SDA为输入状态
LtDet_IIC_Delay();
LtDet_IIC_Delay();
for(i = 0; i < 8; i++)
{
Data <<= 1;
// PORT_SetBit(IIC_SCL_LtDet);
// IIC_SCL_DIR = 0; //SCL被设置为输入,IO口为高阻状态,总线上SCL信号由上拉电阻拉高,此时SCL = 1
LtDet_IIC_Delay();
LtDet_IIC_Delay();
LtDet_IIC_Delay();
// if(PORT_GetBit(IIC_SDA_LtDet))
// Data |= 1;
// PORT_ClrBit(IIC_SCL_LtDet);
// IIC_SCL_DIR = 1; //SCL输出0,此时SCL = 0
// IIC_SCL = 0;
LtDet_IIC_Delay();
LtDet_IIC_Delay();
}
return Data;
}
/******************************************************************************
函数名:IIC_Transmit_ACK
功 能:向IIC总线上发送确认信号
参 数:ACK:确认信号的类型: IIC_ACK 肯定应答
IIC_NAK 否定应答
返回值:无
******************************************************************************/
void LtDet_IIC_Transmit_ACK(uint8_t ACK)
{
LtDet_IICReceivedACK = IIC_INVALID_ACK;
if (ACK > IIC_NAK)
return;
// PORT_Init(IIC_SDA_LtDet, OPENDRAIN_OUTPUT);
// if(ACK)
// PORT_SetBit(IIC_SDA_LtDet);
// IIC_SDA_DIR = 0; //SDA被设置为输入,IO口为高阻状态,总线上SDA信号由上拉电阻拉高,此时SDA = 1
// else
// {
// PORT_ClrBit(IIC_SDA_LtDet);
// IIC_SDA_DIR = 1; //SDA输出0,此时SDA = 0
// IIC_SDA = 0;
// }
// LtDet_IIC_Delay();
// PORT_SetBit(IIC_SCL_LtDet);
// IIC_SCL_DIR = 0; //SCL被设置为输入,IO口为高阻状态,总线上SCL信号由上拉电阻拉高,此时SCL = 1
// LtDet_IIC_Delay();
// LtDet_IIC_Delay();
//
// PORT_ClrBit(IIC_SCL_LtDet);
// // IIC_SCL_DIR = 1; //SCL输出0,此时SCL = 0
// // IIC_SCL = 0;
// LtDet_IIC_Delay();
}
/******************************************************************************
函数名:IIC_Receive_ACK
功 能:获取从总线上接收到的确认信号
参 数:无
返回值:总线上器件的确认信号类型:IIC_ACK 肯定应答
IIC_NAK 否定应答
IIC_INVALID_ACK 无效的应答信号(应答信号还未收到)
******************************************************************************/
uint8_t LtDet_IIC_Receive_ACK(void)
{
uint8_t ACK;
ACK = LtDet_IICReceivedACK;
LtDet_IICReceivedACK = IIC_INVALID_ACK;
return ACK;
}
/******************************************************************************
函数名:IIC_Delay
功 能:延时5us,用于收发过程中的时序控制
注意:必须调整IIC_5us_DELAY_CALIBRATION参数校准延时时间在5us左右
参 数:ACK:确认信号的类型 0 - ACK 1 - NAK
返回值:无
******************************************************************************/
void LtDet_IIC_Delay(void)
{
uint8_t i;
for(i = 0; i < IIC_FREQ_CALIBRATION; i++)
__NOP();
}
#include "bsp_BU27030.h"
#include "common.h"
#include "IIC.h"
uint16_t IIC1_CheckSDA_Times = 0u;
uint16_t IIC1_NACK_Times = 0u;
#define BU2703_SLAVE_ADDR 0X38
#define BU2703_FITER_SIZE 10
typedef struct
{
/* data */
uint16_t data1buf[BU2703_FITER_SIZE];
uint8_t index1;
uint16_t data1;
}BU2703_Datas_Struct;
BU2703_Datas_Struct BU2703Dat;
uint8_t Register_BU2703_Senser[] =
{
0x40,0X80,
0x41,0X00,
0x42,0X22,
0x43,0X01,
};
void LIGHT_IIC_Write_Data(uint8_t devAddr, uint8_t len, uint8_t *Data)
{
uint8_t Ack = 0;
bsp_IIC_Start( );
bsp_IIC_SendByte(devAddr*2);
Ack = bsp_IIC_WaitAck( );
if ( Ack != 0 )
{
bsp_IIC_Stop( );
}
while (len )
{
len-- ;
bsp_IIC_SendByte(*(Data++));
Ack = bsp_IIC_WaitAck( );
if ( Ack != 0 )
{
bsp_IIC_Stop( );
}
};
bsp_IIC_Stop( );
}
void BU2703_Senser_Init(void )
{
uint8_t mAddr = 0XFF;
uint16_t i = 0;
mAddr = 0X38;
Simulated_IIC_2_Init();
for ( i = 0; i < 4; i++)
{
LIGHT_IIC_Write_Data(mAddr, 2, &Register_BU2703_Senser[i*2]);
}
}
uint8_t LIGHT_IIC_Read_Byte(uint8_t devAddr, uint8_t addr2, uint8_t *pData)
{
uint8_t Ack = 0;
bsp_IIC_Start( );
bsp_IIC_SendByte(devAddr * 2);
Ack = bsp_IIC_WaitAck( );
if ( Ack != 0 )
{
bsp_IIC_Stop( );
return 0;
}
bsp_IIC_SendByte(addr2);
Ack = bsp_IIC_WaitAck( );
if ( Ack != 0 )
{
bsp_IIC_Stop( );
return 0;
}
bsp_IIC_Start( );
bsp_IIC_SendByte(devAddr * 2 + 1);
Ack = bsp_IIC_WaitAck( );
if ( Ack != 0 )
{
bsp_IIC_Stop( );
return 0;
}
*pData = bsp_IIC_ReadByte( );
bsp_IIC_NACK();
// 停止IIC
bsp_IIC_Stop( );
return 1;
}
void BU2703_Read_Datas(void )
{
uint8_t dataL = 0;
uint8_t dataH = 0;
uint8_t meas_en = 0;
uint16_t temp = 0xffff;
LIGHT_IIC_Read_Byte(BU2703_SLAVE_ADDR, 0x43, &meas_en);
if(meas_en & 0x80)
{
LIGHT_IIC_Read_Byte(BU2703_SLAVE_ADDR, 0x52, &dataL);
LIGHT_IIC_Read_Byte(BU2703_SLAVE_ADDR, 0x53, &dataH);
temp = dataH;
temp = (temp << 8);
temp |= dataL;
if(BU2703Dat.index1 >= BU2703_FITER_SIZE)
{
BU2703Dat.index1 = 0;
App_SortNWord_u16(BU2703Dat.data1buf, BU2703_FITER_SIZE);
BU2703Dat.data1 = BU2703Dat.data1buf[BU2703_FITER_SIZE / 2]; //15*50ms update
}
if(temp < 0xffff)
{
BU2703Dat.data1buf[BU2703Dat.index1] = temp;
BU2703Dat.index1++;
}
}
}
uint16_t BU2703_Get_Data1(void )
{
return BU2703Dat.data1;
}
void App_SwapWord_u16(uint16_t *pData1,uint16_t *pData2)
{
uint16_t psw;
if((*pData1) > (*pData2))
{
psw = (*pData2);
(*pData2) = (*pData1);
(*pData1) = psw;
}
}
void App_SortNWord_u16(uint16_t *SortData,uint8_t len)
{
uint8_t n=0,m;
for(;n < len-1;n++)
{
for(m = n+1;m < len;m++)
{
App_SwapWord_u16((SortData + n),(SortData + m));
}
}
}
......@@ -538,4 +538,7 @@ typedef enum
NM_Mode_Wakeup,
} NM_Mode_t;
#define GPIO_DIR_OUT 0
#define GPIO_DIR_IN 1
#endif
......@@ -140,7 +140,7 @@ void Sys_Run_Mode_100ms_Tasks_Group2(void)
void Sys_Run_Mode_100ms_Tasks_Group3(void)
{
BU2703_Read_Datas();
}
void Sys_Run_Mode_100ms_Tasks_Group4(void)
......
This diff is collapsed.
......@@ -339,7 +339,7 @@
<MiscControls></MiscControls>
<Define></Define>
<Undefine></Undefine>
<IncludePath>.\source\System;.\source\Driver\inc;.\source\Application;.\source\Application\LINE_IN;.\source\Application\PowerManagement;.\source\Application\RTE;.\source\Application\MCU_Core_Protocol;.\source\Application\Telltales;.\source\Application\Fuel;.\source\Application\Eeprom;.\source\Application\DataProcessor;.\source\Application\LightDetect;.\source\Application\BackLight;.\source\Application\Key;.\source\Application\Menu;.\source\Application\APPCan;.\source\UDS;.\source\Application\Gauges;.\source\Application\CoolantTemp;.\source\UDS2;.\\RTE\Device\BAT32A239KK64FB;.\source\Application\Mileage;.\source\Application\Display\GUI_LCD_DIS;.\source\Application\Display\SEG_LCD_DIS</IncludePath>
<IncludePath>.\source\System;.\source\Driver\inc;.\source\Application;.\source\Application\LINE_IN;.\source\Application\PowerManagement;.\source\Application\RTE;.\source\Application\MCU_Core_Protocol;.\source\Application\Telltales;.\source\Application\Fuel;.\source\Application\Eeprom;.\source\Application\DataProcessor;.\source\Application\LightDetect;.\source\Application\BackLight;.\source\Application\Key;.\source\Application\Menu;.\source\Application\APPCan;.\source\UDS;.\source\Application\Gauges;.\source\Application\CoolantTemp;.\source\UDS2;.\\RTE\Device\BAT32A239KK64FB;.\source\Application\Mileage;.\source\Application\Display\GUI_LCD_DIS;.\source\Application\Display\SEG_LCD_DIS;.\source\Driver\inc</IncludePath>
</VariousControls>
</Cads>
<Aads>
......@@ -543,6 +543,26 @@
<FileType>1</FileType>
<FilePath>.\source\Driver\src\flash.c</FilePath>
</File>
<File>
<FileName>bsp_BU27030.h</FileName>
<FileType>5</FileType>
<FilePath>.\source\Driver\inc\bsp_BU27030.h</FilePath>
</File>
<File>
<FileName>bsp_BU27030.c</FileName>
<FileType>1</FileType>
<FilePath>.\source\Driver\src\bsp_BU27030.c</FilePath>
</File>
<File>
<FileName>IIC.h</FileName>
<FileType>5</FileType>
<FilePath>.\source\Driver\inc\IIC.h</FilePath>
</File>
<File>
<FileName>IIC.c</FileName>
<FileType>1</FileType>
<FilePath>.\source\Driver\src\IIC.c</FilePath>
</File>
</Files>
</Group>
<Group>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment