/******************************************************************************
 gpio_config.c - nRF52840 GPIO configration API Source file
 Version: 0.9.1
 Created on: 07/04/2021
 Created by: Rohit Rajapure

 Copyright (c) 2017 Shalaka Connected Devices LLP.  All rights reserved.
 Software License Agreement

   Redistribution and use in source and binary forms, with or without
   modification, are not permitted.

   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 Shalaka Connected Devices LLP 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 <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "string.h"
#include "app_uart.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "nrf.h"
#include "bsp.h"
#include "nrf_drv_gpiote.h"
#include "nordic_common.h"
#include "sdk_errors.h"
#include "gpio_config.h"
#include "nrf_uart.h"
#include "uart_config.h"
#include "timer_config.h"
/******************************************************************************************
*TASK HANDLES
*
*******************************************************************************************/


/******************************************************************************************
*GLOBAL VARIABLES
*
*******************************************************************************************/
bool boolULP_Pin_Transaction = false;
uint8_t ui8Count=0;
uint8_t ui8TelegramIndex = 0;
uint8_t ui8GetCommandBuffer[BUFFER_SIZE], ui8SetCommandBuffer[BUFFER_SIZE], ui8GetFlagBuffer[BUFFER_SIZE], ui8ResponceValueSend[BUFFER_SIZE], ui8ResponceValueSend2[BUFFER_SIZE], ui8StartFirmwareUpdate[BUFFER_SIZE], ui8TransferFirmware[BUFFER_SIZE], ui8TempGetFlagBuffer[BUFFER_SIZE];
/******************************************************************************************
*EXTERNAL VARIABLES
*
*******************************************************************************************/

/******************************************************************************************
*FUNCTIONS 
*
*******************************************************************************************/
static void Ulp_pin_transaction(void)
{
  nrf_drv_gpiote_in_uninit(ULP_PIN);  //disable ULP pin as input pin
  ulp_output_pin_configuration();   //configure ULP pin as output pin

  nrf_gpio_pin_write(ULP_PIN,1);    //set high ULP pin for 5msec
  nrf_delay_ms(5);  //give 5msec delay to communicate with host controller    

  nrf_drv_gpiote_in_uninit(ULP_PIN);  //disable ULP pin as input pin
  ulp_input_pin_configuration();   //after communication 5msec time is over set ULP pin as input again 
}
/******************************************************************************************
*ULP pin communication transaction task function
*
*******************************************************************************************/
uint8_t GetFlags()
{
    ui8Count=4;
    ui8GetFlagBuffer[0]=SYNC;
    ui8GetFlagBuffer[2]= GET_FLAGS;
    ui8GetFlagBuffer[1]=ui8Count;
    ui8GetFlagBuffer[3]=ui8GenerateCheckSum(ui8GetFlagBuffer, ui8Count);
    //WaitForACK=true;
    return(RESP_ACK);
};

uint8_t GetBootloaderState()
{
    ui8Count=4;
    ui8GetCommandBuffer[0]=SYNC;
    ui8GetCommandBuffer[2]= GET_BOOTLOADER_STATE;
    ui8GetCommandBuffer[1]=ui8Count;
    ui8GetCommandBuffer[3]=ui8GenerateCheckSum(ui8GetCommandBuffer, ui8Count);
   // WaitForACK=true;
    return(RESP_ACK);
};
/******************************************************************************************
*ULP pin interrupt handling routine
*
*******************************************************************************************/
void ulp_in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{  
  nrf_drv_gpiote_clr_task_trigger(ULP_PIN); //clear task
 // boolULP_Pin_Transaction = nrf_gpio_pin_read(ULP_PIN);
  Ulp_pin_transaction();

  GetBootloaderState();
  for(ui8TelegramIndex = 0; ui8TelegramIndex< sizeof(ui8GetCommandBuffer); ui8TelegramIndex++)
  {
        while (app_uart_put(ui8GetCommandBuffer[ui8TelegramIndex]) != NRF_SUCCESS);  //send "Get_Bootloader" telegram to host device to read status
  } 
              
}

/******************************************************************************************
*ULP pin configuration as input pin function
*return void
*******************************************************************************************/
void ulp_input_pin_configuration(void)
{
    uint32_t err_code;
   

    if(!nrf_drv_gpiote_is_init())
    {
      err_code = nrf_drv_gpiote_init();
    }

    nrf_drv_gpiote_in_config_t ulp_in_config = GPIOTE_CONFIG_IN_SENSE_LOTOHI(true);
    ulp_in_config.pull = NRF_GPIO_PIN_PULLDOWN;

    err_code = nrf_drv_gpiote_in_init(ULP_PIN, &ulp_in_config, ulp_in_pin_handler);
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_event_enable(ULP_PIN, true);
}

/******************************************************************************************
*ULP pin configuration as output pin function
*return void
*******************************************************************************************/
void ulp_output_pin_configuration(void)
{
    uint32_t err_code;
    

    if(!nrf_drv_gpiote_is_init())
    {
      err_code = nrf_drv_gpiote_init();
    }

    nrf_drv_gpiote_out_config_t ulp_out_config = GPIOTE_CONFIG_OUT_SIMPLE(false);
   
    err_code = nrf_drv_gpiote_out_init(ULP_PIN, &ulp_out_config);
    APP_ERROR_CHECK(err_code);   
}

/********************************************************************************************************************************/
// This function generate the checksum of command bytes returns checksum byte
/********************************************************************************************************************************/

uint8_t ui8GenerateCheckSum(uint8_t buffer[],uint8_t ui8Count)
{
    uint8_t ui8CheckSum=0,i;
    for(i=0;i<(ui8Count-1);i++)
        ui8CheckSum=ui8CheckSum+buffer[i];
    ui8CheckSum=0xFF-ui8CheckSum;
    ui8CheckSum=ui8CheckSum+1;
    return(ui8CheckSum);
}