242 lines
5.7 KiB
C
242 lines
5.7 KiB
C
#include "Config.h"
|
|
#include "ls1x.h"
|
|
#include "ls1x_latimer.h"
|
|
#include "ls1x_clock.h"
|
|
#include "ls1x_gpio.h"
|
|
#include "1c102_myGPIO.h"
|
|
#include "1c102_myI2C.h"
|
|
|
|
/**初始化部分**/
|
|
void MY_I2C_StructInit(I2C_InitTypeDef *I2C_InitStruct)
|
|
{
|
|
/*---------------- Reset I2C init structure parameters values ----------------*/
|
|
/* initialize the I2C_ClockSpeed member */
|
|
I2C_InitStruct->I2C_ClockSpeed = 100000;
|
|
/* Initialize the I2C_Mode member */
|
|
I2C_InitStruct->I2C_Mode = I2C_Mode_Master;
|
|
// I2C_InitStruct->I2C_Mode = I2C_Mode_Slave;
|
|
/* Initialize the I2C_OwnAddress1 member */
|
|
I2C_InitStruct->I2C_OwnAddress1 = 0x2A;
|
|
/* Initialize the I2C_Buslock Check */
|
|
I2C_InitStruct->I2C_BuslockCheckEn = I2C_Buslock_Check_Enable;
|
|
/* Initialize the I2C_Slave_Autoreset */
|
|
I2C_InitStruct->I2C_SlvAutoresetEn = I2C_Slv_Autoreset_Disable;
|
|
}
|
|
|
|
void MY_I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
|
|
{
|
|
uint32_t tmp = 0;
|
|
uint32_t pclk1 = SystemFreq;
|
|
|
|
/*---------------------------- I2Cx Configuration ------------------------*/
|
|
I2Cx->CTRL = 0x20; //访问分频寄存器
|
|
tmp = pclk1/I2C_InitStruct->I2C_ClockSpeed;
|
|
tmp = (tmp>>2)-1;
|
|
I2Cx->PRERL = tmp;
|
|
I2Cx->PRERH = tmp >> 8;
|
|
|
|
/* Enable the selected I2C peripheral */
|
|
I2Cx->CTRL = 0x80|(I2C_InitStruct->I2C_Mode)|(I2C_InitStruct->I2C_BuslockCheckEn)|(I2C_InitStruct->I2C_SlvAutoresetEn);
|
|
if (I2C_InitStruct->I2C_BuslockCheckEn)
|
|
{
|
|
I2Cx->CR_SR = 0x04;
|
|
}
|
|
|
|
I2Cx->SADDR = I2C_InitStruct->I2C_OwnAddress1;
|
|
I2C_wait(I2C);
|
|
|
|
return;
|
|
}
|
|
|
|
void MY_I2C_InitTask()
|
|
{
|
|
remap_gpio(MYI2C_SDA_GPIO_INDEX, GPIO_FUNC_MAIN);
|
|
remap_gpio(MYI2C_SCL_GPIO_INDEX, GPIO_FUNC_MAIN);
|
|
|
|
I2C_InitTypeDef My_I2C_InitStruct;
|
|
MY_I2C_StructInit(&My_I2C_InitStruct);
|
|
My_I2C_InitStruct.I2C_ClockSpeed = MYI2C_CLOCK_FAST_SPEED;
|
|
I2C_Init(I2C, &My_I2C_InitStruct);
|
|
delay_ms(500);
|
|
|
|
return;
|
|
}
|
|
|
|
/**工具部分*/
|
|
void myI2C_write_signal(unsigned char dev_addr, unsigned char reg_addr, unsigned char w_data)
|
|
{
|
|
//unsigned int v = SaveDisableInt();
|
|
|
|
I2C_GenerateSTART(I2C,ENABLE);
|
|
I2C_wait(I2C);
|
|
|
|
I2C_Send7bitAddress(I2C, dev_addr & 0xff, I2C_Direction_Transmitter);
|
|
I2C_wait(I2C);
|
|
if(I2C->CR_SR & 0x80)
|
|
{
|
|
printf("I2C send 7 bit address failed\n");
|
|
goto exit_point;
|
|
}
|
|
|
|
I2C_SendData(I2C, reg_addr);
|
|
I2C_wait(I2C);
|
|
if(I2C->CR_SR & 0x80)
|
|
{
|
|
printf("I2C send reg address failed\n");
|
|
goto exit_point;
|
|
}
|
|
|
|
I2C_SendData(I2C, w_data);
|
|
I2C_wait(I2C);
|
|
if(I2C->CR_SR & 0x80)
|
|
{
|
|
printf("I2C send data failed\n");
|
|
goto exit_point;
|
|
}
|
|
|
|
I2C_GenerateSTOP(I2C,ENABLE);
|
|
I2C_wait(I2C);
|
|
|
|
exit_point:
|
|
//recvEnableInt(v);
|
|
return;
|
|
}
|
|
|
|
void myI2C_write_mult(uint8_t dev_addr, uint8_t reg_addr, uint8_t *w_data, uint8_t num)
|
|
{
|
|
int i=0;
|
|
|
|
I2C_GenerateSTART(I2C,ENABLE);
|
|
I2C_wait(I2C);
|
|
|
|
I2C_Send7bitAddress(I2C, dev_addr & 0xff, I2C_Direction_Transmitter);
|
|
I2C_wait(I2C);
|
|
if(I2C->CR_SR & 0x80)
|
|
{
|
|
printf("I2C send 7 bit address failed\n");
|
|
goto exit_point;
|
|
}
|
|
|
|
I2C_SendData(I2C, reg_addr);
|
|
I2C_wait(I2C);
|
|
if(I2C->CR_SR & 0x80)
|
|
{
|
|
printf("I2C send reg address failed\n");
|
|
goto exit_point;
|
|
}
|
|
|
|
for(i=0; i<num; ++i)
|
|
{
|
|
I2C_SendData(I2C, w_data[i]);
|
|
I2C_wait(I2C);
|
|
if(I2C->CR_SR & 0x80)
|
|
{
|
|
printf("I2C send data failed\n");
|
|
goto exit_point;
|
|
}
|
|
}
|
|
|
|
I2C_GenerateSTOP(I2C,ENABLE);
|
|
I2C_wait(I2C);
|
|
|
|
exit_point:
|
|
//recvEnableInt(v);
|
|
return;
|
|
}
|
|
|
|
unsigned char myI2C_read_signal(unsigned char dev_addr, unsigned char reg_addr)
|
|
{
|
|
//unsigned int v = SaveDisableInt();
|
|
unsigned char ret = 0xff;
|
|
|
|
I2C_GenerateSTART(I2C,ENABLE);
|
|
I2C_wait(I2C);
|
|
|
|
I2C_Send7bitAddress(I2C, dev_addr & 0xff, I2C_Direction_Transmitter);
|
|
I2C_wait(I2C);
|
|
if(I2C->CR_SR & 0x80)
|
|
{
|
|
printf("I2C send 7 bit address failed\n");
|
|
goto exit_point;
|
|
}
|
|
|
|
I2C_SendData(I2C,reg_addr);
|
|
I2C_wait(I2C);
|
|
if(I2C->CR_SR & 0x80)
|
|
{
|
|
printf("I2C send reg address failed\n");
|
|
goto exit_point;
|
|
}
|
|
|
|
I2C_Send7bitAddress(I2C, dev_addr & 0xff, I2C_Direction_Receiver);
|
|
I2C_wait(I2C);
|
|
if(I2C->CR_SR & 0x80)
|
|
{
|
|
printf("I2C send 7 bit address failed\n");
|
|
goto exit_point;
|
|
}
|
|
|
|
I2C_ReceiveData(I2C,I2C_NACK,I2C_STOP);
|
|
I2C_wait(I2C);
|
|
ret = (uint8_t)I2C->DR;
|
|
|
|
exit_point:
|
|
//recvEnableInt(v);
|
|
return ret;
|
|
}
|
|
|
|
unsigned char myI2C_read_mult(uint8_t dev_addr, uint8_t reg_addr, uint8_t *rece_data)
|
|
{
|
|
unsigned char i = 0;
|
|
unsigned char ret = 0xFF;
|
|
|
|
I2C_GenerateSTART(I2C,ENABLE);
|
|
I2C_wait(I2C);
|
|
|
|
I2C_Send7bitAddress(I2C, dev_addr & 0xff, I2C_Direction_Transmitter);
|
|
I2C_wait(I2C);
|
|
if(I2C->CR_SR & 0x80)
|
|
{
|
|
printf("I2C send 7 bit address failed\n");
|
|
goto exit_point;
|
|
}
|
|
|
|
I2C_SendData(I2C,reg_addr);
|
|
I2C_wait(I2C);
|
|
if(I2C->CR_SR & 0x80)
|
|
{
|
|
printf("I2C send reg address failed\n");
|
|
goto exit_point;
|
|
}
|
|
|
|
I2C_Send7bitAddress(I2C, dev_addr&0xFF, I2C_Direction_Receiver);
|
|
I2C_wait(I2C);
|
|
if(I2C->CR_SR & 0x80)
|
|
{
|
|
printf("I2C send 7 bit address failed\n");
|
|
goto exit_point;
|
|
}
|
|
|
|
unsigned char chk = 0;
|
|
while (!chk)
|
|
{
|
|
if(I2C->CR_SR && 0x80){
|
|
I2C_ReceiveData(I2C, I2C_NACK, I2C_STOP);
|
|
I2C_wait(I2C);
|
|
ret = (uint8_t)I2C->DR;
|
|
chk = 1;
|
|
}
|
|
else{
|
|
I2C_ReceiveData(I2C,I2C_ACK,I2C_NSTOP);
|
|
I2C_wait(I2C);
|
|
ret = (uint8_t)I2C->DR;
|
|
chk = 0;
|
|
}
|
|
rece_data[i++] = ret;
|
|
}
|
|
rece_data[i] = '\0';
|
|
|
|
exit_point:
|
|
//recvEnableInt(v);
|
|
return i;
|
|
} |