This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
hi ,
i work with tiva c TM4C129X development board and i am trynig to pass data with I2C protocol.
i want to use I2C0 in the microcontroller as Master and with I2C1 as slave.
i connected I2C0 SDA and SCL to I2C1 in the borad. meaning: port PB3 to PG1 and port PG0 to PG2.
i wrote the attached program and the program is stuck in the loop in " while(I2CMasterBusy(I2C0_BASE)) "
can you please help me with what am i doing wrong.
best regards,
Eliran.
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/i2c.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_nvic.h"
#include "inc/hw_types.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "driverlib/pin_map.h"
// PROTOTYPE FUNCTIONS
void I2C0Handler(void);
void I2C1Handler(void);
// main function
int main()
{
volatile uint32_t SysClk=0;
volatile uint32_t RECEVIED_DATA=0;
volatile char ch;
// set clock
SysClk=SysCtlClockFreqSet(SYSCTL_OSC_INT|SYSCTL_USE_PLL|SYSCTL_CFG_VCO_320,16000000);
// Enable i2c peripheral
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); // clk PB2 , data PB3
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C0));
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1); // clk PG0 , data PG1
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C1));
//set i2c clock
I2CMasterInitExpClk(I2C0_BASE,SysCtlClockGet(),true);
// specify slave address to send from the master
I2CMasterSlaveAddrSet(I2C0_BASE,0x3B,false);
I2CSlaveInit(I2C1_BASE,0x3B);
I2CMasterDataPut(I2C0_BASE,'q');
I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_SINGLE_SEND);
// Enable processor interrupts.
// IntMasterEnable();
while(I2CMasterBusy(I2C0_BASE))
{
}
RECEVIED_DATA=I2CSlaveDataGet(I2C1_BASE);
ch=(char)RECEVIED_DATA;
return 0;
}
void I2C0Handler(void)
{
}
void I2C1Handler(void)
{
}
Did you connect the pull up resistors? See this for information on properly configuring an I2C bus:
http://www.ti.com/lit/an/slva704/slva704.pdf
Here is an example that connects I2C0 to I2C2:
/cfs-file/__key/communityserver-discussions-components-files/908/I2C0MtoI2C2S.zip
hi BOB,
thank you very much for the response.
i rewrite my code and unfortunatly its still dosen't work.
i activated the pullup resistors in the SDA line
the problem is still in :
while(I2CMasterBusy(I2C0_BASE))
it looks like the master is always busy.
any ideas?
my new code is:
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/i2c.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_nvic.h"
#include "inc/hw_types.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "driverlib/pin_map.h"
//#include "inc/tm4c129xnczad.h"
// PROTOTYPE FUNCTIONS
void I2C0Handler(void);
void I2C1Handler(void);
// main function
int main()
{
volatile uint32_t SysClk=0;
volatile uint32_t RECEVIED_DATA=0;
volatile char ch;
// set clock
SysClk=SysCtlClockFreqSet(SYSCTL_OSC_INT|SYSCTL_USE_PLL|SYSCTL_CFG_VCO_320,16000000);
// Enable i2c peripheral
SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C0);// clk PB2 , data PB3
SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0); // clk PG0 , data PG1
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C0));
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C1));
// ENABLE GPIO PORTS OF THE I2C
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
// configure pin muxing for i2c0
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
// configure pin muxing for i2c1
GPIOPinConfigure(GPIO_PG0_I2C1SCL);
GPIOPinConfigure(GPIO_PG1_I2C1SDA);
//select i2c function for this pins.
//also configure the pins for i2c
//setting them to open drain
//
//
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE,GPIO_PIN_3);
GPIOPinTypeI2C(GPIO_PORTG_BASE, GPIO_PIN_1);
GPIOPinTypeI2CSCL(GPIO_PORTG_BASE,GPIO_PIN_0);
GPIOPadConfigSet(GPIO_PORTB_BASE,GPIO_PIN_2,GPIO_STRENGTH_12MA,GPIO_PIN_TYPE_STD);
GPIOPadConfigSet(GPIO_PORTB_BASE,GPIO_PIN_3,GPIO_STRENGTH_12MA,GPIO_PIN_TYPE_OD);
GPIOPadConfigSet(GPIO_PORTG_BASE,GPIO_PIN_0,GPIO_STRENGTH_12MA,GPIO_PIN_TYPE_STD);
GPIOPadConfigSet(GPIO_PORTG_BASE,GPIO_PIN_1,GPIO_STRENGTH_12MA,GPIO_PIN_TYPE_OD);
//set i2c clock
I2CMasterInitExpClk(I2C0_BASE,SysCtlClockGet(),false);
// specify slave address to send from the master
I2CMasterSlaveAddrSet(I2C0_BASE,0x3B,false);
I2CSlaveEnable(I2C1_BASE);
I2CSlaveInit(I2C1_BASE,0x3B);
I2CMasterDataPut(I2C0_BASE,'q');
I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_SINGLE_SEND);
while(I2CMasterBusy(I2C0_BASE))
{
}
RECEVIED_DATA=I2CSlaveDataGet(I2C1_BASE);
ch=(char)RECEVIED_DATA;
return 0;
}