summaryrefslogtreecommitdiff
path: root/cleopatre/plcdrv/arm/src/mailbox.c
blob: 6d1f4c1134c9272d4a7a869f70cda97fd485a38d (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
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
356
357
358
359
360
361
362
363
364
365
366
/* Cleopatre project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    mailbox.c
 * \brief   Mailbox Layer
 * \ingroup Cleopatre - PlcDrv
 *
 * This file content the mailbox layer, this layer will
 * provide all mechanisms to manage mailboxes.
 */

#ifndef __UTESTS__
#include "common.h"
#include "processing.h"
#include "mailbox.h"
#include "hal.h"
#else
#include "common.h"
#include "mailbox.h"
#endif

/** Define Debug/Trace Level */
#define TRACE(...)      printk("SPC300: " __VA_ARGS__)
//#define TRACE(...)

/** Data parameters */
#define MBX_MSG_DATA_TYPE_DATA 0
#define MBX_MSG_DATA_TYPE_MME  1
typedef struct {
    uint32_t type         :8;
    uint32_t length       :4;
    uint32_t param_type   :1;
    uint32_t param_length :11;
    uint32_t              :8;
} __attribute__ ((__packed__)) mbx_data_hdr_t;

/** Buffer add parameters */
#define MBX_MSG_BUFF_ADD_TYPE_DATA       0
#define MBX_MSG_BUFF_ADD_TYPE_MME        1
#define MBX_MSG_BUFF_ADD_TYPE_INTERFACE  2
typedef struct {
    uint32_t type         :8;
    uint32_t length       :4;
    uint32_t param_type   :2;
    uint32_t reserved     :18;
} __attribute__ ((__packed__)) mbx_buffer_add_hdr_t;

/** Send done parameters */
typedef struct {
    uint32_t type         :8;
    uint32_t length       :4;
    uint32_t reserved     :20;
} __attribute__ ((__packed__)) mbx_send_done_hdr_t;

/** Interface parameters */
#define MBX_MSG_INTERFACE_TYPE_INTERFACE 0
#define MBX_MSG_INTERFACE_TYPE_SNIFFER   1
#define MBX_MSG_INTERFACE_TYPE_FCALL     2
typedef struct {
    uint32_t type         :8;
    uint32_t length       :4;
    uint32_t param_type   :8;
    uint32_t param_length :11;
    uint32_t reserved     :1;
} __attribute__ ((__packed__)) mbx_interface_hdr_t;

/** Mailbox type */
#define MBX_MSG_TYPE_DATA       0
#define MBX_MSG_TYPE_BUFFER_ADD 1
#define MBX_MSG_TYPE_SEND_DONE  2
#define MBX_MSG_TYPE_INTERFACE  3

/** Mailbox Header Structure */
typedef union {
    mbx_data_hdr_t data;
    mbx_buffer_add_hdr_t buffer_add;
    mbx_send_done_hdr_t send_done;
    mbx_interface_hdr_t interface;
} mbx_hdr_t;

/** Mailbox Data Structure */
typedef union {
    uint32_t buffer_ptr;
    uint32_t further_use;
} __attribute__ ((__packed__)) mbx_data_t;

/** Mailbox Structure */
typedef struct {
    mbx_hdr_t header;
    mbx_data_t data;
} __attribute__ ((__packed__)) mbx_t;


/** 
 * Initialize the mailbox layer
 */
void mailbox_init(void)
{
    //Initialize registers
    halmbx_init();

    //Stop all interrupts
    A2La_it_disable();
    L2At_it_disable();

    //Unmask sending interrupts
    L2At_it_enable();
}// mailbox_init

/** 
 * UnInitialize the mailbox layer
 */
void mailbox_uninit(void)
{
    //Mask all interrupts
    A2La_it_disable();
    L2At_it_disable();

    //Uninit lower layer
    halmbx_uninit();
}// mailbox_uninit

/** 
 * Give the message to hardware
 *
 * \param  msg  message pointer.
 * \param  size  size of message.
 * \return  error code or status queue.
 */
int internal_mailbox_send(uint32_t *msg, int size)
{
    int status;

    //Check pointer
    if(msg == NULL)
        return -1;

    //Check room
    status = halmbx_A2Lmail_status_queue();
    if(status == NEARLY_FULL || status == FULL)
    {
        //The mailbox is nearly full stop upper layers
        //and activate tx_ack interrupt
        A2La_it_enable();
        if(status == FULL)
            return status;
    }
    else
    {
        A2La_it_disable();
    }

    //Copy to ring buffer
    if(halmbx_copy_to_ring(msg, size))
        return -1;

    //Update the ring management
    if(halmbx_A2Lmail_update(size))
        return -1;

    return status;
}// internal_mailbox_send

/** 
 * Prepare the mailbox message
 * corresponding to a sending frame
 * and send it to CESAR.
 *
 * \param  pointer  the data message pointer.
 * \param  length  the length of the data message.
 * \param  type  type of message.
 * \return  error code
 */
int mailbox_send(void *pointer, uint32_t length, enum buffer_type type)
{
    mbx_t msg;

    //Check pointer and length
    if(pointer == NULL || length == 0)
        return -1;

    //Reset message variable
    memset(&msg, 0, sizeof(msg));

    //Header construction
    switch(type)
    {
    case DATA:
        msg.header.data.type  = MBX_MSG_TYPE_DATA;
        msg.header.data.length = 1;
        TRACE("SEND data data\n");
        msg.header.data.param_type = MBX_MSG_DATA_TYPE_DATA;
        msg.header.data.param_length = length;
        break;
    case MME:
        msg.header.data.type  = MBX_MSG_TYPE_DATA;
        msg.header.data.length = 1;
        TRACE("SEND data mme\n");
        msg.header.data.param_type = MBX_MSG_DATA_TYPE_MME;
        msg.header.data.param_length = length;
        break;
    case INTERFACE:
        msg.header.interface.type  = MBX_MSG_TYPE_INTERFACE;
        msg.header.interface.length = 1;
        TRACE("SEND interface\n");
        msg.header.interface.param_type = MBX_MSG_INTERFACE_TYPE_FCALL;
        msg.header.interface.param_length = length;
        break;
    default:
        return -1;
    }

    //Data construction
    msg.data.buffer_ptr = (uint32_t)pointer;

#ifdef __UTESTS__
    //For Utests we need to return the msg to check it
    memcpy(mbx_send_result, &msg, sizeof(mbx_send_result));
#endif

    //Send the message
    //we consider that all sending
    //messages(data or interface)
    //have field length at the same position
    return internal_mailbox_send((uint32_t*)&msg, sizeof(mbx_hdr_t) + (msg.header.data.length * sizeof(uint32_t)));
}// mailbox_send

/** 
 * Prepare the mailbox message
 * corresponding to a new buffer
 * and send it to CESAR.
 *
 * \param  pointer  the buffer pointer.
 * \param  type  type of message.
 * \return  error code
 */
int mailbox_buffer_add(void *pointer, enum buffer_type type)
{
    mbx_t msg;

    //Check pointer
    if(pointer == NULL)
        return -1;

    //Reset message variable
    memset(&msg, 0, sizeof(msg));

    //Header construction
    msg.header.buffer_add.type  = MBX_MSG_TYPE_BUFFER_ADD;
    msg.header.buffer_add.length = 1;
    switch(type)
    {
    case DATA:
        TRACE("SEND BUFF ADD type data\n");
        msg.header.buffer_add.param_type = MBX_MSG_BUFF_ADD_TYPE_DATA;
        break;
    case MME:
        TRACE("SEND BUFF ADD type mme\n");
        msg.header.buffer_add.param_type = MBX_MSG_BUFF_ADD_TYPE_MME;
        break;
    case INTERFACE:
        TRACE("SEND BUFF ADD type interface\n");
        msg.header.buffer_add.param_type = MBX_MSG_BUFF_ADD_TYPE_INTERFACE;
        break;
    default:
        return -1;
    }

    //Data construction
    msg.data.buffer_ptr = (uint32_t)pointer;

#ifdef __UTESTS__
    //For Utests we need to return the msg to check it
    memcpy(mbx_buffer_add_result, &msg, sizeof(mbx_buffer_add_result));
#endif

    //Send the message
    return internal_mailbox_send((uint32_t*)&msg, sizeof(mbx_hdr_t) + (msg.header.buffer_add.length * sizeof(uint32_t)));
}// mailbox_buffer_add

/** 
 * Receive everything from the HLE layer
 *
 * \return  error code
 */
int mailbox_receive(void)
{
    mbx_t msg;
    int result=0;

    while(halmbx_L2Amail_not_empty_queue())
    {
        //Copy the new message from the ring buffer
        //we copy the max size of a message even it's to big
        if(halmbx_copy_from_ring((uint32_t*)&msg, sizeof(msg)))
            return -2;

        //Update the ring management
        //we consider that all received
        //messages(data, send_done or interface)
        //have field length at the same position
        if(halmbx_L2Amail_update(sizeof(mbx_hdr_t) + (msg.header.data.length * sizeof(uint32_t))))
            return -3;

        //Proceed the message
        //we consider that all received
        //messages(data, send_done or interface)
        //have field type at the same position
        switch(msg.header.send_done.type)
        {
        case MBX_MSG_TYPE_SEND_DONE:
            TRACE("RECEIVE a send_done\n");
            if(processing_buffer_free((void*)msg.data.buffer_ptr))
                result = -4;
            else
                result = 0;
            break;

        case MBX_MSG_TYPE_INTERFACE:
            TRACE("RECEIVE an interface\n");
            //Transmit to the upper layers
            if(processing_receive((void*)msg.data.buffer_ptr, msg.header.interface.param_length, INTERFACE))
            {
                result = -6;
                break;
            }
            result = 0;
            break;

        case MBX_MSG_TYPE_DATA:
            //Transmit to the upper layers
            if(msg.header.data.param_type == MBX_MSG_DATA_TYPE_MME)
            {
                TRACE("RECEIVE a data data\n");
                if(processing_receive((void*)msg.data.buffer_ptr, msg.header.data.param_length, MME))
                {
                    result = -8;
                    break;
                }
            }
            else
            {
                TRACE("RECEIVE a data mme\n");
                if(processing_receive((void*)msg.data.buffer_ptr, msg.header.data.param_length, DATA))
                {
                    result = -9;
                    break;
                }
            }
            result = 0;
            break;
        default:
            result = -1;
            break;
        }
        if(result)
            break;
    }
    return result;
}