mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
nRF5x: Ensure setWatch doesn't reset pin state
nRF5x: Reset pin states to default on 'reset()'
nRF5x: Move advertising back to 375ms (more reliable connections)
Puck: allow Puck.mag to work while magnetometer is on
This commit is contained in:
parent
fb73e80cd0
commit
b5dbb14a36
@ -8,6 +8,10 @@
|
||||
Fix comma operator regression caused by recent arrow functions addition
|
||||
Remove RTC changes for STM32F1
|
||||
nRF5x: Make sure that updateServices(notify) on a non-notifyable service will just error, rather than reset (fix #973)
|
||||
nRF5x: Ensure setWatch doesn't reset pin state
|
||||
nRF5x: Reset pin states to default on 'reset()'
|
||||
nRF5x: Move advertising back to 375ms (more reliable connections)
|
||||
Puck: allow Puck.mag to work while magnetometer is on
|
||||
|
||||
1v88 : jshSetEventCallback callbacks now get an argument with the channel number
|
||||
Tab complete now offers a much better set of completions (fix #926)
|
||||
|
||||
@ -38,6 +38,8 @@ const Pin PUCK_IO_PINS[] = {1,2,4,6,7,8,23,24,28,29,30,31};
|
||||
|
||||
// Has the magnetometer been turned on?
|
||||
bool mag_enabled = false;
|
||||
int16_t mag_reading[3];
|
||||
|
||||
|
||||
/* TODO: Use software I2C for this instead. Since we're relying
|
||||
* on the internal pullup resistors there might be some gotchas
|
||||
@ -190,18 +192,18 @@ void mag_wait() {
|
||||
}
|
||||
|
||||
// Read a value
|
||||
void mag_read(int16_t d[3]) {
|
||||
void mag_read() {
|
||||
i2c_start();
|
||||
i2c_wr(MAG3110_ADDR<<1);
|
||||
i2c_wr(1);
|
||||
i2c_start();
|
||||
i2c_wr(1|(MAG3110_ADDR<<1));
|
||||
d[0] = i2c_rd(false)<<8;
|
||||
d[0] |= i2c_rd(false);
|
||||
d[1] = i2c_rd(false)<<8;
|
||||
d[1] |= i2c_rd(false);
|
||||
d[2] = i2c_rd(false)<<8;
|
||||
d[2] |= i2c_rd(true);
|
||||
mag_reading[0] = i2c_rd(false)<<8;
|
||||
mag_reading[0] |= i2c_rd(false);
|
||||
mag_reading[1] = i2c_rd(false)<<8;
|
||||
mag_reading[1] |= i2c_rd(false);
|
||||
mag_reading[2] = i2c_rd(false)<<8;
|
||||
mag_reading[2] |= i2c_rd(true);
|
||||
i2c_stop();
|
||||
}
|
||||
|
||||
@ -254,12 +256,15 @@ varies from around 25-60 uT, so the reading will vary by 250 to 600 depending
|
||||
on location.
|
||||
*/
|
||||
JsVar *jswrap_puck_mag() {
|
||||
if (!mag_enabled) mag_on(80000);
|
||||
mag_wait();
|
||||
int16_t d[3];
|
||||
mag_read(d);
|
||||
if (!mag_enabled) mag_off();
|
||||
return mag_to_xyz(d);
|
||||
/* If not enabled, turn on and read. If enabled,
|
||||
* just pass out the last reading */
|
||||
if (!mag_enabled) {
|
||||
mag_on(80000);
|
||||
mag_wait();
|
||||
mag_read();
|
||||
mag_off();
|
||||
}
|
||||
return mag_to_xyz(mag_reading);
|
||||
}
|
||||
|
||||
/*JSON{
|
||||
@ -311,14 +316,16 @@ If given an argument, the sample rate is set (if not, it's at 0.63Hz). The sampl
|
||||
|
||||
*/
|
||||
void jswrap_puck_magOn(JsVarFloat hz) {
|
||||
if (!mag_enabled) {
|
||||
int milliHz = (int)((hz*1000)+0.5);
|
||||
if (milliHz==0) milliHz=630;
|
||||
if (!mag_on(milliHz)) {
|
||||
jsExceptionHere(JSET_ERROR, "Invalid sample rate %f - see docs for valid rates", hz);
|
||||
}
|
||||
jshPinWatch(MAG_INT, true);
|
||||
if (mag_enabled) {
|
||||
jsExceptionHere(JSET_ERROR, "Magnetometer is already on");
|
||||
return;
|
||||
}
|
||||
int milliHz = (int)((hz*1000)+0.5);
|
||||
if (milliHz==0) milliHz=630;
|
||||
if (!mag_on(milliHz)) {
|
||||
jsExceptionHere(JSET_ERROR, "Invalid sample rate %f - must be 80, 40, 20, 10, 5, 2.5, 1.25, 0.63, 0.31, 0.16 or 0.08 Hz", hz);
|
||||
}
|
||||
jshPinWatch(MAG_INT, true);
|
||||
mag_enabled = true;
|
||||
}
|
||||
|
||||
@ -593,11 +600,10 @@ bool jswrap_puck_selfTest() {
|
||||
|
||||
mag_on(80000);
|
||||
mag_wait();
|
||||
int16_t d[3];
|
||||
mag_read(d);
|
||||
mag_read();
|
||||
mag_off();
|
||||
mag_enabled = false;
|
||||
if (d[0]==-1 && d[1]==-1 && d[2]==-1) {
|
||||
if (mag_reading[0]==-1 && mag_reading[1]==-1 && mag_reading[2]==-1) {
|
||||
jsiConsolePrintf("Magnetometer not working?\n");
|
||||
ok = false;
|
||||
}
|
||||
@ -678,8 +684,8 @@ void jswrap_puck_kill() {
|
||||
bool jswrap_puck_idle() {
|
||||
if (mag_enabled && nrf_gpio_pin_read(MAG_INT)) {
|
||||
int16_t d[3];
|
||||
mag_read(d);
|
||||
JsVar *xyz = mag_to_xyz(d);
|
||||
mag_read();
|
||||
JsVar *xyz = mag_to_xyz(mag_reading);
|
||||
JsVar *puck = jsvObjectGetChild(execInfo.root, "Puck", 0);
|
||||
if (jsvHasChildren(puck))
|
||||
jsiQueueObjectCallbacks(puck, JS_EVENT_PREFIX"mag", &xyz, 1);
|
||||
|
||||
@ -52,7 +52,7 @@
|
||||
#define SCAN_INTERVAL MSEC_TO_UNITS(100, UNIT_0_625_MS) /**< Scan interval in units of 0.625 millisecond - 100 msec */
|
||||
#define SCAN_WINDOW MSEC_TO_UNITS(100, UNIT_0_625_MS) /**< Scan window in units of 0.625 millisecond - 100 msec */
|
||||
|
||||
#define ADVERTISING_INTERVAL MSEC_TO_UNITS(750, UNIT_0_625_MS) /**< The advertising interval (in units of 0.625 ms). */
|
||||
#define ADVERTISING_INTERVAL MSEC_TO_UNITS(375, UNIT_0_625_MS) /**< The advertising interval (in units of 0.625 ms). */
|
||||
#define APP_ADV_TIMEOUT_IN_SECONDS 180 /**< The advertising timeout (in units of seconds). */
|
||||
#define MIN_CONN_INTERVAL MSEC_TO_UNITS(7.5, UNIT_1_25_MS) /**< Minimum acceptable connection interval (7.5 ms), Connection interval uses 1.25 ms units. */
|
||||
#define MAX_CONN_INTERVAL MSEC_TO_UNITS(20, UNIT_1_25_MS) /**< Maximum acceptable connection interval (20 ms (was 75)), Connection interval uses 1.25 ms units. */
|
||||
|
||||
@ -191,8 +191,27 @@ void wakeup_handler() {
|
||||
}
|
||||
#endif
|
||||
|
||||
void jshResetPeripherals() {
|
||||
// Reset all pins to their power-on state (apart from default UART :)
|
||||
// Set pin state to input disconnected - saves power
|
||||
Pin i;
|
||||
for (i=0;i<JSH_PIN_COUNT;i++) {
|
||||
#ifdef DEFAULT_CONSOLE_TX_PIN
|
||||
if (i==DEFAULT_CONSOLE_TX_PIN) continue;
|
||||
#endif
|
||||
#ifdef DEFAULT_CONSOLE_RX_PIN
|
||||
if (i==DEFAULT_CONSOLE_RX_PIN) continue;
|
||||
#endif
|
||||
if (!IS_PIN_USED_INTERNALLY(i) && !IS_PIN_A_BUTTON(i)) {
|
||||
jshPinSetState(i, JSHPINSTATE_UNDEFINED);
|
||||
}
|
||||
}
|
||||
BITFIELD_CLEAR(jshPinSoftPWM);
|
||||
}
|
||||
|
||||
void jshInit() {
|
||||
jshInitDevices();
|
||||
jshResetPeripherals();
|
||||
|
||||
#ifdef LED1_PININDEX
|
||||
jshPinOutput(LED1_PININDEX, LED1_ONSTATE);
|
||||
@ -200,8 +219,6 @@ void jshInit() {
|
||||
|
||||
nrf_utils_lfclk_config_and_start();
|
||||
|
||||
BITFIELD_CLEAR(jshPinSoftPWM);
|
||||
|
||||
#ifdef DEFAULT_CONSOLE_RX_PIN
|
||||
#if !defined(NRF51822DK)
|
||||
// Only init UART if something is connected and RX is pulled up on boot...
|
||||
@ -271,7 +288,7 @@ void jshInit() {
|
||||
// When 'reset' is called - we try and put peripherals back to their power-on state
|
||||
void jshReset() {
|
||||
jshResetDevices();
|
||||
// TODO: Reset all pins to their power-on state (apart from default UART :)
|
||||
jshResetPeripherals();
|
||||
}
|
||||
|
||||
void jshKill() {
|
||||
@ -284,7 +301,7 @@ void jshIdle() {
|
||||
|
||||
/// Get this IC's serial number. Passed max # of chars and a pointer to write to. Returns # of chars
|
||||
int jshGetSerialNumber(unsigned char *data, int maxChars) {
|
||||
memcpy(data, NRF_FICR->DEVICEID, sizeof(NRF_FICR->DEVICEID));
|
||||
memcpy(data, (void*)NRF_FICR->DEVICEID, sizeof(NRF_FICR->DEVICEID));
|
||||
return sizeof(NRF_FICR->DEVICEID);
|
||||
}
|
||||
|
||||
@ -689,6 +706,7 @@ IOEventFlags jshPinWatch(Pin pin, bool shouldWatch) {
|
||||
uint32_t p = (uint32_t)pinInfo[pin].pin;
|
||||
if (shouldWatch) {
|
||||
nrf_drv_gpiote_in_config_t cls_1_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true); // FIXME: Maybe we want low accuracy? Otherwise this will
|
||||
cls_1_config.is_watcher = true; // stop this resetting the input state
|
||||
nrf_drv_gpiote_in_init(p, &cls_1_config, jsvPinWatchHandler);
|
||||
nrf_drv_gpiote_in_event_enable(p, true);
|
||||
return jshGetEventFlagsForWatchedPin(p);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user