/******************** (C) COPYRIGHT 2016 STMicroelectronics ********************
* File Name          : DTM_main.c
* Author             : AMG RF Application Team
* Version            : V1.1.0
* Date               : 27-June-2016
* Description        : BlueNRG-1 DTM main
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS 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.2q
*******************************************************************************/
/** @addtogroup BlueNRG1_demonstrations_applications
 *  BlueNRG-1 DTM application \see DTM_main.c for documentation.
 *
 *@{
 */
/** @} */
/** \cond DOXYGEN_SHOULD_SKIP_THIS
 */

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include "BlueNRG1_conf.h"
#include "bluenrg1_stack.h"
#include "DTM_boot.h"
#include "sleep.h"
#include "transport_layer.h"
#include "hw_config.h"


typedef PACKED(struct) packed_crash_info_s {
  uint8_t dummy[2];
  crash_info_t crash_info;
  uint8_t Debug_Data_Length;
} packed_crash_info_t;

#define RESET_REASON_WDG        ((uint8_t)0x05)
#define RESET_REASON_LOCKUP     ((uint8_t)0x06)
#define RESET_REASON_BOR        ((uint8_t)0x07)
#define RESET_REASON_CRASH      ((uint8_t)0x08)


/*
 ******************************************************************************
 ******************************************************************************
 * The DTM project memory layout is as follows:
 * 
 * ------------- 0x10067FFF
 *             
 *      DTM
 * 
 * ------------- 0x10040800
 *  DTM Updater
 * ------------- 0x10040000
 *
 * The DTM Updater allows to access the memory flash through ACI_HAL commands.
 * The DTM Updater can be activated in the following way:
 *  1) Activation by using ACI_HAL_UPDATER_START
 *  2) Activation by using IO3 pin (high level at start up).
 *     Note: if the IO3 pin is used and is high at start up, this will cause the 
 *     DTM Updater starts. So, to avoid this, the support of the DTM Updater
 *     can be removed.
 * 
 * If the DTM Updater support is not necessary, the DTM Updater code can be
 * removed:
 *  1) Disable/Remove the file DTM_Updater_xxx.c inside the folder project
 *     DTM_Updater.
 *  2) Option/Linker: Change the symbol definition from 
 *     MEMORY_FLASH_APP_OFFSET=0x800 to MEMORY_FLASH_APP_OFFSET=0.
 *
 ****************************************************************************
 ****************************************************************************
*/

int main(void)
{
  packed_crash_info_t crash_info;
  
  /* System Init */
  DTM_SystemInit();
  
  /* Stack Initialization */
  DTM_StackInit();
  
  /* Transport Layer Init */
  transport_layer_init();
  
  /* Get crash info */
  GET_CRASH_INFO(crash_info.crash_info);
  
#ifdef LL_ONLY
  uint8_t Value = 1;
  aci_hal_write_config_data(0x2C, 1, &Value);
  
#else
  RESET_REASON_Type xResetReason;
  uint8_t reset_event[] = {0x04,0xFF,0x03,0x01,0x00,0x01};
  
  /* EVT_BLUE_INITIALIZED */  
  xResetReason = SysCtrl_GetWakeupResetReason();
  if(xResetReason == RESET_WDG) {
    reset_event[5] = RESET_REASON_WDG;
  }
  else if(xResetReason == RESET_LOCKUP) {
    reset_event[5] = RESET_REASON_LOCKUP;
  }
  else if(xResetReason == RESET_BLE_BOR) {
    reset_event[5] = RESET_REASON_BOR;
  }
  if((crash_info.crash_info.signature&0xFFFF0000) == ASSERT_SIGNATURE) {  /* ASSERT_SIGNATURE: 0xBCEC0000 */
    reset_event[5] = RESET_REASON_CRASH;
  }
  send_event(reset_event, sizeof(reset_event), 8);
  
#endif
  if((crash_info.crash_info.signature&0xFFFF0000) == ASSERT_SIGNATURE) { /* ASSERT_SIGNATURE: 0xBCEC0000 */
    uint8_t *ptr=(uint8_t *) &crash_info;
    ptr[0] = 0x04;
    ptr[1] = 0xFF;
    ptr[5] = ptr[2]; // Remap ???
    ptr[2] = 40;
    ptr[3] = 0x03;
    ptr[4] = 0x00;
    crash_info.Debug_Data_Length = 0;
    send_event(ptr, sizeof(crash_info), -1);
  }
  
  while(1) {
    /* BlueNRG-1 stack tick */
    BTLE_StackTick();
    transport_layer_tick();
    BlueNRG_Sleep(SLEEPMODE_NOTIMER, IO_WAKEUP_PIN, 0, 0); // 4: IO11 0: low level
  }
}
