summaryrefslogtreecommitdiff
path: root/digital/ucoolib/ucoolib/hal/spi/test/test_spi.cc
blob: 6e229fd3886bd68344537faf116c18a2b3e92fc6 (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
// ucoolib - Microcontroller object oriented library. {{{
//
// Copyright (C) 2013 Nicolas Schodet
//
// APBTeam:
//        Web: http://apbteam.org/
//      Email: team AT apbteam DOT org
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}
#include "ucoolib/hal/spi/spi_soft.hh"
#include "ucoolib/hal/gpio/gpio.hh"
#include "ucoolib/utils/delay.hh"

#include "ucoolib/arch/arch.hh"
#include "ucoolib/base/test/test.hh"

#include <libopencm3/stm32/f4/rcc.h>

#include <algorithm>
#include <cstdio>

int
main (int argc, const char **argv)
{
    ucoo::arch_init (argc, argv);
    ucoo::Stream &ts = ucoo::test_stream ();
    // Use connection to LIS302DL device on discovery board.
    rcc_peripheral_enable_clock (&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN
                                 | RCC_AHB1ENR_IOPEEN);
    ucoo::Gpio ss (GPIOE, 3);
    ss.set ();
    gpio_mode_setup (GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO3);
    ucoo::Gpio sck (GPIOA, 5), mosi (GPIOA, 7), miso (GPIOA, 6);
    ucoo::SpiSoftMaster spi (sck, mosi, miso, 1000000, ucoo::SPI_MODE_3);
    // Loop with simple IU.
    char buf[64];
    unsigned int r;
    int8_t x, y, z;
    while (1)
    {
        ucoo::delay_ms (200);
        // Read X, Y, Z.
        ss.reset ();
        spi.send (0xe9);
        x = spi.recv ();
        spi.recv ();
        y = spi.recv ();
        spi.recv ();
        z = spi.recv ();
        ss.set ();
        ucoo::delay_ns (100);
        r = snprintf (buf, sizeof (buf), "x: %4d, y: %4d, z: %4d\r", x, y, z);
        r = std::min (sizeof (buf) - 1, r);
        ts.write (buf, r);
        // Simple UI.
        while (ts.poll ())
        {
            char c = ts.getc ();
            switch (c)
            {
            case '?':
                static const char help[] =
                    "\n? - help\n"
                    "w - who am I\n"
                    "o - turn on\n"
                    "f - turn off\n";
                ts.write (help, sizeof (help));
                break;
            case 'w':
                {
                    // Read Who am I register.
                    ss.reset ();
                    spi.send (0x8f);
                    char rsp = spi.recv ();
                    ss.set ();
                    ucoo::delay_ns (100);
                    // Report result.
                    r = snprintf (buf, sizeof (buf), "\nI am 0x%02x\n",
                                  static_cast<unsigned char> (rsp));
                    r = std::min (sizeof (buf) - 1, r);
                    ts.write (buf, r);
                }
                break;
            case 'o':
            case 'f':
                {
                    char cmd[] = { 0x20, (char) (c == 'o' ? 0x47 : 0x07) };
                    ss.reset ();
                    spi.send (cmd, sizeof (cmd));
                    ss.set ();
                    ucoo::delay_ns (100);
                }
                break;
            }
        }
    }
}