WEBVTT
Kind: captions
Language: en-US

00:00:00.539 --> 00:00:03.139
Hello everyone, it has been a while.

00:00:03.139 --> 00:00:07.960
This video I would like to share one of the
projects I finished recently, ELterm.

00:00:07.960 --> 00:00:10.860
Which stands for electroluminescence terminal.

00:00:10.860 --> 00:00:15.870
I bought these Planar 640 by 480 electroluminescence
screens a few months back.

00:00:15.870 --> 00:00:21.220
The display is sort of similar to OLED, but
it’s monochrome.

00:00:21.220 --> 00:00:27.040
These dot matrix screens typically emit yellow
or amber ish color, but there are also models

00:00:27.040 --> 00:00:29.930
which are red and green dual color.

00:00:29.930 --> 00:00:35.660
Naturally I want to make something with this
screen, and I decided to make a serial terminal

00:00:35.660 --> 00:00:38.470
with it, so let’s get started.

00:00:38.470 --> 00:00:43.270
This video contains the following parts:
Processor selection and overall design

00:00:43.270 --> 00:00:46.390
Making the hardware
Writing the driver for EL screen

00:00:46.390 --> 00:00:50.240
Implement the serial terminal
Final result

00:00:50.240 --> 00:00:51.590
First is about the processor selection.

00:00:51.590 --> 00:00:55.900
I would like to implement the whole thing
with just a microcontroller.

00:00:55.900 --> 00:01:01.940
The serial terminal part should be okay, the
original VT220 was based on a microcontroller

00:01:01.940 --> 00:01:03.160
anyway.

00:01:03.160 --> 00:01:05.630
But refreshing the screen might not.

00:01:05.630 --> 00:01:11.490
Most of the dot matrix display technology
requires constant refreshing, like at 60Hz.

00:01:11.490 --> 00:01:16.900
Screen modules that’s commonly used with
microcontrollers are typically self-refresh screens.

00:01:16.900 --> 00:01:21.810
The screen module has its own memory and controller,
and would refresh from its own memory without

00:01:21.810 --> 00:01:23.870
microcontroller intervention.

00:01:23.870 --> 00:01:30.240
For example, like these OLED modules, while
the OLED needs to be constantly refreshed, I could

00:01:30.240 --> 00:01:34.030
just disconnect the microcontroller, and it
keeps the display.

00:01:34.030 --> 00:01:39.000
There exists self-refreshing EL screens, but
the one I have isn’t.

00:01:39.000 --> 00:01:44.500
It demands to be refreshed at 120Hz, which
means the microcontroller needs to continuously

00:01:44.500 --> 00:01:47.670
push data to the screen at 120Hz.

00:01:47.670 --> 00:01:50.060
This put some requirements on the microcontroller.

00:01:50.060 --> 00:01:56.030
It would either hold the entire framebuffer
in the RAM, or it could generate image data

00:01:56.030 --> 00:01:58.810
at screen refreshing speed.

00:01:58.810 --> 00:02:02.790
Both works, but bing able to store the frame
buffer would be a bit more flexible.

00:02:02.790 --> 00:02:07.780
Then it should be able to generate the timing
needed by the screen.

00:02:07.780 --> 00:02:13.159
Obviously one could do this with an FPGA,
and I do have an FPGA video coming up in the

00:02:13.159 --> 00:02:15.760
future, but that’s for another time.

00:02:15.760 --> 00:02:21.909
For this time, many of the 32bit microcontroller
works here, I chose the RP2040 from Raspberry

00:02:21.909 --> 00:02:22.909
Pi.

00:02:22.909 --> 00:02:25.939
Here are the main spec for the RP2040.

00:02:25.939 --> 00:02:32.879
Dual-core Cortex-M0, 264KB SRAM, no internal
Flash, executes code from SPI flash.

00:02:32.879 --> 00:02:38.689
The most important part for this project is
it has PIO, which is a micro-code programmable

00:02:38.689 --> 00:02:39.909
IO module.

00:02:39.909 --> 00:02:42.790
I will show how to use it to drive the screen.

00:02:42.790 --> 00:02:45.120
Now let’s take a look at the overall design.

00:02:45.120 --> 00:02:49.230
With the microcontroller in, I just need to
fill in the interfaces I need.

00:02:49.230 --> 00:02:53.129
The screen doesn’t need any extra interface
chips, and could be connected directly to

00:02:53.129 --> 00:02:54.669
the microcontroller.

00:02:54.669 --> 00:02:59.489
The terminal needs keyboard input, so adding
a PS2 port here.

00:02:59.489 --> 00:03:04.510
Of course I could also just use the USB port
of RP2040 itself.

00:03:04.510 --> 00:03:13.159
At last, the terminal needs a serial port,
so adding a MAX3232 for a RS232 port, with

00:03:13.159 --> 00:03:15.540
jumpers for TTL serial options.

00:03:15.540 --> 00:03:22.169
For the power supply, the screen has internal
high voltage generation, so only a 12V is

00:03:22.169 --> 00:03:23.169
needed.

00:03:23.169 --> 00:03:29.510
I decided to use 12V input for the whole board,
and then use a 12V to 5V DCDC to power the

00:03:29.510 --> 00:03:32.120
Raspberry Pi Pico.

00:03:32.120 --> 00:03:33.440
Then comes routing the board.

00:03:33.440 --> 00:03:38.719
To keep things simple, I am using the Pico
as a module and mounting it directly on the

00:03:38.719 --> 00:03:39.719
board.

00:03:39.719 --> 00:03:41.069
There isn't much special about this board.

00:03:41.069 --> 00:03:43.650
So I am not going into details.

00:03:43.650 --> 00:03:48.849
Once done, exporting, submitting
to the manufacturer, and a few days later

00:03:48.849 --> 00:03:50.359
I got the board.

00:03:50.359 --> 00:03:51.589
Then soldering the board.

00:03:51.589 --> 00:03:57.489
I usually solder the capacitors and resistors
first, then chips, and last the connectors.

00:03:57.489 --> 00:04:02.919
There isn't much stuff on this board, and
to be honest, I have been mostly working on

00:04:02.919 --> 00:04:08.230
more complex boards like with FPGA or SoC
or DDR, and it’s refreshing to work with

00:04:08.230 --> 00:04:11.010
these simple 2-layer microcontroller boards
again.

00:04:11.010 --> 00:04:13.359
Now it’s time for coding.

00:04:13.359 --> 00:04:15.189
First about the screen driving.

00:04:15.189 --> 00:04:17.980
There are clear timing requirements in the
screen datasheet.

00:04:17.980 --> 00:04:24.620
The screen is divided into upper half and
lower half, and they are being refreshed simultaneously.

00:04:24.620 --> 00:04:29.790
Refreshing process is also quite straightforward,
just send the pixels, send a horizontal sync

00:04:29.790 --> 00:04:34.000
when reaching the line end, and send a vertical
sync on the first line.

00:04:34.000 --> 00:04:39.360
This sounds similar to a typical RGB or DPI
signal, but the difference is that it transmits

00:04:39.360 --> 00:04:45.340
multiple pixels on each clock, and there are
2 raster beams instead of 1.

00:04:45.340 --> 00:04:49.280
If I directly use GPIO to do that, it looks
like this.

00:04:49.280 --> 00:04:52.262
First fetch the screen buffer address, then
enter the loop.

00:04:52.262 --> 00:04:56.620
The loop only covers half of the screen as
the screen has been divided vertically.

00:04:56.620 --> 00:05:02.630
Each line has 640 pixels divided by 8 pixels
per byte equals 80 bytes.

00:05:02.630 --> 00:05:07.380
The data bus is 4 bit wide, so each byte takes
2 clocks.

00:05:07.380 --> 00:05:13.140
The code for sending each byte is simply setting
the GPIO based on the bits, and sending a

00:05:13.140 --> 00:05:15.070
pulse on the clock line.

00:05:15.070 --> 00:05:18.760
Additionally, send the sync signals based
on the previous description.

00:05:18.760 --> 00:05:22.230
It should be obvious that such code is not
efficient at all.

00:05:22.230 --> 00:05:23.761
And here is the result if I run it.

00:05:23.761 --> 00:05:27.190
It works, but it’s quite flickery.

00:05:27.190 --> 00:05:31.620
If measured with an oscilloscope, the current
refresh rate is about 64Hz.

00:05:31.620 --> 00:05:38.530
It’s kinda far away from 120Hz, or in other
words, it needs to use 188% of the processor

00:05:38.530 --> 00:05:42.370
to achieve 120Hz refresh rate.

00:05:42.370 --> 00:05:43.730
Now on to improve that.

00:05:43.730 --> 00:05:49.890
The obvious thing to optimize is not using
bit tests to output data, but directly shifting

00:05:49.890 --> 00:05:52.820
and writing to the GPIO output register.

00:05:52.820 --> 00:05:58.060
Or even better, we have the PIO, which could
do the shifting and outputting at a preset

00:05:58.060 --> 00:05:59.420
clock rate.

00:05:59.420 --> 00:06:01.820
The PIO needs some microcode for operation.

00:06:01.820 --> 00:06:04.070
For the first test, the code is simple:

00:06:04.070 --> 00:06:10.250
The out command is for outputting data signal,
and side is for outputting the clock signal.

00:06:10.250 --> 00:06:16.310
The code outputs 4 bit data and set the clock
high in the first cycle, and set clock low

00:06:16.310 --> 00:06:17.430
in the second cycle.

00:06:17.430 --> 00:06:19.510
Then it wraps around.

00:06:19.510 --> 00:06:24.270
The screen needs 2 data streams, for the upper
and lower part respectively.

00:06:24.270 --> 00:06:27.390
I am going to just use 2 PIO state machines.

00:06:27.390 --> 00:06:33.070
Put the sync signals aside, directly modify
the code to use PIO for data output:

00:06:33.070 --> 00:06:38.050
The GPIO operations are now replaced with
code filling FIFO of PIO.

00:06:38.050 --> 00:06:42.600
And it waits for the PIO to finish sending
data before sending the synchronization signals.

00:06:42.600 --> 00:06:46.160
PIO also needs some additional initialization.

00:06:46.160 --> 00:06:47.900
For example the clock frequency.

00:06:47.900 --> 00:06:57.380
At 120Hz refresh rate, that’s 120Hz by 240
lines by 160 transmissions, equals to 4.6

00:06:57.380 --> 00:06:59.160
MHz output clock.

00:06:59.160 --> 00:07:04.540
The PIO always sends data at the configured
clock rate, so the CPU just needs to be fast

00:07:04.540 --> 00:07:09.680
enough to feed the data, without needing additional
delay code to match the frame rate.

00:07:09.680 --> 00:07:12.030
The code is still not complete though.

00:07:12.030 --> 00:07:16.450
The current code doesn’t guarantee that
two PIO state machines would always be in

00:07:16.450 --> 00:07:17.450
sync.

00:07:17.450 --> 00:07:22.040
For example here, it writes to the upper screen
state machine, then writes to the lower screen.

00:07:22.040 --> 00:07:24.900
So the upper state machine would start working
first.

00:07:24.900 --> 00:07:27.940
If lucky, both state machines would be in
sync.

00:07:27.940 --> 00:07:34.530
If unlucky, the upper one would be leading
the lower one, causing the image to be shifted.

00:07:34.530 --> 00:07:39.090
A simple way to solve that is to turn off
the state machines, pre fill some data into

00:07:39.090 --> 00:07:43.000
the FIFO, and start the 2 state machines in
sync.

00:07:43.000 --> 00:07:49.080
Now run the code, the flickery is mostly gone,
and now we have a steady 120Hz.

00:07:49.080 --> 00:07:54.280
Though it displays just fine, the CPU spends
all of its time on displaying the image, with

00:07:54.280 --> 00:07:59.190
no resource for other stuff, like implementing
the actual serial terminal.

00:07:59.190 --> 00:08:04.860
There are 2 solutions, one is to just not
solve it at all, use another CPU core to do

00:08:04.860 --> 00:08:07.580
other stuff.

00:08:07.580 --> 00:08:13.800
Another is to use DMA and interrupt, using
the DMA to send data to the PIO.

00:08:13.800 --> 00:08:19.690
DMA is basically a hardware memory copier,
and the CPU just needs to tell the DMA where

00:08:19.690 --> 00:08:21.040
to copy the data.

00:08:21.040 --> 00:08:23.160
I am going to use the DMA method.

00:08:23.160 --> 00:08:27.470
For the code, configuring the DMA to do a
memory to peripheral copy.

00:08:27.470 --> 00:08:31.930
All the subsequent code could simply use DMA
for copying data.

00:08:31.930 --> 00:08:35.219
Run the code again, and it should still work.

00:08:35.219 --> 00:08:38.399
It’s not done yet as it still eats all the
CPU time.

00:08:38.399 --> 00:08:41.209
This step is just to make sure DMA works.

00:08:41.209 --> 00:08:44.670
Next step is to split stuff into interrupts.

00:08:44.670 --> 00:08:49.650
Divide the refresh function into 3 parts,
function for starting a new line, function

00:08:49.650 --> 00:08:53.730
for starting a new frame, and function for
DMA handler.

00:08:53.730 --> 00:08:59.230
The DMA only transfers one line at a time,
once that’s done, the DMA interrupt is called

00:08:59.230 --> 00:09:00.230
again.

00:09:00.230 --> 00:09:04.290
The synchronization signals are generated
in the DMA interrupt, the code then decides

00:09:04.290 --> 00:09:08.300
to start the next line or start a new frame
based on the current position.

00:09:08.300 --> 00:09:14.420
Finally, add code to enable the interrupt,
and remove the call to the old refresh code,

00:09:14.420 --> 00:09:15.420
done.

00:09:15.420 --> 00:09:19.769
Now I can add other stuff into the main function
without affecting the screen refresh.

00:09:19.769 --> 00:09:25.360
To measure the CPU load, I can add some GPIO
toggling in the interrupt service routine.

00:09:25.360 --> 00:09:29.370
For example, if the IO is high, it’s in
the interrupt, otherwise low.

00:09:29.370 --> 00:09:34.920
Measuring the signal, the duty cycle is about
46%, which means the CPU load is 46%.

00:09:34.920 --> 00:09:40.149
Note this measurement doesn’t include the
interrupt overhead, of the actual load would

00:09:40.149 --> 00:09:41.749
be a tiny bit higher.

00:09:41.749 --> 00:09:48.920
While 48% is much better than the initial
188%, but this is not ideal:

00:09:48.920 --> 00:09:52.350
There is a busy loop in the interrupt to generate
the sync signal

00:09:52.350 --> 00:09:55.639
There is a busy loop in the interrupt to wait
for the PIO to finish.

00:09:55.639 --> 00:10:00.079
DMA only pushes the data into PIO, but not
waiting for the PIO to finish

00:10:00.079 --> 00:10:01.169
EL screen is sensitive to timing, if the CPU
could not service the interrupt in time, it

00:10:01.169 --> 00:10:02.169
could cause display artifacts

00:10:02.169 --> 00:10:06.399
The solution is to let PIO generate a sync
signal alongside with putting out data.

00:10:06.399 --> 00:10:12.339
Since we have 2 state machines here, I would
assign one for Hsync, another for Vsync.

00:10:12.339 --> 00:10:15.110
The overall code looks like this.

00:10:15.110 --> 00:10:20.680
PIO needs to maintain 2 counters, for the
X and Y position, conveniently, using the

00:10:20.680 --> 00:10:22.410
X and Y register.

00:10:22.410 --> 00:10:27.680
The X counter needs to be restarted each line,
so I am saving the initial value into the

00:10:27.680 --> 00:10:32.110
ISR register, and the PIO would reload the
value from that.

00:10:32.110 --> 00:10:38.309
For the first line, both state machines need
to output one line of data, and generate both

00:10:38.309 --> 00:10:39.920
horizontal and vertical sync.

00:10:39.920 --> 00:10:44.769
In the subsequent lines, only data and horizontal
sync are needed.

00:10:44.769 --> 00:10:49.330
Looking at the code, both sides load the initial
counter value, then the second state machine

00:10:49.330 --> 00:10:54.209
would wait for the first one to send an interrupt
bit, then both start outputting data.

00:10:54.209 --> 00:10:57.949
After the first line, they would output the
sync signals.

00:10:57.949 --> 00:11:03.110
The second state machine goes back to the
loop, but the first one continues and goes

00:11:03.110 --> 00:11:04.759
on to the second loop.

00:11:04.759 --> 00:11:09.910
Once it has finished preset number of iterations,
it would send an interrupt to the processor,

00:11:09.910 --> 00:11:11.329
and stop.

00:11:11.329 --> 00:11:14.999
Finally, adding the configuration code, enabling
PIO interrupt.

00:11:14.999 --> 00:11:19.829
In the interrupt, it needs to configure the
DMA, setting the initial value for PIO, and

00:11:19.829 --> 00:11:20.829
restart the PIO.

00:11:20.829 --> 00:11:27.720
Running it now, we could see the interrupt
is only taking 1.5 microseconds of time, and

00:11:27.720 --> 00:11:30.570
it’s being fired 120 times per second.

00:11:30.570 --> 00:11:34.079
This translates to a CPU load of only 0.02%.

00:11:34.079 --> 00:11:37.889
This is a huge improvement.

00:11:37.889 --> 00:11:42.380
While it’s possible to remove this interrupt
entirely, precise vertical interrupts are

00:11:42.380 --> 00:11:47.889
extremely helpful for implementing double
buffering, or implementing greyscale.

00:11:47.889 --> 00:11:52.350
With the display sorted out, the next step
is implementing the serial terminal.

00:11:52.350 --> 00:11:59.470
It might sound easy, which is basically displaying
whatever received from the serial port, and

00:11:59.470 --> 00:12:02.509
sending out whatever user typed to the serial
port.

00:12:02.509 --> 00:12:04.829
So let’s do it.

00:12:04.829 --> 00:12:10.720
Prepare a suitable dot matrix font, write
code for displaying the text, building a software

00:12:10.720 --> 00:12:16.519
FIFO for buffering data received in the serial
interrupt and fetched in the main loop.

00:12:16.519 --> 00:12:22.329
Keyboard wise, RP2040 SDK has built in support
for USB keyboards using the TinyUSB library,

00:12:22.329 --> 00:12:26.819
so it’s just a matter of translating the
keycode into ascii and sending them.

00:12:26.819 --> 00:12:28.519
Easy enough right.

00:12:28.519 --> 00:12:30.209
Unfortunately no.

00:12:30.209 --> 00:12:36.309
Obvious issues, there is no arrow key, function
key, or paging keys in the ASCII.

00:12:36.309 --> 00:12:37.559
How do I send these?

00:12:37.559 --> 00:12:41.379
Then, typically the host needs to send more
than just texts to the terminal.

00:12:41.379 --> 00:12:46.990
It also needs to tell the terminal where to
put the text, what color the text should have,

00:12:46.990 --> 00:12:47.990
etc.

00:12:47.990 --> 00:12:50.899
All these are implemented with escape sequences.

00:12:50.899 --> 00:12:57.449
For example, to move the cursor to position
2,4, one could use a sequence like this to

00:12:57.449 --> 00:12:58.449
implement.

00:12:58.449 --> 00:13:02.389
Host just needs to send this sequence, and
the terminal should parse and do the corresponding

00:13:02.389 --> 00:13:03.860
task.

00:13:03.860 --> 00:13:06.740
There are lot of such sequences.

00:13:06.740 --> 00:13:10.089
I selected and implemented a few supported
by xterm.

00:13:10.089 --> 00:13:14.139
Which also implies there are tons more I didn’t
support.

00:13:14.139 --> 00:13:20.639
Plus there are even more subtle behavior differences
between terminal standards, which could be

00:13:20.639 --> 00:13:23.860
quite daunting to figure out and implement
correctly.

00:13:23.860 --> 00:13:28.339
For example, imagine the host keeps spitting
out texts, until one line has been filled

00:13:28.339 --> 00:13:29.339
up.

00:13:29.339 --> 00:13:33.509
If it keeps printing, then the text would
go to the next line by default.

00:13:33.509 --> 00:13:38.350
But where should the cursor be by the time
it has just been filled up?

00:13:38.350 --> 00:13:42.329
What if the application requests to move the
cursor at this time?

00:13:42.329 --> 00:13:47.329
Does that happen before moving to the next
line, or after?

00:13:47.329 --> 00:13:52.629
To help with debugging and testing, I ended
up porting the entire firmware onto Linux

00:13:52.629 --> 00:13:56.079
and macOS, so it functions as a terminal emulator.

00:13:56.079 --> 00:14:01.730
In conclusion, if one is looking to build
a usable terminal, I will still suggest looking

00:14:01.730 --> 00:14:06.560
into existing libraries, such as libtmt or
libtsm.

00:14:06.560 --> 00:14:10.560
But otherwise, it could be fun to start from
scratch.

00:14:10.560 --> 00:14:13.750
Now let’s run it and see it in action.

00:14:13.750 --> 00:14:17.100
I am connecting it directly to the Raspberry
Pi:

00:14:17.100 --> 00:14:19.949
Like running ping, or running htop

00:14:19.949 --> 00:14:30.730
It’s a serial terminal, so there is no graphical
display

00:14:30.730 --> 00:14:32.690
This is about the end of the video.

00:14:32.690 --> 00:14:33.699
See you next time.

