Merge branch 'master' into develop

This commit is contained in:
hathach committed 2018-07-12 21:36:36 +07:00
commit 5adabb9c6a
12 files changed
+355 -83

No files matched your search

+7
View File
@@ -2,6 +2,13 @@
## 0.9.0
## 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.
+1 -5
View File
@@ -66,10 +66,6 @@ uint32_t setLoopStacksize(void);
#include "utility/utilities.h"
#include "utility/AdaCallback.h"
#ifdef NRF52840_XXAA
//#include "tusb.h"
#endif
#ifdef __cplusplus
#include "Uart.h"
#endif
@@ -111,7 +107,7 @@ uint32_t setLoopStacksize(void);
#define bit(b) (1UL << (b))
#define digitalPinToPort(P) ( ((P) < 32) ? NRF_P0 : NRF_P1 )
#define digitalPinToBitMask(P) ( 1 << g_ADigitalPinMap[P] )
#define digitalPinToBitMask(P) ( 1 << (P) )
//#define analogInPinToBit(P) ( )
#define portOutputRegister(port) ( &(port->OUT) )
#define portInputRegister(port) ( &(port->IN) )
+1 -1
View File
@@ -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);
+1
View File
@@ -80,5 +80,6 @@ class HardwareSerial : public Stream
};
extern void serialEventRun(void) __attribute__((weak));
extern void serialEvent() __attribute__((weak));
#endif
+6
View File
@@ -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;
+2
View File
@@ -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())
@@ -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 <bluefruit.h>
/* 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; i<len; i++)
{
Serial.printf("%02X-", buffer[i]);
}
Serial.println();
}
void printReport( const ble_gap_evt_adv_report_t* report )
{
Serial.print( " rssi: " );
Serial.println( report->rssi );
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("");
}
@@ -420,7 +420,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(digitalPinToPort(0)));
uint32_t debounced = ~(*portInputRegister( digitalPinToPort(0) ));
debounced &= (bit(BUTTON_A) | bit(BUTTON_B) | bit(BUTTON_C));
// Copy current state into array
@@ -153,6 +153,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 )
{
+6 -4
View File
@@ -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
@@ -111,8 +111,10 @@ 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
HardwarePWM* pwm;
};
#endif
+19 -68
View File
@@ -22,14 +22,8 @@
#include <Servo.h>
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,24 +33,26 @@ Servo::Servo()
this->servoIndex = INVALID_SERVO; // too many servos
}
this->pwm = NULL;
}
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_RESOLUTION<min) min+=DUTY_CYCLE_RESOLUTION;
this->min = min;
this->max = max;
@@ -67,14 +63,14 @@ 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);
this->pwm = HwPWMx[i];
break;
}
}
this->pwm->setMaxValue(MAXVALUE);
this->pwm->setClockDiv(CLOCKDIV);
}
return this->servoIndex;
}
@@ -82,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
}
@@ -93,7 +88,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);
}
@@ -101,67 +96,23 @@ 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; i<HWPWM_MODULE_NUM; i++)
{
if ( HwPWMx[i]->writePin(pin, value) ) break;
}
#endif
if ( this->pwm ) this->pwm->writePin(pin, value/DUTY_CYCLE_RESOLUTION);
}
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()
{
#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; i<HWPWM_MODULE_NUM; i++)
{
if ( HwPWMx[i]->checkPin(pin) )
{
return HwPWMx[i]->readPin(pin);
}
}
if ( this->pwm ) return this->pwm->readPin(pin)*DUTY_CYCLE_RESOLUTION;
return 0;
#endif
}
bool Servo::attached()
+7 -4
View File
@@ -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__
#endif // __SERVO_TIMERS_H__