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

flash_otp.hpp « cm3cpp - github.com/thirdpin/libopencm3_cpp_extensions.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f0d49093538bd1f5c57fc394f739cf9bab7f0e2d (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
#ifndef CM3CPP_FLASH_OTP_HPP_
#define CM3CPP_FLASH_OTP_HPP_

#include <libopencm3/stm32/flash.h>
#include <libopencmsis/core_cm3.h>

namespace cm3cpp {

namespace flash {

enum class OtpBlock : uint8_t
{
    BLOCK_0,
    BLOCK_1,
    BLOCK_2,
    BLOCK_3,
    BLOCK_4,
    BLOCK_5,
    BLOCK_6,
    BLOCK_7,
    BLOCK_8,
    BLOCK_9,
    BLOCK_10,
    BLOCK_11,
    BLOCK_12,
    BLOCK_13,
    BLOCK_14,
    BLOCK_15,
};

enum class OtpByte
{
    BYTE_0,
    BYTE_1,
    BYTE_2,
    BYTE_3,
    BYTE_4,
    BYTE_5,
    BYTE_6,
    BYTE_7,
    BYTE_8,
    BYTE_9,
    BYTE_10,
    BYTE_11,
    BYTE_12,
    BYTE_13,
    BYTE_14,
    BYTE_15,
    BYTE_16,
    BYTE_17,
    BYTE_18,
    BYTE_19,
    BYTE_20,
    BYTE_21,
    BYTE_22,
    BYTE_23,
    BYTE_24,
    BYTE_25,
    BYTE_26,
    BYTE_27,
    BYTE_28,
    BYTE_29,
    BYTE_30,
    BYTE_31,
};

enum class Flag
{
    BSY = FLASH_SR_BSY,
    PGSERR = FLASH_SR_PGSERR,
    PGPERR = FLASH_SR_PGPERR,
    PGAERR = FLASH_SR_PGAERR,
    WRPERR = FLASH_SR_WRPERR,
    OPERR = FLASH_SR_OPERR,
    EOP = FLASH_SR_EOP,
};

class FlashOtp
{
 public:
    static void write(OtpBlock block, OtpByte byte, uint8_t data)
    {
        flash_unlock();
        clear_flag(Flag::EOP);
        clear_flag(Flag::OPERR);
        clear_flag(Flag::WRPERR);
        clear_flag(Flag::PGAERR);
        clear_flag(Flag::PGPERR);
        clear_flag(Flag::PGSERR);
        flash_wait_for_last_operation();
        uint32_t address = _OTP_START_ADDR +
                           ((uint32_t)block * _OTP_BYTES_IN_BLOCK) +
                           (uint32_t)byte;
        __disable_irq();
        flash_program_byte(address, data);
        __enable_irq();
        flash_lock();
    }

    static uint8_t read(OtpBlock block, OtpByte byte)
    {
        uint32_t address = _OTP_START_ADDR +
                           ((uint32_t)block * _OTP_BYTES_IN_BLOCK) +
                           (uint32_t)byte;
        return *(uint8_t*)address;
    }

    static const uint8_t* const get_pointer(OtpBlock block, OtpByte byte)
    {
        uint32_t address = _OTP_START_ADDR +
                           ((uint32_t)block * _OTP_BYTES_IN_BLOCK) +
                           (uint32_t)byte;
        return (uint8_t*)address;
    }

    static bool get_flag(Flag flag)
    {
        uint32_t status = FLASH_SR | (uint32_t)flag;
        if (status > 0)
            return true;
        return false;
    }

    static void clear_flag(Flag flag)
    {
        if (flag == Flag::BSY)
            return;

        FLASH_SR |= (uint32_t)flag;
    }

    static void lock_block(OtpBlock block)
    {
        flash_unlock();
        clear_flag(Flag::EOP);
        clear_flag(Flag::OPERR);
        clear_flag(Flag::WRPERR);
        clear_flag(Flag::PGAERR);
        clear_flag(Flag::PGPERR);
        clear_flag(Flag::PGSERR);
        flash_wait_for_last_operation();
        uint32_t address = _OTP_LOCK_ADDR + static_cast<uint32_t>(block);
        __disable_irq();
        flash_program_byte(address, _OTP_LOCK_BLOCK);
        __enable_irq();
        flash_lock();
    }

 private:
    static constexpr uint32_t _OTP_START_ADDR = (0x1FFF7800);
    static constexpr uint32_t _OTP_LOCK_ADDR = (0x1FFF7A00);
    static constexpr uint32_t _OTP_BLOCKS = 16;
    static constexpr uint32_t _OTP_BYTES_IN_BLOCK = 32;
    static constexpr uint32_t _OTP_SIZE = (_OTP_BLOCKS * _OTP_BYTES_IN_BLOCK);
    static constexpr uint8_t _OTP_LOCK_BLOCK = 0x00;
};

} /* namespace flash */

} /* namespace cm3cpp */

#endif /* CM3CPP_FLASH_OTP_HPP_ */