Swadge ESP32-S2
APIs to develop for the Magfest Swadge
Toggle main menu visibility
Loading...
Searching...
No Matches
static_i2c.h
Go to the documentation of this file.
1
// Copyright 2012-2023 <>< Charles Lohr
2
// This file may be used for any purposes (commercial or private) just please leave this copyright notice in there
3
// somewhere.
4
5
// Generic I2C static library for AVRs (ATMega and ATTinys) (Commented out portion)
6
// Now, generic I2C library for any GPIO-based system.
7
8
// Include this in your .c file only!!!
9
// Include it after the following are defined:
10
11
// I2CDELAY_FUNC -> If you wish to override the internal delay functions.
12
// I2CSPEEDBASE -> define speed base multiplier 1 = normal, 10 = slow, .1 = fast; failure to define this will result in
13
// the clock being as fast as possible. I2CNEEDGETBYTE -> Do we need to be able to read data?
14
//
15
// I2CPREFIX -> #define to be the prefix, i.e. BOB will cause BOBConfigI2C to be generated.
16
// I2CNOSTATIC -> #define if you want the functions to be generated as not-static code.
17
//
18
// NOTE: You must initially configure the port to be outputs on both DSDA and DSCL and set them both to be driven high.
19
20
#ifndef I2CPREFIX
21
#define I2CPREFIX
22
#endif
23
24
#ifndef I2CNOSTATIC
25
#define I2CSTATICODE
26
#else
27
#define I2CSTATICODE static
28
#endif
29
30
#ifndef I2CFNCOLLAPSE
31
#define INTI2CFNCOLLAPSE(PFX, name) PFX##name
32
#define I2CFNCOLLAPSE(PFX, name) INTI2CFNCOLLAPSE(PFX, name)
33
#endif
34
35
#ifndef I2CNEEDGETBYTE
36
#define I2CNEEDGETBYTE 1
37
#endif
38
39
#ifndef DSCL_IHIGH
40
#define DSCL_IHIGH DSCL_INPUT
41
#endif
42
43
#ifndef DSDA_IHIGH
44
#define DSDA_IHIGH DSDA_INPUT
45
#endif
46
47
I2CSTATICODE
void
I2CFNCOLLAPSE
(I2CPREFIX, ConfigI2C)(){
DSDA_IHIGH
DSCL_IHIGH
}
48
49
I2CSTATICODE
void
I2CFNCOLLAPSE
(I2CPREFIX, SendStart)(){
DELAY1
DSCL_IHIGH
DELAY1
DSDA_OUTPUT
DELAY1
DSCL_OUTPUT
DELAY1
}
50
51
I2CSTATICODE
void
I2CFNCOLLAPSE
(I2CPREFIX, SendStop)(){
DELAY1
DSDA_OUTPUT
DELAY1
DSCL_IHIGH
DELAY1
DSDA_IHIGH
DELAY1
}
52
53
// Return nonzero on failure.
54
I2CSTATICODE
unsigned
char
I2CFNCOLLAPSE
(I2CPREFIX, SendByte)(
unsigned
char
data)
55
{
56
unsigned
int
i;
57
// Assume we are in a started state (DSCL = 0 & DSDA = 0)
58
59
for
(i = 0; i < 8; i++)
60
{
61
DELAY1
62
if
(data & 0x80)
63
{
64
DSDA_IHIGH
65
}
66
else
67
{
68
DSDA_OUTPUT
69
}
70
data <<= 1;
71
DELAY2
72
DSCL_IHIGH
73
DELAY2
74
DSCL_OUTPUT
75
}
76
77
// Immediately after sending last bit, open up DDDR for control.
78
DELAY1
79
DSDA_INPUT
80
DELAY1
81
DSCL_IHIGH
82
DELAY1
83
i =
READ_DSDA
;
84
DELAY1
85
DSCL_OUTPUT
86
DELAY1
87
return
!!i;
88
}
89
90
#if I2CNEEDGETBYTE
91
92
I2CSTATICODE
unsigned
char
I2CFNCOLLAPSE
(I2CPREFIX, GetByte)(uint8_t send_nak)
93
{
94
unsigned
char
i;
95
unsigned
char
ret = 0;
96
97
DSDA_INPUT
98
99
for
(i = 0; i < 8; i++)
100
{
101
DELAY1
102
DSCL_IHIGH
103
DELAY2
104
ret <<= 1;
105
if
(
READ_DSDA
)
106
ret |= 1;
107
DSCL_OUTPUT
108
}
109
110
// Send ack.
111
if
(send_nak)
112
{
113
}
114
else
115
{
116
DSDA_OUTPUT
117
}
118
119
DELAY1
120
DSCL_IHIGH
121
DELAY2
122
DSCL_OUTPUT
123
DELAY1
124
DSDA_IHIGH
125
126
return
ret;
127
}
128
129
#endif
DSCL_OUTPUT
#define DSCL_OUTPUT
Definition
hdw-imu.c:20
READ_DSDA
#define READ_DSDA
Definition
hdw-imu.c:36
DELAY1
#define DELAY1
Definition
hdw-imu.c:45
DSDA_INPUT
#define DSDA_INPUT
Definition
hdw-imu.c:32
DSDA_OUTPUT
#define DSDA_OUTPUT
Definition
hdw-imu.c:28
DELAY2
#define DELAY2
Definition
hdw-imu.c:46
DSCL_IHIGH
#define DSCL_IHIGH
Definition
static_i2c.h:40
I2CSTATICODE
#define I2CSTATICODE
Definition
static_i2c.h:25
I2CFNCOLLAPSE
#define I2CFNCOLLAPSE(PFX, name)
Definition
static_i2c.h:32
DSDA_IHIGH
#define DSDA_IHIGH
Definition
static_i2c.h:44
components
hdw-imu
static_i2c.h
Generated by
1.17.0