Quantcast
Channel: Forums - Recent Threads
Viewing all articles
Browse latest Browse all 262198

MSP432P401R: SPI receive in polling mode

$
0
0

Part Number:MSP432P401R

Hi! I'm using MSP432P401R to communicate with 25LC1024-I/SM EEPROM by SPI in polling mode. The problem is that I have to insert a delay (for loop) before reading a byte, otherwise it reads incorrectly. My code:

void Eeprom_init(void)
{
    /* SPI Master Configuration Parameter */
    const eUSCI_SPI_MasterConfig spiConfig =
    { // SPI mode 3 (1,1)
         EUSCI_A_SPI_CLOCKSOURCE_SMCLK,                              // SMCLK Clock Source
         12000000,                                                   // SMCLK
         1000000,                                                    // SPICLK
         EUSCI_A_SPI_MSB_FIRST,                                      // MSB First
         EUSCI_A_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT,    // Phase
         EUSCI_A_SPI_CLOCKPOLARITY_INACTIVITY_HIGH,                  // High polarity
         EUSCI_A_SPI_3PIN                                            // 3Wire SPI Mode
    };

    // Configuring pins
    MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P2, GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);
    MAP_GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN0); // !CS

    /* Configuring SPI in 3wire master mode */
    MAP_SPI_initMaster(EUSCI_A1_BASE, &spiConfig);

    /* Enable SPI module */
    MAP_SPI_enableModule(EUSCI_A1_BASE);

    /* Enabling interrupts */
    //MAP_SPI_enableInterrupt(EUSCI_A1_BASE, EUSCI_A_SPI_TRANSMIT_INTERRUPT);
    //MAP_SPI_enableInterrupt(EUSCI_A1_BASE, EUSCI_A_SPI_RECEIVE_INTERRUPT);
    //MAP_Interrupt_enableInterrupt(INT_EUSCIA1);

    Eeprom_enableCS();
    Timer32_sleep_cycles(24000); // 1 ms; DCO clock 24MHz
    Eeprom_disableCS();

    char dataRd[2];
    Eeprom_readBytes(2, dataRd, 2);
}

void Eeprom_readBytes(uint32_t address, char data[], int length)
{
    int i;
    Eeprom_enableCS();

    Eeprom_spiWriteByte(E_READ);
    Eeprom_spiWriteByte(address >> 16);
    Eeprom_spiWriteByte(address >> 8);
    Eeprom_spiWriteByte(address);

    for(i = 0; i < length; i++)
    {
        data[i] = Eeprom_spiReadByte();
    }

    Eeprom_disableCS();
}

void Eeprom_spiWriteByte(uint8_t byte)
{
    while (!(MAP_SPI_getInterruptStatus(EUSCI_A1_BASE, EUSCI_A_SPI_TRANSMIT_INTERRUPT)));
    MAP_SPI_transmitData(EUSCI_A1_BASE, byte);

    while (!(MAP_SPI_getInterruptStatus(EUSCI_A1_BASE, EUSCI_A_SPI_RECEIVE_INTERRUPT))); // Dummy byte
}

uint8_t Eeprom_spiReadByte(void) // Adresele nescrise sunt 0xFF
{
//MAP_SPI_clearInterruptFlag(EUSCI_A1_BASE, EUSCI_A_SPI_RECEIVE_INTERRUPT); while (!(MAP_SPI_getInterruptStatus(EUSCI_A1_BASE, EUSCI_A_SPI_TRANSMIT_INTERRUPT))); MAP_SPI_transmitData(EUSCI_A1_BASE, 0xFF); int jj; for(jj=0;jj<50;jj++); //while (!(MAP_SPI_getInterruptStatus(EUSCI_A1_BASE, EUSCI_A_SPI_RECEIVE_INTERRUPT))); return MAP_SPI_receiveData(EUSCI_A1_BASE); }

 With the for loop in Eeprom_spiReadByte() it reads 0x11FF which is the expected value. This is the logic analyzer capture.

Without the loop, it reads 0x0011 which should be 0x11FF.

Any ideas what I am doing wrong? Why is the delay necessary and why doesn't the code work by just polling the receive interrupt in Eeprom_spiReadByte()?

 


Viewing all articles
Browse latest Browse all 262198

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>