-
Notifications
You must be signed in to change notification settings - Fork 3
/
mcb.h
355 lines (324 loc) · 8.94 KB
/
mcb.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
* @file mcb.h
* @brief This file contains API functions of the
* motion control bus (MCB)
*
* @author Firmware department
* @copyright Ingenia Motion Control (c) 2018. All rights reserved.
*/
/**
* \addtogroup MainAPI Main API
*
* @{
*
* Main API containing all structs, variables and functions required
* to use Motion Control Bus.
*/
#ifndef MCB_H
#define MCB_H
#include "mcb_intf.h"
/** Default timeout for blocking mode */
#define MCB_DFLT_TIMEOUT (uint32_t)1000UL
/** Maximum number of mapped registers simultaneously */
#define MAX_MAPPED_REG (uint8_t)15U
/* Return code list during enabling cyclic mode */
/** Cyclic mode reached correctly */
#define CYCLIC_MODE_OK (int32_t)0L
/** Cyclic mode reached correctly */
#define CYCLIC_ERR_RX_MAP (int32_t)-1L
/** Cyclic mode reached correctly */
#define CYCLIC_ERR_TX_MAP (int32_t)-2L
/** Cyclic mode reached correctly */
#define CYCLIC_ERR_VALIDATION (int32_t)-3L
/** Cyclic mode reached correctly */
#define CYCLIC_ERR_SYNC (int32_t)-4L
/** Default MCB Coco Node */
#define DEFAULT_COCO_NODE (uint16_t)0U
/** Default MCB Moco Node */
#define DEFAULT_MOCO_NODE (uint16_t)1U
/** Correct initialization of the MCB interface */
#define MCB_INIT_OK (int32_t)0L
/** Wrong initialization of the MCB interface */
#define MCB_INIT_KO (int32_t)-1L
/** Motion control bus mode of operation */
typedef enum
{
/** Blocking mode, each request block until response */
MCB_BLOCKING = 0,
/** Non Blocking mode, if not ready, return state */
MCB_NON_BLOCKING
} Mcb_EMode;
/** Motion control bus frame types*/
typedef enum
{
/** Cyclic mode without sync */
MCB_CYC_NON_SYNC = 0,
/** Cyclic mode with sync 0 enabled */
MCB_CYC_SYNC0,
/** Cyclic mode with sync 1 enabled */
MCB_CYC_SYNC1,
/** Cyclic mode with sync 0 & sync 1 enabled */
MCB_CYC_SYNC0_SYNC1
} Mcb_ECyclicMode;
/** Frame data struct */
typedef struct
{
/** Destination / source node */
uint16_t u16Node;
/** Target register address */
uint16_t u16Addr;
/** Master / slave command */
uint16_t u16Cmd;
/** Message total size (words) */
uint16_t u16Size;
/** Static data */
uint16_t u16Data[MCB_MAX_DATA_SZ];
/** Message status */
Mcb_EStatus eStatus;
} Mcb_TMsg;
/** Info frame data struct */
typedef struct
{
/** Destination / source node */
uint16_t u16Node;
/** Target register address */
uint16_t u16Addr;
/** Master / slave command */
uint16_t u16Cmd;
/** Message total size (words) */
uint16_t u16Size;
Mcb_TInfoMsgData tInfoMsgData;
/** Message status */
Mcb_EStatus eStatus;
} Mcb_TInfoMsg;
/** List struct to store mapped registers */
typedef struct
{
/** Number of available register on the list */
uint8_t u8Mapped;
/** Word size of mapped registers */
uint16_t u16MappedSize;
/** Array containing key of mapped registers */
uint16_t u16Addr[MAX_MAPPED_REG];
/** Array containing size of mapped registers, in bytes */
uint16_t u16Sz[MAX_MAPPED_REG];
} Mcb_TMappingList;
/** Motion control bus instance */
typedef struct Mcb_TInst Mcb_TInst;
/** Main motion control instance */
struct Mcb_TInst
{
/** Indicates if mcb is in cyclic mode */
volatile bool isCyclic;
/** Indicates the active syncrhonisation config */
Mcb_ECyclicMode eSyncMode;
/** Indicates the timeout applied for blocking transmissions */
uint32_t u32Timeout;
/** Linked mcb module */
Mcb_TIntf tIntf;
/** Transmission mode */
Mcb_EMode eMode;
/** Callback to getinfo function */
void (*Mcb_GetInfo)(Mcb_TInst* ptInst, Mcb_TInfoMsg* pMcbInfoMsg);
/** Callback to read function */
void (*Mcb_Read)(Mcb_TInst* ptInst, Mcb_TMsg* pMcbMsg);
/** Callback to write function */
void (*Mcb_Write)(Mcb_TInst* ptInst, Mcb_TMsg* pMcbMsg);
/** Config transmission Msg request */
Mcb_TMsg tConfigReq;
/** Config transmission Msg reply */
Mcb_TMsg tConfigRpy;
/** Config message user pointer */
Mcb_TMsg* ptUsrConfig;
/** Cyclic transmission (from MCB master point of view) buffer */
uint16_t u16CyclicTx[MCB_FRM_MAX_CYCLIC_SZ];
/** Cyclic reception (from MCB master point of view) buffer */
uint16_t u16CyclicRx[MCB_FRM_MAX_CYCLIC_SZ];
/** Cyclic transmission size */
uint16_t u16CyclicSize;
/** RX mapping (from MCB slave point of view) list */
Mcb_TMappingList tCyclicRxList;
/** TX mapping (from MCB slave point of view) list */
Mcb_TMappingList tCyclicTxList;
/** Callback to config over cyclic frame reception */
void (*CfgOverCyclicEvnt)(Mcb_TInst* ptInst, Mcb_TMsg* pMcbMsg);
};
/**
* Initialization of a mcb instance
*
* @param[in] ptInst
* Instance to be initialized
* @param[in] eMode
* Indicates if blocking or non-blocking mode is applied
* @param[in] u16Id
* Assigns an Id to the instance
* @param[in] bCalcCrc
* If true, @ref Mcb_IntfComputeCrc is called. This function has a built-in CRC,
* or it can be replaced with user-specific implementation.
* If false, no CRC function is called. Used when the CRC is automatically
* computed by hardware.
* @param[in] u32Timeout
* Indicates the applied timeout for blocking tranmissions in milliseconds
*
* @retval 0 if slave IRQ signal is HIGH, -1 otherwise
*/
int32_t Mcb_Init(Mcb_TInst* ptInst, Mcb_EMode eMode, uint16_t u16Id, bool bCalcCrc, uint32_t u32Timeout);
/**
* Deinitializes a mcb instance
*
* @param[in] ptInst
* Instance to be deinitialized
*/
void Mcb_Deinit(Mcb_TInst* ptInst);
/**
* Attach an user callback to the reception event of a config frame over
* Cyclic mode
*
* @note Config over cyclic event can not be attached in blocking mode
*
* @param[in] ptInst
* Instance where callback is going to be linked
* @param[in] Evnt
* User callback to be linked
*/
void
Mcb_AttachCfgOverCyclicCB(Mcb_TInst* ptInst, void (*Evnt)(Mcb_TInst* ptInst, Mcb_TMsg* pMcbMsg));
/**
* Map a Tx cyclic register into the cyclic buffer
*
* @note blocking function
*
* @param[in] ptInst
* Mcb instance where register is mapped
* @param[in] u16Addr
* Key address of the register to be mapped
* @param[in] u16Sz
* Size (bytes) of the register to be mapped
*
* @retval Pointer to cyclic buffer where data is located, NULL if error
*/
void*
Mcb_TxMap(Mcb_TInst* ptInst, uint16_t u16Addr, uint16_t u16Sz);
/**
* Map a Rx cyclic register into the cyclic buffer
*
* @note blocking function
*
* @param[in] ptInst
* Mcb instance where register is mapped
* @param[in] u16Addr
* Key address of the register to be mapped
* @param[in] u16Sz
* Size (bytes) of the register to be mapped
*
* @retval Pointer to cyclic buffer where data is located, NULL if error
*/
void*
Mcb_RxMap(Mcb_TInst* ptInst, uint16_t u16Addr, uint16_t u16Sz);
/**
* Unmap the last Tx mapped register
*
* @note blocking function
*
* @param[in] ptInst
* Mcb instance where register is mapped
*
* @retval Number of remaining mapped registers
*/
uint8_t
Mcb_TxUnmap(Mcb_TInst* ptInst);
/**
* Unmap the last Rx mapped register
*
* @note blocking function
*
* @param[in] ptInst
* Mcb instance where register is mapped
*
* @retval Number of remaining mapped registers
*/
uint8_t
Mcb_RxUnmap(Mcb_TInst* ptInst);
/**
* Unmap All the mapped register
*
* @note blocking function
*
* @param[in] ptInst
* Mcb instance
*/
void
Mcb_UnmapAll(Mcb_TInst* ptInst);
/**
* Enables cyclic mode.
*
* @note Blocking function, while the config is written into driver.
*
* @param[in] ptInst
* Mcb instance
*
* @retval 0 if we are already in cyclic mode.
* > 0 if transition successful, indicating the cyclic size.
* < 0 indicates an errorcode.
*/
int32_t
Mcb_EnableCyclic(Mcb_TInst* ptInst);
/**
* Disables cyclic mode.
*
* @note This function only sets up the config frame, but
* data is not transmitted because cyclic mode is enabled and
* config frames must be transmitted through cyclic transfers.
*
* @param[in] ptInst
* Mcb instance
*
* @retval MCB_CYCLIC_SUCCESS is disable is executed,
* MCB_CYCLIC_ERROR otherwise
*/
Mcb_EStatus
Mcb_DisableCyclic(Mcb_TInst* ptInst);
/**
* Get cyclic mode.
*
* @param[in] ptInst
* Mcb instance
*
* @retval Current cyclic mode.
*/
Mcb_ECyclicMode
Mcb_GetCyclicMode(Mcb_TInst* ptInst);
/**
* Sets the desired cyclic mode.
*
* @note Blocking function, while the config is written into driver.
*
* @param[in] ptInst
* Mcb instance
* @param[in] eNewCycMode Desired cyclic mode.
*
* @retval Current cyclic mode.
*/
Mcb_ECyclicMode
Mcb_SetCyclicMode(Mcb_TInst* ptInst, Mcb_ECyclicMode eNewCycMode);
/**
* Function to be called cyclically when cyclic mode is enabled
*
* @param[in] ptInst
* Mcb instance
*
* @retval true if a cyclic transmission is done
* false otherwise
*/
bool
Mcb_CyclicProcessLatch(Mcb_TInst* ptInst, Mcb_EStatus* eCfgStat);
/**
* Process the received cyclic buffer.
*
* @param[in] ptInst
* Mcb instance
*/
void
Mcb_CyclicFrameProcess(Mcb_TInst* ptInst);
#endif
/** @} */