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

cm3cpp_dma.hpp - github.com/thirdpin/libopencm3_cpp_extensions.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5f164a2e40c8895081f7abb3c0db07e2c12f2df4 (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
/**
 ******************************************************************************
 * @file    cm3cpp_dma.hpp
 * @author
 * @version V1.0
 * @date    05-2019
 * @brief   This is file realise DMA.
 ******************************************************************************
 * @attention
 *
 ******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#pragma once

// LIBOPENCM3 INCLUDES
#include <libopencm3/stm32/dma.h>

namespace cm3cpp {

namespace dma {

enum DmaNumber
{
    _1 = DMA1,
    _2 = DMA2,
};

/// Streams for USARTs
enum Stream
{
    STREAM1 = DMA_STREAM1,
    STREAM2 = DMA_STREAM2,
    STREAM3 = DMA_STREAM3,
    STREAM4 = DMA_STREAM4,
    STREAM5 = DMA_STREAM5,
    STREAM6 = DMA_STREAM6,
    STREAM7 = DMA_STREAM7
};

/// Channels for USARTs
enum Channel
{
    CHANNEL1 = DMA_SxCR_CHSEL_1,
    CHANNEL2 = DMA_SxCR_CHSEL_2,
    CHANNEL3 = DMA_SxCR_CHSEL_3,
    CHANNEL4 = DMA_SxCR_CHSEL_4,
    CHANNEL5 = DMA_SxCR_CHSEL_5,
    CHANNEL6 = DMA_SxCR_CHSEL_6,
    CHANNEL7 = DMA_SxCR_CHSEL_7
};

enum DataTransferDirection
{
    MEM_TO_PERIPHERAL = DMA_SxCR_DIR_MEM_TO_PERIPHERAL,
    PERIPHERAL_TO_MEM = DMA_SxCR_DIR_PERIPHERAL_TO_MEM,
    MEM_TO_MEM = DMA_SxCR_DIR_MEM_TO_MEM
};

/// Shows where or where data will be sent, to the periphery or memory
enum class IncrementedMode
{
    DISABLE,
    ENABLE
};

/// Shows how much the size will be increased for the periphery
enum DataSize
{
    PERIPHERAL_BYTE = DMA_SxCR_PSIZE_8BIT,
    PERIPHERAL_HALF_WORD = DMA_SxCR_PSIZE_16BIT,
    PERIPHERAL_WORD = DMA_SxCR_PSIZE_32BIT,

    MEMORY_BYTE = DMA_SxCR_MSIZE_8BIT,
    MEMORY_HALF_WORD = DMA_SxCR_MSIZE_16BIT,
    MEMORY_WORD = DMA_SxCR_MSIZE_32BIT,
};

/// DMA data recording mode, cyclic or normal
enum class Mode
{
    NORMAL,
    CIRCULAR
};

/// The priorities of DMA
enum Priority
{
    LOW = DMA_SxCR_PL_LOW,
    MEDIUM = DMA_SxCR_PL_MEDIUM,
    HIGH = DMA_SxCR_PL_HIGH,
    VERY_HIGH = DMA_SxCR_PL_VERY_HIGH
};

/// Low level config for DMA
struct LowLevelConfig
{
    DmaNumber dma_num;
    Stream stream;
    Channel channel;
    uint32_t peripheral_base_addr;
    DataTransferDirection direction;
    IncrementedMode periph_inc_mode;
    IncrementedMode mem_inc_mode;
    DataSize periph_size;
    DataSize mem_size;
    Mode mode;
    Priority priority;
    uint8_t irq;
};

/**
 * Class hard DMA
 */
class Dma
{
 public:
    Dma(const LowLevelConfig& config);

    ~Dma() = default;

    bool get_interrupt_flag() const;

    void clear_interrupt_flag() const;

    void set_memory_address(uint32_t address) const;

    void enable_stream() const;

    void disable_stream() const;

    void set_data_counter(uint16_t len) const;
    
    void enable_irq() const;

    void disable_irq() const;

 protected:
    Dma() = delete;                    /// Constructor default is delete
    Dma(const Dma& a) = delete;        /// Constructor copy is delete
    Dma(Dma&& a) = delete;             /// Constructor move is delete

    Dma& operator=(const Dma&) = delete;  /// Operator copy is delete
    Dma& operator=(Dma&&) = delete;       /// Operator move is delete

 private:
    Stream _stream;      ///< Shows the stream on which DMA is configured
    DmaNumber _dma_num;  ///< Number configured DMA

    uint8_t _irq;  ///< Interrupt number
};

}  // namespace dma

}  // namespace cm3cpp  // namespace cm3cpp