Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Strange behavior with TWIxslave

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
Tiny

Bascom Member



Joined: 10 Nov 2010
Posts: 84
Location: The Netherlands

netherlands.gif
PostPosted: Tue Feb 12, 2019 3:03 pm    Post subject: Strange behavior with TWIxslave Reply with quote

I'm busy with making a i2c boot loader for the atxmega32a4u.

When the master sends data to the slave, everything's is fine until the "Twic_btw" counter is reaching the value of 56. After that on the next byte that is received the "Twic_btw" counter get the value of the "twic" + 1

Example

Twic: 113 Twic_btw : 53
Twic: 224 Twic_btw : 54
Twic: 112 Twic_btw : 55
Twic: 147 Twic_btw : 56
Twic: 98 Twic_btw : 148
Twic: 6 Twic_btw : 149
Twic: 114 Twic_btw : 150
Twic: 224 Twic_btw : 151


Regard Tiny

below is my testcode
Code:

'*******************************************************************************
'Compiler 2.0.8.1.000
'
'Cpu        : AtxMega32a4u
'crystal    : 8Mhz external
'eprom size 1024 byte's
'
'Fuses:
'
'  WDWP     = 8CLK
'  WDP      = 8CLK
'  BOOTRST  = BOOTLDR
'  TOSCSEL  = XTAL
'  BODPD    = DISABLED
'  RSTDISBL = [unchecked]
'  SUT      = 4MS
'  WDLOCK   = [unchecked]
'  BODACT   = CONTINUOUS
'  EESAVE   = [unchecked]
'  BODLVL   = 2V4
'
'*******************************************************************************

$regfile = "xm32a4udef.dat"
$crystal = 8000000
$hwstack = 120                                              'setup Stacks
$swstack = 120
$framesize = 120
'$loadersize = 4096                                          ' bootloader starts after the application

'*******************************************************************************
'  constanten
'*******************************************************************************

Config_constanten:

   Config Submode = New
   Const Buildnumber = " 1.0.0.0"
   Const Debugoutput = 1

'*******************************************************************************
'  Config i/o
'*******************************************************************************

Config_io:

'  Config Porta.0 =                                                                                  '
'  Config Porta.1 =
'  Config Porta.2 =
   Config Porta.3 = Output
   Config Porta.4 = Output
'  Config Porta.5 =
   Config Porta.6 = Input
   Config Porta.7 = Input

'  Config Portb.0 =
'  Config Portb.1 =
'  Config Portb.2 =                                                                              '
'  Config Portb.3 =

   Config Portc.0 = Output                                  'SDA
   Config Portc.1 = Input                                   'SCL
   Config Portc.2 = Input
   Config Portc.3 = Output                                  'TXD Debug
   Config Portc.4 = Input
   Config Portc.5 = Input
'  Config Portc.6 =
'  Config Portc.7 =

   Config Portd.0 = Input
   Config Portd.1 = Input
'  Config Portd.2 =
   Config Portd.3 = Output                                  'Led 1
   Config Portd.4 = Output                                  'Led 2
   Config Portd.5 = Output                                  'Led 3
'  Config Portd.6 =
'  Config Portd.7 =

'  Config Porte.0 =
'  Config Porte.1 =
'  Config Porte.2 =
'  Config Porte.3 =

'  Config Portr.0 =
'  Config Portr.1 =

'*******************************************************************************
'  Config dim
'*******************************************************************************

Config_dim:

   Dim I2c_receivebyte(10) As Byte
   Dim I2c_receiverstop As Bit

'*******************************************************************************
'  Config alias
'*******************************************************************************

Config_alias:

   Led1 Alias Portd.3
   Led2 Alias Portd.4
   Led3 Alias Portd.5

'*******************************************************************************
'  Config Clock
'  Enable the external oscillator with Range 2MHz....9MHz
'*******************************************************************************

Config_clock:

   Config Osc = Disabled , Extosc = Enabled , Range = 2mhz_9mhz , Startup = Xtal_256clk
   Config Sysclock = External , Prescalea = 1 , Prescalebc = 1_1

'*******************************************************************************
'   Config timer 0 as 100mS ticker
'
'       1
'   ----------
'   (8MHz/256) = 0.00032 x 3125  --> 100mSecOne Tick
'   Setup overflow interrupt of Timer/Counter C0 and
'   Enable overflow interrupt in LOW Priority
'
'*******************************************************************************

Config_tcc0:

   Config Tcc0 = Normal , Prescale = 256
   Tcc0_per = 3125
   On Tcc0_ovf Tc0_isr
   Enable Tcc0_ovf , Lo

'*******************************************************************************
'  Config compoort
'*******************************************************************************

Config_comport:

   Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8

   Open "COM1:" For Binary As #1

'*******************************************************************************
'  Config eeprom
'*******************************************************************************

Config_eeprom:

   Config Eeprom = Mapped

'*******************************************************************************
'  Configure TWI Slave hardware registers. Base address is 90 but we twiddle
'  the TWAR register if we want a secondary address
'*******************************************************************************

Config_twi:

   Config Scl = Portc.1                                     ' we need to provide the SCL pin name
   Config Sda = Portc.0                                     ' we need to provide the SDA pin name

   Dim Twi_start As Byte , B As Byte
   Config Twicslave = &H46 , Btr = 2                        'use address &H70 which is


'*******************************************************************************
'  Config Defaults
'*******************************************************************************

Config_defaults:


'*******************************************************************************
'  Config interrupts
'*******************************************************************************

Config_interupts:

   Config Priority = Static , Vector = Application , Lo = Enabled , Med = Enabled , Hi = Enabled

   Enable Interrupts                                        'TWIC needs interrupts

'*******************************************************************************
' Enter main loop
'*******************************************************************************

Main:


   Set Led2
   Reset Led3
   Dim Tempb As Byte
   Tempb = &HFF


   Dim Tt As Byte

   Do
      If I2c_receiverstop = 1 Then                          'all data is received
         #if Debugoutput = 1
            Print #1 , "recieve completed"
         #endif
         I2c_receiverstop = 0

         Select Case I2c_receivebyte(1)

            Case 10:

            Case 11:

            Case 12:

            Case 13:

         End Select

      End If


   Loop


   End

'*******************************************************************************
' Timer 0 isr routine
'*******************************************************************************

Tc0_isr:

   Toggle Led1
Return


'*******************************************************************************
'   TWIC routines
'*******************************************************************************


Twi_stop_rstart_received:                                   'Single byte commands
   #if Debugoutput = 1
      Print #1 , "Master sent stop or repeated start"
   #endif
   Set I2c_receiverstop

Return

Twi_addressed_goread:
   #if Debugoutput = 1
      Print #1 , "We were addressed and master will send data"
   #endif
Return

Twi_addressed_gowrite:
   #if Debugoutput = 1
      Print #1 , "We were addressed and master will read data"
   #endif
Return

Twi_gotdata:
   #if Debugoutput = 1
      Print #1 , "Twic: " ; Twic ; " Twic_btw : " ; Twic_btw
   #endif
   I2c_receivebyte(twic_btw) = Twic
Return

Twi_master_needs_byte:
   #if Debugoutput = 1
      Print #1 , "Master needs byte : " ; Twic_btr
   #endif
   Twic = 56
Return

Twi_master_need_nomore_byte:
   #if Debugoutput = 1
      Print #1 , "Master does not need anymore bytes"
   #endif
Return



End
 


(BASCOM-AVR version : 2.0.8.1 )
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1635

blank.gif
PostPosted: Tue Feb 12, 2019 9:00 pm    Post subject: Re: Strange behavior with TWIxslave Reply with quote

Tiny wrote:
I'm busy with making

A bit too busy it seems.

Does that give you some hint?
Code:
Twic: 147 Twic_btw : 56
' ...
Dim I2c_receivebyte(10) As Byte
' ...
I2c_receivebyte(twic_btw) = Twic

If it's the game of 'catch the bug in lengthy code', you certainly have a point. Very Happy
Back to top
View user's profile
Tiny

Bascom Member



Joined: 10 Nov 2010
Posts: 84
Location: The Netherlands

netherlands.gif
PostPosted: Tue Feb 12, 2019 9:36 pm    Post subject: Reply with quote

the dim array declaration is to small Embarassed, but the "Print #1 , "Twic: " ; Twic ; " Twic_btw : " ; Twic_btw" command shows the error.


Code:

Twi_gotdata:
   #if Debugoutput = 1
      Print #1 , "Twic: " ; Twic ; " Twic_btw : " ; Twic_btw
   #endif
   I2c_receivebyte(twic_btw) = Twic
Return
 
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1635

blank.gif
PostPosted: Tue Feb 12, 2019 10:01 pm    Post subject: Reply with quote

Tiny wrote:
but the "Print #1 , "Twic: " ; Twic ; " Twic_btw : " ; Twic_btw" command shows the error.

You can do forensic examinations of why an overwritten memory range will produce a certain fault condition, which is then shown via print.
It actually can be done to find out exactly why this happens, you need to check which variables are affected and watch for interactions.
But this may serve academic purposes only.
Better fix the too small array and report if the effect is gone.
Back to top
View user's profile
Tiny

Bascom Member



Joined: 10 Nov 2010
Posts: 84
Location: The Netherlands

netherlands.gif
PostPosted: Tue Feb 12, 2019 10:04 pm    Post subject: Reply with quote

I will fix the small array and check it tomorrow
Back to top
View user's profile Visit poster's website
Tiny

Bascom Member



Joined: 10 Nov 2010
Posts: 84
Location: The Netherlands

netherlands.gif
PostPosted: Wed Feb 13, 2019 7:53 am    Post subject: Reply with quote

Thanks MWS,

It was the to small array declaration. Embarassed
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
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