]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/nova/spi.py
d22be37aefe77fce093459d5bd7bab42c9046b0e
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / nova / spi.py
1 """SPI Class for Binho Nova"""
2 from adafruit_blinka.microcontroller.nova import Connection
3
4
5 class SPI:
6     """Custom SPI Class for Binho Nova"""
7
8     MSB = 0
9     BUFFER_PAYLOAD_MAX_LENGTH = 64
10     WHR_PAYLOAD_MAX_LENGTH = 1024
11
12     def __init__(self, clock):
13         self._nova = Connection.getInstance()
14         self._nova.setNumericalBase(10)
15         self._nova.setOperationMode(0, "SPI")
16         self._nova.setClockSPI(0, clock)
17         self._nova.setModeSPI(0, 0)
18         self._nova.setIOpinMode(0, "DOUT")
19         self._nova.setIOpinMode(1, "DOUT")
20         self._nova.beginSPI(0)
21         self._novaCMDVer = "0"
22         if hasattr(self._nova, "getCommandVer"):
23             response = self._nova.getCommandVer().split(" ")
24             if response[0] != "-NG":
25                 self._novaCMDVer = response[1]
26
27         # Cpol and Cpha set by mode
28         # Mode  Cpol Cpha
29         #  0     0    0
30         #  1     0    1
31         #  2     1    0
32         #  3     1    1
33
34     def __del__(self):
35         """Close Nova on delete"""
36         self._nova.close()
37
38     # pylint: disable=too-many-arguments,unused-argument
39     def init(
40             self,
41             baudrate=1000000,
42             polarity=0,
43             phase=0,
44             bits=8,
45             firstbit=MSB,
46             sck=None,
47             mosi=None,
48             miso=None,
49     ):
50         """Initialize the Port"""
51         self._nova.setClockSPI(0, baudrate)
52         self._nova.setModeSPI(0, (polarity << 1) | (phase))
53
54     # pylint: enable=too-many-arguments,unused-argument
55
56     @staticmethod
57     def get_received_data(lineOutput):
58         """Return any received data"""
59         return lineOutput.split("RXD ")[1]
60
61     @property
62     def frequency(self):
63         """Return the current frequency"""
64         return self._nova.getClockSPI(0).split("CLK ")[1]
65
66     def write(self, buf, start=0, end=None):
67         """Write data from the buffer to SPI"""
68         end = end if end else len(buf)
69         payloadMaxLength = self.BUFFER_PAYLOAD_MAX_LENGTH
70         if int(self._novaCMDVer) >= 1:
71             payloadMaxLength = self.WHR_PAYLOAD_MAX_LENGTH
72         chunks, rest = divmod(end - start, payloadMaxLength)
73
74         for i in range(chunks):
75             chunk_start = start + i * payloadMaxLength
76             chunk_end = chunk_start + payloadMaxLength
77             if int(self._novaCMDVer) >= 1:
78                 self._nova.writeToReadFromSPI(0,
79                                               True,
80                                               False,
81                                               chunk_end - chunk_start,
82                                               buf[chunk_start:chunk_end])
83             else:
84                 self._nova.clearBuffer(0)
85                 self._nova.writeToBuffer(0, 0, buf[chunk_start:chunk_end])
86                 self._nova.transferBufferSPI(0, chunk_end - chunk_start + 1)
87         if rest:
88             if int(self._novaCMDVer) >= 1:
89                 self._nova.writeToReadFromSPI(0, True, False, rest, buf[-1 * rest :])
90             else:
91                 self._nova.clearBuffer(0)
92                 self._nova.writeToBuffer(0, 0, buf[-1 * rest :])
93                 self._nova.transferBufferSPI(0, rest)
94
95     def readinto(self, buf, start=0, end=None, write_value=0):
96         """Read data from SPI and into the buffer"""
97         end = end if end else len(buf)
98         if int(self._novaCMDVer) >= 1:
99             chunks, rest = divmod(end - start, self.WHR_PAYLOAD_MAX_LENGTH)
100             i = 0
101             for i in range(chunks):
102                 chunk_start = start + i * self.WHR_PAYLOAD_MAX_LENGTH
103                 chunk_end = chunk_start + self.WHR_PAYLOAD_MAX_LENGTH
104                 result = self._nova.writeToReadFromSPI(0,
105                                                        False,
106                                                        True,
107                                                        chunk_end - chunk_start,
108                                                        write_value)
109                 if result != "-NG":
110                     resp = result.split(" ")
111                     resp = resp[2]
112                     # loop over half of resp len as we're reading 2 chars at a time to form a byte
113                     loops = int(len(resp)/2)
114                     for j in range(loops):
115                         buf[(i*self.WHR_PAYLOAD_MAX_LENGTH)+start+j] = \
116                                                         int(resp[j*2]+resp[j*2+1], 16)
117                 else:
118                     raise RuntimeError("Received error response from Binho Nova, result = " + \
119                                        result)
120             if rest:
121                 result = self._nova.writeToReadFromSPI(0, False, True, rest, write_value)
122                 if result != "-NG":
123                     resp = result.split(" ")
124                     resp = resp[2]
125
126                     # loop over half of resp len as we're reading 2 chars at a time to form a byte
127                     loops = int(len(resp)/2)
128                     for j in range(loops):
129                         buf[(i*self.WHR_PAYLOAD_MAX_LENGTH)+start+j] = \
130                                                          int(resp[j*2]+resp[j*2+1], 16)
131                 else:
132                     raise RuntimeError("Received error response from Binho Nova, result = " + \
133                                        result)
134         else:
135             for i in range(start, end):
136                 buf[start+i] = int(self.get_received_data(self._nova.transferSPI(0, write_value)))
137
138     # pylint: disable=too-many-arguments
139     def write_readinto(
140             self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None
141     ):
142         """Perform a half-duplex write from buffer_out and then
143         read data into buffer_in
144         """
145         out_end = out_end if out_end else len(buffer_out)
146         in_end = in_end if in_end else len(buffer_in)
147         readlen = in_end - in_start
148         writelen = out_end - out_start
149         if readlen > writelen:
150             # resize out and pad with 0's
151             tmp = bytearray(buffer_out)
152             tmp.extend([0] * (readlen - len(buffer_out)))
153             buffer_out = tmp
154
155         if int(self._novaCMDVer) >= 1:
156             chunks, rest = divmod(len(buffer_out), self.WHR_PAYLOAD_MAX_LENGTH)
157             i = 0
158             for i in range(chunks):
159                 chunk_start = out_start + i * self.WHR_PAYLOAD_MAX_LENGTH
160                 chunk_end = chunk_start + self.WHR_PAYLOAD_MAX_LENGTH
161                 result = self._nova.writeToReadFromSPI(0,
162                                                        True,
163                                                        True,
164                                                        chunk_end-chunk_start,
165                                                        buffer_out[chunk_start:chunk_end])
166
167                 if result != "-NG":
168                     resp = result.split(" ")
169                     resp = resp[2]
170
171                     # loop over half of resp len as we're reading 2 chars at a time to form a byte
172                     loops = int(len(resp)/2)
173                     for j in range(loops):
174                         buffer_in[(i*self.WHR_PAYLOAD_MAX_LENGTH)+in_start+j] = \
175                                                                  int(resp[j*2]+resp[j*2+1], 16)
176                 else:
177                     raise RuntimeError("Received error response from Binho Nova, result = " + \
178                                        result)
179             if rest:
180                 result = self._nova.writeToReadFromSPI(0, True, True, rest, buffer_out[-1*rest:])
181                 if result != "-NG":
182                     resp = result.split(" ")
183                     resp = resp[2]
184
185                     # loop over half of resp len as we're reading 2 chars at a time to form a byte
186                     loops = int(len(resp)/2)
187                     for j in range(loops):
188                         buffer_in[(i*self.WHR_PAYLOAD_MAX_LENGTH)+in_start+j] = \
189                                                                  int(resp[j*2]+resp[j*2+1], 16)
190                 else:
191                     raise RuntimeError("Received error response from Binho Nova, result = " + \
192                                        result)
193             print(buffer_in)
194         else:
195             for data_out in buffer_out:
196                 data_in = int(self.get_received_data(self._nova.transferSPI(0, data_out)))
197                 if i < readlen:
198                     buffer_in[in_start+i] = data_in
199                 i += 1
200     # pylint: enable=too-many-arguments