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