in new design (new .net micro) have series of led 7 segment displays controlled via spi bus netduino.
now, have seen 1 doesn't have emulate spi bus stuff because .net microframework has emulated spi bus, fantastic.
since "module" controlled spi abstract via spidevice , spibus, have scoured on internet , have not been able find single example of how roll own custom spidevice (and control it) use in .net mf deviceemulator project.
basically in spidevice have series of controll registers plus data each of leds in desperate need example can lead way right direction. when installed .net mf 4.3 did not install samples.
an option might use aggregation achieve looking for.
for example, create class called sevensegmentdisplay exposes methods/properties interact 7 segment led module , wraps private spi instance. internally methods call private spi instance communicate physical device.
for emulator, here code + config wrote flash memory chip has spi interface. used internal testing while waiting physical device.
using system; using microsoft.spot.emulator; using microsoft.spot.emulator.spi; using system.diagnostics; namespace dotnetwarrior.emulator.hardware { class mx25l3206flashmemory : spidevice { private byte[] _memory; public int memorysize { get; set; } public int sectorsize { get; set; } public int pagesize { get; set; } private status _status; [flags] enum status { wip = 1, wel = 2, bp0 = 4, bp1 = 8, bp2 = 16, e_err = 32, p_err = 64, srwd = 128 } public mx25l3206flashmemory() { } public byte getbyte(int address) { return _memory[address]; } public override void setupcomponent() { base.setupcomponent(); _memory = new byte[memorysize]; } protected override byte[] write(byte[] data) { switch (data[0]) { case 0x03: return read(data); case 0x9f: return readidentification(data); case 0x90: return readmanufacturer(data); case 0x06: return writeenable(data); case 0x04: return writedisable(data); case 0x20: return erase4k(data); case 0x40: return erase8k(data); case 0xd8: return erasesector(data); case 0x60: case 0xc7: return erasedevice(data); case 0x02: return pageprograme(data); case 0x05: return readstatus(data); case 0x01: return writestatus(data); case 0x35: return readconfig(data); } throw new notimplementedexception("unexpected flash command : " + data[0].tostring()); } private int getaddress(byte[] data) { byte[] address = new byte[4]; buffer.blockcopy(data, 1, address, 1, 3); array.reverse(address); return (bitconverter.toint32(address, 0) % memorysize); } private byte[] read(byte[] data) { int address = getaddress(data); buffer.blockcopy(_memory, address, data, 4, data.length - 4); return data; } private byte[] readidentification(byte[] data) { return new byte[]{0x01, 0x02, 0x15, 0x4d}; } private byte[] readmanufacturer(byte[] data) { return new byte[]{0x01, 0x02}; } private byte[] writeenable(byte[] data) { _status |= status.wel; return new byte[]{}; } private byte[] writedisable(byte[] data) { _status &= ~status.wel; return new byte[]{}; } private byte[] readstatus(byte[] data) { return new byte[] { (byte)_status, (byte)_status }; } private byte[] writestatus(byte[] data) { _status = (status)data[1]; return new byte[] { }; } private byte[] erase4k(byte[] data) { if (!_status.hasflag(status.wel) || _status.hasflag(status.wip)) return new byte[] { }; try { _status |= status.wip; } { _status &= ~(status.wel | status.wip); } return new byte[] { }; } private byte[] erase8k(byte[] data) { if (!_status.hasflag(status.wel) || _status.hasflag(status.wip)) return new byte[] { }; _status |= status.wip; try { } { _status &= ~(status.wel | status.wip); } return new byte[] { }; } private byte[] erasesector(byte[] data) { if (!_status.hasflag(status.wel) || _status.hasflag(status.wip)) return new byte[] { }; _status |= status.wip; try { int address = getaddress(data); int sector = address / sectorsize; int sectorstartaddress = sector * sectorsize; (int = 0; < sectorsize; i++) { _memory[sectorstartaddress + i] = 0xff; } } { _status &= ~(status.wel | status.wip); } return new byte[] { }; } private byte[] erasedevice(byte[] data) { if (!_status.hasflag(status.wel) || _status.hasflag(status.wip)) return new byte[] { }; _status |= status.wip; try { (int = 0; < memorysize; i++) { _memory[i] = 0xff; } } { _status &= ~(status.wel | status.wip); } return new byte[] { }; } private byte[] pageprograme(byte[] data) { if (!_status.hasflag(status.wel) || _status.hasflag(status.wip)) return new byte[] { }; _status |= status.wip; try { int address = getaddress(data); int offset = address % pagesize; (int = 0; < data.length - 4; i++) { _memory[address + ((offset + i) % pagesize)] &= (byte)data[i + 4]; } } { _status &= ~(status.wel | status.wip); } return new byte[] { }; } private byte[] readconfig(byte[] data) { return new byte[] { }; } } } the corresponding configuration configure flash memory emulator follows (note used in custom emulator).
<types> <mx25l3206>dotnetwarrior.emulator.hardware.mx25l3206flashmemory, dotnetwarrior.emulator</mx25l3206> <accessindicator>dotnetwarrior.emulator.hardware.accessindicator, dotnetwarrior.emulator</accessindicator> </types> <emulatorcomponents> <mx25l3206 id="myflash"> <memorysize>4194304</memorysize> <sectorsize>65536</sectorsize> <pagesize>256</pagesize> <chipselectpin>10</chipselectpin> <!--spi--> <chipselectactivestate>false</chipselectactivestate> <chipselectsetuptime>1</chipselectsetuptime> <chipselectholdtime>1</chipselectholdtime> <clockratekhz>36000</clockratekhz> <clockidlestate>false</clockidlestate> <clockedge>false</clockedge> <spimodule>spi1</spimodule> <!--hardware provider--> <mask>1</mask> <mosi>2</mosi> <miso>3</miso> </mx25l3206>
Comments
Post a Comment