enet_1.0.0_54410889/Ada_Drivers_Library/boards/stm32_common/otm8009a/framebuffer_dsi.adb

  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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
------------------------------------------------------------------------------
--                                                                          --
--                     Copyright (C) 2015-2017, AdaCore                     --
--                                                                          --
--  Redistribution and use in source and binary forms, with or without      --
--  modification, are permitted provided that the following conditions are  --
--  met:                                                                    --
--     1. Redistributions of source code must retain the above copyright    --
--        notice, this list of conditions and the following disclaimer.     --
--     2. Redistributions in binary form must reproduce the above copyright --
--        notice, this list of conditions and the following disclaimer in   --
--        the documentation and/or other materials provided with the        --
--        distribution.                                                     --
--     3. Neither the name of the copyright holder nor the names of its     --
--        contributors may be used to endorse or promote products derived   --
--        from this software without specific prior written permission.     --
--                                                                          --
--   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS    --
--   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT      --
--   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  --
--   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT   --
--   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
--   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT       --
--   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  --
--   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  --
--   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT    --
--   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  --
--   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Real_Time;        use Ada.Real_Time;
with Ada.Unchecked_Conversion;
with Ada.Interrupts.Names;

with Cortex_M.Cache;

with STM32.Device;         use STM32.Device;
with STM32.DMA2D.Interrupt;
with STM32.DMA2D.Polling;
with STM32.GPIO;           use STM32.GPIO;
with STM32.DSI;            use STM32.DSI;
with STM32.SDRAM;          use STM32.SDRAM;

with STM32_SVD.DSI;        use STM32_SVD.DSI;
with STM32_SVD.RCC;        use STM32_SVD.RCC;

package body Framebuffer_DSI is

   protected Sync is
      entry Wait;
      procedure Do_Refresh;

   private
      procedure End_Of_Refresh_Callback;
      procedure Tearing_Effect_Callback;
      procedure Interrupt
        with Attach_Handler => Ada.Interrupts.Names.DSI_Interrupt,
             Unreferenced;

      Refreshed   : Boolean := True;
   end Sync;

   procedure LCD_Reset (Reset : in out GPIO_Point);
   procedure Initialize_Device (Display : in out Frame_Buffer);

   ----------
   -- Sync --
   ----------

   protected body Sync is
      ----------
      -- Wait --
      ----------

      entry Wait when Refreshed is
      begin
         null;
      end Wait;

      ----------------
      -- Do_Refresh --
      ----------------

      procedure Do_Refresh is
      begin
         Refreshed := False;
         --  Enable the end of refresh interrupt
         DSIHOST.DSI_Refresh;
      end Do_Refresh;

      -----------------------------
      -- End_Of_Refresh_Callback --
      -----------------------------

      procedure End_Of_Refresh_Callback
      is
      begin
         Refreshed := True;
      end End_Of_Refresh_Callback;

      -----------------------------
      -- Tearing_Effect_Callback --
      -----------------------------

      procedure Tearing_Effect_Callback
      is
      begin
         null;
      end Tearing_Effect_Callback;

      ---------------
      -- Interrupt --
      ---------------

      procedure Interrupt
      is
      begin
         if DSI_Periph.DSI_WISR.ERIF then
            DSI_Periph.DSI_WIFCR.CERIF := True;
            End_Of_Refresh_Callback;
         end if;

         if DSI_Periph.DSI_WISR.TEIF then
            DSI_Periph.DSI_WIFCR.CTEIF := True;
            Tearing_Effect_Callback;
         end if;
      end Interrupt;

   end Sync;

   ---------------
   -- LCD_Reset --
   ---------------

   procedure LCD_Reset (Reset : in out GPIO_Point) is
   begin
      Enable_Clock (Reset);
      Configure_IO (Reset,
                    (Mode        => Mode_Out,
                     Output_Type => Push_Pull,
                     Speed       => Speed_50MHz,
                     Resistors   => Pull_Up));

      --  Activate XRES active low
      Clear (Reset);

      delay until Clock + Milliseconds (20);

      Set (Reset);

      delay until Clock + Milliseconds (10);
   end LCD_Reset;

   ----------------
   -- Max_Layers --
   ----------------

   overriding function Max_Layers
     (Display : Frame_Buffer) return Positive
   is
      pragma Unreferenced (Display);
   begin
      return 2;
   end Max_Layers;

   ---------------
   -- Supported --
   ---------------

   overriding function Supported
     (Display : Frame_Buffer;
      Mode    : HAL.Framebuffer.FB_Color_Mode) return Boolean
   is
      pragma Unreferenced (Display, Mode);
   begin
      --  The LTDC supports all HAL color modes
      return True;
   end Supported;

   -----------
   -- Width --
   -----------

   overriding function Width
     (Display : Frame_Buffer) return Positive
   is
   begin
      if not Display.Swapped then
         return LCD_Natural_Width;
      else
         return LCD_Natural_Height;
      end if;
   end Width;

   ------------
   -- Height --
   ------------

   overriding function Height
     (Display : Frame_Buffer) return Positive
   is
   begin
      if not Display.Swapped then
         return LCD_Natural_Height;
      else
         return LCD_Natural_Width;
      end if;
   end Height;

   -------------
   -- Swapped --
   -------------

   overriding function Swapped
     (Display : Frame_Buffer) return Boolean
   is
      pragma Unreferenced (Display);
   begin
      --  The display supports natively the swap of the X/Y coordinates.
      --  So to the outside world (e.g. bitmap operations), the buffer always
      --  has to be treated as 'native orientation'
      return False;
   end Swapped;

   --------------------
   -- Set_Background --
   --------------------

   overriding procedure Set_Background
     (Display : Frame_Buffer; R, G, B : UInt8)
   is
      pragma Unreferenced (Display);
   begin
      DSIHOST.DSI_Wrapper_Disable;
      STM32.LTDC.Set_Background (R, G, B);
      DSIHOST.DSI_Wrapper_Enable;
   end Set_Background;

   -----------------------
   -- Initialize_Device --
   -----------------------

   procedure Initialize_Device (Display : in out Frame_Buffer)
   is
   begin

      DSIHOST.DSI_Deinit;

      --  HSE input: 25MHz / IN_Div * N_Div => 1000 MHz = VCO
      --  VCO / ODF => 500 MHz
      DSIHOST.DSI_Initialize
        (Auto_Clock_Lane_Control  => True,
         TX_Escape_Clock_Division => 4, -- 62500 / 4 = 15625 kHz < 20kHz (max)
         Number_Of_Lanes          => Two_Data_Lanes,
         PLL_N_Div                => Display.PLL_N_Div,
         PLL_IN_Div               => Display.PLL_IN_Div,
         PLL_OUT_Div              => Display.PLL_OUT_Div);

      DSIHOST.DSI_Setup_Adapted_Command_Mode
        (Virtual_Channel         => LCD_Channel,
         Color_Coding            => STM32.DSI.RGB888,
         Command_Size            => UInt16 (Display.Width),
         Tearing_Effect_Source   => STM32.DSI.TE_DSI_Link,
         Tearing_Effect_Polarity => STM32.DSI.Rising_Edge,
         HSync_Polarity          => STM32.DSI.Active_High,
         VSync_Polarity          => STM32.DSI.Active_High,
         DataEn_Polarity         => STM32.DSI.Active_High,
         VSync_Edge              => STM32.DSI.Falling_Edge,
         Automatic_Refresh       => False,
         TE_Acknowledge_Request  => True);

      DSIHOST.DSI_Setup_Command
        (LP_Gen_Short_Write_No_P  => True,
         LP_Gen_Short_Write_One_P => True,
         LP_Gen_Short_Write_Two_P => True,
         LP_Gen_Short_Read_No_P   => True,
         LP_Gen_Short_Read_One_P  => True,
         LP_Gen_Short_Read_Two_P  => True,
         LP_Gen_Long_Write        => True,
         LP_DCS_Short_Write_No_P  => True,
         LP_DCS_Short_Write_One_P => True,
         LP_DCS_Short_Read_No_P   => True,
         LP_DCS_Long_Write        => True,
         LP_Max_Read_Packet       => True,
         Acknowledge_Request      => False);

      STM32.LTDC.Initialize
        (Width         => Display.Width,
         Height        => Display.Height,
         H_Sync        => 2,
         H_Back_Porch  => 1,
         H_Front_Porch => 1,
         V_Sync        => 2,
         V_Back_Porch  => 1,
         V_Front_Porch => 1,
         PLLSAI_N      => Display.PLLSAIN,
         PLLSAI_R      => Display.PLLSAIR,
         DivR          => Display.PLLSAI_DIVR);

      --  Enable the DSI Host and Wrapper
      DSIHOST.DSI_Start;

      --  LCD panel init
      Display.Device.Initialize
        (OTM8009A.RGB888,
         (if Display.Swapped then OTM8009A.Portrait else OTM8009A.Landscape));

      DSIHOST.DSI_Setup_Command
        (LP_Gen_Short_Write_No_P  => False,
         LP_Gen_Short_Write_One_P => False,
         LP_Gen_Short_Write_Two_P => False,
         LP_Gen_Short_Read_No_P   => False,
         LP_Gen_Short_Read_One_P  => False,
         LP_Gen_Short_Read_Two_P  => False,
         LP_Gen_Long_Write        => False,
         LP_DCS_Short_Write_No_P  => False,
         LP_DCS_Short_Write_One_P => False,
         LP_DCS_Short_Read_No_P   => False,
         LP_DCS_Long_Write        => False,
         LP_Max_Read_Packet       => False,
         Acknowledge_Request      => False);

      DSIHOST.DSI_Setup_Flow_Control (Flow_Control_BTA);

      DSIHOST.DSI_Refresh;
   end Initialize_Device;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize
     (Display     : in out Frame_Buffer;
      Orientation : HAL.Framebuffer.Display_Orientation := Default;
      Mode        : HAL.Framebuffer.Wait_Mode := Interrupt;
      Reset_Point : in out GPIO_Point;
      PLLSAI_N    : UInt9;
      PLLSAI_R    : UInt3;
      DivR        : Natural;
      PLL_N_Div   : DSI_PLLN_Div;
      PLL_IN_Div  : DSI_PLL_IDF;
      PLL_OUT_Div : DSI_PLL_ODF)
   is
   begin
      LCD_Reset (Reset_Point);

      --  Init clocks on DSI, LTDC and DMA2D
      RCC_Periph.APB2ENR.LTDCEN := True;
      RCC_Periph.APB2RSTR.LTDCRST := True;
      RCC_Periph.APB2RSTR.LTDCRST := False;

      RCC_Periph.AHB1ENR.DMA2DEN := True;
      RCC_Periph.AHB1RSTR.DMA2DRST := True;
      RCC_Periph.AHB1RSTR.DMA2DRST := False;

      RCC_Periph.APB2ENR.DSIEN := True;
      RCC_Periph.APB2RSTR.DSIRST := True;
      RCC_Periph.APB2RSTR.DSIRST := False;

      --  Make sure the SDRAM is enabled
      STM32.SDRAM.Initialize;

      if Orientation = Portrait then
         Display.Swapped := True;
      end if;

      Display.PLLSAIN     := PLLSAI_N;
      Display.PLLSAIR     := PLLSAI_R;
      Display.PLLSAI_DIVR := DivR;
      Display.PLL_N_Div   := PLL_N_Div;
      Display.PLL_IN_Div  := PLL_IN_Div;
      Display.PLL_OUT_Div := PLL_OUT_Div;

      Display.Initialize_Device;

      case Mode is
         when Polling =>
            STM32.DMA2D.Polling.Initialize;
         when Interrupt =>
            STM32.DMA2D.Interrupt.Initialize;
      end case;
   end Initialize;

   -----------------
   -- Initialized --
   -----------------

   overriding function Initialized
     (Display : Frame_Buffer) return Boolean
   is
      pragma Unreferenced (Display);
   begin
      return STM32.LTDC.Initialized;
   end Initialized;

   ---------------------
   -- Set_Orientation --
   ---------------------

   overriding procedure Set_Orientation
     (Display     : in out Frame_Buffer;
      Orientation : HAL.Framebuffer.Display_Orientation)
   is
      Old : constant Boolean := Display.Swapped;
   begin
      Display.Swapped := Orientation = Portrait;

      if Old = Display.Swapped then
         return;
      end if;

      Initialize_Device (Display);
   end Set_Orientation;

   --------------
   -- Set_Mode --
   --------------

   overriding procedure Set_Mode
     (Display : in out Frame_Buffer;
      Mode    : HAL.Framebuffer.Wait_Mode)
   is
      pragma Unreferenced (Display);
   begin
      case Mode is
         when Polling =>
            STM32.DMA2D.Polling.Initialize;
         when Interrupt =>
            STM32.DMA2D.Interrupt.Initialize;
      end case;
   end Set_Mode;

   ----------------------
   -- Initialize_Layer --
   ----------------------

   overriding procedure Initialize_Layer
     (Display : in out Frame_Buffer;
      Layer   : Positive;
      Mode    : HAL.Framebuffer.FB_Color_Mode;
      X       : Natural := 0;
      Y       : Natural := 0;
      Width   : Positive := Positive'Last;
      Height  : Positive := Positive'Last)
   is
      LCD_Layer : constant STM32.LTDC.LCD_Layer :=
                    (if Layer = 1
                     then STM32.LTDC.Layer1
                     else STM32.LTDC.Layer2);
      W         : Natural := Width;
      H         : Natural := Height;

   begin
      if X >= Display.Width then
         raise Constraint_Error with "Layer X position outside of screen";
      elsif Y >= Display.Height then
         raise Constraint_Error with "Layer Y position outside of screen";
      end if;

      if W = Positive'Last or else X + W > Display.Width then
         W := Display.Width - X;
      end if;

      if H = Positive'Last or else Y + H > Display.Height then
         H := Display.Height - Y;
      end if;

      Display.Buffers (LCD_Layer) :=
        (Addr              =>
           Reserve (UInt32 (HAL.Bitmap.Bits_Per_Pixel (Mode) * W * H / 8)),
         Actual_Width      => W,
         Actual_Height     => H,
         Actual_Color_Mode => Mode,
         Currently_Swapped => False,
         Native_Source     => 0);
      Display.Buffers (LCD_Layer).Fill;

      DSIHOST.DSI_Wrapper_Disable;

      STM32.LTDC.Layer_Init
        (Layer          => LCD_Layer,
         Config         => STM32.LTDC.To_LTDC_Mode (Mode),
         Buffer         => Display.Buffers (LCD_Layer).Addr,
         X              => X,
         Y              => Y,
         W              => W,
         H              => H,
         Constant_Alpha => 255,
         BF             => STM32.LTDC.BF_Pixel_Alpha_X_Constant_Alpha);
      STM32.LTDC.Reload_Config (True);

      DSIHOST.DSI_Wrapper_Enable;

      Display.Update_Layers;
   end Initialize_Layer;

   -----------------
   -- Initialized --
   -----------------

   overriding function Initialized
     (Display : Frame_Buffer;
      Layer   : Positive) return Boolean
   is
      LCD_Layer  : constant STM32.LTDC.LCD_Layer :=
                     (if Layer = 1
                      then STM32.LTDC.Layer1
                      else STM32.LTDC.Layer2);
      use type STM32.DMA2D_Bitmap.DMA2D_Bitmap_Buffer;
   begin
      return
        Display.Buffers (LCD_Layer) /= STM32.DMA2D_Bitmap.Null_Buffer;
   end Initialized;

   ------------------
   -- Update_Layer --
   ------------------

   overriding procedure Update_Layer
     (Display   : in out Frame_Buffer;
      Layer     : Positive;
      Copy_Back : Boolean := False)
   is
      pragma Unreferenced (Layer, Copy_Back);
   begin
      Display.Update_Layers;
   end Update_Layer;

   -------------------
   -- Update_Layers --
   -------------------

   overriding procedure Update_Layers
     (Display : in out Frame_Buffer)
   is
      use STM32.DMA2D_Bitmap;
   begin
      STM32.DMA2D.DMA2D_Wait_Transfer;

      if Display.Buffers (STM32.LTDC.Layer1) /= Null_Buffer then
         Cortex_M.Cache.Clean_DCache
           (Start => Display.Buffers (STM32.LTDC.Layer1).Addr,
            Len   => Display.Buffers (STM32.LTDC.Layer1).Buffer_Size);
      end if;

      if Display.Buffers (STM32.LTDC.Layer2) /= Null_Buffer then
         Cortex_M.Cache.Clean_DCache
           (Start => Display.Buffers (STM32.LTDC.Layer2).Addr,
            Len   => Display.Buffers (STM32.LTDC.Layer2).Buffer_Size);
      end if;

      Sync.Do_Refresh;
      Sync.Wait;
   end Update_Layers;

   ----------------
   -- Color_Mode --
   ----------------

   overriding function Color_Mode
     (Display : Frame_Buffer;
      Layer   : Positive) return HAL.Framebuffer.FB_Color_Mode
   is
      LCD_Layer  : constant STM32.LTDC.LCD_Layer :=
                     (if Layer = 1
                      then STM32.LTDC.Layer1
                      else STM32.LTDC.Layer2);
   begin
      return Display.Buffers (LCD_Layer).Color_Mode;
   end Color_Mode;

   -------------------
   -- Hidden_Buffer --
   -------------------

   overriding function Hidden_Buffer
     (Display : in out Frame_Buffer;
      Layer   : Positive) return not null HAL.Bitmap.Any_Bitmap_Buffer
   is
      LCD_Layer  : constant STM32.LTDC.LCD_Layer :=
                     (if Layer = 1
                      then STM32.LTDC.Layer1
                      else STM32.LTDC.Layer2);
   begin
      return Display.Buffers (LCD_Layer)'Unchecked_Access;
   end Hidden_Buffer;

   ----------------
   -- Pixel_Size --
   ----------------

   overriding function Pixel_Size
     (Display : Frame_Buffer;
      Layer   : Positive) return Positive
   is
      LCD_Layer  : constant STM32.LTDC.LCD_Layer :=
                     (if Layer = 1
                      then STM32.LTDC.Layer1
                      else STM32.LTDC.Layer2);
   begin
      return
        HAL.Bitmap.Bits_Per_Pixel
          (Display.Buffers (LCD_Layer).Color_Mode) / 8;
   end Pixel_Size;

end Framebuffer_DSI;