-extracredit

Dependencies:   mbed

Committer:
sheldonfernandes2404
Date:
Wed Jan 28 04:08:50 2015 +0000
Revision:
0:574e155464d5
1

Who changed what in which revision?

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