' Raspberry Pi LPC1114 I/O Processor Expansion Board SPI Agent Firmware ' loopback test program ' Copyright (C)2015-2018, Philip Munts, President, Munts AM Corp. ' ' Redistribution and use in source and binary forms, with or without ' modification, are permitted provided that the following conditions are met: ' ' * Redistributions of source code must retain the above copyright notice, ' this list of conditions and the following disclaimer. ' ' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" ' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ' IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ' ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE ' LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR ' CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF ' SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ' INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN ' CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ' ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ' POSSIBILITY OF SUCH DAMAGE. TextWindow.WriteLine("") TextWindow.WriteLine("Raspberry Pi LPC1114 I/O Processor Expansion Board SPI Agent Firmware Test") TextWindow.WriteLine("") If Program.ArgumentCount = 0 Then TextWindow.Write("Server name: ") SERVERNAME = TextWindow.Read() TextWindow.Write("Iterations: ") ITERATIONS = TextWindow.Read() TextWindow.WriteLine("") ElseIf Program.ArgumentCount = 1 Then SERVERNAME = Program.GetArgument(1) ITERATIONS = 10000 ElseIf Program.ArgumentCount = 2 Then SERVERNAME = Program.GetArgument(1) ITERATIONS = Program.GetArgument(2) Else TextWindow.WriteLine("") TextWindow.WriteLine("Usage: spi_agent_test [iterations]") TextWindow.WriteLine("") TextWindow.Read() Program.End() EndIf SPIAgent.Open(SERVERNAME) If SPIAgent.error <> 0 Then TextWindow.WriteLine("SPIAgent.Open() failed, error=" + SPIAgent.error) TextWindow.Read() Program.End() EndIf ' Try a NOP operation SPIAgent.Command("nop", 0, 0) TextWindow.WriteLine("Response: command:" + SPIAgent.cmd + " pin:" + SPIAgent.pin + " data:" + SPIAgent.data + " error:" + SPIAgent.error) ' Try a LOOPBACK operation SPIAgent.Command("loopback", 2, 3) TextWindow.WriteLine("Response: command:" + SPIAgent.cmd + " pin:" + SPIAgent.pin + " data:" + SPIAgent.data + " error:" + SPIAgent.error) ' Try a GET_GPIO operation with illegal pin number SPIAgent.Command("GET_GPIO", 99, 3) TextWindow.WriteLine("Response: command:" + SPIAgent.cmd + " pin:" + SPIAgent.pin + " data:" + SPIAgent.data + " error:" + SPIAgent.error) ' Try an illegal command SPIAgent.Command(99, 2, 3) TextWindow.WriteLine("Response: command:" + SPIAgent.cmd + " pin:" + SPIAgent.pin + " data:" + SPIAgent.data + " error:" + SPIAgent.error) TextWindow.WriteLine("") ' Query the SPI Agent Firmware version SPIAgent.Command(0, 0, 0) If SPIAgent.error <> 0 Then TextWindow.WriteLine("SPIAgent.Command() failed, error=" + SPIAgent.error) TextWindow.Read() Program.End() Else TextWindow.WriteLine("The LPC1114 firmware version is " + SPIAgent.data) EndIf ' Query the LPC1114 device ID SPIAgent.Command("GET_SFR", "DEVICE_ID", 0) If SPIAgent.error <> 0 Then TextWindow.WriteLine("SPIAgent.Command() failed, error=" + SPIAgent.error) TextWindow.Read() Program.End() Else TextWindow.WriteLine("The LPC1114 device ID is " + SPIAgent.datahex) EndIf ' Query the LED state SPIAgent.Command("GET_GPIO", "LED", 0) If SPIAgent.error <> 0 Then TextWindow.WriteLine("SPIAgent.Command() failed, error=" + SPIAgent.error) TextWindow.Read() Program.End() Else TextWindow.Write("The expansion board LED is ") If SPIAgent.data = 0 Then TextWindow.WriteLine("OFF") Else TextWindow.WriteLine("ON") EndIf EndIf ' Perform SPI loopback commands as fast as possible to stress test the SPI interface TextWindow.WriteLine("") TextWindow.WriteLine("Starting " + ITERATIONS + " SPI agent loopback test transactions...") starttime = Clock.ElapsedMilliseconds For i = 1 To ITERATIONS SPIAgent.Command("NOP", 0, 0) If SPIAgent.error <> 0 Then TextWindow.WriteLine("SPIAgent.Command() failed, error=" + SPIAgent.error) EndIf EndFor endtime = Clock.ElapsedMilliseconds ' Calculate statistics deltat = (endtime - starttime)/1000.0 rate = ITERATIONS/deltat cycletime = deltat/ITERATIONS ' Display statistics TextWindow.WriteLine("") TextWindow.WriteLine("Performed " + ITERATIONS + " loopback tests in " + deltat + " seconds") TextWindow.WriteLine(" " + Math.Round(rate) + " iterations per second") TextWindow.WriteLine(" " + Math.Round(cycletime*1000000.0) + " microseconds per iteration") TextWindow.WriteLine("") SPIAgent.Close() If SPIAgent.error <> 0 Then TextWindow.WriteLine("SPIAgent.Close() failed, error=" + SPIAgent.error) EndIf