Skip to content

Commit 52bf8ac

Browse files
Use enums for states, move one more var to twi struct
Make the TWI states enums and not #defines, in the hope that it will allow GCC to more easily flag problems and general good code organization. 401000cc l F .text1 00000014 twi_delay 401000e8 w F .text1 00000032 twi_releaseBus 40100128 g F .text1 00000217 twi_onTwipEvent 4010034c l F .text1 00000149 onSdaChange 4010049c l F .text1 00000257 onSclChange 401006f4 l F .text1 00000028 onTimer Looks like another 16 bytes IRAM saved from the prior push. Sketch uses 267079 bytes (25%) of program storage space. Maximum is 1044464 bytes. Global variables use 27696 bytes (33%) of dynamic memory, leaving 54224 bytes for local variables. Maximum is 81920 bytes.
1 parent b975996 commit 52bf8ac

File tree

1 file changed

+18
-37
lines changed

1 file changed

+18
-37
lines changed

cores/esp8266/core_esp8266_si2c.cpp

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,48 +23,34 @@
2323
#include "pins_arduino.h"
2424
#include "wiring_private.h"
2525

26-
extern "C" {
2726

28-
unsigned int preferred_si2c_clock = 100000;
27+
28+
extern "C" {
2929
#include "twi_util.h"
3030

3131
#include "ets_sys.h"
3232

33+
3334
#pragma GCC diagnostic push
3435
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
3536

37+
// modes (private)
38+
typedef enum { TWIPM_UNKNOWN = 0, TWIPM_IDLE, TWIPM_ADDRESSED, TWIPM_WAIT} twipModeType;
39+
40+
// states (private)
41+
typedef enum { TWIP_UNKNOWN = 0, TWIP_IDLE, TWIP_START, TWIP_SEND_ACK, TWIP_WAIT_ACK, TWIP_WAIT_STOP, TWIP_SLA_W, TWIP_SLA_R, TWIP_REP_START, TWIP_READ, TWIP_STOP, TWIP_REC_ACK, TWIP_READ_ACK, TWIP_RWAIT_ACK, TWIP_WRITE, TWIP_BUS_ERR } twipStateType;
42+
typedef enum { TWI_READY=0, TWI_MRX, TWI_MTX, TWI_SRX, TWI_STX } twiStateType;
43+
3644
static struct twi {
45+
unsigned int preferred_si2c_clock; // = 100000;
3746
unsigned char twi_dcount;// = 18;
3847
unsigned char twi_sda, twi_scl;
3948
uint32_t twi_clockStretchLimit;
4049
unsigned char twi_addr;// = 0;
4150

42-
// modes (private)
43-
#define TWIPM_UNKNOWN 0
44-
#define TWIPM_IDLE 1
45-
#define TWIPM_ADDRESSED 2
46-
#define TWIPM_WAIT 3
4751

48-
// states (private)
49-
#define TWIP_UNKNOWN 0
50-
#define TWIP_IDLE 1
51-
#define TWIP_START 2
52-
#define TWIP_SEND_ACK 3
53-
#define TWIP_WAIT_ACK 4
54-
#define TWIP_WAIT_STOP 5
55-
#define TWIP_SLA_W 6
56-
#define TWIP_SLA_R 7
57-
#define TWIP_REP_START 8
58-
#define TWIP_READ 9
59-
#define TWIP_STOP 10
60-
#define TWIP_REC_ACK 11
61-
#define TWIP_READ_ACK 12
62-
#define TWIP_RWAIT_ACK 13
63-
#define TWIP_WRITE 14
64-
#define TWIP_BUS_ERR 15
65-
66-
volatile int twip_mode;// = TWIPM_IDLE;
67-
volatile int twip_state;// = TWIP_IDLE;
52+
volatile twipModeType twip_mode;// = TWIPM_IDLE;
53+
volatile twipStateType twip_state;// = TWIP_IDLE;
6854
volatile int twip_status;// = TW_NO_INFO;
6955
volatile int bitCount;// = 0;
7056

@@ -74,12 +60,7 @@ static struct twi {
7460
volatile int twi_ack_rec;// = 0;
7561
volatile int twi_timeout_ms;// = 10;
7662

77-
#define TWI_READY 0
78-
#define TWI_MRX 1
79-
#define TWI_MTX 2
80-
#define TWI_SRX 3
81-
#define TWI_STX 4
82-
volatile int twi_state;// = TWI_READY;
63+
volatile twiStateType twi_state;// = TWI_READY;
8364
volatile uint8_t twi_error;// = 0xFF;
8465

8566
uint8_t twi_txBuffer[TWI_BUFFER_LENGTH];
@@ -101,8 +82,8 @@ static struct twi {
10182

10283
ETSEvent eventTaskQueue[EVENTTASK_QUEUE_SIZE];
10384
ETSTimer timer;
104-
} twi = { 18, 0, 0, 0, 0, TWIPM_IDLE, TWIP_IDLE, TW_NO_INFO, 0, 0, 0, 0, 10, TWI_READY, 0xff, {0}, 0, 0, {0}, 0, NULL, NULL, {{0}}, {0,0,0,0}};
105-
// = { twi_dcount: 18, twi_addr: 0, twip_mode: TWIPM_IDLE, twip_state: TWIP_IDLE, twip_status: TW_NO_INFO, bitCount: 0, twi_data: 0x00, twi_ack: 0, twi_ack_rec: 0, twi_timeout_ms: 10, twi_state: TWI_READY, twi_error: 0xFF };
85+
} twi = { 100000, 18, 0, 0, 0, 0, TWIPM_IDLE, TWIP_IDLE, TW_NO_INFO, 0, 0, 0, 0, 10, TWI_READY, 0xff, {0}, 0, 0, {0}, 0, NULL, NULL, {{0}}, {0,0,0,0}};
86+
// = { preferred_si2c_clock: 100000, twi_dcount: 18, twi_addr: 0, twip_mode: TWIPM_IDLE, twip_state: TWIP_IDLE, twip_status: TW_NO_INFO, bitCount: 0, twi_data: 0x00, twi_ack: 0, twi_ack_rec: 0, twi_timeout_ms: 10, twi_state: TWI_READY, twi_error: 0xFF };
10687
#pragma GCC diagnostic pop
10788

10889
static void onSclChange(void);
@@ -128,7 +109,7 @@ static void onTimer(void *unused);
128109
#endif
129110

130111
void twi_setClock(unsigned int freq){
131-
preferred_si2c_clock = freq;
112+
twi.preferred_si2c_clock = freq;
132113
#if F_CPU == FCPU80
133114
if(freq <= 50000) twi.twi_dcount = 38;//about 50KHz
134115
else if(freq <= 100000) twi.twi_dcount = 19;//about 100KHz
@@ -164,7 +145,7 @@ void twi_init(unsigned char sda, unsigned char scl)
164145
twi.twi_scl = scl;
165146
pinMode(twi.twi_sda, INPUT_PULLUP);
166147
pinMode(twi.twi_scl, INPUT_PULLUP);
167-
twi_setClock(preferred_si2c_clock);
148+
twi_setClock(twi.preferred_si2c_clock);
168149
twi_setClockStretchLimit(230); // default value is 230 uS
169150

170151
if (twi.twi_addr != 0)

0 commit comments

Comments
 (0)