//#############################################################################
//
// FILE:   empty_driverlib_main.c
//
// TITLE:  Empty Project
//
// Empty Project Example
//
// This example is an empty project setup for Driverlib development.
//
//#############################################################################
//
//
// $Copyright:
// Copyright (C) 2013-2025 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions 
// are met:
// 
//   Redistributions of source code must retain the above copyright 
//   notice, this list of conditions and the following disclaimer.
// 
//   Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the 
//   documentation and/or other materials provided with the   
//   distribution.
// 
//   Neither the name of Texas Instruments Incorporated nor the names of
//   its contributors may be used to endorse or promote products derived
//   from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//#############################################################################

//
// Included Files
//
#include "driverlib.h"
#include "device.h"
#include "board.h"
#include "c2000ware_libraries.h"
#include <stdint.h>

//#define ALT_EXAMPLE

uint16_t id = 0;
uint16_t word = 0;
#pragma DATA_SECTION(test_buf, "ramgs0");
uint16_t test_buf[128];
#pragma DATA_SECTION(example_buf, "ramgs0");
uint16_t example_buf[128];

bool is_transfer = false;

__interrupt void INT_example_spi_RX_ISR(){
    // We disable RX so that when we recieve packets for transfer
    //Interrupt_disable(INT_example_spi_RX);
    // We should not have to do this, primarily to debug DMA
    if(is_transfer){
        // Do nothing
        //SPI_resetRxFIFO(example_spi_BASE);
    } else {
        is_transfer = true;
        SPI_resetTxFIFO(example_spi_BASE);

        id = SPI_readDataBlockingFIFO(example_spi_BASE);
        word = SPI_readDataBlockingFIFO(example_spi_BASE);
        // Since we are peripheral, we would be receiving bytes during transmission.
            
        SPI_writeDataBlockingFIFO(example_spi_BASE, 0xAA);
        //data_output1_TX_DMA_ADDRESS
        #ifdef ALT_EXAMPLE
        DMA_configAddresses(example_spi_TX_DMA_BASE, example_buf,  test_buf);
        DMA_configBurst(example_spi_TX_DMA_BASE, 8, 1, 1); // Burst Size, Src step, dest step 
        DMA_configTransfer(example_spi_TX_DMA_BASE, 16,  1, 1); // Transfer Size, Src Step, dest step 
        #else
        DMA_configAddresses(example_spi_TX_DMA_BASE, example_spi_TX_DMA_ADDRESS,  test_buf);
        DMA_configBurst(example_spi_TX_DMA_BASE, 8, 1, 0); // Burst Size, Src step, dest step (Notice how dest step is zero)
        DMA_configTransfer(example_spi_TX_DMA_BASE, 16,  1, 0); // Transfer Size, Src Step, dest step (Again, notice step is zero)
        #endif
        DMA_startChannel(example_spi_TX_DMA_BASE);

        SPI_clearInterruptStatus(example_spi_BASE, SPI_INT_RXFF);
        Interrupt_clearACKGroup(INT_example_spi_RX_INTERRUPT_ACK_GROUP);
    }
    
    
}

__interrupt void INT_example_spi_TX_ISR(){
    // I can't stop this one from generating from the SysConfig tool
}

__interrupt void INT_example_spi_TX_DMA_ISR(){
    // We have recieved a bunch of messages but we don't care about their content. 
    SPI_resetRxFIFO(example_spi_BASE);
    // We disabled the RX interrupt until we have transmitted all the data from the request. 
    //Interrupt_enable(INT_example_spi_RX);
    is_transfer = false;
    Interrupt_clearACKGroup(INT_example_spi_TX_DMA_INTERRUPT_ACK_GROUP);

}

//
// Main
//
void main(void)
{

    //
    // Initialize device clock and peripherals
    //
    Device_init();

    //
    // Disable pin locks and enable internal pull-ups.
    //
    Device_initGPIO();

    //
    // Initialize PIE and clear PIE registers. Disables CPU interrupts.
    //
    Interrupt_initModule();

    //
    // Initialize the PIE vector table with pointers to the shell Interrupt
    // Service Routines (ISR).
    //
    Interrupt_initVectorTable();

    //
    // PinMux and Peripheral Initialization
    //
    Board_init();

    //
    // C2000Ware Library initialization
    //
    C2000Ware_libraries_init();

    //
    // Enable Global Interrupt (INTM) and real time interrupt (DBGM)
    //
    EINT;
    ERTM;
    
    for(uint16_t i = 0; i < 128; i++){
        test_buf[i] = (i << 8) + i;
    }


    while(1)
    {
        
    }
}

//
// End of File
//
