cdcacm.c 2.88 KB
Newer Older
swym's avatar
swym committed
1
/**
Rahix's avatar
Rahix committed
2
 *
swym's avatar
swym committed
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

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

25
26
static volatile int lockup_disable = 0;

swym's avatar
swym committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
}

41
42
43
44
45
46
static int cb_acm_connected(void)
{
	lockup_disable = 0;
	return 0;
}

swym's avatar
swym committed
47
48
49
50
51
int esb_cdc_init(struct esb_config *self)
{
	LOG_DEBUG("cdcacm", "init");
	struct config_descriptor_cdcacm *dsc = descriptors(self);
	usb_read_complete                    = 0;
52
53

	int ret = acm_init(&dsc->comm_interface);
swym's avatar
swym committed
54
55
56
	acm_register_callback(
		ACM_CB_READ_READY,
		cb_acm_read_ready); //SWYM: actually not needed
57
58
	acm_register_callback(ACM_CB_CONNECTED, cb_acm_connected);
	return ret;
swym's avatar
swym committed
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
122
123
124
125
126
127
128
129
130
131
}

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)
{
	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;
132
133
134
135
136
		xTaskNotifyFromISR(
			serial_task_id,
			SERIAL_READ_NOTIFY,
			eSetBits,
			&xHigherPriorityTaskWoken
swym's avatar
swym committed
137
138
139
140
		);
		portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
	}
}