This Automatic mode is the most simple lib for MCU Gear. You don't need to think about Bank.

This is a MCU Gear Automatic mode library.

(日本語は英語の次に書いてあります。)

MCU Gear is a digital wiring extension board.(A kind of dynamic circuit changer.)

It can change its wiring dynamically, without the need of rewiring your device.

Your MCU can be connected to a theoretical maximum of 120 devices.

detail : http://mcugear.com/en/

You can save 8 schematics (bank 0-7) on Baseboard. But this Automatic mode is you don't think about Bank but little bit slow. Manual Mode is here -> http://mbed.org/users/Info/code/MCUGearM/

How to use it?

1,include library

1,include library

#include "mbed.h"

#include "MCUGearBaseA.h"
#include "MCUGearA.h"
#include "commonA.h"

2,Declare Module

2,Declare Module

MCUGear myModule1(p28, p27, N_***_***_***);
MCUGear myModule2(p28, p27, N_***_***_***);

myModule: Optional name for Module.

p28, p27, : I2C pins (KL25Z is "PTE0, PTE1")

N_*_*_* : Address. You can set it on the reverse side of the Module board.

3,Initialize baseboard

3,Initialize baseboard

initBase();

4,Make wiring data

4,Call wiring register

    myModule1.setWire(IO_MBED_P9, IO_REG_IN_DIR, 5);
    myModule1.setWire(IO_MBED_P15, IO_REG_OUT_DIR, 2);
    myModule1.setWire(IO_MBED_P12, IO_REG_OUT_DIR, 0);

    myModule2.setWire(IO_MBED_P13, IO_REG_OUT_DIR, 1);
    myModule2.setWire(IO_MBED_P14, IO_REG_IN_DIR, 3);
    myModule2.setWire(IO_MBED_P16, IO_REG_IN_DIR, 4);

see the reference on MCUGearBaseboardA.h file.

4,Call wiring register

// CPU I/O
typedef enum {

#if defined TARGET_LPC1768
  IO_MBED_P15 = 0x80,
  IO_MBED_P13,
  IO_MBED_P16,
  IO_MBED_P12,
  IO_MBED_P5,
  IO_MBED_P11,
  IO_MBED_P23,
  IO_MBED_P22,
  IO_MBED_P26,
  IO_MBED_P10,
  IO_MBED_P21,
  IO_MBED_P9,
  IO_MBED_P6,
  IO_MBED_P7,
  IO_MBED_P8,
  IO_MBED_P14,
  IO_MBED_P24,
  IO_MBED_P25,
  IO_MBED_P28,
  IO_MBED_P27
#endif

#if defined TARGET_KL25Z

  IO_MBED_PTA5 = 0x80,
  IO_MBED_PTC8,
  IO_MBED_PTC9,
  IO_MBED_PTD5,
  IO_MBED_PTA13,
  IO_MBED_PTD2,
  IO_MBED_PTB1,
  IO_MBED_PTB2,
  IO_MBED_PTA2,
  IO_MBED_PTD4,
  IO_MBED_PTB3,
  IO_MBED_PTA12,
  IO_MBED_PTD0,
  IO_MBED_PTD3,
  IO_MBED_PTD1,
  IO_MBED_PTA4,
  IO_MBED_PTB0,
  IO_MBED_PTA1,
  IO_MBED_PTE0, //SDA
  IO_MBED_PTE1  //SCL
#endif
  
} en_cpu_io;

setWire function

uint8_t setWire(uint8_t mcuIO, uint8_t direction, uint8_t moduleIO);

Set MCU's IO, Directionfrom MCU and module's IO number.

setWire() is wiring data to connect pin of MCU to pin of Module.

uint8_t mcuIO : You can choose 18 IOs.

uint8_t direction: choose signal direction IO_REG_OUT_DIR : output from MCU IO_REG_IN_DIR : input to MCU

uint8_t moduleIO: You can select module pin.

5,Open or close the module gate

5,Open or close the module gate

   myModule1.connectModule();
   ...
   (same as normal mbed code)
   ...
   myModule1.disconnectModule();


   myModule2.connectModule();
   ...
   (same as normal mbed code)
   ...
   myModule2.disconnectModule();

If you want to use many modules simultaneously as one circuit, you should use manual mode.

Sample code 1

Universal Module and LCD module test program.

I set test function board like the below picture.(Universal module)

/media/uploads/Info/test2.png

Sample code 1

#include "mbed.h"
#include "TextLCD.h"

#include "MCUGearBaseA.h"
#include "MCUGearA.h"
#include "commonA.h"

Serial pc(USBTX, USBRX); // tx, rx Set BaudRate = 115200 in main()

void setIOUni(MCUGear *mcugear);
void setIOLCD(MCUGear *mcugear);

DigitalOut myled(LED1);

MCUGear UniM(p28, p27, N_VDD_VDD_VDD);       //2 Connector Universal Module
MCUGear LCDM(p28, p27, N_VDD_VSS_VSS);        //LCD Module

DigitalOut Dout1(p11);
DigitalOut Dout2(p12);
DigitalOut Dout3(p13);
DigitalOut Dout4(p14);
DigitalOut Dout5(p15);

DigitalIn Din1(p9);
DigitalIn Din2(p10);

int main() {
    
    int sw1 = 0;
    int sw2 = 0;
    
    initBase(); //initialize Baseboard
    
    //set schematic select 0-7
    UniM.detectModule();    //save location data in MCUGear class.
    LCDM.detectModule();    //save location data in MCUGear class.
    
    setIOUni(&UniM);//make schematic Bank0
    setIOLCD(&LCDM);//make schematic Bank0
    
    
    LCDM.connectModuleA();
    TextLCD lcd(p21, p22, p23, p24, p25, p26, TextLCD::LCD16x2); // rs, e, d4-d7
    lcd.cls();
    lcd.printf("Hello World!\n");
    wait(1);
    LCDM.disconnectModule();
    
    while(1) {
        
        UniM.connectModuleA();
        sw1 = Din1.read();
        sw2 = Din2.read();
        printf("sw1 = %d  sw2 = %d\r\n",sw1,sw2);
        UniM.disconnectModule();
        
        LCDM.connectModuleA();
        lcd.printf(" ");    //set IO as a neutral position
        lcd.cls();   //clear LCD
        lcd.printf("sw1 = %d\nsw2 = %d",sw1,sw2);   //write LCD data
        wait_ms(10);    //delay for view the LCD
        LCDM.disconnectModule();
        
        UniM.connectModuleA();
        Dout1 = 0;
        Dout2 = 1;
        Dout3 = 1;
        wait(0.3);
        
        Dout1 = 1;
        Dout2 = 0;
        Dout3 = 1;
        wait(0.3);
        
        Dout1 = 1;
        Dout2 = 1;
        Dout3 = 0;
        wait(0.3);
        UniM.disconnectModule();
    }
}


void setIOUni(MCUGear *mcugear){
    mcugear->setWireA(IO_MBED_P9, IO_REG_IN_DIR, 0);
    mcugear->setWireA(IO_MBED_P10, IO_REG_IN_DIR, 1);
    mcugear->setWireA(IO_MBED_P11, IO_REG_OUT_DIR,2);
    mcugear->setWireA(IO_MBED_P12, IO_REG_OUT_DIR,3);
    mcugear->setWireA(IO_MBED_P13, IO_REG_OUT_DIR,4);
}

void setIOLCD(MCUGear *mcugear){
    mcugear->setWireA(IO_MBED_P21, IO_REG_OUT_DIR,0);
    mcugear->setWireA(IO_MBED_P22, IO_REG_OUT_DIR,1);
    mcugear->setWireA(IO_MBED_P23, IO_REG_OUT_DIR,2);
    mcugear->setWireA(IO_MBED_P24, IO_REG_OUT_DIR,3);
    mcugear->setWireA(IO_MBED_P25, IO_REG_OUT_DIR,4);
    mcugear->setWireA(IO_MBED_P26, IO_REG_OUT_DIR,5);
}

日本語説明

MCU Gearの自動モードライブラリです。

detail : http://mcugear.com/en/

自動モードは8つのBank設定は考えずに実行できます。同時に複数のモジュールをつなげたい場合はマニュアルモードをご利用ください -> http://mbed.org/users/Info/code/MCUGearM/

使い方

1,ライブラリーをインクルード

1,include library

#include "mbed.h"

#include "MCUGearBaseA.h"
#include "MCUGearA.h"
#include "commonA.h"

2,モジュールを定義します

2,Declare Module

MCUGear myModule1(p28, p27, N_***_***_***);
MCUGear myModule2(p28, p27, N_***_***_***);

myModule: モジュールの好きな名前を付けられます。

p28, p27, : I2C pins (KL25Z is "PTE0, PTE1")

N_*_*_* : Address. モジュールの裏面に設定したアドレスを入れます。

3,ベースボード初期化

3,Initialize baseboard

initBase();

4,配線データの登録

4,Call wiring register

    myModule1.setWire(IO_MBED_P9, IO_REG_IN_DIR, 5);
    myModule1.setWire(IO_MBED_P15, IO_REG_OUT_DIR, 2);
    myModule1.setWire(IO_MBED_P12, IO_REG_OUT_DIR, 0);

    myModule2.setWire(IO_MBED_P13, IO_REG_OUT_DIR, 1);
    myModule2.setWire(IO_MBED_P14, IO_REG_IN_DIR, 3);
    myModule2.setWire(IO_MBED_P16, IO_REG_IN_DIR, 4);

MCUGearBaseboardA.h ファイルも参考にしてください。

4,Call wiring register

// CPU I/O
typedef enum {

#if defined TARGET_LPC1768
  IO_MBED_P15 = 0x80,
  IO_MBED_P13,
  IO_MBED_P16,
  IO_MBED_P12,
  IO_MBED_P5,
  IO_MBED_P11,
  IO_MBED_P23,
  IO_MBED_P22,
  IO_MBED_P26,
  IO_MBED_P10,
  IO_MBED_P21,
  IO_MBED_P9,
  IO_MBED_P6,
  IO_MBED_P7,
  IO_MBED_P8,
  IO_MBED_P14,
  IO_MBED_P24,
  IO_MBED_P25,
  IO_MBED_P28,
  IO_MBED_P27
#endif

#if defined TARGET_KL25Z

  IO_MBED_PTA5 = 0x80,
  IO_MBED_PTC8,
  IO_MBED_PTC9,
  IO_MBED_PTD5,
  IO_MBED_PTA13,
  IO_MBED_PTD2,
  IO_MBED_PTB1,
  IO_MBED_PTB2,
  IO_MBED_PTA2,
  IO_MBED_PTD4,
  IO_MBED_PTB3,
  IO_MBED_PTA12,
  IO_MBED_PTD0,
  IO_MBED_PTD3,
  IO_MBED_PTD1,
  IO_MBED_PTA4,
  IO_MBED_PTB0,
  IO_MBED_PTA1,
  IO_MBED_PTE0, //SDA
  IO_MBED_PTE1  //SCL
#endif
  
} en_cpu_io;

setWire function

uint8_t setWire(uint8_t mcuIO, uint8_t direction, uint8_t moduleIO);

引数はマイコンのIO、マイコンからの入出力方向とモジュールの端子番号 setWire()関数はmbedの端子とモジュールの端子の割り振り方を決める関数です。

uint8_t mcuIO : mbedの18端子がアダプターボード(MCUボード)を介してベースボードに接続されています。(残りの端子はアダプターボード(MCUボード)から端子が出ています。)

uint8_t direction: mbedから見て入力か出力かを設定します。 IO_REG_OUT_DIR : output from MCU IO_REG_IN_DIR : input to MCU

uint8_t moduleIO: モジュールの出口の端子をどれにするかを決めます。

5,モジュールのゲート開閉

5,Open or close the module gate

   myModule1.connectModule();
   ...
   (same as normal mbed code)
   ...
   myModule1.disconnectModule();


   myModule2.connectModule();
   ...
   (same as normal mbed code)
   ...
   myModule2.disconnectModule();

If you want to use many modules simultaneously as one circuit, you should use manual mode. 同時に複数のモジュールに接続して動かすには

Sample code 1

ユニバーサルモジュールとLCDモジュールを接続した例です。

ユニバーサルモジュールは以下の図のように接続します。

/media/uploads/Info/test2.png

Sample code 1

#include "mbed.h"
#include "TextLCD.h"

#include "MCUGearBaseA.h"
#include "MCUGearA.h"
#include "commonA.h"

Serial pc(USBTX, USBRX); // tx, rx Set BaudRate = 115200 in main()

void setIOUni(MCUGear *mcugear);
void setIOLCD(MCUGear *mcugear);

DigitalOut myled(LED1);

MCUGear UniM(p28, p27, N_VDD_VDD_VDD);       //2 Connector Universal Module
MCUGear LCDM(p28, p27, N_VDD_VSS_VSS);        //LCD Module

DigitalOut Dout1(p11);
DigitalOut Dout2(p12);
DigitalOut Dout3(p13);
DigitalOut Dout4(p14);
DigitalOut Dout5(p15);

DigitalIn Din1(p9);
DigitalIn Din2(p10);

int main() {
    
    int sw1 = 0;
    int sw2 = 0;
    
    initBase(); //initialize Baseboard
    
    //set schematic select 0-7
    UniM.detectModule();    //save location data in MCUGear class.
    LCDM.detectModule();    //save location data in MCUGear class.
    
    setIOUni(&UniM);//make schematic Bank0
    setIOLCD(&LCDM);//make schematic Bank0
    
    
    LCDM.connectModuleA();
    TextLCD lcd(p21, p22, p23, p24, p25, p26, TextLCD::LCD16x2); // rs, e, d4-d7
    lcd.cls();
    lcd.printf("Hello World!\n");
    wait(1);
    LCDM.disconnectModule();
    
    while(1) {
        
        UniM.connectModuleA();
        sw1 = Din1.read();
        sw2 = Din2.read();
        printf("sw1 = %d  sw2 = %d\r\n",sw1,sw2);
        UniM.disconnectModule();
        
        LCDM.connectModuleA();
        lcd.printf(" ");    //set IO as a neutral position
        lcd.cls();   //clear LCD
        lcd.printf("sw1 = %d\nsw2 = %d",sw1,sw2);   //write LCD data
        wait_ms(10);    //delay for view the LCD
        LCDM.disconnectModule();
        
        UniM.connectModuleA();
        Dout1 = 0;
        Dout2 = 1;
        Dout3 = 1;
        wait(0.3);
        
        Dout1 = 1;
        Dout2 = 0;
        Dout3 = 1;
        wait(0.3);
        
        Dout1 = 1;
        Dout2 = 1;
        Dout3 = 0;
        wait(0.3);
        UniM.disconnectModule();
    }
}


void setIOUni(MCUGear *mcugear){
    mcugear->setWireA(IO_MBED_P9, IO_REG_IN_DIR, 0);
    mcugear->setWireA(IO_MBED_P10, IO_REG_IN_DIR, 1);
    mcugear->setWireA(IO_MBED_P11, IO_REG_OUT_DIR,2);
    mcugear->setWireA(IO_MBED_P12, IO_REG_OUT_DIR,3);
    mcugear->setWireA(IO_MBED_P13, IO_REG_OUT_DIR,4);
}

void setIOLCD(MCUGear *mcugear){
    mcugear->setWireA(IO_MBED_P21, IO_REG_OUT_DIR,0);
    mcugear->setWireA(IO_MBED_P22, IO_REG_OUT_DIR,1);
    mcugear->setWireA(IO_MBED_P23, IO_REG_OUT_DIR,2);
    mcugear->setWireA(IO_MBED_P24, IO_REG_OUT_DIR,3);
    mcugear->setWireA(IO_MBED_P25, IO_REG_OUT_DIR,4);
    mcugear->setWireA(IO_MBED_P26, IO_REG_OUT_DIR,5);
}

Committer:
Info
Date:
Sun Mar 09 09:33:14 2014 +0000
Revision:
0:12d93184b350
Automatic module changer for MCU Gear. It make circuit each time. You don't need to think about the Bank.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Info 0:12d93184b350 1 /* MCU Gear Library, only for testing MCUGear without any circuit you connected.
Info 0:12d93184b350 2 * Copyright (c) 2013, NestEgg Inc., http://www.mcugear.com/
Info 0:12d93184b350 3 *
Info 0:12d93184b350 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
Info 0:12d93184b350 5 * of this software and associated documentation files (the "Software"), to deal
Info 0:12d93184b350 6 * in the Software without restriction, including without limitation the rights
Info 0:12d93184b350 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Info 0:12d93184b350 8 * copies of the Software, and to permit persons to whom the Software is
Info 0:12d93184b350 9 * furnished to do so, subject to the following conditions:
Info 0:12d93184b350 10 *
Info 0:12d93184b350 11 * The above copyright notice and this permission notice shall be included in
Info 0:12d93184b350 12 * all copies or substantial portions of the Software.
Info 0:12d93184b350 13 *
Info 0:12d93184b350 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Info 0:12d93184b350 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Info 0:12d93184b350 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Info 0:12d93184b350 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Info 0:12d93184b350 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Info 0:12d93184b350 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Info 0:12d93184b350 20 * THE SOFTWARE.
Info 0:12d93184b350 21 */
Info 0:12d93184b350 22
Info 0:12d93184b350 23 #include "mbed.h"
Info 0:12d93184b350 24 #include "MCUGearBaseA.h"
Info 0:12d93184b350 25
Info 0:12d93184b350 26 #if defined TARGET_LPC1768
Info 0:12d93184b350 27 I2C fpga_i2c(p28, p27);
Info 0:12d93184b350 28 #endif
Info 0:12d93184b350 29
Info 0:12d93184b350 30 #if defined TARGET_KL25Z
Info 0:12d93184b350 31 I2C fpga_i2c(PTE0, PTE1);
Info 0:12d93184b350 32 #endif
Info 0:12d93184b350 33
Info 0:12d93184b350 34 void fpga_write(int dev_adr,unsigned char adr, unsigned char data) {
Info 0:12d93184b350 35 char cmd[2];
Info 0:12d93184b350 36 cmd[0] = adr;
Info 0:12d93184b350 37 cmd[1] = data;
Info 0:12d93184b350 38 fpga_i2c.frequency (FPGA_I2C_CLOCK);
Info 0:12d93184b350 39 fpga_i2c.write(dev_adr, cmd, 2);
Info 0:12d93184b350 40
Info 0:12d93184b350 41 }
Info 0:12d93184b350 42
Info 0:12d93184b350 43 unsigned char fpga_read(int dev_adr,unsigned char adr) {
Info 0:12d93184b350 44 char cmd[2];
Info 0:12d93184b350 45 cmd[0] = adr;
Info 0:12d93184b350 46 fpga_i2c.write(dev_adr, cmd, 1);
Info 0:12d93184b350 47 fpga_i2c.read(dev_adr, cmd, 1);
Info 0:12d93184b350 48 return cmd[0];
Info 0:12d93184b350 49 }
Info 0:12d93184b350 50
Info 0:12d93184b350 51 void initBase(void){
Info 0:12d93184b350 52
Info 0:12d93184b350 53 // FPGA reg clear
Info 0:12d93184b350 54 for (int i=0;i<20;i++)
Info 0:12d93184b350 55 fpga_write(FPGA_I2C_ADR,0x80+i,0);
Info 0:12d93184b350 56
Info 0:12d93184b350 57 // read FPGA registers
Info 0:12d93184b350 58 fpga_read(FPGA_I2C_ADR,FPGA_SYSINFO_0);
Info 0:12d93184b350 59 fpga_read(FPGA_I2C_ADR,FPGA_SYSINFO_0+1);
Info 0:12d93184b350 60 fpga_read(FPGA_I2C_ADR,FPGA_SYSINFO_0+2);
Info 0:12d93184b350 61 fpga_read(FPGA_I2C_ADR,FPGA_SYSINFO_0+3);
Info 0:12d93184b350 62 // FPGA enable
Info 0:12d93184b350 63 fpga_write(FPGA_I2C_ADR,FPGA_ENABLE,1);
Info 0:12d93184b350 64 }
Info 0:12d93184b350 65
Info 0:12d93184b350 66 void I2Cwrite(char addr, char data){
Info 0:12d93184b350 67
Info 0:12d93184b350 68 char cmd[1];
Info 0:12d93184b350 69 cmd[0] = data;
Info 0:12d93184b350 70 fpga_i2c.write(addr, cmd, 1);
Info 0:12d93184b350 71 //wait(0.01);
Info 0:12d93184b350 72
Info 0:12d93184b350 73 }
Info 0:12d93184b350 74
Info 0:12d93184b350 75 void changeBank(uint8_t bank){
Info 0:12d93184b350 76 fpga_write(FPGA_I2C_ADR,0x10,bank);
Info 0:12d93184b350 77 }
Info 0:12d93184b350 78
Info 0:12d93184b350 79 void deleteBank(uint8_t bank){
Info 0:12d93184b350 80 fpga_write(FPGA_I2C_ADR,0x0c,(bank|0x80));
Info 0:12d93184b350 81 fpga_write(FPGA_I2C_ADR,0x10, bank);
Info 0:12d93184b350 82 }
Info 0:12d93184b350 83 /*
Info 0:12d93184b350 84 void startReg(uint8_t bank){
Info 0:12d93184b350 85 fpga_write(FPGA_I2C_ADR,0x0c,bank);
Info 0:12d93184b350 86 }
Info 0:12d93184b350 87
Info 0:12d93184b350 88 void endReg(uint8_t bank){
Info 0:12d93184b350 89 fpga_write(FPGA_I2C_ADR,0x10, (bank|0x04));
Info 0:12d93184b350 90 }
Info 0:12d93184b350 91 */
Info 0:12d93184b350 92
Info 0:12d93184b350 93