@@ -76,10 +76,11 @@ LOG_MODULE_REGISTER(main);
7676
7777#define LED_PORT DT_ALIAS_LED0_GPIOS_CONTROLLER
7878#define LED DT_ALIAS_LED0_GPIOS_PIN
79+ #define LED_FLAGS DT_ALIAS_LED0_GPIOS_FLAGS
7980
8081static const u8_t hid_report_desc [] = HID_MOUSE_REPORT_DESC (2 );
8182
82- static u32_t def_val [4 ];
83+ static u8_t def_val [4 ];
8384static volatile u8_t status [4 ];
8485static K_SEM_DEFINE (sem , 0 , 1 ) ; /* starts off "not available" */
8586static struct gpio_callback callback [4 ];
@@ -103,7 +104,7 @@ static void status_cb(enum usb_dc_status_code status, const u8_t *param)
103104static void left_button (struct device * gpio , struct gpio_callback * cb ,
104105 u32_t pins )
105106{
106- u32_t cur_val ;
107+ int ret ;
107108 u8_t state = status [MOUSE_BTN_REPORT_POS ];
108109
109110 if (IS_ENABLED (CONFIG_USB_DEVICE_REMOTE_WAKEUP )) {
@@ -113,8 +114,14 @@ static void left_button(struct device *gpio, struct gpio_callback *cb,
113114 }
114115 }
115116
116- gpio_pin_read (gpio , PIN0 , & cur_val );
117- if (def_val [0 ] != cur_val ) {
117+ ret = gpio_pin_get (gpio , PIN0 );
118+ if (ret < 0 ) {
119+ LOG_ERR ("Failed to get the state of pin %u, error: %d" ,
120+ PIN0 , ret );
121+ return ;
122+ }
123+
124+ if (def_val [0 ] != (u8_t )ret ) {
118125 state |= MOUSE_BTN_LEFT ;
119126 } else {
120127 state &= ~MOUSE_BTN_LEFT ;
@@ -130,7 +137,7 @@ static void left_button(struct device *gpio, struct gpio_callback *cb,
130137static void right_button (struct device * gpio , struct gpio_callback * cb ,
131138 u32_t pins )
132139{
133- u32_t cur_val ;
140+ int ret ;
134141 u8_t state = status [MOUSE_BTN_REPORT_POS ];
135142
136143 if (IS_ENABLED (CONFIG_USB_DEVICE_REMOTE_WAKEUP )) {
@@ -140,8 +147,14 @@ static void right_button(struct device *gpio, struct gpio_callback *cb,
140147 }
141148 }
142149
143- gpio_pin_read (gpio , PIN1 , & cur_val );
144- if (def_val [0 ] != cur_val ) {
150+ ret = gpio_pin_get (gpio , PIN1 );
151+ if (ret < 0 ) {
152+ LOG_ERR ("Failed to get the state of pin %u, error: %d" ,
153+ PIN1 , ret );
154+ return ;
155+ }
156+
157+ if (def_val [1 ] != (u8_t )ret ) {
145158 state |= MOUSE_BTN_RIGHT ;
146159 } else {
147160 state &= ~MOUSE_BTN_RIGHT ;
@@ -157,12 +170,17 @@ static void right_button(struct device *gpio, struct gpio_callback *cb,
157170#ifdef DT_ALIAS_SW2_GPIOS_PIN
158171static void x_move (struct device * gpio , struct gpio_callback * cb , u32_t pins )
159172{
160- u32_t cur_val ;
173+ int ret ;
161174 u8_t state = status [MOUSE_X_REPORT_POS ];
162175
163- gpio_pin_read (gpio , PIN2 , & cur_val );
176+ ret = gpio_pin_get (gpio , PIN2 );
177+ if (ret < 0 ) {
178+ LOG_ERR ("Failed to get the state of pin %u, error: %d" ,
179+ PIN2 , ret );
180+ return ;
181+ }
164182
165- if (def_val [2 ] != cur_val ) {
183+ if (def_val [2 ] != ( u8_t ) ret ) {
166184 state += 10U ;
167185 }
168186
@@ -176,12 +194,17 @@ static void x_move(struct device *gpio, struct gpio_callback *cb, u32_t pins)
176194#ifdef DT_ALIAS_SW3_GPIOS_PIN
177195static void y_move (struct device * gpio , struct gpio_callback * cb , u32_t pins )
178196{
179- u32_t cur_val ;
197+ int ret ;
180198 u8_t state = status [MOUSE_Y_REPORT_POS ];
181199
182- gpio_pin_read (gpio , PIN3 , & cur_val );
200+ ret = gpio_pin_get (gpio , PIN3 );
201+ if (ret < 0 ) {
202+ LOG_ERR ("Failed to get the state of pin %u, error: %d" ,
203+ PIN3 , ret );
204+ return ;
205+ }
183206
184- if (def_val [3 ] != cur_val ) {
207+ if (def_val [3 ] != ( u8_t ) ret ) {
185208 state += 10U ;
186209 }
187210
@@ -193,29 +216,55 @@ static void y_move(struct device *gpio, struct gpio_callback *cb, u32_t pins)
193216#endif
194217
195218int callbacks_configure (struct device * gpio , u32_t pin , int flags ,
196- void ( * handler )( struct device * , struct gpio_callback * ,
197- u32_t ), struct gpio_callback * callback , u32_t * val )
219+ gpio_callback_handler_t handler ,
220+ struct gpio_callback * callback , u8_t * val )
198221{
222+ int ret ;
223+
199224 if (!gpio ) {
200225 LOG_ERR ("Could not find PORT" );
201226 return - ENXIO ;
202227 }
203- gpio_pin_configure (gpio , pin ,
204- GPIO_DIR_IN | GPIO_INT |
205- GPIO_INT_EDGE | GPIO_INT_DOUBLE_EDGE |
206- flags );
207- gpio_pin_read (gpio , pin , val );
228+
229+ ret = gpio_pin_configure (gpio , pin , GPIO_INPUT | flags );
230+ if (ret < 0 ) {
231+ LOG_ERR ("Failed to configure pin %u, error: %d" ,
232+ pin , ret );
233+ return ret ;
234+ }
235+
236+ ret = gpio_pin_get (gpio , pin );
237+ if (ret < 0 ) {
238+ LOG_ERR ("Failed to get the state of pin %u, error: %d" ,
239+ pin , ret );
240+ return ret ;
241+ }
242+
243+ * val = (u8_t )ret ;
244+
208245 gpio_init_callback (callback , handler , BIT (pin ));
209- gpio_add_callback (gpio , callback );
210- gpio_pin_enable_callback (gpio , pin );
246+ ret = gpio_add_callback (gpio , callback );
247+ if (ret < 0 ) {
248+ LOG_ERR ("Failed to add the callback for pin %u, error: %d" ,
249+ pin , ret );
250+ return ret ;
251+ }
252+
253+ ret = gpio_pin_interrupt_configure (gpio , pin , GPIO_INT_EDGE_BOTH );
254+ if (ret < 0 ) {
255+ LOG_ERR ("Failed to configure interrupt for pin %u, error: %d" ,
256+ pin , ret );
257+ return ret ;
258+ }
259+
211260 return 0 ;
212261}
213262
214263void main (void )
215264{
216265 u8_t report [4 ] = { 0x00 };
217- u8_t toggle = 0U ;
218266 struct device * led_dev , * hid_dev ;
267+ int ret ;
219268
220269 led_dev = device_get_binding (LED_PORT );
221270 if (led_dev == NULL ) {
@@ -229,7 +278,11 @@ void main(void)
229278 return ;
230279 }
231280
232- gpio_pin_configure (led_dev , LED , GPIO_DIR_OUT );
281+ ret = gpio_pin_configure (led_dev , LED , GPIO_OUTPUT | LED_FLAGS );
282+ if (ret < 0 ) {
283+ LOG_ERR ("Failed to configure the LED pin, error: %d" , ret );
284+ return ;
285+ }
233286
234287 if (callbacks_configure (device_get_binding (PORT0 ), PIN0 , PIN0_FLAGS ,
235288 & left_button , & callback [0 ], & def_val [0 ])) {
@@ -280,7 +333,9 @@ void main(void)
280333 hid_int_ep_write (hid_dev , report , sizeof (report ), NULL );
281334
282335 /* Toggle LED on sent report */
283- gpio_pin_write (led_dev , LED , toggle );
284- toggle = !toggle ;
336+ ret = gpio_pin_toggle (led_dev , LED );
337+ if (ret < 0 ) {
338+ LOG_ERR ("Failed to toggle the LED pin, error: %d" , ret );
339+ }
285340 }
286341}
0 commit comments