1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159 |
- #include "stm32f4xx_sai.h"
- #include "stm32f4xx_rcc.h"
-
- #if defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F429_439xx) || \
- defined (STM32F401xx) || defined (STM32F411xE) || defined (STM32F446xx) || defined (STM32F469_479xx) || \
- defined (STM32F413_423xx)
- #define CR1_CLEAR_MASK ((uint32_t)0xFF07C010)
- #define FRCR_CLEAR_MASK ((uint32_t)0xFFF88000)
- #define SLOTR_CLEAR_MASK ((uint32_t)0x0000F020)
- void SAI_DeInit(SAI_TypeDef* SAIx)
- {
-
- assert_param(IS_SAI_PERIPH(SAIx));
-
- if(SAIx == SAI1)
- {
-
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_SAI1, ENABLE);
-
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_SAI1, DISABLE);
- }
- else
- {
- #if defined(STM32F446xx)
- if(SAIx == SAI2)
- {
-
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_SAI2, ENABLE);
-
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_SAI2, DISABLE);
- }
- #endif
- }
- }
- void SAI_Init(SAI_Block_TypeDef* SAI_Block_x, SAI_InitTypeDef* SAI_InitStruct)
- {
- uint32_t tmpreg = 0;
-
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
-
-
- assert_param(IS_SAI_BLOCK_MODE(SAI_InitStruct->SAI_AudioMode));
- assert_param(IS_SAI_BLOCK_PROTOCOL(SAI_InitStruct->SAI_Protocol));
- assert_param(IS_SAI_BLOCK_DATASIZE(SAI_InitStruct->SAI_DataSize));
- assert_param(IS_SAI_BLOCK_FIRST_BIT(SAI_InitStruct->SAI_FirstBit));
- assert_param(IS_SAI_BLOCK_CLOCK_STROBING(SAI_InitStruct->SAI_ClockStrobing));
- assert_param(IS_SAI_BLOCK_SYNCHRO(SAI_InitStruct->SAI_Synchro));
- assert_param(IS_SAI_BLOCK_SYNCEXT(SAI_InitStruct->SAI_SynchroExt));
- assert_param(IS_SAI_BLOCK_OUTPUT_DRIVE(SAI_InitStruct->SAI_OUTDRIV));
- assert_param(IS_SAI_BLOCK_NODIVIDER(SAI_InitStruct->SAI_NoDivider));
- assert_param(IS_SAI_BLOCK_MASTER_DIVIDER(SAI_InitStruct->SAI_MasterDivider));
- assert_param(IS_SAI_BLOCK_FIFO_THRESHOLD(SAI_InitStruct->SAI_FIFOThreshold));
-
-
- tmpreg = SAI_Block_x->CR1;
-
- tmpreg &= CR1_CLEAR_MASK;
-
-
-
-
-
-
-
-
-
-
-
- tmpreg |= (uint32_t)(SAI_InitStruct->SAI_AudioMode | SAI_InitStruct->SAI_Protocol |
- SAI_InitStruct->SAI_DataSize | SAI_InitStruct->SAI_FirstBit |
- SAI_InitStruct->SAI_ClockStrobing | SAI_InitStruct->SAI_Synchro |
- SAI_InitStruct->SAI_OUTDRIV | SAI_InitStruct->SAI_NoDivider |
- SAI_InitStruct->SAI_SynchroExt | (uint32_t)((SAI_InitStruct->SAI_MasterDivider) << 20));
-
- SAI_Block_x->CR1 = tmpreg;
-
-
-
- tmpreg = SAI_Block_x->CR2;
-
- tmpreg &= ~(SAI_xCR2_FTH);
-
-
- tmpreg |= (uint32_t)(SAI_InitStruct->SAI_FIFOThreshold);
-
- SAI_Block_x->CR2 = tmpreg;
- }
- void SAI_FrameInit(SAI_Block_TypeDef* SAI_Block_x, SAI_FrameInitTypeDef* SAI_FrameInitStruct)
- {
- uint32_t tmpreg = 0;
-
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
-
-
- assert_param(IS_SAI_BLOCK_FRAME_LENGTH(SAI_FrameInitStruct->SAI_FrameLength));
- assert_param(IS_SAI_BLOCK_ACTIVE_FRAME(SAI_FrameInitStruct->SAI_ActiveFrameLength));
- assert_param(IS_SAI_BLOCK_FS_DEFINITION(SAI_FrameInitStruct->SAI_FSDefinition));
- assert_param(IS_SAI_BLOCK_FS_POLARITY(SAI_FrameInitStruct->SAI_FSPolarity));
- assert_param(IS_SAI_BLOCK_FS_OFFSET(SAI_FrameInitStruct->SAI_FSOffset));
-
-
- tmpreg = SAI_Block_x->FRCR;
-
- tmpreg &= FRCR_CLEAR_MASK;
-
-
-
-
-
-
- tmpreg |= (uint32_t)((uint32_t)(SAI_FrameInitStruct->SAI_FrameLength - 1) |
- SAI_FrameInitStruct->SAI_FSOffset |
- SAI_FrameInitStruct->SAI_FSDefinition |
- SAI_FrameInitStruct->SAI_FSPolarity |
- (uint32_t)((SAI_FrameInitStruct->SAI_ActiveFrameLength - 1) << 8));
-
-
- SAI_Block_x->FRCR = tmpreg;
- }
- void SAI_SlotInit(SAI_Block_TypeDef* SAI_Block_x, SAI_SlotInitTypeDef* SAI_SlotInitStruct)
- {
- uint32_t tmpreg = 0;
-
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
-
-
- assert_param(IS_SAI_BLOCK_FIRSTBIT_OFFSET(SAI_SlotInitStruct->SAI_FirstBitOffset));
- assert_param(IS_SAI_BLOCK_SLOT_SIZE(SAI_SlotInitStruct->SAI_SlotSize));
- assert_param(IS_SAI_BLOCK_SLOT_NUMBER(SAI_SlotInitStruct->SAI_SlotNumber));
- assert_param(IS_SAI_SLOT_ACTIVE(SAI_SlotInitStruct->SAI_SlotActive));
-
-
- tmpreg = SAI_Block_x->SLOTR;
-
- tmpreg &= SLOTR_CLEAR_MASK;
-
-
-
-
-
- tmpreg |= (uint32_t)(SAI_SlotInitStruct->SAI_FirstBitOffset |
- SAI_SlotInitStruct->SAI_SlotSize |
- SAI_SlotInitStruct->SAI_SlotActive |
- (uint32_t)((SAI_SlotInitStruct->SAI_SlotNumber - 1) << 8));
-
-
- SAI_Block_x->SLOTR = tmpreg;
- }
- void SAI_StructInit(SAI_InitTypeDef* SAI_InitStruct)
- {
-
-
- SAI_InitStruct->SAI_AudioMode = SAI_Mode_MasterTx;
-
- SAI_InitStruct->SAI_Protocol = SAI_Free_Protocol;
-
- SAI_InitStruct->SAI_DataSize = SAI_DataSize_8b;
-
- SAI_InitStruct->SAI_FirstBit = SAI_FirstBit_MSB;
-
- SAI_InitStruct->SAI_ClockStrobing = SAI_ClockStrobing_FallingEdge;
-
- SAI_InitStruct->SAI_Synchro = SAI_Asynchronous;
-
- SAI_InitStruct->SAI_SynchroExt = SAI_SyncExt_Disable;
-
- SAI_InitStruct->SAI_OUTDRIV = SAI_OutputDrive_Disabled;
-
- SAI_InitStruct->SAI_NoDivider = SAI_MasterDivider_Enabled;
-
- SAI_InitStruct->SAI_MasterDivider = 0;
-
- SAI_InitStruct->SAI_FIFOThreshold = SAI_Threshold_FIFOEmpty;
- }
- void SAI_FrameStructInit(SAI_FrameInitTypeDef* SAI_FrameInitStruct)
- {
-
-
- SAI_FrameInitStruct->SAI_FrameLength = 8;
-
- SAI_FrameInitStruct->SAI_ActiveFrameLength = 1;
-
- SAI_FrameInitStruct->SAI_FSDefinition = SAI_FS_StartFrame;
-
- SAI_FrameInitStruct->SAI_FSPolarity = SAI_FS_ActiveLow;
-
- SAI_FrameInitStruct->SAI_FSOffset = SAI_FS_FirstBit;
- }
- void SAI_SlotStructInit(SAI_SlotInitTypeDef* SAI_SlotInitStruct)
- {
-
-
- SAI_SlotInitStruct->SAI_FirstBitOffset = 0;
-
- SAI_SlotInitStruct->SAI_SlotSize = SAI_SlotSize_DataSize;
-
- SAI_SlotInitStruct->SAI_SlotNumber = 1;
-
- SAI_SlotInitStruct->SAI_SlotActive = SAI_Slot_NotActive;
- }
- void SAI_Cmd(SAI_Block_TypeDef* SAI_Block_x, FunctionalState NewState)
- {
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
- assert_param(IS_FUNCTIONAL_STATE(NewState));
- if (NewState != DISABLE)
- {
-
- SAI_Block_x->CR1 |= SAI_xCR1_SAIEN;
- }
- else
- {
-
- SAI_Block_x->CR1 &= ~(SAI_xCR1_SAIEN);
- }
- }
- void SAI_MonoModeConfig(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_Mono_StreoMode)
- {
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
- assert_param(IS_SAI_BLOCK_MONO_STREO_MODE(SAI_MonoMode));
-
- SAI_Block_x->CR1 &= ~(SAI_xCR1_MONO);
-
- SAI_Block_x->CR1 |= SAI_MonoMode;
- }
- void SAI_TRIStateConfig(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_TRIState)
- {
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
- assert_param(IS_SAI_BLOCK_TRISTATE_MANAGEMENT(SAI_TRIState));
-
- SAI_Block_x->CR1 &= ~(SAI_xCR1_MONO);
-
- SAI_Block_x->CR1 |= SAI_MonoMode;
-
- }
- void SAI_CompandingModeConfig(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_CompandingMode)
- {
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
- assert_param(IS_SAI_BLOCK_COMPANDING_MODE(SAI_CompandingMode));
-
- SAI_Block_x->CR2 &= ~(SAI_xCR2_COMP);
-
- SAI_Block_x->CR2 |= SAI_CompandingMode;
- }
- void SAI_MuteModeCmd(SAI_Block_TypeDef* SAI_Block_x, FunctionalState NewState)
- {
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
- assert_param(IS_FUNCTIONAL_STATE(NewState));
- if (NewState != DISABLE)
- {
-
- SAI_Block_x->CR2 |= SAI_xCR2_MUTE;
- }
- else
- {
-
- SAI_Block_x->CR2 &= ~(SAI_xCR2_MUTE);
- }
- }
- void SAI_MuteValueConfig(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_MuteValue)
- {
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
- assert_param(IS_SAI_BLOCK_MUTE_VALUE(SAI_MuteValue));
-
-
- SAI_Block_x->CR2 &= ~(SAI_xCR2_MUTEVAL);
-
- SAI_Block_x->CR2 |= SAI_MuteValue;
- }
- void SAI_MuteFrameCounterConfig(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_MuteCounter)
- {
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
- assert_param(IS_SAI_BLOCK_MUTE_COUNTER(SAI_MuteCounter));
-
-
- SAI_Block_x->CR2 &= ~(SAI_xCR2_MUTECNT);
-
- SAI_Block_x->CR2 |= (SAI_MuteCounter << 7);
- }
- #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
- defined(STM32F469_479xx) || defined(STM32F413_423xx) || defined(STM32F446xx)
- void SAI_BlockSynchroConfig(SAI_InitTypeDef* SAI_InitStruct, SAI_TypeDef* SAIx)
- {
- uint32_t tmpregisterGCR = 0U;
- #if defined(STM32F446xx)
-
- switch(SAI_InitStruct->SAI_SynchroExt)
- {
- case SAI_SyncExt_Disable :
- tmpregisterGCR = 0U;
- break;
- case SAI_SyncExt_OutBlockA_Enable :
- tmpregisterGCR = SAI_GCR_SYNCOUT_0;
- break;
- case SAI_SyncExt_OutBlockB_Enable :
- tmpregisterGCR = SAI_GCR_SYNCOUT_1;
- break;
- default:
- break;
- }
- if(((SAI_InitStruct->SAI_Synchro) == SAI_Synchronous_Ext) && (SAIx == SAI1))
- {
- tmpregisterGCR |= SAI_GCR_SYNCIN_0;
- }
-
- if(SAIx == SAI1)
- {
- SAI1->GCR = tmpregisterGCR;
- }
- else
- {
- SAI2->GCR = tmpregisterGCR;
- }
- #endif
- #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
- defined(STM32F469_479xx) || defined(STM32F413_423xx)
-
- switch(SAI_InitStruct->SAI_SynchroExt)
- {
- case SAI_SyncExt_Disable :
- tmpregisterGCR = 0U;
- break;
- case SAI_SyncExt_OutBlockA_Enable :
- tmpregisterGCR = SAI_GCR_SYNCOUT_0;
- break;
- case SAI_SyncExt_OutBlockB_Enable :
- tmpregisterGCR = SAI_GCR_SYNCOUT_1;
- break;
- default:
- break;
- }
- SAI1->GCR = tmpregisterGCR;
- #endif
- }
- #endif
- void SAI_FlushFIFO(SAI_Block_TypeDef* SAI_Block_x)
- {
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
-
- SAI_Block_x->CR2 |= SAI_xCR2_FFLUSH;
- }
- uint32_t SAI_ReceiveData(SAI_Block_TypeDef* SAI_Block_x)
- {
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
-
-
- return SAI_Block_x->DR;
- }
- void SAI_SendData(SAI_Block_TypeDef* SAI_Block_x, uint32_t Data)
- {
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
-
-
- SAI_Block_x->DR = Data;
- }
- void SAI_DMACmd(SAI_Block_TypeDef* SAI_Block_x, FunctionalState NewState)
- {
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
- assert_param(IS_FUNCTIONAL_STATE(NewState));
-
- if (NewState != DISABLE)
- {
-
- SAI_Block_x->CR1 |= SAI_xCR1_DMAEN;
- }
- else
- {
-
- SAI_Block_x->CR1 &= ~(SAI_xCR1_DMAEN);
- }
- }
- void SAI_ITConfig(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_IT, FunctionalState NewState)
- {
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
- assert_param(IS_FUNCTIONAL_STATE(NewState));
- assert_param(IS_SAI_BLOCK_CONFIG_IT(SAI_IT));
- if (NewState != DISABLE)
- {
-
- SAI_Block_x->IMR |= SAI_IT;
- }
- else
- {
-
- SAI_Block_x->IMR &= ~(SAI_IT);
- }
- }
- FlagStatus SAI_GetFlagStatus(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_FLAG)
- {
- FlagStatus bitstatus = RESET;
-
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
- assert_param(IS_SAI_BLOCK_GET_FLAG(SAI_FLAG));
-
-
- if ((SAI_Block_x->SR & SAI_FLAG) != (uint32_t)RESET)
- {
-
- bitstatus = SET;
- }
- else
- {
-
- bitstatus = RESET;
- }
-
- return bitstatus;
- }
- void SAI_ClearFlag(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_FLAG)
- {
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
- assert_param(IS_SAI_BLOCK_CLEAR_FLAG(SAI_FLAG));
-
-
- SAI_Block_x->CLRFR |= SAI_FLAG;
- }
- ITStatus SAI_GetITStatus(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_IT)
- {
- ITStatus bitstatus = RESET;
- uint32_t enablestatus = 0;
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
- assert_param(IS_SAI_BLOCK_CONFIG_IT(SAI_IT));
-
-
- enablestatus = (SAI_Block_x->IMR & SAI_IT) ;
-
- if (((SAI_Block_x->SR & SAI_IT) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET))
- {
-
- bitstatus = SET;
- }
- else
- {
-
- bitstatus = RESET;
- }
-
- return bitstatus;
- }
- void SAI_ClearITPendingBit(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_IT)
- {
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
- assert_param(IS_SAI_BLOCK_CONFIG_IT(SAI_IT));
-
- SAI_Block_x->CLRFR |= SAI_IT;
- }
- FunctionalState SAI_GetCmdStatus(SAI_Block_TypeDef* SAI_Block_x)
- {
- FunctionalState state = DISABLE;
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
- if ((SAI_Block_x->CR1 & (uint32_t)SAI_xCR1_SAIEN) != 0)
- {
-
- state = ENABLE;
- }
- else
- {
-
- state = DISABLE;
- }
- return state;
- }
- uint32_t SAI_GetFIFOStatus(SAI_Block_TypeDef* SAI_Block_x)
- {
- uint32_t tmpreg = 0;
-
-
- assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
-
-
- tmpreg = (uint32_t)((SAI_Block_x->SR & SAI_xSR_FLVL));
-
- return tmpreg;
- }
-
- #endif
-
-
|