Skip to content

Commit

Permalink
reference/machine: added eeprom reference and machine docs
Browse files Browse the repository at this point in the history
  • Loading branch information
SoleimanyBen committed Dec 21, 2022
1 parent ab71d28 commit 4ec3e72
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 1 deletion.
55 changes: 55 additions & 0 deletions content/docs/concepts/peripherals/eeprom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: "EEPROM"
weight: 3
description: |
How to use the internal EEPROM.
---

For API documentation, see the [machine API](../../../reference/machine#eeprom).

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.

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.

## Controlling the EEPROM

Control of the EEPROM has been simplified through the `EEPROM` variable that can be accessed through the `machine` package.

Unlike most peripherals within TinyGo, you do not need to configure the EEPROM to use it.

Example usage of the EEPROM can be seen below.

```go
package main

import (
"machine"
"time"
)

// Address of the data to be stored.
// Addresses can be either in hex or dec.
const DataAddr = 0x10


func main() {
eeprom := machine.EEPROM0 // the onboard EEPROM

_, err := eeprom.WriteAt([]byte("Hello, World!"), DataAddr)
if err != nil{
panic(err)
}

// Create a buffer to read the data into
// We need to define a buffer with the correct size of the bytes we want to read.
buf := make([]byte, len("Hello, World!"))
_, err := eeprom.ReadAt(buf, DataAddr)
if err != nil {
panic(err)
}

fmt.Println(buf) // Hello, World!
}
```

This example will store a value, `Hello, World!`, into the address `0x10`.
39 changes: 39 additions & 0 deletions content/docs/reference/machine.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,45 @@ func (uart *UART) WriteByte(c byte) error
Write a single byte to the UART output.


## EEPROM

```go
type EEPROM struct {
}

var EEPROM0 = EEPROM{}
```

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.


```go
func (e EEPROM) WriteAt(data []byte, off int64) (int, error)
```

WriteAt writes len(data) bytes in the EEPROMs at the provided offset. This method implements the `io.WriterAt` interface.

```go
func (e EEPROM) WriteByteAt(value byte, addr int64) error
```

WriteByteAt performs the logic to writes a byte into the EEPROM at the given address.

```go
func (e EEPROM) ReadAt(buf []byte, off int64) (int, error)
```

ReadAt reads exactly len(buf) into buf at the offset. It will return the amount of bytes copied or an error if one exists.
The buffer cannot be empty, and an an error is thrown if fewer bytes are read than the size of the buffer.
This method implements the `io.ReaderAt` interface.

```go
func (e EEPROM) ReadByteAt(addr int64) (byte, error)
```

ReadByteAt reads and returns the byte at the specified address. An error is returned if there is a failure to read.


## Other

```go
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ var (
)
```

```go
var EEPROM0 = EEPROM{}
```

EEPROM0 is the internal EEPROM on most AVRs.


```go
Expand Down Expand Up @@ -371,7 +376,6 @@ var (




### func CPUFrequency

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



## type EEPROM

```go
type EEPROM struct {
}
```

### func (e EEPROM) WriteAt
```go
func (e EEPROM) WriteAt(data []byte, off int64) (int, error)
```

WriteAt writes len(data) bytes in the EEPROMs at the provided offset.


### func (e EEPROM) WriteByteAt
```go
func (e EEPROM) WriteByteAt(value byte, addr int64) error
```

WriteByteAt performs the logic to writes a byte into the EEPROM at the given address.


### func (e EEPROM) ReadAt
```go
func (e EEPROM) ReadAt(buf []byte, off int64) (int, error)
```

ReadAt reads exactly len(buf) into buf at the offset. It will return the amount of bytes copied or an error if one exists.
The buffer cannot be empty, and an an error is thrown if fewer bytes are read than the size of the buffer.


### func (e EEPROM) ReadByteAt
```go
func (e EEPROM) ReadByteAt(addr int64) (byte, error)
```

ReadByteAt reads and returns the byte at the specified address. An error is returned if there is a failure to read.




## type I2C

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,13 @@ var (
```


```go
var EEPROM0 = EEPROM{}
```

EEPROM0 is the internal EEPROM on most AVRs.



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



## type EEPROM

```go
type EEPROM struct {
}
```

### func (e EEPROM) WriteAt
```go
func (e EEPROM) WriteAt(data []byte, off int64) (int, error)
```

WriteAt writes len(data) bytes in the EEPROMs at the provided offset.


### func (e EEPROM) WriteByteAt
```go
func (e EEPROM) WriteByteAt(value byte, addr int64) error
```

WriteByteAt performs the logic to writes a byte into the EEPROM at the given address.


### func (e EEPROM) ReadAt
```go
func (e EEPROM) ReadAt(buf []byte, off int64) (int, error)
```

ReadAt reads exactly len(buf) into buf at the offset. It will return the amount of bytes copied or an error if one exists.
The buffer cannot be empty, and an an error is thrown if fewer bytes are read than the size of the buffer.


### func (e EEPROM) ReadByteAt
```go
func (e EEPROM) ReadByteAt(addr int64) (byte, error)
```

ReadByteAt reads and returns the byte at the specified address. An error is returned if there is a failure to read.




## type I2C

Expand Down
48 changes: 48 additions & 0 deletions content/docs/reference/microcontrollers/machine/arduino.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ var (
```


```go
var EEPROM0 = EEPROM{}
```

EEPROM0 is the internal EEPROM on most AVRs.


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



## type EEPROM

```go
type EEPROM struct {
}
```

### func (e EEPROM) WriteAt
```go
func (e EEPROM) WriteAt(data []byte, off int64) (int, error)
```

WriteAt writes len(data) bytes in the EEPROMs at the provided offset.


### func (e EEPROM) WriteByteAt
```go
func (e EEPROM) WriteByteAt(value byte, addr int64) error
```

WriteByteAt performs the logic to writes a byte into the EEPROM at the given address.


### func (e EEPROM) ReadAt
```go
func (e EEPROM) ReadAt(buf []byte, off int64) (int, error)
```

ReadAt reads exactly len(buf) into buf at the offset. It will return the amount of bytes copied or an error if one exists.
The buffer cannot be empty, and an an error is thrown if fewer bytes are read than the size of the buffer.


### func (e EEPROM) ReadByteAt
```go
func (e EEPROM) ReadByteAt(addr int64) (byte, error)
```

ReadByteAt reads and returns the byte at the specified address. An error is returned if there is a failure to read.




## type I2C

```go
Expand Down

0 comments on commit 4ec3e72

Please sign in to comment.