

#include "predef.h"
#include <stdio.h>
#include <ctype.h>
#include <startnet.h>
#include <autoupdate.h>
#include <dhcpclient.h>
#include <taskmon.h>
#include <smarttrap.h>
#include <NetworkDebug.h>
#include <serial.h>
#include <pins.h>
#include <dspi.h>
#include <string.h>
#include <serial.h>
#include <utils.h>
#include <system.h>
#include <sim.h>
#include <i2cmaster.h>					// For I2C applications
#include <ucos.h>
#include <udp.h>
#include <stdlib.h>
#include <sim5441x.h>


void DSPI_setup(void);

BYTE MUX_TX[8], MUX_RX[8];													// MUX TX Buffer. Not receiving data from the mux so MUX_RX might not be used...


extern "C" {
void UserMain(void * pd);
}

const char * AppName="MUX_Demo";

void UserMain(void * pd)
{
    InitializeStack();
    if (EthernetIP == 0) GetDHCPAddress();
    OSChangePrio(MAIN_PRIO);
    EnableAutoUpdate();

    #ifdef _DEBUG
    InitializeNetworkGDB_and_Wait();
    #endif

    iprintf("Application started\n");

    J1[9].function(0);					// mux input
    J1[5].function(0);					// mux CS line
    J1[5] = 1;
    J1[6].function(0);					// mux WR line
    J1[6] = 1;
    J1[7].function(0);					// mux EN line
    J1[7] = 0;
    J2[33].function(0);					// IO_ON enable
    J2[33] = 0;							// enables all IO
    J2[26].function(0);					// GPIO expander reset
    J2[26] = 1;							// Pull reset High - Chip resets at logic low.

    DSPI_setup();



    while (1)
    {

    	for(int i = 0; i < 32; i++)
    	{
    		// MCP23S08  requires that first, the opcode be sent, then the address, then the Data (SPI only):
    		// Opcode: 0b01000000 - LSB = R/W bit -> 0 = W, 1 = R


    		MUX_TX[0] = 0x40;								// sends address and r/w code to gpio expander - 0b01000000
    		MUX_TX[1] = 0x0A;								// loads register address code to set output latch - OLAT
    		MUX_TX[2] = i;									// writes the current position to OLAT
    		J1[5] = 0;										// MUX CS line drop
    		J1[6] = 0;										// MUX WR line drop
    		J1[7] = 1;										// MUX EN line high
    		DSPIStart(1, MUX_TX, MUX_RX, 3, NULL, true);	// Data to GPIO breakout to switch mux
    		while(!DSPIdone(1));							// release when done
    		J1[6] = 1;										// MUX WR line high
    		J1[5] = 1;										// MUX CS line high
    		J1[7] = 0;										// MUX EN line drop
    		iprintf("MUX reads code %d:", i);
    		if (J1[9]) iprintf ("ON \r\n");
    		else iprintf ("OFF\r\n");
    		OSTimeDly(TICKS_PER_SECOND/2);
    	}

    }

}

void DSPI_setup(void)
{
	// SPI initialization (see DSPItoSerial example):
	// SPI 1 for GPIO break-out
	J2[27].function(1);										//SPI 1 Input
	J2[28].function(1);										//SPI 1 Out
	J2[30].function(1);										//SPI 1 chip select 0
	J2[25].function(1);										//SPI 1 clock

	/*

	DSPIInit() function definition:

	void DSPI1Init( BYTE SPIModule = 1, DWORD Baudrate = 10000000,
	BYTE QueueBitSize = 0x8, BYTE CS = 0x0F,
	BYTE CSPol = 0x0F, BYTE ClkPolarity = 0x0,
	BYTE ClkPhase = 0x1, BOOL DoutHiz = TRUE,
	BYTE csToClockDelay = 0, BYTE delayAfterTransfer = 0 );

	 */

	/*
	 *		For DSPI1 (8 bit GPIO Breakout - see dspi.h for more details)
	 *
	 * 		SPIModule = 1
	 * 		Baudrate = 10MHz
	 * 		QueueBitSize = 8 bits
	 * 		CS = 0x0F (CS low active)
	 * 		CSPol = 0x0F (Follows CS)
	 * 		ClkPolarity = 0x0 (rising edge)
	 * 		ClkPhase = 0x1 (latches on falling edge)
	 * 		DoutHiz = true (high impedance output)
	 * 		csToClockDelay = 0 (delay between CS and Clk going high = close to half a clock cycle)
	 * 		DelayAfterTransfer = 0 (delay between transfers)
	 *
	 */

	DSPIInit(1, 10000000, 0x08, 0x0F, 0x0F, 0x01, TRUE, 0, 0);			// initialize SPI1 for MUX

	// Load startup values into MUX
	// be sure to load MUX values into MYX_TX

	MUX_TX[0] = 0x40;								// sends address and r/w code to gpio expander - 0b01000000
	MUX_TX[1] = 0x00;								// loads register address code for IODIR
	MUX_TX[2] = 0x00;								// sets all GPIO as outputs.
	J1[5] = 0;										// MUX CS line drop
	J1[6] = 0;										// MUX WR line drop
	J1[7] = 1;										// MUX EN line high
	DSPIStart(1, MUX_TX, MUX_RX, 3, NULL, true);	// Data to GPIO breakout to switch mux
	while(!DSPIdone(1));							// release when done
	J1[6] = 1;										// MUX WR line high
	J1[5] = 1;										// MUX CS line high
	J1[7] = 0;										// MUX EN line drop
	iprintf("MUX code sent: 0x%X \r\n", MUX_TX[0]);
	iprintf("MUX code sent: 0x%X \r\n", MUX_TX[1]);
	iprintf("MUX code sent: 0x%X \r\n", MUX_TX[2]);
	OSTimeDly(TICKS_PER_SECOND * 2);

	MUX_TX[0] = 0x40;								// sends address and r/w code to gpio expander - 0b01000000
	MUX_TX[1] = 0x05;								// loads register address code for IOCON
	MUX_TX[2] = 0x38;								// disable sequencing, slew control, enable address pins, nothing else matters
	J1[5] = 0;										// MUX CS line drop
	J1[6] = 0;										// MUX WR line drop
	J1[7] = 1;										// MUX EN line high
	DSPIStart(1, MUX_TX, MUX_RX, 3, NULL, true);	// Data to GPIO breakout to switch mux
	while(!DSPIdone(1));							// release when done
	J1[6] = 1;										// MUX WR line high
	J1[5] = 1;										// MUX CS line high
	J1[7] = 0;										// MUX EN line drop
	iprintf("MUX code sent: 0x%X \r\n", MUX_TX[0]);
	iprintf("MUX code sent: 0x%X \r\n", MUX_TX[1]);
	iprintf("MUX code sent: 0x%X \r\n", MUX_TX[2]);
	OSTimeDly(TICKS_PER_SECOND * 2);

	MUX_TX[0] = 0x40;								// sends address and r/w code to gpio expander - 0b01000000
	MUX_TX[1] = 0x0A;								// loads register address code for OLAT
	MUX_TX[2] = 0x00;								// sets output latch to write all zeros
	J1[5] = 0;										// MUX CS line drop
	J1[6] = 0;										// MUX WR line drop
	J1[7] = 1;										// MUX EN line high
	DSPIStart(1, MUX_TX, MUX_RX, 3, NULL, true);	// Data to GPIO breakout to switch mux
	while(!DSPIdone(1));							// release when done
	J1[6] = 1;										// MUX WR line high
	J1[5] = 1;										// MUX CS line high
	J1[7] = 0;										// MUX EN line drop
	iprintf("MUX code sent: 0x%X \r\n", MUX_TX[0]);
	iprintf("MUX code sent: 0x%X \r\n", MUX_TX[1]);
	iprintf("MUX code sent: 0x%X \r\n", MUX_TX[2]);
	OSTimeDly(TICKS_PER_SECOND * 2);
}


