1 /*---------------------------------------------------------------------------- 2 * RL-ARM - RTX 3 *---------------------------------------------------------------------------- 4 * Name: MAIL.C 5 * Purpose: RTX example program 6 *---------------------------------------------------------------------------- 7 * 8 * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH 9 * All rights reserved. 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions are met: 12 * - Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * - Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * - Neither the name of ARM nor the names of its contributors may be used 18 * to endorse or promote products derived from this software without 19 * specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE 25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 *---------------------------------------------------------------------------*/ 33 34 #include <stdio.h> 35 #include "cmsis_os.h" 36 37 /* Thread IDs */ 38 osThreadId tid_thread1; /* assigned ID for thread 1 */ 39 osThreadId tid_thread2; /* assigned ID for thread 2 */ 40 41 typedef struct { /* Mail object structure */ 42 float voltage; /* AD result of measured voltage */ 43 float current; /* AD result of measured current */ 44 uint32_t counter; /* A counter value */ 45 } T_MEAS; 46 47 osMailQDef(mail, 16, T_MEAS); /* Define mail queue */ 48 osMailQId mail; 49 50 /* Forward reference */ 51 void send_thread (void const *argument); 52 void recv_thread (void const *argument); 53 54 /* Thread definitions */ 55 osThreadDef(send_thread, osPriorityNormal, 1, 0); 56 osThreadDef(recv_thread, osPriorityNormal, 1, 2000); 57 58 59 /*---------------------------------------------------------------------------- 60 * Thread 1: Send thread 61 *---------------------------------------------------------------------------*/ 62 void send_thread (void const *argument) { 63 T_MEAS *mptr; 64 65 mptr = osMailAlloc(mail, osWaitForever); /* Allocate memory */ 66 mptr->voltage = 223.72; /* Set the mail content */ 67 mptr->current = 17.54; 68 mptr->counter = 120786; 69 osMailPut(mail, mptr); /* Send Mail */ 70 osDelay(100); 71 72 mptr = osMailAlloc(mail, osWaitForever); /* Allocate memory */ 73 mptr->voltage = 227.23; /* Prepare 2nd mail */ 74 mptr->current = 12.41; 75 mptr->counter = 170823; 76 osMailPut(mail, mptr); /* Send Mail */ 77 osThreadYield(); /* Cooperative multitasking */ 78 osDelay(100); 79 80 mptr = osMailAlloc(mail, osWaitForever); /* Allocate memory */ 81 mptr->voltage = 229.44; /* Prepare 3rd mail */ 82 mptr->current = 11.89; 83 mptr->counter = 237178; 84 osMailPut(mail, mptr); /* Send Mail */ 85 osDelay(100); 86 /* We are done here, exit this thread */ 87 } 88 89 /*---------------------------------------------------------------------------- 90 * Thread 2: Receive thread 91 *---------------------------------------------------------------------------*/ 92 void recv_thread (void const *argument) { 93 T_MEAS *rptr; 94 osEvent evt; 95 96 for (;;) { 97 evt = osMailGet(mail, osWaitForever); /* wait for mail */ 98 if (evt.status == osEventMail) { 99 rptr = evt.value.p; 100 printf ("\nVoltage: %.2f V\n",rptr->voltage); 101 printf ("Current: %.2f A\n",rptr->current); 102 printf ("Number of cycles: %d\n",(int)rptr->counter); 103 #ifdef __USE_FFLUSH 104 fflush (stdout); 105 #endif 106 osMailFree(mail, rptr); /* free memory allocated for mail */ 107 } 108 } 109 } 110 111 /*---------------------------------------------------------------------------- 112 * Main: 113 *---------------------------------------------------------------------------*/ 114 int main (void) { /* program execution starts here */ 115 116 mail = osMailCreate(osMailQ(mail), NULL); /* create mail queue */ 117 118 tid_thread1 = osThreadCreate(osThread(send_thread), NULL); 119 tid_thread2 = osThreadCreate(osThread(recv_thread), NULL); 120 121 osDelay(osWaitForever); 122 for (;;); 123 } 124 125 /*---------------------------------------------------------------------------- 126 * end of file 127 *---------------------------------------------------------------------------*/
1 /*---------------------------------------------------------------------------- 2 * RL-ARM - RTX 3 *---------------------------------------------------------------------------- 4 * Name: MESSAGE.C 5 * Purpose: RTX example program 6 *---------------------------------------------------------------------------- 7 * 8 * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH 9 * All rights reserved. 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions are met: 12 * - Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * - Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * - Neither the name of ARM nor the names of its contributors may be used 18 * to endorse or promote products derived from this software without 19 * specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE 25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 *---------------------------------------------------------------------------*/ 33 34 #include <stdio.h> 35 #include "cmsis_os.h" 36 37 /* Thread IDs */ 38 osThreadId tid_thread1; /* assigned ID for thread 1 */ 39 osThreadId tid_thread2; /* assigned ID for thread 2 */ 40 41 typedef struct { /* Message object structure */ 42 float voltage; /* AD result of measured voltage */ 43 float current; /* AD result of measured current */ 44 uint32_t counter; /* A counter value */ 45 } T_MEAS; 46 47 osPoolDef(mpool, 16, T_MEAS); /* Define memory pool */ 48 osPoolId mpool; 49 osMessageQDef(MsgBox, 16, T_MEAS); /* Define message queue */ 50 osMessageQId MsgBox; 51 52 /* Forward reference */ 53 void send_thread (void const *argument); 54 void recv_thread (void const *argument); 55 56 /* Thread definitions */ 57 osThreadDef(send_thread, osPriorityNormal, 1, 0); 58 osThreadDef(recv_thread, osPriorityNormal, 1, 2000); 59 60 61 /*---------------------------------------------------------------------------- 62 * Thread 1: Send thread 63 *---------------------------------------------------------------------------*/ 64 void send_thread (void const *argument) { 65 T_MEAS *mptr; 66 67 mptr = osPoolAlloc(mpool); /* Allocate memory for the message */ 68 mptr->voltage = 223.72; /* Set the message content */ 69 mptr->current = 17.54; 70 mptr->counter = 120786; 71 osMessagePut(MsgBox, (uint32_t)mptr, osWaitForever); /* Send Message */ 72 osDelay(100); 73 74 mptr = osPoolAlloc(mpool); /* Allocate memory for the message */ 75 mptr->voltage = 227.23; /* Prepare a 2nd message */ 76 mptr->current = 12.41; 77 mptr->counter = 170823; 78 osMessagePut(MsgBox, (uint32_t)mptr, osWaitForever); /* Send Message */ 79 osThreadYield(); /* Cooperative multitasking */ 80 osDelay(100); 81 82 mptr = osPoolAlloc(mpool); /* Allocate memory for the message */ 83 mptr->voltage = 229.44; /* Prepare a 3rd message */ 84 mptr->current = 11.89; 85 mptr->counter = 237178; 86 osMessagePut(MsgBox, (uint32_t)mptr, osWaitForever); /* Send Message */ 87 osDelay(100); 88 /* We are done here, exit this thread */ 89 } 90 91 /*---------------------------------------------------------------------------- 92 * Thread 2: Receive thread 93 *---------------------------------------------------------------------------*/ 94 void recv_thread (void const *argument) { 95 T_MEAS *rptr; 96 osEvent evt; 97 98 for (;;) { 99 evt = osMessageGet(MsgBox, osWaitForever); /* wait for message */ 100 if (evt.status == osEventMessage) { 101 rptr = evt.value.p; 102 printf ("\nVoltage: %.2f V\n",rptr->voltage); 103 printf ("Current: %.2f A\n",rptr->current); 104 printf ("Number of cycles: %d\n",(int)rptr->counter); 105 #ifdef __USE_FFLUSH 106 fflush (stdout); 107 #endif 108 osPoolFree(mpool,rptr); /* free memory allocated for message */ 109 } 110 } 111 } 112 113 /*---------------------------------------------------------------------------- 114 * Main: 115 *---------------------------------------------------------------------------*/ 116 int main (void) { /* program execution starts here */ 117 118 mpool = osPoolCreate(osPool(mpool)); /* create memory pool */ 119 MsgBox = osMessageCreate(osMessageQ(MsgBox), NULL); /* create msg queue */ 120 121 tid_thread1 = osThreadCreate(osThread(send_thread), NULL); 122 tid_thread2 = osThreadCreate(osThread(recv_thread), NULL); 123 124 osDelay(osWaitForever); 125 for (;;); 126 } 127 128 /*---------------------------------------------------------------------------- 129 * end of file 130 *---------------------------------------------------------------------------*/
CMSIS Example - Mail and Message
时间: 2024-10-24 09:53:59