GadgetSeed  0.9.6
adc.c
[詳解]
1 /** @file
2  @brief STM32F7 ADC(GPIO PA6)
3 
4  @date 2017.02.04
5  @author Takashi SHUDO
6 */
7 
8 #include "device.h"
9 #include "tkprintf.h"
10 #include "system.h"
11 
12 #include "stm32f7xx_hal.h"
13 #include "stm32f7xx_hal_adc.h"
14 
15 ADC_HandleTypeDef hadc1;
16 
17 /* ADC1 init function */
18 static void MX_ADC1_Init(void)
19 {
20  ADC_ChannelConfTypeDef sConfig;
21 
22  hadc1.Instance = ADC1;
23  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
24  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
25  hadc1.Init.ScanConvMode = DISABLE;
26  hadc1.Init.ContinuousConvMode = DISABLE;
27  hadc1.Init.DiscontinuousConvMode = DISABLE;
28  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
29  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
30  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
31  hadc1.Init.NbrOfConversion = 1;
32  hadc1.Init.DMAContinuousRequests = DISABLE;
33  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
34  if(HAL_ADC_Init(&hadc1) != HAL_OK) {
35  SYSERR_PRINT("ADC1 Initialize Error.\n");
36  }
37 
38  sConfig.Channel = ADC_CHANNEL_6;
39  sConfig.Rank = 1;
40  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
41  if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
42  SYSERR_PRINT("ADC1 Config Error.\n");
43  }
44 }
45 
46 void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
47 {
48  GPIO_InitTypeDef GPIO_InitStruct;
49 
50  if(hadc->Instance==ADC1) {
51  __ADC1_CLK_ENABLE();
52 
53  GPIO_InitStruct.Pin = GPIO_PIN_6;
54  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
55  GPIO_InitStruct.Pull = GPIO_NOPULL;
56  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
57  }
58 }
59 
60 static int adc_register(struct st_device *dev, char *param)
61 {
62  MX_ADC1_Init();
63 
64  return 0;
65 }
66 
67 static int adc_open(struct st_device *dev)
68 {
69  return 0;
70 }
71 
72 static int adc_close(struct st_device *dev)
73 {
74  return 0;
75 }
76 
77 static long adc_read(struct st_device *dev, unsigned char *data, long size)
78 {
79  unsigned long val;
80  long rtn = 0;
81 
82  HAL_ADC_Start(&hadc1);
83  HAL_ADC_PollForConversion(&hadc1, 100);
84  val = HAL_ADC_GetValue(&hadc1);
85  HAL_ADC_Stop(&hadc1);
86 
87  if(size > 0) {
88  *data = ((val >> 8) & 0xff);
89  data ++;
90  rtn ++;
91  }
92 
93  if(size > 1) {
94  *data = (val & 0xff);
95  rtn ++;
96  }
97 
98  return rtn;
99 }
100 
101 const device adc_device = {
102  .name = "adc",
103  .explan = "STM32F7 ADC(GPIO PC0)",
104  .register_dev = adc_register,
105  .open = adc_open,
106  .close = adc_close,
107  .read = adc_read,
108 };
カーネル用機能限定printf
システム固有初期化関連
デバイスドライバAPI
デバイスドライバ構造体
Definition: device.h:25