Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
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
47I2CSTATICODE void I2CFNCOLLAPSE(I2CPREFIX, ConfigI2C)(){DSDA_IHIGH DSCL_IHIGH}
48
50
52
53// Return nonzero on failure.
54I2CSTATICODE 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 {
65 }
66 else
67 {
69 }
70 data <<= 1;
71 DELAY2
73 DELAY2
75 }
76
77 // Immediately after sending last bit, open up DDDR for control.
78 DELAY1
80 DELAY1
82 DELAY1
83 i = READ_DSDA;
84 DELAY1
86 DELAY1
87 return !!i;
88}
89
90#if I2CNEEDGETBYTE
91
92I2CSTATICODE unsigned char I2CFNCOLLAPSE(I2CPREFIX, GetByte)(uint8_t send_nak)
93{
94 unsigned char i;
95 unsigned char ret = 0;
96
98
99 for (i = 0; i < 8; i++)
100 {
101 DELAY1
103 DELAY2
104 ret <<= 1;
105 if (READ_DSDA)
106 ret |= 1;
108 }
109
110 // Send ack.
111 if (send_nak)
112 {
113 }
114 else
115 {
117 }
118
119 DELAY1
121 DELAY2
123 DELAY1
125
126 return ret;
127}
128
129#endif
#define DSCL_OUTPUT
Definition hdw-imu.c:20
#define READ_DSDA
Definition hdw-imu.c:36
#define DELAY1
Definition hdw-imu.c:45
#define DSDA_INPUT
Definition hdw-imu.c:32
#define DSDA_OUTPUT
Definition hdw-imu.c:28
#define DELAY2
Definition hdw-imu.c:46
#define DSCL_IHIGH
Definition static_i2c.h:40
#define I2CSTATICODE
Definition static_i2c.h:25
#define I2CFNCOLLAPSE(PFX, name)
Definition static_i2c.h:32
#define DSDA_IHIGH
Definition static_i2c.h:44