cdcacm.c 2.73 KB
Newer Older
swym's avatar
swym committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * 
 * epicardium-specific implemnetation of cdcacm device
 * services CDCACM API
 */

#include "usb/epc_usb.h"
#include "usb/cdcacm.h"
#include "usb/descriptors.h"

#include <stdio.h>
#include <stddef.h>
#include <errno.h>

#include "usb.h"
#include "usb_event.h"
#include "cdc_acm.h"

#include "modules/log.h"
20
#include "modules/modules.h"
swym's avatar
swym committed
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

#include "FreeRTOS.h"
#include "task.h"

static inline struct config_descriptor_cdcacm *
descriptors(struct esb_config *self)
{
	return self->descriptors->cdcacm;
}

/******************************************************************************/
static volatile int usb_read_complete; // SWYM: only ever written to
static int cb_acm_read_ready(void)
{
	usb_read_complete = 1;
	return 0;
}

int esb_cdc_init(struct esb_config *self)
{
	LOG_DEBUG("cdcacm", "init");
	struct config_descriptor_cdcacm *dsc = descriptors(self);
	usb_read_complete                    = 0;
	acm_register_callback(
		ACM_CB_READ_READY,
		cb_acm_read_ready); //SWYM: actually not needed
	return acm_init(&dsc->comm_interface);
}

int esb_cdc_configure(struct esb_config *self)
{
	struct config_descriptor_cdcacm *dsc = descriptors(self);

	//acm_configure does not keep the amc_cfg_t pointer around
	//so stack-local lifetime is fine
	acm_cfg_t acm_cfg = {
		.in_ep         = dsc->endpoint_in.bEndpointAddress & 0x0f,
		.in_maxpacket  = MXC_USBHS_MAX_PACKET,
		.out_ep        = dsc->endpoint_out.bEndpointAddress,
		.out_maxpacket = MXC_USBHS_MAX_PACKET,
		.notify_ep     = dsc->endpoint_notify.bEndpointAddress & 0x0f,
		.notify_maxpacket = MXC_USBHS_MAX_PACKET,
	};

	LOG_DEBUG(
		"cdcacm",
		"configure, endpoints %d,%d,%d",
		acm_cfg.in_ep,
		acm_cfg.out_ep,
		acm_cfg.notify_ep
	);

	return acm_configure(&acm_cfg);
}

int esb_cdc_deconfigure(struct esb_config *self)
{
	LOG_DEBUG("cdcacm", "deconfigure");
	return acm_deconfigure();
}

int cdcacm_num_read_avail(void)
{
	return acm_canread();
}

uint8_t cdcacm_read(void)
{
	while (acm_canread() <= 0) {
	}

	uint8_t buf;
	acm_read(&buf, 1);
	return buf;
}

void cdcacm_write(uint8_t *data, int len)
{
	static int lockup_disable = 0;
	if (acm_present() && !lockup_disable) {
		int ret = acm_write(data, len);
		if (ret < 0) {
			LOG_ERR("cdcacm", "fifo lockup detected");
			lockup_disable = 1;
		} else if (ret != len) {
			LOG_WARN(
				"cdcacm", "write length mismatch, got %d", ret
			);
		}
	}
}

/******************************************************************************/

extern TaskHandle_t serial_task_id;
void USB_IRQHandler(void)
{
	usb_event_handler();

	if (serial_task_id != NULL) {
		BaseType_t xHigherPriorityTaskWoken = pdFALSE;
122
123
124
125
126
		xTaskNotifyFromISR(
			serial_task_id,
			SERIAL_READ_NOTIFY,
			eSetBits,
			&xHigherPriorityTaskWoken
swym's avatar
swym committed
127
128
129
130
		);
		portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
	}
}