Shop OBEX P1 Docs P2 Docs Learn Events
SPI driver on seperate cog — Parallax Forums

SPI driver on seperate cog

OtmarOtmar Posts: 7
edited 2008-12-17 14:54 in Propeller 1
Hello i have written a simple SPI driver for interfacing a ISD1700 voice recorder chip.
The driver is writte in Spin because i dont need high data rates.
My problem is to get this driver working correctly on a sperate cog.
When i am using the driver from the main cog, everything workes fine -> so the driver generally works

So what i have to change to get the driver working on a seperate cog?

CON

   baudRate = 10000 
                              
VAR

byte SPI_MISO
byte SPI_MOSI
byte SPI_CLK
byte SPI_CS

byte bitCount
byte byteCount
byte foo
word bitTime

PUB start :okay  
init(20,17,18,19)
 
PUB init(MISO,MOSI,SCK,CS)

  'read variables
   SPI_MISO := MISO
   SPI_MOSI := MOSI
   SPI_CLK  := SCK
   SPI_CS   := CS
   
   
  'init port pins 
   DIRA[noparse][[/noparse]SPI_MOSI]~~     'OUTPUT
   DIRA[noparse][[/noparse]SPI_CLK]~~     'OUTPUT
   DIRA[noparse][[/noparse]SPI_CS]~~       'OUTPUT
   DIRA[noparse][[/noparse]SPI_MISO]~      'INTPUT
   
   OUTA[noparse][[/noparse]SPI_CLK]~~      'set high         
   OUTA[noparse][[/noparse]SPI_CS]~~        'set high
   OUTA[noparse][[/noparse]SPI_MOSI]~        'set_low
   
   bitCount  := 0
   byteCount := 0
   
   bitTime := clkfreq / baudRate
   
   
  OUTA[noparse][[/noparse]SPI_CS]~
  tx($01)
  tx($00)
  OUTA[noparse][[/noparse]SPI_CS]~~
   
  waitcnt(100000+cnt)
   
  OUTA[noparse][[/noparse]SPI_CS]~
  tx($65)
  tx($C0)
  tx($0C)
  OUTA[noparse][[/noparse]SPI_CS]~~
   
   
  repeat while TRUE




PUB tx(dataByte)  | t ,h ,rx

  OUTA[noparse][[/noparse]SPI_CLK]~
  OUTA[noparse][[/noparse]SPI_MOSI]~
    
      t      := cnt
      
      repeat h from 0 to 7
        OUTA[noparse][[/noparse]SPI_MOSI] := dataByte & $01                          'set MOSI pin
        rx:=INA[noparse][[/noparse]SPI_MISO] << 7 | rx >>1 
        waitcnt(t += bitTime)
        !OUTA[noparse][[/noparse]SPI_CLK]
        dataByte:= dataByte >> 1
        
        waitcnt(t += bitTime)
        
        if h <7
           !OUTA[noparse][[/noparse]SPI_CLK]
      
      return rx 
   





Thanks

Andre

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-17 14:54
    What have you tried?

    The main thing that people forget is that each cog has its own I/O registers, so you need to execute the initialization routine in the new cog so the DIRA and OUTA registers get initialized properly. You could do "COGNEW(init(20,17,18,19),@stack)" in your start routine where stack is a minimum of 20 levels. In your init routine, the REPEAT loop would do the actual work after the initialization (and it can be just REPEAT).

    There are examples of multicog programs in the Propeller Education Kit tutorials and in the "sticky" threads at the top of the thread list for this forum.
Sign In or Register to comment.