PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
esos_mail.c
1 /*
2  * "Copyright (c) 2013 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 #include "esos.h"
31 #include "esos_mail.h"
32 
33 // ******** G L O B A L S ***************
34 //MAILBOX __astMailbox[MAX_NUM_USER_TASKS];
35 //uint8_t __au8_MBData[MAX_NUM_USER_TASKS][MAX_SIZE_TASK_MAILBOX];
36 uint8_t __u8_esos_mail_routines_dummy_uint8;
37 
38 /****************************************************************
39 ** F U N C T I O N S
40 ****************************************************************/
41 void __esos_InitMailbox(MAILBOX* pst_Mailbox, uint8_t* pau8_ptr) {
42  __esos_CB_Init( pst_Mailbox->pst_CBuffer, pau8_ptr, MAX_SIZE_TASK_MAILBOX);
43 } // endof esos_InitMailbox()
44 
45 /**
46 * Writes message data to a task's mailbox.
47 *
48 * \param pst_RcvrTask pointer to task structure (ESOS_TASK_HANDLE) whose mailbox will be written
49 * \param pst_Msg pointer to mailbox message structure that contains data to write to the task's mailbox
50 * \note This function <em>ASSUMES</em> that there is ample free space available in specified
51 * mailbox.
52 *
53 * \sa ESOS_TASK_WAIT_ON_TASKS_MAILBOX_HAS_SPACE
54 * \hideinitializer
55 */
56 void __esos_SendMailMessage(ESOS_TASK_HANDLE pst_RcvrTask, MAILMESSAGE* pst_Msg ) {
57  uint8_t u8_i;
58 
59  // first message btye: flags in upper nibble, payload length in lower
60  u8_i = ((pst_Msg->u8_flags)<<4) + (pst_Msg->u8_DataLength & 0x0F);
61  __esos_CB_WriteUINT8( pst_RcvrTask->pst_Mailbox->pst_CBuffer, u8_i );
62  // second message word: Task ID of sending task
63  __esos_CB_WriteUINT16( pst_RcvrTask->pst_Mailbox->pst_CBuffer, ESOS_GET_PMSG_FROMTASK(pst_Msg) );
64  // Now, timestamp the message with double word
65  __esos_CB_WriteUINT32( pst_RcvrTask->pst_Mailbox->pst_CBuffer, esos_GetSystemTick() );
66  // Now write the data depending on what type and how many
67  if ( ESOS_GET_PMSG_FLAGS(pst_Msg) & ESOS_MAILMESSAGE_UINT8) {
68  for (u8_i=0; u8_i<ESOS_GET_PMSG_DATA_LENGTH(pst_Msg); u8_i++) {
69  __esos_CB_WriteUINT8( pst_RcvrTask->pst_Mailbox->pst_CBuffer, pst_Msg->au8_Contents[u8_i] );
70  } // end for UINT8s
71  } else if ( ESOS_GET_PMSG_FLAGS(pst_Msg) & ESOS_MAILMESSAGE_UINT16) {
72  for (u8_i=0; u8_i<pst_Msg->u8_DataLength; u8_i++) {
73  __esos_CB_WriteUINT16( pst_RcvrTask->pst_Mailbox->pst_CBuffer, pst_Msg->au16_Contents[u8_i] );
74  } // end for UINT16s
75  } else if ( ESOS_GET_PMSG_FLAGS(pst_Msg) & ESOS_MAILMESSAGE_UINT32) {
76  for (u8_i=0; u8_i<pst_Msg->u8_DataLength; u8_i++) {
77  __esos_CB_WriteUINT32( pst_RcvrTask->pst_Mailbox->pst_CBuffer, pst_Msg->au32_Contents[u8_i] );
78  } // end for UINT32s
79  } else {
80  } // end for STRINGs
81 } // endof __esos_SendMailMessage()
82 
83 /**
84 * Reads the next (oldest) waiting message from a task's mailbox.
85 *
86 * \param pstTask pointer to task structure (ESOS_TASK_HANDLE) whose mailbox will be written
87 * \param pstMsg pointer to mailbox message structure that contains data to write to the task's mailbox
88 * \note This function <em>ASSUMES</em> that there is ample free space available in specified
89 * mailbox.
90 *
91 * \sa ESOS_TASK_WAIT_ON_TASKS_MAILBOX_HAS_SPACE
92 * \hideinitializer
93 */
94 void __esos_ReadMailMessage(ESOS_TASK_HANDLE pst_Task, MAILMESSAGE* pst_Message ) {
95  uint8_t u8_i;
96  ESOS_TASK_HANDLE pst_From;
97 
98  /* first message btye: flags in upper nibble, payload length in lower */
99  u8_i = __esos_CB_ReadUINT8( pst_Task->pst_Mailbox->pst_CBuffer );
100  pst_Message->u8_flags = u8_i>>4;
101  pst_Message->u8_DataLength = u8_i & 0x0F;
102  /* second message byte: Task ID of sending task */
103  pst_Message->u16_FromTaskID = __esos_CB_ReadUINT16( pst_Task->pst_Mailbox->pst_CBuffer );
104  /* Now, timestamp the message */
105  pst_Message->u32_Postmark = __esos_CB_ReadUINT32( pst_Task->pst_Mailbox->pst_CBuffer );
106  /* Now write the data depending on what type and how many */
107  if ( ESOS_GET_PMSG_FLAGS(pst_Message) & ESOS_MAILMESSAGE_UINT8) {
108  for (u8_i=0; u8_i<ESOS_GET_PMSG_DATA_LENGTH(pst_Message); u8_i++) {
109  pst_Message->au8_Contents[u8_i] = __esos_CB_ReadUINT8( pst_Task->pst_Mailbox->pst_CBuffer );
110  } // end for UINT8s
111  } else if ( ESOS_GET_PMSG_FLAGS(pst_Message) & ESOS_MAILMESSAGE_UINT16) {
112  for (u8_i=0; u8_i<pst_Message->u8_DataLength; u8_i++) {
113  pst_Message->au16_Contents[u8_i] = __esos_CB_ReadUINT16( pst_Task->pst_Mailbox->pst_CBuffer );
114  } // end for UINT16s
115  } else if ( ESOS_GET_PMSG_FLAGS(pst_Message) & ESOS_MAILMESSAGE_UINT32) {
116  for (u8_i=0; u8_i<pst_Message->u8_DataLength; u8_i++) {
117  pst_Message->au32_Contents[u8_i] = __esos_CB_ReadUINT32( pst_Task->pst_Mailbox->pst_CBuffer );
118  } // end for UINT32s
119  } else {
120  } // end strings
121  /* If sending task requests ACK a.k.a. "delivery confirmation, then do it */
122  if ( ESOS_GET_PMSG_FLAGS(pst_Message) & ESOS_MAILMESSAGE_REQUEST_ACK) {
123  pst_From = esos_GetTaskHandleFromID( ESOS_GET_PMSG_FROMTASK(pst_Message) );
124  if (pst_From != NULLPTR) {
125  __ESOS_CLEAR_TASK_MAILNACK_FLAG( pst_From );
126  } // end if ! NULLPTR
127  } //end if
128 } // __esos_ReadMailMessage()
129 
130 
131