Advertisement
ademosh

Untitled

Oct 26th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. Файл OS.c
  2.  
  3. #include <stm32l1xx.h>
  4.  
  5.  
  6. //--------------------STACKS---------------
  7. unsigned char Stacks[8][0x200]; // Объявляем стек и его размер
  8.  
  9. //--------------------CONTEXTS-------------
  10.  
  11. // выделяем память под контекст
  12. struct {
  13. unsigned int GRP[13];
  14. void * PC;
  15. void * LR;
  16. void * SP;
  17. unsigned int PSW;
  18. } contexts[8];
  19.  
  20. //--------------------TASKS-------------
  21. // Импортировать в операционную систему список задач
  22. extern void * Tasks[];
  23. unsigned char tnum;
  24. unsigned char tcur;
  25. typedef void (*func)(void);
  26.  
  27. // метод для инициализации таймера
  28. void OSinit(){
  29. int i;
  30. // реализация таймера
  31. RCC->APB1ENR |= 0x00000001;
  32. TIM2->CNT = 0; // счётчик
  33. TIM2->PSC = 0; // частота
  34. TIM2->ARR = 0x8000;
  35. TIM2->DIER |=0x00000001;
  36. TIM2->CR1 |= 0x00000001;
  37.  
  38. i=0;
  39. // загружаем контекст
  40. while(Tasks[i] != 0){
  41. contexts[i+1].PC = (void *)(((int)(Tasks[i])) | (1));
  42. contexts[i+1].SP = &Stacks[i][0x200-4];
  43. contexts[i+1].PSW = 0x01000000;
  44. i++;
  45. }
  46.  
  47. tnum = i;
  48. tcur = 0; //переменная для смены задач в прерывании
  49. //Enable NVIC
  50. NVIC->ISER[0] |= 0x10000000; //enable TIM2 IRQ reaction
  51. while(1);
  52. }
  53.  
  54. void SaveContext (void * conptr); //метод для сохранения контекста
  55. void RestoreContext (void *conptr); //метод для загрузки контекста
  56.  
  57. //функция void OSkernel (), которая будет
  58. //- вызывать функцию сохранения контекста для текущей задачи;
  59. //- реализовывать алгоритм планирования Round-Robin;
  60. // - вызывать функцию восстановления контекста для выбранной задачи.
  61.  
  62. void OSkernel(){
  63. SaveContext(&(contexts[tcur]));
  64. tcur++;
  65. if(tcur>tnum){
  66. // переключатель между контекстами
  67. tcur=1;
  68. }
  69. RestoreContext(&(contexts[tcur]));
  70. return;
  71. }
  72.  
  73.  
  74. Файл Main.c
  75. #include <stm32l1xx.h>
  76.  
  77.  
  78.  
  79. int Task1(int A) // 1 метод, которой выполняет ОС
  80. {
  81. while(1)
  82. {
  83. __NOP();
  84. }
  85. }
  86.  
  87.  
  88. int Task2(int A,int B) // 2 метод, который выполняет ОС
  89. {
  90. while(1)
  91. {
  92. __NOP();
  93.  
  94. }
  95. }
  96.  
  97. int main()
  98. {
  99. int A = 1;
  100. int B;
  101. *((unsigned int*)(0x40023800+0x20)) |= 0x00004000;
  102. *((unsigned int*)(0x40013800+0x08)) = 0x00000123;
  103. *((unsigned int*)(0x40013800+0x0C)) |= 0x0000200c;
  104. *((unsigned int*)(0x40023800+0x1C)) |= 0x00000001;
  105. *((unsigned int*)(0x40020000+0x00)) |= 0x00280000;
  106. *((unsigned int*)(0x40020000+0x24)) |= 0x00000770;
  107. B= *((unsigned int*)(0x40013800+0x00));
  108.  
  109. while(1)
  110. {
  111. Task1(A);
  112. Task2(A,B);
  113. }
  114. }
  115.  
  116.  
  117.  
  118.  
  119. // массив, в который поместили адреса на написанные процедуры, а также ключевые метки. Приписка __attribute__((at(0x08001000))) необходима для того, чтобы структура была размещена в ПЗУ.
  120. void * const Tasks[] __attribute__((at(0x08001000)))={
  121. Task1,
  122. Task2,
  123. 0
  124. };
  125.  
  126. Файл statup_stm312l1xx_mod.s
  127. Stack_Size EQU 0x00000400
  128.  
  129. AREA STACK, NOINIT, READWRITE, ALIGN=3
  130. Stack_Mem SPACE Stack_Size
  131. __initial_sp
  132. Varlr DCD 0
  133. ALIGN
  134.  
  135. PRESERVE8
  136. THUMB
  137.  
  138.  
  139. ; Vector Table Mapped to Address 0 at Reset
  140. AREA RESET, DATA, READONLY
  141. EXPORT __Vectors
  142. EXPORT __Vectors_End
  143. EXPORT __Vectors_Size
  144.  
  145. __Vectors DCD __initial_sp ; Top of Stack
  146. DCD Reset_Handler ; Reset Handler
  147. space (0x000000B0 - 8)
  148. DCD TIM2_Handler
  149. __Vectors_End
  150.  
  151. __Vectors_Size EQU __Vectors_End - __Vectors
  152.  
  153. AREA |.text|, CODE, READONLY
  154.  
  155.  
  156.  
  157. ; Reset handler routine
  158. Reset_Handler PROC
  159. EXPORT Reset_Handler [WEAK]
  160. IMPORT OSinit
  161.  
  162. LDR R0,=OSinit
  163. BLX R0
  164.  
  165. enable
  166. LDR R0,=0x40020400
  167. cpsid i
  168. LDR R1,[R0,#0x14]
  169. LDR R2,=0x00000080
  170. cmp1 ADD R4,#0x00000001
  171. CMP R4,R3
  172. BNE cmp1
  173. LDR R4,=0x00000000
  174.  
  175. ;#1 GPIO _odr part 2
  176. EOR R1,R2
  177. STR R1,[R0,#0x14]
  178. cpsie i
  179.  
  180. cmp2 ADD R4,#0x00000001
  181. CMP R4,R3
  182. BNE cmp2
  183. LDR R4,=0x00000000
  184. B enable
  185. ENDP
  186. // Процедура TIM2_Handler нужна для обработки прерываний
  187. TIM2_Handler PROC
  188. IMPORT OSkernel
  189. ;сохраняем LR которое меняется в OSkernel
  190. MOV R0, LR
  191. LDR R1, =Varlr
  192. STR R0, [r1]
  193. BL OSkernel
  194. ;TIMx_SR
  195. LDR R0,=0x40000000
  196. LDR R1,[R0,#0x10]
  197. LDR R2,=~0x00000001
  198. AND R1,R2
  199. STR R1,[R0,#0x10]
  200. ;востанавливаем значение LR
  201. LDR R1, =Varlr
  202. LDR R0, [r1]
  203. MOV LR, R0
  204. BX LR
  205. ENDP
  206.  
  207. SaveContext PROC
  208. EXPORT SaveContext
  209. ;Save PSW from task[i] stack to context[i]
  210. LDR R1,[SP,#0x24]
  211. STR R1,[R0,#0x40]
  212. ;Save PC from task[i] stack to context[i]
  213. LDR R1,[SP,#0x20]
  214. STR R1,[R0,#0x34]
  215. ;Save LR from task[i] stack to context[i]
  216. LDR R1,[SP,#0x1C]
  217. STR R1,[R0,#0x38]
  218. ;Save R12 from task[i] stack to context[i]
  219. LDR R1,[SP,#0x18]
  220. STR R1,[R0,#0x30]
  221. ;Save R3 from task[i] stack to context[i]
  222. LDR R1,[SP,#0x14]
  223. STR R1,[R0,#0x0C]
  224. ;Save R2 from task[i] stack to context[i]
  225. LDR R1,[SP,#0x10]
  226. STR R1,[R0,#0x08]
  227. ;Save R1 from task[i] stack to context[i]
  228. LDR R1,[SP,#0x0c]
  229. STR R1,[R0,#0x04]
  230. ;Save R0 from task[i] stack to context[i]
  231. LDR R1,[SP,#0x08]
  232. STR R1,[R0,#0x00]
  233. ;Save R4 from current context to context[i]
  234. STR R4,[R0,#0x10]
  235. ;Save R5 from current context to context[i]
  236. STR R5,[R0,#0x14]
  237. ;Save R6 from current context to context[i]
  238. STR R6,[R0,#0x18]
  239. ;Save R7 from current context to context[i]
  240. STR R7,[R0,#0x1C]
  241. ;Save R8 from current context to context[i]
  242. STR R8,[R0,#0x20]
  243. ;Save R9 from current context to context[i]
  244. STR R9,[R0,#0x24]
  245. ;Save R10 from current context to context[i]
  246. STR R10,[R0,#0x28]
  247. ;Save R11 from current context to context[i]
  248. STR R11,[R0,#0x2C]
  249. ;Save SP from current context to context[i]
  250. STR SP,[R0,#0x3C]
  251. BX LR
  252. ENDP
  253.  
  254. RestoreContext PROC
  255. EXPORT RestoreContext
  256. ;First stack item
  257. LDR R7,[SP,#0x00]
  258. ;Second stack item
  259. LDR R8,[SP,#0x04]
  260. ;Restore SP
  261. LDR SP,[R0,#0x3C]
  262.  
  263. ;Load first value in new stack
  264. STR R7,[SP,#0x00]
  265. ;Load second value in new stack
  266. STR R8,[SP,#0x04]
  267. ;Restore R11
  268. LDR R11,[R0,#0x2C]
  269. ;Restore R10
  270. LDR R10,[R0,#0x28]
  271. ;Restore R9
  272. LDR R9,[R0,#0x24]
  273. ;Restore R8
  274. LDR R8,[R0,#0x20]
  275. ;Restore R7
  276. LDR R7,[R0,#0x1C]
  277. ;Restore R6
  278. LDR R6,[R0,#0x18]
  279. ;Restore R5
  280. LDR R5,[R0,#0x14]
  281. ;Restore R4
  282. LDR R4,[R0,#0x10]
  283. ;Restore R0
  284. LDR R1,[R0,#0x00]
  285. STR R1,[SP,#0x08]
  286. ;Restore R1
  287. LDR R1,[R0,#0x04]
  288. STR R1,[SP,#0x0c]
  289.  
  290. ;Restore R2
  291. LDR R1,[R0,#0x08]
  292. STR R1,[SP,#0x10]
  293. ;Restore R3
  294. LDR R1,[R0,#0x0C]
  295. STR R1,[SP,#0x14]
  296. ;Restore R12
  297. LDR R1,[R0,#0x30]
  298. STR R1,[SP,#0x18]
  299. ;Restore LR
  300. LDR R1,[R0,#0x38]
  301. STR R1,[SP,#0x1C]
  302. ;Restore PC
  303. LDR R1,[R0,#0x34]
  304. STR R1,[SP,#0x20]
  305. ;Restore PSW
  306. LDR R1,[R0,#0x40]
  307. STR R1,[SP,#0x24]
  308. BX LR
  309. ENDP
  310.  
  311. ALIGN
  312. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement