Forked from romilly. Changed the way SPI handler is injected in constructor

Fork of MCP23S17 by Romilly Cocking

Committer:
romilly
Date:
Sun Aug 22 15:37:25 2010 +0000
Revision:
7:53498e24592c
Parent:
6:7b5e59c0e71c
Child:
8:841b19734955

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
romilly 7:53498e24592c 1 /* MCP23S17 - drive the Microchip MCP23S17 16-bit Port Extender using SPI
romilly 7:53498e24592c 2 * Copyright (c) 2010 Romilly Cocking
romilly 7:53498e24592c 3 * Released under the MIT License: http://mbed.org/license/mit
romilly 7:53498e24592c 4 *
romilly 7:53498e24592c 5 * version 0.2
romilly 7:53498e24592c 6 */
romilly 7:53498e24592c 7
romilly 7:53498e24592c 8 #include "mbed.h"
romilly 7:53498e24592c 9 #include "MCP23S17.h"
romilly 7:53498e24592c 10
romilly 7:53498e24592c 11 MCP23S17::MCP23S17(SPI& spi, PinName ncs, char writeOpcode) : _spi(spi), _ncs(ncs) {
romilly 7:53498e24592c 12 _writeOpcode = writeOpcode;
romilly 7:53498e24592c 13 _readOpcode = _writeOpcode | 1; // low order bit = 1 for read
romilly 7:53498e24592c 14 _init();
romilly 7:53498e24592c 15 }
romilly 7:53498e24592c 16
romilly 7:53498e24592c 17 char MCP23S17::_read(char address) {
romilly 7:53498e24592c 18 _ncs = 0;
romilly 7:53498e24592c 19 _spi.write(_readOpcode);
romilly 7:53498e24592c 20 _spi.write(address);
romilly 7:53498e24592c 21 char result = _spi.write(0);
romilly 7:53498e24592c 22 _ncs = 1;
romilly 7:53498e24592c 23 return result;
romilly 7:53498e24592c 24 }
romilly 7:53498e24592c 25
romilly 7:53498e24592c 26 void MCP23S17::_write(char address, char data) {
romilly 7:53498e24592c 27 _ncs = 0;
romilly 7:53498e24592c 28 _spi.write(_writeOpcode);
romilly 7:53498e24592c 29 _spi.write(address);
romilly 7:53498e24592c 30 _spi.write(data);
romilly 7:53498e24592c 31 _ncs = 1;
romilly 7:53498e24592c 32 }
romilly 7:53498e24592c 33
romilly 7:53498e24592c 34 void MCP23S17::_init() {
romilly 7:53498e24592c 35 _write(IOCON, (IOCON_BYTE_MODE | IOCON_HAEN )); // Hardware addressing on, operations toggle between A and B registers
romilly 7:53498e24592c 36 }
romilly 7:53498e24592c 37
romilly 7:53498e24592c 38 void MCP23S17::directionA(char direction) {
romilly 7:53498e24592c 39 _write(IODIRA, direction);
romilly 7:53498e24592c 40 }
romilly 7:53498e24592c 41
romilly 7:53498e24592c 42 void MCP23S17::directionB(char direction) {
romilly 7:53498e24592c 43 _write(IODIRB, direction);
romilly 7:53498e24592c 44 }
romilly 7:53498e24592c 45
romilly 7:53498e24592c 46 void MCP23S17::interruptEnableA(char interruptsEnabledMask) {
romilly 7:53498e24592c 47 _write(GPINTENA, interruptsEnabledMask);
romilly 7:53498e24592c 48 }
romilly 7:53498e24592c 49
romilly 7:53498e24592c 50 void MCP23S17::interruptEnableB(char interruptsEnabledMask) {
romilly 7:53498e24592c 51 _write(GPINTENB, interruptsEnabledMask);
romilly 7:53498e24592c 52 }
romilly 7:53498e24592c 53
romilly 7:53498e24592c 54 void MCP23S17::mirrorInterrupts(bool mirror) {
romilly 7:53498e24592c 55 char iocon = _read(IOCON);
romilly 7:53498e24592c 56 if (mirror) {
romilly 7:53498e24592c 57 iocon = iocon | INTERRUPT_MIRROR_BIT;
romilly 7:53498e24592c 58 } else {
romilly 7:53498e24592c 59 iocon = iocon & ~INTERRUPT_MIRROR_BIT;
romilly 7:53498e24592c 60 }
romilly 7:53498e24592c 61 _write(IOCON, iocon);
romilly 7:53498e24592c 62
romilly 7:53498e24592c 63 }
romilly 7:53498e24592c 64
romilly 7:53498e24592c 65 void MCP23S17::interruptPolarity(Polarity polarity) {
romilly 7:53498e24592c 66 char iocon = _read(IOCON);
romilly 7:53498e24592c 67 if (polarity == ACTIVE_LOW) {
romilly 7:53498e24592c 68 iocon = iocon & ~INTERRUPT_POLARITY_BIT;
romilly 7:53498e24592c 69 } else {
romilly 7:53498e24592c 70 iocon = iocon | INTERRUPT_POLARITY_BIT;
romilly 7:53498e24592c 71 }
romilly 7:53498e24592c 72 _write(IOCON, iocon);
romilly 7:53498e24592c 73 }
romilly 7:53498e24592c 74
romilly 7:53498e24592c 75 void MCP23S17::defaultValueA(char valuesToCompare) {
romilly 7:53498e24592c 76 _write(DEFVALA, valuesToCompare);
romilly 7:53498e24592c 77 }
romilly 7:53498e24592c 78
romilly 7:53498e24592c 79 void MCP23S17::interruptControlA(char interruptContolBits) {
romilly 7:53498e24592c 80 _write(INTCONA, interruptContolBits);
romilly 7:53498e24592c 81 }
romilly 7:53498e24592c 82
romilly 7:53498e24592c 83 void MCP23S17::outputA(char byte) {
romilly 7:53498e24592c 84 _write(OLATA, byte);
romilly 7:53498e24592c 85 }
romilly 7:53498e24592c 86
romilly 7:53498e24592c 87 void MCP23S17::outputB(char byte) {
romilly 7:53498e24592c 88 _write(OLATB, byte);
romilly 7:53498e24592c 89 }
romilly 7:53498e24592c 90
romilly 7:53498e24592c 91 char MCP23S17::inputA() {
romilly 7:53498e24592c 92 return _read(GPIOA);
romilly 7:53498e24592c 93 }
romilly 7:53498e24592c 94
romilly 7:53498e24592c 95 char MCP23S17::inputB() {
romilly 7:53498e24592c 96 return _read(GPIOB);
romilly 7:53498e24592c 97 }