summaryrefslogtreecommitdiff
path: root/ucoo/hal/sdram/test/test_sdram.stm32f4.cc
blob: 17ed1b4842c38cefd685abc3dae42df3ec4b52ab (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
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
// ucoolib - Microcontroller object oriented library. {{{
//
// Copyright (C) 2015 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/sdram/sdram.hh"

#include "ucoo/arch/arch.hh"
#include "ucoo/base/test/test.hh"
#include "ucoo/utils/bits.hh"

#include <cstdio>
#include <cinttypes>

int
memtest (void *addr, int size, uint32_t pattern)
{
    int errors = 0;
    uint32_t *begin = reinterpret_cast<uint32_t *> (addr);
    uint32_t *end = begin + size / 4;
    printf ("memtest %dM, pattern 0x%08" PRIx32 "\n", size / 1024 / 1024, pattern);
    printf (" fill...\n");
    for (uint32_t *p = begin; p != end; p++)
        *p = pattern;
    printf (" check increasing...\n");
    for (uint32_t *p = begin; p != end; p++)
    {
        if (*p != pattern)
            errors++;
        *p = ~pattern;
    }
    printf (" check decreasing...\n");
    for (uint32_t *p = end; p != begin; p--)
    {
        if (*(p - 1) != ~pattern)
            errors++;
        *(p - 1) = pattern;
    }
    printf (" done, %d errors\n", errors);
    return errors;
}

int
main (int argc, const char **argv)
{
    ucoo::arch_init (argc, argv);
    ucoo::test_stream_setup ();
    ucoo::GPIOD.enable ();
    ucoo::GPIOE.enable ();
    ucoo::GPIOF.enable ();
    ucoo::GPIOG.enable ();
    ucoo::GPIOH.enable ();
    ucoo::GPIOI.enable ();
    std::initializer_list<ucoo::Sdram::Io> sdram_ios {
        { ucoo::GPIOD, ucoo::bits<uint16_t> (
                0, 1, 8, 9, 10, 14, 15) },
        { ucoo::GPIOE, ucoo::bits<uint16_t> (
                0, 1, 7, 8, 9, 10, 11, 12, 13, 14, 15) },
        { ucoo::GPIOF, ucoo::bits<uint16_t> (
                0, 1, 2, 3, 4, 5, 11, 12, 13, 14, 15) },
        { ucoo::GPIOG, ucoo::bits<uint16_t> (
                0, 1, 2, 3, 4, 5, 8, 15) },
        { ucoo::GPIOH, ucoo::bits<uint16_t> (
                2, 3, 5, 8, 9, 10, 11, 12, 13, 14, 15) },
        { ucoo::GPIOI, ucoo::bits<uint16_t> (
                0, 1, 2, 3, 4, 5, 6, 7, 9, 10) },
    };
    ucoo::Sdram::Param sdram_param {
        .bank = 1,
        .clock_hz = 133333333,
        .cas = 3,
        .banks = 4,
        .bits = 32,
        .row_bits = 12,
        .col_bits = 8,
        .trcd = 3,
        .trp = 3,
        .twr = 2,
        .trc = 8,
        .tras = 6,
        .txsr = 9,
        .tmrd = 2,
        .tref_ms = 64,
        .init_clock_delay_us = 100,
        .init_auto_refresh = 2,
    };
    ucoo::Sdram sdram (sdram_ios, sdram_param);
    sdram.enable ();
    long long errors = 0;
    for (int i = 0; i < 100; i++)
    {
        errors += memtest (sdram.addr (), sdram.size (), 0x00000000);
        errors += memtest (sdram.addr (), sdram.size (), 0xffffffff);
        errors += memtest (sdram.addr (), sdram.size (), 0x55aaa55a);
        errors += memtest (sdram.addr (), sdram.size (), 0xff00ff00);
        errors += memtest (sdram.addr (), sdram.size (), 0x00ff00ff);
        printf ("total errors: %lld\n", errors);
    }
    return errors == 0 ? 0 : 1;
}