KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches

Driver for the Serial Communication Interface More...

Files

file  sci.h
 Serial Communication Interface functionality.
 

Macros

#define SCI_UART_BAUD_4800   4800 /* Error < 0.2%, very reliable */
 Preset baudrates for UART mode of SCI (internal clock)
 
#define SCI_UART_BAUD_9600   9600 /* Error < 0.2%, very reliable */
 
#define SCI_UART_BAUD_19200   19200 /* Error < 0.5%, very reliable */
 
#define SCI_UART_BAUD_38400   38400 /* Error < 0.8%, very reliable */
 
#define SCI_UART_BAUD_57600   57600 /* Error < 0.5%, very reliable */
 
#define SCI_UART_BAUD_76800   76800 /* Error ~1.7%, reliable */
 
#define SCI_UART_BAUD_115200   115200 /* Error ~3.1%, acceptable */
 
#define SCI_UART_BAUD_230400   230400 /* Error ~3.1%, acceptable */
 
#define SCI_UART_BAUD_256000   256000 /* Error ~1.7%, reliable */
 
#define SCI_UART_BAUD_312500   312500 /* Perfect match, error 0% */
 
#define SCI_UART_BAUD_390625   390625 /* Perfect match, error 0% */
 
#define SCI_UART_BAUD_460800   460800 /* Error ~13.0%, not recommended */
 
#define SCI_UART_BAUD_576000   576000 /* Error < 0.5%, very reliable */
 
#define SCI_UART_BAUD_781250   781250 /* Perfect match, error 0% */
 
#define SCI_UART_BAUD_921600   921600 /* Error ~15.2%, not recommended */
 
#define SCI_UART_BAUD_1562500   1562500 /* Theoretical max, n=0, BRR=0, error 0% */
 
#define SCI_SPI_BAUD_250K   250000 /* Perfect match, error 0% */
 Preset baudrates for SPI mode of SCI (internal clock)
 
#define SCI_SPI_BAUD_312K   312500 /* Perfect match, error 0% */
 
#define SCI_SPI_BAUD_500K   500000 /* Perfect match, error 0% */
 
#define SCI_SPI_BAUD_625K   625000 /* Perfect match, error 0% */
 
#define SCI_SPI_BAUD_781K   781250 /* Perfect match, error 0% */
 
#define SCI_SPI_BAUD_1M562K   1562500 /* Perfect match, error 0% */
 
#define SCI_SPI_BAUD_3M125K   3125000 /* Perfect match, error 0% */
 
#define SCI_SPI_BAUD_6M250K   6250000 /* Perfect match, error 0% */
 
#define SCI_SPI_BAUD_12M500K   12500000 /* Theoretical max, n=0, BRR=0, error 0% */
 
#define SCI_SPI_BAUD_INIT   SCI_SPI_BAUD_312K /* Initialization baudrate for SPI */
 Default baudrate for SPI mode.
 
#define SCI_SPI_BAUD_MAX   SCI_SPI_BAUD_12M500K /* Maximum baudrate for SPI */
 

Enumerations

enum  sci_mode_t { SCI_MODE_NONE = -1 , SCI_MODE_UART = 0 , SCI_MODE_SPI = 1 }
 SCI operating mode definitions. More...
 
enum  sci_clock_t { SCI_CLK_INT = 0 , SCI_CLK_EXT = 1 }
 Clock source options. More...
 
enum  sci_uart_config_t {
  SCI_UART_8N1 = 0x00 , SCI_UART_8N2 = 0x08 , SCI_UART_8E1 = 0x10 , SCI_UART_8O1 = 0x30 ,
  SCI_UART_7N1 = 0x40 , SCI_UART_7N2 = 0x48 , SCI_UART_7E1 = 0x50 , SCI_UART_7O1 = 0x70
}
 UART configuration options. More...
 
enum  sci_spi_cs_mode_t { SCI_SPI_CS_NONE = -1 , SCI_SPI_CS_GPIO = 0 , SCI_SPI_CS_RTS = 1 }
 SPI CS pin options. More...
 
enum  sci_result_t {
  SCI_OK = 0 , SCI_ERR_NOT_INITIALIZED = -1 , SCI_ERR_PARAM = -2 , SCI_ERR_TIMEOUT = -3 ,
  SCI_ERR_OVERRUN = -4 , SCI_ERR_FRAMING = -5 , SCI_ERR_PARITY = -6 , SCI_ERR_DMA = -7
}
 Error codes for SCI operations. More...
 

Functions

sci_result_t sci_init (uint32_t baud_rate, sci_mode_t mode, sci_clock_t clock_src)
 Initialize the SCI port with specified parameters.
 
void sci_configure_uart (sci_uart_config_t config, uint8_t *scsmr1)
 Configure UART parameters.
 
void sci_configure_spi (sci_spi_cs_mode_t cs, size_t buffer_size)
 Configure SPI parameters.
 
void sci_shutdown ()
 Shutdown the SCI port.
 
sci_result_t sci_read_byte (uint8_t *data)
 Read a single byte from the UART.
 
sci_result_t sci_write_byte (uint8_t data)
 Write a single byte to the UART.
 
sci_result_t sci_write_data (uint8_t *data, size_t len)
 Write multiple bytes to the UART.
 
sci_result_t sci_read_data (uint8_t *data, size_t len)
 Read multiple bytes from the UART.
 
sci_result_t sci_dma_write_data (const uint8_t *data, size_t len, dma_callback_t callback, void *cb_data)
 Transfer data using DMA from the UART.
 
sci_result_t sci_dma_read_data (uint8_t *data, size_t len, dma_callback_t callback, void *cb_data)
 Receive data using DMA from the UART.
 
sci_result_t sci_dma_wait_complete (void)
 Wait for DMA transfer to complete in both UART and SPI modes.
 
void sci_spi_set_cs (bool enabled)
 Set or clear the SPI chip select line.
 
sci_result_t sci_spi_rw_byte (uint8_t tx_byte, uint8_t *rx_byte)
 Read and write one byte to the SPI device simultaneously.
 
sci_result_t sci_spi_rw_data (const uint8_t *tx_data, uint8_t *rx_data, size_t len)
 Read and write multiple bytes to/from the SPI device.
 
sci_result_t sci_spi_write_byte (uint8_t tx_byte)
 Write one byte to the SPI device.
 
sci_result_t sci_spi_read_byte (uint8_t *rx_byte)
 Read one byte from the SPI device.
 
sci_result_t sci_spi_write_data (const uint8_t *tx_data, size_t len)
 Write multiple bytes to the SPI device.
 
sci_result_t sci_spi_read_data (uint8_t *rx_data, size_t len)
 Read multiple bytes from the SPI device.
 
sci_result_t sci_spi_dma_write_data (const uint8_t *tx_data, size_t len, dma_callback_t callback, void *cb_data)
 Write multiple bytes to the SPI device using DMA.
 
sci_result_t sci_spi_dma_read_data (uint8_t *rx_data, size_t len, dma_callback_t callback, void *cb_data)
 Read multiple bytes from the SPI device using DMA.
 

Detailed Description

Driver for the Serial Communication Interface

Macro Definition Documentation

◆ SCI_SPI_BAUD_12M500K

#define SCI_SPI_BAUD_12M500K   12500000 /* Theoretical max, n=0, BRR=0, error 0% */

◆ SCI_SPI_BAUD_1M562K

#define SCI_SPI_BAUD_1M562K   1562500 /* Perfect match, error 0% */

◆ SCI_SPI_BAUD_250K

#define SCI_SPI_BAUD_250K   250000 /* Perfect match, error 0% */

Preset baudrates for SPI mode of SCI (internal clock)

◆ SCI_SPI_BAUD_312K

#define SCI_SPI_BAUD_312K   312500 /* Perfect match, error 0% */

◆ SCI_SPI_BAUD_3M125K

#define SCI_SPI_BAUD_3M125K   3125000 /* Perfect match, error 0% */

◆ SCI_SPI_BAUD_500K

#define SCI_SPI_BAUD_500K   500000 /* Perfect match, error 0% */

◆ SCI_SPI_BAUD_625K

#define SCI_SPI_BAUD_625K   625000 /* Perfect match, error 0% */

◆ SCI_SPI_BAUD_6M250K

#define SCI_SPI_BAUD_6M250K   6250000 /* Perfect match, error 0% */

◆ SCI_SPI_BAUD_781K

#define SCI_SPI_BAUD_781K   781250 /* Perfect match, error 0% */

◆ SCI_SPI_BAUD_INIT

#define SCI_SPI_BAUD_INIT   SCI_SPI_BAUD_312K /* Initialization baudrate for SPI */

Default baudrate for SPI mode.

◆ SCI_SPI_BAUD_MAX

#define SCI_SPI_BAUD_MAX   SCI_SPI_BAUD_12M500K /* Maximum baudrate for SPI */

◆ SCI_UART_BAUD_115200

#define SCI_UART_BAUD_115200   115200 /* Error ~3.1%, acceptable */

◆ SCI_UART_BAUD_1562500

#define SCI_UART_BAUD_1562500   1562500 /* Theoretical max, n=0, BRR=0, error 0% */

◆ SCI_UART_BAUD_19200

#define SCI_UART_BAUD_19200   19200 /* Error < 0.5%, very reliable */

◆ SCI_UART_BAUD_230400

#define SCI_UART_BAUD_230400   230400 /* Error ~3.1%, acceptable */

◆ SCI_UART_BAUD_256000

#define SCI_UART_BAUD_256000   256000 /* Error ~1.7%, reliable */

◆ SCI_UART_BAUD_312500

#define SCI_UART_BAUD_312500   312500 /* Perfect match, error 0% */

◆ SCI_UART_BAUD_38400

#define SCI_UART_BAUD_38400   38400 /* Error < 0.8%, very reliable */

◆ SCI_UART_BAUD_390625

#define SCI_UART_BAUD_390625   390625 /* Perfect match, error 0% */

◆ SCI_UART_BAUD_460800

#define SCI_UART_BAUD_460800   460800 /* Error ~13.0%, not recommended */

◆ SCI_UART_BAUD_4800

#define SCI_UART_BAUD_4800   4800 /* Error < 0.2%, very reliable */

Preset baudrates for UART mode of SCI (internal clock)

◆ SCI_UART_BAUD_57600

#define SCI_UART_BAUD_57600   57600 /* Error < 0.5%, very reliable */

◆ SCI_UART_BAUD_576000

#define SCI_UART_BAUD_576000   576000 /* Error < 0.5%, very reliable */

◆ SCI_UART_BAUD_76800

#define SCI_UART_BAUD_76800   76800 /* Error ~1.7%, reliable */

◆ SCI_UART_BAUD_781250

#define SCI_UART_BAUD_781250   781250 /* Perfect match, error 0% */

◆ SCI_UART_BAUD_921600

#define SCI_UART_BAUD_921600   921600 /* Error ~15.2%, not recommended */

◆ SCI_UART_BAUD_9600

#define SCI_UART_BAUD_9600   9600 /* Error < 0.2%, very reliable */

Enumeration Type Documentation

◆ sci_clock_t

Clock source options.

Enumerator
SCI_CLK_INT 

Use internal clock (default)

SCI_CLK_EXT 

Use external clock (SCK pin)

◆ sci_mode_t

enum sci_mode_t

SCI operating mode definitions.

Enumerator
SCI_MODE_NONE 

No mode selected.

SCI_MODE_UART 

UART (asynchronous) mode.

SCI_MODE_SPI 

SPI (synchronous) mode.

◆ sci_result_t

Error codes for SCI operations.

Enumerator
SCI_OK 

No error.

SCI_ERR_NOT_INITIALIZED 

Not initialized or incorrect mode.

SCI_ERR_PARAM 

Invalid parameter.

SCI_ERR_TIMEOUT 

Operation timeout.

SCI_ERR_OVERRUN 

Overrun error.

SCI_ERR_FRAMING 

Framing error.

SCI_ERR_PARITY 

Parity error.

SCI_ERR_DMA 

DMA error.

◆ sci_spi_cs_mode_t

SPI CS pin options.

Enumerator
SCI_SPI_CS_NONE 

No CS mode selected.

SCI_SPI_CS_GPIO 

Use GPIO for SPI CS.

SCI_SPI_CS_RTS 

Use RTS from SCIF for SPI CS.

◆ sci_uart_config_t

UART configuration options.

Enumerator
SCI_UART_8N1 

8 data bits, no parity, 1 stop bit

SCI_UART_8N2 

8 data bits, no parity, 2 stop bits

SCI_UART_8E1 

8 data bits, even parity, 1 stop bit

SCI_UART_8O1 

8 data bits, odd parity, 1 stop bit

SCI_UART_7N1 

7 data bits, no parity, 1 stop bit

SCI_UART_7N2 

7 data bits, no parity, 2 stop bits

SCI_UART_7E1 

7 data bits, even parity, 1 stop bit

SCI_UART_7O1 

7 data bits, odd parity, 1 stop bit

Function Documentation

◆ sci_configure_spi()

void sci_configure_spi ( sci_spi_cs_mode_t cs,
size_t buffer_size )

Configure SPI parameters.

Parameters
csChip select mode for SPI.
buffer_sizeSize of DMA buffer to allocate, 0 for no DMA support, default is 512 bytes.

◆ sci_configure_uart()

void sci_configure_uart ( sci_uart_config_t config,
uint8_t * scsmr1 )

Configure UART parameters.

Parameters
configUART configuration (data bits, parity, stop bits).
scsmr1Pointer to store configuration value if needed.

◆ sci_dma_read_data()

sci_result_t sci_dma_read_data ( uint8_t * data,
size_t len,
dma_callback_t callback,
void * cb_data )

Receive data using DMA from the UART.

Parameters
dataBuffer to store received data.
lenNumber of bytes to read.
callbackOptional callback function for completion notification.
cb_dataData to pass to callback function.
Returns
SCI_OK on success, error code otherwise.
Note
If callback is NULL, the function will wait for the DMA transfer to complete.

◆ sci_dma_wait_complete()

sci_result_t sci_dma_wait_complete ( void )

Wait for DMA transfer to complete in both UART and SPI modes.

Returns
SCI_OK on success, error code otherwise.

◆ sci_dma_write_data()

sci_result_t sci_dma_write_data ( const uint8_t * data,
size_t len,
dma_callback_t callback,
void * cb_data )

Transfer data using DMA from the UART.

Parameters
dataBuffer containing data to write.
lenNumber of bytes to write.
callbackOptional callback function for completion notification.
cb_dataData to pass to callback function.
Returns
SCI_OK on success, error code otherwise.
Note
If callback is NULL, the function will wait for the DMA transfer to complete.

◆ sci_init()

sci_result_t sci_init ( uint32_t baud_rate,
sci_mode_t mode,
sci_clock_t clock_src )

Initialize the SCI port with specified parameters.

Parameters
baud_rateThe baudrate to set.
modeSCI_MODE_UART for UART mode, SCI_MODE_SPI for SPI mode.
clock_srcClock source (internal or external).
Returns
SCI_OK on success, error code otherwise.

◆ sci_read_byte()

sci_result_t sci_read_byte ( uint8_t * data)

Read a single byte from the UART.

Parameters
dataPointer to store the read byte.
Returns
SCI_OK on success, error code otherwise.

◆ sci_read_data()

sci_result_t sci_read_data ( uint8_t * data,
size_t len )

Read multiple bytes from the UART.

Parameters
dataBuffer to store read data.
lenNumber of bytes to read.
Returns
SCI_OK on success, error code otherwise.

◆ sci_shutdown()

void sci_shutdown ( )

Shutdown the SCI port.

◆ sci_spi_dma_read_data()

sci_result_t sci_spi_dma_read_data ( uint8_t * rx_data,
size_t len,
dma_callback_t callback,
void * cb_data )

Read multiple bytes from the SPI device using DMA.

Parameters
rx_dataBuffer to store received data.
lenNumber of bytes to transfer.
callbackOptional callback function for completion notification.
cb_dataData to pass to callback function.
Returns
SCI_OK on success, error code otherwise.
Note
If callback is NULL, the function will wait for the DMA transfer to complete.

◆ sci_spi_dma_write_data()

sci_result_t sci_spi_dma_write_data ( const uint8_t * tx_data,
size_t len,
dma_callback_t callback,
void * cb_data )

Write multiple bytes to the SPI device using DMA.

Parameters
tx_dataBuffer containing data to write.
lenNumber of bytes to transfer.
callbackOptional callback function for completion notification.
cb_dataData to pass to callback function.
Returns
SCI_OK on success, error code otherwise.
Note
If callback is NULL, the function will wait for the DMA transfer to complete.

◆ sci_spi_read_byte()

sci_result_t sci_spi_read_byte ( uint8_t * rx_byte)

Read one byte from the SPI device.

Parameters
rx_bytePointer to store the received byte.
Returns
SCI_OK on success, error code otherwise.

◆ sci_spi_read_data()

sci_result_t sci_spi_read_data ( uint8_t * rx_data,
size_t len )

Read multiple bytes from the SPI device.

Parameters
rx_dataBuffer to store received data.
lenNumber of bytes to transfer.
Returns
SCI_OK on success, error code otherwise.

◆ sci_spi_rw_byte()

sci_result_t sci_spi_rw_byte ( uint8_t tx_byte,
uint8_t * rx_byte )

Read and write one byte to the SPI device simultaneously.

Parameters
tx_byteThe byte to write out to the device.
rx_bytePointer to store the received byte.
Returns
SCI_OK on success, error code otherwise.

◆ sci_spi_rw_data()

sci_result_t sci_spi_rw_data ( const uint8_t * tx_data,
uint8_t * rx_data,
size_t len )

Read and write multiple bytes to/from the SPI device.

Parameters
tx_dataBuffer containing data to write.
rx_dataBuffer to store received data.
lenNumber of bytes to transfer.
Returns
SCI_OK on success, error code otherwise.

◆ sci_spi_set_cs()

void sci_spi_set_cs ( bool enabled)

Set or clear the SPI chip select line.

Parameters
enabledtrue to assert CS (active low), false to deassert.

◆ sci_spi_write_byte()

sci_result_t sci_spi_write_byte ( uint8_t tx_byte)

Write one byte to the SPI device.

Parameters
tx_byteThe byte to write out to the device.
Returns
SCI_OK on success, error code otherwise.

◆ sci_spi_write_data()

sci_result_t sci_spi_write_data ( const uint8_t * tx_data,
size_t len )

Write multiple bytes to the SPI device.

Parameters
tx_dataBuffer containing data to write.
lenNumber of bytes to transfer.
Returns
SCI_OK on success, error code otherwise.

◆ sci_write_byte()

sci_result_t sci_write_byte ( uint8_t data)

Write a single byte to the UART.

Parameters
dataThe byte to write.
Returns
SCI_OK on success, error code otherwise.

◆ sci_write_data()

sci_result_t sci_write_data ( uint8_t * data,
size_t len )

Write multiple bytes to the UART.

Parameters
dataBuffer containing data to write.
lenNumber of bytes to write.
Returns
SCI_OK on success, error code otherwise.