//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// master tm4c1294 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include #include #include #include "inc/hw_ints.h" #include "inc/hw_memmap.h" #include "inc/hw_types.h" #include "driverlib/debug.h" #include "driverlib/fpu.h" #include "driverlib/gpio.h" #include "driverlib/i2c.h" #include "driverlib/interrupt.h" #include "driverlib/pin_map.h" #include "driverlib/rom.h" #include "driverlib/rom_map.h" #include "driverlib/sysctl.h" #include "driverlib/timer.h" #include "driverlib/uart.h" #include "utils/uartstdio.h" void ConfigureGPIO(void); void ConfigureI2C0(void); //***************************************************************************** // Define MSP430 I2C Address. // //***************************************************************************** #define SLAVE_ADDRESS 0x48 //***************************************************************************** // // Counter to count the number of interrupts that have been called. // //***************************************************************************** static volatile uint32_t g_ui32Counter = 0; static uint32_t g_ui32DataRx; static bool g_bIntFlag = false; //**************************************************************************** // // System clock rate in Hz. // //**************************************************************************** uint32_t g_ui32SysClock; uint32_t pui32DataRx[5] = {0}; uint32_t ui32Index; //***************************************************************************** // // Flags that contain the current value of the interrupt indicator as displayed // on the UART. // //***************************************************************************** //uint32_t g_ui32Flags; volatile uint_fast8_t g_ui32Flags; //***************************************************************************** // // 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. // MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // // Enable UART0. // MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); // // Configure GPIO Pins for UART mode. // MAP_GPIOPinConfigure(GPIO_PA0_U0RX); MAP_GPIOPinConfigure(GPIO_PA1_U0TX); MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); // // Initialize the UART for console I/O. // UARTStdioConfig(0, 115200, g_ui32SysClock); } /* void I2C0IntHandler(void) { UARTprintf("\n I2C0MASTERIntHandler........."); // // Clear the I2C0 interrupt flag. // I2CMasterIntClear(I2C0_BASE); // // Read the data from the slave. // g_ui32DataRx = I2CMasterDataGet(I2C0_BASE); // // Set a flag to indicate that the interrupt occurred. // g_bIntFlag = true; } */ //***************************************************************************** // // This example application demonstrates the use of the timers to generate // periodic interrupts. // //***************************************************************************** int main(void) { // // Run from the PLL at 120 MHz. // Note: SYSCTL_CFG_VCO_240 is a new setting provided in TivaWare 2.2.x and // later to better reflect the actual VCO speed due to SYSCTL#22. // g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), 120000000); // // Initialize the UART and write status. // ConfigureUART(); ConfigureI2C0(); g_ui32Counter = 0; // Enable processor interrupts. // MAP_IntMasterEnable(); // Loop forever while the timers run. // while(1) {/* // // Modify the data direction to true, so that seeing the address will // indicate that the I2C Master is initiating a read from the slave. // MAP_I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, true); // // Setup for first read. Use I2C_MASTER_CMD_BURST_RECEIVE_START // to start a burst mode read. The I2C master continues to own // the bus at the end of this transaction. // MAP_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); while(I2CMasterBusy(I2C0_BASE)); // Wait for completion // // Read the first byte data from the slave. // pui32DataRx[0] = MAP_I2CMasterDataGet(I2C0_BASE); // while(I2CMasterBusy(I2C0_BASE)); // // Setup for the second read. Use I2C_MASTER_CMD_BURST_RECEIVE_CONT // to continue the burst mode read. The I2C master continues to own // the bus at the end of this transaction. // MAP_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); while(I2CMasterBusy(I2C0_BASE)); // Wait for completion // // Read the second byte data from the slave. // pui32DataRx[1] = MAP_I2CMasterDataGet(I2C0_BASE); MAP_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); while(I2CMasterBusy(I2C0_BASE)); // Wait for completion // // Read the third byte data from the slave // pui32DataRx[2] = MAP_I2CMasterDataGet(I2C0_BASE); MAP_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); while(I2CMasterBusy(I2C0_BASE)); // Wait for completion // // Read the third byte data from the slave // pui32DataRx[3] = MAP_I2CMasterDataGet(I2C0_BASE); MAP_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); while(I2CMasterBusy(I2C0_BASE)); // Wait for completion // // Read the third byte data from the slave // pui32DataRx[4] = MAP_I2CMasterDataGet(I2C0_BASE); // // Setup for the third read. Use I2C_MASTER_CMD_BURST_RECEIVE_FINISH // to terminate the I2C transaction. At the end of this transaction, // the STOP bit will be issued and the I2C bus is returned to the // Idle state. // MAP_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);*/ MAP_I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, true); // // Do a dummy receive to make sure you don't get junk on the first receive. // // I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); // I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); for(ui32Index = 0; ui32Index < 5; ui32Index++) { // // Read the data from the slave. // pui32DataRx[ui32Index] = MAP_I2CMasterDataGet(I2C0_BASE); // // Wait until master module is done transferring. // while(I2CMasterBusy(I2C0_BASE)) { } MAP_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); // // Display the data that the slave has received. // UARTprintf("Received: '%c'\n", pui32DataRx[ui32Index]); } MAP_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); } } //***************************************************************************** // // Configures I2C port // //***************************************************************************** void ConfigureI2C0(void) { // // The I2C0 peripheral must be enabled before use. // MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);// // // Wait for the I2C0 module to be ready. // while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C0)) { } // Configure the pin muxing for I2C0 functions on port D0 and D1. // This step is not necessary if your part does not support pin muxing. // MAP_GPIOPinConfigure(GPIO_PB2_I2C0SCL); MAP_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. // MAP_GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); MAP_GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); // // IntEnable(INT_I2C0); // I2CMasterIntEnableEx(I2C0_BASE, I2C_MASTER_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. // MAP_I2CMasterInitExpClk(I2C0_BASE, g_ui32SysClock, false); // // Enable the I2C0 MASTER module. // I2CMasterEnable(I2C0_BASE); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// slave msp430 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include unsigned char *PTxData; // Pointer to TX data volatile unsigned char TXByteCtr; const unsigned char TxData[] = // Table of data to transmit { 0x11, 0x22, 0x33, 0x44, 0x55 }; int main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT uart(); i2c(); while (1) { PTxData = (unsigned char *)TxData; // Start of TX buffer TXByteCtr = 0; // Clear TX byte count __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts // Remain in LPM0 until master // finishes RX __no_operation(); // Set breakpoint >>here<< and } // read out the TXByteCtr counter } //------------------------------------------------------------------------------ // The USCI_B0 data ISR is used to move data from MSP430 memory to the // I2C master. PTxData points to the next byte to be transmitted, and TXByteCtr // keeps track of the number of bytes transmitted. //------------------------------------------------------------------------------ #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__) #pragma vector = USCIAB0TX_VECTOR __interrupt void USCIAB0TX_ISR(void) #elif defined(__GNUC__) void __attribute__ ((interrupt(USCIAB0TX_VECTOR))) USCIAB0TX_ISR (void) #else #error Compiler not supported! #endif { UCB0TXBUF = *PTxData++; // Transmit data at address PTxData TXByteCtr++; // Increment TX byte counter } //------------------------------------------------------------------------------ // The USCI_B0 state ISR is used to wake up the CPU from LPM0 in order to do // processing in the main program after data has been transmitted. LPM0 is // only exit in case of a (re-)start or stop condition when actual data // was transmitted. //------------------------------------------------------------------------------ #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__) #pragma vector = USCIAB0RX_VECTOR __interrupt void USCIAB0RX_ISR(void) #elif defined(__GNUC__) void __attribute__ ((interrupt(USCIAB0RX_VECTOR))) USCIAB0RX_ISR (void) #else #error Compiler not supported! #endif { UCB0STAT &= ~(UCSTPIFG + UCSTTIFG); // Clear interrupt flags if (TXByteCtr) // Check TX byte counter __bic_SR_register_on_exit(CPUOFF); // Exit LPM0 if data was } // transmitted void i2c(void) { P1DIR |= 00000001; //LED P1OUT = 0x01; P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0 P1SEL2|= BIT6 + BIT7; // Assign I2C pins to USCI_B0 UCB0CTL1 |= UCSWRST; // Enable SW reset UCB0CTL0 = UCMODE_3 + UCSYNC; // I2C Slave, synchronous mode UCB0I2COA = 0x48; // Own Address is 048h UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation UCB0I2CIE |= UCSTPIE; // Enable STT and STP interrupt IE2 |= UCB0TXIE; // Enable TX interrupt } void uart(void){ if (CALBC1_1MHZ==0xFF) // Check if calibration constant erased { while(1); // do not load program } DCOCTL = 0; // Select lowest DCO settings BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1 MHz DCOCTL = CALDCO_1MHZ; P1SEL = BIT1 + BIT2 ; // Select UART RX/TX function on P1.1,P1.2 P1SEL2 = BIT1 + BIT2; UCA0CTL1 |= UCSSEL_2; // UART Clock -> SMCLK UCA0BR0 = 104; // Baud Rate Setting for 1MHz 9600 UCA0BR1 = 0; // Baud Rate Setting for 1MHz 9600 UCA0MCTL = UCBRS_1; // Modulation Setting for 1MHz 9600 UCA0CTL1 &= ~UCSWRST; // Initialize UART Module }