-
Notifications
You must be signed in to change notification settings - Fork 955
avr: added EEPROM support for ATmega boards #3306
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
base: dev
Are you sure you want to change the base?
Conversation
edit: I have made changes to this commit so this comment does not apply anymore |
2958d56
to
7b06385
Compare
7b06385
to
cd7f6be
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a number of nits, but overall the code looks quite reasonable.
Can you also make a PR to https://github.com/tinygo-org/tinygo-site with API documentation on the EEPROM? At least the machine package, but if you could also write something in here (https://tinygo.org/docs/concepts/peripherals/) that would be great.
src/machine/machine_atmega.go
Outdated
var EEPROM0 = EEPROM{} | ||
|
||
// setAddress sets the address for a given read or write into the MCUs EEARH/L register. | ||
func (e EEPROM) setAddress(addr int64) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an unexported function, so it's best to use the most efficient data type available. In this case, int16
(or even uint16
).
func (e EEPROM) setAddress(addr int64) error { | |
func (e EEPROM) setAddress(addr int16) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I make this change, within this function there is a comparison between an int64
and int16
. Should I be casting the int16
into an int64
to perform this check, or should I change the result of the Size()
function to return int16
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case you know it will never be bigger than an int16
so casting to the most efficient type is best (for AVR that is int16
).
@@ -11,6 +11,11 @@ import ( | |||
|
|||
const irq_USART0_RX = avr.IRQ_USART0_RX | |||
|
|||
// Size returns the size of the EEPROM for this machine. | |||
func (e EEPROM) Size() int64 { | |||
return 4096 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this constant is part of the .atdf files, but that can be fixed in a future patch. (It needs a change to tools/gen-device-avr).
Absolutely. I'll make a write up once I fix the other issues you mentioned! |
cd7f6be
to
96dcf17
Compare
96dcf17
to
f8306ea
Compare
// setAddress sets the address for a given read or write into the MCUs EEARH/L register. | ||
func (e EEPROM) setAddress(addr int16) error { | ||
if addr < 0 || addr > int16(e.Size()) { | ||
return errors.New("address provided is out of bounds") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please define a global constant with this error message to return. For example:
var errEEPROMAddress = errors.New("eeprom: address out of bounds")
Otherwise this forces a heap allocation when the error is triggered, which is not good.
|
||
// setAddress sets the address for a given read or write into the MCUs EEARH/L register. | ||
func (e EEPROM) setAddress(addr int16) error { | ||
if addr < 0 || addr > int16(e.Size()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is an off-by-one.
if addr < 0 || addr > int16(e.Size()) { | |
if addr < 0 || addr >= int16(e.Size()) { |
src/machine/machine_atmega.go
Outdated
var EEPROM0 = EEPROM{} | ||
|
||
// setAddress sets the address for a given read or write into the MCUs EEARH/L register. | ||
func (e EEPROM) setAddress(addr int64) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case you know it will never be bigger than an int16
so casting to the most efficient type is best (for AVR that is int16
).
Sorry for the extreme delay on these changes, will go ahead and add them this week. |
@SoleimanyBen I was looking at this PR today, and had a couple ideas. Seems to me that it would be great to implement the Imaginary code: func main() {
store := machine.EEPROM.Open(address)
defer store.Close()
var data [12]byte
_, err := store.Read(data)
if err != nil {
panic("nope")
}
println(string(data))
store.Write([]byte("Hola, mundo!))
} |
@deadprogram That's a great idea! I'll write up those changes when I make the other adjustments later this week. :) |
Why though? It seems to me that
If an application really wanted to layer a |
@SoleimanyBen I think the interface to create here is actually the This is what I have been doing in my Flash PR #3472 |
@deadprogram EEPROM is not quite the same as flash though. Unlike flash, you can erase and rewrite individual bytes easily so "page size" (a big part of BlockDevice) doesn't make much sense. In my opinion, this PR is almost ready, it just needs a few small changes to mergeable. |
This adds general EEPROM support for AVR devices through the
EEPROM
struct.First MR here so I was hoping to get feedback on what I could improve and what I did wrong here.
It looks like it goes against convention to not have a
Configure()
function for hardware structures, but I did not see how It could make sense in this instance.Also, should there be a specific
EEPROM0
constant for each machine file that is then populated within the specific board file?Thanks again, glad to be able to contribute to such a great project! :)
p.s. im an idiot, sorry about the other PR with the wrong branch selected 😅