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