/*********************************************************************
*                   (c) SEGGER Microcontroller GmbH                  *
*                        The Embedded Experts                        *
*                           www.segger.com                           *
**********************************************************************

-------------------------- END-OF-HEADER -----------------------------

File    : main.c
Purpose : Generic application start

*/

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>

#include "nordic_common.h"
#include "nrf.h"
#include "app_error.h"
#include "nrf_drv_clock.h"

//#include "ble_hci.h"
//#include "ble_gap.h"
//#include "ble_advdata.h"
//#include "ble_advertising.h"
#include "nrf_gpio.h"
#include "softdevice_handler.h"
//#include "ble_srv_common.h"
//#include "ble_conn_params.h"
//#include "boards.h"
//#include "ble_db_discovery.h"
//#include "app_timer.h"
//#include "bsp.h"
//#include "bsp_btn_ble.h"
//#include "peer_manager.h"
#include "fds.h"
#include "fstorage.h"
//#include "ble_conn_state.h"
#define NRF_LOG_MODULE_NAME "APP"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
//#include "nrf_log_default_backends.h"

//--------------------------------------------------------------------------------------------

#define NRF_CLOCK_LFCLKSRC      {.source        = NRF_CLOCK_LF_SRC_XTAL,            \
                                 .rc_ctiv       = 0,                                \
                                 .rc_temp_ctiv  = 0,                                \
                                 .xtal_accuracy = NRF_CLOCK_LF_XTAL_ACCURACY_20_PPM}
/*
#define NRF_CLOCK_LFCLKSRC      {.source        = NRF_CLOCK_LF_SRC_RC,            \
                                 .rc_ctiv       = 16,                                \
                                 .rc_temp_ctiv  = 0,                                \
                                 .xtal_accuracy = NRF_CLOCK_LF_XTAL_ACCURACY_500_PPM}

*/

#define CENTRAL_LINK_COUNT              0                                           /**< Number of central links used by the application. When changing this number remember to adjust the RAM settings*/
#define PERIPHERAL_LINK_COUNT           1                                           /**< Number of peripheral links used by the application. When changing this number remember to adjust the RAM settings*/

//--------------------------------------------------------------------------------------------

static void clock_init(void)
{
    ret_code_t err_code = nrf_drv_clock_init();
    APP_ERROR_CHECK(err_code);
}

//------------------------------------------------------------------------------------

static void log_init(void)
{
    ret_code_t err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    //NRF_LOG_DEFAULT_BACKENDS_INIT();
}

//--------------------------------------------------------------------------------------------


static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
{
    //ble_conn_state_on_ble_evt(p_ble_evt);
    //pm_on_ble_evt(p_ble_evt);
    //ble_db_discovery_on_ble_evt(&m_ble_db_discovery, p_ble_evt);
    //ble_conn_params_on_ble_evt(p_ble_evt);
    //ble_ans_c_on_ble_evt(&m_ans_c, p_ble_evt);
    //bsp_btn_ble_on_ble_evt(p_ble_evt);
    //on_ble_evt(p_ble_evt);
    //ble_advertising_on_ble_evt(p_ble_evt);
}

//--------------------------------------------------------------------------------------------

/**@brief Function for dispatching a system event to interested modules.
 *
 * @details This function is called from the System event interrupt handler after a system
 *          event has been received.
 *
 * @param[in] sys_evt  System stack event.
 */
static void sys_evt_dispatch(uint32_t sys_evt)
{
    // Dispatch the system event to the fstorage module, where it will be
    // dispatched to the Flash Data Storage (FDS) module.
    fs_sys_event_handler(sys_evt);

    // Dispatch to the Advertising module last, since it will check if there are any
    // pending flash operations in fstorage. Let fstorage process system events first,
    // so that it can report correctly to the Advertising module.
    
    //ble_advertising_on_sys_evt(sys_evt);
}

//--------------------------------------------------------------------------------------------

static void ble_stack_init(void)
{
    uint32_t err_code;

    nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC;

    // Initialize the SoftDevice handler module.
    SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL);

    ble_enable_params_t ble_enable_params;
    err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT,
                                                    PERIPHERAL_LINK_COUNT,
                                                    &ble_enable_params);
    APP_ERROR_CHECK(err_code);

    // Check the ram settings against the used number of links
    CHECK_RAM_START_ADDR(CENTRAL_LINK_COUNT, PERIPHERAL_LINK_COUNT);

    // Enable BLE stack.
#if (NRF_SD_BLE_API_VERSION == 3)
    ble_enable_params.gatt_enable_params.att_mtu = NRF_BLE_MAX_MTU_SIZE;
#endif
    err_code = softdevice_enable(&ble_enable_params);
    APP_ERROR_CHECK(err_code);

    // Register with the SoftDevice handler module for BLE events.
    err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
    APP_ERROR_CHECK(err_code);

    // Register with the SoftDevice handler module for System events.
    err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
    APP_ERROR_CHECK(err_code);
}


//--------------------------------------------------------------------------------------------

void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
{
    NRF_LOG_ERROR("Fatal\r\n");
    NRF_LOG_FINAL_FLUSH();
    // On assert, the system can only recover with a reset.
#ifndef DEBUG
    NVIC_SystemReset();
#else
    app_error_save_and_stop(id, pc, info);
    while(1){ id++; id--; };
#endif // DEBUG
}

//--------------------------------------------------------------------------------------------


void main(void) 
{
  int i;

  log_init();
  clock_init();
 

  NRF_LOG_INFO("Start\n");
  
  ble_stack_init();
  //nrf_gpio_cfg_output( 2 );
  printf("LFCK %u\n", nrf_drv_clock_lfclk_is_running() );

  for (i = 0; i <1; i++) {
    printf("Hello World %d!\n", i);
    //nrf_gpio_pin_toggle( 2 );
  }
  do {
    i++;
  } while (1);
}

/*************************** End of file ****************************/
