FreeRTOS-04列表和列表项

根据正点原子FreeRTOS视频整理

单片机:STM32F207VC

FreeRTOS源码版本:v10.0.1

 实验说明:
1. 验证列表项的插入、末尾插入、删除操作
备注:
  末尾插入感觉不是末尾插入,而是插入到列表的后面,所有列表项的前面

1.main.c

  1 /*
  2  * 实验说明:
  3  * 1. 验证列表项的插入、末尾插入、删除操作
  4  *
  5  * 备注:
  6  *   末尾插入感觉不是末尾插入,而是插入到列表的后面,所有列表项的前面
  7  */
  8 #include "main.h"
  9 #include "delay.h"
 10 #include "sys.h"
 11 #include "usart.h"
 12 
 13 #include "stm32f2xx_gpio.h"
 14 
 15 #include "FreeRTOS.h"
 16 #include "task.h"
 17 
 18 #define START_TASK_PRIO             1     /*任务优先级*/
 19 #define START_STK_SIZE              128   /*任务堆栈大小*/
 20 TaskHandle_t StartTask_Handle;            /*任务句柄*/
 21 void StartTask(void *pvParameters);       /*任务函数*/
 22 
 23 #define LIST_TASK_PRIO              2
 24 #define LIST_STK_SIZE               256
 25 TaskHandle_t ListTask_Handle;
 26 void ListTask(void *pvParameters);
 27 
 28 /*定义列表和列表项*/
 29 List_t      TestList;
 30 ListItem_t  ListItem1;
 31 ListItem_t  ListItem2;
 32 ListItem_t  ListItem3;
 33 
 34 /***** 声明 *****/
 35 static void SystemInitial(void);
 36 static void GPIO_LED_Configuration(void);
 37 
 38 static void GPIO_LED_Configuration(void)
 39 {
 40   GPIO_InitTypeDef    GPIO_InitStructure;
 41   
 42   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
 43   
 44   GPIO_InitStructure.GPIO_Pin     = LED_POWER;
 45   GPIO_InitStructure.GPIO_Mode    = GPIO_Mode_OUT;
 46   GPIO_InitStructure.GPIO_Speed   = GPIO_Speed_100MHz;
 47   GPIO_InitStructure.GPIO_OType   = GPIO_OType_PP;
 48   GPIO_InitStructure.GPIO_PuPd    = GPIO_PuPd_NOPULL;
 49   GPIO_Init(GPIOE, &GPIO_InitStructure);  
 50   
 51   LED_Power_On();
 52 }
 53 
 54 
 55 void StartTask(void *pvParameters)
 56 {
 57   taskENTER_CRITICAL();           /*进入临界区*/
 58   
 59   xTaskCreate((TaskFunction_t )ListTask,                  /*任务函数*/
 60                             (const char *   )"ListTask",                  /*任务名称*/
 61                             (uint16_t       )LIST_STK_SIZE,             /*任务堆栈大小*/
 62                             (void *         )NULL,                      /*传递给任务函数的参数*/
 63                             (UBaseType_t    )LIST_TASK_PRIO,            /*任务优先级*/
 64                             (TaskHandle_t   )&ListTask_Handle);         /*任务句柄*/             
 65 
 66   vTaskDelete(StartTask_Handle);    /*删除开始任务*/
 67   taskEXIT_CRITICAL();              /*推出临界区*/
 68 } 
 69 
 70 void ListTask(void *pvParameters)
 71 {
 72   /* 1. 初始化列表和列表项 */
 73   vListInitialise(&TestList);
 74   vListInitialiseItem(&ListItem1);
 75   vListInitialiseItem(&ListItem2);
 76   vListInitialiseItem(&ListItem3);
 77   printf("1. 初始化列表和列表项\r\n");
 78   printf("/**************列表和列表项地址**************/\r\n");
 79   printf("项目                            地址          \r\n");
 80   printf("TestList                        %#x           \r\n", (int)&TestList);
 81   printf("TestList->pxIndex               %#x           \r\n", (int)(TestList.pxIndex));
 82   printf("TestList->xListEnd              %#x           \r\n", (int)(&TestList.xListEnd));
 83   printf("TestList->xListEnd->xItemValue  %#x           \r\n", (int)(&TestList.xListEnd.xItemValue));
 84   printf("TestList->xListEnd->pxNext      %#x           \r\n", (int)(TestList.xListEnd.pxNext));
 85   printf("TestList->xListEnd->pxPrevious  %#x           \r\n", (int)(TestList.xListEnd.pxPrevious));
 86   printf("ListItem1                       %#x           \r\n", (int)&ListItem1);
 87   printf("ListItem2                       %#x           \r\n", (int)&ListItem2);
 88   printf("ListItem3                       %#x           \r\n", (int)&ListItem3);
 89   printf("/******************变量和值******************/\r\n");
 90   printf("变量名                          变量值        \r\n");
 91   printf("TestList->uxNumberOfItems       %ld           \r\n", TestList.uxNumberOfItems);
 92   printf("TestList->pxIndex               %#x           \r\n", (int)(TestList.pxIndex));
 93   printf("TestList->xListEnd->xItemValue  %#x           \r\n", TestList.xListEnd.xItemValue);
 94   printf("ListItem1->xItemValue           %d            \r\n", ListItem1.xItemValue);
 95   printf("ListItem2->xItemValue           %d            \r\n", ListItem2.xItemValue);
 96   printf("ListItem3->xItemValue           %d            \r\n", ListItem3.xItemValue);
 97   printf("/******************* end ********************/\r\n");
 98   printf("\r\n");
 99   
100   /* 2. 插入ListItem1 */
101   ListItem1.xItemValue = 40;
102   vListInsert(&TestList, &ListItem1);
103   printf("2. 插入ListItem1\r\n");
104   printf("/**************列表和列表项地址**************/\r\n");
105   printf("项目                            地址          \r\n");
106   printf("TestList                        %#x           \r\n", (int)&TestList);
107   printf("TestList->pxIndex               %#x           \r\n", (int)(TestList.pxIndex));
108   printf("TestList->xListEnd              %#x           \r\n", (int)(&TestList.xListEnd));
109   printf("TestList->xListEnd->xItemValue  %#x           \r\n", (int)(&TestList.xListEnd.xItemValue));
110   printf("TestList->xListEnd->pxNext      %#x           \r\n", (int)(TestList.xListEnd.pxNext));
111   printf("TestList->xListEnd->pxPrevious  %#x           \r\n", (int)(TestList.xListEnd.pxPrevious));
112   printf("ListItem1                       %#x           \r\n", (int)&ListItem1);
113   printf("ListItem1->pxNext               %#x           \r\n", (int)(ListItem1.pxNext));
114   printf("ListItem1->pxPrevious           %#x           \r\n", (int)(ListItem1.pxPrevious));
115   printf("ListItem2                       %#x           \r\n", (int)&ListItem2);
116   printf("ListItem3                       %#x           \r\n", (int)&ListItem3);
117   printf("/******************变量和值******************/\r\n");
118   printf("变量名                          变量值        \r\n");
119   printf("TestList->uxNumberOfItems       %ld           \r\n", TestList.uxNumberOfItems);
120   printf("TestList->pxIndex               %#x           \r\n", (int)(TestList.pxIndex));
121   printf("TestList->xListEnd->xItemValue  %#x           \r\n", TestList.xListEnd.xItemValue);
122   printf("ListItem1->xItemValue           %d            \r\n", ListItem1.xItemValue);
123   printf("ListItem2->xItemValue           %d            \r\n", ListItem2.xItemValue);
124   printf("ListItem3->xItemValue           %d            \r\n", ListItem3.xItemValue);
125   printf("/******************* end ********************/\r\n");
126   printf("\r\n");
127   
128   /* 3. 插入ListItem2 */
129   ListItem2.xItemValue = 60;
130   vListInsert(&TestList, &ListItem2);
131   printf("3. 插入ListItem2\r\n");
132   printf("/**************列表和列表项地址**************/\r\n");
133   printf("项目                            地址          \r\n");
134   printf("TestList                        %#x           \r\n", (int)&TestList);
135   printf("TestList->pxIndex               %#x           \r\n", (int)(TestList.pxIndex));
136   printf("TestList->xListEnd              %#x           \r\n", (int)(&TestList.xListEnd));
137   printf("TestList->xListEnd->xItemValue  %#x           \r\n", (int)(&TestList.xListEnd.xItemValue));
138   printf("TestList->xListEnd->pxNext      %#x           \r\n", (int)(TestList.xListEnd.pxNext));
139   printf("TestList->xListEnd->pxPrevious  %#x           \r\n", (int)(TestList.xListEnd.pxPrevious));
140   printf("ListItem1                       %#x           \r\n", (int)&ListItem1);
141   printf("ListItem1->pxNext               %#x           \r\n", (int)(ListItem1.pxNext));
142   printf("ListItem1->pxPrevious           %#x           \r\n", (int)(ListItem1.pxPrevious));
143   printf("ListItem2                       %#x           \r\n", (int)&ListItem2);
144   printf("ListItem2->pxNext               %#x           \r\n", (int)(ListItem2.pxNext));
145   printf("ListItem2->pxPrevious           %#x           \r\n", (int)(ListItem2.pxPrevious));
146   printf("ListItem3                       %#x           \r\n", (int)&ListItem3);
147   printf("/******************变量和值******************/\r\n");
148   printf("变量名                          变量值        \r\n");
149   printf("TestList->uxNumberOfItems       %ld           \r\n", TestList.uxNumberOfItems);
150   printf("TestList->pxIndex               %#x           \r\n", (int)(TestList.pxIndex));
151   printf("TestList->xListEnd->xItemValue  %#x           \r\n", TestList.xListEnd.xItemValue);
152   printf("ListItem1->xItemValue           %d            \r\n", ListItem1.xItemValue);
153   printf("ListItem2->xItemValue           %d            \r\n", ListItem2.xItemValue);
154   printf("ListItem3->xItemValue           %d            \r\n", ListItem3.xItemValue);
155   printf("/******************* end ********************/\r\n");
156   printf("\r\n");
157   
158   /* 4. 插入ListItem3 */
159   ListItem3.xItemValue = 50;
160   vListInsert(&TestList, &ListItem3);
161   printf("4. 插入ListItem3\r\n");
162   printf("/**************列表和列表项地址**************/\r\n");
163   printf("项目                            地址          \r\n");
164   printf("TestList                        %#x           \r\n", (int)&TestList);
165   printf("TestList->pxIndex               %#x           \r\n", (int)(TestList.pxIndex));
166   printf("TestList->xListEnd              %#x           \r\n", (int)(&TestList.xListEnd));
167   printf("TestList->xListEnd->xItemValue  %#x           \r\n", (int)(&TestList.xListEnd.xItemValue));
168   printf("TestList->xListEnd->pxNext      %#x           \r\n", (int)(TestList.xListEnd.pxNext));
169   printf("TestList->xListEnd->pxPrevious  %#x           \r\n", (int)(TestList.xListEnd.pxPrevious));
170   printf("ListItem1                       %#x           \r\n", (int)&ListItem1);
171   printf("ListItem1->pxNext               %#x           \r\n", (int)(ListItem1.pxNext));
172   printf("ListItem1->pxPrevious           %#x           \r\n", (int)(ListItem1.pxPrevious));
173   printf("ListItem2                       %#x           \r\n", (int)&ListItem2);
174   printf("ListItem2->pxNext               %#x           \r\n", (int)(ListItem2.pxNext));
175   printf("ListItem2->pxPrevious           %#x           \r\n", (int)(ListItem2.pxPrevious));
176   printf("ListItem3                       %#x           \r\n", (int)&ListItem3);
177   printf("ListItem3->pxNext               %#x           \r\n", (int)(ListItem3.pxNext));
178   printf("ListItem3->pxPrevious           %#x           \r\n", (int)(ListItem3.pxPrevious));
179   printf("/******************变量和值******************/\r\n");
180   printf("变量名                          变量值        \r\n");
181   printf("TestList->uxNumberOfItems       %ld           \r\n", TestList.uxNumberOfItems);
182   printf("TestList->pxIndex               %#x           \r\n", (int)(TestList.pxIndex));
183   printf("TestList->xListEnd->xItemValue  %#x           \r\n", TestList.xListEnd.xItemValue);
184   printf("ListItem1->xItemValue           %d            \r\n", ListItem1.xItemValue);
185   printf("ListItem2->xItemValue           %d            \r\n", ListItem2.xItemValue);
186   printf("ListItem3->xItemValue           %d            \r\n", ListItem3.xItemValue);
187   printf("/******************* end ********************/\r\n");
188   printf("\r\n");
189   
190   /* 5. 删除ListItem2 */
191   uxListRemove(&ListItem2);
192   printf("5. 删除ListItem2\r\n");
193   printf("/**************列表和列表项地址**************/\r\n");
194   printf("项目                            地址          \r\n");
195   printf("TestList                        %#x           \r\n", (int)&TestList);
196   printf("TestList->pxIndex               %#x           \r\n", (int)(TestList.pxIndex));
197   printf("TestList->xListEnd              %#x           \r\n", (int)(&TestList.xListEnd));
198   printf("TestList->xListEnd->xItemValue  %#x           \r\n", (int)(&TestList.xListEnd.xItemValue));
199   printf("TestList->xListEnd->pxNext      %#x           \r\n", (int)(TestList.xListEnd.pxNext));
200   printf("TestList->xListEnd->pxPrevious  %#x           \r\n", (int)(TestList.xListEnd.pxPrevious));
201   printf("ListItem1                       %#x           \r\n", (int)&ListItem1);
202   printf("ListItem1->pxNext               %#x           \r\n", (int)(ListItem1.pxNext));
203   printf("ListItem1->pxPrevious           %#x           \r\n", (int)(ListItem1.pxPrevious));
204   printf("ListItem2                       %#x           \r\n", (int)&ListItem2);
205   printf("ListItem2->pxNext               %#x           \r\n", (int)(ListItem2.pxNext));
206   printf("ListItem2->pxPrevious           %#x           \r\n", (int)(ListItem2.pxPrevious));
207   printf("ListItem3                       %#x           \r\n", (int)&ListItem3);
208   printf("ListItem3->pxNext               %#x           \r\n", (int)(ListItem3.pxNext));
209   printf("ListItem3->pxPrevious           %#x           \r\n", (int)(ListItem3.pxPrevious));
210   printf("/******************变量和值******************/\r\n");
211   printf("变量名                          变量值        \r\n");
212   printf("TestList->uxNumberOfItems       %ld           \r\n", TestList.uxNumberOfItems);
213   printf("TestList->pxIndex               %#x           \r\n", (int)(TestList.pxIndex));
214   printf("TestList->xListEnd->xItemValue  %#x           \r\n", TestList.xListEnd.xItemValue);
215   printf("ListItem1->xItemValue           %d            \r\n", ListItem1.xItemValue);
216   printf("ListItem2->xItemValue           %d            \r\n", ListItem2.xItemValue);
217   printf("ListItem3->xItemValue           %d            \r\n", ListItem3.xItemValue);
218   printf("/******************* end ********************/\r\n");
219   printf("\r\n");
220   
221   /* 6. 末尾插入ListItem2 */
222   TestList.pxIndex = TestList.pxIndex->pxNext;
223   vListInsertEnd(&TestList, &ListItem2);
224   printf("6. 末尾插入ListItem2\r\n");
225   printf("/**************列表和列表项地址**************/\r\n");
226   printf("项目                            地址          \r\n");
227   printf("TestList                        %#x           \r\n", (int)&TestList);
228   printf("TestList->pxIndex               %#x           \r\n", (int)(TestList.pxIndex));
229   printf("TestList->xListEnd              %#x           \r\n", (int)(&TestList.xListEnd));
230   printf("TestList->xListEnd->xItemValue  %#x           \r\n", (int)(&TestList.xListEnd.xItemValue));
231   printf("TestList->xListEnd->pxNext      %#x           \r\n", (int)(TestList.xListEnd.pxNext));
232   printf("TestList->xListEnd->pxPrevious  %#x           \r\n", (int)(TestList.xListEnd.pxPrevious));
233   printf("ListItem1                       %#x           \r\n", (int)&ListItem1);
234   printf("ListItem1->pxNext               %#x           \r\n", (int)(ListItem1.pxNext));
235   printf("ListItem1->pxPrevious           %#x           \r\n", (int)(ListItem1.pxPrevious));
236   printf("ListItem2                       %#x           \r\n", (int)&ListItem2);
237   printf("ListItem2->pxNext               %#x           \r\n", (int)(ListItem2.pxNext));
238   printf("ListItem2->pxPrevious           %#x           \r\n", (int)(ListItem2.pxPrevious));
239   printf("ListItem3                       %#x           \r\n", (int)&ListItem3);
240   printf("ListItem3->pxNext               %#x           \r\n", (int)(ListItem3.pxNext));
241   printf("ListItem3->pxPrevious           %#x           \r\n", (int)(ListItem3.pxPrevious));
242   printf("/******************变量和值******************/\r\n");
243   printf("变量名                          变量值        \r\n");
244   printf("TestList->uxNumberOfItems       %ld           \r\n", TestList.uxNumberOfItems);
245   printf("TestList->pxIndex               %#x           \r\n", (int)(TestList.pxIndex));
246   printf("TestList->xListEnd->xItemValue  %#x           \r\n", TestList.xListEnd.xItemValue);
247   printf("ListItem1->xItemValue           %d            \r\n", ListItem1.xItemValue);
248   printf("ListItem2->xItemValue           %d            \r\n", ListItem2.xItemValue);
249   printf("ListItem3->xItemValue           %d            \r\n", ListItem3.xItemValue);
250   printf("/******************* end ********************/\r\n");
251   printf("\r\n");
252   
253   /* 7. 删除ListItem1 */
254   uxListRemove(&ListItem1);
255   printf("7. 删除ListItem1\r\n");
256   printf("/**************列表和列表项地址**************/\r\n");
257   printf("项目                            地址          \r\n");
258   printf("TestList                        %#x           \r\n", (int)&TestList);
259   printf("TestList->pxIndex               %#x           \r\n", (int)(TestList.pxIndex));
260   printf("TestList->xListEnd              %#x           \r\n", (int)(&TestList.xListEnd));
261   printf("TestList->xListEnd->xItemValue  %#x           \r\n", (int)(&TestList.xListEnd.xItemValue));
262   printf("TestList->xListEnd->pxNext      %#x           \r\n", (int)(TestList.xListEnd.pxNext));
263   printf("TestList->xListEnd->pxPrevious  %#x           \r\n", (int)(TestList.xListEnd.pxPrevious));
264   printf("ListItem1                       %#x           \r\n", (int)&ListItem1);
265   printf("ListItem1->pxNext               %#x           \r\n", (int)(ListItem1.pxNext));
266   printf("ListItem1->pxPrevious           %#x           \r\n", (int)(ListItem1.pxPrevious));
267   printf("ListItem2                       %#x           \r\n", (int)&ListItem2);
268   printf("ListItem2->pxNext               %#x           \r\n", (int)(ListItem2.pxNext));
269   printf("ListItem2->pxPrevious           %#x           \r\n", (int)(ListItem2.pxPrevious));
270   printf("ListItem3                       %#x           \r\n", (int)&ListItem3);
271   printf("ListItem3->pxNext               %#x           \r\n", (int)(ListItem3.pxNext));
272   printf("ListItem3->pxPrevious           %#x           \r\n", (int)(ListItem3.pxPrevious));
273   printf("/******************变量和值******************/\r\n");
274   printf("变量名                          变量值        \r\n");
275   printf("TestList->uxNumberOfItems       %ld           \r\n", TestList.uxNumberOfItems);
276   printf("TestList->pxIndex               %#x           \r\n", (int)(TestList.pxIndex));
277   printf("TestList->xListEnd->xItemValue  %#x           \r\n", TestList.xListEnd.xItemValue);
278   printf("ListItem1->xItemValue           %d            \r\n", ListItem1.xItemValue);
279   printf("ListItem2->xItemValue           %d            \r\n", ListItem2.xItemValue);
280   printf("ListItem3->xItemValue           %d            \r\n", ListItem3.xItemValue);
281   printf("/******************* end ********************/\r\n");
282   printf("\r\n");
283 }
284 
285 static void SystemInitial(void)
286 {
287   /*组4,16级抢占优先级,无响应优先级*/
288   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
289   
290   DelayInitial();
291   USART1_Initialization();
292   GPIO_LED_Configuration();
293 }
294 
295 int main(void)
296 {
297   SystemInitial();
298   
299   /*创建开始任务*/
300   xTaskCreate((TaskFunction_t )StartTask,           /*任务函数*/
301                             (const char *   )"StartTask",            /*任务名称*/
302                             (uint16_t       )START_STK_SIZE,      /*任务堆栈大小*/
303                             (void *         )NULL,                /*传递给任务函数的参数*/
304                             (UBaseType_t    )START_TASK_PRIO,     /*任务优先级*/
305                             (TaskHandle_t * )&StartTask_Handle);  /*任务句柄*/             
306               
307   /*开启任务调度*/
308   vTaskStartScheduler();
309 }
310 
311 /***************************END OF FILE***************************/
View Code

2.main.h

 1 /**/
 2 #ifndef __MAIN_H__
 3 #define __MAIN_H__
 4 
 5 #define LED_POWER         GPIO_Pin_2    /*PE2*/
 6 
 7 #define LED_Power_On()    GPIO_ResetBits(GPIOE, LED_POWER)
 8 
 9 
10 #endif    /*__MAIN_H__*/
11 
12 /***************************END OF FILE***************************/
View Code

3.sys.c

 1 /**/
 2 #include "sys.h"
 3 #include "stdio.h"
 4 
 5 #pragma import(__use_no_semihosting)             
 6 //标准库需要的支持函数                 
 7 struct __FILE 
 8 { 
 9     int handle; 
10 
11 }; 
12 
13 FILE __stdout;       
14 //定义_sys_exit()以避免使用半主机模式    
15 void _sys_exit(int x) 
16 { 
17     x = x; 
18 } 
19 /* //重定义fputc函数 
20 int fputc(int ch, FILE *f)
21 {      
22      while((USART1->SR&0X40)==0)  //循环发送,直到发送完毕   
23     USART1->DR = (u8) ch;      
24     return ch;
25 } */
26 
27 
28 /***************************END OF FILE***************************/
View Code

4.sys.h

 1 /**/
 2 #ifndef __SYS_H__
 3 #define __SYS_H__
 4 
 5 /*0不支持OS,1支持OS*/
 6 #define SYSTEM_SUPPORT_OS        1        /*定义系统文件夹是否支持OS*/
 7 
 8 #endif    /*__SYS_H__*/
 9 
10 /***************************END OF FILE***************************/
View Code

5.delay.c

  1 /**/
  2 #include "delay.h"
  3 #include "sys.h"
  4 /*如果需要使用OS,则包括下面的头文件即可*/
  5 #if SYSTEM_SUPPORT_OS
  6 #include "FreeRTOS.h"                      
  7 #include "task.h" 
  8 #endif
  9 
 10 __IO uint32_t TimingDelay;
 11 
 12 //////////////////////////
 13 static uint8_t  fac_us = 0;
 14 //////////////////////////
 15 
 16 /***** 声明 *****/
 17 extern void xPortSysTickHandler(void);
 18 
 19 /*systick中断服务函数,使用FreeRTOS时用到*/
 20 void SysTick_Handler(void)
 21 {    
 22     TimingDelayDecrement();
 23     
 24     if(xTaskGetSchedulerState()!=taskSCHEDULER_NOT_STARTED)   /*系统已运行*/
 25     {
 26       xPortSysTickHandler();    
 27     }
 28 }
 29 
 30 
 31 void DelayInitial(void)
 32 {
 33   /*
 34      * SystemCoreClock / 1000      1ms中断一次
 35      * SystemCoreClock / 100000    10us中断一次
 36      * SystemCoreClock / 1000000   1us中断一次
 37      */
 38   if (SysTick_Config(SystemCoreClock / 1000))
 39   {
 40     while (1);
 41   }
 42   /*关闭systick timer定时器*/
 43   /*    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;*/
 44 
 45   /*使能滴答定时器*/
 46   SysTick->CTRL |=   SysTick_CTRL_ENABLE_Msk;  
 47 }
 48 
 49 void DelayNus(uint32_t nus)
 50 {
 51   uint32_t ticks;
 52   uint32_t told, tnow, tcnt = 0;
 53   uint32_t reload = SysTick->LOAD;
 54   
 55   fac_us = SystemCoreClock / 1000000;
 56   ticks = nus * fac_us;
 57   told  = SysTick->VAL;
 58   
 59   while (1)
 60   {
 61     tnow = SysTick->VAL;
 62     if (tnow != told)
 63     {
 64       if (tnow < told) 
 65       {
 66         tcnt += told - tnow;
 67       }
 68       else
 69       {
 70         tcnt += reload - tnow + told;
 71       }
 72       told = tnow;
 73       if (tcnt >= ticks)  break;      
 74     }
 75   }
 76 }
 77 
 78 /*不会引起调度*/
 79 void DelayXms(uint32_t nms)
 80 {
 81   uint32_t i;
 82   
 83   for (i=0;i<nms;++i)
 84   {
 85     DelayNus(1000);
 86   }
 87 }
 88 
 89 /*
 90  * 本函数在中断函数中调用,滴答定时器中断一次调用一次。
 91  */
 92 void TimingDelayDecrement(void)
 93 {
 94     if (TimingDelay != 0x00)
 95     {
 96         TimingDelay--;
 97     }
 98 }
 99 
100 /*
101  * TimingDelay值在TimingDelayDecrement函数中递减
102  */
103 void DelayNms(uint32_t nTimes)
104 {
105     TimingDelay = nTimes;
106     
107     while (TimingDelay!=0);   //等待计数停止
108 }
109 
110 /***************************END OF FILE***************************/
View Code

6.delay.h

 1 /**/
 2 #ifndef __DELAY_H__
 3 #define __DELAY_H__
 4 
 5 #include "stm32f2xx.h"
 6 
 7 #include <stdint.h>
 8 
 9 extern void DelayInitial(void);
10 extern void TimingDelayDecrement(void);
11 extern void DelayNms(uint32_t nTimes);
12 
13 /////////////////////////
14 extern void DelayXms(uint32_t nms);
15 /////////////////////////
16 
17 #endif    /*__DELAY_H__*/
18 /***************************END OF FILE***************************/
View Code

7.usart.c

  1 /*
  2  * USART1: 中断优先级选择第4组, 3级抢占优先级  无响应优先级 
  3  */
  4 #include "usart.h"
  5 #include "stdio.h"      /*printf*/
  6 #include "stm32f2xx.h"
  7 #include "stm32f2xx_gpio.h"
  8 #include "stm32f2xx_rcc.h"
  9 #include "stm32f2xx_usart.h"
 10 
 11 
 12 uint8_t  USART1_RxBuffer[USART1_RECEIVE_SIZE];
 13 uint8_t  Flag_USART1Receive = 0;
 14 uint8_t  USART1_ReceiveCount = 0;
 15 uint8_t  USART1_ReceiveIndex = 0;
 16 
 17 
 18 void USART1_Initialization(void)
 19 {
 20     USART1_GPIO_Configuration();
 21     USART1_NVIC_Configuration();
 22     /*USART1使能接收中断*/
 23 //    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
 24     /*USART1使能发送中断*/
 25 /*    USART_ITConfig(USART1, USART_IT_TXE, ENABLE);      */
 26 }
 27 /*
 28  */
 29 void USART1_GPIO_Configuration(void)
 30 {
 31     GPIO_InitTypeDef   GPIO_InitStructure;
 32     USART_InitTypeDef  USART_InitStructure;
 33     
 34     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
 35   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,  ENABLE);
 36   GPIO_PinAFConfig(GPIOA, 9, GPIO_AF_USART1);     /*GPIO连接到串口1上,PA9-TXD*/
 37     GPIO_PinAFConfig(GPIOA, 10, GPIO_AF_USART1);        /*GPIO连接到串口1上,PA10-RXD*/
 38     
 39     /*tx, PA9*/
 40   GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_9;
 41     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;    
 42     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 43   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
 44   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
 45     GPIO_Init(GPIOA, &GPIO_InitStructure);
 46     
 47     /*rx, PA10*/
 48     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
 49     GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_10;
 50   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
 51   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
 52     GPIO_Init(GPIOA, &GPIO_InitStructure);
 53     
 54     /*配置波特率9600*/ 
 55     USART_InitStructure.USART_BaudRate   = 115200;
 56     /*配置串口的模式。为了配置双线全双工通讯,需要把Rx和Tx模式都开启. Tx发送使能和Rx接收使能*/
 57     USART_InitStructure.USART_Mode       = USART_Mode_Rx | USART_Mode_Tx;
 58     /*无奇偶校验*/
 59     USART_InitStructure.USART_Parity     = USART_Parity_No;
 60     /*1停止位*/
 61     USART_InitStructure.USART_StopBits   = USART_StopBits_1;
 62     /*配置串口传输字长8位*/
 63     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
 64     /*不采用硬件流控制*/
 65     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
 66     /*向寄存器写入配置参数*/
 67     USART_Init(USART1, &USART_InitStructure);
 68     /*使能USART1外设。在使用外设时,不仅要使能其时钟,还要调用此函数使能外设才可以正常使用*/
 69     USART_Cmd(USART1, ENABLE);
 70 }
 71 
 72 //void USART1_SendNChar(uint8_t *str, uint8_t n)
 73 //{
 74 //    /*发送区是否为空*/
 75 //    while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
 76 //    
 77 //    while (n--)
 78 //    {
 79 //        USART_SendData(USART1, (uint8_t)(*str++));
 80 //        /*是否发送完成*/
 81 //        while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
 82 //    }
 83 //}
 84 
 85 /*
 86  * 如果一次发送多个字节数据,可能会多次进入此函数
 87  * 调用时,应先延时几十毫秒,确保把数据都接收完
 88  */
 89 //void USART1_ReceiveIRQ(void)
 90 //{
 91 //    /*如果寄存器中有数据*/
 92 //    while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)
 93 //    {
 94 //        USART1_RxBuffer[USART1_ReceiveIndex++] = USART_ReceiveData(USART1);
 95 //        USART1_ReceiveCount++;
 96 //    }
 97 //    
 98 //    Flag_USART1Receive = 1;
 99 //}
100 
101 void USART1_NVIC_Configuration(void)
102 {
103     NVIC_InitTypeDef NVIC_InitStructure;
104     
105     /*中断优先级选择第1组*/
106     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
107 
108     /*3级抢占优先级  0级响应优先级*/   
109     NVIC_InitStructure.NVIC_IRQChannel                    = USART1_IRQn;
110     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority  = 3;
111     NVIC_InitStructure.NVIC_IRQChannelSubPriority         = 0;
112     NVIC_InitStructure.NVIC_IRQChannelCmd                 = ENABLE;
113     NVIC_Init(&NVIC_InitStructure);
114 }
115 
116 /*重定义fputc函数 2种方法都可以*/
117 /*
118 int fputc(int ch,FILE *f)
119 {  
120  while(USART_GetFlagStatus(USART1,USART_FLAG_TC) != SET); 
121  USART_SendData(USART1,(uint8_t)ch);    
122  while(USART_GetFlagStatus(USART1,USART_FLAG_TC) != SET);  
123  
124  return (ch); 
125 } 
126 */
127 
128 int fputc(int ch, FILE *f)
129 {      
130   while((USART1->SR&0X40)==0)   /*循环发送,直到发送完毕*/
131   {}
132   
133   USART1->DR = (uint8_t)ch;      
134   return ch;
135 }
136 /***************************END OF FILE***************************/
View Code

8.usart.h

 1 /*
 2  * 
 3  */
 4 #ifndef __USART_H__
 5 #define __USART_H__
 6 
 7 #include <stdint.h>        /* uint8_t */
 8 
 9 #define USART1_RECEIVE_SIZE            20
10 
11 
12 void USART1_Initialization(void);
13 void USART1_GPIO_Configuration(void);
14 void USART1_SendNChar(uint8_t *str, uint8_t n);
15 void USART1_ReceiveIRQ(void);
16 void USART1_NVIC_Configuration(void);
17 
18 #endif  /*__USART_H__*/
19 
20 /***************************END OF FILE***************************/
View Code

说明:

在串口中断助手中,打印完字符后,接着显示:Error:..\FreeRTOS\portable\RVDS\ARM_CM3\port.c,202

网上一般说是串口的中断优先级低于FreeRTOS的优先级,但是我设置FreeRTOS中可管理的最高中断优先级为5,
串口中断优先级为3,还是会出现这个问题。并且我也没有用串口的中断服务函数。

打印结果:
1. 初始化列表和列表项
/**************列表和列表项地址**************/
项目                            地址         
TestList                              0x200000ac          
TestList->pxIndex                   0x200000b4          
TestList->xListEnd                  0x200000b4          
TestList->xListEnd->xItemValue     0x200000b4          
TestList->xListEnd->pxNext         0x200000b4          
TestList->xListEnd->pxPrevious     0x200000b4          
ListItem1                               0x200000c0          
ListItem2                               0x200000d4          
ListItem3                               0x200000e8          
/******************变量和值******************/
变量名                          变量值       
TestList->uxNumberOfItems         0          
TestList->pxIndex                    0x200000b4          
TestList->xListEnd->xItemValue     0xffffffff          
ListItem1->xItemValue                       0           
ListItem2->xItemValue                       0           
ListItem3->xItemValue               0           
/******************* end ********************/
2. 插入ListItem1
/**************列表和列表项地址**************/
项目                            地址         
TestList                              0x200000ac          
TestList->pxIndex                   0x200000b4          
TestList->xListEnd                  0x200000b4          
TestList->xListEnd->xItemValue     0x200000b4          
TestList->xListEnd->pxNext         0x200000c0          
TestList->xListEnd->pxPrevious     0x200000c0          
ListItem1                              0x200000c0          
ListItem1->pxNext                     0x200000b4          
ListItem1->pxPrevious                 0x200000b4          
ListItem2                              0x200000d4          
ListItem3                                 0x200000e8          
/******************变量和值******************/
变量名                          变量值       
TestList->uxNumberOfItems         1          
TestList->pxIndex                    0x200000b4          
TestList->xListEnd->xItemValue     0xffffffff          
ListItem1->xItemValue               40           
ListItem2->xItemValue               0           
ListItem3->xItemValue               0           
/******************* end ********************/
3. 插入ListItem2
/**************列表和列表项地址**************/
项目                            地址         
TestList                              0x200000ac          
TestList->pxIndex                   0x200000b4          
TestList->xListEnd                  0x200000b4          
TestList->xListEnd->xItemValue    0x200000b4          
TestList->xListEnd->pxNext        0x200000c0          
TestList->xListEnd->pxPrevious     0x200000d4          
ListItem1                              0x200000c0          
ListItem1->pxNext                     0x200000d4          
ListItem1->pxPrevious                0x200000b4          
ListItem2                             0x200000d4          
ListItem2->pxNext                            0x200000b4          
ListItem2->pxPrevious                     0x200000c0          
ListItem3                                          0x200000e8          
/******************变量和值******************/
变量名                          变量值       
TestList->uxNumberOfItems       2          
TestList->pxIndex               0x200000b4          
TestList->xListEnd->xItemValue  0xffffffff          
ListItem1->xItemValue           40           
ListItem2->xItemValue           60           
ListItem3->xItemValue           0           
/******************* end ********************/
4. 插入ListItem3
/**************列表和列表项地址**************/
项目                            地址         
TestList                              0x200000ac          
TestList->pxIndex                   0x200000b4          
TestList->xListEnd                  0x200000b4          
TestList->xListEnd->xItemValue    0x200000b4          
TestList->xListEnd->pxNext        0x200000c0          
TestList->xListEnd->pxPrevious    0x200000d4          
ListItem1                              0x200000c0          
ListItem1->pxNext                   0x200000e8          
ListItem1->pxPrevious              0x200000b4          
ListItem2                             0x200000d4          
ListItem2->pxNext                   0x200000b4          
ListItem2->pxPrevious               0x200000e8          
ListItem3                             0x200000e8          
ListItem3->pxNext                   0x200000d4          
ListItem3->pxPrevious               0x200000c0          
/******************变量和值******************/
变量名                          变量值       
TestList->uxNumberOfItems          3          
TestList->pxIndex                   0x200000b4          
TestList->xListEnd->xItemValue    0xffffffff          
ListItem1->xItemValue                40           
ListItem2->xItemValue                     60           
ListItem3->xItemValue                     50           
/******************* end ********************/
5. 删除ListItem2
/**************列表和列表项地址**************/
项目                            地址         
TestList                              0x200000ac          
TestList->pxIndex                   0x200000b4          
TestList->xListEnd                  0x200000b4          
TestList->xListEnd->xItemValue       0x200000b4          
TestList->xListEnd->pxNext              0x200000c0          
TestList->xListEnd->pxPrevious       0x200000e8          
ListItem1                              0x200000c0          
ListItem1->pxNext                             0x200000e8          
ListItem1->pxPrevious              0x200000b4          
ListItem2                             0x200000d4          
ListItem2->pxNext                   0x200000b4          
ListItem2->pxPrevious               0x200000e8          
ListItem3                             0x200000e8          
ListItem3->pxNext                   0x200000b4          
ListItem3->pxPrevious              0x200000c0          
/******************变量和值******************/
变量名                          变量值       
TestList->uxNumberOfItems         2          
TestList->pxIndex                    0x200000b4          
TestList->xListEnd->xItemValue       0xffffffff          
ListItem1->xItemValue              40           
ListItem2->xItemValue                   60           
ListItem3->xItemValue              50           
/******************* end ********************/
6. 末尾插入ListItem2
/**************列表和列表项地址**************/
项目                            地址         
TestList                              0x200000ac          
TestList->pxIndex                   0x200000c0          
TestList->xListEnd                  0x200000b4          
TestList->xListEnd->xItemValue       0x200000b4          
TestList->xListEnd->pxNext              0x200000d4          
TestList->xListEnd->pxPrevious       0x200000e8          
ListItem1                                           0x200000c0          
ListItem1->pxNext                            0x200000e8          
ListItem1->pxPrevious                0x200000d4          
ListItem2                             0x200000d4          
ListItem2->pxNext                    0x200000c0          
ListItem2->pxPrevious              0x200000b4          
ListItem3                             0x200000e8          
ListItem3->pxNext                    0x200000b4          
ListItem3->pxPrevious                      0x200000c0          
/******************变量和值******************/
变量名                          变量值       
TestList->uxNumberOfItems         3          
TestList->pxIndex                    0x200000c0          
TestList->xListEnd->xItemValue     0xffffffff          
ListItem1->xItemValue               40           
ListItem2->xItemValue               60           
ListItem3->xItemValue               50           
/******************* end ********************/
7. 删除ListItem1
/**************列表和列表项地址**************/
项目                            地址         
TestList                              0x200000ac          
TestList->pxIndex                   0x200000d4          
TestList->xListEnd                  0x200000b4          
TestList->xListEnd->xItemValue       0x200000b4          
TestList->xListEnd->pxNext              0x200000d4          
TestList->xListEnd->pxPrevious        0x200000e8          
ListItem1                             0x200000c0          
ListItem1->pxNext                   0x200000e8          
ListItem1->pxPrevious                0x200000d4          
ListItem2                             0x200000d4          
ListItem2->pxNext                   0x200000e8          
ListItem2->pxPrevious               0x200000b4          
ListItem3                             0x200000e8          
ListItem3->pxNext                   0x200000b4          
ListItem3->pxPrevious               0x200000d4          
/******************变量和值******************/
变量名                          变量值       
TestList->uxNumberOfItems         2          
TestList->pxIndex                    0x200000d4          
TestList->xListEnd->xItemValue        0xffffffff          
ListItem1->xItemValue               40           
ListItem2->xItemValue               60           
ListItem3->xItemValue               50           
/******************* end ********************/

  

posted @ 2018-07-31 14:11  seifguo  阅读(539)  评论(1编辑  收藏  举报