Hi fellow wiki editors!

To help newly registered users get more familiar with the wiki (and maybe older users too) there is now a {{Welcome to the wiki}} template. Have a look at it and feel free to add it to new users discussion pages (and perhaps your own).

I have tried to keep the template short, but meaningful. /Johan G

MCP23017 Class

From FlightGear wiki
Jump to: navigation, search

Description

MCP23017 is a class designed to manage MCP23017 chips from Microchip. These chips use the I2C bus for communication.
An address, unique on the bus, is set to 3 bits. This leaves only 8 possible addresses.

Physically a part of the base address of these chips is set to 0x20 [b00100000 in binary].
Since the last five bits are internally set, only the last three bits remain to set the address. These last three bits [b0010xxx] are exposed to the outside of the case and can be fixed "hardware" with a pullup or pulldown resistor . The address range available for the MCP23017 chips is between 0x20 and 0x27 (Hex Decimal value)

[b00100000] = 0x20
...
[b00100111] = 0x27

MCP23017 Instance

The MCP23017 Class allows the creation and management of a device of type MCP23017 which allows to control a chip MCP23017 from Microchip.
In normal operation this Class is not called directly. This class is invoked when reading the configuration and creating the devices defined in the devices configuration file. Each device with de the devicetype property set to MCP23017 in the device configuration file will be create with this Module Class.

However, as part of a development, it may be necessary to perform tests to have to manually create these device objects.

Remember, after reading the configuration and generating the elements everything is stored in the interface object. Classes for generating devices are no exception. These Classes for creating device objects are part of the interface modules.

MCP23017(devicename, deviceaddr, debug_level)

  • devicename : device name, this is this name will be use in the configiration to identify the device. This name need to be unique
  • deviceaddr : device address on I2C bus. Hexadecimal format.
  • debug_devel : Debug Level Value (Integer)

We need to call the Module MCP23017 to create our device. This done with the getModule(modulename) interface methode
If you want to know what module is loaded in the interface you can call the listLoadedModules().
To create a new device named IOPACK2 (which is not defined in the configuration file but exist on the I2C bus [ connected ] ) with the following parameters

  • devicename : IOPACK2
  • deviceaddr : 0x21
  • debug_level : 0


Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> FGINT1.listLoadedModules()
{'HT16K33': <module 'FGIntHT16K33' from '/opt/fgint/FGInt/FGIntHT16K33.py'>, 'MCP23017': <module 'FGIntMCP23017' from '/opt/fgint/FGInt/FGIntMCP23017.py'>, 'Switch': <module 'FGIntSwitch' from '/opt/fgint/FGInt/FGIntSwitch.py'>, 'SegDisplay': <module 'FGIntSegDisplay' from '/opt/fgint/FGInt/FGIntSegDisplay.py'>}
>>> MODULE_MCP23017 = FGINT1.getModule('MCP23017')
>>> IOPACK2 = MODULE_MCP23017('IOPACK2', '0x21', 0)
>>> print(IOPACK2)
<FGIntMCP23017.MCP23017 object at 0x7689a0f0>
>>>

IOPACK2 is now an instance of the MCP23017 class witch will drive the device named IOPACK2 at the address 0x21 on the I2C bus

MCP23017 Methods

configMCP(state)

This method configure the device.

  • state : Set the state of the device. 0 or 1.(Integer). When set to 1 , the device is active
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> MODULE_MCP23017 = FGINT1.getModule('MCP23017')
>>> IOPACK2 = MODULE_MCP23017('IOPACK2', '0x21', 0)
>>> IOPACK2.config(1) # activate the device
>>>
>>> IOPACK2.config(0) # desactivate the device
>>>

ResetRegisters()

Reset all device buffers to is initial state. This method should never be called during the main exection. Very dangerous !!! Except for development or debugging and knowing what you are doing, there is no reason to invoke this method directly. It is a system method internal to the operation of the driver. This method resets all registers to 0x00 except the registers IODIRA and IODIRB which are set to 0xff (set the port as Input)

Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> MODULE_MCP23017 = FGINT1.getModule('MCP23017')
>>> IOPACK2 = MODULE_MCP23017('IOPACK2', '0x21', 0)
>>> IOPACK2.ResetRegisters()
>>>

setModeBank(bankmode)

The Bank Mode manage internal buffers addresses. This method should never be called during the main exection. Except for development or debugging and knowing what you are doing, there is no reason to invoke this method directly. It is a system method internal to the operation of the driver.

  • bankmode : Bank Mode value 1, 0 (Integer)
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> MODULE_MCP23017 = FGINT1.getModule('MCP23017')
>>> IOPACK2 = MODULE_MCP23017('IOPACK2', '0x21', 0)
>>> IOPACK2.setModeBank(0)
>>>

getName()

Return device name.

Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> MODULE_MCP23017 = FGINT1.getModule('MCP23017')
>>> IOPACK2 = MODULE_MCP23017('IOPACK2', '0x21', 0)
>>> IOPACK2.getName()
'IOPACK2'
>>>

getType()

Return device type.

Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> MODULE_MCP23017 = FGINT1.getModule('MCP23017')
>>> IOPACK2 = MODULE_MCP23017('IOPACK2', '0x21', 0)
>>> IOPACK2.getType()
'MCP23017'
>>>

getAddress()

Return device address on the I2C bus (Hexadecimal value)

Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> MODULE_MCP23017 = FGINT1.getModule('MCP23017')
>>> IOPACK2 = MODULE_MCP23017('IOPACK2', '0x21', 0)
>>> IOPACK2.getAddress()
'0x21'
>>>

getRegisterAddr(registername)

Returns the internal address of the register according to the Bank Mode configuration for registername . Registers name can be found in the chip documentation. Except for development or debugging, there is no reason to invoke this method directly. It is a system method internal to the operation of the driver.

  • registername : Register name (string). This parameter is case sensitive and registername need to be in lower case.
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> MODULE_MCP23017 = FGINT1.getModule('MCP23017')
>>> IOPACK2 = MODULE_MCP23017('IOPACK2', '0x21', 0)
>>> IOPACK2.getRegisterAddr('gpiob')
'0x13'
>>>

getRegister(registername)

Returns the current value of the register registername. Registers name can be found in the chip documentation. Except for development or debugging, there is no reason to invoke this method directly. It is a system method internal to the operation of the driver.

  • registername : Register name (string). This parameter is case sensitive and registername need to be in lower case.
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> MODULE_MCP23017 = FGINT1.getModule('MCP23017')
>>> IOPACK2 = MODULE_MCP23017('IOPACK2', '0x21', 0)
>>> print(hex(IOPACK2.getRegister('iodirb')))
0xff # 255 
>>>

setRegister(registername, registervalue)

Set the registername with registervalue. Registers name can be found in the chip documentation. Except for development or debugging, there is no reason to invoke this method directly. It is a system method internal to the operation of the driver.

  • registername : Register name (string)
  • registervalue : Register value (hex value)
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> MODULE_MCP23017 = FGINT1.getModule('MCP23017')
>>> IOPACK2 = MODULE_MCP23017('IOPACK2', '0x21', 0)
>>> IOPACK2.getRegister('iodira')
255 # 0xff in hexa
>>> IOPACK2.setRegister('iodira', 0x00)
>>> IOPACK2.getRegister('iodira')
0   # 0x00 in hexa
>>> IOPACK2.ResetRegisters()
>>> IOPACK2.getRegister('iodira')
255 # 0xff in hexa
>>>

getBit(registername, bit)

Return value of the bit bit from the register registername. Registers name can be found in the chip documentation. Except for development or debugging, there is no reason to invoke this method directly. It is a system method internal to the operation of the driver.

  • registername : Register name (string)
  • bit : bit ID to get (Integer)
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> MODULE_MCP23017 = FGINT1.getModule('MCP23017')
>>> IOPACK2 = MODULE_MCP23017('IOPACK2', '0x21', 0)
>>> IOPACK2.getBit('iodira', 1)
1
>>>

setBit(registername, bit, bitvalue)

Set the bit bit of registername to bitvalue. Registers name can be found in the chip documentation. Except for development or debugging, there is no reason to invoke this method directly. It is a system method internal to the operation of the driver.

  • registername : Register name (string)
  • bit : bit ID (Integer)
  • bitvalue : 0, 1 (Integer)
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> MODULE_MCP23017 = FGINT1.getModule('MCP23017')
>>> IOPACK2 = MODULE_MCP23017('IOPACK2', '0x21', 0)
>>> IOPACK2.getBit('iodira', 1)
1
>>> IOPACK2.setBit('iodira', 1, 0)
>>> IOPACK2.getBit('iodira', 1)
0
>>>

setPortDirection(port, direction)

Set a port direction for port port. MCP23017 have 2 height bit port. Direction can set to have port in Input or Ouput. Port informations can be found in the chip documentation. It is a system method internal to the operation of the driver.
When the register iodirx [ x is A or B ] is set to 0xff all the register is configured as input. If the register is set to 0x00 all the register is configured as output.
Port direction is setup with the iodira and iodirb register. When a bit of the

  • port : A, B (string)
  • direction : in, out (string). in setting the port in Input and out set the port to Output
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> MODULE_MCP23017 = FGINT1.getModule('MCP23017')
>>> IOPACK2 = MODULE_MCP23017('IOPACK2', '0x21', 0)
>>> print(bin(IOPACK2.getRegister('iodira')))
0b11111111 # 0xff in hexa (all bit are set to 1)
>>> IOPACK2.setPortDirection('A', 'out')
>>> print(bin(IOPACK2.getRegister('iodira')))
0b0 # 0x00 in hexa (all bit are set to 0)
>>>

setPinDirection(port, pin, direction)

Set pin pin direction for port port. MCP23017 have 2 height bit I/O port. Direction can set for each pin in Input or Ouput. Pins informations can be found in the chip documentation. It is a system method internal to the operation of the driver. When a bit of the iodirx register [ x is a or b ] is set to 1 the corresponding pin is configured as input. If a bit of the 'iodirx' register [x is a or b ] is set to 0 the corresponding pin is configured as output.

  • port : A, B (string)
  • pin : I/O number to setup (Integer) Define pin witch will be affected.
  • direction : in, out (string). in setting the port in Input and out set the port to Output
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> MODULE_MCP23017 = FGINT1.getModule('MCP23017')
>>> IOPACK2 = MODULE_MCP23017('IOPACK2', '0x21', 0)
>>> print(bin(IOPACK2.getRegister('iodira')))
0b11111111 # all pin are in input
>>> IOPACK2.setPinDirection('A', 1, 'out')
>>> print(bin(IOPACK2.getRegister('iodira')))
0b11111110 # Pin 1 have been set to output
>>>

getPin(port, pin)

Return the pin value from port port. Pin informations can be found in the chip documentation. Except for development or debugging, there is no reason to invoke this method directly. It is a system method internal to the operation of the driver. The difference between the getBit() and getPin() methods is that the getPin() method only applies to the GPIO (I / O) ports of the MCP23017.

  • port : A, B (string)
  • pin : I/O number to return (Integer). Define pin witch will be affected.
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> MODULE_MCP23017 = FGINT1.getModule('MCP23017')
>>> IOPACK2 = MODULE_MCP23017('IOPACK2', '0x21', 0)
>>> IOPACK2.getPin('A', 1) # port A pin 1
0 # the pin is connected to GND (hardware event)
>>>
>>> IOPACK2.getPin('A', 1) # port A pin 1
1 # the pin is connected to VCC (hardware event)
>>>

setPin(port, pin, pinvalue)

Set the pin from the port port with the value pinvalue.Pin informations can be found in the chip documentation. Except for development or debugging, there is no reason to invoke this method directly. It is a system method internal to the operation of the driver. The difference between the setBit() and setPin() methods is that the setPin() method only applies to the GPIO pin in output mode.

  • port : A, B (string)
  • pin : I/O number to set with the pinvalue
  • pinvalue : 0, 1 (Integer)
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> MODULE_MCP23017 = FGINT1.getModule('MCP23017')
>>> IOPACK2 = MODULE_MCP23017('IOPACK2', '0x21', 0)
>>> IOPACK2.setPortDirection('A', 'out') # Configuring Port '''A''' in '''output'''
>>> print(hex(IOPACK2.getRegister('iodira')))
0x0 # iodira register is set to 0 (0x00 in hexa). port '''A''' is in '''output''' mode
>>> IOPACK2.getPin('A', 1)
0
>>> IOPACK2.setPin('A', 1, 1) # Setting Pin to 1 (Active)
>>> IOPACK2.getPin('A', 1)
1
>>> IOPACK2.setPin('A', 1, 0)
>>> IOPACK2.getPin('A', 1)
0
>>>