Skip to content

Commit

Permalink
sysfs: make frequency error messages more actionable
Browse files Browse the repository at this point in the history
Print which part of the range is exceeded (lower or higher bound), and give hint
about using the physic constant only on the lower end.
  • Loading branch information
maruel committed Jun 26, 2018
1 parent de66911 commit a5c53b7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
7 changes: 5 additions & 2 deletions host/sysfs/i2c.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,11 @@ func (i *I2C) Tx(addr uint16, w, r []byte) error {

// SetSpeed implements i2c.Bus.
func (i *I2C) SetSpeed(f physic.Frequency) error {
if f < 100*physic.KiloHertz || f > 10*physic.MegaHertz {
return fmt.Errorf("sysfs-i2c: invalid speed %s; did you forget to multiply by physic.KiloHertz?", f)
if f > 100*physic.MegaHertz {
return fmt.Errorf("sysfs-i2c: invalid speed %s; maximum supported clock is 100MHz", f)
}
if f < physic.KiloHertz {
return fmt.Errorf("sysfs-i2c: invalid speed %s; minimum supported clock is 1KHz; did you forget to multiply by physic.KiloHertz?", f)
}
drvI2C.mu.Lock()
defer drvI2C.mu.Unlock()
Expand Down
14 changes: 10 additions & 4 deletions host/sysfs/spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ func (s *SPI) String() string {

// LimitSpeed implements spi.ConnCloser.
func (s *SPI) LimitSpeed(f physic.Frequency) error {
if f < 100*physic.Hertz || f > physic.GigaHertz {
return fmt.Errorf("sysfs-spi: invalid speed %s; did you forget to multiply by physic.MegaHertz?", f)
if f > physic.GigaHertz {
return fmt.Errorf("sysfs-spi: invalid speed %s; maximum supported clock is 1GHz", f)
}
if f < 100*physic.Hertz {
return fmt.Errorf("sysfs-spi: invalid speed %s; minimum supported clock is 100Hz; did you forget to multiply by physic.MegaHertz?", f)
}
s.Lock()
defer s.Unlock()
Expand All @@ -105,8 +108,11 @@ func (s *SPI) LimitSpeed(f physic.Frequency) error {
//
// It must be called before any I/O.
func (s *SPI) Connect(f physic.Frequency, mode spi.Mode, bits int) (spi.Conn, error) {
if f < 100*physic.Hertz || f > physic.GigaHertz {
return nil, fmt.Errorf("sysfs-spi: invalid speed %s; did you forget to multiply by physic.MegaHertz?", f)
if f > physic.GigaHertz {
return nil, fmt.Errorf("sysfs-spi: invalid speed %s; maximum supported clock is 1GHz", f)
}
if f < 100*physic.Hertz {
return nil, fmt.Errorf("sysfs-spi: invalid speed %s; minimum supported clock is 100Hz; did you forget to multiply by physic.MegaHertz?", f)
}
if mode&^(spi.Mode3|spi.HalfDuplex|spi.NoCS|spi.LSBFirst) != 0 {
return nil, fmt.Errorf("sysfs-spi: invalid mode %v", mode)
Expand Down

0 comments on commit a5c53b7

Please sign in to comment.