View previous topic :: View next topic |
Author |
Message |
enniom
Joined: 20 Oct 2009 Posts: 548
|
Posted: Mon Apr 21, 2025 2:54 pm Post subject: How to use TWIy on XMEGA with MCU_REVID < 7 |
|
|
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 |
|
 |
EDC
Joined: 26 Mar 2014 Posts: 1126

|
|
Back to top |
|
 |
enniom
Joined: 20 Oct 2009 Posts: 548
|
Posted: Mon Apr 21, 2025 5:37 pm Post subject: |
|
|
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 |
|
 |
EDC
Joined: 26 Mar 2014 Posts: 1126

|
Posted: Mon Apr 21, 2025 6:02 pm Post subject: |
|
|
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 |
|
 |
enniom
Joined: 20 Oct 2009 Posts: 548
|
Posted: Mon Apr 21, 2025 6:13 pm Post subject: |
|
|
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 |
|
 |
_jarek_
Joined: 06 Aug 2014 Posts: 9

|
Posted: Mon Apr 21, 2025 11:21 pm Post subject: |
|
|
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 |
|
 |
EDC
Joined: 26 Mar 2014 Posts: 1126

|
Posted: Tue Apr 22, 2025 4:53 am Post subject: |
|
|
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 |
|
 |
enniom
Joined: 20 Oct 2009 Posts: 548
|
Posted: Tue Apr 22, 2025 12:56 pm Post subject: |
|
|
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 |
|
 |
EDC
Joined: 26 Mar 2014 Posts: 1126

|
|
Back to top |
|
 |
|
|
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
|
|