FreeRTOS C++ Wrappers  1.6.0
C++ interface to FreeRTOS
ctasklet.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
4  *
5  * This file is part of the FreeRTOS Add-ons project.
6  *
7  * Source Code:
8  * https://github.com/michaelbecker/freertos-addons
9  *
10  * Project Page:
11  * http://michaelbecker.github.io/freertos-addons/
12  *
13  * On-line Documentation:
14  * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining a
17  * copy of this software and associated documentation files
18  * (the "Software"), to deal in the Software without restriction, including
19  * without limitation the rights to use, copy, modify, merge, publish,
20  * distribute, sublicense, and/or sell copies of the Software, and to
21  * permit persons to whom the Software is furnished to do so,subject to the
22  * following conditions:
23  *
24  * + The above copyright notice and this permission notice shall be included
25  * in all copies or substantial portions of the Software.
26  * + Credit is appreciated, but not required, if you find this project
27  * useful enough to include in your application, product, device, etc.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
33  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
34  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36  *
37  ***************************************************************************/
38 
39 
40 #include "tasklet.hpp"
41 
42 
43 using namespace cpp_freertos;
44 
45 
47 {
48  DtorLock = xSemaphoreCreateBinary();
49 
50  if (DtorLock == NULL) {
51 #ifndef CPP_FREERTOS_NO_EXCEPTIONS
52  throw TaskletCreateException("Create DtorLock Failed");
53 #else
54  configASSERT(!"Tasklet Constructor Failed");
55 #endif
56  }
57 
58  xSemaphoreGive(DtorLock);
59 }
60 
61 
63 {
64 }
65 
66 
68 {
69  xSemaphoreTake(DtorLock, portMAX_DELAY);
70  vSemaphoreDelete(DtorLock);
71 }
72 
73 
74 void Tasklet::TaskletAdapterFunction(void *reference, uint32_t parameter)
75 {
76  Tasklet *tasklet = static_cast<Tasklet *>(reference);
77  tasklet->Run(parameter);
78  xSemaphoreGive(tasklet->DtorLock);
79 }
80 
81 
82 bool Tasklet::Schedule( uint32_t parameter,
83  TickType_t CmdTimeout)
84 {
85  BaseType_t rc;
86 
87  xSemaphoreTake(DtorLock, portMAX_DELAY);
88 
89  rc = xTimerPendFunctionCall(TaskletAdapterFunction,
90  this,
91  parameter,
92  CmdTimeout);
93 
94  if (rc == pdPASS) {
95  return true;
96  }
97  else {
98  xSemaphoreGive(DtorLock);
99  return false;
100  }
101 }
102 
103 
104 bool Tasklet::ScheduleFromISR( uint32_t parameter,
105  BaseType_t *pxHigherPriorityTaskWoken)
106 {
107  BaseType_t rc;
108 
109  rc = xSemaphoreTakeFromISR(DtorLock, pxHigherPriorityTaskWoken);
110 
111  if (rc != pdTRUE) {
112  return false;
113  }
114 
115  rc = xTimerPendFunctionCallFromISR( TaskletAdapterFunction,
116  this,
117  parameter,
118  pxHigherPriorityTaskWoken);
119 
120  if (rc == pdPASS) {
121  return true;
122  }
123  else {
124  xSemaphoreGive(DtorLock);
125  return false;
126  }
127 }
128 
bool ScheduleFromISR(uint32_t parameter, BaseType_t *pxHigherPriorityTaskWoken)
Definition: ctasklet.cpp:104
bool Schedule(uint32_t parameter, TickType_t CmdTimeout=portMAX_DELAY)
Definition: ctasklet.cpp:82
virtual void Run(uint32_t parameter)=0
SemaphoreHandle_t DtorLock
Definition: tasklet.hpp:209
static void TaskletAdapterFunction(void *ref, uint32_t parameter)
Definition: ctasklet.cpp:74