Skip to content

Commit

Permalink
Merge pull request #65 from jrperson/main
Browse files Browse the repository at this point in the history
Updated NRF Firmware
  • Loading branch information
dchristl authored Mar 25, 2024
2 parents 67a74ff + 55d67ab commit 43c52f6
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 111 deletions.
3 changes: 2 additions & 1 deletion firmware/nrf5x/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ APPLICATION_SRCS += nrf_delay.c
APPLICATION_SRCS += app_error.c
APPLICATION_SRCS += app_error_weak.c
APPLICATION_SRCS += nrf_drv_clock.c
APPLICATION_SRCS += nrf_drv_rtc.c
APPLICATION_SRCS += app_timer.c

USE_NRF52 := $(filter nrf52,$(MAKECMDGOALS))

Expand All @@ -20,6 +20,7 @@ SHELL:=/bin/bash
# BOARD_E104BT5032A is for E104-BT5032A board https://www.aliexpress.com/item/4000538644215.html
# BOARD_ALIEXPRESS is for this "AliExpress beacon" https://www.aliexpress.com/item/32826502025.html
# ADV_KEYS_FILE the advertisment keys file, generated by macless haystack
# BOARD_ALIEXPRESS_NO_XTAL is for AliExpress beacons without an XTAL

ADV_KEYS_FILE ?=
BOARD ?= BOARD_SIMPLE
Expand Down
89 changes: 89 additions & 0 deletions firmware/nrf5x/aliexpress_board_no_xtal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* Copyright (c) 2016 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
#ifndef ALIEXPRESS_BOARD_NO_XTAL_H
#define ALIEXPRESS_BOARD_NO_XTAL_H

#ifdef __cplusplus
extern "C" {
#endif

// LEDs definitions
#define LEDS_NUMBER 1

#define LED_1 29

#define LEDS_ACTIVE_STATE 0

#define LEDS_LIST { LED_1}

#define BSP_LED_0 LED_1

#define LEDS_INV_MASK 0

#define BUTTONS_NUMBER 1

#define BUTTON_START 28
#define BUTTON_1 28
#define BUTTON_STOP 28
#define BUTTON_PULL NRF_GPIO_PIN_PULLUP

#define BUTTONS_ACTIVE_STATE 0

#define BUTTONS_LIST { BUTTON_1 }

#define BSP_BUTTON_0 BUTTON_1

#define RX_PIN_NUMBER 11
#define TX_PIN_NUMBER 12
#define CTS_PIN_NUMBER UART_PIN_DISCONNECTED
#define RTS_PIN_NUMBER UART_PIN_DISCONNECTED
#define HWFC false

// Arduino board mappings
#define ARDUINO_SCL_PIN 27 // SCL signal pin
#define ARDUINO_SDA_PIN 26 // SDA signal pin
#define ARDUINO_AREF_PIN 2 // Aref pin
#define ARDUINO_13_PIN 25 // Digital pin 13
#define ARDUINO_12_PIN 24 // Digital pin 12
#define ARDUINO_11_PIN 23 // Digital pin 11
#define ARDUINO_10_PIN 22 // Digital pin 10
#define ARDUINO_9_PIN 20 // Digital pin 9
#define ARDUINO_8_PIN 19 // Digital pin 8

#define ARDUINO_7_PIN 18 // Digital pin 7
#define ARDUINO_6_PIN 17 // Digital pin 6
#define ARDUINO_5_PIN 16 // Digital pin 5
#define ARDUINO_4_PIN 15 // Digital pin 4
#define ARDUINO_3_PIN 14 // Digital pin 3
#define ARDUINO_2_PIN 13 // Digital pin 2
#define ARDUINO_1_PIN 12 // Digital pin 1
#define ARDUINO_0_PIN 11 // Digital pin 0

#define ARDUINO_A0_PIN 3 // Analog channel 0
#define ARDUINO_A1_PIN 4 // Analog channel 1
#define ARDUINO_A2_PIN 28 // Analog channel 2
#define ARDUINO_A3_PIN 29 // Analog channel 3
#define ARDUINO_A4_PIN 30 // Analog channel 4
#define ARDUINO_A5_PIN 31 // Analog channel 5

// Low frequency clock source to be used by the SoftDevice
#define NRF_CLOCK_LFCLKSRC {.source = NRF_CLOCK_LF_SRC_RC, \
.rc_ctiv = 16, \
.rc_temp_ctiv = 2, \
.xtal_accuracy = 1}


#ifdef __cplusplus
}
#endif

#endif // ARDUINO_PRIMO_H
151 changes: 41 additions & 110 deletions firmware/nrf5x/main.c
Original file line number Diff line number Diff line change
@@ -1,71 +1,49 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "ble_stack.h"
#include "openhaystack.h"
#include "nrf.h"
#include "nrf_delay.h"
#include "nrf_soc.h"
#include <time.h>
#include "nrf_drv_rtc.h"
#include "nrf_drv_clock.h"
#include "nrf_gpio.h"
/**
* advertising interval in milliseconds
*/
#define ADVERTISING_INTERVAL 5000
#define LEDS_COUNT 10

#define KEY_CHANGE_INTERVAL_MINUTES 30
#include "app_timer.h"

#define MAX_KEYS 50

uint32_t LEDS_TO_FLASH_ON_START[LEDS_COUNT] = {10, 29};
#define ADVERTISING_INTERVAL 5000 // advertising interval in milliseconds
#define KEY_CHANGE_INTERVAL_MINUTES 30 // how often to rotate to new key in minutes
#define MAX_KEYS 20 // maximum number of keys to rotate through
#define KEY_CHANGE_INTERVAL_MS (KEY_CHANGE_INTERVAL_MINUTES * 60 * 1000)

const nrf_drv_rtc_t rtc1 = NRF_DRV_RTC_INSTANCE(1);
#define APP_TIMER_PRESCALER 0
#define APP_TIMER_MAX_TIMERS 1
#define TIMER_TICKS APP_TIMER_TICKS(KEY_CHANGE_INTERVAL_MS, APP_TIMER_PRESCALER)
#define APP_TIMER_OP_QUEUE_SIZE 4

int last_filled_index = -1;
int key_index = -1;
int current_index = 0;

APP_TIMER_DEF(m_key_change_timer_id);

static char public_key[MAX_KEYS][28] = {
// Create space for MAX_KEYS public keys
static char public_key[MAX_KEYS][28] = {
"OFFLINEFINDINGPUBLICKEYHERE!",
};

void setRandomIndex()
{
uint8_t random_buffer[sizeof(int)];
int random_integer;
sd_rand_application_vector_get(random_buffer, sizeof(int));
memcpy(&random_integer, random_buffer, sizeof(int));

key_index = abs(random_integer % (last_filled_index + 1));
printf("%d", key_index);
}

char *getCurrentKey()
{
uint8_t random_buffer[sizeof(int)];
int random_integer;
sd_rand_application_vector_get(random_buffer, sizeof(int));
memcpy(&random_integer, random_buffer, sizeof(int));

int randomValue = abs(random_integer % (last_filled_index + 1));

return public_key[randomValue];
}

void setAndAdvertiseNextKey()
{
// Variable to hold the data to advertise
uint8_t *ble_address;
uint8_t *raw_data;
uint8_t data_len;

// Disable advertising
sd_ble_gap_adv_stop();
sd_ble_gap_adv_data_set(NULL, 0, NULL, 0);
key_index = (key_index + 1) % (last_filled_index + 1); // Back to zero if out of range

// Update key index for next advertisement...Back to zero if out of range
current_index = (current_index + 1) % (last_filled_index + 1);

// Set key to be advertised
data_len = setAdvertisementKey(public_key[key_index], &ble_address, &raw_data);
data_len = setAdvertisementKey(public_key[current_index], &ble_address, &raw_data);

// Set bluetooth address
setMacAddress(ble_address);
Expand All @@ -77,73 +55,31 @@ void setAndAdvertiseNextKey()
startAdvertisement(ADVERTISING_INTERVAL);
}

static void rtc1_handler(nrf_drv_rtc_int_type_t int_type)
void key_change_timeout_handler(void *p_context)
{

if (int_type == NRF_DRV_RTC_INT_COMPARE0)
{ // Interrupt from COMPARE0 event.
// nrf_gpio_pin_toggle(LED_GPIO);
setAndAdvertiseNextKey(); // next key
nrf_drv_rtc_int_enable(&rtc1, RTC_CHANNEL_INT_MASK(0)); // re-enable
nrf_drv_rtc_counter_clear(&rtc1); // reset the counter
}
else if (int_type == NRF_DRV_RTC_INT_TICK)
{ // Tick off
// This is an error
}
}

/** @brief Function starting the HFCLK oscillator.
*/
static void lfclk_config(void)
{
ret_code_t err_code = nrf_drv_clock_init();
APP_ERROR_CHECK(err_code);

nrf_drv_clock_lfclk_request(NULL);
}

static void gpio_config(void)
{
// Configure LED-pin as outputs and clear.

for (int i = 0; i < 2; i++)
{
uint32_t currentValue = LEDS_TO_FLASH_ON_START[i];
nrf_gpio_cfg_output(currentValue);
nrf_gpio_pin_clear(currentValue);
nrf_gpio_pin_toggle(currentValue);
nrf_delay_ms(200); // Flash the pin on startup
nrf_gpio_pin_clear(currentValue);
}
setAndAdvertiseNextKey();
}

/** @brief Function initialization and configuration of timer driver instance.
*/
static void timer_config(void)
{
uint32_t err_code;

// Initialize RTC instance
err_code = nrf_drv_rtc_init(&rtc1, NULL, rtc1_handler);
APP_ERROR_CHECK(err_code);

// Disable tick event & interrupt
nrf_drv_rtc_tick_enable(&rtc1, false);
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, NULL);

// Set compare channel 0 to trigger interrupt after RTC1_CC_VALUE*125ms
err_code = nrf_drv_rtc_cc_set(&rtc1, 0, KEY_CHANGE_INTERVAL_MINUTES * 60 * RTC1_CONFIG_FREQUENCY, true);
// Create timer
err_code = app_timer_create(&m_key_change_timer_id, APP_TIMER_MODE_REPEATED, key_change_timeout_handler);
APP_ERROR_CHECK(err_code);

// Power on RTC instance
nrf_drv_rtc_enable(&rtc1);
// Set timer interval
err_code = app_timer_start(m_key_change_timer_id, TIMER_TICKS, NULL);
APP_ERROR_CHECK(err_code);
}

/**
* main function
*/
int main(void)
{
int main(void) {

// Find the last filled index
for (int i = MAX_KEYS - 1; i >= 0; i--)
{
Expand All @@ -154,24 +90,19 @@ int main(void)
}
}

// Select a random index as start
setRandomIndex();

// Init BLE stack
// Init BLE stack and softdevice
init_ble();
gpio_config();
if (last_filled_index > 0)
{
// Start timer
lfclk_config();

// Only use the app_timer to rotate keys if we need to
if (last_filled_index > 0){
timer_config();
}

if (last_filled_index >= 0) {
setAndAdvertiseNextKey();
}

setAndAdvertiseNextKey();

while (1)
{
while (1){
power_manage();
}
return 0;
}

0 comments on commit 43c52f6

Please sign in to comment.