Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Error compiling PUT and GET using HW SPID Solved) - Problem
Goto page 1, 2  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 314

spain.gif
PostPosted: Sat Nov 21, 2020 11:36 am    Post subject: Error compiling PUT and GET using HW SPID Solved) - Problem Reply with quote

I'm getting the following compiling error whenever PUT or GET are encountered in the code
Quote:
Error : 61 Line : 56 Label not found [SPI] ,


What is this error?

This is the example I'm testing.
Code:
'____________________________________________________________________________________
$regfile = "xm192a3def.dat"                                 ' ATxmega192A3
'____________________________________________________________________________________
$hwstack = 256
$swstack = 128
$framesize = 128
'____________________________________________________________________________________
'  CONFIG SYSTEM CLOCK
  Config Osc = Disabled , Extosc = Enabled , Range = 12mhz_16mhz , Startup = Xtal_16kclk , 32khzosc = Enabled , Pllosc = Enabled , Pllsource = Extclock , Pllmul = 2       ' 16x2=32MHz
  Config Sysclock = Pll , Prescalea = 1 , Prescalebc = 1_1  ' 32/1=32 MHz
  $crystal = 32000000
  Const Fclock = 32000000
  '____________________________________________________________________________________
   Config Priority = Static , Vector = Application , Lo = Enabled , Med = Enabled , Hi = Enabled
   Enable Interrupts
  '____________________________________________________________________________________
  Config Com1 = 230400 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
  Config Serialout0 = Buffered , Size = 250                 'Buffer de  TX1
  Open "COM1:" For Binary As #1
  '____________________________________________________________________________________
  Dim Sd_card_ready As Byte
  Sd_card_ready = 0
  Dim Btemp1 As Byte
  Dim File_name_tmp As String * 12
  '____________________________________________________________________________________
'  INI SD CARD
   $include "Config_MMCSD_HC.inc"
   If Gbdriveerror = 0 Then
      $include "Config_AVR-DOS.inc"
      Btemp1 = Initfilesystem(1)
      If Btemp1 = 0 Then
         Sd_card_ready = 1
         Print #1 , " SD Card OK"
         Print #1 , "Filesystem = " ; Gbfilesystem
      End If
   End If
 '____________________________________________________________________________________
 ' DIR SD CARD
   If Sd_card_ready = 1 Then
      File_name_tmp = Dir( "*.*")
      While Len(file_name_tmp) > 0                          ' if there was a file found
         Print #1 , File_name_tmp ; "  " ; Filedate() ; "  " ; Filetime() ; "  " ; Filelen()
         File_name_tmp = Dir()                              ' get next
      Wend
   End If
 '____________________________________________________________________________________
'  WRITE BINARY INTO THE SD CARD
   Dim B As Byte , W As Word , L As Long , Sn As Single
   Dim Stxt As String * 10
   B = 1 : W = 50000 : L = 12345678 : Sn = 123.45 : Stxt = "HOLA"
   Print #1 , "B=" ; B ; " W=" ; W ; " L=" ; L ; " Sn=" ; Sn ; " Stxt=" ; Stxt

   If Sd_card_ready = 1 Then
      Open "test.bin" For Binary As #11
      Put #11 , B                                           ' write a byte
      Close #11
   End If

 '____________________________________________________________________________________
 End


If I remove this line
Code:
Put #11 , B
then it compiles and it works. All files in the SD CARD are printed
Note: I'm using hardware SPID so I had to make the following changes:
    In Config_MMCSD_HC.inc:


Code:
       Portd_pin6ctrl = &B00_011_000                        ' MISO pin pull up
       Config Pind.4 = Output                               ' define here Pin for CS of MMC/SD Card
       Mmc_cs Alias Portd.4
       Set Mmc_cs
      ' Define here SS Pin of HW-SPI of the CPU (f.e. Pinb.0 on M128)
       Spi_ss Alias Portd.4
       'SPI Configuration for XMEGA
       'The MMC-XMEGA.LIB expect SPIC
       'If you want to use SPID, SPIE or SPIF you NEED TO CHANGE THE MMC-XMEGA.LIB
       'It is documented in MMC-XMEGA.LIB where you need to change the Library
       Config Spid = Hard , Master = Yes , Mode = 0 , Clockdiv = Clk2 , Data_order = Msb
       Open "SPID" For Binary As #11
       Const _mmc_spi = Spid_ctrl
 


    In MMC-XMEGA.LIB as indicated, after the line

Quote:
_byte2spi_255:


(BASCOM-AVR version : 2.0.8.3 )


Last edited by hzz on Tue Nov 24, 2020 10:49 pm; edited 1 time in total
Back to top
View user's profile
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 314

spain.gif
PostPosted: Sat Nov 21, 2020 10:07 pm    Post subject: SOLVED? Reply with quote

It seems that the file handle in PUT and GET cannot be a numeric value.

Code:
PUT #2, Byte
gives a compiler error despite this is the BASCOM example

Code:
file_handle = 2
PUT #file_handle, Byte
doesn't give a compiler error
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5916
Location: Holland

blank.gif
PostPosted: Sun Nov 22, 2020 2:12 pm    Post subject: Reply with quote

you need some coffee Very Happy

first of all (or is that second now?) : please always include the files in a ZIP. So that it can be tested without additional assumptions and work.
then did you check this : Open "SPID" For Binary As #11

you open the SPI which is fine for #11

And that will remain open all the time.
So you can not expect that this : Open "test.bin" For Binary As #11
will work.
Why do you assume that will work? And why not using a unique number?

also :
When you use AVR-DOS you can have a unique file number on the fly. Using Freefile() function.

_________________
Mark
Back to top
View user's profile Visit poster's website
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 314

spain.gif
PostPosted: Sun Nov 22, 2020 7:04 pm    Post subject: Reply with quote

Thanks Mark, I do need a coffe!

Code:
Open "SPID" For Binary As #11

I took "Config_MMCSD_HC" from the BASCOM AVR-DOS samples and just changed SPIC to SPID everywhere without even noticing that this file is left open and that I was using the same file number in my code.

Correcting this, using a diferent file number in my code, PUT and GET can be used with a constant for file number, as expected.

Still I wonder if this Open "SPID" For Binary As #11 is needed in Config_MMCSD_HC and where should it be closed ¿!?
Back to top
View user's profile
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 314

spain.gif
PostPosted: Mon Nov 23, 2020 1:12 am    Post subject: Reply with quote

I must heve something else wrong, the behavior is strange. For instance, Seek doesn't work in the following example. Atached is a zip file including also the "Config_MMCSD_HC.inc" and "Config_AVR-DOS.inc" files used in the example

Code:
'____________________________________________________________________________________
$regfile = "xm192a3def.dat"                                 ' ATxmega192A3
'____________________________________________________________________________________
$hwstack = 256
$swstack = 128
$framesize = 128
'____________________________________________________________________________________
'  CONFIG SYSTEM CLOCK
  Config Osc = Disabled , Extosc = Enabled , Range = 12mhz_16mhz , Startup = Xtal_16kclk , 32khzosc = Enabled , Pllosc = Enabled , Pllsource = Extclock , Pllmul = 2       ' 16x2=32MHz
  Config Sysclock = Pll , Prescalea = 1 , Prescalebc = 1_1  ' 32/1=32 MHz
  $crystal = 32000000
  Const Fclock = 32000000
  '____________________________________________________________________________________
   Config Priority = Static , Vector = Application , Lo = Enabled , Med = Enabled , Hi = Enabled
   Enable Interrupts
  '____________________________________________________________________________________
   Const Bps_com1 = 230400
   Config Com1 = Bps_com1 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
'   Config Serialout0 = Buffered , Size = 250                'Buffer de  TX1
   Open "COM1:" For Binary As #1
   '____________________________________________________________________________________
  Dim Sd_card_ready As Byte
  Sd_card_ready = 0
  Dim Btemp1 As Byte
  Dim File_name_tmp As String * 12
  Dim File_number As Byte
   Dim P_file_handle As Byte
   Dim File_name1 As String * 12
   Dim Ltemp As Long

' -------------------------------------------------------------------------------------------------------------------------------------------
  '____________________________________________________________________________________
   Wait 2
   Print #1 ,
   Print #1 , "Test SD Card"
  '____________________________________________________________________________________
'  INI SD CARD
   $include "Config_MMCSD_HC.inc"
 '____________________________________________________________________________________
   If Gbdriveerror = 0 Then
      $include "Config_AVR-DOS.inc"
      Btemp1 = Initfilesystem(1)
      If Btemp1 = 0 Then
         Sd_card_ready = 1
         Print #1 , "SD Card OK"
         Print #1 , "Filesystem = " ; Gbfilesystem
      End If
   End If
 '____________________________________________________________________________________
   If Sd_card_ready = 1 Then
'     Open File
      P_file_handle = Freefile()
      File_name1 = "Test10.bin"                             '**************** PUT HERE THE DESIRED FILE NAME
      Open File_name1 For Binary As #p_file_handle

'     Set starting at position =20
       Ltemp = 20
       Seek #p_file_handle , Ltemp

'      Write data at position 20
      Btemp1 = 33
      Put #p_file_handle , Btemp1
                                                Print #1 , "Data_byte pos=" ; Loc(#p_file_handle)       ' This must be  20    but it is 1 ¿?
'     -----------------------------------------------------------------
'     Save data to SD Card and close file
'      Flush #p_file_handle           ' flush to disk    (This is not needed. The data is already flushed when Closing the file)
      Close #p_file_handle
'     -----------------------------------------------------------------
   End If
 '____________________________________________________________________________________
 End


I Expected to print:
Quote:
Test SD Card
SD Card OK
Filesystem = 11 (Idon't know what this number means)
Data_byte pos=20

But it prints:
Quote:
Test SD Card
SD Card OK
Filesystem = 11
Data_byte pos=1
Back to top
View user's profile
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 314

spain.gif
PostPosted: Tue Nov 24, 2020 12:47 am    Post subject: Reply with quote

I have moved the subject to AVR_DOS
https://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&p=79670#79670
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Tue Nov 24, 2020 2:21 am    Post subject: Reply with quote

I think you miss some basics.
If you have new file which is always binary but with sometimes with another extension then its len depend on how many bytes was entered previously which is NULL/NADA/ZERO Very Happy
So how you want to point into byte at address 20?

I can tell you that even if you make a empty file "test.txt" on the PC and then search that file with AVR like :
Code:
Dim MyLong As Long
MyLong = Filelen("test.txt")
If MyLong > 0 Then
 'you dont find nothing
End If

You dont find this file because it dont contain one single byte....So how to point into byte No. 20?

Please put some 20 Test bytes like that, then point into 19 and put one more byte. Then, I think Filelen will be still 20 cause then Seek will be working.
Code:
For N = 1 to 20
 Put #card , N
Next
Seek #card , 19
Put #card , N
Print Filelen("example.bin") 'should sill be 20 cause 19+new one

So you should use Filelen first and then point into some existing byte.

Cause im dealing with AVR-DOS often then I can give you one more tip.
If you have array of bytes then you cant put only one byte from the array directly.
A whole array will be written into the file from pointer you choose till the array end.
If you want to write only one byte from it you must use some temporary help byte.
Code:
Dim MyArray(10) As Byte
Put #card , MyArray(4)  'this will put seven bytes

Helpb = MyArray(4)
Put #card , Helpb 'this will put MyArray(4) Byte only
Back to top
View user's profile Visit poster's website
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 314

spain.gif
PostPosted: Tue Nov 24, 2020 2:49 am    Post subject: Reply with quote

Thanks!
I thought I could just start writing anywhere in the file and leave empty space at the begining to put a header later. I missed some basics indeed!
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5916
Location: Holland

blank.gif
PostPosted: Tue Nov 24, 2020 9:25 am    Post subject: Reply with quote

from the help for SEEK: In QB/VB you can use seek to make the file bigger. When a file is 100 bytes long, setting the file pointer to 200 will increase the file with 0 bytes. By design this is not the case in AVR-DOS.
_________________
Mark
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5916
Location: Holland

blank.gif
PostPosted: Tue Nov 24, 2020 10:02 am    Post subject: Reply with quote

One thing i forgot to answer : the OPEN was and is just a way so the compiler knows which handles belong to a device.
So there is no need to close them. In fact you should not close them.
The only exception is AVR-DOS which uses real dynamic file handles. Here the CLOSE will actually flush buffers and close the file handle so it can be re-opened later.

_________________
Mark
Back to top
View user's profile Visit poster's website
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 314

spain.gif
PostPosted: Tue Nov 24, 2020 10:32 am    Post subject: Reply with quote

OK.
Although I'm not sure to get it. Shouldn't this code print Data_byte at pos20 is 20?

Code:
If Sd_card_ready = 1 Then
      P_file_handle = Freefile()
      File_name1 = "Test13.bin"
      Open File_name1 For Binary As #p_file_handle

      For Btemp1 = 1 To 255
         Put #p_file_handle , Btemp1                        ' Fill first 255 positions
      Next Btemp1

      Flush #p_file_handle                                  ' Write data to SD card

       Ltemp = 20
       Seek #p_file_handle , Ltemp                          '     Set file pointer at position =20
       Get #p_file_handle , Btemp1                          ' Get the byte at position 20, which should be =20

       Print #1 , "Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1

       Close #p_file_handle
   End If


However it prints Data_byte at pos19 is 0
What is wrong?
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5916
Location: Holland

blank.gif
PostPosted: Tue Nov 24, 2020 12:47 pm    Post subject: Reply with quote

yes you are right. only a close/reopen will let SEEK statement work correct.
i will have a look at it. i dont know if this is by design or a bug.

_________________
Mark
Back to top
View user's profile Visit poster's website
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 314

spain.gif
PostPosted: Tue Nov 24, 2020 2:56 pm    Post subject: Reply with quote

Code:
   If Sd_card_ready = 1 Then
'     ------------------------------------------------------------------------------------
      P_file_handle = Freefile()
      File_name1 = "Test14.bin"
      Open File_name1 For Binary As #p_file_handle
'     ------------------------------------------------------------------------------------
      For Btemp1 = 1 To 255
         Put #p_file_handle , Btemp1                        ' Fill first 255 positions
      Next Btemp1
      Ltemp = 20
      Seek #p_file_handle , Ltemp                           '     Set file pointer at position =20
      Get #p_file_handle , Btemp1                           ' Get the byte at position 20, which should be =20
      Print #1 , "Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1       ' Result:   Data_byte at pos20 is 20
'     --------------------------------------
      Ltemp = 40
      Seek #p_file_handle , Ltemp                           '     Set file pointer at position =40
      Btemp1 = 100
      Put #p_file_handle , Btemp1,                          ' put the value 100 at position 40
      Ltemp = 40
      Seek #p_file_handle , Ltemp                           '     Set file pointer at position =40
      Get #p_file_handle , Btemp1                           ' Get the byte at position 40, which should be =100
      Print #1 , "Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1       '
'     --------------------------------------
      Close #p_file_handle
'     --------------------------------------
   End If

the program above prints
Quote:
Test SD Card
SD Card OK
Filesystem: 11
Data_byte at pos19 is 0
Data_byte at pos40 is 100

which is Not correct

If I just chage the filename to Test13.bin then it prints:
Quote:
Test SD Card
SD Card OK
Filesystem: 11
Data_byte at pos20 is 20
Data_byte at pos40 is 100

which is correct
Both files Test13.bin and Test14.bin have been created with the program and in both cases the files existed in the SD card before running the program ¿Why the behavior is different?


if I change the program to close Test14.bin before the Seek then the proigram works with Test14.bin also ¿?

Code:
   If Sd_card_ready = 1 Then
'     ------------------------------------------------------------------------------------
      P_file_handle = Freefile()
      File_name1 = "Test14.bin"
      Open File_name1 For Binary As #p_file_handle
'     ------------------------------------------------------------------------------------
      For Btemp1 = 1 To 255
         Put #p_file_handle , Btemp1                        ' Fill first 255 positions
      Next Btemp1

      Close #p_file_handle                                  ' We need to close the file in order for SEEK to work
      Open File_name1 For Binary As #p_file_handle

      Ltemp = 20
      Seek #p_file_handle , Ltemp                           '     Set file pointer at position =20
      Get #p_file_handle , Btemp1                           ' Get the byte at position 20, which should be =20
      Print #1 , "Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1       ' Result:   Data_byte at pos20 is 20
'     --------------------------------------
      Ltemp = 40
      Seek #p_file_handle , Ltemp                           '     Set file pointer at position =40
      Btemp1 = 100
      Put #p_file_handle , Btemp1,                          ' put the value 100 at position 40
      Ltemp = 40
      Seek #p_file_handle , Ltemp                           '     Set file pointer at position =40
      Get #p_file_handle , Btemp1                           ' Get the byte at position 40, which should be =100
      Print #1 , "Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1       '
'     --------------------------------------
      Close #p_file_handle
'     --------------------------------------
   End If


If I remove the two lines
Code:
      Close #p_file_handle                                  ' We need to close the file in order for SEEK to work
      Open File_name1 For Binary As #p_file_handle
 


then the program does not work with Test14.bin but it works with Test13.bin

I have tested it with a different SD Card creating both files with exactly the same code. Then the behavior is the same for both files. In none of them SEEK works if the file is not closed before calling SEEK.
So I wonder what did I do to Test13.bin in the first card that didn't need the file to be closed for SEEK to work.

Also, Do we need to close the file each time SEEK is going to be called?
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5916
Location: Holland

blank.gif
PostPosted: Tue Nov 24, 2020 3:08 pm    Post subject: Reply with quote

As i wrote before the SEEK statement seems to require that the file is closed.
Or with other words, SEEK statement will only work when the file does not change in size.
When an existing file is opened it will work. when you use it to relocate the position after writing data, it will not work.
While the SEEK by design will not let the file grown when you position the file pointer beyond the end, i do not know why it does not update using a FLUSH or after writing data.
it is probably by design but it might be a bug. i will check it later and either document it or change it.

_________________
Mark
Back to top
View user's profile Visit poster's website
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 314

spain.gif
PostPosted: Tue Nov 24, 2020 4:56 pm    Post subject: Reply with quote

But SEEK does work without closing the file after writing data as in this example:
Code:
   If Sd_card_ready = 1 Then
'     ------------------------------------------------------------------------------------
      P_file_handle = Freefile()
      File_name1 = "Test14.bin"
      Open File_name1 For Binary As #p_file_handle
'     ------------------------------------------------------------------------------------
      For Btemp1 = 1 To 255
         Put #p_file_handle , Btemp1                        ' Fill first 255 positions
      Next Btemp1
      Close #p_file_handle                                  ' We need to close the file in order for SEEK to work
'     --------------------------------------
      Open File_name1 For Binary As #p_file_handle
       Ltemp = 20
      Seek #p_file_handle , Ltemp                           '     Set file pointer at position =20
      Get #p_file_handle , Btemp1                           ' Get the byte at position 20, which should be =20
      Print #1 , "Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1       ' Result::   Data_byte at pos20 is 20  OK
'     --------------------------------------
      Ltemp = 40
      Seek #p_file_handle , Ltemp                           '     Set file pointer at position =40
      Btemp1 = 100 : Put #p_file_handle , Btemp1,           ' put the value 100 at position 40
      Btemp1 = 101 : Put #p_file_handle , Btemp1,           ' put the value 101 at position 41
      Btemp1 = 102 : Put #p_file_handle , Btemp1,           ' put the value 102 at position 42

'     With the previous PUT's we have changed the pointer without closing  the file so the following SEEK should not work

      Ltemp = 50
      Seek #p_file_handle , Ltemp                           '     Set file pointer at position =50
      Get #p_file_handle , Btemp1                           ' Get the byte at position 50, which should be =50
      Print #1 , "Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1       ' Result    Data_byte at pos50 is 50  OK

      Ltemp = 40
      Seek #p_file_handle , Ltemp                           '     Set file pointer at position =40
      Get #p_file_handle , Btemp1                           ' Get the byte at position 40, which should be =100
      Print #1 , "Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1       ' Result    Data_byte at pos40 is 100       OK
      Get #p_file_handle , Btemp1                           ' Get the byte at position 41, which should be =101
      Print #1 , "Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1       ' Result    Data_byte at pos41 is 101        OK
      Get #p_file_handle , Btemp1                           ' Get the byte at position 42, which should be =102
      Print #1 , "Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1       ' Result    Data_byte at pos42 is 102       OK

'     --------------------------------------
      Close #p_file_handle
'     --------------------------------------
   End If
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum