/*******************************************************************************
  File Name:
    nm_common.c

  Summary:
    This module contains common APIs implementations.

  Description:
    This module contains common APIs implementations.
 *******************************************************************************/

//DOM-IGNORE-BEGIN
/*******************************************************************************
* Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries.
*
* Subject to your compliance with these terms, you may use Microchip software
* and any derivatives exclusively with Microchip products. It is your
* responsibility to comply with third party license terms applicable to your
* use of third party software (including open source software) that may
* accompany Microchip software.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
* PARTICULAR PURPOSE.
*
* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*******************************************************************************/

#include "nm_common.h"
#include "timer.h"
#include "BoardCfg.h"
//#include "wdrv_winc_common.h"
//#include "wdrv_winc_gpio.h"



void __ISR(_EXTERNAL_0_VECTOR , ipl3) chip_isr(void)
//static void chip_isr(void)
{
	//JFM WINC LOW LEVEL
	//JFM à valider si ça fuck pas la patente vu qu'on est en interrupt...
	//m2m_wifi_handle_events();
    
    IFS0bits.INT0IF = 0;
}

/*!
 *  @fn         nm_sleep
 *  @brief      Sleep in units of mSec
 *  @param[IN]  u32TimeMsec
 *              Time in milliseconds
 */
void nm_sleep(uint32_t u32TimeMsec)
{
	Sleep(u32TimeMsec);
}

/*!
 *  @fn     nm_reset
 *  @brief  Reset NMC3400 SoC by setting CHIP_EN and RESET_N signals low,
 *           CHIP_EN high then RESET_N high
 */
void nm_reset(void)
{
	//JFM WINC LOW LEVEL
    WIFI_CHP_EN_PIN = 0;
    WIFI_CHP_RST_PIN = 0;
//    Sleep(1); //JFM
    Sleep(100);
    WIFI_CHP_EN_PIN = 1;
//    Sleep(5);  JFM
    Sleep(150);
    WIFI_CHP_RST_PIN = 1;
}

/* Convert hexchar to value 0-15 */
static uint8_t hexchar_2_val(uint8_t ch)
{
    ch -= 0x30;
    if (ch <= 9)
        return ch;
    ch |= 0x20;
    ch -= 0x31;
    if (ch <= 5)
        return ch + 10;
    return 0xFF;
}

/* Convert hexstring to bytes */
int8_t hexstr_2_bytes(uint8_t *pu8Out, uint8_t *pu8In, uint8_t u8SizeOut)
{
    while(u8SizeOut--)
    {
        uint8_t   u8Out = hexchar_2_val(*pu8In++);
        if (u8Out > 0xF)
            return M2M_ERR_INVALID_ARG;
        *pu8Out = u8Out * 0x10;
        u8Out = hexchar_2_val(*pu8In++);
        if (u8Out > 0xF)
            return M2M_ERR_INVALID_ARG;
        *pu8Out += u8Out;
        pu8Out++;
    }
    return M2M_SUCCESS;
}

//DOM-IGNORE-END
