/
mem.h
189 lines (168 loc) · 6.5 KB
/
mem.h
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
/**
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
* +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
* || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
*
* Crazyflie control firmware
*
* Copyright (C) 2012-2020 BitCraze AB
*
* 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, in version 3.
*
* 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/>.
*
* mem.h - Memory sub system
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
typedef enum {
MEM_TYPE_EEPROM = 0x00,
MEM_TYPE_OW = 0x01,
MEM_TYPE_LED12 = 0x10,
MEM_TYPE_LOCO = 0x11,
MEM_TYPE_TRAJ = 0x12,
MEM_TYPE_LOCO2 = 0x13,
MEM_TYPE_LH = 0x14,
MEM_TYPE_TESTER = 0x15,
MEM_TYPE_USD = 0x16,
MEM_TYPE_LEDMEM = 0x17,
MEM_TYPE_APP = 0x18,
MEM_TYPE_DECK_MEM = 0x19,
} MemoryType_t;
#define MEMORY_SERIAL_LENGTH 8
typedef struct {
const MemoryType_t type;
uint32_t (*getSize)(void);
bool (*read)(const uint32_t memAddr, const uint8_t readLen, uint8_t* buffer);
bool (*write)(const uint32_t memAddr, const uint8_t writeLen, const uint8_t* buffer);
} MemoryHandlerDef_t;
typedef struct {
uint8_t nrOfMems;
const uint32_t size;
bool (*getSerialNr)(const uint8_t selectedMem, uint8_t* serialNr);
bool (*read)(const uint8_t selectedMem, const uint32_t memAddr, const uint8_t readLen, uint8_t* buffer);
bool (*write)(const uint8_t selectedMem, const uint32_t memAddr, const uint8_t writeLen, const uint8_t* buffer);
} MemoryOwHandlerDef_t;
// Public functions
void memInit(void);
bool memTest(void);
/**
* @brief Register a memory handler
*
* @param handlerDef A pointer to a memory handler definition
*/
void memoryRegisterHandler(const MemoryHandlerDef_t* handlerDef);
/**
* @brief Register the One Wire memory handler.
* It is only possible to register a single One Wire memory handler.
*
* @param handlerDef A pointer to a One Wire memory handler definition.
*/
void memoryRegisterOwHandler(const MemoryOwHandlerDef_t* handlerDef);
/**
* @brief Block the possibility to register any more memory handlers.
* This function can be called by a protocol implementation to "freeze" the available set of handlers.
*/
void memBlockHandlerRegistration();
/**
* @brief Get the number of registered handlers
*
* @return uint16_t The number of registered memory handlers
*/
uint16_t memGetNrOfMems();
/**
* @brief Get the number of registered One Wire memory handlers
*
* @return uint16_t The number of registered handlers
*/
uint16_t memGetNrOfOwMems();
/**
* @brief Get the type of a specific memory handler
*
* @param memId The id of the memory handler to get the type for. The id is an index based on the registration order, between 0 and memGetNrOfMems() - 1.
* @return MemoryType_t The type of the memory handler
*/
MemoryType_t memGetType(const uint16_t memId);
/**
* @brief Get the size of the mapped memory for a specific memory handler
*
* @param memId The id of the memory handler to get the type for. The id is an index based on the registration order, between 0 and memGetNrOfMems() - 1.
* @return uint32_t The size of the mapped memory
*/
uint32_t memGetSize(const uint16_t memId);
/**
* @brief Get the size of a One Wire memory. Note: at the time of writing, only one type of One Wire memory is supported.
*
* @return uint32_t The size of a One Wire memory
*/
uint32_t memGetOwSize();
/**
* @brief Read data from a memory handler
*
* @param memId The id of the memory handler to get the type for. The id is an index based on the registration order, between 0 and memGetNrOfMems() - 1.
* @param memAddr The mapped start address to read from. Starts from 0 for each mapped memory.
* @param readLen The number of bytes to read
* @param buffer The buffer to copy the data into
* @return true If successful
* @return false If failure
*/
bool memRead(const uint16_t memId, const uint32_t memAddr, const uint8_t readLen, uint8_t* buffer);
/**
* @brief Write data to a memory handler
*
* @param memId The id of the memory handler to get the type for. The id is an index based on the registration order, between 0 and memGetNrOfMems() - 1.
* @param memAddr The mapped start address to read from. Starts from 0 for each mapped memory.
* @param writeLen The number of bytes to write
* @param buffer The buffer to copy data from
* @return true If successful
* @return false If failure
*/
bool memWrite(const uint16_t memId, const uint32_t memAddr, const uint8_t writeLen, const uint8_t* buffer);
/**
* @brief Read from a One Wire memory
*
* @param owMemId The id of the one wire memory. The id is an index based on the available One Wire memories on decks for instance, between 0 and memGetNrOfOwMems() - 1.
* @param memAddr The address to start reading from
* @param readLen The number of bytes to read
* @param buffer The buffer to copy data into
* @return true If successful
* @return false If failure
*/
bool memReadOw(const uint16_t owMemId, const uint32_t memAddr, const uint8_t readLen, uint8_t* buffer);
/**
* @brief Get the serial number of a specific One Wire memory
*
* @param owMemId The id of the one wire memory. The id is an index based on the available One Wire memories on decks for instance, between 0 and memGetNrOfOwMems() - 1.
* @param serialNr A buffer of minimum 8 bytes to write the serial number to
* @return true If successful
* @return false If failure
*/
bool memGetOwSerialNr(const uint8_t owMemId, uint8_t* serialNr);
/**
* @brief Write to a One Wire
*
* @param owMemId The id of the one wire memory. The id is an index based on the available One Wire memories on decks for instance, between 0 and memGetNrOfOwMems() - 1.
* @param memAddr The address to start writing to
* @param writeLen The number of bytes to write
* @param buffer The buffer to copy data from
* @return true If successful
* @return false If failure
*/
bool memWriteOw(const uint16_t owMemId, const uint32_t memAddr, const uint8_t writeLen, const uint8_t* buffer);
#ifdef UNIT_TEST_MODE
/**
* @brief Reset function for unit testing
*/
void memReset();
#endif