Takao Aratani / uvchost

Dependents:   WebCamera_SD

Fork of uvchost by Norimasa Okamoto

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 #include "mbed.h"
00024 #define __DEBUG
00025 #include "mydbg.h"
00026 #include "usb_mem.h"
00027 #include "string.h" //For memcpy, memmove, memset
00028 #include "UsbInc.h"
00029 
00030 #define EDS_COUNT 16
00031 #define TDS_COUNT  0
00032 #define ITDS_COUNT 0
00033 #define UTDS_COUNT 32
00034 #define BPS_COUNT 9
00035 
00036 #define HCCA_SIZE 0x100
00037 #define ED_SIZE 0x10
00038 #define TD_SIZE 0x10
00039 #define ITD_SIZE 0x20
00040 #define UTD_SIZE (32+16)
00041 #define BP_SIZE  (128*8)
00042 
00043 #define TOTAL_SIZE (HCCA_SIZE + (EDS_COUNT*ED_SIZE) + (TDS_COUNT*TD_SIZE) + (ITDS_COUNT*ITD_SIZE))
00044 
00045 static volatile __align(256) byte usb_buf[TOTAL_SIZE] __attribute((section("AHBSRAM0"),aligned));  //256 bytes aligned!
00046 static volatile __align(32) uint8_t usb_utdBuf[UTDS_COUNT*UTD_SIZE] __attribute((section("AHBSRAM0"),aligned));
00047 
00048 static volatile byte* usb_hcca;  //256 bytes aligned!
00049 
00050 static volatile byte* usb_edBuf;  //4 bytes aligned!
00051 
00052 static byte usb_edBufAlloc[EDS_COUNT] __attribute((section("AHBSRAM0"),aligned));
00053 static uint8_t usb_utdBufAlloc[UTDS_COUNT] __attribute((section("AHBSRAM0"),aligned));
00054 static uint8_t usb_bpBufAlloc[BPS_COUNT] __attribute((section("AHBSRAM0"),aligned));
00055 static uint8_t usb_bpBuf[BP_SIZE*BPS_COUNT] __attribute((section("AHBSRAM0"),aligned));
00056 
00057 static void utd_init()
00058 {
00059     DBG_ASSERT(sizeof(HCTD) == 16);
00060     DBG_ASSERT(sizeof(HCITD) == 32);
00061     DBG_ASSERT(sizeof(HCUTD) == 48);
00062 
00063     DBG_ASSERT(((uint32_t)usb_utdBuf % 16) == 0);
00064     DBG_ASSERT(((uint32_t)usb_utdBuf % 32) == 0);
00065 
00066     DBG_ASSERT((uint32_t)usb_utdBufAlloc >= 0x2007c000);
00067     DBG_ASSERT((uint32_t)&usb_utdBufAlloc[UTDS_COUNT] <= 0x2007ffff);
00068 
00069     DBG_ASSERT((uint32_t)usb_utdBuf >= 0x2007c000);
00070     DBG_ASSERT((uint32_t)&usb_utdBuf[UTD_SIZE*UTDS_COUNT] <= 0x2007cfff);
00071 
00072     memset(usb_utdBufAlloc, 0x00, UTDS_COUNT);
00073 }
00074 
00075 static void pb_init()
00076 {
00077     memset(usb_bpBufAlloc, 0x00, BPS_COUNT);
00078 
00079     DBG_ASSERT((uint32_t)usb_bpBufAlloc >= 0x2007c000);
00080     DBG_ASSERT((uint32_t)&usb_bpBufAlloc[BPS_COUNT] <= 0x2007ffff);
00081     DBG_ASSERT((uint32_t)usb_bpBuf >= 0x2007c000);
00082     DBG_ASSERT((uint32_t)(&usb_bpBuf[BP_SIZE*BPS_COUNT]) <= 0x2007ffff);
00083 }
00084 
00085 void usb_mem_init()
00086 {
00087   usb_hcca = usb_buf;
00088   usb_edBuf = usb_buf + HCCA_SIZE;
00089   memset(usb_edBufAlloc, 0, EDS_COUNT);
00090 
00091   utd_init();
00092   pb_init();
00093 
00094   DBG("--- Memory Map --- \n");
00095   DBG("usb_hcca       =%p\n", usb_hcca);
00096   DBG("usb_edBuf      =%p\n", usb_edBuf);
00097   DBG("usb_utdBuf     =%p\n", usb_utdBuf);
00098   DBG("usb_edBufAlloc =%p\n", usb_edBufAlloc);
00099   DBG("usb_utdBufAlloc=%p\n", usb_utdBufAlloc);
00100   DBG("usb_bpBuf      =%p\n", usb_bpBuf);
00101   DBG("               =%p\n", &usb_bpBuf[BP_SIZE*BPS_COUNT]);
00102   DBG_ASSERT(((uint32_t)usb_hcca % 256) == 0);
00103   DBG_ASSERT(((uint32_t)usb_edBuf % 16) == 0);
00104   DBG_ASSERT(((uint32_t)usb_utdBuf % 32) == 0);
00105 }
00106 
00107 volatile byte* usb_get_hcca()
00108 {
00109   return usb_hcca;
00110 }
00111 
00112 volatile byte* usb_get_ed()
00113 {
00114   int i;
00115   for(i = 0; i < EDS_COUNT; i++)
00116   {
00117     if( !usb_edBufAlloc[i] )
00118     {
00119       usb_edBufAlloc[i] = 1;
00120       return usb_edBuf + i*ED_SIZE;
00121     }
00122   }
00123   return NULL; //Could not alloc ED
00124 }
00125 
00126 static uint8_t* usb_get_utd(int al)
00127 {
00128     DBG_ASSERT(al == 16 || al == 32); // GTD or ITD
00129     if (al == 16) {
00130         for(int i = 1; i < UTDS_COUNT; i += 2) {
00131             if (usb_utdBufAlloc[i] == 0) {
00132                 uint32_t p = (uint32_t)usb_utdBuf + i * UTD_SIZE;
00133                 if ((p % al) == 0) {
00134                     usb_utdBufAlloc[i] = 1;
00135                     DBG_ASSERT((p % al) == 0);
00136                     return (uint8_t*)p;
00137                 }
00138             }
00139         }
00140     }
00141     for(int i = 0; i < UTDS_COUNT; i++) {
00142         if (usb_utdBufAlloc[i] == 0) {
00143             uint32_t p = (uint32_t)usb_utdBuf + i * UTD_SIZE;
00144             if ((p % al) == 0) {
00145                 usb_utdBufAlloc[i] = 1;
00146                 DBG_ASSERT((p % al) == 0);
00147                 return (uint8_t*)p;
00148             }
00149         }
00150     }
00151     return NULL;
00152 }
00153 
00154 volatile byte* usb_get_td(uint32_t endpoint)
00155 {
00156     DBG_ASSERT(endpoint);
00157     uint8_t* td = usb_get_utd(16);
00158     if (td) {
00159         HCUTD* utd = (HCUTD*)td;
00160         memset(utd, 0x00, sizeof(HCTD));
00161         utd->type = 1;
00162         utd->UsbEndpoint = endpoint;
00163     }
00164     return td;
00165 }
00166 
00167 volatile byte* usb_get_itd(uint32_t endpoint)
00168 {
00169     DBG_ASSERT(endpoint);
00170     uint8_t* itd = usb_get_utd(32);
00171     if (itd) {
00172         HCUTD* utd = (HCUTD*)itd;
00173         memset(utd, 0x00, sizeof(HCITD));
00174         utd->type = 2;
00175         utd->UsbEndpoint = endpoint;
00176     }
00177     return itd;
00178 }
00179 
00180 volatile byte* usb_get_bp(int size)
00181 {
00182   DBG_ASSERT(size >= 128 && size <= BP_SIZE);
00183   if (size > BP_SIZE)
00184       return NULL;
00185       
00186   for(int i = 0; i < BPS_COUNT; i++)
00187   {
00188     if( !usb_bpBufAlloc[i] )
00189     {
00190       usb_bpBufAlloc[i] = 1;
00191       return usb_bpBuf + i*BP_SIZE;
00192     }
00193   }
00194   return NULL; //Could not alloc Buffer Page
00195 }
00196 
00197 int usb_bp_size()
00198 {
00199     return BP_SIZE; 
00200 }
00201 
00202 void usb_free_ed(volatile byte* ed)
00203 {
00204   int i;
00205   i = (ed - usb_edBuf) / ED_SIZE;
00206   usb_edBufAlloc[i] = 0;
00207 }
00208 
00209 static void usb_free_utd(volatile uint8_t* utd)
00210 {
00211   DBG_ASSERT(utd >= usb_utdBuf);
00212   DBG_ASSERT(utd <= (usb_utdBuf+UTD_SIZE*(UTDS_COUNT-1)));
00213   DBG_ASSERT(((uint32_t)utd % 16) == 0);
00214   int i = (utd - usb_utdBuf) / UTD_SIZE;
00215   DBG_ASSERT(usb_utdBufAlloc[i] == 1);
00216   usb_utdBufAlloc[i] = 0;
00217 }
00218 
00219 void usb_free_td(volatile byte* td)
00220 {
00221     usb_free_utd(td);
00222     return;
00223 }
00224 
00225 void usb_free_itd(volatile byte* itd)
00226 {
00227     usb_free_utd(itd);
00228     return;
00229 }
00230 
00231 void usb_free_bp(volatile byte* bp)
00232 {
00233   DBG_ASSERT(bp >= usb_bpBuf);
00234   int i;
00235   i = (bp - usb_bpBuf) / BP_SIZE;
00236   DBG_ASSERT(usb_bpBufAlloc[i] == 1);
00237   usb_bpBufAlloc[i] = 0;
00238 }
00239 
00240 bool usb_is_td(volatile byte* td)
00241 {
00242     DBG_ASSERT(td);
00243     HCUTD* utd = (HCUTD*)td;
00244     DBG_ASSERT(utd->type != 0);
00245     return utd->type == 1;
00246 }
00247 
00248 bool usb_is_itd(volatile byte* itd)
00249 {
00250     DBG_ASSERT(itd);
00251     HCUTD* utd = (HCUTD*)itd;
00252     DBG_ASSERT(utd->type != 0);
00253     return utd->type == 2;
00254 }