Skip to content
Snippets Groups Projects

Modified the scripts to run on Raspberry Pi.

1 file
+ 87
0
Compare changes
  • Side-by-side
  • Inline
I2C_serial_pi.py 0 → 100644
+ 87
0
 
'''
 
Copyright 2021 Stichting Nederlandse Wetenschappelijk Onderzoek Instituten,
 
ASTRON Netherlands Institute for Radio Astronomy
 
Licensed under the Apache License, Version 2.0 (the "License");
 
you may not use this file except in compliance with the License.
 
You may obtain a copy of the License at
 
http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
 
distributed under the License is distributed on an "AS IS" BASIS,
 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
See the License for the specific language governing permissions and
 
limitations under the License.
 
 
I2C_serial_Pi
 
Started by Gijs
 
 
Class for using the I2C bus of the I2C. This class is used for the
 
basic I2C scripts to read and write the RCU2, PCC etc.
 
 
'''
 
import smbus
 
import sys
 
from time import *
 
 
DEBUG=False
 
BUS_NR = 1
 
 
class I2C:
 
 
def __init__(self, ADDRESS='040'):
 
self.I2C_Address = ADDRESS
 
 
bus = smbus.SMBus(BUS_NR)
 
 
 
def read_bytes(self, register, bytes_to_read=2):
 
try:
 
ret_value = bus.read_i2c_block_data(self.I2C_Address, register, bytes_to_read)
 
ret_ack = 1
 
except IOError, err:
 
ret_ack = 0
 
ret_value = 0
 
if DEBUG:
 
print("Reading error")
 
return ret_ack, ret_value
 
 
 
def read_last_reg(self, bytes_to_read):
 
ret_value=[]
 
for cnt in bytes_to_read:
 
try:
 
ret_value.append(bus.read_byte(self.I2C_Address))
 
ret_ack = 1
 
except IOError, err:
 
ret_ack = 0
 
ret_value = 0
 
if DEBUG:
 
print("Reading error")
 
return ret_ack,ret_value
 
 
def write_bytes(self, register, data):
 
try:
 
bus.write_i2c_block_data(self.I2C_Address, register, data)
 
ret_ack = 1
 
except IOError, err:
 
ret_ack = 0
 
ret_value = 0
 
if DEBUG:
 
print("Reading error")
 
return ret_ack
 
 
def write_pointer(self, register):
 
try:
 
ret_value = bus.read_i2c_block_data(self.I2C_Address, register, 1)
 
ret_ack = 1
 
except IOError, err:
 
ret_ack = 0
 
ret_value = 0
 
if DEBUG:
 
print("Reading error")
 
return ret_ack
 
 
if __name__ == "__main__":
 
I2C_Device = I2C(0x40)
 
I2C_Device.write_bytes(0x00, 0x00)
 
ret_ack, ret_value = I2C_Device.read_bytes(0x8C, 2)
 
print(ret_value)
Loading