TI中文支持网
TI专业的中文技术问题搜集分享网站

tm4c129 进不去 i2c从接收中断

 使用的examples\peripherals\i2c目录下的i2c 从接收程序,不触发接收中断程序,下面是代码

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_i2c.h"
#include "inc/hw_ints.h"
#include "driverlib/gpio.h"
#include "drivers/pinout.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/i2c.h"

#include "utils/uartstdio.h"

//*****************************************************************************
//
// System clock rate in Hz.
//
//*****************************************************************************
uint32_t g_ui32SysClock;

#define SLAVE_ADDRESS           0x3C
static uint32_t g_ui32DataRx;
static bool g_bIntFlag = false;

//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif

//*****************************************************************************
//
// Configure the UART and its pins.  This must be called before UARTprintf().
//
//*****************************************************************************
void
ConfigureUART(void)
{
    //
    // Enable the GPIO Peripheral used by the UART.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    //
    // Enable UART0
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART2);

    //
    // Configure GPIO Pins for UART mode.
    //
    ROM_GPIOPinConfigure(GPIO_PA6_U2RX);
    ROM_GPIOPinConfigure(GPIO_PA7_U2TX);
    ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7);

    //
    // Initialize the UART for console I/O.
    //
    UARTStdioConfig(2, 115200, g_ui32SysClock);
}

void
I2C0SlaveIntHandler(void)
{
    //
    // Clear the I2C0 interrupt flag.
    //
    I2CSlaveIntClear(I2C0_BASE);

    //
    // Read the data from the slave.
    //
    g_ui32DataRx = I2CSlaveDataGet(I2C0_BASE);

    //
    // Set a flag to indicate that the interrupt occurred.
    //
    g_bIntFlag = true;
}

int
main(void)
{
   uint32_t ui32DataTx;
 
    //
    // Run from the PLL at 120 MHz.
    //
    g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
                SYSCTL_CFG_VCO_480), 120000000);

    //
    // The I2C0 peripheral must be enabled before use.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);

    //
    // For this example I2C0 is used with PortB[3:2].  The actual port and
    // pins used may be different on your part, consult the data sheet for
    // more information.  GPIO port B needs to be enabled so these pins can
    // be used.
    // TODO: change this to whichever GPIO port you are using.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

    //
    // Configure the pin muxing for I2C0 functions on port B2 and B3.
    // This step is not necessary if your part does not support pin muxing.
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinConfigure(GPIO_PB2_I2C0SCL);
    GPIOPinConfigure(GPIO_PB3_I2C0SDA);

    //
    // Select the I2C function for these pins.  This function will also
    // configure the GPIO pins pins for I2C operation, setting them to
    // open-drain operation with weak pull-ups.  Consult the data sheet
    // to see which functions are allocated per pin.
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
    GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);

    //
    // Enable loopback mode.  Loopback mode is a built in feature that helps
    // for debug the I2Cx module.  It internally connects the I2C master and
    // slave terminals, which effectively lets you send data as a master and
    // receive data as a slave.  NOTE: For external I2C operation you will need
    // to use external pull-ups that are faster than the internal pull-ups.
    // Refer to the datasheet for more information.
    //
    HWREG(I2C0_BASE + I2C_O_MCR) |= 0x01;

    //
    // Enable the I2C0 interrupt on the processor (NVIC).
    //
    ROM_IntEnable(INT_I2C0);

    //
    // Configure and turn on the I2C0 slave interrupt.  The I2CSlaveIntEnableEx()
    // gives you the ability to only enable specific interrupts.  For this case
    // we are only interrupting when the slave device receives data.
    //
    I2CSlaveIntEnableEx(I2C0_BASE, I2C_SLAVE_INT_DATA);

    //
    // Enable and initialize the I2C0 master module.  Use the system clock for
    // the I2C0 module.  The last parameter sets the I2C data transfer rate.
    // If false the data rate is set to 100kbps and if true the data rate will
    // be set to 400kbps.  For this example we will use a data rate of 100kbps.
    //
    I2CMasterInitExpClk(I2C0_BASE, g_ui32SysClock, false);

    //
    // Enable the I2C0 slave module.
    //
    I2CSlaveEnable(I2C0_BASE);

    //
    // Set the slave address to SLAVE_ADDRESS.  In loopback mode, it's an
    // arbitrary 7-bit number (set in a macro above) that is sent to the
    // I2CMasterSlaveAddrSet function.
    //
    I2CSlaveInit(I2C0_BASE, SLAVE_ADDRESS);

    //
    // Tell the master module what address it will place on the bus when
    // communicating with the slave.  Set the address to SLAVE_ADDRESS
    // (as set in the slave module).  The receive parameter is set to false
    // which indicates the I2C Master is initiating a writes to the slave.  If
    // true, that would indicate that the I2C Master is initiating reads from
    // the slave.
    //
    I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);

    //
    // Set up the serial console to use for displaying messages.  This is just
    // for this example program and is not needed for proper I2C operation.
    //
    ConfigureUART();

    //
    // Enable interrupts to the processor.
    //
    ROM_IntMasterEnable();

    //
    // Display the example setup on the console.
    //
    UARTprintf("I2C Slave Interrupt Example ->");
    UARTprintf("\n   Module = I2C0");
    UARTprintf("\n   Mode = Receive interrupt on the Slave module");
    UARTprintf("\n   Rate = 100kbps\n\n");

    //
    // Initialize the data to send.
    //
    ui32DataTx = 'I';

    //
    // Indicate the direction of the data.
    //
    UARTprintf("Transferring from: Master -> Slave\n");

    //
    // Display the data that I2C0 is transferring.
    //
    UARTprintf("  Sending: '%c'", ui32DataTx);

    //
    // Place the data to be sent in the data register.
    //
    I2CMasterDataPut(I2C0_BASE, ui32DataTx);

    //
    // Initiate send of single piece of data from the master.  Since the
    // loopback mode is enabled, the Master and Slave units are connected
    // allowing us to receive the same data that we sent out.
    //
    I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);

    //
    // Wait for interrupt to occur.
    //
    while(!g_bIntFlag)
    {
    }

    //
    // Display that interrupt was received.
    //
    UARTprintf("\n  Slave Interrupt Received!\n");

    //
    // Display the data that the slave has received.
    //
    UARTprintf("  Received: '%c'\n\n", g_ui32DataRx);

    //
    // Loop forever.
    //
    while(1)
    {
    }
}

xyz549040622:

你的这个中断,在启动文件中申明和定义了木有,估计没有吧

chunxiao liu:

回复 xyz549040622:

这个我定义了  就是进不去中断

******************************************************************************;; Place code into the reset code section.;;******************************************************************************        AREA    RESET, CODE, READONLY        THUMB

  EXTERN  I2C0SlaveIntHandler;******************************************************************************;; The vector table.;;******************************************************************************        EXPORT  __Vectors__Vectors        DCD     StackMem + Stack            ; Top of Stack        DCD     Reset_Handler               ; Reset Handler        DCD     NmiSR                       ; NMI Handler        DCD     FaultISR                    ; Hard Fault Handler        DCD     IntDefaultHandler           ; The MPU fault handler        DCD     IntDefaultHandler           ; The bus fault handler        DCD     IntDefaultHandler           ; The usage fault handler        DCD     0                           ; Reserved        DCD     0                           ; Reserved        DCD     0                           ; Reserved        DCD     0                           ; Reserved        DCD     IntDefaultHandler           ; SVCall handler        DCD     IntDefaultHandler           ; Debug monitor handler        DCD     0                           ; Reserved        DCD     IntDefaultHandler           ; The PendSV handler        DCD     IntDefaultHandler           ; The SysTick handler        DCD     IntDefaultHandler           ; GPIO Port A        DCD     IntDefaultHandler           ; GPIO Port B        DCD     IntDefaultHandler           ; GPIO Port C        DCD     IntDefaultHandler           ; GPIO Port D        DCD     IntDefaultHandler           ; GPIO Port E        DCD     IntDefaultHandler           ; UART0 Rx and Tx        DCD     IntDefaultHandler           ; UART1 Rx and Tx        DCD     IntDefaultHandler           ; SSI0 Rx and Tx        DCD     I2C0SlaveIntHandler         ; I2C0 Master and Slave        DCD     IntDefaultHandler           ; PWM Fault        DCD     IntDefaultHandler           ; PWM Generator 0        DCD     IntDefaultHandler           ; PWM Generator 1        DCD     IntDefaultHandler           ; PWM Generator 2        DCD     IntDefaultHandler           ; Quadrature Encoder 0        DCD     IntDefaultHandler           ; ADC Sequence 0

xyz549040622:

回复 chunxiao liu:

申明了木有,如果都OK了,那就是你不满足进入中断的条件吧?跟踪中断标志看看,看看程序跑到哪里了?

chunxiao liu:

回复 xyz549040622:

我追踪了寄存器值发现是I2CSlaveIntEnableEx(I2C0_BASE, I2C_SLAVE_INT_DATA); 这个语句没有执行成功,把它放到从模式使能之后,中断触发了但是到while循环那卡住了,在while循环之前加个打印语句就好了,不加就会卡到while循环,这是什么问题?

xyz549040622:

回复 chunxiao liu:

好奇怪的问题,你把打印语句去掉,加个延时看看,是由于时间不够呢,还是和printf中的语句有关了。

chunxiao liu:

回复 xyz549040622:

应该和延时有关,I2CMasterControl和while循环之间要有个延时,关键是while阻塞等待中断为什么不行, 在while循环里加格延时也可以,感觉好奇怪。

赞(0)
未经允许不得转载:TI中文支持网 » tm4c129 进不去 i2c从接收中断
分享到: 更多 (0)