Allocating TX and RX Buffers

The bt. object uses two data buffers — TX buffer for sending data and RX buffer for receiving data. Each buffer has a certain size, i.e, memory capacity. This capacity is allocated upon request from your program. When the device initially boots, no memory is allocated to any buffers.

Memory for buffers is allocated in pages. A page is 256 bytes of memory. Allocating memory for a buffer is a two-step process: First, you have to request for a specific allocation (a number of pages) using bt.txbuffrq and bt.rxbuffrq, and then you must execute the actual allocation using sys.buffalloc, which applies to all buffers of all objects.

Here is a code snippet showing the buffer allocation for the bt. object — you've already seen this in Bringing Up and Enabling the BLE Interface:

** Tibbo Basic **


You may not always get the full amount of memory you have requested. Memory is not an infinite resource, and if you have already requested (and received) allocations for 95% of the memory for your platform, your next request will get up to 5% of memory, even if you requested for 10%.

You can only change the size of buffers if the BLE interface is not enabled (bt.enabled= 0- NO).

There is a small overhead for each buffer. Meaning, not 100% of the memory allocated to a buffer is actually available for use. Each buffer requires 33 bytes for "internal housekeeping". Thus, if you requested (and received) a buffer with 2 pages (256 * 2 = 512), then we can only store 512 - 33 = 479 bytes in this buffer.

The current size of TX and RX buffers can be inspected via bt.txbuffsize and bt.rxbuffsize R/O properties.