[关闭]
@zwh8800 2017-08-23T02:17:24.000000Z 字数 1836 阅读 190665

自己造操作系统 (2) – zOS-0.2 版本发布!

blog 归档 操作系统


自己造操作系统系列之 0.2 版


新增了信号量功能, 实现进程间同步.

zOS 暂时分成两条线, 一条是上文说的那些功能 (类似于宏内核, 加载执行)

另一条是今天发布的 zOS-mini, 只包含调度, 同步设施. 而且只需要三个文件

还是只能在 stm32 上运行

链接: https://lengzzz.com/zOS/zOS_mini-0.2.zip

下面是一个测试用例
四个线程, 以不同的时间闪烁 led, 线程间通过 sem 信号量同步:

  1. #include <stm32f10x.h>
  2. #include <stm32f10x_rcc.h>
  3. #include <stm32f10x_gpio.h>
  4. #include "zOS.h"
  5. void init();
  6. #define BitP(addr, n) (*(volatile unsigned int *)(0x42000000 + (((unsigned)(addr) - 0x40000000) * 8 + (n)) * 4))
  7. #define BitM(addr, n) (*(volatile unsigned int *)(0x22000000 + (((unsigned)(addr) - 0x20000000) * 8 + (n)) * 4))
  8. #define STACK_SIZE 32
  9. semaphore sem;
  10. uint32_t stack0[STACK_SIZE];
  11. void process0()
  12. {
  13. while (1)
  14. {
  15. BitP(&GPIOA->ODR, 4) = 1;
  16. delay_ms(1000);
  17. BitP(&GPIOA->ODR, 4) = 0;
  18. delay_ms(1000);
  19. }
  20. }
  21. uint32_t stack1[STACK_SIZE];
  22. void process1()
  23. {
  24. while (1/*SYS_TICK < 10 * 1000 * 1000*/)
  25. {
  26. down_sem(&sem);
  27. BitP(&GPIOA->ODR, 0) = 1;
  28. delay_ms(125);
  29. BitP(&GPIOA->ODR, 0) = 0;
  30. delay_ms(125);
  31. }
  32. //create_process(&stack0[STACK_SIZE], process0);
  33. //kill_process();
  34. }
  35. uint32_t stack2[STACK_SIZE];
  36. void process2()
  37. {
  38. while (1)
  39. {
  40. down_sem(&sem);
  41. BitP(&GPIOA->ODR, 1) = 1;
  42. delay_ms(250);
  43. BitP(&GPIOA->ODR, 1) = 0;
  44. delay_ms(250);
  45. }
  46. }
  47. uint32_t stack3[STACK_SIZE];
  48. void process3()
  49. {
  50. while (1)
  51. {
  52. down_sem(&sem);
  53. BitP(&GPIOA->ODR, 2) = 1;
  54. delay_ms(500);
  55. BitP(&GPIOA->ODR, 2) = 0;
  56. delay_ms(500);
  57. }
  58. }
  59. uint32_t stack4[STACK_SIZE];
  60. void process4()
  61. {
  62. while (1)
  63. {
  64. up_sem(&sem);
  65. up_sem(&sem);
  66. BitP(&GPIOA->ODR, 3) = 1;
  67. delay_ms(1000);
  68. BitP(&GPIOA->ODR, 3) = 0;
  69. delay_ms(1000);
  70. }
  71. }
  72. int main(void)
  73. {
  74. init();
  75. init_zos();
  76. init_sem(&sem, 0);
  77. create_process(&stack1[STACK_SIZE], process1);
  78. create_process(&stack2[STACK_SIZE], process2);
  79. create_process(&stack3[STACK_SIZE], process3);
  80. create_process(&stack4[STACK_SIZE], process4);
  81. schedule();
  82. while (1);
  83. }
  84. void init()
  85. {
  86. GPIO_InitTypeDef gpio;
  87. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  88. gpio.GPIO_Pin =
  89. GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
  90. gpio.GPIO_Mode = GPIO_Mode_Out_PP;
  91. gpio.GPIO_Speed = GPIO_Speed_50MHz;
  92. GPIO_Init(GPIOA, &gpio);
  93. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注