Class BufferTarget
An ESP32 SPI target that remains armed using native response and receive buffers.
Unlike Target, this class does not wait for a Toit task to prepare each transaction. The peripheral is armed before the constructor returns and is re-armed from its completion callback. Received buffers are rotated instead of copied in that callback, so re-arming does not depend on the transaction size or on Toit task scheduling. This is useful for protocols whose controller cannot observe a separate ready signal. As with any ESP32 SPI target, the controller must still leave CS inactive long enough for the completion interrupt to re-arm the peripheral; a zero-width CS-inactive interval is not supported. SPI does not define a register-address protocol. The response is therefore a plain byte buffer: each controller transaction starts at offset zero. Toit code can update that buffer using indexing, read, and write. Individual bytes are atomic, but a controller transaction concurrent with an update may observe the response one byte at a time. Complete MOSI transactions are copied into a bounded native queue. This is not a streaming API: the controller must release CS at each buffer boundary.
On the classic ESP32 with DMA enabled, only complete four-byte words received on MOSI are queued. A trailing one to three bytes are discarded, matching the ESP-IDF target DMA restriction.
On configurations where ESP-IDF copies the response into hardware registers while arming, a response update can be too late for the next transaction. An update is guaranteed to be visible in the transaction after the next completed transaction, and can be visible sooner.
On the classic ESP32, SPI target interrupts are not placed in IRAM in the Toit firmware configuration. The target therefore cannot re-arm while flash operations disable the instruction cache. Controllers that may communicate during flash erase or write must use a separate ready signal or a newer ESP32 variant.
Constructors
constructor transmit/ByteArray=#[ ] --mosi/int=null --miso/int=null --clock/int --cs/int --mode/int=0 --transmit-lsb-first/bool=false --receive-lsb-first/bool=false --buffer-size/int=TARGET-NON-DMA-MAX-TRANSFER-SIZE --receive-queue-depth/int=4 --fill-byte/int=0xff --dma/bool=true Constructs an autonomous buffer-backed SPI target.
buffer-size is the maximum size of one controller transaction and must not exceed DEFAULT-TARGET-MAX-TRANSFER-SIZE. transmit is copied to the start of the native response buffer; remaining bytes are initialized to fill-byte. receive-queue-depth is the number of complete MOSI transactions retained until receive consumes them. Further transactions are still answered and re-arm the peripheral, but their received data is discarded and counted by dropped-receive-count. Pin, mode, bit-order, DMA, and classic ESP32 restrictions are the same as for Target.
Methods
Stops the target and releases its peripheral, pins, and native buffers.
Number of complete MOSI transactions discarded because the queue was full.
Returns the response byte stored at index.
Stores value in the response buffer and returns it.
See the class documentation for when the update becomes visible on MISO.
Whether this object is equal to the other.
Inheritance
Classes overwrite this operator to get an equality specific to their needs. Equality operators often compare the type and field contents. For example:
class Pin:
number/int
constructor .number:
operator == other:
if other is not Pin: return false
return number == other.number
A class doesn't have to follow the above format, but it must keep the operator in sync with any hash-code method. That is, if a class has a hash-code member, then the equality and hash-code must agree. If two instances are equal (a == b), then their hash codes must also be equal (a.hash-code == b.hash-code).
Returns count response bytes starting at index.
Waits for and returns the next complete MOSI transaction.
If the controller released CS before size bytes, the returned array is correspondingly shorter.
Stringifies this object.
Inheritance
Objects that need a human-friendly string representation should overwrite this method. The default string is based on the internal class-ID.
Writes bytes into the response buffer starting at index.
See the class documentation for when the update becomes visible on MISO.