SparkFun Fingerprint Sensor - FPC2534 Pro  v0.9.9-3-g5d0d172
Library for the SparkFun Fingerprint Sensor - FPC2534 Pro
Loading...
Searching...
No Matches
sfDevFPC2534I2C_rp2.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#pragma once
12
13#include "sfDevFPC2534I2C.h"
14// RP2 implementation for the FPC2534 I2C communication class - read protocol.
15
16#if defined(ARDUINO_ARCH_RP2040)
17#include <hardware/i2c.h>
18
19class sfDevFPC2534I2C_Helper : public sfDevFPC2534I2C_IRead
20{
21 public:
22 sfDevFPC2534I2C_Helper()
23 : _device_address{0}, _i2cPort{nullptr}, _isInitialized{false}, _pendingStop{false}, _timeOutMillis{5000}
24 {
25 }
26 void initialize(uint8_t i2cBusNumber)
27 {
28 // Need to map the provided bus number to the actual i2c port
29
30 if (i2cBusNumber == 0)
31 {
32 // is a port defined?
33#if defined(__WIRE0_DEVICE)
34 _i2cPort = __WIRE0_DEVICE;
35#else
36 _i2cPort = i2c0;
37#endif
38 }
39 else if (i2cBusNumber == 1)
40 {
41 // is a port defined?
42#if defined(__WIRE1_DEVICE)
43 _i2cPort = __WIRE1_DEVICE;
44#else
45 _i2cPort = i2c1;
46#endif
47 }
48 else
49 {
50 // invalid bus number - for now default to i2c0
51 _i2cPort = i2c0;
52 }
53 _isInitialized = true;
54 _pendingStop = false;
55 }
56 //--------------------------------------------------------------------------------------------
57 // Read the payload data from the device - this is called after readTransferSize() to get
58 // the actual data.
59 //--------------------------------------------------------------------------------------------
60 uint16_t readPayload(size_t len, uint8_t *data)
61 {
62
63 if (!_isInitialized)
64 return 0;
65
66 // Since we want to continue the read operation and not restart, set the restart_on_next flag to false.
67 bool restart0 = _i2cPort->restart_on_next;
68
69 _i2cPort->restart_on_next = false;
70 int rc =
71 i2c_read_blocking_until(_i2cPort, _device_address, data, len, false, make_timeout_time_ms(_timeOutMillis));
72
73 // restore the restart flag to its previous state
74 _i2cPort->restart_on_next = restart0;
75 _pendingStop = false;
76
77 // Problem?
78 if (rc == PICO_ERROR_GENERIC || rc == PICO_ERROR_TIMEOUT)
79 len = 0;
80
81 return len;
82 }
83
84 //--------------------------------------------------------------------------------------------
85 // For the FPC data, the first two bytes are the length of the data to follow. So this method reads in
86 // in the length and returns it. This method is the "start" of a FPC data read operation. It doesn't
87 // stop/end the I2C read operation, that is done in the readPayload() method.
88
89 uint16_t readTransferSize(uint8_t device_address)
90 {
91 if (!_isInitialized)
92 return 0;
93
94 _device_address = device_address;
95 uint16_t theSize = 0;
96 int rc = i2c_read_blocking_until(_i2cPort, device_address, (uint8_t *)&theSize, sizeof(theSize), true,
97 make_timeout_time_ms(_timeOutMillis));
98
99 if (rc == PICO_ERROR_GENERIC || rc == PICO_ERROR_TIMEOUT)
100 theSize = 0;
101 else
102 _pendingStop = true;
103
104 return theSize;
105 }
106
107 private:
108 uint8_t _device_address;
109 i2c_inst_t *_i2cPort;
110 bool _isInitialized;
111 bool _pendingStop;
112 uint16_t _timeOutMillis;
113};
114
115#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