"LPC54100" running on M0 event-driven Architecture (i)

Source: Internet
Author: User

@20150216

The first contact with asymmetric dual-core MCU, due to a lot of knowledge unfamiliar, these three days encountered a lot of problems. Now finally the event driven on the M0 nuclear run up, send a brief introduction of this event-driven architecture and the 5410x chip learning from these days.
With regard to the event-driven architecture, it is mainly used for low power design and can easily enter sleep mode. General single-chip microcomputer bare Ben are polling, such as:

    1. void Main ()
    2. {
    3. while (1)
    4. {
    5. Tesk1 ();
    6. Tesk2 ();
    7. Tesk3 ();
    8. }
    9. }

Copy Code

In this polling process, it is difficult to determine when to hibernate, how long to sleep, and what level of sleep to use.

The event-driven main function is as follows (take VSF as an example):

  1. while (1)
  2. {
  3. Vsfsm_poll ();
  4. Vsf_enter_critical ();
  5. if (!vsfsm_get_event_pending ())
  6. {
  7. Vsf_leave_critical ();
  8. __wfi ();
  9. }
  10. Else
  11. {
  12. Vsf_leave_critical ();
  13. }
  14. }

Copy Code

VSF adds an event queue to the program that sleeps when the event queue is empty. The sleep time is determined by the wake-up timer configured by the program, and of course some external hardware interrupts can also be awakened. If you need to use a different sleep level, you also need to do a sleep level manager, different threads require different sleep levels, and then the manager decides the sleep level. Of course, these fine-grained configuration will be more troublesome ...

In fact, the event-driven capable, in the RTOs are capable, many things rtos can do better, more comfortable. However, the event-driven has one of the biggest advantages: the province of RAM, in many low-power MCU, RAM is generally small, although the RTOs can be cropped, but the allocation of each task stack can not be cropped, when more tasks, RAM allocation is also very annoying. The event driver is still a bare-Ben, just put the thread (a function pointer plus an event state) in the queue for management, one by one call. Each thread only spends more than 10 bytes of RAM.

This time with Simon's event-driven VSF platform, the code is on GitHub, and Github.com/versaloon/vsf,21ic has some introductory posts that are interested to see for themselves.
The following example code caught dead, very very coarse, the code in the post added a Chinese explanation

  1. Thread 1
  2. Define an event state
  3. #define EVENT_1_USER_LOCAL_SCANF (vsfsm_evt_user_local + 1)
  4. static struct vsfsm_state_t *
  5. Event_1_handler (struct vsfsm_t *sm, vsfsm_evt_t evt);
  6. struct vsfsm_t event_1_sm =
  7. {
  8. {Event_1_handler},//Initialize Handler
  9. };
  10. For generating periodic event timers at intervals of 5 seconds
  11. static struct vsftimer_timer_t Event_1_timer =
  12. {
  13. 5000,//interval is 5 seconds
  14. &EVENT_1_SM,//corresponding state machine
  15. EVENT_1_USER_LOCAL_SCANF,//Cycle events
  16. };
  17. struct vsfsm_state_t *
  18. Event_1_handler (struct vsfsm_t *sm, vsfsm_evt_t evt)
  19. {
  20. Switch (EVT)
  21. {
  22. Case VSFSM_EVT_INIT://Initialize event, default execution
  23. Vsftimer_register (&event_1_timer);
  24. Break
  25. Case EVENT_1_USER_LOCAL_SCANF://Periodic event handling
  26. Board_uartputstr ("Event 1rn"); Print out "Event 1"
  27. Print_tick (); Print the current system tick
  28. Break
  29. Default
  30. Break
  31. }
  32. return NULL;
  33. }
  34. Thread 2, change the thread interval to 3 seconds
  35. #define EVENT_2_USER_LOCAL_SCANF (vsfsm_evt_user_local + 1)
  36. static struct vsfsm_state_t *
  37. Event_2_handler (struct vsfsm_t *sm, vsfsm_evt_t evt);
  38. struct vsfsm_t event_2_sm =
  39. {
  40. {Event_2_handler},
  41. };
  42. static struct vsftimer_timer_t Event_2_timer =
  43. {
  44. 3000,
  45. &EVENT_2_SM,
  46. EVENT_2_USER_LOCAL_SCANF,
  47. };
  48. struct vsfsm_state_t *
  49. Event_2_handler (struct vsfsm_t *sm, vsfsm_evt_t evt)
  50. {
  51. Switch (EVT)
  52. {
  53. Case Vsfsm_evt_init:
  54. Vsftimer_register (&event_2_timer);
  55. Break
  56. Case EVENT_2_USER_LOCAL_SCANF:
  57. Board_uartputstr ("event 2rn");
  58. Print_tick ();
  59. Break
  60. Default
  61. Break
  62. }
  63. return NULL;
  64. }
  65. int main (void)
  66. {
  67. Serial initialization
  68. Board_uart_init ();
  69. Wake with Utick interrupt
  70. Board_utick_init ();
  71. Vsftimer_init ();
  72. Board_uartputstr ("Start!rn");
  73. Vsfsm_init (&EVENT_1_SM);
  74. Vsfsm_init (&EVENT_2_SM);
  75. Vsf_leave_critical ();
  76. while (1)
  77. {
  78. Vsfsm_poll (); Polling state machine
  79. Vsf_enter_critical ();
  80. if (!vsfsm_get_event_pending ())//Determine if there are unhandled events
  81. {
  82. Vsf_leave_critical ();
  83. __wfi ();
  84. }
  85. Else
  86. {
  87. Vsf_leave_critical ();
  88. }
  89. }
  90. }

Copy Code

Serial output:

PS: Now the Vsftimer does not do the dynamic management of sleep time, but rather a simple rough timing wake-up once to see if there are any events to deal with. This feature I will be perfected in the back.
There's a lot of fun in Simon's architecture, and it's interesting to study.

"LPC54100" running on M0 event-driven Architecture (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.