编写时间:2024/04/30
编写者:Caohh
使用软件:S32 Design Studio for RAM
使用硬件:S32K144EVB
一、项目创建与SDK配置
初始化GPIO功能之前需要先对GPIO功能进行配置,GPIO的基本功能是输入、输出功能,在创建项目之初建议勾选SDK;
勾选SDK之后创建的工程在编程页面的左下角可以设置相应的控件,配置需要的功能;
配置GPIO引脚为需要的功能,此处默认为输出功能;
将箭头移动到引脚号“PTExx”上,鼠标右键选择“Pin Functional Properties”配置GPIO更为详细的设置;
:::info
interrupt status field 中断状态标准位 ——不修改 清晰的标志位
interrupt configuration field 中断触发方式 (ISF)----------失能、DMA、上升沿、下降沿、上升下降沿 、逻辑1、逻辑0。
pin Mux field- ----------O复用
lock Field---------锁控制
pull enable fieled---------推挽使能
pull select field-----------选择上拉还是下拉
initial value field---------- 默认输出状态
digital filter field ----------数字滤波器
引用自:https://blog.csdn.net/weixin_45812987/article/details/136262602
:::
如果需要使用推挽输出功能,则需要使能“Pull Enable Field”;
配置好GPIO的设置,Ctrl+S保存设置;
然后点击下图图标,将SDK配置生效为项目代码;
二、底层代码初始化
使用S32DS创建K144的空白工程,需要先初始化时钟相关的代码,代码如下:
CLOCK_SYS_Init(g_clockManConfigsArr,CLOCK_MANAGER_CONFIG_CNT,g_clockManCallbacksArr,CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U,CLOCK_MANAGER_POLICY_AGREEMENT);
GPIO相关设置若想生效,需要将先运行初始化文件,其实质是根据SDK的配置的功能来配置GPIO相关寄存器,使得GPIO功能能够正常驱动;
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
因为S32DS可以通过更改SDK设置来更改项目的底层配置,从而更新底层代码。所以个人编写的代码需要放在合适的位置,杜绝在底层代码更新环节,个人代码被覆盖或者删除的情况;
int main(void)
{
/* Write your local variable definition here */
/*** 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 */
/* For example: for(;;) { } */
CLOCK_SYS_Init(g_clockManConfigsArr,CLOCK_MANAGER_CONFIG_CNT,g_clockManCallbacksArr,CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U,CLOCK_MANAGER_POLICY_AGREEMENT);
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
while(1)
{
PINS_DRV_WritePin(PTE,10,1);
PINS_DRV_WritePin(PTE,11,1);
}
/*** 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!!! ***/
三、应用层代码
3.1 GPIO输出驱动代码
/*FUNCTION**********************************************************************
*
* Function Name : PINS_DRV_WritePin
* Description : This function writes the given pin from a port, with the given value
* ('0' represents LOW, '1' represents HIGH).
*
* Implements : PINS_DRV_WritePin_Activity
*END**************************************************************************/
void PINS_DRV_WritePin(GPIO_Type * const base,
pins_channel_type_t pin,
pins_level_type_t value)
/*FUNCTION**********************************************************************
*
* Function Name : PINS_DRV_WritePins
* Description : This function writes all pins configured as output with the values given in
* the parameter pins. '0' represents LOW, '1' represents HIGH.
*
* Implements : PINS_DRV_WritePins_Activity
*END**************************************************************************/
void PINS_DRV_WritePins(GPIO_Type * const base,
pins_channel_type_t pins)
{
PINS_GPIO_WritePins(base, pins);
}
//使用实例-PTE10引脚输出高电平
//方法一:
PINS_DRV_WritePin(PTE,10,1);
//方法二:
PINS_DRV_WritePins(PTE,1<<10);
/*FUNCTION**********************************************************************
*
* Function Name : PINS_DRV_WritePin
* Description : This function writes the given pin from a port, with the given value
* ('0' represents LOW, '1' represents HIGH).
*
* Implements : PINS_DRV_WritePin_Activity
*END**************************************************************************/
void PINS_DRV_WritePin(GPIO_Type * const base,
pins_channel_type_t pin,
pins_level_type_t value)
/*FUNCTION**********************************************************************
*
* Function Name : PINS_DRV_WritePins
* Description : This function writes all pins configured as output with the values given in
* the parameter pins. '0' represents LOW, '1' represents HIGH.
*
* Implements : PINS_DRV_WritePins_Activity
*END**************************************************************************/
void PINS_DRV_WritePins(GPIO_Type * const base,
pins_channel_type_t pins)
{
PINS_GPIO_WritePins(base, pins);
}
//使用实例-PTE10输出高电平、PTE11引脚输出低电平
//方法一:
PINS_DRV_WritePin(PTE,10,1);
PINS_DRV_WritePin(PTE,11,0);
//方法二:
PINS_DRV_WritePins(PTE,1<<10|0<<11);
以上代码使用开发板做了验证,可以正常使用;GPIO引脚的输出电平是根据对应引脚与GND的电位差来观测的;
3.2 GPIO输入驱动代码
SDK中GPIO引脚的配置需要由上述的输出模式变更为输入模式:
PTE引脚具体的配置有了变动,放弃了"Pull Enable Filed"的使能设置;
:::info
问题:配置推挽使能之后,GPIO输入会有影响吗?
相关的博客:https://blog.csdn.net/luotong86/article/details/108332262
:::
更新底层代码:
//main函数之前的声明
uint8_t pin_pte_10;
uint8_t pin_pte_11;
pins_channel_type_t read_pin;
//main函数中运行的代码
read_pin=PINS_DRV_ReadPins(PTE);
pin_pte_10 = (read_pin>>10)&0x01;
pin_pte_11 = (read_pin>>11)&0x01;
//主要函数的原型
/*FUNCTION**********************************************************************
*
* Function Name : PINS_DRV_ReadPins
* Description : This function returns the current input values from a port. Only pins
* configured as input will have meaningful values.
*此函数从端口返回当前输入值。只有配置为输入的引脚才会具有有意义的值。
* Implements : PINS_DRV_ReadPins_Activity
*END**************************************************************************/
pins_channel_type_t PINS_DRV_ReadPins(const GPIO_Type * const base)
{
return PINS_GPIO_ReadPins(base);
}
以上代码使用开发板做了验证,可以正常使用;GPIO引脚的输入电平采用的是3.3V供电;