init:初始框架
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
/*********************************************************************************************************************
|
||||
* MSPM0G3507 Opensource Library 即(MSPM0G3507 开源库)是一个基于官方 SDK 接口的第三方开源库
|
||||
* Copyright (c) 2022 SEEKFREE 逐飞科技
|
||||
*
|
||||
* 本文件是 MSPM0G3507 开源库的一部分
|
||||
*
|
||||
* MSPM0G3507 开源库 是免费软件
|
||||
* 您可以根据自由软件基金会发布的 GPL(GNU General Public License,即 GNU通用公共许可证)的条款
|
||||
* 即 GPL 的第3版(即 GPL3.0)或(您选择的)任何后来的版本,重新发布和/或修改它
|
||||
*
|
||||
* 本开源库的发布是希望它能发挥作用,但并未对其作任何的保证
|
||||
* 甚至没有隐含的适销性或适合特定用途的保证
|
||||
* 更多细节请参见 GPL
|
||||
*
|
||||
* 您应该在收到本开源库的同时收到一份 GPL 的副本
|
||||
* 如果没有,请参阅<https://www.gnu.org/licenses/>
|
||||
*
|
||||
* 额外注明:
|
||||
* 本开源库使用 GPL3.0 开源许可证协议 以上许可申明为译文版本
|
||||
* 许可申明英文版在 libraries/doc 文件夹下的 GPL3_permission_statement.txt 文件中
|
||||
* 许可证副本在 libraries 文件夹下 即该文件夹下的 LICENSE 文件
|
||||
* 欢迎各位使用并传播本程序 但修改内容时必须保留逐飞科技的版权声明(即本声明)
|
||||
*
|
||||
* 文件名称 zf_common_clock
|
||||
* 公司名称 成都逐飞科技有限公司
|
||||
* 版本信息 查看 libraries/doc 文件夹内 version 文件 版本说明
|
||||
* 开发环境 MDK 5.37
|
||||
* 适用平台 MSPM0G3507
|
||||
* 店铺链接 https://seekfree.taobao.com/
|
||||
*
|
||||
* 修改记录
|
||||
* 日期 作者 备注
|
||||
* 2025-06-1 SeekFree first version
|
||||
********************************************************************************************************************/
|
||||
|
||||
#include "zf_common_function.h"
|
||||
#include "zf_common_interrupt.h"
|
||||
|
||||
#include "zf_common_clock.h"
|
||||
|
||||
uint32 system_clock = SYSTEM_CLOCK_80M; // 系统时钟信息
|
||||
uint32 bus_clock = SYSTEM_CLOCK_80M / 2; // 总线时钟信息
|
||||
extern void system_delay_init (void);
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 核心时钟恢复初始设置
|
||||
// 参数说明 void
|
||||
// 返回参数 void
|
||||
// 使用示例 clock_reset();
|
||||
// 备注信息 本函数在文件内部调用 用户不用关注 也不可修改
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
static void clock_reset (void)
|
||||
{
|
||||
DL_GPIO_reset(GPIOA);
|
||||
DL_GPIO_reset(GPIOB);
|
||||
DL_GPIO_enablePower(GPIOA);
|
||||
DL_GPIO_enablePower(GPIOB);
|
||||
|
||||
DL_TimerA_reset(TIMA0);
|
||||
DL_TimerA_reset(TIMA1);
|
||||
DL_TimerA_enablePower(TIMA0);
|
||||
DL_TimerA_enablePower(TIMA1);
|
||||
|
||||
DL_TimerG_reset(TIMG0 );
|
||||
DL_TimerG_reset(TIMG6 );
|
||||
DL_TimerG_reset(TIMG7 );
|
||||
DL_TimerG_reset(TIMG8 );
|
||||
DL_TimerG_reset(TIMG12);
|
||||
DL_TimerG_enablePower(TIMG0 );
|
||||
DL_TimerG_enablePower(TIMG6 );
|
||||
DL_TimerG_enablePower(TIMG7 );
|
||||
DL_TimerG_enablePower(TIMG8 );
|
||||
DL_TimerG_enablePower(TIMG12);
|
||||
|
||||
DL_UART_Main_reset(UART0);
|
||||
DL_UART_Main_reset(UART1);
|
||||
DL_UART_Main_reset(UART2);
|
||||
DL_UART_Main_reset(UART3);
|
||||
DL_UART_Main_enablePower(UART0);
|
||||
DL_UART_Main_enablePower(UART1);
|
||||
DL_UART_Main_enablePower(UART2);
|
||||
DL_UART_Main_enablePower(UART3);
|
||||
|
||||
DL_SPI_reset(SPI0);
|
||||
DL_SPI_reset(SPI1);
|
||||
DL_SPI_enablePower(SPI0);
|
||||
DL_SPI_enablePower(SPI1);
|
||||
|
||||
DL_ADC12_reset(ADC0);
|
||||
DL_ADC12_reset(ADC1);
|
||||
DL_ADC12_enablePower(ADC0);
|
||||
DL_ADC12_enablePower(ADC1);
|
||||
|
||||
DL_VREF_reset(VREF);
|
||||
DL_VREF_enablePower(VREF);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 核心时钟设置
|
||||
// 参数说明 clock 时钟频率 推荐使用 zf_common_clock.h 中 system_clock_enum 定义的选项
|
||||
// 返回参数 void
|
||||
// 使用示例 clock_set_freq(clock);
|
||||
// 备注信息 本函数在文件内部调用 用户不用关注 也不可修改
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
static void clock_set_freq (uint32 clock)
|
||||
{
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 核心时钟初始化
|
||||
// 参数说明 clock 时钟频率 推荐使用 zf_common_clock.h 中 system_clock_enum 定义的选项
|
||||
// 返回参数 void
|
||||
// 使用示例 clock_init(SYSTEM_CLOCK_80M);
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void clock_init (uint32 clock)
|
||||
{
|
||||
clock_reset();
|
||||
SYSCFG_DL_init();
|
||||
interrupt_init();
|
||||
system_delay_init();
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*********************************************************************************************************************
|
||||
* MSPM0G3507 Opensource Library 即(MSPM0G3507 开源库)是一个基于官方 SDK 接口的第三方开源库
|
||||
* Copyright (c) 2022 SEEKFREE 逐飞科技
|
||||
*
|
||||
* 本文件是 MSPM0G3507 开源库的一部分
|
||||
*
|
||||
* MSPM0G3507 开源库 是免费软件
|
||||
* 您可以根据自由软件基金会发布的 GPL(GNU General Public License,即 GNU通用公共许可证)的条款
|
||||
* 即 GPL 的第3版(即 GPL3.0)或(您选择的)任何后来的版本,重新发布和/或修改它
|
||||
*
|
||||
* 本开源库的发布是希望它能发挥作用,但并未对其作任何的保证
|
||||
* 甚至没有隐含的适销性或适合特定用途的保证
|
||||
* 更多细节请参见 GPL
|
||||
*
|
||||
* 您应该在收到本开源库的同时收到一份 GPL 的副本
|
||||
* 如果没有,请参阅<https://www.gnu.org/licenses/>
|
||||
*
|
||||
* 额外注明:
|
||||
* 本开源库使用 GPL3.0 开源许可证协议 以上许可申明为译文版本
|
||||
* 许可申明英文版在 libraries/doc 文件夹下的 GPL3_permission_statement.txt 文件中
|
||||
* 许可证副本在 libraries 文件夹下 即该文件夹下的 LICENSE 文件
|
||||
* 欢迎各位使用并传播本程序 但修改内容时必须保留逐飞科技的版权声明(即本声明)
|
||||
*
|
||||
* 文件名称 zf_common_clock
|
||||
* 公司名称 成都逐飞科技有限公司
|
||||
* 版本信息 查看 libraries/doc 文件夹内 version 文件 版本说明
|
||||
* 开发环境 MDK 5.37
|
||||
* 适用平台 MSPM0G3507
|
||||
* 店铺链接 https://seekfree.taobao.com/
|
||||
*
|
||||
* 修改记录
|
||||
* 日期 作者 备注
|
||||
* 2025-06-1 SeekFree first version
|
||||
********************************************************************************************************************/
|
||||
|
||||
#ifndef _zf_common_clock_h_
|
||||
#define _zf_common_clock_h_
|
||||
|
||||
#include "zf_common_typedef.h"
|
||||
|
||||
#define BOARD_XTAL_FREQ ( 40000000 ) // 晶振频率 如果自己用的不是这个频率就修改这里 UM 定义范围为 4-24Mhz
|
||||
#define XTAL_STARTUP_TIMEOUT ( 0x0F00 ) // 晶振就绪等待超时时长
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SYSTEM_CLOCK_80M = 80000000, // 80Mhz
|
||||
}system_clock_enum;
|
||||
|
||||
extern uint32 system_clock; // 全局变量 系统时钟信息 SYSTICK AHB
|
||||
extern uint32 bus_clock; // 全局变量 总线时钟信息 APB1 APB2
|
||||
|
||||
void clock_init (uint32 clock); // 核心时钟初始化
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,542 @@
|
||||
/*********************************************************************************************************************
|
||||
* MSPM0G3507 Opensource Library 即(MSPM0G3507 开源库)是一个基于官方 SDK 接口的第三方开源库
|
||||
* Copyright (c) 2022 SEEKFREE 逐飞科技
|
||||
*
|
||||
* 本文件是 MSPM0G3507 开源库的一部分
|
||||
*
|
||||
* MSPM0G3507 开源库 是免费软件
|
||||
* 您可以根据自由软件基金会发布的 GPL(GNU General Public License,即 GNU通用公共许可证)的条款
|
||||
* 即 GPL 的第3版(即 GPL3.0)或(您选择的)任何后来的版本,重新发布和/或修改它
|
||||
*
|
||||
* 本开源库的发布是希望它能发挥作用,但并未对其作任何的保证
|
||||
* 甚至没有隐含的适销性或适合特定用途的保证
|
||||
* 更多细节请参见 GPL
|
||||
*
|
||||
* 您应该在收到本开源库的同时收到一份 GPL 的副本
|
||||
* 如果没有,请参阅<https://www.gnu.org/licenses/>
|
||||
*
|
||||
* 额外注明:
|
||||
* 本开源库使用 GPL3.0 开源许可证协议 以上许可申明为译文版本
|
||||
* 许可申明英文版在 libraries/doc 文件夹下的 GPL3_permission_statement.txt 文件中
|
||||
* 许可证副本在 libraries 文件夹下 即该文件夹下的 LICENSE 文件
|
||||
* 欢迎各位使用并传播本程序 但修改内容时必须保留逐飞科技的版权声明(即本声明)
|
||||
*
|
||||
* 文件名称 zf_common_debug
|
||||
* 公司名称 成都逐飞科技有限公司
|
||||
* 版本信息 查看 libraries/doc 文件夹内 version 文件 版本说明
|
||||
* 开发环境 MDK 5.37
|
||||
* 适用平台 MSPM0G3507
|
||||
* 店铺链接 https://seekfree.taobao.com/
|
||||
*
|
||||
* 修改记录
|
||||
* 日期 作者 备注
|
||||
* 2025-06-1 SeekFree first version
|
||||
********************************************************************************************************************/
|
||||
|
||||
#include "zf_common_fifo.h"
|
||||
#include "zf_common_interrupt.h"
|
||||
|
||||
#include "zf_driver_gpio.h"
|
||||
//#include "zf_driver_pwm.h"
|
||||
#include "zf_driver_uart.h"
|
||||
|
||||
#include "zf_common_debug.h"
|
||||
|
||||
uint8 debug_uart_buffer[DEBUG_RING_BUFFER_LEN]; // 数据存放数组
|
||||
uint8 debug_uart_data = 0;
|
||||
fifo_struct debug_uart_fifo;
|
||||
|
||||
static debug_output_struct debug_output_info;
|
||||
static volatile uint8 zf_debug_init_flag = 0;
|
||||
static volatile uint8 zf_debug_assert_enable = 1;
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 debug 软延时函数 在 120MHz 下是一秒多的时间 各单片机需要根据各自时钟试验
|
||||
// 参数说明 void
|
||||
// 返回参数 void
|
||||
// 使用示例 debug_delay();
|
||||
// 备注信息 本函数在文件内部调用 用户不用关注 也不可修改
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
static void debug_delay (void)
|
||||
{
|
||||
vuint32 loop_1 = 0, loop_2 = 0;
|
||||
for(loop_1 = 0; 0xFF >= loop_1; loop_1 ++)
|
||||
{
|
||||
for(loop_2 = 0; 0xFFFF >= loop_2; loop_2 ++)
|
||||
{
|
||||
__NOP();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 debug 保护处理 主要是防止断言后出现信号维持而导致硬件失控
|
||||
// 参数说明 void
|
||||
// 返回参数 void
|
||||
// 使用示例 debug_protective_handler();
|
||||
// 备注信息 本函数在文件内部调用 用户不用关注 也不可修改
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
static void debug_protective_handler (void)
|
||||
{
|
||||
// const pwm_channel_enum pin_list[] =
|
||||
// {
|
||||
// };
|
||||
|
||||
// uint32 loop_temp = 0;
|
||||
|
||||
// for(loop_temp = 0; 79 > loop_temp; loop_temp ++)
|
||||
// {
|
||||
// uint8 gpio_group_index = (((pin_list[loop_temp] & 0xFE0000) >> 17));
|
||||
// uint8 gpio_bit_index = ((pin_list[loop_temp] & 0x01F000) >> 12);
|
||||
// uint32 gpio_af_state = 0;
|
||||
// if(7 >= gpio_bit_index)
|
||||
// {
|
||||
// gpio_af_state = (gpio_group[gpio_group_index]->AFRL);
|
||||
// gpio_af_state = (gpio_af_state & (GPIO_AFRL_AFRY(0xF) << (gpio_bit_index * 4)));
|
||||
// gpio_af_state = (gpio_af_state >> (gpio_bit_index * 4));
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// gpio_af_state = (gpio_group[gpio_group_index]->AFRH);
|
||||
// gpio_af_state = (gpio_af_state & (GPIO_AFRH_AFRY(0xF) << ((gpio_bit_index - 8) * 4)));
|
||||
// gpio_af_state = (gpio_af_state >> ((gpio_bit_index - 8) * 4));
|
||||
// }
|
||||
// if(gpio_af_state == ((pin_list[loop_temp] & 0xF00) >> 8))
|
||||
// {
|
||||
// gpio_init((gpio_pin_enum)((pin_list[loop_temp] & 0xFFF000) >> 12), GPO, 0, GPO_PUSH_PULL);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 debug 串口输出接口
|
||||
// 参数说明 *str 需要输出的字符串
|
||||
// 返回参数 void
|
||||
// 使用示例 debug_uart_str_output("Log message");
|
||||
// 备注信息 本函数在文件内部调用 用户不用关注 也不可修改
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
static void debug_uart_str_output (const char *str)
|
||||
{
|
||||
uart_write_string(DEBUG_UART_INDEX, str);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 debug 输出接口
|
||||
// 参数说明 *type log 类型
|
||||
// 参数说明 *file 文件名
|
||||
// 参数说明 line 目标行数
|
||||
// 参数说明 *str 信息
|
||||
// 返回参数 void
|
||||
// 使用示例 debug_output("Log message", file, line, str);
|
||||
// 备注信息 本函数在文件内部调用 用户不用关注 也不可修改
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
static void debug_output (char *type, char *file, int line, char *str)
|
||||
{
|
||||
char *file_str;
|
||||
vuint16 i = 0, j = 0;
|
||||
vint16 len_origin = 0;
|
||||
vint16 show_len = 0;
|
||||
vint16 show_line_index = 0;
|
||||
len_origin = strlen(file);
|
||||
|
||||
char output_buffer[256];
|
||||
char file_path_buffer[64];
|
||||
|
||||
if(debug_output_info.type_index)
|
||||
{
|
||||
debug_output_info.output_screen_clear();
|
||||
}
|
||||
|
||||
if(zf_debug_init_flag)
|
||||
{
|
||||
if(debug_output_info.type_index)
|
||||
{
|
||||
// 需要分行将文件的路径和行数输出
|
||||
// <不输出完整路径 只输出一级目录 例如 src/main.c>
|
||||
// 输出 line : xxxx
|
||||
debug_output_info.output_screen(0, show_line_index ++, type);
|
||||
|
||||
file_str = file;
|
||||
len_origin = strlen(file);
|
||||
show_len = (debug_output_info.display_x_max / debug_output_info.font_x_size);
|
||||
|
||||
while('\0' != *file_str ++);
|
||||
|
||||
// 只取一级目录 如果文件放在盘符根目录 或者 MDK 的工程根目录 就会直接输出当前目录
|
||||
for(j = 0; (2 > j) && (0 <= len_origin); len_origin --) // 查找两个 '/'
|
||||
{
|
||||
file_str --;
|
||||
if(('/' == *file_str) || (0x5C == *file_str))
|
||||
{
|
||||
j ++;
|
||||
}
|
||||
}
|
||||
|
||||
// 文件路径保存到数组中
|
||||
if(0 <= len_origin)
|
||||
{
|
||||
file_str ++;
|
||||
sprintf(output_buffer, "file: %s", file_str);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(0 == j)
|
||||
{
|
||||
sprintf(output_buffer, "file: mdk/%s", file_str);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(output_buffer, "file: %s", file_str);
|
||||
}
|
||||
}
|
||||
|
||||
// 屏幕显示路径
|
||||
for(i = 0; i < ((strlen(output_buffer) / show_len) + 1); i ++)
|
||||
{
|
||||
for(j = 0; j < show_len; j ++)
|
||||
{
|
||||
if(strlen(output_buffer) < (j + i * show_len))
|
||||
{
|
||||
break;
|
||||
}
|
||||
file_path_buffer[j] = output_buffer[j + i * show_len];
|
||||
}
|
||||
|
||||
file_path_buffer[j] = '\0'; // 末尾添加\0
|
||||
|
||||
debug_output_info.output_screen(0, debug_output_info.font_y_size * show_line_index ++, file_path_buffer);
|
||||
}
|
||||
|
||||
// 屏幕显示行号
|
||||
sprintf(output_buffer, "line: %d", line);
|
||||
debug_output_info.output_screen(0, debug_output_info.font_y_size * show_line_index ++, output_buffer);
|
||||
|
||||
// 屏幕显示 Log 如果有的话
|
||||
if(NULL != str)
|
||||
{
|
||||
for(i = 0; i < ((strlen(str) / show_len) + 1); i ++)
|
||||
{
|
||||
for(j = 0; j < show_len; j ++)
|
||||
{
|
||||
if(strlen(str) < (j + i * show_len))
|
||||
{
|
||||
break;
|
||||
}
|
||||
file_path_buffer[j] = str[j + i * show_len];
|
||||
}
|
||||
|
||||
file_path_buffer[j] = '\0'; // 末尾添加\0
|
||||
|
||||
debug_output_info.output_screen(0, debug_output_info.font_y_size * show_line_index ++, file_path_buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
char output_buffer[256];
|
||||
memset(output_buffer, 0, 256);
|
||||
debug_output_info.output_uart(type);
|
||||
if(NULL != str)
|
||||
{
|
||||
sprintf(output_buffer, "\r\nfile %s line %d: %s.\r\n", file, line, str);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(output_buffer, "\r\nfile %s line %d.\r\n", file, line);
|
||||
}
|
||||
debug_output_info.output_uart(output_buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 调试串口发送缓冲区
|
||||
// 参数说明 *buff 读出数据存放的数组指针
|
||||
// 参数说明 len 需要发送的长度
|
||||
// 返回参数 uint32 剩余未发送的长度
|
||||
// 使用示例
|
||||
// 备注信息 本函数需要开启 DEBUG_UART_USE_INTERRUPT 宏定义才可使用
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
uint32 debug_send_buffer(const uint8 *buff, uint32 len)
|
||||
{
|
||||
uart_write_buffer(DEBUG_UART_INDEX, buff, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 读取 debug 环形缓冲区数据
|
||||
// 参数说明 *buff 读出数据存放的数组指针
|
||||
// 参数说明 len 需要读取的长度
|
||||
// 返回参数 uint32 读出数据的实际长度
|
||||
// 使用示例
|
||||
// 备注信息 本函数需要开启 DEBUG_UART_USE_INTERRUPT 宏定义才可使用
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
uint32 debug_read_ring_buffer (uint8 *buff, uint32 len)
|
||||
{
|
||||
fifo_read_buffer(&debug_uart_fifo, buff, &len, FIFO_READ_AND_CLEAN);
|
||||
return len;
|
||||
}
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 debug 串口中断处理函数 isr.c 中对应串口中断服务函数调用
|
||||
// 参数说明 void
|
||||
// 返回参数 void
|
||||
// 使用示例 debug_interrupr_handler();
|
||||
// 备注信息 本函数需要开启 DEBUG_UART_USE_INTERRUPT 宏定义才可使用
|
||||
// 并且本函数默认放置在 UART1 的串口接收中断处理处
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void debug_interrupr_handler (void)
|
||||
{
|
||||
if(DEBUG_UART_USE_INTERRUPT)
|
||||
{
|
||||
uart_query_byte(DEBUG_UART_INDEX, &debug_uart_data); // 读取串口数据
|
||||
fifo_write_buffer(&debug_uart_fifo, &debug_uart_data, 1); // 存入 FIFO
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------- // printf 重定向 此部分不允许用户更改
|
||||
#if !defined(__MICROLIB)
|
||||
//不使用微库的话就需要添加下面的函数
|
||||
#if (__ARMCLIB_VERSION <= 6000000)
|
||||
//如果编译器是AC5 就定义下面这个结构体
|
||||
struct __FILE
|
||||
{
|
||||
int handle;
|
||||
};
|
||||
#endif
|
||||
|
||||
FILE __stdout;
|
||||
|
||||
//定义_sys_exit()以避免使用半主机模式
|
||||
void _sys_exit(int x)
|
||||
{
|
||||
x = x;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__ICCARM__)
|
||||
#define PUTCHAR_PROTOTYPE int32_t fputc (int32_t ch, FILE *f)
|
||||
#define GETCHAR_PROTOTYPE int32_t fgetc (FILE *f)
|
||||
#elif defined(__GNUC__)
|
||||
#define PUTCHAR_PROTOTYPE int32_t __io_putchar (int32_t ch)
|
||||
#define GETCHAR_PROTOTYPE int32_t __io_getchar ()
|
||||
#endif
|
||||
|
||||
#if defined(__ICCARM__)
|
||||
PUTCHAR_PROTOTYPE
|
||||
{
|
||||
if(zf_debug_init_flag)
|
||||
{
|
||||
uart_write_byte(DEBUG_UART_INDEX, (ch & 0xFF));
|
||||
}
|
||||
return ch;
|
||||
}
|
||||
|
||||
GETCHAR_PROTOTYPE
|
||||
{
|
||||
int data = 0;
|
||||
if(zf_debug_init_flag)
|
||||
{
|
||||
data = uart_read_byte(DEBUG_UART_INDEX);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
#else
|
||||
int32_t fputc (int32_t ch, FILE* f)
|
||||
{
|
||||
if(zf_debug_init_flag)
|
||||
{
|
||||
uart_write_byte(DEBUG_UART_INDEX, (ch & 0xFF));
|
||||
}
|
||||
|
||||
return ch;
|
||||
}
|
||||
|
||||
int fgetc(FILE *f)
|
||||
{
|
||||
uint8 data = 0;
|
||||
if(zf_debug_init_flag)
|
||||
{
|
||||
uart_read_byte(DEBUG_UART_INDEX, &data);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
#endif
|
||||
//------------------------------------------------------------------------- // printf 重定向 此部分不允许用户更改
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 启用断言
|
||||
// 参数说明 void
|
||||
// 返回参数 void
|
||||
// 使用示例 debug_assert_enable();
|
||||
// 备注信息 断言默认开启 建议开启断言
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void debug_assert_enable (void)
|
||||
{
|
||||
zf_debug_assert_enable = 1;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 禁用断言
|
||||
// 参数说明 void
|
||||
// 返回参数 void
|
||||
// 使用示例 debug_assert_disable();
|
||||
// 备注信息 断言默认开启 不建议禁用断言
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void debug_assert_disable (void)
|
||||
{
|
||||
zf_debug_assert_enable = 0;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 debug 断言处理函数
|
||||
// 参数说明 pass 判断是否触发断言
|
||||
// 参数说明 *file 文件名
|
||||
// 参数说明 line 目标行数
|
||||
// 返回参数 void
|
||||
// 使用示例 zf_assert(0);
|
||||
// 备注信息 这个函数不是直接调用的 此部分不允许用户更改
|
||||
// 使用 zf_commmon_debug.h 中的 zf_assert(x) 接口
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void debug_assert_handler (uint8 pass, char *file, int line)
|
||||
{
|
||||
do
|
||||
{
|
||||
if(pass || !zf_debug_assert_enable)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
static uint8 assert_nest_index = 0;
|
||||
|
||||
if(0 != assert_nest_index)
|
||||
{
|
||||
while(1);
|
||||
}
|
||||
assert_nest_index ++;
|
||||
|
||||
interrupt_global_disable();
|
||||
debug_protective_handler();
|
||||
|
||||
while(1)
|
||||
{
|
||||
// 如果代码跳转到这里停住了
|
||||
// 一般你的函数参数传递出错了
|
||||
// 或者你自己调用的 zf_assert(x) 接口处报错了
|
||||
|
||||
// 如果调用了 debug_init 初始化了 log 输出
|
||||
// 就在对应串口输出去查看是哪个文件的哪一行报错
|
||||
|
||||
// 如果没有初始化 debug
|
||||
// 那就看看这个 file 的字符串值和 line 的行数
|
||||
// 那代表报错的文件路径名称和对应报错行数
|
||||
|
||||
// 再去调试看看是为什么参数出错
|
||||
|
||||
debug_output("Assert error", file, line, NULL);
|
||||
debug_delay();
|
||||
}
|
||||
}while(0);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 debug 调试信息处理函数
|
||||
// 参数说明 pass 判断是否触发断言
|
||||
// 参数说明 *str 输出的信息
|
||||
// 参数说明 *file 文件名
|
||||
// 参数说明 line 目标行数
|
||||
// 返回参数 void
|
||||
// 使用示例 zf_log(0, "Log Message");
|
||||
// 备注信息 这个函数不是直接调用的 此部分不允许用户更改
|
||||
// 使用 zf_commmon_debug.h 中的 zf_log(x, str) 接口
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void debug_log_handler (uint8 pass, char *str, char *file, int line)
|
||||
{
|
||||
do
|
||||
{
|
||||
if(pass)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if(zf_debug_init_flag)
|
||||
{
|
||||
debug_output("Log message", file, line, str);
|
||||
// printf("Log message from %s line %d :\"%s\".\r\n", file, line, str);
|
||||
}
|
||||
}while(0);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 debug 输出绑定信息初始化
|
||||
// 参数说明 *info debug 输出的信息结构体
|
||||
// 返回参数 void
|
||||
// 使用示例 debug_output_struct_init(info);
|
||||
// 备注信息 这个函数一般不由用户调用
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void debug_output_struct_init (debug_output_struct *info)
|
||||
{
|
||||
info->type_index = 0;
|
||||
|
||||
info->display_x_max = 0xFFFF;
|
||||
info->display_y_max = 0xFFFF;
|
||||
|
||||
info->font_x_size = 0xFF;
|
||||
info->font_y_size = 0xFF;
|
||||
|
||||
info->output_uart = NULL;
|
||||
info->output_screen = NULL;
|
||||
info->output_screen_clear = NULL;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 debug 输出绑定初始化
|
||||
// 参数说明 *info debug 输出的信息结构体
|
||||
// 返回参数 void
|
||||
// 使用示例 debug_output_init(info);
|
||||
// 备注信息 这个函数一般不由用户调用
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void debug_output_init (debug_output_struct *info)
|
||||
{
|
||||
debug_output_info.type_index = info->type_index;
|
||||
|
||||
debug_output_info.display_x_max = info->display_x_max;
|
||||
debug_output_info.display_y_max = info->display_y_max;
|
||||
|
||||
debug_output_info.font_x_size = info->font_x_size;
|
||||
debug_output_info.font_y_size = info->font_y_size;
|
||||
|
||||
debug_output_info.output_uart = info->output_uart;
|
||||
debug_output_info.output_screen = info->output_screen;
|
||||
debug_output_info.output_screen_clear = info->output_screen_clear;
|
||||
|
||||
zf_debug_init_flag = 1;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 debug 串口初始化
|
||||
// 参数说明 void
|
||||
// 返回参数 void
|
||||
// 使用示例 debug_init();
|
||||
// 备注信息 开源库示例默认调用 但默认禁用中断接收
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void debug_init (void)
|
||||
{
|
||||
debug_output_struct info;
|
||||
debug_output_struct_init(&info);
|
||||
info.output_uart = debug_uart_str_output;
|
||||
debug_output_init(&info);
|
||||
|
||||
uart_init(
|
||||
DEBUG_UART_INDEX, // 在 zf_common_debug.h 中查看对应值
|
||||
DEBUG_UART_BAUDRATE, // 在 zf_common_debug.h 中查看对应值
|
||||
DEBUG_UART_TX_PIN, // 在 zf_common_debug.h 中查看对应值
|
||||
DEBUG_UART_RX_PIN); // 在 zf_common_debug.h 中查看对应值
|
||||
|
||||
#if DEBUG_UART_USE_INTERRUPT // 条件编译 只有在启用串口中断才编译
|
||||
fifo_init(&debug_uart_fifo, FIFO_DATA_8BIT, debug_uart_buffer, DEBUG_RING_BUFFER_LEN);
|
||||
uart_set_interrupt_config(DEBUG_UART_INDEX, UART_INTERRUPT_CONFIG_RX_ENABLE);
|
||||
interrupt_set_priority(DEBUG_UART_PRIORITY, DEBUG_UART_PRIORITY0_7); // 设置对应 UART_INDEX 的中断优先级为 0
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
/*********************************************************************************************************************
|
||||
* MSPM0G3507 Opensource Library 即(MSPM0G3507 开源库)是一个基于官方 SDK 接口的第三方开源库
|
||||
* Copyright (c) 2022 SEEKFREE 逐飞科技
|
||||
*
|
||||
* 本文件是 MSPM0G3507 开源库的一部分
|
||||
*
|
||||
* MSPM0G3507 开源库 是免费软件
|
||||
* 您可以根据自由软件基金会发布的 GPL(GNU General Public License,即 GNU通用公共许可证)的条款
|
||||
* 即 GPL 的第3版(即 GPL3.0)或(您选择的)任何后来的版本,重新发布和/或修改它
|
||||
*
|
||||
* 本开源库的发布是希望它能发挥作用,但并未对其作任何的保证
|
||||
* 甚至没有隐含的适销性或适合特定用途的保证
|
||||
* 更多细节请参见 GPL
|
||||
*
|
||||
* 您应该在收到本开源库的同时收到一份 GPL 的副本
|
||||
* 如果没有,请参阅<https://www.gnu.org/licenses/>
|
||||
*
|
||||
* 额外注明:
|
||||
* 本开源库使用 GPL3.0 开源许可证协议 以上许可申明为译文版本
|
||||
* 许可申明英文版在 libraries/doc 文件夹下的 GPL3_permission_statement.txt 文件中
|
||||
* 许可证副本在 libraries 文件夹下 即该文件夹下的 LICENSE 文件
|
||||
* 欢迎各位使用并传播本程序 但修改内容时必须保留逐飞科技的版权声明(即本声明)
|
||||
*
|
||||
* 文件名称 zf_common_debug
|
||||
* 公司名称 成都逐飞科技有限公司
|
||||
* 版本信息 查看 libraries/doc 文件夹内 version 文件 版本说明
|
||||
* 开发环境 MDK 5.37
|
||||
* 适用平台 MSPM0G3507
|
||||
* 店铺链接 https://seekfree.taobao.com/
|
||||
*
|
||||
* 修改记录
|
||||
* 日期 作者 备注
|
||||
* 2025-06-1 SeekFree first version
|
||||
********************************************************************************************************************/
|
||||
|
||||
#ifndef _zf_common_debug_h_
|
||||
#define _zf_common_debug_h_
|
||||
|
||||
#include "zf_common_typedef.h"
|
||||
|
||||
#define DEBUG_UART_INDEX ( UART_0 ) // 指定 debug uart 所使用的的串口
|
||||
#define DEBUG_UART_BAUDRATE ( 115200 ) // 指定 debug uart 所使用的的串口波特率
|
||||
#define DEBUG_UART_TX_PIN ( UART0_TX_A10 ) // 指定 debug uart 所使用的的串口引脚
|
||||
#define DEBUG_UART_RX_PIN ( UART0_RX_A11 ) // 指定 debug uart 所使用的的串口引脚
|
||||
|
||||
|
||||
#define DEBUG_UART_USE_INTERRUPT ( 1 ) // 是否启用 debug uart 接收中断
|
||||
|
||||
#if DEBUG_UART_USE_INTERRUPT // 条件编译 只有在启用串口中断才编译
|
||||
#define DEBUG_UART_PRIORITY (UART0_INT_IRQn) // 对应串口中断的中断编号 mspm0g350x IRQn_Type 枚举体
|
||||
#define DEBUG_UART_PRIORITY0_7 (1) // 中断优先级
|
||||
|
||||
#endif
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 断言
|
||||
// 参数说明 x 判断是否触发断言 0-触发断言 1-不触发断言
|
||||
// 返回参数 void
|
||||
// 使用示例 zf_assert(0);
|
||||
// 备注信息 一般用于参数判断 zf_assert(0) 就断言报错
|
||||
// 默认情况下会在 Debug UART 输出
|
||||
// 但如果使用开源库内屏幕接口初始化了屏幕 则会在屏幕上显示
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
#define zf_assert(x) ( debug_assert_handler((x), __FILE__, __LINE__) )
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 Log 信息输出
|
||||
// 参数说明 x 判断是否触发输出 0-触发输出 1-不触发输出
|
||||
// 参数说明 *str 需要输出的 Log 信息
|
||||
// 返回参数 void
|
||||
// 使用示例 zf_log(0, "Error");
|
||||
// 备注信息 调试信息输出 用来做一些报错或者警告之类的输出
|
||||
// 默认情况下会在 Debug UART 输出
|
||||
// 但如果使用开源库内屏幕接口初始化了屏幕 则会在屏幕上显示
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
#define zf_log(x, str) ( debug_log_handler((x), (str), __FILE__, __LINE__) )
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16 type_index; // 显示类型 0-串口 1-屏幕
|
||||
|
||||
uint16 display_x_max; // 屏幕显示范围
|
||||
uint16 display_y_max; // 屏幕显示范围
|
||||
|
||||
uint8 font_x_size; // 屏幕显示字体大小
|
||||
uint8 font_y_size; // 屏幕显示字体大小
|
||||
|
||||
void (*output_uart) (const char *str); // 串口显示函数指针
|
||||
void (*output_screen) (uint16 x, uint16 y, const char *str); // 屏幕字符串显示函数指针
|
||||
void (*output_screen_clear) (void); // 屏幕清屏函数指针
|
||||
}debug_output_struct;
|
||||
|
||||
|
||||
uint32 debug_send_buffer (const uint8 *buff, uint32 len); // 调试串口发送缓冲区
|
||||
#define DEBUG_RING_BUFFER_LEN ( 64 ) // 定义环形缓冲区大小 默认 64byte
|
||||
void debug_interrupr_handler (void);
|
||||
uint32 debug_read_ring_buffer (uint8 *buff, uint32 len);
|
||||
|
||||
void debug_assert_enable (void);
|
||||
void debug_assert_disable (void);
|
||||
void debug_assert_handler (uint8 pass, char *file, int line);
|
||||
void debug_log_handler (uint8 pass, char *str, char *file, int line);
|
||||
void debug_output_struct_init (debug_output_struct *info);
|
||||
void debug_output_init (debug_output_struct *info);
|
||||
void debug_init (void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,559 @@
|
||||
/*********************************************************************************************************************
|
||||
* MSPM0G3507 Opensource Library 即(MSPM0G3507 开源库)是一个基于官方 SDK 接口的第三方开源库
|
||||
* Copyright (c) 2022 SEEKFREE 逐飞科技
|
||||
*
|
||||
* 本文件是 MSPM0G3507 开源库的一部分
|
||||
*
|
||||
* MSPM0G3507 开源库 是免费软件
|
||||
* 您可以根据自由软件基金会发布的 GPL(GNU General Public License,即 GNU通用公共许可证)的条款
|
||||
* 即 GPL 的第3版(即 GPL3.0)或(您选择的)任何后来的版本,重新发布和/或修改它
|
||||
*
|
||||
* 本开源库的发布是希望它能发挥作用,但并未对其作任何的保证
|
||||
* 甚至没有隐含的适销性或适合特定用途的保证
|
||||
* 更多细节请参见 GPL
|
||||
*
|
||||
* 您应该在收到本开源库的同时收到一份 GPL 的副本
|
||||
* 如果没有,请参阅<https://www.gnu.org/licenses/>
|
||||
*
|
||||
* 额外注明:
|
||||
* 本开源库使用 GPL3.0 开源许可证协议 以上许可申明为译文版本
|
||||
* 许可申明英文版在 libraries/doc 文件夹下的 GPL3_permission_statement.txt 文件中
|
||||
* 许可证副本在 libraries 文件夹下 即该文件夹下的 LICENSE 文件
|
||||
* 欢迎各位使用并传播本程序 但修改内容时必须保留逐飞科技的版权声明(即本声明)
|
||||
*
|
||||
* 文件名称 zf_common_fifo
|
||||
* 公司名称 成都逐飞科技有限公司
|
||||
* 版本信息 查看 libraries/doc 文件夹内 version 文件 版本说明
|
||||
* 开发环境 MDK 5.37
|
||||
* 适用平台 MSPM0G3507
|
||||
* 店铺链接 https://seekfree.taobao.com/
|
||||
*
|
||||
* 修改记录
|
||||
* 日期 作者 备注
|
||||
* 2025-06-1 SeekFree first version
|
||||
********************************************************************************************************************/
|
||||
|
||||
#include "zf_common_debug.h"
|
||||
|
||||
#include "zf_common_fifo.h"
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 FIFO 头指针位移
|
||||
// 参数说明 *fifo FIFO 对象指针
|
||||
// 参数说明 offset 偏移量
|
||||
// 返回参数 void
|
||||
// 使用示例 fifo_head_offset(fifo, 1);
|
||||
// 备注信息 本函数在文件内部调用 用户不用关注 也不可修改
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
static void fifo_head_offset (fifo_struct *fifo, uint32 offset)
|
||||
{
|
||||
fifo->head += offset;
|
||||
|
||||
while(fifo->max <= fifo->head) // 如果范围超过则减缓冲区大小 直到小于最大缓冲区大小
|
||||
{
|
||||
fifo->head -= fifo->max;
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 FIFO 尾指针位移
|
||||
// 参数说明 *fifo FIFO 对象指针
|
||||
// 参数说明 offset 偏移量
|
||||
// 返回参数 void
|
||||
// 使用示例 fifo_end_offset(fifo, 1);
|
||||
// 备注信息 本函数在文件内部调用 用户不用关注 也不可修改
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
static void fifo_end_offset (fifo_struct *fifo, uint32 offset)
|
||||
{
|
||||
fifo->end += offset;
|
||||
|
||||
while(fifo->max <= fifo->end) // 如果范围超过则减缓冲区大小 直到小于最大缓冲区大小
|
||||
{
|
||||
fifo->end -= fifo->max;
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 FIFO 重置缓冲器
|
||||
// 参数说明 *fifo FIFO 对象指针
|
||||
// 返回参数 void
|
||||
// 使用示例 fifo_clear(fifo);
|
||||
// 备注信息 清空当前 FIFO 对象的内存
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
fifo_state_enum fifo_clear (fifo_struct *fifo)
|
||||
{
|
||||
zf_assert(NULL != fifo);
|
||||
fifo_state_enum return_state = FIFO_SUCCESS; // 操作结果初值
|
||||
do
|
||||
{
|
||||
// if(FIFO_IDLE != fifo->execution) // 判断是否当前 FIFO 是否空闲
|
||||
// {
|
||||
// return_state = FIFO_RESET_UNDO; // 重置操作未完成
|
||||
// break;
|
||||
// }
|
||||
fifo->execution |= FIFO_RESET; // 重置操作置位
|
||||
fifo->head = 0; // 重置 FIFO 所有数值复位
|
||||
fifo->end = 0; // 重置 FIFO 所有数值复位
|
||||
fifo->size = fifo->max; // 重置 FIFO 所有数值复位
|
||||
switch(fifo->type)
|
||||
{
|
||||
case FIFO_DATA_8BIT: memset(fifo->buffer, 0, fifo->max); break;
|
||||
case FIFO_DATA_16BIT: memset(fifo->buffer, 0, fifo->max * 2); break;
|
||||
case FIFO_DATA_32BIT: memset(fifo->buffer, 0, fifo->max * 4); break;
|
||||
}
|
||||
fifo->execution = FIFO_IDLE; // 操作状态复位
|
||||
}while(0);
|
||||
return return_state;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 FIFO 查询当前数据个数
|
||||
// 参数说明 *fifo FIFO 对象指针
|
||||
// 返回参数 uint32 已使用长度
|
||||
// 使用示例 uint32 len = fifo_used(fifo);
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
uint32 fifo_used (fifo_struct *fifo)
|
||||
{
|
||||
zf_assert(fifo != NULL);
|
||||
return (fifo->max - fifo->size); // 返回当前 FIFO 缓冲区中数据个数
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 向 FIFO 中写入数据
|
||||
// 参数说明 *fifo FIFO 对象指针
|
||||
// 参数说明 dat 数据
|
||||
// 返回参数 fifo_state_enum 操作状态
|
||||
// 使用示例 zf_log(fifo_write_element(&fifo, data) == FIFO_SUCCESS, "fifo_write_byte error");
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
fifo_state_enum fifo_write_element (fifo_struct *fifo, uint32 dat)
|
||||
{
|
||||
zf_assert(NULL != fifo);
|
||||
fifo_state_enum return_state = FIFO_SUCCESS; // 操作结果初值
|
||||
|
||||
do
|
||||
{
|
||||
if((FIFO_RESET | FIFO_WRITE) & fifo->execution) // 不在写入与重置状态 避免写入竞争与指向错误
|
||||
{
|
||||
return_state = FIFO_WRITE_UNDO; // 写入操作未完成
|
||||
break;
|
||||
}
|
||||
fifo->execution |= FIFO_WRITE; // 写入操作置位
|
||||
|
||||
if(1 <= fifo->size) // 剩余空间足够装下本次数据
|
||||
{
|
||||
switch(fifo->type)
|
||||
{
|
||||
case FIFO_DATA_8BIT: ((uint8 *)fifo->buffer)[fifo->head] = dat; break;
|
||||
case FIFO_DATA_16BIT: ((uint16 *)fifo->buffer)[fifo->head] = dat; break;
|
||||
case FIFO_DATA_32BIT: ((uint32 *)fifo->buffer)[fifo->head] = dat; break;
|
||||
}
|
||||
fifo_head_offset(fifo, 1); // 头指针偏移
|
||||
fifo->size -= 1; // 缓冲区剩余长度减小
|
||||
}
|
||||
else
|
||||
{
|
||||
return_state = FIFO_SPACE_NO_ENOUGH; // 当前 FIFO 缓冲区满 不能再写入数据 返回空间不足
|
||||
}
|
||||
fifo->execution &= ~FIFO_WRITE; // 写入操作复位
|
||||
}while(0);
|
||||
|
||||
return return_state;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 向 FIFO 中写入数据
|
||||
// 参数说明 *fifo FIFO 对象指针
|
||||
// 参数说明 *dat 数据来源缓冲区指针
|
||||
// 参数说明 length 需要写入的数据长度
|
||||
// 返回参数 fifo_state_enum 操作状态
|
||||
// 使用示例 zf_log(fifo_write_buffer(&fifo, data, 32) == FIFO_SUCCESS, "fifo_write_buffer error");
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
fifo_state_enum fifo_write_buffer (fifo_struct *fifo, void *dat, uint32 length)
|
||||
{
|
||||
zf_assert(NULL != fifo);
|
||||
fifo_state_enum return_state = FIFO_SUCCESS; // 操作结果初值
|
||||
uint32 temp_length = 0;
|
||||
|
||||
do
|
||||
{
|
||||
if(NULL == dat)
|
||||
{
|
||||
return_state = FIFO_BUFFER_NULL; // 用户缓冲区异常
|
||||
break;
|
||||
}
|
||||
if((FIFO_RESET | FIFO_WRITE) & fifo->execution) // 不在写入与重置状态 避免写入竞争与指向错误
|
||||
{
|
||||
return_state = FIFO_WRITE_UNDO; // 写入操作未完成
|
||||
break;
|
||||
}
|
||||
fifo->execution |= FIFO_WRITE; // 写入操作置位
|
||||
|
||||
if(length <= fifo->size) // 剩余空间足够装下本次数据
|
||||
{
|
||||
temp_length = fifo->max - fifo->head; // 计算头指针距离缓冲区尾还有多少空间
|
||||
|
||||
if(length > temp_length) // 距离缓冲区尾长度不足写入数据 环形缓冲区分段操作
|
||||
{
|
||||
switch(fifo->type)
|
||||
{
|
||||
case FIFO_DATA_8BIT:
|
||||
{
|
||||
memcpy(
|
||||
&(((uint8 *)fifo->buffer)[fifo->head]),
|
||||
dat, temp_length); // 拷贝第一段数据
|
||||
fifo_head_offset(fifo, temp_length); // 头指针偏移
|
||||
memcpy(
|
||||
&(((uint8 *)fifo->buffer)[fifo->head]),
|
||||
&(((uint8 *)dat)[temp_length]),
|
||||
length - temp_length); // 拷贝第二段数据
|
||||
fifo_head_offset(fifo, length - temp_length); // 头指针偏移
|
||||
}break;
|
||||
case FIFO_DATA_16BIT:
|
||||
{
|
||||
memcpy(
|
||||
&(((uint16 *)fifo->buffer)[fifo->head]),
|
||||
dat, temp_length * 2); // 拷贝第一段数据
|
||||
fifo_head_offset(fifo, temp_length); // 头指针偏移
|
||||
memcpy(
|
||||
&(((uint16 *)fifo->buffer)[fifo->head]),
|
||||
&(((uint16 *)dat)[temp_length]),
|
||||
(length - temp_length) * 2); // 拷贝第二段数据
|
||||
fifo_head_offset(fifo, length - temp_length); // 头指针偏移
|
||||
}break;
|
||||
case FIFO_DATA_32BIT:
|
||||
{
|
||||
memcpy(
|
||||
&(((uint32 *)fifo->buffer)[fifo->head]),
|
||||
dat, temp_length * 4); // 拷贝第一段数据
|
||||
fifo_head_offset(fifo, temp_length); // 头指针偏移
|
||||
memcpy(
|
||||
&(((uint32 *)fifo->buffer)[fifo->head]),
|
||||
&(((uint32 *)dat)[temp_length]),
|
||||
(length - temp_length) * 4); // 拷贝第二段数据
|
||||
fifo_head_offset(fifo, length - temp_length); // 头指针偏移
|
||||
}break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch(fifo->type)
|
||||
{
|
||||
case FIFO_DATA_8BIT:
|
||||
{
|
||||
memcpy(
|
||||
&(((uint8 *)fifo->buffer)[fifo->head]),
|
||||
dat, length); // 一次完整写入
|
||||
fifo_head_offset(fifo, length); // 头指针偏移
|
||||
}break;
|
||||
case FIFO_DATA_16BIT:
|
||||
{
|
||||
memcpy(
|
||||
&(((uint16 *)fifo->buffer)[fifo->head]),
|
||||
dat, length * 2); // 一次完整写入
|
||||
fifo_head_offset(fifo, length); // 头指针偏移
|
||||
}break;
|
||||
case FIFO_DATA_32BIT:
|
||||
{
|
||||
memcpy(
|
||||
&(((uint32 *)fifo->buffer)[fifo->head]),
|
||||
dat, length * 4); // 一次完整写入
|
||||
fifo_head_offset(fifo, length); // 头指针偏移
|
||||
}break;
|
||||
}
|
||||
}
|
||||
|
||||
fifo->size -= length; // 缓冲区剩余长度减小
|
||||
}
|
||||
else
|
||||
{
|
||||
return_state = FIFO_SPACE_NO_ENOUGH; // 当前 FIFO 缓冲区满 不能再写入数据 返回空间不足
|
||||
}
|
||||
fifo->execution &= ~FIFO_WRITE; // 写入操作复位
|
||||
}while(0);
|
||||
|
||||
return return_state;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 从 FIFO 读取数据
|
||||
// 参数说明 *fifo FIFO 对象指针
|
||||
// 参数说明 *dat 目标缓冲区指针
|
||||
// 参数说明 flag 是否变更 FIFO 状态 可选择是否清空读取的数据
|
||||
// 返回参数 fifo_state_enum 操作状态
|
||||
// 使用示例 zf_log(fifo_read_element(&fifo, data, FIFO_READ_ONLY) == FIFO_SUCCESS, "fifo_read_byte error");
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
fifo_state_enum fifo_read_element (fifo_struct *fifo, void *dat, fifo_operation_enum flag)
|
||||
{
|
||||
zf_assert(NULL != fifo);
|
||||
fifo_state_enum return_state = FIFO_SUCCESS; // 操作结果初值
|
||||
|
||||
do
|
||||
{
|
||||
if(NULL == dat)
|
||||
{
|
||||
return_state = FIFO_BUFFER_NULL; // 用户缓冲区异常
|
||||
}
|
||||
else
|
||||
{
|
||||
if((FIFO_RESET | FIFO_CLEAR) & fifo->execution) // 判断是否当前 FIFO 是否在执行清空或重置操作
|
||||
{
|
||||
return_state = FIFO_READ_UNDO; // 读取操作未完成
|
||||
break;
|
||||
}
|
||||
|
||||
if(1 > fifo_used(fifo))
|
||||
{
|
||||
return_state = FIFO_DATA_NO_ENOUGH; // 缓冲区没有数据 返回数据长度不足
|
||||
break; // 直接退出操作
|
||||
}
|
||||
|
||||
fifo->execution |= FIFO_READ; // 读操作置位
|
||||
switch(fifo->type)
|
||||
{
|
||||
case FIFO_DATA_8BIT: *((uint8 *)dat) = ((uint8 *)fifo->buffer)[fifo->end]; break;
|
||||
case FIFO_DATA_16BIT: *((uint16 *)dat) = ((uint16 *)fifo->buffer)[fifo->end]; break;
|
||||
case FIFO_DATA_32BIT: *((uint32 *)dat) = ((uint32 *)fifo->buffer)[fifo->end]; break;
|
||||
}
|
||||
fifo->execution &= ~FIFO_READ; // 读操作复位
|
||||
}
|
||||
|
||||
if(FIFO_READ_AND_CLEAN == flag) // 如果选择读取并更改 FIFO 状态
|
||||
{
|
||||
if((FIFO_RESET | FIFO_CLEAR | FIFO_READ) == fifo->execution) // 不在 重置 清空 读取 状态 避免异常
|
||||
{
|
||||
return_state = FIFO_CLEAR_UNDO; // 清空操作未完成
|
||||
break;
|
||||
}
|
||||
fifo->execution |= FIFO_CLEAR; // 清空作置位
|
||||
fifo_end_offset(fifo, 1); // 移动 FIFO 头指针
|
||||
fifo->size += 1; // 释放对应长度空间
|
||||
fifo->execution &= ~FIFO_CLEAR; // 清空作复位
|
||||
}
|
||||
}while(0);
|
||||
|
||||
return return_state;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 从 FIFO 读取数据
|
||||
// 参数说明 *fifo FIFO 对象指针
|
||||
// 参数说明 *dat 目标缓冲区指针
|
||||
// 参数说明 *length 读取的数据长度 如果没有这么多数据这里会被修改
|
||||
// 参数说明 flag 是否变更 FIFO 状态 可选择是否清空读取的数据
|
||||
// 返回参数 fifo_state_enum 操作状态
|
||||
// 使用示例 zf_log(fifo_read_buffer(&fifo, data, &length, FIFO_READ_ONLY) == FIFO_SUCCESS, "fifo_read_buffer error");
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
fifo_state_enum fifo_read_buffer (fifo_struct *fifo, void *dat, uint32 *length, fifo_operation_enum flag)
|
||||
{
|
||||
zf_assert(NULL != fifo);
|
||||
zf_assert(NULL != length);
|
||||
fifo_state_enum return_state = FIFO_SUCCESS; // 操作结果初值
|
||||
uint32 temp_length = 0;
|
||||
uint32 fifo_data_length = 0;
|
||||
|
||||
do
|
||||
{
|
||||
if(NULL == dat)
|
||||
{
|
||||
return_state = FIFO_BUFFER_NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
if((FIFO_RESET | FIFO_CLEAR) & fifo->execution) // 判断是否当前 FIFO 是否在执行清空或重置操作
|
||||
{
|
||||
*length = fifo_data_length; // 纠正读取的长度
|
||||
return_state = FIFO_READ_UNDO; // 读取操作未完成
|
||||
break;
|
||||
}
|
||||
|
||||
fifo_data_length = fifo_used(fifo); // 获取当前数据有多少
|
||||
if(*length > fifo_data_length) // 判断长度是否足够
|
||||
{
|
||||
*length = fifo_data_length; // 纠正读取的长度
|
||||
return_state = FIFO_DATA_NO_ENOUGH; // 标志数据不够
|
||||
if(0 == fifo_data_length) // 如果没有数据 就直接退出
|
||||
{
|
||||
fifo->execution &= ~FIFO_READ; // 读操作复位
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fifo->execution |= FIFO_READ; // 读操作置位
|
||||
temp_length = fifo->max - fifo->end; // 计算尾指针距离缓冲区尾还有多少空间
|
||||
if(*length <= temp_length) // 足够一次性读取完毕
|
||||
{
|
||||
switch(fifo->type)
|
||||
{
|
||||
case FIFO_DATA_8BIT: memcpy(dat, &(((uint8 *)fifo->buffer)[fifo->end]), *length); break;
|
||||
case FIFO_DATA_16BIT: memcpy(dat, &(((uint16 *)fifo->buffer)[fifo->end]), *length * 2); break;
|
||||
case FIFO_DATA_32BIT: memcpy(dat, &(((uint32 *)fifo->buffer)[fifo->end]), *length * 4); break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch(fifo->type)
|
||||
{
|
||||
case FIFO_DATA_8BIT:
|
||||
{
|
||||
memcpy(dat, &(((uint8 *)fifo->buffer)[fifo->end]), temp_length);
|
||||
memcpy(&(((uint8 *)dat)[temp_length]), fifo->buffer, *length - temp_length);
|
||||
}break;
|
||||
case FIFO_DATA_16BIT:
|
||||
{
|
||||
memcpy(dat, &(((uint16 *)fifo->buffer)[fifo->end]), temp_length * 2);
|
||||
memcpy(&(((uint16 *)dat)[temp_length]), fifo->buffer, (*length - temp_length) * 2);
|
||||
}break;
|
||||
case FIFO_DATA_32BIT:
|
||||
{
|
||||
memcpy(dat, &(((uint32 *)fifo->buffer)[fifo->end]), temp_length * 4);
|
||||
memcpy(&(((uint32 *)dat)[temp_length]), fifo->buffer, (*length - temp_length) * 4);
|
||||
}break;
|
||||
}
|
||||
}
|
||||
fifo->execution &= ~FIFO_READ; // 读操作复位
|
||||
}
|
||||
|
||||
if(FIFO_READ_AND_CLEAN == flag) // 如果选择读取并更改 FIFO 状态
|
||||
{
|
||||
if((FIFO_RESET | FIFO_CLEAR | FIFO_READ) == fifo->execution) // 不在 重置 清空 读取 状态 避免异常
|
||||
{
|
||||
return_state = FIFO_CLEAR_UNDO; // 清空操作未完成
|
||||
break;
|
||||
}
|
||||
fifo->execution |= FIFO_CLEAR; // 清空作置位
|
||||
fifo_end_offset(fifo, *length); // 移动 FIFO 头指针
|
||||
fifo->size += *length; // 释放对应长度空间
|
||||
fifo->execution &= ~FIFO_CLEAR; // 清空作复位
|
||||
}
|
||||
}while(0);
|
||||
|
||||
return return_state;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 从 FIFO 尾部读取指定长度 buffer
|
||||
// 参数说明 *fifo FIFO 对象指针
|
||||
// 参数说明 *dat 目标缓冲区指针
|
||||
// 参数说明 *length 读取的数据长度 如果没有这么多数据这里会被修改
|
||||
// 参数说明 flag 是否变更 FIFO 状态 可选择是否清空读取的数据
|
||||
// 返回参数 fifo_state_enum 操作状态
|
||||
// 使用示例 zf_log(fifo_read_tail_buffer(&fifo, data, &length, FIFO_READ_ONLY) == FIFO_SUCCESS, "fifo_read_buffer error");
|
||||
// 备注信息 如果使用 FIFO_READ_AND_CLEAN 操作 将会丢弃所有数据并清空整个 FIFO
|
||||
// 如果使用 FIFO_READ_AND_CLEAN 操作 将会丢弃所有数据并清空整个 FIFO
|
||||
// 如果使用 FIFO_READ_AND_CLEAN 操作 将会丢弃所有数据并清空整个 FIFO
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
fifo_state_enum fifo_read_tail_buffer (fifo_struct *fifo, void *dat, uint32 *length, fifo_operation_enum flag)
|
||||
{
|
||||
zf_assert(NULL != fifo);
|
||||
zf_assert(NULL != length);
|
||||
fifo_state_enum return_state = FIFO_SUCCESS; // 操作结果初值
|
||||
uint32 temp_length = 0;
|
||||
uint32 fifo_data_length = 0;
|
||||
|
||||
do
|
||||
{
|
||||
if(NULL == dat)
|
||||
{
|
||||
return_state = FIFO_BUFFER_NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
if((FIFO_RESET | FIFO_CLEAR | FIFO_WRITE) & fifo->execution) // 判断是否当前 FIFO 是否在执行清空或重置操作
|
||||
{
|
||||
*length = fifo_data_length; // 纠正读取的长度
|
||||
return_state = FIFO_READ_UNDO; // 读取操作未完成
|
||||
break;
|
||||
}
|
||||
|
||||
fifo_data_length = fifo_used(fifo); // 获取当前数据有多少
|
||||
if(*length > fifo_data_length) // 判断长度是否足够
|
||||
{
|
||||
*length = fifo_data_length; // 纠正读取的长度
|
||||
return_state = FIFO_DATA_NO_ENOUGH; // 标志数据不够
|
||||
if(0 == fifo_data_length) // 如果没有数据 就直接退出
|
||||
{
|
||||
fifo->execution &= ~FIFO_READ; // 读操作复位
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fifo->execution |= FIFO_READ; // 读操作置位
|
||||
if((fifo->head > fifo->end) || (fifo->head >= *length))
|
||||
{
|
||||
switch(fifo->type)
|
||||
{
|
||||
case FIFO_DATA_8BIT: memcpy(dat, &(((uint8 *)fifo->buffer)[fifo->head - *length]), *length); break;
|
||||
case FIFO_DATA_16BIT: memcpy(dat, &(((uint16 *)fifo->buffer)[fifo->head - *length]), *length * 2);break;
|
||||
case FIFO_DATA_32BIT: memcpy(dat, &(((uint32 *)fifo->buffer)[fifo->head - *length]), *length * 4);break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
temp_length = *length - fifo->head; // 计算尾指针距离缓冲区尾还有多少空间
|
||||
switch(fifo->type)
|
||||
{
|
||||
case FIFO_DATA_8BIT:
|
||||
{
|
||||
memcpy(dat, &(((uint8 *)fifo->buffer)[fifo->max - temp_length]), temp_length);
|
||||
memcpy(&(((uint8 *)dat)[temp_length]), &(((uint8 *)fifo->buffer)[fifo->head - *length]), (*length - temp_length));
|
||||
}break;
|
||||
case FIFO_DATA_16BIT:
|
||||
{
|
||||
memcpy(dat, &(((uint16 *)fifo->buffer)[fifo->max - temp_length]), temp_length * 2);
|
||||
memcpy(&(((uint16 *)dat)[temp_length]), &(((uint16 *)fifo->buffer)[fifo->head - *length]), (*length - temp_length) * 2);
|
||||
}break;
|
||||
case FIFO_DATA_32BIT:
|
||||
{
|
||||
memcpy(dat, &(((uint32 *)fifo->buffer)[fifo->max - temp_length]), temp_length * 4);
|
||||
memcpy(&(((uint32 *)dat)[temp_length]), &(((uint32 *)fifo->buffer)[fifo->head - *length]), (*length - temp_length) * 4);
|
||||
}break;
|
||||
}
|
||||
}
|
||||
fifo->execution &= ~FIFO_READ; // 读操作复位
|
||||
}
|
||||
|
||||
if(FIFO_READ_AND_CLEAN == flag) // 如果选择读取并更改 FIFO 状态
|
||||
{
|
||||
if((FIFO_RESET | FIFO_CLEAR | FIFO_READ) == fifo->execution) // 不在 重置 清空 读取 状态 避免异常
|
||||
{
|
||||
return_state = FIFO_CLEAR_UNDO; // 清空操作未完成
|
||||
break;
|
||||
}
|
||||
fifo_clear(fifo);
|
||||
}
|
||||
}while(0);
|
||||
|
||||
return return_state;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 FIFO 初始化 挂载对应缓冲区
|
||||
// 参数说明 *fifo FIFO 对象指针
|
||||
// 参数说明 type FIFO 数据位数
|
||||
// 参数说明 *buffer_addr 要挂载的缓冲区
|
||||
// 参数说明 size 缓冲区大小
|
||||
// 返回参数 fifo_state_enum 操作状态
|
||||
// 使用示例 fifo_init(&user_fifo, user_buffer, 64);
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
fifo_state_enum fifo_init (fifo_struct *fifo, fifo_data_type_enum type, void *buffer_addr, uint32 size)
|
||||
{
|
||||
zf_assert(NULL != fifo);
|
||||
fifo_state_enum return_state = FIFO_SUCCESS;
|
||||
do
|
||||
{
|
||||
fifo->buffer = buffer_addr;
|
||||
fifo->execution = FIFO_IDLE;
|
||||
fifo->type = type;
|
||||
fifo->head = 0;
|
||||
fifo->end = 0;
|
||||
fifo->size = size;
|
||||
fifo->max = size;
|
||||
}while(0);
|
||||
return return_state;
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/*********************************************************************************************************************
|
||||
* MSPM0G3507 Opensource Library 即(MSPM0G3507 开源库)是一个基于官方 SDK 接口的第三方开源库
|
||||
* Copyright (c) 2022 SEEKFREE 逐飞科技
|
||||
*
|
||||
* 本文件是 MSPM0G3507 开源库的一部分
|
||||
*
|
||||
* MSPM0G3507 开源库 是免费软件
|
||||
* 您可以根据自由软件基金会发布的 GPL(GNU General Public License,即 GNU通用公共许可证)的条款
|
||||
* 即 GPL 的第3版(即 GPL3.0)或(您选择的)任何后来的版本,重新发布和/或修改它
|
||||
*
|
||||
* 本开源库的发布是希望它能发挥作用,但并未对其作任何的保证
|
||||
* 甚至没有隐含的适销性或适合特定用途的保证
|
||||
* 更多细节请参见 GPL
|
||||
*
|
||||
* 您应该在收到本开源库的同时收到一份 GPL 的副本
|
||||
* 如果没有,请参阅<https://www.gnu.org/licenses/>
|
||||
*
|
||||
* 额外注明:
|
||||
* 本开源库使用 GPL3.0 开源许可证协议 以上许可申明为译文版本
|
||||
* 许可申明英文版在 libraries/doc 文件夹下的 GPL3_permission_statement.txt 文件中
|
||||
* 许可证副本在 libraries 文件夹下 即该文件夹下的 LICENSE 文件
|
||||
* 欢迎各位使用并传播本程序 但修改内容时必须保留逐飞科技的版权声明(即本声明)
|
||||
*
|
||||
* 文件名称 zf_common_fifo
|
||||
* 公司名称 成都逐飞科技有限公司
|
||||
* 版本信息 查看 libraries/doc 文件夹内 version 文件 版本说明
|
||||
* 开发环境 MDK 5.37
|
||||
* 适用平台 MSPM0G3507
|
||||
* 店铺链接 https://seekfree.taobao.com/
|
||||
*
|
||||
* 修改记录
|
||||
* 日期 作者 备注
|
||||
* 2025-06-1 SeekFree first version
|
||||
********************************************************************************************************************/
|
||||
|
||||
#ifndef _zf_common_fifo_h_
|
||||
#define _zf_common_fifo_h_
|
||||
|
||||
#include "zf_common_typedef.h"
|
||||
|
||||
typedef enum
|
||||
{
|
||||
FIFO_SUCCESS, // FIFO 操作成功
|
||||
|
||||
FIFO_RESET_UNDO, // FIFO 重置操作未执行
|
||||
FIFO_CLEAR_UNDO, // FIFO 清空操作未执行
|
||||
FIFO_BUFFER_NULL, // FIFO 用户缓冲区异常
|
||||
FIFO_WRITE_UNDO, // FIFO 写入操作未执行
|
||||
FIFO_SPACE_NO_ENOUGH, // FIFO 写入操作 缓冲区空间不足
|
||||
FIFO_READ_UNDO, // FIFO 读取操作未执行
|
||||
FIFO_DATA_NO_ENOUGH, // FIFO 读取操作 数据长度不足
|
||||
}fifo_state_enum; // FIFO 操作结果
|
||||
|
||||
// 操作逻辑
|
||||
// 整体重置操作 将会强制清空 FIFO 谨慎使用
|
||||
// 数据写入操作 不能在重置以及写入操作时进行
|
||||
// 顺序读取操作 不能在清空和重置操作时进行
|
||||
// 尾部读取操作 不能在清空和重置以及写入操作时进行
|
||||
// 读取清空操作 不能在清空和重置以及读取操作时进行
|
||||
// 这是为了防止中断嵌套导致数据混乱
|
||||
typedef enum
|
||||
{
|
||||
FIFO_IDLE = 0x00, // 空闲状态
|
||||
|
||||
FIFO_RESET = 0x01, // 正在执行重置缓冲区
|
||||
FIFO_CLEAR = 0x02, // 正在执行清空缓冲区
|
||||
FIFO_WRITE = 0x04, // 正在执行写入缓冲区
|
||||
FIFO_READ = 0x08, // 正在执行读取缓冲区
|
||||
}fifo_execution_enum; // FIFO 操作状态 为嵌套使用预留 无法完全避免误操作
|
||||
|
||||
typedef enum
|
||||
{
|
||||
FIFO_READ_AND_CLEAN, // FIFO 读操作模式 读取后清空释放对应缓冲区
|
||||
FIFO_READ_ONLY, // FIFO 读操作模式 仅读取
|
||||
}fifo_operation_enum;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
FIFO_DATA_8BIT, // FIFO 数据位宽 8bit
|
||||
FIFO_DATA_16BIT, // FIFO 数据位宽 16bit
|
||||
FIFO_DATA_32BIT, // FIFO 数据位宽 32bit
|
||||
}fifo_data_type_enum;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
uint8 execution; // 执行步骤
|
||||
fifo_data_type_enum type; // 数据类型
|
||||
void *buffer; // 缓存指针
|
||||
uint32 head; // 缓存头指针 总是指向空的缓存
|
||||
uint32 end; // 缓存尾指针 总是指向非空缓存(缓存全空除外)
|
||||
uint32 size; // 缓存剩余大小
|
||||
uint32 max; // 缓存总大小
|
||||
}fifo_struct;
|
||||
|
||||
fifo_state_enum fifo_clear (fifo_struct *fifo);
|
||||
uint32 fifo_used (fifo_struct *fifo);
|
||||
|
||||
fifo_state_enum fifo_write_element (fifo_struct *fifo, uint32 dat);
|
||||
fifo_state_enum fifo_write_buffer (fifo_struct *fifo, void *dat, uint32 length);
|
||||
fifo_state_enum fifo_read_element (fifo_struct *fifo, void *dat, fifo_operation_enum flag);
|
||||
fifo_state_enum fifo_read_buffer (fifo_struct *fifo, void *dat, uint32 *length, fifo_operation_enum flag);
|
||||
fifo_state_enum fifo_read_tail_buffer (fifo_struct *fifo, void *dat, uint32 *length, fifo_operation_enum flag);
|
||||
|
||||
fifo_state_enum fifo_init (fifo_struct *fifo, fifo_data_type_enum type, void *buffer_addr, uint32 size);
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,67 @@
|
||||
/*********************************************************************************************************************
|
||||
* MSPM0G3507 Opensource Library 即(MSPM0G3507 开源库)是一个基于官方 SDK 接口的第三方开源库
|
||||
* Copyright (c) 2022 SEEKFREE 逐飞科技
|
||||
*
|
||||
* 本文件是 MSPM0G3507 开源库的一部分
|
||||
*
|
||||
* MSPM0G3507 开源库 是免费软件
|
||||
* 您可以根据自由软件基金会发布的 GPL(GNU General Public License,即 GNU通用公共许可证)的条款
|
||||
* 即 GPL 的第3版(即 GPL3.0)或(您选择的)任何后来的版本,重新发布和/或修改它
|
||||
*
|
||||
* 本开源库的发布是希望它能发挥作用,但并未对其作任何的保证
|
||||
* 甚至没有隐含的适销性或适合特定用途的保证
|
||||
* 更多细节请参见 GPL
|
||||
*
|
||||
* 您应该在收到本开源库的同时收到一份 GPL 的副本
|
||||
* 如果没有,请参阅<https://www.gnu.org/licenses/>
|
||||
*
|
||||
* 额外注明:
|
||||
* 本开源库使用 GPL3.0 开源许可证协议 以上许可申明为译文版本
|
||||
* 许可申明英文版在 libraries/doc 文件夹下的 GPL3_permission_statement.txt 文件中
|
||||
* 许可证副本在 libraries 文件夹下 即该文件夹下的 LICENSE 文件
|
||||
* 欢迎各位使用并传播本程序 但修改内容时必须保留逐飞科技的版权声明(即本声明)
|
||||
*
|
||||
* 文件名称 zf_common_font
|
||||
* 公司名称 成都逐飞科技有限公司
|
||||
* 版本信息 查看 libraries/doc 文件夹内 version 文件 版本说明
|
||||
* 开发环境 MDK 5.37
|
||||
* 适用平台 MSPM0G3507
|
||||
* 店铺链接 https://seekfree.taobao.com/
|
||||
*
|
||||
* 修改记录
|
||||
* 日期 作者 备注
|
||||
* 2025-06-1 SeekFree first version
|
||||
********************************************************************************************************************/
|
||||
|
||||
#ifndef _zf_common_font_h_
|
||||
#define _zf_common_font_h_
|
||||
|
||||
#include "zf_common_typedef.h"
|
||||
|
||||
//-------常用颜色----------
|
||||
typedef enum
|
||||
{
|
||||
RGB565_WHITE = (0xFFFF), // 白色
|
||||
RGB565_BLACK = (0x0000), // 黑色
|
||||
RGB565_BLUE = (0x001F), // 蓝色
|
||||
RGB565_PURPLE = (0xF81F), // 紫色
|
||||
RGB565_PINK = (0xFE19), // 粉色
|
||||
RGB565_RED = (0xF800), // 红色
|
||||
RGB565_MAGENTA = (0xF81F), // 品红
|
||||
RGB565_GREEN = (0x07E0), // 绿色
|
||||
RGB565_CYAN = (0x07FF), // 青色
|
||||
RGB565_YELLOW = (0xFFE0), // 黄色
|
||||
RGB565_BROWN = (0xBC40), // 棕色
|
||||
RGB565_GRAY = (0x8430), // 灰色
|
||||
|
||||
RGB565_39C5BB = (0x3616),
|
||||
RGB565_66CCFF = (0x665F),
|
||||
}rgb565_color_enum;
|
||||
|
||||
extern const uint8 ascii_font_8x16[][16];
|
||||
extern const uint8 ascii_font_6x8[][6];
|
||||
extern const uint8 chinese_test[8][16];
|
||||
extern const uint8 oled_16x16_chinese[][16];
|
||||
extern const uint8 gImage_seekfree_logo[38400];
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,929 @@
|
||||
/*********************************************************************************************************************
|
||||
* MSPM0G3507 Opensource Library 即(MSPM0G3507 开源库)是一个基于官方 SDK 接口的第三方开源库
|
||||
* Copyright (c) 2022 SEEKFREE 逐飞科技
|
||||
*
|
||||
* 本文件是 MSPM0G3507 开源库的一部分
|
||||
*
|
||||
* MSPM0G3507 开源库 是免费软件
|
||||
* 您可以根据自由软件基金会发布的 GPL(GNU General Public License,即 GNU通用公共许可证)的条款
|
||||
* 即 GPL 的第3版(即 GPL3.0)或(您选择的)任何后来的版本,重新发布和/或修改它
|
||||
*
|
||||
* 本开源库的发布是希望它能发挥作用,但并未对其作任何的保证
|
||||
* 甚至没有隐含的适销性或适合特定用途的保证
|
||||
* 更多细节请参见 GPL
|
||||
*
|
||||
* 您应该在收到本开源库的同时收到一份 GPL 的副本
|
||||
* 如果没有,请参阅<https://www.gnu.org/licenses/>
|
||||
*
|
||||
* 额外注明:
|
||||
* 本开源库使用 GPL3.0 开源许可证协议 以上许可申明为译文版本
|
||||
* 许可申明英文版在 libraries/doc 文件夹下的 GPL3_permission_statement.txt 文件中
|
||||
* 许可证副本在 libraries 文件夹下 即该文件夹下的 LICENSE 文件
|
||||
* 欢迎各位使用并传播本程序 但修改内容时必须保留逐飞科技的版权声明(即本声明)
|
||||
*
|
||||
* 文件名称 zf_common_function
|
||||
* 公司名称 成都逐飞科技有限公司
|
||||
* 版本信息 查看 libraries/doc 文件夹内 version 文件 版本说明
|
||||
* 开发环境 MDK 5.37
|
||||
* 适用平台 MSPM0G3507
|
||||
* 店铺链接 https://seekfree.taobao.com/
|
||||
*
|
||||
* 修改记录
|
||||
* 日期 作者 备注
|
||||
* 2025-06-1 SeekFree first version
|
||||
********************************************************************************************************************/
|
||||
|
||||
#include "math.h"
|
||||
|
||||
#include "zf_common_debug.h"
|
||||
#include "zf_common_function.h"
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 正弦波转换函数 获取指定采样点
|
||||
// 参数说明 *data_buffer 数据缓冲区
|
||||
// 参数说明 sample_max 采样点数
|
||||
// 参数说明 amplitude_max 最大幅值
|
||||
// 参数说明 offset_degree 偏移相位角
|
||||
// 返回参数 void
|
||||
// 使用示例 func_get_sin_amplitude_table(sound_amplitude, 1024, 512, 270);
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void func_get_sin_amplitude_table (uint32 *data_buffer, uint32 sample_max, uint32 amplitude_max, uint32 offset_degree)
|
||||
{
|
||||
uint32 i = 0, j = 0;
|
||||
double rad = 0.0;
|
||||
double amplitude = 0.0;
|
||||
|
||||
offset_degree = offset_degree % 360;
|
||||
offset_degree = (uint32)((float)offset_degree / 360.0 * sample_max);
|
||||
|
||||
j = amplitude_max / 2; // 偏置幅值 默认为一半最大幅值
|
||||
rad = 3.1415926535898 * 2 / sample_max; // 换算每个采样点的弧度差
|
||||
for(i = offset_degree; i < sample_max + offset_degree; i ++)
|
||||
{
|
||||
amplitude = j * sin(rad * i) + j;
|
||||
*data_buffer = (uint32)amplitude;
|
||||
data_buffer ++;
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 获取整型数的最大公约数 九章算术之更相减损术
|
||||
// 参数说明 num1 数字1
|
||||
// 参数说明 num2 数字2
|
||||
// 返回参数 uint32 最大公约数
|
||||
// 使用示例 return func_get_greatest_common_divisor(144, 36); // 获取 144 与 36 的最大公约数
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
uint32 func_get_greatest_common_divisor (uint32 num1, uint32 num2)
|
||||
{
|
||||
while(num1 != num2)
|
||||
{
|
||||
if(num1 > num2)
|
||||
{
|
||||
num1 = num1 - num2;
|
||||
}
|
||||
if(num1 < num2)
|
||||
{
|
||||
num2 = num2 - num1;
|
||||
}
|
||||
}
|
||||
return num1;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 软件延时
|
||||
// 参数说明 t 延时时间
|
||||
// 返回参数 void
|
||||
// 使用示例 func_soft_delay(100);
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void func_soft_delay (volatile long t)
|
||||
{
|
||||
while(t --);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 字符串转整形数字 数据范围是 [-32768,32767]
|
||||
// 参数说明 *str 传入字符串 可带符号
|
||||
// 返回参数 int32 转换后的数据
|
||||
// 使用示例 int32 dat = func_str_to_int("-100");
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
int32 func_str_to_int (char *str)
|
||||
{
|
||||
zf_assert(NULL != str);
|
||||
uint8 sign = 0; // 标记符号 0-正数 1-负数
|
||||
int32 temp = 0; // 临时计算变量
|
||||
do
|
||||
{
|
||||
if(NULL == str)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if('-' == *str) // 如果第一个字符是负号
|
||||
{
|
||||
sign = 1; // 标记负数
|
||||
str ++;
|
||||
}
|
||||
else if('+' == *str) // 如果第一个字符是正号
|
||||
{
|
||||
str ++;
|
||||
}
|
||||
|
||||
while(('0' <= *str) && ('9' >= *str)) // 确定这是个数字
|
||||
{
|
||||
temp = temp * 10 + ((uint8)(*str) - 0x30); // 计算数值
|
||||
str ++;
|
||||
}
|
||||
|
||||
if(sign)
|
||||
{
|
||||
temp = -temp;
|
||||
}
|
||||
}while(0);
|
||||
return temp;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 整形数字转字符串 数据范围是 [-32768,32767]
|
||||
// 参数说明 *str 字符串指针
|
||||
// 参数说明 number 传入的数据
|
||||
// 返回参数 void
|
||||
// 使用示例 func_int_to_str(data_buffer, -300);
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void func_int_to_str (char *str, int32 number)
|
||||
{
|
||||
zf_assert(NULL != str);
|
||||
uint8 data_temp[16]; // 缓冲区
|
||||
uint8 bit = 0; // 数字位数
|
||||
int32 number_temp = 0;
|
||||
|
||||
do
|
||||
{
|
||||
if(NULL == str)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if(0 > number) // 负数
|
||||
{
|
||||
*str ++ = '-';
|
||||
number = -number;
|
||||
}
|
||||
else if(0 == number) // 或者这是个 0
|
||||
{
|
||||
*str = '0';
|
||||
break;
|
||||
}
|
||||
|
||||
while(0 != number) // 循环直到数值归零
|
||||
{
|
||||
number_temp = number % 10;
|
||||
data_temp[bit ++] = func_abs(number_temp); // 倒序将数值提取出来
|
||||
number /= 10; // 削减被提取的个位数
|
||||
}
|
||||
while(0 != bit) // 提取的数字个数递减处理
|
||||
{
|
||||
*str ++ = (data_temp[bit - 1] + 0x30); // 将数字从倒序数组中倒序取出 变成正序放入字符串
|
||||
bit --;
|
||||
}
|
||||
}while(0);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 字符串转整形数字 数据范围是 [0,65535]
|
||||
// 参数说明 *str 传入字符串 无符号
|
||||
// 返回参数 uint32 转换后的数据
|
||||
// 使用示例 uint32 dat = func_str_to_uint("100");
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
uint32 func_str_to_uint (char *str)
|
||||
{
|
||||
zf_assert(NULL != str);
|
||||
uint32 temp = 0; // 临时计算变量
|
||||
|
||||
do
|
||||
{
|
||||
if(NULL == str)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
while(('0' <= *str) && ('9' >= *str)) // 确定这是个数字
|
||||
{
|
||||
temp = temp * 10 + ((uint8)(*str) - 0x30); // 计算数值
|
||||
str ++;
|
||||
}
|
||||
}while(0);
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 整形数字转字符串 数据范围是 [0,65535]
|
||||
// 参数说明 *str 字符串指针
|
||||
// 参数说明 number 传入的数据
|
||||
// 返回参数 void
|
||||
// 使用示例 func_uint_to_str(data_buffer, 300);
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void func_uint_to_str (char *str, uint32 number)
|
||||
{
|
||||
zf_assert(NULL != str);
|
||||
int8 data_temp[16]; // 缓冲区
|
||||
uint8 bit = 0; // 数字位数
|
||||
|
||||
do
|
||||
{
|
||||
if(NULL == str)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if(0 == number) // 这是个 0
|
||||
{
|
||||
*str = '0';
|
||||
break;
|
||||
}
|
||||
|
||||
while(0 != number) // 循环直到数值归零
|
||||
{
|
||||
data_temp[bit ++] = (number % 10); // 倒序将数值提取出来
|
||||
number /= 10; // 削减被提取的个位数
|
||||
}
|
||||
while(0 != bit) // 提取的数字个数递减处理
|
||||
{
|
||||
*str ++ = (data_temp[bit - 1] + 0x30); // 将数字从倒序数组中倒序取出 变成正序放入字符串
|
||||
bit --;
|
||||
}
|
||||
}while(0);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 字符串转浮点数 有效累计精度为小数点后六位
|
||||
// 参数说明 *str 传入字符串 可带符号
|
||||
// 返回参数 float 转换后的数据
|
||||
// 使用示例 float dat = func_str_to_float("-100.2");
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
float func_str_to_float (char *str)
|
||||
{
|
||||
zf_assert(NULL != str);
|
||||
uint8 sign = 0; // 标记符号 0-正数 1-负数
|
||||
float temp = 0.0; // 临时计算变量 整数部分
|
||||
float temp_point = 0.0; // 临时计算变量 小数部分
|
||||
float point_bit = 1; // 小数累计除数
|
||||
|
||||
do
|
||||
{
|
||||
if(NULL == str)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if('-' == *str) // 负数
|
||||
{
|
||||
sign = 1; // 标记负数
|
||||
str ++;
|
||||
}
|
||||
else if('+' == *str) // 如果第一个字符是正号
|
||||
{
|
||||
str ++;
|
||||
}
|
||||
|
||||
// 提取整数部分
|
||||
while(('0' <= *str) && ('9' >= *str)) // 确定这是个数字
|
||||
{
|
||||
temp = temp * 10 + ((uint8)(*str) - 0x30); // 将数值提取出来
|
||||
str ++;
|
||||
}
|
||||
if('.' == *str)
|
||||
{
|
||||
str ++;
|
||||
while(('0' <= *str) && ('9' >= *str) && 1000000.0 > point_bit) // 确认这是个数字 并且精度控制还没到六位
|
||||
{
|
||||
temp_point = temp_point * 10 + ((uint8)(*str) - 0x30); // 提取小数部分数值
|
||||
point_bit *= 10; // 计算这部分小数的除数
|
||||
str ++;
|
||||
}
|
||||
temp_point /= point_bit; // 计算小数
|
||||
}
|
||||
temp += temp_point; // 将数值拼合
|
||||
|
||||
if(sign)
|
||||
{
|
||||
temp = -temp;
|
||||
}
|
||||
}while(0);
|
||||
return temp;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 浮点数字转字符串
|
||||
// 参数说明 *str 字符串指针
|
||||
// 参数说明 number 传入的数据
|
||||
// 参数说明 point_bit 小数点精度
|
||||
// 返回参数 void
|
||||
// 使用示例 func_float_to_str(data_buffer, 3.1415, 2); // 结果输出 data_buffer = "3.14"
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void func_float_to_str (char *str, float number, uint8 point_bit)
|
||||
{
|
||||
zf_assert(NULL != str);
|
||||
int data_int = 0; // 整数部分
|
||||
int data_float = 0.0; // 小数部分
|
||||
int data_temp[8]; // 整数字符缓冲
|
||||
int data_temp_point[6]; // 小数字符缓冲
|
||||
uint8 bit = point_bit; // 转换精度位数
|
||||
|
||||
do
|
||||
{
|
||||
if(NULL == str)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// 提取整数部分
|
||||
data_int = (int)number; // 直接强制转换为 int
|
||||
if(0 > number) // 判断源数据是正数还是负数
|
||||
{
|
||||
*str ++ = '-';
|
||||
}
|
||||
else if(0.0 == number) // 如果是个 0
|
||||
{
|
||||
*str ++ = '0';
|
||||
*str ++ = '.';
|
||||
*str = '0';
|
||||
break;
|
||||
}
|
||||
|
||||
// 提取小数部分
|
||||
number = number - data_int; // 减去整数部分即可
|
||||
while(bit --)
|
||||
{
|
||||
number = number * 10; // 将需要的小数位数提取到整数部分
|
||||
}
|
||||
data_float = (int)number; // 获取这部分数值
|
||||
|
||||
// 整数部分转为字符串
|
||||
bit = 0;
|
||||
do
|
||||
{
|
||||
data_temp[bit ++] = data_int % 10; // 将整数部分倒序写入字符缓冲区
|
||||
data_int /= 10;
|
||||
}while(0 != data_int);
|
||||
while(0 != bit)
|
||||
{
|
||||
*str ++ = (func_abs(data_temp[bit - 1]) + 0x30); // 再倒序将倒序的数值写入字符串 得到正序数值
|
||||
bit --;
|
||||
}
|
||||
|
||||
// 小数部分转为字符串
|
||||
if(0 != point_bit)
|
||||
{
|
||||
bit = 0;
|
||||
*str ++ = '.';
|
||||
if(0 == data_float)
|
||||
{
|
||||
*str = '0';
|
||||
}
|
||||
else
|
||||
{
|
||||
while(0 != point_bit) // 判断有效位数
|
||||
{
|
||||
data_temp_point[bit ++] = data_float % 10; // 倒序写入字符缓冲区
|
||||
data_float /= 10;
|
||||
point_bit --;
|
||||
}
|
||||
while(0 != bit)
|
||||
{
|
||||
*str ++ = (func_abs(data_temp_point[bit - 1]) + 0x30); // 再倒序将倒序的数值写入字符串 得到正序数值
|
||||
bit --;
|
||||
}
|
||||
}
|
||||
}
|
||||
}while(0);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 字符串转浮点数 有效累计精度为小数点后九位
|
||||
// 参数说明 str 传入字符串 可带符号
|
||||
// 返回参数 double 转换后的数据
|
||||
// 使用示例 double dat = func_str_to_double("-100.2");
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
double func_str_to_double (char *str)
|
||||
{
|
||||
zf_assert(NULL != str);
|
||||
uint8 sign = 0; // 标记符号 0-正数 1-负数
|
||||
double temp = 0.0; // 临时计算变量 整数部分
|
||||
double temp_point = 0.0; // 临时计算变量 小数部分
|
||||
double point_bit = 1; // 小数累计除数
|
||||
|
||||
do
|
||||
{
|
||||
if(NULL == str)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if('-' == *str) // 负数
|
||||
{
|
||||
sign = 1; // 标记负数
|
||||
str ++;
|
||||
}
|
||||
else if('+' == *str) // 如果第一个字符是正号
|
||||
{
|
||||
str ++;
|
||||
}
|
||||
|
||||
// 提取整数部分
|
||||
while(('0' <= *str) && ('9' >= *str)) // 确定这是个数字
|
||||
{
|
||||
temp = temp * 10 + ((uint8)(*str) - 0x30); // 将数值提取出来
|
||||
str ++;
|
||||
}
|
||||
if('.' == *str)
|
||||
{
|
||||
str ++;
|
||||
while(('0' <= *str) && ('9' >= *str) && 1000000000.0 > point_bit) // 确认这是个数字 并且精度控制还没到九位
|
||||
{
|
||||
temp_point = temp_point * 10 + ((uint8)(*str) - 0x30); // 提取小数部分数值
|
||||
point_bit *= 10; // 计算这部分小数的除数
|
||||
str ++;
|
||||
}
|
||||
temp_point /= point_bit; // 计算小数
|
||||
}
|
||||
temp += temp_point; // 将数值拼合
|
||||
|
||||
if(sign)
|
||||
{
|
||||
temp = -temp;
|
||||
}
|
||||
}while(0);
|
||||
return temp;
|
||||
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 浮点数字转字符串
|
||||
// 参数说明 *str 字符串指针
|
||||
// 参数说明 number 传入的数据
|
||||
// 参数说明 point_bit 小数点精度
|
||||
// 返回参数 void
|
||||
// 使用示例 func_double_to_str(data_buffer, 3.1415, 2); // 结果输出 data_buffer = "3.14"
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void func_double_to_str (char *str, double number, uint8 point_bit)
|
||||
{
|
||||
zf_assert(NULL != str);
|
||||
int data_int = 0; // 整数部分
|
||||
int data_float = 0.0; // 小数部分
|
||||
int data_temp[12]; // 整数字符缓冲
|
||||
int data_temp_point[9]; // 小数字符缓冲
|
||||
uint8 bit = point_bit; // 转换精度位数
|
||||
|
||||
do
|
||||
{
|
||||
if(NULL == str)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// 提取整数部分
|
||||
data_int = (int)number; // 直接强制转换为 int
|
||||
if(0 > number) // 判断源数据是正数还是负数
|
||||
{
|
||||
*str ++ = '-';
|
||||
}
|
||||
else if(0.0 == number) // 如果是个 0
|
||||
{
|
||||
*str ++ = '0';
|
||||
*str ++ = '.';
|
||||
*str = '0';
|
||||
break;
|
||||
}
|
||||
|
||||
// 提取小数部分
|
||||
number = number - data_int; // 减去整数部分即可
|
||||
while(bit --)
|
||||
{
|
||||
number = number * 10; // 将需要的小数位数提取到整数部分
|
||||
}
|
||||
data_float = (int)number; // 获取这部分数值
|
||||
|
||||
// 整数部分转为字符串
|
||||
bit = 0;
|
||||
do
|
||||
{
|
||||
data_temp[bit ++] = data_int % 10; // 将整数部分倒序写入字符缓冲区
|
||||
data_int /= 10;
|
||||
}while(0 != data_int);
|
||||
while(0 != bit)
|
||||
{
|
||||
*str ++ = (func_abs(data_temp[bit - 1]) + 0x30); // 再倒序将倒序的数值写入字符串 得到正序数值
|
||||
bit --;
|
||||
}
|
||||
|
||||
// 小数部分转为字符串
|
||||
if(0 != point_bit)
|
||||
{
|
||||
bit = 0;
|
||||
*str ++ = '.';
|
||||
if(0 == data_float)
|
||||
{
|
||||
*str = '0';
|
||||
}
|
||||
else
|
||||
{
|
||||
while(0 != point_bit) // 判断有效位数
|
||||
{
|
||||
data_temp_point[bit ++] = data_float % 10; // 倒序写入字符缓冲区
|
||||
data_float /= 10;
|
||||
point_bit --;
|
||||
}
|
||||
while(0 != bit)
|
||||
{
|
||||
*str ++ = (func_abs(data_temp_point[bit - 1]) + 0x30); // 再倒序将倒序的数值写入字符串 得到正序数值
|
||||
bit --;
|
||||
}
|
||||
}
|
||||
}
|
||||
}while(0);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 字符串转 Hex
|
||||
// 参数说明 str 传入字符串 无符号
|
||||
// 返回参数 uint32 转换后的数据
|
||||
// 使用示例 uint32 dat = func_str_to_hex("0x11");
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
uint32 func_str_to_hex (char *str)
|
||||
{
|
||||
zf_assert(NULL != str);
|
||||
uint32 str_len = strlen(str); // 字符串长
|
||||
uint32 result_data = 0; // 结果缓存
|
||||
uint8 temp = 0; // 计算变量
|
||||
uint8 flag = 0; // 标志位
|
||||
|
||||
do
|
||||
{
|
||||
if(NULL == str)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if(flag)
|
||||
{
|
||||
if(('a' <= *str) && ('f' >= *str))
|
||||
{
|
||||
temp = (*str - 87);
|
||||
}
|
||||
else if(('A' <= *str) && ('F' >= *str))
|
||||
{
|
||||
temp = (*str - 55);
|
||||
}
|
||||
else if(('0' <= *str) && ('9' >= *str))
|
||||
{
|
||||
temp = (*str - 48);
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
result_data = ((result_data << 4) | (temp & 0x0F));
|
||||
}
|
||||
else
|
||||
{
|
||||
// if(strncmp("0x", str, 2))
|
||||
if(('0' == *str) && ('x' == *(str + 1)))
|
||||
{
|
||||
str ++;
|
||||
flag = 1;
|
||||
}
|
||||
}
|
||||
str ++;
|
||||
}while(str_len --);
|
||||
|
||||
return result_data;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 Hex 转字符串
|
||||
// 参数说明 *str 字符串指针
|
||||
// 参数说明 number 传入的数据
|
||||
// 返回参数 void
|
||||
// 使用示例 func_hex_to_str(data_buffer, 0x11); // 结果输出 data_buffer = "0x11"
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void func_hex_to_str (char *str, uint32 number)
|
||||
{
|
||||
zf_assert(NULL != str);
|
||||
const char hex_index[16] = {
|
||||
'0', '1', '2', '3',
|
||||
'4', '5', '6', '7',
|
||||
'8', '9', 'A', 'B',
|
||||
'C', 'D', 'E', 'F'};
|
||||
int8 data_temp[12]; // 缓冲区
|
||||
uint8 bit = 0; // 数字位数
|
||||
|
||||
*str ++ = '0';
|
||||
*str ++ = 'x';
|
||||
do
|
||||
{
|
||||
if(NULL == str)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if(0 == number) // 这是个 0
|
||||
{
|
||||
*str = '0';
|
||||
break;
|
||||
}
|
||||
|
||||
while(0 != number) // 循环直到数值归零
|
||||
{
|
||||
data_temp[bit ++] = (number & 0xF); // 倒序将数值提取出来
|
||||
number >>= 4; // 削减被提取的个位数
|
||||
}
|
||||
while(0 != bit) // 提取的数字个数递减处理
|
||||
{
|
||||
*str ++ = hex_index[data_temp[bit - 1]]; // 将数字从倒序数组中倒序取出 变成正序放入字符串
|
||||
bit --;
|
||||
}
|
||||
}while(0);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 数字转换为 ASCII 值
|
||||
// 参数说明 dat 传入的数据
|
||||
// 参数说明 *p 数据缓冲
|
||||
// 参数说明 neg_type 数据类型
|
||||
// 参数说明 radix 进制
|
||||
// 返回参数 uint8 数据
|
||||
// 使用示例 number_conversion_ascii((uint32)ival, vstr, 1, 10);
|
||||
// 备注信息 本函数在文件内部调用 用户不用关注 也不可修改
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
static uint8 number_conversion_ascii (uint32 dat, int8 *p, uint8 neg_type, uint8 radix)
|
||||
{
|
||||
int32 neg_dat = 0;
|
||||
uint32 pos_dat = 0;
|
||||
uint8 temp_data = 0;
|
||||
uint8 valid_num = 0;
|
||||
|
||||
if(neg_type)
|
||||
{
|
||||
neg_dat = (int32)dat;
|
||||
if(0 > neg_dat)
|
||||
{
|
||||
neg_dat = -neg_dat;
|
||||
}
|
||||
while(1)
|
||||
{
|
||||
*p = neg_dat % radix + '0';
|
||||
neg_dat = neg_dat / radix;
|
||||
valid_num ++;
|
||||
|
||||
if(!neg_dat)
|
||||
{
|
||||
break;
|
||||
}
|
||||
p ++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pos_dat = dat;
|
||||
while(1)
|
||||
{
|
||||
temp_data = pos_dat % radix;
|
||||
if(10 <= temp_data)
|
||||
{
|
||||
temp_data += 'A' - 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
temp_data += '0';
|
||||
}
|
||||
|
||||
*p = temp_data;
|
||||
|
||||
pos_dat = pos_dat / radix;
|
||||
valid_num ++;
|
||||
|
||||
if(!pos_dat)
|
||||
{
|
||||
break;
|
||||
}
|
||||
p ++;
|
||||
}
|
||||
}
|
||||
return valid_num;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 printf 显示转换
|
||||
// 参数说明 *d_buff 缓冲区
|
||||
// 参数说明 len 长度
|
||||
// 返回参数 void
|
||||
// 使用示例 printf_reverse_order(vstr, vlen);
|
||||
// 备注信息 本函数在文件内部调用 用户不用关注 也不可修改
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
static void printf_reverse_order (int8 *d_buff, uint32 len)
|
||||
{
|
||||
uint32 i = 0;
|
||||
int8 temp_data = 0;
|
||||
for(i = 0; len / 2 > i; i ++)
|
||||
{
|
||||
temp_data = d_buff[len - 1 - i];
|
||||
d_buff[len - 1 - i] = d_buff[i];
|
||||
d_buff[i] = temp_data;
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 sprintf 函数实现
|
||||
// 参数说明 *buff 缓冲区
|
||||
// 参数说明 *format 源字符串
|
||||
// 参数说明 ... 可变参数列表
|
||||
// 返回参数 uint32 处理后数据长
|
||||
// 使用示例 zf_sprintf(buff, "Data : %d", 100);
|
||||
// 备注信息 本函数在文件内部调用 用户不用关注 也不可修改
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
uint32 zf_sprintf (int8 *buff, const int8 *format, ...)
|
||||
{
|
||||
uint32 buff_len = 0;
|
||||
va_list arg;
|
||||
va_start(arg, format);
|
||||
|
||||
while (*format)
|
||||
{
|
||||
int8 ret = *format;
|
||||
if ('%' == ret)
|
||||
{
|
||||
switch (*++ format)
|
||||
{
|
||||
case 'a':// 十六进制p计数法输出浮点数 暂未实现
|
||||
{
|
||||
}break;
|
||||
|
||||
case 'c':// 一个字符
|
||||
{
|
||||
int8 ch = (int8)va_arg(arg, uint32);
|
||||
*buff = ch;
|
||||
buff ++;
|
||||
buff_len ++;
|
||||
}break;
|
||||
|
||||
case 'd':
|
||||
case 'i':// 有符号十进制整数
|
||||
{
|
||||
int8 vstr[33];
|
||||
int32 ival = (int32)va_arg(arg, int32);
|
||||
uint8 vlen = number_conversion_ascii((uint32)ival, vstr, 1, 10);
|
||||
|
||||
if(0 > ival)
|
||||
{
|
||||
vstr[vlen] = '-';
|
||||
vlen ++;
|
||||
}
|
||||
printf_reverse_order(vstr, vlen);
|
||||
memcpy(buff, vstr, vlen);
|
||||
buff += vlen;
|
||||
buff_len += vlen;
|
||||
}break;
|
||||
|
||||
case 'f':// 浮点数,输出小数点后六位 不能指定输出精度
|
||||
case 'F':// 浮点数,输出小数点后六位 不能指定输出精度
|
||||
{
|
||||
int8 vstr[33];
|
||||
double ival = (double)va_arg(arg, double);
|
||||
uint8 vlen = number_conversion_ascii((uint32)(int32)ival, vstr, 1, 10);
|
||||
|
||||
if(0 > ival)
|
||||
{
|
||||
vstr[vlen] = '-';
|
||||
vlen ++;
|
||||
}
|
||||
printf_reverse_order(vstr, vlen);
|
||||
memcpy(buff, vstr, vlen);
|
||||
buff += vlen;
|
||||
buff_len += vlen;
|
||||
|
||||
ival = ((double)ival - (int32)ival) * 1000000;
|
||||
if(ival)
|
||||
{
|
||||
vlen = number_conversion_ascii((uint32)(int32)ival, vstr, 1, 10);
|
||||
}
|
||||
else
|
||||
{
|
||||
vstr[0] = vstr[1] = vstr[2] = vstr[3] = vstr[4] = vstr[5] = '0';
|
||||
vlen = 6;
|
||||
}
|
||||
|
||||
while(6 > vlen)
|
||||
{
|
||||
vstr[vlen] = '0';
|
||||
vlen ++;
|
||||
}
|
||||
|
||||
vstr[vlen] = '.';
|
||||
vlen ++;
|
||||
|
||||
printf_reverse_order(vstr, vlen);
|
||||
memcpy(buff, vstr, vlen);
|
||||
buff_len += vlen;
|
||||
}break;
|
||||
|
||||
case 'u':// 无符号十进制整数
|
||||
{
|
||||
int8 vstr[33];
|
||||
uint32 ival = (uint32)va_arg(arg, uint32);
|
||||
uint8 vlen = number_conversion_ascii(ival, vstr, 0, 10);
|
||||
|
||||
printf_reverse_order(vstr, vlen);
|
||||
memcpy(buff, vstr, vlen);
|
||||
buff += vlen;
|
||||
buff_len += vlen;
|
||||
}break;
|
||||
|
||||
case 'o':// 无符号八进制整数
|
||||
{
|
||||
int8 vstr[33];
|
||||
uint32 ival = (uint32)va_arg(arg, uint32);
|
||||
uint8 vlen = number_conversion_ascii(ival, vstr, 0, 8);
|
||||
|
||||
printf_reverse_order(vstr, vlen);
|
||||
memcpy(buff, vstr, vlen);
|
||||
buff += vlen;
|
||||
buff_len += vlen;
|
||||
|
||||
}break;
|
||||
|
||||
case 'x':// 无符号十六进制整数
|
||||
case 'X':// 无符号十六进制整数
|
||||
{
|
||||
int8 vstr[33];
|
||||
uint32 ival = (uint32)va_arg(arg, uint32);
|
||||
uint8 vlen = number_conversion_ascii(ival, vstr, 0, 16);
|
||||
|
||||
printf_reverse_order(vstr, vlen);
|
||||
memcpy(buff, vstr, vlen);
|
||||
buff += vlen;
|
||||
buff_len += vlen;
|
||||
}break;
|
||||
|
||||
case 's':// 字符串
|
||||
{
|
||||
int8 *pc = va_arg(arg, int8 *);
|
||||
while (*pc)
|
||||
{
|
||||
*buff = *pc;
|
||||
buff ++;
|
||||
buff_len ++;
|
||||
pc ++;
|
||||
}
|
||||
}break;
|
||||
|
||||
case 'p':// 以16进制形式输出指针
|
||||
{
|
||||
int8 vstr[33];
|
||||
uint32 ival = (uint32)va_arg(arg, uint32);
|
||||
uint8 vlen = number_conversion_ascii(ival, vstr, 0, 16);
|
||||
|
||||
printf_reverse_order(vstr, 8);
|
||||
memcpy(buff, vstr, 8);
|
||||
buff += 8;
|
||||
buff_len += 8;
|
||||
}break;
|
||||
|
||||
case '%':// 输出字符%
|
||||
{
|
||||
*buff = '%';
|
||||
buff ++;
|
||||
buff_len ++;
|
||||
}break;
|
||||
|
||||
default:
|
||||
{
|
||||
}break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
*buff = (int8)(*format);
|
||||
buff ++;
|
||||
buff_len ++;
|
||||
}
|
||||
format ++;
|
||||
}
|
||||
va_end(arg);
|
||||
|
||||
return buff_len;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
/*********************************************************************************************************************
|
||||
* MSPM0G3507 Opensource Library 即(MSPM0G3507 开源库)是一个基于官方 SDK 接口的第三方开源库
|
||||
* Copyright (c) 2022 SEEKFREE 逐飞科技
|
||||
*
|
||||
* 本文件是 MSPM0G3507 开源库的一部分
|
||||
*
|
||||
* MSPM0G3507 开源库 是免费软件
|
||||
* 您可以根据自由软件基金会发布的 GPL(GNU General Public License,即 GNU通用公共许可证)的条款
|
||||
* 即 GPL 的第3版(即 GPL3.0)或(您选择的)任何后来的版本,重新发布和/或修改它
|
||||
*
|
||||
* 本开源库的发布是希望它能发挥作用,但并未对其作任何的保证
|
||||
* 甚至没有隐含的适销性或适合特定用途的保证
|
||||
* 更多细节请参见 GPL
|
||||
*
|
||||
* 您应该在收到本开源库的同时收到一份 GPL 的副本
|
||||
* 如果没有,请参阅<https://www.gnu.org/licenses/>
|
||||
*
|
||||
* 额外注明:
|
||||
* 本开源库使用 GPL3.0 开源许可证协议 以上许可申明为译文版本
|
||||
* 许可申明英文版在 libraries/doc 文件夹下的 GPL3_permission_statement.txt 文件中
|
||||
* 许可证副本在 libraries 文件夹下 即该文件夹下的 LICENSE 文件
|
||||
* 欢迎各位使用并传播本程序 但修改内容时必须保留逐飞科技的版权声明(即本声明)
|
||||
*
|
||||
* 文件名称 zf_common_function
|
||||
* 公司名称 成都逐飞科技有限公司
|
||||
* 版本信息 查看 libraries/doc 文件夹内 version 文件 版本说明
|
||||
* 开发环境 MDK 5.37
|
||||
* 适用平台 MSPM0G3507
|
||||
* 店铺链接 https://seekfree.taobao.com/
|
||||
*
|
||||
* 修改记录
|
||||
* 日期 作者 备注
|
||||
* 2025-06-1 SeekFree first version
|
||||
********************************************************************************************************************/
|
||||
|
||||
#ifndef _zf_common_function_h_
|
||||
#define _zf_common_function_h_
|
||||
|
||||
#include "zf_common_typedef.h"
|
||||
|
||||
//====================================================宏定义函数区====================================================
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 绝对值函数 数据范围是 [-32767,32767]
|
||||
// 参数说明 dat 需要求绝对值的数
|
||||
// 返回参数 int 返回绝对值
|
||||
// 使用示例 dat = func_abs(dat); // 将dat变成正数
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
#define func_abs(x) ((x) >= 0 ? (x): -(x))
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 限幅 数据范围是 [-32768,32767]
|
||||
// 参数说明 x 被限幅的数据
|
||||
// 参数说明 y 限幅范围(数据会被限制在-y至+y之间)
|
||||
// 返回参数 int 限幅之后的数据
|
||||
// 使用示例 int dat = func_limit(500, 300); // 数据被限制在-300至+300之间 因此返回的结果是300
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
#define func_limit(x, y) ((x) > (y) ? (y) : ((x) < -(y) ? -(y) : (x)))
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 双边限幅 数据范围是 [-32768,32767]
|
||||
// 参数说明 x 被限幅的数据
|
||||
// 参数说明 a 限幅范围左边界
|
||||
// 参数说明 b 限幅范围右边界
|
||||
// 返回参数 int 限幅之后的数据
|
||||
// 使用示例 int dat = func_limit_ab(500, -300, 400); //数据被限制在-300至+400之间 因此返回的结果是400
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
#define func_limit_ab(x, a, b) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x)))
|
||||
|
||||
//====================================================宏定义函数区====================================================
|
||||
|
||||
//=====================================================常规函数区=====================================================
|
||||
void func_get_sin_amplitude_table (uint32 *data_buffer, uint32 sample_max, uint32 amplitude_max, uint32 offset_degree);
|
||||
|
||||
uint32 func_get_greatest_common_divisor (uint32 num1, uint32 num2);
|
||||
|
||||
void func_soft_delay (volatile long t);
|
||||
|
||||
int32 func_str_to_int (char *str);
|
||||
void func_int_to_str (char *str, int32 number);
|
||||
uint32 func_str_to_uint (char *str);
|
||||
void func_uint_to_str (char *str, uint32 number);
|
||||
float func_str_to_float (char *str);
|
||||
void func_float_to_str (char *str, float number, uint8 point_bit);
|
||||
double func_str_to_double (char *str);
|
||||
void func_double_to_str (char *str, double number, uint8 point_bit);
|
||||
uint32 func_str_to_hex (char *str);
|
||||
void func_hex_to_str (char *str, uint32 number);
|
||||
|
||||
uint32 zf_sprintf (int8 *buff, const int8 *format, ...);
|
||||
//=====================================================常规函数区=====================================================
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,105 @@
|
||||
/*********************************************************************************************************************
|
||||
* MSPM0G3507 Opensource Library 即(MSPM0G3507 开源库)是一个基于官方 SDK 接口的第三方开源库
|
||||
* Copyright (c) 2022 SEEKFREE 逐飞科技
|
||||
*
|
||||
* 本文件是 MSPM0G3507 开源库的一部分
|
||||
*
|
||||
* MSPM0G3507 开源库 是免费软件
|
||||
* 您可以根据自由软件基金会发布的 GPL(GNU General Public License,即 GNU通用公共许可证)的条款
|
||||
* 即 GPL 的第3版(即 GPL3.0)或(您选择的)任何后来的版本,重新发布和/或修改它
|
||||
*
|
||||
* 本开源库的发布是希望它能发挥作用,但并未对其作任何的保证
|
||||
* 甚至没有隐含的适销性或适合特定用途的保证
|
||||
* 更多细节请参见 GPL
|
||||
*
|
||||
* 您应该在收到本开源库的同时收到一份 GPL 的副本
|
||||
* 如果没有,请参阅<https://www.gnu.org/licenses/>
|
||||
*
|
||||
* 额外注明:
|
||||
* 本开源库使用 GPL3.0 开源许可证协议 以上许可申明为译文版本
|
||||
* 许可申明英文版在 libraries/doc 文件夹下的 GPL3_permission_statement.txt 文件中
|
||||
* 许可证副本在 libraries 文件夹下 即该文件夹下的 LICENSE 文件
|
||||
* 欢迎各位使用并传播本程序 但修改内容时必须保留逐飞科技的版权声明(即本声明)
|
||||
*
|
||||
* 文件名称 zf_common_headfile
|
||||
* 公司名称 成都逐飞科技有限公司
|
||||
* 版本信息 查看 libraries/doc 文件夹内 version 文件 版本说明
|
||||
* 开发环境 MDK 5.37
|
||||
* 适用平台 MSPM0G3507
|
||||
* 店铺链接 https://seekfree.taobao.com/
|
||||
*
|
||||
* 修改记录
|
||||
* 日期 作者 备注
|
||||
* 2025-06-1 SeekFree first version
|
||||
********************************************************************************************************************/
|
||||
|
||||
#ifndef _zf_common_headfile_h_
|
||||
#define _zf_common_headfile_h_
|
||||
|
||||
#include "stdio.h"
|
||||
#include "stdint.h"
|
||||
#include "string.h"
|
||||
|
||||
//===================================================芯片 SDK 底层===================================================
|
||||
#include "ti_msp_dl_config.h"
|
||||
//===================================================芯片 SDK 底层===================================================
|
||||
|
||||
//====================================================开源库公共层====================================================
|
||||
#include "zf_common_typedef.h"
|
||||
#include "zf_common_clock.h"
|
||||
#include "zf_common_debug.h"
|
||||
#include "zf_common_fifo.h"
|
||||
#include "zf_common_font.h"
|
||||
#include "zf_common_function.h"
|
||||
#include "zf_common_interrupt.h"
|
||||
//====================================================开源库公共层====================================================
|
||||
|
||||
//===================================================芯片外设驱动层===================================================
|
||||
#include "zf_driver_adc.h"
|
||||
#include "zf_driver_delay.h"
|
||||
#include "zf_driver_exti.h"
|
||||
#include "zf_driver_flash.h"
|
||||
#include "zf_driver_gpio.h"
|
||||
#include "zf_driver_pit.h"
|
||||
#include "zf_driver_pwm.h"
|
||||
#include "zf_driver_soft_iic.h"
|
||||
#include "zf_driver_spi.h"
|
||||
#include "zf_driver_timer.h"
|
||||
#include "zf_driver_uart.h"
|
||||
//===================================================芯片外设驱动层===================================================
|
||||
|
||||
//===================================================外接设备驱动层===================================================
|
||||
#include "zf_device_absolute_encoder.h"
|
||||
#include "zf_device_oled.h"
|
||||
#include "zf_device_tft180.h"
|
||||
#include "zf_device_ips114.h"
|
||||
#include "zf_device_ips200.h"
|
||||
#include "zf_device_ips200pro.h"
|
||||
#include "zf_device_imu660ra.h"
|
||||
#include "zf_device_imu963ra.h"
|
||||
#include "zf_device_imu660rb.h"
|
||||
#include "zf_device_imu660rc.h"
|
||||
#include "zf_device_type.h"
|
||||
#include "zf_device_wifi_uart.h"
|
||||
#include "zf_device_wifi_spi.h"
|
||||
#include "zf_device_tsl1401.h"
|
||||
#include "zf_device_dl1b.h"
|
||||
#include "zf_device_dl1a.h"
|
||||
#include "zf_device_wireless_uart.h"
|
||||
#include "zf_device_key.h"
|
||||
|
||||
//===================================================外接设备驱动层===================================================
|
||||
|
||||
//===================================================应用组件层===================================================
|
||||
#include "seekfree_assistant.h"
|
||||
#include "seekfree_assistant_interface.h"
|
||||
//===================================================应用组件层===================================================
|
||||
|
||||
//===================================================用户自定义文件===================================================
|
||||
|
||||
|
||||
|
||||
|
||||
//===================================================用户自定义文件===================================================
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,123 @@
|
||||
/*********************************************************************************************************************
|
||||
* MSPM0G3507 Opensource Library 即(MSPM0G3507 开源库)是一个基于官方 SDK 接口的第三方开源库
|
||||
* Copyright (c) 2022 SEEKFREE 逐飞科技
|
||||
*
|
||||
* 本文件是 MSPM0G3507 开源库的一部分
|
||||
*
|
||||
* MSPM0G3507 开源库 是免费软件
|
||||
* 您可以根据自由软件基金会发布的 GPL(GNU General Public License,即 GNU通用公共许可证)的条款
|
||||
* 即 GPL 的第3版(即 GPL3.0)或(您选择的)任何后来的版本,重新发布和/或修改它
|
||||
*
|
||||
* 本开源库的发布是希望它能发挥作用,但并未对其作任何的保证
|
||||
* 甚至没有隐含的适销性或适合特定用途的保证
|
||||
* 更多细节请参见 GPL
|
||||
*
|
||||
* 您应该在收到本开源库的同时收到一份 GPL 的副本
|
||||
* 如果没有,请参阅<https://www.gnu.org/licenses/>
|
||||
*
|
||||
* 额外注明:
|
||||
* 本开源库使用 GPL3.0 开源许可证协议 以上许可申明为译文版本
|
||||
* 许可申明英文版在 libraries/doc 文件夹下的 GPL3_permission_statement.txt 文件中
|
||||
* 许可证副本在 libraries 文件夹下 即该文件夹下的 LICENSE 文件
|
||||
* 欢迎各位使用并传播本程序 但修改内容时必须保留逐飞科技的版权声明(即本声明)
|
||||
*
|
||||
* 文件名称 zf_common_interrupt
|
||||
* 公司名称 成都逐飞科技有限公司
|
||||
* 版本信息 查看 libraries/doc 文件夹内 version 文件 版本说明
|
||||
* 开发环境 MDK 5.37
|
||||
* 适用平台 MSPM0G3507
|
||||
* 店铺链接 https://seekfree.taobao.com/
|
||||
*
|
||||
* 修改记录
|
||||
* 日期 作者 备注
|
||||
* 2025-06-1 SeekFree first version
|
||||
********************************************************************************************************************/
|
||||
|
||||
#include "zf_common_interrupt.h"
|
||||
|
||||
static uint32 interrupt_nest_count = 0;
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 全局中断使能
|
||||
// 参数说明 primask 当前屏蔽嵌套层数
|
||||
// 返回参数 void
|
||||
// 使用示例 interrupt_global_enable(primask);
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void interrupt_global_enable (uint32 primask)
|
||||
{
|
||||
if(primask)
|
||||
{
|
||||
interrupt_nest_count --;
|
||||
}
|
||||
if(!interrupt_nest_count)
|
||||
{
|
||||
__ASM volatile("cpsie i");
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 全局中断屏蔽
|
||||
// 参数说明 void
|
||||
// 返回参数 uint32 当前屏蔽嵌套层数
|
||||
// 使用示例 primask = interrupt_global_disable();
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
uint32 interrupt_global_disable (void)
|
||||
{
|
||||
if(!interrupt_nest_count)
|
||||
{
|
||||
__ASM volatile("cpsid i");
|
||||
}
|
||||
interrupt_nest_count ++;
|
||||
return interrupt_nest_count;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 指定中断使能
|
||||
// 参数说明 irqn 指定中断号 可查看 isr.c 对应中断服务函数的标注
|
||||
// 返回参数 void
|
||||
// 使用示例 interrupt_enable(UART1_IRQn);
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void interrupt_enable (IRQn_Type irqn)
|
||||
{
|
||||
NVIC_EnableIRQ(irqn);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 指定中断屏蔽
|
||||
// 参数说明 irqn 指定中断号 可查看 isr.c 对应中断服务函数的标注
|
||||
// 返回参数 void
|
||||
// 使用示例 interrupt_disable(UART1_IRQn);
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void interrupt_disable (IRQn_Type irqn)
|
||||
{
|
||||
NVIC_DisableIRQ(irqn);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 指定中断设置优先级
|
||||
// 参数说明 irqn 指定中断号 可查看 isr.c 对应中断服务函数的标注
|
||||
// 参数说明 priority 中断优先级 0-7 值越小,优先级越高
|
||||
// 返回参数 void
|
||||
// 使用示例 interrupt_set_priority(UART1_IRQn, 0);
|
||||
// 备注信息
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void interrupt_set_priority (IRQn_Type irqn, uint8 priority)
|
||||
{
|
||||
NVIC_SetPriority(irqn, priority);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
// 函数简介 中断组初始化
|
||||
// 参数说明 void
|
||||
// 返回参数 void
|
||||
// 使用示例 interrupt_init();
|
||||
// 备注信息 会在 clock_init 内部调用
|
||||
//-------------------------------------------------------------------------------------------------------------------
|
||||
void interrupt_init (void)
|
||||
{
|
||||
NVIC_SetPriorityGrouping(4);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*********************************************************************************************************************
|
||||
* MSPM0G3507 Opensource Library 即(MSPM0G3507 开源库)是一个基于官方 SDK 接口的第三方开源库
|
||||
* Copyright (c) 2022 SEEKFREE 逐飞科技
|
||||
*
|
||||
* 本文件是 MSPM0G3507 开源库的一部分
|
||||
*
|
||||
* MSPM0G3507 开源库 是免费软件
|
||||
* 您可以根据自由软件基金会发布的 GPL(GNU General Public License,即 GNU通用公共许可证)的条款
|
||||
* 即 GPL 的第3版(即 GPL3.0)或(您选择的)任何后来的版本,重新发布和/或修改它
|
||||
*
|
||||
* 本开源库的发布是希望它能发挥作用,但并未对其作任何的保证
|
||||
* 甚至没有隐含的适销性或适合特定用途的保证
|
||||
* 更多细节请参见 GPL
|
||||
*
|
||||
* 您应该在收到本开源库的同时收到一份 GPL 的副本
|
||||
* 如果没有,请参阅<https://www.gnu.org/licenses/>
|
||||
*
|
||||
* 额外注明:
|
||||
* 本开源库使用 GPL3.0 开源许可证协议 以上许可申明为译文版本
|
||||
* 许可申明英文版在 libraries/doc 文件夹下的 GPL3_permission_statement.txt 文件中
|
||||
* 许可证副本在 libraries 文件夹下 即该文件夹下的 LICENSE 文件
|
||||
* 欢迎各位使用并传播本程序 但修改内容时必须保留逐飞科技的版权声明(即本声明)
|
||||
*
|
||||
* 文件名称 zf_common_interrupt
|
||||
* 公司名称 成都逐飞科技有限公司
|
||||
* 版本信息 查看 libraries/doc 文件夹内 version 文件 版本说明
|
||||
* 开发环境 MDK 5.37
|
||||
* 适用平台 MSPM0G3507
|
||||
* 店铺链接 https://seekfree.taobao.com/
|
||||
*
|
||||
* 修改记录
|
||||
* 日期 作者 备注
|
||||
* 2025-06-1 SeekFree first version
|
||||
********************************************************************************************************************/
|
||||
|
||||
#ifndef _zf_common_interrupt_h_
|
||||
#define _zf_common_interrupt_h_
|
||||
|
||||
#include "ti_msp_dl_config.h"
|
||||
|
||||
#include "zf_common_typedef.h"
|
||||
|
||||
void interrupt_global_enable (uint32 primask);
|
||||
uint32 interrupt_global_disable (void);
|
||||
void interrupt_enable (IRQn_Type irqn);
|
||||
void interrupt_disable (IRQn_Type irqn);
|
||||
void interrupt_set_priority (IRQn_Type irqn, uint8 priority);
|
||||
void interrupt_init (void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,214 @@
|
||||
/*********************************************************************************************************************
|
||||
* MSPM0G3507 Opensource Library 即(MSPM0G3507 开源库)是一个基于官方 SDK 接口的第三方开源库
|
||||
* Copyright (c) 2022 SEEKFREE 逐飞科技
|
||||
*
|
||||
* 本文件是 MSPM0G3507 开源库的一部分
|
||||
*
|
||||
* MSPM0G3507 开源库 是免费软件
|
||||
* 您可以根据自由软件基金会发布的 GPL(GNU General Public License,即 GNU通用公共许可证)的条款
|
||||
* 即 GPL 的第3版(即 GPL3.0)或(您选择的)任何后来的版本,重新发布和/或修改它
|
||||
*
|
||||
* 本开源库的发布是希望它能发挥作用,但并未对其作任何的保证
|
||||
* 甚至没有隐含的适销性或适合特定用途的保证
|
||||
* 更多细节请参见 GPL
|
||||
*
|
||||
* 您应该在收到本开源库的同时收到一份 GPL 的副本
|
||||
* 如果没有,请参阅<https://www.gnu.org/licenses/>
|
||||
*
|
||||
* 额外注明:
|
||||
* 本开源库使用 GPL3.0 开源许可证协议 以上许可申明为译文版本
|
||||
* 许可申明英文版在 libraries/doc 文件夹下的 GPL3_permission_statement.txt 文件中
|
||||
* 许可证副本在 libraries 文件夹下 即该文件夹下的 LICENSE 文件
|
||||
* 欢迎各位使用并传播本程序 但修改内容时必须保留逐飞科技的版权声明(即本声明)
|
||||
*
|
||||
* 文件名称 zf_common_typedef
|
||||
* 公司名称 成都逐飞科技有限公司
|
||||
* 版本信息 查看 libraries/doc 文件夹内 version 文件 版本说明
|
||||
* 开发环境 MDK 5.37
|
||||
* 适用平台 MSPM0G3507
|
||||
* 店铺链接 https://seekfree.taobao.com/
|
||||
*
|
||||
* 修改记录
|
||||
* 日期 作者 备注
|
||||
* 2025-06-1 SeekFree first version
|
||||
********************************************************************************************************************/
|
||||
|
||||
#ifndef _zf_common_typedef_h_
|
||||
#define _zf_common_typedef_h_
|
||||
|
||||
// 标准库
|
||||
#include "stdio.h"
|
||||
#include "stdint.h"
|
||||
#include "stdbool.h"
|
||||
#include "stdarg.h"
|
||||
#include "string.h"
|
||||
#include "stdlib.h"
|
||||
|
||||
// 注释的标准缩进 空行的 20 个 Tab 键为基础
|
||||
|
||||
// 此处定义 常用类型 如果与 SDK 冲突的话就将 USE_ZF_TYPEDEF 设置为 0
|
||||
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Part Start >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
#define USE_ZF_TYPEDEF (1) // 是否启用类型定义申明
|
||||
|
||||
#if USE_ZF_TYPEDEF
|
||||
// 数据类型声明
|
||||
// 尽量使用 stdint.h 定义的类型名称 避免冲突 这里可以裁剪
|
||||
typedef unsigned char uint8; // 无符号 8 bits
|
||||
typedef unsigned short int uint16; // 无符号 16 bits
|
||||
typedef unsigned int uint32; // 无符号 32 bits
|
||||
typedef unsigned long long uint64; // 无符号 64 bits
|
||||
|
||||
typedef signed char int8; // 有符号 8 bits
|
||||
typedef signed short int int16; // 有符号 16 bits
|
||||
typedef signed int int32; // 有符号 32 bits
|
||||
typedef signed long long int64; // 有符号 64 bits
|
||||
|
||||
typedef volatile uint8 vuint8; // 易变性修饰 无符号 8 bits
|
||||
typedef volatile uint16 vuint16; // 易变性修饰 无符号 16 bits
|
||||
typedef volatile uint32 vuint32; // 易变性修饰 无符号 32 bits
|
||||
typedef volatile uint64 vuint64; // 易变性修饰 无符号 64 bits
|
||||
|
||||
typedef volatile int8 vint8; // 易变性修饰 有符号 8 bits
|
||||
typedef volatile int16 vint16; // 易变性修饰 有符号 16 bits
|
||||
typedef volatile int32 vint32; // 易变性修饰 有符号 32 bits
|
||||
typedef volatile int64 vint64; // 易变性修饰 有符号 64 bits
|
||||
#endif
|
||||
|
||||
typedef enum
|
||||
{
|
||||
COMMON_DATA_SIZE_8BIT = 1, // 数据位宽 8bit
|
||||
COMMON_DATA_SIZE_16BIT = 2, // 数据位宽 16bit
|
||||
COMMON_DATA_SIZE_32BIT = 4, // 数据位宽 32bit
|
||||
}common_data_size_enum;
|
||||
|
||||
#define ZF_NO_ERROR ( 0 ) // 通用状态返回检查值 通常情况下返回 0 代表无异常
|
||||
#define ZF_ERROR ( 1 ) // 通用状态返回检查值 通常情况下返回 非 0 值代表异常码
|
||||
|
||||
#define ZF_ENABLE ( 1 ) // 使能 定义
|
||||
#define ZF_DISABLE ( 0 ) // 禁止 定义
|
||||
|
||||
#define ZF_TRUE ( 1 ) // 布尔值 真 定义
|
||||
#define ZF_FALSE ( 0 ) // 布尔值 假 定义
|
||||
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Part End <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
||||
|
||||
// 此处定义 常用函数指针类型
|
||||
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Part Start >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
typedef void (*void_function_void ) (void);
|
||||
typedef void (*void_function_uint32 ) (uint32 parameter);
|
||||
typedef void (*void_function_ptr ) (void *ptr);
|
||||
|
||||
typedef uint32 (*uint32_function_void ) (void);
|
||||
typedef uint32 (*uint32_function_uint32 ) (uint32 parameter);
|
||||
typedef uint32 (*uint32_function_ptr ) (void *ptr);
|
||||
|
||||
typedef void* (*ptr_function_void ) (void);
|
||||
typedef void* (*ptr_function_uint32 ) (uint32 parameter);
|
||||
typedef void* (*ptr_function_ptr ) (void *ptr);
|
||||
|
||||
typedef void (*void_callback_uint32_ptr ) (uint32 state, void *ptr);
|
||||
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Part End <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
||||
|
||||
// 此处定义 IDE 适配 因为不同 IDE 工具链有差异
|
||||
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Part Start >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
#define IDE_MDK ( 0x01 )
|
||||
#define IDE_IAR ( 0x02 )
|
||||
#define IDE_ADS ( 0x04 )
|
||||
#define IDE_MRS ( 0x08 )
|
||||
|
||||
#if defined(__ICCARM__)
|
||||
#define IDE_TYPE ( IDE_IAR )
|
||||
#else
|
||||
#define IDE_TYPE ( IDE_MDK )
|
||||
#endif
|
||||
|
||||
#if (IDE_MDK & IDE_TYPE)
|
||||
// -------------------------- MDK IDE 适配 --------------------------
|
||||
// MDK 使用 GNUC 工具链
|
||||
// 内联函数修饰
|
||||
#define ZF_INLINE static inline
|
||||
#define ZF_WEAK __attribute__((weak))
|
||||
|
||||
// 内存对齐与紧凑结构
|
||||
#define ZF_PACKED __attribute__((packed))
|
||||
#define ZF_PACKED_ENABLE
|
||||
#define ZF_PACKED_DISABLE
|
||||
|
||||
// 内存屏障指令 用于同步数据与指令 防止工程开优化导致问题
|
||||
#define ZF_DSB() __DSB()
|
||||
#define ZF_ISB() __ISB()
|
||||
#define ZF_DMB() __DMB()
|
||||
|
||||
// 文件定位
|
||||
#define ZF_FILE_MESSAGE ( __FILE__ )
|
||||
#define ZF_LINE_MESSAGE ( __LINE__ )
|
||||
// -------------------------- MDK IDE 适配 --------------------------
|
||||
#elif (IDE_IAR & IDE_TYPE)
|
||||
// -------------------------- IAR IDE 适配 --------------------------
|
||||
// IAR 使用 ICCARM 工具链
|
||||
// 内联函数修饰
|
||||
#define ZF_INLINE static inline
|
||||
#define ZF_WEAK __attribute__((weak))
|
||||
|
||||
// 内存对齐与紧凑结构
|
||||
// IAR 支持 __attribute__((packed)) 因此不需要 _Pragma
|
||||
#define ZF_PACKED __attribute__((packed))
|
||||
#define ZF_PACKED_ENABLE // _Pragma("pack(push,1)")
|
||||
#define ZF_PACKED_DISABLE // _Pragma("pack(pop)")
|
||||
|
||||
// 内存屏障指令 用于同步数据与指令 防止工程开优化导致问题
|
||||
#define ZF_DSB() __DSB()
|
||||
#define ZF_ISB() __ISB()
|
||||
#define ZF_DMB() __DMB()
|
||||
|
||||
// 文件定位
|
||||
#define ZF_FILE_MESSAGE ( __FILE__ )
|
||||
#define ZF_LINE_MESSAGE ( __LINE__ )
|
||||
// -------------------------- IAR IDE 适配 --------------------------
|
||||
#elif (IDE_ADS & IDE_TYPE)
|
||||
// -------------------------- ADS IDE 适配 --------------------------
|
||||
// ADS 使用 ADS* 工具链
|
||||
// 内联函数修饰
|
||||
#define ZF_INLINE static inline
|
||||
#define ZF_WEAK __attribute__((weak))
|
||||
|
||||
// ADS 仅支持 2/4 字节对齐
|
||||
#define ZF_PACKED
|
||||
#define ZF_PACKED_ENABLE _Pragma("pack 2")
|
||||
#define ZF_PACKED_DISABLE _Pragma("pack 0")
|
||||
|
||||
// 内存屏障指令 用于同步数据与指令 防止工程开优化导致问题
|
||||
#define ZF_DSB()
|
||||
#define ZF_ISB()
|
||||
#define ZF_DMB()
|
||||
|
||||
// 文件定位
|
||||
#define ZF_FILE_MESSAGE ( __FILE__ )
|
||||
#define ZF_LINE_MESSAGE ( __LINE__ )
|
||||
// -------------------------- ADS IDE 适配 --------------------------
|
||||
#elif (IDE_MRS & IDE_TYPE)
|
||||
// -------------------------- MRS IDE 适配 --------------------------
|
||||
// MRS 使用 GNUC 工具链
|
||||
// 内联函数修饰
|
||||
#define ZF_INLINE static inline
|
||||
#define ZF_WEAK __attribute__((weak))
|
||||
|
||||
// 内存对齐与紧凑结构
|
||||
#define ZF_PACKED __attribute__((packed))
|
||||
#define ZF_PACKED_ENABLE
|
||||
#define ZF_PACKED_DISABLE
|
||||
|
||||
// 内存屏障指令 用于同步数据与指令 防止工程开优化导致问题
|
||||
#define ZF_DSB()
|
||||
#define ZF_ISB()
|
||||
#define ZF_DMB()
|
||||
|
||||
// 文件定位
|
||||
#define ZF_FILE_MESSAGE ( __FILE__ )
|
||||
#define ZF_LINE_MESSAGE ( __LINE__ )
|
||||
// -------------------------- MRS IDE 适配 --------------------------
|
||||
#else
|
||||
#error "IDE Type error!"
|
||||
#endif
|
||||
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Part End <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user