### register calculations added by andyjpb on Sat Jul 11 18:52:46 2020

```#if 0
#define MAX(X, Y) ({                      \
__typeof__ (X) _X = (X);  \
__typeof__ (Y) _Y = (Y);  \
(_X > _X) ? _X : _Y;      \
})
#else
#define _SPI_MAX(X, Y) ((X) > (Y) ? (X) : (Y))
#endif

#define SPI_DIVISOR(X) _SPI_MAX((F_CPU / (double)(X)), (F_CPU / (double)(SPI_MAX_HZ)))

/*
* Produces an integer that represents {SPR1, SPR0, ~SPI2X}.
*
* SPR1  SPR0  ~SPI2X  SPI2X  SPI_HZ()  SPI_DIVISOR()
*  0     0      0      1        0             2
*  0     0      1      0        1             4
*  0     1      0      1        2             8
*  0     1      1      0        3            16
*  1     0      0      1        4            32
*  1     0      1      0        5            64
*  1     1      0      1        6            64
*  1     1      1      0        7           128
*
* If the requested speed is slower than we can support then we return the
* value that represents the slowest speed as there is no way to cause a
* compile error.
*
*/
#define SPI_HZ(X) _SPI_HZ(SPI_DIVISOR(X))

#define _SPI_HZ(X)              \
((X) > 128 ? 7 :        \
((X) >  64 ? 7 :       \
((X) >  32 ? 5 :      \
((X) >  16 ? 4 :     \
((X) >   8 ? 3 :    \
((X) >   4 ? 2 :   \
((X) >   2 ? 1 :  \
0)))))))

/*
* Writes out the character C to the device and puts the character returned by
* the device into the memory pointed at by I.
*/
#define SPI_swapchar(I, C) \
do {                                                                            \
/*                                                                      \
* Write straight to the register because we always wait for writes to  \
* finish before returning to the caller.                               \
*/                                                                     \
\
SPDR = C;                                                               \
\
/* Wait for transmission and reception to complete. */                  \
while(!(SPSR & (1<<SPIF)))                                              \
;                                                               \
\
\
/* Return the data that the SPI device sent us. */                      \
*I = SPDR;                                                              \
} while(0)

```