红枫
红枫 - 认证专家
This guy hasn't written anything yet

注册于 1 year ago

回答
256
文章
4
关注者
30

使用函数rt_hwtimer_control直接设置频率

卡在的while循环在哪个文件,上下文是什么?既然是状态不满足条件导致的死循环,查查是什么原因导致的?找到根源才好解决问题。

改不了。分区表定义是const类型,并且需要magic标识,就是防止随便修改的。

如果线程有需要释放的动态申请资源需用户设置线程的clearup回调,由回调函数完成资源释放。

AB两个线程都频繁地写文件,易造成写文件冲突,导致文件系统结构数据被破坏,进而形成文件系统崩溃。改进方案有2个:
1.改为只有一个线程写文件。A线程把数据通过邮箱传递给B线程,然后计算、写文件。
2.A线程写文件时提前预留出写结果空间,避免B线程再次写入时引起文件系统结构变化。可能lfs下,此方案不可行。

调用main的调用者没有使用此返回值,被编译器就优化掉了

可能是高优先级线程占死了cpu,导致低优先级线程得不到执行。加上syswatch看看是哪个线程死了。

自动初始化过程使用的是main线程,所以main线程堆栈不能太小,否则会堆栈溢出系统崩溃。

spi从机驱动,可以使用spi接收中断实现也可以使用cs引脚下降沿中断加spi状态轮询实现,都是接收主机命令进行处理,做spi从机的难点在与命令的处理速度,尤其是主机读数据时的处理速度,需要几个us完成响应数据准备。

/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-12-5      SummerGift   first version
 * 2019-3-2       jinsheng     add Macro judgment
 * 2020-1-6       duminmin     support single bank mode
 * 2020-5-17      yufanyufan77 support support H7
 */

#include "board.h"

#ifdef BSP_USING_ON_CHIP_FLASH
#include "drv_config.h"
#include "drv_flash.h"

#if defined(PKG_USING_FAL)
#include "fal.h"
#endif

//#define DRV_DEBUG
#define LOG_TAG                 "drv.flash"
#include <drv_log.h>

/**
  * @brief  Gets the Sector of a given address
  * @param  Addr: Address of the FLASH Memory
  * @retval The page of a given address
  */
static uint32_t GetSector(uint32_t Addr)
{
    uint32_t Sector = 0;

    if (Addr < (FLASH_BASE + FLASH_BANK_SIZE))
    {
        /* Bank 1 */
        Sector = (Addr - FLASH_BASE) / FLASH_SECTOR_SIZE;
    }
    else
    {
        /* Bank 2 */
        Sector = (Addr - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_SECTOR_SIZE;
    }

    return Sector;
}

/**
  * @brief  Gets the bank of a given address
  * @param  Addr: Address of the FLASH Memory
  * @retval The bank of a given address
  */
static uint32_t GetBank(uint32_t Addr)
{
    uint32_t bank = 0;
#ifndef FLASH_BANK_2
    bank = FLASH_BANK_1;
#else
    //if (READ_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_FB_MODE) == 0)
    //{
        /* No Bank swap */
        if (Addr < (FLASH_BASE + FLASH_BANK_SIZE))
        {
            bank = FLASH_BANK_1;
        }
        else
        {
            bank = FLASH_BANK_2;
        }
    //}
    //else
    //{
        /* Bank swap */
    //    if (Addr < (FLASH_BASE + FLASH_BANK_SIZE))
    //    {
    //        bank = FLASH_BANK_2;
    //    }
    //    else
    //    {
    //        bank = FLASH_BANK_1;
    //    }
    //}
#endif
    return bank;
}


/**
 * 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 stm32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
{
    size_t i;

    if ((addr + size) > STM32_FLASH_END_ADDRESS)
    {
        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 stm32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
{
    rt_err_t result      = RT_EOK;
    rt_uint32_t end_addr = addr + size;

    if ((end_addr) > STM32_FLASH_END_ADDRESS)
    {
        LOG_E("write outrange flash size! addr is (0x%p)", (void *)(addr + size));
        return -RT_EINVAL;
    }

    if (size < 1)
    {
        return -RT_EINVAL;
    }

    /* Unlock the Flash to enable the flash control register access */
    HAL_FLASH_Unlock();
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR );

    while(addr < end_addr)
    {
        /* write data to flash */
        if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, addr,  (uint32_t)buf) != HAL_OK)
        {
            result = -RT_ERROR;
            break;
        }
        addr += 32;
        buf  += 32;
    }

    HAL_FLASH_Lock();

    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 stm32_flash_erase(rt_uint32_t addr, size_t size)
{
    rt_err_t result = RT_EOK;
    rt_uint32_t FirstSector = 0, NbOfSectors = 0;
    rt_uint32_t SECTORError = 0;
    rt_uint32_t bank = 0;

    if ((addr + size) > STM32_FLASH_END_ADDRESS)
    {
        LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
        return -RT_EINVAL;
    }

    /*Variable used for Erase procedure*/
    FLASH_EraseInitTypeDef EraseInitStruct;

    /* Unlock the Flash to enable the flash control register access */
    HAL_FLASH_Unlock();

    /* Get the 1st sector to erase */
    bank = GetBank(addr);
    FirstSector = GetSector(addr);
    NbOfSectors = GetSector(addr + size - 1);
    NbOfSectors = NbOfSectors - FirstSector + 1;
    /* Fill EraseInit structure */
    EraseInitStruct.TypeErase     = FLASH_TYPEERASE_SECTORS;
    EraseInitStruct.VoltageRange  = FLASH_VOLTAGE_RANGE_3;
    EraseInitStruct.Sector        = FirstSector;
    EraseInitStruct.NbSectors     = NbOfSectors;
    EraseInitStruct.Banks         = bank;

    if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)
    {
        result = -RT_ERROR;
        goto __exit;
    }

__exit:

    HAL_FLASH_Lock();

    if (result != RT_EOK)
    {
        return result;
    }

    LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
    return size;
}

#if defined(PKG_USING_FAL)
static int fal_flash_read_128k(long offset, 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_128k(long offset, size_t size);
const struct fal_flash_dev stm32_onchip_flash_128k = { "onchip_flash_128k", STM32_FLASH_START_ADRESS, STM32_FLASH_SIZE, FLASH_SECTOR_SIZE, {NULL, fal_flash_read_128k, fal_flash_write_128k, fal_flash_erase_128k} };

static int fal_flash_read_128k(long offset, rt_uint8_t *buf, size_t size)
{
    return stm32_flash_read(stm32_onchip_flash_128k.addr + offset, buf, size);
}
static int fal_flash_write_128k(long offset, const rt_uint8_t *buf, size_t size)
{
    return stm32_flash_write(stm32_onchip_flash_128k.addr + offset, buf, size);
}

static int fal_flash_erase_128k(long offset, size_t size)
{
    return stm32_flash_erase(stm32_onchip_flash_128k.addr + offset, size);
}

#endif
#endif /* BSP_USING_ON_CHIP_FLASH */

回到
顶部

发布
问题

投诉
建议