Platform/ECM3532/M3/hw/hal/ecm3532/pdm/src/pdm_hal.c
/**
 * Initialize PDM channel
 * @param sPdm channel configuration
 * @param fPcmCb PCM fram callback function
 * @return 0 on success, non-zero error code on failure
 */
Initialize PDM channel
@param sPdm channel configuration
@param fPcmCb PCM fram callback function
@return 0 on success, non-zero error code on failure
Initialize PDM channel @param sPdm channel configuration @param fPcmCb PCM fram callback function @return 0 on success non - zero error code on failure
int ecm3532_pdm_init(tPdmcfg *sPdm, tPCMFrameCb fCb, void *cbPtr)
{
  tPdmClkSrc pdmClk;
  tAudRPCfg *audRcfg;
  void *aBuf;
  uint16_t bLen = 0;
  if (!sPdm || !fCb)
    return -EINVAL;
  else if ((sPdm->pdmNum > CONFIG_PDM_COUNT) || (sPdm->sRate > PCM16KHZ) ||
      (sPdm->cMode > STEREO))
  {
    ecm35xx_printf ("Invalid configuration\r\n");
    return -EINVAL;
  }
  else if (sPdmDev[PDM_INDEX(sPdm->pdmNum)].st)
  {
    return -EINPROGRESS;
  }
  pdmClk = CONFIG_PDM_CLK_SRC;
  switch(pdmClk)
  {
    case (ePdmClkSrc2MhzXtal):
      {
        REG_M1(RTC_OSC_CSR2, VHFO_PD_N, 1);
        EtaCspPdmFence();
        EtaCspSocCtrlAdcFreqSet(eSocCtrlAdcSelXtal2MHz);
        break;
      }
    case (ePdmClkSrc2MhzHfo):
      {
        EtaCspSocCtrlAdcFreqSet(eSocCtrlAdcSelHfo2MHz);
        break;
      }
    case (ePdmClkSrc1MhzHfo):
      {
        EtaCspSocCtrlAdcFreqSet(eSocCtrlAdcSelHfo1MHz);
        break;
      }
    case (ePdmClkSrcFromPad):
      {
        EtaCspSocCtrlAdcFreqSet(eSocCtrlAdcSelPad2MHz);
        break;
      }
    default:
      {
        return(ePdmConfigInvalidConfig);
        break;
      }
  }
  REGN_M1(sPdm->pdmNum, PDM_PCMA_CTRL, SFTRST, 1);
  vTaskDelay(50);
  REGN_M1(sPdm->pdmNum, PDM_PDM_CORE_CONF, PGA_R, CONFIG_PGA_R);
  REGN_M1(sPdm->pdmNum, PDM_PDM_CORE_CONF, PGA_L, CONFIG_PGA_L);
#ifdef CONFIG_HP_FILTER
  REGN_M2(sPdm->pdmNum, PDM_PDM_CORE_CONF, ADCHPD, 1,
            HPGAIN,CONFIG_HP_COEF);
#endif
  REGN_M1(sPdm->pdmNum, PDM_PDM_CORE_CONF, SINC_RATE, 0x40);
  REGN_M1(sPdm->pdmNum, PDM_PDM_CORE_CONF, MCLKDIV, 0x0);
  REGN_M1(sPdm->pdmNum, PDM_PDM_CORE2_CONF, PDMCKO_DLY, 0x3);
  if(sPdm->sRate == PCM16KHZ)
  {
    REGN_M1(sPdm->pdmNum, PDM_PDM_CORE2_CONF, DIV_MCLKQ,
        BFV_PDM_PDM_CORE2_CONF_DIV_MCLKQ_BYPASS);
    bLen = (16 * sPdm->rFLen);
  }
  else
  {
    REGN_M1(sPdm->pdmNum, PDM_PDM_CORE2_CONF, DIV_MCLKQ,
        BFV_PDM_PDM_CORE2_CONF_DIV_MCLKQ_DIV_BY_1);
    bLen = (8 * sPdm->rFLen);
  }
  if (sPdm->cMode == STEREO)
  {
    bLen = bLen * 2;
  }
  audRcfg = SharedMemAlloc(sizeof(tAudRPCfg));
  if (!audRcfg) {
    ecm35xx_printf("Fail to alloc shared memory\r\n");
    configASSERT(0);
  }
  audioDspRpcInit();
  aBuf = SharedMemAlloc(bLen * sizeof(int16_t) * FRAME_CNT);
  if (!aBuf)
  {
    ecm35xx_printf("shmem mallox failed for PCM buffer\r\n");
    configASSERT(0);
  }
  sPdmDev[PDM_INDEX(sPdm->pdmNum)].fAppCb = fCb;
  sPdmDev[PDM_INDEX(sPdm->pdmNum)].cbPtr = cbPtr;
  sPdmDev[PDM_INDEX(sPdm->pdmNum)].fBLen = bLen;
  sPdmDev[PDM_INDEX(sPdm->pdmNum)].fPtr = aBuf;
  audRcfg->pdmNum = sPdm->pdmNum;
  audRcfg->cMode = sPdm->cMode;
  audRcfg->bInfo.fCnt = FRAME_CNT;
  audRcfg->bInfo.sFbuf.Flen = bLen;
  audRcfg->bInfo.sFbuf.fPtrHi = ((int)(aBuf) >> 16);
  audRcfg->bInfo.sFbuf.fPtrLo = ((int)(aBuf)& 0xFFFF);
  rpcSubmitWork(RPC_MODULE_ID_AUDIO, AUDIO_INIT, audRcfg);
  xSemaphoreTake(pdmDspRespSem, portMAX_DELAY);
  SharedMemFree(audRcfg);
  sPdmDev[PDM_INDEX(sPdm->pdmNum)].st = AUDIO_INIT;
  return 0;
}
int ecm3532_pdm_init ( tPdmcfg * sPdm , tPCMFrameCb fCb , void * cbPtr ) { tPdmClkSrc pdmClk ; tAudRPCfg * audRcfg ; void * aBuf ; uint16_t bLen = 0 ; if ( ! sPdm || ! fCb ) return - EINVAL ; else if ( ( sPdm -> pdmNum > CONFIG_PDM_COUNT ) || ( sPdm -> sRate > PCM16KHZ ) || ( sPdm -> cMode > STEREO ) ) { ecm35xx_printf ( " \r \n " ) ; return - EINVAL ; } else if ( sPdmDev [ PDM_INDEX ( sPdm -> pdmNum ) ] . st ) { return - EINPROGRESS ; } pdmClk = CONFIG_PDM_CLK_SRC ; switch ( pdmClk ) { case ( ePdmClkSrc2MhzXtal ) : { REG_M1 ( RTC_OSC_CSR2 , VHFO_PD_N , 1 ) ; EtaCspPdmFence ( ) ; EtaCspSocCtrlAdcFreqSet ( eSocCtrlAdcSelXtal2MHz ) ; break ; } case ( ePdmClkSrc2MhzHfo ) : { EtaCspSocCtrlAdcFreqSet ( eSocCtrlAdcSelHfo2MHz ) ; break ; } case ( ePdmClkSrc1MhzHfo ) : { EtaCspSocCtrlAdcFreqSet ( eSocCtrlAdcSelHfo1MHz ) ; break ; } case ( ePdmClkSrcFromPad ) : { EtaCspSocCtrlAdcFreqSet ( eSocCtrlAdcSelPad2MHz ) ; break ; } default : { return ( ePdmConfigInvalidConfig ) ; break ; } } REGN_M1 ( sPdm -> pdmNum , PDM_PCMA_CTRL , SFTRST , 1 ) ; vTaskDelay ( 50 ) ; REGN_M1 ( sPdm -> pdmNum , PDM_PDM_CORE_CONF , PGA_R , CONFIG_PGA_R ) ; REGN_M1 ( sPdm -> pdmNum , PDM_PDM_CORE_CONF , PGA_L , CONFIG_PGA_L ) ; #ifdef CONFIG_HP_FILTER REGN_M2 ( sPdm -> pdmNum , PDM_PDM_CORE_CONF , ADCHPD , 1 , HPGAIN , CONFIG_HP_COEF ) ; #endif REGN_M1 ( sPdm -> pdmNum , PDM_PDM_CORE_CONF , SINC_RATE , 0x40 ) ; REGN_M1 ( sPdm -> pdmNum , PDM_PDM_CORE_CONF , MCLKDIV , 0x0 ) ; REGN_M1 ( sPdm -> pdmNum , PDM_PDM_CORE2_CONF , PDMCKO_DLY , 0x3 ) ; if ( sPdm -> sRate == PCM16KHZ ) { REGN_M1 ( sPdm -> pdmNum , PDM_PDM_CORE2_CONF , DIV_MCLKQ , BFV_PDM_PDM_CORE2_CONF_DIV_MCLKQ_BYPASS ) ; bLen = ( 16 * sPdm -> rFLen ) ; } else { REGN_M1 ( sPdm -> pdmNum , PDM_PDM_CORE2_CONF , DIV_MCLKQ , BFV_PDM_PDM_CORE2_CONF_DIV_MCLKQ_DIV_BY_1 ) ; bLen = ( 8 * sPdm -> rFLen ) ; } if ( sPdm -> cMode == STEREO ) { bLen = bLen * 2 ; } audRcfg = SharedMemAlloc ( sizeof ( tAudRPCfg ) ) ; if ( ! audRcfg ) { ecm35xx_printf ( " \r \n " ) ; configASSERT ( 0 ) ; } audioDspRpcInit ( ) ; aBuf = SharedMemAlloc ( bLen * sizeof ( int16_t ) * FRAME_CNT ) ; if ( ! aBuf ) { ecm35xx_printf ( " \r \n " ) ; configASSERT ( 0 ) ; } sPdmDev [ PDM_INDEX ( sPdm -> pdmNum ) ] . fAppCb = fCb ; sPdmDev [ PDM_INDEX ( sPdm -> pdmNum ) ] . cbPtr = cbPtr ; sPdmDev [ PDM_INDEX ( sPdm -> pdmNum ) ] . fBLen = bLen ; sPdmDev [ PDM_INDEX ( sPdm -> pdmNum ) ] . fPtr = aBuf ; audRcfg -> pdmNum = sPdm -> pdmNum ; audRcfg -> cMode = sPdm -> cMode ; audRcfg -> bInfo . fCnt = FRAME_CNT ; audRcfg -> bInfo . sFbuf . Flen = bLen ; audRcfg -> bInfo . sFbuf . fPtrHi = ( ( int ) ( aBuf ) >> 16 ) ; audRcfg -> bInfo . sFbuf . fPtrLo = ( ( int ) ( aBuf ) & 0xFFFF ) ; rpcSubmitWork ( RPC_MODULE_ID_AUDIO , AUDIO_INIT , audRcfg ) ; xSemaphoreTake ( pdmDspRespSem , portMAX_DELAY ) ; SharedMemFree ( audRcfg ) ; sPdmDev [ PDM_INDEX ( sPdm -> pdmNum ) ] . st = AUDIO_INIT ; return 0 ; }
Initialize PDM channel
 @param sPdm channel configuration
 @param fPcmCb PCM fram callback function
 @return 0 on success, non-zero error code on failure
Initialize PDM channel @param sPdm channel configuration @param fPcmCb PCM fram callback function @return 0 on success non - zero error code on failure
// Power up the 16MHz oscillator // Fence // Select ACLK as clock source /* confifure PDM */ /* need only 2 frame in pingpong mode */