-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.c
447 lines (387 loc) · 12.4 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
* Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
Application : USB Keyboard USB application
Author : Abdelali Boussetta
*/
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include "nrf.h"
#include "nrf_drv_usbd.h"
#include "nrf_drv_clock.h"
#include "nrf_gpio.h"
#include "nrf_drv_power.h"
#include "app_timer.h"
#include "app_usbd.h"
#include "app_usbd_core.h"
#include "app_usbd_hid_kbd.h"
#include "app_error.h"
#include "bsp.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#define BUTTON_DETECTION_TIME 30
#define UART_PRINTING_ENABLED // comment to disable the UART loggin
/**
* All the keys are available in the app_usbd_hid_kbd.h file
* Located under components\libraries\usbd\class\hid\kbd\
*/
#define BUTTON1_CONFIG APP_USBD_HID_KBD_A
#define BUTTON2_CONFIG APP_USBD_HID_KBD_B
#define BUTTON3_CONFIG APP_USBD_HID_KBD_C
#define BUTTON4_CONFIG APP_USBD_HID_KBD_D
/**
* @brief Enable USB power detection
*/
#ifndef USBD_POWER_DETECTION
#define USBD_POWER_DETECTION true
#endif
#define LED_CAPSLOCK (BSP_BOARD_LED_0) /**< CAPSLOCK */
#define LED_NUMLOCK (BSP_BOARD_LED_1) /**< NUMLOCK */
#define LED_HID_REP (BSP_BOARD_LED_2) /**< Changes its state if any HID report was received or transmitted */
#define LED_USB_START (BSP_BOARD_LED_3) /**< The USBD library has been started and the bus is not in SUSPEND state */
/**
* @brief USB composite interfaces
*/
#define APP_USBD_INTERFACE_KBD 1
/**
* @brief User event handler, HID keyboard
*/
static void hid_kbd_user_ev_handler(app_usbd_class_inst_t const * p_inst,
app_usbd_hid_user_event_t event);
/**
* @brief Global HID keyboard instance
*/
APP_USBD_HID_KBD_GLOBAL_DEF(m_app_hid_kbd,
APP_USBD_INTERFACE_KBD,
NRF_DRV_USBD_EPIN2,
hid_kbd_user_ev_handler,
APP_USBD_HID_SUBCLASS_BOOT
);
/**
* @brief keyboard status
*/
static void kbd_status(void)
{
if(app_usbd_hid_kbd_led_state_get(&m_app_hid_kbd, APP_USBD_HID_KBD_LED_NUM_LOCK))
{
bsp_board_led_on(LED_NUMLOCK);
}
else
{
bsp_board_led_off(LED_NUMLOCK);
}
if(app_usbd_hid_kbd_led_state_get(&m_app_hid_kbd, APP_USBD_HID_KBD_LED_CAPS_LOCK))
{
bsp_board_led_on(LED_CAPSLOCK);
}
else
{
bsp_board_led_off(LED_CAPSLOCK);
}
}
/**
* @brief Class specific event handler.
*
* @param p_inst Class instance.
* @param event Class specific event.
* */
static void hid_kbd_user_ev_handler(app_usbd_class_inst_t const * p_inst,
app_usbd_hid_user_event_t event)
{
UNUSED_PARAMETER(p_inst);
switch (event) {
case APP_USBD_HID_USER_EVT_OUT_REPORT_READY:
/* Only one output report IS defined for HID keyboard class. Update LEDs state. */
bsp_board_led_invert(LED_HID_REP);
kbd_status();
break;
case APP_USBD_HID_USER_EVT_IN_REPORT_DONE:
bsp_board_led_invert(LED_HID_REP);
break;
case APP_USBD_HID_USER_EVT_SET_BOOT_PROTO:
UNUSED_RETURN_VALUE(hid_kbd_clear_buffer(p_inst));
break;
case APP_USBD_HID_USER_EVT_SET_REPORT_PROTO:
UNUSED_RETURN_VALUE(hid_kbd_clear_buffer(p_inst));
break;
default:
break;
}
}
/**
* @brief USBD library specific event handler.
*
* @param event USBD library event.
* */
static void usbd_user_ev_handler(app_usbd_event_type_t event)
{
switch (event)
{
case APP_USBD_EVT_DRV_SOF:
break;
case APP_USBD_EVT_DRV_SUSPEND:
app_usbd_suspend_req(); // Allow the library to put the peripheral into sleep mode
bsp_board_leds_off();
break;
case APP_USBD_EVT_DRV_RESUME:
bsp_board_led_on(LED_USB_START);
kbd_status(); /* Restore LED state - during SUSPEND all LEDS are turned off */
break;
case APP_USBD_EVT_STARTED:
bsp_board_led_on(LED_USB_START);
break;
case APP_USBD_EVT_STOPPED:
app_usbd_disable();
bsp_board_leds_off();
break;
case APP_USBD_EVT_POWER_DETECTED:
#ifdef UART_PRINTING_ENABLED
NRF_LOG_INFO("USB power detected");
#endif
if (!nrf_drv_usbd_is_enabled())
{
app_usbd_enable();
}
break;
case APP_USBD_EVT_POWER_REMOVED:
#ifdef UART_PRINTING_ENABLED
NRF_LOG_INFO("USB power removed");
#endif
app_usbd_stop();
break;
case APP_USBD_EVT_POWER_READY:
#ifdef UART_PRINTING_ENABLED
NRF_LOG_INFO("USB ready");
#endif
app_usbd_start();
break;
default:
break;
}
}
static void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
ret_code_t err_code;
switch(pin_no)
{
case BUTTON_1:
if(button_action == APP_BUTTON_PUSH) {
UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, BUTTON1_CONFIG, true));
#ifdef UART_PRINTING_ENABLED
NRF_LOG_INFO("button1 pressed.");
#endif
}
else if(button_action == APP_BUTTON_RELEASE) {
UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, BUTTON1_CONFIG, false));
#ifdef UART_PRINTING_ENABLED
NRF_LOG_INFO("button1 released.");
#endif
}
break;
case BUTTON_2:
if(button_action == APP_BUTTON_PUSH) {
UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, BUTTON2_CONFIG, true));
#ifdef UART_PRINTING_ENABLED
NRF_LOG_INFO("button2 pressed.");
#endif
}
else if(button_action == APP_BUTTON_RELEASE) {
UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, BUTTON2_CONFIG, false));
#ifdef UART_PRINTING_ENABLED
NRF_LOG_INFO("button2 released.");
#endif
}
break;
case BUTTON_3:
if(button_action == APP_BUTTON_PUSH) {
UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, BUTTON3_CONFIG, true));
#ifdef UART_PRINTING_ENABLED
NRF_LOG_INFO("button3 pressed.");
#endif
}
else if(button_action == APP_BUTTON_RELEASE) {
UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, BUTTON3_CONFIG, false));
#ifdef UART_PRINTING_ENABLED
NRF_LOG_INFO("button3 released.");
#endif
}
break;
case BUTTON_4:
if(button_action == APP_BUTTON_PUSH) {
UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, BUTTON4_CONFIG, true));
#ifdef UART_PRINTING_ENABLED
NRF_LOG_INFO("button4 pressed.");
#endif
}
else if(button_action == APP_BUTTON_RELEASE) {
UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, BUTTON4_CONFIG, false));
#ifdef UART_PRINTING_ENABLED
NRF_LOG_INFO("button4 released.");
#endif
}
break;
default:
return; // no implementation needed
}
}
/**@brief Function for buttons configuration.
*
*/
static const app_button_cfg_t app_buttons[BUTTONS_NUMBER] =
{
// BUTTONS_NUMBER,
// BUTTON_PIN,
// BUTTONS_ACTIVE_STATE,
// BUTTON_PULL,
// are all declared in your components/boards/pca10056.h file
{BUTTON_1, BUTTONS_ACTIVE_STATE, BUTTON_PULL, button_event_handler},
{BUTTON_2, BUTTONS_ACTIVE_STATE, BUTTON_PULL, button_event_handler},
{BUTTON_3, BUTTONS_ACTIVE_STATE, BUTTON_PULL, button_event_handler},
{BUTTON_4, BUTTONS_ACTIVE_STATE, BUTTON_PULL, button_event_handler}
};
/**
*@brief Function for initialising the timers.
* It is necessary for the buttons to work when using the app_button library.
*/
static void timers_init(void)
{
// Initialize timer module.
ret_code_t err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
}
/**@brief Function for initializing the buttons with the app_button library.
*
*/
void buttons_init(){
ret_code_t err_code;
// BUTTONS_NUMBER is declared in your components/boards/Ppca10056.h file
err_code = app_button_init((app_button_cfg_t *)app_buttons,
BUTTONS_NUMBER,
BUTTON_DETECTION_TIME);
APP_ERROR_CHECK(err_code);
err_code = app_button_enable();
APP_ERROR_CHECK(err_code);
}
/**@brief Function for initializing the leds with the bsp library.
*
*/
static void leds_init()
{
ret_code_t err_code;
err_code = bsp_init(BSP_INIT_LEDS , NULL);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for initializing the nrf log module.
*/
static void log_init(void)
{
ret_code_t err_code = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(err_code);
NRF_LOG_DEFAULT_BACKENDS_INIT();
}
/**@brief Function for initializing the usb.
*/
void usb_init(void)
{
ret_code_t ret;
static const app_usbd_config_t usbd_config = {
.ev_state_proc = usbd_user_ev_handler,
};
ret = nrf_drv_clock_init();
APP_ERROR_CHECK(ret);
nrf_drv_clock_lfclk_request(NULL);
while(!nrf_drv_clock_lfclk_is_running());
ret = app_usbd_init(&usbd_config);
APP_ERROR_CHECK(ret);
}
/**@brief Function for initializing the usb classes.
*/
void usb_classes_init(void)
{
ret_code_t ret;
app_usbd_class_inst_t const * class_inst_kbd;
class_inst_kbd = app_usbd_hid_kbd_class_inst_get(&m_app_hid_kbd);
ret = app_usbd_class_append(class_inst_kbd);
APP_ERROR_CHECK(ret);
}
/**@brief Function for the checking the usb power.
*/
void usb_power_check(void)
{
ret_code_t ret;
if (USBD_POWER_DETECTION)
{
ret = app_usbd_power_events_enable();
APP_ERROR_CHECK(ret);
}
else
{
#ifdef UART_PRINTING_ENABLED
NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now");
#endif
app_usbd_enable();
app_usbd_start();
}
}
// main
int main(void)
{
#ifdef UART_PRINTING_ENABLED
log_init();
#endif
timers_init();
leds_init();
buttons_init();
usb_init();
usb_classes_init();
usb_power_check();
while (true)
{
while (app_usbd_event_queue_process())
{
/* Nothing to do */
}
UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
/* Sleep CPU only if there was no interrupt since last loop processing */
__WFE();
}
}