summaryrefslogtreecommitdiff
path: root/ucoo/hal/frame_buffer/ltdc.stm32f4.cc
blob: f80083c4d312693e868b8d3b476600d27cb293ce (plain)
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
// ucoolib - Microcontroller object oriented library. {{{
//
// Copyright (C) 2016 Nicolas Schodet
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// }}}
#include "ucoo/hal/frame_buffer/ltdc.stm32f4.hh"

#include "ucoo/arch/reg.hh"
#include "ucoo/arch/rcc.stm32.hh"

namespace ucoo {

Ltdc::Ltdc (int width, int heigth,
            int hsync, int hbp, int hfp,
            int vsync, int vbp, int vfp)
    : width_ (width), heigth_ (heigth),
      hsync_ (hsync), hbp_ (hbp), hfp_ (hfp),
      vsync_ (vsync), vbp_ (vbp), vfp_ (vfp)
{
}

void
Ltdc::enable ()
{
    // TODO: make this more configurable.
    // Enable clocks.
    rcc_peripheral_clock_enable (Rcc::LTDC);
    // Polarity of control signals.
    reg::LTDC->GCR &= (LTDC_GCR_HSPOL | LTDC_GCR_VSPOL | LTDC_GCR_DEPOL
                       | LTDC_GCR_PCPOL);
    // Timings.
    reg::LTDC->SSCR = (hsync_ - 1) << 16 | (vsync_ - 1);
    reg::LTDC->BPCR = (hsync_ + hbp_ - 1) << 16 | (vsync_ + vbp_ - 1);
    reg::LTDC->AWCR = (hsync_ + hbp_ + width_ - 1) << 16
        | (vsync_ + vbp_ + heigth_ - 1);
    reg::LTDC->TWCR = (hsync_ + hbp_ + width_ + hfp_ - 1) << 16
        | (vsync_ + vbp_ + heigth_ + vfp_ - 1);
    // Enable.
    reg::LTDC->GCR |= LTDC_GCR_LTDCEN;
}

void
Ltdc::disable ()
{
    reg::LTDC->GCR &= ~LTDC_GCR_LTDCEN;
    rcc_peripheral_clock_disable (Rcc::LTDC);
    // TODO: shut down PLL.
}

void
Ltdc::layer_setup (int layer, const Surface &surface, int x, int y)
{
    auto reg = layer == 0 ? reg::LTDC_Layer1 : reg::LTDC_Layer2;
    reg->WHPCR = (hsync_ + hbp_ + x + surface.w - 1) << 16
        | (hsync_ + hbp_ + x);
    reg->WVPCR = (vsync_ + vbp_ + y + surface.h - 1) << 16
        | (vsync_ + vbp_ + y);
    reg->PFCR = static_cast<int> (surface.format) & 0xff;
    reg->CFBAR = reinterpret_cast<uintptr_t> (surface.pixels);
    reg->CFBLR = (surface.stride * surface.bpp () / 8) << 16
        | (surface.w * surface.bpp () / 8 + 3);
    reg->CFBLNR = surface.h;
    reg->CR = LTDC_LxCR_LEN;
    // Load parameters.
    reg::LTDC->SRCR = LTDC_SRCR_VBR;
}

void
Ltdc::wait_reload ()
{
    while (reg::LTDC->SRCR & LTDC_SRCR_VBR)
        ;
}

} // namespace ucoo