/******************************************************************************
 * @file rf_debug_receiver.c
 * @brief Remote Serial Receiver for TDxxxx RF modules.
 * @author Telecom Design S.A.
 * @version 1.0
 ******************************************************************************
 * @section License
 * <b>(C) Copyright 2012-2015 Telecom Design S.A., http://www.telecomdesign.fr</b>
 ******************************************************************************
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 *
 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Telecom Design SA has no
 * obligation to support this Software. Telecom Design SA is providing the
 * Software "AS IS", with no express or implied warranties of any kind,
 * including, but not limited to, any implied warranties of merchantability
 * or fitness for any particular purpose or warranties against infringement
 * of any proprietary rights of a third party.
 *
 * Telecom Design SA will not be liable for any consequential, incidental, or
 * special damages, or any other relief, or for any claim by any third party,
 * arising from your use of this Software.
 *
  ******************************************************************************/

#include <stdint.h>
#include <stdbool.h>

#include <efm32.h>

#include <em_gpio.h>

#include <td_core.h>
#include <td_uart.h>
#include <td_printf.h>
#include <td_rtc.h>
#include <td_utils.h>

#include <td_lan.h>

/* This file declare all "dynamic" library data. It should be last included file
 * Standard size value can be override before including this file
 */
#define FORCE_RADIO_RESET 0
#define TD_SENSOR_USE_CODE 0
#define TD_GEOLOC_USE_CODE 0
#include <td_config.h>

/*******************************************************************************
 *************************   DEFINES   *****************************************
 ******************************************************************************/

#define FREQUENCY		869000000			/**< Debug Channel frequency */
#define POWER_LEVEL		14					/**< Debug Transmit power level */

/*******************************************************************************
 ******************************  CONSTANTS  ************************************
 ******************************************************************************/

static TD_LAN_frame_t RX;
static uint8_t Timer;

/*******************************************************************************
 **************************   PUBLIC FUNCTIONS   *******************************
 ******************************************************************************/

// Frame receive callback
int FrameReceived(TD_LAN_frame_t *tx_frame, TD_LAN_frame_t *rx_frame)
{
	//print data, padding 0 will be ignored
	tfp_printf((char *)rx_frame->payload);

	//restart reception
	TD_LAN_ReceiveFrame(0, 0, &RX);

	//restart scheduler if we received a frame
	TD_SCHEDULER_Restart(Timer);
	return 0;
}

// Restart reception callback
static void Restart(uint32_t arg, uint8_t rep)
{
	TD_LAN_Abort();
	TD_LAN_ReceiveFrame(0, 0, &RX);
}

/***************************************************************************//**
 * @brief
 *   User setup function.
 ******************************************************************************/
void TD_USER_Setup(void)
{

    init_printf(TD_UART_Init(115200, true, false),
    		TD_UART_Putc,
    		TD_UART_Start,
    		TD_UART_Stop);

    // Initialize the LAN
    if (TD_LAN_Init(true, 0, 0) == false) {
    	printf("Problem in init\r\n");
	}

    // Set the operating frequency and power level
    if (TD_LAN_SetFrequencyLevel(FREQUENCY, POWER_LEVEL) == false) {
    	printf("Problem while setting frequency/level\r\n");
    }


    //LAN does not like to stay in continuous reception longer than 512s
    //due to rtc limit. Stop and restart if no frame receive.
    Timer = TD_SCHEDULER_Append(500,0,0,0xFF, Restart,0);

    //Start continuous reception
    TD_LAN_SetUserCallback(FrameReceived);
    TD_LAN_ReceiveFrame(0, 0, &RX);
}

/***************************************************************************//**
 * @brief
 *   User loop function.
 ******************************************************************************/
void TD_USER_Loop(void)
{
	TD_LAN_Process();
}
