forked from nonolith/cee-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacketbuffer.c
62 lines (49 loc) · 1.42 KB
/
packetbuffer.c
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
54
55
56
57
58
59
60
61
62
// Bufferred BULK streaming
// http://nonolithlabs.com
// (C) 2011 Kevin Mehall (Nonolith Labs) <[email protected]>
//
// Licensed under the terms of the GNU GPLv3+
#include "packetbuffer.h"
uint8_t in_buf[PACKETS_BUFFER][IN_PACKET_SIZE] ATTR_ALIGNED(2);
uint8_t out_buf[PACKETS_BUFFER][OUT_PACKET_SIZE] ATTR_ALIGNED(2);
uint8_t in_start_index=0;
uint8_t in_end_index=0;
uint8_t in_count=0;
uint8_t out_start_index=0;
uint8_t out_end_index=0;
uint8_t out_count=0;
void packetbuf_endpoint_init(void){
USB_ep_in_init(1, USB_EP_TYPE_BULK_gc, IN_PACKET_SIZE);
USB_ep_out_init(2, USB_EP_TYPE_BULK_gc, OUT_PACKET_SIZE);
packetbuf_reset();
}
void packetbuf_reset(void){
in_start_index = in_end_index = in_count = 0;
out_start_index = out_count = 0;
USB_ep_in_reset(1);
USB_ep_out_reset(2);
out_end_index = 1;
USB_ep_out_start(2, out_buf[0]);
}
static void packetbuf_in_next_packet(void){
if (USB_ep_in_sent(1) && packetbuf_in_can_read()){
cli();
USB_ep_in_start(1, in_buf[in_start_index], IN_PACKET_SIZE);
in_start_index = (in_start_index+1)%PACKETS_BUFFER;
in_count--;
sei();
}
}
static void packetbuf_out_next_packet(void){
if (USB_ep_out_received(2) && packetbuf_out_can_write()){
cli();
USB_ep_out_start(2, out_buf[out_end_index]);
out_end_index = (out_end_index+1)%PACKETS_BUFFER;
out_count++;
sei();
}
}
void packetbuf_endpoint_poll(void){
packetbuf_in_next_packet();
packetbuf_out_next_packet();
}