Allen 先楫資深 FAE
8年產(chǎn)品研發(fā)經(jīng)驗,具有變頻器、PLC等工業(yè)產(chǎn)品開發(fā)經(jīng)驗,也參與過汽車儀表、中控屏等車載產(chǎn)品的研發(fā)工作。在產(chǎn)品底層驅(qū)動、伺服驅(qū)動器、顯示儀表等領(lǐng)域有著豐富開發(fā)經(jīng)驗。
在嵌入式產(chǎn)品應(yīng)用開發(fā)中,經(jīng)常需要MCU芯片產(chǎn)生任意的方波信號,從而驅(qū)動外設(shè)執(zhí)行相應(yīng)的操作。比如,驅(qū)動模擬量芯片、miniLED屏等。不同于PWM波這種占空比固定的信號,這些驅(qū)動信號往往是由等寬的高低電平任意排列的方波。
傳統(tǒng)的GPIO模擬方波時序,不僅占用CPU資源,而且波形的脈寬較大。在驅(qū)動miniLED屏這類外設(shè)時,達(dá)不到系統(tǒng)的功能要求。先楫半導(dǎo)體的全系列MCU可以將內(nèi)存中的數(shù)據(jù)通過DMA來設(shè)置GPIO的電平,以TRGM互聯(lián)管理器和PWM比較器配合使用來設(shè)置脈沖寬度,從而產(chǎn)生任意時序的方波信號。該方案基于硬件方式來實現(xiàn),不會占用CPU處理時間,波形寬度可達(dá)到50ns。下面介紹該方案的實現(xiàn)過程,例程基于HPM6360EVK實現(xiàn)。
定義波形數(shù)據(jù)
定義數(shù)組如下所示,其中32位無符號整數(shù)可以映射32個GPIO,每個數(shù)據(jù)位對應(yīng)一個管腳。數(shù)組長度4096對應(yīng)4096個方波周期。
ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(8) uint32_t g_u32LedBufForGpio32[4096] = {0xFFFFFFFF, 0, 0xFFFFFFFF, 0, 0xFFFFFFFF, 0, 0xFFFFFFFF, 0, 0xFFFFFFFF, 0, 0xFFFFFFFF, 0, 0xFFFFFFFF, 0, 0xFFFFFFFF, 0};
初始化GPIO管腳
此處將24個GPIO配置為輸出模式。
static void zh_led_gpio_config(void)
{
uint32_t pad_ctl = IOC_PAD_PAD_CTL_PE_SET(1) | IOC_PAD_PAD_CTL_PS_SET(1);
HPM_IOC->PAD[IOC_PAD_PC00].FUNC_CTL = IOC_PC00_FUNC_CTL_GPIO_C_00;
HPM_IOC->PAD[IOC_PAD_PC00].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC01].FUNC_CTL = IOC_PC01_FUNC_CTL_GPIO_C_01;
HPM_IOC->PAD[IOC_PAD_PC01].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC02].FUNC_CTL = IOC_PC02_FUNC_CTL_GPIO_C_02;
HPM_IOC->PAD[IOC_PAD_PC02].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC03].FUNC_CTL = IOC_PC03_FUNC_CTL_GPIO_C_03;
HPM_IOC->PAD[IOC_PAD_PC03].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC04].FUNC_CTL = IOC_PC04_FUNC_CTL_GPIO_C_04;
HPM_IOC->PAD[IOC_PAD_PC04].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC05].FUNC_CTL = IOC_PC05_FUNC_CTL_GPIO_C_05;
HPM_IOC->PAD[IOC_PAD_PC05].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC06].FUNC_CTL = IOC_PC06_FUNC_CTL_GPIO_C_06;
HPM_IOC->PAD[IOC_PAD_PC06].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC07].FUNC_CTL = IOC_PC07_FUNC_CTL_GPIO_C_07;
HPM_IOC->PAD[IOC_PAD_PC07].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC08].FUNC_CTL = IOC_PC08_FUNC_CTL_GPIO_C_08;
HPM_IOC->PAD[IOC_PAD_PC08].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC09].FUNC_CTL = IOC_PC09_FUNC_CTL_GPIO_C_09;
HPM_IOC->PAD[IOC_PAD_PC09].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC10].FUNC_CTL = IOC_PC10_FUNC_CTL_GPIO_C_10;
HPM_IOC->PAD[IOC_PAD_PC10].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC11].FUNC_CTL = IOC_PC11_FUNC_CTL_GPIO_C_11;
HPM_IOC->PAD[IOC_PAD_PC11].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC12].FUNC_CTL = IOC_PC12_FUNC_CTL_GPIO_C_12;
HPM_IOC->PAD[IOC_PAD_PC12].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC13].FUNC_CTL = IOC_PC13_FUNC_CTL_GPIO_C_13;
HPM_IOC->PAD[IOC_PAD_PC13].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC14].FUNC_CTL = IOC_PC14_FUNC_CTL_GPIO_C_14;
HPM_IOC->PAD[IOC_PAD_PC14].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC15].FUNC_CTL = IOC_PC15_FUNC_CTL_GPIO_C_15;
HPM_IOC->PAD[IOC_PAD_PC15].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC16].FUNC_CTL = IOC_PC16_FUNC_CTL_GPIO_C_16;
HPM_IOC->PAD[IOC_PAD_PC16].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC17].FUNC_CTL = IOC_PC17_FUNC_CTL_GPIO_C_17;
HPM_IOC->PAD[IOC_PAD_PC17].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC18].FUNC_CTL = IOC_PC18_FUNC_CTL_GPIO_C_18;
HPM_IOC->PAD[IOC_PAD_PC18].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC19].FUNC_CTL = IOC_PC19_FUNC_CTL_GPIO_C_19;
HPM_IOC->PAD[IOC_PAD_PC19].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC20].FUNC_CTL = IOC_PC20_FUNC_CTL_GPIO_C_20;
HPM_IOC->PAD[IOC_PAD_PC20].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC21].FUNC_CTL = IOC_PC21_FUNC_CTL_GPIO_C_21;
HPM_IOC->PAD[IOC_PAD_PC21].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC22].FUNC_CTL = IOC_PC22_FUNC_CTL_GPIO_C_22;
HPM_IOC->PAD[IOC_PAD_PC22].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC23].FUNC_CTL = IOC_PC23_FUNC_CTL_GPIO_C_23;
HPM_IOC->PAD[IOC_PAD_PC23].PAD_CTL = pad_ctl;
HPM_IOC->PAD[IOC_PAD_PC24].FUNC_CTL = IOC_PC24_FUNC_CTL_GPIO_C_24;
HPM_IOC->PAD[IOC_PAD_PC24].PAD_CTL = pad_ctl;
for(int i=0; i<24; i++) {
gpio_set_pin_output(HPM_GPIO0, GPIO_DO_GPIOC, i);
gpio_write_pin(HPM_GPIO0, GPIO_DO_GPIOC, i, 0);
}
}
配置DMA
在每次輸出波形之前都要進(jìn)行一下DMA的設(shè)置,用戶通過調(diào)整波形數(shù)組的數(shù)據(jù)來設(shè)置想要的波形。DMA將數(shù)組的數(shù)據(jù)不斷搬運到DO[VALUE]寄存器中,因此要注意GPIO管腳和數(shù)組數(shù)據(jù)的對應(yīng)關(guān)系。例程是輸出占空比為50%的PWM波。
static void zh_led_dma_config(void)
{
dma_channel_config_t ch_config = { 0 };
unsigned int i = 0;
for(i = 0;i<4096;i++)
{
g_u32LedBufForGpio32[i] = (i&1)?(~0):(0);
//g_u32LedBufForGpio32[i] = ~0;
//if(i%5 == 0)
//{
// g_u32LedBufForGpio32[i] = ~0;
//}
//else
//{
// g_u32LedBufForGpio32[i] = 0;
//}
}
dma_reset(HPM_HDMA);
intc_m_enable_irq_with_priority(BOARD_APP_HDMA_IRQ, 1);
dma_default_channel_config(HPM_HDMA, &ch_config);
ch_config.src_addr = (uint32_t)&g_u32LedBufForGpio32[0];
ch_config.dst_addr = (uint32_t)&HPM_GPIO0->DO[GPIO_DO_GPIOC].VALUE;
ch_config.src_width = DMA_TRANSFER_WIDTH_WORD; // 32位
ch_config.dst_width = DMA_TRANSFER_WIDTH_WORD; // 32位
ch_config.src_addr_ctrl = DMA_ADDRESS_CONTROL_INCREMENT;
ch_config.dst_addr_ctrl = DMA_ADDRESS_CONTROL_FIXED;
ch_config.size_in_byte = sizeof(g_u32LedBufForGpio32); //32 * sizeof(uint32_t);
ch_config.dst_mode = DMA_HANDSHAKE_MODE_NORMAL;
ch_config.src_burst_size = 0;
if (status_success != dma_setup_channel(HPM_HDMA, 0, &ch_config, false))
{
printf(" dma setup channel failed\n");
return;
}
dmamux_config(HPM_DMAMUX, DMAMUX_MUXCFG_HDMA_MUX0, HPM_DMA_SRC_MOT0_0, false);
trgm_dma_request_config(HPM_TRGM0, 0, 18);
pwm_enable_dma_request(HPM_PWM0, PWM_IRQ_CMP(18));
synt_enable_counter(HPM_SYNT, true);
pwm_start_counter(HPM_PWM0);
dmamux_enable_channel(HPM_DMAMUX, DMAMUX_MUXCFG_HDMA_MUX0);
dma_enable_channel(HPM_HDMA, 0);
}
設(shè)置DMA中斷,在輸出波形結(jié)束以后會觸發(fā)中斷響應(yīng)函數(shù)。
void isr_dma(void)
{
uint32_t stat;
stat = dma_check_transfer_status(HPM_HDMA, 0);
if (0 != (stat & DMA_CHANNEL_STATUS_TC)) {
printf("Transfer done!");
}
}
SDK_DECLARE_EXT_ISR_M(BOARD_APP_HDMA_IRQ, isr_dma)
配置CLK時鐘
以PWM比較器產(chǎn)生需要的波形周期。
/**
* u8Phase from 1~18, 其中設(shè)置1 == 設(shè)置18, 設(shè)置1時 上升沿的邊與數(shù)據(jù)的開始時刻對齊,隨著設(shè)置的數(shù)字增大,上升沿的邊向右移動,設(shè)置8時,上升沿的邊大約在中間。
* */
static int zh_led_clk_config(uint8_t u8Phase)
{
pwm_cmp_config_t cmp_config_ch0[4] = {0};
pwm_config_t pwm_config = {0};
uint32_t u32CmpValue[4];
switch (u8Phase)
{
case 1:
u32CmpValue[0] = 6;
u32CmpValue[1] = 16;
u32CmpValue[2] = 18;
u32CmpValue[3] = 18;
break;
case 2:
u32CmpValue[0] = 7;
u32CmpValue[1] = 18;
u32CmpValue[2] = 18;
u32CmpValue[3] = 18;
break;
case 3:
u32CmpValue[0] = 17;
u32CmpValue[1] = 0;
u32CmpValue[2] = 18;
u32CmpValue[3] = 8;
break;
case 4:
u32CmpValue[0] = 17;
u32CmpValue[1] = 1;
u32CmpValue[2] = 18;
u32CmpValue[3] = 9;
break;
case 5:
u32CmpValue[0] = 17;
u32CmpValue[1] = 2;
u32CmpValue[2] = 18;
u32CmpValue[3] = 10;
break;
case 6:
u32CmpValue[0] = 17;
u32CmpValue[1] = 3;
u32CmpValue[2] = 18;
u32CmpValue[3] = 11;
break;
case 7:
u32CmpValue[0] = 17;
u32CmpValue[1] = 4;
u32CmpValue[2] = 18;
u32CmpValue[3] = 12;
break;
case 8:
u32CmpValue[0] = 17;
u32CmpValue[1] = 5;
u32CmpValue[2] = 18;
u32CmpValue[3] = 13;
break;
case 9:
u32CmpValue[0] = 17;
u32CmpValue[1] = 6;
u32CmpValue[2] = 18;
u32CmpValue[3] = 14;
break;
case 10:
u32CmpValue[0] = 18;
u32CmpValue[1] = 7;
u32CmpValue[2] = 17;
u32CmpValue[3] = 15;
break;
case 11:
u32CmpValue[0] = 18;
u32CmpValue[1] = 8;
u32CmpValue[2] = 17;
u32CmpValue[3] = 16;
break;
case 12:
u32CmpValue[0] = 17;
u32CmpValue[1] = 9;
u32CmpValue[2] = 18;
u32CmpValue[3] = 18;
break;
case 13:
u32CmpValue[0] = 0;
u32CmpValue[1] = 10;
u32CmpValue[2] = 18;
u32CmpValue[3] = 18;
break;
case 14:
u32CmpValue[0] = 1;
u32CmpValue[1] = 11;
u32CmpValue[2] = 18;
u32CmpValue[3] = 18;
break;
case 15:
u32CmpValue[0] = 2;
u32CmpValue[1] = 12;
u32CmpValue[2] = 18;
u32CmpValue[3] = 18;
break;
case 16:
u32CmpValue[0] = 3;
u32CmpValue[1] = 13;
u32CmpValue[2] = 18;
u32CmpValue[3] = 18;
break;
case 17:
u32CmpValue[0] = 4;
u32CmpValue[1] = 14;
u32CmpValue[2] = 18;
u32CmpValue[3] = 18;
break;
case 18:
u32CmpValue[0] = 5;
u32CmpValue[1] = 15;
u32CmpValue[2] = 18;
u32CmpValue[3] = 18;
break;
break;
default:
break;
}
pwm_stop_counter(HPM_PWM0);
pwm_set_reload(HPM_PWM0, 0, zh_led_PWM_FREQ-1);
pwm_set_start_count(HPM_PWM0, 0, 0);
pwm_config.enable_output = true;
pwm_config.invert_output = false;
pwm_config.update_trigger = pwm_shadow_register_update_on_modify;
pwm_config.fault_mode = pwm_fault_mode_force_output_highz;
pwm_config.fault_recovery_trigger = pwm_fault_recovery_on_fault_clear;
pwm_config.force_source = pwm_force_source_software;
pwm_config.dead_zone_in_half_cycle = 0;
/*cmp0 cmp1 cmp2 cmp3 for pwm ch0*/
cmp_config_ch0[0].cmp = u32CmpValue[0];
cmp_config_ch0[0].enable_ex_cmp = false;
cmp_config_ch0[0].mode = pwm_cmp_mode_output_compare;
cmp_config_ch0[0].update_trigger = pwm_shadow_register_update_on_hw_event;
cmp_config_ch0[0].ex_cmp = 0;
cmp_config_ch0[0].half_clock_cmp = 0;
cmp_config_ch0[0].jitter_cmp = 0;
cmp_config_ch0[1].cmp = u32CmpValue[1];
cmp_config_ch0[1].enable_ex_cmp = false;
cmp_config_ch0[1].mode = pwm_cmp_mode_output_compare;
cmp_config_ch0[1].update_trigger = pwm_shadow_register_update_on_hw_event;
cmp_config_ch0[1].ex_cmp = 0;
cmp_config_ch0[1].half_clock_cmp = 0;
cmp_config_ch0[1].jitter_cmp = 0;
cmp_config_ch0[2].cmp = u32CmpValue[2];
cmp_config_ch0[2].enable_ex_cmp = false;
cmp_config_ch0[2].mode = pwm_cmp_mode_output_compare;
cmp_config_ch0[2].update_trigger = pwm_shadow_register_update_on_hw_event;
cmp_config_ch0[2].ex_cmp = 0;
cmp_config_ch0[2].half_clock_cmp = 0;
cmp_config_ch0[2].jitter_cmp = 0;
cmp_config_ch0[3].cmp = u32CmpValue[3];
cmp_config_ch0[3].enable_ex_cmp = false;
cmp_config_ch0[3].mode = pwm_cmp_mode_output_compare;
cmp_config_ch0[3].update_trigger = pwm_shadow_register_update_on_hw_event;
cmp_config_ch0[3].ex_cmp = 0;
cmp_config_ch0[3].half_clock_cmp = 0;
cmp_config_ch0[3].jitter_cmp = 0;
if (status_success != pwm_setup_waveform(HPM_PWM0, 1, &pwm_config, 0, cmp_config_ch0, 4)) {
printf("failed to setup waveform for ch0\n");
return status_fail;
}
cmp_config_ch0[0].cmp = zh_led_PWM_FREQ-1;
cmp_config_ch0[0].update_trigger = pwm_shadow_register_update_on_modify;
pwm_load_cmp_shadow_on_match(HPM_PWM0, 4, &cmp_config_ch0[0]);
pwm_issue_shadow_register_lock_event(HPM_PWM0);
/* enable pwm fault protect */
pwm_fault_source_config_t config;
//config.external_fault_active_low = false;
config.source_mask = PWM_GCR_FAULTI0EN_MASK;
config.fault_recover_at_rising_edge = false;
config.fault_output_recovery_trigger = 0;
pwm_config_fault_source(HPM_PWM0, &config);
return status_success;
}
配置TRGM互聯(lián)管理器
互聯(lián)管理器TRGM以PWM的周期來觸發(fā)DMA進(jìn)行數(shù)據(jù)搬運。
static void zh_led_trgm_config(void)
{
trgm_output_t stTrgmOutput;
stTrgmOutput.invert = false;
stTrgmOutput.type = trgm_output_same_as_input;
stTrgmOutput.input = 44;
trgm_output_config(HPM_TRGM0, 14, &stTrgmOutput);
pwm_enable_reload_at_synci(HPM_PWM0);
synt_reset_counter(HPM_SYNT);
synt_set_reload(HPM_SYNT, zh_led_PWM_FREQ-1);
synt_set_comparator(HPM_SYNT, 0, zh_led_PWM_FREQ-1);
}
測試程序如下:
int main(void)
{
unsigned int j=3;
board_init();
zh_led_gpio_config();
zh_led_clk_config(2);
zh_led_trgm_config();
zh_led_dma_config();
printf("start\n");
while(j--)
{
printf("j = %d\n", j);
zh_led_dma_config();
pwm_start_counter(HPM_PWM0);
board_delay_ms(500);
};
printf("stop\n");
while(1);
return 0;
}
串口打印結(jié)果如下:
測量GPIO的波形:
因為HDMA訪問AHB SRAM速度更快,可以將數(shù)據(jù)存儲到AHB SRAM來提高刷新速度。
__attribute__ ((section(".ahb_sram"))) uint32_t g_u32LedBufForGpio32[1024]={0xFFFFFFFF, 0, 0xFFFFFFFF, 0, 0xFFFFFFFF, 0, 0xFFFFFFFF, 0, 0xFFFFFFFF, 0, 0xFFFFFFFF, 0, 0xFFFFFFFF, 0, 0xFFFFFFFF, 0};
使用邏輯分析儀測試波形如下:
用示波器測量波形如下圖所示,信號質(zhì)量表現(xiàn)優(yōu)異。
-
mcu
+關(guān)注
關(guān)注
146文章
17123瀏覽量
350973 -
嵌入式
+關(guān)注
關(guān)注
5082文章
19104瀏覽量
304784 -
dma
+關(guān)注
關(guān)注
3文章
560瀏覽量
100544 -
GPIO
+關(guān)注
關(guān)注
16文章
1204瀏覽量
52051
發(fā)布評論請先 登錄
相關(guān)推薦
評論