| 1 | /*********************************************************************** |
| 2 | * Langford SPI files. |
| 3 | * |
| 4 | * This implements the SPI protocol for when programming data. |
| 5 | * (Bit banging) |
| 6 | * |
| 7 | * (c) 2013 Per Vices Corporation |
| 8 | * |
| 9 | * See LICENSE.txt for license and copyright information. |
| 10 | ************************************************************************/ |
| 11 | |
| 12 | |
| 13 | #include "langford_spi.h" |
| 14 | |
| 15 | int LangordSpiWrite(int DevFD, int NumBytes, int Content, int nCS, int SClk, int SDO) { |
| 16 | int RetVal = 0; |
| 17 | int Val, Val0 = 0, Val1 = 1; |
| 18 | int i; |
| 19 | |
| 20 | //Set pins to known state |
| 21 | RetVal |= ioctl(DevFD, nCS, &Val1); |
| 22 | RetVal |= ioctl(DevFD, SClk, &Val0); |
| 23 | |
| 24 | //Start transfer |
| 25 | RetVal |= ioctl(DevFD, nCS, &Val0); |
| 26 | |
| 27 | //Transfer data |
| 28 | for (i = NumBytes * 8 - 1; i >= 0; i--) { |
| 29 | Val = (Content >> i) & 1; |
| 30 | RetVal |= ioctl(DevFD, SDO, &Val); |
| 31 | RetVal |= ioctl(DevFD, SClk, &Val1); |
| 32 | RetVal |= ioctl(DevFD, SClk, &Val0); |
| 33 | }; |
| 34 | |
| 35 | //Signal end of transfer |
| 36 | RetVal |= ioctl(DevFD, nCS, &Val1); |
| 37 | |
| 38 | return RetVal; |
| 39 | }; |