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 swire.h
21 *
22 * @brief This is the header file for B91
23 *
24 * @author Driver Group
25 *
26 *******************************************************************************************************/
27
28 #ifndef _SWIRE_H_
29 #define _SWIRE_H_
30 #include "compiler.h"
31 #include "reg_include/register_b91.h"
32
33
34
35 /**
36 * @brief This function resets the SWIRE module.
37 * @return none
38 */
swire_reset(void)39 static inline void swire_reset(void)
40 {
41 reg_rst0 &= (~FLD_RST0_SWIRE);
42 reg_rst0 |= (FLD_RST0_SWIRE);
43 }
44 /**
45 * @brief This function is used to wait for the completion of writing.
46 * @return none
47 */
swire_wait_wr_done(void)48 static inline void swire_wait_wr_done(void)
49 {
50 while (reg_swire_ctl & FLD_SWIRE_WR);
51 }
52 /**
53 * @brief This function is used to write cmd.
54 * @param[in] cmd - cmd value.
55 * @return none
56 */
swire_master_write_cmd(unsigned char cmd)57 static inline void swire_master_write_cmd(unsigned char cmd)
58 {
59 reg_swire_data = cmd;
60 reg_swire_ctl = (FLD_SWIRE_CMD | FLD_SWIRE_WR );
61 swire_wait_wr_done();
62 }
63 /**
64 * @brief This function is used to write data.
65 * @param[in] data - data value.
66 * @return none
67 */
swire_master_write_data(unsigned char data)68 static inline void swire_master_write_data(unsigned char data)
69 {
70 reg_swire_data = data;
71 reg_swire_ctl = FLD_SWIRE_WR ;
72 swire_wait_wr_done();
73 }
74 /**
75 * @brief This function is to disable fifo mode.
76 * The default is this mode. When multi-byte reads and writes, the address will automatically increase by 1.
77 * For example, for write instructions, the address is 1, and the data is 0x11, 0x22,
78 * then 0x11 will be written to address 1, and 0x22 will be written to address 2.
79 * @return none
80 */
swire_fifo_mode_dis(void)81 static inline void swire_fifo_mode_dis(void)
82 {
83 BM_CLR(reg_swire_id, FLD_SWIRE_FIFO_MODE);
84
85 }
86 /**
87 * @brief This function is to enable fifo mode.
88 * When multi-byte read and write, the address will not be automatically incremented by 1,
89 * but the value is written to the same address.
90 * For example, if you want to operate the fifo register on the slave side, you will use this function.
91 * @return none
92 */
swire_fifo_mode_en(void)93 static inline void swire_fifo_mode_en(void)
94 {
95 BM_SET(reg_swire_id, FLD_SWIRE_FIFO_MODE);
96
97 }
98 /**
99 * @brief This function is to send a specific timing to reset the status of the slave device.
100 * When the master device connects the DP pin of the slave device, this function must be called first,
101 * because the DP pin of the slave device is USB function by default. Only after calling this function to send a specific timing,
102 * the slave device will be enabled as the swire function.
103 * When communication fails, you can also try to call this function to resume communication.
104 *
105 * @param[in] dp_through_swire_en - If the master is connected to the DP pin of the slave device, this parameter needs to be set to 1.
106 * @return none.
107 */
108 void swire_sync (unsigned char dp_through_swire_en);
109 /**
110 * @brief This function is to set GPIO_PC0 as the swm function.
111 * @return none.
112 */
113 void swire_set_swm_en(void);
114 /**
115 * @brief This function is to set GPIO_PA7 as the sws function.
116 * @return none.
117 */
118 void swire_set_sws_en(void);
119
120 /**
121 * @brief This function is to set the clock of the swire module,
122 * the clock source of the swire module is HCLK, the clock of the actual swire module: swire_clock = HCLK/div.
123 * swire_clock cannot be greater than HCLK.(div cannot be 0).
124 * Whether the chip as a master or slave, clock source are the following principles:
125 * the clock for sending data is swire_clock, and the clock for detecting received data is HCLK.
126 *
127 * @return none.
128 */
129 void swire_set_clk(unsigned int swire_clock);
130
131 /**
132 * @brief This function is used to set the ID of the slave.
133 * When the host accesses multiple slaves, the device can be distinguished by the ID of the slave.
134 * @param[in] id - slave id.The setting range is 0~0x1f.
135 * @return none.
136 */
137 void swire_set_slave_id(unsigned char id);
138 /**
139 * @brief This function is used by the master device to write data to the slave device.
140 * The swire protocol is to send the address first and then send the data.
141 * @param[in] slave_id - slave id.The setting range is 0~0x1f.
142 * [6:5]: this function tells the slave to operate in byte/half-word/word.
143 * the reason is that some addresses on the slave side can only be operated with 4 bytes.
144 * 10--32bit; 01--16bit ; 00--8bit
145 * [4:0]: ID
146 * @param[in] addr - slave address(little endian format).
147 * @param[in] addr_len - slave address byte length,It is determined by the slave device, for example: B91s is four bytes, B85s is three bytes.
148 * @param[in] data - The data that needs to be written to the slave.
149 * @param[in] data_len - data length.
150 * @return none.
151 */
152
153 void swire_master_write(unsigned char slave_id,unsigned char *addr, unsigned char addr_len,unsigned char *data,unsigned int data_len);
154 /**
155 * @brief This function is used by the master device to read data to the slave device.
156 * The swire protocol is to send the address first and then wait to read the data returned by the slave.
157 * @param[in] slave_id - slave id.The setting range is 0~0x1f.
158 * [6:5]: this function tells the slave to operate in byte/half-word/word.
159 * the reason is that some addresses on the slave side can only be operated with 4 bytes.
160 * 10--32bit; 01--16bit ; 00--8bit
161 * [4:0]: ID
162 * @param[in] addr - slave address(little endian format).
163 * @param[in] addr_len - slave address byte length,It is determined by the slave device, for example: B91s is four bytes, B85s is three bytes.
164 * @param[out] data - this is the data read from the slave.
165 * @param[in] data_len - data length.
166 * @return 0:read timeout 1:read success.
167 */
168 unsigned char swire_master_read (unsigned char slave_id,unsigned char *addr, unsigned char addr_len,unsigned char *data,unsigned int data_len);
169
170 #endif
171