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
use prelude::v1::*;
use base::*;
use shim::*;
use units::*;
use utils::*;
use isr::*;

unsafe impl Send for Task {}

/// Handle for a FreeRTOS task
pub struct Task {
    task_handle: FreeRtosTaskHandle,
}

/// Task's execution priority
#[derive(Debug, Copy, Clone)]
pub enum TaskPriority {
    BelowNormal,
    Normal,
    AboveNormal,
    High,
}

/// Notification to be sent to a task.
#[derive(Debug, Copy, Clone)]
pub enum TaskNotification {
    /// Send the event, unblock the task, the task's notification value isn't changed.
    NoAction,
    /// Perform a logical or with the task's notification value.
    SetBits(u32),
    /// Increment the task's notification value by one.
    Increment,
    /// Set the task's notification value to this value.
    OverwriteValue(u32),
    /// Try to set the task's notification value to this value. Succeeds
    /// only if the task has no pending notifications. Otherwise, the
    /// notification call will fail.
    SetValue(u32),
}

impl TaskNotification {
    fn to_freertos(&self) -> (u32, u8) {
        match *self {
            TaskNotification::NoAction => (0, 0),
            TaskNotification::SetBits(v) => (v, 1),
            TaskNotification::Increment => (0, 2),
            TaskNotification::OverwriteValue(v) => (v, 3),
            TaskNotification::SetValue(v) => (v, 4),
        }
    }
}

impl TaskPriority {
    fn to_freertos(&self) -> FreeRtosUBaseType {
        match *self {
            TaskPriority::BelowNormal => 6,
            TaskPriority::Normal => 5,
            TaskPriority::AboveNormal => 4,
            TaskPriority::High => 3,
        }
    }
}

/// Helper for spawning a new task. Instantiate with [`Task::new()`].
///
/// [`Task::new()`]: struct.Task.html#method.new
pub struct TaskBuilder {
    task_name: String,
    task_stack_size: u16,
    task_priority: TaskPriority,
}

impl TaskBuilder {
    /// Set the task's name.
    pub fn name(&mut self, name: &str) -> &mut Self {
        self.task_name = name.into();
        self
    }

    /// Set the stack size, in words.
    pub fn stack_size(&mut self, stack_size: u16) -> &mut Self {
        self.task_stack_size = stack_size;
        self
    }

    /// Set the task's priority.
    pub fn priority(&mut self, priority: TaskPriority) -> &mut Self {
        self.task_priority = priority;
        self
    }

    /// Start a new task that can't return a value.
    pub fn start<F>(&self, func: F) -> Result<Task, FreeRtosError>
        where F: FnOnce() -> (),
              F: Send + 'static
    {

        Task::spawn(&self.task_name,
                    self.task_stack_size,
                    self.task_priority,
                    func)

    }
}



impl Task {
    /// Prepare a builder object for the new task.
    pub fn new() -> TaskBuilder {
        TaskBuilder {
            task_name: "rust_task".into(),
            task_stack_size: 1024,
            task_priority: TaskPriority::Normal,
        }
    }

    unsafe fn spawn_inner<'a>(f: Box<FnBox() + Send + 'a>,
                              name: &str,
                              stack_size: u16,
                              priority: TaskPriority)
                              -> Result<Task, FreeRtosError> {
        let f = Box::new(f);
        let param_ptr = &*f as *const _ as *mut _;

        let (success, task_handle) = {
            let name = name.as_bytes();
            let name_len = name.len();
            let mut task_handle = mem::zeroed::<CVoid>();

            let ret = freertos_rs_spawn_task(thread_start,
                                             param_ptr,
                                             name.as_ptr(),
                                             name_len as u8,
                                             stack_size,
                                             priority.to_freertos(),
                                             &mut task_handle);

            (ret == 0, task_handle)
        };

        if success {
            mem::forget(f);
        } else {
            return Err(FreeRtosError::OutOfMemory);
        }

        extern "C" fn thread_start(main: *mut CVoid) -> *mut CVoid {
            unsafe {
                {
                    let b = Box::from_raw(main as *mut Box<FnBox()>);
                    b();
                }

                freertos_rs_delete_task(0 as *const _);
            }

            0 as *mut _
        }

        Ok(Task { task_handle: task_handle as usize as *const _ })
    }


    fn spawn<F>(name: &str,
                stack_size: u16,
                priority: TaskPriority,
                f: F)
                -> Result<Task, FreeRtosError>
        where F: FnOnce() -> (),
              F: Send + 'static
    {
        unsafe {
            return Task::spawn_inner(Box::new(f), name, stack_size, priority);
        }
    }


    /// Get the name of the current task.
    pub fn get_name(&self) -> Result<String, ()> {
        unsafe {
            let name_ptr = freertos_rs_task_get_name(self.task_handle);
            let name = str_from_c_string(name_ptr);
            if let Ok(name) = name {
                return Ok(name);
            }

            Err(())
        }
    }

    /// Try to find the task of the current execution context.
    pub fn current() -> Result<Task, FreeRtosError> {
        unsafe {
            let t = freertos_rs_get_current_task();
            if t != 0 as *const _ {
                Ok(Task { task_handle: t })
            } else {
                Err(FreeRtosError::TaskNotFound)
            }
        }
    }

    /// Forcibly set the notification value for this task.
    pub fn set_notification_value(&self, val: u32) {
        self.notify(TaskNotification::OverwriteValue(val))
    }

    /// Notify this task.
    pub fn notify(&self, notification: TaskNotification) {
        unsafe {
            let n = notification.to_freertos();
            freertos_rs_task_notify(self.task_handle, n.0, n.1);
        }
    }

    /// Notify this task from an interrupt.
    pub fn notify_from_isr(&self,
                           context: &InterruptContext,
                           notification: TaskNotification)
                           -> Result<(), FreeRtosError> {
        unsafe {
            let n = notification.to_freertos();
            let t = freertos_rs_task_notify_isr(self.task_handle,
                                                n.0,
                                                n.1,
                                                context.get_task_field_mut());
            if t != 0 {
                Err(FreeRtosError::QueueFull)
            } else {
                Ok(())
            }
        }
    }

    /// Take the notification and either clear the notification value or decrement it by one.
    pub fn take_notification(&self, clear: bool, wait_for: Duration) -> u32 {
        unsafe { freertos_rs_task_notify_take(if clear { 1 } else { 0 }, wait_for.to_ticks()) }
    }

    /// Wait for a notification to be posted.
    pub fn wait_for_notification(&self,
                                 clear_bits_enter: u32,
                                 clear_bits_exit: u32,
                                 wait_for: Duration)
                                 -> Result<u32, FreeRtosError> {
        unsafe {
            let mut val = 0;
            let r = freertos_rs_task_notify_wait(clear_bits_enter,
                                                 clear_bits_exit,
                                                 &mut val as *mut _,
                                                 wait_for.to_ticks());

            if r == 0 {
                Ok(val)
            } else {
                Err(FreeRtosError::Timeout)
            }
        }
    }
}

/// Helper methods to be performed on the task that is currently executing.
pub struct CurrentTask;
impl CurrentTask {
    pub fn get_tick_count() -> FreeRtosTickType {
        unsafe { freertos_rs_xTaskGetTickCount() }
    }

    /// Delay the execution of the current task.
    pub fn delay(delay: Duration) {
        unsafe {
            freertos_rs_vTaskDelay(delay.to_ticks());
        }
    }
}