2016年10月31日月曜日

mbedのPinDetectをHackしてMCP23S17で使えるようにする。

mbedのチャタリング対策用のライブラリのPinDetectのソースをよく見てみると割込みではなくてポーリングで処理していた。

中で使っているのはDigitalInなのでこれをMCP23S17経由で読み取れるようにしてみた。

MCP23S17のPortB:0だけトグル動作。

mbed repository:
https://developer.mbed.org/users/ryood/code/ExioController_Test/ Revision:4

ExioController_Test/main.cpp
#include "mbed.h"
#include "rtos.h"
#include "ExioMcp23s17.h"
#include "ExioMcp23s17DigitalIn.h"
#include "ExioMcp23s17PinDetect.h"

SPI Spi(PC_12, PC_11, PC_10); // SPI3: mosi, miso, sclk
//SPI Spi(PA_7, PA_6, PA_5); // SPI1:  mosi, miso, sclk

// MExioMcp23s17(int hardwareaddress, SPI& spi, PinName nCs, PinName nReset);
ExioMcp23s17 Exio(0x00, Spi, PD_2, PA_13);

// トグル動作
bool pb0state = false;
void pb0Falled() {
    uint8_t data = Exio.readPort(ExioPortA);
    pb0state = !pb0state;
    if (pb0state) {
        data |= (1 << 0);
    } else {
        data &= ~(1 << 0);
    } 
    Exio.writePort(ExioPortA, data);
}

void pb1Falled() {
    Exio.writePort(ExioPortA, (1 << 1));
}

void pb2Falled() {
    Exio.writePort(ExioPortA, (1 << 2));
}

void pb3Falled() {
    Exio.writePort(ExioPortA, (1 << 3));
}

void pb4Falled() {
    Exio.writePort(ExioPortA, (1 << 4));
}

void pb5Falled() {
    Exio.writePort(ExioPortA, (1 << 5));
}

void pb6Falled() {
    Exio.writePort(ExioPortA, (1 << 6));
}

void pb7Falled() {
    Exio.writePort(ExioPortA, (1 << 7));
}

void ExioMcp23s17PinDetectTest()
{
    printf("\r\n\n*** ExioMcp23s17PinDetect test ***\r\n");
    
    Exio.reset();
    
    // set PORTA as output
    Exio.ioDirection(ExioPortA, 0x00);
    
    // create PORTB pins as inputs via ExioMcp23s17PinDetect
    ExioMcp23s17PinDetect PinB0(Exio, ExioPortB, 0);
    ExioMcp23s17PinDetect PinB1(Exio, ExioPortB, 1);
    ExioMcp23s17PinDetect PinB2(Exio, ExioPortB, 2);
    ExioMcp23s17PinDetect PinB3(Exio, ExioPortB, 3);
    ExioMcp23s17PinDetect PinB4(Exio, ExioPortB, 4);
    ExioMcp23s17PinDetect PinB5(Exio, ExioPortB, 5);
    ExioMcp23s17PinDetect PinB6(Exio, ExioPortB, 6);
    ExioMcp23s17PinDetect PinB7(Exio, ExioPortB, 7);

    // assign callback functions
    PinB0.attach_asserted(&pb0Falled);
    PinB0.setAssertValue(0);
    PinB0.setSampleFrequency();
    
    PinB1.attach_asserted(&pb1Falled);
    PinB1.setAssertValue(0);
    PinB1.setSampleFrequency();
    
    PinB2.attach_asserted(&pb2Falled);
    PinB2.setAssertValue(0);
    PinB2.setSampleFrequency();
    
    PinB3.attach_asserted(&pb3Falled);
    PinB3.setAssertValue(0);
    PinB3.setSampleFrequency();
    
    PinB4.attach_asserted(&pb4Falled);
    PinB4.setAssertValue(0);
    PinB4.setSampleFrequency();
    
    PinB5.attach_asserted(&pb5Falled);
    PinB5.setAssertValue(0);
    PinB5.setSampleFrequency();
    
    PinB6.attach_asserted(&pb6Falled);
    PinB6.setAssertValue(0);
    PinB6.setSampleFrequency();
    
    PinB7.attach_asserted(&pb7Falled);
    PinB7.setAssertValue(0);
    PinB7.setSampleFrequency();
    

    // LED Check (PortA)
    for (int i = 0; i < 8; i++) {
        Exio.writePort(ExioPortA, 1 << i);
        wait(0.1);
    }
    Exio.writePort(ExioPortA, 0x00);
    
    while (true) {
    }
}

int main()
{
    ExioMcp23s17PinDetectTest();
}

0 件のコメント:

コメントを投稿