{"id":8910,"date":"2007-08-14T16:57:00","date_gmt":"2007-08-14T20:57:00","guid":{"rendered":"https:\/\/nuxx.net\/blog\/2007\/08\/14\/spi-sniffer-firmware\/"},"modified":"2026-07-01T11:35:51","modified_gmt":"2026-07-01T15:35:51","slug":"spi-sniffer-firmware","status":"publish","type":"post","link":"https:\/\/nuxx.net\/blog\/2007\/08\/14\/spi-sniffer-firmware\/","title":{"rendered":"SPI Sniffer Firmware"},"content":{"rendered":"<p>Well, I still haven&#8217;t written the bits to fire an interrupt when the SPI buffer fills up, so that <tt>interrupt()<\/tt> can write its contents (a single byte) into the FIFO, nor have I written the bits to actually fill the FIFO. Still, it&#8217;s not bad for less than an hour&#8217;s work while on the conference call.<\/p>\n<p>Oh, and it&#8217;s written in <a href=\"http:\/\/www.mikroe.com\/en\/compilers\/mikrobasic\/pic\/index.htm\">mikroBasic for PIC<\/a>.<\/p>\n<p><!--more Click for code...--><br \/>\n<tt>program spi_sniff<\/p>\n<p>' Set up the FIFO for data going out the serial port.<br \/>\nconst FIFOSIZE as byte = 40<\/p>\n<p>dim fifohead, fifotail as byte<br \/>\ndim fifoempty, fifofull as boolean<br \/>\ndim fifo as byte[FIFOSIZE]<\/p>\n<p>' fifodatawaiting()<br \/>\nsub function fifodatawaiting as boolean<br \/>\n&nbsp;&nbsp;if fifoempty then<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;result = false<br \/>\n&nbsp;&nbsp;else<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;result = true<br \/>\n&nbsp;&nbsp;end if<br \/>\nend sub<\/p>\n<p>' writefifo()<br \/>\nsub procedure writefifo<br \/>\n&nbsp;&nbsp;dim j as byte<br \/>\n&nbsp;&nbsp;j = 0 ' If the buffer is empty, read 0.<br \/>\n&nbsp;&nbsp;if fifoempty = false then ' As long as the buffer isn't empty...<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;j = fifo[fifohead] ' Read in the contents of the head of the FIFO.<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;fifo[fifohead] = 0 ' Set the previously read byte to 0. Destructive read.<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;fifofull = false ' Since we've read data, the buffer can no longer be full.<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;inc(fifohead) ' Move the head pointer to the next place.<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;if fifohead = FIFOSIZE then ' If the head is now greater than buffer size,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fifohead = 0                '  move it back to place 0.<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;end if<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;if fifohead = fifotail then     ' If the head and tail are in the same place,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fifoempty = true         '  the buffer can be considered to be empty.<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;end if<br \/>\n&nbsp;&nbsp;end if<br \/>\n&nbsp;&nbsp;usart_write(j)               ' Write the FIFO out the serial port.<br \/>\nend sub<\/p>\n<p>' interrupt()<br \/>\n'sub procedure interrupt<br \/>\n'end sub<\/p>\n<p>main:<\/p>\n<p>memset(@fifo,0,FIFOSIZE)     ' Fill the FIFO with zeros.<br \/>\nfifohead = 0                 ' Head is at 0.<br \/>\nfifotail = 0                 ' Tail is at 0.<br \/>\nfifoempty = true             ' FIFO is empty.<br \/>\nfifofull = false             ' FIFO is not full.<br \/>\nusart_init(19200)            ' Set the USART to 19.2K<br \/>\nspi_init_advanced(slave_ss_dis,data_sample_middle,clk_idle_low,low_2_high)<\/p>\n<p>while true<br \/>\n&nbsp;&nbsp;while fifodatawaiting<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;usart_write($FF)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;usart_write($55)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;writefifo<br \/>\n&nbsp;&nbsp;wend<br \/>\nwend<br \/>\nend.<\/tt><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well, I still haven&#8217;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&#8217;s not bad for less than an hour&#8217;s work while on the conference\u2026<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,11,34],"tags":[],"class_list":["post-8910","post","type-post","status-publish","format-standard","hentry","category-electronics","category-making-things","category-moved-from-livejournal"],"_links":{"self":[{"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/posts\/8910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/comments?post=8910"}],"version-history":[{"count":1,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/posts\/8910\/revisions"}],"predecessor-version":[{"id":12706,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/posts\/8910\/revisions\/12706"}],"wp:attachment":[{"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/media?parent=8910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/categories?post=8910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/tags?post=8910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}