You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

365 lines
16 KiB

  1. < [Known issues](known_issues.md) | [Index](../README.md) | [Sponsoring](sponsoring.md) >
  2. # Technical details
  3. In this section, you can find some of the information that was gathered
  4. during the reverse engineering of the Bedside Lamp 2 hardware.
  5. Table of contents:
  6. * [High level overview](#high-level-overview)
  7. * [ESP32 pinout](#esp32-pinout)
  8. * [Front panel](#front-panel)
  9. * [Build requirements](#build-requirements)
  10. * [Original firmware](#original-firmware)
  11. ## High level overview
  12. No documentation is complete without some ASCII art schematics.
  13. ```
  14. RX/TX/GND for
  15. 12V power supply flashing and logs
  16. | |
  17. v | Front panel
  18. +---------------+ +---------------+ .---.
  19. | Power supply |---- 3.3V -.----->| ESP-WROOM-32D |<- I2C ->| O | -- color
  20. +---------------+ \ | single core | | | button
  21. | \ | 4 MB flash |<- IRQ --| | |
  22. 12V \ +---------------+ | | |
  23. | `-------|------------------->| | | -- slider
  24. v | | | |
  25. +---------------+ | | | |
  26. | RGB and white |<---- RGBW + master ---+ | | power
  27. | LED circuitry | PWM on/off | O | -- button
  28. +---------------+ `---`
  29. ```
  30. The LED circuitry provides two light modes:
  31. * Colored RGB light;
  32. * Warm to cool white light.
  33. The front panel of the device contains a two touch buttons (power on/off and
  34. color selection) and a touch slider (for setting the brightness level). This
  35. panel is lit when the device is turned on. The light behind the slider will
  36. represent the actual brightness setting of the device.
  37. ## ESP32 pinout
  38. In the following image, you can find the pinout as used for the ESP32:
  39. <img src="images/hardware/ESP32_pinout.jpg" width="600">
  40. Here's an overview of all exposed pins of the chip, starting at the GND +
  41. 3.3V pins, and going anti-clockwise. The table shows not only the functions
  42. of the pins that are actually in use by the lamp's circuitry, but also the
  43. pins that are not in use and their possible use.
  44. | PIN | GPIO# | Function | Description | Possible use |
  45. |------|--------|-----------|--------------------------------|--------------|
  46. | GND | | Ground | Connected to ground | - |
  47. | 3.3V | | Power | Power supply input | - |
  48. | 9 | | Reset | Can be pulled to GND to reset | - |
  49. | 5 | GPIO36 | - | | IN |
  50. | 8 | GPIO39 | - | | IN |
  51. | 10 | GPIO34 | - | | IN |
  52. | 11 | GPIO35 | - | | IN |
  53. | 12 | GPIO32 | - | | IN/OUT |
  54. | 13 | GPIO33 | LEDs | LEDs, master switch 1 | - |
  55. | 14 | GPIO25 | ??? | 10k pull up, unknown function | IN/OUT (1) |
  56. | 15 | GPIO26 | - | | IN/OUT |
  57. | 16 | GPIO27 | - | | IN/OUT |
  58. | 17 | GPIO14 | LEDs | LEDs, green PWM channel | - |
  59. | 18 | GPIO12 | LEDs | LEDs, white PWM channel | - |
  60. | GND | | Ground | Connected to ground | - |
  61. | 20 | GPIO13 | LEDs | LEDs, red PWM channel | - |
  62. | 28 | GPIO9 | SPI | SPI flash memory | - |
  63. | 29 | GPIO10 | SPI | SPI flash memory | - |
  64. | 30 | GPIO11 | SPI | SPI flash memory | - |
  65. | 31 | GPIO6 | SPI | SPI flash memory | - |
  66. | 32 | GPIO7 | SPI | SPI flash memory | - |
  67. | 33 | GPIO8 | SPI | SPI flash memory | - |
  68. | 21 | GPIO15 | - | | IN/OUT (2) |
  69. | 22 | GPIO2 | ??? | Debug pad, unknown function | IN/OUT (3) |
  70. | 23 | GPIO0 | Boot mode | Pull to GND for flashing mode | - |
  71. | 24 | GPIO4 | LEDs | LEDs, master switch 2 | - |
  72. | 25 | GPIO16 | Front pnl | Front panel interrupt | - |
  73. | 27 | GPIO17 | EEPROM | EEPROM I2C SDA (4) | - |
  74. | 34 | GPIO5 | LEDs | LEDs, blue PWM channel | - |
  75. | 35 | GPIO18 | EEPROM | EEPROM I2C CLK (4) | - |
  76. | 38 | GPIO19 | Front pnl | Front panel I2C SCL | - |
  77. | N/C | | | | |
  78. | 42 | GPIO21 | Front pnl | Front panel I2C SDA | - |
  79. | 40 | GPIO3 | Serial | Debug pad, RX (flashing, logs) | - |
  80. | 41 | GPIO1 | Serial | Debug pad, TX (flashing, logs) | - |
  81. | 39 | GPIO22 | - | | IN/OUT |
  82. | 36 | GPIO23 | - | | IN/OUT |
  83. | GND | | Ground | Connected to ground | - |
  84. 1. GPIO25 is connected to a 10k pull up resistor. This suggests that it
  85. might have some function in the lamp, but I have not found that function
  86. yet. If you find the actual use for this pin, or find that you can indeed
  87. repurpose it, then please let me know.
  88. 1. Beware that GPIO15 outputs a PWM signal at boot. This might make the pin
  89. less useful for your use case.
  90. 1. Often, GPIO2 is used for an on-board LED, but it looks like it is only
  91. connected to the debug pad here. I think the pin is usable, and that it
  92. might only be used for testing purposes in the original firmware.
  93. 1. The connected IC, using I2C address 0x10, looks a lot like an EEPROM,
  94. but this has yet to be confirmed. It uses a decicated I2C bus, separate
  95. from the I2C bus of the front panel.
  96. [This picture](images/hardware/IC_on_I2C_GPIO1718.jpg) shows the IC.
  97. For more information on the use of pins on the ESP32 chip, please check out
  98. this [ESP32 pinout reference information](https://randomnerdtutorials.com/esp32-pinout-reference-gpios/).
  99. ## Front panel
  100. <img src="images/hardware/front_panel.jpg" width="150">
  101. The front panel is a stand-alone component, with its own control chip
  102. (KungFu KF8TS2716). Communication between the ESP32 and the front panel
  103. is done using:
  104. - **An I2C bus**
  105. - the front panel is the I2C slave, the ESP32 is the I2C master
  106. (pardon the standard terminology, I am aware of the controversy)
  107. - the front panel device ID is 0x2C
  108. - SDA is connected to ESP32 pin GPIO21
  109. - SCL is connected to ESP32 pin GPIO19
  110. - **An interrupt data line to signal the ESP32 about new events**
  111. - this line is connected to ESP32 pin GPIO16
  112. - the default state is HIGH
  113. - line is pulled LOW for at least 6 ms when a new event is available
  114. Commands can be written to and data can be read from the front panel
  115. component using I2C. The I2C protocol is fairly simple. All read and write
  116. operations uses 6 bytes of data. No register selection is done before
  117. reading or writing.
  118. The interrupt data line is used by the front panel, to signal the ESP32 that
  119. a new button or slider event is available. Further details on this can be
  120. found below.
  121. **Connection to the main board**
  122. The front panel is connected to the main board using a flat cable.
  123. The picture below shows the connector on the main board, including the
  124. functions of the cable pins:
  125. <img src="images/hardware/front_panel_flat_cable_connection.jpg" width="400">
  126. **Writing commands to the front panel**
  127. Commands can be written to the front panel at any time.
  128. The available commands are:
  129. | Command | Byte sequence to send |
  130. |-----------------|-----------------------|
  131. | TURN PANEL ON | 02 03 5E 00 64 00 00 |
  132. | TURN PANEL OFF | 02 03 0C 00 64 00 00 |
  133. | SET LEVEL 1 | 02 03 5E 00 64 00 00 |
  134. | SET LEVEL 2 | 02 03 5F 00 64 00 00 |
  135. | SET LEVEL 3 | 02 03 5F 80 64 00 00 |
  136. | SET LEVEL 4 | 02 03 5F C0 64 00 00 |
  137. | SET LEVEL 5 | 02 03 5F E0 64 00 00 |
  138. | SET LEVEL 6 | 02 03 5F F0 64 00 00 |
  139. | SET LEVEL 7 | 02 03 5F F8 64 00 00 |
  140. | SET LEVEL 8 | 02 03 5F FC 64 00 00 |
  141. | SET LEVEL 9 | 02 03 5F FE 64 00 00 |
  142. | SET LEVEL 10 | 02 03 5F FF 64 00 00 |
  143. | READY FOR EVENT | 01 00 00 00 00 00 01 |
  144. *Note: The `READY FOR EVENT` command is only used when a new event is provided
  145. by the front panel. Information about this command can be found in the next
  146. section.*
  147. **Reading events from the front panel**
  148. The types of events that can occur can be summarized as:
  149. - Touch or release the power button
  150. - Touch or release the color button
  151. - Touch or release the slider at a certain level
  152. Because the front panel is an I2C slave device, it cannot contact the ESP32 via
  153. I2C. Only an I2C master device can initiate communication. Therefore, when the
  154. front panel has a new event available, it will pull down the interrupt line for
  155. a short period of time, to signal the ESP32 about this new event.
  156. *Note that the ESP32 needs to poll the interrupt line at least at 667 Hz to be
  157. able to trustworthy detect the 6 ms signal. Unfortunately, the interrupt line
  158. does not wait for the ESP32 to respond to its signalling. The best way to
  159. handle signals from this line, is to use an actual interrupt handler.*
  160. After detecting this signal, the ESP32 must first write the "READY FOR EVENT"
  161. command (`01 00 00 00 00 00 01`) via I2C to the front panel.
  162. After the front panel has ACK'ed this command, the ESP32 can read 6 bytes,
  163. which will represent the event that occurred.
  164. Here's the mapping for the events and their corresponding byte sequences:
  165. | | Touch event | Release event |
  166. |-----------------|----------------------|----------------------|
  167. | POWER BUTTON | 04 04 01 00 01 01 03 | 04 04 01 00 01 02 04 |
  168. | COLOR BUTTON | 04 04 01 00 02 01 04 | 04 04 01 00 02 02 05 |
  169. | SLIDER LEVEL 1 | 04 04 01 00 03 16 1A | 04 04 01 00 04 16 1B |
  170. | SLIDER LEVEL 2 | 04 04 01 00 03 15 19 | 04 04 01 00 04 15 1A |
  171. | SLIDER LEVEL 3 | 04 04 01 00 03 14 18 | 04 04 01 00 04 14 19 |
  172. | SLIDER LEVEL 4 | 04 04 01 00 03 13 17 | 04 04 01 00 04 13 18 |
  173. | SLIDER LEVEL 5 | 04 04 01 00 03 12 16 | 04 04 01 00 04 12 17 |
  174. | SLIDER LEVEL 6 | 04 04 01 00 03 11 15 | 04 04 01 00 04 11 16 |
  175. | SLIDER LEVEL 7 | 04 04 01 00 03 10 14 | 04 04 01 00 04 10 15 |
  176. | SLIDER LEVEL 8 | 04 04 01 00 03 0F 13 | 04 04 01 00 04 0F 14 |
  177. | SLIDER LEVEL 9 | 04 04 01 00 03 0E 12 | 04 04 01 00 04 0E 13 |
  178. | SLIDER LEVEL 10 | 04 04 01 00 03 0D 11 | 04 04 01 00 04 0D 12 |
  179. | SLIDER LEVEL 11 | 04 04 01 00 03 0C 10 | 04 04 01 00 04 0C 11 |
  180. | SLIDER LEVEL 12 | 04 04 01 00 03 0B 0F | 04 04 01 00 04 0B 10 |
  181. | SLIDER LEVEL 13 | 04 04 01 00 03 0A 0E | 04 04 01 00 04 0A 0F |
  182. | SLIDER LEVEL 14 | 04 04 01 00 03 09 0D | 04 04 01 00 04 09 0E |
  183. | SLIDER LEVEL 15 | 04 04 01 00 03 08 0C | 04 04 01 00 04 08 0D |
  184. | SLIDER LEVEL 16 | 04 04 01 00 03 07 0B | 04 04 01 00 04 07 0C |
  185. | SLIDER LEVEL 17 | 04 04 01 00 03 06 0A | 04 04 01 00 04 06 0B |
  186. | SLIDER LEVEL 18 | 04 04 01 00 03 05 09 | 04 04 01 00 04 05 0A |
  187. | SLIDER LEVEL 19 | 04 04 01 00 03 04 08 | 04 04 01 00 04 04 09 |
  188. | SLIDER LEVEL 20 | 04 04 01 00 03 03 07 | 04 04 01 00 04 03 08 |
  189. | SLIDER LEVEL 21 | 04 04 01 00 03 02 06 | 04 04 01 00 04 02 07 |
  190. | SLIDER LEVEL 22 | 04 04 01 00 03 01 05 | 04 04 01 00 04 01 06 |
  191. **Behavior when more events come in than can be handled**
  192. The front panel does not queue events. When a new event occurs, before the
  193. previous event has be read by the ESP32, the new event will replace the old
  194. event and a new signal is sent over the interrupt line.
  195. The ESP32 can read the last event multiple times. It will not be cleared
  196. by the front panel after reading it.
  197. ## Build requirements
  198. The ESP-WROOM-32D that is used for this lamp (and for various other Xiaomi devices),
  199. contains a single core CPU, even though the data sheet for ESP-WROOM-32D specifies
  200. a dual core CPU. Therefore, when flashing the device with a generic ESP32 build,
  201. you will end up with the following boot error:
  202. ```
  203. E (459) cpu_start: Running on single core chip, but application is built with dual core support.
  204. E (459) cpu_start: Please enable CONFIG_FREERTOS_UNICORE option in menuconfig.
  205. ```
  206. Another issue with a lot of these devices, is that the MAC address that is burnt into
  207. EFUSE does not match the CRC checksum that is also burnt into EFUSE. Using a generic
  208. ESP32 build, you will end up with the boot error:
  209. ```
  210. Base MAC address from BLK0 of EFUSE CRC error
  211. ```
  212. For these reasons, you must build the firmware using a taylored version of arduino-esp32.
  213. You can make use of the version [created by @pauln](https://github.com/pauln/arduino-esp32)
  214. or the version [created by @mmakaay](https://github.com/mmakaay/arduino-esp32-unicore-no-mac-crc).
  215. To make use of one of these in an ESPHome build, you'll have to provide a platform package
  216. definition for the PlatformIO build. Here's an example configuration that will work for
  217. these Xiaomi devices:
  218. ```yaml
  219. esphome:
  220. name: my_device_name
  221. platform: ESP32
  222. board: esp32doit-devkit-v1
  223. platformio_options:
  224. platform: espressif32@3.2.0
  225. platform_packages: |-
  226. framework-arduinoespressif32 @ https://github.com/mmakaay/arduino-esp32-unicore-no-mac-crc
  227. ```
  228. If you want to build your own platform package, then you can checkout
  229. the build scripts [by @mmakaay here](https://github.com/mmakaay/arduino-esp32-unicore-no-mac-crc-builder).
  230. ## Original firmware
  231. Below, I have gathered some of the interesting boot messages from the
  232. original firmware. These messages are logged via the serial interface.
  233. **SPI Flash memory:**
  234. ```
  235. boot: SPI Flash RID : 0xB20B4
  236. boot: SPI Flash MF : 0xB4
  237. boot: SPI Flash ID : 0x200B
  238. boot: SPI Speed : 40MHz
  239. boot: SPI Mode : DIO
  240. boot: SPI Flash Size : 4MB
  241. ```
  242. **Partition table:**
  243. ```
  244. boot: Partition Table:
  245. boot: ## Label Usage Type ST Offset Length
  246. boot: 0 nvs WiFi data 01 02 00009000 00004000
  247. boot: 1 otadata OTA data 01 00 0000d000 00002000
  248. boot: 2 phy_init RF data 01 01 0000f000 00001000
  249. boot: 3 miio_fw1 OTA app 00 10 00010000 001e0000
  250. boot: 4 miio_fw2 OTA app 00 11 001f0000 001e0000
  251. boot: 5 test test app 00 20 003d0000 00013000
  252. boot: 6 mfi_p Unknown data 01 82 003e3000 00001000
  253. boot: 7 factory_nvs WiFi data 01 02 003e4000 00004000
  254. boot: 8 coredump Unknown data 01 03 003e8000 00010000
  255. boot: 9 minvs Unknown data 01 fe 003f8000 00004000
  256. boot: End of partition table
  257. ```
  258. **MIIO initialization:**
  259. ```
  260. _| _| _|_|_| _|_|_| _|_|
  261. _|_| _|_| _| _| _| _|
  262. _| _| _| _| _| _| _|
  263. _| _| _| _| _| _|
  264. _| _| _|_|_| _|_|_| _|_|
  265. 08:00:00.200 [I] did=332985470 hostname=MiBedsideLamp2-7651
  266. JENKINS BUILD NUMBER: N/A
  267. BUILD TIME: Sep 5 2019,07:12:39
  268. BUILT BY: N/A
  269. MIIO APP VER: 2.0.6_0030
  270. Setup ID: 95XJ
  271. Getting setup info from factory NVS
  272. MIIO MCU VER:
  273. MIIO DID: *********
  274. MIIO WIFI MAC: ************
  275. MIIO MODEL: yeelink.light.bslamp2
  276. ARCH TYPE: esp32,0x0000a601
  277. ARCH VER: d178b9b
  278. ```
  279. **Network initialized:**
  280. ```
  281. [20:27:05]08:00:04.180 [I] miio_net: Wifi station connected
  282. [20:27:05]Registering HomeKit web handlers
  283. [20:27:05]Announcing _hap._tcp mDNS service
  284. ```
  285. **Phoning home to the Mijia cloud:**
  286. ```
  287. ots: httpdns resolve start failed, -12 (ots_cloud_host_update,850)
  288. otu: Opened.
  289. ots: de.ots.io.mi.com resolved to 3.126.247.75.
  290. ots: ots connect 3.126.247.75::443...
  291. tls: connect to server Mijia Cloud, domain is 3.126.247.75, port is 443.
  292. tls: timeout[100]! mbedtls_ssl_handshake returned -0x6800 (d0_tls_open,369)
  293. tls: timeout[200]! mbedtls_ssl_handshake returned -0x6800 (d0_tls_open,369)
  294. tls: timeout[300]! mbedtls_ssl_handshake returned -0x6800 (d0_tls_open,369)
  295. tls: timeout[400]! mbedtls_ssl_handshake returned -0x6800 (d0_tls_open,369)
  296. ots: Connected.
  297. ```
  298. < [Known issues](known_issues.md) | [Index](../README.md) | [Sponsoring](sponsoring.md) >