wokwi / avr8js

Arduino (8-bit AVR) simulator, written in JavaScript and runs in the browser / Node.js
https://blog.wokwi.com/avr8js-simulate-arduino-in-javascript/
MIT License
461 stars 73 forks source link

Question in regards of how does portConfig works #109

Closed Sharvin26 closed 2 years ago

Sharvin26 commented 2 years ago

Hello,

First of all, thank you for developing such a great library.

I completed this video tutorial that I found in the readme of this project.

Now when I tried changing the port from portDConfig to portBConfig or something else, the LED stopped working.

Working:

const port = new AVRIOPort(cpu, portDConfig);

Not Working:

const port = new AVRIOPort(cpu, portBConfig);

I also tried changing the pin both in code and this code snippet below:

Working:

port.addListener(() => {
      const turnOn = port.pinState(7) === PinState.High;
      setLedState(turnOn);
});

Not Working:

port.addListener(() => {
      const turnOn = port.pinState(8) === PinState.High;
      setLedState(turnOn);
});

What I observed was it worked for 0 to 7 but stopped working for numbers bigger than 7.

I made sure the number here and in arduino code are same.

I was going through the issues of this project to understand what I was doing wrong and I found a discussion where the author of this repo has commented.

But I still didn't understood how this is working.

I am more confused when various arduino boards are brought in.

For example:

Arduino Mega has 0 to 53 Digital GPIO Pins. So now to determine port for each pin number I went to the arduino code

https://github.com/arduino/ArduinoCore-avr/blob/60f0d0b125e06dbf57b800192c80e5f60d681438/variants/mega/pins_arduino.h#L162

Thanks to above mentioned github discussion I found this.

Now I am wondering why other portConfig are not working except portDConfig and why only range 0 to 7 is working.

This is the sandbox link attached here.

Any help in this matter would be appreciated as I am much of beginner in electronics and trying to understand how everything is combining together for educational purpose.

urish commented 2 years ago

Hi Sharvin,

Nice to meet you!

In general, each port has up to 8 pins (most of them have 8). For Arduino Uno, the mapping is as follows:

So if you wanted to listen for Arduino's pin 8, that'd correspond to pin number 0 (the first pin) on Port B, and the code would look something like:

const portB = new AVRIOPort(cpu, portBConfig);
portB.addListener(() => {
      const turnOn = port.pinState(0) === PinState.High;
      setLedState(turnOn);
});

For Arduino mega, it's a similar concept, and I see that you have already found the mapping. Here's an example how to read these lines:

    PG  , // PG 2 ** 39 ** D39  

The first part of the comment is the port name (G) and pin index in the port (2), and the second part is the Mega pin number.

So what this line says: Mega pin 39 is pin 2 on Port G. So you would listen it using something like:

const portG = new AVRIOPort(cpu, portGConfig);
portG.addListener(() => {
      const turnOn = port.pinState(2) === PinState.High;
      setLedState(turnOn);
});

I hope this is helpful!

Sharvin26 commented 2 years ago

Hey Urish,

thank you for the response and help.

I went through your suggestion and update the code as follows:

const arduinoCode = `
void setup() {
  // put your setup code here, to run once:
  pinMode(39, OUTPUT); 
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(39, HIGH);
  delay(1000);
  digitalWrite(39, LOW);
  delay(1000);
}`;

I also updated the port config as follows:

const port = new AVRIOPort(cpu, portGConfig);
port.addListener(() => {
      const turnOn = port.pinState(2) === PinState.High;
      setLedState(turnOn);
      console.log("LED", turnOn);
});

Now when I run the code led doesn't turn on so I console logged and I got the following observation:

value of port.pinState(2) is 2 and value of PinState.High is 1.

Also it doesn't attach the listener thus the function inside listerner doesn't occur.

I don't know what am I doing wrong or missing here. Any help is appreciate.

Following is the link for sanbox code updated.

urish commented 2 years ago

Hi Sharvin,

I'm on vacation today, I'll have a look when i get back and update here

On Sun, Oct 10, 2021, 12:00 PM Sharvin Shah @.***> wrote:

Hey Urish,

thank you for the response and help.

I went through your suggestion and update the code as follows:

const arduinoCode = ` void setup() { // put your setup code here, to run once: pinMode(39, OUTPUT); }

void loop() { // put your main code here, to run repeatedly: digitalWrite(39, HIGH); delay(1000); digitalWrite(39, LOW); delay(1000); }`;

I also updated the port config as follows:

const port = new AVRIOPort(cpu, portGConfig); port.addListener(() => { const turnOn = port.pinState(2) === PinState.High; setLedState(turnOn); console.log("LED", turnOn); });

Now when I run the code led doesn't turn on so I console logged and I got the following observation:

value of port.pinState(2) is 2 and value of PinState.High is 1.

Also it doesn't attach the listener thus the function inside listerner doesn't occur.

I don't know what am I doing wrong or missing here. Any help is appreciate.

Following is the link https://codesandbox.io/s/nice-forest-8i5h7?file=/src/App.js for sanbox code updated.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/wokwi/avr8js/issues/109#issuecomment-939434614, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGZ3HXKRNVC6OCK72CI3YLUGFIZ5ANCNFSM5FCNJP5Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Sharvin26 commented 2 years ago

Sure thanks

urish commented 2 years ago

So for the mega:

  1. You have to compile your code for the Arduino Mega. By default, the build server compiles for Arduino Uno. To compile for mega, add "board": "mega" to the JSON object you're sending to the build server (the one with the "sketch") property
  2. You also need to set a larger RAM size when you create the CPU: const cpu = new CPU(new Uint16Array(progData.buffer), 0x2200);
  3. There are some required changes to the timer configuration: const timer = new AVRTimer(cpu, {...timer0Config, compAInterrupt: 0x02a, compBInterrupt: 0x02c, ovfInterrupt: 0x02e,});

You can find a complete example (somewhat complex, but functional) here: https://stackblitz.com/edit/avr8js-mega-ws2812?file=execute.ts

Sharvin26 commented 2 years ago

Hey @urish thank you for solving the doubt. Much appreciated the help.