Skip to content

Commit a1e4a3f

Browse files
committed
give capsense a timeout, add it to Puck object, and fix Ctrl-C behaviour on nRF52
1 parent 4faca02 commit a1e4a3f

File tree

9 files changed

+89
-50
lines changed

9 files changed

+89
-50
lines changed

Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,8 +1368,6 @@ ifdef USE_NFC
13681368
INCLUDE += -I$(NRF5X_SDK_PATH)/components/nfc/ndef/uri
13691369
INCLUDE += -I$(NRF5X_SDK_PATH)/components/nfc/ndef/generic/message
13701370
INCLUDE += -I$(NRF5X_SDK_PATH)/components/nfc/ndef/generic/record
1371-
TARGETSOURCES += $(NRF5X_SDK_PATH)/components/drivers_nrf/clock/nrf_drv_clock.c
1372-
TARGETSOURCES += $(NRF5X_SDK_PATH)/components/libraries/util/app_util_platform.c
13731371
TARGETSOURCES += $(NRF5X_SDK_PATH)/components/nfc/ndef/uri/nfc_uri_msg.c
13741372
TARGETSOURCES += $(NRF5X_SDK_PATH)/components/nfc/ndef/uri/nfc_uri_rec.c
13751373
TARGETSOURCES += $(NRF5X_SDK_PATH)/components/nfc/ndef/generic/message/nfc_ndef_msg.c

boards/PUCKJS.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
'LED2' : { 'pin' : 'D4' },
5959
'LED3' : { 'pin' : 'D3' },
6060
'IR' : { 'pin_anode' : 'D26', 'pin_cathode' : 'D25' },
61-
'BTN1' : { 'pin' : 'D0', 'pinstate' : 'IN_PULLDOWN' }
62-
# CAPSENSE D8
61+
'BTN1' : { 'pin' : 'D0', 'pinstate' : 'IN_PULLDOWN' },
62+
'CAPSENSE' : { 'pin_rx' : 'D11', 'pin_tx' : 'D12' }
6363
# NFC D9/D10
6464

6565
};

libs/bluetooth/jswrap_bluetooth.c

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ void ble_app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t
240240
*
241241
* @details This function will be called in case of an assert in the SoftDevice.
242242
*
243-
* @warning This handler is an example only and does not fit a final product. You need to analyse
243+
* @warning This handler is an example only and does not fit a final product. You need to analyse
244244
* how your product is supposed to react in case of Assert.
245245
* @warning On assert from the SoftDevice, the system can only recover on reset.
246246
*
@@ -365,7 +365,7 @@ static void reset_prepare(void)
365365

366366
/**@brief Function for the GAP initialization.
367367
*
368-
* @details This function will set up all the necessary GAP (Generic Access Profile) parameters of
368+
* @details This function will set up all the necessary GAP (Generic Access Profile) parameters of
369369
* the device. It also sets the permissions and appearance.
370370
*/
371371
static void gap_params_init(void)
@@ -483,11 +483,11 @@ static void services_init(void)
483483
{
484484
uint32_t err_code;
485485
ble_nus_init_t nus_init;
486-
486+
487487
memset(&nus_init, 0, sizeof(nus_init));
488488

489489
nus_init.data_handler = nus_data_handler;
490-
490+
491491
err_code = ble_nus_init(&m_nus, &nus_init);
492492
APP_ERROR_CHECK(err_code);
493493

@@ -524,7 +524,7 @@ static void services_init(void)
524524
*/
525525
static void on_conn_params_evt(ble_conn_params_evt_t * p_evt) {
526526
uint32_t err_code;
527-
527+
528528
if(p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED) {
529529
err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
530530
APP_ERROR_CHECK(err_code);
@@ -546,7 +546,7 @@ static void conn_params_error_handler(uint32_t nrf_error) {
546546
static void conn_params_init(void) {
547547
uint32_t err_code;
548548
ble_conn_params_init_t cp_init;
549-
549+
550550
memset(&cp_init, 0, sizeof(cp_init));
551551

552552
cp_init.p_conn_params = NULL;
@@ -557,7 +557,7 @@ static void conn_params_init(void) {
557557
cp_init.disconnect_on_fail = false;
558558
cp_init.evt_handler = on_conn_params_evt;
559559
cp_init.error_handler = conn_params_error_handler;
560-
560+
561561
err_code = ble_conn_params_init(&cp_init);
562562
APP_ERROR_CHECK(err_code);
563563
}
@@ -680,7 +680,7 @@ static void on_ble_evt(ble_evt_t * p_ble_evt)
680680
{
681681
uint32_t err_code;
682682
//jsiConsolePrintf("\n[%d]\n", p_ble_evt->header.evt_id);
683-
683+
684684
switch (p_ble_evt->header.evt_id) {
685685
case BLE_GAP_EVT_TIMEOUT:
686686
// the timeout for sd_ble_gap_adv_start expired - kick it off again
@@ -842,10 +842,10 @@ static void on_ble_evt(ble_evt_t * p_ble_evt)
842842
}
843843

844844

845-
/**@brief Function for dispatching a SoftDevice event to all modules with a SoftDevice
845+
/**@brief Function for dispatching a SoftDevice event to all modules with a SoftDevice
846846
* event handler.
847847
*
848-
* @details This function is called from the SoftDevice event interrupt handler after a
848+
* @details This function is called from the SoftDevice event interrupt handler after a
849849
* SoftDevice event has been received.
850850
*
851851
* @param[in] p_ble_evt SoftDevice event.
@@ -914,7 +914,7 @@ void nfc_callback(void *context, NfcEvent event, const char *data, size_t dataLe
914914
static void ble_stack_init(void)
915915
{
916916
uint32_t err_code;
917-
917+
918918
// TODO: enable if we're on a device with 32kHz xtal
919919
/*nrf_clock_lf_cfg_t clock_lf_cfg = {
920920
.source = NRF_CLOCK_LF_SRC_XTAL,
@@ -929,13 +929,13 @@ static void ble_stack_init(void)
929929

930930
// Initialize SoftDevice.
931931
SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, false);
932-
932+
933933
ble_enable_params_t ble_enable_params;
934934
err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT,
935935
PERIPHERAL_LINK_COUNT,
936936
&ble_enable_params);
937937
APP_ERROR_CHECK(err_code);
938-
938+
939939
#ifdef USE_BOOTLOADER
940940
ble_enable_params.common_enable_params.vs_uuid_count = 2;
941941
ble_enable_params.gatts_enable_params.service_changed = 1;
@@ -953,7 +953,7 @@ static void ble_stack_init(void)
953953
// Enable BLE stack.
954954
err_code = softdevice_enable(&ble_enable_params);
955955
APP_ERROR_CHECK(err_code);
956-
956+
957957
// Subscribe for BLE events.
958958
err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
959959
APP_ERROR_CHECK(err_code);
@@ -1069,7 +1069,7 @@ void jswrap_nrf_bluetooth_init(void) {
10691069
// Initialize.
10701070
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
10711071
ble_stack_init();
1072-
1072+
10731073
#ifdef USE_BOOTLOADER
10741074
bool erase_bonds = false;
10751075
device_manager_init(erase_bonds);
@@ -1839,24 +1839,6 @@ void jswrap_nrf_nfcURL(JsVar *url) {
18391839
#endif
18401840
}
18411841

1842-
/*JSON{
1843-
"type" : "staticmethod",
1844-
"class" : "NRF",
1845-
"name" : "cap",
1846-
"#ifdef" : "NRF52",
1847-
"generate" : "jswrap_nrf_cap",
1848-
"params" : [
1849-
["tx","pin",""],
1850-
["rx","pin",""]
1851-
],
1852-
"return" : ["int", "Capacitive sense counter" ]
1853-
}
1854-
Capacitive sense. TX must be connected to RX pin and sense plate via 1MOhm resistor
1855-
*/
1856-
int jswrap_nrf_cap(Pin tx, Pin rx) {
1857-
return (int)nrf_utils_cap_sense(tx, rx);
1858-
}
1859-
18601842
/* ---------------------------------------------------------------------
18611843
* TESTING
18621844
* ---------------------------------------------------------------------
@@ -1907,5 +1889,3 @@ NRF.setServices({
19071889
});
19081890
19091891
*/
1910-
1911-

libs/bluetooth/jswrap_bluetooth.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ void jswrap_nrf_bleservice_discoverCharacteristics(JsVar *service);
3636
void jswrap_nrf_blecharacteristic_write(JsVar *characteristic, JsVar *data);
3737

3838
void jswrap_nrf_nfcURL(JsVar *url);
39-
int jswrap_nrf_cap(Pin tx, Pin rx);
40-
4139

4240
bool jswrap_nrf_idle();
4341
void jswrap_nrf_kill();

libs/puckjs/jswrap_puck.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
#include "jsinteractive.h"
2020
#include "jsdevices.h"
2121
#include "jshardware.h"
22+
#include "jsdevices.h"
23+
#include "jspin.h"
2224
#include "jstimer.h"
23-
2425
#include "nrf_gpio.h"
26+
#include "nrf5x_utils.h"
2527

2628
#define MAG_PWR 18
2729
#define MAG_INT 17
@@ -331,6 +333,29 @@ void jswrap_puck_IR(JsVar *data) {
331333
}
332334

333335

336+
/*JSON{
337+
"type" : "staticmethod",
338+
"class" : "Puck",
339+
"name" : "capSense",
340+
"#ifdef" : "NRF52",
341+
"generate" : "jswrap_puck_capSense",
342+
"params" : [
343+
["tx","pin",""],
344+
["rx","pin",""]
345+
],
346+
"return" : ["int", "Capacitive sense counter" ]
347+
}
348+
Capacitive sense. TX must be connected to RX pin and sense plate via 1MOhm resistor.
349+
350+
If no pins are supplied, the NFC ring is used for capacitive sense.
351+
*/
352+
int jswrap_puck_capSense(Pin tx, Pin rx) {
353+
if (jshIsPinValid(tx) && jshIsPinValid(rx)) {
354+
return (int)nrf_utils_cap_sense(tx, rx);
355+
}
356+
return (int)nrf_utils_cap_sense(CAPSENSE_TX_PIN, CAPSENSE_RX_PIN);
357+
}
358+
334359
/*JSON{
335360
"type" : "kill",
336361
"generate" : "jswrap_puck_kill"
@@ -340,7 +365,6 @@ void jswrap_puck_kill() {
340365
mag_off();
341366
mag_enabled = false;
342367
}
343-
344368
}
345369

346370
/*JSON{

libs/puckjs/jswrap_puck.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ void jswrap_puck_magOn();
1717
void jswrap_puck_magOff();
1818
JsVar *jswrap_puck_mag();
1919
void jswrap_puck_IR(JsVar *data);
20+
int jswrap_puck_capSense(Pin tx, Pin rx);
2021

2122
void jswrap_puck_kill();
2223
bool jswrap_puck_idle();

scripts/build_platform_config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@ def codeOutDevicePin(device, pin, definition_name):
384384
codeOutDevicePin("IR", "pin_anode", "IR_ANODE_PIN")
385385
codeOutDevicePin("IR", "pin_cathode", "IR_CATHODE_PIN")
386386

387+
if "CAPSENSE" in board.devices:
388+
codeOutDevicePin("CAPSENSE", "pin_rx", "CAPSENSE_RX_PIN")
389+
codeOutDevicePin("CAPSENSE", "pin_tx", "CAPSENSE_TX_PIN")
390+
387391
for device in ["USB","SD","LCD","JTAG","ESP8266","IR"]:
388392
if device in board.devices:
389393
for entry in board.devices[device]:

targets/nrf5x/jshardware.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ volatile bool flashIsBusy = false;
7272
volatile bool hadEvent = false; // set if we've had an event we need to deal with
7373
bool uartIsSending = false;
7474
bool uartInitialised = false;
75+
unsigned int ticksSinceStart = 0;
7576

7677
JshPinFunction pinStates[JSH_PIN_COUNT];
7778

@@ -95,6 +96,24 @@ void sys_evt_handler(uint32_t sys_evt) {
9596
}
9697
}
9798

99+
#ifdef NRF52
100+
/* SysTick interrupt Handler. */
101+
void SysTick_Handler(void) {
102+
/* Handle the delayed Ctrl-C -> interrupt behaviour (see description by EXEC_CTRL_C's definition) */
103+
if (execInfo.execute & EXEC_CTRL_C_WAIT)
104+
execInfo.execute = (execInfo.execute & ~EXEC_CTRL_C_WAIT) | EXEC_INTERRUPTED;
105+
if (execInfo.execute & EXEC_CTRL_C)
106+
execInfo.execute = (execInfo.execute & ~EXEC_CTRL_C) | EXEC_CTRL_C_WAIT;
107+
108+
ticksSinceStart++;
109+
/* One second after start, call jsinteractive. This is used to swap
110+
* to USB (if connected), or the Serial port. */
111+
if (ticksSinceStart == 5) {
112+
jsiOneSecondAfterStartup();
113+
}
114+
}
115+
#endif
116+
98117
#ifdef NRF52
99118
NRF_PWM_Type *nrf_get_pwm(JshPinFunction func) {
100119
if ((func&JSH_MASK_TYPE) == JSH_TIMER1) return NRF_PWM0;
@@ -195,12 +214,16 @@ void jshInit() {
195214
wakeup_handler);
196215
if (err_code) jsiConsolePrintf("app_timer_create error %d\n", err_code);
197216
#endif
198-
217+
199218
// Softdevice is initialised now
200219
softdevice_sys_evt_handler_set(sys_evt_handler);
201220
// Enable PPI driver
202221
err_code = nrf_drv_ppi_init();
203222
APP_ERROR_CHECK(err_code);
223+
#ifdef NRF52
224+
// Turn on SYSTICK - used for handling Ctrl-C behaviour
225+
SysTick_Config(0xFFFFFF);
226+
#endif
204227
}
205228

206229
// When 'reset' is called - we try and put peripherals back to their power-on state
@@ -215,10 +238,12 @@ void jshKill() {
215238

216239
// stuff to do on idle
217240
void jshIdle() {
241+
#ifndef NRF52
218242
if (init == 1) {
219243
jsiOneSecondAfterStartup(); // Do this the first time we enter jshIdle() after we have called jshInit() and never again.
220244
init = 0;
221245
}
246+
#endif
222247
}
223248

224249
/// Get this IC's serial number. Passed max # of chars and a pointer to write to. Returns # of chars

targets/nrf5x/nrf5x_utils.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "nrf_drv_gpiote.h"
2828
#include "nrf_drv_ppi.h"
2929

30+
#include "jsparse.h"
31+
3032
unsigned int nrf_utils_get_baud_enum(int baud) {
3133
switch (baud) {
3234
case 1200: return UART_BAUDRATE_BAUDRATE_Baud1200;
@@ -61,8 +63,8 @@ void nrf_utils_lfclk_config_and_start()
6163
NRF_CLOCK->TASKS_LFCLKSTART = 1;
6264
while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);
6365

64-
/*
65-
// wait until the clock is running - Xtal only?
66+
/*
67+
// wait until the clock is running - Xtal only?
6668
while (((NRF_CLOCK->LFCLKSTAT & CLOCK_LFCLKSTAT_STATE_Msk) != ((CLOCK_LFCLKSTAT_STATE_Running << CLOCK_LFCLKSTAT_STATE_Pos) & CLOCK_LFCLKSTAT_STATE_Msk)))
6769
{
6870
// Do nothing...
@@ -91,7 +93,7 @@ int nrf_utils_get_device_id(uint8_t * device_id, int maxChars)
9193

9294
uint8_t nrf_utils_get_random_number()
9395
{
94-
96+
9597
NRF_RNG->CONFIG = 0x00000001; // Use the bias generator.
9698
NRF_RNG->TASKS_START = 1;
9799

@@ -177,9 +179,11 @@ unsigned int nrf_utils_cap_sense(int capSenseTxPin, int capSenseRxPin) {
177179
NRF_TIMER2->EVENTS_COMPARE[1] = 0;
178180
int i;
179181
for (i=0;i<50;i++) {
180-
while (!NRF_TIMER2->EVENTS_COMPARE[1]);
182+
unsigned int timeout = 100000;
183+
while (!NRF_TIMER2->EVENTS_COMPARE[1] && --timeout);
181184
NRF_TIMER2->EVENTS_COMPARE[1] = 0;
182185
sum += NRF_TIMER2->CC[2];
186+
if (jspIsInterrupted()) break;
183187
}
184188

185189
nrf_gpio_cfg_input(capSenseTxPin, NRF_GPIO_PIN_NOPULL);
@@ -208,10 +212,15 @@ unsigned int nrf_utils_cap_sense(int capSenseTxPin, int capSenseRxPin) {
208212
nrf_gpio_cfg_output(capSenseTxPin);
209213

210214
for (i=0;i<100;i++) {
215+
const unsigned int CTR_MAX = 100000;
216+
unsigned int ctr = CTR_MAX;
211217
nrf_gpio_pin_set(capSenseTxPin);
212-
while (!(NRF_GPIO->IN & mask)) sum++;
218+
while (!(NRF_GPIO->IN & mask) && ctr--);
219+
sum += CTR_MAX-ctr;
213220
nrf_gpio_pin_clear(capSenseTxPin);
214-
while (NRF_GPIO->IN & mask) sum++;
221+
while (NRF_GPIO->IN & mask && ctr--);
222+
sum += CTR_MAX-ctr;
223+
if (jspIsInterrupted()) break;
215224
}
216225
nrf_gpio_cfg_input(capSenseTxPin, NRF_GPIO_PIN_NOPULL);
217226

0 commit comments

Comments
 (0)