/*
 * THE FOLLOWING FIRMWARE IS PROVIDED: (1) "AS IS" WITH NO WARRANTY; AND 
 * (2)TO ENABLE ACCESS TO CODING INFORMATION TO GUIDE AND FACILITATE CUSTOMER.
 * CONSEQUENTLY, SEMTECH SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
 * CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
 * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
 * CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
 * 
 * Copyright (C) SEMTECH S.A.
 */
/*! 
 * \file       sx1276-Hal.c
 * \brief      SX1276 Hardware Abstraction Layer
 *
 * \version    2.0.B2 
 * \date       Nov 21 2012
 * \author     Miguel Luis
 *
 * Last modified by Miguel Luis on Jun 19 2013
 */
#include <stdint.h>
#include <stdbool.h> 

#include "platform.h"

#if defined( USE_SX1276_RADIO )


#include "spi.h"
#include "sx1276-Hal.h"

/*!
 * SX1276 RESET I/O definitions
 */
#define RESET_IOPORT                                GPIOB
#define RESET_PIN                                   GPIO_Pin_11


/*!
 * SX1276 DIO pins  I/O definitions
 */
#define DIO0_IOPORT                                 GPIOG
#define DIO0_PIN                                    GPIO_Pin_6

void SX1276InitIo( void )
{
	RCC_AHB1PeriphClockCmd ( RCC_AHB1Periph_GPIOB, ENABLE); 
	// Configure RESET PIN as output
	GPIO_InitTypeDef GPIO_InitStruct;
  GPIO_InitStruct.GPIO_Pin = RESET_PIN;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
	GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_Init(RESET_IOPORT, &GPIO_InitStruct);
	//DIO0_PIN.mode(INPUT);
	RCC_AHB1PeriphClockCmd ( RCC_AHB1Periph_GPIOG, ENABLE); 
  GPIO_InitStruct.GPIO_Pin = DIO0_PIN;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_NOPULL;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(DIO0_IOPORT, &GPIO_InitStruct);
	spi_config();
}

void SX1276SetReset( uint8_t state )
{
  GPIO_WriteBit( RESET_IOPORT, RESET_PIN,(BitAction) state );	
}

void SX1276Write( uint8_t addr, uint8_t data )
{
    SX1276WriteBuffer( addr, &data, 1 );
}

void SX1276Read( uint8_t addr, uint8_t *data )
{
    SX1276ReadBuffer( addr, data, 1 );
}

void SX1276WriteBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
{
    uint8_t i;

    //NSS = 0;
		SpiNSSEnable(0);
    SPI2_ReadWriteByte( addr | 0x80 );
    for( i = 0; i < size; i++ )
    {
        SPI2_ReadWriteByte( buffer[i] );
    }

    //NSS = 1;
    SpiNSSEnable(1);
}

void SX1276ReadBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
{
    uint8_t i;

    //NSS = 0;
    SpiNSSEnable(0);

    SPI2_ReadWriteByte( addr & 0x7F );

    for( i = 0; i < size; i++ )
    {
        buffer[i] = SPI2_ReadWriteByte( 0 );
    }

    //NSS = 1;
    SpiNSSEnable( 1 );
}

void SX1276WriteFifo( uint8_t *buffer, uint8_t size )
{
    SX1276WriteBuffer( 0, buffer, size );
}

void SX1276ReadFifo( uint8_t *buffer, uint8_t size )
{
    SX1276ReadBuffer( 0, buffer, size );
}

inline uint8_t SX1276ReadDio0( void )
{
    return GPIO_ReadInputDataBit( DIO0_IOPORT, DIO0_PIN );
}

inline uint8_t SX1276ReadDio1( void )
{
    return 1;
}

inline uint8_t SX1276ReadDio2( void )
{
    return 1;
}

inline uint8_t SX1276ReadDio3( void )
{
    return 1;
}

inline uint8_t SX1276ReadDio4( void )
{
    return 1;
}

inline uint8_t SX1276ReadDio5( void )
{
    return 1;
}

inline void SX1276WriteRxTx( uint8_t txEnable )
{
//    if( txEnable != 0 )
//    {
//        IoePinOn( FEM_CTX_PIN );
//        IoePinOff( FEM_CPS_PIN );
//    }
//    else
//    {
//        IoePinOff( FEM_CTX_PIN );
//        IoePinOn( FEM_CPS_PIN );
//    }
}

#endif // USE_SX1276_RADIO