Hi
Im doing a little program using the USIC module as I2C. For this mi using the XMC1200 boot kit.
I need to do a driver to control a I2C serial EEPROM (24LC16B).
I did two functions, one for write and another to read the momery. In link is the component datasheet.
http://www.microchip.com/wwwproducts/en/24LC16B
I think I configure properly but it doesn't run. Can someone help?? I cant see the I2C signals with the oscilloscope.
This is my code:
#include "xmc_scu.h"
#include "xmc_gpio.h"
#include "xmc_i2c.h"
#define EEPROM_I2C_ADR 0xA0
#define SDA_PIN P0_7
#define SCL_PIN P0_8
void EEPROM_SendByte (void);
void EEPROM_ReadByte (void);
uint8_t DataToSend = 0x55;
uint8_t ReceivedData;
uint8_t MemoryAddress = 0x00;
XMC_SCU_CLOCK_CONFIG_t clock_config;
XMC_GPIO_CONFIG_t led1_config;
XMC_GPIO_CONFIG_t sda_pin_config;
XMC_GPIO_CONFIG_t scl_pin_config;
/* I2C configuration */
XMC_USIC_CH_t *i2c = XMC_I2C0_CH1;
XMC_I2C_CH_CONFIG_t i2c_cfg;
void EEPROM_SendByte (void){
XMC_I2C_CH_MasterStart(i2c, EEPROM_I2C_ADR, XMC_I2C_CH_CMD_WRITE); // I2C send repeated start command (with slave address) to write Data to PCA9502
while((XMC_I2C_CH_GetStatusFlag(i2c) & XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED) == 0U){} // wait until ACK
XMC_I2C_CH_ClearStatusFlag(i2c, XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED); // clear ACK flag in register PSR
XMC_I2C_CH_MasterTransmit(i2c, MemoryAddress); // I2C send TxData=IO_STATE
while((XMC_I2C_CH_GetStatusFlag(i2c) & XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED) == 0U){} // wait until ACK
XMC_I2C_CH_ClearStatusFlag(i2c, XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED);
XMC_I2C_CH_MasterTransmit(i2c, DataToSend); // I2C send TxData=IO_STATE
while((XMC_I2C_CH_GetStatusFlag(i2c) & XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED) == 0U){} // wait until ACK
XMC_I2C_CH_ClearStatusFlag(i2c, XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED);
XMC_I2C_CH_MasterStop(i2c);
}
void EEPROM_ReadByte (void){
//Random read operations allow the master to access any memory location in a random manner
XMC_I2C_CH_MasterStart(i2c, EEPROM_I2C_ADR, XMC_I2C_CH_CMD_WRITE); // I2C send repeated start command (with slave address) to write Data to PCA9502
while((XMC_I2C_CH_GetStatusFlag(i2c) & XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED) == 0U){} // wait until ACK
XMC_I2C_CH_ClearStatusFlag(i2c, XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED); // clear ACK flag in register PSR
XMC_I2C_CH_MasterTransmit(i2c, MemoryAddress); // I2C send TxData=IO_STATE
while((XMC_I2C_CH_GetStatusFlag(i2c) & XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED) == 0U){} // wait until ACK
XMC_I2C_CH_ClearStatusFlag(i2c, XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED); // clear ACK flag in register PSR
XMC_I2C_CH_MasterStop(i2c);
XMC_I2C_CH_MasterStart(i2c, EEPROM_I2C_ADR, XMC_I2C_CH_CMD_READ); // I2C send repeated start command (with slave address) to Read Data from PCA9502
while((XMC_I2C_CH_GetStatusFlag(i2c) & XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED) == 0U){} // wait until ACK
XMC_I2C_CH_ClearStatusFlag(i2c, XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED); // clear ACK flag in register PSR
XMC_I2C_CH_MasterReceiveNack(i2c); // I2C send dummy with NACK to read only one byte
while((XMC_I2C_CH_GetStatusFlag(i2c) & (XMC_I2C_CH_STATUS_FLAG_RECEIVE_INDICATION |
XMC_I2C_CH_STATUS_FLAG_ALTERNATIVE_RECEIVE_INDICAT ION)) == 0U){} // wait until AIR or RI ('USIC_AI.007')
XMC_I2C_CH_ClearStatusFlag(i2c, XMC_I2C_CH_STATUS_FLAG_RECEIVE_INDICATION |
XMC_I2C_CH_STATUS_FLAG_ALTERNATIVE_RECEIVE_INDICAT ION); // clear both AIR and RI
ReceivedData = XMC_I2C_CH_GetReceivedData(i2c); // get RxData from RBUF
XMC_I2C_CH_MasterStop(i2c);
}
int main(void)
{
/* Configure Clock */
clock_config.pclk_src = XMC_SCU_CLOCK_PCLKSRC_MCLK; /*PCLK = MCLK*/
clock_config.fdiv = 0; /**< Fractional divider */
clock_config.idiv = 0; /**MCLK = 32MHz */
XMC_SCU_CLOCK_Init(&clock_config);
/* Configure LED1 */
led1_config.mode = XMC_GPIO_MODE_OUTPUT_PUSH_PULL;
XMC_GPIO_Init(XMC_GPIO_PORT0,6, &led1_config);
/* I2C configuration */
i2c_cfg.baudrate = 400000U;
XMC_I2C_CH_Init(i2c, &i2c_cfg);
XMC_I2C_CH_Start(i2c);
/* I2C initialization sequence*/
XMC_I2C_CH_SetInputSource(i2c, XMC_I2C_CH_INPUT_SDA , USIC0_C0_DX1_P0_7);
XMC_I2C_CH_SetInputSource(i2c, XMC_I2C_CH_INPUT_SCL , USIC0_C0_DX1_P0_8);
/* I2C port pin configuration*/
sda_pin_config.mode = XMC_GPIO_MODE_OUTPUT_OPEN_DRAIN_ALT7;
XMC_GPIO_Init(SDA_PIN, &sda_pin_config);
scl_pin_config.mode = XMC_GPIO_MODE_OUTPUT_OPEN_DRAIN_ALT7;
XMC_GPIO_Init(SCL_PIN, &scl_pin_config);
while(1U){
ReceivedData = 0;
EEPROM_SendByte();
EEPROM_ReadByte();
if (DataToSend==ReceivedData){
XMC_GPIO_ToggleOutput(XMC_GPIO_PORT0,6);
}
}
}
Regards
Paul