Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

twi slave and i2v master on the same chip

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1570
Location: Basel, Switzerland

switzerland.gif
PostPosted: Sun May 25, 2014 7:03 am    Post subject: twi slave and i2v master on the same chip Reply with quote

Hi all,

I have an "interesting" problem, maybe some can help:

I have an i2c sensor that I need to read at very regular intervals (to produce an accumulated value). When the sensor is performing a measurement the i2c bus is totally blocked (Clock stretching) for up to 60ms. The maths to calculate the accumulated value is also quite nasty (It costs about 10K instructions).

My idea is to use a small AVR with hardware TWI, use the TWI slave lib to communicate with the main CPU and a secondary i2c bus (using the normal soft i2c lib) to communicate with the sensor. The slave AVR can handle the low level stuff for the sensor and simply return the data to the main CPU. This would also mean that I could transfer some IO to the slave as it would have free pins (Reading jumper settings, driving LED's etc) which are currently handled by a i2c GPIO.

I know the new atxmega chips have multiple i2c/twi busses but I don't have much experience with them and don't have the time/budget to learn a complete new architecture.

SO is it possible to use both the TWI hardware slave and the software i2c on the same chip? I think I'd use something like a mega8 or tiny48/88.


Regards
Ian Dobson

(BASCOM-AVR version : 2.0.7.7 )

_________________
Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sun May 25, 2014 11:40 am    Post subject: Re: twi slave and i2v master on the same chip Reply with quote

i.dobson wrote:
SO is it possible to use both the TWI hardware slave and the software i2c on the same chip?

Bascom uses exclusively one of the i2c-libs, so you have to chose.
But as Soft-I2C is nothing more than bitbanging the pins, basically as much soft-i2c channels can be implemented, as pins are available.

Did that some time ago for one additional soft-i2c channel, to have soft-i2c available for a XMega.

You need to copy the i2c.lib, alter unique names like _sdaDDR, as well as any label like _i2c_get_ack so it does not collide with the i2c-lib used by Bascom.
Then you need to provide a new interface for accessing this new copied/modified lib, something like that, excerpt:
Code:
Sub xI2CRByte(ByRef i2cb As Byte , ByVal anack As Byte)
  !LDD  R26,Y+0
  !LDD  R27,Y+1
  !LD   R17,X
  !SUBI R17,xack
  _xI2C_read
  !LDD  R26,Y+2
  !LDD  R27,Y+3
  !ST   X,  R17
End Sub

Which tells the lib where to find the arguments.
Quote:
I think I'd use something like a mega8 or tiny48/88.

This additional lib will use up flash memory to some extent.
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1570
Location: Basel, Switzerland

switzerland.gif
PostPosted: Sun May 25, 2014 11:55 am    Post subject: Reply with quote

Hi MWS,

Thanks for the answer, but are you sure?

Looking at i2c_twi_slave.lbx I see

_twi_init:
* Sbi _TWIsdaPORT,_TWIsda
* Sbi _TWIsclPORT,_TWIscl
* Cbi _TWIsdaDDR,_TWIsda
* Cbi _TWIsclDDR,_TWIscl

So the port/pin combination includes twi in the "Name" and the labels contain twi in their name.

I've already created a software that has both the twi slave and software master, as well as the code to read/process the sensor at it's about 3Kb. It's still missing some code to actually pass the data back to the master (double buffering) but that shouldn't take too much code.

Maybe I should just pull out an old test board and try the software on real hardware.

Regards
Ian Dobson

_________________
Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sun May 25, 2014 12:35 pm    Post subject: Reply with quote

What do you mean with "are you sure"?

Yes, as well I have no experience with the slave lib, I think it will use the uCs TWI hardware, so your goal seems to be, to set up a soft-i2c master at random pins.

Basically the i2c.lib (soft) could already do that, if - no routines from the i2c master-lib (soft or hard) are used by the slave library, which includes also definitions like assignment for SCL/SDA pins. Check this out and you may be already done. You should be able to learn that by simply compiling such a combination and check the compiler's output/complaints.

As well my suggestion was made for a different use, it circumvents potential problems by renaming any label or definition, to make sure nothing interferes.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Sun May 25, 2014 11:36 pm    Post subject: Reply with quote

it will work without any mods. just use the slave with the hardware TWI interface and use the softslave for master.
here is a sample.

Code:
'-------------------------------------------------------------------------------
'                            (c) 2004-2010 MCS Electronics
'                This demo shows an example of the TWI in SLAVE mode
'                       Not all AVR chips have TWI (hardware I2C)
'  This demo is a Mega88 I2C A/D converter slave chip
'  This demo also shows that PORTB can be set for example to be used as an output
' NOTICE that this demo will only work with the TWI slave library which is avaialble as an add on
' SDA pin is PORTC.4 (pin 27)
' SCL pin is PORTC.5 (pin 2Cool
'-------------------------------------------------------------------------------
$regfile = "M88def.dat"                                     ' the chip we use
$crystal = 8000000                                          ' crystal oscillator value
$baud = 19200                                               ' baud rate
$hwstack = 40
$swstack = 20
$framesize = 40

$lib "i2c_twi-slave.lbx"

Print "MCS Electronics M88 TWI-slave demo"
Print "Use with M88-TWI master demo"

Config Adc = Single , Prescaler = Auto , Reference = Avcc
'Now give power to the chip
Start Adc

Dim W As Word
Config Portb = Output

Dim Status As Byte                                          'only for debug
'Print Hex(status)

Config Twislave = &H70 , Btr = 2 , Bitrate = 100000
'                   ^--- slave address
'                         ^---------- 2 bytes to receive
'                                     ^--- bitrate is 100 KHz


'The variables  Twi , Twi_btr and Twi_btw are created by the compiler. These are all bytes
'The TWI interrupt is enabled but you need to enabled the global interrupt
config SCL=PORTD.2
Config SDA=PORTD.3

Enable Interrupts

'this is just an empty loop but you could perform other tasks there
Do
  'Print Getadc(0)
  'Waitms 500
   i2cstart
   i2cwbyte &h70
   I2CwByte &B1010_1010
   i2cstop
   waitms 1
Loop
End




'The following labels are called from the library. You need to insert code in these subroutines
'Notice that the PRINT commands are remarked.
'You can unmark them and see what happens, but it will result in occasional errors in the transmission
'The idea is that you write your code in the called labels. And this code must execute in as little time
'as possible. So when you slave must read the A/D converter, you can best do it in the main program
'then the data is available when the master needs it, and you do not need to do the conversion which cost time.


'A master can send or receive bytes.
'A master protocol can also send some bytes, then receive some bytes
'The master and slave must match.

'the following labels are called from the library  when master send stop or start
Twi_stop_rstart_received:
 ' Print "Master sent stop or repeated start"
Return

'master sent our slave address and will not send data
Twi_addressed_goread:
 ' Print "We were addressed and master will send data"
Return


Twi_addressed_gowrite:
 ' Print "We were addressed and master will read data"
Return

'this label is called when the master sends data and the slave has received the byte
'the variable TWI holds the received value
Twi_gotdata:
   'Print "received : " ; Twi ; " byte no : " ; Twi_btw
   Select Case Twi_btw
     Case 1 : Portb = Twi                                   ' first byte
     Case 2:                                                'you can set another port here for example
   End Select                                               ' the setting of portb has nothing to do with the ADC
Return

'this label is called when the master receives data and needs a byte
'the variable twi_btr is a byte variable that holds the index of the needed byte
'so when sending multiple bytes from an array, twi_btr can be used for the index
Twi_master_needs_byte:
  'Print "Master needs byte : " ; Twi_btr
  Select Case Twi_btr
    Case 1:                                                 ' first byte
              W = Getadc(0)                                 'in this example the conversion is done here
              ' but a better option would have been to just pass the value of W and do the conversion in the main loop
              'Print "ADC-SLAVE:" ; W
              Twi = Low(w)
    Case 2                                                  ' send second byte
              Twi = High(w)
  End Select
Return


'when the mast has all bytes received this label will be called
Twi_master_need_nomore_byte:
 ' Print "Master does not need anymore bytes"
Return

Mydbg:
'  Print Hex(status)
Return

_________________
Mark
Back to top
View user's profile Visit poster's website
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1570
Location: Basel, Switzerland

switzerland.gif
PostPosted: Mon May 26, 2014 4:34 am    Post subject: Reply with quote

Thanks for the answer Mark.

Regards
Ian Dobson

_________________
Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive 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