SparkFun Fingerprint Sensor - FPC2534 Pro  v0.9.9-3-g5d0d172
Library for the SparkFun Fingerprint Sensor - FPC2534 Pro
Loading...
Searching...
No Matches
sfDevFPC2534I2C_esp32.h
Go to the documentation of this file.
1/*
2 *---------------------------------------------------------------------------------
3 *
4 * Copyright (c) 2025, SparkFun Electronics Inc.
5 *
6 * SPDX-License-Identifier: MIT
7 *
8 *---------------------------------------------------------------------------------
9 */
10
11// I2C helper for the ESP32 platform
12
13#pragma once
14
15#include "sfDevFPC2534I2C.h"
16// ESP32 implementation for the FPC2534 I2C communication class - read protocol.
17
18#ifdef ESP32
19#include "driver/i2c.h"
20
21class sfDevFPC2534I2C_Helper : public sfDevFPC2534I2C_IRead
22{
23 public:
24 sfDevFPC2534I2C_Helper() : _i2cBusNumber{0}, _isInitialized{false}, _pendingStop{false}, _timeOutMillis{50}
25 {
26 }
27 void initialize(uint8_t i2cBusNumber)
28 {
29 _i2cBusNumber = i2cBusNumber;
30 _isInitialized = true;
31 _pendingStop = false;
32 }
33 //--------------------------------------------------------------------------------------------
34 // Read the payload data from the device - this is called after readTransferSize() to get
35 // the actual data.
36 //--------------------------------------------------------------------------------------------
37 uint16_t readPayload(size_t len, uint8_t *data)
38 {
39 if (!_isInitialized)
40 return 0;
41
42 // Create a read command and execute it. This is a continued read from the previous
43 // readTransferSize() call, so we don't need to send a start condition or the device address.
44 esp_err_t err = ESP_OK;
45 uint8_t buffer[256] = {0};
46
47 uint16_t theSize = 0;
48
49 i2c_cmd_handle_t handle = i2c_cmd_link_create_static(buffer, sizeof(buffer));
50 if (handle == NULL)
51 return 0;
52
53 err = i2c_master_read(handle, (uint8_t *)data, len, I2C_MASTER_LAST_NACK);
54 if (err == ESP_OK)
55 {
56
57 i2c_master_stop(handle);
58 err = i2c_master_cmd_begin((i2c_port_t)_i2cBusNumber, handle, _timeOutMillis / portTICK_PERIOD_MS);
59
60 if (err == ESP_OK)
61 theSize = len;
62
63 _pendingStop = false;
64
65 i2c_cmd_link_delete_static(handle);
66 }
67
68 return theSize;
69 }
70
71 //--------------------------------------------------------------------------------------------
72 // For the FPC data, the first two bytes are the length of the data to follow. So this method reads in
73 // in the length and returns it. This method is the "start" of a FPC data read operation. It doesn't
74 // stop/end the I2C read operation, that is done in the readPayload() method.
75 //
76 uint16_t readTransferSize(uint8_t device_address)
77 {
78 if (!_isInitialized)
79 return 0;
80
81 esp_err_t err = ESP_OK;
82 uint8_t buffer[256] = {0};
83
84 uint16_t theSize = 0;
85
86 i2c_cmd_handle_t handle = i2c_cmd_link_create_static(buffer, sizeof(buffer));
87 if (handle == NULL)
88 return 0;
89
90 // do we need to stop a previous read operation?
91 if (_pendingStop)
92 {
93 i2c_master_stop(handle);
94 err = i2c_master_cmd_begin((i2c_port_t)_i2cBusNumber, handle, _timeOutMillis / portTICK_PERIOD_MS);
95 i2c_cmd_link_delete_static(handle);
96 handle = i2c_cmd_link_create_static(buffer, sizeof(buffer));
97 if (handle == NULL)
98 return 0;
99 }
100
101 // build up our read command
102 err = i2c_master_start(handle);
103 if (err == ESP_OK)
104 {
105 // address
106 err = i2c_master_write_byte(handle, device_address << 1 | I2C_MASTER_READ, true);
107 if (err == ESP_OK)
108 {
109 // read the size
110 err = i2c_master_read(handle, (uint8_t *)&theSize, sizeof(theSize), I2C_MASTER_ACK);
111 if (err == ESP_OK)
112 {
113 // run the command
114 err = i2c_master_cmd_begin((i2c_port_t)_i2cBusNumber, handle, _timeOutMillis / portTICK_PERIOD_MS);
115
116 if (err != ESP_OK)
117 theSize = 0;
118 else
119 _pendingStop = true;
120 }
121 }
122 }
123 i2c_cmd_link_delete_static(handle);
124
125 return theSize;
126 }
127
128 private:
129 uint8_t _i2cBusNumber;
130 bool _isInitialized;
131 bool _pendingStop;
132 uint16_t _timeOutMillis;
133};
134
135#endif
Definition: sfDevFPC2534I2C.h:28
virtual uint16_t readPayload(size_t len, uint8_t *data)=0
virtual void initialize(uint8_t i2cBusNumber)=0
virtual uint16_t readTransferSize(uint8_t device_address)=0