Skip to content

Commit e79c913

Browse files
committed
capacitance sense
1 parent 6d5e74e commit e79c913

File tree

6 files changed

+152
-8
lines changed

6 files changed

+152
-8
lines changed

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,6 @@ ifdef NFC
13541354
INCLUDE += -I$(NRF5X_SDK_PATH)/components/nfc/ndef/uri
13551355
INCLUDE += -I$(NRF5X_SDK_PATH)/components/nfc/ndef/generic/message
13561356
INCLUDE += -I$(NRF5X_SDK_PATH)/components/nfc/ndef/generic/record
1357-
TARGETSOURCES += $(NRF5X_SDK_PATH)/components/libraries/util/app_util_platform.c
13581357
TARGETSOURCES += $(NRF5X_SDK_PATH)/components/drivers_nrf/clock/nrf_drv_clock.c
13591358
TARGETSOURCES += $(NRF5X_SDK_PATH)/components/nfc/ndef/uri/nfc_uri_msg.c
13601359
TARGETSOURCES += $(NRF5X_SDK_PATH)/components/nfc/ndef/uri/nfc_uri_rec.c
@@ -1486,7 +1485,8 @@ ifdef NRF5X
14861485
INCLUDE += -I$(NRF5X_SDK_PATH)/components/libraries/trace
14871486
INCLUDE += -I$(NRF5X_SDK_PATH)/components/softdevice/common/softdevice_handler
14881487
INCLUDE += -I$(NRF5X_SDK_PATH)/components/drivers_nrf/twi_master
1489-
INCLUDE += -I$(NRF5X_SDK_PATH)/components/drivers_nrf/hal/nrf_pwm
1488+
INCLUDE += -I$(NRF5X_SDK_PATH)/components/drivers_nrf/ppi
1489+
INCLUDE += -I$(NRF5X_SDK_PATH)/components/drivers_nrf/hal/nrf_pwm
14901490

14911491
TARGETSOURCES += \
14921492
$(NRF5X_SDK_PATH)/components/libraries/util/app_error.c \
@@ -1508,9 +1508,13 @@ ifdef NRF5X
15081508
$(NRF5X_SDK_PATH)/components/softdevice/common/softdevice_handler/softdevice_handler.c \
15091509
$(NRF5X_SDK_PATH)/components/drivers_nrf/hal/nrf_nvmc.c \
15101510
$(NRF5X_SDK_PATH)/components/drivers_nrf/twi_master/nrf_drv_twi.c \
1511+
$(NRF5X_SDK_PATH)/components/drivers_nrf/ppi/nrf_drv_ppi.c \
15111512
$(NRF5X_SDK_PATH)/components/drivers_nrf/hal/nrf_adc.c
1513+
15121514
# $(NRF5X_SDK_PATH)/components/libraries/util/nrf_log.c
15131515

1516+
TARGETSOURCES += $(NRF5X_SDK_PATH)/components/libraries/util/app_util_platform.c
1517+
15141518
ifdef USE_BOOTLOADER
15151519
INCLUDE += -I$(NRF5X_SDK_PATH)/components/ble/device_manager
15161520
INCLUDE += -I$(NRF5X_SDK_PATH)/components/ble/ble_services/ble_dfu

libs/bluetooth/jswrap_bluetooth.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "jswrap_bluetooth.h"
1414
#include "jsinteractive.h"
1515
#include "jsdevices.h"
16+
#include "nrf5x_utils.h"
1617

1718
#include <stdint.h>
1819
#include <string.h>
@@ -1722,6 +1723,23 @@ void jswrap_nrf_nfcURL(JsVar *url) {
17221723
#endif
17231724
}
17241725

1726+
/*JSON{
1727+
"type" : "staticmethod",
1728+
"class" : "NRF",
1729+
"name" : "cap",
1730+
"#ifdef" : "NRF52",
1731+
"generate" : "jswrap_nrf_cap",
1732+
"params" : [
1733+
["tx","pin",""],
1734+
["rx","pin",""]
1735+
],
1736+
"return" : ["int", "Capacitive sense counter" ]
1737+
}
1738+
Capacitive sense. TX must be connected to RX pin and sense plate via 1MOhm resistor
1739+
*/
1740+
int jswrap_nrf_cap(Pin tx, Pin rx) {
1741+
return (int)nrf_utils_cap_sense(tx, rx);
1742+
}
17251743

17261744
/* ---------------------------------------------------------------------
17271745
* TESTING

libs/bluetooth/jswrap_bluetooth.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void jswrap_nrf_bleservice_discoverCharacteristics(JsVar *service);
3737
void jswrap_nrf_blecharacteristic_write(JsVar *characteristic, JsVar *data);
3838

3939
void jswrap_nrf_nfcURL(JsVar *url);
40+
int jswrap_nrf_cap(Pin tx, Pin rx);
4041

4142

4243
bool jswrap_nrf_idle();

targets/nrf5x/jshardware.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,23 @@
3131

3232
#include "nrf_gpio.h"
3333
#include "nrf_gpiote.h"
34-
#include "nrf_drv_twi.h"
35-
#include "nrf_drv_gpiote.h"
3634
#include "nrf_temp.h"
3735
#include "nrf_timer.h"
38-
#include "app_uart.h"
39-
#include "nrf_drv_uart.h"
4036
#include "nrf_delay.h"
41-
4237
#ifdef NRF52
4338
#include "nrf_saadc.h"
4439
#include "nrf_pwm.h"
4540
#else
4641
#include "nrf_adc.h"
4742
#endif
4843

44+
#include "nrf_drv_uart.h"
45+
#include "nrf_drv_twi.h"
46+
#include "nrf_drv_gpiote.h"
47+
#include "nrf_drv_ppi.h"
48+
49+
#include "app_uart.h"
50+
4951
#include "nrf5x_utils.h"
5052
#include "softdevice_handler.h"
5153

@@ -182,6 +184,9 @@ void jshInit() {
182184

183185
// Softdevice is initialised now
184186
softdevice_sys_evt_handler_set(sys_evt_handler);
187+
// Enable PPI driver
188+
err_code = nrf_drv_ppi_init();
189+
APP_ERROR_CHECK(err_code);
185190
}
186191

187192
// When 'reset' is called - we try and put peripherals back to their power-on state
@@ -574,7 +579,7 @@ IOEventFlags jshPinWatch(Pin pin, bool shouldWatch) {
574579
if (!jshIsPinValid(pin)) return EV_NONE;
575580
uint32_t p = (uint32_t)pinInfo[pin].pin;
576581
if (shouldWatch) {
577-
nrf_drv_gpiote_in_config_t cls_1_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
582+
nrf_drv_gpiote_in_config_t cls_1_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true); // FIXME: Maybe we want low accuracy? Otherwise this will
578583
nrf_drv_gpiote_in_init(p, &cls_1_config, jsvPinWatchHandler);
579584
nrf_drv_gpiote_in_event_enable(p, true);
580585
return jshGetEventFlagsForWatchedPin(p);

targets/nrf5x/nrf5x_utils.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818

1919
#include "nrf.h"
2020
#include "nrf_gpio.h"
21+
#include "nrf_gpiote.h"
2122
#include "app_uart.h"
2223
#include "nrf_error.h"
2324
#include "nrf_nvmc.h"
25+
#include "nrf_timer.h"
26+
27+
#include "nrf_drv_gpiote.h"
28+
#include "nrf_drv_ppi.h"
2429

2530
unsigned int nrf_utils_get_baud_enum(int baud) {
2631
switch (baud) {
@@ -104,3 +109,112 @@ uint8_t nrf_utils_get_random_number()
104109
return rand_num;
105110

106111
}
112+
113+
unsigned int nrf_utils_cap_sense(int capSenseTxPin, int capSenseRxPin) {
114+
#ifdef NRF5DDD
115+
uint32_t err_code;
116+
117+
nrf_drv_gpiote_in_config_t rxconfig = GPIOTE_CONFIG_IN_SENSE_LOTOHI(true);
118+
nrf_drv_gpiote_in_init(capSenseRxPin, &rxconfig, 0);
119+
nrf_drv_gpiote_in_event_enable(capSenseRxPin, false /* no interrupt */);
120+
//
121+
122+
nrf_drv_gpiote_out_config_t txconfig = GPIOTE_CONFIG_OUT_TASK_TOGGLE(true);
123+
nrf_drv_gpiote_out_init(capSenseTxPin, &txconfig);
124+
nrf_drv_gpiote_out_task_enable(capSenseTxPin);
125+
//;
126+
127+
nrf_timer_mode_set(NRF_TIMER2, TIMER_MODE_MODE_Timer);
128+
nrf_timer_bit_width_set(NRF_TIMER2, NRF_TIMER_BIT_WIDTH_16);
129+
nrf_timer_frequency_set(NRF_TIMER2, NRF_TIMER_FREQ_16MHz);
130+
nrf_timer_cc_write(NRF_TIMER2, 0, 0);
131+
nrf_timer_cc_write(NRF_TIMER2, 1, 2047);
132+
nrf_timer_cc_write(NRF_TIMER2, 3, 4095);
133+
nrf_timer_shorts_enable(NRF_TIMER2, NRF_TIMER_SHORT_COMPARE3_CLEAR_MASK);
134+
135+
nrf_ppi_channel_t ppi_channel1, ppi_channel2, ppi_channel3;
136+
137+
// 1: When RX pin goes low->high, capture it in the timer
138+
err_code = nrf_drv_ppi_channel_alloc(&ppi_channel1);
139+
APP_ERROR_CHECK(err_code);
140+
err_code = nrf_drv_ppi_channel_assign(ppi_channel1,
141+
nrf_drv_gpiote_in_event_addr_get(capSenseRxPin),
142+
nrf_timer_task_address_get(NRF_TIMER2, NRF_TIMER_TASK_CAPTURE2));
143+
APP_ERROR_CHECK(err_code);
144+
145+
// 2: When timer is at Compare0, toggle
146+
err_code = nrf_drv_ppi_channel_alloc(&ppi_channel2);
147+
APP_ERROR_CHECK(err_code);
148+
err_code = nrf_drv_ppi_channel_assign(ppi_channel2,
149+
nrf_timer_event_address_get(NRF_TIMER2, NRF_TIMER_EVENT_COMPARE0),
150+
nrf_drv_gpiote_out_task_addr_get(capSenseTxPin));
151+
APP_ERROR_CHECK(err_code);
152+
// 3: When timer is at Compare1, toggle
153+
err_code = nrf_drv_ppi_channel_alloc(&ppi_channel3);
154+
APP_ERROR_CHECK(err_code);
155+
err_code = nrf_drv_ppi_channel_assign(ppi_channel3,
156+
nrf_timer_event_address_get(NRF_TIMER2, NRF_TIMER_EVENT_COMPARE1),
157+
nrf_drv_gpiote_out_task_addr_get(capSenseTxPin));
158+
APP_ERROR_CHECK(err_code);
159+
160+
// Enable both configured PPI channels
161+
err_code = nrf_drv_ppi_channel_enable(ppi_channel1);
162+
APP_ERROR_CHECK(err_code);
163+
err_code = nrf_drv_ppi_channel_enable(ppi_channel2);
164+
APP_ERROR_CHECK(err_code);
165+
err_code = nrf_drv_ppi_channel_enable(ppi_channel3);
166+
APP_ERROR_CHECK(err_code);
167+
168+
// start timer
169+
170+
NRF_TIMER2->CC[2] = 0;
171+
nrf_timer_task_trigger(NRF_TIMER2, NRF_TIMER_TASK_START);
172+
nrf_gpio_cfg_input(capSenseRxPin, NRF_GPIO_PIN_NOPULL);
173+
nrf_gpio_cfg_output(capSenseTxPin);
174+
175+
//
176+
unsigned int sum = 0;
177+
NRF_TIMER2->EVENTS_COMPARE[1] = 0;
178+
int i;
179+
for (i=0;i<50;i++) {
180+
while (!NRF_TIMER2->EVENTS_COMPARE[1]);
181+
NRF_TIMER2->EVENTS_COMPARE[1] = 0;
182+
sum += NRF_TIMER2->CC[2];
183+
}
184+
185+
nrf_gpio_cfg_input(capSenseTxPin, NRF_GPIO_PIN_NOPULL);
186+
187+
nrf_timer_task_trigger(NRF_TIMER2, NRF_TIMER_TASK_STOP);
188+
nrf_timer_shorts_disable(NRF_TIMER2, NRF_TIMER_SHORT_COMPARE3_CLEAR_MASK);
189+
nrf_drv_ppi_channel_disable(ppi_channel3);
190+
nrf_drv_ppi_channel_disable(ppi_channel2);
191+
nrf_drv_ppi_channel_disable(ppi_channel1);
192+
nrf_drv_ppi_channel_free(ppi_channel3);
193+
nrf_drv_ppi_channel_free(ppi_channel2);
194+
nrf_drv_ppi_channel_free(ppi_channel1);
195+
196+
nrf_drv_gpiote_in_uninit(capSenseTxPin);
197+
nrf_drv_gpiote_in_uninit(capSenseRxPin);
198+
199+
return sum;
200+
#else
201+
unsigned int sum = 0;
202+
203+
int i;
204+
unsigned int mask = 1<<capSenseRxPin;
205+
206+
nrf_gpio_cfg_input(capSenseRxPin, NRF_GPIO_PIN_NOPULL);
207+
nrf_gpio_pin_clear(capSenseTxPin);
208+
nrf_gpio_cfg_output(capSenseTxPin);
209+
210+
for (i=0;i<100;i++) {
211+
nrf_gpio_pin_set(capSenseTxPin);
212+
while (!(NRF_GPIO->IN & mask)) sum++;
213+
nrf_gpio_pin_clear(capSenseTxPin);
214+
while (NRF_GPIO->IN & mask) sum++;
215+
}
216+
nrf_gpio_cfg_input(capSenseTxPin, NRF_GPIO_PIN_NOPULL);
217+
218+
return sum;
219+
#endif
220+
}

targets/nrf5x/nrf5x_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ void nrf_utils_lfclk_config_and_start(void);
2828
int nrf_utils_get_device_id(uint8_t * device_id, int maxChars);
2929
uint8_t nrf_utils_get_random_number(void);
3030

31+
unsigned int nrf_utils_cap_sense(int capSenseTxPin, int capSenseRxPin);
32+
3133
#endif // NRF5X_UTILS_H__
3234

3335
/** @} */

0 commit comments

Comments
 (0)