Flash2 Click - flash2_HighSpeedRread malfunction

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
Area51
Posts: 19
Joined: 27 Feb 2016 09:26

Flash2 Click - flash2_HighSpeedRread malfunction

#1 Post by Area51 » 05 Feb 2020 18:50

Hi all,

that's my Hw configuration :

- EasyPICPro V7 with :
- 18F87K22 @ 64 MHz clock
- EasyTFT (ILI9341)
- Flash2 Click on MikroBUS port 1
- IDE : Mikrobasic 7.1 running on Xp PC (can't use W10 for now), newer version of MB malfunctions on XP

My final scope is to store .bmp images to external Flash (SST26VF064B) so I installed the Flash2 module
'cause the 128KB of th 87K22 isn't enough.

I'm really new in handling external Flash, so, after lerning the SST26VF064B datasheet, I wrote a test program
embedded in the TFT project.

Here the code for Flash2 :

Code: Select all

module Click_Flash_2_config

include Icon_Resources
include Serial
include TFT_Procedures

dim     RdData      as Byte[256]

typedef uint8_t     as byte
typedef int8_t      as short
typedef uint16_t    as word
typedef int16_t     as integer
typedef uint32_t    as longword
typedef int32_t     as longint
typedef T_FLASH2_P  as ^const uint8_t

const _FLASH2_SPI_CFG as uint32_t[4] = (_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH)  ' 16 MHz SPI Clock
'const _FLASH2_SPI_CFG as uint32_t[4] = (_SPI_MASTER_OSC_DIV16, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH) '  4 MHz SPI Clock
'const _FLASH2_SPI_CFG as uint32_t[4] = (_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH) '  1 MHz SPI Clock

sub procedure Fl2_Init()
sub procedure Fl_to_Fl_Wr_Rd()
sub procedure Flash_Rd_256(dim Addr as LongWord)

implements

sub procedure Fl2_Init()
    mikrobus_gpioInit(_MIKROBUS1, _MIKROBUS_CS_PIN, _GPIO_OUTPUT)
    mikrobus_gpioInit(_MIKROBUS1, _MIKROBUS_PWM_PIN, _GPIO_OUTPUT)
    mikrobus_gpioInit(_MIKROBUS1, _MIKROBUS_RST_PIN, _GPIO_OUTPUT)
    mikrobus_spiInit(_MIKROBUS1, @_FLASH2_SPI_CFG[0])
    Delay_ms(100)
    flash2_spiDriverInit(T_FLASH2_P(@_MIKROBUS1_GPIO), T_FLASH2_P(@_MIKROBUS1_SPI))
    flash2_init()
    Delay_ms(300)
    flash2_globalBlockUnlock()
    Delay_ms(400)
end sub

sub procedure Fl_to_Fl_Wr_Rd()   ' Write/Read test routine (From PIC Flash constant-code to Flash2 memory)
dim i          as Byte
dim r          as Byte
dim j          as Byte
dim Loops      as Byte
dim Cicles     as Byte
dim Last       as Byte
dim Byte_Cnt   as Byte
dim ChsmR      as Byte
dim ChsmW      as Byte
dim Buff       as Byte[256]
dim Src_Addr   as Word
dim Image_Size as Word
dim Fl_Addr    as LongWord

   Fl_Addr = 0x10000
'   Flash2_BlockErase(Fl_Addr)    ' 64kB erase (Block = 8 x 4kB sectors from 0x10000)
   Flash2_SectorErase(Fl_Addr)   '  4kB erase (Sector) [8k => 2 x Sector_Erase : (Fl_Addr & Fl_Addr + 0x1000)]
   Image_Size = 3018
   Cicles = Image_Size / 256     ' Number of loops
   Last = Image_Size mod 256     ' Division reminder
   Src_Addr = 0
   ChsmR    = 0
   ChsmW    = 0
   Byte_Cnt = 255
   if Last > 0 then   ' (If Image_Size / 256) > 0 then add a loop
      Loops = Cicles
   else
      Loops = Cicles - 1
   end if
' Read image data from PIC Flash (Constant-code)
   for i = 0 to Loops
      for r = 0 to Byte_Cnt
         Buff[r] = Bright_50_On2_bmp[r + Src_Addr]
         ChsmR = ChsmR xor Buff[r]
         UART1_Write(Buff[r])                                  ' DEBUG
      next r
      UART1_Write(0xFE)                                        ' DEBUG
      ByteToStr(ChsmR, DebugB)                                 ' DEBUG
      ltrim (DebugB)                                           ' DEBUG
      WordToStr(Src_Addr, DebugW)                              ' DEBUG
      ltrim (DebugW)                                           ' DEBUG
      UART1_Write_text ("Buffer:" + DebugW + "-End-ChsmR=>" + DebugB + "============================" + char (0xFE)) ' DEBUG
' Write image data to Flash2
      flash2_write(Fl_Addr, @Buff[0], (Byte_Cnt + 1))
' Read image data from Flash2
      flash2_read(Fl_Addr, @rdData[0], 256)
'      flash2_HighSpeedRread(Fl_Addr, @RdData[0], 256)  ' not Working - data mismatch and subsequent Chsm error with any SPI clock!!!
      for j = 0 to Byte_Cnt
         ChsmW = ChsmW xor RdData[j]
         UART1_Write(RdData[j])                                ' DEBUG
      next j
      UART1_Write(0xFE)                                        ' DEBUG
      ByteToStr(ChsmW, DebugB)                                 ' DEBUG
      ltrim (DebugB)                                           ' DEBUG
      LongWordToHex(Fl_Addr, DebugLH)                          ' DEBUG
      ltrim (DebugLH)                                          ' DEBUG
      UART1_Write_text ("Flash:" + DebugLH + "-End-ChsmW=>" + DebugB + "============================" + char (0xFE)) ' DEBUG
      if ChsmR <> ChsmW then                                   ' DEBUG
         UART1_Write_text ("Checksum_Error!" + char (0xFE))    ' DEBUG
      else                                                     ' DEBUG
         UART1_Write_text ("Checksum_OK!" + char (0xFE))       ' DEBUG
      end if                                                   ' DEBUG
      if (i = Loops - 1) and (Last > 0) then
         Byte_Cnt = (Last - 1)
      end if
      Src_Addr = Src_Addr + 256
      Fl_Addr = Fl_Addr + 256
      ChsmR = 0
      ChsmW = 0
   next i
end sub

sub procedure Flash_Rd_256(dim Addr as LongWord)
dim i as Byte
   flash2_read(Addr, @RdData[0], 256)
   for i = 0 to 255           ' DEBUG
      UART2_Write(RdData[i])  ' DEBUG
   next i                     ' DEBUG
   UART2_Write(0x0A)          ' DEBUG
end sub

end.
The "Fl_to_Fl_Wr_Rd" proc, simply reads the bitmap stored in the PIC Flash as

Code: Select all

const Bright_50_On2_bmp as byte[3018] = (
' 0    1    2    3    4    5    6    7    8    9   0A   0B   0C   0D   0E   0F          ' Loop 0
0x00,0x08,0x32,0x00,0x32,0x00,0xFF,0xFF,0x00,0x00,0x61,0x08,0xE3,0x18,0x25,0xFF,  
0x20,0x00,0xE0,0xD5,0x82,0x10,0x20,0x52,0x20,0xFF,0x41,0x08,0x45,0x29,0x86,0x31,
0x24,0x21,0xC3,0x18,0xA2,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, ...) code
... and then write it to Flash2 Click in 256Byte blocks.
There's some "DEBUG" lines used to send "Read and Write" data to the UART1 to check if errors, also a simple XOR based Checksum
is included.

After presentations :-) ... here the problem :

Code: Select all

      flash2_read(Fl_Addr, @rdData[0], 256)
'      flash2_HighSpeedRread(Fl_Addr, @RdData[0], 256)  ' not Working - data mismatch and subsequent Chsm error with any SPI clock!!!
If I try to use "Flash2_HighSpeedRread()" read function, data read from Flash2 (after writing) is corrupted.

Initially I supposed the SPI speed I set was too High, so I downgraded it to the minimum (_SPI_MASTER_OSC_DIV64 => 1 Mhz I think) ...

Code: Select all

'const _FLASH2_SPI_CFG as uint32_t[4] = (_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH)  ' 16 MHz SPI Clock
'const _FLASH2_SPI_CFG as uint32_t[4] = (_SPI_MASTER_OSC_DIV16, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH) '  4 MHz SPI Clock
const _FLASH2_SPI_CFG as uint32_t[4] = (_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH) '  1 MHz SPI Clock
But nothing changed, data read is always corrupted ... if I use the "flash2_read()" instead of "flash2_HighSpeedRread()" function,
everything is OK, also at maximum SPI speed.

Now, here my questions :

1) Where I'm wrong with "Flash2_HighSpeedRread()" ?
2) Which is the maximum speed I can obtain on the SPI with my 64 MHz CPU Clock ?
_SPI_MASTER_OSC_DIV4 is the maximum ?
3) Which relationship between "Flash2_read()", "Flash2_HighSpeedRread()" and SPI Clock ?

Next I need an help to create a Function to read Images from Flash2 instead of PIC Flash ... I accurately read the help about
"TFT_Set_Ext_Buffer" function but I did'n understand at all the entire thing.

Do I need to create a BUFFER where store ALL Image data Bytes read from Flash2 or just need to create some "pointer" to Flash 2
where "TFT_Ext_Image" is addressed ?
If I need to create a whole Image data BUFFER, it need a huge amount of RAM that the 87K22 doesn't have: the images I handle are (at least) 3 kB wide ... some are 8 KB ...

Does someone has a working example on how to read images from Flash2, passing them correctly to TFT routines ?

Sorry for this long post, but "Flash2 functions Help" is very "short" and I'm a little "hard-to-learn" ... not an expert !

Thanks to anyone can help, Andrea
Attachments
Click_Flash_2_config.zip
(1.54 KiB) Downloaded 82 times

User avatar
filip.grujcic
Posts: 822
Joined: 14 May 2018 08:34

Re: Flash2 Click - flash2_HighSpeedRread malfunction

#2 Post by filip.grujcic » 12 Feb 2020 11:05

Hi,

Let us continue the conversation on the ticket you submitted.

Kind regards,
Filip Grujcic

Post Reply

Return to “mikroBasic PRO for PIC General”