Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

How to use TWIy on XMEGA with MCU_REVID < 7

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

Bascom Member



Joined: 20 Oct 2009
Posts: 548

PostPosted: Mon Apr 21, 2025 2:54 pm    Post subject: How to use TWIy on XMEGA with MCU_REVID < 7 Reply with quote

Dear Forum,

Mark has documented this problem very well. In my case, I ordered 25 of these uC and, when I received them, found out they were MCU_REVID = 06. When programmed with this code, the uC simply stops responding.

Code:
'----------------------------------------------------------------
'                  (c) 1995-2021, MCS
'                      xm128-TWI.bas
'-----------------------------------------------------------------
$regfile = "XM192A3Udef.dat"
$hwstack = 64
$swstack = 40
$framesize = 40

$crystal = 32000000
Config Osc = Enabled , 32mhzosc = Enabled
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1

'NOTE" "Xmega revision: " ; Mcu_revid                   ' make sure it is 7 or higher !!! lower revs have many flaws

Dim Twi_start As Byte                                       ' you MUST dim this variable since it is used by the lib
Const _twi_stop_2 = 1

Open "TWIE" For Binary As #2                                
Config Twi = 100000                                         ' 100KHz
 
I2cinit #2

I2cstop #2

I2cstart #2                                                      'the uC "HANGS" / STOPS here. No additional code is processed
 


My question is: Is there a way to use the TWI software library routines with XMega?? Has anyone successfully modified the library to use software TWI on XMega uC? Any and all assistance would be greatly appreciated.

Code:
$lib "i2c_twi.lbx"                                         ' we do not use software emulated I2C but the TWI
Config Scl = Porte.1                                        ' we need to provide the SCL pin name
Config Sda = Porte.0                                        ' we need to provide the SDA pin name
I2cinit                                                     ' we need to set the pins in the proper state

Config Twi = 100000                                         ' wanted clock frequency when using $lib "i2c_twi.lbx"
 


As expected, this code generates errors as it intended for regular Mega AVRs.

(BASCOM-AVR version : 2.0.8.7 )
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 1126

poland.gif
PostPosted: Mon Apr 21, 2025 3:43 pm    Post subject: Reply with quote

Here you have Very Happy Happy coding Very Happy

Code:
'*****************************************************************
'*                         I2C CONFIG                            *
'*****************************************************************
   Const Softi2c = 0

#if Softi2c
 $forcesofti2c                                              ' with this the software I2C/TWI commands are used when inlcuding i2c.lbx
 $lib "i2cv2.lib"
  Config Sda = Porte.0
  Config Scl = Porte.1
  I2cinit

  Config I2cdelay = 40                                      '40 - i2c na 10kHz
' Config I2cdelay = 10                                       'should be 100kHz
#else
 Dim Twi_start As Byte
 Open "twie" For Binary As #14
 Config Twie = 10000                                        '10kHz
 I2cinit #14
#endif


Curious is that I have Xm256A3U REV-6 and Xm384C3 REV-1 and TWIE works well on them. I have this code abowe because I switch someone`s Soft_I2C code into more stable TWIE (this code have much interrupts that can do some impact to that software bitbang).

But I can tell that firstly I found mistake in the this owner hardware. There was a level shifter for 3V3 to 5V1 I2C that was soldered inverted. Soft_I2C works with this somehow but TWIE did not want to. When I fix owner hardware then it turned out to TWIE works much better than soft Very Happy

_________________
Check B-Flash -my MCS bootloader app for Android (updated)
Back to top
View user's profile Visit poster's website
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 548

PostPosted: Mon Apr 21, 2025 5:37 pm    Post subject: Reply with quote

Thank you EDC for taking the time to help. In this case there is no level shifter - the slave pressure/temperature sensor is on 3V3.

I would like to ask a favor (since I'm not so familiar with the soft I2C). This is the code I'm using:

Code:
$regfile = "XM192A3Udef.dat"
$hwstack = 64
$swstack = 40
$framesize = 40

$crystal = 32000000
Config Osc = Enabled , 32mhzosc = Enabled
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1

'NOTE" "Xmega revision: " ; Mcu_revid                   ' make sure it is 7 or higher !!! lower revs have many flaws

$forcesofti2c                                               ' with this the software I2C/TWI commands are used when inlcuding i2c.lbx
$lib "i2cv2.lib"

'Config I2cdelay = 40                                        '~10kHz
Config I2cdelay = 10                                        '~100kHz
Const _twi_stop_2 = 1

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

I2cinit                                                     ' we need to set the pins in the proper state
Waitms 10

I2crepstart
Waitms 10

I2cstop
Waitms 10

'TWI for Amphenol 442-ELVH-L04D-I-N2A4

Dim I, Twi_addr , Rawb(4) As Byte

Twi_addr = &H28
Shift Twi_addr , Left , 1
Twi_addr.0 = 1                                              'READ

Do
  For I = 1 To 4
      Rawb(i) = 0
  Next

  I2crepstart
  Waitms 1

  I2cwbyte Twi_addr
  Waitms 1

  I2crbyte Rawb(1) , Ack
  Waitms 1

  I2crbyte Rawb(2) , Ack
  Waitms 1

  I2crbyte Rawb(3) , Ack
  Waitms 1

  I2crbyte Rawb(4) , Nack
  Waitms 1

  I2cstop
'  Waitms 10

  Wait 3
Loop
End


This code receives &HFF for only the first byte receive (and only every second time it is read from the slave chip). All other values are &H00.

Thanks in advance for any your help you can offer.
E
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 1126

poland.gif
PostPosted: Mon Apr 21, 2025 6:02 pm    Post subject: Reply with quote

Im not familiar wit those sensors yet but i think your first "repstart" then "stop" causes the first exeption described in the note.
Then consistent using the "repstart" dont clears the error that can be done by normal "start" I think.
So using "repstart" in the loop I think cuses second exeption and so on...



Did you try normal "start" instead of "repstart"?

_________________
Check B-Flash -my MCS bootloader app for Android (updated)
Back to top
View user's profile Visit poster's website
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 548

PostPosted: Mon Apr 21, 2025 6:13 pm    Post subject: Reply with quote

Thank EDC.

Removing I2cREPSTART and I2cStop before the loop and using only I2cStart and I2cStop in the loop gives the same result.

If you have a minute, please consider the requirements in the section "START condition (ST)" of the datasheet. I cannot determine if this code meets those conditions.

Thanks
E
Back to top
View user's profile
_jarek_

Bascom Member



Joined: 06 Aug 2014
Posts: 9

poland.gif
PostPosted: Mon Apr 21, 2025 11:21 pm    Post subject: Reply with quote

Arrow enniom
You don't need
Code:

Const _twi_stop_2 = 1
 


you must also change the entry
Code:
Config Twi = 100000  


to
Code:
Config TwiE = 100000  


declaration running on 64A3U

Code:

config porte.0=output
config porte.1=output
dim twi_start as byte
open "twie" for binary as #5
config twie=400000

i2cinit #5
 
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 1126

poland.gif
PostPosted: Tue Apr 22, 2025 4:53 am    Post subject: Reply with quote

I would recommend some debug info even if you dont use any COMx from Xmega.
Here, for example, I open a serial on PORTF.3 where I print the debug data.

Code:
$regfile = "XM192A3Udef.dat"
$hwstack = 64
$swstack = 40
$framesize = 40

' ***************** SYSTEM CLOCK CONFIG **************************
'                INTERNAL 32MHz NO PRESCALE
'
Config Osc = Disabled , 32mhzosc = Enabled , 32khzosc = Enabled
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1
'
' ENABLING AUTOMATIC OSCILLATOR CALIBRATION
Osc_dfllctrl.0 = 1
Dfllrc32m_ctrl.0 = 1
'*****************************************************************
'*                         I2C CONFIG                            *
'*****************************************************************
Const Softi2c = 0

#if Softi2c
   $forcesofti2c                                              ' with this the software I2C/TWI commands are used when inlcuding i2c.lbx
   $lib "i2cv2.lib"
   Config Sda = Porte.0
   Config Scl = Porte.1
   I2cinit
   Config I2cdelay = 10                                      ' 100kHz
#else
   Dim Twi_start As Byte
   Open "twie" For Binary As #14
   Config Twie = 100000                                       '100kHz
   I2cinit #14
#endif

'************ CONFIGURATION FOR SERIAL ON PORTF ******************
Open "COMF.3:115200,8,n,1" For Output As #7
Const Com = 7

Debug On


Dim I , Twi_addr , Rawb(4) As Byte

Twi_addr = &H28
Shift Twi_addr , Left , 1
Twi_addr.0 = 1                                              'READ

Debug #com , "{013}{010}Debug started"

Do
 #if Softi2c

   I2cstart
   If Err = 0 Then
      I2cwbyte Twi_addr
      If Err = 0 Then
         I2crbyte Rawb(1) , Ack
         I2crbyte Rawb(2) , Ack
         I2crbyte Rawb(3) , Ack
         I2crbyte Rawb(4) , Nack
         For I = 1 To 4
            Debug #com , Hex(rawb(i)) ;
         Next
         Debug #com , ""
      Else
         Debug #com , "Device dont responding at this address"
      End If
   Else
      Debug #com , "Bus start error"
   End If
   I2cstop

 #else

   I2cstart #14
   If Err = 0 Then
      I2cwbyte Twi_addr , #14
      If Err = 0 Then
         I2crbyte Rawb(1) , Ack , #14
         I2crbyte Rawb(2) , Ack , #14
         I2crbyte Rawb(3) , Ack , #14
         I2crbyte Rawb(4) , Nack , #14
         For I = 1 To 4
            Debug #com , Hex(rawb(i)) ;
         Next
         Debug #com , ""
      Else
         Debug #com , "Device dont responding at this address"
      End If
   Else
      Debug #com , "Bus start error"
   End If
   I2cstop #14

 #endif

 Wait 1
Loop

End
 

_________________
Check B-Flash -my MCS bootloader app for Android (updated)
Back to top
View user's profile Visit poster's website
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 548

PostPosted: Tue Apr 22, 2025 12:56 pm    Post subject: Reply with quote

Thank you Jarek and EDC. You have given me more than enough to get started.

In the future, we will ask the "Polish Connection" for help with I2C/TWI problems.

:D

E
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 1126

poland.gif
PostPosted: Tue Apr 22, 2025 1:29 pm    Post subject: Reply with quote

If you finally find the cause of this troubles then you can convert those all I2c routines into one line.
Code:
I2creceive Twi_addr , Rawb(1) , 0 , 4

This will do Start, read four bytes and do Stop Very Happy
You only should not set that R/W bit manually.

_________________
Check B-Flash -my MCS bootloader app for Android (updated)
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