1717#include "supervisor/port_heap.h"
1818
1919#if defined(DEFAULT_DVI_BUS_CLK_DP )
20- static bool picodvi_autoconstruct_enabled (void ) {
20+ static bool picodvi_autoconstruct_enabled (mp_int_t * default_width , mp_int_t * default_height ) {
2121 char buf [sizeof ("detect" )];
2222 buf [0 ] = 0 ;
2323
@@ -42,6 +42,48 @@ static bool picodvi_autoconstruct_enabled(void) {
4242 return false;
4343 }
4444 bool probed = common_hal_busio_i2c_probe (i2c , 0x50 );
45+ if (probed ) {
46+ uint8_t edid [128 ];
47+ uint8_t out [1 ] = {0 };
48+ common_hal_busio_i2c_write_read (i2c , 0x50 , out , 1 , edid , sizeof (edid ));
49+ bool edid_ok = true;
50+ if (edid [0 ] != 0x00 || edid [1 ] != 0xFF || edid [2 ] != 0xFF || edid [3 ] != 0xFF || edid [4 ] != 0xFF || edid [5 ] != 0xFF || edid [6 ] != 0xFF || edid [7 ] != 0x00 ) {
51+ edid_ok = false;
52+ }
53+ uint8_t checksum = 0 ;
54+ for (size_t i = 0 ; i < sizeof (edid ); i ++ ) {
55+ checksum += edid [i ];
56+ }
57+ if (checksum != 0 ) {
58+ edid_ok = false;
59+ }
60+
61+ if (edid_ok ) {
62+ uint8_t established_timings = edid [35 ];
63+ if ((established_timings & 0xa0 ) == 0 ) {
64+ // Check that 720x400@70Hz or 640x480@60Hz is supported. If not
65+ // and we read EDID ok, then don't autostart.
66+ probed = false;
67+ } else {
68+ size_t offset = 54 ;
69+ uint16_t preferred_pixel_clock = edid [offset ] | (edid [offset + 1 ] << 8 );
70+ if (preferred_pixel_clock != 0 ) {
71+ size_t preferred_width = ((edid [offset + 4 ] & 0xf0 ) << 4 ) | edid [offset + 2 ];
72+ size_t preferred_height = ((edid [offset + 7 ] & 0xf0 ) << 4 ) | edid [offset + 5 ];
73+ // Use 720x400 on 1080p, 4k and 8k displays.
74+ if ((established_timings & 0x80 ) != 0 &&
75+ preferred_width % 1920 == 0 &&
76+ preferred_height % 1080 == 0 ) {
77+ * default_width = 720 / 2 ;
78+ * default_height = 400 / 2 ;
79+ } else {
80+ * default_width = 640 / 2 ;
81+ * default_height = 480 / 2 ;
82+ }
83+ }
84+ }
85+ }
86+ }
4587 common_hal_busio_i2c_unlock (i2c );
4688 return probed ;
4789}
@@ -53,11 +95,13 @@ void picodvi_autoconstruct(void) {
5395 return ;
5496 }
5597
56- if (!picodvi_autoconstruct_enabled ()) {
98+ mp_int_t default_width = 320 ;
99+ mp_int_t default_height = 240 ;
100+ if (!picodvi_autoconstruct_enabled (& default_width , & default_height )) {
57101 return ;
58102 }
59103
60- mp_int_t width = 320 ;
104+ mp_int_t width = default_width ;
61105 mp_int_t height = 0 ;
62106 mp_int_t color_depth = 16 ;
63107 mp_int_t rotation = 0 ;
@@ -75,6 +119,9 @@ void picodvi_autoconstruct(void) {
75119 case 320 :
76120 height = 240 ;
77121 break ;
122+ case 360 :
123+ height = 200 ;
124+ break ;
78125 }
79126 }
80127
@@ -85,8 +132,8 @@ void picodvi_autoconstruct(void) {
85132
86133 if (!common_hal_picodvi_framebuffer_preflight (width , height , color_depth )) {
87134 // invalid configuration, set back to default
88- width = 320 ;
89- height = 240 ;
135+ width = default_width ;
136+ height = default_height ;
90137 color_depth = 16 ;
91138 }
92139
0 commit comments