-
Notifications
You must be signed in to change notification settings - Fork 898
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
Support Arduino MEGA 2560 #724
Comments
Hi. I found ATMega2560's declaration in tinygo sources :
So I tried to create a new local json target "arduino-mega2560.json"
... and run a simple build:
Some more debugs :
Could you please tell us more about the error ? Thanks Olivier Fauchon |
I debugged it a bit and found that you also need to pass "ldflags": [
"-Wl,--defsym=_bootloader_size=512",
"-Wl,--defsym=_stack_size=512",
"-mmcu=atmega2560"
], It got me a bit further (I think):
|
Hello, I've been following this thread for a while and as I actually have the Mega 2560 board, I wanted to try to make tinygo work on it :) After few tries, I actually managed to compile and flash my Mega 2560 with the I'm fairly new to the Arduino world so it's more likely that what I did is not correct and I'm sure it can be improved a lot !. Also, I did not change much things and I have to say that you guys did all the work, here is what's different :
My {
"inherits":[
"avr"
],
"llvm-target":"avr-atmel-none",
"cpu":"atmega2560",
"build-tags":[
"arduino",
"atmega2560",
"atmega",
"avr6"
],
"cflags":[
"-mmcu=avr6"
],
"ldflags":[
"-Wl,--defsym=_bootloader_size=512",
"-Wl,--defsym=_stack_size=512",
"-mmcu=avr6"
],
"linkerscript":"src/device/avr/atmega2560.ld",
"extra-files":[
"targets/avr.S",
"src/device/avr/atmega2560.s"
],
"flash-command":"avrdude -c wiring -b 115200 -p atmega2560 -P {port} -U flash:w:{hex} -v -D"
} Build works great :
And flash as well :
I will make some tests to see how we can do more with this. Next step is to be able to control a LED 😃 |
That is some amazing progress! Looks like you're getting very close. To define your own pin mapping, you can change the |
This interests me as well. Any more progress? Is your work up in a branch somewhere? |
I made more tests but except the serial communication, I could not find any way to do anything else. I did what @aykevl advised me and created a dedicated board file like this one (also updated build tags so that it uses it instead of the arduino one) : // +build atmega2560
package machine
// Return the current CPU frequency in hertz.
func CPUFrequency() uint32 {
return 16000000
}
// LED on the Arduino
const LED Pin = 13 The Am I supposed to do anything else for pin mapping ? |
I can't get serial communication to work either. I did create a pin mapping file the same as you and was attempting to populate it. I do run into an issue with an invalid reference:
I was fixing that manually by changing it to I am positive this is the wrong fix for this issue. I believe we may need to create a |
FYI: I pushed my work up to my fork here: https://github.com/tmclane/tinygo/tree/feature/mega2560 I ended up creating alternate files and putting build flags in them such that the compile error doesn't happen any more with Programs will now compile and flash but don't seem to execute on my particular mega2560. Perhaps it is a clone issue? |
The best way to fix that right now is to make two new files // +build atmega328p
package machine
const uartInterrupt = avr.IRQ_USART_RX // +build atmega2560
package machine
const uartInterrupt = avr.IRQ_USART0_RX and then you can use that constant in machine_atmega.go. |
Thanks.. I have updated my branch with your suggestion. I've never done any low-level hardware coding like this. How do I figure out what is missing / wrong? What's the best way to get started? |
I can't give a single answer to that. But my usual approach to this is to reduce as far as possible, and start with a known-good state (just like any other bug, really). // untested
avr.DDRB.Set(1 << uint8(5))
avr.PORTB.Set(1 << uint8(5)) In this case, it looks like @rbretecher actually got serial working, so if you can get that to work as well you could start from there. |
I don't get anything from the How do you figure the offsets for those |
Ah looks like the default Baud rate is actually |
You mean But checking the internet (https://www.exp-tech.de/blog/arduino-mega-2560-pinbelegung) it looks like my guess was wrong. The built-in LED is actually on PB7, which means the 7th bit on port B: // untested
avr.DDRB.SetBits(1 << uint8(7))
avr.PORTB.SetBits(1 << uint8(7)) Actually, you probably should turn the output low to turn the LED on: // untested
avr.DDRB.SetBits(1 << uint8(7))
avr.PORTB.ClearBits(1 << uint8(7)) |
Ah ok so that's how those I knew there was something basic I was missing. |
Thank you! |
Looks like the
I see where |
It's the combination of all the |
Ok. Actually I think my board might be restarting after it does a I put two in a row in the |
@rbretecher Does your board print more than one |
Sorry for the late reply. Indeed, I have the same problem, only the first |
It's hard to say what is going on without using a debugger. And besides that good debuggers are hard to get for AVR, the LLVM AVR backend doesn't support it either due to a bug (fixed in master and part of LLVM 11 to be released somewhere around October). Once this fix lands, it may be possible to debug it in My usual strategy is trying to reduce it to the bare minimum. |
I have a local branch that adds support for the ATmega2560 chip, based on #946. You can in fact look at that branch, the changes for the ATmega2560 are pretty trivial (replace the MCU with |
I'll give it a look and a try. The link to your branch appears to just link to the same PR :) |
I think I understand the changes you mean. I'm going to try to do them myself and see if I am correct when you update your link. |
Lo and behold it works and doesn't reboot now after doing a Hallelujah! Thanks for the help! Maybe I can figure out how to make it blink now. |
My branch: https://github.com/tmclane/tinygo/tree/feature/mega2560 was updated. Flashing it with command:
Made this change to
Results in:
|
And blinking works if I use the PORTB directly as suggested.
|
Cool! |
The offset defined in the machine_atmega.go is wrong:
Should this be placed into another file? Or should I do something similar to the Oops I left in my offset definition I used to test it with the blinky1 example. |
How would you recommend this be approached? |
Do you just adjust the PIN constants such that they subtract off to match the shift amount? (seems like a bad way to do it) |
I believe I need to make a mega2560 specific file to define the Pin functions:
This enables me to get the blinky1 program to run without modifications. I'll open a PR with what I have and you guys can help direct my efforts there. |
Alright. I finally understand how all the parts work. |
Yes this seems like the most sensible approach to me. |
This has now been released, so now closing. Thanks! |
Please add support for the Ardunio MEGA 2560 boards, as this board provides a larger number of GPIO's than most of the other boards (54 off).
https://www.arduino.cc/en/Main/arduinoBoard_Mega2560?setlang=en
The text was updated successfully, but these errors were encountered: