S32K144的GPIO驱动样例 S32DS for RAM

编写时间:2024/04/30
编写者:Caohh
使用软件:S32 Design Studio for RAM
使用硬件:S32K144EVB

一、项目创建与SDK配置

初始化GPIO功能之前需要先对GPIO功能进行配置,GPIO的基本功能是输入、输出功能,在创建项目之初建议勾选SDK;
image.png
勾选SDK之后创建的工程在编程页面的左下角可以设置相应的控件,配置需要的功能;
982d1e4b8dc0b95f6836d1fdf4f0de6.png 33805d48c9d87a52d2cf9d591b358a2.png
配置GPIO引脚为需要的功能,此处默认为输出功能;
第一步:使能引脚的GPIO功能并配置输入输出特性
b2661e0f8c18ca1255100fab72d31a0.jpg
将箭头移动到引脚号“PTExx”上,鼠标右键选择“Pin Functional Properties”配置GPIO更为详细的设置;
image.png
image.png
3f03844b5550fe4192ac8a78b901a5b.png
:::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配置生效为项目代码;
c44bb517851080574a7d6b428e7d79e.png

二、底层代码初始化

使用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引脚的配置需要由上述的输出模式变更为输入模式:
image.png
PTE引脚具体的配置有了变动,放弃了"Pull Enable Filed"的使能设置;
:::info
问题:配置推挽使能之后,GPIO输入会有影响吗?
相关的博客:https://blog.csdn.net/luotong86/article/details/108332262
:::
0885811ac9bb5a66054f6cacff5a8c7.png
18ea05c7e4c79ac0ee2425f69db68ef.png
更新底层代码:

//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供电;

  • 16
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风筝~断弦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值