Tool/software: TI C/C++ Compiler
Hi sir,
I am using two tiva board one act as a master and other one act as a slave.I am trying to send some string from master to slave and then slave send the same data back to the master and display the data through UART, but I am unable to send back the data.
When I tried to send data from master to slave, the data reading correctly through UART. But when I am trying to send back the same data to master it is not working properly.Please help me to solve the issue.
Given below is the master code,
#define SLAVE_ADDRESS 0x20 //void InitConsole(void); unsigned long k=0; void InitConsole(void); uint32_t n=3,i; char char_tx[]="abc"; char char_rx[3],data; int main() { 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)); //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); while(1) { I2CMasterSlaveAddrSet(I2C0_BASE,SLAVE_ADDRESS,false); //put data to be sent into FIFO I2CMasterDataPut(I2C0_BASE, char_tx[0]); if(char_tx[1] == '\0') { //Initiate send of data from the MCU I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND); // Wait until MCU is done transferring. while(I2CMasterBusy(I2C0_BASE)); } else{ I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START); // Wait until MCU is done transferring. while(I2CMasterBusy(I2C0_BASE)); I2CMasterSlaveAddrSet(I2C0_BASE,SLAVE_ADDRESS,true); I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_RECEIVE_START); char_rx[0]= I2CMasterDataGet(I2C0_BASE); while(I2CMasterBusy(I2C0_BASE)); UARTCharPut(UART0_BASE,char_rx[0]); while(char_tx[i + 1] != '\0') for(i=1;i<n;i++) { I2CMasterSlaveAddrSet(I2C0_BASE,SLAVE_ADDRESS,false); //put next piece of data into I2C FIFO I2CMasterDataPut(I2C0_BASE, char_tx[i]); // //send next data that was just placed into FIFO I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT); // Wait until MCU is done transferring. while(I2CMasterBusy(I2C0_BASE)); I2CMasterSlaveAddrSet(I2C0_BASE,SLAVE_ADDRESS,true); char_rx[i]= I2CMasterDataGet(I2C0_BASE); I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); while(I2CMasterBusy(I2C0_BASE)); UARTCharPut(UART0_BASE,char_rx[i]); i++; } I2CMasterSlaveAddrSet(I2C0_BASE,SLAVE_ADDRESS,false); //put last piece of data into I2C FIFO I2CMasterDataPut(I2C0_BASE,char_tx[n]); //send next data that was just placed into FIFO I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH); while(I2CMasterBusy(I2C0_BASE)); I2CMasterSlaveAddrSet(I2C0_BASE,SLAVE_ADDRESS,true); char_rx[n]= I2CMasterDataGet(I2C0_BASE); I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); while(I2CMasterBusy(I2C0_BASE)); UARTCharPut(UART0_BASE,char_rx[n]); } } } void InitConsole(void) { // // Enable the UART0 module. // SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); // // Wait for the UART0 module to be ready. // while(!SysCtlPeripheralReady(SYSCTL_PERIPH_UART0)); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOA)); GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); //UARTClockSourceSet(SYSCTL_PERIPH_UART0,UART_CLOCK_SYSTEM); // UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC); 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)); UARTCharPut(UART0_BASE, 'J'); }
Slave code
int main() { char data, char_rx[5],receive; // uint32_t pui32DataTx; uint32_t n=3,i; //uint32_t pui32DataRx[5]; //uint32_t ui32Period; SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_OSC|SYSCTL_XTAL_8MHZ|SYSCTL_OSC_MAIN); //seting system clock as 8Mhz //InitConsole(); // Enable the I2C0 peripheral // 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) { // // Place the data to be sent in the data register // for(i=0;i<=n;i++){ char_rx[i]=I2CSlaveDataGet(I2C0_BASE); // Wait until the slave has received and acknowledged the data. while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_RREQ)); I2CSlaveDataPut(I2C0_BASE,char_rx[i]); while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_TREQ)); } } }
Thank you,
Alphy