Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question in regards of how does portConfig works #109

Closed
Sharvin26 opened this issue Sep 30, 2021 · 6 comments
Closed

Question in regards of how does portConfig works #109

Sharvin26 opened this issue Sep 30, 2021 · 6 comments

Comments

@Sharvin26
Copy link

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
Copy link
Contributor

urish commented Sep 30, 2021

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:

  • pins 0 to 7 are on Port D (0..7)
  • pins 8 to 13 are on Port B (0..5)
  • pins A0..A5 are on Port C (0..5)

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
Copy link
Author

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
Copy link
Contributor

urish commented Oct 10, 2021 via email

@Sharvin26
Copy link
Author

Sure thanks

@urish
Copy link
Contributor

urish commented Oct 11, 2021

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants