/*
 * Copyright (c) 2020, Texas Instruments Incorporated
 * All rights reserved.
 *
 * 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.
 */

#include "ti_msp_dl_config.h"
#define GPIO_CAPTURE_0_C0_PIN_BIT 3
volatile uint32_t gCaptureCnt;
volatile bool gSynced;
volatile bool gCheckCaptures;
uint32_t gLoadValue;
uint32_t uart_data_frame_received[10];
uint32_t number_of_zero_interrupts=0;
uint32_t number_of_CC_interrupts=0;
uint32_t data_bit_value;
uint8_t number_of_bits_in_UART_frame=0;
bool uart_frame_received=0;
uint8_t hex_value=0;
uint8_t uart_data_received=0;
bool uart_frame_erroneous=0;
int main(void)
{

    SYSCFG_DL_init();
    NVIC_EnableIRQ(CAPTURE_0_INST_INT_IRQN);
    number_of_bits_in_UART_frame=sizeof(uart_data_frame_received)/sizeof(uart_data_frame_received[0]);
    DL_Timer_setRepeatCounter(CAPTURE_0_INST, number_of_bits_in_UART_frame);//Set the repeat counter= No. of bits to be received in the UART Frame

    while(1){
    while (uart_frame_received==0){};

        if(uart_data_frame_received[0]!=0){
            uart_frame_erroneous=true;//If 0th Bit i.e. the START bit is not 0, raise an ERROR Flag
        }
        if(uart_data_frame_received[9]!=1){
            uart_frame_erroneous=true;//If 9th Bit i.e. the STOP bit is not 1, raise an ERROR Flag
        }
        for(int i=1;i<number_of_bits_in_UART_frame-1;i++){
            hex_value|= uart_data_frame_received[i]<<(i-1);//Convert the values to HEX
        }
        uart_data_received=hex_value;
        uart_frame_received=0;
}
}
void CAPTURE_0_INST_IRQHandler(void)
{
    switch (DL_TimerA_getPendingInterrupt(CAPTURE_0_INST)) {
        case DL_TIMERA_IIDX_CC0_UP:

            data_bit_value=(DL_GPIO_readPins(GPIO_CAPTURE_0_C0_PORT, GPIO_CAPTURE_0_C0_PIN))>>GPIO_CAPTURE_0_C0_PIN_BIT;//Read the GPIO value, to sample the input state and the Bit value
            uart_data_frame_received[number_of_CC_interrupts++]=data_bit_value;//Store the bit value in an array
            break;

        case DL_TIMERG_IIDX_ZERO:
                uart_frame_received=1;
                DL_TimerA_stopCounter(CAPTURE_0_INST);//Zero interrupt will come once all the UART bits have been sampled, stop the counter when Zero Interrupt comes
                number_of_CC_interrupts=0;
                number_of_zero_interrupts=0;
            break;
        default:
            break;
    }
}
