Shop OBEX P1 Docs P2 Docs Learn Events
Understanding RTC communication — Parallax Forums

Understanding RTC communication

ErlendErlend Posts: 612
edited 2013-04-07 15:28 in Propeller 1
I am working my way through Kwabena's RTC engine. Plan is to mod it to be a reader only, which returns the timedate data packed into one LONG variable - into bits 1-5(sec), 6-10(min), 11-14(hr), 15-17(dow), 18-21(day), 22-25(month), 26-29(year-2000). I want it to contiously poll the time, package it, and write it to the parent cmpTime variable. So, it has to run in it's own cog.
Below is the core of the code. I do not understand what is going on. Confusingly many returns, bolean operations, and bitshifts. Would anyone care to explain me?
PUB readTime '' 11 Stack Longs                        'Reads the RTC data and converts to time-data, and puts it into time[]
  return getRam(0)


PRI getRAM(index) ' 8 Stack Longs

  startDataTransfer                                      

  result := transmitPacket(constant(104 << 1))
  result and= transmitPacket(index)

  stopDataTransfer
  startDataTransfer

  result and= transmitPacket(constant((104 << 1) | 1))

  if(index)
    result &= receivePacket(false)

  else
    bytefill(@time, 0, 7)
    if(result)
      repeat 7
        timeTemporary := receivePacket(6 <> index)
        time[index++] := (((timeTemporary >> 4) * 10) + (timeTemporary & $F))

  stopDataTransfer

  
PRI transmitPacket(value) ' 4 Stack Longs

  value := ((!value) >< 8)                           

  repeat 8
    dira[dataPin] := value                           
    dira[clockPin] := false                          
    dira[clockPin] := true
    value >>= 1                                      

  dira[dataPin] := false
  dira[clockPin] := false
  result := not(ina[dataPin])                        
  dira[clockPin] := true
  dira[dataPin] := true

PRI receivePacket(aknowledge) ' 4 Stack Longs

  dira[dataPin] := false                            '
                                                     
  repeat 8                                           
    result <<= 1                                    '
    dira[clockPin] := false                          
    result |= ina[dataPin]                          '                          
    dira[clockPin] := true                                            
                                                                                      
  dira[dataPin] := (not(not(aknowledge)))                                             
  dira[clockPin] := false                                                             
  dira[clockPin] := true                                                        
  dira[dataPin] := true

  
PRI startDataTransfer ' 3 Stack Longs

  outa[dataPin] := false
  outa[clockPin] := false
  dira[dataPin] := true
  dira[clockPin] := true

  
PRI stopDataTransfer ' 3 Stack Longs

  dira[clockPin] := false
  dira[dataPin] := false

Erlend

Comments

  • KyeKye Posts: 2,200
    edited 2013-04-07 11:14
    The transmitPacket function just preforms a write to the I2C bus. The receive packet function reads from the I2C bus. Look at the I2C bus spec for what is going on. The start and stop functions are also I2C bus things.

    The getRam function just gets the bytes of time data from the RTC. You should read the DS1307 RTC data sheet for more information about what that is doing. The constant(104) thing is the RTC address. Its shifted 1 because bit 0 specifies a read when the it is 1 and a write when it is 0.

    Just try to get a good grasp on the I2C bus and then read the data sheet for the DS1307 and you'll understand.
  • ErlendErlend Posts: 612
    edited 2013-04-07 12:47
    I guess I worded myself a bit wrong; the thing is I do understand what is going on - wrt I2C and DS1307 - I have read the specs, and that is stuff I easily grasp. So, consequently I do know what is basically happening in transmitPacket, getRam, and the start/stopDataTransfer. I just don't know how - I don't understand the language so to speak - although I do understand the words. Like below there are three dira, but no ina or outa. And the NOT value different from 8?

    value := ((!value) >< 8)
    repeat 8
    dira[dataPin] := value
    dira[clockPin] := false
    dira[clockPin] := true value >>= 1

    I do know the whole thing is just about picking one bit at the time and put them together - and vice versa.
    Erlend
  • KyeKye Posts: 2,200
    edited 2013-04-07 15:28
    Its open draining the output. OUTA is zero. So, setting dira makes the prop pull the line low. Clearing dira lets the line float high.
Sign In or Register to comment.