Welcome to mirror list, hosted at ThFree Co, Russian Federation.

spi.hpp « cm3cpp - github.com/thirdpin/libopencm3_cpp_extensions.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f217d9b6db05921e8ca5949e050e9768e31ad8fc (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
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
/*
 * This file is part of the libopencm3_cpp_extensions project.
 * hosted at http://github.com/thirdpin/libopencm3_cpp_extensions
 *
 * Copyright (C) 2016  Third Pin LLC
 * Written by Anastasiia Lazareva <a.lazareva@thirdpin.ru>
 * Written by Maxim Ambrosevich
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

/*
SPI C++ Wrapper of libopencm3 library for STM32F2, STM32F4
*/

#ifndef SPI_EXT_H
#define SPI_EXT_H

#include <libopencm3/stm32/spi.h>

#include "gpio.hpp"
#include "irq/irq.hpp"

namespace cm3cpp {

namespace spi {

constexpr bool OK = true;
constexpr bool ERROR = false;

enum Flag : uint32_t
{
    RECEIVE_BUFFER_NOT_EMPTY = 0x0001,
    TRANSMIT_BUFFER_EMPTY = 0x0002,
    CHANEL_SIDE = 0x0004,
    UNDERRUN_FLAG = 0x0008,
    CRC_ERROR = 0x0010,
    MODE_FAULT = 0x0020,
    OVERRUN_FLAG = 0x0040,
    BUSY_FLAG = 0x0080,
    TI_FRAME_FORMAT_ERROR = 0x0100,
};

enum BaudRate : uint8_t
{
    BAUDRATE_FPCLK_DIV_2 = 0x00,
    BAUDRATE_FPCLK_DIV_4 = 0x01,
    BAUDRATE_FPCLK_DIV_8 = 0x02,
    BAUDRATE_FPCLK_DIV_16 = 0x03,
    BAUDRATE_FPCLK_DIV_32 = 0x04,
    BAUDRATE_FPCLK_DIV_64 = 0x05,
    BAUDRATE_FPCLK_DIV_128 = 0x06,
    BAUDRATE_FPCLK_DIV_256 = 0x07,
};

enum NextTx
{
    NEXT_TX_FROM_BUFFER,
    NEXT_TX_FROM_CRC
};

enum DataFrameFormat
{
    DFF_8BIT,
    DFF_16BIT
};

enum State
{
    DISABLE,
    ENABLE
};

enum NssState
{
    LOW,
    HIGH
};

enum Polarity
{
    POLARITY_LOW,
    POLARITY_HIGH
};

enum Phase
{
    PHASE_LOW,
    PHASE_HIGH
};

enum BitPos
{
    MSB_FIRST,
    LSB_FIRST
};

enum StdMode
{
    MODE_0 = 0,
    MODE_1,
    MODE_2,
    MODE_3
};

class Spi
{
 public:
    using Gpio = gpio::Gpio;

    struct Config
    {
        uint8_t spi_number;
        Gpio::Pinout mosi_pin;
        Gpio::Pinout miso_pin;
        Gpio::Pinout scl_pin;
    };

    Spi();
    Spi(Config spi_conf);

    bool get_flag_status(Flag flag) const;

    void reset() { spi_reset(_spi); }

    void enable() { spi_enable(_spi); }

    void disable() { spi_disable(_spi); }

    void clean_disable() { spi_clean_disable(_spi); }

    void write(uint16_t data)
    {
        while (!get_flag_status(Flag::TRANSMIT_BUFFER_EMPTY))
            ;
        SPI_DR(_spi) = data;
    }

    void write_end()
    {
        while (!get_flag_status(Flag::RECEIVE_BUFFER_NOT_EMPTY))
            ;
        (void)SPI_DR(_spi);
    }

    uint16_t read(uint16_t data)
    {
        while (!get_flag_status(Flag::TRANSMIT_BUFFER_EMPTY))
            ;
        SPI_DR(_spi) = data;
        while (!get_flag_status(Flag::RECEIVE_BUFFER_NOT_EMPTY))
            ;
        return SPI_DR(_spi);
    }

    void set_master_mode() { spi_set_master_mode(_spi); }

    void set_slave_mode() { spi_set_slave_mode(_spi); }

    void full_duplex_mode() { spi_set_full_duplex_mode(_spi); }

    void set_bidirectional_mode() { spi_set_bidirectional_mode(_spi); }

    void set_bidirectional_transmit_only_mode()
    {
        spi_set_bidirectional_transmit_only_mode(_spi);
    }

    void set_bidirectional_receive_only_mode()
    {
        spi_set_bidirectional_receive_only_mode(_spi);
    }

    void set_unidirectional_mode() { spi_set_unidirectional_mode(_spi); }

    void set_receive_only_mode() { spi_set_receive_only_mode(_spi); }

    void enable_crc() { spi_enable_crc(_spi); }

    void disable_crc() { spi_disable_crc(_spi); }

    void set_next_tx_from(NextTx next);
    void set_data_drame_format(DataFrameFormat dff);
    void set_software_slave_management(State state);
    void set_nss(NssState nss);
    void set_bit_position(BitPos pos);

    void set_baudrate_prescaler(BaudRate baudrate)
    {
        spi_set_baudrate_prescaler(_spi, (uint8_t)baudrate);
    }

    void set_clock_polarity(Polarity polarity);
    void set_clock_phase(Phase phase);

    void enable_nvic();

    void enable_tx_buffer_empty_interrupt()
    {
        spi_enable_tx_buffer_empty_interrupt(_spi);
    }

    void disable_tx_buffer_empty_interrupt()
    {
        spi_disable_tx_buffer_empty_interrupt(_spi);
    }

    void enable_rx_buffer_not_empty_interrupt()
    {
        spi_enable_rx_buffer_not_empty_interrupt(_spi);
    }

    void disable_rx_buffer_not_empty_interrupt()
    {
        spi_disable_rx_buffer_not_empty_interrupt(_spi);
    }

    void enable_error_interrupt() { spi_enable_error_interrupt(_spi); }

    void disable_error_interrupt() { spi_disable_error_interrupt(_spi); }

    void enable_ss_output() { spi_enable_ss_output(_spi); }

    void disable_ss_output() { spi_disable_ss_output(_spi); }

    void enable_tx_dma() { spi_enable_tx_dma(_spi); }

    void disable_tx_dma() { spi_disable_tx_dma(_spi); }

    void enable_rx_dma() { spi_enable_rx_dma(_spi); }

    void disable_rx_dma() { spi_disable_rx_dma(_spi); }

    void set_standard_mode(StdMode mode) { spi_set_standard_mode(_spi, mode); }

 private:
    uint32_t _spi;
    Interrupt _irq;
};

}  // namespace spi

}  // namespace cm3cpp

#endif