
Note 1: the delay between the received packet and start of reply is known as T3.5 (or T35). 
        This is defined as 3.5 char times. Note that the spec says to run N8,no parity, 2 stop
        but N-8-1 is common.
        
          baud      T35 = (1/baud) * 10 * 3.5
          ----      -----
          9.6K      3.6ms
          19.2K     1.8ms
          38.4K     0.9ms
          57.6K     0.6ms
          115.2K    0.3ms
          230.4K    152us
          460.8K    76us
          921.6K    38us
          
          In testing at 115.2K baud, the RS485 adapters auto-tx feature worked reliably with T35 set to 100us
          so I set T35 to 200us. The actual turn-around delay (which includes all processing) with T35=200us
          was 400 +/-20us. The FC3 transaction requesting 8x16b registers looked like:
          <01><03><00><00><00><08><44><0C>___ 400us ___<01><03><10><00><03><05><87><24><31><00><04><00><02><1C><0E><6E><16><00><08><5B><20>
          
          Running at 921600 baud with t35 set to 200us, this transaction still had a nominal 400us delay
          which made the total 750us. Testing for an hour reported no errors. 

          MODBUS docs recommend a minimum T35 of 1.75ms to reduce loading. But this is not required 
          for most modern platforms. I'll allow this to be set by application.

Note 2: To run RS-485, the PC side uses a Dtech USB2 to RS-422/485 converter with auto-txen.
        the ClearCore side uses a chinese TTL to RS-485 converter with auto-txen.
        RS-485 connections are gnd-gnd, T/R+ to A+, T/R- to B-.
        CC TX to converter TX, RX to RX, 3.3V and GND. (rx, tx label misleading)
        Cable is only 10', runs at 921600 w/o errors.
        PC runs between 0 and 0.1% CPU time.
        
        Also runs with cheap USB to RS-232 converter for single point-to-point.
        Cable is:
        
         CC    CHEAP     CHEAP     WIRE
        RJ45   RS485     RS232     COLOR (proto)
        ----   -----     -----     -----
         7      COM       COM       BLU
         2      +5V        x        YEL
         8       RX        TX       RED
         5       TX        RX       BLK
         
Coil           Read-write     1 bit     00001 - 09999  discrete outputs
Discrete input Read-only      1 bit     10001 - 19999  discrete inputs
Input register Read-only      16 bits   30001 - 39999  AIN or other slave data. multi-regs can be float, string
Holding register Read-write   16 bits   40001 - 49999  AOUT ""

Modbus Functions:
    01: Read coils
    02: Read discrete inputs
    03: Read holding registers
    04: Read input registers
    05: Write single coil
    06: Write single register
    15: Write multiple coils
    16: Write multiple registers

union DIOBuf {
     struct __attribute__((packed)) {
          uint16_t    base_io;    // ClearCore bd (only lower 14 bits used)
          uint64_t    ccio;       // CCIO-8 expansion bds.
     } io_bits;
     uint16_t    reg16[sizeof(io_bits) / sizeof(uint16_t)];
};

typedef struct {
     DIOBuf  io_state;       // the I/O state of all connectors used as simple digital I/O
     DIOBuf  dir_out;        // a 1 indicates a cnct used as simple digital output
     int16_t ain[4];         // if ClearCore cnct 9-12 are analog_in, values go here
     int16_t aout;           // if ClearCore cnct 0 is analog_out, value goes here
     int16_t io_special;     // a 1 indicates special (non-simple I/O) for 
                             // ClearCore cnct 0, 4-5, 9-12
     int32_t baudrate;
     // ToDo: add more registers here to facilitate special uses; PWM, motor drive, etc.
} MODBUS_MEMORY;

11/18/2020: version 0.1.1.0 - init all pins to DIN; A9-A12 were AIN

12/20/21:
Set motor HLFB mode "ASG/Position, w/Measured Torque" where HLFB is asserted when Move Done,
PWM'ing measured torque during motion, and deasserted when disabled/shutdown. 
With this ClearCore now has the ability to detect motor shutdowns, so the alert register has expanded:

https://teknic-inc.github.io/ClearCore-library/union_clear_core_1_1_motor_driver_1_1_alert_reg_motor.html
