@@ -83,9 +83,19 @@ For example:
83
83
- [ available()] ( #available )
84
84
- [ read()] ( #read )
85
85
- [ write()] ( #write )
86
- - [ SPI] ( #spi )
87
- - [ Stream] ( #stream )
88
- - [ Wire] ( #wire )
86
+ - [ SPI] ( #spi )
87
+ - [ begin()] ( #begin-1 )
88
+ - [ read()] ( #read-1 )
89
+ - [ transfer()] ( #transfer )
90
+ - [ read() \& write()] ( #read--write )
91
+ - [ Bit Size] ( #bit-size )
92
+ - [ Wire / I2C] ( #wire--i2c )
93
+ - [ Wire.begin()] ( #wirebegin )
94
+ - [ Wire.available()] ( #wireavailable )
95
+ - [ Wire.read()] ( #wireread )
96
+ - [ Wire.write()] ( #wirewrite )
97
+ - [ Wire.setClock()] ( #wiresetclock )
98
+ - [ SoftI2C] ( #softi2c )
89
99
- [ USB] ( #usb )
90
100
- [ Keyboard] ( #keyboard )
91
101
- [ Mouse] ( #mouse )
@@ -1029,15 +1039,181 @@ while True:
1029
1039
time.sleep(1 )
1030
1040
```
1031
1041
1032
- ### SPI
1033
- <!-- TODO -->
1034
- ### Stream
1035
- <!-- TODO -->
1036
- ### Wire
1037
- <!-- TODO -->
1042
+ ## SPI
1043
+
1044
+ SPI communication is initialized using the ` SPI ` object from the ` machine ` module.
1045
+
1046
+ ### begin()
1047
+
1048
+ ` spi = machine.SPI(port, baudrate, polarity, phase) `
1049
+
1050
+ SPI is initalized by importing the ` machine ` module and specifying a number of parameters, such as baudrate and polarity.
1051
+
1052
+ ** Example:**
1053
+
1054
+ ``` python
1055
+ import machine
1056
+
1057
+ spi = machine.SPI(0 , baudrate = 1000000 , polarity = 0 , phase = 0 )
1058
+ ```
1059
+
1060
+ ### read()
1061
+
1062
+ ` spi.readinto(data_in) `
1063
+
1064
+ Reads incoming data and stores it in an array. The size of the array needs to be adjusted to match the size of the incoming data size.
1065
+
1066
+ ** Example:**
1067
+
1068
+ ``` python
1069
+ import machine
1070
+ import time
1071
+
1072
+ spi = machine.SPI(0 , baudrate = 1000000 , polarity = 0 , phase = 0 )
1073
+ data_in = bytearray (3 )
1074
+
1075
+ spi.readinto(data_in)
1076
+
1077
+ print (" Received Data:" , data_in)
1078
+ ```
1079
+
1080
+ ### transfer()
1081
+
1082
+ ` spi.write(data_out) `
1083
+
1084
+ Writes data to the SPI bus.
1085
+
1086
+ ** Example:**
1087
+
1088
+ ``` python
1089
+ import machine
1090
+ import time
1091
+
1092
+ spi = machine.SPI(0 , baudrate = 1000000 , polarity = 0 , phase = 0 )
1093
+ data_out = b ' \x01\x02\x03 '
1094
+
1095
+ spi.write(data_out)
1096
+ ```
1097
+
1098
+ ### read() & write()
1099
+
1100
+ ` spi.write_readinto(data_out, data_in) `
1101
+
1102
+ Reads & writes data simultaneously, where incoming data is stored in a buffer.
1103
+
1104
+ ** Example:**
1105
+
1106
+ ``` python
1107
+ import machine
1108
+ import time
1109
+
1110
+ spi = machine.SPI(0 , baudrate = 1000000 , polarity = 0 , phase = 0 )
1111
+
1112
+ data_to_send = b ' \x01\x02\x03 '
1113
+
1114
+ data_received = bytearray (len (data_to_send))
1115
+
1116
+ spi.write_readinto(data_to_send, data_received)
1117
+
1118
+ print (" Received Data:" , data_received)
1119
+
1120
+ spi.deinit()
1121
+ ```
1122
+
1123
+ ### Bit Size
1124
+
1125
+ ` spi.bits = 8 `
1126
+
1127
+ Sets the number of bits per word.
1128
+
1129
+ ** Example:**
1130
+
1131
+ ``` python
1132
+ spi.bits = 8
1133
+ ```
1134
+
1135
+ ## Wire / I2C
1136
+
1137
+ ### Wire.begin()
1138
+
1139
+ ` i2c = I2C(port, scl, sda, freq) `
1140
+
1141
+ Initializes I2C communication on specified port and pins. The ` port ` and ` freq ` are optional parameters, if left unspecified, default values will be set.
1142
+ ``` python
1143
+ from machine import I2C
1144
+
1145
+ i2c = I2C(0 , scl = Pin(SCL_PIN ), sda = Pin(SDA_PIN ), freq = 100000 )
1146
+ ```
1147
+
1148
+ ### Wire.available()
1149
+
1150
+ ` i2c.in_waiting() `
1151
+
1152
+ Checks the number of available bytes to read.
1153
+
1154
+ ``` python
1155
+ available_bytes = i2c.in_waiting()
1156
+
1157
+ print (" Available bytes to read: " , available_bytes)
1158
+ ```
1159
+
1160
+ ### Wire.read()
1161
+
1162
+ ` i2c.readfrom(address, num_bytes) `
1163
+
1164
+ Reads data in specified number of bytes from a device with the specified address.
1165
+
1166
+ ``` python
1167
+ data_in = i2c.readfrom(address, num_bytes)
1168
+
1169
+ print (" I2C data: " , data_in)
1170
+ ```
1171
+
1172
+ ### Wire.write()
1173
+
1174
+ ` i2c.writeto(address, data_out) `
1175
+
1176
+ Writes data to specified address.
1177
+
1178
+ ** Example:**
1179
+
1180
+ ``` python
1181
+ from machine import I2C , Pin
1182
+
1183
+ # Initialize I2C anc configure device & reg addresses
1184
+ i2c = I2C(0 , scl = Pin(22 ), sda = Pin(21 ), freq = 100000 ) # Adjust pins and frequency as needed
1185
+ device_address = 0x 68
1186
+ register_address = 0x 00
1187
+
1188
+ # The array of bytes to be send out
1189
+ # This buffer simply stores 1,2,3,4
1190
+ data_out = bytearray ([0x 01 , 0x 02 , 0x 03 , 0x 04 ])
1191
+
1192
+ # Send the device address with the write bit to indicate a write operation
1193
+ i2c.writeto(device_address, bytearray ([register_address]))
1194
+
1195
+ # Finally, send data to the device
1196
+ i2c.writeto(device_address, data_out)
1197
+ ```
1198
+
1199
+ ### Wire.setClock()
1200
+
1201
+ The frequency for the clock is set during initialization. See [ begin()] ( #wirebegin ) for more info.
1202
+
1203
+ ### SoftI2C
1204
+
1205
+ MicroPython has a built in class called ` SoftI2C ` (as in software I2C). Software I2C does not use a dedicated hardware I2C peripheral, but instead relies on the CPU to handle the clock signal, communication protocol etc.
1206
+
1207
+ ` SoftI2C ` is avaialable through the ` machine ` module, and uses the same API as hardware I2C, with a few additional methods.
1208
+
1209
+ - ` softi2c = machine.SoftI2C(scl,sda,freq,timeout) ` - creates the ` softi2c ` object with specified pins, frequency and timeout.
1210
+ - ` softi2c.start() ` - create the start condition for initializing communication over I2C (SDA goes to ** LOW** while SCL is ** HIGH** ).
1211
+ - ` softi2c.stop() ` - create the stop condition for ending communication over I2C (SDA goes to ** HIGH** while SCL is ** HIGH** ).
1212
+
1038
1213
## USB
1039
1214
1040
1215
1216
+
1041
1217
### Keyboard
1042
1218
<!-- TODO -->
1043
1219
### Mouse
0 commit comments