Skip to content
This repository has been archived by the owner on Sep 23, 2021. It is now read-only.

Commit

Permalink
Merge pull request #125 from ojousima/master
Browse files Browse the repository at this point in the history
Fixes from v1-backports, fix boot on used tags
  • Loading branch information
ojousima authored May 26, 2018
2 parents 525cfbc + a3e4629 commit 41fb065
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 43 deletions.
6 changes: 3 additions & 3 deletions drivers/bluetooth/bluetooth_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "ruuvi_endpoints.h"
#include "ble_event_handlers.h"

#if APPLICATION_GATT
#if APP_GATT_PROFILE_ENABLED
#include "application_service_if.h"
#endif

Expand Down Expand Up @@ -150,7 +150,7 @@ void bluetooth_name_postfix_add(char* name_base, size_t base_length)
{
unsigned int addr0 = NRF_FICR->DEVICEADDR[0];
char postfix[4] = { 0 };
sprintf(postfix,"%x", addr0&0xFFFF);
sprintf(postfix,"%04x", addr0&0xFFFF);
memcpy(name_base + base_length, postfix, sizeof(postfix));
}

Expand Down Expand Up @@ -361,7 +361,7 @@ ret_code_t bluetooth_stack_init(void)
NRF_LOG_INFO("Peer manager init \r\n");
nrf_delay_ms(10);

#if APPLICATION_GATT
#if APP_GATT_PROFILE_ENABLED
err_code |= application_services_init();
NRF_LOG_INFO("Services init status %d\r\n", err_code);
nrf_delay_ms(10);
Expand Down
39 changes: 39 additions & 0 deletions drivers/lis2dh12/lis2dh12.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,45 @@ lis2dh12_ret_t lis2dh12_set_fifo_watermark(size_t count)
return err_code;
}

/**
* Enable activity detection interrupt on pin 2. Interrupt is high for samples where high-passed acceleration exceeds mg
*/
lis2dh12_ret_t lis2dh12_set_activity_interrupt_pin_2(uint16_t mg)
{

// // Configure activity interrupt - TODO: Implement in driver, add tests.
// uint8_t ctrl[1];
// // Enable high-pass for Interrupt function 2.
// //CTRLREG2 = 0x02
// ctrl[0] = LIS2DH12_HPIS2_MASK;
// lis2dh12_write_register(LIS2DH12_CTRL_REG2, ctrl, 1);

lis2dh12_set_highpass(LIS2DH12_HPIS2_MASK);

// Enable interrupt 2 on X-Y-Z HI/LO.
// INT2_CFG = 0x7F
// ctrl[0] = 0x7F;
// lis2dh12_write_register(LIS2DH12_INT2_CFG, ctrl, 1);
lis2dh12_set_interrupt_configuration(0x7F, 2);
// Interrupt on 64 mg+ (highpassed, +/-).
//INT2_THS= 0x04 // 4 LSB = 64 mg @2G scale
// ctrl[0] = LIS2DH12_ACTIVITY_THRESHOLD;
// lis2dh12_write_register(LIS2DH12_INT2_THS, ctrl, 1);

uint8_t threshold = mg / get_mgpb();
if(0 == threshold)
{
threshold = 1;
}

lis2dh12_set_threshold(threshold, 2);

// Enable Interrupt function 2 on LIS interrupt pin 2 (stays high for 1/ODR).
lis2dh12_set_interrupts(LIS2DH12_I2C_INT2_MASK, 2);

return LIS2DH12_RET_OK;
}

/**
* Set interrupt on pin. Write "0" To disable interrupt on pin.
* NOTE: pin 1 and pin 2 DO NOT support identical configurations.
Expand Down
7 changes: 6 additions & 1 deletion drivers/lis2dh12/lis2dh12.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,15 @@ lis2dh12_ret_t lis2dh12_set_threshold(uint8_t bits, uint8_t pin);
*/
lis2dh12_ret_t lis2dh12_set_activity_threshold(uint8_t bits);

/**
* Enable activity detection interrupt on pin 2. Interrupt is high for samples where high-passed acceleration exceeds mg
*/
lis2dh12_ret_t lis2dh12_set_activity_interrupt_pin_2(uint16_t mg);

/**
* Internal functions for reading/writing registers.
*/
lis2dh12_ret_t lis2dh12_read_register(uint8_t address, uint8_t* const p_toRead, size_t count);
lis2dh12_ret_t lis2dh12_write_register(uint8_t address, uint8_t* const dataToWrite, size_t count);

#endif /* LIS2DH12_H */
#endif /* LIS2DH12_H */
11 changes: 9 additions & 2 deletions drivers/rtc/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(APP_RTC_INSTANCE); /**< Declaring an instance of nrf_drv_rtc for RTC2. */

static uint64_t overflows = 0;

static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
{
if (int_type == NRF_DRV_RTC_INT_COMPARE0)
Expand All @@ -25,6 +27,10 @@ static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
{
NRF_LOG_DEBUG("Tick\r\n");
}
else if (int_type == NRF_DRV_RTC_INT_OVERFLOW)
{
overflows++;
}
}

uint32_t init_rtc(void)
Expand All @@ -50,11 +56,12 @@ uint32_t init_rtc(void)
return err_code;
}

uint32_t millis(void)
uint64_t millis(void)
{
uint64_t ms = nrf_drv_rtc_counter_get(&rtc);
ms += overflows<<24;
//Compensate tick roundoff
ms*=32000;
ms/=32768;
return (uint32_t)ms;
return ms;
}
2 changes: 1 addition & 1 deletion drivers/rtc/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

uint32_t init_rtc(void);

uint32_t millis(void);
uint64_t millis(void);

#endif
4 changes: 2 additions & 2 deletions ruuvi_examples/ruuvi_firmware/application_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#define LIS2DH12_SAMPLERATE_URL LIS2DH12_RATE_0

// LSB, i.e. scale and resolution affect the threshold //TODO: verify resolution
// 64 mg on 2G/10bit
#define LIS2DH12_ACTIVITY_THRESHOLD 0x04
// 64 mg
#define LIS2DH12_ACTIVITY_THRESHOLD 64

#endif
11 changes: 6 additions & 5 deletions ruuvi_examples/ruuvi_firmware/bluetooth_application_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define APPLICATION_ADV_INTERVAL 1010 /**< ms. Use value which is not exactly divisible by 1000 ms to be seen by gateways which have limited scan windows in second divisible intervals. **/
#define APP_TX_POWER 4 /**< dBm **/
#define INIT_FWREV "2.2.2" /**< Github tag. Do not include specifiers such as "alpha" so you can accept ready binaries as they are **/
#define INIT_SWREV INIT_FWREV /**< FW and SW are same thing in this context **/
#define INIT_SWREV INIT_FWREV /**< FW and SW are same thing in this context **/

// milliseconds until main loop timer function is called. Other timers can bring
// application out of sleep at higher (or lower) interval.
Expand All @@ -19,12 +19,11 @@
#define ADVERTISING_STARTUP_PERIOD 30000u //milliseconds app advertises at startup speed.
#define ADVERTISING_INTERVAL_STARTUP 100u //milliseconds app advertises at startup speed.



// Base length includes URL scheme prefix, URL is 17 bytes
#define URL_BASE_LENGTH 9
#define URL_DATA_LENGTH 9
#define URL_BASE {0x03, 'r', 'u', 'u', '.', 'v', 'i', '/', '#'}; // https://ruu.vi/#

//Raw v2
#define RAW_DATA_LENGTH 24

Expand All @@ -34,8 +33,10 @@
* BLE_GAP_ADV_TYPE_ADV_SCAN_IND 0x02 Nonconnectable, scannable
* BLE_GAP_ADV_TYPE_ADV_NONCONN_IND 0x03 Nonconnectable, nonscannable
*/

#define APPLICATION_ADVERTISEMENT_TYPE 0x00

//Set to 0 if you don't want to include GATT connectivity. Remember to adjust advertisement type
#define APPLICATION_GATT 1
#define APP_GATT_PROFILE_ENABLED 1

#endif
#endif
40 changes: 12 additions & 28 deletions ruuvi_examples/ruuvi_firmware/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ void main_timer_handler(void * p_context)
fast_advetising = false;
if (highres) { bluetooth_configure_advertising_interval(ADVERTISING_INTERVAL_RAW);}
else {bluetooth_configure_advertising_interval(ADVERTISING_INTERVAL_URL);}
bluetooth_apply_configuration();
}

// If we have all the sensors.
Expand Down Expand Up @@ -286,7 +287,7 @@ ret_code_t lis2dh12_int2_handler(const ruuvi_standard_message_t message)
*/
int main(void)
{
ret_code_t err_code = NRF_SUCCESS; // counter, gets incremented by each failed init. It is 0 in the end if init was ok.
ret_code_t err_code = NRF_SUCCESS; // Error flag, OR any errors to it and check the error code at the end.

// Initialize log.
err_code |= init_log();
Expand Down Expand Up @@ -315,19 +316,20 @@ int main(void)

// Start interrupts.
err_code |= pin_interrupt_init();

// Initialize button.
err_code |= pin_interrupt_enable(BSP_BUTTON_0, NRF_GPIOTE_POLARITY_HITOLO, button_press_handler);

// Interrupt handler is defined in lis2dh12_acceleration_handler.c, reads the buffer and passes the data onwards to application as configured.
// Try using PROPRIETARY as a target of accelerometer to implement your own logic.
err_code |= pin_interrupt_enable(INT_ACC1_PIN, NRF_GPIOTE_POLARITY_LOTOHI, lis2dh12_int1_handler);

// Initialize BME 280 and lis2dh12.
if (model_plus)
{
// Clear memory.
lis2dh12_reset();
// Wait for reboot.

// Enable LOTOHI interrupt on nRF52 to detect acceleration events.
err_code |= pin_interrupt_enable(INT_ACC2_PIN, NRF_GPIOTE_POLARITY_LOTOHI, lis2dh12_int2_handler);

// Wait for LIS reboot.
nrf_delay_ms(10);
// Enable XYZ axes.
lis2dh12_enable();
Expand All @@ -336,29 +338,9 @@ int main(void)
lis2dh12_set_sample_rate(LIS2DH12_SAMPLERATE_RAW);
lis2dh12_set_resolution(LIS2DH12_RESOLUTION);

//XXX If you read this, I'm sorry about line below.
#include "lis2dh12_registers.h"
// Configure activity interrupt - TODO: Implement in driver, add tests.
uint8_t ctrl[1];
// Enable high-pass for Interrupt function 2.
//CTRLREG2 = 0x02
ctrl[0] = LIS2DH12_HPIS2_MASK;
lis2dh12_write_register(LIS2DH12_CTRL_REG2, ctrl, 1);

// Enable interrupt 2 on X-Y-Z HI/LO.
//INT2_CFG = 0x7F
ctrl[0] = 0x7F;
lis2dh12_write_register(LIS2DH12_INT2_CFG, ctrl, 1);
// Interrupt on 64 mg+ (highpassed, +/-).
//INT2_THS= 0x04 // 4 LSB = 64 mg @2G scale
ctrl[0] = LIS2DH12_ACTIVITY_THRESHOLD;
lis2dh12_write_register(LIS2DH12_INT2_THS, ctrl, 1);

// Enable LOTOHI interrupt on nRF52.
err_code |= pin_interrupt_enable(INT_ACC2_PIN, NRF_GPIOTE_POLARITY_LOTOHI, lis2dh12_int2_handler);
lis2dh12_set_activity_interrupt_pin_2(LIS2DH12_ACTIVITY_THRESHOLD);


// Enable Interrupt function 2 on LIS interrupt pin 2 (stays high for 1/ODR).
lis2dh12_set_interrupts(LIS2DH12_I2C_INT2_MASK, 2);

// Setup BME280 - oversampling must be set for each used sensor.
bme280_set_oversampling_hum(BME280_HUMIDITY_OVERSAMPLING);
Expand All @@ -383,6 +365,8 @@ int main(void)

// Init ok, start watchdog with default wdt event handler (reset).
init_watchdog(NULL);

// start advertising
bluetooth_advertising_start();

// Enter main loop.
Expand Down
17 changes: 16 additions & 1 deletion ruuvi_examples/ruuvi_firmware/sdk_application_config.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#ifndef SDK_APPLICATION_CONFIG_H
#define SDK_APPLICATION_CONFIG_H
#include "bluetooth_application_config.h"


// Define any application-specific sdk configuration overrides here
#define PWM_ENABLED 0
Expand All @@ -15,8 +17,21 @@
#define NFC_HAL_ENABLED 1
#define CRC16_ENABLED 1 //CRC required by DFU
#define CRC32_ENABLED 1
#define NRF_LOG_ENABLED 0
#define NRF_LOG_ENABLED 1

#if APP_GATT_PROFILE_ENABLED
#define BLE_DIS_ENABLED 1 //Device information service
#define BLE_NUS_ENABLED 1 //Nordic UART Service
#define BLE_DFU_ENABLED 1 //DFU service
#endif

// Fix error if there is leftover configuration flash
#define FDS_OP_QUEUE_SIZE 10
#define FDS_CHUNK_QUEUE_SIZE 15
#define FDS_MAX_USERS 8
#define FDS_VIRTUAL_PAGES 10
#define FDS_VIRTUAL_PAGE_SIZE 1024


// WDT_CONFIG_RELOAD_VALUE - Reload value <15-4294967295> (ms)
// Watchdog cannot be stopped even when entering bootloader,
Expand Down

0 comments on commit 41fb065

Please sign in to comment.