1 /******************************************************************************
2  * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************/
18 
19 /********************************************************************************************************
20  * @file	s7816.h
21  *
22  * @brief	This is the header file for B91
23  *
24  * @author	Driver Group
25  *
26  *******************************************************************************************************/
27 #ifndef S7816_H_
28 #define S7816_H_
29 
30 #include "gpio.h"
31 #include "uart.h"
32 #include "stimer.h"
33 
34 #define s7816_en(uart_num)                uart_rtx_en(uart_num)
35 #define s7816_set_rtx_pin(s7816_trx_pin)  uart_set_rtx_pin(s7816_trx_pin)
36 
37 /**
38  *  @brief  Define 7816 TRX pin.
39  */
40 typedef enum{
41 	S7816_UART0_RTX_A4=GPIO_PA4,
42 	S7816_UART0_RTX_B3=GPIO_PB3,
43 	S7816_UART0_RTX_D3=GPIO_PD3,
44 
45 	S7816_UART1_RTX_C7=GPIO_PC7,
46 	S7816_UART1_RTX_D7=GPIO_PD7,
47 	S7816_UART1_RTX_E2=GPIO_PE2,
48 }s7816_rtx_pin_e;
49 
50 /**
51  *  @brief  Define 7816 clock.
52  */
53 typedef enum{
54 	S7816_4MHZ=4,
55 	S7816_6MHZ=6,
56 	S7816_12MHZ=12,
57 }s7816_clock_e;
58 
59 /**
60  *  @brief  Define 7816 clk pin.
61  */
62 typedef enum{
63 	S7817_CLK_PA0=GPIO_PA0,
64 }s7816_clk_pin_e;
65 
66 
67 /**
68  * @brief      	This function is used to set the s7816 clock.
69  * @param[in]  	div	- set the divider of clock of 7816 module.
70  * @return     	none.
71  * @note        the clk-source of s7816 is 24M-pad,the clk of clk-pin can be divided as follow.
72  * 				div:        0x60-4Mhz     0x40-6Mhz   0x20-12Mhz
73  * 				baudrate:   0x60-10752    0x40-16194  0x20-32388
74  */
75 extern void s7816_set_clk(unsigned char div);
76 
77 
78 /**
79  * @brief      	This function is used to initialize the s7816 module.
80  * @param[in]  	uart_num     - UART0 or UART1.
81  * @param[in]  	clock        - the clock of s7816.
82  * @param[in]  	f            - the clock frequency conversion factor of s7816.
83  * @param[in]  	d            - the bitrate regulator of s7816.
84  * @return     	none.
85  */
86 extern void s7816_init(uart_num_e uart_num,s7816_clock_e clock,int f,int d);
87 
88 /**
89  * @brief      	This function is used to active the IC card,set the trx pin and coldreset.
90  * @param[in]  	none.
91  * @return     	none.
92  * @note        extra time is needed for initial-atr after the function.
93  */
94 extern void s7816_coldreset();
95 
96 /**
97  * @brief      	This function is used to set all the pin of s7816 module.
98  * @param[in]  	rst_pin     - the rst pin of s7816.
99  * @param[in]  	vcc_pin     - the vcc pin of s7816.
100  * @param[in]  	clk_pin     - the clk pin of s7816.
101  * @param[in]  	trx_pin     - the trx pin of s7816.
102  * @return     	none.
103  */
104 extern void s7816_set_pin(gpio_pin_e rst_pin,gpio_pin_e vcc_pin,s7816_clk_pin_e clk_pin,s7816_rtx_pin_e trx_pin);
105 
106 /**
107  * @brief      	This function is used to release the trigger
108  * @param[in]  	none.
109  * @return     	none.
110  */
111 extern void s7816_release_trig();
112 
113 /**
114  * @brief      	This function is used to set the RST pin of s7816.
115  * @param[in]  	pin_7816_rst - the RST pin of s7816.
116  * @return     	none.
117  */
118 extern void s7816_set_rst_pin(gpio_pin_e pin_7816_rst);
119 
120 /**
121  * @brief      	This function is used to set the VCC pin of s7816.
122  * @param[in]   pin_7816_vcc - the VCC pin of s7816.
123  * @return     	none.
124  */
125 extern void s7816_set_vcc_pin(gpio_pin_e pin_7816_vcc);
126 
127 /**
128  * @brief      	This function is used to warmreset.
129  * @param[in]  	none.
130  * @return     	none.
131  * @note        the warmreset is required after the IC-CARD active,extra time is needed for initial-atr after the function.
132  */
133 extern void s7816_warmreset();
134 
135 /**
136  * @brief      	This function is used to set the rst-wait time of the s7816 module.
137  * @param[in]  	rst_time_us - set the s7816_rst_time.
138  * @return     	none.
139  */
140 extern void s7816_set_time(int rst_time_us);
141 
142 /**
143  * @brief      	This function is used to warmreset.
144  * @param[in]  	uart_num - UART0 or UART1.
145  * @param[in]   tx_data  - the data need to send.
146  * return       none.
147  */
148 extern void s7816_send_byte(uart_num_e uart_num, unsigned char tx_data);
149 
150 #endif /* S7816_H_ */
151 
152