Skip to content

Commit e08ce0e

Browse files
DineshDK03nashif
authored andcommitted
drivers: sensor: grow_r502a: add upload and download template
1. Add functionality for upload and download fingerprint template data of the R502A fingerprint sensor device. 2. change data type for data_len parameter of transceive_packet function to uint16_t as it may hold value 256 for dowload template data to sensor. Signed-off-by: Dinesh Kumar K <[email protected]>
1 parent 7ae0158 commit e08ce0e

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed

drivers/sensor/grow_r502a/grow_r502a.c

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
LOG_MODULE_REGISTER(GROW_R502A, CONFIG_SENSOR_LOG_LEVEL);
2121

2222
static int transceive_packet(const struct device *dev, union r502a_packet *tx_packet,
23-
union r502a_packet *rx_packet, char const data_len)
23+
union r502a_packet *rx_packet, uint16_t data_len)
2424
{
2525
const struct grow_r502a_config *cfg = dev->config;
2626
struct grow_r502a_data *drv_data = dev->data;
@@ -784,6 +784,135 @@ static int fps_capture(const struct device *dev)
784784
return ret;
785785
}
786786

787+
/**
788+
* @brief upload template from sensor device's RAM buffer 1 to controller.
789+
*
790+
* @result temp->data holds the template to be uploaded to controller.
791+
* temp->len holds the length of the template.
792+
*/
793+
int fps_upload_char_buf(const struct device *dev, struct r502a_template *temp)
794+
{
795+
struct grow_r502a_data *drv_data = dev->data;
796+
union r502a_packet rx_packet = {0};
797+
char const upload_temp_len = 2;
798+
int ret = 0, idx = 0;
799+
800+
if (!temp->data || (temp->len < R502A_TEMPLATE_MAX_SIZE)) {
801+
LOG_ERR("Invalid temp data");
802+
return -EINVAL;
803+
}
804+
805+
union r502a_packet tx_packet = {
806+
.pid = R502A_COMMAND_PACKET,
807+
.data = {R502A_UPCHAR, R502A_CHAR_BUF_1}
808+
};
809+
810+
k_mutex_lock(&drv_data->lock, K_FOREVER);
811+
812+
ret = transceive_packet(dev, &tx_packet, &rx_packet, upload_temp_len);
813+
if (ret != 0) {
814+
goto unlock;
815+
}
816+
817+
if (rx_packet.pid != R502A_ACK_PACKET) {
818+
LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
819+
ret = -EIO;
820+
goto unlock;
821+
}
822+
823+
if (rx_packet.buf[R502A_CC_IDX] == R502A_OK) {
824+
LOG_DBG("Upload to host controller");
825+
} else {
826+
LOG_ERR("Error uploading template 0x%X",
827+
rx_packet.buf[R502A_CC_IDX]);
828+
ret = -EIO;
829+
goto unlock;
830+
}
831+
832+
do {
833+
ret = transceive_packet(dev, NULL, &rx_packet, 0);
834+
if (ret != 0) {
835+
goto unlock;
836+
}
837+
838+
memcpy(&temp->data[idx], &rx_packet.data,
839+
sys_be16_to_cpu(rx_packet.len) - R502A_CHECKSUM_LEN);
840+
idx += sys_be16_to_cpu(rx_packet.len) - R502A_CHECKSUM_LEN;
841+
} while (rx_packet.pid != R502A_END_DATA_PACKET);
842+
843+
temp->len = idx;
844+
845+
unlock:
846+
k_mutex_unlock(&drv_data->lock);
847+
return ret;
848+
}
849+
850+
/**
851+
* @brief download template from controller to sensor device's RAM buffer.
852+
* @Notes char_buf_id - other than value 1 will be considered as value 2
853+
* by R502A sensor.
854+
*/
855+
int fps_download_char_buf(const struct device *dev, uint8_t char_buf_id,
856+
const struct r502a_template *temp)
857+
{
858+
struct grow_r502a_data *drv_data = dev->data;
859+
union r502a_packet rx_packet = {0};
860+
char const down_temp_len = 2;
861+
int ret = 0, i = 0;
862+
863+
if (!temp->data || (temp->len < R502A_TEMPLATE_MAX_SIZE)) {
864+
LOG_ERR("Invalid temp data");
865+
return -EINVAL;
866+
}
867+
868+
union r502a_packet tx_packet = {
869+
.pid = R502A_COMMAND_PACKET,
870+
.data = {R502A_DOWNCHAR, char_buf_id}
871+
};
872+
873+
k_mutex_lock(&drv_data->lock, K_FOREVER);
874+
875+
ret = transceive_packet(dev, &tx_packet, &rx_packet, down_temp_len);
876+
if (ret != 0) {
877+
goto unlock;
878+
}
879+
880+
if (rx_packet.pid != R502A_ACK_PACKET) {
881+
LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
882+
ret = -EIO;
883+
goto unlock;
884+
}
885+
886+
if (rx_packet.buf[R502A_CC_IDX] == R502A_OK) {
887+
LOG_DBG("Download to R502A sensor");
888+
} else {
889+
LOG_ERR("Error downloading template 0x%X",
890+
rx_packet.buf[R502A_CC_IDX]);
891+
ret = -EIO;
892+
goto unlock;
893+
}
894+
895+
while (i < (R502A_TEMPLATE_MAX_SIZE - CONFIG_R502A_DATA_PKT_SIZE)) {
896+
tx_packet.pid = R502A_DATA_PACKET;
897+
memcpy(tx_packet.data, &temp->data[i], CONFIG_R502A_DATA_PKT_SIZE);
898+
899+
ret = transceive_packet(dev, &tx_packet, NULL, CONFIG_R502A_DATA_PKT_SIZE);
900+
if (ret != 0) {
901+
goto unlock;
902+
}
903+
904+
i += CONFIG_R502A_DATA_PKT_SIZE;
905+
}
906+
907+
memcpy(tx_packet.data, &temp->data[i], (R502A_TEMPLATE_MAX_SIZE - i));
908+
tx_packet.pid = R502A_END_DATA_PACKET;
909+
ret = transceive_packet(dev, &tx_packet, NULL, (R502A_TEMPLATE_MAX_SIZE - i));
910+
911+
unlock:
912+
k_mutex_unlock(&drv_data->lock);
913+
return ret;
914+
}
915+
787916
static int fps_init(const struct device *dev)
788917
{
789918
struct grow_r502a_data *drv_data = dev->data;

drivers/sensor/grow_r502a/grow_r502a.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126

127127
#define R502A_CHAR_BUF_SIZE 384 /* Maximum size of characteristic value buffer*/
128128
#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)
129130

130131
#define R502A_MAX_BUF_SIZE (CONFIG_R502A_DATA_PKT_SIZE + R502A_COMMON_ACK_LEN)
131132

include/zephyr/drivers/sensor/grow_r502a.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ struct r502a_sys_param {
5757
uint32_t baud;
5858
} __packed;
5959

60+
struct r502a_template {
61+
uint8_t *data;
62+
size_t len;
63+
};
6064
enum sensor_channel_grow_r502a {
6165
/** Fingerprint template count, ID number for enrolling and searching*/
6266
SENSOR_CHAN_FINGERPRINT = SENSOR_CHAN_PRIV_START,
@@ -127,6 +131,9 @@ enum sensor_attribute_grow_r502a {
127131
};
128132

129133
int r502a_read_sys_param(const struct device *dev, struct r502a_sys_param *val);
134+
int fps_upload_char_buf(const struct device *dev, struct r502a_template *temp);
135+
int fps_download_char_buf(const struct device *dev, uint8_t char_buf_id,
136+
const struct r502a_template *temp);
130137

131138
#ifdef __cplusplus
132139
}

0 commit comments

Comments
 (0)