To use this file copy and paste this:    // #URL-lib "http://pin1.org/forthlib/flb/General/SPI.flb"   into BV Terminal 3 or here to download.

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // SPI - This is the basic interface for SPI which has a very simple // method of working. There is a data in, data out and chip select // when data is going in, data is also comming out so if just data out is // wanted then a dummy data in is sent. // CS is hardware / device dependant and can be any port set to o/p normally // a high hill select the device

// REQUIRES: // #URL-lib "http://pin1.org/forthlib/flb/General/soft1.flb" // #URL-lib "http://pin1.org/forthlib/flb/General/pinsel.flb"

// CONSTANTS: // SPI registers &E0020000 constant S0SPCR // config &E0020004 constant S0SPSR // status &E0020008 constant S0SPDR // data &E002000C constant S0SPCCR // clock &E002001C constant S0SPINT variable inital


Full Contents of File

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// SPI - This is the basic interface for SPI which has a very simple
// method of working. There is a data in, data out and chip select
// when data is going in, data is also comming out so if just data out is
// wanted then a dummy data in is sent.
// CS is hardware / device dependant and can be any port set to o/p normally
// a high hill select the device

// REQUIRES:
// #URL-lib "http://pin1.org/forthlib/flb/General/soft1.flb"
// #URL-lib "http://pin1.org/forthlib/flb/General/pinsel.flb"

// SPI - This is the basic low level card access

// CONSTANTS:
// SPI registers
&E0020000  constant  S0SPCR      // config
&E0020004  constant  S0SPSR      // status
&E0020008  constant  S0SPDR      // data
&E002000C  constant  S0SPCCR    // clock
&E002001C  constant  S0SPINT
variable  inital

// sets spi speed
: set-speed ( speedHz --- )   // must be > 8 and even number
    pclk  swap  / 8 max &fe and S0SPCCR !
    -1  inital  !
;   
// testing only
// : y S0SPSR @ ph. ;

// Hardest part of spi is getting the configuration correct
// CPOL, CPHA and MSB or LSB first need setting correctly
: init
        &20  S0SPCR  !        // master CPOL & CPHA = 0
        inital  @  0=  if  254  S0SPCCR  !  then      // default slow
        4  1  pinselect      // SCK function 1 for SPI
        5  1  pinselect      // MISO
        6  1  pinselect      // MOSI
;

// -*-*-*-*-*-*-*- P U B L I C *-*-*-*-*-*-*-*-*-*-*-*-*-*-*
// This is all there is, it clocks in and out at the same
// time n1 is the byte to clock in and n2 is what is fetched
// from the device so when writing drop n2
// ( n1 --- n2)
: <0>spi
// inital @ 0= if init -1 inital ! then // remove check for more speed
        S0SPDR  !       
        0
        100  for
                S0SPSR  @  &80  and
                if  drop  -1  leave  then
          next
          if 
                S0SPDR  @
          else
                ."  SPI  problem"  abort
          then
;
: <0>spi.initinit  ;
: <0>spi.speed ( Hz --- )   set-speed  ;