ble-rtc-low-power-advertiser works with an external XTAL

This commit is contained in:
rbaron 2021-03-11 22:28:49 +01:00
parent ce59908a4e
commit b283f9e12b
3 changed files with 47 additions and 28 deletions

View file

@ -6,7 +6,7 @@ GROUP(-lgcc -lc -lnosys)
MEMORY MEMORY
{ {
FLASH (rx) : ORIGIN = 0x27000, LENGTH = 0xd9000 FLASH (rx) : ORIGIN = 0x27000, LENGTH = 0xd9000
RAM (rwx) : ORIGIN = 0x200018d8, LENGTH = 0x3e728 RAM (rwx) : ORIGIN = 0x20002300, LENGTH = 0x3dd00
} }
SECTIONS SECTIONS

View file

@ -1730,7 +1730,7 @@
// <196609=> External Full Swing // <196609=> External Full Swing
#ifndef NRFX_CLOCK_CONFIG_LF_SRC #ifndef NRFX_CLOCK_CONFIG_LF_SRC
#define NRFX_CLOCK_CONFIG_LF_SRC 0 #define NRFX_CLOCK_CONFIG_LF_SRC 1
#endif #endif
// <o> NRFX_CLOCK_CONFIG_IRQ_PRIORITY - Interrupt priority // <o> NRFX_CLOCK_CONFIG_IRQ_PRIORITY - Interrupt priority
@ -3248,7 +3248,7 @@
// <e> NRFX_RTC_ENABLED - nrfx_rtc - RTC peripheral driver // <e> NRFX_RTC_ENABLED - nrfx_rtc - RTC peripheral driver
//========================================================== //==========================================================
#ifndef NRFX_RTC_ENABLED #ifndef NRFX_RTC_ENABLED
#define NRFX_RTC_ENABLED 1 #define NRFX_RTC_ENABLED 0
#endif #endif
// <q> NRFX_RTC0_ENABLED - Enable RTC0 instance // <q> NRFX_RTC0_ENABLED - Enable RTC0 instance
@ -3308,7 +3308,7 @@
// <e> NRFX_RTC_CONFIG_LOG_ENABLED - Enables logging in the module. // <e> NRFX_RTC_CONFIG_LOG_ENABLED - Enables logging in the module.
//========================================================== //==========================================================
#ifndef NRFX_RTC_CONFIG_LOG_ENABLED #ifndef NRFX_RTC_CONFIG_LOG_ENABLED
#define NRFX_RTC_CONFIG_LOG_ENABLED 0 #define NRFX_RTC_CONFIG_LOG_ENABLED 1
#endif #endif
// <o> NRFX_RTC_CONFIG_LOG_LEVEL - Default Severity level // <o> NRFX_RTC_CONFIG_LOG_LEVEL - Default Severity level
@ -4823,7 +4823,7 @@
#ifndef CLOCK_CONFIG_LF_CAL_ENABLED #ifndef CLOCK_CONFIG_LF_CAL_ENABLED
#define CLOCK_CONFIG_LF_CAL_ENABLED 1 #define CLOCK_CONFIG_LF_CAL_ENABLED 0
#endif #endif
// <o> CLOCK_CONFIG_IRQ_PRIORITY - Interrupt priority // <o> CLOCK_CONFIG_IRQ_PRIORITY - Interrupt priority
@ -5384,7 +5384,7 @@
#ifndef RTC0_ENABLED #ifndef RTC0_ENABLED
#define RTC0_ENABLED 1 #define RTC0_ENABLED 0
#endif #endif
// <q> RTC1_ENABLED - Enable RTC1 instance // <q> RTC1_ENABLED - Enable RTC1 instance
@ -7625,7 +7625,7 @@
// <e> NRF_LOG_BACKEND_RTT_ENABLED - nrf_log_backend_rtt - Log RTT backend // <e> NRF_LOG_BACKEND_RTT_ENABLED - nrf_log_backend_rtt - Log RTT backend
//========================================================== //==========================================================
#ifndef NRF_LOG_BACKEND_RTT_ENABLED #ifndef NRF_LOG_BACKEND_RTT_ENABLED
#define NRF_LOG_BACKEND_RTT_ENABLED 0 #define NRF_LOG_BACKEND_RTT_ENABLED 1
#endif #endif
// <o> NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE - Size of buffer for partially processed strings. // <o> NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE - Size of buffer for partially processed strings.
// <i> Size of the buffer is a trade-off between RAM usage and processing. // <i> Size of the buffer is a trade-off between RAM usage and processing.
@ -7703,7 +7703,7 @@
// <e> NRF_LOG_ENABLED - nrf_log - Logger // <e> NRF_LOG_ENABLED - nrf_log - Logger
//========================================================== //==========================================================
#ifndef NRF_LOG_ENABLED #ifndef NRF_LOG_ENABLED
#define NRF_LOG_ENABLED 0 #define NRF_LOG_ENABLED 1
#endif #endif
// <h> Log message pool - Configuration of log message pool // <h> Log message pool - Configuration of log message pool
@ -8070,7 +8070,7 @@
// <e> CLOCK_CONFIG_LOG_ENABLED - Enables logging in the module. // <e> CLOCK_CONFIG_LOG_ENABLED - Enables logging in the module.
//========================================================== //==========================================================
#ifndef CLOCK_CONFIG_LOG_ENABLED #ifndef CLOCK_CONFIG_LOG_ENABLED
#define CLOCK_CONFIG_LOG_ENABLED 0 #define CLOCK_CONFIG_LOG_ENABLED 1
#endif #endif
// <o> CLOCK_CONFIG_LOG_LEVEL - Default Severity level // <o> CLOCK_CONFIG_LOG_LEVEL - Default Severity level
@ -8638,7 +8638,7 @@
// <e> RTC_CONFIG_LOG_ENABLED - Enables logging in the module. // <e> RTC_CONFIG_LOG_ENABLED - Enables logging in the module.
//========================================================== //==========================================================
#ifndef RTC_CONFIG_LOG_ENABLED #ifndef RTC_CONFIG_LOG_ENABLED
#define RTC_CONFIG_LOG_ENABLED 0 #define RTC_CONFIG_LOG_ENABLED 1
#endif #endif
// <o> RTC_CONFIG_LOG_LEVEL - Default Severity level // <o> RTC_CONFIG_LOG_LEVEL - Default Severity level
@ -8649,7 +8649,7 @@
// <4=> Debug // <4=> Debug
#ifndef RTC_CONFIG_LOG_LEVEL #ifndef RTC_CONFIG_LOG_LEVEL
#define RTC_CONFIG_LOG_LEVEL 3 #define RTC_CONFIG_LOG_LEVEL 4
#endif #endif
// <o> RTC_CONFIG_INFO_COLOR - ANSI escape code prefix. // <o> RTC_CONFIG_INFO_COLOR - ANSI escape code prefix.
@ -10282,7 +10282,7 @@
// <e> NRF_SDH_BLE_LOG_ENABLED - Enable logging in SoftDevice handler (BLE) module. // <e> NRF_SDH_BLE_LOG_ENABLED - Enable logging in SoftDevice handler (BLE) module.
//========================================================== //==========================================================
#ifndef NRF_SDH_BLE_LOG_ENABLED #ifndef NRF_SDH_BLE_LOG_ENABLED
#define NRF_SDH_BLE_LOG_ENABLED 0 #define NRF_SDH_BLE_LOG_ENABLED 1
#endif #endif
// <o> NRF_SDH_BLE_LOG_LEVEL - Default Severity level // <o> NRF_SDH_BLE_LOG_LEVEL - Default Severity level
@ -10333,7 +10333,7 @@
// <e> NRF_SDH_LOG_ENABLED - Enable logging in SoftDevice handler module. // <e> NRF_SDH_LOG_ENABLED - Enable logging in SoftDevice handler module.
//========================================================== //==========================================================
#ifndef NRF_SDH_LOG_ENABLED #ifndef NRF_SDH_LOG_ENABLED
#define NRF_SDH_LOG_ENABLED 0 #define NRF_SDH_LOG_ENABLED 1
#endif #endif
// <o> NRF_SDH_LOG_LEVEL - Default Severity level // <o> NRF_SDH_LOG_LEVEL - Default Severity level
@ -11374,7 +11374,7 @@
// <o> NRF_SDH_BLE_PERIPHERAL_LINK_COUNT - Maximum number of peripheral links. // <o> NRF_SDH_BLE_PERIPHERAL_LINK_COUNT - Maximum number of peripheral links.
#ifndef NRF_SDH_BLE_PERIPHERAL_LINK_COUNT #ifndef NRF_SDH_BLE_PERIPHERAL_LINK_COUNT
#define NRF_SDH_BLE_PERIPHERAL_LINK_COUNT 0 #define NRF_SDH_BLE_PERIPHERAL_LINK_COUNT 1
#endif #endif
// <o> NRF_SDH_BLE_CENTRAL_LINK_COUNT - Maximum number of central links. // <o> NRF_SDH_BLE_CENTRAL_LINK_COUNT - Maximum number of central links.
@ -11403,12 +11403,13 @@
// <o> NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE - Attribute Table size in bytes. The size must be a multiple of 4. // <o> NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE - Attribute Table size in bytes. The size must be a multiple of 4.
#ifndef NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE #ifndef NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE
#define NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE 248 // #define NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE 248
#define NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE 1408
#endif #endif
// <o> NRF_SDH_BLE_VS_UUID_COUNT - The number of vendor-specific UUIDs. // <o> NRF_SDH_BLE_VS_UUID_COUNT - The number of vendor-specific UUIDs.
#ifndef NRF_SDH_BLE_VS_UUID_COUNT #ifndef NRF_SDH_BLE_VS_UUID_COUNT
#define NRF_SDH_BLE_VS_UUID_COUNT 0 #define NRF_SDH_BLE_VS_UUID_COUNT 10
#endif #endif
// <q> NRF_SDH_BLE_SERVICE_CHANGED - Include the Service Changed characteristic in the Attribute Table. // <q> NRF_SDH_BLE_SERVICE_CHANGED - Include the Service Changed characteristic in the Attribute Table.
@ -11870,14 +11871,13 @@
// <2=> NRF_CLOCK_LF_SRC_SYNTH // <2=> NRF_CLOCK_LF_SRC_SYNTH
#ifndef NRF_SDH_CLOCK_LF_SRC #ifndef NRF_SDH_CLOCK_LF_SRC
#define NRF_SDH_CLOCK_LF_SRC 0 #define NRF_SDH_CLOCK_LF_SRC 1
// #define NRF_SDH_CLOCK_LF_SRC 1
#endif #endif
// <o> NRF_SDH_CLOCK_LF_RC_CTIV - SoftDevice calibration timer interval. // <o> NRF_SDH_CLOCK_LF_RC_CTIV - SoftDevice calibration timer interval.
#ifndef NRF_SDH_CLOCK_LF_RC_CTIV #ifndef NRF_SDH_CLOCK_LF_RC_CTIV
// #define NRF_SDH_CLOCK_LF_RC_CTIV 1 #define NRF_SDH_CLOCK_LF_RC_CTIV 0
#define NRF_SDH_CLOCK_LF_RC_CTIV 16 // #define NRF_SDH_CLOCK_LF_RC_CTIV 16
#endif #endif
// <o> NRF_SDH_CLOCK_LF_RC_TEMP_CTIV - SoftDevice calibration timer interval under constant temperature. // <o> NRF_SDH_CLOCK_LF_RC_TEMP_CTIV - SoftDevice calibration timer interval under constant temperature.
@ -11885,8 +11885,8 @@
// <i> if the temperature has not changed. // <i> if the temperature has not changed.
#ifndef NRF_SDH_CLOCK_LF_RC_TEMP_CTIV #ifndef NRF_SDH_CLOCK_LF_RC_TEMP_CTIV
// #define NRF_SDH_CLOCK_LF_RC_TEMP_CTIV 0 #define NRF_SDH_CLOCK_LF_RC_TEMP_CTIV 0
#define NRF_SDH_CLOCK_LF_RC_TEMP_CTIV 8 // #define NRF_SDH_CLOCK_LF_RC_TEMP_CTIV 8
#endif #endif
// <o> NRF_SDH_CLOCK_LF_ACCURACY - External clock accuracy used in the LL to compute timing. // <o> NRF_SDH_CLOCK_LF_ACCURACY - External clock accuracy used in the LL to compute timing.
@ -11905,8 +11905,8 @@
// <11=> NRF_CLOCK_LF_ACCURACY_1_PPM // <11=> NRF_CLOCK_LF_ACCURACY_1_PPM
#ifndef NRF_SDH_CLOCK_LF_ACCURACY #ifndef NRF_SDH_CLOCK_LF_ACCURACY
// #define NRF_SDH_CLOCK_LF_ACCURACY 7 #define NRF_SDH_CLOCK_LF_ACCURACY 7
#define NRF_SDH_CLOCK_LF_ACCURACY 1 // #define NRF_SDH_CLOCK_LF_ACCURACY 1
#endif #endif
// </h> // </h>

View file

@ -17,7 +17,8 @@
#include "nrf_soc.h" #include "nrf_soc.h"
// P0.03 // P0.03
#define LED_PIN 3 // #define LED_PIN 3
#define LED_PIN NRF_GPIO_PIN_MAP(1, 11)
// Environmental sensing // Environmental sensing
#define SERVICE_UUID 0x181a #define SERVICE_UUID 0x181a
@ -33,7 +34,7 @@ static uint8_t service_data[SERVICE_DATA_LEN];
#define APP_BLE_CONN_CFG_TAG 1 #define APP_BLE_CONN_CFG_TAG 1
// Seconds between RTC 2 events. // Seconds between RTC 2 events.
#define COMPARE_COUNTERTIME (6UL) #define COMPARE_COUNTERTIME (2UL)
// RTC0 is used by softdevice! We need to pick another instance. // RTC0 is used by softdevice! We need to pick another instance.
const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(2); const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(2);
@ -105,6 +106,7 @@ static void advertising_start(void) {
err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG); err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
APP_ERROR_CHECK(err_code); APP_ERROR_CHECK(err_code);
NRF_LOG_INFO("Advertising started\n.")
} }
static void advertising_stop(void) { static void advertising_stop(void) {
@ -165,11 +167,14 @@ static void idle_state_handle(void) {
} }
static void rtc_handler(nrf_drv_rtc_int_type_t int_type) { static void rtc_handler(nrf_drv_rtc_int_type_t int_type) {
NRF_LOG_INFO("CALLBACK!\n");
NRF_LOG_FLUSH();
if (int_type == NRF_DRV_RTC_INT_COMPARE2) { if (int_type == NRF_DRV_RTC_INT_COMPARE2) {
// nrf_gpio_pin_toggle(LED_PIN); nrf_gpio_pin_set(LED_PIN);
advertising_start(); advertising_start();
nrf_delay_ms(1000); nrf_delay_ms(1000);
advertising_stop(); advertising_stop();
nrf_gpio_pin_clear(LED_PIN);
nrf_drv_rtc_counter_clear(&rtc); nrf_drv_rtc_counter_clear(&rtc);
// We need to re-enable the RTC2 interrupt. // We need to re-enable the RTC2 interrupt.
nrf_drv_rtc_int_enable(&rtc, NRF_RTC_INT_COMPARE2_MASK); nrf_drv_rtc_int_enable(&rtc, NRF_RTC_INT_COMPARE2_MASK);
@ -182,21 +187,25 @@ static void rtc_handler(nrf_drv_rtc_int_type_t int_type) {
static void rtc_config(void) { static void rtc_config(void) {
uint32_t err_code; uint32_t err_code;
NRF_LOG_INFO("1!\n"); NRF_LOG_FLUSH();
// Initialize RTC instance. // Initialize RTC instance.
nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG; nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
config.prescaler = 4095; config.prescaler = 4095;
err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler); err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
APP_ERROR_CHECK(err_code); APP_ERROR_CHECK(err_code);
NRF_LOG_INFO("2!\n"); NRF_LOG_FLUSH();
nrf_drv_rtc_tick_disable(&rtc); nrf_drv_rtc_tick_disable(&rtc);
nrf_drv_rtc_overflow_disable(&rtc); nrf_drv_rtc_overflow_disable(&rtc);
nrf_drv_rtc_counter_clear(&rtc); nrf_drv_rtc_counter_clear(&rtc);
NRF_LOG_INFO("3!\n"); NRF_LOG_FLUSH();
// Set compare channel to trigger interrupt after COMPARE_COUNTERTIME // Set compare channel to trigger interrupt after COMPARE_COUNTERTIME
// seconds. // seconds.
err_code = nrf_drv_rtc_cc_set(&rtc, 2, COMPARE_COUNTERTIME * 8, true); err_code = nrf_drv_rtc_cc_set(&rtc, 2, COMPARE_COUNTERTIME * 8, true);
APP_ERROR_CHECK(err_code); APP_ERROR_CHECK(err_code);
NRF_LOG_INFO("4!\n"); NRF_LOG_FLUSH();
// Power on RTC instance. // Power on RTC instance.
nrf_drv_rtc_enable(&rtc); nrf_drv_rtc_enable(&rtc);
} }
@ -212,15 +221,25 @@ static void power_manage(void) {
int main(void) { int main(void) {
// Initialize. // Initialize.
// leds_init(); log_init();
leds_init();
power_management_init(); power_management_init();
NRF_LOG_INFO("Power inited\n"); NRF_LOG_FLUSH();
ble_stack_init(); ble_stack_init();
NRF_LOG_INFO("BLE stack inited\n"); NRF_LOG_FLUSH();
advertising_init(); advertising_init();
NRF_LOG_INFO("Adv inited\n"); NRF_LOG_FLUSH();
rtc_config(); rtc_config();
// advertising_start();
NRF_LOG_FLUSH();
// Enter main loop. // Enter main loop.
for (;;) { for (;;) {
// sd_power_system_off(); // sd_power_system_off();
power_manage(); power_manage();
// NRF_LOG_INFO("Hello, RTT!\n");
// NRF_LOG_FLUSH();
// nrf_delay_ms(500);
} }
} }