Debian Codegraph @

io/spi/spi.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package spi allows users to read from and write to an SPI device.
6 //
7 // # Deprecated
8 //
9 // This package is not maintained anymore. An actively supported cross-platform
10 // alternative is https://periph.io/.
11 package spi // import "golang.org/x/exp/io/spi"
12
13 import (
14 "time"
15
16 "golang.org/x/exp/io/spi/driver"
17 )
18
19 // Mode represents the SPI mode number where clock parity (CPOL)
20 // is the high order and clock edge (CPHA) is the low order bit.
21 type Mode int
22
23 const (
24 Mode0 = Mode(0)
25 Mode1 = Mode(1)
26 Mode2 = Mode(2)
27 Mode3 = Mode(3)
28 )
29
30 // Order is the bit justification to be used while transferring
31 // words to the SPI device. MSB-first encoding is more popular
32 // than LSB-first.
33 type Order int
34
35 const (
36 MSBFirst = Order(0)
37 LSBFirst = Order(1)
38 )
39
40 type Device struct {
41 conn driver.Conn
42 }
43
44 // SetMode sets the SPI mode. SPI mode is a combination of polarity and phases.
45 // CPOL is the high order bit, CPHA is the low order. Pre-computed mode
46 // values are Mode0, Mode1, Mode2 and Mode3.
47 // The value can be changed by SPI device's driver.
48 func (d *Device) SetMode(mode Mode) error {
49 return d.conn.Configure(driver.Mode, int(mode))
50 }
51
52 // SetMaxSpeed sets the maximum clock speed in Hz.
53 // The value can be overridden by SPI device's driver.
54 func (d *Device) SetMaxSpeed(speed int) error {
55 return d.conn.Configure(driver.MaxSpeed, speed)
56 }
57
58 // SetBitsPerWord sets how many bits it takes to represent a word, e.g. 8 represents 8-bit words.
59 // The default is 8 bits per word.
60 func (d *Device) SetBitsPerWord(bits int) error {
61 return d.conn.Configure(driver.Bits, bits)
62 }
63
64 // SetBitOrder sets the bit justification used to transfer SPI words.
65 // Valid values are MSBFirst and LSBFirst.
66 func (d *Device) SetBitOrder(o Order) error {
67 return d.conn.Configure(driver.Order, int(o))
68 }
69
70 // SetDelay sets the amount of pause will be added after each frame write.
71 func (d *Device) SetDelay(t time.Duration) error {
72 return d.conn.Configure(driver.Delay, int(t.Nanoseconds()/1000))
73 }
74
75 // SetCSChange sets whether to leave the chipselect enabled after a Tx.
76 func (d *Device) SetCSChange(leaveEnabled bool) error {
77 v := 0
78 if leaveEnabled {
79 v = 1
80 }
81 return d.conn.Configure(driver.CSChange, v)
82 }
83
84 // Tx performs a duplex transmission to write w to the SPI device
85 // and read len(r) bytes to r.
86 // User should not mutate the w and r until this call returns.
87 func (d *Device) Tx(w, r []byte) error {
88 // TODO(jbd): Allow nil w.
89 return d.conn.Tx(w, r)
90 }
91
92 // Open opens a device with the specified bus and chip select
93 // by using the given driver. If a nil driver is provided,
94 // the default driver (devfs) is used.
95
96 func Open(o driver.Opener) (*Device, error) {
97 conn, err := o.Open()
98 if err != nil {
99 return nil, err
100 }
101 return &Device{conn: conn}, nil
102 }
103
104 // Close closes the SPI device and releases the related resources.
105 func (d *Device) Close() error {
106 return d.conn.Close()
107 }