Michiel Berckvens / Mbed 2 deprecated ProjectHTTP

Dependencies:   DS1307 TextLCD mbed

Committer:
Michielber
Date:
Thu Dec 04 10:36:40 2014 +0000
Revision:
0:f615d151a72c
Berckvens Michiel & Basteyns Jonas 4/12/2014

Who changed what in which revision?

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