GadgetSeed  0.9.6
32f469i-disc_led.c
[詳解]
1 /** @file
2  @brief 32F469IDISCOVERY LEDドライバ
3 
4  @date 2018.08.15
5  @author Takashi SHUDO
6 
7  LD1(Green) PG6
8  LD2(Orange) PD4
9  LD3(Red) PD5
10  LD4(Blue) PK3
11 */
12 
13 #include "device.h"
14 
15 #include "stm32f4xx_hal.h"
16 #include "stm32f4xx_hal_gpio.h"
17 
18 /* Legacy GPIO Type Definition */
19 typedef struct {
20  __IO uint32_t MODER; /*!< GPIO port mode register, Address offset: 0x00 */
21  __IO uint32_t OTYPER; /*!< GPIO port output type register, Address offset: 0x04 */
22  __IO uint32_t OSPEEDR; /*!< GPIO port output speed register, Address offset: 0x08 */
23  __IO uint32_t PUPDR; /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */
24  __IO uint32_t IDR; /*!< GPIO port input data register, Address offset: 0x10 */
25  __IO uint32_t ODR; /*!< GPIO port output data register, Address offset: 0x14 */
26  __IO uint16_t BSRR; /*!< GPIO port bit set/reset register, Address offset: 0x18 */
27  __IO uint16_t BRR; /*!< GPIO port bit set/reset register, Address offset: 0x1A */
28  __IO uint32_t LCKR; /*!< GPIO port configuration lock register, Address offset: 0x1C */
29  __IO uint32_t AFR[2]; /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */
30 } GPIO_LegacyTypeDef;
31 
32 /* LED1 */
33 #define GPIO_LED1 (GPIOG)
34 #define RCC_AHBPeriph_GPIO_LED1 (RCC_AHB1ENR_GPIOGEN)
35 #define LED_D1 (GPIO_PIN_6)
36 #define LED1_IO_HI(x) (((GPIO_LegacyTypeDef *)GPIO_LED1)->BSRR = x)
37 #define LED1_IO_LO(x) (((GPIO_LegacyTypeDef *)GPIO_LED1)->BRR = x)
38 #define LED_D1_OFF() LED1_IO_HI(LED_D1) /* Active Low */
39 #define LED_D1_ON() LED1_IO_LO(LED_D1) /* Active Low */
40 /* LED2 */
41 #define GPIO_LED2 (GPIOD)
42 #define RCC_AHBPeriph_GPIO_LED2 (RCC_AHB1ENR_GPIODEN)
43 #define LED_D2 (GPIO_PIN_4)
44 #define LED2_IO_HI(x) (((GPIO_LegacyTypeDef *)GPIO_LED2)->BSRR = x)
45 #define LED2_IO_LO(x) (((GPIO_LegacyTypeDef *)GPIO_LED2)->BRR = x)
46 #define LED_D2_OFF() LED2_IO_HI(LED_D2) /* Active Low */
47 #define LED_D2_ON() LED2_IO_LO(LED_D2) /* Active Low */
48 /* LED3 */
49 #define GPIO_LED3 (GPIOD)
50 #define RCC_AHBPeriph_GPIO_LED3 (RCC_AHB1ENR_GPIODEN)
51 #define LED_D3 (GPIO_PIN_5)
52 #define LED3_IO_HI(x) (((GPIO_LegacyTypeDef *)GPIO_LED3)->BSRR = x)
53 #define LED3_IO_LO(x) (((GPIO_LegacyTypeDef *)GPIO_LED3)->BRR = x)
54 #define LED_D3_OFF() LED3_IO_HI(LED_D3) /* Active Low */
55 #define LED_D3_ON() LED3_IO_LO(LED_D3) /* Active Low */
56 /* LED4 */
57 #define GPIO_LED4 (GPIOK)
58 #define RCC_AHBPeriph_GPIO_LED4 (RCC_AHB1ENR_GPIOKEN)
59 #define LED_D4 (GPIO_PIN_3)
60 #define LED4_IO_HI(x) (((GPIO_LegacyTypeDef *)GPIO_LED4)->BSRR = x)
61 #define LED4_IO_LO(x) (((GPIO_LegacyTypeDef *)GPIO_LED4)->BRR = x)
62 #define LED_D4_OFF() LED4_IO_HI(LED_D4) /* Active Low */
63 #define LED_D4_ON() LED4_IO_LO(LED_D4) /* Active Low */
64 
65 static unsigned char led_stat;
66 
67 static void LED_Configuration(void)
68 {
69  GPIO_InitTypeDef GPIO_InitStructure;
70 
71  /* Enable GPIO_LED clock */
72  RCC->AHB1ENR |= (RCC_AHBPeriph_GPIO_LED1 | RCC_AHBPeriph_GPIO_LED2 | RCC_AHBPeriph_GPIO_LED4);
73 
74  /* Configure GPIO for LEDs as Output push-pull */
75  GPIO_InitStructure.Pin = LED_D1;
76  GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
77  GPIO_InitStructure.Pull = GPIO_NOPULL;
78  GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
79  GPIO_InitStructure.Alternate = 0;
80  HAL_GPIO_Init(GPIO_LED1, &GPIO_InitStructure);
81 
82  GPIO_InitStructure.Pin = LED_D2 | LED_D3;
83  HAL_GPIO_Init(GPIO_LED2, &GPIO_InitStructure);
84 
85  GPIO_InitStructure.Pin = LED_D4;
86  HAL_GPIO_Init(GPIO_LED4, &GPIO_InitStructure);
87 
88  LED_D1_OFF();
89  LED_D2_OFF();
90  LED_D3_OFF();
91  LED_D4_OFF();
92 }
93 
94 /**
95  @brief LEDドライバの登録
96 
97  @param[in] param 未使用
98 
99  @return !=0:エラー
100 */
101 static int led_register(struct st_device *dev, char *param)
102 {
103  LED_Configuration();
104 
105  led_stat = 0;
106 
107  return 0;
108 }
109 
110 /**
111  @brief LEDの状態を取得する
112 
113  @param[out] rd LEDデータポインタ
114 
115  @return !=0:データあり 0:データ無し
116 */
117 static int led_getc(struct st_device *dev, unsigned char *rd)
118 {
119  *rd = led_stat;
120 
121  return 1;
122 }
123 
124 /**
125  @brief LEDの状態を設定する
126 
127  @param[out] ch LEDデータポインタ
128 
129  @return 1:正常終了,!=0:エラー
130 */
131 static int led_putc(struct st_device *dev, unsigned char ch)
132 {
133  if(ch & 0x01) {
134  LED_D1_ON();
135  } else {
136  LED_D1_OFF();
137  }
138 
139  if(ch & 0x02) {
140  LED_D2_ON();
141  } else {
142  LED_D2_OFF();
143  }
144 
145  if(ch & 0x04) {
146  LED_D3_ON();
147  } else {
148  LED_D3_OFF();
149  }
150 
151  if(ch & 0x08) {
152  LED_D4_ON();
153  } else {
154  LED_D4_OFF();
155  }
156 
157  led_stat = (ch & 0x0f);
158 
159  return 1;
160 }
161 
162 const struct st_device led_device = {
163  .name = DEF_DEV_NAME_LED,
164  .explan = "32F469I-Discovery LED",
165  .register_dev = led_register,
166  .getc = led_getc,
167  .putc = led_putc,
168 }; //!< LEDドライバ
static int led_putc(struct st_device *dev, unsigned char ch)
LEDの状態を設定する
static int led_getc(struct st_device *dev, unsigned char *rd)
LEDの状態を取得する
static int led_register(struct st_device *dev, char *param)
LEDドライバの登録
デバイスドライバAPI
デバイスドライバ構造体
Definition: device.h:25
char name[MAX_DEVNAMELRN]
デバイス名文字列
Definition: device.h:26