1 | // defines for readability
|
2 | #define SPAREINPUT_CHANNEL ADC_Channel_0
|
3 | #define SPEEDINPUT_CHANNEL ADC_Channel_1
|
4 | #define PHASE_CURRENTS_CHANNEL ADC_Channel_2
|
5 | #define TEMP_CHANNEL ADC_Channel_3
|
6 |
|
7 | typedef struct ADCValues
|
8 | {
|
9 | uint16_t spare1;
|
10 | uint16_t speedInput;
|
11 | uint16_t current;
|
12 | uint16_t temp;
|
13 | uint16_t spare2;
|
14 | } ADCValues_t;
|
15 |
|
16 | volatile ADCValues_t ADCRead;
|
17 | /* Init the ADC converter to free-run with 4 channels
|
18 | *
|
19 | */
|
20 | void ADCInit(void) {
|
21 | ADC_InitTypeDef ADC_InitStructure;
|
22 | DMA_InitTypeDef DMA_InitStructure;
|
23 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
|
24 | RCC_ADCCLKConfig(RCC_PCLK2_Div8);
|
25 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
|
26 | /* DMA1 channel1 configuration ----------------------------------------------*/
|
27 | DMA_DeInit(DMA1_Channel1); // Clear everything we had
|
28 | DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(ADC1->DR); // source address is the ADC result register
|
29 | DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADCRead; // Its the target struct
|
30 | DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; // from Peripheral to RAM
|
31 | DMA_InitStructure.DMA_BufferSize = 4; /// Note that we have 4 Channels
|
32 | DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // Source stays same address
|
33 | DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //destination will be incremented
|
34 | DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; // 16 bit results
|
35 | DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // yeah same size -> 16 bit
|
36 | DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; // Wrap around the destination address
|
37 | DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; // why not? its the only DMA thingy in this program
|
38 | DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; // we don't want RAM to RAM
|
39 | DMA_Init(DMA1_Channel1, &DMA_InitStructure); // Init the stuff finally
|
40 | /* Enable DMA1 channel1 */
|
41 | DMA_Cmd(DMA1_Channel1, ENABLE); // Run, but we wait for the ADC init
|
42 | /* ADC1 configuration
|
43 | * 4 channel continuous
|
44 | */
|
45 | ADC_DeInit(ADC1);
|
46 |
|
47 | ADC_StructInit(&ADC_InitStructure);
|
48 | ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
|
49 | ADC_InitStructure.ADC_ScanConvMode = ENABLE;
|
50 | ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
|
51 | ADC_InitStructure.ADC_NbrOfChannel = 4;
|
52 | ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
|
53 | ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
|
54 | /* Now do the setup */
|
55 | ADC_DMACmd(ADC1, ENABLE);
|
56 | ADC_Init(ADC1, &ADC_InitStructure);
|
57 | /* Enable ADC1 */
|
58 | ADC_RegularChannelConfig(ADC1,SPAREINPUT_CHANNEL,1,ADC_SampleTime_239Cycles5);
|
59 | ADC_RegularChannelConfig(ADC1,SPEEDINPUT_CHANNEL,2,ADC_SampleTime_239Cycles5);
|
60 | ADC_RegularChannelConfig(ADC1,PHASE_CURRENTS_CHANNEL,3,ADC_SampleTime_239Cycles5);
|
61 | ADC_RegularChannelConfig(ADC1,TEMP_CHANNEL,4,ADC_SampleTime_239Cycles5);
|
62 | ADC_Cmd(ADC1, ENABLE);
|
63 | /* Enable ADC1 reset calibaration register */
|
64 | ADC_ResetCalibration(ADC1);
|
65 | /* Check the end of ADC1 reset calibration register */
|
66 | while(ADC_GetResetCalibrationStatus(ADC1));
|
67 | /* Start ADC1 calibaration */
|
68 | ADC_StartCalibration(ADC1);
|
69 | /* Check the end of ADC1 calibration */
|
70 | while(ADC_GetCalibrationStatus(ADC1));
|
71 |
|
72 | ADC_SoftwareStartConvCmd(ADC1,ENABLE);
|
73 | Delay(200);
|
74 | }
|