def set_direction_bit(board, port, bit, value): # select the correspondent device if((port == "A") or (port == "B")): selection.gpioAB(board) else: selection.gpioCD(board) #----------------------------------- # read current configuration if((port == "A") or (port == "C")): direction = int(gpio.OCR1_read()) else: direction = int(gpio.OCR2_read()) print "direction BEFORE = " + str(direction) #----------------------------------- # if bit will be set as INPUT ("1" state), performs an "or" with previous byte if (value): direction = (direction | (1 << bit)) # if bit will be set as OUTPUT ("0" state), performs an "and" with previous byte else: # mask is an XOR of 0xFF and bit position mask = 0xFF ^ (1 << bit) direction = (direction & mask) #----------------------------------- # rewrite previous byte with bit updated if((port == "A") or (port == "C")): gpio.OCR1_write(direction) else: gpio.OCR2_write(direction) print "direction AFTER = " + str(direction) + "\n=========================="
def read_digital_input_byte(board): ''' input parameters: - board: board address - bit: bit position (MSB = 7, LSB = 0) output parameters: - byte read in digital Port A ---------------------------- description: this function reads a full byte in digital port A. ''' selection.gpioAB(board) return gpio.GSR1()
def read_portB_digital_output_bit(board, bit_pos): ''' input parameters: - board: board address - bit_pos: bit position (MSB = 7, LSB = 0) output parameters: - bit: value of the bit in position "pos" ---------------------------- description: this function reads the setpoint of a single bit in digital port B. ''' selection.gpioAB(board) byte = int(gpio.OCR2_read()) # performs an "and" between byte and mask mask = (1 << bit_pos) bit = (byte & mask) >> bit_pos return bit
def read_portB_digital_input_bit(board, bit_pos): ''' input parameters: - board: board address - bit_pos: bit position (MSB = 7, LSB = 0) output parameters: - bit_val: value of the bit in position "pos" ---------------------------- description: this function reads a single bit in digital port A. ''' selection.gpioAB(board) # digital.read() returns a string represented in hexadecimal (e.g.: '0x54') byte = int(gpio.GSR2()) # performs an "and" between byte and mask mask = (1 << bit_pos) bit = (byte & mask) >> bit_pos return bit
def set_direction_bit(board, port, bit, value): ''' input parameters: - board: board address - port: pin port that will be configured (A, B, C or D) - bit: bit position (MSB = 7, LSB = 0) - value: "0" for OUTPUT "1" for INPUT output parameters: - none ---------------------------- description: this function defines the direction of a bit. ''' global lock with lock: # select the correspondent device if((port == "A") or (port == "B")): selection.gpioAB(board) else: selection.gpioCD(board) #----------------------------------- # read current configuration if((port == "A") or (port == "C")): direction = int(gpio.OCR1_read()) else: direction = int(gpio.OCR2_read()) #print "direction BEFORE = " + str(direction) #----------------------------------- # if bit will be set as INPUT ("1" state), performs an "or" with previous byte if (value): direction = (direction | (1 << bit)) # if bit will be set as OUTPUT ("0" state), performs an "and" with previous byte else: # mask is an XOR of 0xFF and bit position mask = 0xFF ^ (1 << bit) direction = (direction & mask) #----------------------------------- # rewrite previous byte with bit updated if((port == "A") or (port == "C")): gpio.OCR1_write(direction) else: gpio.OCR2_write(direction)
def set_digital_output_byte(board, value): ''' input parameters: - board: board address - value: byte that will be written output parameters: - none ---------------------------- description: this function writes a full byte in digital port B. ''' #----------------------------------- # update digital output stored value global digital_output digital_output = value flash.sector_write(board, 0x68, [value]) #----------------------------------- selection.gpioAB(board) gpio.OCR2_write(value)
def config(board): ''' input parameters: - board: board address output parameters: - none ---------------------------- description: this function configures the digital pins direction: Port A: all pins are input Port B: the 4 MSB are output and the 4 LSB are input ---------------------------- GCR: 0: configures that pin as OUTPUT. 1: configures that pin as INPUT. TSCR: 0: DISABLE three-state mode. 1: ENABLE three-state mode. ''' selection.gpioAB(board) # enable tri-state mode in ports A and B TSCR1_write(0xFF) TSCR2_write(0xFF) # set port A as input GCR1_write(0xFF) # set port B (4 inputs, 4 outputs) GCR2_write(0xF0) # set all initial outputs value low OCR1_write(0x00) OCR2_write(0x00) # disable tri-state mode in ports A and B TSCR1_write(0x00) TSCR2_write(0x00) #--------------------------------------------------- selection.gpioCD(board) # enable tri-state mode in ports C and D TSCR1_write(0xFF) TSCR2_write(0xFF) #======================================================= # initial GPIO configuration #======================================================= '''
def set_portB_digital_output_bit(board, bit, value): ''' input parameters: - board: board address - bit: bit position (MSB = 7, LSB = 0) - value: "0" or "1" output parameters: - none ---------------------------- description: this function modifies a single bit in digital port B. ''' global lock #----------------------------------- # previous state stored in lower part of block 5 (0x050000) # current state stored in upper part of block 5 (0x058000) # restore digital output stored value global digital_output # read previous state #digital_output = flash.read(board, 0x050000, 1) #digital_output = digital_output[0] #----------------------------------- # if bit to be written is "1", performs an "or" with previous byte if (value): digital_output = (digital_output | (1 << bit)) # if bit to be written is "0", performs an "and" with previous byte else: # mask is an XOR of 0xFF and bit position mask = 0xFF ^ (1 << bit) digital_output = (digital_output & mask) # rewrite previous byte with bit updated #----------------------------------- # write new digital output in current state address (0x058000) #flash.sector_write(board, 0x058000, [digital_output, 0xFF]) # update digital output stored value in previous state (0x050000) #flash.sector_write(board, 0x050000, [digital_output, 0xFF]) #----------------------------------- with lock: selection.gpioAB(board) gpio.OCR2_write(digital_output)