|
NetBurner 3.5.8
PDF Version |
Example Path: examples/PlatformSpecific/SAME70/SerialEmptyCallback/SimpleTiming
Supported Platforms:** MODM7AE70, SBE70LC
This example demonstrates using the TX empty callback to insert a precise inter-frame delay after a serial transmission completes. This is the pattern needed by protocols like Modbus RTU, where a silent interval of at least 3.5 character times must separate frames on the bus.
The NetBurner serial write() functions return when data has been copied into the software FIFO - not when it has been physically transmitted. If you start an inter-frame delay timer immediately after writestring() returns, the timer begins too early (while data is still being shifted out of the UART), and the actual silent period on the wire will be shorter than intended. For timing-sensitive protocols, this can cause framing errors on the receiving end.
The TX empty callback fires from the ISR the instant the UART shift register empties - the precise moment the last bit of the last byte has left the TX pin. By waiting for this callback before starting the delay, the silent period is measured from the correct point in time.
The example uses the same simple callback + semaphore pattern as the Simple example:
Each iteration sends a pair of frames with a guaranteed silent period between them:
For Modbus RTU, the required silent period is 3.5 character times. One character at standard Modbus framing (1 start + 8 data + 1 parity + 1 stop = 11 bits) takes:
| Baud Rate | Character Time | 3.5 Characters |
|---|---|---|
| 9600 | 1.146 ms | 4.01 ms |
| 19200 | 0.573 ms | 2.01 ms |
| 115200 | 0.095 ms | 0.334 ms |
Convert to RTOS ticks by dividing by your tick period and rounding up. At the default 20 ticks/sec (50 ms per tick), sub-tick delays round up to 1 tick. For finer granularity, increase the RTOS tick rate or use a hardware timer.
The callback tells you when the wire is idle - not just when the software buffer accepted your data. This is essential for any protocol that requires precise timing between transmissions.