diff --git a/MikuPi.h b/MikuPi.h index 57190b0..bcd6811 100755 --- a/MikuPi.h +++ b/MikuPi.h @@ -6,6 +6,19 @@ #define FALSE (1==2) #endif +//https://www.arduino.cc/en/Reference/Constants + +#define true (1) +#define false (0) + +#define HIGH (1) +#define LOW (0) + +#define INPUT (0) +#define INPUT_PULLUP (2) +#define OUTPUT (1) + + #define PI_MODEL_UNKNOWN 0 #define PI_MODEL_M1 1 #define PI_MODEL_R1 2 @@ -14,7 +27,7 @@ #define PI_MODEL_M3 5 #define PI_MODEL_M2p 6 -#define VERSION "0.20" +#define VERSION "0.21" extern const char *piModelNames [7] ; @@ -23,4 +36,7 @@ void delay (unsigned int howLong); void piBoardId(int *model, int *mem); void sayhello(); +void pinMode(int pin, int mode); +void digitalWrite(pin, value); + #endif diff --git a/README.md b/README.md index daf06da..51851a4 100755 --- a/README.md +++ b/README.md @@ -1,6 +1,33 @@ -# MikuPi by MikuQ.com +# MikuPi by MikuQ.com

 gcc -Wall -o hello3.exe hello3.c -lMikuDuino
 ./hello3.exe
-
\ No newline at end of file + + + +-----+-----+---------+------+---+- MikuPi -+---+------+---------+-----+-----+ + | CPU | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | CPU | + +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ + | | | 3.3v | | | 1 || 2 | | | 5v | | | + | 229 | 8 | SDA.1 | IN | 0 | 3 || 4 | | | 5V | | | + | 228 | 9 | SCL.1 | IN | 0 | 5 || 6 | | | GND | | | + | 362 | 7 | GCLK | OUT | 1 | 7 || 8 | 0 | IN | TxD0 | 15 | 32 | + | | | GND | | | 9 || 10 | 0 | IN | RxD0 | 16 | 33 | + | 68 | 0 | GEN0 | ALT3 | 0 | 11 || 12 | 0 | IN | GEN1 | 1 | 35 | + | 71 | 2 | GEN2 | ALT3 | 0 | 13 || 14 | | | GND | | | + | 81 | 3 | GEN3 | ALT3 | 0 | 15 || 16 | 0 | IN | GEN4 | 4 | 34 | + | | | 3.3v | | | 17 || 18 | 1 | OUT | GEN5 | 5 | 360 | + | 64 | 12 | MOSI | ALT4 | 0 | 19 || 20 | | | GND | | | + | 65 | 13 | MISO | ALT4 | 0 | 21 || 22 | 1 | OUT | GEN6 | 6 | 361 | + | 66 | 14 | SCLK | ALT4 | 0 | 23 || 24 | 0 | ALT4 | CE0 | 10 | 67 | + | | | GND | | | 25 || 26 | 0 | IN | CE1 | 11 | 234 | + | 227 | 30 | SDA.0 | IN | 0 | 27 || 28 | 0 | IN | SCL.0 | 31 | 226 | + | 82 | 21 | GPIO.21 | ALT3 | 0 | 29 || 30 | | | GND | | | + | 202 | 22 | GPIO.22 | ALT2 | 0 | 31 || 32 | 0 | ALT3 | GPIO.26 | 26 | 205 | + | 203 | 23 | GPIO.23 | ALT3 | 0 | 33 || 34 | | | GND | | | + | 204 | 24 | GPIO.24 | OUT | 0 | 35 || 36 | 0 | ALT3 | GPIO.27 | 27 | 133 | + | 132 | 25 | GPIO.25 | ALT3 | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 146 | + | | | GND | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 147 | + +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ + | CPU | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | CPU | + +-----+-----+---------+------+---+- BPI-M3 -+---+------+---------+-----+-----+ \ No newline at end of file diff --git a/test/gpio_lib.c b/test/gpio_lib.c new file mode 100755 index 0000000..798f98e --- /dev/null +++ b/test/gpio_lib.c @@ -0,0 +1,159 @@ +/* + * gpio_lib.c + * + * Copyright 2013 Stefan Mavrodiev + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "gpio_lib.h" + + +unsigned int SUNXI_PIO_BASE = 0; +static volatile long int *gpio_map = NULL; + +int sunxi_gpio_init(void) { + int fd; + unsigned int addr_start, addr_offset; + unsigned int PageSize, PageMask; + + + fd = open("/dev/mem", O_RDWR); + if(fd < 0) { + return SETUP_DEVMEM_FAIL; + } + + PageSize = sysconf(_SC_PAGESIZE); + PageMask = (~(PageSize-1)); + + addr_start = SW_PORTC_IO_BASE & PageMask; + addr_offset = SW_PORTC_IO_BASE & ~PageMask; + + gpio_map = (void *)mmap(0, PageSize*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, addr_start); + if(gpio_map == MAP_FAILED) { + return SETUP_MMAP_FAIL; + } + + SUNXI_PIO_BASE = (unsigned int)gpio_map; + SUNXI_PIO_BASE += addr_offset; + + close(fd); + return SETUP_OK; +} + +int sunxi_gpio_set_cfgpin(unsigned int pin, unsigned int val) { + + unsigned int cfg; + unsigned int bank = GPIO_BANK(pin); + unsigned int index = GPIO_CFG_INDEX(pin); + unsigned int offset = GPIO_CFG_OFFSET(pin); + + if(SUNXI_PIO_BASE == 0) { + return -1; + } + + struct sunxi_gpio *pio = + &((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank]; + + + cfg = *(&pio->cfg[0] + index); + cfg &= ~(0xf << offset); + cfg |= val << offset; + + *(&pio->cfg[0] + index) = cfg; + + return 0; +} + +int sunxi_gpio_get_cfgpin(unsigned int pin) { + + unsigned int cfg; + unsigned int bank = GPIO_BANK(pin); + unsigned int index = GPIO_CFG_INDEX(pin); + unsigned int offset = GPIO_CFG_OFFSET(pin); + if(SUNXI_PIO_BASE == 0) + { + return -1; + } + struct sunxi_gpio *pio = &((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank]; + cfg = *(&pio->cfg[0] + index); + cfg >>= offset; + return (cfg & 0xf); +} +int sunxi_gpio_output(unsigned int pin, unsigned int val) { + + unsigned int bank = GPIO_BANK(pin); + unsigned int num = GPIO_NUM(pin); + + if(SUNXI_PIO_BASE == 0) + { + return -1; + } + struct sunxi_gpio *pio =&((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank]; + + if(val) + *(&pio->dat) |= 1 << num; + else + *(&pio->dat) &= ~(1 << num); + + return 0; +} + +int sunxi_gpio_input(unsigned int pin) { + + unsigned int dat; + unsigned int bank = GPIO_BANK(pin); + unsigned int num = GPIO_NUM(pin); + + if(SUNXI_PIO_BASE == 0) + { + return -1; + } + + struct sunxi_gpio *pio =&((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank]; + + dat = *(&pio->dat); + dat >>= num; + + return (dat & 0x1); +} +void sunxi_gpio_cleanup(void) +{ + unsigned int PageSize; + if (gpio_map == NULL) + return; + + PageSize = sysconf(_SC_PAGESIZE); + munmap((void*)gpio_map, PageSize*2); +} + diff --git a/test/gpio_lib.h b/test/gpio_lib.h new file mode 100755 index 0000000..5deb936 --- /dev/null +++ b/test/gpio_lib.h @@ -0,0 +1,167 @@ +#ifndef _GPIO_LIB_H_ +#define _GPIO_LIB_H_ + + +#define SW_PORTC_IO_BASE 0x01c20800 + + +#define SUNXI_GPIO_A 0 +#define SUNXI_GPIO_B 1 +#define SUNXI_GPIO_C 2 +#define SUNXI_GPIO_D 3 +#define SUNXI_GPIO_E 4 +#define SUNXI_GPIO_F 5 +#define SUNXI_GPIO_G 6 +#define SUNXI_GPIO_H 7 +#define SUNXI_GPIO_I 8 +#define SUNXI_GPIO_J 9 +#define SUNXI_GPIO_K 10 +#define SUNXI_GPIO_L 11 + +#define SETUP_OK 0 +#define SETUP_DEVMEM_FAIL 1 +#define SETUP_MALLOC_FAIL 2 +#define SETUP_MMAP_FAIL 3 + +#define HIGH 1 +#define LOW 0 + +#define INPUT 0 +#define OUTPUT 1 +#define PER 2 + + +struct sunxi_gpio { + unsigned int cfg[4]; + unsigned int dat; + unsigned int drv[2]; + unsigned int pull[2]; +}; + +/* gpio interrupt control */ +struct sunxi_gpio_int { + unsigned int cfg[3]; + unsigned int ctl; + unsigned int sta; + unsigned int deb; +}; + +struct sunxi_gpio_reg { + struct sunxi_gpio gpio_bank[12]; + unsigned char res[0xbc]; + struct sunxi_gpio_int gpio_int; +}; + +#define GPIO_BANK(pin) ((pin) >> 5) +#define GPIO_NUM(pin) ((pin) & 0x1F) + +#define GPIO_CFG_INDEX(pin) (((pin) & 0x1F) >> 3) +#define GPIO_CFG_OFFSET(pin) ((((pin) & 0x1F) & 0x7) << 2) + +/* GPIO bank sizes */ +#define SUNXI_GPIO_A_NR (32) +#define SUNXI_GPIO_B_NR (32) +#define SUNXI_GPIO_C_NR (32) +#define SUNXI_GPIO_D_NR (32) +#define SUNXI_GPIO_E_NR (32) +#define SUNXI_GPIO_F_NR (32) +#define SUNXI_GPIO_G_NR (32) +#define SUNXI_GPIO_H_NR (32) +#define SUNXI_GPIO_I_NR (32) +#define SUNXI_GPIO_J_NR (32) +#define SUNXI_GPIO_K_NR (32) +#define SUNXI_GPIO_L_NR (32) + +#define SUNXI_GPIO_NEXT(__gpio) ((__gpio##_START)+(__gpio##_NR)+0) + +enum sunxi_gpio_number { + SUNXI_GPIO_A_START = 0, + SUNXI_GPIO_B_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_A), //32 + SUNXI_GPIO_C_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_B), //64 + SUNXI_GPIO_D_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_C), //96 + SUNXI_GPIO_E_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_D), //128 + SUNXI_GPIO_F_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_E), //160 + SUNXI_GPIO_G_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_F), //192 + SUNXI_GPIO_H_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_G), //224 + SUNXI_GPIO_I_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_H), //256 + SUNXI_GPIO_J_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_I), //192 + SUNXI_GPIO_K_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_J), //224 + SUNXI_GPIO_L_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_K) //256 +}; + +/* SUNXI GPIO number definitions */ +#define SUNXI_GPA(_nr) (SUNXI_GPIO_A_START + (_nr)) +#define SUNXI_GPB(_nr) (SUNXI_GPIO_B_START + (_nr)) +#define SUNXI_GPC(_nr) (SUNXI_GPIO_C_START + (_nr)) +#define SUNXI_GPD(_nr) (SUNXI_GPIO_D_START + (_nr)) +#define SUNXI_GPE(_nr) (SUNXI_GPIO_E_START + (_nr)) +#define SUNXI_GPF(_nr) (SUNXI_GPIO_F_START + (_nr)) +#define SUNXI_GPG(_nr) (SUNXI_GPIO_G_START + (_nr)) +#define SUNXI_GPH(_nr) (SUNXI_GPIO_H_START + (_nr)) +#define SUNXI_GPI(_nr) (SUNXI_GPIO_I_START + (_nr)) +#define SUNXI_GPJ(_nr) (SUNXI_GPIO_J_START + (_nr)) +#define SUNXI_GPK(_nr) (SUNXI_GPIO_K_START + (_nr)) +#define SUNXI_GPL(_nr) (SUNXI_GPIO_L_START + (_nr)) + +/* GPIO pin function config */ +#define SUNXI_GPIO_INPUT (0) +#define SUNXI_GPIO_OUTPUT (1) +#define SUNXI_GPIO_PER (2) + +#define SUNXI_GPA0_ERXD3 (2) +#define SUNXI_GPA0_SPI1_CS0 (3) +#define SUNXI_GPA0_UART2_RTS (4) + +#define SUNXI_GPA1_ERXD2 (2) +#define SUNXI_GPA1_SPI1_CLK (3) +#define SUNXI_GPA1_UART2_CTS (4) + +#define SUNXI_GPA2_ERXD1 (2) +#define SUNXI_GPA2_SPI1_MOSI (3) +#define SUNXI_GPA2_UART2_TX (4) + +#define SUNXI_GPA10_UART1_TX (4) +#define SUNXI_GPA11_UART1_RX (4) + +#define SUN4I_GPB22_UART0_TX (2) +#define SUN4I_GPB23_UART0_RX (2) + +#define SUN5I_GPG3_UART0_TX (4) +#define SUN5I_GPG4_UART0_RX (4) + +#define SUNXI_GPC2_NCLE (2) +#define SUNXI_GPC2_SPI0_CLK (3) + +#define SUNXI_GPC6_NRB0 (2) +#define SUNXI_GPC6_SDC2_CMD (3) + +#define SUNXI_GPC7_NRB1 (2) +#define SUNXI_GPC7_SDC2_CLK (3) + +#define SUNXI_GPC8_NDQ0 (2) +#define SUNXI_GPC8_SDC2_D0 (3) + +#define SUNXI_GPC9_NDQ1 (2) +#define SUNXI_GPC9_SDC2_D1 (3) + +#define SUNXI_GPC10_NDQ2 (2) +#define SUNXI_GPC10_SDC2_D2 (3) + +#define SUNXI_GPC11_NDQ3 (2) +#define SUNXI_GPC11_SDC2_D3 (3) + +#define SUNXI_GPF2_SDC0_CLK (2) +#define SUNXI_GPF2_UART0_TX (4) + +#define SUNXI_GPF4_SDC0_D3 (2) +#define SUNXI_GPF4_UART0_RX (4) + +extern int sunxi_gpio_input(unsigned int pin); +extern int sunxi_gpio_init(void); +extern int sunxi_gpio_set_cfgpin(unsigned int pin, unsigned int val); +extern int sunxi_gpio_get_cfgpin(unsigned int pin); +extern int sunxi_gpio_output(unsigned int pin, unsigned int val); +extern void sunxi_gpio_cleanup(void); + +extern unsigned int SUNXI_PIO_BASE; +#endif diff --git a/test/hello.exe b/test/hello.exe deleted file mode 100755 index 94946dc..0000000 Binary files a/test/hello.exe and /dev/null differ diff --git a/test/hello2.exe b/test/hello2.exe deleted file mode 100755 index 3273f00..0000000 Binary files a/test/hello2.exe and /dev/null differ diff --git a/test/hello3.exe b/test/hello3.exe deleted file mode 100755 index 73f7260..0000000 Binary files a/test/hello3.exe and /dev/null differ diff --git a/test/hello4.c b/test/hello4.c index c8062dd..ecf4de9 100755 --- a/test/hello4.c +++ b/test/hello4.c @@ -2,73 +2,96 @@ #include #include +#include "gpio_lib.h" +// pinToGpio: +// Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin +// Cope for 3 different board revisions here. +static int *wPinToGpio ; - -int isH3(void) +static int wPinToGpioM2p [32] = { - FILE *cpuFd ; - char line [120] ; - char *d; - int processorCount=0; - - if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL) - piBoardRevOops ("Unable to open /proc/cpuinfo") ; - while (fgets (line, 120, cpuFd) != NULL) - { - if (strncmp (line, "processor", 9) == 0) - processorCount++; - if (strncmp (line, "Hardware", 8) == 0) - break; - } - - fclose (cpuFd) ; - if (strncmp (line, "Hardware", 8) != 0) - piBoardRevOops ("No \"Hardware\" line") ; - - for (d = &line [strlen (line) - 1] ; (*d == '\n') || (*d == '\r') ; --d) - *d = 0 ; - if (MikuPiDebug) - printf ("piboardRev: Hardware string: %s\n", line) ; - - if (strstr(line,"sun8i") != NULL) - { - if (processorCount==4) - { - //H3-M2P - } - if (processorCount==8) - { - //A83T-M3 - } - if (MikuPiDebug) - { - printf ("Hardware:%s\n",line) ; - printf ("processorCount:%d\n",processorCount) ; - } - return TRUE; - } - else - { - if (MikuPiDebug) - printf ("Hardware:%s\n",line) ; - return FALSE; - } -} + 1,16,0,3, 15,68,2,6, + 12,11,67,71, 64,65,66,13, + 14,-1,-1,-1, -1,7,8,9, + 10,17,354,356, 21,20,19,18 +} ; +static int wPinToGpioM3 [32] = +{ + 68,35,71,81, 34,360,361,362, + 229,228,67,234, 64,65,66,32, + 33,-1,-1,-1, -1,82,202,203, + 204,132,205,133, 146,147,227,226 +} ; +//static int *bPinTowPin; +static int bPinTowPin[41] = +{ + -1, // 0 + -1, -1, // 1, 2 + 8, -1, //3, 4 + 9, -1, //5, 6 + 7, 15, //7, 8 + -1, 16, //9,10 + 0, 1, //11,12 + 2, -1, //13,14 + 3, 4, //15,16 + -1, 5, //17,18 + 12, -1, //19,20 + 13, 6, //21,22 + 14, 10, //23, 24 + -1, 11, // 25, 26 + + 30, 31, //27, 28 + 21, -1, //29, 30 + 22, 26, //31, 32 + 23, -1, //33, 34 + 24, 27, //35, 36 + 25, 28, //37, 38 + -1, 29, //39, 40 +} ; +//#define PA6 SUNXI_GPA(6) *32+6 +#define PA6 wPinToGpioM2p[bPinTowPin[36]] int main() { + /* + if (geteuid () != 0) + { + printf("wiringPiSetup: Must be root. (Did you forget sudo?)\n") ; + return -1; + } + */ + if(SETUP_OK!=sunxi_gpio_init()){ + printf("Failed to initialize GPIO\n"); + return -1; + } + + if(SETUP_OK!=sunxi_gpio_set_cfgpin(PA6,OUTPUT)){ + printf("Failed to config GPIO pin\n"); + return -1; + } + + int i,j; + while(1) { + if(sunxi_gpio_output(PA6,HIGH)){ + printf("Failed to set GPIO pin value\n"); + return -1; + } + + usleep(500000); + if(sunxi_gpio_output(PA6,LOW)){ + printf("Failed to set GPIO pin value\n"); + return -1; + } + usleep(500000); + +} + sunxi_gpio_cleanup(); - /* - if (isH3()) - printf("is H3!\n"); - else - printf("is not H3!\n"); - */ return 0; } \ No newline at end of file diff --git a/test/hello4.exe b/test/hello4.exe index 3d74356..7791612 100755 Binary files a/test/hello4.exe and b/test/hello4.exe differ