Repository: RT-Thread/rt-thread
Pull request #10329: [bsp][Infineon]修复psoc6-evaluationkit-062S2调用rt_adc_disable()时报错的问题
Author: login: yans6666
Labels: name: BSP
name: BSP: Infineon
description: BSP related with Infineon
Created: 2025-05-27T11:34:22Z | Merged: 2025-05-29T05:04:49Z

## 拉取/合并请求描述：(PR description)

[
<!-- 这段方括号里的内容是您**必须填写并替换掉**的，否则PR不可能被合并。**方括号外面的内容不需要修改，但请仔细阅读。**
The content in this square bracket must be filled in and replaced, otherwise, PR can not be merged. The contents outside square brackets need not be changed, but please read them carefully.

请在这里填写您的PR描述，可以包括以下之一的内容：为什么提交这份PR；解决的问题是什么，你的解决方案是什么；
Please fill in your PR description here, which can include one of the following items: why to submit this PR; what is the problem solved and what is your solution;

并确认并列出已经在什么情况或板卡上进行了测试。
And confirm in which case or board has been tested. -->

#### 为什么提交这份PR (why to submit this PR)


#### 你的解决方案是什么 (what is your solution)



#### 请提供验证的bsp和config (provide the config and bsp) 

<!-- 请填写验证bsp目录下面的目录比如bsp/stm32/stm32l496-st-nucleo

Please provide the path of verfied bsp. Like bsp/stm32/stm32l496-st-nucleo  bsp/ESP32_C3 -->

- BSP: bsp/Infineon/psoc6-evaluationkit-062S2

<!-- 请填写.config 文件中需要改动的config

Please provide the changed config of .config file to how to verify the PR file like CONFIG_BSP_USING_I2C CONFIG_BSP_USING_WDT -->

- .config:

<!-- 请提供自己仓库的PR branch的action的编译链接相关PR文件成功的链接：

Please provide the link of action triggered by your own repo's action  

https://github.com/RT-Thread/rt-thread/actions/workflows/manual_dist.yml -->

- action:

]

<!-- 以下的内容不应该在提交PR时的message修改，修改下述message，PR会被直接关闭。请在提交PR后，浏览器查看PR并对以下检查项逐项check，没问题后逐条在页面上打钩。
The following content must not be changed in the submitted PR message. Otherwise, the PR will be closed immediately. After submitted PR, please use a web browser to visit PR, and check items one by one, and ticked them if no problem. -->

### 当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

- [ ] 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
- [x] 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

### 代码质量 Code Quality：

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

- [x] 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
- [x] 代码风格正确，包括缩进空格，命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
- [x] 没有垃圾代码，代码尽量精简，不包含`#if 0`代码，不包含已经被注释了的代码 All redundant code is removed and cleaned up
- [ ] 所有变更均有原因及合理的，并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
- [ ] 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
- [ ] 代码是高质量的 Code in this PR is of high quality
- [ ] 已经使用[formatting](https://github.com/mysterywolf/formatting) 等源码格式化工具确保格式符合[RT-Thread代码规范](https://github.com/RT-Thread/rt-thread/blob/master/documentation/contribution_guide/coding_style_cn.md) This PR complies with [RT-Thread code specification](https://github.com/RT-Thread/rt-thread/blob/master/documentation/contribution_guide/coding_style_en.md)
- [ ] 如果是新增bsp, 已经添加ci检查到[.github/workflows/bsp_buildings.yml](workflows/bsp_buildings.yml)  详细请参考链接[BSP自查](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/development-guide/bsp-selfcheck/bsp_selfcheck)



Diff:
diff --git a/bsp/Infineon/libraries/HAL_Drivers/config/cy8c62/adc_config.h b/bsp/Infineon/libraries/HAL_Drivers/config/cy8c62/adc_config.h
new file mode 100644
index 00000000000..a2db6eac66c
--- /dev/null
+++ b/bsp/Infineon/libraries/HAL_Drivers/config/cy8c62/adc_config.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2006-2025, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author    Notes
+ * 2025-05-28     RTT       the first version
+ */
+#ifndef __ADC_CONFIG_H__
+#define __ADC_CONFIG_H__
+
+#include <rtthread.h>
+#include <rtdevice.h>
+#include "drv_common.h"
+#include "cyhal.h"
+#include "cybsp.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#if defined(BSP_USING_ADC1) || defined(BSP_USING_ADC2)
+
+cyhal_adc_t adc_obj;
+cyhal_adc_channel_t adc_chan_obj;
+
+const cyhal_adc_config_t adc_config = {
+        .continuous_scanning = false,   // Continuous Scanning is disabled
+        .average_count = 1,             // Average count disabled
+        .vref = CYHAL_ADC_REF_VDDA,     // VREF for Single ended channel set to VDDA
+        .vneg = CYHAL_ADC_VNEG_VSSA,    // VNEG for Single ended channel set to VSSA
+        .resolution = 12u,              // 12-bit resolution
+        .ext_vref = NC,                 // No connection
+        .bypass_pin = NC                // No connection
+};
+
+struct ifx_adc
+{
+    struct rt_adc_device ifx_adc_device;
+    cyhal_adc_channel_t *adc_ch;
+    char *name;
+};
+
+const cyhal_gpio_t adc_gpio[6] = {P10_0, P10_1, P10_2, P10_3, P10_4, P10_5};
+
+#ifndef ADC1_CONFIG
+#define ADC1_CONFIG                 \
+    {                               \
+        .adc_ch = &adc_chan_obj,    \
+        .name = "adc1",             \
+    }
+#endif
+
+#endif /* defined(BSP_USING_ADC1) || defined(BSP_USING_ADC2) */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ADC_CONFIG_H__ */
diff --git a/bsp/Infineon/libraries/HAL_Drivers/config/drv_config.h b/bsp/Infineon/libraries/HAL_Drivers/config/drv_config.h
new file mode 100644
index 00000000000..3f87ecb5c86
--- /dev/null
+++ b/bsp/Infineon/libraries/HAL_Drivers/config/drv_config.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2006-2025, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author    Notes
+ * 2025-05-28     RTT       the first version
+ */
+#ifndef __DRV_CONFIG_H__
+#define __DRV_CONFIG_H__
+
+#include "board.h"
+#include <rtthread.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#if defined(SOC_CY8C624ALQI_S2D42) || defined(SOC_CY8C6347BZI_BLD53) || defined(SOC_CY8C6247BZI_D54) || defined(SOC_CY8C6244LQI_S4D92)
+
+#ifdef BSP_USING_ADC
+#include "cy8c62/adc_config.h"
+#endif
+
+#endif /* SOC_CY8C624ALQI_S2D42 */
+
+#if defined(SOC_XMC7100D_F144K4160AA)
+
+#ifdef BSP_USING_ADC
+#include "xmc7100/adc_config.h"
+#endif
+
+#endif /* SOC_XMC7100D_F144K4160AA */
+
+#if defined(SOC_XMC7200D_E272K8384AA)
+
+#ifdef BSP_USING_ADC
+#include "xmc7200/adc_config.h"
+#endif
+
+#endif /* SOC_XMC7200D_E272K8384AA */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __DRV_CONFIG_H__ */
diff --git a/bsp/Infineon/libraries/HAL_Drivers/config/xmc7100/adc_config.h b/bsp/Infineon/libraries/HAL_Drivers/config/xmc7100/adc_config.h
new file mode 100644
index 00000000000..7c664c1a20d
--- /dev/null
+++ b/bsp/Infineon/libraries/HAL_Drivers/config/xmc7100/adc_config.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2006-2025, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author    Notes
+ * 2025-05-28     RTT       the first version
+ */
+#ifndef __ADC_CONFIG_H__
+#define __ADC_CONFIG_H__
+
+#include <rtthread.h>
+#include <rtdevice.h>
+#include "drv_common.h"
+#include "cyhal.h"
+#include "cybsp.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#if defined(BSP_USING_ADC1) || defined(BSP_USING_ADC2)
+
+cyhal_adc_t adc_obj;
+cyhal_adc_channel_t adc_chan_obj;
+
+const cyhal_adc_config_t adc_config = {
+        .continuous_scanning = false,   // Continuous Scanning is disabled
+        .average_count = 1,             // Average count disabled
+        .vref = CYHAL_ADC_REF_VDDA,     // VREF for Single ended channel set to VDDA
+        .vneg = CYHAL_ADC_VNEG_VSSA,    // VNEG for Single ended channel set to VSSA
+        .resolution = 12u,              // 12-bit resolution
+        .ext_vref = NC,                 // No connection
+        .bypass_pin = NC                // No connection
+};
+
+struct ifx_adc
+{
+    struct rt_adc_device ifx_adc_device;
+    cyhal_adc_channel_t *adc_ch;
+    char *name;
+};
+
+const cyhal_gpio_t adc_gpio[8] = {P6_0, P11_0, P11_1, P11_2, P12_4, P13_7, P14_2, P14_3};
+
+#ifndef ADC1_CONFIG
+#define ADC1_CONFIG                 \
+    {                               \
+        .adc_ch = &adc_chan_obj,    \
+        .name = "adc1",             \
+    }
+#endif
+
+#endif /* defined(BSP_USING_ADC1) || defined(BSP_USING_ADC2) */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ADC_CONFIG_H__ */
diff --git a/bsp/Infineon/libraries/HAL_Drivers/config/xmc7200/adc_config.h b/bsp/Infineon/libraries/HAL_Drivers/config/xmc7200/adc_config.h
new file mode 100644
index 00000000000..7af3d53fe04
--- /dev/null
+++ b/bsp/Infineon/libraries/HAL_Drivers/config/xmc7200/adc_config.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2006-2025, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author    Notes
+ * 2025-05-28     RTT       the first version
+ */
+#ifndef __ADC_CONFIG_H__
+#define __ADC_CONFIG_H__
+
+#include <rtthread.h>
+#include <rtdevice.h>
+#include "drv_common.h"
+#include "cyhal.h"
+#include "cybsp.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#if defined(BSP_USING_ADC1) || defined(BSP_USING_ADC2)
+
+cyhal_adc_t adc_obj;
+cyhal_adc_channel_t adc_chan_obj;
+
+const cyhal_adc_config_t adc_config = {
+        .continuous_scanning = false,   // Continuous Scanning is disabled
+        .average_count = 1,             // Average count disabled
+        .vref = CYHAL_ADC_REF_VDDA,     // VREF for Single ended channel set to VDDA
+        .vneg = CYHAL_ADC_VNEG_VSSA,    // VNEG for Single ended channel set to VSSA
+        .resolution = 12u,              // 12-bit resolution
+        .ext_vref = NC,                 // No connection
+        .bypass_pin = NC                // No connection
+};
+
+struct ifx_adc
+{
+    struct rt_adc_device ifx_adc_device;
+    cyhal_adc_channel_t *adc_ch;
+    char *name;
+};
+
+const cyhal_gpio_t adc_gpio[8] = {P14_0, P14_1, P14_2, P14_3, P14_4, P14_5, P14_6, P14_7};
+
+#ifndef ADC1_CONFIG
+#define ADC1_CONFIG                 \
+    {                               \
+        .adc_ch = &adc_chan_obj,    \
+        .name = "adc1",             \
+    }
+#endif
+
+#endif /* defined(BSP_USING_ADC1) || defined(BSP_USING_ADC2) */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ADC_CONFIG_H__ */
diff --git a/bsp/Infineon/libraries/HAL_Drivers/drv_adc.c b/bsp/Infineon/libraries/HAL_Drivers/drv_adc.c
index 6b043743467..674ba727fa1 100644
--- a/bsp/Infineon/libraries/HAL_Drivers/drv_adc.c
+++ b/bsp/Infineon/libraries/HAL_Drivers/drv_adc.c
@@ -7,11 +7,7 @@
  * Date           Author       Notes
  * 2022-07-04     Rbb666       first version
  */
-#include "drv_common.h"
-#include "drv_adc.h"
-
-#include "cyhal.h"
-#include "cybsp.h"
+#include "drv_config.h"
 
 #if defined(BSP_USING_ADC1) || defined(BSP_USING_ADC2)
 
@@ -19,15 +15,6 @@
 #define LOG_TAG             "drv.adc"
 #include <drv_log.h>
 
-#define VPLUS_CHANNEL_0     (P10_0)
-
-struct ifx_adc
-{
-    struct rt_adc_device ifx_adc_device;
-    cyhal_adc_channel_t *adc_ch;
-    char *name;
-};
-
 static struct ifx_adc ifx_adc_obj[] =
 {
     #ifdef BSP_USING_ADC1
@@ -52,8 +39,7 @@ static rt_err_t ifx_adc_enabled(struct rt_adc_device *device, rt_uint32_t channe
 
     if (enabled)
     {
-        /* Initialize ADC. The ADC block which can connect to pin 10[0] is selected */
-        result = cyhal_adc_init(&adc_obj, VPLUS_CHANNEL_0, NULL);
+        result = cyhal_adc_init(&adc_obj, adc_gpio[channel], NULL);
 
         if (result != RT_EOK)
         {
@@ -61,8 +47,7 @@ static rt_err_t ifx_adc_enabled(struct rt_adc_device *device, rt_uint32_t channe
             return -RT_ENOSYS;
         }
 
-        /* Initialize a channel 0 and configure it to scan P10_0 in single ended mode. */
-        result  = cyhal_adc_channel_init_diff(adc_ch, &adc_obj, VPLUS_CHANNEL_0,
+        result  = cyhal_adc_channel_init_diff(adc_ch, &adc_obj, adc_gpio[channel],
                                               CYHAL_ADC_VNEG, &channel_config);
 
         if (result != RT_EOK)
@@ -76,14 +61,14 @@ static rt_err_t ifx_adc_enabled(struct rt_adc_device *device, rt_uint32_t channe
 
         if (result != RT_EOK)
         {
-            printf("ADC configuration update failed. Error: %u\n", result);
+            rt_kprintf("ADC configuration update failed. Error: %u\n", result);
             return -RT_ENOSYS;
         }
     }
     else
     {
-        cyhal_adc_free(&adc_obj);
         cyhal_adc_channel_free(adc_ch);
+        cyhal_adc_free(&adc_obj);
     }
 
     return RT_EOK;
@@ -119,7 +104,7 @@ static int rt_hw_adc_init(void)
         /* register ADC device */
         if (rt_hw_adc_register(&ifx_adc_obj[i].ifx_adc_device, ifx_adc_obj[i].name, &at_adc_ops, ifx_adc_obj[i].adc_ch) == RT_EOK)
         {
-            LOG_D("%s register success", at32_adc_obj[i].name);
+            LOG_D("%s register success", ifx_adc_obj[i].name);
         }
         else
         {


Reviews:
user: login: Rbb666
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-28T02:56:24Z
user: login: Rbb666
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-28T09:03:36Z
user: login: yans6666
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-28T09:19:54Z
user: login: Rbb666
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-29T02:08:46Z
user: login: Rbb666
state: APPROVED
author_association: MEMBER
submitted_at: 2025-05-29T05:04:42Z

Review comments:
diff_hunk: @@ -62,7 +62,7 @@ static rt_err_t ifx_adc_enabled(struct rt_adc_device *device, rt_uint32_t channe
         }
 
         /* Initialize a channel 0 and configure it to scan P10_0 in single ended mode. */
path: bsp/Infineon/libraries/HAL_Drivers/drv_adc.c
user: login: Rbb666
body: 这么改了后，注释记得也修改下吧
created_at: 2025-05-28T02:56:20Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2025-05-28     RTT       the first version
+ */
+#ifndef __ADC_CONFIG_H__
+#define __ADC_CONFIG_H__
+
+#include <rtthread.h>
+#include <rtdevice.h>
+#include "drv_common.h"
+#include "cyhal.h"
+#include "cybsp.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#if defined(BSP_USING_ADC1) || defined(BSP_USING_ADC2)
+
+cyhal_adc_t adc_obj;
+cyhal_adc_channel_t adc_chan_obj;
+
+const cyhal_adc_config_t adc_config = {
+        .continuous_scanning = false,   // Continuous Scanning is disabled
+        .average_count = 1,             // Average count disabled
+        .vref = CYHAL_ADC_REF_VDDA,     // VREF for Single ended channel set to VDDA
+        .vneg = CYHAL_ADC_VNEG_VSSA,    // VNEG for Single ended channel set to VSSA
+        .resolution = 12u,              // 12-bit resolution
+        .ext_vref = NC,                 // No connection
+        .bypass_pin = NC                // No connection
+};
+
+struct ifx_adc
+{
+    struct rt_adc_device ifx_adc_device;
+    cyhal_adc_channel_t *adc_ch;
+    char *name;
+};
+
+const cyhal_gpio_t adc_gpio[8] = {P14_0, P14_1, P14_2, P14_3, P14_4, P14_5, P14_6, P14_7};
path: bsp/Infineon/libraries/HAL_Drivers/config/xmc7200/adc_config.h
user: login: Rbb666
body: 这几个IO在psoc6平台上都是通用的吗？
created_at: 2025-05-28T09:02:24Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2025-05-28     RTT       the first version
+ */
+#ifndef __ADC_CONFIG_H__
+#define __ADC_CONFIG_H__
+
+#include <rtthread.h>
+#include <rtdevice.h>
+#include "drv_common.h"
+#include "cyhal.h"
+#include "cybsp.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#if defined(BSP_USING_ADC1) || defined(BSP_USING_ADC2)
+
+cyhal_adc_t adc_obj;
+cyhal_adc_channel_t adc_chan_obj;
+
+const cyhal_adc_config_t adc_config = {
+        .continuous_scanning = false,   // Continuous Scanning is disabled
+        .average_count = 1,             // Average count disabled
+        .vref = CYHAL_ADC_REF_VDDA,     // VREF for Single ended channel set to VDDA
+        .vneg = CYHAL_ADC_VNEG_VSSA,    // VNEG for Single ended channel set to VSSA
+        .resolution = 12u,              // 12-bit resolution
+        .ext_vref = NC,                 // No connection
+        .bypass_pin = NC                // No connection
+};
+
+struct ifx_adc
+{
+    struct rt_adc_device ifx_adc_device;
+    cyhal_adc_channel_t *adc_ch;
+    char *name;
+};
+
+const cyhal_gpio_t adc_gpio[8] = {P14_0, P14_1, P14_2, P14_3, P14_4, P14_5, P14_6, P14_7};
path: bsp/Infineon/libraries/HAL_Drivers/config/xmc7200/adc_config.h
user: login: yans6666
body: SOC_CY8C624ALQI_S2D42, SOC_CY8C6347BZI_BLD53, SOC_CY8C6247BZI_D54, SOC_CY8C6244LQI_S4D92这几个芯片的ADC引脚是一样的
created_at: 2025-05-28T09:19:54Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
path: bsp/Infineon/libraries/HAL_Drivers/config/cy8c62/adc_config.h
user: login: Rbb666
body: 日期需要都修改下
created_at: 2025-05-29T02:08:12Z
author_association: MEMBER

PR comments:
user: login: CLAassistant
created_at: 2025-05-27T11:34:29Z
body: [![CLA assistant check](https://cla-assistant.io/pull/badge/signed)](https://cla-assistant.io/RT-Thread/rt-thread?pullRequest=10329) <br/>All committers have signed the CLA.
author_association: NONE
user: login: kurisaW
created_at: 2025-05-28T01:33:30Z
body: 需考虑一下rt_device级别的API实现是不是支持这样的写法
author_association: MEMBER
user: login: kurisaW
created_at: 2025-05-28T08:43:51Z
body: lgtm
author_association: MEMBER