This short program illustrates how to use the DS130x_I2C library. My objective is to share the same RTC with Microchip 18F MCU.

Dependencies:   mbed DebugLibrary

Committer:
Yann
Date:
Wed Feb 09 13:57:49 2011 +0000
Revision:
0:f30e2135b0db
V0.0.0.1

Who changed what in which revision?

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