//------------------------------------------------- // . //------------------------------------------------- #define dst_src (3<<30) // - 16 (). #define src_inc (1<<26) // 16 . #define src_size (1<<24) // 16 . #define dst_size (1<<28) // 16 . . #define n_minus_1 (49<<4) //50 (-1) DMA. #define cycle_ctrl (3<<0) //-. struct DAC_ST { uint32_t Destination_end_pointer; // . uint32_t Source_end_pointer; // uint32_t channel_cfg; // . uint32_t NULL; // . } __align(1024) DAC_ST; // 1024 . struct DAC_ST DAC_ST_ADC[32+32]; // . /// = 16 , / = 16 , , 50 , -. uint32_t DMA_DAC_InitST_PR = dst_src|src_inc|src_size|dst_size|n_minus_1|cycle_ctrl; uint32_t DMA_DAC_InitST_ALT = dst_src|src_inc|src_size|dst_size|n_minus_1|cycle_ctrl;
// . DAC_ST_ADC[10-1].Destination_end_pointer = (uint32_t)C_4 + (sizeof(C_4))/2 - 1; // (C_4 - 100 ). DAC_ST_ADC[10-1].Source_end_pointer = (uint32_t)&(DAC->DAC2_DATA); // ( ) ( DAC). DAC_ST_ADC[10-1].channel_cfg = (uint32_t)(DMA_DAC_InitST_PR); // . DAC_ST_ADC[10-1].NULL = (uint32_t)0; // . // . DAC_ST_ADC[10-1+32].Destination_end_pointer = (uint32_t)C_4 + sizeof(C_4) - 1; // (C_4 - 100 ). DAC_ST_ADC[10-1+32].Source_end_pointer = (uint32_t)&(DAC->DAC2_DATA); // ( ) ( DAC). DAC_ST_ADC[10-1+32].channel_cfg = (uint32_t)(DMA_DAC_InitST_ALT); // . DAC_ST_ADC[10-1+32].NULL = (uint32_t)0; // .
#define CFG_master_enable (1<<0)// . #define PCLK_EN_DMA (1<<5)// DMA. // DMA. RST_CLK->PER_CLOCK|=PCLK_EN_DMA; // DMA. DMA->CTRL_BASE_PTR = (uint32_t)&DAC_ST_ADC; // . DMA->CFG = CFG_master_enable; // DMA.
/** @defgroup DMA_valid_channels DMA valid channels * @{ */ #define DMA_Channel_UART1_TX ((uint8_t)(0)) #define DMA_Channel_UART1_RX ((uint8_t)(1)) #define DMA_Channel_UART2_TX ((uint8_t)(2)) #define DMA_Channel_UART2_RX ((uint8_t)(3)) #define DMA_Channel_SSP1_TX ((uint8_t)(4)) #define DMA_Channel_SSP1_RX ((uint8_t)(5)) #define DMA_Channel_SSP2_TX ((uint8_t)(6)) #define DMA_Channel_SSP2_RX ((uint8_t)(7)) #define DMA_Channel_ADC1 ((uint8_t)(8)) #define DMA_Channel_ADC2 ((uint8_t)(9)) #define DMA_Channel_TIM1 ((uint8_t)(10)) #define DMA_Channel_TIM2 ((uint8_t)(11)) #define DMA_Channel_TIM3 ((uint8_t)(12)) #define DMA_Channel_SW1 ((uint8_t)(13)) #define DMA_Channel_SW2 ((uint8_t)(14)) #define DMA_Channel_SW3 ((uint8_t)(15)) #define DMA_Channel_SW4 ((uint8_t)(16)) #define DMA_Channel_SW5 ((uint8_t)(17)) #define DMA_Channel_SW6 ((uint8_t)(18)) #define DMA_Channel_SW7 ((uint8_t)(19)) #define DMA_Channel_SW8 ((uint8_t)(20)) #define DMA_Channel_SW9 ((uint8_t)(21)) #define DMA_Channel_SW10 ((uint8_t)(22)) #define DMA_Channel_SW11 ((uint8_t)(23)) #define DMA_Channel_SW12 ((uint8_t)(24)) #define DMA_Channel_SW13 ((uint8_t)(25)) #define DMA_Channel_SW14 ((uint8_t)(26)) #define DMA_Channel_SW15 ((uint8_t)(27)) #define DMA_Channel_SW16 ((uint8_t)(28)) #define DMA_Channel_SW17 ((uint8_t)(29)) #define DMA_Channel_SW18 ((uint8_t)(30)) #define DMA_Channel_SW19 ((uint8_t)(31)) #define IS_DMA_CHANNEL(CHANNEL) (CHANNEL <= (DMA_Channels_Number - 1)) /** @} */ /* End of group DMA_valid_channels */
// . DMA->CHNL_ENABLE_SET = 1<<10; // 10 .
//------------------------------------------------- // DMA DAC. //------------------------------------------------- void DMA_to_DAC_and_TIM1 (void) { // . DAC_ST_ADC[10-1].Destination_end_pointer = (uint32_t)C_4 + (sizeof(C_4))/2 - 1; // (C_4 - 100 ). DAC_ST_ADC[10-1].Source_end_pointer = (uint32_t)&(DAC->DAC2_DATA); // ( ) ( DAC). DAC_ST_ADC[10-1].channel_cfg = (uint32_t)(DMA_DAC_InitST_PR); // . DAC_ST_ADC[10-1].NULL = (uint32_t)0; // . // . DAC_ST_ADC[10-1+32].Destination_end_pointer = (uint32_t)C_4 + sizeof(C_4) - 1; // (C_4 - 100 ). DAC_ST_ADC[10-1+32].Source_end_pointer = (uint32_t)&(DAC->DAC2_DATA); // ( ) ( DAC). DAC_ST_ADC[10-1+32].channel_cfg = (uint32_t)(DMA_DAC_InitST_ALT); // . DAC_ST_ADC[10-1+32].NULL = (uint32_t)0; // . // DMA. RST_CLK->PER_CLOCK|=PCLK_EN_DMA; // DMA. DMA->CTRL_BASE_PTR = (uint32_t)&DAC_ST_ADC; // . DMA->CFG = CFG_master_enable; // DMA. // . DMA->CHNL_ENABLE_SET = 1<<10; // 10 . }
#define PER_CLOCK_TIMER1_ONCLK (1<<14) // 1. RST_CLK->PER_CLOCK |= PER_CLOCK_TIMER1_ONCLK; // .
#define CNTRL_CNT_EN (1<<0) // . TIMER1->CNTRL |= CNTRL_CNT_EN; // , = .
TIMER1->ARR = 0xFFFF; // ...
Next you need to associate our timer with DMA. As the event for which the transmission will occur, we select CNT == ARR in timer 1. This is a simple comparison of the current value of the CNT timer with the number in the ARR register. #define PER_CLOCK_TIMER1_ONCLK (1<<14) // 1. #define TIM_CLOCK_TIM1_CLK_EN (1<<24) // 1. #define SHARE_HCLK_TIMER 7 // HCLK ... (0 = , 1 = /2, 2 = /4). #define TIM_CLOCK_TIM1_BRG (SHARE_HCLK_TIMER<<0) // 1 SHARE_HCLK_TIMER. #define CNTRL_CNT_EN (1<<0) // . #define DMA_RE_CNT_ARR_EVENT_RE (1<<1) // DMA CNT == ARR; void Init_TIMER1_to_DMA_and_DAC2 (void) { RST_CLK->PER_CLOCK |= PER_CLOCK_TIMER1_ONCLK; // . TIMER1->CNTRL |= CNTRL_CNT_EN; // , = . TIMER1->ARR = 0xFFFF; // ... TIMER1->DMA_RE |= DMA_RE_CNT_ARR_EVENT_RE; // "" DMA. RST_CLK->TIM_CLOCK |= TIM_CLOCK_TIM1_CLK_EN|TIM_CLOCK_TIM1_BRG; // . }
RST_CLK->PER_CLOCK |= PER_CLOCK_TIMER1_ONCLK|PER_CLOCK_TIMER2_ONCLK; // 1 2.
RST_CLK->TIM_CLOCK = 0; // .
// 8000000 /261.63/100 = 305. TIMER1->ARR = 305; // ... TIMER1->DMA_RE |= DMA_RE_CNT_ARR_EVENT_RE; // DMA.
TIMER2->ARR = 305*25; // .
#define TIMERx_IE_CNT_ARR_EVENT_IE (1<<1) //: CNT ARR. TIMER2->IE = TIMERx_IE_CNT_ARR_EVENT_IE; // .
TIMER2->STATUS=0; // .
DCD Timer2_IRQHandler ; IRQ15
NVIC->ISER[0] = 1<<15; // 2.
void Timer2_IRQHandler (void) // . TIMER2->STATUS=0; }
#define CNTRL_CNT_EN (1<<0) // . #define TIM_CLOCK_TIM1_CLK_EN (1<<24) // 1. #define TIM_CLOCK_TIM2_CLK_EN (1<<25) // 2. TIMER1->CNTRL = CNTRL_CNT_EN; // . TIMER2->CNTRL = CNTRL_CNT_EN; RST_CLK->TIM_CLOCK = TIM_CLOCK_TIM1_CLK_EN|TIM_CLOCK_TIM2_CLK_EN; // .
#define PER_CLOCK_TIMER1_ONCLK (1<<14) // 1. #define PER_CLOCK_TIMER2_ONCLK (1<<15) // 1. #define TIM_CLOCK_TIM1_CLK_EN (1<<24) // 1. #define SHARE_HCLK_TIMER1 7 // HCLK ... (0 = , 1 = /2, 2 = /4). #define TIM_CLOCK_TIM1_BRG (SHARE_HCLK_TIMER1<<0) // 1 SHARE_HCLK_TIMER. #define CNTRL_CNT_EN (1<<0) // . #define CNTRL_EVENT_SEL (1<<8) // : CNT == ARR; #define DMA_RE_CNT_ARR_EVENT_RE (1<<1) // DMA CNT == ARR; #define IE_CNT_ARR_EVENT_IE (1<<1) // CNT == ARR; #define TIM_CLOCK_TIM2_CLK_EN (1<<25) // 2. #define SHARE_HCLK_TIMER2 7 // HCLK ... (0 = , 1 = /2, 2 = /4). #define TIM_CLOCK_TIM2_BRG (SHARE_HCLK_TIMER2<<8)// 1 SHARE_HCLK_TIMER. #define CH1_CNTRL_CAP_nPWM_Z (1<<15) //: "". #define CH1_CNTRL_CHPSC_8 (3<<6) //: 8. #define CHy_CNTRL2_CCR1_EN (1<<2) //: 1. #define TIMERx_IE_CNT_ARR_EVENT_IE (1<<1) //: CNT ARR. void Init_TIMER1_to_DMA_and_DAC2 (void) { RST_CLK->PER_CLOCK |= PER_CLOCK_TIMER1_ONCLK|PER_CLOCK_TIMER2_ONCLK; // 1 2. RST_CLK->TIM_CLOCK = 0; // . // 8000000 /261.63/100 = 305. TIMER1->ARR = 305; // ... TIMER1->DMA_RE |= DMA_RE_CNT_ARR_EVENT_RE; // DMA. TIMER2->ARR = 305*25; // . TIMER2->IE = TIMERx_IE_CNT_ARR_EVENT_IE; // . TIMER2->STATUS=0; // . NVIC->ISER[0] = 1<<15; // 2. TIMER1->CNTRL = CNTRL_CNT_EN; // . TIMER2->CNTRL = CNTRL_CNT_EN; RST_CLK->TIM_CLOCK = TIM_CLOCK_TIM1_CLK_EN|TIM_CLOCK_TIM2_CLK_EN; // . }
#define ST_Play_P (DAC_ST_ADC[10-1].channel_cfg & (1023<<4)) // . #define ST_Play_ALT (DAC_ST_ADC[10-1+32].channel_cfg & (1023<<4)) // . void Timer2_IRQHandler (void) // . { if ((ST_Play_P == 0) && (ST_Play_ALT <= (48<<4))) // - 2-. DAC_ST_ADC[10-1].channel_cfg = (uint32_t)(DMA_DAC_InitST_PR); if ((ST_Play_ALT == 0) && (ST_Play_P <= (48<<4))) DAC_ST_ADC[10-1+32].channel_cfg = (uint32_t)(DMA_DAC_InitST_ALT); DMA->CHNL_ENABLE_SET = 1<<10; TIMER2->STATUS=0; }
int main (void) { HSE_Clock_ON(); // HSE . HSE_Clock_OffPLL(); // "" HSE . Buzzer_out_DAC_init(); // . DAC_Init(); // . DMA_to_DAC_and_TIM1(); // DMA DAC2 1. Init_TIMER1_to_DMA_and_DAC2(); // 1. while (1) { } }
Source: https://habr.com/ru/post/256577/
All Articles