Contiki-NG
usb-arch.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30/*---------------------------------------------------------------------------*/
31/**
32 * \addtogroup nrf
33 * @{
34 *
35 * \addtogroup nrf-dev Device drivers
36 * @{
37 *
38 * \addtogroup nrf-usb USB driver
39 * @{
40 *
41 * \file
42 * USB implementation for the nRF.
43 * \author
44 * Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
45 *
46 */
47/*---------------------------------------------------------------------------*/
48#include "contiki.h"
49/*---------------------------------------------------------------------------*/
50#if NRF_HAS_USB
51/*---------------------------------------------------------------------------*/
52#include "usb.h"
53#include "usb_descriptors.h"
54
55#include "nrfx.h"
56#include "nrfx_power.h"
57#include "nrf_ficr.h"
58/*---------------------------------------------------------------------------*/
59extern void tusb_hal_nrf_power_event(uint32_t event);
60/*---------------------------------------------------------------------------*/
61#define SERIAL_NUMBER_STRING_SIZE 12
62/*---------------------------------------------------------------------------*/
63static char serial[SERIAL_NUMBER_STRING_SIZE + 1];
64/*---------------------------------------------------------------------------*/
65void
66USBD_IRQHandler(void)
67{
69}
70/*---------------------------------------------------------------------------*/
71static void
72power_event_handler(nrfx_power_usb_evt_t event)
73{
74 tusb_hal_nrf_power_event((uint32_t)event);
75}
76/*---------------------------------------------------------------------------*/
77void
78usb_arch_init(void)
79{
80 const uint16_t serial_num_high_bytes = nrf_ficr_deviceid_get(NRF_FICR, 1) | 0xC000;
81 const uint32_t serial_num_low_bytes = nrf_ficr_deviceid_get(NRF_FICR, 0);
82 const nrfx_power_config_t power_config = { 0 };
83 const nrfx_power_usbevt_config_t power_usbevt_config = {
84 .handler = power_event_handler
85 };
86
87 nrfx_power_init(&power_config);
88
89 nrfx_power_usbevt_init(&power_usbevt_config);
90
91 nrfx_power_usbevt_enable();
92
93 // Set up descriptor
94 snprintf(serial,
95 SERIAL_NUMBER_STRING_SIZE + 1,
96 "%04"PRIX16"%08"PRIX32,
97 serial_num_high_bytes,
98 serial_num_low_bytes);
99
101
102 nrfx_power_usb_state_t usb_reg = nrfx_power_usbstatus_get();
103 if(usb_reg == NRFX_POWER_USB_STATE_CONNECTED) {
104 tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED);
105 } else if(usb_reg == NRFX_POWER_USB_STATE_READY) {
106 tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
107 }
108}
109/*---------------------------------------------------------------------------*/
110#endif /* NRF_HAS_USB */
111/*---------------------------------------------------------------------------*/
112/**
113 * @}
114 * @}
115 * @}
116 */
void usb_interrupt_handler(void)
Handles the interrupt.
Definition: usb.c:64
void usb_descriptor_set_serial(char *serial)
Set the serial.
void usb_arch_init(void)
Initialize the architecture specific USB driver.
USB descriptors header file for the nRF.