-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path128x64 i2c.fth
494 lines (430 loc) · 12.8 KB
/
128x64 i2c.fth
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
\ *********************************************************************
\ Display text on OLED SCREEN SSD1306 128x64 *
\ Filename: SSD1306textDisplay.txt *
\ Date: 25.11.2020 *
\ Updated: 10.03.2023 *
\ File Version: 1.0 *
\ MCU: ARDUINO all models *
\ GNU General Public License *
\ FF Version: 5.0 * *
\ Copyright Marc PETREMANN *
\ Modified by Phil Waller *
\ *********************************************************************
\ *** essential code for I2C transmission ***********
-i2c-new
marker -i2c-new
\ reg: is an alias for CONSTANT
\ use to define registers - for more readability
: reg: ( comp: n --- | exec: --- n)
create
,
does>
@
;
\ alias for CONSTANT, use to define bits
: bit: ( c --- )
reg:
;
flash
\ i2c Two-Wire-Interface Registers
184 reg: TWBR \ TWI Bit Rate register
185 reg: TWSR \ TWI Status Register
\ 186 reg: TWAR \ TWI (Slave) Address register - unused
187 reg: TWDR \ TWI Data register
188 reg: TWCR \ TWI Control Register
\ Bits in the Address register TWAR
%11111110 bit: TWAR_TWA \ (Slave) Address register Bits
%00000001 bit: TWAR_TWGCE \ General Call Recognition Enable Bit
\ Bits in the Control Register TWCR
%10000000 bit: TWCR_TWINT \ Interrupt Flag
%01000000 bit: TWCR_TWEA \ Enable Acknowledge Bit
%00100000 bit: TWCR_TWSTA \ Start Condition Bit
%00010000 bit: TWCR_TWSTO \ Stop Condition Bit
%00001000 bit: TWCR_TWWC \ Write Collition Flag
%00000100 bit: TWCR_TWEN \ Enable Bit
%00000001 bit: TWCR_TWIE \ Interrupt Enable
\ bits in the Status Register TWI
%11111000 bit: TWSR_TWS \ TWI Status
%00000011 bit: TWSR_TWPS \ TWI Prescaler
ram
\ Set clock frequency to 100kHz
: i2c.init ( -- )
TWSR_TWPS TWSR mclr \ prescale value = 1
[ Fcy 100 / 16 - 2/ ] literal TWBR c!
%00000011 TWCR mset
;
\ Wait for operation to complete
: i2c.wait ( -- )
\ When TWI operations are done, the hardware sets
\ the TWINT interrupt flag, which we will poll.
begin
TWCR c@ TWCR_TWINT and
until
;
\ Send start condition
: i2c.start ( -- )
[ TWCR_TWINT TWCR_TWEN or TWCR_TWSTA or ] literal TWCR c!
i2c.wait
;
\ Send repeated start condition
: i2c.rsen ( -- )
i2c.start \ AVR doesn't distinguish
;
: i2c.restart
i2c.start \ AVR doesn't distinguish
;
\ Send stop condition
: i2c.stop ( -- )
[ TWCR_TWINT TWCR_TWEN or TWCR_TWSTO or ] literal TWCR c!
;
\ Write one byte to bus, returning 0 if ACK was received, -1 otherwise.
: i2c.c! ( c -- f )
i2c.wait \ Must have TWINT high to write data
TWDR c!
[ TWCR_TWINT TWCR_TWEN or ] literal TWCR c!
i2c.wait
\ Test for arrival of an ACK depending on what was sent.
TWSR c@ $f8 and $18 xor 0= if 0 exit then \ SLA+W
TWSR c@ $f8 and $28 xor 0= if 0 exit then \ data byte
TWSR c@ $f8 and $40 xor 0= if 0 exit then \ SLA+R
-1 \ Something other than an ACK resulted
;
\ Write one byte to bus
: i2c.tx ( c ---)
i2c.c! drop ;
\ Read one byte and ack for another.
: [email protected] ( -- c )
[ TWCR_TWINT TWCR_TWEN or TWCR_TWEA or ] literal TWCR c!
i2c.wait
TWDR c@
;
\ Read one last byte.
: [email protected] ( -- c )
[ TWCR_TWINT TWCR_TWEN or ] literal TWCR c!
i2c.wait
TWDR c@
;
\ Address slave for writing, leaving true if slave ready.
: i2c.addr.write ( 7-bit-addr -- f )
1 lshift 1 invert and \ Build full byte with write-bit as 0
i2c.start i2c.c!
if false
else true then
;
\ Address slave for reading, leaving true if slave ready.
: i2c.addr.read ( 7-bit-addr -- f )
1 lshift 1 or \ Build full byte with read-bit as 1
i2c.start i2c.c!
if false
else true then
;
\ Detect presence of device, leaving true if slave responded.
\ If the slave ACKs the read request, fetch one byte only.
: i2c.ping? ( 7-bit-addr -- f )
1 lshift 1 or \ Build full byte with read-bit as 1
i2c.start i2c.c! 0=
if [email protected] drop true
else false
then
;
\ require part of i2c-new.txt
\ usage of stream: example
\ eeprom
\ stream: XX \ create XX like as array with no datas
\ 2 c, \ compile one byte (command or data)
\ 3 c, \ compile other datas or commands
\ 4 c,
\ ;stream \ store at XX addr the legth of datas
\ ram
-stream
marker -stream
\ do nothing - default action for stream:
: nothing ( ---)
;
defer stream.action \ default action for stream:
\ define a command or data stream for SSD1306
: stream:
\ set nothing as execute action by default
\ ['] nothing is stream.action
create
here \ leave current dictionnary pointer on stack
0 c, \ initial length of data is 0
does>
stream.action
;
\ store at addr length of datas compiled beetween
\ and here
: ;stream ( addr-var len ---)
dup 1+ here
swap - \ calculate cdata length
\ store c in first byte of word defined by stream:
swap c!
;
-streamDebug
marker -streamDebug
\ get real addr2 and u length form addr1
: count ( addr1 --- addr2 u)
dup c@ \ push real length
swap 1+ swap \ push start address of datas
;
\ used for debugging streams
\ for use:
\ ' disp.stream is stream.action
: disp.stream ( stream-addr ---)
count
for
c@+ .
next
drop
;
\ *** Manage OLED display 128x64 ********
-ssd1306
marker -ssd1306
$3c constant addrSSD1306 \ i2c device address
\ control: $00 for commands
\ $40 for datas
$00 constant CTRL_COMMANDS
$40 constant CTRL_DATAS
\ send stream of datas or commands to SSD1306
: i2c.stream.tx ( stream-addr ---)
addrSSD1306 i2c.addr.write drop \ send SSD1306 address
count \ fetch real addr and length of datas to send
for
c@+ i2c.tx \ send commands or datas
next
drop
i2c.stop
;
\ usage:
\ ' i2c.stream.tx is stream.action
-commands
marker -commands
\ define SSD1306 128x64 ram size
128 constant DISPLAY_WIDTH
64 constant DISPLAY_HEIGHT
DISPLAY_WIDTH DISPLAY_HEIGHT * 8 / constant SSDramSize
flash
stream: disp.setup
CTRL_COMMANDS c,
$ae c, ( DISP_SLEEP )
$d5 c, ( SET_DISP_CLOCK )
$80 c,
$a8 c, ( SET_MULTIPLEX_RATIO )
$3f c,
$d3 c, ( SET_VERTICAL_OFFSET )
$00 c,
$40 c, ( SET_DISP_START_LINE )
$8d c, ( CHARGE_PUMP_REGULATOR )
$14 c, ( CHARGE_PUMP_ON )
$20 c, ( MEM_ADDRESSING )
$00 c,
$a0 c, ( SET_SEG_REMAP_0 )
$c0 c, ( SET_COM_SCAN_NORMAL )
$da c, ( SETCOMPINS )
$12 c, \ $02 or $12 ???
$db c, ( SET_VCOM_DESELECT_LEVEL )
$40 c,
$a4 c, ( RESUME_TO_RAM_CONTENT )
$a6 c, ( NORMALDISPLAY )
$af c, ( DISP_ON )
;stream
stream: disp.reset
CTRL_COMMANDS c,
$21 c, \ COL START_END
$00 c, \ start
$7f c, \ end
$22 c, \ PAGE START_END
$00 c, \ start
$07 c, \ end
;stream
ram
: disp.init ( -- )
disp.setup
disp.reset
;
: disp.clear ( ---)
disp.reset
addrSSD1306 i2c.addr.write drop \ send SSD1306 address
CTRL_DATAS i2c.tx
SSDramSize
for
$00 i2c.tx \ send commands or datas
next
i2c.stop
disp.reset
;
: init ( ---)
i2c.init
['] i2c.stream.tx is stream.action
disp.init ;
-dispUtil
marker -dispUtil
0 value currentPage
: set.line
addrSSD1306 i2c.addr.write drop \ send SSD1306 address
CTRL_COMMANDS i2c.tx
$21 i2c.tx \ COL START_END
$00 i2c.tx \ start
$7f i2c.tx \ end
$22 i2c.tx \ PAGE START_END
currentPage i2c.tx \ start
currentPage i2c.tx \ end
i2c.stop
;
: line.clear ( ---)
addrSSD1306 i2c.addr.write drop \ send SSD1306 address
CTRL_DATAS i2c.tx
DISPLAY_WIDTH
for
$00 i2c.tx \ send commands or datas
next
i2c.stop
;
: crLine ( ---)
currentPage 1+ 7 and to currentPage
set.line
line.clear
;
-fonts
marker -fonts
flash
hex
create FONTS \ 5x8
00 c, 00 c, 00 c, 00 c, 00 c, \
00 c, 00 c, 4f c, 00 c, 00 c, \ !
00 c, 03 c, 00 c, 03 c, 00 c, \ "
14 c, 3e c, 14 c, 3e c, 14 c, \ #
24 c, 2a c, 7f c, 2a c, 12 c, \ $
63 c, 13 c, 08 c, 64 c, 63 c, \ %
36 c, 49 c, 55 c, 22 c, 50 c, \ &
00 c, 00 c, 07 c, 00 c, 00 c, \ '
00 c, 1c c, 22 c, 41 c, 00 c, \ (
00 c, 41 c, 22 c, 1c c, 00 c, \ )
0a c, 04 c, 1f c, 04 c, 0a c, \ *
04 c, 04 c, 1f c, 04 c, 04 c, \ +
50 c, 30 c, 00 c, 00 c, 00 c, \ ,
08 c, 08 c, 08 c, 08 c, 08 c, \ -
60 c, 60 c, 00 c, 00 c, 00 c, \ .
00 c, 60 c, 1c c, 03 c, 00 c, \ /
3e c, 41 c, 49 c, 41 c, 3e c, \ 0
00 c, 02 c, 7f c, 00 c, 00 c, \ 1
46 c, 61 c, 51 c, 49 c, 46 c, \ 2
21 c, 49 c, 4d c, 4b c, 31 c, \ 3
18 c, 14 c, 12 c, 7f c, 10 c, \ 4
4f c, 49 c, 49 c, 49 c, 31 c, \ 5
3e c, 51 c, 49 c, 49 c, 32 c, \ 6
01 c, 01 c, 71 c, 0d c, 03 c, \ 7
36 c, 49 c, 49 c, 49 c, 36 c, \ 8
26 c, 49 c, 49 c, 49 c, 3e c, \ 9
00 c, 33 c, 33 c, 00 c, 00 c, \ :
00 c, 53 c, 33 c, 00 c, 00 c, \ ;
00 c, 08 c, 14 c, 22 c, 41 c, \ <
14 c, 14 c, 14 c, 14 c, 14 c, \ =
41 c, 22 c, 14 c, 08 c, 00 c, \ >
06 c, 01 c, 51 c, 09 c, 06 c, \ ?
3e c, 41 c, 49 c, 15 c, 1e c, \ @
78 c, 16 c, 11 c, 16 c, 78 c, \ A
7f c, 49 c, 49 c, 49 c, 36 c, \ B
3e c, 41 c, 41 c, 41 c, 22 c, \ C
7f c, 41 c, 41 c, 41 c, 3e c, \ D
7f c, 49 c, 49 c, 49 c, 49 c, \ E
7f c, 09 c, 09 c, 09 c, 09 c, \ F
3e c, 41 c, 41 c, 49 c, 7b c, \ G
7f c, 08 c, 08 c, 08 c, 7f c, \ H
00 c, 41 c, 7f c, 41 c, 00 c, \ I
38 c, 40 c, 40 c, 41 c, 3f c, \ J
7f c, 08 c, 08 c, 14 c, 63 c, \ K
7f c, 40 c, 40 c, 40 c, 40 c, \ L
7f c, 06 c, 18 c, 06 c, 7f c, \ M
7f c, 06 c, 18 c, 60 c, 7f c, \ N
3e c, 41 c, 41 c, 41 c, 3e c, \ O
7f c, 09 c, 09 c, 09 c, 06 c, \ P
3e c, 41 c, 51 c, 21 c, 5e c, \ Q
7f c, 09 c, 19 c, 29 c, 46 c, \ R
26 c, 49 c, 49 c, 49 c, 32 c, \ S
01 c, 01 c, 7f c, 01 c, 01 c, \ T
3f c, 40 c, 40 c, 40 c, 7f c, \ U
0f c, 30 c, 40 c, 30 c, 0f c, \ V
1f c, 60 c, 1c c, 60 c, 1f c, \ W
63 c, 14 c, 08 c, 14 c, 63 c, \ X
03 c, 04 c, 78 c, 04 c, 03 c, \ Y
61 c, 51 c, 49 c, 45 c, 43 c, \ Z
00 c, 7f c, 41 c, 00 c, 00 c, \ [
00 c, 03 c, 1c c, 60 c, 00 c, \ \
00 c, 41 c, 7f c, 00 c, 00 c, \ ]
0c c, 02 c, 01 c, 02 c, 0c c, \ ^
40 c, 40 c, 40 c, 40 c, 40 c, \ _
00 c, 01 c, 02 c, 04 c, 00 c, \ `
20 c, 54 c, 54 c, 54 c, 78 c, \ a
7f c, 48 c, 44 c, 44 c, 38 c, \ b
38 c, 44 c, 44 c, 44 c, 44 c, \ c
38 c, 44 c, 44 c, 48 c, 7f c, \ d
38 c, 54 c, 54 c, 54 c, 18 c, \ e
08 c, 7e c, 09 c, 09 c, 00 c, \ f
0c c, 52 c, 52 c, 54 c, 3e c, \ g
7f c, 08 c, 04 c, 04 c, 78 c, \ h
00 c, 00 c, 7d c, 00 c, 00 c, \ i
00 c, 40 c, 3d c, 00 c, 00 c, \ j
7f c, 10 c, 28 c, 44 c, 00 c, \ k
00 c, 00 c, 3f c, 40 c, 00 c, \ l
7c c, 04 c, 18 c, 04 c, 78 c, \ m
7c c, 08 c, 04 c, 04 c, 78 c, \ n
38 c, 44 c, 44 c, 44 c, 38 c, \ o
7f c, 12 c, 11 c, 11 c, 0e c, \ p
0e c, 11 c, 11 c, 12 c, 7f c, \ q
00 c, 7c c, 08 c, 04 c, 04 c, \ r
48 c, 54 c, 54 c, 54 c, 24 c, \ s
04 c, 3e c, 44 c, 44 c, 00 c, \ t
3c c, 40 c, 40 c, 20 c, 7c c, \ u
1c c, 20 c, 40 c, 20 c, 1c c, \ v
1c c, 60 c, 18 c, 60 c, 1c c, \ w
44 c, 28 c, 10 c, 28 c, 44 c, \ x
46 c, 28 c, 10 c, 08 c, 06 c, \ y
44 c, 64 c, 54 c, 4c c, 44 c, \ z
00 c, 08 c, 77 c, 41 c, 00 c, \ {
00 c, 00 c, 7f c, 00 c, 00 c, \ |
00 c, 41 c, 77 c, 08 c, 00 c, \ }
10 c, 08 c, 18 c, 10 c, 08 c, \ ~
decimal
ram
-gestFonts
marker -gestFonts
\ Translates ASCII to address of bitpatterns:
: a>bp ( c -- c-adr )
32 max 127 min
32 - 5 * FONTS +
;
\ Draw character:
: char.tx ( c --)
\ if 'cr' go to next line
dup $0d =
if
crLine drop
exit
then
\ otherwise, display character
addrSSD1306 i2c.addr.write drop \ send SSD1306 address
CTRL_DATAS i2c.tx
a>bp \ start addr
5
for
c@+ \ get byte and inc addr
i2c.tx \ transmit byte
next
drop
$00 i2c.tx \ transmit 'blank'
i2c.stop
;
\ display text compiled with s"
: string.tx ( adr len --)
for
c@+ char.tx
next
drop
;
: disp.test
init
disp.clear
s" test"
string.tx
;