nuxx.net
Making, baking, and (un-)breaking things in Southeast Michigan.

SPI Sniffer Firmware

Well, I still haven’t written the bits to fire an interrupt when the SPI buffer fills up, so that interrupt() can write its contents (a single byte) into the FIFO, nor have I written the bits to actually fill the FIFO. Still, it’s not bad for less than an hour’s work while on the conference call.

Oh, and it’s written in mikroBasic for PIC.


program spi_sniff

' Set up the FIFO for data going out the serial port.
const FIFOSIZE as byte = 40

dim fifohead, fifotail as byte
dim fifoempty, fifofull as boolean
dim fifo as byte[FIFOSIZE]

' fifodatawaiting()
sub function fifodatawaiting as boolean
  if fifoempty then
    result = false
  else
    result = true
  end if
end sub

' writefifo()
sub procedure writefifo
  dim j as byte
  j = 0 ' If the buffer is empty, read 0.
  if fifoempty = false then ' As long as the buffer isn't empty...
    j = fifo[fifohead] ' Read in the contents of the head of the FIFO.
    fifo[fifohead] = 0 ' Set the previously read byte to 0. Destructive read.
    fifofull = false ' Since we've read data, the buffer can no longer be full.
    inc(fifohead) ' Move the head pointer to the next place.
    if fifohead = FIFOSIZE then ' If the head is now greater than buffer size,
      fifohead = 0 ' move it back to place 0.
    end if
    if fifohead = fifotail then ' If the head and tail are in the same place,
      fifoempty = true ' the buffer can be considered to be empty.
    end if
  end if
  usart_write(j) ' Write the FIFO out the serial port.
end sub

' interrupt()
'sub procedure interrupt
'end sub

main:

memset(@fifo,0,FIFOSIZE) ' Fill the FIFO with zeros.
fifohead = 0 ' Head is at 0.
fifotail = 0 ' Tail is at 0.
fifoempty = true ' FIFO is empty.
fifofull = false ' FIFO is not full.
usart_init(19200) ' Set the USART to 19.2K
spi_init_advanced(slave_ss_dis,data_sample_middle,clk_idle_low,low_2_high)

while true
  while fifodatawaiting
    usart_write($FF)
    usart_write($55)
    writefifo
  wend
wend
end.

Leave a reply