From 4f778420d234f976a31dc42503e496d1c95d1ee4 Mon Sep 17 00:00:00 2001 From: LZerro <2669707827@qq.com> Date: Thu, 22 Aug 2024 17:00:51 +0800 Subject: [PATCH 1/2] [bsp][Infineon][psoc6-evk-062s2]Adapt to RTduino --- bsp/Infineon/libraries/HAL_Drivers/drv_pwm.c | 52 +++++------ bsp/Infineon/libraries/HAL_Drivers/drv_pwm.h | 38 +++++++- .../applications/SConscript | 14 +-- .../applications/arduino_main.cpp | 24 +++++ .../applications/arduino_pinout/README.md | 43 +++++++++ .../applications/arduino_pinout/SConscript | 11 +++ .../arduino_pinout/pins_arduino.c | 40 +++++++++ .../arduino_pinout/pins_arduino.h | 45 ++++++++++ .../psoc6-evaluationkit-062S2/board/Kconfig | 90 +++++++++++++++---- .../config/GeneratedSource/cycfg_pins.c | 4 +- 10 files changed, 311 insertions(+), 50 deletions(-) create mode 100644 bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_main.cpp create mode 100644 bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_pinout/README.md create mode 100644 bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_pinout/SConscript create mode 100644 bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_pinout/pins_arduino.c create mode 100644 bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_pinout/pins_arduino.h diff --git a/bsp/Infineon/libraries/HAL_Drivers/drv_pwm.c b/bsp/Infineon/libraries/HAL_Drivers/drv_pwm.c index b25e61e9e06..7a6a624f6ae 100644 --- a/bsp/Infineon/libraries/HAL_Drivers/drv_pwm.c +++ b/bsp/Infineon/libraries/HAL_Drivers/drv_pwm.c @@ -31,35 +31,51 @@ struct ifx_pwm static struct ifx_pwm ifx_pwm_obj[] = { -#ifdef BSP_USING_PWM0_PORT0 +#ifdef BSP_USING_PWM0_CH0_PORT0 PWM0_CH0_PORT0_CONFIG, #endif -#ifdef BSP_USING_PWM0_PORT2 +#ifdef BSP_USING_PWM0_CH2_PORT11_COMPL + PWM0_CH2_PORT11_COMPL_CONFIG, +#endif + +#ifdef BSP_USING_PWM0_CH3_PORT11 + PWM0_CH3_PORT11_CONFIG, +#endif + +#ifdef BSP_USING_PWM0_CH4_PORT5_COMPL + PWM0_CH4_PORT5_COMPL_CONFIG, +#endif + +#ifdef BSP_USING_PWM0_CH7_PORT2 PWM0_CH7_PORT2_CONFIG, #endif -#ifdef BSP_USING_PWM0_PORT5 +#ifdef BSP_USING_PWM0_CH7_PORT5 PWM0_CH7_PORT5_CONFIG, #endif -#ifdef BSP_USING_PWM0_PORT7 +#ifdef BSP_USING_PWM0_CH7_PORT5_COMPL + PWM0_CH7_PORT5_COMPL_CONFIG, +#endif + +#ifdef BSP_USING_PWM0_CH7_PORT7 PWM0_CH7_PORT7_CONFIG, #endif -#ifdef BSP_USING_PWM0_PORT9 +#ifdef BSP_USING_PWM0_CH7_PORT9 PWM0_CH7_PORT9_CONFIG, #endif -#ifdef BSP_USING_PWM0_PORT10 +#ifdef BSP_USING_PWM0_CH7_PORT10 PWM0_CH7_PORT10_CONFIG, #endif -#ifdef BSP_USING_PWM0_PORT12 +#ifdef BSP_USING_PWM0_CH7_PORT12 PWM0_CH7_PORT12_CONFIG, #endif -#ifdef BSP_USING_PWM0_PORT13 +#ifdef BSP_USING_PWM0_CH7_PORT13 PWM0_CH3_PORT13_CONFIG, #endif }; @@ -73,26 +89,12 @@ static rt_err_t drv_pwm_enable(cyhal_pwm_t *htim, struct rt_pwm_configuration *c { if (!enable) { - if (channel == 3) - { - htim->tcpwm.resource.channel_num = channel; - } - else if (channel == 7) - { - htim->tcpwm.resource.channel_num = channel; - } + htim->tcpwm.resource.channel_num = channel; cyhal_pwm_stop(htim); } else { - if (channel == 3) - { - htim->tcpwm.resource.channel_num = channel; - } - else if (channel == 7) - { - htim->tcpwm.resource.channel_num = channel; - } + htim->tcpwm.resource.channel_num = channel; cyhal_pwm_start(htim); } } @@ -217,7 +219,7 @@ static int rt_hw_pwm_init(void) INIT_BOARD_EXPORT(rt_hw_pwm_init); #define PWM_DEV_NAME "pwm0" -#define PWM_DEV_CHANNEL 7 +#define PWM_DEV_CHANNEL 4 struct rt_device_pwm *pwm_dev; diff --git a/bsp/Infineon/libraries/HAL_Drivers/drv_pwm.h b/bsp/Infineon/libraries/HAL_Drivers/drv_pwm.h index be5dbee9c98..6733b310cf6 100644 --- a/bsp/Infineon/libraries/HAL_Drivers/drv_pwm.h +++ b/bsp/Infineon/libraries/HAL_Drivers/drv_pwm.h @@ -29,7 +29,34 @@ extern "C" .channel = 0, \ .gpio = GET_PIN(0, 0), \ } -#endif /* PWM0_CH7_PORT2_CONFIG */ +#endif /* PWM0_CH0_PORT2_CONFIG */ + +#ifndef PWM0_CH2_PORT11_COMPL_CONFIG +#define PWM0_CH2_PORT11_COMPL_CONFIG \ + { \ + .name = "pwm0", \ + .channel = 2, \ + .gpio = GET_PIN(11, 3), \ + } +#endif /*PWM0_CH2_PORT11_COMPL_CONFIG*/ + +#ifndef PWM0_CH3_PORT11_CONFIG +#define PWM0_CH3_PORT11_CONFIG \ + { \ + .name = "pwm0", \ + .channel = 3, \ + .gpio = GET_PIN(11, 4), \ + } +#endif /*PWM0_CH3_PORT11_CONFIG*/ + +#ifndef PWM0_CH4_PORT5_COMPL_CONFIG +#define PWM0_CH4_PORT5_COMPL_CONFIG \ + { \ + .name = "pwm0", \ + .channel = 4, \ + .gpio = GET_PIN(5, 1), \ + } +#endif /*PWM0_CH4_PORT5_COMPL_CONFIG*/ #ifndef PWM0_CH7_PORT2_CONFIG #define PWM0_CH7_PORT2_CONFIG \ @@ -49,6 +76,15 @@ extern "C" } #endif /* PWM0_CH7_PORT5_CONFIG */ +#ifndef PWM0_CH7_PORT5_COMPL_CONFIG +#define PWM0_CH7_PORT5_COMPL_CONFIG \ + { \ + .name = "pwm0", \ + .channel = 7, \ + .gpio = GET_PIN(5, 7), \ + } +#endif /*PWM0_CH7_PORT5_COMPL_CONFIG*/ + #ifndef PWM0_CH7_PORT7_CONFIG #define PWM0_CH7_PORT7_CONFIG \ { \ diff --git a/bsp/Infineon/psoc6-evaluationkit-062S2/applications/SConscript b/bsp/Infineon/psoc6-evaluationkit-062S2/applications/SConscript index 034056eca76..46993674577 100644 --- a/bsp/Infineon/psoc6-evaluationkit-062S2/applications/SConscript +++ b/bsp/Infineon/psoc6-evaluationkit-062S2/applications/SConscript @@ -1,16 +1,18 @@ -import rtconfig from building import * import os -cwd = GetCurrentDir() -path = [cwd] -src = Glob('*.c') +cwd = GetCurrentDir() +src = Glob('*.c') +CPPPATH = [cwd] -group = DefineGroup('Applications', src, depend = [''], CPPPATH = path) +if GetDepend(['PKG_USING_RTDUINO']) and not GetDepend(['RTDUINO_NO_SETUP_LOOP']): + src += ['arduino_main.cpp'] + +group = DefineGroup('Applications', src, depend = [''], CPPPATH = CPPPATH) list = os.listdir(cwd) for item in list: if os.path.isfile(os.path.join(cwd, item, 'SConscript')): group = group + SConscript(os.path.join(item, 'SConscript')) -Return('group') +Return('group') \ No newline at end of file diff --git a/bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_main.cpp b/bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_main.cpp new file mode 100644 index 00000000000..124dde19033 --- /dev/null +++ b/bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_main.cpp @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2006-2024, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2024-08-16 LZerro first version + */ + +#include + +void setup(void) +{ + /* put your setup code here, to run once: */ + Serial.begin(); +} + +void loop(void) +{ + /* put your main code here, to run repeatedly: */ + Serial.println("Hello Arduino!"); + delay(800); +} diff --git a/bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_pinout/README.md b/bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_pinout/README.md new file mode 100644 index 00000000000..2bc7ffe75d5 --- /dev/null +++ b/bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_pinout/README.md @@ -0,0 +1,43 @@ +# board_default 开发板的Arduino生态兼容说明 + +## 1 RTduino - RT-Thread的Arduino生态兼容层 + +board_default 开发板已经完整适配了[RTduino软件包](https://github.com/RTduino/RTduino),即RT-Thread的Arduino生态兼容层。用户可以按照Arduino的编程习惯来操作该BSP,并且可以使用大量Arduino社区丰富的库,是对RT-Thread生态的极大增强。更多信息,请参见[RTduino软件包说明文档](https://github.com/RTduino/RTduino)。 + +### 1.1 如何开启针对本BSP的Arduino生态兼容层 + +Env 工具下敲入 menuconfig 命令,或者 RT-Thread Studio IDE 下选择 RT-Thread Settings: + +```Kconfig +Hardware Drivers Config ---> + Onboard Peripheral Drivers ---> + [*] Compatible with Arduino Ecosystem (RTduino) +``` + +## 2 Arduino引脚排布 + +更多引脚布局相关信息参见 [pins_arduino.c](pins_arduino.c) 和 [pins_arduino.h](pins_arduino.h)。 + +| Arduino引脚编号 | STM32引脚编号 | 5V容忍 | 备注 | +| ------------------- | --------- | ---- | ------------------------------------------------------------------------- | +| 0 (D0) | BSP_IO_PORT_06_PIN_04 | 否 | Serial-RX,默认被RT-Thread的UART设备框架uart6接管 | +| 1 (D1) | BSP_IO_PORT_06_PIN_05 | 否 | Serial-TX,默认被RT-Thread的UART设备框架uart6接管 | +| 2 (D2) | BSP_IO_PORT_05_PIN_00 | 否 | | +| 3 (D3) | BSP_IO_PORT_05_PIN_01 | 否 | PWM0-CH0,默认被RT-Thread的PWM设备框架pwm0接管 | +| 4 (D4) | BSP_IO_PORT_11_PIN_02 | 否 | | +| 5 (D5) | BSP_IO_PORT_11_PIN_03 | 否 | PWM0-CH0,默认被RT-Thread的PWM设备框架pwm0接管 | +| 6 (D6) | BSP_IO_PORT_11_PIN_04 | 否 | PWM0-CH0,默认被RT-Thread的PWM设备框架pwm0接管 | +| 7 (D7) | BSP_IO_PORT_11_PIN_05 | 否 | | +| 8 (D8) | BSP_IO_PORT_11_PIN_06 | 否 | | +| 9 (D9) | BSP_IO_PORT_05_PIN_07 | 否 | PWM0-CH0,默认被RT-Thread的PWM设备框架pwm0接管 | +| 10 (D10) | BSP_IO_PORT_00_PIN_05 | 否 | | +| 11 (D11) | BSP_IO_PORT_00_PIN_02 | 否 | SPI0-MOSI,默认被RT-Thread的SPI设备框架spi0接管 | +| 12 (D12) | BSP_IO_PORT_00_PIN_03 | 否 | SPI0-MISO,默认被RT-Thread的SPI设备框架spi0接管 | +| 13 (D13) | BSP_IO_PORT_00_PIN_04 | 否 | SPI0-SCK,默认被RT-Thread的SPI设备框架spi0接管 | +| 14 (D14) | BSP_IO_PORT_08_PIN_01 | 否 | I2C0-SDA,默认被RT-Thread的I2C设备框架i2c0接管 | +| 15 (D15) | BSP_IO_PORT_08_PIN_00 | 否 | I2C0-SCL,默认被RT-Thread的I2C设备框架i2c0接管 | + +> 注意: +> +> 1. xxxxxxxxx +> 2. xxxxxxxxx diff --git a/bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_pinout/SConscript b/bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_pinout/SConscript new file mode 100644 index 00000000000..d6ff4e1030f --- /dev/null +++ b/bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_pinout/SConscript @@ -0,0 +1,11 @@ +from building import * + +cwd = GetCurrentDir() +src = Glob('*.c') + Glob('*.cpp') +inc = [cwd] + +print(cwd) + +group = DefineGroup('RTduino', src, depend = ['PKG_USING_RTDUINO'], CPPPATH = inc) + +Return('group') diff --git a/bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_pinout/pins_arduino.c b/bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_pinout/pins_arduino.c new file mode 100644 index 00000000000..64f3a59b21e --- /dev/null +++ b/bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_pinout/pins_arduino.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2006-2024, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2024-08-16 LZerro first version + */ + +#include +#include "drv_gpio.h" +#include "pins_arduino.h" + +/* + * {Arduino Pin, RT-Thread Pin [, Device Name, Channel]} + * [] means optional + * Digital pins must NOT give the device name and channel. + * Analog pins MUST give the device name and channel(ADC, PWM or DAC). + * Arduino Pin must keep in sequence. + */ +const pin_map_t pin_map_table[]= +{ + {D00, GET_PIN(6, 4), "uart6"}, /* Serial6-RX */ + {D01, GET_PIN(6, 5), "uart6"}, /* Serial6-TX */ + {D2, GET_PIN(5, 0)}, + {D3, GET_PIN(5, 1), "pwm0", 4}, /* PWM */ + {D4, GET_PIN(11, 2)}, + {D5, GET_PIN(11, 3), "pwm0", 2}, /* PWM */ + {D6, GET_PIN(11, 4), "pwm0", 3}, + {D7, GET_PIN(11, 5)}, + {D8, GET_PIN(5, 6)}, + {D9, GET_PIN(5, 7), "pwm0", 7}, /* PWM */ + {D10, GET_PIN(0, 5)}, /* SPI0_CS*/ + {D11, GET_PIN(0, 2), "spi0"}, /* SPI0_MOSI */ + {D12, GET_PIN(0, 3), "spi0"}, /* SPI0_MISO */ + {D13, GET_PIN(0, 4), "spi0"}, /* SPI_SCK */ + {D14, GET_PIN(8, 1), "i2c4"}, /* I2C-SDA (Soft Wire) */ + {D15, GET_PIN(8, 0), "i2c4"}, /* I2C-SCL (Soft Wire) */ +}; diff --git a/bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_pinout/pins_arduino.h b/bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_pinout/pins_arduino.h new file mode 100644 index 00000000000..d56dfea9bde --- /dev/null +++ b/bsp/Infineon/psoc6-evaluationkit-062S2/applications/arduino_pinout/pins_arduino.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2006-2024, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2024-08-16 LZerro first version + */ + +#ifndef Pins_Arduino_h +#define Pins_Arduino_h + +/* pins alias. Must keep in sequence */ +#define D00 (0) +#define D01 (1) +#define D2 (2) +#define D3 (3) +#define D4 (4) +#define D5 (5) +#define D6 (6) +#define D7 (7) +#define D8 (8) +#define D9 (9) +#define D10 (10) +#define D11 (11) +#define D12 (12) +#define D13 (13) +#define D14 (14) +#define D15 (15) + +#define RTDUINO_PIN_MAX_LIMIT D15 /* pin number max limit check */ + +#define F_CPU 150000000L /* CPU:150MHz */ + +// #define LED_BUILTIN D0 /* Default Built-in LED */ + +/* i2c0 : P08.01-SDA P08.00-SCL */ +#define RTDUINO_DEFAULT_IIC_BUS_NAME "i2c4" + +/* spi0 : P0.04-SCK P00.03-MISO P00.02-MOSI */ +#define SS D10 /* Chip select pin of default spi */ +#define RTDUINO_DEFAULT_SPI_BUS_NAME "spi0" + +#endif /* Pins_Arduino_h */ diff --git a/bsp/Infineon/psoc6-evaluationkit-062S2/board/Kconfig b/bsp/Infineon/psoc6-evaluationkit-062S2/board/Kconfig index 133c3f2c38e..f528ce37697 100644 --- a/bsp/Infineon/psoc6-evaluationkit-062S2/board/Kconfig +++ b/bsp/Infineon/psoc6-evaluationkit-062S2/board/Kconfig @@ -15,6 +15,31 @@ menu "Onboard Peripheral Drivers" select BSP_USING_UART6 default y + config BSP_USING_ARDUINO + bool "Compatible with Arduino Ecosystem (RTduino)" + select PKG_USING_RTDUINO + select BSP_USING_STLINK_TO_USART + select BSP_USING_UART6 + select BSP_USING_GPIO + select BSP_USING_PWM + select BSP_USING_PWM0 + select BSP_USING_PWM0_CH2 + select BSP_USING_PWM0_CH3 + select BSP_USING_PWM0_CH4 + select BSP_USING_PWM0_CH7 + select BSP_USING_PWM0_CH2_PORT11_COMPL + select BSP_USING_PWM0_CH3_PORT11 + select BSP_USING_PWM0_CH4_PORT5_COMPL + select BSP_USING_PWM0_CH7_PORT5_COMPL + select BSP_USING_HW_I2C + select BSP_USING_HW_I2C4 + select BSP_USING_SPI + select BSP_USING_SPI0 + imply RTDUINO_USING_SERVO + imply RTDUINO_USING_WIRE + imply RTDUINO_USING_SPI + default n + endmenu menu "On-chip Peripheral Drivers" @@ -65,41 +90,74 @@ menu "On-chip Peripheral Drivers" bool "Enable PWM0 channel0" default n if BSP_USING_PWM0_CH0 - config BSP_USING_PWM0_PORT0 - bool "Enable PWM0-PORT0 output pwm" + config BSP_USING_PWM0_CH0_PORT0 + bool "Enable PWM0-CH0-PORT0 output pwm" + default n + endif + + menuconfig BSP_USING_PWM0_CH2 + bool "Enable PWM0 channel2" + default n + if BSP_USING_PWM0_CH2 + config BSP_USING_PWM0_CH2_PORT11_COMPL + bool "Enable PWM0-CH2-PORT11-COMPL output pwm (Arduino PWM)" + default n + endif + + menuconfig BSP_USING_PWM0_CH3 + bool "Enable PWM0 channel3" + default n + if BSP_USING_PWM0_CH3 + config BSP_USING_PWM0_CH3_PORT11 + bool "Enable PWM0-CH3-PORT11 output pwm (Arduino PWM)" + default n + endif + + menuconfig BSP_USING_PWM0_CH4 + bool "Enable PWM0 channel4" + default n + if BSP_USING_PWM0_CH4 + config BSP_USING_PWM0_CH4_PORT5_COMPL + bool "Enable PWM0-CH4-PORT5-CPMPL output pwm (Arduino PWM)" default n endif + menuconfig BSP_USING_PWM0_CH7 bool "Enable PWM0 channel7" default n if BSP_USING_PWM0_CH7 - config BSP_USING_PWM0_PORT2 - bool "Enable PWM0-PORT2 output pwm" + config BSP_USING_PWM0_CH7_PORT2 + bool "Enable PWM0-CH7-PORT2 output pwm" default n endif if BSP_USING_PWM0_CH7 - config BSP_USING_PWM0_PORT5 - bool "Enable PWM0-PORT5 output pwm" + config BSP_USING_PWM0_CH7_PORT5 + bool "Enable PWM0-CH7-PORT5 output pwm" default n endif if BSP_USING_PWM0_CH7 - config BSP_USING_PWM0_PORT7 - bool "Enable PWM0-PORT7 output pwm" + config BSP_USING_PWM0_CH7_PORT5_COMPL + bool "Enable PWM0-CH7-PORT5-COMPL output pwm (Arduino PWM)" default n endif if BSP_USING_PWM0_CH7 - config BSP_USING_PWM0_PORT9 - bool "Enable PWM0-PORT9 output pwm" + config BSP_USING_PWM0_CH7_PORT7 + bool "Enable PWM0-CH7-PORT7 output pwm" default n endif if BSP_USING_PWM0_CH7 - config BSP_USING_PWM0_PORT10 - bool "Enable PWM0-PORT10 output pwm" + config BSP_USING_PWM0_CH7_PORT9 + bool "Enable PWM0-CH7-PORT9 output pwm" default n endif if BSP_USING_PWM0_CH7 - config BSP_USING_PWM0_PORT12 - bool "Enable PWM0-PORT12 output pwm" + config BSP_USING_PWM0_CH7_PORT10 + bool "Enable PWM0-CH7-PORT10 output pwm" + default n + endif + if BSP_USING_PWM0_CH7 + config BSP_USING_PWM0_CH7_PORT12 + bool "Enable PWM0-CH7-PORT12 output pwm" default n endif endif @@ -368,7 +426,7 @@ menu "Board extended module Drivers" select BSP_USING_PWM select BSP_USING_PWM0 select BSP_USING_PWM0_CH0 - select BSP_USING_PWM0_PORT0 + select BSP_USING_PWM0_CH0_PORT0 default n endif @@ -429,4 +487,4 @@ menu "Board extended module Drivers" default n endmenu -endmenu +endmenu \ No newline at end of file diff --git a/bsp/Infineon/psoc6-evaluationkit-062S2/libs/TARGET_RTT-062S2/config/GeneratedSource/cycfg_pins.c b/bsp/Infineon/psoc6-evaluationkit-062S2/libs/TARGET_RTT-062S2/config/GeneratedSource/cycfg_pins.c index e466749efd2..9d6249c5ebf 100644 --- a/bsp/Infineon/psoc6-evaluationkit-062S2/libs/TARGET_RTT-062S2/config/GeneratedSource/cycfg_pins.c +++ b/bsp/Infineon/psoc6-evaluationkit-062S2/libs/TARGET_RTT-062S2/config/GeneratedSource/cycfg_pins.c @@ -300,7 +300,7 @@ void init_cycfg_pins(void) Cy_GPIO_Pin_Init(CYBSP_SWDCK_PORT, CYBSP_SWDCK_PIN, &CYBSP_SWDCK_config); Cy_GPIO_Pin_Init(CYBSP_CINA_PORT, CYBSP_CINA_PIN, &CYBSP_CINA_config); Cy_GPIO_Pin_Init(CYBSP_CINB_PORT, CYBSP_CINB_PIN, &CYBSP_CINB_config); - Cy_GPIO_Pin_Init(CYBSP_CSD_BTN0_PORT, CYBSP_CSD_BTN0_PIN, &CYBSP_CSD_BTN0_config); + //Cy_GPIO_Pin_Init(CYBSP_CSD_BTN0_PORT, CYBSP_CSD_BTN0_PIN, &CYBSP_CSD_BTN0_config); } void reserve_cycfg_pins(void) @@ -312,7 +312,7 @@ void reserve_cycfg_pins(void) cyhal_hwmgr_reserve(&CYBSP_CINB_obj); cyhal_hwmgr_reserve(&CYBSP_LED_RGB_BLUE_obj); cyhal_hwmgr_reserve(&CYBSP_CMOD_obj); - cyhal_hwmgr_reserve(&CYBSP_CSD_BTN0_obj); + //cyhal_hwmgr_reserve(&CYBSP_CSD_BTN0_obj); cyhal_hwmgr_reserve(&CYBSP_A8_obj); cyhal_hwmgr_reserve(&CYBSP_A9_obj); cyhal_hwmgr_reserve(&CYBSP_A10_obj); From 2e954064c65580ada771c8bd59c9dd53d972eac7 Mon Sep 17 00:00:00 2001 From: LZerro Date: Mon, 15 Jun 2026 10:54:07 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8DDFS=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E5=A4=A7=E6=96=87=E4=BB=B6=E6=97=B6=E7=B4=A2=E5=BC=95=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/dfs/Kconfig | 10 +++ .../dfs_v1/filesystems/cromfs/dfs_cromfs.c | 2 +- .../dfs/dfs_v1/filesystems/elmfat/dfs_elm.c | 26 +++++- .../dfs/dfs_v1/filesystems/nfs/dfs_nfs.c | 2 +- .../dfs_v1/filesystems/skeleton/skeleton.c | 2 +- components/dfs/dfs_v1/include/dfs_file.h | 4 + components/dfs/dfs_v1/src/dfs_file.c | 48 ++++++++++- components/dfs/dfs_v1/src/dfs_posix.c | 2 +- .../dfs/dfs_v2/filesystems/elmfat/dfs_elm.c | 84 +++++++++++++++---- .../dfs/dfs_v2/filesystems/ramfs/dfs_ramfs.c | 2 +- .../dfs_v2/filesystems/skeleton/skeleton.c | 2 +- components/dfs/dfs_v2/include/dfs_file.h | 4 + components/dfs/dfs_v2/src/dfs_file.c | 52 +++++++++++- .../compilers/common/extension/sys/stat.h | 8 ++ .../compilers/common/extension/sys/types.h | 5 ++ 15 files changed, 226 insertions(+), 27 deletions(-) diff --git a/components/dfs/Kconfig b/components/dfs/Kconfig index 4d1418b8085..315671ff64b 100644 --- a/components/dfs/Kconfig +++ b/components/dfs/Kconfig @@ -16,6 +16,16 @@ if RT_USING_DFS bool "Using working directory" default y + config RT_USING_DFS_LARGE_FILE + bool "Enable large file support with 64-bit file offsets" + default n + select RT_KLIBC_USING_VSNPRINTF_LONGLONG if !RT_KLIBC_USING_LIBC_VSNPRINTF + help + Enable 64-bit signed off_t for DFS/POSIX file offsets and + file sizes. This is required to access FAT32 files larger + than 2GB on 32-bit platforms. Enabling this option changes + the ABI of file offset related APIs and structures. + if RT_USING_DFS_V1 config RT_USING_DFS_MNTTABLE bool "Using mount table for file system" diff --git a/components/dfs/dfs_v1/filesystems/cromfs/dfs_cromfs.c b/components/dfs/dfs_v1/filesystems/cromfs/dfs_cromfs.c index fee8b8dbd15..0a09a7a5adf 100644 --- a/components/dfs/dfs_v1/filesystems/cromfs/dfs_cromfs.c +++ b/components/dfs/dfs_v1/filesystems/cromfs/dfs_cromfs.c @@ -788,7 +788,7 @@ static int dfs_cromfs_read(struct dfs_file *file, void *buf, size_t count) return length; } -static int dfs_cromfs_lseek(struct dfs_file *file, off_t offset) +static off_t dfs_cromfs_lseek(struct dfs_file *file, off_t offset) { if (offset <= file->vnode->size) { diff --git a/components/dfs/dfs_v1/filesystems/elmfat/dfs_elm.c b/components/dfs/dfs_v1/filesystems/elmfat/dfs_elm.c index effd1bcd7c7..826c5c0e653 100644 --- a/components/dfs/dfs_v1/filesystems/elmfat/dfs_elm.c +++ b/components/dfs/dfs_v1/filesystems/elmfat/dfs_elm.c @@ -86,6 +86,18 @@ static int elm_result_to_dfs(FRESULT result) return status; } +static int dfs_elm_off_to_fsize(off_t offset, FSIZE_t *fsize) +{ + if (offset < 0) + return -EINVAL; + + if ((uint64_t)offset > (uint64_t)(FSIZE_t)-1) + return -EFBIG; + + *fsize = (FSIZE_t)offset; + return 0; +} + /* results: * -1, no space to install fatfs driver * >= 0, there is an space to install fatfs driver @@ -505,12 +517,16 @@ int dfs_elm_ioctl(struct dfs_file *file, int cmd, void *args) FIL *fd; FSIZE_t fptr, length; FRESULT result = FR_OK; + int ret; fd = (FIL *)(file->data); RT_ASSERT(fd != RT_NULL); /* save file read/write point */ fptr = fd->fptr; - length = *(off_t*)args; + ret = dfs_elm_off_to_fsize(*(off_t *)args, &length); + if (ret < 0) + return ret; + if (length <= fd->obj.objsize) { fd->fptr = length; @@ -597,12 +613,18 @@ off_t dfs_elm_lseek(struct dfs_file *file, off_t offset) if (file->vnode->type == FT_REGULAR) { FIL *fd; + FSIZE_t fsize; + int ret; /* regular file type */ fd = (FIL *)(file->data); RT_ASSERT(fd != RT_NULL); - result = f_lseek(fd, offset); + ret = dfs_elm_off_to_fsize(offset, &fsize); + if (ret < 0) + return ret; + + result = f_lseek(fd, fsize); if (result == FR_OK) { /* return current position */ diff --git a/components/dfs/dfs_v1/filesystems/nfs/dfs_nfs.c b/components/dfs/dfs_v1/filesystems/nfs/dfs_nfs.c index b24e6361845..815b45e4c74 100644 --- a/components/dfs/dfs_v1/filesystems/nfs/dfs_nfs.c +++ b/components/dfs/dfs_v1/filesystems/nfs/dfs_nfs.c @@ -685,7 +685,7 @@ int nfs_write(struct dfs_file *file, const void *buf, size_t count) return total; } -int nfs_lseek(struct dfs_file *file, off_t offset) +off_t nfs_lseek(struct dfs_file *file, off_t offset) { nfs_file *fd; nfs_filesystem *nfs; diff --git a/components/dfs/dfs_v1/filesystems/skeleton/skeleton.c b/components/dfs/dfs_v1/filesystems/skeleton/skeleton.c index 5c35cd1ffc3..d6feafa3ef8 100644 --- a/components/dfs/dfs_v1/filesystems/skeleton/skeleton.c +++ b/components/dfs/dfs_v1/filesystems/skeleton/skeleton.c @@ -34,7 +34,7 @@ int dfs_skt_read(struct dfs_file *file, void *buf, rt_size_t count) return count; } -int dfs_skt_lseek(struct dfs_file *file, rt_off_t offset) +off_t dfs_skt_lseek(struct dfs_file *file, off_t offset) { return -RT_EIO; } diff --git a/components/dfs/dfs_v1/include/dfs_file.h b/components/dfs/dfs_v1/include/dfs_file.h index 26c74c8f161..fc4d78365ee 100644 --- a/components/dfs/dfs_v1/include/dfs_file.h +++ b/components/dfs/dfs_v1/include/dfs_file.h @@ -50,7 +50,11 @@ struct dfs_vnode const struct dfs_file_ops *fops; uint32_t flags; /* self flags, is dir etc.. */ +#ifdef RT_USING_DFS_LARGE_FILE + off_t size; /* Size in bytes */ +#else size_t size; /* Size in bytes */ +#endif void *data; /* Specific file system data */ }; diff --git a/components/dfs/dfs_v1/src/dfs_file.c b/components/dfs/dfs_v1/src/dfs_file.c index f1c4e7ee4d2..33efc94bb00 100644 --- a/components/dfs/dfs_v1/src/dfs_file.c +++ b/components/dfs/dfs_v1/src/dfs_file.c @@ -580,7 +580,7 @@ int dfs_file_flush(struct dfs_file *fd) */ off_t dfs_file_lseek(struct dfs_file *fd, off_t offset) { - int result; + off_t result; if (fd == NULL) return -EINVAL; @@ -783,6 +783,48 @@ int dfs_file_mmap2(struct dfs_file *fd, struct dfs_mmap2_args *mmap2) #ifdef RT_USING_FINSH #include +#ifdef RT_USING_DFS_LARGE_FILE +static void dfs_print_file_size(off_t size) +{ + char tmp[21]; + char buf[22]; + int index = 0; + int out = 0; + uint64_t value; + + if (size < 0) + { + value = (uint64_t)(-(size + 1)) + 1; + buf[out++] = '-'; + } + else + { + value = (uint64_t)size; + } + + if (value == 0) + { + tmp[index++] = '0'; + } + else + { + while ((value != 0) && (index < (int)sizeof(tmp))) + { + tmp[index++] = (char)('0' + (value % 10)); + value /= 10; + } + } + + while ((index > 0) && (out + 1 < (int)sizeof(buf))) + { + buf[out++] = tmp[--index]; + } + buf[out] = '\0'; + + rt_kprintf(" %-25s\n", buf); +} +#endif + void ls(const char *pathname) { struct dfs_file fd; @@ -835,7 +877,11 @@ void ls(const char *pathname) } else { +#ifdef RT_USING_DFS_LARGE_FILE + dfs_print_file_size(stat.st_size); +#else rt_kprintf(" %-25lu\n", (unsigned long)stat.st_size); +#endif } } else diff --git a/components/dfs/dfs_v1/src/dfs_posix.c b/components/dfs/dfs_v1/src/dfs_posix.c index f3d7b70c318..4350deaa1e4 100644 --- a/components/dfs/dfs_v1/src/dfs_posix.c +++ b/components/dfs/dfs_v1/src/dfs_posix.c @@ -286,7 +286,7 @@ RTM_EXPORT(write); */ off_t lseek(int fd, off_t offset, int whence) { - int result; + off_t result; struct dfs_file *d; d = fd_get(fd); diff --git a/components/dfs/dfs_v2/filesystems/elmfat/dfs_elm.c b/components/dfs/dfs_v2/filesystems/elmfat/dfs_elm.c index 90b87a178b4..b1f671e445d 100644 --- a/components/dfs/dfs_v2/filesystems/elmfat/dfs_elm.c +++ b/components/dfs/dfs_v2/filesystems/elmfat/dfs_elm.c @@ -109,6 +109,18 @@ static int elm_result_to_dfs(FRESULT result) return status; } +static int dfs_elm_off_to_fsize(off_t offset, FSIZE_t *fsize) +{ + if (offset < 0) + return -EINVAL; + + if ((uint64_t)offset > (uint64_t)(FSIZE_t)-1) + return -EFBIG; + + *fsize = (FSIZE_t)offset; + return 0; +} + /* results: * -1, no space to install fatfs driver * >= 0, there is an space to install fatfs driver @@ -549,7 +561,7 @@ int dfs_elm_ioctl(struct dfs_file *file, int cmd, void *args) { case RT_FIOFTRUNCATE: { - off_t offset = (off_t)(size_t)(args); + off_t offset = *(off_t *)args; return dfs_elm_truncate(file, offset); } case F_GETLK: @@ -565,6 +577,8 @@ ssize_t dfs_elm_read(struct dfs_file *file, void *buf, size_t len, off_t *pos) FIL *fd; FRESULT result = FR_OK; UINT byte_read; + FSIZE_t fsize; + int ret; if (file->vnode->type == FT_DIRECTORY) { @@ -573,13 +587,20 @@ ssize_t dfs_elm_read(struct dfs_file *file, void *buf, size_t len, off_t *pos) if (file->vnode->size > *pos) { + ret = dfs_elm_off_to_fsize(*pos, &fsize); + if (ret < 0) + return ret; + fd = (FIL *)(file->vnode->data); RT_ASSERT(fd != RT_NULL); rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER); - f_lseek(fd, *pos); - result = f_read(fd, buf, len, &byte_read); - /* update position */ - *pos = fd->fptr; + result = f_lseek(fd, fsize); + if (result == FR_OK) + { + result = f_read(fd, buf, len, &byte_read); + /* update position */ + *pos = fd->fptr; + } rt_mutex_release(&file->vnode->lock); if (result == FR_OK) return byte_read; @@ -593,6 +614,8 @@ ssize_t dfs_elm_write(struct dfs_file *file, const void *buf, size_t len, off_t FIL *fd; FRESULT result; UINT byte_write; + FSIZE_t fsize; + int ret; if (file->vnode->type == FT_DIRECTORY) { @@ -601,12 +624,19 @@ ssize_t dfs_elm_write(struct dfs_file *file, const void *buf, size_t len, off_t fd = (FIL *)(file->vnode->data); RT_ASSERT(fd != RT_NULL); + ret = dfs_elm_off_to_fsize(*pos, &fsize); + if (ret < 0) + return ret; + rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER); - f_lseek(fd, *pos); - result = f_write(fd, buf, len, &byte_write); - /* update position and file size */ - *pos = fd->fptr; - file->vnode->size = f_size(fd); + result = f_lseek(fd, fsize); + if (result == FR_OK) + { + result = f_write(fd, buf, len, &byte_write); + /* update position and file size */ + *pos = fd->fptr; + file->vnode->size = f_size(fd); + } rt_mutex_release(&file->vnode->lock); if (result == FR_OK) return byte_write; @@ -650,12 +680,18 @@ off_t dfs_elm_lseek(struct dfs_file *file, off_t offset, int wherece) if (file->vnode->type == FT_REGULAR) { FIL *fd; + FSIZE_t fsize; + int ret; /* regular file type */ fd = (FIL *)(file->vnode->data); RT_ASSERT(fd != RT_NULL); + ret = dfs_elm_off_to_fsize(offset, &fsize); + if (ret < 0) + return ret; + rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER); - result = f_lseek(fd, offset); + result = f_lseek(fd, fsize); pos = fd->fptr; rt_mutex_release(&file->vnode->lock); if (result == FR_OK) @@ -687,21 +723,26 @@ off_t dfs_elm_lseek(struct dfs_file *file, off_t offset, int wherece) static int dfs_elm_truncate(struct dfs_file *file, off_t offset) { FIL *fd; - FSIZE_t fptr; + FSIZE_t fptr, fsize; FRESULT result = FR_OK; + int ret; fd = (FIL *)(file->vnode->data); RT_ASSERT(fd != RT_NULL); + ret = dfs_elm_off_to_fsize(offset, &fsize); + if (ret < 0) + return ret; + /* save file read/write point */ fptr = fd->fptr; - if (offset <= fd->obj.objsize) + if (fsize <= fd->obj.objsize) { - fd->fptr = offset; + fd->fptr = fsize; result = f_truncate(fd); } else { - result = f_lseek(fd, offset); + result = f_lseek(fd, fsize); } /* restore file read/write point */ fd->fptr = fptr; @@ -1042,6 +1083,8 @@ ssize_t dfs_elm_page_write(struct dfs_page *page) FIL *fd; FRESULT result; UINT byte_write; + FSIZE_t fsize; + int ret; if (page->aspace->vnode->type == FT_DIRECTORY) { @@ -1050,9 +1093,16 @@ ssize_t dfs_elm_page_write(struct dfs_page *page) fd = (FIL *)(page->aspace->vnode->data); RT_ASSERT(fd != RT_NULL); + ret = dfs_elm_off_to_fsize(page->fpos, &fsize); + if (ret < 0) + return ret; + rt_mutex_take(&page->aspace->vnode->lock, RT_WAITING_FOREVER); - f_lseek(fd, page->fpos); - result = f_write(fd, page->page, page->len, &byte_write); + result = f_lseek(fd, fsize); + if (result == FR_OK) + { + result = f_write(fd, page->page, page->len, &byte_write); + } rt_mutex_release(&page->aspace->vnode->lock); if (result == FR_OK) { diff --git a/components/dfs/dfs_v2/filesystems/ramfs/dfs_ramfs.c b/components/dfs/dfs_v2/filesystems/ramfs/dfs_ramfs.c index fd7326bd018..f90b955baa9 100644 --- a/components/dfs/dfs_v2/filesystems/ramfs/dfs_ramfs.c +++ b/components/dfs/dfs_v2/filesystems/ramfs/dfs_ramfs.c @@ -151,7 +151,7 @@ int dfs_ramfs_write(struct dfs_file *fd, const void *buf, size_t count) return count; } -int dfs_ramfs_lseek(struct dfs_file *file, off_t offset) +off_t dfs_ramfs_lseek(struct dfs_file *file, off_t offset) { if (offset <= (off_t)file->vnode->size) { diff --git a/components/dfs/dfs_v2/filesystems/skeleton/skeleton.c b/components/dfs/dfs_v2/filesystems/skeleton/skeleton.c index af2c871dfc3..a048ae0caf1 100644 --- a/components/dfs/dfs_v2/filesystems/skeleton/skeleton.c +++ b/components/dfs/dfs_v2/filesystems/skeleton/skeleton.c @@ -34,7 +34,7 @@ int dfs_skt_read(struct dfs_file *file, void *buf, rt_size_t count) return count; } -int dfs_skt_lseek(struct dfs_file *file, rt_off_t offset) +off_t dfs_skt_lseek(struct dfs_file *file, off_t offset) { return -RT_EIO; } diff --git a/components/dfs/dfs_v2/include/dfs_file.h b/components/dfs/dfs_v2/include/dfs_file.h index 5010ed45346..5d41f1fa8e1 100644 --- a/components/dfs/dfs_v2/include/dfs_file.h +++ b/components/dfs/dfs_v2/include/dfs_file.h @@ -63,7 +63,11 @@ struct dfs_vnode struct dfs_mnt *mnt; /* which mounted file system does this vnode belong to */ +#ifdef RT_USING_DFS_LARGE_FILE + off_t size; +#else size_t size; +#endif uint32_t nlink; const struct dfs_file_ops *fops; diff --git a/components/dfs/dfs_v2/src/dfs_file.c b/components/dfs/dfs_v2/src/dfs_file.c index 9c6e383e1b4..09600f170c4 100644 --- a/components/dfs/dfs_v2/src/dfs_file.c +++ b/components/dfs/dfs_v2/src/dfs_file.c @@ -2521,6 +2521,48 @@ int dfs_file_mmap2(struct dfs_file *file, struct dfs_mmap2_args *mmap2) #define _COLOR_WHITE "\033[37m" #define _COLOR_NORMAL "\033[0m" +#ifdef RT_USING_DFS_LARGE_FILE +static void dfs_print_file_size(off_t size) +{ + char tmp[21]; + char buf[22]; + int index = 0; + int out = 0; + uint64_t value; + + if (size < 0) + { + value = (uint64_t)(-(size + 1)) + 1; + buf[out++] = '-'; + } + else + { + value = (uint64_t)size; + } + + if (value == 0) + { + tmp[index++] = '0'; + } + else + { + while ((value != 0) && (index < (int)sizeof(tmp))) + { + tmp[index++] = (char)('0' + (value % 10)); + value /= 10; + } + } + + while ((index > 0) && (out + 1 < (int)sizeof(buf))) + { + buf[out++] = tmp[--index]; + } + buf[out] = '\0'; + + rt_kprintf(" %-25s\n", buf); +} +#endif + /** * @brief List directory contents with colored output * @@ -2655,7 +2697,11 @@ void ls(const char *pathname) else if (stat.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { rt_kprintf(_COLOR_GREEN "%-20s" _COLOR_NORMAL, dirent.d_name); +#ifdef RT_USING_DFS_LARGE_FILE + dfs_print_file_size(stat.st_size); +#else rt_kprintf(" %-25lu\n", (unsigned long)stat.st_size); +#endif } else if (S_ISCHR(stat.st_mode)) { @@ -2665,7 +2711,11 @@ void ls(const char *pathname) else { rt_kprintf("%-20s", dirent.d_name); +#ifdef RT_USING_DFS_LARGE_FILE + dfs_print_file_size(stat.st_size); +#else rt_kprintf(" %-25lu\n", (unsigned long)stat.st_size); +#endif } } else @@ -3029,4 +3079,4 @@ void copy(const char *src, const char *dst) } FINSH_FUNCTION_EXPORT(copy, copy file or dir) -#endif \ No newline at end of file +#endif diff --git a/components/libc/compilers/common/extension/sys/stat.h b/components/libc/compilers/common/extension/sys/stat.h index e4b2da764c2..9d9ad2f0669 100644 --- a/components/libc/compilers/common/extension/sys/stat.h +++ b/components/libc/compilers/common/extension/sys/stat.h @@ -62,7 +62,11 @@ struct stat uint16_t st_uid; uint16_t st_gid; struct rt_device *st_rdev; +#ifdef RT_USING_DFS_LARGE_FILE + off_t st_size; +#else uint32_t st_size; +#endif time_t st_atime; long st_spare1; time_t st_mtime; @@ -70,7 +74,11 @@ struct stat time_t st_ctime; long st_spare3; uint32_t st_blksize; +#ifdef RT_USING_DFS_LARGE_FILE + uint64_t st_blocks; +#else uint32_t st_blocks; +#endif long st_spare4[2]; }; diff --git a/components/libc/compilers/common/extension/sys/types.h b/components/libc/compilers/common/extension/sys/types.h index cae4d4adcd6..457bb98e120 100644 --- a/components/libc/compilers/common/extension/sys/types.h +++ b/components/libc/compilers/common/extension/sys/types.h @@ -12,6 +12,7 @@ #ifndef __SYS_TYPES_H__ #define __SYS_TYPES_H__ +#include #include #include #include @@ -25,7 +26,11 @@ typedef int32_t key_t; /* Used for interprocess communication. typedef int pid_t; /* Used for process IDs and process group IDs. */ typedef unsigned short uid_t; typedef unsigned short gid_t; +#ifdef RT_USING_DFS_LARGE_FILE +typedef int64_t off_t; +#else typedef signed long off_t; +#endif typedef int mode_t; typedef signed long ssize_t; /* Used for a count of bytes or an error indication. */ typedef unsigned long __timer_t;