summaryrefslogtreecommitdiffstats
path: root/usb/endpt1.c
blob: 65684b52cae069a94eb476d953f871e323f60765 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
#include <reg/usbotg.h>
#include <stddef.h>
#include <stdint.h>

#include "../uart/uart.h"

#include "bdt.h"
#include "endpt1.h"

#define MAX_PACKET 64

static unsigned char report[4][4] = {
	{ 0x00, 5, 0, 0x00, },
	{ 0x00, 0, 5, 0x00, },
	{ 0x00, -5, 0, 0x00, },
	{ 0x00, 0, -5, 0x00, },
};
static int nextrep;

void usb_endpt1_enable(void)
{
	nextrep = 0;

	usb_bdt[1][BDT_TX][BDT_EVEN].addr = &report[0];
	usb_bdt[1][BDT_TX][BDT_EVEN].desc = USB0_BD_INIT(4, nextrep % 2);
	nextrep++;

	usb_bdt[1][BDT_TX][BDT_ODD].addr = &report[1];
	usb_bdt[1][BDT_TX][BDT_ODD].desc = USB0_BD_INIT(4, nextrep % 2);
	nextrep++;

	USB0_ENDPT(1) = BV(ENDPT_EPTXEN) | BV(ENDPT_EPHSHK);
}

void usb_endpt1_disable(void)
{
	USB0_ENDPT(1) = 0;
}

void usb_endpt1_token(uint8_t state)
{
	volatile struct usb0_bd *bd;

	bd = &usb_bdt[1][GET_BIT(state, STAT_TX)][GET_BIT(state, STAT_ODD)];

	switch (GET_BITS(bd->desc, BD_TOK_PID)) {
	case BD_TOK_PID_IN:
		bd->addr = &report[nextrep];
		bd->desc = USB0_BD_INIT(4, nextrep % 2);
		nextrep = (nextrep + 1) % 4;
		break;
	}
}