Skip to content

Commit

Permalink
add USB functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
joeycastillo committed Jul 12, 2024
1 parent b945c1d commit d5d1054
Show file tree
Hide file tree
Showing 8 changed files with 407 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ GOSSAMER_PATH=gossamer
# or omit it and provide it on the command line (make BOARD=foo).
BOARD=sensorwatch_green

# TinyUSB configuration: we want one CDC interface.
TINYUSB_CDC=1

# Leave this line here.
include $(GOSSAMER_PATH)/make.mk

Expand All @@ -15,8 +18,12 @@ SRCS += \
./watch-library/hardware/watch/watch_buzzer.c \
./watch-library/hardware/watch/watch_led.c \
./watch-library/hardware/watch/watch_private.c \
./watch-library/hardware/watch/watch_usb_descriptors.c \
./watch-library/hardware/watch/watch_usb_cdc.c \

INCLUDES += \
-I./tinyusb/src \
-I./watch-library/hardware/watch \
-I./watch-library/shared/watch \

# Finally, leave this line at the bottom of the file.
Expand Down
45 changes: 45 additions & 0 deletions app.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
/*
* MIT License
*
* Copyright (c) 2021-2024 Joey Castillo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include "app.h"
#include "delay.h"
#include "watch.h"
#include "watch_private.h"
#include "usb.h"
#include "tusb.h"
#include "watch_usb_cdc.h"

void yield(void) {
tud_task();
cdc_task();
}

void app_init(void) {
// check if we are plugged into USB power.
HAL_GPIO_VBUS_DET_in();
HAL_GPIO_VBUS_DET_pulldown();
if (HAL_GPIO_VBUS_DET_read()){
/// if so, enable USB functionality.
_watch_enable_usb();
}
HAL_GPIO_VBUS_DET_off();

_watch_init();
}

Expand All @@ -17,5 +58,9 @@ bool app_loop(void) {
watch_set_led_off();
delay_ms(500);

if (usb_is_enabled()) {
yield();
}

return false;
}
6 changes: 6 additions & 0 deletions watch-library/hardware/watch/watch_private.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "watch_private.h"
#include "tcc.h"
#include "tc.h"
#include "usb.h"

void _watch_init(void) {
// disable debugger hot-plugging
Expand Down Expand Up @@ -197,3 +198,8 @@ void _watch_disable_tcc(void) {
#endif
tcc_disable(0);
}

void _watch_enable_usb(void) {
usb_init();
usb_enable();
}
157 changes: 157 additions & 0 deletions watch-library/hardware/watch/watch_usb_cdc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* MIT License
*
* Copyright (c) 2020 Joey Castillo
* Copyright (c) 2023 Edward Shin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <stddef.h>
#include "watch_usb_cdc.h"
#include "tusb.h"

/*
* Implement a circular buffer for the USB CDC Serial read buffer.
* The size of the buffer must be a power of two for this circular buffer
* implementation to work.
*/

// Size of the circular buffer. Must be a power of two.
#define CDC_WRITE_BUF_SZ (1024)
// Macro function to perform modular arithmetic on an index.
// eg. (63 + 2) & (64 - 1) -> 1
#define CDC_WRITE_BUF_IDX(x) ((x) & (CDC_WRITE_BUF_SZ - 1))
static char s_write_buf[CDC_WRITE_BUF_SZ] = {0};
static size_t s_write_buf_pos = 0;
static size_t s_write_buf_len = 0;

#define CDC_READ_BUF_SZ (256)
#define CDC_READ_BUF_IDX(x) ((x) & (CDC_READ_BUF_SZ - 1))
static char s_read_buf[CDC_READ_BUF_SZ] = {0};
static size_t s_read_buf_pos = 0;
static size_t s_read_buf_len = 0;

// Mask TC1 interrupts, preventing calls to cdc_task()
static inline void prv_critical_section_enter(void) {
NVIC_DisableIRQ(TC1_IRQn);
}

// Unmask TC1 interrupts, allowing calls to cdc_task()
static inline void prv_critical_section_exit(void) {
NVIC_EnableIRQ(TC1_IRQn);
}

int _write(int file, char *ptr, int len) {
(void) file;

if (ptr == NULL || len <= 0) {
return -1;
}

int bytes_written = 0;

prv_critical_section_enter();

for (int i = 0; i < len; i++) {
s_write_buf[s_write_buf_pos] = ptr[i];
s_write_buf_pos = CDC_WRITE_BUF_IDX(s_write_buf_pos + 1);
if (s_write_buf_len < CDC_WRITE_BUF_SZ) {
s_write_buf_len++;
}
bytes_written++;
}

prv_critical_section_exit();

return bytes_written;
}

int _read(int file, char *ptr, int len) {
(void) file;

prv_critical_section_enter();

if (ptr == NULL || len <= 0 || s_read_buf_len == 0) {
prv_critical_section_exit();
return -1;
}

// Clamp to the length of the read buffer
if ((size_t) len > s_read_buf_len) {
len = s_read_buf_len;
}

// Calculate the start of the circular buffer, and iterate from there
const size_t start_pos = CDC_READ_BUF_IDX(s_read_buf_pos - len);
for (size_t i = 0; i < (size_t) len; i++) {
const size_t idx = CDC_READ_BUF_IDX(start_pos + i);
ptr[i] = s_read_buf[idx];
s_read_buf[idx] = 0;
}

// Update circular buffer position and length
s_read_buf_len -= len;
s_read_buf_pos = CDC_READ_BUF_IDX(s_read_buf_pos - len);

prv_critical_section_exit();

return len;
}

static void prv_handle_reads(void) {
while (tud_cdc_available()) {
int c = tud_cdc_read_char();
if (c < 0) {
continue;
}
s_read_buf[s_read_buf_pos] = c;
s_read_buf_pos = CDC_READ_BUF_IDX(s_read_buf_pos + 1);
if (s_read_buf_len < CDC_READ_BUF_SZ) {
s_read_buf_len++;
}
}
}

static void prv_handle_writes(void) {
if (s_write_buf_len > 0) {
const size_t start_pos =
CDC_WRITE_BUF_IDX(s_write_buf_pos - s_write_buf_len);
for (size_t i = 0; i < (size_t) s_write_buf_len; i++) {
const size_t idx = CDC_WRITE_BUF_IDX(start_pos + i);
if (tud_cdc_available() > 0) {
// If we receive data while doing a large write, we need to
// fully service it before continuing to write, or the
// stack will crash.
prv_handle_reads();
}
if (tud_cdc_write_available()) {
tud_cdc_write(&s_write_buf[idx], 1);
}
s_write_buf[idx] = 0;
s_write_buf_len--;
}
tud_cdc_write_flush();
}
}

void cdc_task(void) {
prv_handle_reads();
prv_handle_writes();
}
33 changes: 33 additions & 0 deletions watch-library/hardware/watch/watch_usb_cdc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* MIT License
*
* Copyright (c) 2020 Joey Castillo
* Copyright (c) 2023 Edward Shin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef _WATCH_PRIVATE_CDC_H_INCLUDED
#define _WATCH_PRIVATE_CDC_H_INCLUDED

int _write(int file, char *ptr, int len);
int _read(int file, char *ptr, int len);
void cdc_task(void);

#endif
Loading

0 comments on commit d5d1054

Please sign in to comment.