From bf7941cffbcf78643308934c1a5795adbdfad27e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sergi=20Hern=C3=A1ndez?= <shernand@iri.upc.edu>
Date: Sat, 29 Mar 2014 17:56:20 +0000
Subject: [PATCH] Added an ADC module using DMA.

---
 include/adc_dma.h             |   9 +++
 include/dynamixel_slave_spi.h |  16 ----
 include/ram.h                 |   6 ++
 src/adc_dma.c                 | 146 ++++++++++++++++++++++++++++++++++
 4 files changed, 161 insertions(+), 16 deletions(-)
 create mode 100644 include/adc_dma.h
 delete mode 100644 include/dynamixel_slave_spi.h
 create mode 100644 include/ram.h
 create mode 100644 src/adc_dma.c

diff --git a/include/adc_dma.h b/include/adc_dma.h
new file mode 100644
index 0000000..c2bdcdd
--- /dev/null
+++ b/include/adc_dma.h
@@ -0,0 +1,9 @@
+#ifndef _ADC_DAM_H
+#define _ADC_DMA_H
+
+#include "stm32f4xx.h"
+
+void adc_init(void);
+uint16_t adc_get_temperature(void);
+
+#endif
diff --git a/include/dynamixel_slave_spi.h b/include/dynamixel_slave_spi.h
deleted file mode 100644
index cbf764a..0000000
--- a/include/dynamixel_slave_spi.h
+++ /dev/null
@@ -1,16 +0,0 @@
-#ifndef _DYNAMIXEL_SLAVE_SPI_H
-#define _DYNAMIXEL_SLAVE_SPI_H
-
-#include "stm32f4xx.h"
-#include "dynamixel.h"
-
-// public functions
-void dyn_slave_init(void);
-void dyn_slave_set_address(uint8_t id);
-uint8_t dyn_slave_get_address(void);
-uint8_t dyn_slave_is_packet_ready(void);
-void dyn_slave_get_inst_packet(uint8_t *packet);
-void dyn_slave_send_status_packet(uint8_t error,uint8_t length, uint8_t *data);
-void dyn_slave_resend_status_packet(uint8_t *packet);
-
-#endif
diff --git a/include/ram.h b/include/ram.h
new file mode 100644
index 0000000..94a1512
--- /dev/null
+++ b/include/ram.h
@@ -0,0 +1,6 @@
+#ifndef _RAM_H
+#define _RAM_H
+
+uint8_t ram_data[256];
+
+#endif
diff --git a/src/adc_dma.c b/src/adc_dma.c
new file mode 100644
index 0000000..eab7f26
--- /dev/null
+++ b/src/adc_dma.c
@@ -0,0 +1,146 @@
+#include "adc_dma.h"
+ 
+#define ADC1_CH1           ADC_Channel_TempSensor
+#define ADC1_CH2           ADC_Channel_8
+#define ADC1_CH3           ADC_Channel_10
+#define ADC1_CH4           ADC_Channel_12
+
+#define ADC2_CH1           ADC_Channel_14
+#define ADC2_CH2           ADC_Channel_9
+#define ADC2_CH3           ADC_Channel_11
+#define ADC2_CH4           ADC_Channel_13
+
+#define ADC1_CH2_PIN       GPIO_Pin_8
+#define ADC1_CH2_PORT      GPIOB
+#define ADC1_CH2_PORT_CLK  RCC_AHB1Periph_GPIOB
+#define ADC1_CH3_PIN       GPIO_Pin_0
+#define ADC1_CH3_PORT      GPIOC
+#define ADC1_CH3_PORT_CLK  RCC_AHB1Periph_GPIOC
+#define ADC1_CH4_PIN       GPIO_Pin_2
+#define ADC1_CH4_PORT      GPIOC
+#define ADC1_CH4_PORT_CLK  RCC_AHB1Periph_GPIOC
+
+#define ADC2_CH1_PIN       GPIO_Pin_4
+#define ADC2_CH1_PORT      GPIOC
+#define ADC2_CH1_PORT_CLK  RCC_AHB1Periph_GPIOC
+#define ADC2_CH2_PIN       GPIO_Pin_1
+#define ADC2_CH2_PORT      GPIOB
+#define ADC2_CH2_PORT_CLK  RCC_AHB1Periph_GPIOB
+#define ADC2_CH3_PIN       GPIO_Pin_1
+#define ADC2_CH3_PORT      GPIOC
+#define ADC2_CH3_PORT_CLK  RCC_AHB1Periph_GPIOC
+#define ADC2_CH4_PIN       GPIO_Pin_3
+#define ADC2_CH4_PORT      GPIOC
+#define ADC2_CH4_PORT_CLK  RCC_AHB1Periph_GPIOC
+
+#define ADC_CCR_ADDRESS    ((uint32_t)0x40012308)
+
+// private variables
+uint8_t adc_data[16];
+
+// public functions
+void adc_init(void)
+{
+  ADC_CommonInitTypeDef ADC_CommonInitStructure;
+  ADC_InitTypeDef ADC_InitStructure;
+  GPIO_InitTypeDef GPIO_InitStructure;
+  DMA_InitTypeDef DMA_InitStructure;
+
+  /* enable clocks */
+  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
+  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
+  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
+  RCC_AHB1PeriphClockCmd(ADC1_CH2_PORT_CLK | ADC1_CH3_PORT_CLK | ADC1_CH4_PORT_CLK, ENABLE);
+  RCC_AHB1PeriphClockCmd(ADC2_CH1_PORT_CLK | ADC2_CH2_PORT_CLK | ADC2_CH3_PORT_CLK | ADC2_CH4_PORT_CLK, ENABLE);
+
+  /* DMA Config */
+  DMA_InitStructure.DMA_Channel = DMA_Channel_0;
+  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)adc_data;
+  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC_CCR_ADDRESS;
+  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
+  DMA_InitStructure.DMA_BufferSize = 4;
+  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
+  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
+  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
+  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
+  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
+  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
+  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
+  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
+  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
+  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
+  DMA_Init(DMA2_Stream0, &DMA_InitStructure);
+
+  /* DMA2_Stream0 enable */
+  DMA_Cmd(DMA2_Stream0, ENABLE);
+
+  /* configure GPIO */
+  GPIO_InitStructure.GPIO_Pin = ADC1_CH2_PIN | ADC2_CH2_PIN;
+  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
+  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
+  GPIO_Init(ADC1_CH2_PORT, &GPIO_InitStructure);
+
+  GPIO_InitStructure.GPIO_Pin = ADC1_CH3_PIN | ADC1_CH4_PIN | ADC2_CH1_PIN | ADC2_CH3_PIN | ADC2_CH4_PIN;
+  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
+  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
+  GPIO_Init(ADC2_CH1_PORT, &GPIO_InitStructure);
+
+  /* ADC Common Init */
+  ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult;
+  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
+  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;
+  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
+  ADC_CommonInit(&ADC_CommonInitStructure);
+
+  /* ADC1 configuration */
+  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
+  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
+  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
+  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
+  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
+  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
+  ADC_InitStructure.ADC_NbrOfConversion = 4;
+  ADC_Init(ADC1, &ADC_InitStructure);
+
+  /* ADC1 regular channels 10, 11 configuration */
+  ADC_RegularChannelConfig(ADC1, ADC1_CH1, 1, ADC_SampleTime_480Cycles);
+  ADC_RegularChannelConfig(ADC1, ADC1_CH2, 2, ADC_SampleTime_480Cycles);
+  ADC_RegularChannelConfig(ADC1, ADC1_CH3, 3, ADC_SampleTime_480Cycles);
+  ADC_RegularChannelConfig(ADC1, ADC1_CH4, 4, ADC_SampleTime_480Cycles);
+
+  /* ADC1 configuration */
+  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
+  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
+  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
+  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
+  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
+  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
+  ADC_InitStructure.ADC_NbrOfConversion = 4;
+  ADC_Init(ADC2, &ADC_InitStructure);
+
+  /* ADC1 regular channels 10, 11 configuration */
+  ADC_RegularChannelConfig(ADC2, ADC2_CH1, 1, ADC_SampleTime_480Cycles);
+  ADC_RegularChannelConfig(ADC2, ADC2_CH2, 2, ADC_SampleTime_480Cycles);
+  ADC_RegularChannelConfig(ADC2, ADC2_CH3, 3, ADC_SampleTime_480Cycles);
+  ADC_RegularChannelConfig(ADC2, ADC2_CH4, 4, ADC_SampleTime_480Cycles);
+
+  /* Enable DMA request after last transfer (Multi-ADC mode) */
+  ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);
+
+  /* Enable ADC1 */
+  ADC_Cmd(ADC1, ENABLE);
+
+  /* Enable ADC2 */
+  ADC_Cmd(ADC2, ENABLE);
+
+  /* Start ADC1 Software Conversion */
+  ADC_SoftwareStartConv(ADC1);
+}
+
+uint16_t adc_get_temperature(void)
+{
+  uint16_t value;
+
+  value=adc_data[0]+(adc_data[1]<<8);
+  return value;
+}
-- 
GitLab