BK7256—pwm接口资料:
应用步骤:
0.下载官方sdk框架
1.在cpu0或者cpu1的配置文件【bk7256.config/bk7256_cp1.config】中添加宏定义CONFIG_PWM=y 【取决于你在哪个核使用PWM】
2.测试代码:【4KHz蜂鸣器每秒响1次】
#include "bk_private/bk_init.h"
#include <driver/pwm.h>
#include "pwm_hal.h"
#ifdef CONFIG_CACHE_CUSTOM_SRAM_MAPPING
const unsigned int g_sram_addr_map[4] =
{
0x38000000,
0x30020000,
0x38020000,
0x30000000};
#endif
#define BUZZ_PWM_ID (PWM_ID_11) // beep gpio对应的pwm号
#define BUZZ_FREQ (4000) // Buzz pwm 4000HZ
#define BUZZ_PERIOD (26000000 / BUZZ_FREQ) // SystemClk=26Mhz
static int g_beep_cnt = 0;
static void make_beep(int cnt)
{
g_beep_cnt += cnt;
}
static void buzz_thread(void *para)
{
// buzz pwm initial
bk_pwm_driver_init();
pwm_init_config_t config = {0};
config.period_cycle = BUZZ_PERIOD;
config.duty_cycle = 0.5 * BUZZ_PERIOD; // 50% Pwm Duty
config.duty2_cycle = 0;
config.duty3_cycle = 0;
bk_pwm_init(BUZZ_PWM_ID, &config);
bk_pwm_stop(BUZZ_PWM_ID);
// loop logic
while (1)
{
if (g_beep_cnt > 0)
{
g_beep_cnt--;
bk_pwm_start(BUZZ_PWM_ID);
rtos_delay_milliseconds(150);
bk_pwm_stop(BUZZ_PWM_ID);
if (g_beep_cnt > 5) // 防止Buzz狂响
g_beep_cnt = 0;
}
rtos_delay_milliseconds(10);
}
}
int main(void)
{
//bk_printf_raw
bk_init();
BK_LOG_RAW("[bk7256] main program build:%s %s.\n", __DATE__, __TIME__);
BK_LOG_RAW("Buzz frequency:%d Hz, PWM_ID=%d.\n",BUZZ_FREQ,BUZZ_PWM_ID );
rtos_create_thread(NULL, 10, "buzz_thread", buzz_thread, 2 * 4096, NULL);
while (1)
{ // beep sound every 1 second
make_beep(1);
rtos_delay_milliseconds(1000);
}
return 0;
}
3.编译指令: make bk7256 PROJECT=项目相对路径 ,建议做成sh脚本。
4.烧录技巧:生成的bin文件一般是 armino/build/项目名/bk7256/all-app.bin ,将BK7256的CEN(ChipEn)引脚拉倒串口助手的RTS信号,辅助烧录时自动复位。