// *********************************************************************
// *                           E07 - SPI                               *
// *                              SPI                                  *
// *********************************************************************
// SPI communication.
//
// Intended for:
//   HW: - TR-72G + CK-USB-04x development kit
//
//   OS: - v4.06G
//
// Description:
//   This example shows how to use SPI related functions. It should be
//   used with IQRF IDE Terminal to test how SPI communication works.
//   SPI commands from Terminal are interpreted in TR module.
//   Commands:
//   - 1 to 3: Number of LED flashes
//   - i:      Send the "0123456789" string via SPI
//   - d:      Disable SPI peripheral
//   - s:      Stop SPI communication
//   SPI status of the module is displayed in the lower left corner
//   of the IQRF IDE main window.
//
// ---------------------------------------------------------------------
// Online IQRF OS Reference Guide: https://doc.iqrf.org/IQRF-OS-Reference-guide
//
// Copyright (c) IQRF Tech s.r.o.
//
// File:    E07-SPI.c
// Version: v3.00                                   Revision: 11/06/2021
//
// Revision history:
//   v3.00: 11/06/2021  First release for TR-7xG.
//
// *********************************************************************
// Default IQRF include (modify the path according to your setup)
#include "IQRF.h"

// *********************************************************************
void APPLICATION()
{
uns8 i;

    enableSPI();                    // Enable SPI Rx/Tx on background

    appInfo();                      // Copy menu from EEPROM
    copyBufferINFO2COM();           // (from Appl. info) to bufferCOM
    startSPI(32);                   // and then try to send it via SPI

    while (1)
    {
        if (getStatusSPI())         // Update SPIstatus, check SPI busy
                                    //   SPIpacketLength: data length
                                    //   param2: SPI status
            continue;               // Wait until message is picked up

        if (_SPIRX)                 // Anything received?
        {                           // Yes:
            if (!_SPICRCok)         //   CRCM matched?
            {                       //     No:
                startSPI(0);        //       Restart SPI communication
                continue;           //       and continue
            }

            waitDelay(35);          // Wait 350 ms before indication
            clrwdt();

            switch (bufferCOM[0])   // Check what is in bufferCOM
            {                               
                case '3':                       // 3 LED flashes
                            pulseLEDR();
                            waitDelay(35);
                case '2':                       // 2 LED flashes
                            pulseLEDR();
                            waitDelay(35);
                case '1':                       // 1 LED flash
                            pulseLEDR();
                            waitDelay(35);
                            startSPI(0);        // Restart SPI
                            break;
                            
                case 'i':
                            for (i=0; i<10; i++)
                                writeToRAM(bufferCOM+i, i+'0');
                                
                            startSPI(10);
                            break;
                            
                case 'd':
                case 's':
                            if (bufferCOM[0] == 'd')
                                disableSPI();   // Disables SPI

                            if (bufferCOM[0] == 's')
                                stopSPI();      // After stopSPI() SPI
                                                // returns "STOP" status

                            for (i=0; i<20; i++)
                            {
                                pulseLEDR();    // 20 x LED flash
                                waitDelay(50);
                                clrwdt();
                            }
                            
                            enableSPI();        // Both stopped or disabled
                                                //   communication restarted
                            break;              //   after 20 cycles.
                            
                default:                        // Unknown command:
                            stopLEDR();         // Long flash LED (1 s)
                            _LEDR = 1;
                            waitDelay(100);
                            _LEDR = 0;
                            startSPI(0);        // and restart SPI
                            break;
            }
        }
    }
}

// *********************************************************************
#pragma packedCdataStrings 0
//                             00000000001111111111222222222233
#pragma cdata[__EEAPPINFO] =  "Commands: 3 2 1 i d s           "

// *********************************************************************

