-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathWire.h
78 lines (62 loc) · 2.08 KB
/
Wire.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Copyright (c) 2022 Dhruva Gole
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <Arduino.h>
#include <api/HardwareI2C.h>
#include <api/Print.h>
#include <zephyr/sys/ring_buffer.h>
typedef void (*voidFuncPtrParamInt)(int);
namespace arduino {
class ZephyrI2C : public HardwareI2C {
public:
ZephyrI2C(const struct device* i2c);
virtual void begin();
virtual void end();
virtual void begin(uint8_t address);
virtual void setClock(uint32_t freq);
virtual void beginTransmission(uint8_t address);
virtual uint8_t endTransmission(bool stopBit);
virtual uint8_t endTransmission(void);
virtual size_t requestFrom(uint8_t address, size_t len, bool stopBit);
virtual size_t requestFrom(uint8_t address, size_t len);
virtual void onReceive(void (*)(int));
virtual void onRequest(void (*)(void));
virtual size_t write(uint8_t data);
virtual size_t write(int data) { return write((uint8_t)data); };
virtual size_t write(const uint8_t *buffer, size_t size);
using Print::write;
virtual int read();
virtual int peek();
virtual void flush();
virtual int available();
private:
int _address;
uint8_t txBuffer[256];
uint32_t usedTxBuffer;
struct rx_ring_buf{
struct ring_buf rb;
uint8_t buffer[256];
};
struct rx_ring_buf rxRingBuffer;
const struct device* i2c_dev;
};
} // namespace arduino
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), i2cs) && (DT_PROP_LEN(DT_PATH(zephyr_user), i2cs) > 1)
#define ARDUINO_WIRE_DEFINED_0 1
#define DECL_EXTERN_WIRE_0(i) extern arduino::ZephyrI2C Wire;
#define DECL_EXTERN_WIRE_N(i) extern arduino::ZephyrI2C Wire##i;
#define DECLARE_EXTERN_WIRE_N(n, p, i) \
COND_CODE_1(ARDUINO_WIRE_DEFINED_##i, (DECL_EXTERN_WIRE_0(i)), (DECL_EXTERN_WIRE_N(i)))
/* Declare Wire, Wire1, Wire2, ... */
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), i2cs, DECLARE_EXTERN_WIRE_N)
#undef DECLARE_EXTERN_WIRE_N
#undef DECL_EXTERN_WIRE_N
#undef DECL_EXTERN_WIRE_0
#undef ARDUINO_WIRE_DEFINED_0
#else
extern arduino::ZephyrI2C Wire;
#endif
typedef arduino::ZephyrI2C TwoWire;