diff --git a/readme.md b/readme.md index d72c7a9..321c188 100644 --- a/readme.md +++ b/readme.md @@ -1,7 +1,6 @@ # MPLabX Simulator UART I/O Plug-in This is a plug-in for MPLabX v4.20, which support basic UART reading and injection. -The plug-in reads and writes to pre-configured request and response paths that are -external to the simulator, and allow interaction with the developer's platform of choice +The plug-in reads and writes to socket 5556, and allow interaction with the developer's platform of choice for simulating peripherals (Modem, GPS, etc.) The possibilities are endless! The majority of the code is under src/Uart.java, but if you wish to implement your own @@ -20,6 +19,7 @@ SFRObservers, then you'll want to look under src/UartObserver.java 3. Copy `config.yml` from the `src` directory of `uart_mplabx_plugin` to your MPLab X bin folder. * MacOS: `/Applications/microchip/mplabx/vX.XX/mplab_platform/bin` * Windows: `C:\Program Files (x86)\Microchip\MPLABX\vX.XX\mplab_ide\bin` + * Ubuntu 16.04: `/opt/microchip/mplabx/vX.XX/mplab_platform/bin` 4. Open Netbeans 5. `File -> Open Project` Open the `uart_mplabx_plugin` project folder 6. Open `uart_mplabx_plugin -> Source Packages -> zipcar.emulator.uart -> Uart.java` from the projects tab diff --git a/src/zipcar/emulator/uart/Uart.java b/src/zipcar/emulator/uart/Uart.java index bfbcfe6..7da345a 100644 --- a/src/zipcar/emulator/uart/Uart.java +++ b/src/zipcar/emulator/uart/Uart.java @@ -7,12 +7,16 @@ import com.microchip.mplab.mdbcore.simulator.MessageHandler; import com.microchip.mplab.mdbcore.simulator.SimulatorDataStore.SimulatorDataStore; import com.microchip.mplab.mdbcore.simulator.PeripheralSet; +import java.io.BufferedInputStream; +import java.io.BufferedWriter; import java.io.File; import java.util.LinkedList; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.OutputStreamWriter; +import java.net.Socket; import org.openide.util.lookup.ServiceProvider; import java.util.Map; import org.yaml.snakeyaml.Yaml; @@ -39,9 +43,9 @@ public class Uart implements Peripheral { int cycleCount = 0; SCL scl; boolean notInitialized = true; - LinkedList chars = new LinkedList(); - FileOutputStream request; - FileInputStream response; + LinkedList chars = new LinkedList(); + BufferedWriter request; + BufferedInputStream response; Yaml yaml = new Yaml(); @Override @@ -80,14 +84,23 @@ public boolean init(SimulatorDataStore DS) { } // Setup pipes - try { + /* try { request = new FileOutputStream(REQUEST_FILE); response = new FileInputStream(RESPONSE_FILE); } catch (FileNotFoundException e) { messageHandler.outputMessage("Exception in init: " + e); return false; - } + } */ + + try { + Socket reqSocket = new Socket("localhost", 5556); + request = new BufferedWriter(new OutputStreamWriter(reqSocket.getOutputStream())); + response = new BufferedInputStream(reqSocket.getInputStream()); + } catch (Exception e) { + messageHandler.outputError(e); + } + // Add observers UartObserver obs = new UartObserver(); sfrTX.addObserver(obs); @@ -132,11 +145,11 @@ public void reset() { @Override public void update() { - if (cycleCount % (4000) == 0) { + if (cycleCount % (267) == 0) { try { - if (response.available() != 0) { // If there are unread bytes, read them and add the chars - messageHandler.outputMessage("Available bytes: " + response.available()); - chars.add((char) response.read()); + if (response.available() > 0) { + messageHandler.outputMessage(response.available() + ""); + chars.add((byte) response.read()); } } catch (IOException e) { messageHandler.outputMessage("Exception reading character from res " + e); @@ -144,6 +157,7 @@ public void update() { } if (!chars.isEmpty()) { // Inject anything in chars if (sfrSTA.getFieldValue("UTXEN") == 1) { // If STA is ready to receive !! IMPORTANT + messageHandler.outputMessage("Injecting: " + chars.peek()); // messageHandler.outputMessage(chars.peek() + ""); // Returns the next char which will be injected sfrRX.privilegedWrite(chars.pop()); // Inject the next char sfrInterrupt.privilegedSetFieldValue("U2RXIF", 1); // Trigger the interrupt @@ -156,14 +170,16 @@ public void update() { // Debugging function for manually adding a string to chars public void setString(String str) { for (int i = 0; i < str.length(); i++) { - chars.add(str.charAt(i)); + chars.add((byte) str.charAt(i)); } } // Try to write bytes to the request file public void output() { try { + messageHandler.outputMessage("writing to lmpp"); request.write((byte) sfrTX.read()); + request.flush(); } catch (Exception e) { messageHandler.outputMessage("failed to write request byte " + e); }