CN / EN
文档反馈
感谢关注汇顶文档,期待您的宝贵建议!
感谢您的反馈,祝您愉快!
无匹配项 共计114个匹配页面

示例代码

I2S Master和Slave中断传输

示例实现I2S之间的通信,其中一端为Master主设备,另一端为Slave从设备。工程路径:SDK_Folder\projects\peripheral\i2s\app_i2s

示例工程流程图

图 18 I2S Master和Slave中断传输流程

初始化参数

I2S主设备初始化参数,从设备类似:

app_i2s_params_t i2sm_params = {
   .id = APP_I2S_ID_MASTER,
   .pin_cfg = {
       .ws = {
           .type = APP_I2S_MASTER_WS_IO_TYPE,
           .mux  = APP_I2S_MASTER_WS_PINMUX,
           .pin  = APP_I2S_MASTER_WS_PIN,
           .pull = APP_IO_NOPULL,
       },
       .sdo = {
           .type = APP_I2S_MASTER_SDO_IO_TYPE,
           .mux  = APP_I2S_MASTER_SDO_PINMUX,
           .pin  = APP_I2S_MASTER_SDO_PIN,
           .pull = APP_IO_NOPULL,
       },
       .sdi = {
           .type = APP_I2S_MASTER_SDI_IO_TYPE,
           .mux  = APP_I2S_MASTER_SDI_PINMUX,
           .pin  = APP_I2S_MASTER_SDI_PIN,
           .pull = APP_IO_NOPULL,
       },
       .sclk = {
           .type = APP_I2S_MASTER_SCLK_IO_TYPE,
           .mux  = APP_I2S_MASTER_SCLK_PINMUX,
           .pin  = APP_I2S_MASTER_SCLK_PIN,
           .pull = APP_IO_NOPULL,
       },
   },
    .dma_cfg = {
#if (APP_DRIVER_CHIP_TYPE != APP_DRIVER_GR551X)
        .tx_dma_instance = DMA1,
        .rx_dma_instance = DMA1,
#else
        .tx_dma_instance = DMA,
        .rx_dma_instance = DMA,
#endif
        .tx_dma_channel  = DMA_Channel2,
        .rx_dma_channel  = DMA_Channel3,
    },
    .init = {
        .data_size    = I2S_DATASIZE_32BIT,
        .clock_source = I2S_CLOCK_SRC_96M,
 #if (APP_DRIVER_CHIP_TYPE != APP_DRIVER_GR551X)
        .ws_cycles   = I2S_WS_CYCLES_32,
 #endif
        .audio_freq = 48000,
    }, };
  • id:角色配置

    APP_I2S_ID_MASTER:主设备

  • pin_cfg:引脚配置

    主要配置IO类型、复用MUX模式、引脚和上下拉模式,具体选择的参数请参考board_SK.h

    以GR5526为例,下面是具体IO配置:

    /*******I2S IO CONFIG***************************/  
    #define APP_I2S_MASTER_WS_PIN               APP_IO_PIN_6
    #define APP_I2S_MASTER_SDO_PIN              APP_IO_PIN_7
    #define APP_I2S_MASTER_SDI_PIN              APP_IO_PIN_8
    #define APP_I2S_MASTER_SCLK_PIN             APP_IO_PIN_9
    #define APP_I2S_MASTER_WS_IO_TYPE           APP_IO_TYPE_GPIOA
    #define APP_I2S_MASTER_SDO_IO_TYPE          APP_IO_TYPE_GPIOA
    #define APP_I2S_MASTER_SDI_IO_TYPE          APP_IO_TYPE_GPIOA
    #define APP_I2S_MASTER_SCLK_IO_TYPE         APP_IO_TYPE_GPIOA
    #define APP_I2S_MASTER_WS_PINMUX            APP_IO_MUX_3
    #define APP_I2S_MASTER_SDO_PINMUX           APP_IO_MUX_3
    #define APP_I2S_MASTER_SDI_PINMUX           APP_IO_MUX_3
    #define APP_I2S_MASTER_SCLK_PINMUX          APP_IO_MUX_3
    
    #define APP_I2S_SLAVE_WS_PIN                APP_IO_PIN_3
    #define APP_I2S_SLAVE_SDO_PIN               APP_IO_PIN_2
    #define APP_I2S_SLAVE_SDI_PIN               APP_IO_PIN_1
    #define APP_I2S_SLAVE_SCLK_PIN              APP_IO_PIN_0
    #define APP_I2S_SLAVE_WS_IO_TYPE            APP_IO_TYPE_MSIO
    #define APP_I2S_SLAVE_SDO_IO_TYPE           APP_IO_TYPE_MSIO
    #define APP_I2S_SLAVE_SDI_IO_TYPE           APP_IO_TYPE_MSIO
    #define APP_I2S_SLAVE_SCLK_IO_TYPE          APP_IO_TYPE_MSIO
    #define APP_I2S_SLAVE_WS_PINMUX             APP_IO_MUX_4
    #define APP_I2S_SLAVE_SDO_PINMUX            APP_IO_MUX_4
    #define APP_I2S_SLAVE_SDI_PINMUX            APP_IO_MUX_4
    #define APP_I2S_SLAVE_SCLK_PINMUX           APP_IO_MUX_4
    

    使用MSIO3作为Slave端WS引脚,MSIO0作为Slave端SCL引脚,MSIO2作为Slave端SDO引脚,MSIO1作为Slave端SDI引脚。使用GPIO6作为Master端WS引脚,GPIO9作为Master端SCL引脚,GPIO7作为Master端SDO引脚,GPIO8作为Master端SDI引脚。

  • dma_cfg:DMA配置

    • dma_cfg. tx_dma_instance:I2S发送使用的DMA实例
    • dma_cfg. rx_dma_instance:I2S接收使用的DMA实例
    • dma_cfg. tx_dma_channel:I2S发送使用的DMA实例通道
    • dma_cfg. rx_dma_channel:I2S接收使用的DMA实例通道
  • init:I2S传输参数配置

    • init.data_size:I2S数据位宽I2S_DATASIZE_32BIT,数据位宽32 bits
    • init. clock_source:I2S时钟源I2S_CLOCK_SRC_96M,选择96 MHz的时钟源
    • init. ws_cycles:I2S WS时钟长度I2S_WS_CYCLES_32,32个BCLK时钟周期代表传输32 bits的数据
    • init. audio_freq:I2S音频采样频率48000 Hz

重要函数

  • I2S初始化
    ret = app_i2s_init(&i2sm_params, app_i2sm_callback)
  • I2S清理TX FIFO
    app_i2s_flush_tx_fifo(APP_I2S_ID_MASTER);
  • I2S清理RX FIFO
    app_i2s_flush_rx_fifo(APP_I2S_ID_MASTER)
  • I2S接收数据
    app_i2s_receive_async(APP_I2S_ID_SLAVE, rdata, sizeof(rdata) >> 2)
  • I2S发送数据
    app_i2s_transmit_async(APP_I2S_ID_MASTER, wdata, sizeof(wdata) >> 2)

测试验证

  1. 引脚连接
    表 202 引脚连接方式
    芯片 连接方式
    GR5526(SK板GPIO6、GPIO7、GPIO9默认连接Codec芯片)
    • GPIO6连接MSIO3
    • GPIO7连接MSIO1
    • GPIO8连接MSIO2
    • GPIO9连接MSIO0
    GR551x
    • GPIO24连接AON2
    • GPIO25连接AON4
    • GPIO16连接AON3
    • GPIO17连接AON5
  2. 连接开发板到PC端,打开串口助手,连接串口,查看串口日志如下:
    I2SM interrupt write start.
    Please check I2SS Received: 
    0x00010000 0x00030002 0x00050004 0x00070006 
    0x00090008 0x000B000A 0x000D000C 0x000F000E 
    0x00110010 0x00130012 0x00150014 0x00170016 
    0x00190018 0x001B001A 0x001D001C 0x001F001E 
    0x00210020 0x00230022 0x00250024 0x00270026 
    0x00290028 0x002B002A 0x002D002C 0x002F002E 
    0x00310030 0x00330032 0x00350034 0x00370036 
    0x00390038 0x003B003A 0x003D003C 0x003F003E 
    0x00410040 0x00430042 0x00450044 0x00470046 
    0x00490048 0x004B004A 0x004D004C 0x004F004E 
    0x00510050 0x00530052 0x00550054 0x00570056 
    0x00590058 0x005B005A 0x005D005C 0x005F005E 
    0x00610060 0x00630062 0x00650064 0x00670066 
    0x00690068 0x006B006A 0x006D006C 0x006F006E 
    0x00710070 0x00730072 0x00750074 0x00770076 
    0x00790078 0x007B007A 0x007D007C 0x007F007E 
    0x00810080 0x00830082 0x00850084 0x00870086 
    0x00890088 0x008B008A 0x008D008C 0x008F008E 
    0x00910090 0x00930092 0x00950094 0x00970096 
    0x00990098 0x009B009A 0x009D009C 0x009F009E 
    0x00A100A0 0x00A300A2 0x00A500A4 0x00A700A6 
    0x00A900A8 0x00AB00AA 0x00AD00AC 0x00AF00AE 
    0x00B100B0 0x00B300B2 0x00B500B4 0x00B700B6 
    0x00B900B8 0x00BB00BA 0x00BD00BC 0x00BF00BE 
    0x00C100C0 0x00C300C2 0x00C500C4 0x00C700C6 
    0x00C900C8 0x00CB00CA 0x00CD00CC 0x00CF00CE 
    0x00D100D0 0x00D300D2 0x00D500D4 0x00D700D6 
    0x00D900D8 0x00DB00DA 0x00DD00DC 0x00DF00DE 
    0x00E100E0 0x00E300E2 0x00E500E4 0x00E700E6 
    0x00E900E8 0x00EB00EA 0x00ED00EC 0x00EF00EE 
    0x00F100F0 0x00F300F2 0x00F500F4 0x00F700F6 
    0x00F900F8 0x00FB00FA 0x00FD00FC 0x00FF00FE 
    0x01010100 0x01030102 0x01050104 0x01070106 
    0x01090108 0x010B010A 0x010D010C 0x010F010E 
    0x01110110 0x01130112 0x01150114 0x01170116 
    0x01190118 0x011B011A 0x011D011C 0x011F011E 
    0x01210120 0x01230122 0x01250124 0x01270126 
    0x01290128 0x012B012A 0x012D012C 0x012F012E 
    0x01310130 0x01330132 0x01350134 0x01370136 
    0x01390138 0x013B013A 0x013D013C 0x013F013E 
    0x01410140 0x01430142 0x01450144 0x01470146 
    0x01490148 0x014B014A 0x014D014C 0x014F014E 
    0x01510150 0x01530152 0x01550154 0x01570156 
    0x01590158 0x015B015A 0x015D015C 0x015F015E 
    0x01610160 0x01630162 0x01650164 0x01670166 
    0x01690168 0x016B016A 0x016D016C 0x016F016E 
    0x01710170 0x01730172 0x01750174 0x01770176 
    0x01790178 0x017B017A 0x017D017C 0x017F017E 
    0x01810180 0x01830182 0x01850184 0x01870186 
    0x01890188 0x018B018A 0x018D018C 0x018F018E 
    0x01910190 0x01930192 0x01950194 0x01970196 
    0x01990198 0x019B019A 0x019D019C 0x019F019E 
    0x01A101A0 0x01A301A2 0x01A501A4 0x01A701A6 
    0x01A901A8 0x01AB01AA 0x01AD01AC 0x01AF01AE 
    0x01B101B0 0x01B301B2 0x01B501B4 0x01B701B6 
    0x01B901B8 0x01BB01BA 0x01BD01BC 0x01BF01BE 
    0x01C101C0 0x01C301C2 0x01C501C4 0x01C701C6 
    0x01C901C8 0x01CB01CA 0x01CD01CC 0x01CF01CE 
    0x01D101D0 0x01D301D2 0x01D501D4 0x01D701D6 
    0x01D901D8 0x01DB01DA 0x01DD01DC 0x01DF01DE 
    0x01E101E0 0x01E301E2 0x01E501E4 0x01E701E6 
    0x01E901E8 0x01EB01EA 0x01ED01EC 0x01EF01EE 
    0x01F101F0 0x01F301F2 0x01F501F4 0x01F701F6 
    0x01F901F8 0x01FB01FA 0x01FD01FC 0x01FF01FE 
    
    
    I2SM interrupt read start.
    Please check I2SM Received: 
    0x00010000 0x00030002 0x00050004 0x00070006 
    0x00090008 0x000B000A 0x000D000C 0x000F000E 
    0x00110010 0x00130012 0x00150014 0x00170016 
    0x00190018 0x001B001A 0x001D001C 0x001F001E 
    0x00210020 0x00230022 0x00250024 0x00270026 
    0x00290028 0x002B002A 0x002D002C 0x002F002E 
    0x00310030 0x00330032 0x00350034 0x00370036 
    0x00390038 0x003B003A 0x003D003C 0x003F003E 
    0x00410040 0x00430042 0x00450044 0x00470046 
    0x00490048 0x004B004A 0x004D004C 0x004F004E 
    0x00510050 0x00530052 0x00550054 0x00570056 
    0x00590058 0x005B005A 0x005D005C 0x005F005E 
    0x00610060 0x00630062 0x00650064 0x00670066 
    0x00690068 0x006B006A 0x006D006C 0x006F006E 
    0x00710070 0x00730072 0x00750074 0x00770076 
    0x00790078 0x007B007A 0x007D007C 0x007F007E 
    0x00810080 0x00830082 0x00850084 0x00870086 
    0x00890088 0x008B008A 0x008D008C 0x008F008E 
    0x00910090 0x00930092 0x00950094 0x00970096 
    0x00990098 0x009B009A 0x009D009C 0x009F009E 
    0x00A100A0 0x00A300A2 0x00A500A4 0x00A700A6 
    0x00A900A8 0x00AB00AA 0x00AD00AC 0x00AF00AE 
    0x00B100B0 0x00B300B2 0x00B500B4 0x00B700B6 
    0x00B900B8 0x00BB00BA 0x00BD00BC 0x00BF00BE 
    0x00C100C0 0x00C300C2 0x00C500C4 0x00C700C6 
    0x00C900C8 0x00CB00CA 0x00CD00CC 0x00CF00CE 
    0x00D100D0 0x00D300D2 0x00D500D4 0x00D700D6 
    0x00D900D8 0x00DB00DA 0x00DD00DC 0x00DF00DE 
    0x00E100E0 0x00E300E2 0x00E500E4 0x00E700E6 
    0x00E900E8 0x00EB00EA 0x00ED00EC 0x00EF00EE 
    0x00F100F0 0x00F300F2 0x00F500F4 0x00F700F6 
    0x00F900F8 0x00FB00FA 0x00FD00FC 0x00FF00FE 
    0x01010100 0x01030102 0x01050104 0x01070106 
    0x01090108 0x010B010A 0x010D010C 0x010F010E 
    0x01110110 0x01130112 0x01150114 0x01170116 
    0x01190118 0x011B011A 0x011D011C 0x011F011E 
    0x01210120 0x01230122 0x01250124 0x01270126 
    0x01290128 0x012B012A 0x012D012C 0x012F012E 
    0x01310130 0x01330132 0x01350134 0x01370136 
    0x01390138 0x013B013A 0x013D013C 0x013F013E 
    0x01410140 0x01430142 0x01450144 0x01470146 
    0x01490148 0x014B014A 0x014D014C 0x014F014E 
    0x01510150 0x01530152 0x01550154 0x01570156 
    0x01590158 0x015B015A 0x015D015C 0x015F015E 
    0x01610160 0x01630162 0x01650164 0x01670166 
    0x01690168 0x016B016A 0x016D016C 0x016F016E 
    0x01710170 0x01730172 0x01750174 0x01770176 
    0x01790178 0x017B017A 0x017D017C 0x017F017E 
    0x01810180 0x01830182 0x01850184 0x01870186 
    0x01890188 0x018B018A 0x018D018C 0x018F018E 
    0x01910190 0x01930192 0x01950194 0x01970196 
    0x01990198 0x019B019A 0x019D019C 0x019F019E 
    0x01A101A0 0x01A301A2 0x01A501A4 0x01A701A6 
    0x01A901A8 0x01AB01AA 0x01AD01AC 0x01AF01AE 
    0x01B101B0 0x01B301B2 0x01B501B4 0x01B701B6 
    0x01B901B8 0x01BB01BA 0x01BD01BC 0x01BF01BE 
    0x01C101C0 0x01C301C2 0x01C501C4 0x01C701C6 
    0x01C901C8 0x01CB01CA 0x01CD01CC 0x01CF01CE 
    0x01D101D0 0x01D301D2 0x01D501D4 0x01D701D6 
    0x01D901D8 0x01DB01DA 0x01DD01DC 0x01DF01DE 
    0x01E101E0 0x01E301E2 0x01E501E4 0x01E701E6 
    0x01E901E8 0x01EB01EA 0x01ED01EC 0x01EF01EE 
    0x01F101F0 0x01F301F2 0x01F501F4 0x01F701F6 
    0x01F901F8 0x01FB01FA 0x01FD01FC 0x01FF01FE
    

I2S Master和Slave DMA传输

示例实现I2S之间的DMA通信,其中一端为Master主设备,另一端为Slave从设备。工程路径:SDK_Folder\projects\peripheral\i2s\app_i2s

示例工程流程图

图 19 I2S Master和Slave DMA传输流程

重要函数

  • I2S初始化
    ret = app_i2s_init(&i2sm_params, app_i2sm_callback);
  • 配置DMA模式
    ret = app_i2s_dma_init(&i2sm_params);
  • I2S清理TX FIFO
    app_i2s_flush_tx_fifo(APP_I2S_ID_MASTER);
  • I2S清理RX FIFO
    app_i2s_flush_rx_fifo(APP_I2S_ID_MASTER)
  • I2S接收数据
    app_i2s_receive_async(APP_I2S_ID_SLAVE, rdata, sizeof(rdata) >> 2)
  • I2S发送数据
    app_i2s_transmit_async(APP_I2S_ID_MASTER, wdata, sizeof(wdata) >> 2)

测试验证

  1. 引脚连接
    表 203 引脚连接方式
    芯片 连接方式
    GR5526(SK板GPIO6、GPIO7、GPIO9默认连接Codec芯片)
    • GPIO6连接MSIO3
    • GPIO7连接MSIO1
    • GPIO8连接MSIO2
    • GPIO9连接MSIO0
    GR551x
    • GPIO24连接AON2
    • GPIO25连接AON4
    • GPIO16连接AON3
    • GPIO17连接AON5
  2. 连接开发板到PC端,打开串口助手,连接串口,查看串口日志如下:
    I2SM dma write start.
    Please check I2SS Received: 
    0x00010000 0x00030002 0x00050004 0x00070006 
    0x00090008 0x000B000A 0x000D000C 0x000F000E 
    0x00110010 0x00130012 0x00150014 0x00170016 
    0x00190018 0x001B001A 0x001D001C 0x001F001E 
    0x00210020 0x00230022 0x00250024 0x00270026 
    0x00290028 0x002B002A 0x002D002C 0x002F002E 
    0x00310030 0x00330032 0x00350034 0x00370036 
    0x00390038 0x003B003A 0x003D003C 0x003F003E 
    0x00410040 0x00430042 0x00450044 0x00470046 
    0x00490048 0x004B004A 0x004D004C 0x004F004E 
    0x00510050 0x00530052 0x00550054 0x00570056 
    0x00590058 0x005B005A 0x005D005C 0x005F005E 
    0x00610060 0x00630062 0x00650064 0x00670066 
    0x00690068 0x006B006A 0x006D006C 0x006F006E 
    0x00710070 0x00730072 0x00750074 0x00770076 
    0x00790078 0x007B007A 0x007D007C 0x007F007E 
    0x00810080 0x00830082 0x00850084 0x00870086 
    0x00890088 0x008B008A 0x008D008C 0x008F008E 
    0x00910090 0x00930092 0x00950094 0x00970096 
    0x00990098 0x009B009A 0x009D009C 0x009F009E 
    0x00A100A0 0x00A300A2 0x00A500A4 0x00A700A6 
    0x00A900A8 0x00AB00AA 0x00AD00AC 0x00AF00AE 
    0x00B100B0 0x00B300B2 0x00B500B4 0x00B700B6 
    0x00B900B8 0x00BB00BA 0x00BD00BC 0x00BF00BE 
    0x00C100C0 0x00C300C2 0x00C500C4 0x00C700C6 
    0x00C900C8 0x00CB00CA 0x00CD00CC 0x00CF00CE 
    0x00D100D0 0x00D300D2 0x00D500D4 0x00D700D6 
    0x00D900D8 0x00DB00DA 0x00DD00DC 0x00DF00DE 
    0x00E100E0 0x00E300E2 0x00E500E4 0x00E700E6 
    0x00E900E8 0x00EB00EA 0x00ED00EC 0x00EF00EE 
    0x00F100F0 0x00F300F2 0x00F500F4 0x00F700F6 
    0x00F900F8 0x00FB00FA 0x00FD00FC 0x00FF00FE 
    0x01010100 0x01030102 0x01050104 0x01070106 
    0x01090108 0x010B010A 0x010D010C 0x010F010E 
    0x01110110 0x01130112 0x01150114 0x01170116 
    0x01190118 0x011B011A 0x011D011C 0x011F011E 
    0x01210120 0x01230122 0x01250124 0x01270126 
    0x01290128 0x012B012A 0x012D012C 0x012F012E 
    0x01310130 0x01330132 0x01350134 0x01370136 
    0x01390138 0x013B013A 0x013D013C 0x013F013E 
    0x01410140 0x01430142 0x01450144 0x01470146 
    0x01490148 0x014B014A 0x014D014C 0x014F014E 
    0x01510150 0x01530152 0x01550154 0x01570156 
    0x01590158 0x015B015A 0x015D015C 0x015F015E 
    0x01610160 0x01630162 0x01650164 0x01670166 
    0x01690168 0x016B016A 0x016D016C 0x016F016E 
    0x01710170 0x01730172 0x01750174 0x01770176 
    0x01790178 0x017B017A 0x017D017C 0x017F017E 
    0x01810180 0x01830182 0x01850184 0x01870186 
    0x01890188 0x018B018A 0x018D018C 0x018F018E 
    0x01910190 0x01930192 0x01950194 0x01970196 
    0x01990198 0x019B019A 0x019D019C 0x019F019E 
    0x01A101A0 0x01A301A2 0x01A501A4 0x01A701A6 
    0x01A901A8 0x01AB01AA 0x01AD01AC 0x01AF01AE 
    0x01B101B0 0x01B301B2 0x01B501B4 0x01B701B6 
    0x01B901B8 0x01BB01BA 0x01BD01BC 0x01BF01BE 
    0x01C101C0 0x01C301C2 0x01C501C4 0x01C701C6 
    0x01C901C8 0x01CB01CA 0x01CD01CC 0x01CF01CE 
    0x01D101D0 0x01D301D2 0x01D501D4 0x01D701D6 
    0x01D901D8 0x01DB01DA 0x01DD01DC 0x01DF01DE 
    0x01E101E0 0x01E301E2 0x01E501E4 0x01E701E6 
    0x01E901E8 0x01EB01EA 0x01ED01EC 0x01EF01EE 
    0x01F101F0 0x01F301F2 0x01F501F4 0x01F701F6 
    0x01F901F8 0x01FB01FA 0x01FD01FC 0x01FF01FE 
    
    
    I2SM dma read start.
    Please check I2SM Received: 
    0x00010000 0x00030002 0x00050004 0x00070006 
    0x00090008 0x000B000A 0x000D000C 0x000F000E 
    0x00110010 0x00130012 0x00150014 0x00170016 
    0x00190018 0x001B001A 0x001D001C 0x001F001E 
    0x00210020 0x00230022 0x00250024 0x00270026 
    0x00290028 0x002B002A 0x002D002C 0x002F002E 
    0x00310030 0x00330032 0x00350034 0x00370036 
    0x00390038 0x003B003A 0x003D003C 0x003F003E 
    0x00410040 0x00430042 0x00450044 0x00470046 
    0x00490048 0x004B004A 0x004D004C 0x004F004E 
    0x00510050 0x00530052 0x00550054 0x00570056 
    0x00590058 0x005B005A 0x005D005C 0x005F005E 
    0x00610060 0x00630062 0x00650064 0x00670066 
    0x00690068 0x006B006A 0x006D006C 0x006F006E 
    0x00710070 0x00730072 0x00750074 0x00770076 
    0x00790078 0x007B007A 0x007D007C 0x007F007E 
    0x00810080 0x00830082 0x00850084 0x00870086 
    0x00890088 0x008B008A 0x008D008C 0x008F008E 
    0x00910090 0x00930092 0x00950094 0x00970096 
    0x00990098 0x009B009A 0x009D009C 0x009F009E 
    0x00A100A0 0x00A300A2 0x00A500A4 0x00A700A6 
    0x00A900A8 0x00AB00AA 0x00AD00AC 0x00AF00AE 
    0x00B100B0 0x00B300B2 0x00B500B4 0x00B700B6 
    0x00B900B8 0x00BB00BA 0x00BD00BC 0x00BF00BE 
    0x00C100C0 0x00C300C2 0x00C500C4 0x00C700C6 
    0x00C900C8 0x00CB00CA 0x00CD00CC 0x00CF00CE 
    0x00D100D0 0x00D300D2 0x00D500D4 0x00D700D6 
    0x00D900D8 0x00DB00DA 0x00DD00DC 0x00DF00DE 
    0x00E100E0 0x00E300E2 0x00E500E4 0x00E700E6 
    0x00E900E8 0x00EB00EA 0x00ED00EC 0x00EF00EE 
    0x00F100F0 0x00F300F2 0x00F500F4 0x00F700F6 
    0x00F900F8 0x00FB00FA 0x00FD00FC 0x00FF00FE 
    0x01010100 0x01030102 0x01050104 0x01070106 
    0x01090108 0x010B010A 0x010D010C 0x010F010E 
    0x01110110 0x01130112 0x01150114 0x01170116 
    0x01190118 0x011B011A 0x011D011C 0x011F011E 
    0x01210120 0x01230122 0x01250124 0x01270126 
    0x01290128 0x012B012A 0x012D012C 0x012F012E 
    0x01310130 0x01330132 0x01350134 0x01370136 
    0x01390138 0x013B013A 0x013D013C 0x013F013E 
    0x01410140 0x01430142 0x01450144 0x01470146 
    0x01490148 0x014B014A 0x014D014C 0x014F014E 
    0x01510150 0x01530152 0x01550154 0x01570156 
    0x01590158 0x015B015A 0x015D015C 0x015F015E 
    0x01610160 0x01630162 0x01650164 0x01670166 
    0x01690168 0x016B016A 0x016D016C 0x016F016E 
    0x01710170 0x01730172 0x01750174 0x01770176 
    0x01790178 0x017B017A 0x017D017C 0x017F017E 
    0x01810180 0x01830182 0x01850184 0x01870186 
    0x01890188 0x018B018A 0x018D018C 0x018F018E 
    0x01910190 0x01930192 0x01950194 0x01970196 
    0x01990198 0x019B019A 0x019D019C 0x019F019E 
    0x01A101A0 0x01A301A2 0x01A501A4 0x01A701A6 
    0x01A901A8 0x01AB01AA 0x01AD01AC 0x01AF01AE 
    0x01B101B0 0x01B301B2 0x01B501B4 0x01B701B6 
    0x01B901B8 0x01BB01BA 0x01BD01BC 0x01BF01BE 
    0x01C101C0 0x01C301C2 0x01C501C4 0x01C701C6 
    0x01C901C8 0x01CB01CA 0x01CD01CC 0x01CF01CE 
    0x01D101D0 0x01D301D2 0x01D501D4 0x01D701D6 
    0x01D901D8 0x01DB01DA 0x01DD01DC 0x01DF01DE 
    0x01E101E0 0x01E301E2 0x01E501E4 0x01E701E6 
    0x01E901E8 0x01EB01EA 0x01ED01EC 0x01EF01EE 
    0x01F101F0 0x01F301F2 0x01F501F4 0x01F701F6 
    0x01F901F8 0x01FB01FA 0x01FD01FC 0x01FF01FE 
    
    
    This example demo end.
    

I2S和PDM麦克风

开发板带PDM模块和编解码Codec芯片,示例使用PDM模块作为音频输入,通过I2S传输音频到Codec芯片,播放音乐。工程路径:SDK_Folder\projects\peripheral\pdm\app_loopback。该示例仅适用于GR5526。

示例工程流程图

图 20 简易麦克风实现流程

初始化参数

  1. PDM初始化参数
    app_pdm_params_t g_pdm_params = {
        .pin_cfg = {
            .clk = {
                .type = APP_IO_TYPE_GPIOA,
                .mux  = APP_IO_MUX_1,
                .pin  = APP_IO_PIN_2,
                .pull = APP_IO_NOPULL,
            },
            .data = {
                .type = APP_IO_TYPE_GPIOA,
                .mux  = APP_IO_MUX_1,
                .pin  = APP_IO_PIN_3,
                .pull = APP_IO_NOPULL,
            },
        },
        .dma_cfg = {
            .dma_instance = DMA1,
            .dma_channel  = DMA_Channel0,
        },
        .init = {
            .mode   = PDM_MODE_STEREO,
            .gain_l = 0x400U, // gain(db): 20 * log(0x400/1024) = 0db, 20 * log(0x800/1024) = 6db
            .gain_r = 0x400U, // gain(db): 20 * log(0x400/1024) = 0db, 20 * log(0x800/1024) = 6db
            .sample_rate = PDM_SAMPLE_RATE_16K,
        },
    };
    
    • pin_cfg:引脚配置
      • pin_cfg.clk:PDM时钟引脚配置,IO类型为APP_IO_TYPE_GPIOA,引脚为APP_IO_PIN_2,复用方式为APP_IO_MUX_1,并配置为上拉模式APP_IO_NOPULL,即PDM时钟引脚为GPIO2
      • pin_cfg.data:PDM数据引脚配置,IO类型为APP_IO_TYPE_GPIOA,引脚为APP_IO_PIN_3,复用方式为APP_IO_MUX_1,并配置为上拉模式APP_IO_NOPULL,即PDM时钟引脚为GPIO3
    • dma_cfg:DMA参数配置
      • dma_cfg.dma_instance:指定使用DMA1实例
      • dma_cfg. dma_channel:指定使用DMA1实例通道,选择通道0
    • init:传输参数配置
      • init.mode:立体声采样PDM_MODE_STEREO
      • init.gain_l:左声道增益
      • init.gain_r:右声道增益
      • init.sample_rate:采样频率PDM_SAMPLE_RATE_16K,即16 KHz
  2. I2S初始化参数
    app_i2s_params_t g_i2s_master_params = {
       .id = APP_I2S_ID_MASTER,
       .pin_cfg = {
           .ws = {
               .type = APP_IO_TYPE_GPIOA,
               .mux  = APP_IO_MUX_3,
               .pin  = APP_IO_PIN_6,
               .pull = APP_IO_NOPULL,
           },
           .sdo = {
               .type = APP_IO_TYPE_GPIOA,
               .mux  = APP_IO_MUX_3,
               .pin  = APP_IO_PIN_7,
               .pull = APP_IO_NOPULL,
           },
           .sdi = {
               .type = APP_IO_TYPE_GPIOA,
               .mux  = APP_IO_MUX_3,
               .pin  = APP_IO_PIN_8,
               .pull = APP_IO_NOPULL,
           },
           .sclk = {
               .type = APP_IO_TYPE_GPIOA,
               .mux  = APP_IO_MUX_3,
               .pin  = APP_IO_PIN_9,
               .pull = APP_IO_NOPULL,
           },
       },
       .dma_cfg = {
           .tx_dma_instance = I2S_DMA,
           .rx_dma_instance = I2S_DMA,
           .tx_dma_channel  = DMA_Channel2,
           .rx_dma_channel  = DMA_Channel3,
       },
       .init = {
           .data_size    = I2S_DATASIZE_16BIT,
           .clock_source = I2S_CLOCK_SRC_64M,
           .ws_cycles    = I2S_WS_CYCLES_16,
           .audio_freq   = 16000,
       },
    };
    
    • id:I2S角色定义,为APP_I2S_ID_MASTER
    • pin_cfg:I2S引脚定义,包含ws、sdo、sdi、sclk引脚的IO类型、引脚、复用模式和上下拉模式
    • dma_cfg:DMA参数配置
      • dma_cfg.tx_dma_instance:指定I2S发送引脚使用的实例
      • dma_cfg.rx_dma_instance:指定I2S接收引脚使用的实例,定义如下:
        #if (APP_DRIVER_CHIP_TYPE == APP_DRIVER_GR551x)
        #define I2S_DMA                    DMA
        #else
        #define I2S_DMA                    DMA1
        #endif
        
      • dma_cfg.tx_dma_channel:指定I2S发送引脚使用的实例通道,选择DMA_Channel2,即通道2
      • dma_cfg.rx_dma_channel:指定I2S接收引脚使用的实例通道,选择DMA_Channel3,即通道3
    • init:传输参数配置
      • init.data_size:I2S数据位宽I2S_DATASIZE_16BIT,数据位宽16 bits

    init.clock_source:I2S时钟源I2S_CLOCK_SRC_64M,选择64 MHz的时钟源

    init.ws_cycles:I2S WS时钟长度I2S_WS_CYCLES_16,16个BCLK时钟周期代表传输32 bits的数据

    init.audio_freq:I2S音频采样频率,16000 Hz

重要函数

  • 初始化PDM
    uint16_t ret = loopback_pdm_init()
  • 初始化I2S
    ret = loopback_i2s_master_init(APP_I2S_TYPE_DMA)
  • PDM开启音频数据采集传输
    ret = app_pdm_stereo_start_dma_sg_llp(loopback_ctrl_p->pdm_audio_buf, PDM_AUDIO_FRAME_LEN, &loopback_ctrl_p->pdm_dma.sg_llp_config)
  • I2S将音频数据传输到Codec芯片
    ret = loopback_i2s_stereo_start(block_num, &loopback_ctrl_p->i2s_audio_buf[block_num * I2S_AUDIO_FRAME_LEN * I2S_CHANNEL_NUM], I2S_AUDIO_FRAME_LEN)

测试验证

  1. 开发板连接到PC端,打开串口助手,打印如下,表明初始化成功,开始数据传输。
    loopback_pdm_init ret = 0
    loopback_i2s_master_init ret = 0
    loopback_pdm_dma_start!
  2. 连接耳机到开发板,对着麦克风讲话,通过耳机听到所讲内容。

扫描关注

打开微信,使用“扫一扫”即可关注。