From 01a65632493d6deeaf589b07358da8c9603890ea Mon Sep 17 00:00:00 2001 From: "Man, Jianting (Meco)" <920369182@qq.com> Date: Sun, 12 Dec 2021 22:36:51 -0500 Subject: [PATCH] =?UTF-8?q?[stm32l475][arduino]=20=E5=88=9D=E6=AD=A5?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0Arduino=E7=94=9F=E6=80=81=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=20(#5367)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [arduino] 初步实现Arduino生态兼容组件 * [stm32f475] 增加PC2 作为ADC1_IN3 * [stm32l475][port] 将路径Arduino改为arduino --- .../applications/SConscript | 9 +- .../applications/arduino_main.cpp | 23 ++++ .../applications/nrf24l01_init.c | 2 +- .../board/.ignore_format.yml | 6 + .../board/CubeMX_Config/STM32L475VE.ioc | 105 +++++++++--------- .../board/CubeMX_Config/Src/main.c | 38 ++++--- .../CubeMX_Config/Src/stm32l4xx_hal_msp.c | 6 +- bsp/stm32/stm32l475-atk-pandora/board/Kconfig | 13 +++ .../board/ports/arduino/SConscript | 12 ++ .../board/ports/arduino/arduino_layout.c | 47 ++++++++ .../board/ports/arduino/arduino_layout.h | 29 +++++ 11 files changed, 218 insertions(+), 72 deletions(-) create mode 100644 bsp/stm32/stm32l475-atk-pandora/applications/arduino_main.cpp create mode 100644 bsp/stm32/stm32l475-atk-pandora/board/.ignore_format.yml create mode 100644 bsp/stm32/stm32l475-atk-pandora/board/ports/arduino/SConscript create mode 100644 bsp/stm32/stm32l475-atk-pandora/board/ports/arduino/arduino_layout.c create mode 100644 bsp/stm32/stm32l475-atk-pandora/board/ports/arduino/arduino_layout.h diff --git a/bsp/stm32/stm32l475-atk-pandora/applications/SConscript b/bsp/stm32/stm32l475-atk-pandora/applications/SConscript index 5335ab9bb..bfa9a40db 100644 --- a/bsp/stm32/stm32l475-atk-pandora/applications/SConscript +++ b/bsp/stm32/stm32l475-atk-pandora/applications/SConscript @@ -3,12 +3,13 @@ import os cwd = GetCurrentDir() -src = Split(''' -main.c -''') +src = ['main.c'] if GetDepend(['PKG_USING_NRF24L01']): - src += Glob('nrf24l01_init.c') + src += ['nrf24l01_init.c'] + +if GetDepend(['RT_USING_ARDUINO']): + src += ['arduino_main.cpp'] CPPPATH = [cwd] diff --git a/bsp/stm32/stm32l475-atk-pandora/applications/arduino_main.cpp b/bsp/stm32/stm32l475-atk-pandora/applications/arduino_main.cpp new file mode 100644 index 000000000..455e8b849 --- /dev/null +++ b/bsp/stm32/stm32l475-atk-pandora/applications/arduino_main.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-12-10 Meco Man first version + */ +#include + +void setup(void) +{ + // put your setup code here, to run once: + pinMode(LED_BUILTIN, OUTPUT); +} + +void loop(void) +{ + // put your main code here, to run repeatedly: + digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); + delay(200); +} diff --git a/bsp/stm32/stm32l475-atk-pandora/applications/nrf24l01_init.c b/bsp/stm32/stm32l475-atk-pandora/applications/nrf24l01_init.c index 59c27ee55..e29115423 100644 --- a/bsp/stm32/stm32l475-atk-pandora/applications/nrf24l01_init.c +++ b/bsp/stm32/stm32l475-atk-pandora/applications/nrf24l01_init.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * diff --git a/bsp/stm32/stm32l475-atk-pandora/board/.ignore_format.yml b/bsp/stm32/stm32l475-atk-pandora/board/.ignore_format.yml new file mode 100644 index 000000000..0d7f3e360 --- /dev/null +++ b/bsp/stm32/stm32l475-atk-pandora/board/.ignore_format.yml @@ -0,0 +1,6 @@ +# files format check exclude path, please follow the instructions below to modify; +# If you need to exclude an entire folder, add the folder path in dir_path; +# If you need to exclude a file, add the path to the file in file_path. + +dir_path: +- CubeMX_Config diff --git a/bsp/stm32/stm32l475-atk-pandora/board/CubeMX_Config/STM32L475VE.ioc b/bsp/stm32/stm32l475-atk-pandora/board/CubeMX_Config/STM32L475VE.ioc index e7711fa51..f9229d26f 100644 --- a/bsp/stm32/stm32l475-atk-pandora/board/CubeMX_Config/STM32L475VE.ioc +++ b/bsp/stm32/stm32l475-atk-pandora/board/CubeMX_Config/STM32L475VE.ioc @@ -37,66 +37,67 @@ Mcu.Name=STM32L475V(C-E-G)Tx Mcu.Package=LQFP100 Mcu.Pin0=PE2 Mcu.Pin1=PE3 -Mcu.Pin10=PA3 -Mcu.Pin11=PA4 -Mcu.Pin12=PA5 -Mcu.Pin13=PA6 -Mcu.Pin14=PA7 -Mcu.Pin15=PC5 -Mcu.Pin16=PE9 -Mcu.Pin17=PE10 -Mcu.Pin18=PE11 -Mcu.Pin19=PE12 +Mcu.Pin10=PA2 +Mcu.Pin11=PA3 +Mcu.Pin12=PA4 +Mcu.Pin13=PA5 +Mcu.Pin14=PA6 +Mcu.Pin15=PA7 +Mcu.Pin16=PC5 +Mcu.Pin17=PE9 +Mcu.Pin18=PE10 +Mcu.Pin19=PE11 Mcu.Pin2=PE4 -Mcu.Pin20=PE13 -Mcu.Pin21=PE14 -Mcu.Pin22=PE15 -Mcu.Pin23=PB10 -Mcu.Pin24=PB11 -Mcu.Pin25=PB13 -Mcu.Pin26=PB14 -Mcu.Pin27=PB15 -Mcu.Pin28=PC8 -Mcu.Pin29=PC9 +Mcu.Pin20=PE12 +Mcu.Pin21=PE13 +Mcu.Pin22=PE14 +Mcu.Pin23=PE15 +Mcu.Pin24=PB10 +Mcu.Pin25=PB11 +Mcu.Pin26=PB13 +Mcu.Pin27=PB14 +Mcu.Pin28=PB15 +Mcu.Pin29=PC8 Mcu.Pin3=PE5 -Mcu.Pin30=PA9 -Mcu.Pin31=PA10 -Mcu.Pin32=PA11 -Mcu.Pin33=PA12 -Mcu.Pin34=PA13 (JTMS-SWDIO) -Mcu.Pin35=PA14 (JTCK-SWCLK) -Mcu.Pin36=PC10 -Mcu.Pin37=PC11 -Mcu.Pin38=PC12 -Mcu.Pin39=PD2 +Mcu.Pin30=PC9 +Mcu.Pin31=PA9 +Mcu.Pin32=PA10 +Mcu.Pin33=PA11 +Mcu.Pin34=PA12 +Mcu.Pin35=PA13 (JTMS-SWDIO) +Mcu.Pin36=PA14 (JTCK-SWCLK) +Mcu.Pin37=PC10 +Mcu.Pin38=PC11 +Mcu.Pin39=PC12 Mcu.Pin4=PE6 -Mcu.Pin40=PB3 (JTDO-TRACESWO) -Mcu.Pin41=PB5 -Mcu.Pin42=PB7 -Mcu.Pin43=PB8 -Mcu.Pin44=VP_IWDG_VS_IWDG -Mcu.Pin45=VP_LPTIM1_VS_LPTIM_counterModeInternalClock -Mcu.Pin46=VP_RTC_VS_RTC_Activate -Mcu.Pin47=VP_SAI1_VP_$IpInstance_SAIA_SAI_BASIC -Mcu.Pin48=VP_SAI1_VP_$IpInstance_SAIB_SAI_BASIC -Mcu.Pin49=VP_SYS_VS_Systick +Mcu.Pin40=PD2 +Mcu.Pin41=PB3 (JTDO-TRACESWO) +Mcu.Pin42=PB5 +Mcu.Pin43=PB7 +Mcu.Pin44=PB8 +Mcu.Pin45=VP_IWDG_VS_IWDG +Mcu.Pin46=VP_LPTIM1_VS_LPTIM_counterModeInternalClock +Mcu.Pin47=VP_RTC_VS_RTC_Activate +Mcu.Pin48=VP_SAI1_VP_$IpInstance_SAIA_SAI_BASIC +Mcu.Pin49=VP_SAI1_VP_$IpInstance_SAIB_SAI_BASIC Mcu.Pin5=PC14-OSC32_IN (PC14) -Mcu.Pin50=VP_TIM1_VS_ClockSourceINT -Mcu.Pin51=VP_TIM2_VS_ClockSourceINT -Mcu.Pin52=VP_TIM4_VS_ClockSourceINT -Mcu.Pin53=VP_TIM15_VS_ClockSourceINT -Mcu.Pin54=VP_TIM16_VS_ClockSourceINT -Mcu.Pin55=VP_TIM17_VS_ClockSourceINT +Mcu.Pin50=VP_SYS_VS_Systick +Mcu.Pin51=VP_TIM1_VS_ClockSourceINT +Mcu.Pin52=VP_TIM2_VS_ClockSourceINT +Mcu.Pin53=VP_TIM4_VS_ClockSourceINT +Mcu.Pin54=VP_TIM15_VS_ClockSourceINT +Mcu.Pin55=VP_TIM16_VS_ClockSourceINT +Mcu.Pin56=VP_TIM17_VS_ClockSourceINT Mcu.Pin6=PC15-OSC32_OUT (PC15) Mcu.Pin7=PH0-OSC_IN (PH0) Mcu.Pin8=PH1-OSC_OUT (PH1) -Mcu.Pin9=PA2 -Mcu.PinsNb=56 +Mcu.Pin9=PC2 +Mcu.PinsNb=57 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32L475VETx -MxCube.Version=6.2.1 -MxDb.Version=DB.6.0.21 +MxCube.Version=6.4.0 +MxDb.Version=DB.6.0.40 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false NVIC.ForceEnableDMAVector=true @@ -171,6 +172,8 @@ PC14-OSC32_IN\ (PC14).Mode=LSE-External-Oscillator PC14-OSC32_IN\ (PC14).Signal=RCC_OSC32_IN PC15-OSC32_OUT\ (PC15).Mode=LSE-External-Oscillator PC15-OSC32_OUT\ (PC15).Signal=RCC_OSC32_OUT +PC2.Locked=true +PC2.Signal=ADCx_IN3 PC5.Signal=ADCx_IN14 PC8.Mode=SD_4_bits_Wide_bus PC8.Signal=SDMMC1_D0 @@ -309,6 +312,8 @@ SAI1.VirtualProtocol-SAI_A_BASIC=VM_BASIC_PROTOCOL SAI1.VirtualProtocol-SAI_B_BASIC=VM_BASIC_PROTOCOL SH.ADCx_IN14.0=ADC1_IN14,IN14-Single-Ended SH.ADCx_IN14.ConfNb=1 +SH.ADCx_IN3.0=ADC1_IN3,IN3-Single-Ended +SH.ADCx_IN3.ConfNb=1 SH.COMP_DAC11_group.0=DAC1_OUT1,DAC_OUT1 SH.COMP_DAC11_group.ConfNb=1 SH.S_TIM1_CH1.0=TIM1_CH1,PWM Generation1 CH1 diff --git a/bsp/stm32/stm32l475-atk-pandora/board/CubeMX_Config/Src/main.c b/bsp/stm32/stm32l475-atk-pandora/board/CubeMX_Config/Src/main.c index 2a9dcd82a..9c8957add 100644 --- a/bsp/stm32/stm32l475-atk-pandora/board/CubeMX_Config/Src/main.c +++ b/bsp/stm32/stm32l475-atk-pandora/board/CubeMX_Config/Src/main.c @@ -101,6 +101,7 @@ PCD_HandleTypeDef hpcd_USB_OTG_FS; /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); +void PeriphCommonClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART1_UART_Init(void); static void MX_USART2_UART_Init(void); @@ -154,6 +155,9 @@ int main(void) /* Configure the system clock */ SystemClock_Config(); +/* Configure the peripherals common clocks */ + PeriphCommonClock_Config(); + /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ @@ -205,8 +209,13 @@ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; - RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; + /** Configure the main internal regulator output voltage + */ + if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK) + { + Error_Handler(); + } /** Configure LSE Drive Capability */ HAL_PWR_EnableBkUpAccess(); @@ -243,16 +252,22 @@ void SystemClock_Config(void) { Error_Handler(); } - PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USART1 - |RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_LPTIM1 - |RCC_PERIPHCLK_SAI1|RCC_PERIPHCLK_USB +} + +/** + * @brief Peripherals Common Clock Configuration + * @retval None + */ +void PeriphCommonClock_Config(void) +{ + RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; + + /** Initializes the peripherals clock + */ + PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_SAI1|RCC_PERIPHCLK_USB |RCC_PERIPHCLK_SDMMC1|RCC_PERIPHCLK_ADC; - PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2; - PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1; - PeriphClkInit.Lptim1ClockSelection = RCC_LPTIM1CLKSOURCE_PCLK; PeriphClkInit.Sai1ClockSelection = RCC_SAI1CLKSOURCE_PLLSAI1; PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI1; - PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1; PeriphClkInit.Sdmmc1ClockSelection = RCC_SDMMC1CLKSOURCE_PLLSAI1; PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_HSE; @@ -267,12 +282,6 @@ void SystemClock_Config(void) { Error_Handler(); } - /** Configure the main internal regulator output voltage - */ - if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK) - { - Error_Handler(); - } } /** @@ -1190,4 +1199,3 @@ void assert_failed(uint8_t *file, uint32_t line) } #endif /* USE_FULL_ASSERT */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/bsp/stm32/stm32l475-atk-pandora/board/CubeMX_Config/Src/stm32l4xx_hal_msp.c b/bsp/stm32/stm32l475-atk-pandora/board/CubeMX_Config/Src/stm32l4xx_hal_msp.c index 77fb0eecc..12993e351 100644 --- a/bsp/stm32/stm32l475-atk-pandora/board/CubeMX_Config/Src/stm32l4xx_hal_msp.c +++ b/bsp/stm32/stm32l475-atk-pandora/board/CubeMX_Config/Src/stm32l4xx_hal_msp.c @@ -118,9 +118,10 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) __HAL_RCC_GPIOC_CLK_ENABLE(); /**ADC1 GPIO Configuration + PC2 ------> ADC1_IN3 PC5 ------> ADC1_IN14 */ - GPIO_InitStruct.Pin = GPIO_PIN_5; + GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); @@ -149,9 +150,10 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc) __HAL_RCC_ADC_CLK_DISABLE(); /**ADC1 GPIO Configuration + PC2 ------> ADC1_IN3 PC5 ------> ADC1_IN14 */ - HAL_GPIO_DeInit(GPIOC, GPIO_PIN_5); + HAL_GPIO_DeInit(GPIOC, GPIO_PIN_2|GPIO_PIN_5); /* USER CODE BEGIN ADC1_MspDeInit 1 */ diff --git a/bsp/stm32/stm32l475-atk-pandora/board/Kconfig b/bsp/stm32/stm32l475-atk-pandora/board/Kconfig index 0a9f253d5..67571d48f 100644 --- a/bsp/stm32/stm32l475-atk-pandora/board/Kconfig +++ b/bsp/stm32/stm32l475-atk-pandora/board/Kconfig @@ -15,6 +15,19 @@ menu "Onboard Peripheral Drivers" select BSP_USING_UART1 default y + config BSP_USING_ARDUINO_UNO + bool "Support Arduino UNO" + select RT_USING_ARDUINO + select RT_USING_ARDUINO_UNO_LAYOUT + select BSP_USING_PWM + select BSP_USING_PWM1 + select BSP_USING_PWM1_CH1 + select BSP_USING_PWM4 + select BSP_USING_PWM4_CH3 + select BSP_USING_ADC + select BSP_USING_ADC1 + default n + config BSP_USING_KEY bool "Enable onboard keys" select RT_USING_PIN diff --git a/bsp/stm32/stm32l475-atk-pandora/board/ports/arduino/SConscript b/bsp/stm32/stm32l475-atk-pandora/board/ports/arduino/SConscript new file mode 100644 index 000000000..f8f52e280 --- /dev/null +++ b/bsp/stm32/stm32l475-atk-pandora/board/ports/arduino/SConscript @@ -0,0 +1,12 @@ +from building import * + +cwd = GetCurrentDir() +src = [] +inc = [cwd] + +if GetDepend('RT_USING_ARDUINO_UNO_LAYOUT'): + src += ['arduino_layout.c'] + +group = DefineGroup('Arduino', src, depend = ['RT_USING_ARDUINO'], CPPPATH = inc) + +Return('group') diff --git a/bsp/stm32/stm32l475-atk-pandora/board/ports/arduino/arduino_layout.c b/bsp/stm32/stm32l475-atk-pandora/board/ports/arduino/arduino_layout.c new file mode 100644 index 000000000..dce998812 --- /dev/null +++ b/bsp/stm32/stm32l475-atk-pandora/board/ports/arduino/arduino_layout.c @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-12-10 Meco Man first version + */ +#include +#include + +const pin_map_t pin_map_table[ARDUINO_LAYOUT_PIN_MAX]= +{ + /* + {Arduino Pin, RT-Thread Pin [, Device Name(PWM or ADC), Channel]} + [] means optional + Digital pins must NOT give the device name and channel. + Analog pins MUST give the device name and channel(ADC or PWM). + */ + {0}, /* D0, RX */ + {1}, /* D1, TX */ + {2, GET_PIN(D,10)}, /* D2, BSP:KEY0 */ + {3}, /* D3, PWM */ + {4, GET_PIN(D,9)}, /* D4, BSP:KEY1 */ + {5, GET_PIN(D,8)}, /* D5, BSP:KEY2 */ + {6}, /* D6, PWM */ + {7}, /* D7 */ + {8, GET_PIN(E,7)}, /* D8, BSP: RED-LED */ + {9}, /* D9, PWM */ + {10, GET_PIN(E,9), "pwm1", 1}, /* D10, PWM, BSP: BLUE-LED */ + {11, GET_PIN(B,8), "pwm4", 3}, /* D11, PWM */ + {12, GET_PIN(B,2)}, /* D12, BSP: BEEP */ + {13, GET_PIN(E,8)}, /* D13, LED_BUILTIN, BSP: GREEN-LED */ + {14, GET_PIN(C,2), "adc1", 3}, /* D14, A0 */ + {15}, /* D15, A1 */ + {16}, /* D16, A2 */ + {17}, /* D17, A3 */ + {18}, /* D18, A4 */ + {19} /* D19, A5 */ +}; + +/* initialization for BSP; maybe a blank function */ +void initVariant(void) +{ + /* Nothing needs to initialize for this BSP */ +} diff --git a/bsp/stm32/stm32l475-atk-pandora/board/ports/arduino/arduino_layout.h b/bsp/stm32/stm32l475-atk-pandora/board/ports/arduino/arduino_layout.h new file mode 100644 index 000000000..f5a1b8f95 --- /dev/null +++ b/bsp/stm32/stm32l475-atk-pandora/board/ports/arduino/arduino_layout.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-12-10 Meco Man first version + */ +#ifndef __UNO_LAYOUT_H__ +#define __UNO_LAYOUT_H__ + +#include + +#define LED_BUILTIN 13 /* Built-in LED */ + +#define ARDUINO_PWM_HZ 500 /* Arduino UNO's PWM is around 500Hz */ +#define ARDUINO_LAYOUT_PIN_MAX 20 + +#define A0 (14) +#define A1 (15) +#define A2 (16) +#define A3 (17) +#define A4 (18) +#define A5 (19) +#define A6 (20) +#define A7 (21) + +#endif -- GitLab