Emulation of LocalFileSystem with virtual COM.
Dependents: KL46Z-lpc81isp lpcterm2
#include "USBLocalFileSystem.h" int main() { USBLocalFileSystem* usb_local = new USBLocalFileSystem(); // RamDisk(64KB) while(1) { usb_local->lock(true); usb_local->remount(); char filename[32]; if (usb_local->find(filename, sizeof(filename), "*.TXT")) { FILE* fp = fopen(filename, "r"); if (fp) { int c; while((c = fgetc(fp)) != EOF) { usb_local->putc(c); } fclose(fp); } } usb_local->lock(false); wait_ms(1000*5); } }
Sample application:
Import programKL46Z-lpc81isp
ISP example program.
Import programlpcterm2
semihost server example program
SWSPI/SWSPI.cpp@6:528036abfb02, 2014-06-21 (annotated)
- Committer:
- va009039
- Date:
- Sat Jun 21 22:39:59 2014 +0000
- Revision:
- 6:528036abfb02
- Parent:
- 0:39eb4d5b97df
add LPC11U68
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:39eb4d5b97df | 1 | /* SWSPI, Software SPI library |
va009039 | 0:39eb4d5b97df | 2 | * Copyright (c) 2012-2014, David R. Van Wagner, http://techwithdave.blogspot.com |
va009039 | 0:39eb4d5b97df | 3 | * |
va009039 | 0:39eb4d5b97df | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
va009039 | 0:39eb4d5b97df | 5 | * of this software and associated documentation files (the "Software"), to deal |
va009039 | 0:39eb4d5b97df | 6 | * in the Software without restriction, including without limitation the rights |
va009039 | 0:39eb4d5b97df | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
va009039 | 0:39eb4d5b97df | 8 | * copies of the Software, and to permit persons to whom the Software is |
va009039 | 0:39eb4d5b97df | 9 | * furnished to do so, subject to the following conditions: |
va009039 | 0:39eb4d5b97df | 10 | * |
va009039 | 0:39eb4d5b97df | 11 | * The above copyright notice and this permission notice shall be included in |
va009039 | 0:39eb4d5b97df | 12 | * all copies or substantial portions of the Software. |
va009039 | 0:39eb4d5b97df | 13 | * |
va009039 | 0:39eb4d5b97df | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
va009039 | 0:39eb4d5b97df | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
va009039 | 0:39eb4d5b97df | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
va009039 | 0:39eb4d5b97df | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
va009039 | 0:39eb4d5b97df | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
va009039 | 0:39eb4d5b97df | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
va009039 | 0:39eb4d5b97df | 20 | * THE SOFTWARE. |
va009039 | 0:39eb4d5b97df | 21 | */ |
va009039 | 0:39eb4d5b97df | 22 | |
va009039 | 0:39eb4d5b97df | 23 | #include <mbed.h> |
va009039 | 0:39eb4d5b97df | 24 | #include "SWSPI.h" |
va009039 | 0:39eb4d5b97df | 25 | |
va009039 | 0:39eb4d5b97df | 26 | SWSPI::SWSPI(PinName mosi_pin, PinName miso_pin, PinName sclk_pin):_fast(false) |
va009039 | 0:39eb4d5b97df | 27 | { |
va009039 | 0:39eb4d5b97df | 28 | mosi = new DigitalOut(mosi_pin); |
va009039 | 0:39eb4d5b97df | 29 | miso = new DigitalIn(miso_pin); |
va009039 | 0:39eb4d5b97df | 30 | sclk = new DigitalOut(sclk_pin); |
va009039 | 0:39eb4d5b97df | 31 | format(8); |
va009039 | 0:39eb4d5b97df | 32 | frequency(); |
va009039 | 0:39eb4d5b97df | 33 | } |
va009039 | 0:39eb4d5b97df | 34 | |
va009039 | 0:39eb4d5b97df | 35 | SWSPI::~SWSPI() |
va009039 | 0:39eb4d5b97df | 36 | { |
va009039 | 0:39eb4d5b97df | 37 | delete mosi; |
va009039 | 0:39eb4d5b97df | 38 | delete miso; |
va009039 | 0:39eb4d5b97df | 39 | delete sclk; |
va009039 | 0:39eb4d5b97df | 40 | } |
va009039 | 0:39eb4d5b97df | 41 | |
va009039 | 0:39eb4d5b97df | 42 | void SWSPI::format(int bits, int mode) |
va009039 | 0:39eb4d5b97df | 43 | { |
va009039 | 0:39eb4d5b97df | 44 | this->bits = bits; |
va009039 | 0:39eb4d5b97df | 45 | this->mode = mode; |
va009039 | 0:39eb4d5b97df | 46 | polarity = (mode >> 1) & 1; |
va009039 | 0:39eb4d5b97df | 47 | phase = mode & 1; |
va009039 | 0:39eb4d5b97df | 48 | sclk->write(polarity); |
va009039 | 0:39eb4d5b97df | 49 | } |
va009039 | 0:39eb4d5b97df | 50 | |
va009039 | 0:39eb4d5b97df | 51 | void SWSPI::frequency(int hz) |
va009039 | 0:39eb4d5b97df | 52 | { |
va009039 | 0:39eb4d5b97df | 53 | this->freq = hz; |
va009039 | 0:39eb4d5b97df | 54 | _fast = (hz >= 1000000) ? true : false; |
va009039 | 0:39eb4d5b97df | 55 | } |
va009039 | 0:39eb4d5b97df | 56 | |
va009039 | 0:39eb4d5b97df | 57 | #pragma Otime |
va009039 | 0:39eb4d5b97df | 58 | |
va009039 | 0:39eb4d5b97df | 59 | int SWSPI::write(int value) |
va009039 | 0:39eb4d5b97df | 60 | { |
va009039 | 0:39eb4d5b97df | 61 | if (_fast) { |
va009039 | 0:39eb4d5b97df | 62 | return fast_write(value); |
va009039 | 0:39eb4d5b97df | 63 | } |
va009039 | 0:39eb4d5b97df | 64 | int read = 0; |
va009039 | 0:39eb4d5b97df | 65 | for (int bit = bits-1; bit >= 0; --bit) |
va009039 | 0:39eb4d5b97df | 66 | { |
va009039 | 0:39eb4d5b97df | 67 | mosi->write(((value >> bit) & 0x01) != 0); |
va009039 | 0:39eb4d5b97df | 68 | |
va009039 | 0:39eb4d5b97df | 69 | if (phase == 0) |
va009039 | 0:39eb4d5b97df | 70 | { |
va009039 | 0:39eb4d5b97df | 71 | if (miso->read()) |
va009039 | 0:39eb4d5b97df | 72 | read |= (1 << bit); |
va009039 | 0:39eb4d5b97df | 73 | } |
va009039 | 0:39eb4d5b97df | 74 | |
va009039 | 0:39eb4d5b97df | 75 | sclk->write(!polarity); |
va009039 | 0:39eb4d5b97df | 76 | |
va009039 | 0:39eb4d5b97df | 77 | wait_us(1000000/freq/2); |
va009039 | 0:39eb4d5b97df | 78 | |
va009039 | 0:39eb4d5b97df | 79 | if (phase == 1) |
va009039 | 0:39eb4d5b97df | 80 | { |
va009039 | 0:39eb4d5b97df | 81 | if (miso->read()) |
va009039 | 0:39eb4d5b97df | 82 | read |= (1 << bit); |
va009039 | 0:39eb4d5b97df | 83 | } |
va009039 | 0:39eb4d5b97df | 84 | |
va009039 | 0:39eb4d5b97df | 85 | sclk->write(polarity); |
va009039 | 0:39eb4d5b97df | 86 | |
va009039 | 0:39eb4d5b97df | 87 | wait_us(1000000/freq/2); |
va009039 | 0:39eb4d5b97df | 88 | } |
va009039 | 0:39eb4d5b97df | 89 | |
va009039 | 0:39eb4d5b97df | 90 | return read; |
va009039 | 0:39eb4d5b97df | 91 | } |
va009039 | 0:39eb4d5b97df | 92 | |
va009039 | 0:39eb4d5b97df | 93 | uint8_t SWSPI::fast_write(uint8_t value) |
va009039 | 0:39eb4d5b97df | 94 | { |
va009039 | 0:39eb4d5b97df | 95 | uint8_t data = 0; |
va009039 | 0:39eb4d5b97df | 96 | for(uint8_t mask = 0x80; mask; mask >>= 1) { |
va009039 | 0:39eb4d5b97df | 97 | mosi->write((value & mask) ? 1 : 0); |
va009039 | 0:39eb4d5b97df | 98 | if (miso->read()) { |
va009039 | 0:39eb4d5b97df | 99 | data |= mask; |
va009039 | 0:39eb4d5b97df | 100 | } |
va009039 | 0:39eb4d5b97df | 101 | sclk->write(1); |
va009039 | 0:39eb4d5b97df | 102 | sclk->write(0); |
va009039 | 0:39eb4d5b97df | 103 | } |
va009039 | 0:39eb4d5b97df | 104 | return data; |
va009039 | 0:39eb4d5b97df | 105 | } |
va009039 | 0:39eb4d5b97df | 106 |