From 8956b2ea461e0333b9c2251e6e72a826589d5cfd Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Sat, 17 Apr 2021 16:28:09 +0800 Subject: [stm32 pandora] implement drv_key and modify drv_sdcard --- bsp/stm32/stm32l475-atk-pandora/board/Kconfig | 7 ++ bsp/stm32/stm32l475-atk-pandora/board/SConscript | 5 +- .../stm32l475-atk-pandora/board/ports/drv_key.c | 122 +++++++++++++++++++++ .../stm32l475-atk-pandora/board/ports/drv_sdcard.c | 81 ++++++++++++++ .../board/ports/sdcard_port.c | 81 -------------- components/drivers/misc/pin.c | 2 +- 6 files changed, 215 insertions(+), 83 deletions(-) create mode 100644 bsp/stm32/stm32l475-atk-pandora/board/ports/drv_key.c create mode 100644 bsp/stm32/stm32l475-atk-pandora/board/ports/drv_sdcard.c delete mode 100644 bsp/stm32/stm32l475-atk-pandora/board/ports/sdcard_port.c diff --git a/bsp/stm32/stm32l475-atk-pandora/board/Kconfig b/bsp/stm32/stm32l475-atk-pandora/board/Kconfig index 5a11d21cc1..bb34e37366 100644 --- a/bsp/stm32/stm32l475-atk-pandora/board/Kconfig +++ b/bsp/stm32/stm32l475-atk-pandora/board/Kconfig @@ -15,6 +15,13 @@ menu "Onboard Peripheral Drivers" select BSP_USING_UART1 default y + config BSP_USING_KEY + bool "Enable onboard keys" + select RT_USING_PIN + select RT_USING_TIMER_SOFT + select PKG_USING_MULTIBUTTON + default n + config BSP_USING_QSPI_FLASH bool "Enable QSPI FLASH (W25Q128 qspi1)" select BSP_USING_QSPI diff --git a/bsp/stm32/stm32l475-atk-pandora/board/SConscript b/bsp/stm32/stm32l475-atk-pandora/board/SConscript index 1b069dffe8..086974334a 100644 --- a/bsp/stm32/stm32l475-atk-pandora/board/SConscript +++ b/bsp/stm32/stm32l475-atk-pandora/board/SConscript @@ -12,6 +12,9 @@ board.c CubeMX_Config/Src/stm32l4xx_hal_msp.c ''') +if GetDepend('BSP_USING_KEY'): + src = src + ['ports/drv_key.c'] + if GetDepend(['BSP_USING_QSPI_FLASH']): src += Glob('ports/drv_qspi_flash.c') @@ -19,7 +22,7 @@ if GetDepend('BSP_USING_SPI_LCD'): src = src + ['ports/drv_lcd.c'] if GetDepend(['BSP_USING_SDCARD']): - src += Glob('ports/sdcard_port.c') + src += Glob('ports/drv_sdcard.c') if GetDepend(['BSP_USING_ICM20608']) or GetDepend(['BSP_USING_AHT10']): src += Glob('ports/sensor_port.c') diff --git a/bsp/stm32/stm32l475-atk-pandora/board/ports/drv_key.c b/bsp/stm32/stm32l475-atk-pandora/board/ports/drv_key.c new file mode 100644 index 0000000000..9c357cd5b0 --- /dev/null +++ b/bsp/stm32/stm32l475-atk-pandora/board/ports/drv_key.c @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-04-17 Meco Man first version + */ +#include + +#ifdef BSP_USING_KEY +#define DBG_TAG "KEY" +#define DBG_LVL DBG_INFO +#include +#include +#include +#include + +#define KEY0_PIN GET_PIN(D, 10) +#define KEY1_PIN GET_PIN(D, 9) +#define KEY2_PIN GET_PIN(D, 8) +#define KEY_WKUP_PIN GET_PIN(C, 13) + +static struct rt_timer key_timer; + +static struct button key0; +static struct button key1; +static struct button key2; +static struct button key_wkup; + +/*---- user codes area begin ----*/ +/*ssers can modify according to needs*/ +static void key0_BtnCallback(void* state) +{ + rt_kprintf("key0!\n"); +} + +static void key1_BtnCallback(void* state) +{ + rt_kprintf("key1!\n"); +} + +static void key2_BtnCallback(void* state) +{ + rt_kprintf("key2!\n"); +} + +static void key_wkup_BtnCallback(void* state) +{ + rt_kprintf("key wkup!\n"); +} +/*---- user codes area end ----*/ + +static void _cb_key_timer(void *parameter) +{ + button_ticks(); +} + +static uint8_t _cb_key0_pin_level(void) +{ + return rt_pin_read(KEY0_PIN); +} + +static uint8_t _cb_key1_pin_level(void) +{ + return rt_pin_read(KEY1_PIN); +} + +static uint8_t _cb_key2_pin_level(void) +{ + return rt_pin_read(KEY2_PIN); +} + +static uint8_t _cb_key_wkup_pin_level(void) +{ + return rt_pin_read(KEY_WKUP_PIN); +} + +static int onboard_key_init(void) +{ + rt_timer_init(&key_timer, + "key timer", + _cb_key_timer, + RT_NULL, + rt_tick_from_millisecond(TICKS_INTERVAL), + RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER); + + if(rt_timer_start(&key_timer) < 0) + { + LOG_E("drv_key timer initialization failed"); + return -1; + } + + rt_pin_mode(KEY0_PIN, PIN_MODE_INPUT); + rt_pin_mode(KEY1_PIN, PIN_MODE_INPUT); + rt_pin_mode(KEY2_PIN, PIN_MODE_INPUT); + rt_pin_mode(KEY_WKUP_PIN, PIN_MODE_INPUT); + + button_init(&key0, _cb_key0_pin_level, PIN_LOW); + button_init(&key1, _cb_key1_pin_level, PIN_LOW); + button_init(&key2, _cb_key2_pin_level, PIN_LOW); + button_init(&key_wkup, _cb_key_wkup_pin_level, PIN_HIGH); + + /*---- user codes area begin ----*/ + /*ssers can modify according to needs*/ + button_attach(&key0, PRESS_DOWN, key0_BtnCallback); + button_attach(&key1, PRESS_DOWN, key1_BtnCallback); + button_attach(&key2, PRESS_DOWN, key2_BtnCallback); + button_attach(&key_wkup, PRESS_UP, key_wkup_BtnCallback); + /*---- user codes area end ----*/ + + button_start(&key0); + button_start(&key1); + button_start(&key2); + button_start(&key_wkup); + + return 0; +} +INIT_APP_EXPORT(onboard_key_init); + +#endif diff --git a/bsp/stm32/stm32l475-atk-pandora/board/ports/drv_sdcard.c b/bsp/stm32/stm32l475-atk-pandora/board/ports/drv_sdcard.c new file mode 100644 index 0000000000..2bed0ef415 --- /dev/null +++ b/bsp/stm32/stm32l475-atk-pandora/board/ports/drv_sdcard.c @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-12-14 balanceTWK add sdcard port file + * 2021-02-26 Meco Man fix a bug that cannot use fatfs in the main thread at starting up + */ + +#include + +#ifdef BSP_USING_SDCARD + +#include +#include +#include +#include "drv_spi.h" +#include "spi_msd.h" + +#define DBG_TAG "app.card" +#define DBG_LVL DBG_INFO +#include + +static void sd_mount(void *parameter) +{ + while (1) + { + rt_thread_mdelay(500); + if(rt_device_find("sd0") != RT_NULL) + { + if (dfs_mount("sd0", "/", "elm", 0, 0) == RT_EOK) + { + LOG_I("sd card mount to '/'"); + break; + } + else + { + LOG_W("sd card mount to '/' failed!"); + } + } + } +} + +static int onboard_sdcard_mount(void) +{ + rt_thread_t tid; + + if (dfs_mount("sd0", "/", "elm", 0, 0) == RT_EOK) + { + LOG_I("sd card mount to '/'"); + } + else + { + tid = rt_thread_create("sd_mount", sd_mount, RT_NULL, + 1024, RT_THREAD_PRIORITY_MAX - 2, 20); + if (tid != RT_NULL) + { + rt_thread_startup(tid); + } + else + { + LOG_E("create sd_mount thread err!"); + } + } + + return RT_EOK; +} +INIT_APP_EXPORT(onboard_sdcard_mount); + +static int rt_hw_spi1_tfcard(void) +{ + __HAL_RCC_GPIOC_CLK_ENABLE(); + rt_hw_spi_device_attach("spi1", "spi10", GPIOC, GPIO_PIN_3); + return msd_init("sd0", "spi10"); +} +INIT_DEVICE_EXPORT(rt_hw_spi1_tfcard); + +#endif /* BSP_USING_SDCARD */ + diff --git a/bsp/stm32/stm32l475-atk-pandora/board/ports/sdcard_port.c b/bsp/stm32/stm32l475-atk-pandora/board/ports/sdcard_port.c deleted file mode 100644 index bf4babd15f..0000000000 --- a/bsp/stm32/stm32l475-atk-pandora/board/ports/sdcard_port.c +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2018-12-14 balanceTWK add sdcard port file - * 2021-02-26 Meco Man fix a bug that cannot use fatfs in the main thread at starting up - */ - -#include - -#ifdef BSP_USING_SDCARD - -#include -#include -#include -#include "drv_spi.h" -#include "spi_msd.h" - -#define DBG_TAG "app.card" -#define DBG_LVL DBG_INFO -#include - -void sd_mount(void *parameter) -{ - while (1) - { - rt_thread_mdelay(500); - if(rt_device_find("sd0") != RT_NULL) - { - if (dfs_mount("sd0", "/", "elm", 0, 0) == RT_EOK) - { - LOG_I("sd card mount to '/'"); - break; - } - else - { - LOG_W("sd card mount to '/' failed!"); - } - } - } -} - -int stm32_sdcard_mount(void) -{ - rt_thread_t tid; - - if (dfs_mount("sd0", "/", "elm", 0, 0) == RT_EOK) - { - LOG_I("sd card mount to '/'"); - } - else - { - tid = rt_thread_create("sd_mount", sd_mount, RT_NULL, - 1024, RT_THREAD_PRIORITY_MAX - 2, 20); - if (tid != RT_NULL) - { - rt_thread_startup(tid); - } - else - { - LOG_E("create sd_mount thread err!"); - } - } - - return RT_EOK; -} -INIT_APP_EXPORT(stm32_sdcard_mount); - -static int rt_hw_spi1_tfcard(void) -{ - __HAL_RCC_GPIOC_CLK_ENABLE(); - rt_hw_spi_device_attach("spi1", "spi10", GPIOC, GPIO_PIN_3); - return msd_init("sd0", "spi10"); -} -INIT_DEVICE_EXPORT(rt_hw_spi1_tfcard); - -#endif /* BSP_USING_SDCARD */ - diff --git a/components/drivers/misc/pin.c b/components/drivers/misc/pin.c index 91bf7058dc..3bf9a7b039 100644 --- a/components/drivers/misc/pin.c +++ b/components/drivers/misc/pin.c @@ -146,7 +146,7 @@ void rt_pin_write(rt_base_t pin, rt_base_t value) } FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_write, pinWrite, write value to hardware pin); -int rt_pin_read(rt_base_t pin) +int rt_pin_read(rt_base_t pin) { RT_ASSERT(_hw_pin.ops != RT_NULL); return _hw_pin.ops->pin_read(&_hw_pin.parent, pin); -- cgit v1.3.1