【FR801xH】富芮坤FR801xH之PWM

00. 目录

01. FR801xH概述

在这里插入图片描述

FR801xH 系列芯片是面向SOC(片上系统),易于快速开发的低功耗蓝牙芯片。基于 Freqchip 的蓝牙智能固件和协议栈的支持,完全兼容蓝牙 V5.3(LE 模式)协议。同时用户可以基于芯片内置的 ARM CorteM3 嵌入式 32 位高性能单片机开发各种应用程序。

蓝牙智能固件包括 L2CAP 服务层协议、安全管理器 (SM)、属性协议(ATT)、通用属性配置文件 (GATT)和通用访问配置文件(GAP)。此外,还 支持应用程序配置文件,例如接近度、健康温度计、 心率、血压、血糖、人机界面设备(HID)和 SDK (包括驱动程序、OS-API 等)。SDK 还集成了用于网络应用程序的 SIG Mesh 协议。

采用 Freqchip 的创新技术,将 PMU(锂电池充电 器+LDO)、带 XIP 模式的 QSPI FLASH ROM、 I2C、UART、GPIO、ADC、PWM 集成在一块芯片中,为客户提供:

  • 竞争力的功耗
  • 稳定的蓝牙连接
  • 极低的 BOM 成本

02. FR801xH功能框图

在这里插入图片描述

03. PWM相关类型

位于 components\driver\include\driver_pwm.h。

3.1 pwm_channel_t

enum pwm_channel_t {
    PWM_CHANNEL_0,
    PWM_CHANNEL_1,
    PWM_CHANNEL_2,
    PWM_CHANNEL_3,
    PWM_CHANNEL_4,
    PWM_CHANNEL_5,
    PWM_CHANNEL_MAX,
};

04. 普通PWM相关API

4.1 pwm_init

/*********************************************************************
 * @fn      pwm_init
 *
 * @brief   init one pwm channel.
 *
 * @param   channel     - pwm channel, @ref pwm_channel_t.
 *          frequency   - output wave frequency
 *          high_duty   - duty taken by high level, should be from 0 to 99
 *
 * @return  None.
 */
void pwm_init(enum pwm_channel_t channel, uint32_t frequency, uint8_t high_duty);
功能:
	初始化 pwm。调用任何 pwm 函数前,需要首先调用 该函数。
参数:
	channel  -  初始化对象,可选 PWM_CHANNEL_0 等
	frequency  -  设置 PWM 的工作频率
	high_duty  -  设置 PWM 高电平所占比例,取值为 0~99
返回值:
	None

4.2 pwm_start

/*********************************************************************
 * @fn      pwm_start
 *
 * @brief   start one pwm channel.
 *
 * @param   channel     - pwm channel, @ref pwm_channel_t.
 *
 * @return  None.
 */
void pwm_start(enum pwm_channel_t channel);
功能:
	开启一个已经初始化好的 PWM
参数:
	channel  -  操作对象
返回值:
	None

4.3 pwm_stop

/*********************************************************************
 * @fn      pwm_stop
 *
 * @brief   stop one working pwm channel.
 *
 * @param   channel     - pwm channel, @ref pwm_channel_t.
 *
 * @return  None.
 */
void pwm_stop(enum pwm_channel_t channel);
功能:
	停止一个已经开始工作的 PWM
参数:
	channel  -  操作对象
返回值:
	None

4.4 pwm_update

/*********************************************************************
 * @fn      pwm_update
 *
 * @brief   update one pwm channel with new parameter.
 *
 * @param   channel     - pwm channel, @ref pwm_channel_t.
 *          frequency   - output wave frequency
 *          high_duty   - duty taken by high level, should be from 0 to 99
 *
 * @return  None.
 */
void pwm_update(enum pwm_channel_t channel, uint32_t frequency, uint8_t high_duty);
功能:
	更新一个正在运行的 PWM 的参数
参数:
	channel  -  更新对象,可选 PWM_CHANNEL_0 等
	frequency  -  设置 PWM 的工作频率
	high_duty  -  设置 PWM 高电平所占比例,取值为 0~99
返回值:
	None

05. 低功耗PWM相关API

头文件components/driver/include/driver_pmu_pwm.h

5.1 pmu_pwm_init

/*********************************************************************
 * @fn      pmu_pwm_init
 *
 * @brief   init pmu pwm block.
 *
 * @param   None.
 *
 * @return  None.
 */
void pmu_pwm_init(void);
功能:
	初始化 pmu_pwm。调用任何 pmu_pwm 函数前,需要首先调用 该函数。
参数:
	None
返回值:
	None

5.2 pmu_pwm_set_param

/*********************************************************************
 * @fn      pmu_pwm_set_param
 *
 * @brief   set one pmu pwm channel with parameter.
 *
 * @param   port  - IO port which is mapped with pmu pwm, @ref enum system_port_t.
 *          bit   - IO bit which is mapped with pmu pwm, @ref enum system_port_t.
 *          high_count  - high level output time, unit: RC_Period / 2;  range: 0~65535
 *                        where RC_CLK can be accessed by pmu_get_rc_clk(false)
 *          low_count   - low level output time, unit: RC_Period / 2;   range: 0~65535
 *                        where RC_CLK can be accessed by pmu_get_rc_clk(false)
 *
 * @return  None.
 */
void pmu_pwm_set_param(enum system_port_t port, enum system_port_bit_t bit,uint16_t high_count,uint16_t low_count);
功能:
	设置 pmu_pwm 的周期参数。需要在 pmu_pwm_init()之后调用。pin 脚对应的 pmu_pwm 功能选择由 PMU_IO_MUX 表格决定。
参数:
    port  -  pwm 对应的 port 口,参见 enum system_port_t 定义。
    bit  -  pwm 对应的 pin 脚号,参见 enum system_port_bit_t 定义。
    high_count  -  pwm 在一个周期内输出高电平的持续时间,单位:pmu 时钟周期。
    low_count  -  pwm 在一个周期内输出低电平的持续时间,单位:pmu 时钟周期。Pmu 时钟选择由函数 void pmu_set_lp_clk_src(enum pmu_lp_clk_src_t src) 决定。
返回值:
	None

5.3 pmu_pwm_start

/*********************************************************************
 * @fn      pmu_pwm_start
 *
 * @brief   start one pmu pwm channel
 *
 * @param   port  - IO port which is mapped with pmu pwm, @ref enum system_port_t.
 *          bit   - IO bit which is mapped with pmu pwm, @ref enum system_port_t.
 *          repeat_flag   - true = pmu pwm will output continously. 0 = pmu pwm will output one period.
 *          reverse_flag   - true = pmu pwm start from low voltage. fasle = pmu pwm start from high voltage.
 * 
 * @return  None.
 */
void pmu_pwm_start(enum system_port_t port, enum system_port_bit_t bit,bool repeat_flag,bool reverse_flag);
功能:
	启动 pmu_pwm。pmu_pwm 在 sleep 情况下,依然能保持运行。pin 脚对应的 pmu_pwm 功能选择由 PMU_IO_MUX 表格决
定。
参数:
    port pwm 对应的 port 口,参见 enum system_port_t 定义
    bit pwm 对应的 pin 号码,参见 enum system_port_bit_t 定义
    repeat_flag pwm 是否循环发送标志位。 True - pwm 会循环。 False -pwm 启动后只发送一个周期
    reverse_flag pwm 电平是否会翻转发送。True -pwm 从低电平开始。 False -pwm 从高电平开始
返回值:
	None


示例

pmu_pwm_init();
// configure PB0~PB2 is controlled by PMU
pmu_set_pin_to_PMU(GPIO_PORT_B, BIT(0)|BIT(1)|BIT(2));
// configure PB0~PB2 as PMU ouput
pmu_set_pin_dir(GPIO_PORT_B, BIT(0)|BIT(1)|BIT(2),true);
// configure PB0~PB2 as PMU_PWM function
pmu_set_port_mux(GPIO_PORT_B,GPIO_BIT_0,PMU_PORT_MUX_PWM);
pmu_set_port_mux(GPIO_PORT_B,GPIO_BIT_1,PMU_PORT_MUX_PWM);
pmu_set_port_mux(GPIO_PORT_B,GPIO_BIT_2,PMU_PORT_MUX_PWM);
// set PMU_PWM high count/low count in one period
pmu_pwm_set_param(GPIO_PORT_B,GPIO_BIT_0,20,10);
pmu_pwm_set_param(GPIO_PORT_B,GPIO_BIT_1,20,10);
pmu_pwm_set_param(GPIO_PORT_B,GPIO_BIT_2,20,10);
// start PB0~PB2 PMU_PWM.
pmu_pwm_start(GPIO_PORT_B,GPIO_BIT_0,1,0);
pmu_pwm_start(GPIO_PORT_B,GPIO_BIT_1,0,0);
pmu_pwm_start(GPIO_PORT_B,GPIO_BIT_2,1,1);
co_delay_100us(10000);
// stop PB0~PB2 PMU_PWM.
pmu_pwm_stop(GPIO_PORT_B,GPIO_BIT_0);
pmu_pwm_stop(GPIO_PORT_B,GPIO_BIT_1);
pmu_pwm_stop(GPIO_PORT_B,GPIO_BIT_2);

5.4 pmu_pwm_stop

/*********************************************************************
 * @fn      pmu_pwm_stop
 *
 * @brief   stop one pmu pwm channel
 *
 * @param   port  - IO port which is mapped with pmu pwm, @ref enum system_port_t.
 *          bit   - IO bit which is mapped with pmu pwm, @ref enum system_port_t.
 *
 * @return  None.
 */
void pmu_pwm_stop(enum system_port_t port, enum system_port_bit_t bit);
功能:
	停止 pmu_pwm。pin 脚对应的 pmu_pwm 功能选择由 PMU_IO_MUX 表格决定。
参数:
	port  -  pwm 对应的 port 口,参见 enum system_port_t 定义。
	bit  -  pwm 对应的 pin 脚号,参见 enum system_port_bit_t 定义。
返回值:
	None


06. 普通PWM程序示例

程序示例

#include "driver_pwm.h"
#include "driver_iomux.h"

void peripheral_demo(void)
{
    co_printf("digital_pwm demo\r\n");

    system_set_port_mux(GPIO_PORT_D, GPIO_BIT_4, PORTD4_FUNC_PWM4);
    system_set_port_mux(GPIO_PORT_D, GPIO_BIT_5, PORTD5_FUNC_PWM5);

    pwm_init(PWM_CHANNEL_4, 1000, 50);
    pwm_init(PWM_CHANNEL_5, 1000, 80);

    pwm_start(PWM_CHANNEL_4);
    pwm_start(PWM_CHANNEL_5);
    co_delay_100us(20000); // 1K hz for 2s

    pwm_update(PWM_CHANNEL_4, 10000, 50);
    pwm_update(PWM_CHANNEL_5, 10000, 80);
    co_delay_100us(10000); // 10K hz for 1s

    pwm_stop(PWM_CHANNEL_4);
    pwm_stop(PWM_CHANNEL_5);
    co_delay_100us(20000); // stop for 2s

    pwm_start(PWM_CHANNEL_4);
    pwm_start(PWM_CHANNEL_5);
    co_delay_100us(10000); // restart pwm with 10K hz for 1s
    
    pwm_stop(PWM_CHANNEL_4);
    pwm_stop(PWM_CHANNEL_5);
}

07. 低功耗PWM程序示例

程序示例

#include "driver_pmu_pwm.h"

void peripheral_demo(void)
{
    co_printf("pmu_pwm demo\r\n");
    pmu_pwm_init();
    pmu_set_pin_to_PMU(GPIO_PORT_D, BIT(4) | BIT(5));
    pmu_set_pin_dir(GPIO_PORT_D, BIT(4) | BIT(5), GPIO_DIR_OUT);

    pmu_set_port_mux(GPIO_PORT_D, GPIO_BIT_4, PMU_PORT_MUX_PWM);
    pmu_set_port_mux(GPIO_PORT_D, GPIO_BIT_5, PMU_PORT_MUX_PWM);

    pmu_pwm_set_param(GPIO_PORT_D, GPIO_BIT_4, 2000, 1000);
    pmu_pwm_set_param(GPIO_PORT_D, GPIO_BIT_5, 2000, 1000);
    // PD4,will keep output
    pmu_pwm_start(GPIO_PORT_D, GPIO_BIT_4, true, 0);
    // PD5,will keep output
    pmu_pwm_start(GPIO_PORT_D, GPIO_BIT_5, true, 0);
    co_delay_100us(20000); // for 2s

    pmu_pwm_set_param(GPIO_PORT_D, GPIO_BIT_4, 1000, 2000);
    pmu_pwm_set_param(GPIO_PORT_D, GPIO_BIT_5, 1000, 2000);
    co_delay_100us(10000); // 10K hz for 1s
    
    pmu_pwm_stop(GPIO_PORT_D, GPIO_BIT_4);
    pmu_pwm_stop(GPIO_PORT_D, GPIO_BIT_5);

    co_delay_100us(20000); // stop for 2s
    // PD4,will last only one period
    pmu_pwm_start(GPIO_PORT_D, GPIO_BIT_4, false, 0);
    // PD5,will last only one period
    pmu_pwm_start(GPIO_PORT_D, GPIO_BIT_5, false, 0);
    co_delay_100us(10000); // restart pwm for 1s
    pmu_pwm_stop(GPIO_PORT_D, GPIO_BIT_4);
    pmu_pwm_stop(GPIO_PORT_D, GPIO_BIT_5);
}

08. 附录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沧海一笑-dj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值