WEBVTT
Kind: captions
Language: en-US

00:00:00.260 --> 00:00:02.287
Hello everyone, welcome back to my channel.

00:00:02.287 --> 00:00:08.525
Recently, my friend Yukidamayaki-san wants
to use the Touchbar screen for some DIY projects.

00:00:08.525 --> 00:00:12.429
He asked me to help reverse engineer and drive
that long screen.

00:00:12.429 --> 00:00:17.230
I can't find any information about it online,
so I have to do it myself.

00:00:17.230 --> 00:00:21.910
In the previous video I have shown several
methods on tackling screens without datasheet.

00:00:21.910 --> 00:00:26.349
In the first video, I measured the size of
the chip, and counting the number of pixels,

00:00:26.349 --> 00:00:27.870
trying to find a match.

00:00:27.870 --> 00:00:32.759
Then I can use the microscope to look at the
ITO and FPC traces to get the pinout.

00:00:32.759 --> 00:00:37.070
The second video is also using a similar method,
but for that screen I didn't need to know

00:00:37.070 --> 00:00:39.410
the exact driver chip model number.

00:00:39.410 --> 00:00:43.390
So with the help of a same size, the same
resolution and the same controller of the

00:00:43.390 --> 00:00:47.850
screen, I can derive the pinout then driving
that with a universal driver board.

00:00:47.850 --> 00:00:53.260
However, these methods are not applicable
this time, Apple's screen probably has a custom 

00:00:53.260 --> 00:00:57.009
made controller, means I simply can not find
the datasheet.

00:00:57.009 --> 00:01:01.600
This time the idea is actually a bit simpler, 
starting directly from the original MacBook

00:01:01.600 --> 00:01:06.869
Pro, probing the signals on the MacBook Pro
to understand how to control the screen.

00:01:06.869 --> 00:01:10.799
The first thing is the hardware connection,
basically the pinout of the screen.

00:01:10.799 --> 00:01:15.110
One can basically probe on the board to derive
the pinout cause there is not many signal

00:01:15.110 --> 00:01:19.770
to begin with, or cheat a little bit by just
look at the schematics for repairing.

00:01:19.770 --> 00:01:25.369
I'm not going to show it here, but the result
is the design of a breakout board like this.

00:01:25.369 --> 00:01:30.470
You can see that the definition of this screen
is indeed very simple, basically it has a

00:01:30.470 --> 00:01:37.530
3.3V power supply, an 1.8V power supply, one
unused I2C channel and a single lane MIPI

00:01:37.530 --> 00:01:38.530
DSI.

00:01:38.530 --> 00:01:40.530
There is nothing else.

00:01:40.530 --> 00:01:42.130
Next is the software driver.

00:01:42.130 --> 00:01:47.010
Considering that many viewer may not have
played with any MIPI DSI screens before, here

00:01:47.010 --> 00:01:48.010
is a brief introduction.

00:01:48.010 --> 00:01:54.150
The MIPI DSI is a screen interface protocol
commonly used in cell phones and tablet PCs.

00:01:54.150 --> 00:01:59.909
The data rate depends on the version is up
to 1Gb to 4.5Gb per second.

00:01:59.909 --> 00:02:04.329
Similar to the PCIe, it can run multiple lanes
in parallel for higher the bandwidth.

00:02:04.329 --> 00:02:09.200
It should be noted that the MIPI DSI is a
screen interface, not a monitor interface.

00:02:09.200 --> 00:02:15.480
So unlike the eDP, which inherited the DisplayPort's
plug and play feature, the DSI is in some

00:02:15.480 --> 00:02:21.430
sense is a faster SPI + RGB two-in-one interface.

00:02:21.430 --> 00:02:25.129
Plug-and-play is just not quite a thing in
the DSI world.

00:02:25.129 --> 00:02:29.950
The host is required to initialize the screen
before using, and the initializing sequence

00:02:29.950 --> 00:02:32.500
can be different for different screen models.

00:02:32.500 --> 00:02:37.480
The main task in writing the driver is to
know what needs to be sent over during initialization.

00:02:37.480 --> 00:02:39.990
The answer to that is hidden in this MacBook.

00:02:39.990 --> 00:02:46.170
I should be able to just use a logic analyzer
to grab whatever it sents and just use it.

00:02:46.170 --> 00:02:50.980
However, there is a problem here, I do not
have any particularly good logic analyzers

00:02:50.980 --> 00:02:52.000
on hand.

00:02:52.000 --> 00:02:55.849
The best one I have right now is this Digilent
Digital Discovery.

00:02:55.849 --> 00:03:02.340
Although the 100MSa/s sampling rate is pretty
good, it does sounds a bit low compared to

00:03:02.340 --> 00:03:05.290
the gigabits rate on MIPI DSI.

00:03:05.290 --> 00:03:07.260
But that's not entirely hopeless.

00:03:07.260 --> 00:03:11.940
The MIPI DSI has two transmission modes, low-power
mode and high-speed mode.

00:03:11.940 --> 00:03:18.540
The former speed is anything up to 10M, while
the latter speed is from 80M-gigabits.

00:03:18.540 --> 00:03:23.030
Generally speaking, the screen initialization
will be completed in the low-power mode, and

00:03:23.030 --> 00:03:26.080
then switch to the high-speed mode for sending
data.

00:03:26.080 --> 00:03:30.680
If the initialization of this screen is also
done in the low-power mode, then it should

00:03:30.680 --> 00:03:34.500
be easy to see the data with a 100M sampling
rate logic analyzer.

00:03:34.500 --> 00:03:41.070
However, it should be noted that the low-power
mode uses 1.2V signaling, so the logic analyzer

00:03:41.070 --> 00:03:43.930
needs to be able to adjust the threshold level.

00:03:43.930 --> 00:03:45.790
So here I do have some data.

00:03:45.790 --> 00:03:47.410
It seems quite reasonable.

00:03:47.410 --> 00:03:52.209
Because digital discovery software does not
support MIPI decoding, I exported it to a

00:03:52.209 --> 00:03:54.760
csv file and then do the processing later.

00:03:54.760 --> 00:03:59.769
I've imported the waveforms into gtkwave,
so we can take a look at what's going on.

00:03:59.769 --> 00:04:04.019
First of all, there are a lot of chunks of
data here, which is likely transferring the

00:04:04.019 --> 00:04:06.090
pixels on the screen.

00:04:06.090 --> 00:04:12.659
If I measure the interval between these chunks,
it's just under 17ms, which corresponds to

00:04:12.659 --> 00:04:17.040
a refresh rate of around 60Hz, as expected.

00:04:17.040 --> 00:04:21.670
However, if I zoom in, there is nothing there,
which is also as expected.

00:04:21.670 --> 00:04:26.880
The voltage is too low in the high speed mode
for the logic analyzer to see anything.

00:04:26.880 --> 00:04:31.490
Looking at signals here, looks like some data
transmission in the low-power mode.

00:04:31.490 --> 00:04:33.510
But what exactly is it transmitting?

00:04:33.510 --> 00:04:37.620
So it's time to take a look at the MIPI low-power
mode transmission protocol.

00:04:37.620 --> 00:04:43.180
There are two data lines, P and N, which form
a differential pair in the high speed mode,

00:04:43.180 --> 00:04:47.600
but they are two separate single ended signal
lines in low power mode.

00:04:47.600 --> 00:04:49.979
Both lines are high when idle.

00:04:49.979 --> 00:04:54.949
When starting a new transmission, it first
performs this escape mode entry sequence,

00:04:54.949 --> 00:04:57.570
then starting to send data bit by bit.

00:04:57.570 --> 00:05:03.690
There is no clock line, a pulse on the P line
means bit 1, and a pulse on the N line means

00:05:03.690 --> 00:05:05.120
bit 0.

00:05:05.120 --> 00:05:09.810
After the transmission is complete, there
is a stop sequence, where both lines return

00:05:09.810 --> 00:05:10.970
to a high state.

00:05:10.970 --> 00:05:15.430
So I wrote a simple script to implement a
small state machine to decode the data received.

00:05:15.430 --> 00:05:18.830
Run it and here is the received bytes.

00:05:18.830 --> 00:05:20.510
Do they make sense?

00:05:20.510 --> 00:05:25.240
In the low power mode, the first byte sent
indicates the mode entered, and there are

00:05:25.240 --> 00:05:26.660
several possible values.

00:05:26.660 --> 00:05:32.961
The ones received here are all 0x87, which
means that it enters LPDT, Low Power Data

00:05:32.961 --> 00:05:33.970
Transfer mode.

00:05:33.970 --> 00:05:39.110
That is to say, all the packets sent later
are one or more MIPI packets.

00:05:39.110 --> 00:05:43.780
MIPI packets can be categorized into short
packets and long packets.

00:05:43.780 --> 00:05:49.540
Short packets only have 4 bytes of header,
while long packets have 4 bytes of header+data

00:05:49.540 --> 00:05:52.140
payload+2 bytes of checksum.

00:05:52.140 --> 00:05:56.919
The first byte of each packet indicates the
packet type, such as whether it is a command

00:05:56.919 --> 00:06:00.260
or a pixel, a long packet or a short packet.

00:06:00.260 --> 00:06:04.340
For short commands, the command is the two
bytes in the middle of the packet header,

00:06:04.340 --> 00:06:08.880
for long commands, these two bytes indicate
the length, and the data after the header

00:06:08.880 --> 00:06:10.830
is the command content.

00:06:10.830 --> 00:06:15.789
Inside this boot sequence the mac is sending
quite a few commands, some of which are non-standard,

00:06:15.789 --> 00:06:20.710
while others are more straightforward, basically
setting the resolution range and then turning

00:06:20.710 --> 00:06:21.710
on the screen.

00:06:21.710 --> 00:06:26.910
The initialization sequence is fairly short,
in fact the touchbar screen has a dedicated

00:06:26.910 --> 00:06:31.580
SPI flash directly connected to the screen
controller, so it's pulling most of the configuration

00:06:31.580 --> 00:06:36.680
there instead of relying on the host to write
them during boot.

00:06:36.680 --> 00:06:40.979
In terms of implementation, I can use some
hardware MIPI controller, or I can also just

00:06:40.979 --> 00:06:42.889
do GPIO bitbanging.

00:06:42.889 --> 00:06:48.190
I am using this NXP iMXRT1176 microcontroller
evaluation board here.

00:06:48.190 --> 00:06:53.070
The microcontroller has an integrated MIPI
DSI controller, sounds like a good fit.

00:06:53.070 --> 00:06:58.800
If I just try copying and pasting this sequence
directly and then run the code...... the screen

00:06:58.800 --> 00:07:00.229
doesn't seem to be impressed.

00:07:00.229 --> 00:07:02.860
That's strange, what went wrong?

00:07:02.860 --> 00:07:05.530
So I tried GPIO bitbanging, still nothing.

00:07:05.530 --> 00:07:10.850
At this time I realized that after switching
to GPIO bitbanging, I am now only driving

00:07:10.850 --> 00:07:12.669
the data lines not the clock lines.

00:07:12.669 --> 00:07:15.419
They are floating, which is not good.

00:07:15.419 --> 00:07:18.710
I then added two resistors to pull these lines
high.

00:07:18.710 --> 00:07:20.550
Turned out it now works.

00:07:20.550 --> 00:07:24.740
I tried more stuff and finally narrowed down
to the cause, this screen requires the clock

00:07:24.740 --> 00:07:28.680
lane to stay high or inactive during initialization.

00:07:28.680 --> 00:07:34.680
NXP's example code enables the high speed
clock as soon as the screen is out of reset.

00:07:34.680 --> 00:07:38.879
Generally this is okay, but this screen in
particular just doesn't like it.

00:07:38.879 --> 00:07:43.930
Ok now the screen is initializaed, the next
step is to get it to display some image.

00:07:43.930 --> 00:07:48.840
This brings us to the two modes of operation
of the MIPI screen, command mode and video

00:07:48.840 --> 00:07:49.930
mode.

00:07:49.930 --> 00:07:54.509
Command mode is like a common SPI or 8-bit
parallel screen where you send commands to

00:07:54.509 --> 00:07:56.340
control the screen or write pixels.

00:07:56.340 --> 00:08:00.919
The video mode is similar to the controllerless
screen that requires sending video data and

00:08:00.919 --> 00:08:04.210
HV synchronization signals continuously.

00:08:04.210 --> 00:08:09.120
Both are done by sending MIPI packets, but
the type of packets sent is different.

00:08:09.120 --> 00:08:13.370
For example, in the table of packet types,
there are synchronization packet, as well

00:08:13.370 --> 00:08:15.620
as pixel packets of different formats.

00:08:15.620 --> 00:08:17.490
These are used in the video mode.

00:08:17.490 --> 00:08:21.889
Note the difference between low-power and
high-speed modes as mentioned before, MIPI

00:08:21.889 --> 00:08:25.669
commands can be sent in both low-power and
high-speed modes.

00:08:25.669 --> 00:08:29.270
Video should only be sent in high-speed mode
according to the specification.

00:08:29.270 --> 00:08:33.260
Back to the driver code, to use the video
mode that's bascially enabling the hardware

00:08:33.260 --> 00:08:38.490
LCD controller and the DMA, connecting the
LCD controller output to the DSI controller,

00:08:38.490 --> 00:08:40.979
and run the code it...... doesn't work.

00:08:40.979 --> 00:08:47.020
This is not exactly unexpected, the DSI screen
video mode also has several different options,

00:08:47.020 --> 00:08:52.960
such as the clock can always running, but
could also be gated during idle; during the

00:08:52.960 --> 00:08:59.100
blanking period, the controller may send blanking
packets, or null packets, or exit the high-speed

00:08:59.100 --> 00:09:03.600
mode and return to the low-power mode; For
the synchronization, it might only send the

00:09:03.600 --> 00:09:08.709
synchronization start packets but not the
end packets, it could also send both.

00:09:08.709 --> 00:09:14.720
Generally speaking, based on my past experience,
the screen does not support all of these combinations,

00:09:14.720 --> 00:09:17.610
there might be only one or a few that work.

00:09:17.610 --> 00:09:21.959
Also blanking length and the synchronization
position are also a bit tricky, but generally

00:09:21.959 --> 00:09:26.350
getting those wrong resulting in the image
being in the wrong location or just a black

00:09:26.350 --> 00:09:29.420
screen, not like this, which is completely
unresponsive.

00:09:29.420 --> 00:09:34.050
However, after trying all combinations, it
just doesn't do anything.

00:09:34.050 --> 00:09:39.650
One possible guess is that this screen doesn't
support video mode at all, it's a pure command

00:09:39.650 --> 00:09:40.660
mode screen.

00:09:40.660 --> 00:09:46.680
I tried writing pixels with the typical 2C
and 3C pixel write command, yes they do work,

00:09:46.680 --> 00:09:50.360
but I just want a confirmation that this is
indeed the case.

00:09:50.360 --> 00:09:52.610
Turning back to this Macbook.

00:09:52.610 --> 00:09:58.150
As mentioned earlier, I can't see the signals
in high-speed mode with a 100M-sample logic

00:09:58.150 --> 00:10:00.960
analyzer, so I'll take a look with this 2G-sample
oscilloscope.

00:10:00.960 --> 00:10:06.970
However, due to the lack of better differential
probes, combined with the toally not impedance

00:10:06.970 --> 00:10:10.209
matched bodge wires, the results aren't the
best.

00:10:10.209 --> 00:10:16.240
I can only say that I hope that this relatively
bad signal can still tell me which command

00:10:16.240 --> 00:10:17.240
was sent.

00:10:17.240 --> 00:10:22.310
Similarly I exported the waveform to a csv
file, then wrote a script to decode the data.

00:10:22.310 --> 00:10:28.810
MIPI in th high-speed mode first has a synchronization
sequence, after which is the transmitted data.

00:10:28.810 --> 00:10:33.400
The definition of the packet is exactly the
same as in the low power mode.

00:10:33.400 --> 00:10:37.000
This synchronization sequence is evident from
the waveform here.

00:10:37.000 --> 00:10:42.019
It is true that the error rate is quite higher
here, so the results isn't exactly reliable.

00:10:42.019 --> 00:10:47.950
Here it seems that if these packet headers
are to be believed, what is being sent in

00:10:47.950 --> 00:10:51.740
high-speed mode is not a video mode packet,
but is still commands.

00:10:51.740 --> 00:10:56.139
The command is exactly 0x3C, which is continous
pixel write.

00:10:56.139 --> 00:11:01.079
This basically confirms my previous guess
that this is a pure command mode screen, and

00:11:01.079 --> 00:11:04.700
in high speed mode it is just sending more
command faster.

00:11:04.700 --> 00:11:08.839
So I modified the code to let it send 3C in
high speed mode, and it's done.

00:11:08.839 --> 00:11:14.830
However, a small issue is that the iMXRT LCD
controller does not natively support pure

00:11:14.830 --> 00:11:20.649
command mode DSI screen, the integrated video
DMA in this case is kind of useless.

00:11:20.649 --> 00:11:25.300
But of course I can still use general purpose
DMA with some glue code.

00:11:25.300 --> 00:11:29.779
This issue may also prevent this screen to
be used with other chips with MIPI interfaces

00:11:29.779 --> 00:11:32.230
that only support video mode screens.

00:11:32.230 --> 00:11:34.330
So that's it for this video, see you next
time!

