Skip to content

Commit d882bb2

Browse files
committed
test i2c library using a SSD1306 OLED display
1 parent 10ca2a3 commit d882bb2

8 files changed

Lines changed: 742 additions & 24 deletions

File tree

sduino/Arduino.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ void yield(void);
4040

4141

4242

43-
4443
#define HIGH 0x1
4544
#define LOW 0x0
4645

sduino/libraries/I2C/I2C.c

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
#include <WProgram.h>
6464
#endif
6565

66-
#include <inttypes.h>
66+
//#include <inttypes.h>
6767
#include "I2C.h"
6868

6969

@@ -77,14 +77,20 @@ static uint8_t bufferIndex = 0;
7777
static uint8_t totalBytes = 0;
7878
static uint16_t timeOutDelay = 0;
7979

80+
static uint8_t start(void);
81+
static uint8_t sendAddress(uint8_t);
82+
static uint8_t sendByte(uint8_t);
83+
static uint8_t receiveByte(uint8_t);
84+
static uint8_t stop(void);
85+
static void lockUp(void);
8086

8187

8288
////////////// Public Methods ////////////////////////////////////////
8389

8490

85-
8691
void I2C_begin()
8792
{
93+
#if 0
8894
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
8995
// activate internal pull-ups for twi
9096
// as per note from atmega8 manual pg167
@@ -102,18 +108,34 @@ void I2C_begin()
102108
TWBR = ((F_CPU / 100000) - 16) / 2;
103109
// enable twi module and acks
104110
TWCR = _BV(TWEN) | _BV(TWEA);
111+
#else
112+
/* I2C Initialize */
113+
I2C_Init(
114+
I2C_MAX_STANDARD_FREQ, // 100000// I2C_SPEED,
115+
0xA0, // OwnAddress, egal
116+
I2C_DUTYCYCLE_2, // 0x00
117+
I2C_ACK_CURR, // 0x01
118+
I2C_ADDMODE_7BIT, // 0x00
119+
16 // InputClockFreqencyMhz
120+
);
121+
#endif
105122
}
106123

124+
#if 0
107125
void I2C_end()
108126
{
109127
TWCR = 0;
110128
}
129+
#endif
111130

131+
#if 0
112132
void I2C_timeOut(uint16_t _timeOut)
113133
{
114134
timeOutDelay = _timeOut;
115135
}
136+
#endif
116137

138+
#if 0
117139
void I2C_setSpeed(uint8_t _fast)
118140
{
119141
if(!_fast)
@@ -125,7 +147,9 @@ void I2C_setSpeed(uint8_t _fast)
125147
TWBR = ((F_CPU / 400000) - 16) / 2;
126148
}
127149
}
150+
#endif
128151

152+
#if 0
129153
void I2C_pullup(uint8_t activate)
130154
{
131155
if(activate)
@@ -157,7 +181,9 @@ void I2C_pullup(uint8_t activate)
157181
#endif
158182
}
159183
}
184+
#endif
160185

186+
#if 0
161187
void I2C_scan()
162188
{
163189
uint16_t tempTime = timeOutDelay;
@@ -194,13 +220,16 @@ void I2C_scan()
194220
if(!totalDevicesFound){Serial.println("No devices found");}
195221
timeOutDelay = tempTime;
196222
}
223+
#endif
197224

198-
225+
#if 0
199226
uint8_t I2C_available()
200227
{
201228
return(bytesAvailable);
202229
}
230+
#endif
203231

232+
#if 0
204233
uint8_t I2C_receive()
205234
{
206235
bufferIndex = totalBytes - bytesAvailable;
@@ -212,7 +241,7 @@ uint8_t I2C_receive()
212241
bytesAvailable--;
213242
return(data[bufferIndex]);
214243
}
215-
244+
#endif
216245

217246
/*return values for new functions that use the timeOut feature
218247
will now return at what point in the transmission the timeout
@@ -262,6 +291,7 @@ uint8_t I2C_write(uint8_t address, uint8_t registerAddress)
262291
return(returnStatus);
263292
}
264293

294+
#if 1
265295
uint8_t I2C_write_c(uint8_t address, uint8_t registerAddress, uint8_t data)
266296
{
267297
returnStatus = 0;
@@ -293,14 +323,17 @@ uint8_t I2C_write_c(uint8_t address, uint8_t registerAddress, uint8_t data)
293323
}
294324
return(returnStatus);
295325
}
326+
#endif
296327

328+
#if 0
297329
uint8_t I2C_write_s(uint8_t address, uint8_t registerAddress, char *data)
298330
{
299331
uint8_t bufferLength = strlen(data);
300332
returnStatus = 0;
301333
returnStatus = write(address, registerAddress, (uint8_t*)data, bufferLength);
302334
return(returnStatus);
303335
}
336+
#endif
304337

305338
uint8_t I2C_write_sn(uint8_t address, uint8_t registerAddress, uint8_t *data, uint8_t numberBytes)
306339
{
@@ -337,6 +370,7 @@ uint8_t I2C_write_sn(uint8_t address, uint8_t registerAddress, uint8_t *data, ui
337370
return(returnStatus);
338371
}
339372

373+
#if 0
340374
uint8_t I2C_read(uint8_t address, uint8_t numberBytes)
341375
{
342376
bytesAvailable = 0;
@@ -379,7 +413,9 @@ uint8_t I2C_read(uint8_t address, uint8_t numberBytes)
379413
}
380414
return(returnStatus);
381415
}
416+
#endif
382417

418+
#if 0
383419
uint8_t I2C_read(uint8_t address, uint8_t registerAddress, uint8_t numberBytes)
384420
{
385421
bytesAvailable = 0;
@@ -439,7 +475,9 @@ uint8_t I2C_read(uint8_t address, uint8_t registerAddress, uint8_t numberBytes)
439475
}
440476
return(returnStatus);
441477
}
478+
#endif
442479

480+
#if 0
443481
uint8_t I2C_read_sn(uint8_t address, uint8_t numberBytes, uint8_t *dataBuffer)
444482
{
445483
bytesAvailable = 0;
@@ -481,7 +519,9 @@ uint8_t I2C_read_sn(uint8_t address, uint8_t numberBytes, uint8_t *dataBuffer)
481519
}
482520
return(returnStatus);
483521
}
522+
#endif
484523

524+
#if 0
485525
uint8_t I2C_read_sn(uint8_t address, uint8_t registerAddress, uint8_t numberBytes, uint8_t *dataBuffer)
486526
{
487527
bytesAvailable = 0;
@@ -541,13 +581,13 @@ uint8_t I2C_read_sn(uint8_t address, uint8_t registerAddress, uint8_t numberByte
541581
}
542582
return(returnStatus);
543583
}
544-
584+
#endif
545585

546586
/////////////// Private Methods ////////////////////////////////////////
547587

548-
549-
uint8_t I2C_start()
588+
static uint8_t start(void)
550589
{
590+
#if 0
551591
unsigned long startingTime = millis();
552592
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
553593
while (!(TWCR & (1<<TWINT)))
@@ -571,10 +611,15 @@ uint8_t I2C_start()
571611
return(bufferedStatus);
572612
}
573613
return(TWI_STATUS);
614+
#else
615+
I2C->CR2 |= I2C_CR2_START; // I2C_GenerateSTART(enable);
616+
return 0;
617+
#endif
574618
}
575619

576-
uint8_t I2C_sendAddress(uint8_t i2cAddress)
620+
static uint8_t sendAddress(uint8_t i2cAddress)
577621
{
622+
#if 0
578623
TWDR = i2cAddress;
579624
unsigned long startingTime = millis();
580625
TWCR = (1<<TWINT) | (1<<TWEN);
@@ -603,10 +648,23 @@ uint8_t I2C_sendAddress(uint8_t i2cAddress)
603648
lockUp();
604649
return(bufferedStatus);
605650
}
651+
#else
652+
/* Test on EV5 and clear it */
653+
while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
654+
655+
/* Send the Address + Direction */
656+
I2C->DR = i2cAddress; // I2C_Send7bitAddress()
657+
658+
/* Test on EV6 and clear it */
659+
while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
660+
661+
return 0;
662+
#endif
606663
}
607664

608-
uint8_t I2C_sendByte(uint8_t i2cData)
665+
uint8_t sendByte(uint8_t i2cData)
609666
{
667+
#if 0
610668
TWDR = i2cData;
611669
unsigned long startingTime = millis();
612670
TWCR = (1<<TWINT) | (1<<TWEN);
@@ -635,10 +693,18 @@ uint8_t I2C_sendByte(uint8_t i2cData)
635693
lockUp();
636694
return(bufferedStatus);
637695
}
696+
#else
697+
/* Test on EV8 */
698+
while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
699+
700+
I2C->DR = i2cData;
701+
return 1;
702+
#endif
638703
}
639704

640-
uint8_t I2C_receiveByte(uint8_t ack)
705+
static uint8_t receiveByte(uint8_t ack)
641706
{
707+
#if 0
642708
unsigned long startingTime = millis();
643709
if(ack)
644710
{
@@ -665,10 +731,15 @@ uint8_t I2C_receiveByte(uint8_t ack)
665731
return(bufferedStatus);
666732
}
667733
return(TWI_STATUS);
734+
#else
735+
(void) ack;
736+
return 1;
737+
#endif
668738
}
669739

670-
uint8_t I2C_stop()
740+
static uint8_t stop(void)
671741
{
742+
#if 0
672743
unsigned long startingTime = millis();
673744
TWCR = (1<<TWINT)|(1<<TWEN)| (1<<TWSTO);
674745
while ((TWCR & (1<<TWSTO)))
@@ -681,11 +752,20 @@ uint8_t I2C_stop()
681752
}
682753

683754
}
755+
#else
756+
/* Test on EV8_2 */
757+
while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
758+
759+
/* Generate a STOP condition */
760+
I2C->CR2 |= I2C_CR2_STOP;
761+
#endif
684762
return(0);
685763
}
686764

687-
void I2C_lockUp()
765+
static void lockUp(void)
688766
{
767+
#if 0
689768
TWCR = 0; //releases SDA and SCL lines to high impedance
690769
TWCR = _BV(TWEN) | _BV(TWEA); //reinitialize TWI
770+
#endif
691771
}

sduino/libraries/I2C/I2C.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
#include <WProgram.h>
6464
#endif
6565

66-
#include <inttypes.h>
66+
//#include <inttypes.h>
6767

6868
#ifndef I2C_h
6969
#define I2C_h
@@ -100,23 +100,24 @@
100100
uint8_t I2C_available();
101101
uint8_t I2C_receive();
102102
uint8_t I2C_write(uint8_t, uint8_t);
103-
uint8_t I2C_write(uint8_t, uint8_t, uint8_t);
104-
uint8_t I2C_write(uint8_t, uint8_t, char*);
105-
uint8_t I2C_write(uint8_t, uint8_t, uint8_t*, uint8_t);
103+
uint8_t I2C_write_c(uint8_t, uint8_t, uint8_t);
104+
uint8_t I2C_write_s(uint8_t, uint8_t, char*);
105+
uint8_t I2C_write_sn(uint8_t, uint8_t, uint8_t*, uint8_t);
106106
uint8_t I2C_read(uint8_t, uint8_t);
107-
uint8_t I2C_read(uint8_t, uint8_t, uint8_t);
108-
uint8_t I2C_read(uint8_t, uint8_t, uint8_t*);
109-
uint8_t I2C_read(uint8_t, uint8_t, uint8_t, uint8_t*);
107+
uint8_t I2C_read_c(uint8_t, uint8_t, uint8_t);
108+
uint8_t I2C_read_s(uint8_t, uint8_t, uint8_t*);
109+
uint8_t I2C_read_sn(uint8_t, uint8_t, uint8_t, uint8_t*);
110110

111111

112112
// private:
113-
uint8_t I2C_start();
113+
/*
114+
uint8_t I2C_start(void);
114115
uint8_t I2C_sendAddress(uint8_t);
115116
uint8_t I2C_sendByte(uint8_t);
116117
uint8_t I2C_receiveByte(uint8_t);
117-
uint8_t I2C_stop();
118-
void I2C_lockUp();
119-
118+
uint8_t I2C_stop(void);
119+
void I2C_lockUp(void);
120+
*/
120121
// not sure if these really need to be public:
121122
extern uint8_t returnStatus;
122123
extern uint8_t nack;

test/oled-mini/Makefile

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
BASENAME=$(shell basename $$(pwd))
2+
EXECUTABLE=$(BASENAME).ihx
3+
4+
#SDCCBASE=/usr/local
5+
SDCCBASE=/opt/sdcc
6+
BINDIR=$(SDCCBASE)/bin
7+
CC=$(BINDIR)/sdcc
8+
LD=$(BINDIR)/sdld
9+
10+
LIBBASE=../../STM8S_StdPeriph_Driver
11+
SDUINO=../../sduino
12+
13+
CFLAGS= --debug -mstm8 -DF_CPU=16000000L -DSTM8S103 -DARDUINO=180 \
14+
-I. -I$(SDUINO) -I$(SDUINO)/libraries/I2C -I$(LIBBASE)/inc \
15+
-I/usr/share/sdcc/include/
16+
# -DSUPPORT_ALTERNATE_MAPPINGS
17+
18+
LDFLAGS=-L$(LIBBASE)/src -L/opt/sdcc/share/sdcc/lib/stm8 -lstm8s103
19+
20+
OBJECTS=$(BASENAME).rel ssd1306.rel
21+
#SDLIBS=I2C
22+
SDOBJECTS=main.rel wiring.rel wiring_digital.rel
23+
SDLIBOBJECTS=I2C.rel
24+
25+
.PHONY: all clean flash
26+
27+
#all: $(OBJECTS)
28+
29+
$(EXECUTABLE): $(OBJECTS) $(SDOBJECTS) $(SDLIBOBJECTS)
30+
#test.rel wiring_digital.rel
31+
$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
32+
33+
$(OBJECTS) : %.rel : %.c
34+
$(CC) -c $(CFLAGS) $^ -o $@
35+
36+
$(SDOBJECTS) : %.rel : $(SDUINO)/%.c
37+
$(CC) -c $(CFLAGS) $^ -o $@
38+
39+
$(SDLIBOBJECTS) : %.rel : $(SDUINO)/libraries/I2C/%.c
40+
$(CC) -c $(CFLAGS) $^ -o $@
41+
42+
flash: $(EXECUTABLE)
43+
stm8flash -cstlinkv2 -pstm8s103?3 -w $^
44+
45+
readopt:
46+
stm8flash -c stlinkv2 -p stm8s103?3 -s opt -r opt.bin
47+
48+
49+
clean:
50+
rm -f *.lib *.rst *.rel *.lst *.ihx *.sym *.asm *.lk *.map \
51+
*.cdb *.adb *~ *.bak
52+
rm -f $(EXECUTABLE)

0 commit comments

Comments
 (0)