From e99bd4dbaa999254586c6f90a54a5c99ac66ae53 Mon Sep 17 00:00:00 2001 From: ichbtm <7622549+ichbtm@users.noreply.github.com> Date: Fri, 30 Mar 2018 17:00:19 +0200 Subject: [PATCH 01/11] use user limits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit and µs functions use our return µs --- libraries/Servo/src/nrf52/Servo.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/libraries/Servo/src/nrf52/Servo.cpp b/libraries/Servo/src/nrf52/Servo.cpp index 5f5414ff..455552d6 100644 --- a/libraries/Servo/src/nrf52/Servo.cpp +++ b/libraries/Servo/src/nrf52/Servo.cpp @@ -44,19 +44,21 @@ Servo::Servo() uint8_t Servo::attach(int pin) { - return this->attach(pin, 0, 2500); + return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); } uint8_t Servo::attach(int pin, int min, int max) { - int servo_min, servo_max; if (this->servoIndex < MAX_SERVOS) { pinMode(pin, OUTPUT); // set servo pin to output servos[this->servoIndex].Pin.nbr = pin; - if(min < servo_min) min = servo_min; - if (max > servo_max) max = servo_max; + if(min < MIN_PULSE_WIDTH) min = MIN_PULSE_WIDTH; + if (max > MAX_PULSE_WIDTH) max = MAX_PULSE_WIDTH; + //fix min if conversion to pulse cycle value is too low + if((min/DUTY_CYCLE_RESOLUTION)*DUTY_CYCLE_RESOLUTIONmin = min; this->max = max; @@ -67,9 +69,8 @@ uint8_t Servo::attach(int pin, int min, int max) { if ( HwPWMx[i]->addPin(pin) ) { - // 20ms - 50Hz - HwPWMx[i]->setMaxValue(2500); - HwPWMx[i]->setClockDiv(PWM_PRESCALER_PRESCALER_DIV_128); + HwPWMx[i]->setMaxValue(MAXVALUE); + HwPWMx[i]->setClockDiv(CLOCKDIV); break; } @@ -93,7 +94,7 @@ void Servo::write(int value) value = 0; else if (value > 180) value = 180; - value = map(value, 0, 180, MIN_PULSE, MAX_PULSE); + value = map(value, 0, 180, this->min, this->max); writeMicroseconds(value); } @@ -127,17 +128,17 @@ void Servo::writeMicroseconds(int value) nrf_pwm_task_trigger(PWMInstance, NRF_PWM_TASK_SEQSTART0); #else uint8_t pin = servos[this->servoIndex].Pin.nbr; - + for(int i=0; iwritePin(pin, value) ) break; + if ( HwPWMx[i]->writePin(pin, value/DUTY_CYCLE_RESOLUTION) ) break; } #endif } int Servo::read() // return the value as degrees { - return map(readMicroseconds(), MIN_PULSE, MAX_PULSE, 0, 180); + return map(readMicroseconds(), this->min, this->max, 0, 180); } int Servo::readMicroseconds() @@ -156,7 +157,7 @@ int Servo::readMicroseconds() { if ( HwPWMx[i]->checkPin(pin) ) { - return HwPWMx[i]->readPin(pin); + return HwPWMx[i]->readPin(pin)*DUTY_CYCLE_RESOLUTION; } } From a6b744fe3cfb58eb36220991df7c28c0ed9b2b53 Mon Sep 17 00:00:00 2001 From: ichbtm <7622549+ichbtm@users.noreply.github.com> Date: Fri, 30 Mar 2018 17:02:01 +0200 Subject: [PATCH 02/11] added named constants and removed hoardcoded pulse cycle limits --- libraries/Servo/src/nrf52/ServoTimers.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/libraries/Servo/src/nrf52/ServoTimers.h b/libraries/Servo/src/nrf52/ServoTimers.h index 51759bea..ce6c7de6 100644 --- a/libraries/Servo/src/nrf52/ServoTimers.h +++ b/libraries/Servo/src/nrf52/ServoTimers.h @@ -28,11 +28,14 @@ * NRF52 Only definitions * --------------------- */ - -#define MIN_PULSE 55 -#define MAX_PULSE 284 +//PWM_PRESCALER_PRESCALER_DIV_128 -> NRF_PWM_CLK_125kHz -> resolution 8µs +//MaxValue = 2500 -> PWM period = 20ms +//20ms - 50Hz +#define DUTY_CYCLE_RESOLUTION 8 +#define MAXVALUE 2500 +#define CLOCKDIV PWM_PRESCALER_PRESCALER_DIV_128 // define one timer in order to have MAX_SERVOS = 12 typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t; -#endif // __SERVO_TIMERS_H__ \ No newline at end of file +#endif // __SERVO_TIMERS_H__ From f07db89047fc728db21473b41de2570f6c06808e Mon Sep 17 00:00:00 2001 From: ichbtm <7622549+ichbtm@users.noreply.github.com> Date: Fri, 30 Mar 2018 17:03:24 +0200 Subject: [PATCH 03/11] changed min and max to uint16_t --- libraries/Servo/src/Servo.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Servo/src/Servo.h b/libraries/Servo/src/Servo.h index 4890a141..640f7436 100644 --- a/libraries/Servo/src/Servo.h +++ b/libraries/Servo/src/Servo.h @@ -111,8 +111,8 @@ public: bool attached(); // return true if this servo is attached, otherwise false private: uint8_t servoIndex; // index into the channel data for this servo - int8_t min; // minimum is this value times 4 added to MIN_PULSE_WIDTH - int8_t max; // maximum is this value times 4 added to MAX_PULSE_WIDTH + int16_t min; // minimum pulse in µs + int16_t max; // maximum pulse in µs }; #endif From 644c0a247d313555baa3d82f902ec1de7aaa1319 Mon Sep 17 00:00:00 2001 From: Sean Wilson Date: Fri, 6 Apr 2018 10:50:15 -0400 Subject: [PATCH 04/11] Fix crash caused by incorrectly formatted device reports. --- libraries/Bluefruit52Lib/src/BLEScanner.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libraries/Bluefruit52Lib/src/BLEScanner.cpp b/libraries/Bluefruit52Lib/src/BLEScanner.cpp index 905a8ce3..a055326a 100644 --- a/libraries/Bluefruit52Lib/src/BLEScanner.cpp +++ b/libraries/Bluefruit52Lib/src/BLEScanner.cpp @@ -147,6 +147,8 @@ uint8_t BLEScanner::parseReportByType(const uint8_t* scandata, uint8_t scanlen, uint8_t len = 0; uint8_t const* ptr = NULL; + if ( scanlen < 2 ) return 0; + // len (1+data), type, data while ( scanlen ) { From c4cb13266e61e21ee5f05e8ca206a6e2dc2fbea6 Mon Sep 17 00:00:00 2001 From: "LAPTOP-JCFJCJ8H\\acer" Date: Fri, 1 Jun 2018 16:13:47 -0700 Subject: [PATCH 05/11] Central connection sample with Peripheral Texas Instruments Sensor Tag (CC2650). Connect, enable optical sensor, adjust measurement/transmission period. http://processors.wiki.ti.com/index.php/CC2650_SensorTag_User%27s_Guide --- .../central_ti_sensortag_optical.ino | 302 ++++++++++++++++++ 1 file changed, 302 insertions(+) create mode 100644 libraries/Bluefruit52Lib/examples/Central/central_ti_sensortag_optical/central_ti_sensortag_optical.ino diff --git a/libraries/Bluefruit52Lib/examples/Central/central_ti_sensortag_optical/central_ti_sensortag_optical.ino b/libraries/Bluefruit52Lib/examples/Central/central_ti_sensortag_optical/central_ti_sensortag_optical.ino new file mode 100644 index 00000000..5d75f79d --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/Central/central_ti_sensortag_optical/central_ti_sensortag_optical.ino @@ -0,0 +1,302 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* This sketch shows how to use BLEClientService and BLEClientCharacteristic + * to implement a custom client that is used to talk with Gatt server on + * the TI SensorTag peripheral device. + * + * Online uuid/hex converters + * https://yupana-engineering.com/online-uuid-to-c-array-converter + * https://www.scadacore.com/tools/programming-calculators/online-hex-converter/ + * https://codebeautify.org/string-hex-converter + * + * Adafruit Bluefruit Feather nRF52 + * https://learn.adafruit.com/bluefruit-nrf52-feather-learning-guide/blescanner + * + * Sensor Tag + * http://processors.wiki.ti.com/index.php/CC2650_SensorTag_User%27s_Guide#Optical_Sensor + * + * Scan Response (Extended Advertisement data retrieval) + * https://devzone.nordicsemi.com/tutorials/b/bluetooth-low-energy/posts/ble-advertising-a-beginners-tutorial + * + */ + + #include + +/* TI Sensor Tag UUID Definitions + * Base SensorTag UUID pattern: F000-0000-0451-4000-B000-000000000000 + * Optical Service: 0xA070 + * Optical Characteristic: 0xAA71 + * Optical Data Collection Enabler: 0xAA72 + * Optical Measurement Period: 0xAA73 + * Local Name: "CC2650 SensorTag" + */ + + uint8_t SENSORTAG_OPTICAL_SERVICE_UUID[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xB0,0x00,0x40,0x51,0x04,0x70,0xAA,0x00,0xF0}; + uint8_t SENSORTAG_OPTICAL_CHARACTERISTIC_UUID[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xB0,0x00,0x40,0x51,0x04,0x71,0xAA,0x00,0xF0}; + uint8_t SENSORTAG_OPTICAL_ENABLE_CHARACTERISTIC_UUID[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xB0,0x00,0x40,0x51,0x04,0x72,0xAA,0x00,0xF0}; + uint8_t SENSORTAG_OPTICAL_PERIOD_CHARACTERISTIC_UUID[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xB0,0x00,0x40,0x51,0x04,0x73,0xAA,0x00,0xF0}; + uint8_t SENSORTAG_ADV_COMPLETE_LOCAL_NAME[] = {0x43,0x43,0x32,0x36,0x35,0x30,0x20,0x53,0x65,0x6e,0x73,0x6f,0x72,0x54,0x61,0x67}; + +BLEClientService sensorTagOpticalService(SENSORTAG_OPTICAL_SERVICE_UUID); +BLEClientCharacteristic sensorTagOpticalCharacteristic(SENSORTAG_OPTICAL_CHARACTERISTIC_UUID); +BLEClientCharacteristic opticalCharacteristicDataCollectionEnabler(SENSORTAG_OPTICAL_ENABLE_CHARACTERISTIC_UUID); +BLEClientCharacteristic opticalCharacteristicPeriod(SENSORTAG_OPTICAL_PERIOD_CHARACTERISTIC_UUID); + + +void setup() +{ + Serial.begin(115200); + + Serial.println("Bluefruit52 Central TI Sensor Tag Example"); + Serial.println("-----------------------------------------\n"); + + // Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1 + // SRAM usage required by SoftDevice will increase dramatically with number of connections + Bluefruit.begin(0, 1); + + Bluefruit.setName("Bluefruit52 TI SensorTag Central"); + + // Initialize Service + sensorTagOpticalService.begin(); + + // set up callback for receiving measurement + sensorTagOpticalCharacteristic.setNotifyCallback(sensortag_optical_notify_callback); + sensorTagOpticalCharacteristic.begin(); + + // set up the characteristic to enable the optical component on the device + opticalCharacteristicDataCollectionEnabler.begin(); + + // set up the characteristic to adjust the measurement period + opticalCharacteristicPeriod.begin(); + + // Increase Blink rate to different from PrPh advertising mode + Bluefruit.setConnLedInterval(250); + + // Callbacks for Central + Bluefruit.Central.setDisconnectCallback(disconnect_callback); + Bluefruit.Central.setConnectCallback(connect_callback); + + /* Start Central Scanning + * - Enable auto scan if disconnected + * - Interval = 100 ms, window = 80 ms + * - No UUID Filter + * - Use active scan + * - Start(timeout) with timeout = 0 will scan forever (until connected) + */ + + Bluefruit.Scanner.setRxCallback(scan_callback); + Bluefruit.Scanner.restartOnDisconnect(true); + Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 ms + // Bluefruit.Scanner.filterUuid(); // do not not set anything here or Scan Response won't work + Bluefruit.Scanner.useActiveScan(true); // required for SensorTag to reveal the Local Name in the advertisement. + Bluefruit.Scanner.start(0); // 0 = Don't stop scanning after n seconds +} + +void loop() +{ + // do nothing +} + +/** + * Callback invoked when scanner pick up an advertising data + * @param report Structural advertising data + */ +void scan_callback(ble_gap_evt_adv_report_t* report) +{ + Serial.println(""); + Serial.println("Scan Callback"); + printReport( report ); + + /* Choose a peripheral to connect with by searching for an advertisement packet with a + Complete Local Name matching our target device*/ + uint8_t buffer[BLE_GAP_ADV_MAX_SIZE] = { 0 }; + + Serial.print("Parsing report for Local Name ... "); + if(Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, buffer, sizeof(buffer))) + { + Serial.println("Found Local Name"); + Serial.printf("%14s %s\n", "Local Name:", buffer); + + Serial.print(" Local Name data: "); + printHexList(buffer, BLE_GAP_ADV_MAX_SIZE ); + + Serial.print("Determining Local Name Match ... "); + if ( !memcmp( buffer, SENSORTAG_ADV_COMPLETE_LOCAL_NAME, sizeof(SENSORTAG_ADV_COMPLETE_LOCAL_NAME)) ) + { + Serial.println("Local Name Match!"); + + Serial.println("Connecting to Peripheral ... "); + Bluefruit.Central.connect(report); + } + else + { + Serial.println("No Match"); + } + } + else + { + Serial.println("Failed"); + } +} + +/** + * Callback invoked when an connection is established + * @param conn_handle + */ +void connect_callback(uint16_t conn_handle) +{ + Serial.println(""); + Serial.print("Connect Callback, conn_handle: "); + Serial.println( conn_handle ); + + /* Complete Local Name */ + uint8_t buffer[BLE_GAP_ADV_MAX_SIZE] = { 0 }; + + // If Service is not found, disconnect and return + Serial.print("Discovering Optical Service ... "); + if ( !sensorTagOpticalService.discover(conn_handle) ) + { + Serial.println("No Service Found"); + + // disconect since we couldn't find service + Bluefruit.Central.disconnect(conn_handle); + + return; + } + Serial.println("Service Found"); + + // Once service is found, we continue to discover the primary characteristic + Serial.print("Discovering Optical Characteristic ... "); + if ( !sensorTagOpticalCharacteristic.discover() ) + { + // Measurement chr is mandatory, if it is not found (valid), then disconnect + Serial.println("No Characteristic Found. Characteristic is mandatory but not found. "); + Bluefruit.Central.disconnect(conn_handle); + return; + } + Serial.println("Characteristic Found"); + + // Data Collection Charactistic. Find and enable. + // You enable data collection on the Characteristic before the peripheral will start measuring values. + // This is different than setting the Notify descriptor, which is handled below. + Serial.print("Discovering Data Collection Configuration Characteristic ... "); + if ( !opticalCharacteristicDataCollectionEnabler.discover() ) + { + Serial.println("No Characteristic Found. Characteristic is mandatory but not found."); + Bluefruit.Central.disconnect(conn_handle); + return; + } + Serial.println("Characteristic Found"); + // Found it, now write 0x01 to turn on optical data collection + Serial.print("Enabling Data Collection, return value: "); + Serial.println( opticalCharacteristicDataCollectionEnabler.write8( 0x01 ) ); + + // Measurement Period Characteristic. Find and adjust. + Serial.print("Measurement Period Characteristic ... "); + if ( !opticalCharacteristicPeriod.discover() ) + { + Serial.println("No Characteristic Found, but not mandatory so not disconnecting"); + } + Serial.println("Characteristic Found"); + // Found it, now adjust it: + // Resolution 10 ms. Range 100 ms (0x0A) to 2.55 sec (0xFF). Default is 800 milliseconds (0x50). + // 19 is 250 + Serial.print("Adjusting Measurement Period, return value: "); + // Serial.println( opticalCharacteristicPeriod.write8( 0xFF ) ); // Slowest + Serial.println( opticalCharacteristicPeriod.write8( 0x0A ) ); // Fastest + + // Reaching here means we are ready to go, let's enable notification on measurement chr + Serial.print("Enabling Notify on the Characteristic ... "); + if ( sensorTagOpticalCharacteristic.enableNotify() ) + { + Serial.println("enableNotify success, now ready to receive Characteristic values"); + }else + { + Serial.println("Couldn't enable notify for Characteristic. Increase DEBUG LEVEL for troubleshooting"); + } +} + +/** + * Callback invoked when a connection is dropped + * @param conn_handle + * @param reason + */ +void disconnect_callback(uint16_t conn_handle, uint8_t reason) +{ + (void) conn_handle; + (void) reason; + + Serial.println("Disconnected"); +} + +/** + * Hooked callback that triggered when a measurement value is sent from peripheral + * @param chr Pointer client characteristic that even occurred, + * in this example it should be sensorTagOpticalCharacteristic + * @param data Pointer to received data + * @param len Length of received data + */ +void sensortag_optical_notify_callback(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len) +{ + Serial.print("Optical data: "); + uint16_t tData = (uint16_t) ( ( data[0] & 0xFF ) | ( data[1] & 0xFF ) << 8 ); + Serial.print( sensorOpt3001Convert( tData ) ); + Serial.println( " Lux" ); +} + +/* + * Conversion to Lux. Algorithm from the TI Sensor Tag Wiki documentation page + */ +float sensorOpt3001Convert(uint16_t rawData) +{ + uint16_t e, m; + + m = rawData & 0x0FFF; + e = (rawData & 0xF000) >> 12; + + /** e on 4 bits stored in a 16 bit unsigned => it can store 2 << (e - 1) with e < 16 */ + e = (e == 0) ? 1 : 2 << (e - 1); + + return m * (0.01 * e); +} + +/* Prints a hex list to the Serial Monitor */ +void printHexList(uint8_t* buffer, uint8_t len) +{ + // print forward order + for(int i=0; irssi ); + Serial.print( " scan_rsp: " ); + Serial.println( report->scan_rsp ); + Serial.print( " type: " ); + Serial.println( report->type ); + Serial.print( " dlen: " ); + Serial.println( report->dlen ); + Serial.print( " data: " ); + for( int i = 0; i < report->dlen; i+= sizeof(uint8_t) ) + { + Serial.printf( "%02X-", report->data[ i ] ); + } + Serial.println(""); +} From 456cb4a628a71c2427bad076e36daf974642d1de Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 12 Jul 2018 15:58:01 +0700 Subject: [PATCH 06/11] fix PWM max_value type cause incorrect servo behavior (640Hz instead of 50hz) --- cores/nRF5/HardwarePWM.h | 2 +- libraries/Servo/src/Servo.h | 8 ++-- libraries/Servo/src/nrf52/Servo.cpp | 70 +++++------------------------ 3 files changed, 16 insertions(+), 64 deletions(-) diff --git a/cores/nRF5/HardwarePWM.h b/cores/nRF5/HardwarePWM.h index 27551c3f..280f4ac0 100644 --- a/cores/nRF5/HardwarePWM.h +++ b/cores/nRF5/HardwarePWM.h @@ -50,7 +50,7 @@ class HardwarePWM uint8_t _count; uint16_t _seq0[MAX_CHANNELS]; - uint8_t _max_value; + uint16_t _max_value; uint8_t _clock_div; void _start(void); diff --git a/libraries/Servo/src/Servo.h b/libraries/Servo/src/Servo.h index 640f7436..1dfc1c8c 100644 --- a/libraries/Servo/src/Servo.h +++ b/libraries/Servo/src/Servo.h @@ -75,8 +75,8 @@ #define Servo_VERSION 2 // software version of this library -#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo -#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo +#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo +#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo #define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached #define REFRESH_INTERVAL 20000 // minumim time to refresh servos in microseconds @@ -112,7 +112,9 @@ public: private: uint8_t servoIndex; // index into the channel data for this servo int16_t min; // minimum pulse in µs - int16_t max; // maximum pulse in µs + int16_t max; // maximum pulse in µs + + HardwarePWM* pwm; }; #endif diff --git a/libraries/Servo/src/nrf52/Servo.cpp b/libraries/Servo/src/nrf52/Servo.cpp index 455552d6..664fc5f8 100644 --- a/libraries/Servo/src/nrf52/Servo.cpp +++ b/libraries/Servo/src/nrf52/Servo.cpp @@ -22,14 +22,8 @@ #include -static servo_t servos[MAX_SERVOS]; // static array of servo structures - -uint8_t ServoCount = 0; // the total number of attached servos - - - -//uint32_t group_pins[3][NRF_PWM_CHANNEL_COUNT]={{NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED}, {NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED}, {NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED}}; -//static uint16_t seq_values[3][NRF_PWM_CHANNEL_COUNT]={{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}; +static servo_t servos[MAX_SERVOS]; // static array of servo structures +uint8_t ServoCount = 0; // the total number of attached servos Servo::Servo() { @@ -39,15 +33,14 @@ Servo::Servo() this->servoIndex = INVALID_SERVO; // too many servos } + this->pwm = NULL; } uint8_t Servo::attach(int pin) { - return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); } - uint8_t Servo::attach(int pin, int min, int max) { if (this->servoIndex < MAX_SERVOS) { @@ -56,6 +49,7 @@ uint8_t Servo::attach(int pin, int min, int max) if(min < MIN_PULSE_WIDTH) min = MIN_PULSE_WIDTH; if (max > MAX_PULSE_WIDTH) max = MAX_PULSE_WIDTH; + //fix min if conversion to pulse cycle value is too low if((min/DUTY_CYCLE_RESOLUTION)*DUTY_CYCLE_RESOLUTIONaddPin(pin) ) { - HwPWMx[i]->setMaxValue(MAXVALUE); - HwPWMx[i]->setClockDiv(CLOCKDIV); - + this->pwm = HwPWMx[i]; break; } } + this->pwm->setMaxValue(MAXVALUE); + this->pwm->setClockDiv(CLOCKDIV); + } return this->servoIndex; } @@ -83,7 +78,6 @@ uint8_t Servo::attach(int pin, int min, int max) void Servo::detach() { servos[this->servoIndex].Pin.isActive = false; - // TODO Adafruit remove pin from HW PWM } @@ -102,38 +96,9 @@ void Servo::write(int value) void Servo::writeMicroseconds(int value) { -#if 0 - uint8_t channel, instance; - uint8_t pin = servos[this->servoIndex].Pin.nbr; - //instance of pwm module is MSB - look at VWariant.h - instance=(g_APinDescription[pin].ulPWMChannel & 0xF0)/16; - //index of pwm channel is LSB - look at VWariant.h - channel=g_APinDescription[pin].ulPWMChannel & 0x0F; - group_pins[instance][channel]=g_APinDescription[pin].ulPin; - NRF_PWM_Type * PWMInstance = instance == 0 ? NRF_PWM0 : (instance == 1 ? NRF_PWM1 : NRF_PWM2); - //configure pwm instance and enable it - seq_values[instance][channel]= value | 0x8000; - nrf_pwm_sequence_t const seq={ - seq_values[instance], - NRF_PWM_VALUES_LENGTH(seq_values), - 0, - 0 - }; - nrf_pwm_pins_set(PWMInstance, group_pins[instance]); - nrf_pwm_enable(PWMInstance); - nrf_pwm_configure(PWMInstance, NRF_PWM_CLK_125kHz, NRF_PWM_MODE_UP, 2500); // 20ms - 50Hz - nrf_pwm_decoder_set(PWMInstance, NRF_PWM_LOAD_INDIVIDUAL, NRF_PWM_STEP_AUTO); - nrf_pwm_sequence_set(PWMInstance, 0, &seq); - nrf_pwm_loop_set(PWMInstance, 0UL); - nrf_pwm_task_trigger(PWMInstance, NRF_PWM_TASK_SEQSTART0); -#else uint8_t pin = servos[this->servoIndex].Pin.nbr; - for(int i=0; iwritePin(pin, value/DUTY_CYCLE_RESOLUTION) ) break; - } -#endif + if ( this->pwm ) this->pwm->writePin(pin, value/DUTY_CYCLE_RESOLUTION); } int Servo::read() // return the value as degrees @@ -143,26 +108,11 @@ int Servo::read() // return the value as degrees int Servo::readMicroseconds() { -#if 0 - uint8_t channel, instance; - uint8_t pin=servos[this->servoIndex].Pin.nbr; - instance=(g_APinDescription[pin].ulPWMChannel & 0xF0)/16; - channel=g_APinDescription[pin].ulPWMChannel & 0x0F; - // remove the 16th bit we added before - return seq_values[instance][channel] & 0x7FFF; -#else uint8_t pin=servos[this->servoIndex].Pin.nbr; - for(int i=0; icheckPin(pin) ) - { - return HwPWMx[i]->readPin(pin)*DUTY_CYCLE_RESOLUTION; - } - } + if ( this->pwm ) return this->pwm->readPin(pin)*DUTY_CYCLE_RESOLUTION; return 0; -#endif } bool Servo::attached() From 2ebb5de83059d88b6c97d929063aad3a8bc583d8 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 12 Jul 2018 16:13:26 +0700 Subject: [PATCH 07/11] fix windows user name with space when uploading issue --- platform.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform.txt b/platform.txt index 20050b1c..4badb59b 100644 --- a/platform.txt +++ b/platform.txt @@ -132,7 +132,7 @@ tools.nrfutil.cmd.macosx=/usr/local/bin/nrfutil tools.nrfutil.upload.params.verbose=--verbose tools.nrfutil.upload.params.quiet= -tools.nrfutil.upload.pattern={cmd} {upload.verbose} dfu serial -pkg "{build.path}/{build.project_name}.zip" -p {serial.port} -b 115200 +tools.nrfutil.upload.pattern="{cmd}" {upload.verbose} dfu serial -pkg "{build.path}/{build.project_name}.zip" -p {serial.port} -b 115200 #********************************************** # Burning bootloader with either jlink or nrfutil From 1977aea69ee5d85bdf69733701da815bd18c4d12 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 12 Jul 2018 16:19:27 +0700 Subject: [PATCH 08/11] correct update digitalPinToPort/portOutputRegister --- cores/nRF5/Arduino.h | 10 +++++----- .../examples/Peripheral/ancs_oled/ancs_oled.ino | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cores/nRF5/Arduino.h b/cores/nRF5/Arduino.h index 9a0880ef..93cb5a7b 100644 --- a/cores/nRF5/Arduino.h +++ b/cores/nRF5/Arduino.h @@ -106,12 +106,12 @@ uint32_t setLoopStacksize(void); #define bit(b) (1UL << (b)) -#define digitalPinToPort(P) ( &(NRF_GPIO[P]) ) -#define digitalPinToBitMask(P) ( 1 << g_ADigitalPinMap[P] ) +#define digitalPinToPort(P) ( NRF_P0 ) +#define digitalPinToBitMask(P) ( 1 << (P) ) //#define analogInPinToBit(P) ( ) -#define portOutputRegister(port) ( &(NRF_GPIO->OUT) ) -#define portInputRegister(port) ( &(NRF_GPIO->IN) ) -#define portModeRegister(port) ( &(NRF_GPIO->DIRSET) ) +#define portOutputRegister(port) ( &(port->OUT) ) +#define portInputRegister(port) ( &(port->IN) ) +#define portModeRegister(port) ( &(port->DIR) ) #define digitalPinHasPWM(P) ( (P) > 1 ) void rtos_idle_callback(void) ATTR_WEAK; diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/ancs_oled/ancs_oled.ino b/libraries/Bluefruit52Lib/examples/Peripheral/ancs_oled/ancs_oled.ino index d06dacd6..88300403 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/ancs_oled/ancs_oled.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/ancs_oled/ancs_oled.ino @@ -410,7 +410,7 @@ uint32_t readPressedButtons(void) // Take current read and masked with BUTTONs // Note: Bitwise inverted since buttons are active (pressed) LOW - uint32_t debounced = ~(*portInputRegister(0)); + uint32_t debounced = ~(*portInputRegister( digitalPinToPort(0) )); debounced &= (bit(BUTTON_A) | bit(BUTTON_B) | bit(BUTTON_C)); // Copy current state into array From e6c4b0ff7e54f15d51a4c0f0389951f41df9c14d Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 12 Jul 2018 16:51:28 +0700 Subject: [PATCH 09/11] support serialEvent() --- cores/nRF5/HardwareSerial.h | 1 + cores/nRF5/Uart.cpp | 6 ++++++ cores/nRF5/main.cpp | 2 ++ 3 files changed, 9 insertions(+) diff --git a/cores/nRF5/HardwareSerial.h b/cores/nRF5/HardwareSerial.h index 62508e78..bd5eb84a 100644 --- a/cores/nRF5/HardwareSerial.h +++ b/cores/nRF5/HardwareSerial.h @@ -80,5 +80,6 @@ class HardwareSerial : public Stream }; extern void serialEventRun(void) __attribute__((weak)); +extern void serialEvent() __attribute__((weak)); #endif diff --git a/cores/nRF5/Uart.cpp b/cores/nRF5/Uart.cpp index f478f21a..0fa1dcca 100644 --- a/cores/nRF5/Uart.cpp +++ b/cores/nRF5/Uart.cpp @@ -21,6 +21,12 @@ #include "Arduino.h" #include "wiring_private.h" + +void serialEventRun(void) +{ + if (serialEvent && Serial.available() ) serialEvent(); +} + Uart::Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX) { nrfUart = _nrfUart; diff --git a/cores/nRF5/main.cpp b/cores/nRF5/main.cpp index ce7c7ad3..d7d38ebc 100644 --- a/cores/nRF5/main.cpp +++ b/cores/nRF5/main.cpp @@ -61,6 +61,8 @@ static void loop_task(void* arg) { loop(); + if (serialEvent && serialEventRun) serialEventRun(); + #if CFG_DEBUG >= 2 && DBG_MEM_INFO static uint32_t meminfo_ms = 0; if (meminfo_ms + DBG_MEM_INFO_INTERVAL < millis()) From 79db751dc2d76f860b620ca6f363c0d4124c4ef6 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 12 Jul 2018 16:58:09 +0700 Subject: [PATCH 10/11] update changelog for 0.8.4 --- changelog.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/changelog.md b/changelog.md index 528a2471..d8cb4206 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # Adafruit nRF52 Arduino Core Changelog +## 0.8.4 + +- Fix #160: hardware PWM issue that cause Servo freq is 640 hz instead of 50hz +- Fix #134: hardcoded pulse limits with Servo +- Fix upload issue with windows when username has spaces +- Support serialEvent() + ## 0.8.3 - Enhanced indicate API() to wait for confirm or timeout from peer. From c44725b07867a6e6894b8582f22648761e21cb32 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 12 Jul 2018 17:04:14 +0700 Subject: [PATCH 11/11] increase version --- libraries/Bluefruit52Lib/library.properties | 2 +- platform.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Bluefruit52Lib/library.properties b/libraries/Bluefruit52Lib/library.properties index 6532283e..6651a730 100644 --- a/libraries/Bluefruit52Lib/library.properties +++ b/libraries/Bluefruit52Lib/library.properties @@ -1,5 +1,5 @@ name=Adafruit BLE Libraries for Bluefruit52 -version=0.8.3 +version=0.8.4 author=Adafruit maintainer=Adafruit sentence=Arduino library for nRF52-based Adafruit Bluefruit LE modules diff --git a/platform.txt b/platform.txt index 4badb59b..2176d6c0 100644 --- a/platform.txt +++ b/platform.txt @@ -17,7 +17,7 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA name=Adafruit nRF52 Boards -version=0.8.3 +version=0.8.4 # Compile variables # -----------------