1010#include "parse.h"
1111#include "obj.h"
1212#include "runtime.h"
13+ #include "timer.h"
1314#include "dac.h"
1415
15- TIM_HandleTypeDef TIM6_Handle ;
1616STATIC DAC_HandleTypeDef DAC_Handle ;
1717
1818void dac_init (void ) {
@@ -22,19 +22,8 @@ void dac_init(void) {
2222}
2323
2424STATIC void TIM6_Config (uint freq ) {
25- // TIM6 clock enable
26- __TIM6_CLK_ENABLE ();
27-
28- // Compute the prescaler value so TIM6 triggers at freq-Hz
29- uint16_t period = (uint16_t ) ((SystemCoreClock / 2 ) / freq ) - 1 ;
30-
31- // time base clock configuration
32- TIM6_Handle .Instance = TIM6 ;
33- TIM6_Handle .Init .Period = period ;
34- TIM6_Handle .Init .Prescaler = 0 ; // timer runs at SystemCoreClock / 2
35- TIM6_Handle .Init .ClockDivision = 0 ; // unused for TIM6
36- TIM6_Handle .Init .CounterMode = TIM_COUNTERMODE_UP ; // unused for TIM6
37- HAL_TIM_Base_Init (& TIM6_Handle );
25+ // Init TIM6 at the required frequency (in Hz)
26+ timer_tim6_init (freq );
3827
3928 // TIM6 TRGO selection
4029 TIM_MasterConfigTypeDef config ;
@@ -203,17 +192,14 @@ mp_obj_t pyb_dac_dma(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
203192 DAC_Init(self->dac_channel, &DAC_InitStructure);
204193 */
205194
206- if (self -> state != 3 ) {
207- DAC_ChannelConfTypeDef config ;
208- config .DAC_Trigger = DAC_TRIGGER_T6_TRGO ;
209- config .DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE ;
210- HAL_DAC_ConfigChannel (& DAC_Handle , & config , self -> dac_channel );
211- self -> state = 3 ;
212- }
213-
214195 // DMA1_Stream[67] channel7 configuration
215196 DMA_HandleTypeDef DMA_Handle ;
216197 DMA_Handle .Instance = self -> dma_stream ;
198+
199+ // Need to deinit DMA first
200+ DMA_Handle .State = HAL_DMA_STATE_READY ;
201+ HAL_DMA_DeInit (& DMA_Handle );
202+
217203 DMA_Handle .Init .Channel = DMA_CHANNEL_7 ;
218204 DMA_Handle .Init .Direction = DMA_MEMORY_TO_PERIPH ;
219205 DMA_Handle .Init .PeriphInc = DMA_PINC_DISABLE ;
@@ -231,6 +217,18 @@ mp_obj_t pyb_dac_dma(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
231217
232218 __HAL_LINKDMA (& DAC_Handle , DMA_Handle1 , DMA_Handle );
233219
220+ DAC_Handle .Instance = DAC ;
221+ DAC_Handle .State = HAL_DAC_STATE_RESET ;
222+ HAL_DAC_Init (& DAC_Handle );
223+
224+ if (self -> state != 3 ) {
225+ DAC_ChannelConfTypeDef config ;
226+ config .DAC_Trigger = DAC_TRIGGER_T6_TRGO ;
227+ config .DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE ;
228+ HAL_DAC_ConfigChannel (& DAC_Handle , & config , self -> dac_channel );
229+ self -> state = 3 ;
230+ }
231+
234232 HAL_DAC_Start_DMA (& DAC_Handle , self -> dac_channel , (uint32_t * )bufinfo .buf , bufinfo .len , DAC_ALIGN_8B_R );
235233
236234 /*
0 commit comments