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 #define R502A_NOT_FOUND 0x09 /*fail to find the matching finger*/
65 
66 /*Package Identifier's definition*/
67 #define R502A_COMMAND_PACKET 0x1 /*Command packet*/
68 #define R502A_DATA_PACKET 0x2 /*Data packet, must follow command packet or acknowledge packet*/
69 #define R502A_ACK_PACKET 0x7 /*Acknowledge packet*/
70 #define R502A_END_DATA_PACKET 0x8 /*End of data packet*/
71 
72 /*Instruction code's definition*/
73 #define R502A_GENIMAGE 0x01 /*Collect finger image*/
74 #define R502A_IMAGE2TZ 0x02 /*To generate character file from image*/
75 #define R502A_MATCH 0x03 /*Carry out precise matching of two templates*/
76 #define R502A_SEARCH 0x04 /*Search the finger library*/
77 #define R502A_REGMODEL 0x05 /*To combine character files and generate template*/
78 #define R502A_STORE 0x06 /*To store template*/
79 #define R502A_LOAD 0x07 /*To read/load template*/
80 #define R502A_UPCHAR 0x08 /*To upload template*/
81 #define R502A_DOWNCHAR 0x09 /*To download template*/
82 #define R502A_IMGUPLOAD 0x0A /*To upload image*/
83 #define R502A_DELETE 0x0C /*To delete template*/
84 #define R502A_EMPTYLIBRARY 0x0D /*To empty the library*/
85 #define R502A_SETSYSPARAM 0x0E /*To set system parameter*/
86 #define R502A_READSYSPARAM 0x0F /*To read system parameter*/
87 #define R502A_SETPASSWORD 0x12 /*To set password*/
88 #define R502A_VERIFYPASSWORD 0x13 /*To verify password*/
89 #define R502A_GETRANDOM 0x14 /*To generate a random code*/
90 #define R502A_TEMPLATECOUNT 0x1D /*To read finger template numbers*/
91 #define R502A_READTEMPLATEINDEX 0x1F /*Read fingerprint template index table*/
92 #define R502A_LED_CONFIG 0x35 /*Aura LED Config*/
93 #define R502A_CHECKSENSOR 0x36 /*Check sensor*/
94 #define R502A_SOFTRESET 0x3D /*Soft reset*/
95 #define R502A_HANDSHAKE 0x40 /*Handshake*/
96 #define R502A_BADPACKET 0xFE /* Bad packet was sent*/
97 
98 #define R502A_STARTCODE 0xEF01 /*Fixed value, High byte transferred first*/
99 #define R502A_DEFAULT_PASSWORD 0x00000000
100 #define R502A_DEFAULT_ADDRESS 0xFFFFFFFF
101 #define R502A_DEFAULT_CAPACITY 200
102 #define R502A_HANDSHAKE_BYTE 0x55
103 #define R02A_LIBRARY_START_IDX	0
104 
105 #define R502A_STARTCODE_IDX 0
106 #define R502A_ADDRESS_IDX 2
107 #define R502A_PID_IDX 6 /* Package identifier index*/
108 #define R502A_PKG_LEN_IDX 7
109 #define R502A_CC_IDX 9 /* Confirmation code index*/
110 
111 #define R502A_STARTCODE_LEN 2
112 #define R502A_ADDRESS_LEN 4
113 #define R502A_PKG_LEN	2
114 #define R502A_CHECKSUM_LEN 2 /* Checksum length in uart packages*/
115 #define R502A_HEADER_LEN 9
116 
117 #define R502A_CHAR_BUF_1 1
118 #define R502A_CHAR_BUF_2 2
119 #define R502A_CHAR_BUF_SIZE 384 /* Maximum size of characteristic value buffer*/
120 #define R502A_TEMPLATE_SIZE 768 /* Maximum size of template, twice of CHAR_BUF*/
121 #define R502A_MAX_BUF_SIZE 779 /*sum of checksum, header and template sizes*/
122 #define R502A_BUF_SIZE 64
123 #define R502A_TEMPLATES_PER_PAGE 256
124 #define R502A_TEMP_TABLE_BUF_SIZE 32
125 #define R502A_DELETE_COUNT_OFFSET 1
126 
127 #define R502A_DELAY 200
128 #define R502A_RETRY_DELAY 5
129 
130 #define LED_CTRL_BREATHING 0x01
131 #define LED_CTRL_FLASHING 0x02
132 #define LED_CTRL_ON_ALWAYS 0x03
133 #define LED_CTRL_OFF_ALWAYS 0x04
134 #define LED_CTRL_ON_GRADUALLY 0x05
135 #define LED_CTRL_OFF_GRADUALLY 0x06
136 
137 #define LED_SPEED_HALF 0x50
138 #define LED_SPEED_FULL 0xFF
139 
140 #define LED_COLOR_RED 0x01
141 #define LED_COLOR_BLUE 0x02
142 #define LED_COLOR_PURPLE 0x03
143 
144 struct led_params {
145 	uint8_t ctrl_code;
146 	uint8_t color_idx;
147 	uint8_t speed; /* Speed 0x00-0xff */
148 	uint8_t cycle; /* Number of cycles | 0-infinite, 1-255 */
149 };
150 
151 union r502a_packet {
152 	struct {
153 		uint8_t	start[R502A_STARTCODE_LEN];
154 		uint8_t	addr[R502A_ADDRESS_LEN];
155 		uint8_t	pid;
156 		uint8_t	len[R502A_PKG_LEN];
157 		uint8_t data[R502A_BUF_SIZE];
158 	};
159 
160 	uint8_t buf[R502A_BUF_SIZE];
161 };
162 
163 struct r502a_buf {
164 	uint8_t *data;
165 	size_t len;
166 };
167 
168 struct grow_r502a_data {
169 #ifdef CONFIG_GROW_R502A_TRIGGER
170 	const struct device *gpio_dev;
171 	struct gpio_callback gpio_cb;
172 
173 	sensor_trigger_handler_t th_handler;
174 	const struct sensor_trigger *th_trigger;
175 #if defined(CONFIG_GROW_R502A_TRIGGER_OWN_THREAD)
176 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_GROW_R502A_THREAD_STACK_SIZE);
177 	struct k_sem gpio_sem;
178 	struct k_thread thread;
179 #elif defined(CONFIG_GROW_R502A_TRIGGER_GLOBAL_THREAD)
180 	struct k_work work;
181 #endif
182 #endif /* CONFIG_GROW_R502A_TRIGGER */
183 
184 	struct r502a_buf tx_buf;
185 	struct r502a_buf rx_buf;
186 
187 	struct k_mutex lock;
188 	struct k_sem uart_rx_sem;
189 
190 	uint16_t finger_id;
191 	uint16_t matching_score;
192 	uint16_t template_count;
193 	int8_t free_idx;
194 };
195 
196 struct grow_r502a_config {
197 	const struct device *dev;
198 	struct gpio_dt_spec vin_gpios;
199 	struct gpio_dt_spec act_gpios;
200 	uint32_t comm_addr;
201 #ifdef CONFIG_GROW_R502A_TRIGGER
202 	struct gpio_dt_spec int_gpios;
203 #endif /* CONFIG_GROW_R502A_TRIGGER */
204 };
205 
206 #ifdef CONFIG_GROW_R502A_TRIGGER
207 int grow_r502a_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
208 			   sensor_trigger_handler_t handler);
209 
210 int grow_r502a_init_interrupt(const struct device *dev);
211 #endif /* CONFIG_GROW_R502A_TRIGGER */
212 
213 #endif /*_GROW_R502A_H_*/
214