官方提供了很多的参考例程,每个历程分别配置了不同的外设,这里挨个尝试解读一下。
示例效果
RGB红灯绿灯交替闪烁。
导入示例
示例文件所在目录:
该示例使用PCC和PORT模块交替切换两个LED。
硬件连接
配置引脚功能 生成代码
S32DS自带引脚配置器:pin_mux,就和stm32的cubmux一样,只不过直接集成到这个软件里面了。
当然也可以在这里直接配置时钟:
等等,这一切配置完之后,点生成代码按钮:
生成之后的代码会在这个文件夹下:
很直观的你也可以看到你配置的东西,已经转化成了代码。
应用层代码解析
很简单:
1、引用生成的代码头文件
/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "clockMan1.h"
#include "pin_mux.h"
#if CPU_INIT_CONFIG
#include "Init_Config.h"
#endif
volatile int exit_code = 0;
/* User includes (#include below this line is not maintained by Processor Expert) */
#include <stdint.h>
#include <stdbool.h>
2、根据不同的硬件,条件编译一些define
/* This example is setup to work by default with EVB. To use it with other boards
please comment the following line
*/
#define EVB
#ifdef EVB
#define LED_PORT PORTD
#define GPIO_PORT PTD
#define PCC_CLOCK PCC_PORTD_CLOCK
#define LED1 15U
#define LED2 16U
#else
#define LED_PORT PORTC
#define GPIO_PORT PTC
#define PCC_CLOCK PCC_PORTC_CLOCK
#define LED1 0U
#define LED2 1U
#endif
3、主函数
配置好时钟:
/* Write your code here */
/* Initialize and configure clocks
* - see clock manager component for details
*/
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
对引脚进行初始化:
/* Initialize pins
* - See PinSettings component for more info
*/
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
/* Output direction for LED0 & LED1 */
PINS_DRV_SetPinsDirection(GPIO_PORT, ((1 << LED1) | (1 << LED2)));
/* Set Output value LED0 & LED1 */
PINS_DRV_SetPins(GPIO_PORT, 1 << LED1);
PINS_DRV_ClearPins(GPIO_PORT, 1 << LED2);
然后进入无限的for循环(这里是和以前while不一样的地方)。
for (;;)
{
/* Insert a small delay to make the blinking visible */
delay(720000);
/* Toggle output value LED0 & LED1 */
PINS_DRV_TogglePins(GPIO_PORT, ((1 << LED1) | (1 << LED2)));
}
附录代码:
/*
* Copyright (c) 2015 - 2016 , Freescale Semiconductor, Inc.
* Copyright 2016-2017 NXP
* All rights reserved.
*
* THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "clockMan1.h"
#include "pin_mux.h"
#if CPU_INIT_CONFIG
#include "Init_Config.h"
#endif
volatile int exit_code = 0;
/* User includes (#include below this line is not maintained by Processor Expert) */
#include <stdint.h>
#include <stdbool.h>
/* This example is setup to work by default with EVB. To use it with other boards
please comment the following line
*/
#define EVB
#ifdef EVB
#define LED_PORT PORTD
#define GPIO_PORT PTD
#define PCC_CLOCK PCC_PORTD_CLOCK
#define LED1 15U
#define LED2 16U
#else
#define LED_PORT PORTC
#define GPIO_PORT PTC
#define PCC_CLOCK PCC_PORTC_CLOCK
#define LED1 0U
#define LED2 1U
#endif
void delay(volatile int cycles)
{
/* Delay function - do nothing for a number of cycles */
while(cycles--);
}
/*!
\brief The main function for the project.
\details The startup initialization sequence is the following:
* - __start (startup asm routine)
* - __init_hardware()
* - main()
* - PE_low_level_init()
* - Common_Init()
* - Peripherals_Init()
*/
int main(void)
{
/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
#ifdef PEX_RTOS_INIT
PEX_RTOS_INIT(); /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of Processor Expert internal initialization. ***/
/* Write your code here */
/* Initialize and configure clocks
* - see clock manager component for details
*/
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
/* Initialize pins
* - See PinSettings component for more info
*/
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
/* Output direction for LED0 & LED1 */
PINS_DRV_SetPinsDirection(GPIO_PORT, ((1 << LED1) | (1 << LED2)));
/* Set Output value LED0 & LED1 */
PINS_DRV_SetPins(GPIO_PORT, 1 << LED1);
PINS_DRV_ClearPins(GPIO_PORT, 1 << LED2);
for (;;)
{
/* Insert a small delay to make the blinking visible */
delay(720000);
/* Toggle output value LED0 & LED1 */
PINS_DRV_TogglePins(GPIO_PORT, ((1 << LED1) | (1 << LED2)));
}
/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
/*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
#ifdef PEX_RTOS_START
PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of RTOS startup code. ***/
/*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
for(;;) {
if(exit_code != 0) {
break;
}
}
return exit_code;
/*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
/* END main */
/*!
** @}
*/
/*
** ###################################################################
**
** This file was created by Processor Expert 10.1 [05.21]
** for the Freescale S32K series of microcontrollers.
**
** ###################################################################
*/