cancel
Showing results for 
Search instead for 
Did you mean: 

I2C not working

aleena
Associate
Posted on December 05, 2015 at 10:29

Hi,

 I'm using STM8S003F3 controller to monitor a temperature sensor using I2C Interface. I'm able to monitor while in debug and Run mode (in STVD IDE) using SWIM, but once I program into flash (using STVP through SWIM) im not getting clock and data in I2C lines. Is there any setting that has to be taken care of while programming through STVP. Any help would be appreciated.

Thanks.

Attached is the I2C code. Is there any more configuration that has to be done ?

 void init(void)

{

i2c_deinit();

// Initialize I2C interface 

i2c_init();

}

void i2c_deinit(void)

{

/* Disable I2C peripheral */

I2C_Cmd(DISABLE);

/* Deinit I2C module */

I2C_DeInit();

/* Disable I2C module clock */

CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, DISABLE);

/* Pins configuration */

/* I2C SCL as GPIO */

GPIO_Init(I2C_SCL_GPIO_PORT, I2C_SCL_GPIO_PIN,  GPIO_MODE_OUT_PP_LOW_FAST);

/* I2C SDA as GPIO */

GPIO_Init(I2C_SDA_GPIO_PORT, I2C_SDA_GPIO_PIN,  GPIO_MODE_OUT_PP_LOW_FAST);

}

void i2c_init(void)

{

__IO uint8_t value;

uint16_t i;

/* Enable I2C module clock */

CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, ENABLE);

I2C_Init(I2C_OUT_FREQ, I2C_ADDR, 

              I2C_DUTYCYCLE_2, I2C_ACK_CURR, 

              I2C_ADDMODE_7BIT, I2C_INPUT_FREQ);

/* Enable I2C peripheral */

I2C_Cmd(ENABLE);

 

}

int8_t i2c_write(uint8_t command, uint8_t value)

{

uint16_t timeout = 0xFFFF/*0xA000*/; /* TODO: Verify the timeout period */

while (I2C_GetFlagStatus(I2C_FLAG_BUSBUSY) == SET)

{

timeout--;

if (timeout == 0)

return -1;

}

// Send START condition

I2C_GenerateSTART(ENABLE);

// EV 5

// Wait for START condition to be released correctly on the I2C bus (the bus is free, no other devices is communicating)

while(I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT) == FALSE)

{

if (!(timeout--))

return -2;

}

// Send the Slave address for write operation

I2C_Send7bitAddress(I2C_ADDR, I2C_DIRECTION_TX);

// EV 6

while(I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == FALSE)

{

if (!(timeout--))

return -3;

}

// Send the command

I2C_SendData(command);

// EV 8

while(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING) == FALSE)

{

if (!(timeout--))

return -4;

}

// Send the data

I2C_SendData(value);

// EV 8_2

while(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED) == FALSE)

{

if (!(timeout--))

return -5;

}

// Send STOP condition

I2C_GenerateSTOP(ENABLE);

return 0;

}

int8_t i2c_read(uint8_t command, uint8_t *value)

{

uint8_t timeout = 0xFFFF;

while (I2C_GetFlagStatus(I2C_FLAG_BUSBUSY) == SET)

{

if (!(timeout--))

return -1;

}

// Send START condition

I2C_GenerateSTART(ENABLE);

// EV 5

// Wait for START condition to be released correctly on the I2C bus (the bus is free, no other devices is communicating)

while(I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT) == FALSE)

{

if (!(timeout--))

return -2;

}

// Send the Slave address for write operation

I2C_Send7bitAddress(I2C_ADDR, I2C_DIRECTION_TX);

// EV 6

while(I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == FALSE)

{

if (!(timeout--))

return -3;

}

// Send the command

I2C_SendData(command);

while(I2C_GetFlagStatus(I2C_FLAG_TRANSFERFINISHED) == RESET)

{

if (!(timeout--))

return -4;

}

/* TODO: Enable if Stop is required

// Send STOP condition

I2C_GenerateSTOP(ENABLE);

*/

// Send START condition for second time

I2C_GenerateSTART(ENABLE);

// EV 5

// Wait for START condition to be released correctly on the I2C bus (the bus is free, no other devices is communicating)

while(I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT) == FALSE)

{

if (!(timeout--))

return -5;

}

// Send the Slave address for read operation

I2C_Send7bitAddress(I2C_ADDR, I2C_DIRECTION_RX);

// EV 6

while(I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == FALSE)

{

if (!(timeout--))

return -6;

}

// EV7

while(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED) == FALSE)

{

if (!(timeout--))

return -7;

}

// Read a byte from Slave

*value = I2C_ReceiveData();

// Disable Acknowledgement

I2C_AcknowledgeConfig(I2C_ACK_NONE);

// Send STOP condition

I2C_GenerateSTOP(ENABLE);

return 0;

}

0 REPLIES 0