Skip to content

Commit 4ec3e72

Browse files
committed
reference/machine: added eeprom reference and machine docs
1 parent ab71d28 commit 4ec3e72

File tree

5 files changed

+236
-1
lines changed

5 files changed

+236
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: "EEPROM"
3+
weight: 3
4+
description: |
5+
How to use the internal EEPROM.
6+
---
7+
8+
For API documentation, see the [machine API](../../../reference/machine#eeprom).
9+
10+
The EEPROM is a form of non-volatile memory that is uusually integrated along side an MCU. EEPROMs allows you to store a relatively small amount of data by allowing bytes to be erased and reprogrammed. For the most part, EEPROMs are not integrated directly onto a board, but in some cases they are provided as a standard peripheral.
11+
12+
Although it may seem useful to have some type of data storage for your applications, the EEPROM is often limited in the amount of write cycles that it contains. This means that after a certain amount of writes, the EEPROM will not writeable, but you may still read data from it.
13+
14+
## Controlling the EEPROM
15+
16+
Control of the EEPROM has been simplified through the `EEPROM` variable that can be accessed through the `machine` package.
17+
18+
Unlike most peripherals within TinyGo, you do not need to configure the EEPROM to use it.
19+
20+
Example usage of the EEPROM can be seen below.
21+
22+
```go
23+
package main
24+
25+
import (
26+
"machine"
27+
"time"
28+
)
29+
30+
// Address of the data to be stored.
31+
// Addresses can be either in hex or dec.
32+
const DataAddr = 0x10
33+
34+
35+
func main() {
36+
eeprom := machine.EEPROM0 // the onboard EEPROM
37+
38+
_, err := eeprom.WriteAt([]byte("Hello, World!"), DataAddr)
39+
if err != nil{
40+
panic(err)
41+
}
42+
43+
// Create a buffer to read the data into
44+
// We need to define a buffer with the correct size of the bytes we want to read.
45+
buf := make([]byte, len("Hello, World!"))
46+
_, err := eeprom.ReadAt(buf, DataAddr)
47+
if err != nil {
48+
panic(err)
49+
}
50+
51+
fmt.Println(buf) // Hello, World!
52+
}
53+
```
54+
55+
This example will store a value, `Hello, World!`, into the address `0x10`.

content/docs/reference/machine.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,45 @@ func (uart *UART) WriteByte(c byte) error
222222
Write a single byte to the UART output.
223223

224224

225+
## EEPROM
226+
227+
```go
228+
type EEPROM struct {
229+
}
230+
231+
var EEPROM0 = EEPROM{}
232+
```
233+
234+
The `EEPROM` struct contains the collection of methods that can be used to interact with the EEPROM. The `EEPROM` struct can be accessed through the preinitialized variable: `EEPROM0`. Depending on the type of board, you may or may not have an EEPROM built-in.
235+
236+
237+
```go
238+
func (e EEPROM) WriteAt(data []byte, off int64) (int, error)
239+
```
240+
241+
WriteAt writes len(data) bytes in the EEPROMs at the provided offset. This method implements the `io.WriterAt` interface.
242+
243+
```go
244+
func (e EEPROM) WriteByteAt(value byte, addr int64) error
245+
```
246+
247+
WriteByteAt performs the logic to writes a byte into the EEPROM at the given address.
248+
249+
```go
250+
func (e EEPROM) ReadAt(buf []byte, off int64) (int, error)
251+
```
252+
253+
ReadAt reads exactly len(buf) into buf at the offset. It will return the amount of bytes copied or an error if one exists.
254+
The buffer cannot be empty, and an an error is thrown if fewer bytes are read than the size of the buffer.
255+
This method implements the `io.ReaderAt` interface.
256+
257+
```go
258+
func (e EEPROM) ReadByteAt(addr int64) (byte, error)
259+
```
260+
261+
ReadByteAt reads and returns the byte at the specified address. An error is returned if there is a failure to read.
262+
263+
225264
## Other
226265

227266
```go

content/docs/reference/microcontrollers/machine/arduino-mega1280.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ var (
282282
)
283283
```
284284

285+
```go
286+
var EEPROM0 = EEPROM{}
287+
```
288+
289+
EEPROM0 is the internal EEPROM on most AVRs.
285290

286291

287292
```go
@@ -371,7 +376,6 @@ var (
371376

372377

373378

374-
375379
### func CPUFrequency
376380

377381
```go
@@ -457,6 +461,47 @@ value of each parameter will use the peripheral's default settings.
457461

458462

459463

464+
## type EEPROM
465+
466+
```go
467+
type EEPROM struct {
468+
}
469+
```
470+
471+
### func (e EEPROM) WriteAt
472+
```go
473+
func (e EEPROM) WriteAt(data []byte, off int64) (int, error)
474+
```
475+
476+
WriteAt writes len(data) bytes in the EEPROMs at the provided offset.
477+
478+
479+
### func (e EEPROM) WriteByteAt
480+
```go
481+
func (e EEPROM) WriteByteAt(value byte, addr int64) error
482+
```
483+
484+
WriteByteAt performs the logic to writes a byte into the EEPROM at the given address.
485+
486+
487+
### func (e EEPROM) ReadAt
488+
```go
489+
func (e EEPROM) ReadAt(buf []byte, off int64) (int, error)
490+
```
491+
492+
ReadAt reads exactly len(buf) into buf at the offset. It will return the amount of bytes copied or an error if one exists.
493+
The buffer cannot be empty, and an an error is thrown if fewer bytes are read than the size of the buffer.
494+
495+
496+
### func (e EEPROM) ReadByteAt
497+
```go
498+
func (e EEPROM) ReadByteAt(addr int64) (byte, error)
499+
```
500+
501+
ReadByteAt reads and returns the byte at the specified address. An error is returned if there is a failure to read.
502+
503+
504+
460505

461506
## type I2C
462507

content/docs/reference/microcontrollers/machine/arduino-mega2560.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,13 @@ var (
341341
```
342342

343343

344+
```go
345+
var EEPROM0 = EEPROM{}
346+
```
347+
348+
EEPROM0 is the internal EEPROM on most AVRs.
349+
350+
344351

345352
```go
346353
var I2C0 *I2C = nil
@@ -502,6 +509,47 @@ value of each parameter will use the peripheral's default settings.
502509

503510

504511

512+
## type EEPROM
513+
514+
```go
515+
type EEPROM struct {
516+
}
517+
```
518+
519+
### func (e EEPROM) WriteAt
520+
```go
521+
func (e EEPROM) WriteAt(data []byte, off int64) (int, error)
522+
```
523+
524+
WriteAt writes len(data) bytes in the EEPROMs at the provided offset.
525+
526+
527+
### func (e EEPROM) WriteByteAt
528+
```go
529+
func (e EEPROM) WriteByteAt(value byte, addr int64) error
530+
```
531+
532+
WriteByteAt performs the logic to writes a byte into the EEPROM at the given address.
533+
534+
535+
### func (e EEPROM) ReadAt
536+
```go
537+
func (e EEPROM) ReadAt(buf []byte, off int64) (int, error)
538+
```
539+
540+
ReadAt reads exactly len(buf) into buf at the offset. It will return the amount of bytes copied or an error if one exists.
541+
The buffer cannot be empty, and an an error is thrown if fewer bytes are read than the size of the buffer.
542+
543+
544+
### func (e EEPROM) ReadByteAt
545+
```go
546+
func (e EEPROM) ReadByteAt(addr int64) (byte, error)
547+
```
548+
549+
ReadByteAt reads and returns the byte at the specified address. An error is returned if there is a failure to read.
550+
551+
552+
505553

506554
## type I2C
507555

content/docs/reference/microcontrollers/machine/arduino.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ var (
189189
```
190190

191191

192+
```go
193+
var EEPROM0 = EEPROM{}
194+
```
195+
196+
EEPROM0 is the internal EEPROM on most AVRs.
197+
192198

193199
```go
194200
var I2C0 *I2C = nil
@@ -361,6 +367,48 @@ value of each parameter will use the peripheral's default settings.
361367

362368

363369

370+
## type EEPROM
371+
372+
```go
373+
type EEPROM struct {
374+
}
375+
```
376+
377+
### func (e EEPROM) WriteAt
378+
```go
379+
func (e EEPROM) WriteAt(data []byte, off int64) (int, error)
380+
```
381+
382+
WriteAt writes len(data) bytes in the EEPROMs at the provided offset.
383+
384+
385+
### func (e EEPROM) WriteByteAt
386+
```go
387+
func (e EEPROM) WriteByteAt(value byte, addr int64) error
388+
```
389+
390+
WriteByteAt performs the logic to writes a byte into the EEPROM at the given address.
391+
392+
393+
### func (e EEPROM) ReadAt
394+
```go
395+
func (e EEPROM) ReadAt(buf []byte, off int64) (int, error)
396+
```
397+
398+
ReadAt reads exactly len(buf) into buf at the offset. It will return the amount of bytes copied or an error if one exists.
399+
The buffer cannot be empty, and an an error is thrown if fewer bytes are read than the size of the buffer.
400+
401+
402+
### func (e EEPROM) ReadByteAt
403+
```go
404+
func (e EEPROM) ReadByteAt(addr int64) (byte, error)
405+
```
406+
407+
ReadByteAt reads and returns the byte at the specified address. An error is returned if there is a failure to read.
408+
409+
410+
411+
364412
## type I2C
365413

366414
```go

0 commit comments

Comments
 (0)