This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Efficiently sending 'printed' data with Nordic UART

In my code, I have some 'print'-style functions that write data to a buffer, and that buffer then goes to the Nordic UART implementation. This is user code, and I don't know when it's 'done' printing. Let's say something like this happens:

output_char('{');
for (int i=0;i<10;i++) {
   output_str(foo);
   output_char(':');
   output_str(bar);
   output_char(',');
}
output_char('}');

And:

void output_char(char ch) {
  if (!is_ble_busy) nus_string_send(as_string(ch));
  else add_to_buffer(ch);
}
void nus_string_is_sent(char ch) {
  if (!buffer_empty) nus_string_send(buffer_as_string());
}

So my problem is that on the first output_char I call ble_nus_string_send immediately with just one character, and then everything else goes into a buffer while I wait for an acknowledgement that the data has been received.

Ideally I would put everything into a buffer and then issue a ble_nus_string_send only after I'd filled the buffer or a timeout had passed.

Now obviously I could add a timer, but is there an interrupt I could use that fires just before the timeslot where BLE notification data could be written? For instance could I use ble_radio_notification_init or something similar?

Something like:

void output_char(char ch) {
  add_to_buffer(ch);
}
void ble_radio_notification() {
  if (!buffer_empty) nus_string_send(buffer_as_string());
}
Parents
  • Hi Gordon

    Yes, you could use radio notification to sync your operation with the BLE event, advertising or connection. There is a tutorial here on how to set it up. You can configure how much time before the BLE event you get the notification, and you will also get a notification at the end of the BLE event.

    So if you want to send your whole string in a single packet, you could e.g. use the radio notification at the end of the BLE event, which should give you plenty of time to fill up the buffer and call nus_string_send before the following BLE event. Note that calling nus_string_send will put your packet into a softdevice buffer where it is ready to send on the next BLE connection event.

Reply
  • Hi Gordon

    Yes, you could use radio notification to sync your operation with the BLE event, advertising or connection. There is a tutorial here on how to set it up. You can configure how much time before the BLE event you get the notification, and you will also get a notification at the end of the BLE event.

    So if you want to send your whole string in a single packet, you could e.g. use the radio notification at the end of the BLE event, which should give you plenty of time to fill up the buffer and call nus_string_send before the following BLE event. Note that calling nus_string_send will put your packet into a softdevice buffer where it is ready to send on the next BLE connection event.

Children
Related