DMAを使用してDAC CubeMXで生成したコードを流用

Revision:
0:0ea9475fc06f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DMAC.cpp	Tue Jun 26 06:27:49 2018 +0000
@@ -0,0 +1,120 @@
+#include "DMAC.h"
+
+/* HAL objects */
+static DAC_HandleTypeDef hdac;
+static DMA_HandleTypeDef hdma_dac1;
+static TIM_HandleTypeDef htim6;
+
+void DMAC::init()
+{
+    MX_DMA_Init();
+    MX_TIM6_Init();
+    MX_DAC_Init();
+    
+    // disable interrupt
+    HAL_NVIC_DisableIRQ(DMA1_Stream5_IRQn);
+    
+    // start DAC
+    HAL_TIM_Base_Start(&htim6);
+    HAL_DAC_Start(&hdac,DAC_CHANNEL_1);
+}
+
+void DMAC::play(const uint16_t* data, uint32_t len)
+{
+    // stop DMA
+    HAL_DAC_Stop_DMA(&hdac, DAC_CHANNEL_1);
+    
+    // start DMA
+    HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)data, len/2, DAC_ALIGN_12B_R);
+}
+
+
+
+/* HAL functions */
+void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
+{
+  if(htim_base->Instance==TIM6)
+  {
+    __HAL_RCC_TIM6_CLK_ENABLE();
+  }
+}
+
+void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac)
+{
+  GPIO_InitTypeDef GPIO_InitStruct;
+  if(hdac->Instance==DAC)
+  {
+    /* Peripheral clock enable */
+    __HAL_RCC_DAC_CLK_ENABLE();
+  
+    /**DAC GPIO Configuration    
+    PA4     ------> DAC_OUT1 
+    */
+    GPIO_InitStruct.Pin = GPIO_PIN_4;
+    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
+    GPIO_InitStruct.Pull = GPIO_NOPULL;
+    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+
+    /* DAC DMA Init */
+    /* DAC1 Init */
+    hdma_dac1.Instance = DMA1_Stream5;
+    hdma_dac1.Init.Channel = DMA_CHANNEL_7;
+    hdma_dac1.Init.Direction = DMA_MEMORY_TO_PERIPH;
+    hdma_dac1.Init.PeriphInc = DMA_PINC_DISABLE;
+    hdma_dac1.Init.MemInc = DMA_MINC_ENABLE;
+    hdma_dac1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
+    hdma_dac1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
+    hdma_dac1.Init.Mode = DMA_NORMAL;
+    hdma_dac1.Init.Priority = DMA_PRIORITY_LOW;
+    hdma_dac1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
+    HAL_DMA_Init(&hdma_dac1);
+
+    __HAL_LINKDMA(hdac,DMA_Handle1,hdma_dac1);
+  }
+}
+
+/* DAC init function */
+void DMAC::MX_DAC_Init()
+{
+  DAC_ChannelConfTypeDef sConfig;
+
+  /* DAC Initialization */
+  hdac.Instance = DAC;
+  HAL_DAC_Init(&hdac);
+
+  /*DAC channel OUT1 config */
+  sConfig.DAC_Trigger = DAC_TRIGGER_T6_TRGO;
+  sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
+  HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1);
+}
+
+/* TIM6 init function */
+void DMAC::MX_TIM6_Init()
+{
+
+  TIM_MasterConfigTypeDef sMasterConfig;
+
+  htim6.Instance = TIM6;
+  htim6.Init.Prescaler = 0;
+  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
+  htim6.Init.Period = 90 - 1;
+  HAL_TIM_Base_Init(&htim6);
+  
+  sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
+  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
+  HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig);
+}
+
+/* Enable DMA controller clock */
+void DMAC::MX_DMA_Init() 
+{
+  /* DMA controller clock enable */
+  __HAL_RCC_DMA1_CLK_ENABLE();
+
+  /* DMA interrupt init */
+  /* DMA1_Stream5_IRQn interrupt configuration */
+  HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 0, 0);
+  HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);
+
+}
+