Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
CameronRP committed May 27, 2024
0 parents commit 70ede9c
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist
33 changes: 33 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
project_name: eeprom-programmer

release:
github:
owner: TheCacophonyProject
name: eeprom-programmer
name_template: '{{.Tag}}'

builds:
- id: eeprom-programmer
binary: eeprom-programmer
main: ./cmd/eeprom-programmer
goos:
- linux
goarch:
- arm64
ldflags: -s -w -X main.version={{.Version}}

nfpms:
- vendor: The Cacophony Project
homepage: http://cacophony.org.nz/
maintainer: Cacophony Developers <coredev@cacophony.org.nz>
description: Controls the ATtiny and other device on the Pi HAT
license: GPL v3.0
file_name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Arch }}"
formats:
- deb
bindir: /usr/bin

checksum:
name_template: '{{ .ProjectName }}_{{ .Version }}_checksums.txt'

dist: dist
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: go

go:
- "1.22.x"

script:
- go mod tidy
- git diff --exit-code || (echo "Please run 'go mod tidy' to clean up the 'go.mod' and 'go.sum' files. Your go version should match the one used with travis."; false)
- go vet ./...
- go test ./...
- curl -sL https://git.io/goreleaser | bash -s check

# calls goreleaser
deploy:
- provider: script
skip_cleanup: true
script: curl -sL https://git.io/goreleaser | bash
on:
tags: true
go: "1.22.x"
115 changes: 115 additions & 0 deletions cmd/eeprom-programmer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package main

import (
"bytes"
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/TheCacophonyProject/tc2-hat-controller/eeprom"
"github.com/alexflint/go-arg"
"periph.io/x/conn/v3/i2c/i2creg"
"periph.io/x/host/v3"
)

func main() {
if err := runMain(); err != nil {
log.Fatal(err)
}
}

func runMain() error {
args := procArgs()

parts := strings.Split(args.PCBVersion, ".")
if len(parts) != 3 {
return fmt.Errorf("invalid hardware version '%s'", args.PCBVersion)
}

major, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return err
}
minor, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return err
}
patch, err := strconv.ParseInt(parts[2], 10, 64)
if err != nil {
return err
}

eepromData := &eeprom.EepromData{
Version: 1,
Major: byte(major),
Minor: byte(minor),
Patch: byte(patch),
ID: eeprom.GenerateRandomID(),
Time: time.Now().Truncate(time.Second),
}

data := eepromData.WriteData()

log.Println("Initializing host")
if _, err := host.Init(); err != nil {
return err
}

bus, err := i2creg.Open("")
if err != nil {
return err
}

log.Printf("Writing EEPROM data: %+v", eepromData)
pageLength := 16 // Can only read one page on the eeprom chip at a time
dataLength := len(data)
for i := 0; i < dataLength; i += pageLength {
writeLen := min(pageLength, dataLength-i)
pageWriteData := data[i : i+writeLen]
d := append([]byte{byte(i)}, pageWriteData...)
err = bus.Tx(eeprom.EEPROM_ADDRESS, d, nil)
if err != nil {
log.Println("Error writing EEPROM: ", err)
return err
}
time.Sleep(10 * time.Millisecond)
}

log.Println("EEPROM data written to.")
eepromDataLength := len(data)
readData := []byte{}
for i := 0; i < eepromDataLength; i += pageLength {
readLen := min(pageLength, eepromDataLength-i)
r := make([]byte, readLen)
err := bus.Tx(eeprom.EEPROM_ADDRESS, []byte{byte(i)}, r)
if err != nil {
return err
}
readData = append(readData, r...)
time.Sleep(10 * time.Millisecond)
}

if !bytes.Equal(data, readData) {
return fmt.Errorf("data read from eeprom doesn't match data written to")
}

return nil
}

var version = "<not set>"

func (Args) Version() string {
return version
}

func procArgs() Args {
args := Args{}
arg.MustParse(&args)
return args
}

type Args struct {
PCBVersion string `arg:"required" help:"Write to a register."`
}
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/TheCacophonyProject/eeprom-programmer

go 1.22.3

require (
github.com/TheCacophonyProject/tc2-hat-controller v0.6.7
github.com/alexflint/go-arg v1.5.0
periph.io/x/conn/v3 v3.7.0
periph.io/x/host/v3 v3.8.2
)

require (
github.com/alexflint/go-scalar v1.2.0 // indirect
github.com/godbus/dbus v4.1.0+incompatible // indirect
)
23 changes: 23 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
github.com/TheCacophonyProject/tc2-hat-controller v0.6.7 h1:QKvyekMUPTSgrrDrKSIisHAcrt/zZK86scb1lxIgLBQ=
github.com/TheCacophonyProject/tc2-hat-controller v0.6.7/go.mod h1:lei4dqVdRWhBmUvG4v3lD8dxmqbmbidix/dsLiDRUnE=
github.com/alexflint/go-arg v1.5.0 h1:rwMKGiaQuRbXfZNyRUvIfke63QvOBt1/QTshlGQHohM=
github.com/alexflint/go-arg v1.5.0/go.mod h1:A7vTJzvjoaSTypg4biM5uYNTkJ27SkNTArtYXnlqVO8=
github.com/alexflint/go-scalar v1.2.0 h1:WR7JPKkeNpnYIOfHRa7ivM21aWAdHD0gEWHCx+WQBRw=
github.com/alexflint/go-scalar v1.2.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4=
github.com/godbus/dbus v4.1.0+incompatible/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/jonboulle/clockwork v0.3.0 h1:9BSCMi8C+0qdApAp4auwX0RkLGUjs956h0EkuQymUhg=
github.com/jonboulle/clockwork v0.3.0/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
periph.io/x/conn/v3 v3.7.0 h1:f1EXLn4pkf7AEWwkol2gilCNZ0ElY+bxS4WE2PQXfrA=
periph.io/x/conn/v3 v3.7.0/go.mod h1:ypY7UVxgDbP9PJGwFSVelRRagxyXYfttVh7hJZUHEhg=
periph.io/x/host/v3 v3.8.2 h1:ayKUDzgUCN0g8+/xM9GTkWaOBhSLVcVHGTfjAOi8OsQ=
periph.io/x/host/v3 v3.8.2/go.mod h1:yFL76AesNHR68PboofSWYaQTKmvPXsQH2Apvp/ls/K4=

0 comments on commit 70ede9c

Please sign in to comment.