我有 MSP430 SPI 设置、可与从外设通信。 唤醒序列总共有9个事务。 事务5之后需要一个返回字节。 没有其他事务返回一个字节。 我遇到 SPI 接收问题。 似乎每个字节采用0xff 的值、这会触发接收中断。 我无法成功完成序列、因为我没有返回所需的字节。 从器件可通过评估 GUI 正常工作。 我很感激这里可以更改的内容的指示。
我们将尝试一种轮询方法。
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #include <driverlib.h> #include <msp430.h> #include "clock~.h" // Clock configurations static uint8_t TXData = 0; //transaction count static uint8_t RXData = 0; //SPI receive byte static uint8_t waitForReceive = 0; typedef struct { uint8_t Len; uint8_t Data[5]; } spi_MaximTrans_t; static spi_MaximTrans_t DCInit_transactions[15]; uint8_t SPI_TX_index = 0; //byte count in transaction /** * Initialize system clocks */ static void init_clock(void) { // Configure one FRAM waitstate as required by the device datasheet for MCLK // operation beyond 8MHz _before_ configuring the clock system. FRAMCtl_configureWaitStateControl(FRAMCTL_ACCESS_TIME_CYCLES_1); //Set DCO FLL reference = REFO CS_initClockSignal(CS_FLLREF, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1); //Set ACLK = REFO CS_initClockSignal(CS_ACLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1); CS_initFLLParam param = {0}; //Set Ratio/Desired MCLK Frequency, initialize DCO, save trim values CS_initFLLCalculateTrim(CS_MCLK_DESIRED_FREQUENCY_IN_KHZ, CS_MCLK_FLLREF_RATIO, ¶m); //Set MCLK = REFO CS_initClockSignal(CS_MCLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1); //Set SMCLK = DCO CS_initClockSignal(CS_SMCLK, CS_DCOCLKDIV_SELECT, CS_CLOCK_DIVIDER_1); //Clear all OSC fault flag CS_clearAllOscFlagsWithTimeout(1000); } /** * Initialize all of the IO pins per their configuration */ static void init_gpio(void) { // Set all GPIO pins to output low to prevent floating input and reduce power consumption GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN_ALL8); GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN_ALL8); GPIO_setOutputLowOnPin(GPIO_PORT_P3, GPIO_PIN_ALL8); GPIO_setOutputLowOnPin(GPIO_PORT_P4, GPIO_PIN_ALL8); GPIO_setOutputLowOnPin(GPIO_PORT_P5, GPIO_PIN_ALL8); GPIO_setOutputLowOnPin(GPIO_PORT_P6, GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2); GPIO_setAsOutputPin( GPIO_PORT_P1, GPIO_PIN_ALL8); GPIO_setAsOutputPin( GPIO_PORT_P2, GPIO_PIN_ALL8); GPIO_setAsOutputPin( GPIO_PORT_P3, GPIO_PIN_ALL8); GPIO_setAsOutputPin( GPIO_PORT_P4, GPIO_PIN_ALL8); GPIO_setAsOutputPin( GPIO_PORT_P5, GPIO_PIN_ALL8); GPIO_setAsOutputPin( GPIO_PORT_P6, GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2); } /* * Initialize the SPI peripheral on EUSCI A1 */ void init_spi_peripheral() { //Initialize Master EUSCI_A_SPI_initMasterParam param = {0}; param.selectClockSource = EUSCI_A_SPI_CLOCKSOURCE_SMCLK; param.clockSourceFrequency = CS_getSMCLK(); param.desiredSpiClock = 1000000; param.msbFirst = UCMSB; param.clockPhase = 0; param.clockPolarity = EUSCI_A_SPI_CLOCKPOLARITY_INACTIVITY_LOW; param.spiMode = EUSCI_A_SPI_3PIN; EUSCI_A_SPI_initMaster(EUSCI_A1_BASE, ¶m); EUSCI_A_SPI_enable(EUSCI_A1_BASE); // Enable the TX and RX interrupts for the SPI bus. EUSCI_A_SPI_enableInterrupt(EUSCI_A1_BASE, EUSCI_A_SPI_TRANSMIT_INTERRUPT); EUSCI_A_SPI_enableInterrupt(EUSCI_A1_BASE, EUSCI_A_SPI_RECEIVE_INTERRUPT); } void SetUpTransactions(void){ //Enable keep alive mode DCInit_transactions[0].Len = 2; DCInit_transactions[0].Data[0] = 0x10; DCInit_transactions[0].Data[1] = 0x5; //No Receive //Enable Rx Interrupt flags DCInit_transactions[1].Len = 2; DCInit_transactions[1].Data[0] = 0x4; DCInit_transactions[1].Data[1] = 0x88; //No Receive //Clear receive buffer DCInit_transactions[2].Len = 1; DCInit_transactions[2].Data[0] = 0xe0; //No Receive //Wakeup UART slave devices DCInit_transactions[3].Len = 2; DCInit_transactions[3].Data[0] = 0xe0; DCInit_transactions[3].Data[1] = 0x30; //No Receive //Wait for all UART slave devices to wake up DCInit_transactions[4].Len = 1; DCInit_transactions[4].Data[0] = 0x01; //Receive of 0x21 expected //End of UART slave device wake-up period DCInit_transactions[5].Len = 2; DCInit_transactions[5].Data[0] = 0x0e; DCInit_transactions[5].Data[1] = 0x10; //No Receive //Wait for null message to be received DCInit_transactions[6].Len = 1; DCInit_transactions[6].Data[0] = 0x01; //No Receive //Clear transmit buffer DCInit_transactions[7].Len = 1; DCInit_transactions[7].Data[0] = 0x20; //No Receive //Clear receive buffer DCInit_transactions[8].Len = 1; DCInit_transactions[8].Data[0] = 0xe0; //No Receive } /** * main.c */ int main(void) { WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer init_clock(); init_gpio(); // Set up IO pins GPIO_setOutputHighOnPin(GPIO_PORT_P3, GPIO_PIN1); // Configure SPI Pins for UCA1CLK, UCA1TXD/UCA1SIMO and UCA1RXD/UCA1SOMI /* * Select Port 2 * Set Pin 4, Pin 5 and Pin 6 to input Secondary Module Function */ GPIO_setAsPeripheralModuleFunctionInputPin( GPIO_PORT_P2, GPIO_PIN4 + GPIO_PIN5 + GPIO_PIN6, GPIO_PRIMARY_MODULE_FUNCTION ); // Set P1.0 to output direction GPIO_setAsOutputPin (GPIO_PORT_P1, GPIO_PIN0); GPIO_setAsInputPinWithPullUpResistor (GPIO_PORT_P4, GPIO_PIN2); GPIO_enableInterrupt (GPIO_PORT_P4, GPIO_PIN2); GPIO_selectInterruptEdge (GPIO_PORT_P4, GPIO_PIN2, GPIO_HIGH_TO_LOW_TRANSITION); GPIO_clearInterrupt (GPIO_PORT_P4, GPIO_PIN2); PMM_unlockLPM5(); SetUpTransactions(); TXData = 0x0; // Holds transaction number // Setup peripheral(s) now that gpio and clocks are setup init_spi_peripheral(); // Init Maxim spi peripheral //Wait for slave to initialize __delay_cycles(100); GPIO_setOutputLowOnPin(GPIO_PORT_P3, GPIO_PIN1); //Maxim chip select low UCA1IFG |= UCTXIFG; __bis_SR_register(LPM0_bits + GIE); // CPU off, enable interrupts __no_operation(); // Remain in LPM0 return 0; } //****************************************************************************** // //This is the PORT2_VECTOR interrupt vector service routine // //****************************************************************************** #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__) #pragma vector=PORT4_VECTOR __interrupt #elif defined(__GNUC__) __attribute__((interrupt(PORT2_VECTOR))) #endif void P4_ISR (void) { // GPIO_setOutputHighOnPin(GPIO_PORT_P3, GPIO_PIN1); GPIO_clearInterrupt (GPIO_PORT_P4, GPIO_PIN2); // Toggle P1.0 output GPIO_toggleOutputOnPin (GPIO_PORT_P1, GPIO_PIN0); GPIO_disableInterrupt (GPIO_PORT_P4, GPIO_PIN2); } /**************************************************************************** * EUSCI_A1_isr() * * This interrupt is used for SPI interrupts (UCA1). ****************************************************************************/ #pragma vector = EUSCI_A1_VECTOR __interrupt void EUSCI_A1_isr(void) { // SPI TX interrupt? if (UCA1IFG & UCTXIFG) { while (UCA1STATW & UCBUSY); EUSCI_A_SPI_transmitData(EUSCI_A1_BASE, DCInit_transactions[TXData].Data[SPI_TX_index]); SPI_TX_index++; if (SPI_TX_index == DCInit_transactions[TXData].Len){ SPI_TX_index = 0; if (!waitForReceive) TXData = TXData+1; if (TXData == 5){ TXData = 4; waitForReceive = 1; } if (TXData == 9) { while (UCA1STATW & UCBUSY); //wait for the last byte to transmit EUSCI_A_SPI_disableInterrupt(EUSCI_A1_BASE, EUSCI_A_SPI_TRANSMIT_INTERRUPT); EUSCI_A_SPI_disableInterrupt(EUSCI_A1_BASE, EUSCI_A_SPI_RECEIVE_INTERRUPT); } } /* Put a new byte to send into the TX buffer. * This prevents us from stuffing an extra byte. *****/ /* Disable the TX interrupt. Since the last byte of the message * has been sent, disable the TX interrupt. If this is not done, * the ISR will be repeatedly invoked between the time that the * time that the UCA1TXBUF is empty and the time that the receive * byte is available in UCA1RXBUF. *****/ } if (UCA1IFG & UCRXIFG) { RXData = EUSCI_A_SPI_receiveData(EUSCI_A1_BASE); if (waitForReceive && (RXData == 0x21)){ waitForReceive = 0; TXData = 5; } } }