KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
sci.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 dc/sci.h
4 Copyright (C) 2025 Ruslan Rostovtsev
5
6*/
7
8/** \file dc/sci.h
9 \brief Serial Communication Interface functionality.
10 \ingroup system_sci
11
12 This file provides access to the Dreamcast/Naomi SCI module, which can operate
13 in both UART and SPI modes.
14
15 \author Ruslan Rostovtsev
16*/
17
18#ifndef __DC_SCI_H
19#define __DC_SCI_H
20
21#include <sys/cdefs.h>
22__BEGIN_DECLS
23
24#include <arch/types.h>
25#include <arch/dmac.h>
26#include <stdint.h>
27#include <stdbool.h>
28
29/** \defgroup system_sci SCI
30 \brief Driver for the Serial Communication Interface
31 \ingroup system
32
33 @{
34*/
35
36/*
37 * Baudrate configuration for SH7750 SCI (PCLK = 50 MHz)
38 *
39 * The maximum achievable baudrate depends on internal divider settings.
40 *
41 * Asynchronous mode (UART):
42 * Formula: BRR = (PCLK / (64 × 2^(2n-1) × Baudrate)) - 1, where n = 0–3 (CKS bits)
43 * Maximum with internal clock (n=0, BRR=0): ~1,562,500 bps
44 *
45 * Synchronous mode (SPI):
46 * Formula: BRR = (PCLK / (8 × 2^(2n-1) × Baudrate)) - 1, where n = 0–3 (CKS bits)
47 * Maximum with internal clock (n=0, BRR=0): ~12.5 Mbps
48 */
49
50/** \brief Preset baudrates for UART mode of SCI (internal clock) */
51#define SCI_UART_BAUD_4800 4800 /* Error < 0.2%, very reliable */
52#define SCI_UART_BAUD_9600 9600 /* Error < 0.2%, very reliable */
53#define SCI_UART_BAUD_19200 19200 /* Error < 0.5%, very reliable */
54#define SCI_UART_BAUD_38400 38400 /* Error < 0.8%, very reliable */
55#define SCI_UART_BAUD_57600 57600 /* Error < 0.5%, very reliable */
56#define SCI_UART_BAUD_76800 76800 /* Error ~1.7%, reliable */
57#define SCI_UART_BAUD_115200 115200 /* Error ~3.1%, acceptable */
58#define SCI_UART_BAUD_230400 230400 /* Error ~3.1%, acceptable */
59#define SCI_UART_BAUD_256000 256000 /* Error ~1.7%, reliable */
60#define SCI_UART_BAUD_312500 312500 /* Perfect match, error 0% */
61#define SCI_UART_BAUD_390625 390625 /* Perfect match, error 0% */
62#define SCI_UART_BAUD_460800 460800 /* Error ~13.0%, not recommended */
63#define SCI_UART_BAUD_576000 576000 /* Error < 0.5%, very reliable */
64#define SCI_UART_BAUD_781250 781250 /* Perfect match, error 0% */
65#define SCI_UART_BAUD_921600 921600 /* Error ~15.2%, not recommended */
66#define SCI_UART_BAUD_1562500 1562500 /* Theoretical max, n=0, BRR=0, error 0% */
67
68/** \brief Preset baudrates for SPI mode of SCI (internal clock) */
69#define SCI_SPI_BAUD_250K 250000 /* Perfect match, error 0% */
70#define SCI_SPI_BAUD_312K 312500 /* Perfect match, error 0% */
71#define SCI_SPI_BAUD_500K 500000 /* Perfect match, error 0% */
72#define SCI_SPI_BAUD_625K 625000 /* Perfect match, error 0% */
73#define SCI_SPI_BAUD_781K 781250 /* Perfect match, error 0% */
74#define SCI_SPI_BAUD_1M562K 1562500 /* Perfect match, error 0% */
75#define SCI_SPI_BAUD_3M125K 3125000 /* Perfect match, error 0% */
76#define SCI_SPI_BAUD_6M250K 6250000 /* Perfect match, error 0% */
77#define SCI_SPI_BAUD_12M500K 12500000 /* Theoretical max, n=0, BRR=0, error 0% */
78
79/** \brief Default baudrate for SPI mode */
80#define SCI_SPI_BAUD_INIT SCI_SPI_BAUD_312K /* Initialization baudrate for SPI */
81#define SCI_SPI_BAUD_MAX SCI_SPI_BAUD_12M500K /* Maximum baudrate for SPI */
82
83/** \brief SCI operating mode definitions */
84typedef enum {
85 SCI_MODE_NONE = -1, /**< No mode selected */
86 SCI_MODE_UART = 0, /**< UART (asynchronous) mode */
87 SCI_MODE_SPI = 1 /**< SPI (synchronous) mode */
89
90/** \brief Clock source options */
91typedef enum {
92 SCI_CLK_INT = 0, /**< Use internal clock (default) */
93 SCI_CLK_EXT = 1 /**< Use external clock (SCK pin) */
95
96/** \brief UART configuration options */
97typedef enum {
98 SCI_UART_8N1 = 0x00, /**< 8 data bits, no parity, 1 stop bit */
99 SCI_UART_8N2 = 0x08, /**< 8 data bits, no parity, 2 stop bits */
100 SCI_UART_8E1 = 0x10, /**< 8 data bits, even parity, 1 stop bit */
101 SCI_UART_8O1 = 0x30, /**< 8 data bits, odd parity, 1 stop bit */
102 SCI_UART_7N1 = 0x40, /**< 7 data bits, no parity, 1 stop bit */
103 SCI_UART_7N2 = 0x48, /**< 7 data bits, no parity, 2 stop bits */
104 SCI_UART_7E1 = 0x50, /**< 7 data bits, even parity, 1 stop bit */
105 SCI_UART_7O1 = 0x70 /**< 7 data bits, odd parity, 1 stop bit */
107
108/** \brief SPI CS pin options */
109typedef enum {
110 SCI_SPI_CS_NONE = -1, /**< No CS mode selected */
111 SCI_SPI_CS_GPIO = 0, /**< Use GPIO for SPI CS */
112 SCI_SPI_CS_RTS = 1 /**< Use RTS from SCIF for SPI CS */
114
115/** \brief Error codes for SCI operations */
116typedef enum {
117 SCI_OK = 0, /**< No error */
118 SCI_ERR_NOT_INITIALIZED = -1, /**< Not initialized or incorrect mode */
119 SCI_ERR_PARAM = -2, /**< Invalid parameter */
120 SCI_ERR_TIMEOUT = -3, /**< Operation timeout */
121 SCI_ERR_OVERRUN = -4, /**< Overrun error */
122 SCI_ERR_FRAMING = -5, /**< Framing error */
123 SCI_ERR_PARITY = -6, /**< Parity error */
124 SCI_ERR_DMA = -7 /**< DMA error */
126
127/** \brief Initialize the SCI port with specified parameters.
128 \param baud_rate The baudrate to set.
129 \param mode SCI_MODE_UART for UART mode, SCI_MODE_SPI for SPI mode.
130 \param clock_src Clock source (internal or external).
131 \return SCI_OK on success, error code otherwise.
132*/
133sci_result_t sci_init(uint32_t baud_rate, sci_mode_t mode, sci_clock_t clock_src);
134
135/** \brief Configure UART parameters.
136 \param config UART configuration (data bits, parity, stop bits).
137 \param scsmr1 Pointer to store configuration value if needed.
138*/
139void sci_configure_uart(sci_uart_config_t config, uint8_t *scsmr1);
140
141/** \brief Configure SPI parameters.
142 \param cs Chip select mode for SPI.
143 \param buffer_size Size of DMA buffer to allocate,
144 0 for no DMA support, default is 512 bytes.
145*/
146void sci_configure_spi(sci_spi_cs_mode_t cs, size_t buffer_size);
147
148/** \brief Shutdown the SCI port.
149*/
151
152/** \brief Read a single byte from the UART.
153 \param data Pointer to store the read byte.
154 \return SCI_OK on success, error code otherwise.
155*/
157
158/** \brief Write a single byte to the UART.
159 \param data The byte to write.
160 \return SCI_OK on success, error code otherwise.
161*/
163
164/** \brief Write multiple bytes to the UART.
165 \param data Buffer containing data to write.
166 \param len Number of bytes to write.
167 \return SCI_OK on success, error code otherwise.
168*/
169sci_result_t sci_write_data(uint8_t *data, size_t len);
170
171/** \brief Read multiple bytes from the UART.
172 \param data Buffer to store read data.
173 \param len Number of bytes to read.
174 \return SCI_OK on success, error code otherwise.
175*/
176sci_result_t sci_read_data(uint8_t *data, size_t len);
177
178/** \brief Transfer data using DMA from the UART.
179 \param data Buffer containing data to write.
180 \param len Number of bytes to write.
181 \param callback Optional callback function for completion notification.
182 \param cb_data Data to pass to callback function.
183 \return SCI_OK on success, error code otherwise.
184
185 \note If callback is NULL, the function will wait for the DMA transfer to complete.
186*/
187sci_result_t sci_dma_write_data(const uint8_t *data, size_t len, dma_callback_t callback, void *cb_data);
188
189/** \brief Receive data using DMA from the UART.
190 \param data Buffer to store received data.
191 \param len Number of bytes to read.
192 \param callback Optional callback function for completion notification.
193 \param cb_data Data to pass to callback function.
194 \return SCI_OK on success, error code otherwise.
195
196 \note If callback is NULL, the function will wait for the DMA transfer to complete.
197*/
198sci_result_t sci_dma_read_data(uint8_t *data, size_t len, dma_callback_t callback, void *cb_data);
199
200/** \brief Wait for DMA transfer to complete in both UART and SPI modes.
201 \return SCI_OK on success, error code otherwise.
202*/
204
205/** \brief Set or clear the SPI chip select line.
206 \param enabled true to assert CS (active low), false to deassert.
207*/
208void sci_spi_set_cs(bool enabled);
209
210/** \brief Read and write one byte to the SPI device simultaneously.
211 \param tx_byte The byte to write out to the device.
212 \param rx_byte Pointer to store the received byte.
213 \return SCI_OK on success, error code otherwise.
214*/
215sci_result_t sci_spi_rw_byte(uint8_t tx_byte, uint8_t *rx_byte);
216
217/** \brief Read and write multiple bytes to/from the SPI device.
218 \param tx_data Buffer containing data to write.
219 \param rx_data Buffer to store received data.
220 \param len Number of bytes to transfer.
221 \return SCI_OK on success, error code otherwise.
222*/
223sci_result_t sci_spi_rw_data(const uint8_t *tx_data, uint8_t *rx_data, size_t len);
224
225/** \brief Write one byte to the SPI device.
226 \param tx_byte The byte to write out to the device.
227 \return SCI_OK on success, error code otherwise.
228*/
230
231/** \brief Read one byte from the SPI device.
232 \param rx_byte Pointer to store the received byte.
233 \return SCI_OK on success, error code otherwise.
234*/
236
237/** \brief Write multiple bytes to the SPI device.
238 \param tx_data Buffer containing data to write.
239 \param len Number of bytes to transfer.
240 \return SCI_OK on success, error code otherwise.
241*/
242sci_result_t sci_spi_write_data(const uint8_t *tx_data, size_t len);
243
244/** \brief Read multiple bytes from the SPI device.
245 \param rx_data Buffer to store received data.
246 \param len Number of bytes to transfer.
247 \return SCI_OK on success, error code otherwise.
248*/
249sci_result_t sci_spi_read_data(uint8_t *rx_data, size_t len);
250
251/** \brief Write multiple bytes to the SPI device using DMA.
252 \param tx_data Buffer containing data to write.
253 \param len Number of bytes to transfer.
254 \param callback Optional callback function for completion notification.
255 \param cb_data Data to pass to callback function.
256 \return SCI_OK on success, error code otherwise.
257
258 \note If callback is NULL, the function will wait for the DMA transfer to complete.
259*/
260sci_result_t sci_spi_dma_write_data(const uint8_t *tx_data, size_t len, dma_callback_t callback, void *cb_data);
261
262/** \brief Read multiple bytes from the SPI device using DMA.
263 \param rx_data Buffer to store received data.
264 \param len Number of bytes to transfer.
265 \param callback Optional callback function for completion notification.
266 \param cb_data Data to pass to callback function.
267 \return SCI_OK on success, error code otherwise.
268
269 \note If callback is NULL, the function will wait for the DMA transfer to complete.
270*/
271sci_result_t sci_spi_dma_read_data(uint8_t *rx_data, size_t len, dma_callback_t callback, void *cb_data);
272
273/** @} */
274
275__END_DECLS
276
277#endif /* __DC_SCI_H */
SH4 DMA Controller API.
void(* dma_callback_t)(void *data)
DMA callback type.
Definition dmac.h:42
sci_result_t sci_read_byte(uint8_t *data)
Read a single byte from the UART.
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_dma_wait_complete(void)
Wait for DMA transfer to complete in both UART and SPI modes.
sci_uart_config_t
UART configuration options.
Definition sci.h:97
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_write_byte(uint8_t data)
Write a single byte to the UART.
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_write_data(const uint8_t *tx_data, size_t len)
Write multiple bytes to the SPI device.
sci_result_t sci_spi_write_byte(uint8_t tx_byte)
Write one byte to the SPI device.
void sci_configure_uart(sci_uart_config_t config, uint8_t *scsmr1)
Configure UART parameters.
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_spi_read_byte(uint8_t *rx_byte)
Read one byte from the SPI device.
sci_mode_t
SCI operating mode definitions.
Definition sci.h:84
void sci_shutdown()
Shutdown the SCI port.
sci_spi_cs_mode_t
SPI CS pin options.
Definition sci.h:109
sci_result_t sci_read_data(uint8_t *data, size_t len)
Read multiple bytes from the UART.
sci_result_t sci_write_data(uint8_t *data, size_t len)
Write multiple bytes to the UART.
sci_result_t
Error codes for SCI operations.
Definition sci.h:116
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_spi_read_data(uint8_t *rx_data, size_t len)
Read multiple bytes from the SPI device.
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.
sci_clock_t
Clock source options.
Definition sci.h:91
void sci_configure_spi(sci_spi_cs_mode_t cs, size_t buffer_size)
Configure SPI parameters.
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.
void sci_spi_set_cs(bool enabled)
Set or clear the SPI chip select line.
@ SCI_UART_8O1
8 data bits, odd parity, 1 stop bit
Definition sci.h:101
@ SCI_UART_7N1
7 data bits, no parity, 1 stop bit
Definition sci.h:102
@ SCI_UART_8E1
8 data bits, even parity, 1 stop bit
Definition sci.h:100
@ SCI_UART_8N2
8 data bits, no parity, 2 stop bits
Definition sci.h:99
@ SCI_UART_7O1
7 data bits, odd parity, 1 stop bit
Definition sci.h:105
@ SCI_UART_7E1
7 data bits, even parity, 1 stop bit
Definition sci.h:104
@ SCI_UART_8N1
8 data bits, no parity, 1 stop bit
Definition sci.h:98
@ SCI_UART_7N2
7 data bits, no parity, 2 stop bits
Definition sci.h:103
@ SCI_MODE_NONE
No mode selected.
Definition sci.h:85
@ SCI_MODE_SPI
SPI (synchronous) mode.
Definition sci.h:87
@ SCI_MODE_UART
UART (asynchronous) mode.
Definition sci.h:86
@ SCI_SPI_CS_NONE
No CS mode selected.
Definition sci.h:110
@ SCI_SPI_CS_GPIO
Use GPIO for SPI CS.
Definition sci.h:111
@ SCI_SPI_CS_RTS
Use RTS from SCIF for SPI CS.
Definition sci.h:112
@ SCI_ERR_FRAMING
Framing error.
Definition sci.h:122
@ SCI_ERR_PARITY
Parity error.
Definition sci.h:123
@ SCI_ERR_TIMEOUT
Operation timeout.
Definition sci.h:120
@ SCI_ERR_OVERRUN
Overrun error.
Definition sci.h:121
@ SCI_ERR_DMA
DMA error.
Definition sci.h:124
@ SCI_ERR_PARAM
Invalid parameter.
Definition sci.h:119
@ SCI_OK
No error.
Definition sci.h:117
@ SCI_ERR_NOT_INITIALIZED
Not initialized or incorrect mode.
Definition sci.h:118
@ SCI_CLK_INT
Use internal clock (default)
Definition sci.h:92
@ SCI_CLK_EXT
Use external clock (SCK pin)
Definition sci.h:93
Common integer types.