Announcements

Help us improve the Power & Sensing Selection Guide. Share feedback

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Bond_007
Level 3
Level 3
10 questions asked 25 sign-ins 10 replies posted

Hi Team,

I'm Working on I2C Driver I want to use X104 USIC (P1.4 and P1.5) pins. But in datasheet mentioned they belongs to DX0 and DX1 stages. In DAVE V4.5 not defined P1.4 & P1.5 to DX0 and DX1. Below is my driver implementation please help me solve this problem.

 

#include <stdint.h>
#include "xmc_common.h"
#include "bldc_scalar_user_interface.h"
#include "../Power_Tools_Kit_DirectFET/power_tools_kit_DirectFET.h"
#include "xmc_gpio.h"
#include "xmc_uart.h"
#include "xmc_i2c.h"

//#include "bldc_scalar_conrol_hall.h"

#define EEPROM_I2C_ADR 0x0C

#define SDA_PIN P1_4
#define SCL_PIN P1_5
#define USIC0_C1_DX1_P1_4   	4
#define USIC0_C0_DX0_P1_5   	5


void Init_I2C(void);
void EEPROM_SendByte(uint8_t MemoryAddress, uint8_t DataToSend);
uint8_t EEPROM_ReadByte(uint8_t MemoryAddress);

uint8_t ReceivedData;

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 Init_I2C(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, 4, &led1_config);

	/* I2C configuration */
	i2c_cfg.baudrate = 100000U;

	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_DX0_P1_5); //DX0 -> DOUT0
	XMC_I2C_CH_SetInputSource(i2c, XMC_I2C_CH_INPUT_SCL, USIC0_C1_DX1_P1_4); //DX1 -> SCLKOUT

	/* I2C port pin configuration*/
	sda_pin_config.mode = XMC_GPIO_MODE_OUTPUT_OPEN_DRAIN_ALT2;
	sda_pin_config.output_level = XMC_GPIO_OUTPUT_LEVEL_HIGH;
	XMC_GPIO_Init(SDA_PIN, &sda_pin_config);
	scl_pin_config.mode = XMC_GPIO_MODE_OUTPUT_OPEN_DRAIN_ALT2;
	scl_pin_config.output_level = XMC_GPIO_OUTPUT_LEVEL_HIGH;
	XMC_GPIO_Init(SCL_PIN, &scl_pin_config);

}

void EEPROM_SendByte(uint8_t MemoryAddress, uint8_t DataToSend) {
	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)
			== 1U) {
	} // 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
	while ((XMC_I2C_CH_GetStatusFlag(i2c) & XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED)
			== 1U) {
	} // 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)
			== 1U) {
	} // wait until ACK
	XMC_I2C_CH_ClearStatusFlag(i2c, XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED);

	XMC_I2C_CH_MasterStop(i2c);
}

uint8_t EEPROM_ReadByte(uint8_t MemoryAddress) {
	uint8_t data = 0;

	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)
			== 1U) {
	} // 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
	while ((XMC_I2C_CH_GetStatusFlag(i2c) & XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED)
			== 1U) {
	} // 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)
			== 1U) {
	} // 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_INDICATION))
			== 1U) {
	} // wait until AIR or RI
	XMC_I2C_CH_ClearStatusFlag(i2c,
			XMC_I2C_CH_STATUS_FLAG_RECEIVE_INDICATION
					| XMC_I2C_CH_STATUS_FLAG_ALTERNATIVE_RECEIVE_INDICATION); // clear both AIR and RI

	data = XMC_I2C_CH_GetReceivedData(i2c); // get RxData from RBUF

	XMC_I2C_CH_MasterStop(i2c);

	return data;
}

int main(void) {
	Init_I2C();

	while (1U) {
		ReceivedData = 0;
		EEPROM_SendByte(0x01, 0x02);
		for (volatile uint8_t gu8Index = 0; gu8Index < 10; gu8Index++);
		ReceivedData = EEPROM_ReadByte(0x55);
		if (ReceivedData == 0x20) {
			XMC_GPIO_ToggleOutput(XMC_GPIO_PORT0, 4);
		}
	}
}

 

Is there any wrong in the above code.? any help would be very thank full. 

0 Likes
5 Replies
ncbs
Moderator
Moderator
Moderator
500 replies posted 50 likes received 250 sign-ins

Hi @Bond_007,

Can you share a minimal project here so that we can take a closer look at the issue?

Regards,
Nikhil

0 Likes
lock attach
Attachments are accessible only for community members.
Bond_007
Level 3
Level 3
10 questions asked 25 sign-ins 10 replies posted

Hi @Nikhil,

Thanks for the reply,

1) I have changed I2C Pins mode to Internal Pull Up then I observed SDA and SCL are High at 5V level.
    But Clock is not generating & No data transmitting. What might be the issue ?

2) Another doubt is my XMC1302 Pin levels are at 5V range. How can I interface to 3.3V level EEPROM. Is 5V Pin Levels works with EEPROM 3.3V level. If Yes, How can it be possible ? else please suggest alternatives.

Please find the I2C Project which I'm working on. Also I need to use only USIC0 P1.4 and P1.5(these are in different channels) for I2C interface.

Please help me to resolve this problem.

0 Likes

Hi @ncbs.,

Can you please suggest I2C driver for XMC1302 - USIC0_C0_DX0_P1_5, USIC0_C1_DX1_P1_4.
Also Clock configuration. We are using 24C04 EEPROM (STM) using I2C Interface.

where the issue.? Any help would be very  thankful.

0 Likes
Alakananda_BG
Moderator
Moderator
Moderator
50 likes received 250 sign-ins 250 replies posted

Hi @Vamshi_b ,

You should the pins of the same Channel because of which you might be facing issues.

Please select both the pins from either Channel0 or Channel1.

Regards,

Alakananda
0 Likes

Hi @Alakananda_BG ,

Thanks for the reply,

I Tested with the above suggestion where both pins belonged to same channel but the SDA and SCL remains high at 5V level. Start condition is not generating.

Used pins are  below. what else might be the issue ? any help would be very thankful.

USIC0_C1_DX0_P0_7
 USIC0_C1_DX1_P0_8
0 Likes