GadgetSeed  0.9.6
irq.c
[詳解]
1 /** @file
2  @brief STM32F411 外部割り込み(EXTI)(IRQ) ドライバ
3 
4  @date 2015.10.13
5  @author Takashi SHUDO
6 
7  @note
8 
9  EXTI0
10  PA0 - PI0
11 
12  PH0 IRQ0
13 */
14 
15 #include "interrupt.h"
16 #include "device.h"
17 #include "device/irq_ioctl.h"
18 #include "tkprintf.h"
19 
20 #include "stm32f4xx_hal.h"
21 
22 //#define DEBUGKBITS 0x03
23 #include "dkprintf.h"
24 
25 
26 static unsigned char flg_have_int = 0;
27 static void (* inth_func)(unsigned int intnum, void *sp);
28 
29 static void MX_GPIO_Init(void)
30 {
31  GPIO_InitTypeDef GPIO_InitStruct;
32 
33  /* GPIO Ports Clock Enable */
34  __GPIOC_CLK_ENABLE();
35 
36  /*Configure GPIO pins : PH0 */
37  GPIO_InitStruct.Pin = GPIO_PIN_13;
38 // GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; // 下りエッジ割り込み
39  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING; // 両エッジ割り込み
40 // GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
41  GPIO_InitStruct.Pull = GPIO_PULLUP;
42  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
43  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
44 
45 // HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
46 // HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
47 }
48 
49 static void init_gpio(void)
50 {
51  MX_GPIO_Init();
52 }
53 
54 static void inthdr_exti15_10(unsigned int intnum, void *sp)
55 {
56  __HAL_GPIO_EXTI_CLEAR_IT(EXTI_IMR_MR13);
57  DKPRINTF(0x01, "INT %d(%d)\n", intnum, HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13));
58 
59  if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == 0) {
60  flg_have_int = 1;
61  if(inth_func) {
62  inth_func(intnum, sp);
63  }
64  } else {
65  flg_have_int = 0;
66  }
67 }
68 
69 static int irq_register(struct st_device *dev, char *param)
70 {
71  inth_func = 0;
72 
73  init_gpio();
74 
75  register_interrupt(IRQ2VECT(EXTI15_10_IRQn), inthdr_exti15_10);
76 
77  return 0;
78 }
79 
80 static int irq_ioctl(struct st_device *dev, unsigned int com, unsigned int arg, void *param)
81 {
82  switch(com) {
83  case 0: // DEBUG
84  return HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13);
85 
86  case IOCMD_IRQ_REGISTER:
87  inth_func = (void (*)(unsigned int, void *))param;
88  break;
89 
91  inth_func = 0;
92  break;
93 
94  case IOCMD_IRQ_ENABLE:
95  HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
96  break;
97 
98  case IOCMD_IRQ_DISABLE:
99  HAL_NVIC_DisableIRQ(EXTI15_10_IRQn);
100  break;
101 
102  case IOCMD_IRQ_SET_EDGE:
103  // [TODO]
104  SYSERR_PRINT("IOCMD_IRQ_SET_EDGE not support\n");
105  break;
106 
107  case IOCMD_IRQ_GET_LEVEL:
108  return HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13);
109 
110  case IOCMD_IRQ_GET_INT:
111  return flg_have_int;
112 
113  default:
114  SYSERR_PRINT("Unknow command %08lX arg %08lX\n", com, arg);
115  return -1;
116  }
117 
118  return 0;
119 }
120 
121 const struct st_device irq_device = {
123  .explan = "STM32F4 EXIT0-15",
124  .register_dev = irq_register,
125  .ioctl = irq_ioctl,
126 };
外部割り込み(IRQ)ドライバ ioctl 用マクロ定義
#define DEF_DEV_NAME_IRQ
標準外部割り込み(IRQ)ドライバ名
Definition: irq_ioctl.h:15
#define IOCMD_IRQ_GET_LEVEL
割込端子のレベルを取得する
Definition: irq_ioctl.h:22
#define IOCMD_IRQ_SET_EDGE
割込エッジを設定する[TODO]
Definition: irq_ioctl.h:21
カーネル用機能限定printf
#define IOCMD_IRQ_UNREGISTER
割込処理関数を登録解除する
Definition: irq_ioctl.h:18
割り込みハンドラ
#define IOCMD_IRQ_REGISTER
割込処理関数を登録する
Definition: irq_ioctl.h:17
デバイスドライバAPI
デバイスドライバ構造体
Definition: device.h:25
#define IOCMD_IRQ_GET_INT
割込状態を取得する
Definition: irq_ioctl.h:23
カーネル、ドライバ(非タスク)デバッグ用マクロ
#define IOCMD_IRQ_ENABLE
割込を有効にする
Definition: irq_ioctl.h:19
char name[MAX_DEVNAMELRN]
デバイス名文字列
Definition: device.h:26
#define IOCMD_IRQ_DISABLE
割込を無効にする
Definition: irq_ioctl.h:20