From 74ff9808943467b9f2a713445ae2d9ec082b2616 Mon Sep 17 00:00:00 2001
From: Aapo Torkkeli <aamato@utu.fi>
Date: Wed, 6 Nov 2019 18:46:46 +0200
Subject: [PATCH] Project that compiles.

Runs both imu and ble simultanously.
---
 project/ble.c                                 | 195 +++++++++++++++---
 project/imu.c                                 | 100 +++++++++
 project/main.c                                | 184 +----------------
 project/pca10040/s132/ses/ble.h               |  87 ++++++++
 .../ble_app_template_pca10040_s132.emProject  |   4 +
 project/pca10040/s132/ses/imu.h               |  28 +++
 6 files changed, 385 insertions(+), 213 deletions(-)
 create mode 100644 project/pca10040/s132/ses/ble.h
 create mode 100644 project/pca10040/s132/ses/imu.h

diff --git a/project/ble.c b/project/ble.c
index 2afc9dc..50911e7 100644
--- a/project/ble.c
+++ b/project/ble.c
@@ -1,24 +1,153 @@
+/**
+ * Copyright (c) 2014 - 2019, Nordic Semiconductor ASA
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form, except as embedded into a Nordic
+ *    Semiconductor ASA integrated circuit in a product or a software update for
+ *    such product, must reproduce the above copyright notice, this list of
+ *    conditions and the following disclaimer in the documentation and/or other
+ *    materials provided with the distribution.
+ *
+ * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
+ *    contributors may be used to endorse or promote products derived from this
+ *    software without specific prior written permission.
+ *
+ * 4. This software, with or without modification, must only be used with a
+ *    Nordic Semiconductor ASA integrated circuit.
+ *
+ * 5. Any software provided in binary form under this license must not be reverse
+ *    engineered, decompiled, modified and/or disassembled.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+/** @file
+ *
+ * @defgroup ble_sdk_app_template_main main.c
+ * @{
+ * @ingroup ble_sdk_app_template
+ * @brief Template project main file.
+ *
+ * This file contains a template for creating a new application. It has the code necessary to wakeup
+ * from button, advertise, get a connection restart advertising on disconnect and if no new
+ * connection created go back to system-off mode.
+ * It can easily be used as a starting point for creating a new application, the comments identified
+ * with 'YOUR_JOB' indicates where and how you can customize.
+ */
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "nordic_common.h"
+#include "nrf.h"
+#include "app_error.h"
+#include "ble.h"
+#include "ble_hci.h"
+#include "ble_srv_common.h"
+#include "ble_advdata.h"
+#include "ble_advertising.h"
+#include "ble_conn_params.h"
+#include "nrf_sdh.h"
+#include "nrf_sdh_soc.h"
+#include "nrf_sdh_ble.h"
+#include "app_timer.h"
+#include "fds.h"
+#include "peer_manager.h"
+#include "peer_manager_handler.h"
+#include "bsp_btn_ble.h"
+#include "sensorsim.h"
+#include "ble_conn_state.h"
+#include "nrf_ble_gatt.h"
+#include "nrf_ble_qwr.h"
+#include "nrf_pwr_mgmt.h"
+#include "nrf_log.h"
+#include "nrf_log_ctrl.h"
+#include "nrf_log_default_backends.h"
+#include "nrf_temp.h"
+#include "ble_hts.h"
+#include "ble_bas.h"
+
+
+#define DEVICE_NAME                     "nRF_Aapo"                       /**< Name of device. Will be included in the advertising data. */
+#define MANUFACTURER_NAME               "NordicSemiconductor"                   /**< Manufacturer. Will be passed to Device Information Service. */
+#define APP_ADV_INTERVAL                300                                     /**< The advertising interval (in units of 0.625 ms. This value corresponds to 187.5 ms). */
+
+#define APP_ADV_DURATION                18000                                   /**< The advertising duration (180 seconds) in units of 10 milliseconds. */
+#define APP_BLE_OBSERVER_PRIO           3                                       /**< Application's BLE observer priority. You shouldn't need to modify this value. */
+#define APP_BLE_CONN_CFG_TAG            1                                       /**< A tag identifying the SoftDevice BLE configuration. */
+
+#define MIN_CONN_INTERVAL               MSEC_TO_UNITS(100, UNIT_1_25_MS)        /**< Minimum acceptable connection interval (0.1 seconds). */
+#define MAX_CONN_INTERVAL               MSEC_TO_UNITS(200, UNIT_1_25_MS)        /**< Maximum acceptable connection interval (0.2 second). */
+#define SLAVE_LATENCY                   0                                       /**< Slave latency. */
+#define CONN_SUP_TIMEOUT                MSEC_TO_UNITS(4000, UNIT_10_MS)         /**< Connection supervisory timeout (4 seconds). */
+
+#define FIRST_CONN_PARAMS_UPDATE_DELAY  APP_TIMER_TICKS(5000)                   /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
+#define NEXT_CONN_PARAMS_UPDATE_DELAY   APP_TIMER_TICKS(30000)                  /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
+#define MAX_CONN_PARAMS_UPDATE_COUNT    3                                       /**< Number of attempts before giving up the connection parameter negotiation. */
+
+#define SEC_PARAM_BOND                  1                                       /**< Perform bonding. */
+#define SEC_PARAM_MITM                  0                                       /**< Man In The Middle protection not required. */
+#define SEC_PARAM_LESC                  0                                       /**< LE Secure Connections not enabled. */
+#define SEC_PARAM_KEYPRESS              0                                       /**< Keypress notifications not enabled. */
+#define SEC_PARAM_IO_CAPABILITIES       BLE_GAP_IO_CAPS_NONE                    /**< No I/O capabilities. */
+#define SEC_PARAM_OOB                   0                                       /**< Out Of Band data not available. */
+#define SEC_PARAM_MIN_KEY_SIZE          7                                       /**< Minimum encryption key size. */
+#define SEC_PARAM_MAX_KEY_SIZE          16                                      /**< Maximum encryption key size. */
+
+#define DEAD_BEEF                       0xDEADBEEF                              /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
+
+// Determines if temperature type is given as characteristic (1) or as a field of measurement (0)
+#define TEMP_TYPE_AS_CHARACTERISTIC 0
+
+
+NRF_BLE_GATT_DEF(m_gatt);                                                       /**< GATT module instance. */
+NRF_BLE_QWR_DEF(m_qwr);                                                         /**< Context for the Queued Write module.*/
+BLE_ADVERTISING_DEF(m_advertising);                                             /**< Advertising module instance. */
+
+uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;                        /**< Handle of the current connection. */
+
+BLE_HTS_DEF(m_hts); // Macro for defining a ble_hts instance
+BLE_BAS_DEF(m_bas); // Macro for defining a ble_bas instance
+
+
 // Flag to keep track of when an indication confirmation is pending
-static bool m_hts_meas_ind_conf_pending = false;
+bool m_hts_meas_ind_conf_pending = false;
 volatile uint32_t hts_counter; // hold dummy hts data
 
 // Function declarations
-static void on_hts_evt(ble_hts_t * p_hts, ble_hts_evt_t * p_evt);
-static void temperature_measurement_send(void);
+void on_hts_evt(ble_hts_t * p_hts, ble_hts_evt_t * p_evt);
+void temperature_measurement_send(void);
 
 // YOUR_JOB: Use UUIDs for service(s) used in your application.
-static ble_uuid_t m_adv_uuids[] =                                               /**< Universally unique service identifiers. */
+ble_uuid_t m_adv_uuids[] =                                               /**< Universally unique service identifiers. */
 {
     {BLE_UUID_DEVICE_INFORMATION_SERVICE, BLE_UUID_TYPE_BLE}
 };
 
-static void advertising_start(bool erase_bonds);
+void advertising_start(bool erase_bonds);
 
 /*
 * Function for generating a dummy temperature information packet.
 */
-static void generate_temperature(ble_hts_meas_t * p_meas) {
-    static ble_date_time_t time_stamp = { 2018, 16, 10, 16, 15, 0 };
+void generate_temperature(ble_hts_meas_t * p_meas) {
+    ble_date_time_t time_stamp = { 2018, 16, 10, 16, 15, 0 };
 
     uint32_t celciusX100;
 
@@ -49,7 +178,7 @@ static void generate_temperature(ble_hts_meas_t * p_meas) {
 /*
 * Function for handling the Health Thermometer Service events.
 */
-static void on_hts_evt(ble_hts_t * p_hts, ble_hts_evt_t * p_evt) {
+void on_hts_evt(ble_hts_t * p_hts, ble_hts_evt_t * p_evt) {
     switch (p_evt->evt_type) {
     case BLE_HTS_EVT_INDICATION_ENABLED:
         // Indication has been enabled, send a single temperature measurement
@@ -64,7 +193,7 @@ static void on_hts_evt(ble_hts_t * p_hts, ble_hts_evt_t * p_evt) {
     }
 }
 
-static void temperature_measurement_send(void) {
+void temperature_measurement_send(void) {
     ble_hts_meas_t hts_meas; //Health Thermometer Service measurement structure
     ret_code_t
     err_code;
@@ -108,7 +237,7 @@ void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name) {
  *
  * @param[in] p_evt  Peer Manager event.
  */
-static void pm_evt_handler(pm_evt_t const * p_evt) {
+void pm_evt_handler(pm_evt_t const * p_evt) {
     pm_handler_on_pm_evt(p_evt);
     pm_handler_flash_clean(p_evt);
 
@@ -127,7 +256,7 @@ static void pm_evt_handler(pm_evt_t const * p_evt) {
  *
  * @details Initializes the timer module. This creates and starts application timers.
  */
-static void timers_init(void) {
+void timers_init(void) {
     // Initialize timer module.
     ret_code_t err_code = app_timer_init();
     APP_ERROR_CHECK(err_code);
@@ -149,7 +278,7 @@ static void timers_init(void) {
  * @details This function sets up all the necessary GAP (Generic Access Profile) parameters of the
  *          device including the device name, appearance, and the preferred connection parameters.
  */
-static void gap_params_init(void) {
+void gap_params_init(void) {
     ret_code_t              err_code;
     ble_gap_conn_params_t   gap_conn_params;
     ble_gap_conn_sec_mode_t sec_mode;
@@ -179,7 +308,7 @@ static void gap_params_init(void) {
 
 /**@brief Function for initializing the GATT module.
  */
-static void gatt_init(void) {
+void gatt_init(void) {
     ret_code_t err_code = nrf_ble_gatt_init(&m_gatt, NULL);
     APP_ERROR_CHECK(err_code);
 }
@@ -192,7 +321,7 @@ static void gatt_init(void) {
  *
  * @param[in]   nrf_error   Error code containing information about what went wrong.
  */
-static void nrf_qwr_error_handler(uint32_t nrf_error) {
+void nrf_qwr_error_handler(uint32_t nrf_error) {
     APP_ERROR_HANDLER(nrf_error);
 }
 
@@ -207,7 +336,7 @@ static void nrf_qwr_error_handler(uint32_t nrf_error) {
  * @param[in]   p_evt          Event received from the YY Service.
  *
  *
-static void on_yys_evt(ble_yy_service_t     * p_yy_service,
+void on_yys_evt(ble_yy_service_t     * p_yy_service,
                        ble_yy_service_evt_t * p_evt)
 {
     switch (p_evt->evt_type)
@@ -225,7 +354,7 @@ static void on_yys_evt(ble_yy_service_t     * p_yy_service,
 
 /**@brief Function for initializing services that will be used by the application.
  */
-static void services_init(void) {
+void services_init(void) {
     ret_code_t         err_code;
     nrf_ble_qwr_init_t qwr_init = {0};
 
@@ -278,7 +407,7 @@ static void services_init(void) {
  *
  * @param[in] p_evt  Event received from the Connection Parameters Module.
  */
-static void on_conn_params_evt(ble_conn_params_evt_t * p_evt) {
+void on_conn_params_evt(ble_conn_params_evt_t * p_evt) {
     ret_code_t err_code;
 
     if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED) {
@@ -292,14 +421,14 @@ static void on_conn_params_evt(ble_conn_params_evt_t * p_evt) {
  *
  * @param[in] nrf_error  Error code containing information about what went wrong.
  */
-static void conn_params_error_handler(uint32_t nrf_error) {
+void conn_params_error_handler(uint32_t nrf_error) {
     APP_ERROR_HANDLER(nrf_error);
 }
 
 
 /**@brief Function for initializing the Connection Parameters module.
  */
-static void conn_params_init(void) {
+void conn_params_init(void) {
     ret_code_t             err_code;
     ble_conn_params_init_t cp_init;
 
@@ -321,7 +450,7 @@ static void conn_params_init(void) {
 
 /**@brief Function for starting timers.
  */
-static void application_timers_start(void) {
+void application_timers_start(void) {
     /* YOUR_JOB: Start your timers. below is an example of how to start a timer.
        ret_code_t err_code;
        err_code = app_timer_start(m_app_timer_id, TIMER_INTERVAL, NULL);
@@ -334,7 +463,7 @@ static void application_timers_start(void) {
  *
  * @note This function will not return.
  */
-static void sleep_mode_enter(void) {
+void sleep_mode_enter(void) {
     ret_code_t err_code;
 
     err_code = bsp_indication_set(BSP_INDICATE_IDLE);
@@ -356,7 +485,7 @@ static void sleep_mode_enter(void) {
  *
  * @param[in] ble_adv_evt  Advertising event.
  */
-static void on_adv_evt(ble_adv_evt_t ble_adv_evt) {
+void on_adv_evt(ble_adv_evt_t ble_adv_evt) {
     ret_code_t err_code;
 
     switch (ble_adv_evt) {
@@ -381,7 +510,7 @@ static void on_adv_evt(ble_adv_evt_t ble_adv_evt) {
  * @param[in]   p_ble_evt   Bluetooth stack event.
  * @param[in]   p_context   Unused.
  */
-static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) {
+void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) {
     ret_code_t err_code = NRF_SUCCESS;
 
     switch (p_ble_evt->header.evt_id) {
@@ -438,7 +567,7 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) {
  *
  * @details Initializes the SoftDevice and the BLE event interrupt.
  */
-static void ble_stack_init(void) {
+void ble_stack_init(void) {
     ret_code_t err_code;
 
     err_code = nrf_sdh_enable_request();
@@ -461,7 +590,7 @@ static void ble_stack_init(void) {
 
 /**@brief Function for the Peer Manager initialization.
  */
-static void peer_manager_init(void) {
+void peer_manager_init(void) {
     ble_gap_sec_params_t sec_param;
     ret_code_t           err_code;
 
@@ -494,7 +623,7 @@ static void peer_manager_init(void) {
 
 /**@brief Clear bond information from persistent storage.
  */
-static void delete_bonds(void) {
+void delete_bonds(void) {
     ret_code_t err_code;
 
     NRF_LOG_INFO("Erase bonds!");
@@ -508,7 +637,7 @@ static void delete_bonds(void) {
  *
  * @param[in]   event   Event generated when button is pressed.
  */
-static void bsp_event_handler(bsp_event_t event) {
+void bsp_event_handler(bsp_event_t event) {
     ret_code_t err_code;
 
     switch (event) {
@@ -545,7 +674,7 @@ static void bsp_event_handler(bsp_event_t event) {
 
 /**@brief Function for initializing the Advertising functionality.
  */
-static void advertising_init(void) {
+void advertising_init(void) {
     ret_code_t             err_code;
     ble_advertising_init_t init;
 
@@ -574,7 +703,7 @@ static void advertising_init(void) {
  *
  * @param[out] p_erase_bonds  Will be true if the clear bonding button was pressed to wake the application up.
  */
-static void buttons_leds_init(bool * p_erase_bonds) {
+void buttons_leds_init(bool * p_erase_bonds) {
     ret_code_t err_code;
     bsp_event_t startup_event;
 
@@ -590,7 +719,7 @@ static void buttons_leds_init(bool * p_erase_bonds) {
 
 /**@brief Function for initializing the nrf log module.
  */
-static void log_init(void) {
+void log_init(void) {
     ret_code_t err_code = NRF_LOG_INIT(NULL);
     APP_ERROR_CHECK(err_code);
 
@@ -600,7 +729,7 @@ static void log_init(void) {
 
 /**@brief Function for initializing power management.
  */
-static void power_management_init(void) {
+void power_management_init(void) {
     ret_code_t err_code;
     err_code = nrf_pwr_mgmt_init();
     APP_ERROR_CHECK(err_code);
@@ -611,7 +740,7 @@ static void power_management_init(void) {
  *
  * @details If there is no pending log operation, then sleep until next the next event occurs.
  */
-static void idle_state_handle(void) {
+void idle_state_handle(void) {
     if (NRF_LOG_PROCESS() == false) {
         nrf_pwr_mgmt_run();
     }
@@ -620,7 +749,7 @@ static void idle_state_handle(void) {
 
 /**@brief Function for starting advertising.
  */
-static void advertising_start(bool erase_bonds) {
+void advertising_start(bool erase_bonds) {
     if (erase_bonds == true) {
         delete_bonds();
         // Advertising is started by PM_EVT_PEERS_DELETED_SUCEEDED event
diff --git a/project/imu.c b/project/imu.c
index 7452480..caba0b9 100644
--- a/project/imu.c
+++ b/project/imu.c
@@ -1,3 +1,103 @@
+/**
+ * Copyright (c) 2009 - 2019, Nordic Semiconductor ASA
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form, except as embedded into a Nordic
+ *    Semiconductor ASA integrated circuit in a product or a software update for
+ *    such product, must reproduce the above copyright notice, this list of
+ *    conditions and the following disclaimer in the documentation and/or other
+ *    materials provided with the distribution.
+ *
+ * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
+ *    contributors may be used to endorse or promote products derived from this
+ *    software without specific prior written permission.
+ *
+ * 4. This software, with or without modification, must only be used with a
+ *    Nordic Semiconductor ASA integrated circuit.
+ *
+ * 5. Any software provided in binary form under this license must not be reverse
+ *    engineered, decompiled, modified and/or disassembled.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+/** @file
+* @brief Example template project.
+* @defgroup nrf_templates_example Example Template
+*
+*/
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "nrf_drv_spi.h"
+#include "bmi160.h"
+#include "nrf_delay.h"
+#include "nrf_drv_gpiote.h"
+#include "fdacoefs.h"
+#include "arm_math.h"
+
+
+#define SPI_INSTANCE 0 // SPI instance index. We use SPI master 0
+#define SPI_SS_PIN 26
+#define SPI_MISO_PIN 23
+#define SPI_MOSI_PIN 24
+#define SPI_SCK_PIN 22
+#define INT_PIN 27
+
+#define NUM_TAPS 58
+#define BLOCK_SIZE 28
+
+
+// Declare a state array
+static float32_t firStateF32[BLOCK_SIZE + NUM_TAPS - 1];
+// Declare an instance for the low-pass FIR filter
+arm_fir_instance_f32 fir_lpf;
+
+float32_t acc_x_in_buf[140];
+float32_t acc_y_in_buf[140];
+float32_t acc_z_in_buf[140];
+float32_t acc_x_out_buf[140];
+float32_t acc_y_out_buf[140];
+float32_t acc_z_out_buf[140];
+
+int in_array[16];
+int out_array[16];
+
+int block_cnt = 0;
+
+//SPI instance
+static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);
+//Flag used to indicate that SPI instance completed the transfer
+static volatile bool spi_xfer_done;
+
+static uint8_t SPI_RX_Buffer[201]; // Allocate a buffer for SPI reads
+struct bmi160_dev sensor; // An instance of bmi160 sensor
+
+// Declare memory to store the raw FIFO buffer information
+uint8_t fifo_buff[200];
+// Modify the FIFO buffer instance and link to the device instance
+struct bmi160_fifo_frame fifo_frame;
+
+// 200 bytes -> ~7bytes per frame -> ~28 data frames
+struct bmi160_sensor_data acc_data[28];
+
 /**
 * Function for initializing the FIR filter instance
 */
diff --git a/project/main.c b/project/main.c
index 1682b52..70a18c0 100644
--- a/project/main.c
+++ b/project/main.c
@@ -1,195 +1,21 @@
-/**
- * Copyright (c) 2014 - 2019, Nordic Semiconductor ASA
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- *    list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form, except as embedded into a Nordic
- *    Semiconductor ASA integrated circuit in a product or a software update for
- *    such product, must reproduce the above copyright notice, this list of
- *    conditions and the following disclaimer in the documentation and/or other
- *    materials provided with the distribution.
- *
- * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
- *    contributors may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 4. This software, with or without modification, must only be used with a
- *    Nordic Semiconductor ASA integrated circuit.
- *
- * 5. Any software provided in binary form under this license must not be reverse
- *    engineered, decompiled, modified and/or disassembled.
- *
- * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
- * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-/** @file
- *
- * @defgroup ble_sdk_app_template_main main.c
- * @{
- * @ingroup ble_sdk_app_template
- * @brief Template project main file.
- *
- * This file contains a template for creating a new application. It has the code necessary to wakeup
- * from button, advertise, get a connection restart advertising on disconnect and if no new
- * connection created go back to system-off mode.
- * It can easily be used as a starting point for creating a new application, the comments identified
- * with 'YOUR_JOB' indicates where and how you can customize.
- */
-
 #include <stdbool.h>
 #include <stdint.h>
 #include <string.h>
 
-#include "nordic_common.h"
-#include "nrf.h"
-#include "app_error.h"
+#include "imu.h"
 #include "ble.h"
-#include "ble_hci.h"
-#include "ble_srv_common.h"
-#include "ble_advdata.h"
-#include "ble_advertising.h"
-#include "ble_conn_params.h"
-#include "nrf_sdh.h"
-#include "nrf_sdh_soc.h"
-#include "nrf_sdh_ble.h"
-#include "app_timer.h"
-#include "fds.h"
-#include "peer_manager.h"
-#include "peer_manager_handler.h"
-#include "bsp_btn_ble.h"
-#include "sensorsim.h"
-#include "ble_conn_state.h"
-#include "nrf_ble_gatt.h"
-#include "nrf_ble_qwr.h"
-#include "nrf_pwr_mgmt.h"
-#include "nrf_log.h"
-#include "nrf_log_ctrl.h"
-#include "nrf_log_default_backends.h"
-#include "nrf_temp.h"
-#include "ble_hts.h"
-#include "ble_bas.h"
-#include "nrf_drv_spi.h"
-#include "bmi160.h"
-#include "nrf_delay.h"
-#include "nrf_drv_gpiote.h"
-#include "fdacoefs.h"
-#include "arm_math.h"
-
-#define SPI_INSTANCE 0 // SPI instance index. We use SPI master 0
-#define SPI_SS_PIN 26
-#define SPI_MISO_PIN 23
-#define SPI_MOSI_PIN 24
-#define SPI_SCK_PIN 22
-#define INT_PIN 27
-
-#define NUM_TAPS 58
-#define BLOCK_SIZE 28
-
-#define DEVICE_NAME                     "nRF_Aapo"                       /**< Name of device. Will be included in the advertising data. */
-#define MANUFACTURER_NAME               "NordicSemiconductor"                   /**< Manufacturer. Will be passed to Device Information Service. */
-#define APP_ADV_INTERVAL                300                                     /**< The advertising interval (in units of 0.625 ms. This value corresponds to 187.5 ms). */
-
-#define APP_ADV_DURATION                18000                                   /**< The advertising duration (180 seconds) in units of 10 milliseconds. */
-#define APP_BLE_OBSERVER_PRIO           3                                       /**< Application's BLE observer priority. You shouldn't need to modify this value. */
-#define APP_BLE_CONN_CFG_TAG            1                                       /**< A tag identifying the SoftDevice BLE configuration. */
-
-#define MIN_CONN_INTERVAL               MSEC_TO_UNITS(100, UNIT_1_25_MS)        /**< Minimum acceptable connection interval (0.1 seconds). */
-#define MAX_CONN_INTERVAL               MSEC_TO_UNITS(200, UNIT_1_25_MS)        /**< Maximum acceptable connection interval (0.2 second). */
-#define SLAVE_LATENCY                   0                                       /**< Slave latency. */
-#define CONN_SUP_TIMEOUT                MSEC_TO_UNITS(4000, UNIT_10_MS)         /**< Connection supervisory timeout (4 seconds). */
-
-#define FIRST_CONN_PARAMS_UPDATE_DELAY  APP_TIMER_TICKS(5000)                   /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
-#define NEXT_CONN_PARAMS_UPDATE_DELAY   APP_TIMER_TICKS(30000)                  /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
-#define MAX_CONN_PARAMS_UPDATE_COUNT    3                                       /**< Number of attempts before giving up the connection parameter negotiation. */
-
-#define SEC_PARAM_BOND                  1                                       /**< Perform bonding. */
-#define SEC_PARAM_MITM                  0                                       /**< Man In The Middle protection not required. */
-#define SEC_PARAM_LESC                  0                                       /**< LE Secure Connections not enabled. */
-#define SEC_PARAM_KEYPRESS              0                                       /**< Keypress notifications not enabled. */
-#define SEC_PARAM_IO_CAPABILITIES       BLE_GAP_IO_CAPS_NONE                    /**< No I/O capabilities. */
-#define SEC_PARAM_OOB                   0                                       /**< Out Of Band data not available. */
-#define SEC_PARAM_MIN_KEY_SIZE          7                                       /**< Minimum encryption key size. */
-#define SEC_PARAM_MAX_KEY_SIZE          16                                      /**< Maximum encryption key size. */
-
-#define DEAD_BEEF                       0xDEADBEEF                              /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
-
-// Determines if temperature type is given as characteristic (1) or as a field of measurement (0)
-#define TEMP_TYPE_AS_CHARACTERISTIC 0
-
-// Declare a state array
-static float32_t firStateF32[BLOCK_SIZE + NUM_TAPS - 1];
-// Declare an instance for the low-pass FIR filter
-arm_fir_instance_f32 fir_lpf;
-
-float32_t acc_x_in_buf[140];
-float32_t acc_y_in_buf[140];
-float32_t acc_z_in_buf[140];
-float32_t acc_x_out_buf[140];
-float32_t acc_y_out_buf[140];
-float32_t acc_z_out_buf[140];
-
-int in_array[16];
-int out_array[16];
 
-int block_cnt = 0;
-
-//SPI instance
-static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);
-//Flag used to indicate that SPI instance completed the transfer
-static volatile bool spi_xfer_done;
-
-static uint8_t SPI_RX_Buffer[201]; // Allocate a buffer for SPI reads
-struct bmi160_dev sensor; // An instance of bmi160 sensor
-
-// Declare memory to store the raw FIFO buffer information
-uint8_t fifo_buff[200];
-// Modify the FIFO buffer instance and link to the device instance
-struct bmi160_fifo_frame fifo_frame;
-
-// 200 bytes -> ~7bytes per frame -> ~28 data frames
-struct bmi160_sensor_data acc_data[28];
-
-NRF_BLE_GATT_DEF(m_gatt);                                                       /**< GATT module instance. */
-NRF_BLE_QWR_DEF(m_qwr);                                                         /**< Context for the Queued Write module.*/
-BLE_ADVERTISING_DEF(m_advertising);                                             /**< Advertising module instance. */
-
-static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;                        /**< Handle of the current connection. */
-
-BLE_HTS_DEF(m_hts); // Macro for defining a ble_hts instance
-BLE_BAS_DEF(m_bas); // Macro for defining a ble_bas instance
-
-#include "imu.c"
-#include "ble.c"
 
 int main(void) {
 
-    bool erase_bonds;
-
+    // imu config
     dsp_config();
     spi_config();
     config_gpio();
     sensor_config();
 
-    while(true) {
-        __WFE(); // sleep until an event wakes us up
-        __SEV(); // sleep until an event wakes us up
-        __WFE(); // sleep until an event wakes us up
-    }
+    // imu config
+    bool erase_bonds;
 
     log_init();
     timers_init();
@@ -204,9 +30,7 @@ int main(void) {
     peer_manager_init();
 
     // Start execution.
-    NRF_LOG_INFO("Template example started.");
     application_timers_start();
-
     advertising_start(erase_bonds);
 
     // Enter main loop.
diff --git a/project/pca10040/s132/ses/ble.h b/project/pca10040/s132/ses/ble.h
new file mode 100644
index 0000000..79d6f95
--- /dev/null
+++ b/project/pca10040/s132/ses/ble.h
@@ -0,0 +1,87 @@
+#include <stdbool.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "nordic_common.h"
+#include "nrf.h"
+#include "app_error.h"
+#include "ble.h"
+#include "ble_hci.h"
+#include "ble_srv_common.h"
+#include "ble_advdata.h"
+#include "ble_advertising.h"
+#include "ble_conn_params.h"
+#include "nrf_sdh.h"
+#include "nrf_sdh_soc.h"
+#include "nrf_sdh_ble.h"
+#include "app_timer.h"
+#include "fds.h"
+#include "peer_manager.h"
+#include "peer_manager_handler.h"
+#include "bsp_btn_ble.h"
+#include "sensorsim.h"
+#include "ble_conn_state.h"
+#include "nrf_ble_gatt.h"
+#include "nrf_ble_qwr.h"
+#include "nrf_pwr_mgmt.h"
+#include "nrf_log.h"
+#include "nrf_log_ctrl.h"
+#include "nrf_log_default_backends.h"
+#include "nrf_temp.h"
+#include "ble_hts.h"
+#include "ble_bas.h"
+
+
+void advertising_start(bool erase_bonds);
+
+void generate_temperature(ble_hts_meas_t * p_meas);
+
+void on_hts_evt(ble_hts_t * p_hts, ble_hts_evt_t * p_evt);
+
+void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name);
+
+void pm_evt_handler(pm_evt_t const * p_evt);
+
+void timers_init(void);
+
+void gap_params_init(void);
+
+void gatt_init(void);
+
+void nrf_qwr_error_handler(uint32_t nrf_error);
+
+void services_init(void);
+
+void on_conn_params_evt(ble_conn_params_evt_t * p_evt);
+
+void conn_params_error_handler(uint32_t nrf_error);
+
+void conn_params_init(void);
+
+void application_timers_start(void);
+
+void sleep_mode_enter(void);
+
+void on_adv_evt(ble_adv_evt_t ble_adv_evt);
+
+void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context);
+
+void ble_stack_init(void);
+
+void peer_manager_init(void);
+
+void delete_bonds(void);
+
+void bsp_event_handler(bsp_event_t event);
+
+void advertising_init(void);
+
+void buttons_leds_init(bool * p_erase_bonds);
+
+void log_init(void);
+
+void power_management_init(void);
+
+void idle_state_handle(void);
+
+void advertising_start(bool erase_bonds);
diff --git a/project/pca10040/s132/ses/ble_app_template_pca10040_s132.emProject b/project/pca10040/s132/ses/ble_app_template_pca10040_s132.emProject
index ebf02e0..cc951d0 100644
--- a/project/pca10040/s132/ses/ble_app_template_pca10040_s132.emProject
+++ b/project/pca10040/s132/ses/ble_app_template_pca10040_s132.emProject
@@ -165,6 +165,10 @@
     <folder Name="Application">
       <file file_name="../../../main.c" />
       <file file_name="../config/sdk_config.h" />
+      <file file_name="../../../ble.c" />
+      <file file_name="../../../imu.c" />
+      <file file_name="ble.h" />
+      <file file_name="imu.h" />
     </folder>
     <folder Name="nRF_Segger_RTT">
       <file file_name="../../../../../nRF5_SDK_15.3.0_59ac345/external/segger_rtt/SEGGER_RTT.c" />
diff --git a/project/pca10040/s132/ses/imu.h b/project/pca10040/s132/ses/imu.h
new file mode 100644
index 0000000..d7f5e92
--- /dev/null
+++ b/project/pca10040/s132/ses/imu.h
@@ -0,0 +1,28 @@
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "nrf_drv_spi.h"
+#include "bmi160.h"
+#include "nrf_delay.h"
+#include "nrf_drv_gpiote.h"
+#include "arm_math.h"
+
+void dsp_config();
+
+void compute_fir();
+
+int8_t get_bmi160_fifo_data();
+
+void spi_event_handler(nrf_drv_spi_evt_t const * p_event, void *p_context);
+
+void gpio_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action);
+
+uint32_t spi_config();
+
+int8_t bmi160_spi_bus_write(uint8_t hw_addr, uint8_t reg_addr, uint8_t *reg_data, uint16_t cnt);
+
+int8_t bmi160_spi_bus_read(uint8_t hw_addr, uint8_t reg_addr, uint8_t *reg_data, uint16_t len);
+
+int8_t sensor_config();
+
+uint32_t config_gpio();
-- 
GitLab