/* Copyright (C) Bike Tracker Ltd - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 * Written by Eliot Stock, eliot@biketracker.cc
 */
#ifndef APP_CONFIG_H
#define APP_CONFIG_H

#define NRF_BALLOC_ENABLED 1
#define NRF_CLI_ENABLED 1
#define NRF_CLI_ECHO_STATUS 1
#define NRF_CRYPTO_ENABLED 1
#define NRF_CRYPTO_BACKEND_MICRO_ECC 1
#define NRF_CRYPTO_BACKEND_SW 1
#define NRF_FPRINTF_ENABLED 1
#define NRF_FSTORAGE_ENABLED 1
#define NRF_SDH_ENABLED 1
#define NRF_SDH_BLE_ENABLED 1
#define NRF_SDH_BLE_PERIPHERAL_LINK_COUNT 1
#define NRF_SDH_SOC_ENABLED 1
#define NRF_TWI_MNGR_ENABLED 1
#define TWI_ENABLED 1
#define TWI0_ENABLED 1
// Don't turn Easy DMA on for TWI without first reading this:
// https://devzone.nordicsemi.com/question/116348/app_twi-with-easydma/
#define TWI0_USE_EASY_DMA 0

#define BLE_ADVERTISING_ENABLED 1

#define NRF_BLE_GATT_ENABLED 1

// Maximum tranmission unit. Default and min: 23 bytes. Max: 517 bytes. Using a larger MTU increases efficiency when sending lots of data. We only do that when the central reads back the historical firmware log a little while after it connects. 247 is the value used by Nordic's throughput example.
// Note that there's also a define for NRF_BLE_GATT_MAX_MTU_SIZE in the SDK, which is not what we want.
// TODO (P3): This may be the cause of the following error, and we haven't yet proved that it improves performance when logging.
//   E ble_gatt sd_ble_gap_data_length_update() (request) on connection 0x0 returned unexpected value 0x13.
// However, leaving it out breaks the ability to read back the historical firmware log from the central and generates warnings on RAM size:
//   W nrf_sdh_ble RAM starts at 0x200028A0, can be adjusted to 0x20002230.
//   W nrf_sdh_ble RAM size can be adjusted to 0xDDD0.
#define NRF_SDH_BLE_GATT_MAX_MTU_SIZE 247

// Size of the attribute table in the sof device's RAM. Must be a multiple of 4. sdkconfig.h default: 1408.
// Adding the buttonless DFU characteristic and using the data length extension both require an increase here. For the DLE, see comments in ble_stack_init() and: https://devzone.nordicsemi.com/f/nordic-q-a/30852/data-length-extension-causes-nrf_error_no_mem-on-services-init
// DLE would require an extra 256 bytes.
// 1408 + 256 = 1664
#define NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE 1664
// #define NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE 1416

#define NRF_BLE_CONN_PARAMS_ENABLED 1

#define PEER_MANAGER_ENABLED 1

#define BLE_NUS_ENABLED 1
#define BLE_TPS_ENABLED 1
#define BLE_BAS_ENABLED 1
#define BLE_DFU_ENABLED 1

// This has the sense of "requires" rather than "supports", ie. you must be bonded before you can reset into DFU. The upside of this is that, once the device is bonded, an attacker who is not bonded cannot disable the device by flashing a broken application. The downside is that if we ever discover a bad bug in the code that executes *before* a bond is established and prevents the bond being established, we can never fix that bug with DFU OTA.
#define NRF_DFU_BLE_BUTTONLESS_SUPPORTS_BONDS 1
// For SDK 15.3.0:
#define NRF_DFU_BLE_REQUIRES_BONDS 1
#define NRF_DFU_TRANSPORT_BLE 1

// Without this, entering the bootloader for DFU may fail when sd_ble_gatts_sys_attr_get() returns NRF_ERROR_NOT_FOUND in ble_dfu_bonded.c. See:
//   https://devzone.nordicsemi.com/f/nordic-q-a/18086/dfu-failed-to-reopen-transport-backend/69789#69789
#define IS_SRVC_CHANGED_CHARACT_PRESENT 1

// This seems to duplicate the above define, but is required nonetheless. With it present, nrf_sdh_ble_default_cfg_set() will add the service changed characteristic. See Nordic support ticket:
//   https://devzone.nordicsemi.com/support/216745
#define NRF_SDH_BLE_SERVICE_CHANGED 1

#define CLOCK_ENABLED 1

#define GPIOTE_ENABLED 1
#define GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS 4

#define POWER_ENABLED 1

#define RTC_ENABLED 1
#define RTC0_ENABLED 1
#define RTC_CONFIG_LOG_ENABLED 1

#define SAADC_ENABLED 1

// <0=> 8 bit
// <1=> 10 bit
// <2=> 12 bit
// <3=> 14 bit
#define SAADC_CONFIG_RESOLUTION 2

// Note that the SAADC module has a bug in SDK 13.0.0 through to at least 14.0.0. When gcc's optimisation flag is -Og, nrf_drv_saadc.c won't compile. Leave this off, turn it on but change optimisation to -O3, or wait for a fix in a future SDK release. See: https://devzone.nordicsemi.com/question/128163/nrf_drv_saadcc-doesnt-compile-with-gcc-sdk-1300-evt_to_str-compares-nrf_saadc_limit_t-to-enum/
#define SAADC_CONFIG_LOG_ENABLED 0

#define SPI_ENABLED 1
#define SPI0_ENABLED 0
#define SPI0_USE_EASY_DMA 0
#define SPI1_ENABLED 0
#define SPI1_USE_EASY_DMA 0
#define SPI2_ENABLED 1
#define SPI2_USE_EASY_DMA 1
#define SPI_CONFIG_LOG_ENABLED 0
// <0=> Off
// <1=> Error
// <2=> Warning
// <3=> Info
// <4=> Debug
#define SPI_CONFIG_LOG_LEVEL 2
// <i> Priorities 0,1,4,5 are reserved for SoftDevice
// <0=> 0 (highest)
// <1=> 1
// <2=> 2
// <3=> 3
// <4=> 4
// <5=> 5
// <6=> 6
// <7=> 7
#define SPI_DEFAULT_CONFIG_IRQ_PRIORITY 6
// <0=> NRF_GPIO_PIN_NOPULL
// <1=> NRF_GPIO_PIN_PULLDOWN
// <3=> NRF_GPIO_PIN_PULLUP
#define NRF_SPI_DRV_MISO_PULLUP_CFG 0

#define TIMER_ENABLED 1

// RTC1 prescaler/frequency. See: http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/rtc.html?cp=2_1_0_24#concept_rvn_vkj_sr
// The values here are for the PRESCALER register (so in fact APP_TIMER_CONFIG_RTC_FREQUENCY is not a frequency at all). The frequency of RTC1 ticks is given by:
//   fRTC [kHz] = 32.768 / (PRESCALER + 1)
// Which means the period is then given by:
//       T [ms] = ((PRESCALER + 1) / 32,768) * 1,000
// The overflow period is given by:
//   0xFFFFFF * RTC1 period.
// The counter is only 24 bits wide. With APP_TIMER_CONFIG_RTC_FREQUENCY set to the sdk_config.h default of 0, the RTC1 will overflow at 8.5 mins, which is way too short for us. To find all the places we start a timer, search for "app_timer_start". Our longest timer timeout is an hour, but we also have some that it'd be nice to run forever: the ones that are really counters, not timers.
// PRESCALER,  RTC1 freq,  RTC1 period,  overflow period
//         0,  32,768 Hz,  31  us,        8.6 mins
//         1,  16,384 Hz,  61  us,       17   mins
//         3,   8,192 Hz,  122 us,       34   mins
//         7,   4,096 Hz,  244 us,       68   mins
//        15,   2,048 Hz,  488 us,      136   mins
//        31,   1,024 Hz,  977 us,        4.6 hours
// <0=> 32768 Hz
// <1=> 16384 Hz
// <3=> 8192 Hz
// <7=> 4096 Hz
// <15=> 2048 Hz
// <31=> 1024 Hz
#define APP_TIMER_CONFIG_RTC_FREQUENCY 31

// The sdk_config.h default here is 10. We know we've overflowed that when we see NRF_ERROR_NO_MEM on calling app_timer_start().
#define APP_TIMER_CONFIG_OP_QUEUE_SIZE 14

// See apply_old_config.h in SDK 15.3.0. There's no need for any NRFX_* macros here - they'll only get undef'ed.
#define UART_ENABLED              1
#define UART0_ENABLED             1
#define UART0_USE_EASY_DMA        0
#define UART_EASY_DMA_SUPPORT     0
#define UART_LEGACY_SUPPORT       1
// Note that these aren't defined here, because we pass them to APP_UART_FIFO_INIT() directly instead.
//   UART_DEFAULT_CONFIG_HWFC
//   UART_DEFAULT_CONFIG_PARITY
//   UART_DEFAULT_CONFIG_BAUDRATE
#define UART_DEFAULT_CONFIG_IRQ_PRIORITY 6
// #define UART0_CONFIG_USE_EASY_DMA 0
#define UART_CONFIG_LOG_ENABLED 1
// <0=> Off
// <1=> Error
// <2=> Warning
// <3=> Info
// <4=> Debug
#define UART_CONFIG_LOG_LEVEL 2

#define APP_GPIOTE_ENABLED 1

#define APP_SCHEDULER_ENABLED 1

#define APP_TIMER_ENABLED 1

#define APP_UART_ENABLED 1
#define APP_UART_DRIVER_INSTANCE 0

#define CRC16_ENABLED 1

#define FDS_ENABLED 1
#define FSTORAGE_ENABLED 1

// <2=> NRF_FSTORAGE_SD
#define FDS_BACKEND 2

#define NRF_PWR_MGMT_ENABLED 1
#define NRF_PWR_MGMT_CONFIG_LOG_ENABLED 1
#define NRF_PWR_MGMT_CONFIG_LOG_LEVEL 4 // Debug

#define NRF_STRERROR_ENABLED 1

#define NRF_SECTION_ITER_ENABLED 1

#define NRF_QUEUE_ENABLED 1

// Even PROD builds have logging enabled, but they use less logging.
#define NRF_LOG_ENABLED 1

// The flashlog will buffer everything until it has time to do a flash write, which will take a while. This can be mitigated either by using deferred logging, or by calling NRF_LOG_PROCESS() frequently from application code.
#define NRF_LOG_DEFERRED 0

#define NRF_LOG_BACKEND_FLASH_ENABLED 1
#define NRF_LOG_BACKEND_FLASHLOG_ENABLED 1
#define NRF_LOG_BACKEND_CRASHLOG_ENABLED 1
#define NRF_LOG_BACKEND_FLASH_SER_BUFFER_SIZE 64
#define NRF_LOG_BACKEND_FLASHLOG_QUEUE_SIZE 8
#define NRF_LOG_BACKEND_CRASHLOG_FIFO_SIZE 8
#define NRF_LOG_BACKEND_PAGES 1

#define HARDFAULT_HANDLER_ENABLED 1

// <0=> Off
// <1=> Error
// <2=> Warning
// <3=> Info
// <4=> Debug
// Our own sources all use a specific level based on the build type. Do NOT change NRF_LOG_DEFAULT_LEVEL to be lower than Debug: the level that gets logged for a given module is the *quietest* of the default level and the module level. If you turn the default level down, you have no chance to turn it up again in an indiviudal module. See: https://devzone.nordicsemi.com/f/nordic-q-a/18307/logger-module-config-question-sdk12/70704
#define NRF_LOG_DEFAULT_LEVEL 4

// Get warning level logging from the code that enables the soft device.
#define NRF_SDH_BLE_LOG_ENABLED 1

#define NRF_LOG_USES_TIMESTAMP 0
#define NRF_LOG_BACKEND_SERIAL_USES_UART 0
#define NRF_LOG_BACKEND_SERIAL_USES_RTT 1
#define NRF_LOG_BACKEND_RTT_ENABLED 1
#define NRF_LOG_FILTERS_ENABLED 0

// Size of buffer for partially processed strings. Size of the buffer is a trade-off between RAM usage and processing. If buffer is smaller then strings will often be fragmented. It is recommended to use size which will fit typical log and only the longer one will be fragmented.
#define NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE 64

// Size of upstream buffer. Note that either @ref NRF_LOG_BACKEND_RTT_OUTPUT_BUFFER_SIZE or this value is actually used. It depends on which one is bigger.
#define SEGGER_RTT_CONFIG_BUFFER_SIZE_UP 512

// Maximum number of upstream buffers.
#define SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS 2

// Size of downstream buffer.
#define SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN 16

// Maximum number of downstream buffers.
#define SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS 2

// RTT behavior if the buffer is full. The following modes are supported:
// 0: SKIP  - Do not block, output nothing.
// 1: TRIM  - Do not block, output as much as fits.
// 2: BLOCK - Wait until there is space in the buffer.
#define SEGGER_RTT_CONFIG_DEFAULT_MODE 1

// Writing to RTT retries.  If RTT fails to accept any new data after retries module assumes that host is not active and on next request it will perform only one write attempt. On successful writing, module assumes that host is active and scheme with retry is applied again.
#define NRF_LOG_BACKEND_RTT_TX_RETRY_CNT 3

// Period before retrying writing to RTT
#define NRF_LOG_BACKEND_RTT_TX_RETRY_DELAY_MS 1

// Find some free space in flash after the application for the flash log
// TODO (P1): See components/libraries/bootloader/nrf_bootloader.c. This isn't going to work with the bootloader. We need to find some space statically. And why is the static assert there not being hit when we build?
#define NRF_LOG_BACKEND_FLASH_START_PAGE 0

#define NRF_BLE_QWR_ENABLED 1
#define NRF_BLE_QWR_BLE_OBSERVER_PRIO 2

// These aren't defined anywhere in <SDK>/config/nrf52832/config/sdk_config.h, but probably should be.
#define APP_FIFO_ENABLED 1
#define NRF_BLE_CONN_PARAMS_MAX_SLAVE_LATENCY_DEVIATION 499
#define NRF_BLE_CONN_PARAMS_MAX_SUPERVISION_TIMEOUT_DEVIATION 65535

#endif // APP_CONFIG_H
