Skip to content

Commit b8beb42

Browse files
committed
complete bufferTXD() for bleuart
- reduce default max connection interval from 40 to 30 ms - finish firmata port
1 parent 2528b45 commit b8beb42

File tree

8 files changed

+493
-32
lines changed

8 files changed

+493
-32
lines changed

cores/nRF5/Arduino.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ uint32_t setLoopStacksize(void);
9797
#define lowByte(w) ((uint8_t) ((w) & 0xff))
9898
#define highByte(w) ((uint8_t) ((w) >> 8))
9999

100-
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
100+
#define bitRead(value, bit) (((value) >> (bit)) & 0x01ul)
101101
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
102102
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
103103
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
@@ -110,7 +110,7 @@ uint32_t setLoopStacksize(void);
110110
#define portOutputRegister(port) ( &(NRF_GPIO->OUT) )
111111
#define portInputRegister(port) ( &(NRF_GPIO->IN) )
112112
#define portModeRegister(port) ( &(NRF_GPIO->DIRSET) )
113-
#define digitalPinHasPWM(P) ( true )
113+
#define digitalPinHasPWM(P) ( (P) > 1 )
114114

115115
void rtos_idle_callback(void) ATTR_WEAK;
116116
/*

cores/nRF5/utility/adafruit_fifo.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ void Adafruit_FIFO::begin(void)
6767
void Adafruit_FIFO::begin(uint16_t depth)
6868
{
6969
_depth = depth;
70-
_buffer = (uint8_t*) malloc(_item_size*_depth);
71-
_mutex = xSemaphoreCreateMutex();
70+
begin();
7271
}
7372

7473
void Adafruit_FIFO::overwriteIfFull(bool enable)

libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmata/LICENSE.txt

Lines changed: 458 additions & 0 deletions
Large diffs are not rendered by default.

libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmata/StandardFirmata.ino

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -302,15 +302,16 @@ void setPinModeCallback(byte pin, int mode)
302302
}
303303
break;
304304
case INPUT:
305-
if (IS_PIN_DIGITAL(pin)) {
306-
pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
307-
#if ARDUINO <= 100
308-
// deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6
309-
digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups
310-
#endif
311-
Firmata.setPinMode(pin, INPUT);
312-
}
313-
break;
305+
// Adafruit: Input without pull up cause pin state changes randomly --> lots of transmission data
306+
// if (IS_PIN_DIGITAL(pin)) {
307+
// pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
308+
//#if ARDUINO <= 100
309+
// // deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6
310+
// digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups
311+
//#endif
312+
// Firmata.setPinMode(pin, INPUT);
313+
// }
314+
// break;
314315
case PIN_MODE_PULLUP:
315316
if (IS_PIN_DIGITAL(pin)) {
316317
pinMode(PIN_TO_DIGITAL(pin), INPUT_PULLUP);
@@ -730,11 +731,8 @@ void systemResetCallback()
730731
// turns off pullup, configures everything
731732
setPinModeCallback(i, PIN_MODE_ANALOG);
732733
} else if (IS_PIN_DIGITAL(i)) {
733-
if ( i != LED_BLUE ) // skip connection LED
734-
{
735-
// sets the output to 0, configures portConfigInputs
736-
setPinModeCallback(i, OUTPUT);
737-
}
734+
// sets the output to 0, configures portConfigInputs
735+
setPinModeCallback(i, OUTPUT);
738736
}
739737

740738
servoPinMap[i] = 255;
@@ -761,12 +759,15 @@ void setup()
761759
{
762760
Serial.begin(115200);
763761
Serial.println("Bluefruit52 Standard Firmata via BLEUART Example");
764-
Serial.println("---------------------------\n");
762+
Serial.println("------------------------------------------------\n");
765763

766764
Bluefruit.begin();
767-
Bluefruit.setTxPower(4); // Maximum TX power = 4 dBm
768765
Bluefruit.setName("Bluefruit52");
769-
766+
Bluefruit.setTxPower(4); // Maximum TX power = 4 dBm
767+
// try to go as fast as possible, could be rejected by some central, increase it if needed
768+
// iOS won't negotitate and will mostly use 30ms
769+
Bluefruit.setConnInterval(9, 16); // min = 9*1.25=11.25 ms, max = 16*1.25=20ms
770+
770771
// Configure and Start BLE Uart Service
771772
// Firmata use several small write(1) --> buffering TXD is required to run smoothly
772773
// Enable buffering TXD

libraries/Bluefruit52Lib/src/BLECharacteristic.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ bool BLECharacteristic::notify(const void* data, uint16_t len)
500500
.p_data = (uint8_t*) u8data,
501501
};
502502

503+
LOG_LV2(CHR, "Notify %d bytes", packet_len);
503504
VERIFY_STATUS( sd_ble_gatts_hvx(Bluefruit.connHandle(), &hvx_params), false );
504505

505506
remaining -= packet_len;

libraries/Bluefruit52Lib/src/bluefruit_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
#define BLE_GAP_CONN_SLAVE_LATENCY 0
5050

5151
#define BLE_GAP_CONN_MIN_INTERVAL_DFLT MS100TO125(20)
52-
#define BLE_GAP_CONN_MAX_INTERVAL_DFLT MS100TO125(40)
52+
#define BLE_GAP_CONN_MAX_INTERVAL_DFLT MS100TO125(30)
5353

5454
// Converts an integer of 1.25ms units to msecs
5555
#define MS100TO125(ms100) (((ms100)*4)/5)

libraries/Bluefruit52Lib/src/services/BLEUart.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ BLEUart::BLEUart(uint16_t fifo_depth)
7171
{
7272
_rx_cb = NULL;
7373

74-
_tx_buffered = false;
74+
_tx_buffered = 0;
7575
_buffered_th = NULL;
7676

7777
_tx_fifo = NULL;
@@ -114,7 +114,7 @@ void bleuart_txd_buffered_hdlr(TimerHandle_t timer)
114114
// skip if null (unlikely)
115115
if ( !svc->_tx_fifo ) return;
116116

117-
// flush data (send notiication)
117+
// flush tx data
118118
svc->flush_tx_buffered();
119119
}
120120

@@ -144,7 +144,7 @@ void BLEUart::setRxCallback( rx_callback_t fp)
144144
* Note: packet is sent right away if it reach MTU bytes
145145
* @param enable true or false
146146
*/
147-
void BLEUart::bufferTXD(bool enable)
147+
void BLEUart::bufferTXD(uint8_t enable)
148148
{
149149
_tx_buffered = enable;
150150

@@ -206,15 +206,18 @@ void BLEUart::_disconnect_cb(void)
206206
{
207207
xTimerDelete(_buffered_th, 0);
208208
_buffered_th = NULL;
209+
210+
if (_tx_fifo) _tx_fifo->clear();
209211
}
210212
}
211213

212214
void BLEUart::_connect_cb (void)
213215
{
214216
if ( _tx_buffered)
215217
{
216-
// create buffered timer with interval = connection interval
217-
_buffered_th = xTimerCreate(NULL, (5*ms2tick(Bluefruit.connInterval())) / 4, true, this, bleuart_txd_buffered_hdlr);
218+
// create TXD timer TODO take connInterval into account
219+
// ((5*ms2tick(Bluefruit.connInterval())) / 4) / 2
220+
_buffered_th = xTimerCreate(NULL, ms2tick(10), true, this, bleuart_txd_buffered_hdlr);
218221
}
219222
}
220223

libraries/Bluefruit52Lib/src/services/BLEUart.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class BLEUart : public BLEService, public Stream
6161
bool notifyEnabled(void);
6262

6363
void setRxCallback( rx_callback_t fp);
64-
void bufferTXD(bool enable);
64+
void bufferTXD(uint8_t enable);
6565

6666
// Stream API
6767
virtual int read ( void );
@@ -83,10 +83,9 @@ class BLEUart : public BLEService, public Stream
8383
Adafruit_FIFO _rx_fifo;
8484
rx_callback_t _rx_cb;
8585

86-
bool _tx_buffered;
87-
TimerHandle_t _buffered_th;
88-
89-
Adafruit_FIFO* _tx_fifo;
86+
uint8_t _tx_buffered;
87+
TimerHandle_t _buffered_th;
88+
Adafruit_FIFO* _tx_fifo;
9089

9190
bool flush_tx_buffered(void);
9291

0 commit comments

Comments
 (0)