초보의 아웅다웅 설계하기/W7500P

W7500P Soft I2C Setting

로망와니 2019. 4. 16. 11:26

W7500P - Soft I2C Setting

 

 

/* Defines -------------------------------------------------------------------*/
#define I2C_Delay() {delay_us(5);}

/*******************************************************************************
* Function Name : static void I2C_Config(u32 speed)
* Description   : Initialize the i2c port
* Parameters    : None
* Return        : None
*******************************************************************************/
void I2C_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;

/* Enable the GPIO_LED clock */
GPIO_System_ClockEnable(I2C_PORT);

  PAD_AFConfig(I2C_PAD, I2C_SCL, PAD_AF1); 
  PAD_AFConfig(I2C_PAD, I2C_SDA, PAD_AF1); 

/* Configure Led pin as output */
GPIO_InitStruct.GPIO_Pin = I2C_SCL | I2C_SDA;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(I2C_PORT, &GPIO_InitStruct);
}

/*******************************************************************************
* Function Name : 
* Parameters    : 
* Return        : none
* Description   : 
*******************************************************************************/
void I2C_WRITE(unsigned char dat)
{
uint8_t bit_cnt, tmp;
uint8_t bit_value;

for(bit_cnt=0; bit_cnt<8; bit_cnt++)
{
tmp = (dat << bit_cnt) & 0x80;
bit_value = (tmp >> 7) & 0x01;

if(bit_value == 1)
GPIO_SetBits(I2C_PORT,I2C_SDA);
else
GPIO_ResetBits(I2C_PORT,I2C_SDA);

I2C_Delay();
GPIO_SetBits(I2C_PORT,I2C_SCL);
I2C_Delay();
GPIO_ResetBits(I2C_PORT,I2C_SCL);
I2C_Delay();
}
}

/*******************************************************************************
* Function Name : 
* Parameters    : 
* Return        : none
* Description   : 
*******************************************************************************/
void I2C_START(void)
{
GPIO_SetBits(I2C_PORT,I2C_SCL);
I2C_Delay();
GPIO_SetBits(I2C_PORT,I2C_SDA);
I2C_Delay();
GPIO_ResetBits(I2C_PORT,I2C_SDA);
I2C_Delay();
GPIO_ResetBits(I2C_PORT,I2C_SCL);
I2C_Delay();
}

/*******************************************************************************
* Function Name : 
* Parameters    : 
* Return        : none
* Description   : 
*******************************************************************************/
void I2C_STOP(void)
{
GPIO_ResetBits(I2C_PORT,I2C_SDA);
I2C_Delay();
GPIO_SetBits(I2C_PORT,I2C_SCL);
I2C_Delay();
GPIO_SetBits(I2C_PORT,I2C_SDA);
I2C_Delay();
}

/*******************************************************************************
* Function Name : 
* Parameters    : 
* Return        : none
* Description   : 
*******************************************************************************/
void I2C_SEND_ACK(uint8_t bit_value)
{
if(bit_value == 1)
GPIO_SetBits(I2C_PORT,I2C_SDA);
else
GPIO_ResetBits(I2C_PORT,I2C_SDA);

I2C_Delay();
GPIO_SetBits(I2C_PORT,I2C_SCL);
I2C_Delay();
GPIO_ResetBits(I2C_PORT,I2C_SCL);
I2C_Delay();
GPIO_SetBits(I2C_PORT,I2C_SDA);
I2C_Delay();
}

/*******************************************************************************
* Function Name : 
* Parameters    : 
* Return        : none
* Description   : 
*******************************************************************************/
uint8_t I2C_READ(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;
uint8_t i_byte, n;

GPIO_SetBits(I2C_PORT,I2C_SDA);
I2C_Delay();


/* Configure I2C1 pins: SDA */
//  PAD_AFConfig(I2C_PAD, I2C_SDA, PAD_AF1); 
GPIO_InitStruct.GPIO_Pin = I2C_SDA;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_Init(I2C_PORT, &GPIO_InitStruct);
I2C_Delay();

for(n=0; n<8; n++)
{
GPIO_SetBits(I2C_PORT,I2C_SCL);
I2C_Delay();

if (GPIO_ReadInputDataBit(I2C_PORT,I2C_SDA)){
i_byte = (i_byte << 1) | 0x01; // msbit first
}
else{
i_byte = i_byte << 1;
}
GPIO_ResetBits(I2C_PORT,I2C_SCL);
I2C_Delay();
}

/* Configure I2C1 pins: SDA */
//  PAD_AFConfig(I2C_PAD, I2C_SDA, PAD_AF1); 
GPIO_InitStruct.GPIO_Pin = I2C_SDA;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(I2C_PORT, &GPIO_InitStruct);
I2C_Delay();

return(i_byte);
}

/*******************************************************************************
* Function Name : 
* Parameters    : 
* Return        : none
* Description   : 
*******************************************************************************/
uint8_t I2C_WAIT_ACK(void)        //Acknowledge
{
GPIO_InitTypeDef  GPIO_InitStruct;

uint8_t ack_bit_value = 0;
uint32_t timeout = 6000 * 10; // 10ms

GPIO_SetBits(I2C_PORT,I2C_SDA);
GPIO_SetBits(I2C_PORT,I2C_SCL);

/* Configure I2C1 pins: SDA */
//  PAD_AFConfig(I2C_PAD, I2C_SDA, PAD_AF1); 
GPIO_InitStruct.GPIO_Pin = I2C_SDA;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_Init(I2C_PORT, &GPIO_InitStruct);
I2C_Delay();

while(GPIO_ReadInputDataBit(I2C_PORT,I2C_SDA) && timeout--);
if(timeout==0) ack_bit_value = 1;

/* Configure I2C1 pins: SDA */
//  PAD_AFConfig(I2C_PAD, I2C_SDA, PAD_AF1); 
GPIO_InitStruct.GPIO_Pin = I2C_SDA;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(I2C_PORT, &GPIO_InitStruct);
I2C_Delay();

GPIO_ResetBits(I2C_PORT, I2C_SCL);

return ack_bit_value;
}

 

'초보의 아웅다웅 설계하기 > W7500P' 카테고리의 다른 글

W7500P - BOR  (0) 2019.04.17
W7500P - 리셋타이밍  (0) 2019.04.16
W7500P UART Setting  (0) 2019.04.16
W7500P - SWD와 PA3, PA4  (0) 2019.04.12
Wiznet W7500P Keil에서 J-link(or ST-Link) 사용하여 디버깅  (0) 2018.12.18