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.

TMS320F280037: I2C轮询读写

Part Number: TMS320F280037
Other Parts Discussed in Thread: C2000WARE

//#############################################################################
//
// FILE: i2c_ex1_loopback.c
//
// TITLE: I2C Digital Loopback with FIFO Interrupts
//
//! \addtogroup driver_example_list
//! <h1>I2C Digital Loopback with FIFO Interrupts</h1>
//!
//! This program uses the internal loopback test mode of the I2C module. Both
//! the TX and RX I2C FIFOs and their interrupts are used. The pinmux and I2C
//! initialization is done through the sysconfig file.
//!
//! A stream of data is sent and then compared to the received stream.
//! The sent data looks like this: \n
//! 0000 0001 \n
//! 0001 0002 \n
//! 0002 0003 \n
//! .... \n
//! 00FE 00FF \n
//! 00FF 0000 \n
//! etc.. \n
//! This pattern is repeated forever.
//!
//! \b External \b Connections \n
//! - None
//!
//! \b Watch \b Variables \n
//! - \b sData - Data to send
//! - \b rData - Received data
//! - \b rDataPoint - Used to keep track of the last position in the receive
//! stream for error checking
//!
//
//#############################################################################
//
//
// $Copyright:
// Copyright (C) 2023 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//#############################################################################

//
// Included Files
//
#include "driverlib.h"
#include "device.h"
#include "board.h"

//
// Defines
//
#define TARGET_ADDRESS 0x50

//
// Globals
//
uint16_t sData[2]; // Send data buffer
uint16_t rData[100]; // Receive data buffer
uint16_t rDataPoint = 0; // To keep track of where we are in the
// data stream to check received data

//
// Function Prototypes
//
__interrupt void i2cFIFOISR(void);

void Cla_delayus(int k)
{
int32_t i,m,j;
k = k;
for(i = 0; i<k; i++)
{
for(m=0;m<10;m++)
{
j++;
}
}
}
void main(void)
{
uint16_t i;

//
// Initialize device clock and peripherals
//
Device_init();

//
// Disable pin locks and enable internal pullups.
//
Device_initGPIO();

//
// Initialize PIE and clear PIE registers. Disables CPU interrupts.
//
Interrupt_initModule();

//
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
//
Interrupt_initVectorTable();

//
// Board initialization
//
Board_init();

GPIO_setPinConfig(GPIO_39_GPIO39);
GPIO_setPadConfig(39, GPIO_PIN_TYPE_PULLUP);
GPIO_setQualificationMode(39, GPIO_QUAL_ASYNC);
GPIO_setDirectionMode(39, GPIO_DIR_MODE_OUT);

GPIO_setPinConfig(GPIO_42_I2CA_SDA);
GPIO_setPadConfig(42, GPIO_PIN_TYPE_PULLUP);
GPIO_setQualificationMode(42, GPIO_QUAL_ASYNC);

GPIO_setPinConfig(GPIO_43_I2CA_SCL);
GPIO_setPadConfig(43, GPIO_PIN_TYPE_PULLUP);
GPIO_setQualificationMode(43, GPIO_QUAL_ASYNC);

GPIO_setPinConfig(GPIO_45_GPIO45);
GPIO_setPadConfig(45, GPIO_PIN_TYPE_PULLUP);
GPIO_setQualificationMode(45, GPIO_QUAL_ASYNC);
GPIO_setDirectionMode(45, GPIO_DIR_MODE_OUT);
// For loopback mode only
//
// I2C_setOwnAddress(I2CA_BASE, TARGET_ADDRESS);

I2C_disableModule(I2CA_BASE);
I2C_initController(I2CA_BASE, DEVICE_SYSCLK_FREQ, 400000, I2C_DUTYCYCLE_50);

I2C_setTargetAddress(I2CA_BASE, 0x50);
// I2C_enableLoopback(I2CA_BASE);
I2C_setOwnAddress(I2CA_BASE, 0);
I2C_setBitCount(I2CA_BASE, I2C_BITCOUNT_8);

I2C_setAddressMode(I2CA_BASE, I2C_ADDR_MODE_7BITS);
// I2C_enableFIFO(I2CA_BASE);
// I2C_clearInterruptStatus(I2CA_BASE, I2C_INT_RXFF | I2C_INT_TXFF);
// I2C_setFIFOInterruptLevel(I2CA_BASE, I2C_FIFO_TX1, I2C_FIFO_RX2);
// I2C_enableInterrupt(I2CA_BASE, I2C_INT_RXFF | I2C_INT_TXFF);
I2C_setEmulationMode(I2CA_BASE, I2C_EMULATION_FREE_RUN);
I2C_enableModule(I2CA_BASE);
// Interrupts that are used in this example are re-mapped to ISR functions
// found within this file.
//
// Interrupt_register(INT_I2CA_FIFO, &i2cFIFOISR);

GPIO_writePin(45,1);
// Initialize the data buffers
//
GPIO_writePin(39,0);
for(i = 0; i < 2; i++)
{
sData[i] = i;
rData[i]= 0;
}

//
// Enable interrupts required for this example
//
// Interrupt_enable(INT_I2CA_FIFO);

//
// Enable Global Interrupt (INTM) and realtime interrupt (DBGM)
//
EINT;
ERTM;


while(1)
{
GPIO_writePin(45,0);
I2C_setTargetAddress(I2CA_BASE, 0x50);
I2C_setConfig(I2CA_BASE, I2C_CONTROLLER_SEND_MODE);
I2C_setDataCount(I2CA_BASE, (100 + 2));
I2C_putData(I2CA_BASE, 0);
I2C_putData(I2CA_BASE, 1);//地址0x0001
I2C_sendStartCondition(I2CA_BASE);
for(i=0;i<100;i++)
{
Cla_delayus(15);
I2C_putData(I2CA_BASE,i);
}
Cla_delayus(50);
GPIO_writePin(45,1);
I2C_sendStopCondition(I2CA_BASE);

Cla_delayus(1000);

while(I2C_isBusBusy(I2CA_BASE));
I2C_setTargetAddress(I2CA_BASE, 0x50);
I2C_setDataCount(I2CA_BASE, 2);
I2C_putData(I2CA_BASE, 0);
I2C_putData(I2CA_BASE, 1);//地址0x0001
I2C_setConfig(I2CA_BASE, I2C_CONTROLLER_SEND_MODE);
I2C_sendStartCondition(I2CA_BASE);
Cla_delayus(20);
I2C_setDataCount(I2CA_BASE, 100);
I2C_setConfig(I2CA_BASE, I2C_CONTROLLER_RECEIVE_MODE);
rData[0] = I2C_getData(I2CA_BASE);
I2C_sendStartCondition(I2CA_BASE);
for(i=1;i<100;i++)
{
Cla_delayus(15);
rData[i] = I2C_getData(I2CA_BASE);
}

I2C_sendStopCondition(I2CA_BASE);
Cla_delayus(30000);
}

}

在I2C_ex2_eeprom.c历程的基础上更改上述程序,写时会卡在

while(I2C_isBusBusy(I2CA_BASE)); 一直在busy,写时那里程序写的不对?还有读取一直没读取成功。

实际应用的程序由于逻辑关系不能用例如历程中的中断,只想轮询写。