NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usb_mem.c Source File

usb_mem.c

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #include "usb_mem.h"
00025 
00026 #include "string.h" //For memcpy, memmove, memset
00027 
00028 #include "netCfg.h"
00029 #if NET_USB
00030 
00031 #define EDS_COUNT 4
00032 #define TDS_COUNT 8
00033 
00034 #define HCCA_SIZE 0x100
00035 #define ED_SIZE 0x10
00036 #define TD_SIZE 0x10
00037 
00038 #define TOTAL_SIZE (HCCA_SIZE + (EDS_COUNT*ED_SIZE) + (TDS_COUNT*TD_SIZE))
00039 
00040 
00041 static volatile __align(256) byte usb_buf[TOTAL_SIZE] __attribute((section("AHBSRAM1"),aligned));  //256 bytes aligned!
00042 
00043 static volatile byte* usb_hcca;  //256 bytes aligned!
00044 
00045 static volatile byte* usb_edBuf;  //4 bytes aligned!
00046 static volatile byte* usb_tdBuf;  //4 bytes aligned!
00047 
00048 static byte usb_edBufAlloc[EDS_COUNT] __attribute((section("AHBSRAM1"),aligned));
00049 static byte usb_tdBufAlloc[TDS_COUNT] __attribute((section("AHBSRAM1"),aligned));
00050 
00051 void usb_mem_init()
00052 {
00053   usb_hcca = usb_buf;
00054   usb_edBuf = usb_buf + HCCA_SIZE;
00055   usb_tdBuf = usb_buf + HCCA_SIZE + (EDS_COUNT*ED_SIZE);
00056   memset(usb_edBufAlloc, 0, EDS_COUNT);
00057   memset(usb_tdBufAlloc, 0, TDS_COUNT);
00058 }
00059 
00060 volatile byte* usb_get_hcca()
00061 {
00062   return usb_hcca;
00063 }
00064 
00065 volatile byte* usb_get_ed()
00066 {
00067   int i;
00068   for(i = 0; i < EDS_COUNT; i++)
00069   {
00070     if( !usb_edBufAlloc[i] )
00071     {
00072       usb_edBufAlloc[i] = 1;
00073       return usb_edBuf + i*ED_SIZE;
00074     }
00075   }
00076   return NULL; //Could not alloc ED
00077 }
00078 
00079 volatile byte* usb_get_td()
00080 {
00081   int i;
00082   for(i = 0; i < TDS_COUNT; i++)
00083   {
00084     if( !usb_tdBufAlloc[i] )
00085     {
00086       usb_tdBufAlloc[i] = 1;
00087       return usb_tdBuf + i*TD_SIZE;
00088     }
00089   }
00090   return NULL; //Could not alloc TD
00091 }
00092 
00093 void usb_free_ed(volatile byte* ed)
00094 {
00095   int i;
00096   i = (ed - usb_edBuf) / ED_SIZE;
00097   usb_edBufAlloc[i] = 0;
00098 }
00099 
00100 void usb_free_td(volatile byte* td)
00101 {
00102   int i;
00103   i = (td - usb_tdBuf) / TD_SIZE;
00104   usb_tdBufAlloc[i] = 0;
00105 }
00106 
00107 
00108 #endif