1 /*
2  * Copyright (c) 2021 Linumiz
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef ZEPHYR_DRIVERS_SENSOR_GROW_R502A_H_
7 #define ZEPHYR_DRIVERS_SENSOR_GROW_R502A_H_
8 
9 /*
10  * @brief confirmation code present in acknowledgment packet
11  *
12  *################################################################################
13  *|Confirmation code	|		Definition |
14  *################################################################################
15  *|0x00			|commad execution complete |
16  *--------------------------------------------------------------------------------
17  *|0x01			|error when receiving data package |
18  *--------------------------------------------------------------------------------
19  *|0x02			|no finger on the sensor |
20  *--------------------------------------------------------------------------------
21  *|0x03			|fail to enroll the finger |
22  *--------------------------------------------------------------------------------
23  *|0x06			|fail to generate character file due to over-disorderly |
24  *|			|fingerprint image |
25  *--------------------------------------------------------------------------------
26  *|0x07			|fail to generate character file due to lackness of |
27  *|			|character point or over-smallness of fingerprint image.|
28  *--------------------------------------------------------------------------------
29  *|0x08			|finger doesn’t match |
30  *--------------------------------------------------------------------------------
31  *|0x09			|fail to find the matching finger |
32  *--------------------------------------------------------------------------------
33  *|0x0A			|fail to combine the character files |
34  *--------------------------------------------------------------------------------
35  *|0x0B			|addressing PageID is beyond the finger library |
36  *--------------------------------------------------------------------------------
37  *|0x0C			|error reading template from library or invalid |
38  *|			|template |
39  *--------------------------------------------------------------------------------
40  *|0x0D			|error when uploading template |
41  *--------------------------------------------------------------------------------
42  *|0x0E			|Module can’t receive the following data packages |
43  *--------------------------------------------------------------------------------
44  *|0x0F			|error when uploading image |
45  *--------------------------------------------------------------------------------
46  *|0x10			|fail to delete the template |
47  *--------------------------------------------------------------------------------
48  *|0x11			|fail to clear finger library |
49  *--------------------------------------------------------------------------------
50  *|0x13			|wrong password! |
51  *--------------------------------------------------------------------------------
52  *|0x15			|fail to generate image for the lackness of valid |
53  *|			|primary image |
54  *--------------------------------------------------------------------------------
55  *|0x18			|error when writing flash |
56  *--------------------------------------------------------------------------------
57  *|0x1A			|invalid register number |
58  *--------------------------------------------------------------------------------
59  *|0x1B			|incorrect configuration of register |
60  *--------------------------------------------------------------------------------
61  */
62 
63 #define R502A_OK 0x00 /*commad execution complete*/
64 
65 /*Package Identifier's definition*/
66 #define R502A_COMMAND_PACKET 0x1 /*Command packet*/
67 #define R502A_DATA_PACKET 0x2 /*Data packet, must follow command packet or acknowledge packet*/
68 #define R502A_ACK_PACKET 0x7 /*Acknowledge packet*/
69 #define R502A_END_DATA_PACKET 0x8 /*End of data packet*/
70 
71 /*Instruction code's definition*/
72 #define R502A_GENIMAGE 0x01 /*Collect finger image*/
73 #define R502A_IMAGE2TZ 0x02 /*To generate character file from image*/
74 #define R502A_MATCH 0x03 /*Carry out precise matching of two templates*/
75 #define R502A_SEARCH 0x04 /*Search the finger library*/
76 #define R502A_REGMODEL 0x05 /*To combine character files and generate template*/
77 #define R502A_STORE 0x06 /*To store template*/
78 #define R502A_LOAD 0x07 /*To read/load template*/
79 #define R502A_UPCHAR 0x08 /*To upload template*/
80 #define R502A_DOWNCHAR 0x09 /*To download template*/
81 #define R502A_IMGUPLOAD 0x0A /*To upload image*/
82 #define R502A_DELETE 0x0C /*To delete template*/
83 #define R502A_EMPTYLIBRARY 0x0D /*To empty the library*/
84 #define R502A_SETSYSPARAM 0x0E /*To set system parameter*/
85 #define R502A_READSYSPARAM 0x0F /*To read system parameter*/
86 #define R502A_SETPASSWORD 0x12 /*To set password*/
87 #define R502A_VERIFYPASSWORD 0x13 /*To verify password*/
88 #define R502A_GETRANDOM 0x14 /*To generate a random code*/
89 #define R502A_TEMPLATECOUNT 0x1D /*To read finger template numbers*/
90 #define R502A_READTEMPLATEINDEX 0x1F /*Read fingerprint template index table*/
91 #define R502A_LED_CONFIG 0x35 /*Aura LED Config*/
92 #define R502A_CHECKSENSOR 0x36 /*Check sensor*/
93 #define R502A_SOFTRESET 0x3D /*Soft reset*/
94 #define R502A_HANDSHAKE 0x40 /*Handshake*/
95 #define R502A_BADPACKET 0xFE /* Bad packet was sent*/
96 
97 #define R502A_NOT_MATCH_CC 0x08 /* templates of two buffers not matching*/
98 #define R502A_NOT_FOUND_CC 0x09 /*fail to find the matching finger*/
99 #define R502A_FINGER_MATCH_NOT_FOUND 0
100 #define R502A_FINGER_MATCH_FOUND 1
101 
102 #define R502A_STARTCODE 0xEF01 /*Fixed value, High byte transferred first*/
103 #define R502A_DEFAULT_PASSWORD 0x00000000
104 #define R502A_DEFAULT_ADDRESS 0xFFFFFFFF
105 #define R502A_DEFAULT_CAPACITY 200
106 #define R502A_HANDSHAKE_BYTE 0x55
107 #define R02A_LIBRARY_START_IDX	0
108 
109 #define R502A_STARTCODE_IDX 0
110 #define R502A_ADDRESS_IDX 2
111 #define R502A_PID_IDX 6 /* Package identifier index*/
112 #define R502A_PKG_LEN_IDX 7
113 #define R502A_CC_IDX 9 /* Confirmation code index*/
114 
115 #define R502A_COMMON_ACK_LEN 12
116 
117 #define R502A_STARTCODE_LEN 2
118 #define R502A_ADDRESS_LEN 4
119 #define R502A_PKG_LEN	2
120 #define R502A_CHECKSUM_LEN 2 /* Checksum length in uart packages*/
121 #define R502A_HEADER_LEN 9
122 
123 #define R502A_CHAR_BUF_1 1
124 #define R502A_CHAR_BUF_2 2
125 #define R502A_CHAR_BUF_TOTAL 2
126 
127 #define R502A_CHAR_BUF_SIZE 384 /* Maximum size of characteristic value buffer*/
128 #define R502A_TEMPLATE_SIZE 768 /* Maximum size of template, twice of CHAR_BUF*/
129 #define R502A_TEMPLATE_MAX_SIZE (R502A_CHAR_BUF_TOTAL * R502A_TEMPLATE_SIZE)
130 
131 #define R502A_MAX_BUF_SIZE  (CONFIG_R502A_DATA_PKT_SIZE + R502A_COMMON_ACK_LEN)
132 
133 #define R502A_TEMPLATES_PER_PAGE 256
134 #define R502A_TEMP_TABLE_BUF_SIZE 32
135 #define R502A_DELETE_COUNT_OFFSET 1
136 
137 #define R502A_DELAY 200
138 #define R502A_RETRY_DELAY 5
139 
140 /*LED glow control code*/
141 enum r502a_led_ctrl_code {
142 	R502A_LED_CTRL_BREATHING = 0x01,
143 	R502A_LED_CTRL_FLASHING,
144 	R502A_LED_CTRL_ON_ALWAYS,
145 	R502A_LED_CTRL_OFF_ALWAYS,
146 	R502A_LED_CTRL_ON_GRADUALLY,
147 	R502A_LED_CTRL_OFF_GRADUALLY,
148 };
149 
150 /* LED glow speed code
151  * if needed, use desired speed between 0-255
152  */
153 enum r502a_led_speed {
154 	R502A_LED_SPEED_MAX = 0x00,
155 	R502A_LED_SPEED_HALF = 0x50,
156 	R502A_LED_SPEED_MIN = 0xFF,
157 };
158 
159 /* LED glowing cycle
160  * if needed, use desired cycle count between 1-255
161  */
162 enum r502a_led_cycle {
163 	R502A_LED_CYCLE_INFINITE = 0x00,
164 	R502A_LED_CYCLE_1,
165 	R502A_LED_CYCLE_2,
166 	R502A_LED_CYCLE_3,
167 	R502A_LED_CYCLE_4,
168 	R502A_LED_CYCLE_5,
169 	R502A_LED_CYCLE_255 = 0xFF,
170 };
171 
172 
173 struct r502a_led_params {
174 	uint8_t ctrl_code;
175 	uint8_t color_idx;
176 	uint8_t speed; /* Speed 0x00-0xff */
177 	uint8_t cycle; /* Number of cycles | 0-infinite, 1-255 */
178 };
179 
180 union r502a_packet {
181 	struct {
182 		uint16_t start;
183 		uint32_t addr;
184 		uint8_t	pid;
185 		uint16_t len;
186 		uint8_t data[CONFIG_R502A_DATA_PKT_SIZE];
187 	} __packed;
188 
189 	uint8_t buf[R502A_MAX_BUF_SIZE];
190 };
191 
192 struct r502a_buf {
193 	uint8_t *data;
194 	size_t len;
195 };
196 
197 struct grow_r502a_data {
198 #ifdef CONFIG_GROW_R502A_TRIGGER
199 	const struct device *gpio_dev;
200 	struct gpio_callback gpio_cb;
201 
202 	sensor_trigger_handler_t th_handler;
203 	const struct sensor_trigger *th_trigger;
204 #if defined(CONFIG_GROW_R502A_TRIGGER_OWN_THREAD)
205 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_GROW_R502A_THREAD_STACK_SIZE);
206 	struct k_sem gpio_sem;
207 	struct k_thread thread;
208 #elif defined(CONFIG_GROW_R502A_TRIGGER_GLOBAL_THREAD)
209 	struct k_work work;
210 #endif
211 #endif /* CONFIG_GROW_R502A_TRIGGER */
212 
213 	struct r502a_buf tx_buf;
214 	struct r502a_buf rx_buf;
215 	uint16_t pkt_len;
216 
217 	struct k_mutex lock;
218 	struct k_sem uart_tx_sem;
219 	struct k_sem uart_rx_sem;
220 
221 	uint16_t template_count;
222 	uint8_t led_color;
223 };
224 
225 struct grow_r502a_config {
226 	const struct device *dev;
227 	struct gpio_dt_spec vin_gpios;
228 	struct gpio_dt_spec act_gpios;
229 	uint32_t comm_addr;
230 #ifdef CONFIG_GROW_R502A_TRIGGER
231 	struct gpio_dt_spec int_gpios;
232 #endif /* CONFIG_GROW_R502A_TRIGGER */
233 };
234 
235 #ifdef CONFIG_GROW_R502A_TRIGGER
236 int grow_r502a_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
237 			   sensor_trigger_handler_t handler);
238 
239 int grow_r502a_init_interrupt(const struct device *dev);
240 #endif /* CONFIG_GROW_R502A_TRIGGER */
241 
242 #endif /*_GROW_R502A_H_*/
243