TS-7670 DIO: Difference between revisions
(Initial adding of pins, needs review and fixes) |
No edit summary |
||
Line 1: | Line 1: | ||
This board uses CPU DIO, typically there are 4 functions associated with each physical pin (I2C, PWM, SPI, DIO, etc). See the [http://cache.freescale.com/files/dsp/doc/ref_manual/MCIMX28RM.pdf CPU manual] for the complete listing and for information on how to control these DIO.<br><br> | This board uses CPU DIO, typically there are 4 functions associated with each physical pin (I2C, PWM, SPI, DIO, etc). See the [http://cache.freescale.com/files/dsp/doc/ref_manual/MCIMX28RM.pdf CPU manual] for the complete listing and for information on how to control these DIO.<br><br> | ||
Note that most of the pins start in their "Function" mode and must be set to DIO mode to be able to be used as DIO. Be aware that doing this on a running system may have adverse effects if there are kernel drivers trying to use specific pins in their peripheral mode(s).<br><br> | Note that most of the pins start in their "Function" mode and must be set to DIO mode to be able to be used as DIO. Be aware that doing this on a running system may have adverse effects if there are kernel drivers trying to use specific pins in their peripheral mode(s).<br><br> | ||
DIO numbers are referenced in bank_pin notation in the table below. This makes associating pins with DIO registers much simpler when looking at the manual. The example code below is also based around using the bank and pin notation.<br> | |||
<br> | <br> | ||
Line 35: | Line 35: | ||
|- | |- | ||
| 1_13 | | 1_13 | ||
| | | N/A | ||
| EN_MODBUS_24V | | EN_MODBUS_24V | ||
|- | |- | ||
| 1_15 | | 1_15 | ||
| | | N/A | ||
| EN_MODBUS_3V# | | EN_MODBUS_3V# | ||
|- | |- | ||
| 1_22 | | 1_22 | ||
| J5_7 | | J5_7 | ||
| UART1_CTS<ref>Not peripheral controlled</ref> | | UART1_CTS<ref name="ctsrts">Not peripheral controlled</ref> | ||
|- | |- | ||
| 1_23 | | 1_23 | ||
| J5_8 | | J5_8 | ||
| UART1_RTS<ref | | UART1_RTS<ref name="ctsrts"/> | ||
|- | |- | ||
| 1_27 | | 1_27 | ||
| | | N/A | ||
| EN_HOST_USB_5V | | EN_HOST_USB_5V | ||
|- | |- | ||
| 3_30 | | 3_30 | ||
| | | N/A | ||
| BLUE_LED | | BLUE_LED | ||
|- | |- | ||
| 1_26 | | 1_26 | ||
| | | N/A | ||
| YEL_LED# | | YEL_LED# | ||
|- | |- | ||
| 0_17 | | 0_17 | ||
| | | N/A | ||
| RED_LED# | | RED_LED# | ||
|- | |- | ||
| 0_28 | | 0_28 | ||
| | | N/A | ||
| GREEN_LED# | | GREEN_LED# | ||
|- | |- | ||
Line 100: | Line 88: | ||
| 2_18 | | 2_18 | ||
| HD1_10 | | HD1_10 | ||
| UART3_RXD <ref> Input only, 5v tolerant</ref> | | UART3_RXD <ref name="rxd">Input only, 5v tolerant</ref> | ||
|- | |- | ||
| 2_19 | | 2_19 | ||
Line 108: | Line 96: | ||
| 2_16 | | 2_16 | ||
| HD2_3 | | HD2_3 | ||
| UART2_RXD <ref | | UART2_RXD <ref name="rxd"/> | ||
|- | |- | ||
| 2_17 | | 2_17 | ||
| HD2_1 | | HD2_1 | ||
| UART2_TXD | | UART2_TXD | ||
|} | |} | ||
<references/> | |||
Example code for DIO manipulation: | |||
<source lang="c"> | |||
#include <assert.h> | |||
#include <fcntl.h> | |||
#include <string.h> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <sys/mman.h> | |||
#include <sys/stat.h> | |||
#include <sys/types.h> | |||
#include <unistd.h> | |||
#define DIO_Z 2 | |||
volatile unsigned int *pinctl = NULL; | |||
/******************************************************************************* | |||
* setdiopin: accepts a DIO register and value to place in that DIO pin. | |||
* Values can be 0 (low), 1 (high), or 2 (z - high impedance). | |||
*******************************************************************************/ | |||
void setdiopin(int bank, int pin, int val) | |||
{ | |||
if(val == 2) { | |||
pinctl[((0xB00) + (0x10 * bank) + 0x8)/4] = (0x1 << pin); | |||
} else { | |||
pinctl[((0x700) + (0x10 * bank) + (0x8 / (val+1)))/4] = | |||
(0x1 << pin); | |||
pinctl[((0xB00) + (0x10 * bank) + 0x4)/4] = (0x1 << pin); | |||
} | |||
} | |||
/******************************************************************************* | |||
* getdiopin: accepts a DIO pin number and returns its value. | |||
*******************************************************************************/ | |||
int getdiopin(int bank, int pin) | |||
{ | |||
return (((pinctl[((0x900) + (0x10 * bank))/4]) >> pin) & 0x1); | |||
} | |||
/******************************************************************************* | |||
* Main: accept input from the command line and act accordingly. | |||
*******************************************************************************/ | |||
int main(int argc, char **argv) | |||
{ | |||
int devmem = 0; | |||
int pin, bank, reg = 0, muxpin; | |||
int val; | |||
int returnedValue; | |||
// Check for invalid command line arguments | |||
if ((argc > 4) | (argc < 3)) { | |||
printf("Usage: %s bank pin [0|1|2]>\n", argv[0]); | |||
return 1; | |||
} | |||
// We only want to get val if there are more than 3 command line arguments | |||
if (argc == 3) { | |||
bank = strtoul(argv[1], NULL, 0); | |||
pin = strtoul(argv[2], NULL, 0); | |||
val = 0; | |||
} | |||
else { | |||
bank = strtoul(argv[1], NULL, 0); | |||
pin = strtoul(argv[2], NULL, 0); | |||
val = strtoul(argv[3], NULL, 0); | |||
} | |||
assert(bank >= 0 && bank < 5); | |||
assert(pin >= 0 && pin < 32); | |||
assert(val >=0 && val < 3); | |||
devmem = open("/dev/mem", O_RDWR|O_SYNC); | |||
pinctl = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, devmem, 0x80018000); | |||
/* We determine what muxsel reg we need, and set the right pin to DIO*/ | |||
if(pin > 15) { | |||
reg = 1; | |||
muxpin = pin - 16; | |||
} else muxpin = pin; | |||
pinctl[((0x100) + (0x20 * bank) + (0x10 * reg) + 0x4)/4] = (0x3 << (muxpin * 2)); | |||
// Parse through the command line arguments, check for valid inputs, and exec | |||
if (argc == 3) { | |||
returnedValue = getdiopin(bank, pin); | |||
printf("dio_%d_%d=%d\n", bank, pin, returnedValue); | |||
} else if(argc == 4) { | |||
setdiopin(bank, pin, val); | |||
} | |||
return 0; | |||
} | |||
</source> |
Revision as of 13:45, 3 October 2013
This board uses CPU DIO, typically there are 4 functions associated with each physical pin (I2C, PWM, SPI, DIO, etc). See the CPU manual for the complete listing and for information on how to control these DIO.
Note that most of the pins start in their "Function" mode and must be set to DIO mode to be able to be used as DIO. Be aware that doing this on a running system may have adverse effects if there are kernel drivers trying to use specific pins in their peripheral mode(s).
DIO numbers are referenced in bank_pin notation in the table below. This makes associating pins with DIO registers much simpler when looking at the manual. The example code below is also based around using the bank and pin notation.
DIO | Location | Function |
---|---|---|
1_7 | HD1_14 | DC_DIO_4 |
1_8 | HD1_9 | DC_DIO_5 |
1_9 | HD1_7 | DC_DIO_6 |
1_10 | HD1_1 | DC_DIO_7 |
1_11 | HD2_4 | DC_DIO_8 |
1_12 | HD2_6 | DC_DIO_9 |
1_13 | N/A | EN_MODBUS_24V |
1_15 | N/A | EN_MODBUS_3V# |
1_22 | J5_7 | UART1_CTS[1] |
1_23 | J5_8 | UART1_RTS[1] |
1_27 | N/A | EN_HOST_USB_5V |
3_30 | N/A | BLUE_LED |
1_26 | N/A | YEL_LED# |
0_17 | N/A | RED_LED# |
0_28 | N/A | GREEN_LED# |
3_29 | HD1_2 | M0_SPI_CS# |
3_20 | HD1_8 | M0_SPI_MOSI |
3_21 | HD1_6 | M0_SPI_MISO |
3_23 | HD1_4 | M0_SPI_CLK |
2_18 | HD1_10 | UART3_RXD [2] |
2_19 | HD1_12 | UART3_TXD |
2_16 | HD2_3 | UART2_RXD [2] |
2_17 | HD2_1 | UART2_TXD |
Example code for DIO manipulation:
#include <assert.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define DIO_Z 2
volatile unsigned int *pinctl = NULL;
/*******************************************************************************
* setdiopin: accepts a DIO register and value to place in that DIO pin.
* Values can be 0 (low), 1 (high), or 2 (z - high impedance).
*******************************************************************************/
void setdiopin(int bank, int pin, int val)
{
if(val == 2) {
pinctl[((0xB00) + (0x10 * bank) + 0x8)/4] = (0x1 << pin);
} else {
pinctl[((0x700) + (0x10 * bank) + (0x8 / (val+1)))/4] =
(0x1 << pin);
pinctl[((0xB00) + (0x10 * bank) + 0x4)/4] = (0x1 << pin);
}
}
/*******************************************************************************
* getdiopin: accepts a DIO pin number and returns its value.
*******************************************************************************/
int getdiopin(int bank, int pin)
{
return (((pinctl[((0x900) + (0x10 * bank))/4]) >> pin) & 0x1);
}
/*******************************************************************************
* Main: accept input from the command line and act accordingly.
*******************************************************************************/
int main(int argc, char **argv)
{
int devmem = 0;
int pin, bank, reg = 0, muxpin;
int val;
int returnedValue;
// Check for invalid command line arguments
if ((argc > 4) | (argc < 3)) {
printf("Usage: %s bank pin [0|1|2]>\n", argv[0]);
return 1;
}
// We only want to get val if there are more than 3 command line arguments
if (argc == 3) {
bank = strtoul(argv[1], NULL, 0);
pin = strtoul(argv[2], NULL, 0);
val = 0;
}
else {
bank = strtoul(argv[1], NULL, 0);
pin = strtoul(argv[2], NULL, 0);
val = strtoul(argv[3], NULL, 0);
}
assert(bank >= 0 && bank < 5);
assert(pin >= 0 && pin < 32);
assert(val >=0 && val < 3);
devmem = open("/dev/mem", O_RDWR|O_SYNC);
pinctl = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, devmem, 0x80018000);
/* We determine what muxsel reg we need, and set the right pin to DIO*/
if(pin > 15) {
reg = 1;
muxpin = pin - 16;
} else muxpin = pin;
pinctl[((0x100) + (0x20 * bank) + (0x10 * reg) + 0x4)/4] = (0x3 << (muxpin * 2));
// Parse through the command line arguments, check for valid inputs, and exec
if (argc == 3) {
returnedValue = getdiopin(bank, pin);
printf("dio_%d_%d=%d\n", bank, pin, returnedValue);
} else if(argc == 4) {
setdiopin(bank, pin, val);
}
return 0;
}