summaryrefslogtreecommitdiff
path: root/digital/ucoolib/ucoolib/hal/spi/spi_soft.cc
blob: af5ac12414b74aff63d594039028b21fe8f08ee3 (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
119
120
121
122
123
124
125
// 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 "spi_soft.hh"

#include "ucoolib/utils/delay.hh"
#include "ucoolib/common.hh"

namespace ucoo {

SpiSoftMaster::SpiSoftMaster (Io &sck, Io &mosi, Io &miso, int speed,
                              SpiMode mode)
    : sck_ (sck), mosi_ (mosi), miso_ (miso)
{
    setup (speed, mode);
}

SpiSoftMaster::~SpiSoftMaster ()
{
    setup (0);
}

void
SpiSoftMaster::setup (int speed, SpiMode mode)
{
    if (speed)
    {
        // Take pins ownership, set SCK according to CPOL.
        sck_.set (mode >= SPI_MODE_2);
        sck_.output ();
        mosi_.reset ();
        mosi_.output ();
        miso_.input ();
        // Set clock period and clock phase.
        half_period_ns_ = 1000 * 1000 * 1000 / 2 / speed;
        cpha_ = mode & 1;
    }
    else
    {
        // Release pins.
        sck_.input ();
        sck_.reset ();
        mosi_.input ();
    }
}

void
SpiSoftMaster::send_and_recv (const char *tx_buf, char *rx_buf, int count)
{
    while (count--)
        *rx_buf++ = send_and_recv (*tx_buf++);
}

char
SpiSoftMaster::send_and_recv (char tx)
{
    uint8_t i, rx = 0;
    // Depending on clock phase, sample is done on first transition or second
    // transition.
    for (i = 0x80; i; i >>= 1)
    {
        // Setup stage.
        if (cpha_)
            sck_.toggle ();
        mosi_.set (tx & i);
        delay_ns (half_period_ns_);
        // Sample stage.
        sck_.toggle ();
        if (miso_.get ())
            rx |= i;
        delay_ns (half_period_ns_);
        // SCK toggle for next setup stage.
        if (!cpha_)
            sck_.toggle ();
    }
    return rx;
}

void
SpiSoftMaster::send (const char *tx_buf, int count)
{
    while (count--)
        send (*tx_buf++);
}

void
SpiSoftMaster::send (char tx)
{
    send_and_recv (tx);
}

void
SpiSoftMaster::recv (char *rx_buf, int count)
{
    while (count--)
        *rx_buf++ = recv ();
}

char
SpiSoftMaster::recv ()
{
    return send_and_recv (0);
}

} // namespace ucoo