[add] 添加 flash 驱动

[add] 添加 SPI、flash、ADC、DAC 说明文档
This commit is contained in:
Sherman 2021-10-27 18:51:05 +08:00
parent ff45ee7bbb
commit c5933a652c
23 changed files with 514 additions and 10 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@ -80,7 +80,9 @@
![image-20211019152407572](picture/wdt_config.png)
3. [如何在 ENV 中打开 WDT](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/device/watchdog/watchdog)
3. 如何在 ENV 中打开 WDT 以及 [WDT 接口使用说明](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/device/watchdog/watchdog)
![image-20211027183406251](picture/wdt_env.png)
## RTC
@ -92,4 +94,68 @@
![image-20211019152627412](picture/rtc_config.png)
3. [如何在 ENV 中打开 RTC](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/device/rtc/rtc)
3. 如何在 ENV 中打开 RTC 以及[ RTC 接口使用说明](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/device/rtc/rtc)
![image-20211027181550233](picture/rtc_env.png)
## Flash
1. 创建 Flash
![image-20211026105031200](picture/add_flash.png)
2. 配置 Flash需要注意在 RT-Thread 中只使用了一个 flash 设备,所以没有对其进行编号,如果是新创建的 flash 设备需要注意 name 字段,在驱动中默认使用的是 `g_flash`
![image-20211026105628706](picture/config_flash.png)
3. 如何在 ENV 中打开 Flash
![image-20211026123252310](picture/flash_menuconfig.png)
## SPI
1. 添加一个 SPI 外设端口
![image-20211027180820968](picture/spi_add.png)
2. 配置 channel、name、Clock Phase、Clock Polarity、Callback、 SPI Mode 等参数,波特率在代码中可通过 API 修改,这里可以设置一个默认值。
![img](picture/spi.png)
3. 如何在 ENV 中打开 SPI 以及 [SPI 接口使用说明](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/device/spi/spi)
![image-20211027181444023](picture/spi_env.png)
## ADC/DAC
创建 ADC/DAC
![img](picture/adc_dac.png)
- ADC
1. 配置 name、unit、mode选择扫描的通道编号
![img](picture/adc_config.png)
2. 配置扫描通道对应的引脚
![img](picture/adc_config1.png)
- DAC
1. 需要先关闭 P014 的默认 mode
![img](picture/dac_config0.png)
2. 开启 DAC0 通道
![img](picture/dac_config1.png)
3. 修改通道号为 0与 DAC0 对应
![img](picture/dac_config2.png)

View File

@ -141,6 +141,9 @@ menu "Hardware Drivers Config"
default n
endif
config BSP_USING_ONCHIP_FLASH
bool "Enable Onchip FLASH"
default n
endmenu
menu "Board extended module Drivers"

View File

@ -9,35 +9,38 @@ src = Split("""
drv_common.c
""")
if GetDepend(['RT_USING_SERIAL']):
if GetDepend(['BSP_USING_SERIAL']):
if GetDepend(['RT_USING_SERIAL_V2']):
src += ['drv_usart_v2.c']
else:
print("\nThe current project does not support serial-v1\n")
Return('group')
if GetDepend(['RT_USING_PIN']):
if GetDepend(['BSP_USING_PIN']):
src += ['drv_gpio.c']
if GetDepend(['RT_USING_WDT']):
if GetDepend(['BSP_USING_WDT']):
src += ['drv_wdt.c']
if GetDepend(['BSP_USING_ONCHIP_RTC']):
src += ['drv_rtc.c']
if GetDepend(['RT_USING_I2C', 'RT_USING_I2C_BITOPS']):
if GetDepend(['BSP_USING_I2C', 'RT_USING_I2C_BITOPS']):
if GetDepend('BSP_USING_I2C0') or GetDepend('BSP_USING_I2C1'):
src += ['drv_soft_i2c.c']
if GetDepend(['RT_USING_SPI']):
if GetDepend(['BSP_USING_SPI']):
src += ['drv_spi.c']
if GetDepend(['RT_USING_ADC']):
if GetDepend(['BSP_USING_ADC']):
src += ['drv_adc.c']
if GetDepend(['RT_USING_DAC']):
if GetDepend(['BSP_USING_DAC']):
src += ['drv_dac.c']
if GetDepend(['BSP_USING_ONCHIP_FLASH']):
src += ['drv_flash.c']
path = [cwd]
path += [cwd + '/config']

View File

@ -0,0 +1,298 @@
/*
* 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
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "board.h"
#include "hal_data.h"
#include "drv_flash.h"
#include <rtthread.h>
#if defined(PKG_USING_FAL)
#include "fal.h"
#endif
//#define DRV_DEBUG
#define LOG_TAG "drv.flash"
#ifdef DRV_DEBUG
#define DBG_LVL DBG_LOG
#else
#define DBG_LVL DBG_INFO
#endif /* DRV_DEBUG */
#include <rtdbg.h>
int _flash_init(void)
{
fsp_err_t err = FSP_SUCCESS;
/* Open Flash_HP */
err = R_FLASH_HP_Open(&g_flash_ctrl, &g_flash_cfg);
/* Handle Error */
if (FSP_SUCCESS != err)
{
LOG_E("\r\n Flah_HP_Open API failed");
}
/* Setup Default Block 0 as Startup Setup Block */
err = R_FLASH_HP_StartUpAreaSelect(&g_flash_ctrl, FLASH_STARTUP_AREA_BLOCK0, true);
if (err != FSP_SUCCESS)
{
LOG_E("\r\n Flah_HP_StartUpAreaSelect API failed");
}
return 0;
}
/**
* 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
*/
int _flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
{
size_t i;
if ((addr + size) > FLASH_HP_CF_BLCOK_10 + BSP_FEATURE_FLASH_HP_CF_REGION1_BLOCK_SIZE)
{
LOG_E("read outrange flash size! addr is (0x%p)", (void *)(addr + size));
return -1;
}
for (i = 0; i < size; i++, buf++, addr++)
{
*buf = *(rt_uint8_t *) addr;
}
return size;
}
/**
* 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
*/
int _flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
{
rt_err_t result = RT_EOK;
rt_base_t level;
fsp_err_t err = FSP_SUCCESS;
size_t written_size = 0;
if ((addr + size) > FLASH_HP_CF_BLCOK_10 + BSP_FEATURE_FLASH_HP_CF_REGION1_BLOCK_SIZE)
{
LOG_E("write outrange flash size! addr is (0x%p)", (void *)(addr + size));
return -RT_EINVAL;
}
if (size % BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE)
{
LOG_E("Flash Write size must be an integer multiple of %d", BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE);
return -RT_EINVAL;
}
while (written_size < size)
{
level = rt_hw_interrupt_disable();
/* Write code flash data*/
err = R_FLASH_HP_Write(&g_flash_ctrl, (uint32_t)(buf + written_size), addr + written_size, BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE);
rt_hw_interrupt_enable(level);
/* Error Handle */
if (FSP_SUCCESS != err)
{
LOG_E("Write API failed");
return -RT_EIO;
}
written_size += BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE;
}
if (result != RT_EOK)
{
return result;
}
return size;
}
/**
* 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
*/
int _flash_erase_8k(rt_uint32_t addr, size_t size)
{
fsp_err_t err = FSP_SUCCESS;
rt_base_t level;
if ((addr + size) > BSP_FEATURE_FLASH_HP_CF_REGION0_SIZE)
{
LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
return -RT_EINVAL;
}
if (size < 1)
{
return -RT_EINVAL;
}
level = rt_hw_interrupt_disable();
/* Erase Block */
err = R_FLASH_HP_Erase(&g_flash_ctrl, RT_ALIGN_DOWN(addr, FLASH_HP_CF_BLOCK_SIZE_8KB), (size - 1) / BSP_FEATURE_FLASH_HP_CF_REGION0_BLOCK_SIZE + 1);
rt_hw_interrupt_enable(level);
if (err != FSP_SUCCESS)
{
LOG_E("Erase API failed");
return -RT_EIO;
}
LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
return size;
}
int _flash_erase_128k(rt_uint32_t addr, size_t size)
{
fsp_err_t err = FSP_SUCCESS;
rt_base_t level;
if ((addr + size) > FLASH_HP_CF_BLCOK_10 + BSP_FEATURE_FLASH_HP_CF_REGION1_BLOCK_SIZE)
{
LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
return -RT_EINVAL;
}
if (size < 1)
{
return -RT_EINVAL;
}
level = rt_hw_interrupt_disable();
/* Erase Block */
err = R_FLASH_HP_Erase(&g_flash_ctrl, RT_ALIGN_DOWN(addr, FLASH_HP_CF_BLOCK_SIZE_32KB), (size - 1) / BSP_FEATURE_FLASH_HP_CF_REGION1_BLOCK_SIZE + 1);
rt_hw_interrupt_enable(level);
if (err != FSP_SUCCESS)
{
LOG_E("Erase API failed");
return -RT_EIO;
}
LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
return size;
}
#if defined(PKG_USING_FAL)
static int fal_flash_read_8k(long offset, rt_uint8_t *buf, size_t size);
static int fal_flash_read_128k(long offset, rt_uint8_t *buf, size_t size);
static int fal_flash_write_8k(long offset, const rt_uint8_t *buf, size_t size);
static int fal_flash_write_128k(long offset, const rt_uint8_t *buf, size_t size);
static int fal_flash_erase_8k(long offset, size_t size);
static int fal_flash_erase_128k(long offset, size_t size);
const struct fal_flash_dev _onchip_flash_8k = { "onchip_flash_8k", FLASH_HP_CF_BLCOK_0, FLASH_HP_CF_BLOCK_8, (8 * 1024), {_flash_init, fal_flash_read_8k, fal_flash_write_8k, fal_flash_erase_8k} };
const struct fal_flash_dev _onchip_flash_128k = { "onchip_flash_128k", FLASH_HP_CF_BLOCK_8, 32 * 3 * 1024, (128 * 1024), {_flash_init, fal_flash_read_128k, fal_flash_write_128k, fal_flash_erase_128k} };
static int fal_flash_read_8k(long offset, rt_uint8_t *buf, size_t size)
{
return _flash_read(_onchip_flash_8k.addr + offset, buf, size);
}
static int fal_flash_read_128k(long offset, rt_uint8_t *buf, size_t size)
{
return _flash_read(_onchip_flash_128k.addr + offset, buf, size);
}
static int fal_flash_write_8k(long offset, const rt_uint8_t *buf, size_t size)
{
return _flash_write(_onchip_flash_8k.addr + offset, buf, size);
}
static int fal_flash_write_128k(long offset, const rt_uint8_t *buf, size_t size)
{
return _flash_write(_onchip_flash_128k.addr + offset, buf, size);
}
static int fal_flash_erase_8k(long offset, size_t size)
{
return _flash_erase_8k(_onchip_flash_8k.addr + offset, size);
}
static int fal_flash_erase_128k(long offset, size_t size)
{
return _flash_erase_128k(_onchip_flash_128k.addr + offset, size);
}
int flash_test(void)
{
#define TEST_OFF 0x10000
const struct fal_partition *param;
uint8_t write_buffer[BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE] = {0};
uint8_t read_buffer[BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE] = {0};
/* Set write buffer, clear read buffer */
for (uint8_t index = 0; index < BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE; index++)
{
write_buffer[index] = index;
read_buffer[index] = 0;
}
fal_init();
param = fal_partition_find("param");
if (param == RT_NULL)
{
LOG_E("not find partition param!");
return -1;
}
LOG_I("Erase Start...");
fal_partition_erase(param, TEST_OFF, BSP_FEATURE_FLASH_HP_CF_REGION1_BLOCK_SIZE);
LOG_I("Erase succeeded!");
LOG_I("Write Start...");
fal_partition_write(param, TEST_OFF, write_buffer, sizeof(write_buffer));
LOG_I("Write succeeded!");
LOG_I("Read Start...");
fal_partition_read(param, TEST_OFF, read_buffer, BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE);
LOG_I("Read succeeded!");
for (int i = 0; i < BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE; i++)
{
if (read_buffer[i] != write_buffer[i])
{
LOG_E("Data verification failed!");
return -1;
}
}
LOG_I("Data verification succeeded!");
return 0;
}
MSH_CMD_EXPORT(flash_test, "drv flash test.");
#endif

View File

@ -0,0 +1,64 @@
/*
* 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 __DRV_FLASH_H__
#define __DRV_FLASH_H__
#include <rtthread.h>
#include "rtdevice.h"
#include <rthw.h>
#include <drv_common.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Code Flash */
#define FLASH_HP_CF_BLOCK_SIZE_32KB (32*1024) /* Block Size 32 KB */
#define FLASH_HP_CF_BLOCK_SIZE_8KB (8*1024) /* Block Size 8KB */
#define FLASH_HP_CF_BLCOK_0 0x00000000U /* 8 KB: 0x00000000 - 0x00001FFF */
#define FLASH_HP_CF_BLOCK_1 0x00002000U /* 8 KB: 0x00002000 - 0x00003FFF */
#define FLASH_HP_CF_BLOCK_2 0x00004000U /* 8 KB: 0x00004000 - 0x00005FFF */
#define FLASH_HP_CF_BLOCK_3 0x00006000U /* 8 KB: 0x00006000 - 0x00007FFF */
#define FLASH_HP_CF_BLOCK_4 0x00008000U /* 8 KB: 0x00008000 - 0x00009FFF */
#define FLASH_HP_CF_BLOCK_5 0x0000A000U /* 8 KB: 0x0000A000 - 0x0000BFFF */
#define FLASH_HP_CF_BLOCK_6 0x0000C000U /* 8 KB: 0x0000C000 - 0x0000DFFF */
#define FLASH_HP_CF_BLOCK_7 0x0000E000U /* 8 KB: 0x0000E000 - 0x0000FFFF */
#define FLASH_HP_CF_BLOCK_8 0x00010000U /* 32 KB: 0x00010000 - 0x00017FFF */
#define FLASH_HP_CF_BLOCK_9 0x00018000U /* 32 KB: 0x00018000 - 0x0001FFFF */
#define FLASH_HP_CF_BLCOK_10 0x00020000U /* 32 KB: 0x00020000 - 0x0004FFFF */
#define FLASH_HP_DF_BLOCK_SIZE (64)
/* Data Flash */
#if (defined (BOARD_RA6M4_EK) || defined (BOARD_RA6M5_EK) || defined (BOARD_RA4M3_EK)||defined(BOARD_RA4M2_EK))
#define FLASH_HP_DF_BLOCK_0 0x08000000U /* 64 B: 0x40100000 - 0x4010003F */
#define FLASH_HP_DF_BLOCK_1 0x08000040U /* 64 B: 0x40100040 - 0x4010007F */
#define FLASH_HP_DF_BLOCK_2 0x08000080U /* 64 B: 0x40100080 - 0x401000BF */
#define FLASH_HP_DF_BLOCK_3 0x080000C0U /* 64 B: 0x401000C0 - 0x401000FF */
#else
#define FLASH_HP_DF_BLOCK_0 0x40100000U /* 64 B: 0x40100000 - 0x4010003F */
#define FLASH_HP_DF_BLOCK_1 0x40100040U /* 64 B: 0x40100040 - 0x4010007F */
#define FLASH_HP_DF_BLOCK_2 0x40100080U /* 64 B: 0x40100080 - 0x401000BF */
#define FLASH_HP_DF_BLOCK_3 0x401000C0U /* 64 B: 0x401000C0 - 0x401000FF */
#endif
#define BLOCK_SIZE (128)
#define BLOCK_NUM (2)
#ifdef __cplusplus
}
#endif
#endif /* __DRV_FLASH_H__ */

View File

@ -44,7 +44,6 @@ static rt_err_t wdt_control(rt_watchdog_t *wdt, int cmd, void *arg)
/* set watchdog timeout */
case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
/**< set*/
// g_wdt_cfg.timeout = *(rt_uint32_t *)arg;
break;
case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
wdt_value = (struct st_wdt_timeout_values *)arg;

View File

@ -0,0 +1,12 @@
import os
from building import *
objs = []
cwd = GetCurrentDir()
list = os.listdir(cwd)
for item in list:
if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
objs = objs + SConscript(os.path.join(item, 'SConscript'))
Return('objs')

View File

@ -0,0 +1,20 @@
from building import *
import rtconfig
cwd = GetCurrentDir()
src = []
src += Glob('*.c')
CPPPATH = [cwd]
LOCAL_CCFLAGS = ''
if rtconfig.CROSS_TOOL == 'gcc':
LOCAL_CCFLAGS += ' -std=c99'
elif rtconfig.CROSS_TOOL == 'keil':
LOCAL_CCFLAGS += ' --c99'
group = DefineGroup('FAL', src, depend = ['PKG_USING_FAL'], CPPPATH = CPPPATH, LOCAL_CCFLAGS = LOCAL_CCFLAGS)
Return('group')

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-08-21 MurphyZhao the first version
*/
#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_
#include <rtthread.h>
#include <board.h>
/* enable stm32l4 onchip flash driver sample */
#define FAL_FLASH_PORT_DRIVER_STM32L4
/* enable SFUD flash driver sample */
#define FAL_FLASH_PORT_DRIVER_SFUD
extern const struct fal_flash_dev _onchip_flash_8k;
extern const struct fal_flash_dev _onchip_flash_128k;
/* flash device table */
#define FAL_FLASH_DEV_TABLE \
{ \
&_onchip_flash_8k, \
&_onchip_flash_128k, \
}
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG
/* partition table */
#define FAL_PART_TABLE \
{ \
{FAL_PART_MAGIC_WROD, "app", "onchip_flash_8k", 0, 64 * 1024, 0}, \
{FAL_PART_MAGIC_WROD, "param", "onchip_flash_128k", 0, 3 * 128 * 1024, 0}, \
}
#endif /* FAL_PART_HAS_TABLE_CFG */
#endif /* _FAL_CFG_H_ */