/* This is an example for the usage of the SPI/MicroWire driver (/dev/spi).
	It talks to a chip from Harris Semiconductor, the CDP68HC68P1, referred
	to by P1 in the rest of this file. This chip is a 8 bit parallel port.
	Each pin can be programmed to be an input or an output. The example
	first programs all pins to be outputs and then sets D1 and D2 low.
	The program does not use the value returned by the chip. 
	The P1 autodetects the polarity of the clock, so we could use 
	SEL_SPI_CPOL_1 as well.
	
	This example is for the V2 version of the driver and is INCOMPATIBLE
	with any V1 driver!!!!
*/
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/sched.h>
#include <linux/malloc.h>
#include <linux/ioport.h>
#include <linux/fcntl.h>
#include <linux/unistd.h>

#include <asm/io.h>
#include <asm/segment.h>
#include <asm/system.h>
#include "spi.h"

void main (void) {
        
        int spi_handle;
	static char spi_buf[10]; /* or whatever size you might need */

	spi_handle = open("/dev/spi", O_RDWR); 
	if (spi_handle == -1) { 
	  printf ("Could not open device\n");
	}
		
		/* Setup values that remain constant */
	spi_buf[0] = SEL_SPI_CPOL_0; /* P1 is a SPI device */
	spi_buf[1] = 0xfe; /* CE0 is ours, is active low (starting CE settings */
	spi_buf[4] = 0xff; /* set all CEs high after transfer */

		/* Setup data part of buffer for the transfer(s) to be done */
	spi_buf[2] = 0x30; /* tell chip that we want to write to the dir reg */
	spi_buf[3] = 0xff; /* program all pins to output */
	write (spi_handle, spi_buf, 5); /* do the transfer. Note the count (5) */
	read (spi_handle, spi_buf, 5);	/* if we would need the values read in, we would have them
			in spi_buf[2] and spi_buf[3] now.
		*/

	spi_buf[2] = 0x10; /* now write to data reg */
	spi_buf[3] = 0xf9; /* set D1 and D2 low */
	write (spi_handle, spi_buf, 5);	/* transfer the data */
	read (spi_handle, spi_buf, 5);	/* if we would need the values read in, we would have them
			in spi_buf[2] and spi_buf[3] now.
		*/
	close (spi_handle);
}