10 Replies Latest reply on Jul 22, 2018 2:54 AM by herb_2497906

    SPI Driver for ST M95320 EE prom

    herb_2497906

      I have run out of ideas on why my SPI is not working, just will not function, I have tried so many ideas, none seems to work.

       

      In the code attached, I am simply trying to read the ID register address 0x00 which should read 0x20, all I get is 0xFF.  This device has an ID register, and the EE_READ_ID function is supposed to just read that value for now.

       

      I could really use some help, any suggestions would be appreciated.  I reviewed the SPI Master example and coded my SUBR ST_EE_PROM.C based on the instructions.  I just do not see why it will not read the register.

       

      Herb

        • 1. Re: SPI Driver for ST M95320 EE prom
          rajiv.vasanth.badiger

          Herb,

           

          You need to wait for 4 bytes to be sent before reading the ID from the FIFO. Please use the below code after SPI_SpiUartPutArray(EE_TX_buf,4) function call-

           

          while(SPIM_SpiUartGetRxBufferSize() != 4);

            

          SPI_SpiUartPutArray() function waits only if the TX buffer is not empty; otherwise, it simply loads the data into the buffer and returns.

           

          -Rajiv

          • 2. Re: SPI Driver for ST M95320 EE prom
            herb_2497906

            Well that did not work, so please remove the correct answer, all that did was make the code halt there and reset, nothing is being sent, no data, SS or clock.

             

            Here is the modified code, this is becoming and urgent problem, this should be working.

             

            Herb

            • 3. Re: SPI Driver for ST M95320 EE prom
              herb_2497906

              We may have found a hardware connection issue, that was not on the test board.  If so, it would explain why things were not working.

              • 4. Re: SPI Driver for ST M95320 EE prom
                rajiv.vasanth.badiger

                Good to know. But note that you still need to wait for the data to be sent completely before reading the RX buffer. The code should not have hung up at the while loop; need to see if there is something else causing the problem. Let us know the result after fixing the hardware connection issue which you mentioned.

                • 5. Re: SPI Driver for ST M95320 EE prom
                  herb_2497906

                  The hardware issue was not correct, the device is wired correctly, the pins are assigned correctly.

                   

                  Still does not work, and hangs on the buffer check line until it resets.  If I comment out that line, it does not reset.

                   

                  Also, I my BLE will now not show up advertising, I checked the instruction with the debugger and it execute the Stack on and the Start_Advertising, but nothing on the scanners.

                   

                  Rest of the functional code that controls the outputs works?  Does not appears to be resetting.

                   

                  Please take a closer look?  I really need the help to get over these concerns.

                   

                  Herb

                  • 6. Re: SPI Driver for ST M95320 EE prom
                    herb_2497906

                    The while loop is blocking and my WDT interrupt was crushed by it, so it crashed.  I added a CY_Delay(10); into my EE code and it all works now.

                     

                    Is there a code function that will wait but allow interrupts?

                    • 7. Re: SPI Driver for ST M95320 EE prom
                      rajiv.vasanth.badiger

                      I see that you are using the wrong API function. As I mentioned in the previous post, you need to use SPIM_SpiUartGetRxBufferSize(); instead TX buffer size is being read.

                       

                      -Rajiv

                      • 8. Re: SPI Driver for ST M95320 EE prom
                        herb_2497906

                        Thank you Rajiv, as we all get there when coding, you cannot see the problem right in front of you.

                         

                        And for those who may need it, here is a working, Write Enable, Checks the Write Enable bit, Writes, Reads and compares a single byte to any address in an EEprom.  Below is the Read function.

                         

                        /*******************************************************************************

                        * Function Name: Write a memory location

                        ********************************************************************************

                        * Summary:

                        *   Write a particular memory location, defined by High Byte, Low Byte

                        *   The read it back to make sure it was written

                        * Parameters: 

                        *   Up to 11 bits of addressability 0b (XXXX a11a10a9a8a) (7a6a5a4a 3a2a1a0)

                        *

                        * Return:

                        *   if successful, the data word is returned

                        *   if unsuccessful EE_WRITE_ERROR constant is returned

                        *******************************************************************************/

                        uint8 EE_Write_Addr(uint8 HighByte, uint8 LowByte, uint8 data)

                        {   

                            SPI_SpiUartClearRxBuffer();

                            SPI_SpiUartClearTxBuffer();

                         

                         

                            // Send the wrtie enable command

                            SPI_SpiUartWriteTxData(EE_WRITE_ENABLE);

                           

                            while(SPI_SpiUartGetRxBufferSize() != 1);

                         

                            SPI_SpiUartClearRxBuffer();

                            SPI_SpiUartClearTxBuffer();

                         

                         

                            // build the output buffer to read the status

                            EE_TX_buf[0] = EE_READ_STATUS; // read status instruction

                            EE_TX_buf[1] = EE_PAD_BYTE; // pad one for status word

                            EE_TX_buf[2] = EE_PAD_BYTE; // pad two for status word

                            // write the buffer

                            SPI_SpiUartPutArray(EE_TX_buf,3);

                           

                            while(SPI_SpiUartGetRxBufferSize() != 3);

                           

                            // read the buffer

                            EE_dummy_read = SPI_SpiUartReadRxData();

                            EE_RX_buf[0] = SPI_SpiUartReadRxData();

                         

                         

                            // test for write enable bit

                            if(EE_RX_buf[0] != EE_WEL_BIT)

                              {  

                                ID_Value = EE_WREN_NOT_OK;

                                    return ID_Value;

                              }

                           

                            // WREN is good so write the byte

                            EE_Adr_high_byte = HighByte;

                            EE_Adr_low_byte = LowByte;

                           

                            // build the output buffer

                            EE_TX_buf[0] = EE_WRITE; // write instruction

                            EE_TX_buf[1] = EE_Adr_high_byte; // High byte

                            EE_TX_buf[2] = EE_Adr_low_byte; // Low byte

                            EE_TX_buf[3] = data; // data for writing

                         

                         

                            SPI_SpiUartPutArray(EE_TX_buf,4);

                           

                            while(SPI_SpiUartGetRxBufferSize() != 4);

                          

                            // Read the address

                            EE_Read_Addr(HighByte,LowByte);  // returns READ_Value

                           

                            // Test it against data to ensure it was written correctly

                            if(data != READ_Value)

                            {

                                return EE_WRITE_ERROR; // if no match signal an error

                            }

                            return READ_Value; // if a match return the value

                           }

                         

                        /*******************************************************************************

                        * Function Name: Read a memory location

                        ********************************************************************************

                        * Summary:

                        *   Read a particular memory location, defined by High Byte, Low Byte

                        *   

                        * Parameters: 

                        *   Up to 11 bits of addressability 0b (XXXX a11a10a9a8a) (7a6a5a4a 3a2a1a0)

                        *

                        * Return:

                        *   READ_Value value at that address

                        *  

                        *******************************************************************************/

                        uint8 EE_Read_Addr(uint8 HighByte, uint8 LowByte)

                        {   

                            SPI_SpiUartClearRxBuffer();

                            SPI_SpiUartClearTxBuffer();

                         

                         

                            EE_Adr_high_byte = HighByte;

                            EE_Adr_low_byte = LowByte;

                           

                            // build the output buffer

                            EE_TX_buf[0] = EE_READ; // read the ID instruction

                            EE_TX_buf[1] = EE_Adr_high_byte; // High byte

                            EE_TX_buf[2] = EE_Adr_low_byte; // Low byte

                            EE_TX_buf[3] = EE_PAD_BYTE; // pad for data collection

                         

                         

                            SPI_SpiUartPutArray(EE_TX_buf,4);

                         

                            while(SPI_SpiUartGetRxBufferSize() != 4);

                           

                            EE_dummy_read = SPI_SpiUartReadRxData();

                            EE_dummy_read = SPI_SpiUartReadRxData();

                            EE_dummy_read = SPI_SpiUartReadRxData();

                            EE_RX_buf[0] = SPI_SpiUartReadRxData();

                         

                         

                            READ_Value = EE_RX_buf[0];

                           

                            return READ_Value;

                        }

                        • 9. Re: SPI Driver for ST M95320 EE prom
                          rajiv.vasanth.badiger

                          You're welcome Herb. I too missed seeing the error in the edited code in the first reading.

                           

                          Also, thanks for putting the code. It will definitely help others. Can you please let us know the code example you referred? I will check if it requires any improvement.

                           

                          - Rajiv

                          • 10. Re: SPI Driver for ST M95320 EE prom
                            herb_2497906

                            The SPI master, I get it now, but it was hard to follow.

                            Also, I have another request in the system on Apple phones losing BT connection in 30 seconds, Android stays connected, please look into that one ASAP.

                             

                            Herb