Contiki-NG
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];
57static char manufacturer[] = "Contiki-NG";
58static char product[] = "Contiki-NG USB";
59static char cdc_interface[] = "Contiki-NG CDC";
60/*---------------------------------------------------------------------------*/
61PROCESS(usb_arch_process, "USB Arch");
62/*---------------------------------------------------------------------------*/
63void
65{
66 tud_int_handler(0);
67
68 /* Poll the process since we are in interrupt context */
69 process_poll(&usb_arch_process);
70}
71/*---------------------------------------------------------------------------*/
72
73/*---------------------------------------------------------------------------*/
74void
75usb_set_input(int (*input)(unsigned char c))
76{
77 input_handler = input;
78}
79/*---------------------------------------------------------------------------*/
80void
82{
86
88
89 /* Initialize the usb process */
90 process_start(&usb_arch_process, NULL);
91}
92/*---------------------------------------------------------------------------*/
93void
94usb_write(uint8_t *buffer, uint32_t buffer_size)
95{
96 uint32_t i;
97
98 i = 0;
99 /* Operating with BULK_PACKET_SIZE as 64 (Not high speed) */
100 while(buffer_size > BULK_PACKET_SIZE) {
101 tud_cdc_write(buffer + i, BULK_PACKET_SIZE);
102 usb_flush();
103 i += BULK_PACKET_SIZE;
104 buffer_size -= BULK_PACKET_SIZE;
105 }
106
107 /* Don't flush on the last write */
108 tud_cdc_write(buffer + i, buffer_size);
109}
110/*---------------------------------------------------------------------------*/
111void
113{
114 tud_cdc_write_flush();
115 /* Call the task to handle the events immediately */
116 tud_task();
117}
118/*---------------------------------------------------------------------------*/
119void
120cdc_task(void)
121{
122 uint32_t usb_read_length;
123 uint32_t i;
124
125 if(tud_cdc_available()) {
126 usb_read_length = tud_cdc_read(usb_buffer, sizeof(usb_buffer));
127 for(i = 0; i < usb_read_length && input_handler; i++) {
128 input_handler(usb_buffer[i]);
129 }
130 }
131}
132/*---------------------------------------------------------------------------*/
133PROCESS_THREAD(usb_arch_process, ev, data)
134{
136
137 tusb_init();
138
139 while(1) {
140 PROCESS_YIELD_UNTIL(tud_task_event_ready());
141
142 tud_task();
143
144 cdc_task();
145 }
146
147 PROCESS_END();
148}
149/*---------------------------------------------------------------------------*/
150/**
151 * @}
152 * @}
153 */
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1154
void usb_set_input(int(*input)(unsigned char c))
Sets the input handler called in the event handler.
Definition: usb.c:75
void usb_interrupt_handler(void)
Handles the interrupt.
Definition: usb.c:64
void usb_flush(void)
Flush USB buffer.
Definition: usb.c:112
void usb_write(uint8_t *buffer, uint32_t buffer_size)
Writes to the USB driver.
Definition: usb.c:94
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:81
void usb_descriptor_set_product(char *product)
Set the product.
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
#define PROCESS_YIELD_UNTIL(c)
Yield the currently running process until a condition occurs.
Definition: process.h:178
void process_poll(struct process *p)
Request a process to be polled.
Definition: process.c:371
static void input(void)
Process a received 6lowpan packet.
Definition: sicslowpan.c:1833
USB header file for the nRF.
USB descriptors header file for the nRF.