summaryrefslogtreecommitdiffstats
path: root/usb/endpt1.c
diff options
context:
space:
mode:
Diffstat (limited to 'usb/endpt1.c')
-rw-r--r--usb/endpt1.c71
1 files changed, 51 insertions, 20 deletions
diff --git a/usb/endpt1.c b/usb/endpt1.c
index 0580349..dcfc61b 100644
--- a/usb/endpt1.c
+++ b/usb/endpt1.c
@@ -19,40 +19,73 @@
#include <reg/usbotg.h>
#include <stddef.h>
#include <stdint.h>
+#include <stdbool.h>
+
+#include <reg/gpio.h>
#include "../uart.h"
#include "bdt.h"
#include "endpt1.h"
+#include "keycodes.h"
+#include "txhandler.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;
+static struct tx_ctx tx;
+#define THIS_EP 1
+static volatile struct usb0_bd (* const this_bdt)[2] = usb_bdt[THIS_EP];
-void usb_endpt1_enable(void)
+unsigned char report[29];
+volatile bool sendrep;
+
+void usb_endpt1_setkey(enum keycode keycode, bool state)
{
- nextrep = 0;
+ unsigned char byte, bit;
+
+ if (keycode == KEY_NONE)
+ return;
+
+ keycode -= 4;
+ byte = keycode / 8;
+ bit = keycode % 8;
+
+ if (state)
+ SET_BIT(report[byte], bit);
+ else
+ UNSET_BIT(report[byte], bit);
+
+ sendrep = true;
+}
- 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++;
+void usb_endpt1_send(void)
+{
+ if (!sendrep)
+ return;
+ if (!tx_isempty(&tx)) {
+ return;
+ }
+
+ tx_que(&tx, report, sizeof report);
+ sendrep = false;
+}
+
+void usb_endpt1_enable(void)
+{
+ this_bdt[BDT_TX][BDT_EVEN].addr = NULL;
+ this_bdt[BDT_TX][BDT_EVEN].desc = 0;
- 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++;
+ this_bdt[BDT_TX][BDT_ODD].addr = NULL;
+ this_bdt[BDT_TX][BDT_ODD].desc = 0;
- USB0_ENDPT(1) = BV(ENDPT_EPTXEN) | BV(ENDPT_EPHSHK);
+ USB0_ENDPT(THIS_EP) = BV(ENDPT_EPTXEN) | BV(ENDPT_EPHSHK);
+ tx = TX_CTX(this_bdt[BDT_TX], MAX_PACKET);
+ tx_que(&tx, report, sizeof report);
}
void usb_endpt1_disable(void)
{
- USB0_ENDPT(1) = 0;
+ USB0_ENDPT(THIS_EP) = 0;
}
void usb_endpt1_token(uint8_t state)
@@ -63,9 +96,7 @@ void usb_endpt1_token(uint8_t state)
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;
+ tx_push(&tx);
break;
}
}