原始版本

This commit is contained in:
冯佳
2025-06-19 21:56:46 +08:00
parent fe98e5f010
commit a4841450cf
4152 changed files with 1910684 additions and 0 deletions

View File

@ -0,0 +1,70 @@
import os
import rtconfig
from building import *
Import('SDK_LIB')
cwd = GetCurrentDir()
# add general drivers
src = []
path = [cwd]
if GetDepend(['BSP_USING_ETH']):
src += Glob('phy_reset.c')
if GetDepend(['BSP_USING_RS485']):
src += Glob('drv_rs485.c')
if GetDepend(['BSP_USING_SOFT_SPI_FLASH']):
src += Glob('soft_spi_flash_init.c')
if GetDepend(['BSP_USING_SPI_FLASH']):
src += Glob('spi_flash_init.c')
if GetDepend(['BSP_USING_FS']):
src += Glob('drv_filesystem.c')
if GetDepend(['BSP_USING_FAL']):
src += Glob('fal/fal_spi_flash_sfud_port.c')
path += [cwd + '/fal']
if GetDepend(['BSP_USING_SRAM']):
src += Glob('drv_sram.c')
if GetDepend(['BSP_USING_ONBOARD_LCD']):
src += Glob('lcd/drv_lcd.c')
path += [cwd + '/lcd']
if GetDepend(['BSP_USING_ONBOARD_LED_MATRIX']):
src += Glob('led_matrix/drv_matrix_led.c')
path += [cwd + '/led_matrix']
if GetDepend(['BSP_USING_EASYFLASH']):
src += Glob('ef_fal_port.c')
if GetDepend(['BSP_USING_ENC28j60']):
src += Glob('drv_enc28j60.c')
if GetDepend(['BSP_USING_ONBOARD_PM']):
src += Glob('pm/drv_pm.c')
src += Glob('pm/drv_wakeup.c')
path += [cwd + '/pm']
if GetDepend(['BSP_USING_AUDIO']):
src += Glob('audio/drv_es8388.c')
src += Glob('audio/drv_sound.c')
path += [cwd + '/audio']
if GetDepend(['BSP_USING_AUDIO_RECORD']):
src += Glob('audio/drv_mic.c')
CPPDEFINES = ['STM32F407xx']
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = path, CPPDEFINES = CPPDEFINES)
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')

View File

@ -0,0 +1,123 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-12-13 balanceTWK add sdcard port file
* 2021-05-10 Meco Man fix a bug that cannot use fatfs in the main thread at starting up
* 2021-07-28 Meco Man implement romfs as the root filesystem
*/
#include <rtthread.h>
#include <dfs_romfs.h>
#include <dfs_fs.h>
#include <dfs_file.h>
#if DFS_FILESYSTEMS_MAX < 4
#error "Please define DFS_FILESYSTEMS_MAX more than 4"
#endif
#if DFS_FILESYSTEM_TYPES_MAX < 4
#error "Please define DFS_FILESYSTEM_TYPES_MAX more than 4"
#endif
#define DBG_TAG "app.filesystem"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#ifdef BSP_USING_FS_AUTO_MOUNT
#ifdef BSP_USING_SDCARD_FATFS
static int onboard_sdcard_mount(void)
{
if (dfs_mount("sd0", "/sdcard", "elm", 0, 0) == RT_EOK)
{
LOG_I("SD card mount to '/sdcard'");
}
else
{
LOG_E("SD card mount to '/sdcard' failed!");
}
return RT_EOK;
}
#endif /* BSP_USING_SDCARD_FATFS */
#endif /* BSP_USING_FS_AUTO_MOUNT */
#ifdef BSP_USING_FLASH_FS_AUTO_MOUNT
#ifdef BSP_USING_FLASH_FATFS
#define FS_PARTITION_NAME "filesystem"
static int onboard_fal_mount(void)
{
/* 初始化 fal 功能 */
extern int fal_init(void);
extern struct rt_device *fal_blk_device_create(const char *parition_name);
fal_init();
/* 在 spi flash 中名为 "filesystem" 的分区上创建一个块设备 */
struct rt_device *flash_dev = fal_blk_device_create(FS_PARTITION_NAME);
if (flash_dev == NULL)
{
LOG_E("Can't create a block device on '%s' partition.", FS_PARTITION_NAME);
}
else
{
LOG_D("Create a block device on the %s partition of flash successful.", FS_PARTITION_NAME);
}
/* 挂载 spi flash 中名为 "filesystem" 的分区上的文件系统 */
if (dfs_mount(flash_dev->parent.name, "/fal", "elm", 0, 0) == 0)
{
LOG_I("Filesystem initialized!");
}
else
{
LOG_E("Failed to initialize filesystem!");
LOG_D("You should create a filesystem on the block device first!");
}
return RT_EOK;
}
#endif /*BSP_USING_FLASH_FATFS*/
#endif /*BSP_USING_FLASH_FS_AUTO_MOUNT*/
const struct romfs_dirent _romfs_root[] =
{
#ifdef BSP_USING_SDCARD_FATFS
{ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0},
#endif
#ifdef BSP_USING_FLASH_FATFS
{ROMFS_DIRENT_DIR, "fal", RT_NULL, 0},
#endif
};
const struct romfs_dirent romfs_root =
{
ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_romfs_root, sizeof(_romfs_root) / sizeof(_romfs_root[0])
};
static int filesystem_mount(void)
{
#ifdef BSP_USING_FS
if (dfs_mount(RT_NULL, "/", "rom", 0, &(romfs_root)) != 0)
{
LOG_E("rom mount to '/' failed!");
}
/* 确保块设备注册成功之后再挂载文件系统 */
rt_thread_delay(500);
#endif
#ifdef BSP_USING_FS_AUTO_MOUNT
onboard_sdcard_mount();
#endif /* BSP_USING_FS_AUTO_MOUNT */
#ifdef BSP_USING_FLASH_FS_AUTO_MOUNT
onboard_fal_mount();
#endif
return RT_EOK;
}
INIT_APP_EXPORT(filesystem_mount);

View File

@ -0,0 +1,210 @@
/*
* This file is part of the EasyFlash Library.
*
* Copyright (c) 2015, Armink, <armink.ztl@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* 'Software'), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Function: Portable interface for FAL (Flash Abstraction Layer) partition.
* Created on: 2018-05-19
*/
#include <easyflash.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <rthw.h>
#include <rtthread.h>
#include <fal.h>
/* EasyFlash partition name on FAL partition table */
#define FAL_EF_PART_NAME "easyflash"
/* default ENV set for user */
static const ef_env default_env_set[] = {
{"iap_need_copy_app", "0"},
{"iap_need_crc32_check", "0"},
{"iap_copy_app_size", "0"},
{"stop_in_bootloader", "0"},
};
static char log_buf[RT_CONSOLEBUF_SIZE];
static struct rt_semaphore env_cache_lock;
static const struct fal_partition *part = NULL;
/**
* Flash port for hardware initialize.
*
* @param default_env default ENV set for user
* @param default_env_size default ENV size
*
* @return result
*/
EfErrCode ef_port_init(ef_env const **default_env, rt_size_t *default_env_size) {
EfErrCode result = EF_NO_ERR;
*default_env = default_env_set;
*default_env_size = sizeof(default_env_set) / sizeof(default_env_set[0]);
rt_sem_init(&env_cache_lock, "env lock", 1, RT_IPC_FLAG_PRIO);
part = fal_partition_find(FAL_EF_PART_NAME);
EF_ASSERT(part);
return result;
}
/**
* Read data from flash.
* @note This operation's units is word.
*
* @param addr flash address
* @param buf buffer to store read data
* @param size read bytes size
*
* @return result
*/
EfErrCode ef_port_read(rt_uint32_t addr, rt_uint32_t *buf, rt_size_t size) {
EfErrCode result = EF_NO_ERR;
fal_partition_read(part, addr, (rt_uint8_t *)buf, size);
return result;
}
/**
* Erase data on flash.
* @note This operation is irreversible.
* @note This operation's units is different which on many chips.
*
* @param addr flash address
* @param size erase bytes size
*
* @return result
*/
EfErrCode ef_port_erase(rt_uint32_t addr, rt_size_t size) {
EfErrCode result = EF_NO_ERR;
/* make sure the start address is a multiple of FLASH_ERASE_MIN_SIZE */
EF_ASSERT(addr % EF_ERASE_MIN_SIZE == 0);
if (fal_partition_erase(part, addr, size) < 0)
{
result = EF_ERASE_ERR;
}
return result;
}
/**
* Write data to flash.
* @note This operation's units is word.
* @note This operation must after erase. @see flash_erase.
*
* @param addr flash address
* @param buf the write data buffer
* @param size write bytes size
*
* @return result
*/
EfErrCode ef_port_write(rt_uint32_t addr, const rt_uint32_t *buf, rt_size_t size) {
EfErrCode result = EF_NO_ERR;
if (fal_partition_write(part, addr, (rt_uint8_t *)buf, size) < 0)
{
result = EF_WRITE_ERR;
}
return result;
}
/**
* lock the ENV ram cache
*/
void ef_port_env_lock(void) {
rt_sem_take(&env_cache_lock, RT_WAITING_FOREVER);
}
/**
* unlock the ENV ram cache
*/
void ef_port_env_unlock(void) {
rt_sem_release(&env_cache_lock);
}
/**
* This function is print flash debug info.
*
* @param file the file which has call this function
* @param line the line number which has call this function
* @param format output format
* @param ... args
*
*/
void ef_log_debug(const char *file, const long line, const char *format, ...) {
#ifdef PRINT_DEBUG
va_list args;
/* args point to the first variable parameter */
va_start(args, format);
ef_print("[Flash] (%s:%ld) ", file, line);
/* must use vprintf to print */
rt_vsprintf(log_buf, format, args);
ef_print("%s", log_buf);
va_end(args);
#endif
}
/**
* This function is print flash routine info.
*
* @param format output format
* @param ... args
*/
void ef_log_info(const char *format, ...) {
va_list args;
/* args point to the first variable parameter */
va_start(args, format);
ef_print("[Flash] ");
/* must use vprintf to print */
rt_vsprintf(log_buf, format, args);
ef_print("%s", log_buf);
va_end(args);
}
/**
* This function is print flash non-package info.
*
* @param format output format
* @param ... args
*/
void ef_print(const char *format, ...) {
va_list args;
/* args point to the first variable parameter */
va_start(args, format);
/* must use vprintf to print */
rt_vsprintf(log_buf, format, args);
rt_kprintf("%s", log_buf);
va_end(args);
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-12-5 SummerGift first version
*/
#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_
#include <board.h>
#include <fal_def.h>
#define FLASH_SIZE_GRANULARITY_16K (4 * 16 * 1024)
#define FLASH_SIZE_GRANULARITY_64K (8 * 64 * 1024)
#define FLASH_SIZE_GRANULARITY_128K (8 * 128 * 1024)
#define STM32_FLASH_START_ADRESS_16K STM32_FLASH_START_ADRESS
#define STM32_FLASH_START_ADRESS_64K STM32_FLASH_START_ADRESS
#define STM32_FLASH_START_ADRESS_128K STM32_FLASH_START_ADRESS
extern const struct fal_flash_dev stm32_onchip_flash_16k;
extern const struct fal_flash_dev stm32_onchip_flash_64k;
extern const struct fal_flash_dev stm32_onchip_flash_128k;
extern struct fal_flash_dev w25q16jv;
/* flash device table */
#define FAL_FLASH_DEV_TABLE \
{ \
&stm32_onchip_flash_128k, \
&w25q16jv, \
}
/* ====================== Partition Configuration ========================== */
// #define FAL_PART_TABLE \
// { \
// {FAL_PART_MAGIC_WROD, "app", "onchip_flash_128k", 0, 384 * 1024, 0}, \
// {FAL_PART_MAGIC_WROD, "param", "onchip_flash_128k", 384 * 1024, 512 * 1024, 0}, \
// {FAL_PART_MAGIC_WROD, "easyflash", "W25Q16JV", 0, 512 * 1024, 0}, \
// {FAL_PART_MAGIC_WROD, "download", "W25Q16JV", 512 * 1024, 512 * 1024, 0}, \
// {FAL_PART_MAGIC_WROD, "filesystem", "W25Q16JV", (512 + 512) * 1024, 1 * 1024 * 1024, 0}, \
// }
#define FAL_PART_TABLE \
{ \
{FAL_PART_MAGIC_WROD, "app", "onchip_flash_128k", 0, 384*1024, 0}, \
{FAL_PART_MAGIC_WROD, "param", "onchip_flash_128k", 384*1024, 512*1024, 0}, \
{FAL_PART_MAGIC_WROD, "easyflash", "W25Q16JV", 0, 512*1024, 0}, \
{FAL_PART_MAGIC_WROD, "download", "W25Q16JV", 512*1024, 512*1024, 0}, \
{FAL_PART_MAGIC_WROD, "filesystem", "W25Q16JV", 1024*1024, 1024*1024, 0}, \
}
#endif /*FAL_PART_TABLE*/

View File

@ -0,0 +1,80 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-08-07 Meco Man first version
*/
#include <fal.h>
#include <sfud.h>
#ifdef RT_USING_SFUD
#include <dev_spi_flash_sfud.h>
#endif
static int init(void);
static int read(long offset, rt_uint8_t *buf, rt_size_t size);
static int write(long offset, const rt_uint8_t *buf, rt_size_t size);
static int erase(long offset, rt_size_t size);
static sfud_flash_t sfud_dev = NULL;
struct fal_flash_dev w25q16jv =
{
.name = "W25Q16JV",
.addr = 0,
.len = 2 * 1024 * 1024,
.blk_size = 4096,
.ops = {init, read, write, erase},
.write_gran = 1
};
static int init(void)
{
sfud_dev = rt_sfud_flash_find_by_dev_name("W25Q16JV");
if (RT_NULL == sfud_dev)
{
return -1;
}
/* update the flash chip information */
w25q16jv.blk_size = sfud_dev->chip.erase_gran;
w25q16jv.len = sfud_dev->chip.capacity;
return 0;
}
static int read(long offset, rt_uint8_t *buf, rt_size_t size)
{
RT_ASSERT(sfud_dev);
RT_ASSERT(sfud_dev->init_ok);
sfud_read(sfud_dev, w25q16jv.addr + offset, size, buf);
return size;
}
static int write(long offset, const rt_uint8_t *buf, rt_size_t size)
{
RT_ASSERT(sfud_dev);
RT_ASSERT(sfud_dev->init_ok);
if (sfud_write(sfud_dev, w25q16jv.addr + offset, size, buf) != SFUD_SUCCESS)
{
return -1;
}
return size;
}
static int erase(long offset, rt_size_t size)
{
RT_ASSERT(sfud_dev);
RT_ASSERT(sfud_dev->init_ok);
if (sfud_erase(sfud_dev, w25q16jv.addr + offset, size) != SFUD_SUCCESS)
{
return -1;
}
return size;
}

View File

@ -0,0 +1,28 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-11-23 flybreak first version
*/
#include <board.h>
#define RESET_IO GET_PIN(D, 3)
void phy_reset(void)
{
rt_pin_write(RESET_IO, PIN_LOW);
rt_thread_mdelay(50);
rt_pin_write(RESET_IO, PIN_HIGH);
}
int phy_init(void)
{
rt_pin_mode(RESET_IO, PIN_MODE_OUTPUT);
rt_pin_write(RESET_IO, PIN_HIGH);
return RT_EOK;
}
INIT_BOARD_EXPORT(phy_init);

View File

@ -0,0 +1,121 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-07-31 tanek first version
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
/**
* This function will put STM32F4xx into sleep mode.
*
* @param pm pointer to power manage structure
*/
static void sleep(struct rt_pm *pm, uint8_t mode)
{
switch (mode)
{
case PM_SLEEP_MODE_NONE:
break;
case PM_SLEEP_MODE_IDLE:
break;
case PM_SLEEP_MODE_LIGHT:
HAL_SuspendTick(); /* 关闭系统时钟中断 */
HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); /* 进入 F407 sleep 模式,这个模式会停掉所有时钟,可被任意中断唤醒 */
break;
case PM_SLEEP_MODE_DEEP:
HAL_SuspendTick(); /* 关闭系统时钟中断 */
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); /* 进入 F407 stop 模式,这个模式会停掉所有时钟,可被任意中断唤醒 */
break;
case PM_SLEEP_MODE_STANDBY:
break;
case PM_SLEEP_MODE_SHUTDOWN:
break;
default:
RT_ASSERT(0);
break;
}
}
/**
* This function will be Called in Wake up interrupt callback
*
* @param pm pointer to power manage structure
*/
static struct rt_device *device = RT_NULL;
static struct rt_pm *pm = RT_NULL;
void pm_wk_up()
{
switch (pm->sleep_mode)
{
case PM_SLEEP_MODE_NONE:
break;
case PM_SLEEP_MODE_IDLE:
break;
case PM_SLEEP_MODE_LIGHT:
HAL_ResumeTick(); /* 启动系统时钟中断 */
break;
case PM_SLEEP_MODE_DEEP:
SystemClock_Config(); /* 重新配置系统时钟 */
HAL_ResumeTick(); /* 启动系统时钟中断 */
break;
case PM_SLEEP_MODE_STANDBY:
break;
case PM_SLEEP_MODE_SHUTDOWN:
break;
default:
RT_ASSERT(0);
break;
}
}
/**
* This function initialize the power manager
*/
static int drv_pm_hw_init(void)
{
static const struct rt_pm_ops _ops =
{
sleep,
RT_NULL,
RT_NULL,
RT_NULL,
RT_NULL
};
/* initialize system pm module */
rt_system_pm_init(&_ops, 0, RT_NULL);
/* get pm device */
device = rt_device_find("pm");
if(device == RT_NULL)
{
rt_kprintf("rt_pm find error");
return 0;
}
pm = rt_container_of(device,struct rt_pm,parent);
return 1;
}
INIT_DEVICE_EXPORT(drv_pm_hw_init);

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-08-07 Tanek first implementation
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <stm32F4xx.h>
#include "board.h"
#include "drv_gpio.h"
#define USER_WAKEUP_PIN GET_PIN(C, 5)
#define DRV_WKUP_PIN_IRQ_MODE PIN_IRQ_MODE_FALLING
static void (*_wakeup_hook)(void);
void bsp_register_wakeup(void (*hook)(void))
{
RT_ASSERT(hook != RT_NULL);
_wakeup_hook = hook;
}
static void _wakeup_callback(void *args)
{
extern void pm_wk_up();
pm_wk_up(); /* wakeup from deep sleep */
if (_wakeup_hook)
_wakeup_hook();
}
static int rt_hw_wakeup_init(void)
{
rt_pin_mode(USER_WAKEUP_PIN, PIN_MODE_INPUT_PULLUP);
rt_pin_attach_irq(USER_WAKEUP_PIN, DRV_WKUP_PIN_IRQ_MODE, _wakeup_callback, RT_NULL);
rt_pin_irq_enable(USER_WAKEUP_PIN, 1);
return 0;
}
INIT_BOARD_EXPORT(rt_hw_wakeup_init);

View File

@ -0,0 +1,17 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-08-07 Tanek first implementation
*/
#ifndef __DRV_WAKEUP_H__
#define __DRV_WAKEUP_H__
extern void bsp_register_wakeup(void (*hook)(void));
#endif /* __DRV_WAKEUP_H__ */

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-6-14 solar first version
*/
#include <rtthread.h>
#include "dev_spi_flash.h"
#include "dev_spi_flash_sfud.h"
#include <drv_spi.h>
#include <drv_soft_spi.h>
#ifdef BSP_USING_SOFT_SPI_FLASH
static int rt_soft_spi_flash_init(void)
{
__HAL_RCC_GPIOB_CLK_ENABLE();
rt_hw_soft_spi_device_attach("sspi2", "sspi20", "PB.0");
if (RT_NULL == rt_sfud_flash_probe("W25Q16JV", "sspi20"))
{
return -RT_ERROR;
}
return RT_EOK;
}
INIT_COMPONENT_EXPORT(rt_soft_spi_flash_init);
#endif /* BSP_USING_SOFT_SPI_FLASH */

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-11-27 SummerGift add spi flash port file
*/
#include <rtthread.h>
#include "dev_spi_flash.h"
#include "dev_spi_flash_sfud.h"
#include <drv_spi.h>
#include <drv_gpio.h>
#if defined(BSP_USING_SPI_FLASH)
static int rt_hw_spi_flash_init(void)
{
__HAL_RCC_GPIOB_CLK_ENABLE();
rt_hw_spi_device_attach("spi1", "spi10", GET_PIN(B, 0));
if (RT_NULL == rt_sfud_flash_probe("W25Q16JV", "spi10"))
{
return -RT_ERROR;
}
return RT_EOK;
}
INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init);
#endif