GadgetSeed  0.9.6
tim3_buzzer.c
[詳解]
1 /** @file
2  @brief 圧電ブザードライバ
3 
4  @date 2015.08.23
5  @author Takashi SHUDO
6 
7  @info
8 
9  接続
10 
11  圧電ブザー STM32F4(CPU PIN)
12  ----------- ----------------
13  DIN PB1(27pin)
14 
15  ブザー音のパルスはTIM3で作成
16 
17  使用方法
18 
19  ioctl(0:Off/1:On, 周波数(Hz))
20 */
21 
22 #include "sysconfig.h"
23 #include "device.h"
24 #include "device/buzzer_ioctl.h"
25 #include "timer.h"
26 #include "tkprintf.h"
27 
28 #include "stm32f4xx_hal.h"
29 
30 TIM_HandleTypeDef htim3;
31 
32 /* TIM3 init function */
33 void MX_TIM3_Init(void)
34 {
35  TIM_ClockConfigTypeDef sClockSourceConfig;
36  TIM_MasterConfigTypeDef sMasterConfig;
37  TIM_OC_InitTypeDef sConfigOC;
38 
39  htim3.Instance = TIM3;
40  htim3.Init.Prescaler = 0;
41  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
42  htim3.Init.Period = 0;
43  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
44  HAL_TIM_Base_Init(&htim3);
45 
46  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
47  HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);
48 
49  HAL_TIM_OC_Init(&htim3);
50 
51  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
52  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
53  HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);
54 
55  sConfigOC.OCMode = TIM_OCMODE_TIMING;
56  sConfigOC.Pulse = 0;
57  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
58  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
59  HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4);
60 }
61 
62 void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
63 {
64  GPIO_InitTypeDef GPIO_InitStruct;
65  if(htim_base->Instance==TIM3) {
66  __TIM3_CLK_ENABLE();
67 
68  GPIO_InitStruct.Pin = GPIO_PIN_1;
69  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
70  GPIO_InitStruct.Pull = GPIO_NOPULL;
71  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
72  GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
73  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
74  }
75 }
76 
77 
78 void start_tim3(void)
79 {
80  MX_TIM3_Init();
81 }
82 
83 void on_buzzer(long cycle)
84 {
85  TIM_OC_InitTypeDef sConfigOC;
86  htim3.Init.Period = cycle;
87  HAL_TIM_Base_Init(&htim3);
88 
89  sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
90  sConfigOC.Pulse = cycle/2;
91  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
92  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
93  HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4);
94 
95  HAL_TIM_OC_Start(&htim3, TIM_CHANNEL_4);
96 }
97 
98 void off_buzzer(void)
99 {
100  HAL_TIM_OC_Stop(&htim3, TIM_CHANNEL_4);
101 }
102 
103 static int buzzer_register(struct st_device *dev, char *param)
104 {
105  start_tim3();
106 
107  return 0;
108 }
109 
110 static int buzzer_unregister(struct st_device *dev)
111 {
112  off_buzzer();
113 
114  return 0;
115 }
116 
117 static int buzzer_open(struct st_device *dev)
118 {
119  return 0;
120 }
121 
122 static int buzzer_close(struct st_device *dev)
123 {
124  off_buzzer();
125 
126  return 0;
127 }
128 
129 static int buzzer_ioctl(struct st_device *dev, unsigned int com, unsigned int arg, void *param)
130 {
131  switch(com) {
132  case IOCMD_BUZZER_OFF: // Stop
133  off_buzzer();
134  break;
135 
136  case IOCMD_BUZZER_ON: // Play
137  {
138  long cycle = (GSC_CPU_CLOCK_HZ/4) / arg;
139  on_buzzer(cycle);
140  }
141  break;
142 
143  default:
144  off_buzzer();
145  SYSERR_PRINT("Unknown ioctl(%08lX)\n", com);
146  return -1;
147  }
148 
149  return 0;
150 }
151 
152 static int buzzer_suspend(struct st_device *dev)
153 {
154  off_buzzer();
155 
156  return 0;
157 }
158 
159 static int buzzer_resume(struct st_device *dev)
160 {
161  start_tim3();
162 
163  return 0;
164 }
165 
166 const struct st_device buzzer_device = {
168  .explan = "STM32F4 TIM3 buzzer",
169  .register_dev = buzzer_register,
170  .unregister_dev = buzzer_unregister,
171  .open = buzzer_open,
172  .close = buzzer_close,
173  .ioctl = buzzer_ioctl,
174  .suspend = buzzer_suspend,
175  .resume = buzzer_resume,
176 };
Buzzerドライバ ioctl 用マクロ定義
カーネルタイマ
#define IOCMD_BUZZER_OFF
ブザー音声出力停止
Definition: buzzer_ioctl.h:18
カーネル用機能限定printf
#define DEF_DEV_NAME_BUZZER
標準ブザーデバイス名
Definition: buzzer_ioctl.h:15
#define IOCMD_BUZZER_ON
ブザー音声出力開始
Definition: buzzer_ioctl.h:17
デバイスドライバAPI
デバイスドライバ構造体
Definition: device.h:25
char name[MAX_DEVNAMELRN]
デバイス名文字列
Definition: device.h:26