A minimal driver for the NXP SC16IS740 I2C/SPI to UART bridge.
This is an introduction for me to writing custom drivers for external MCU peripherals so it will be focused on educational value for me and for use in my future projects.
The driver currently supports transmission and reception in bursts of up to 64 bytes, or single bytes and frames can be configured as desired.
- I2C register read/write testing
- Complete UART frame config
- Byte-wise Tx/Rx
- 64-Byte burst Tx/Rx
- Loopback testing
- Basic error reporting
- Continuous blocking Tx/Rx
- Interrupt support
- Software Flow-control
- Hardware Flow-control
NOTE: IS740 is used as an abbreviation for SC16IS740. In all references to functions within documentation, the "IS740_" prefix will be omitted.
- Create read and write functions that use SPI or I2C to communicate with the IC. They must return an IS740error_t error code and take 3 parameters: register address, data buffer, and buffer size.
- Create a
IS740handle_tinstance and assignreadFuncandwriteFuncaccordingly. - Use the config struct within the handle to configure the parity, stop bits, word length, and desired baud rate.
- Use
setBaudRatepassing in the address of the handle and the IC's XTAL1 frequency. If the calculated baud rate has too much error, the divisor can be overriden usingsetClkDiv. - Call
initwith the handle address to configure the frame settings. - Profit.
EXAMPLE USAGE:
IS740handle_t bridge;
bridge.readFunc= I2CReadFromBridge; // IS740error_t I2CReadFromBridge(uint8_t regaddr, uint8_t *buffer, uint8_t size);
bridge.writeFunc = I2CWriteToBridge; // IS740error_t I2CWriteToBridge(uint8_t regaddr, uint8_t *buffer, uint8_t size);
bridge.config.baudRate = 9600;
bridge.config.parity = IS740_PARITY_NONE;
bridge.config.stopBits = IS740_STOPLEN_1;
bridge.config.wordLen = IS740_WORDLEN_8;
// Get peripheral clock frequency (XTAl1 is supplied by PCLK1)
uint32_t pclk1 = HAL_RCC_GetPCLK1Freq();
IS740_setBaudRate(&bridge, pclk1);
IS740_init(&bridge);
uint8_t string[] = "Hello World!\n";
uint8_t rxbuf[64];
IS740_transmit64(&bridge, string, sizeof(string));
// ...
IS740_receive64(&bridge, rxbuf, sizeof(string));