Hi,
I am a student , I was trying to communicate between two tiva board using I2C.I have two doubt, one is related to i2c communication with interrupt and other one is back and forth communication using i2c.
Doubt 1.
I was doing the I2C communication with interrupt and it is working.
The code given below.
#include "stdio.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "hw_memmap.h"
#include "inc/hw_ints.h"
#include "driverlib/pin_map.h"
#include "inc/hw_types.h"
#include "driverlib/interrupt.h"
#include "driverlib/systick.h"
#include "driverlib/timer.h"
#include "inc/hw_nvic.h"
#include "driverlib/timer.c"
#include "driverlib/systick.c"
#include "driverlib/adc.h"
#include "driverlib/debug.h"
#include "driverlib/uart.h"
#include "driverlib/rom.h"
#include "inc/hw_ints.h"
#include "driverlib/ssi.h"
#include "driverlib/i2c.h"
#include "inc/hw_i2c.h"
void delay(int a);
#define SLAVE_ADDRESS 0x20
int main()
{
SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_OSC |SYSCTL_XTAL_8MHZ|SYSCTL_OSC_MAIN);
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C0));
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOB));
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
I2CMasterEnable(I2C0_BASE);
I2CMasterInitExpClk(I2C0_BASE,SysCtlClockGet(),false);
IntMasterEnable();
I2CMasterIntEnable(I2C0_BASE);
IntEnable(INT_I2C0);
while(1)
{
I2CMasterSlaveAddrSet(I2C0_BASE,SLAVE_ADDRESS,false);
I2CMasterDataPut(I2C0_BASE,0xBB);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
SysCtlDelay(1000);
}}
void I2C0_Handler(void){
uint32_t ui32Status;
ui32Status = I2CMasterIntStatus(I2C0_BASE, true); //get interrupt status
I2CMasterSlaveAddrSet(I2C0_BASE,SLAVE_ADDRESS,false);
I2CMasterDataPut(I2C0_BASE,0xCC);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
//while(!I2CMasterBusy(I2C0_BASE));
I2CMasterIntClear(I2C0_BASE);//Clear the interrupt
}
I was using interrupt in master code only. my doubt is, if I am not giving the highlighted condition inside the while loop or outside the while loop also the interrupt handler function is executing. If I am not using those commands then i2c interrupt handler function wont execute it will stop in IntEnable(INT_I2C0) this command. Please let me know why it is happening. Slave code given below
Slave code
#include "stdio.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "hw_memmap.h"
#include "inc/hw_ints.h"
#include "driverlib/pin_map.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/uart.h"
#include "inc/hw_ints.h"
#include "driverlib/i2c.h"
void InitConsole(void);
int main()
{
char data, char_rx,receive;
SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_OSC|SYSCTL_XTAL_8MHZ|SYSCTL_OSC_MAIN); //seting system clock as 8Mhz
InitConsole();
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C0));
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOB));
//configure the muxing and GPIO settings to bring the SSI functions out to the pins
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
I2CSlaveAddressSet(I2C0_BASE,0,0x20);
I2CSlaveInit(I2C0_BASE,0x20);
I2CSlaveEnable(I2C0_BASE);
while(1) {
char_rx=I2CSlaveDataGet(I2C0_BASE);
while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_RREQ));
UARTCharPut(UART0_BASE,char_rx );
}}
void InitConsole(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_UART0));
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOA));
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 9600,(UART_CONFIG_WLEN_8|UART_CONFIG_STOP_ONE|UART_CONFIG_PAR_NONE));
}
Doubt 2
I want to send data back and forth between 2 tiva board using i2c, without interrupt I am doing this. I was able to achieve master to slave communication but the aim is send back the same data to master and display through UART. In serial monitor I was getting garbage values.
The code is given below
Master code
#define SLAVE_ADDRESS 0x20
void InitConsole(void);
char data;
int main()
{
SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_OSC |SYSCTL_XTAL_8MHZ|SYSCTL_OSC_MAIN); //seting system clock as 8Mhz
InitConsole();
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C0));
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOB));
//configure the muxing and GPIO settings to bring the SSI functions out to the pins
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
I2CMasterEnable(I2C0_BASE);
I2CMasterInitExpClk(I2C0_BASE,SysCtlClockGet(),false);
{
I2CMasterSlaveAddrSet(I2C0_BASE,SLAVE_ADDRESS,false);
I2CMasterDataPut(I2C0_BASE,0xBB);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
I2CMasterSlaveAddrSet(I2C0_BASE,SLAVE_ADDRESS,true);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
data= I2CMasterDataGet(I2C0_BASE);
while(I2CMasterBusy(I2C0_BASE));
UARTCharPut(UART0_BASE,data);
}}
void InitConsole(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_UART0));
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOA));
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,(UART_CONFIG_WLEN_8|UART_CONFIG_STOP_ONE|UART_CONFIG_PAR_NONE));
}
Slave code
int main()
{
char data, char_rx,receive;
SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_OSC|SYSCTL_XTAL_8MHZ|SYSCTL_OSC_MAIN);
InitConsole();
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C0));
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOB));
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
I2CSlaveAddressSet(I2C0_BASE,0,0x20);
I2CSlaveInit(I2C0_BASE,0x20);
I2CSlaveEnable(I2C0_BASE);
while(1) {
char_rx=I2CSlaveDataGet(I2C0_BASE);
while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_RREQ));
I2CSlaveDataPut(I2C0_BASE,char_rx);
}}
Please let me know what are the Issues and how I can make it as a working code. I am a student and not having that much experience with embedded C and Tiva also. so please help me. I am hoping some one can help me to solve these issues.
Thank you so much,
Mariya