Contiki-NG
Loading...
Searching...
No Matches
usb.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-usb USB driver
36 * @{
37 * *
38 * \file
39 * USB driver for the nRF.
40 * \author
41 * Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
42 *
43 */
44/*---------------------------------------------------------------------------*/
45#include "contiki.h"
46
47#include "usb.h"
48#include "usb_descriptors.h"
49
50#include "tusb.h"
51#include "usbd.h"
52/*---------------------------------------------------------------------------*/
53#define BULK_PACKET_SIZE 64
54/*---------------------------------------------------------------------------*/
55static int (*input_handler)(unsigned char c) = NULL;
56static unsigned char usb_buffer[BULK_PACKET_SIZE];
57/* Match the strings the OLD arch/platform/nrf52840 USB stack used so
58 * the dongle keeps the same iManufacturer / iProduct across firmware
59 * versions. The iManufacturer string should also match the owner of
60 * the iVendor VID (0x1915 = Nordic Semiconductor) per the USB IF. */
61static char manufacturer[] = "Nordic Semiconductor";
62static char product[] = "nRF52 USB CDC on Contiki-NG";
63static char cdc_interface[] = "Contiki-NG CDC";
64/*---------------------------------------------------------------------------*/
65PROCESS(usb_arch_process, "USB Arch");
66/*---------------------------------------------------------------------------*/
67void
69{
70 tud_int_handler(0);
71
72 /* Poll the process since we are in interrupt context */
73 process_poll(&usb_arch_process);
74}
75/*---------------------------------------------------------------------------*/
76
77/*---------------------------------------------------------------------------*/
78void
79usb_set_input(int (*input)(unsigned char c))
80{
81 input_handler = input;
82}
83/*---------------------------------------------------------------------------*/
84void
86{
90
92
93 /* Initialize the usb process */
94 process_start(&usb_arch_process, NULL);
95}
96/*---------------------------------------------------------------------------*/
97void
98usb_write(uint8_t *buffer, uint32_t buffer_size)
99{
100 uint32_t i;
101
102 i = 0;
103 /* Operating with BULK_PACKET_SIZE as 64 (Not high speed) */
104 while(buffer_size > BULK_PACKET_SIZE) {
105 tud_cdc_write(buffer + i, BULK_PACKET_SIZE);
106 usb_flush();
107 i += BULK_PACKET_SIZE;
108 buffer_size -= BULK_PACKET_SIZE;
109 }
110
111 /* Don't flush on the last write */
112 tud_cdc_write(buffer + i, buffer_size);
113}
114/*---------------------------------------------------------------------------*/
115void
117{
118 tud_cdc_write_flush();
119 /* Call the task to handle the events immediately */
120 tud_task();
121}
122/*---------------------------------------------------------------------------*/
123void
124cdc_task(void)
125{
126 uint32_t usb_read_length;
127 uint32_t i;
128
129 if(tud_cdc_available()) {
130 usb_read_length = tud_cdc_read(usb_buffer, sizeof(usb_buffer));
131 for(i = 0; i < usb_read_length && input_handler; i++) {
132 input_handler(usb_buffer[i]);
133 }
134 }
135}
136/*---------------------------------------------------------------------------*/
137PROCESS_THREAD(usb_arch_process, ev, data)
138{
140
141 tusb_init();
142
143 while(1) {
144 PROCESS_YIELD_UNTIL(tud_task_event_ready());
145
146 tud_task();
147
148 cdc_task();
149 }
150
151 PROCESS_END();
152}
153/*---------------------------------------------------------------------------*/
154/**
155 * @}
156 * @}
157 */
void usb_set_input(int(*input)(unsigned char c))
Sets the input handler called in the event handler.
Definition usb.c:79
void usb_interrupt_handler(void)
Handles the interrupt.
Definition usb.c:68
void usb_flush(void)
Flush USB buffer.
Definition usb.c:116
void usb_write(uint8_t *buffer, uint32_t buffer_size)
Writes to the USB driver.
Definition usb.c:98
void usb_descriptor_set_manufacturer(char *manufacturer)
Set the manufactorer.
void usb_descriptor_set_cdc_interface(char *cdc_interface)
Set the cdc interface.
void usb_arch_init(void)
Initialize the architecture specific USB driver.
void usb_init(void)
Initialize the USB driver.
Definition usb.c:85
void usb_descriptor_set_product(char *product)
Set the product.
#define PROCESS(name, strname)
Declare a process.
Definition process.h:309
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition process.h:122
#define PROCESS_END()
Define the end of a process.
Definition process.h:133
void process_start(struct process *p, process_data_t data)
Start a process.
Definition process.c:121
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition process.h:275
#define PROCESS_YIELD_UNTIL(c)
Yield the currently running process until a condition occurs.
Definition process.h:180
void process_poll(struct process *p)
Request a process to be polled.
Definition process.c:366
static void input(void)
Process a received 6lowpan packet.
USB header file for the nRF.
USB descriptors header file for the nRF.