User | Revision | Line number | New contents of line |
mbed714 |
0:d616ece2d859
|
1
|
|
mbed714 |
0:d616ece2d859
|
2
|
/*
|
mbed714 |
0:d616ece2d859
|
3
|
Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
|
mbed714 |
0:d616ece2d859
|
4
|
|
mbed714 |
0:d616ece2d859
|
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
mbed714 |
0:d616ece2d859
|
6
|
of this software and associated documentation files (the "Software"), to deal
|
mbed714 |
0:d616ece2d859
|
7
|
in the Software without restriction, including without limitation the rights
|
mbed714 |
0:d616ece2d859
|
8
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
mbed714 |
0:d616ece2d859
|
9
|
copies of the Software, and to permit persons to whom the Software is
|
mbed714 |
0:d616ece2d859
|
10
|
furnished to do so, subject to the following conditions:
|
mbed714 |
0:d616ece2d859
|
11
|
|
mbed714 |
0:d616ece2d859
|
12
|
The above copyright notice and this permission notice shall be included in
|
mbed714 |
0:d616ece2d859
|
13
|
all copies or substantial portions of the Software.
|
mbed714 |
0:d616ece2d859
|
14
|
|
mbed714 |
0:d616ece2d859
|
15
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
mbed714 |
0:d616ece2d859
|
16
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
mbed714 |
0:d616ece2d859
|
17
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
mbed714 |
0:d616ece2d859
|
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
mbed714 |
0:d616ece2d859
|
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
mbed714 |
0:d616ece2d859
|
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
mbed714 |
0:d616ece2d859
|
21
|
THE SOFTWARE.
|
mbed714 |
0:d616ece2d859
|
22
|
*/
|
mbed714 |
0:d616ece2d859
|
23
|
|
mbed714 |
0:d616ece2d859
|
24
|
#include "usb_mem.h"
|
mbed714 |
0:d616ece2d859
|
25
|
|
mbed714 |
0:d616ece2d859
|
26
|
#include "string.h" //For memcpy, memmove, memset
|
mbed714 |
0:d616ece2d859
|
27
|
|
mbed714 |
0:d616ece2d859
|
28
|
#include "netCfg.h"
|
mbed714 |
0:d616ece2d859
|
29
|
#if NET_USB
|
mbed714 |
0:d616ece2d859
|
30
|
|
mbed714 |
0:d616ece2d859
|
31
|
#define EDS_COUNT 4
|
mbed714 |
0:d616ece2d859
|
32
|
#define TDS_COUNT 8
|
mbed714 |
0:d616ece2d859
|
33
|
|
mbed714 |
0:d616ece2d859
|
34
|
#define HCCA_SIZE 0x100
|
mbed714 |
0:d616ece2d859
|
35
|
#define ED_SIZE 0x10
|
mbed714 |
0:d616ece2d859
|
36
|
#define TD_SIZE 0x10
|
mbed714 |
0:d616ece2d859
|
37
|
|
mbed714 |
0:d616ece2d859
|
38
|
#define TOTAL_SIZE (HCCA_SIZE + (EDS_COUNT*ED_SIZE) + (TDS_COUNT*TD_SIZE))
|
mbed714 |
0:d616ece2d859
|
39
|
|
mbed714 |
0:d616ece2d859
|
40
|
|
mbed714 |
0:d616ece2d859
|
41
|
static volatile __align(256) byte usb_buf[TOTAL_SIZE] __attribute((section("AHBSRAM1"),aligned)); //256 bytes aligned!
|
mbed714 |
0:d616ece2d859
|
42
|
|
mbed714 |
0:d616ece2d859
|
43
|
static volatile byte* usb_hcca; //256 bytes aligned!
|
mbed714 |
0:d616ece2d859
|
44
|
|
mbed714 |
0:d616ece2d859
|
45
|
static volatile byte* usb_edBuf; //4 bytes aligned!
|
mbed714 |
0:d616ece2d859
|
46
|
static volatile byte* usb_tdBuf; //4 bytes aligned!
|
mbed714 |
0:d616ece2d859
|
47
|
|
mbed714 |
0:d616ece2d859
|
48
|
static byte usb_edBufAlloc[EDS_COUNT] __attribute((section("AHBSRAM1"),aligned));
|
mbed714 |
0:d616ece2d859
|
49
|
static byte usb_tdBufAlloc[TDS_COUNT] __attribute((section("AHBSRAM1"),aligned));
|
mbed714 |
0:d616ece2d859
|
50
|
|
mbed714 |
0:d616ece2d859
|
51
|
void usb_mem_init()
|
mbed714 |
0:d616ece2d859
|
52
|
{
|
mbed714 |
0:d616ece2d859
|
53
|
usb_hcca = usb_buf;
|
mbed714 |
0:d616ece2d859
|
54
|
usb_edBuf = usb_buf + HCCA_SIZE;
|
mbed714 |
0:d616ece2d859
|
55
|
usb_tdBuf = usb_buf + HCCA_SIZE + (EDS_COUNT*ED_SIZE);
|
mbed714 |
0:d616ece2d859
|
56
|
memset(usb_edBufAlloc, 0, EDS_COUNT);
|
mbed714 |
0:d616ece2d859
|
57
|
memset(usb_tdBufAlloc, 0, TDS_COUNT);
|
mbed714 |
0:d616ece2d859
|
58
|
}
|
mbed714 |
0:d616ece2d859
|
59
|
|
mbed714 |
0:d616ece2d859
|
60
|
volatile byte* usb_get_hcca()
|
mbed714 |
0:d616ece2d859
|
61
|
{
|
mbed714 |
0:d616ece2d859
|
62
|
return usb_hcca;
|
mbed714 |
0:d616ece2d859
|
63
|
}
|
mbed714 |
0:d616ece2d859
|
64
|
|
mbed714 |
0:d616ece2d859
|
65
|
volatile byte* usb_get_ed()
|
mbed714 |
0:d616ece2d859
|
66
|
{
|
mbed714 |
0:d616ece2d859
|
67
|
int i;
|
mbed714 |
0:d616ece2d859
|
68
|
for(i = 0; i < EDS_COUNT; i++)
|
mbed714 |
0:d616ece2d859
|
69
|
{
|
mbed714 |
0:d616ece2d859
|
70
|
if( !usb_edBufAlloc[i] )
|
mbed714 |
0:d616ece2d859
|
71
|
{
|
mbed714 |
0:d616ece2d859
|
72
|
usb_edBufAlloc[i] = 1;
|
mbed714 |
0:d616ece2d859
|
73
|
return usb_edBuf + i*ED_SIZE;
|
mbed714 |
0:d616ece2d859
|
74
|
}
|
mbed714 |
0:d616ece2d859
|
75
|
}
|
mbed714 |
0:d616ece2d859
|
76
|
return NULL; //Could not alloc ED
|
mbed714 |
0:d616ece2d859
|
77
|
}
|
mbed714 |
0:d616ece2d859
|
78
|
|
mbed714 |
0:d616ece2d859
|
79
|
volatile byte* usb_get_td()
|
mbed714 |
0:d616ece2d859
|
80
|
{
|
mbed714 |
0:d616ece2d859
|
81
|
int i;
|
mbed714 |
0:d616ece2d859
|
82
|
for(i = 0; i < TDS_COUNT; i++)
|
mbed714 |
0:d616ece2d859
|
83
|
{
|
mbed714 |
0:d616ece2d859
|
84
|
if( !usb_tdBufAlloc[i] )
|
mbed714 |
0:d616ece2d859
|
85
|
{
|
mbed714 |
0:d616ece2d859
|
86
|
usb_tdBufAlloc[i] = 1;
|
mbed714 |
0:d616ece2d859
|
87
|
return usb_tdBuf + i*TD_SIZE;
|
mbed714 |
0:d616ece2d859
|
88
|
}
|
mbed714 |
0:d616ece2d859
|
89
|
}
|
mbed714 |
0:d616ece2d859
|
90
|
return NULL; //Could not alloc TD
|
mbed714 |
0:d616ece2d859
|
91
|
}
|
mbed714 |
0:d616ece2d859
|
92
|
|
mbed714 |
0:d616ece2d859
|
93
|
void usb_free_ed(volatile byte* ed)
|
mbed714 |
0:d616ece2d859
|
94
|
{
|
mbed714 |
0:d616ece2d859
|
95
|
int i;
|
mbed714 |
0:d616ece2d859
|
96
|
i = (ed - usb_edBuf) / ED_SIZE;
|
mbed714 |
0:d616ece2d859
|
97
|
usb_edBufAlloc[i] = 0;
|
mbed714 |
0:d616ece2d859
|
98
|
}
|
mbed714 |
0:d616ece2d859
|
99
|
|
mbed714 |
0:d616ece2d859
|
100
|
void usb_free_td(volatile byte* td)
|
mbed714 |
0:d616ece2d859
|
101
|
{
|
mbed714 |
0:d616ece2d859
|
102
|
int i;
|
mbed714 |
0:d616ece2d859
|
103
|
i = (td - usb_tdBuf) / TD_SIZE;
|
mbed714 |
0:d616ece2d859
|
104
|
usb_tdBufAlloc[i] = 0;
|
mbed714 |
0:d616ece2d859
|
105
|
}
|
mbed714 |
0:d616ece2d859
|
106
|
|
mbed714 |
0:d616ece2d859
|
107
|
|
mbed714 |
0:d616ece2d859
|
108
|
#endif
|