UCOS-II移植到PIC2FJ256GB106
一 、软硬件平台及任务
1、 硬件平台:
n 开发板:Microchip公司的的PIC24F_Starter_Kit;
n 编程器:PIC18F67J50作为Starter Kits烧写程序;
n 控制器:PIC24FJ256GB106;
2、 软件平台:
n IDE:MPLAB IDE V8.66
n 编译器:MPLAB C30
n UCOS代码:基于PIC24的UCOS-IIv2.83源程序
3、 任务:点亮开发板上的绿色LED,并使每隔1s闪烁。
二 、移植步骤
工程文件组织结构(建立文件夹如下)
PIC24F_UCOS |
Application |
Project |
UCOS_CPU |
UCOS_II |
UCOS_VIEW |
BSP |
Configure |
Mplab |
Source |
Ports |
Output |
Ports |
Source |
PIC24F |
PIC24F |
PIC24F |
PIC24F |
1、 IDE配置
1) 建立工程,并选择编译器C30。
2) 工程配置(build options)
Directorys:(以下目录可以根据自己文件所在目录修改)
Output Directory: D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output |
Intermediary Directory: D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output |
注意:Intermediary Directory 和Output Directory必须为同一个文件夹,不然最后LINK会 报错。
Assembler Include Search Path: D:\PROJECT\PIC24F_UCOS\Project\PIC24F\BSP D:\PROJECT\PIC24F_UCOS\UCOS_II/Ports/PIC24F |
Include Search Path: D:\PROJECT\PIC24F_UCOS\UCOS_CPU D:\PROJECT\PIC24F_UCOS\UCOS_VIEW\Source D:\PROJECT\PIC24F_UCOS\UCOS_VIEW\Ports\PIC24F D:\PROJECT\PIC24F_UCOS\UCOS_II\Source D:\PROJECT\PIC24F_UCOS\UCOS_II\Ports\PIC24F D:\PROJECT\PIC24F_UCOS\UCOS_CPU\PIC24F D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Configure D:\PROJECT\PIC24F_UCOS\Project\PIC24F\BSP |
Library Search Path: C:\Microchip Starter Kits\PIC24F Starter Kit 1\MPLAB C30\lib |
3) 编译器设置(MPLAB C30)
Categories中选择Memory Model
2、 添加程序
A、为了使整个结构清晰,应用程序应放入Application文件夹中,如果应用程序过大,需要分类,也可以多建立几个分文件夹,如USB、图形库、TCP/IP库就应该独立存放。这个时候上面的Directory and Search Paths也应该随着文件夹的重新布局做相应的调整。
B、应用程序一般一个.c文件对应一个.h文件,根据uCOS-II的编程习惯,我们应该在所有.c文件中直接包含#include "includes.h",而将所有的.h文件全部放入includes.h文件中。该文件在Configure文件夹中。
C、将“基于PIC24的UCOS-IIv2.83源程序”的各个文件拷贝自己的工程相应文件夹下,最后拷贝MPLAB C30文件夹下的SUPPORT文件夹下的3个文件到BSP文件夹下:p24FJ256GB106.gld,p24FJ256GB106.h,p24FJ256GB106.inc。
D、 MPLAB工程下添加Source Files 和Header Files以及Linker Script如下:
3、修改程序
由于源程序的CPU和自己手头的开发板的CPU是一个类型的,所以只需要一些很少的修改。
首先从app.c函数看起,红色表示添加,蓝色表示去除。
INT16S main (void) //主函数
BSP_IntDisAll(); //该子函数bsp.c文件下,关闭所有中断,需自己添加关闭中断。
static void AppStartTask (void *p_arg) //任务函数
static void AppStartTask (void *p_arg) { INT8U i; INT8U j; INT8U m,n;
(void)p_arg;
BSP_Init(); /* Initialize BSP functions */
#if OS_TASK_STAT_EN > 0 OSStatInit(); /* Determine CPU capacity */ #endif
//#if OS_VIEW_MODULE > 0 // OSView_Init(); // OSView_TerminalRxSetCallback(AppTerminalRx); // OSView_RxIntEn(); /* Enable Rx Interrupts */ //#endif
// LED_Off(0); /* Turn OFF all the LEDs */
while (TRUE) { _LATG8=1; OSTimeDlyHMSM(0, 0, 0, 1000); _LATG8=0; OSTimeDlyHMSM(0, 0, 0, 1000); /* Task body, always written as an infinite loop. */ // for (i = 0; i < 4; i++) { /* Sweep right then left 4 times */ // for (j = 1; j <= 8; j++) { /* Scroll the LEDs to the right */ // LED_On(j); // for( m = 0; m < 0xFF; m++) // for( n = 0; n < 0xFF; n++) {} /* add for increase cpu load */ // OSTimeDlyHMSM(0, 0, 0, 25); /* Delay 25ms */ // LED_Off(j); // }
// for (j = 7; j >= 2; j--) { /* Scroll the LEDs to the left */ // LED_On(j); // for( m = 0; m < 0xFF; m++) /* add for increase cpu load */ // for( n = 0; n < 0xFF; n++) {} // OSTimeDlyHMSM(0, 0, 0, 25); // LED_Off(j); // } // }
//for (i = 0; i < 4; i++) { /* Blink all 8 LEDs 4 times */ // LED_On(0); // for( m = 0; m < 0xFF; m++) // for( n = 0; n < 0xFF; n++) {} /* add for increase cpu load */ // OSTimeDlyHMSM(0, 0, 0, 25); // LED_Off(0); // for( m = 0; m < 0xFF; m++) // for( n = 0; n < 0xFF; n++) {} /* add for increase cpu load */ // OSTimeDlyHMSM(0, 0, 0, 25); //} } } |
函数bsp.c,开发板使用2种时钟,第一种由PIC18F提供的12M,第二种是内部提供的FRC的8M。
#define PLL_96MHZ_OFF 0xFFFF #define PLL_96MHZ_ON 0xF7FF _CONFIG2(IESO_OFF & PLL_96MHZ_ON & PLLDIV_DIV2 & FNOSC_FRCPLL & POSCMOD_NONE) // Primary osc disabled, FRC OSC with PLL, USBPLL /2 _CONFIG1(JTAGEN_OFF & ICS_PGx2 & FWDTEN_OFF) // JTAG off, watchdog timer off // _CONFIG1(JTAGEN_OFF); /* Disable the JTAG which allows use of all port A pins */ //_CONFIG2(FNOSC_PRIPLL & POSCMOD_XT);
INT32U BSP_CPU_ClkFrq (void) { INT8U Clk_Selected; INT16U FRC_Div; INT32U CPU_Clk_Frq;
FRC_Div = ((CLKDIV & FRCDIV_MASK) >> 8); /* Get the FRC Oscillator Divider register value */ FRC_Div = ((1 << FRC_Div) * 2); /* Compute the FRC Divider value */
Clk_Selected = (OSCCON & COSC_MASK) >> 12; /* Determine which clock source is currently selected */
switch (Clk_Selected) { case 0: /* Fast Oscillator (FRC) Selected */ CPU_Clk_Frq = CPU_FRC_OSC_FRQ; /* Return the frequency of the internal fast oscillator */ break;
case 1: /* Fast Oscillator (FRC) with PLL Selected */ CPU_Clk_Frq = (CPU_FRC_OSC_FRQ * 4); /* Compute the PLL output frequency = (FRC * 4) */ break;
case 2: /* Primary External Oscillator Selected */ CPU_Clk_Frq = CPU_PRIMARY_OSC_FRQ; /* Return the frequency of the primary external oscillator */ break;
case 3: /* Primary External Oscillator with PLL Selected */ CPU_Clk_Frq = (CPU_PRIMARY_OSC_FRQ * 4); /* Compute the PLL output frq as (CPU_PRIMARY_OSC_FRQ * 4) */ break;
// case 4: /* Secondary Oscillator Selected (SOCS) */ // CPU_Clk_Frq = CPU_SECONDARY_OSC_FRQ; /* Return the frq of the external secondary oscillator */ // break;
// case 5: /* Low Power Oscillator (LPOSC) Selected */ // CPU_Clk_Frq = CPU_LOW_POWER_OSC_FRQ; /* Return the frq of the Low Power Oscillator */ // break;
case 6: CPU_Clk_Frq = 0; /* Return 0 for the Reserved clock setting */ break;
case 7: /* Fast Oscillator (FRC) with FRCDIV Selected */ CPU_Clk_Frq = CPU_FRC_OSC_FRQ / FRC_Div; /* Return the clock frequency of FRC / FRC_Div */ break;
default: CPU_Clk_Frq = 0; /* Return 0 if the clock source cannot be determined */ break; }
CPU_Clk_Frq /= 2; /* Divide the final frq by 2, get the actual CPU Frq (Fcy) */
return (CPU_Clk_Frq); /* Return the operating frequency */ } 去除掉所有关于PORTA的程序段,不需要。
Bsp.H文件也需要修改: //#define CPU_PRIMARY_OSC_FRQ 8000000L /* Primary External Oscillator Frequency */ //#define CPU_FRC_OSC_FRQ 7370000L /* Internal Fast Oscillator Frequency */ //#define CPU_SECONDARY_OSC_FRQ 32768L /* Secondary External Oscillator Frequency */ //#define CPU_LOW_POWER_OSC_FRQ 31000L /* Low Power Oscillator (LPOSC) 31Khz Nominal */ #define CPU_FRC_OSC_FRQ 8000000L #define CPU_PRIMARY_OSC_FRQ 3000000L |
其它文件(bsp_a.s /os_cpu_a.s/ OS_VIEWa.S/includes.h)将24FJ128GA010替换成24FJ256GB106。至此修改完毕。
4、 编译程序
Release build of project `D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\PIC24F_UCOS.mcp' started.
Thu Nov 03 19:40:03 2011
----------------------------------------------------------------------
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\os_core.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\os_flag.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\os_mbox.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\os_mem.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\os_mutex.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\os_q.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\os_sem.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\os_task.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\os_time.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\os_tmr.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\app.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\bsp.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\bsp_a.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\os_cpu_a.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\os_cpu_c.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\os_cpu_util_a.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\os_dbg.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\OS_VIEWa.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\OS_VIEWc.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\OS_VIEW.o" is up to date.
Make: The target "D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\PIC24F_UCOS.cof" is up to date.
Loaded D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\Output\PIC24F_UCOS.cof.
----------------------------------------------------------------------
Release build of project `D:\PROJECT\PIC24F_UCOS\Project\PIC24F\Mplab\PIC24F_UCOS.mcp' succeeded.
Thu Nov 03 19:40:05 2011
----------------------------------------------------------------------
BUILD SUCCEEDED
5、下载程序
成功移植到自己的开发板!!