Skip to content

Commit bfe0258

Browse files
committed
Reset working on both hardware versions
1 parent 3357ced commit bfe0258

File tree

8 files changed

+98
-35
lines changed

8 files changed

+98
-35
lines changed

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@
7171
"BMPGDBSerialPort": "/dev/ttyACM0",
7272
},
7373
"osx": {
74-
// "BMPGDBSerialPort": "/dev/cu.usbmodem72ADB7F21",
75-
"BMPGDBSerialPort": "/dev/cu.usbmodem72AE45F31",
74+
// "BMPGDBSerialPort": "/dev/cu.usbmodem72AE45F31",
75+
"BMPGDBSerialPort": "/dev/cu.usbmodem72AE30F31",
7676
}
7777
},
7878
{

source/application/bluetooth.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ void SD_EVT_IRQHandler(void)
356356
}
357357
}
358358

359-
void bluetooth_setup(bool factory_reset, bool *is_paired)
359+
void bluetooth_setup()
360360
{
361361
// Enable the softdevice using internal RC oscillator
362362
check_error(sd_softdevice_enable(NULL, softdevice_assert_handler));
@@ -433,21 +433,6 @@ void bluetooth_setup(bool factory_reset, bool *is_paired)
433433
bond.sec_param.kdist_peer.enc = 1;
434434
bond.sec_param.kdist_peer.id = 1;
435435

436-
if (factory_reset)
437-
{
438-
flash_erase_page(bond_storage);
439-
flash_wait_until_complete();
440-
}
441-
442-
// Check if already paired
443-
ble_gap_enc_info_t zero_struct;
444-
memset(&zero_struct, 0xFF, sizeof(ble_gap_enc_info_t));
445-
if (memcmp((void *)bond_storage, &zero_struct, sizeof(ble_gap_enc_info_t)))
446-
{
447-
LOG("Device is paired");
448-
*is_paired = true;
449-
}
450-
451436
// Read stored encryption key from memory
452437
memcpy(&bond.own_key.enc_info,
453438
(void *)bond_storage,
@@ -578,6 +563,26 @@ void bluetooth_setup(bool factory_reset, bool *is_paired)
578563
check_error(sd_ble_gap_adv_start(ble_handles.advertising, 1));
579564
}
580565

566+
bool bluetooth_is_paired(void)
567+
{
568+
bool is_paired = false;
569+
570+
ble_gap_enc_info_t zero_struct;
571+
memset(&zero_struct, 0xFF, sizeof(ble_gap_enc_info_t));
572+
if (memcmp((void *)bond_storage, &zero_struct, sizeof(ble_gap_enc_info_t)))
573+
{
574+
is_paired = true;
575+
}
576+
577+
return is_paired;
578+
}
579+
580+
void bluetooth_unpair(void)
581+
{
582+
flash_erase_page(bond_storage);
583+
flash_wait_until_complete();
584+
}
585+
581586
bool bluetooth_is_connected(void)
582587
{
583588
return ble_handles.connection == BLE_CONN_HANDLE_INVALID ? false : true;

source/application/bluetooth.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
#define BLE_PREFERRED_MAX_MTU 247
3232
extern uint16_t ble_negotiated_mtu;
3333

34-
void bluetooth_setup(bool factory_reset, bool *is_paired);
34+
void bluetooth_setup(void);
35+
36+
bool bluetooth_is_paired(void);
37+
38+
void bluetooth_unpair(void);
3539

3640
bool bluetooth_is_connected(void);
3741

source/application/flash.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "error_logging.h"
2828
#include "lfs.h"
2929
#include "main.h"
30+
#include "nrf_sdm.h"
3031
#include "nrf_soc.h"
3132
#include "nrfx_log.h"
3233

@@ -62,9 +63,14 @@ void flash_write(uint32_t address, const uint32_t *data, size_t length)
6263

6364
void flash_wait_until_complete(void)
6465
{
65-
// TODO add a timeout
66-
while (flash_is_busy)
66+
uint8_t sd_is_enabled = false;
67+
sd_softdevice_is_enabled(&sd_is_enabled);
68+
69+
if (sd_is_enabled == 1)
6770
{
71+
while (flash_is_busy)
72+
{
73+
}
6874
}
6975
}
7076

source/application/luaport.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void lua_break_signal_interrupt(void)
6363
1);
6464
}
6565

66-
void run_lua(bool factory_reset, bool is_paired)
66+
void run_lua(bool is_paired)
6767
{
6868
lua_State *L = luaL_newstate();
6969
L_global = L; // Only used for interrupts
@@ -99,7 +99,7 @@ void run_lua(bool factory_reset, bool is_paired)
9999
lua_open_imu_library(L);
100100
lua_open_time_library(L);
101101

102-
lua_open_file_library(L, factory_reset);
102+
lua_open_file_library(L, !is_paired);
103103

104104
// Make sure the above functions cleared up the stack correctly
105105
if (lua_gettop(L) != 0)

source/application/luaport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ void lua_write_to_repl(uint8_t *buffer, uint8_t length);
3838

3939
void lua_break_signal_interrupt(void);
4040

41-
void run_lua(bool factory_reset, bool is_paired);
41+
void run_lua(bool is_paired);

source/application/main.c

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,23 @@ void case_detect_pin_interrupt_handler(nrfx_gpiote_pin_t unused_gptiote_pin,
164164
shutdown(false);
165165
}
166166

167+
void frame_lite_button_interrupt_handler(nrfx_gpiote_pin_t unused_gptiote_pin,
168+
nrfx_gpiote_trigger_t unused_gptiote_trigger,
169+
void *unused_gptiote_context_pointer)
170+
{
171+
nrfx_gpiote_trigger_disable(FRAME_LITE_BUTTON_PIN);
172+
bluetooth_unpair();
173+
NVIC_SystemReset();
174+
}
175+
167176
static void fpga_send_bitstream_bytes(void *context,
168177
void *data,
169178
size_t data_size)
170179
{
171180
spi_write_raw(FPGA, data, data_size);
172181
}
173182

174-
static void hardware_setup(bool *factory_reset)
183+
static void hardware_setup()
175184
{
176185
// Configure watchdog
177186
{
@@ -183,6 +192,20 @@ static void hardware_setup(bool *factory_reset)
183192
nrfx_systick_init();
184193
}
185194

195+
// Check if Frame or Frame lite
196+
{
197+
nrf_gpio_cfg_input(FRAME_LITE_HW_DETECT_PIN, NRF_GPIO_PIN_PULLUP);
198+
199+
if (nrf_gpio_pin_read(FRAME_LITE_HW_DETECT_PIN))
200+
{
201+
LOG("Running on Frame");
202+
}
203+
else
204+
{
205+
LOG("Running on Frame Lite");
206+
}
207+
}
208+
186209
// Configure the I2C and SPI drivers
187210
{
188211
i2c_configure();
@@ -299,7 +322,7 @@ static void hardware_setup(bool *factory_reset)
299322
else
300323
{
301324
LOG("Factory reset");
302-
*factory_reset = true;
325+
bluetooth_unpair();
303326
stay_awake = true;
304327
}
305328
}
@@ -308,6 +331,30 @@ static void hardware_setup(bool *factory_reset)
308331
nrfx_gpiote_trigger_enable(CASE_DETECT_PIN, true);
309332
}
310333

334+
// Configure the Frame lite button
335+
{
336+
nrfx_gpiote_input_config_t input_config = {
337+
.pull = NRF_GPIO_PIN_PULLUP,
338+
};
339+
340+
nrfx_gpiote_trigger_config_t trigger_config = {
341+
.trigger = NRFX_GPIOTE_TRIGGER_HITOLO,
342+
.p_in_channel = NULL,
343+
};
344+
345+
nrfx_gpiote_handler_config_t handler_config = {
346+
.handler = frame_lite_button_interrupt_handler,
347+
.p_context = NULL,
348+
};
349+
350+
check_error(nrfx_gpiote_input_configure(FRAME_LITE_BUTTON_PIN,
351+
&input_config,
352+
&trigger_config,
353+
&handler_config));
354+
355+
nrfx_gpiote_trigger_enable(FRAME_LITE_BUTTON_PIN, true);
356+
}
357+
311358
// Load and start the FPGA image
312359
{
313360
nrf_gpio_cfg_output(FPGA_PROGRAM_PIN);
@@ -412,19 +459,14 @@ int main(void)
412459
{
413460
LOG("Frame firmware " BUILD_VERSION " (" GIT_COMMIT ")");
414461

415-
bool factory_reset = false;
416-
bool is_paired = false;
462+
hardware_setup();
417463

418-
hardware_setup(&factory_reset);
419-
420-
bluetooth_setup(factory_reset, &is_paired);
464+
bluetooth_setup();
421465

422466
while (1)
423467
{
424468
reload_watchdog(NULL, NULL);
425469

426-
run_lua(factory_reset, is_paired);
427-
428-
factory_reset = false;
470+
run_lua(bluetooth_is_paired());
429471
}
430472
}

source/pinout.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,10 @@
4848
#define IMU_INTERRUPT_PIN NRF_GPIO_PIN_MAP(1, 5)
4949

5050
#define MICROPHONE_CLOCK_PIN NRF_GPIO_PIN_MAP(0, 4)
51-
#define MICROPHONE_DATA_PIN NRF_GPIO_PIN_MAP(0, 1)
51+
#define MICROPHONE_DATA_PIN NRF_GPIO_PIN_MAP(0, 1)
52+
53+
#define FRAME_LITE_HW_DETECT_PIN NRF_GPIO_PIN_MAP(0, 9) // Inverted pin
54+
#define FRAME_LITE_BUTTON_PIN NRF_GPIO_PIN_MAP(1, 0) // Inverted pin
55+
#define FRAME_LITE_LED_RED_PIN NRF_GPIO_PIN_MAP(1, 7)
56+
#define FRAME_LITE_LED_GREEN_PIN NRF_GPIO_PIN_MAP(1, 4)
57+
#define FRAME_LITE_LED_BLUE_PIN NRF_GPIO_PIN_MAP(1, 1)

0 commit comments

Comments
 (0)