C3 SPI Drivers - part 0 ( introduction )

Since SPI is a serial communication system it's not as fast as a parallel one, so if we want to use within an emulation project where speed is critical, we need to build some fast driver in assembly.

You can decide to create asm routines and embed them in drivers that need them, or you can develop a dedicated driver which runs on its own cog. Of course it depends on how many cogs you will use at the end of the project.

I assume to write a dedicated driver right now so that I can use for test purposes. 

What we'll need is:
The SPIN code that will interface with "the rest of the world", with SPI init, SPI write and SPI read access routines.
        The ASM code that handles the access to asm routines, and the real SPI  routines.

First we need to porepare the "host" where the emulator will run. A cog needs its variables, constants, procedures and assembly code in order to run.

Costant section will contain a list of commands we will use. In memory these costants will look like as if they were =1, =2, =3 etc.
CON
#1, _spiInit, _setChannel, _spiWr, _spiRd, _loop
Variable section contains some variables used in the driver
VAR
  long command, cogon, cog
First we need to reserve space to point the cog with two variables
PUB start : okay
  stop
  okay := cogon := ( cog := cognew( @entry, @command )) > 0
Then we can define some public routines to control the cog.
Start wil stop eventually the cog if it is already started, then setup the return value "okay" if the cog could be started correctly. We also pass a pointer to the cog which gives it some parameter as for example the audio pin etc. Don't forget to point the cog to the starting label of the assembly main routine, in this case "entry".
PUB stop
  '' Stop sound driver - frees a cog
  if cogon~
  indent1 cogstop(cog)
Stop will stop the cog if already started, nothing of special.