IO over I2C


Control IO over I2C.

Toolkit:STM Development System

Location:/bipom/devtools/STM32/examples/i2c_io

Code Example


int main(void) 
{
	delayMs(250);

	tprintf("\n\rMini-Max/STM32F1");
	tprintf("\n\rI2C IO TEST REV 1.01");

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	SoftI2cDelay(DELAY);
	Soft_SDA_OUTPUT();
	SoftI2cDelay(DELAY);
	Soft_SCL_OUTPUT();
	SoftI2cDelay(DELAY);

	Soft_Transmit(0x42);
	Soft_Transmit(0x06); 

	for(;;);
	return 0;
}

//********************************************************************************

void Soft_SDA_INPUT	(void)
{
	GPIO_InitTypeDef	GPIO_InitStructure;
	//

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  	GPIO_Init(GPIOB, &GPIO_InitStructure);
}

//********************************************************************************

void Soft_SCL_INPUT	(void)
{
	GPIO_InitTypeDef	GPIO_InitStructure;
	//

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  	GPIO_Init(GPIOB, &GPIO_InitStructure);
}

//********************************************************************************

void Soft_SDA_OUTPUT(void)
{
	GPIO_InitTypeDef	GPIO_InitStructure;
	//

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_ResetBits(GPIOB,GPIO_Pin_7);
}

//********************************************************************************

void Soft_SCL_OUTPUT(void)
{
	GPIO_InitTypeDef	GPIO_InitStructure;
	//

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_ResetBits(GPIOB, GPIO_Pin_6);
}

//********************************************************************************

uint8_t Soft_SDA_PORT_IN(void)
{
	return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7);
}

//********************************************************************************

uint8_t Soft_SCL_PORT_IN(void)
{
	return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6);
}

//********************************************************************************

void SoftI2cDelay (volatile unsigned long delay)
{
	while (delay--);
}

//********************************************************************************

void Soft_Transmit(unsigned char dataByte)
{
	int ndx=0,indx=0;
	//

	for (ndx =0; ndx < 8; ndx++)
	{
		if(dataByte & 0x80)	Soft_SDA_INPUT();			// SDA = 1

		else				Soft_SDA_OUTPUT();		// SDA = 0

		SoftI2cDelay(DELAY);
		//

		Soft_SCL_INPUT();								// SCL = 1

		SoftI2cDelay(DELAY);
		//

		if(!ndx)
		{
			for (indx =0; indx < 64; indx++)
			{
				if(Soft_SCL_PORT_IN()) break;
				SoftI2cDelay(DELAY);
			}
		}
		
		
		//

		Soft_SCL_OUTPUT();								// SCL = 0

		SoftI2cDelay(DELAY);
		//

		dataByte <<=1;
	}
	Soft_SDA_INPUT();
	SoftI2cDelay(DELAY);
	//

	Soft_SCL_INPUT();						// SCL = 1

	SoftI2cDelay(DELAY);
	//

	Soft_SDA_PORT_IN();		
	//

	Soft_SCL_OUTPUT();						// SCL = 0

	SoftI2cDelay(DELAY);
	//


}