Contiki calls protothread programming

Contiki is an open-source, highly portable multitasking operating system designed for networked embedded systems and wireless sensor networks. It is specifically optimized for microcontroller-based environments with very limited memory resources. In typical configurations, Contiki requires only about 2KB of RAM and 40KB of flash memory. Additionally, it offers an optional GUI subsystem that supports graphical interfaces for local serial terminals, VNC-based networked displays, or Telnet connections. The blocking mechanism in Contiki processes is based on the concept of "function return." As illustrated in the figure above, when a process calls `PROCESS_YIELD()`, it essentially returns control back to the ContikiOS kernel, allowing Other processes to run. When a process involves more complex logic, placing all blocking statements inside the main function can lead to overly long and complicated code. This not only makes the code harder to maintain but also contradicts the core principle of software development: simplifying program complexity. In contrast, real-time operating systems like uC/OS-II allow tasks to block using mechanisms such as semaphores, mailboxes, or message queues. These systems rely on context switching, enabling a task to "hang" at any point within a function, as shown in the image. However, Contiki introduces a unique approach called protothreading, which allows for "function-level blocking." As demonstrated in the figure: 1. When a protothread waits for an event, it first calls `PT_YIELD()` to suspend execution. Once the process receives the `PT_YIELDED` return value, it triggers `PROCESS_YIELD()` to hand control back to ContikiOS. 2. When an external event (typically an interrupt) sends a message to ContikiOS, the system schedules the relevant process, which then continues by calling the protothread to handle the event. 3. After the protothread completes its logic, it returns `PT_ENDED` to the process, signaling that the task is done. This function-level blocking technique enables developers to break down complex processes into smaller, manageable protothreads, significantly reducing the complexity of both development and maintenance. In summary, Contiki's scheduling and blocking mechanism fundamentally relies on "function returns," leading to three key design principles for processes: 1. Each process should respond to a hardware polling message. 2. The timing between multiple hardware components should be managed via a state machine handled by the Interrupt Service Routine (ISR). 3. The main process function should remain as simple as possible, with complex logic divided into multiple protothreads. By following these principles, developers can create efficient, modular, and maintainable embedded applications using Contiki.

Rubber Seal

Rubber Seal, Waterproof Rubber Seal, Rubber Sealing Ring

Wenzhou Hesheng Electronic Co., Ltd. , https://www.heshengelec.com