SPI Push / Pop Variables

AgentSmithers
Posts: 158
Joined: Sat Apr 01, 2017 1:21 am
Contact:

SPI Push / Pop Variables

Postby AgentSmithers » Sun Feb 03, 2019 9:18 am

Hi Everyone!!
I believe I have a simple error and wanted to reach out to see what it may be. It appears after the write spi_flash_write it returns success but does not return the correct values.
The whole idea of this function is to allow me to push and pop variables like booleans and char arrays.

Additionally, if anyone has a better way to implement this via flash I am totally open for suggestions!

The operations of a Push and Pop flow is FILO (FIRST IN LAST OUT), However, I also want a function similar that is FIFO.

Thank you, everyone!!!

Code: Select all

#define SPIStackAddress 0x70000
uint32 SPIStackPtr = SPIStackAddress;
typedef enum datatype
{
   EOF = 0,
   booleanTrue = 1,
   booleanFalse = 2,
   integer = 2,
   string = 3
}thedatatype;


uint32 pushSPIData(thedatatype ArgType, void * data) //returns 32bit address for L106
{   
   os_printf("Start");
   SPIStackPtr = SPIStackAddress;
   if (ArgType == booleanTrue || ArgType == booleanFalse)
   {
      spi_flash_erase_sector(SPIStackAddress);
      os_printf("Address now at: %x\r\n", SPIStackPtr);
      uint32 ArgTypeData;
      ArgTypeData = ArgType;

      os_printf("Writing: ");
      hex_printf(&ArgTypeData, sizeof(ArgTypeData));      
      os_printf("\r\n\r\n");

      SpiFlashOpResult writeResult = spi_flash_write(SPIStackPtr, (uint32 *)&ArgTypeData, sizeof(ArgTypeData));
      if (writeResult == SPI_FLASH_RESULT_OK)
      {
         os_printf("ThermSettings config written...\r\n");      
      }
      else if(writeResult == SPI_FLASH_RESULT_ERR)
      {
         os_printf("ThermSettings SPI_FLASH_RESULT_ERR...\r\n");
      }
      else if(writeResult == SPI_FLASH_RESULT_TIMEOUT)
      {
         os_printf("ThermSettings SPI_FLASH_RESULT_TIMEOUT...\r\n");
      }

      SPIStackPtr += sizeof(bool);
      os_printf("Address now at: %x\r\n", SPIStackPtr);

      char buffer[8] = {0};
      hex_printf(buffer, 8);
      if (spi_flash_read(SPIStackAddress, buffer, 8) != 0)
      {
         os_printf("Read failed at offset=0x%02x index=%d\r\n", SPIStackAddress, index);
         return 0;
      }
      
      hex_printf(buffer, 8);
      os_printf("\r\n");
      
      return writeResult;
   }
}



   pushSPIData(booleanFalse, NULL);
   pushSPIData(booleanTrue, NULL);
   pushSPIData(booleanFalse, NULL);
   pushSPIData(booleanTrue, NULL);


StartAddress now at: 70000
Writing: 02 00 00 00


ThermSettings config written...
Address now at: 70001
00 00 00 00 00 00 00 00
02 00 00 00 FF FF FF FF

StartAddress now at: 70000
Writing: 01 00 00 00


ThermSettings config written...
Address now at: 70001
00 00 00 00 00 00 00 00
00 00 00 00 FF FF FF FF

StartAddress now at: 70000
Writing: 02 00 00 00


ThermSettings config written...
Address now at: 70001
00 00 00 00 00 00 00 00
00 00 00 00 FF FF FF FF

StartAddress now at: 70000
Writing: 01 00 00 00


ThermSettings config written...
Address now at: 70001
00 00 00 00 00 00 00 00
00 00 00 00 FF FF FF FF

mode : null

AgentSmithers
Posts: 158
Joined: Sat Apr 01, 2017 1:21 am
Contact:

Re: SPI Push / Pop Variables

Postby AgentSmithers » Sun Feb 03, 2019 10:37 am

I resolved it. It was because erase_sector takes a 2 digit sector number vs. an actual address.. If you want include upgrade.h and take the address and divide it by SPI_FLASH_SEC_SIZE.

spi_flash_erase_sector(0x60000/SPI_FLASH_SEC_SIZE); =====>>>>> spi_flash_erase_sector(0x60);

Thanks anyways! =]

After doing some read's and writes it seems that SPI flash writes the LSB to the MSB of a 32bit value seems to be written in that order.. Check these examples out

Start -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Address now at: 70000
Buffer inital state = FF FF FF FF FF FF FF FF
Writing: FF FF FF 7F
After: FF FF FF 7F FF FF FF FF

Start -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Address now at: 70001
Buffer currently: FF FF FF 7F FF FF FF FF
Writing: FF FF FF BF
FF FF FF 7F BF FF FF FF

Start -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Address now at: 70002
Buffer currently: FF FF FF 7F BF FF FF FF
Writing: FF FF FF 7F
After: FF FF FF 7F BF 7F FF FF


It seems that I may need to move my Type flags to the start of the 32bits and then bitshift the values accordingly to get the correct results back.

AgentSmithers
Posts: 158
Joined: Sat Apr 01, 2017 1:21 am
Contact:

Re: SPI Push / Pop Variables

Postby AgentSmithers » Sun Feb 03, 2019 12:16 pm

Update: Got it.. I had to flip my bits so Flash writes correctly then after I read the type, shift the bits over by 3 then pull the actual value acordingly.


Start -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Address now at: 70000
Buffer00 00 00 00 00 00 00 00
FF FF FF FF FF FF FF FF
Writing: FD FF FF FF


FF FF FF FF FF FF FF FF
FD FF FF FF FF FF FF FF

Start -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Address now at: 70001
Buffer00 00 00 00 00 00 00 00
FD FF FF FF FF FF FF FF
Writing: FE FF FF FF


FD FF FF FF FF FF FF FF
FD FE FF FF FF FF FF FF

Start -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Address now at: 70002
Buffer00 00 00 00 00 00 00 00
FD FE FF FF FF FF FF FF
Writing: FD FF FF FF


FD FE FF FF FF FF FF FF
FD FE FD FF FF FF FF FF

Start -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Address now at: 70003
Buffer00 00 00 00 00 00 00 00
FD FE FD FF FF FF FF FF
Writing: FE FF FF FF


FD FE FD FF FF FF FF FF
FD FE FD FE FF FF FF FF

mode : null



I had to format my types like this..

Code: Select all

#define SPIStackAddress 0x70000
uint32 SPIStackPtr = SPIStackAddress;
typedef enum datatype
{
   COMPLETED = 0,          //00000000000000000000000000000000
   string = 0xFFFFFFFB,         //11111111111111111111111111111100
   integer = 0xFFFFFFFC,        //11111111111111111111111111111010
   booleanFalse = 0xFFFFFFFD,   //11111111111111111111111111111110
   booleanTrue = 0xFFFFFFFFE,    //11111111111111111111111111111101
   EOF =  0xFFFFFFFF,       //11111111111111111111111111111111
}thedatatype;

Who is online

Users browsing this forum: No registered users and 6 guests