/*
*  This example should work accompany by other examples 
*	1. project\realtek_ameba1_va0_example\example_sources\gspi	
*	2. project\realtek_ameba1_va0_example\example_sources\gspi_fw_loader
*/

Example Description

	This example describes how to download encrypted firmware to slave and do loopback test. 

	copy "src/main.c" to cover the original ones under the default project path.


Requirement Components:
    2 Ameba DEV_3V0

    Ameba 8195AM(A): Assign as SPI master
	
	 SCK: PC_1(D13)
	  CS: PB_2(D15)
	MOSI: PC_2(D11)
	MISO: PC_3(D12)
        
	 INT: PB_4(D8)

    Ameba 8711AF/AN(B): Assign as GSPI slave (must be configured as boot from SDIO)

	SPI_IN:	PA_4(D5)
	SPI_OUT:PA_2(D7)
	SPI_CLK:PA_3(D6)
	SPI_CS: PA_1(D16)
	SPI_INT:PA_5(D2)

Build bin files:
    In order to run this demo, at least three **.bin should be generated seperately with diffrent example sources.
	1. project\realtek_ameba1_va0_example\example_sources\gspi_fw_downloader\src\main.c
		(A) Ram_all.bin : Host firmware downloader
			Use #define OAT_CIPHER_TYPE to config the encryption status of firmware to be download (ota.bin)
			#define OTA_CIPHER_TYPE		0 // NONE encryption (defualt)
			#define OTA_CIPHER_TYPE		1 // AES_128_CBC encryption

	2. project\realtek_ameba1_va0_example\example_sources\gspi_fw_loader\src\main.c		
		(B) Ram_all.bin : Slave firmware loader

	2. project\realtek_ameba1_va0_example\example_sources\gspi\src\main.c
		(c) Ota.bin:	Slave test firmware. generated by config  #define CONFIG_GPSI_SLAVE 1
		(D) Ota.e.bin:	Encypted ota.bin
			Encrypte ota.bin according to configuration(OTA_CIPHER_TYPE) when generate firmware (A).
			If(#define OTA_CIPHER_TYPE 1),  use EncryptTools (encrypt.bat) to generate ota.e.bin.
		

Demo setup:
	Use ImageTool.exe to load firmware (B) and (C)or(D) to host end flash at customized image erea. 
	(Highly Recommended: Erase the whole flash first)

	#if (#define OTA_CIPHER_TYPE 0)
		1. load (C) to address 0xB1000   // #define OTA_ADDR	0x000B1000 in main.c
	#else(#define OTA_CIPHER_TYPE 1)
		1. load (D) to address 0xB1000   // #define OTA_ADDR	0x000B1000 in main.c
	#endif

	2. load (B) to address 0x91000	// #define RAM_ALL_ADDR	0x00091000 in main.c
	  
	3. Download (A) to host as usual method.


	connect Ameba A and B as given
	
	A:	B:

	SCK --- SPI_CLK
	CS  --- SPI_CS
	MOSI---	SPI_OUT
	MISO---	SPI_IN
	INT ---	SPI_INT

	V3.3--- V3.3
	GND --- GND

Behavior:
	1. Slave end boot from SDIO, waiting master end download firmware (ram_all.bin + ota.bin or ota.e.bin)
	2. Master end download firmware to slave end, and wait slave firmware ready
	3. Master begin to do loopback test.