在arch文件夹中的文件为MCU内核架构相关的文件,如cortex-m3,主要内容为M3的启动文件(见M3编程指南),MCU的固件库。
在boards文件夹中的文件为各个MCU的实现,包括对板子的配置。
system文件夹中包含了一个OSEK/VDX标准的操作系统。
四、在N32G45X上完成AUTOSAR的MCAL
以MCU驱动的时钟设置为例,static void SetClocks(Mcu_ClockSettingConfigType *clockSettingsPtr)是autosar的mcal定义的一个接口,用于设置MCU的时钟,在n32g45x的实现如下:
/**
* Set bus clocks. SysClk,AHBClk,APB1Clk,APB2Clk
*/
static void SetClocks(Mcu_ClockSettingConfigType *clockSettingsPtr)
{
volatile uint32 StartUpCounter = 0, HSEStatus = 0;
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
/* Enable HSE */
RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
/* Wait till HSE is ready and if Time out is reached exit */
do
{
HSEStatus = RCC->CTRL & RCC_CTRL_HSERDF;
StartUpCounter++;
} while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));
if ((RCC->CTRL & RCC_CTRL_HSERDF) != RESET)
{
HSEStatus = (uint32_t)0x01;
}
else
{
HSEStatus = (uint32_t)0x00;
}
if (HSEStatus == (uint32_t)0x01)
{
/* Enable Prefetch Buffer */
FLASH->AC |= FLASH_AC_PRFTBFEN;
/* Flash 2 wait state */
FLASH->AC &= (uint32_t)((uint32_t)~FLASH_AC_LATENCY);
FLASH->AC |= (uint32_t)FLASH_AC_LATENCY_2;
/* HCLK = SYSCLK */
RCC->CFG |= (uint32_t)RCC_CFG_AHBPRES_DIV1;
/* PCLK2 = HCLK */
RCC->CFG |= (uint32_t)RCC_CFG_APB2PRES_DIV1;
/* PCLK1 = HCLK */
RCC->CFG |= (uint32_t)RCC_CFG_APB1PRES_DIV2;
#ifdef
STM32F10X_CL
/* Configure PLLs ------------------------------------------------------*/
/* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
/* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
RCC->CFG2 &= (uint32_t)~(RCC_CFG2_PREDIV2 | RCC_CFG2_PLL2MUL |
RCC_CFG2_PREDIV1 | RCC_CFG2_PREDIV1SRC);
RCC->CFG2 |= (uint32_t)(RCC_CFG2_PREDIV2_DIV5 | GetPll2ValueFromMult(clockSettingsPtr->Pll2) |
RCC_CFG2_PREDIV1SRC_PLL2 | RCC_CFG2_PREDIV1_DIV5);
/* Enable PLL2 */
RCC->CTRL |= RCC_CR_PLL2ON;
/* Wait till PLL2 is ready */
while((RCC->CTRL & RCC_CR_PLL2RDY) == 0)
{
}
/* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */
RCC->CFG &= (uint32_t)~(RCC_CFG_PLLXTPRE | RCC_CFG_PLLSRC | RCC_CFG_PLLMULL);
RCC->CFG |= (uint32_t)(RCC_CFG_PLLXTPRE_PREDIV1 | RCC_CFG_PLLSRC_PREDIV1 |
GetPllValueFromMult(clockSettingsPtr->Pll1));
#else
/* PLL configuration: PLLCLK = HSE * 9 = 72 MHz */
RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_PLLSRC | RCC_CFG_PLLHSEPRES |
RCC_CFG_PLLMULFCT));
RCC->CFG |= (uint32_t)(RCC_CFG_PLLSRC_HSE | GetPllValueFromMult(clockSettingsPtr->Pll1));
#endif /* STM32F10X_CL */
/* Enable PLL */
RCC->CTRL |= RCC_CTRL_PLLEN;
/* Wait till PLL is ready */
while((RCC->CTRL & RCC_CTRL_PLLRDF) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SCLKSW));
RCC->CFG |= (uint32_t)RCC_CFG_SCLKSW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFG & (uint32_t)RCC_CFG_SCLKSTS) != (uint32_t)0x08)
{
}
}
else
{ /* HSE fails to start-up, the application will have wrong clock */
NVIC_SystemReset();
}
}
写好mcal的mcu后编译成可执行文件下载到板子上,AUTOSAR能够在n32g45x上正常运行,可以看到三个任务块在调度。下载工具用的是pyocd。
五、RT-THREAD的移植
由于源码采用的构建工具是makefile,所以需要将rtthread的源码和头文件路径添加到makefile文件。编译下载到板子上可以看到rtthread正常运行。
本次移植过程中,mcal的移植较为轻松,适配n32g45x的寄存器花了不少时间,需要查看n32g45x的参考手册的寄存器定义一个一个的去改。移植rtthread到arctic core的源码比较简单,只是添加rtt的源码到arctic core的makefile。遗憾的是还没完成rtt适配OSEK/VDX的接口,希望对OSEK/VDX感兴趣的小伙伴加入进来,一起完成rtt对OESK/VDX的适配。
原作者:hello_world^_^