1 | // aus setup.h
|
2 | /* ADC definitions */
|
3 | #define SPAREINPUT_ADC ADC1
|
4 | #define SPAREINPUT_CHANNEL ADC_Channel_0
|
5 | #define SPAREINPUT_GPIO_PORT GPIOA
|
6 | #define SPAREINPUT_GPIO_PIN GPIO_Pin_0
|
7 |
|
8 | /* Speed Input */
|
9 | #define SPEEDINPUT_ADC ADC1
|
10 | #define SPEEDINPUT_CHANNEL ADC_Channel_1
|
11 | #define SPEEDINPUT_GPIO_PORT GPIOA
|
12 | #define SPEEDINPUT_GPIO_PIN GPIO_Pin_1
|
13 |
|
14 | /* Only for 1 shunt resistor case */
|
15 | #define PHASE_CURRENTS_ADC ADC1
|
16 | #define PHASE_CURRENTS_CHANNEL ADC_Channel_2
|
17 | #define PHASE_CURRENTS_GPIO_PORT GPIOA
|
18 | #define PHASE_CURRENTS_GPIO_PIN GPIO_Pin_2
|
19 |
|
20 | /** Bus and temperature readings **/
|
21 | #define TEMP_ADC ADC1
|
22 | #define TEMP_CHANNEL ADC_Channel_3
|
23 | #define TEMP_GPIO_PORT GPIOA
|
24 | #define TEMP_GPIO_PIN GPIO_Pin_3
|
25 |
|
26 | typedef struct ADCValues
|
27 | {
|
28 | uint16_t spare1;
|
29 | uint16_t speedInput;
|
30 | uint16_t current;
|
31 | uint16_t temp;
|
32 | uint16_t spare2;
|
33 | } ADCValues_t;
|
34 | // aus main.c
|
35 | volatile ADCValues_t ADCRead;
|
36 | /* Init the ADC converter to free-run with 4 channels
|
37 | *
|
38 | */
|
39 | void ADCInit(void) {
|
40 | ADC_InitTypeDef ADC_InitStructure;
|
41 | DMA_InitTypeDef DMA_InitStructure;
|
42 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
|
43 | RCC_ADCCLKConfig(RCC_PCLK2_Div8);
|
44 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
|
45 | /* DMA1 channel1 configuration ----------------------------------------------*/
|
46 | DMA_DeInit(DMA1_Channel1); // Clear everything we had
|
47 | DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(ADC1->DR); // Source is the ADC data register
|
48 | DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADCRead; // Its the destination here
|
49 | DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; // from Peripheral to RAM
|
50 | DMA_InitStructure.DMA_BufferSize = 4; /// Note that we have 4 Channels
|
51 | DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // Source stays same address
|
52 | DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // Destination will be incremented
|
53 | DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; // 16 bit results
|
54 | DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // yeah same size -> 16 bit
|
55 | DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; // Wrap around the destination address
|
56 | DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; // why not? its the only DMA thingy in this program
|
57 | DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; // we don't want RAM to RAM
|
58 | DMA_Init(DMA1_Channel1, &DMA_InitStructure); // Init the stuff finally
|
59 | /* Enable DMA1 channel1 */
|
60 | DMA_Cmd(DMA1_Channel1, ENABLE); // Run, but we wait for the ADC init
|
61 | /* ADC1 configuration
|
62 | * 4 channel continuous
|
63 | */
|
64 | ADC_DeInit(ADC1); // again clear the whole ADC out
|
65 | ADC_StructInit(&ADC_InitStructure);
|
66 | ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; // no external triggers here
|
67 | ADC_InitStructure.ADC_ScanConvMode = ENABLE;
|
68 | ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
|
69 | ADC_InitStructure.ADC_NbrOfChannel = 4;
|
70 | ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
|
71 | ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
|
72 | /* Now do the setup */
|
73 | ADC_DMACmd(ADC1, ENABLE);
|
74 | ADC_Init(ADC1, &ADC_InitStructure);
|
75 | /* Enable ADC1 */
|
76 | ADC_RegularChannelConfig(ADC1,SPAREINPUT_CHANNEL,1,ADC_SampleTime_239Cycles5);
|
77 | ADC_RegularChannelConfig(ADC1,SPEEDINPUT_CHANNEL,2,ADC_SampleTime_239Cycles5);
|
78 | ADC_RegularChannelConfig(ADC1,PHASE_CURRENTS_CHANNEL,3,ADC_SampleTime_239Cycles5);
|
79 | ADC_RegularChannelConfig(ADC1,TEMP_CHANNEL,4,ADC_SampleTime_239Cycles5);
|
80 | ADC_Cmd(ADC1, ENABLE);
|
81 | /* Enable ADC1 reset calibaration register */
|
82 | ADC_ResetCalibration(ADC1);
|
83 | /* Check the end of ADC1 reset calibration register */
|
84 | while(ADC_GetResetCalibrationStatus(ADC1));
|
85 | /* Start ADC1 calibaration */
|
86 | ADC_StartCalibration(ADC1);
|
87 | /* Check the end of ADC1 calibration */
|
88 | while(ADC_GetCalibrationStatus(ADC1));
|
89 |
|
90 | ADC_SoftwareStartConvCmd(ADC1,ENABLE);
|
91 | Delay(200);
|
92 | }
|