EVE 1.0
sc16is7x0.h
Go to the documentation of this file.
1 #ifndef SC16IS7XX_H
2 #define SC16IS7XX_H
3 /**********************************************************************/
4 /*
5  * Copyright (c) 2014, Jetro AS
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  * derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONRIBUTORS ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
28  * OF SUCH DAMAGE.
29  *
30  * This file is part of the EVE platform.
31  */
32 
33 /**
34  * \file
35  * @brief Driver for SC16IS740/SC16IS750/SC16IS760 chip UART and GPIO.
36  *
37  * @author SE, Jetro AS
38  */ /******************************************************************/
39 
40 #include <dev/uart.h>
41 #include <dev/gpio-ext.h>
42 #include <dev/spi.h>
43 
44 /** \defgroup sc16 SC16IS7x0 chip driver
45  * \ingroup extdrv
46  */
47 
48 /** \defgroup uart_sc16 SC16IS7x0 UART driver
49  * \ingroup sc16
50  * \ingroup uart
51  * \{
52  */
53 
54 /***********************************************************************
55  * Global defines
56  ***********************************************************************/
57 
58 /**
59  * SC16 register addresses
60  */
61 #define SC16_REG_RHR 0x00 //!< RHR/THR Receive Holding / Transmit Holding
62 #define SC16_REG_THR 0x00 //!< RHR/THR Receive Holding / Transmit Holding
63 #define SC16_REG_IER 0x01 //!< IER Interrupt Enable
64 #define SC16_REG_FCR 0x02 //!< FCR/IIR FIFO Control
65 #define SC16_REG_IIR 0x02 //!< FCR/IIR FIFO Control
66 #define SC16_REG_LCR 0x03 //!< LCR Line Control
67 #define SC16_REG_MCR 0x04 //!< MCR Modem Control
68 #define SC16_REG_LSR 0x05 //!< LSR Line Status
69 #define SC16_REG_MSR 0x06 //!< MSR/TCR Modem Status / Transmission Control (TCR is selected by EFR[4] = 1 and MCR[2] = 1)
70 #define SC16_REG_TCR 0x06 //!< MSR/TCR Modem Status / Transmission Control (TCR is selected by EFR[4] = 1 and MCR[2] = 1)
71 #define SC16_REG_TLR 0x07 //!< TLR Trigger Level (can only be written to when EFR[4] = 1 and MCR[2] = 1)
72 #define SC16_REG_TXLVL 0x08 //!< TXLVL Transmitter FIFO Level
73 #define SC16_REG_RXLVL 0x09 //!< RXLVL Receiver FIFO Level
74 #define SC16_REG_IODIR 0x0A //!< SC16_REG_DIRECTION
75 #define SC16_REG_IOSTATE 0x0B //!< SC16_REG_PORT
76 #define SC16_REG_IOINT 0x0C //!< SC16_REG_INT_MASK
77 #define SC16_REG_IOCTRL 0x0E //!< SC16_REG_CONTROL
78 #define SC16_REG_EFCR 0x0F //!< EFCR Extra Features Control
79 
80 /**
81  * SC16 special register set, accessible only when LCR[7] = 1 and not 0xBF
82  */
83 #define SC16_REG_DLL 0x00 //!< Division register low
84 #define SC16_REG_DLH 0x01 //!< Division register high
85 
86 /**
87  * SC16 enhanced register set, accessible only when LCR = 0xBF.
88  */
89 #define SC16_REG_EFR 0x02 //!< EFR Enhanced Features Register - Set LCR to 0xBF to access it!
90 #define SC16_REG_XON1 0x04 //!< XON1 char
91 #define SC16_REG_XON2 0x05 //!< XON2 char
92 #define SC16_REG_XOFF1 0x06 //!< XOFF1 char
93 #define SC16_REG_XOFF2 0x07 //!< XOFF2 char
94 
95 /**
96  * IER Interrupt Enable Register bits
97  */
98 #define SC16_BIT_IER_CTS (1 << 7) //!< CTS interrupt enable
99 #define SC16_BIT_IER_RTS (1 << 6) //!< RTS interrupt enable
100 #define SC16_BIT_IER_XOFF (1 << 5) //!< Xoff interrupt enable
101 #define SC16_BIT_IER_SLEEP (1 << 4) //!< enable Sleep mode enable
102 #define SC16_BIT_IER_MODEM (1 << 3) //!< Modem Status Interrupt enable
103 #define SC16_BIT_IER_LINE (1 << 2) //!< Receive Line Status interrupt enable
104 #define SC16_BIT_IER_TX (1 << 1) //!< Transmit Holding Register interrupt enable
105 #define SC16_BIT_IER_RX (1 << 0) //!< Receive Holding Register interrupt enable
106 
107 /**
108  * FCR FIFO Control Register bits
109  */
110 #define SC16_SHIFT_FCR_RX_TRIGGER (6) //!< 2 bits defining RX trigger. Sets the trigger level for the RX FIFO
111 #define SC16_SHIFT_FCR_TX_TRIGGER (4) //!< 2 bits defining TX trigger. Sets the trigger level for the TX FIFO.
112 #define SC16_BIT_FCR_RESET_TX (1 << 2) //!< reset TX FIFO
113 #define SC16_BIT_FCR_RESET_RX (1 << 1) //!< reset RX FIFO
114 #define SC16_BIT_FCR_FIFO_ENABLE (1 << 0) //!< FIFO enable
115 
116 /**
117  * IIR register bits
118  */
119 #define SC16_BIT_IIR_NONE 0x01 //!< No interrupts pending
120 #define SC16_BIT_IIR_THR 0x02 //!< THR interrupt
121 #define SC16_BIT_IIR_RHR 0x04 //!< RHR interrupt
122 #define SC16_BIT_IIR_RXE 0x06 //!< Receive Line Status error
123 #define SC16_BIT_IIR_RTO 0x0C //!< Receiver time-out interrupt
124 #define SC16_BIT_IIR_IO 0x30 //!< Input pin change of state
125 
126 
127 /**
128  * LCR Line Control Register bits
129  */
130 #define SC16_BIT_LCR_DIVISOR_LATCH (1 << 7) //!< divisor latch enable
131 #define SC16_BIT_LCR_BREAK (1 << 6) //!< Break control bit. When enabled, the break control bit causes a break condition to be transmitted
132 #define SC16_BIT_LCR_PARITY_FORCED (1 << 5) //!< forced parity (inverted SC16_BIT_LCR_PARITY_EVEN)
133 #define SC16_BIT_LCR_PARITY_EVEN (1 << 4) //!< parity type (odd/even)
134 #define SC16_BIT_LCR_PARITY_ENABLE (1 << 3) //!< parity enable
135 #define SC16_BIT_LCR_STOPS (1 << 2) //!< Number of stop bits
136 #define SC16_SHIFT_LCR_WORD_LENGTH (0) //!< 2 bits for selection of Word length
137 
138 /**
139  * MCR Modem Control Register bits
140  */
141 #define SC16_BIT_MCR_CLOCK_DIVISOR (1 << 7) //!< 0 = clock/1, 1 = clock/4
142 #define SC16_BIT_MCR_IRDA (1 << 6) //!< 0 = normal UART mode, 1 = IrDA mode
143 #define SC16_BIT_MCR_XON_ANY (1 << 5) //!< 1 = enable Xon Any function
144 #define SC16_BIT_MCR_LOOPBACK (1 << 4) //!< 1 = enable local Loopback mode
145 #define SC16_BIT_MCR_TCR_ENABLE (1 << 2) //!< enable the TCR and TLR register.
146 #define SC16_BIT_MCR_FORCE_RTS (1 << 1) //!< 0 = force RTS to inactive (HIGH), 1 = force RTS to active (LOW).
147 #define SC16_BIT_MCR_FORCE_DTR (1 << 0) //!< Force DTR. Only in modem mode!
148 
149 /**
150  * LSR Line Status Register bits
151  */
152 #define SC16_BIT_LSR_FIFO_ERR (1 << 7) //!< FIFO data error
153 #define SC16_BIT_LSR_TX_EMPTY (1 << 6) //!< THR and TSR empty. This bit is the Transmit Empty indicator
154 #define SC16_BIT_LSR_THR_EMPTY (1 << 5) //!< Transmit Holding Register Empty indicator
155 #define SC16_BIT_LSR_BREAK (1 << 4) //!< A break condition occurred
156 #define SC16_BIT_LSR_FRAME_ERR (1 << 3) //!< received data did not have a valid stop bit
157 #define SC16_BIT_LSR_PARITY_ERR (1 << 2) //!< parity error in data being read from RX FIFO
158 #define SC16_BIT_LSR_OVERRUN (1 << 1) //!< overrun error has occurred
159 #define SC16_BIT_LSR_RX_READY (1 << 0) //!< there is at least one character in the RX FIFO
160 
161 /**
162  * TCR Transmission Control Register bits
163  */
164 #define SC16_SHIFT_TCR_FIFO_RESUME (4) //!< 4 bits for RX FIFO level to resume transmission
165 #define SC16_SHIFT_TCR_FIFO_HALT (0) //!< 4 bits for RX FIFO level to halt transmission
166 
167 /**
168  * TLR Trigger Level Register bits
169  */
170 #define SC16_SHIFT_TLR_RX_FIFO (4) //!< 4 bits for RX FIFO levels (4 to 60) to generate interrupt
171 #define SC16_SHIFT_TLR_TX_FIFO (0) //!< 4 bits for TX FIFO levels (4 to 60) to generate interrupt
172 
173 /**
174  * EFCR Extra Features Control Register bits
175  */
176 #define SC16_BIT_EFCR_IRDA_MODE (1 << 7) //!< IrDA mode
177 #define SC16_BIT_EFCR_RTS_INVERT (1 << 5) //!< invert RTS signal in RS-485 mode
178 #define SC16_BIT_EFCR_RTS_AUTO (1 << 4) //!< enable the transmitter to control the RTS pin
179 #define SC16_BIT_EFCR_TX_OFF (1 << 2) //!< Disable transmitter
180 #define SC16_BIT_EFCR_RX_OFF (1 << 1) //!< Disable receiver
181 #define SC16_BIT_EFCR_9BIT (1 << 0) //!< Enable 9-bit or Multidrop mode (RS-485)
182 
183 /**
184  * EFR Enhanced Features Control Register bits
185  */
186 #define SC16_BIT_EFR_CTS_ON (1 << 7) //!< CTS flow control enable
187 #define SC16_BIT_EFR_RTS_ON (1 << 6) //!< RTS flow control enable
188 #define SC16_BIT_EFR_SPECIAL_CHAR (1 << 5) //!< Special character detect (in XOFF2 register)
189 #define SC16_BIT_EFR_ENHANCED_ON (1 << 4) //!< Enhanced functions enable
190 #define SC16_SHIFT_EFR_FLOW_CTRL (0) //!< 4 bits for flow control selection
191 #define SC16_EFR_ACCESS 0xBF //!< This value should be written to LCR register in order to access EFR
192 
193 /**
194  * IOCtrl I/O Control Register bits
195  */
196 #define SC16_BIT_IOCTRL_SRESET (1 << 3) //!< Software reset. A write to bit will reset the device. Once the device is reset this bit is automatically set to ‘0’
197 #define SC16_BIT_IOCTRL_GPIO_ON (1 << 1) //!< This bit programs GPIO[7:4] as I/O pins or modem RI, CD, DTR, DSR pins. 0 = I/O pins, 1 = modem
198 #define SC16_BIT_IOCTRL_IOLATCH (1 << 0) //!< Enable/disable inputs latching (keep value caused interrupt in IOState until it is read)
199 
200 #define SC16_CHANNEL_A 0 //!< Select channel A UART on sc16is750, the only setting on sc16is740
201 #define SC16_CHANNEL_B 1 //!< Select channel B UART on sc16is750
202 
203 #define SC16_FIFO_SIZE 64 //!< Tx/Rx FIFO length
204 
205 /** UART Rx result */
206 typedef enum
207 {
208  SC16RX_OK,
209  SC16RX_TO,
210  SC16RX_ERR,
212 
213 /** UART FIFO on/off settings */
215 {
216  UART_FIFO_OFF = 0,
217  UART_FIFO_ON = 1,
218 };
219 
220 /** UART FIFO trigger settings
221  * 1) RX threshold level to halt transmission in auto flow control mode
222  * 2) RX threshold level to resume transmission in auto flow control mode
223  * 3) Generating interrupt if TX has the settled number of free bytes
224  * 4) Generating interrupt if RX contains the settled number of bytes received
225  */
227 {
228  UART_FIFO_4 = 1,
229  UART_FIFO_8 = 2,
230  UART_FIFO_12 = 3,
231  UART_FIFO_16 = 4,
232  UART_FIFO_20 = 5,
233  UART_FIFO_24 = 6,
234  UART_FIFO_28 = 7,
235  UART_FIFO_32 = 8,
236  UART_FIFO_36 = 9,
237  UART_FIFO_40 = 10,
238  UART_FIFO_44 = 11,
239  UART_FIFO_48 = 12,
240  UART_FIFO_52 = 13,
241  UART_FIFO_56 = 14,
242  UART_FIFO_60 = 15
243 };
244 
245 /** UART 9-bit (multidrop) mode control */
247 {
248  RS485_WORDLENGTH_8 = 0,
249  RS485_WORDLENGTH_9 = 1,
250 };
251 
252 /** SC16 UART Tx/Rx state */
254 {
255  UART_STATUS_READY = 0,
256  UART_STATUS_BUSY,
257  UART_STATUS_BUSY_NOINT,
258 };
259 
260 /** SC16 register read/write/set statemachine operation */
261 typedef enum
262 {
263  SC16_SM_IDLE,
264  SC16_SM_READREG,
265  SC16_SM_WRITEREG,
266  SC16_SM_RX,
267  SC16_SM_TX,
268  SC16_SM_SETREG,
270 
271 /** SC16 register read/write/set statemachine state */
272 typedef enum
273 {
274  SC16_REGMACHINE_IDLE = 0,
275  SC16_REGMACHINE_START,
276  SC16_REGMACHINE_ADDRESS,
277  SC16_REGMACHINE_RX,
278  SC16_REGMACHINE_TX,
279  SC16_REGMACHINE_DONE,
281 
282 /** SC16 Tx handler statemachine state */
283 typedef enum
284 {
285  SC16_TX_START = 0,
286  SC16_TX_INT_ON,
287  SC16_TX_WAIT_FIFO,
288  SC16_TX_FIFO_LVL,
289  SC16_TX_WRITE,
290  SC16_TX_LOOP,
291  SC16_TX_INT_OFF,
292  SC16_TX_READ_LINE_STATUS,
293  SC16_TX_FIFO_LVL_2,
294  SC16_TX_FINISH,
296 
297 /** SC16 Tx handler statemachine state */
298 typedef enum
299 {
300  SC16_RX_START = 0,
301  SC16_RX_INT_ON,
302  SC16_RX_SWITCH_ON,
303  SC16_RX_LOOP_START,
304  SC16_RX_FIFO_LVL,
305  SC16_RX_READ,
306  SC16_RX_LOOP_END,
307  SC16_RX_INT_OFF,
308  SC16_RX_SWITCH_OFF,
309  SC16_RX_FINISH,
311 
312 /** SC16 interrupt handler statemachine state */
313 typedef enum
314 {
315  SC16_INT_START = 0,
316  SC16_INT_READ_IIR,
317  SC16_INT_SERVE,
319 
320 /** Descriptor for scheduled work of SC16 register statemachine */
322 {
323  struct work_t CbWork;
324  uint8_t Reg; // Sc16 register address to read or write !ready! (shifted, with channel)
325  uint8_t Val; // Sc16 register value to write or read to
326  uint8_t Mask; // register bitmap for SET operation
327  uint8_t* DataPtr; // buffer for read / write operation
328  uint16_t DataLen; // length of buffer for read / write operation
329  uint8_t Buffer[2]; // SPI operational buffer
330  sc16_regmashine_oper_t Operation;
331 };
332 
333 /**
334  * UART driver run-time state
335  */
337 {
338  const struct uart_t* Uart; //!< Pointer to the parent structure
339  uint8_t TxStatus; //!< Holds the current operative Tx status
340  uint8_t RxStatus; //!< Holds the current operative Rx status
341  uint8_t OverflowCounter; //!< Holds the position in FIFO of an overflow error
342  sc16_rxresult_t RxResult : 8; //!< Last Rx result (OK, interbyte timeout / line error)
343  enum uart_rx_status_t RxError : 8; //!< Rx result returned to the application
344  sc16_txmachine_state_t TxStateMachine : 8; //!< Tx statemachine state
345  struct sc16_reg_workitem_t TxRegWorkItem; //!< Descriptior of Tx request to register statemachine
346  struct uwork_t TxUwork; //!< uwork used by Tx on waiting for Tx end (empty FIFO)
347  sc16_rxmachine_state_t RxStateMachine : 8; //!< Rx statemachine state
348  struct sc16_reg_workitem_t RxRegWorkItem; //!< Descriptior of Rx request to register statemachine
349  struct sc16_reg_workitem_t RtsRegWorkItem; //!< Descriptior of SetRts request to register statemachine
350  struct sc16_reg_workitem_t BrkRegWorkItem; //!< Descriptior of SendBreak request to register statemachine
351 };
352 
353 /**
354  * sc16_uart_t structure holds SPI instance and SC16 settings to be used
355  */
357 {
358  struct sc16_uart_state_t* State; //!< Address of struct with RAM variables used by the uart driver
359  const struct sc16_chip_t* Chip; //!< Address of SC16 chip descriptor
360  uint32_t FifoRxTrigger : 4; //!< Generating interrupt if RX contains the settled number of bytes received, SC16 Trigger Level Register (TLR)
361  uint32_t FifoTxTrigger : 4; //!< Generating interrupt if TX has the settled number of free bytes, SC16 Trigger Level Register (TLR)
362  uint32_t FifoRxWatermarkLow : 4; //!< RX threshold level to resume transmission in auto flow control mode, SC16 Transmission Control Register (TCR)
363  uint32_t FifoRxWatermarkHigh : 4; //!< RX threshold level to halt transmission in auto flow control mode, SC16 Transmission Control Register (TCR)
364  uint32_t RS485WordLength : 1; //!< Normal 8-bit or RS-485 multidrop 9-bit, SC16 Extra Features Control Register (EFCR)
365  uint32_t RS485RtsAuto : 1; //!< RTS controlled by SC16 in RS485 mode, SC16 Extra Features Control Register (EFCR)
366  uint32_t RS485RtsInverse : 1; //!< Inverse RTS (active 0) in RS485 mode, SC16 Extra Features Control Register (EFCR)
367  uint32_t Channel : 1; //!< Select one of two UARTs on sc16is752 - SC16_CHANNEL_A or SC16_CHANNEL_B; should always be SC16_CHANNEL_A for sc16is750
368 };
369 
370 /**
371  * gpio_ext_sc16_t structure holds SC16 GPIO settings
372  */
374 {
375  const struct sc16_chip_t* Chip; //!< Pointer to SC16 chip settings
376 };
377 
378 struct sc16_chip_state_t
379 {
380  const struct sc16_chip_t* Chip; //!< SC16 chip settings
381  sc16_intmachine_state_t InterruptStateMachine : 8; //!< IRQ handler statemachine state
382  uint8_t Iir[2]; //!< Last read IIR register from Uart0 and Uart1
383  struct sc16_reg_workitem_t IntRegWorkItem; //!< Descriptior of IRQ handler request to register statemachine
384  struct dlist_t PendingRegWorks; //!< List of Register r/w works waiting
385  struct work_t RegWork; //!< Work to be called when current Reg r/w completed
386  sc16_regmashine_state_t RegState : 8; //!< Register r/w statemachine state
387  struct sc16_reg_workitem_t GpioRegWorkItem; //!< Descriptior of GPIO request to register statemachine
388  struct work_t IoWork; //!< work used by GPIO interrupt handler
389  uint8_t InInterrupt : 1; //!< Flag: Inside interrupt handler, IRQ disabled
390 };
391 
392 /**
393  * sc16_chip_t structure holds SC16 chip settings common for both UARTS and GPIO
394  */
396 {
397  struct sc16_chip_state_t* State; //!< SC16 variables
398  const struct spi_t* Spi; //!< Pointer to the SPI interface instance
399  struct spi_cs_t SpiCs; //!< SPI CS settings
400  uint32_t SpiBaudrate; //!< SPI speed
401  uint32_t Xtal; //!< SC16 quartz frequency
402  uint8_t IrqPin; //!< HW IRQ pin number
403  const struct uart_t* Uart[2]; //!< 2 SC16 Uarts
404 };
405 
406 /**
407  * Pointer to UartSc16Func structure uses to initialize the array of UART
408  * virtual functions .Func in struct uart_t UART descriptor.
409  */
410 extern const struct uart_api_t UartSc16Func;
411 
412 /******************************************************************************
413  * @name Functions called from application programs
414  * @{
415  *****************************************************************************/
416  /** @name Internally synchronous functions
417  * @{ */
418 void Sc16Init(const struct sc16_chip_t* Chip);
419 void UartSc16Init(const struct uart_t *Uart);
420 void UartSc16Disable(const struct uart_t *Uart);
421 bool UartSc16IsBusyTx(const struct uart_t *Uart);
422 void UartSc16ResetFifo(const struct uart_t* Uart, bool Tx, bool Rx);
423 void UartSc16SetBaudrate(const struct uart_t *Uart);
424 void UartSc16SetRxTimeouts(const struct uart_t *Uart, uint32_t RxStartTimeoutUs, uint32_t RxStopTimeoutUs);
425  /// @}
426  /** @name Internally asynchronous functions
427  * @{ */
428 void UartSc16CancelTx(const struct uart_t *Uart);
429 void UartSc16StartBasicTx(const struct uart_t *Uart, uint8_t *DataSrc, uint16_t DataSize, uart_tx_callback_t Callback, void *CallbackData);
430 void UartSc16StartBasicRx(const struct uart_t *Uart, uint8_t *DataDest, uint16_t DataSize, uart_rx_callback_t Callback, void *CallbackData);
431 void UartSc16StartPingPongTx(const struct uart_t *Uart, uint8_t *PrimDataSrc, uint16_t PrimDataSize, uint8_t *AltDataSrc, uint16_t AltDataSize, uart_tx_callback_t Callback, void *CallbackData);
432 void UartSc16StartPingPongRx(const struct uart_t *Uart, uint8_t *PrimDataSrc, uint16_t PrimDataSize, uint8_t *AltDataSrc, uint16_t AltDataSize,uart_rx_callback_t Callback, void *CallbackData);
433 void UartSc16RefreshPingPongTx(const struct uart_t *Uart, uint8_t *DataSrc, uint16_t DataSize, bool Stop);
434 void UartSc16RefreshPingPongRx(const struct uart_t *Uart, uint8_t *DataDest, uint16_t DataSize, bool Stop);
435 void UartSc16CancelRx(const struct uart_t *Uart);
436 bool UartSc16IsBusyRx(const struct uart_t *Uart);
437 void UartSc16SendBreak(const struct uart_t *Uart, uart_break_callback_t Callback, void *CallbackData);
438 void UartSc16SetRts(const struct uart_t *Uart, bool Active);
439 void UartSc16InterruptHandler(struct work_t* Work);
440  /// @}
441 /// @}
442 
443 /** \} uart_sc16 */
444 
445 /** \defgroup gpio_sc16 SC16IS7x0 GPIO driver
446  * \ingroup gpio_ext
447  * \ingroup sc16
448  * \{
449  */
450 
451 /**
452  * Pointer to GpioSc16Func structure uses to initialize the array of GPIO
453  * virtual functions .Func in struct ext_gpio_t GPIO descriptor.
454  */
455 extern const struct gpio_ext_api_t GpioSc16Func;
456 
457 /**
458  * @brief Synchronous function writes SC16 register asynchronously and waits
459  * for the end of the operation. Uses internally by inline functions.
460  */
461 void Sc16OutPort(const struct gpio_ext_t *GpioExt, uint8_t Address, uint16_t Data);
462 
463 /**
464  * @brief Synchronous function reads SC16 register asynchronously and waits
465  * for the end of the operation. Uses internally by inline functions.
466  */
467 uint16_t Sc16InPort(const struct gpio_ext_t *GpioExt, uint8_t Address);
468 
469 /**
470  * @brief Initialize GPIO expander
471  *
472  * @param GpioExt Parameters for the IO port expander to be used
473  */
474 static inline void GpioExtInitSc16(const struct gpio_ext_t *GpioExt)
475 {
476  // nothing to do
477 }
478 
479 /**
480  * @brief De-initialize IO expander
481  *
482  * @param GpioExt Parameters for the IO port expander to be used
483  */
484 static inline void GpioExtDisableSc16(const struct gpio_ext_t *GpioExt)
485 {
486  // nothing to do
487 }
488 
489 /**
490  * @brief Read 8-bits data from GPIO port.
491  *
492  * @param GpioExt Parameters for the IO port expander to be used
493  * @return Data read
494  */
495 static inline uint16_t GpioExtInPortSc16(const struct gpio_ext_t *GpioExt)
496 {
497  return Sc16InPort(GpioExt, SC16_REG_IOSTATE);
498 }
499 
500 /**
501  * @brief Write 8-bits data to GPIO port.
502  *
503  * @param GpioExt Parameters for the IO port expander to be used
504  * @param Data Data to be written to IO expander ports
505  */
506 static inline void GpioExtOutPortSc16(const struct gpio_ext_t *GpioExt, uint16_t Data)
507 {
508  Sc16OutPort(GpioExt, SC16_REG_IOSTATE, Data);
509 }
510 
511 /**
512  * @brief Set pins direction to GPIO port.
513  *
514  * @param GpioExt Parameters for the IO port expander to be used
515  * @param Direction New direction to be settled: 0 - input, 1 - output
516  */
517 static inline void GpioExtSetDirectionSc16(const struct gpio_ext_t* GpioExt,
518  uint16_t Direction)
519 {
520  Sc16OutPort(GpioExt, SC16_REG_IODIR, Direction);
521 }
522 
523 /**
524  * @brief Select pullup or pulldown for GPIO port.
525  *
526  * @param GpioExt Parameters for the IO port expander to be used
527  * @param PullSelect 0 - pulldown, 1 - pullup
528  */
529 static inline void GpioExtSetPullSelectSc16(const struct gpio_ext_t* GpioExt,
530  uint16_t PullSelect)
531 {
532  // Not supported by SC16: always pullup
533 }
534 
535 /**
536  * @brief Enable pullup/pulldown to GPIO port.
537  *
538  * @param GpioExt Parameters for the IO port expander to be used
539  * @param PullEnable 0 - disable, 1 - enable
540  */
541 static inline void GpioExtSetPullEnableSc16(const struct gpio_ext_t* GpioExt,
542  uint16_t PullEnable)
543 {
544  // Not supported by SC16: always pullup
545 }
546 
547 /**
548  * @brief Enable/disable interrupts on input GPIO pins.
549  *
550  * @param GpioExt Parameters for the IO port expander to be used
551  * @param Mask Each bit represents a pin #0-7, 1 enables interrupt.
552  */
553 static inline void GpioExtSetInterruptsSc16(const struct gpio_ext_t *GpioExt, uint16_t Mask)
554 {
555  Sc16OutPort(GpioExt, SC16_REG_IOINT, Mask);
556 }
557 
558 /**
559  * @brief Enable/disable latching values on input GPIO pins after interrupt
560  * until they are read.
561  *
562  * @param GpioExt Parameters for the IO port expander to be used
563  * @param Mask Each bit represents a pin #0-7, 1 enables latching.
564  */
565 static inline void GpioExtSetLatchSc16(const struct gpio_ext_t *GpioExt, uint16_t Mask)
566 {
567  uint8_t Ctrl = Sc16InPort(GpioExt, SC16_REG_IOCTRL);
568  if (Mask)
569  Ctrl |= SC16_BIT_IOCTRL_IOLATCH;
570  else
571  Ctrl &= ~SC16_BIT_IOCTRL_IOLATCH;
572  Sc16OutPort(GpioExt, SC16_REG_IOCTRL, Ctrl);
573 }
574 
575 /**
576  * @brief Set drive level for output pins.
577  *
578  * @param GpioExt Parameters for the IO port expander to be used.
579  * @param Values Array represents drive for pins #1-7 in percents.
580  */
581 static inline void GpioExtSetDriveSc16(const struct gpio_ext_t *GpioExt, uint8_t Values[])
582 {
583  // Not supported by SC16
584 }
585 
586 /** @} gpio_sc16 */
587 
588 #endif // SC16IS7XX_H
#define SC16_REG_IOCTRL
SC16_REG_CONTROL.
Definition: sc16is7x0.h:77
uint32_t SpiBaudrate
SPI speed.
Definition: sc16is7x0.h:400
Abstract UART driver header.
void Sc16OutPort(const struct gpio_ext_t *GpioExt, uint8_t Address, uint16_t Data)
Synchronous function writes SC16 register asynchronously and waits for the end of the operation...
sc16_rxmachine_state_t
Definition: sc16is7x0.h:298
sc16_regmashine_oper_t
Definition: sc16is7x0.h:261
void(* uart_rx_callback_t)(const struct uart_t *Uart, void *CallbackData, enum uart_rx_status_t RxStatus)
Definition: uart.h:129
const struct sc16_chip_t * Chip
Address of SC16 chip descriptor.
Definition: sc16is7x0.h:359
static void GpioExtInitSc16(const struct gpio_ext_t *GpioExt)
Initialize GPIO expander.
Definition: sc16is7x0.h:474
uart_fifo_t
Definition: sc16is7x0.h:214
uint8_t OverflowCounter
Holds the position in FIFO of an overflow error.
Definition: sc16is7x0.h:341
Definition: uart.h:222
#define SC16_BIT_IOCTRL_IOLATCH
Enable/disable inputs latching (keep value caused interrupt in IOState until it is read) ...
Definition: sc16is7x0.h:198
uart_fifo_trigger_t
Definition: sc16is7x0.h:226
struct sc16_uart_state_t * State
Address of struct with RAM variables used by the uart driver.
Definition: sc16is7x0.h:358
const struct spi_t * Spi
Pointer to the SPI interface instance.
Definition: sc16is7x0.h:398
const struct uart_t * Uart
Pointer to the parent structure.
Definition: sc16is7x0.h:338
const struct sc16_chip_t * Chip
Pointer to SC16 chip settings.
Definition: sc16is7x0.h:375
Definition: spi.h:113
static void GpioExtSetPullEnableSc16(const struct gpio_ext_t *GpioExt, uint16_t PullEnable)
Enable pullup/pulldown to GPIO port.
Definition: sc16is7x0.h:541
Driver for the MCU&#39;s SPI blocks.
sc16_intmachine_state_t
Definition: sc16is7x0.h:313
static uint16_t GpioExtInPortSc16(const struct gpio_ext_t *GpioExt)
Read 8-bits data from GPIO port.
Definition: sc16is7x0.h:495
#define SC16_REG_IOSTATE
SC16_REG_PORT.
Definition: sc16is7x0.h:75
sc16_regmashine_state_t
Definition: sc16is7x0.h:272
sc16_rxresult_t
Definition: sc16is7x0.h:206
uart_rx_status_t
Definition: uart.h:93
static void GpioExtSetPullSelectSc16(const struct gpio_ext_t *GpioExt, uint16_t PullSelect)
Select pullup or pulldown for GPIO port.
Definition: sc16is7x0.h:529
uint8_t IrqPin
HW IRQ pin number.
Definition: sc16is7x0.h:402
Abstract IO extender driver.
static void GpioExtSetLatchSc16(const struct gpio_ext_t *GpioExt, uint16_t Mask)
Enable/disable latching values on input GPIO pins after interrupt until they are read.
Definition: sc16is7x0.h:565
uint8_t TxStatus
Holds the current operative Tx status.
Definition: sc16is7x0.h:339
void(* uart_tx_callback_t)(const struct uart_t *Uart, void *CallbackData)
Definition: uart.h:137
static void GpioExtSetDriveSc16(const struct gpio_ext_t *GpioExt, uint8_t Values[])
Set drive level for output pins.
Definition: sc16is7x0.h:581
const struct uart_api_t UartSc16Func
rs485_wordlength_t
Definition: sc16is7x0.h:246
static void GpioExtSetInterruptsSc16(const struct gpio_ext_t *GpioExt, uint16_t Mask)
Enable/disable interrupts on input GPIO pins.
Definition: sc16is7x0.h:553
static void GpioExtOutPortSc16(const struct gpio_ext_t *GpioExt, uint16_t Data)
Write 8-bits data to GPIO port.
Definition: sc16is7x0.h:506
Definition: dlist.h:66
uint32_t Xtal
SC16 quartz frequency.
Definition: sc16is7x0.h:401
sc16_txmachine_state_t
Definition: sc16is7x0.h:283
uint8_t RxStatus
Holds the current operative Rx status.
Definition: sc16is7x0.h:340
#define SC16_REG_IODIR
SC16_REG_DIRECTION.
Definition: sc16is7x0.h:74
static void GpioExtSetDirectionSc16(const struct gpio_ext_t *GpioExt, uint16_t Direction)
Set pins direction to GPIO port.
Definition: sc16is7x0.h:517
Definition: uwork.h:142
uint16_t Sc16InPort(const struct gpio_ext_t *GpioExt, uint8_t Address)
Synchronous function reads SC16 register asynchronously and waits for the end of the operation...
const struct gpio_ext_api_t GpioSc16Func
static void GpioExtDisableSc16(const struct gpio_ext_t *GpioExt)
De-initialize IO expander.
Definition: sc16is7x0.h:484
Definition: spi.h:166
sc16_uart_status_t
Definition: sc16is7x0.h:253
Definition: work.h:140
void(* uart_break_callback_t)(const struct uart_t *Uart, void *CallbackData)
Definition: uart.h:145
#define SC16_REG_IOINT
SC16_REG_INT_MASK.
Definition: sc16is7x0.h:76
struct sc16_chip_state_t * State
SC16 variables.
Definition: sc16is7x0.h:397