diff --git a/MikuDuino.c b/MikuDuino.c index 39221da..62bc921 100755 --- a/MikuDuino.c +++ b/MikuDuino.c @@ -2,6 +2,7 @@ int main() { + mikuPiSetup(); setup(); while(1) loop(); diff --git a/MikuDuino.h b/MikuDuino.h index cf0e70b..8c9c70e 100755 --- a/MikuDuino.h +++ b/MikuDuino.h @@ -4,6 +4,35 @@ #include #include "MikuPi.h" +#define D3 bPinTowPin[3] +#define D5 bPinTowPin[5] +#define D7 bPinTowPin[7] +#define D8 bPinTowPin[8] +#define D10 bPinTowPin[10] +#define D11 bPinTowPin[11] +#define D12 bPinTowPin[12] +#define D13 bPinTowPin[13] +#define D15 bPinTowPin[15] +#define D16 bPinTowPin[16] +#define D18 bPinTowPin[18] +#define D19 bPinTowPin[19] +#define D21 bPinTowPin[21] +#define D22 bPinTowPin[22] +#define D23 bPinTowPin[23] +#define D24 bPinTowPin[24] +#define D26 bPinTowPin[26] +#define D27 bPinTowPin[27] +#define D28 bPinTowPin[28] +#define D29 bPinTowPin[29] +#define D31 bPinTowPin[31] +#define D32 bPinTowPin[32] +#define D33 bPinTowPin[33] +#define D35 bPinTowPin[35] +#define D36 bPinTowPin[36] +#define D37 bPinTowPin[37] +#define D38 bPinTowPin[38] +#define D40 bPinTowPin[40] + void setup(); void loop(); diff --git a/MikuPi.c b/MikuPi.c index bc58888..a92920e 100755 --- a/MikuPi.c +++ b/MikuPi.c @@ -2,21 +2,73 @@ #include #include #include +#include +#include +#include + #include "MikuPi.h" const char *piModelNames [7] = { "Unknown", - "BPI-M1", - "BPI-R1", - "BPI-M2", + "BPI-M1 ", + "BPI-R1 ", + "BPI-M2 ", "BPI-M1+", - "BPI-M3", + "BPI-M3 ", "BPI-M2+", } ; int MikuPiDebug = FALSE; +static int *wPinToGpio ; + +static int wPinToGpioM2p [32] = +{ + 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 +} ; + +const 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 +} ; + +unsigned int SUNXI_PIO_BASE = 0; +unsigned int SUNXI_PIO_LM_BASE = 0; +static volatile long int *gpio_map = NULL; + void delay(unsigned int howLong) { struct timespec sleeper, dummy ; @@ -29,7 +81,7 @@ void delay(unsigned int howLong) static void piBoardRevOops (const char *why) { - fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ; + //fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ; fprintf (stderr, " -> %s\n", why) ; fprintf (stderr, " -> You may want to check:\n") ; fprintf (stderr, " -> http://MikuQ.com\n") ; @@ -121,3 +173,98 @@ void sayhello() piModelNames [model], mem) ; } } + +void mikuPiSetup (void) +{ + int fd; + unsigned int addr_start, addr_offset; + unsigned int PageSize, PageMask; + + if (geteuid () != 0) + piBoardRevOops("MikuPiSetup: Must be root. (Did you forget sudo?)"); + + int model, mem; + piBoardId (&model, &mem); + if (model == PI_MODEL_UNKNOWN) + { + printf ("Your Pi has an unknown model type. Please report this to\n") ; + printf (" i@mikuq.com\n") ; + piBoardRevOops ("with a copy of your /proc/cpuinfo if possible") ; + } + if (model == PI_MODEL_M2p) + { + wPinToGpio=wPinToGpioM2p; + } + if (model == PI_MODEL_M3) + { + wPinToGpio=wPinToGpioM3; + } + + fd = open("/dev/mem", O_RDWR); + + 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); + + SUNXI_PIO_BASE = (unsigned int)gpio_map; + SUNXI_PIO_BASE += addr_offset; + + addr_start = SW_PORTL_IO_BASE & PageMask; + addr_offset = SW_PORTL_IO_BASE & ~PageMask; + gpio_map = (void *)mmap(0, PageSize*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, addr_start); + + SUNXI_PIO_LM_BASE = (unsigned int)gpio_map; + SUNXI_PIO_LM_BASE += addr_offset; + + close(fd); +} + +void pinMode(int pin, int mode) +{ + pin=wPinToGpio[pin]; + unsigned int val = mode; + 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; + + struct sunxi_gpio *pio; + if(bank>10) + pio = &((struct sunxi_gpio_reg *)SUNXI_PIO_LM_BASE)->gpio_bank[bank-11]; + else + 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; +} + +void digitalWrite(int pin, int value) +{ + pin=wPinToGpio[pin]; + unsigned int val = value; + unsigned int bank = GPIO_BANK(pin); + unsigned int num = GPIO_NUM(pin); + + if(SUNXI_PIO_BASE == 0) + return; + + struct sunxi_gpio *pio; + if(bank>10) + pio = &((struct sunxi_gpio_reg *)SUNXI_PIO_LM_BASE)->gpio_bank[bank-11]; + else + pio = &((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank]; + + if(val) + *(&pio->dat) |= 1 << num; + else + *(&pio->dat) &= ~(1 << num); +} diff --git a/MikuPi.h b/MikuPi.h index bcd6811..3086001 100755 --- a/MikuPi.h +++ b/MikuPi.h @@ -27,6 +27,8 @@ #define PI_MODEL_M3 5 #define PI_MODEL_M2p 6 +extern const int bPinTowPin[41]; + #define VERSION "0.21" extern const char *piModelNames [7] ; @@ -34,9 +36,43 @@ extern const char *piModelNames [7] ; void delay (unsigned int howLong); void piBoardId(int *model, int *mem); -void sayhello(); +void sayHello(); + +void mikuPiSetup (void); void pinMode(int pin, int mode); -void digitalWrite(pin, value); +void digitalWrite(int pin,int value); + +#define SW_PORTC_IO_BASE 0x01c20800 +#define SW_PORTL_IO_BASE 0x01f02c00 + +extern unsigned int SUNXI_PIO_BASE; +extern unsigned int SUNXI_PIO_LM_BASE; + +struct sunxi_gpio { + unsigned int cfg[4]; + unsigned int dat; + unsigned int drv[2]; + unsigned int pull[2]; +}; + +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[9]; + 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) #endif diff --git a/examples/blink b/examples/blink new file mode 100755 index 0000000..0de6d35 Binary files /dev/null and b/examples/blink differ diff --git a/examples/blink.c b/examples/blink.c new file mode 100755 index 0000000..a5e77b1 --- /dev/null +++ b/examples/blink.c @@ -0,0 +1,27 @@ +/* + * blink.c: + * + * Welcome to MikuQ.com! MikuDuino for BananaPi + * + * by MikuQ(i@mikuq.com) 2016-6-2 + * + * https://github.com/bpiq/MikuPi + * + */ + +#include "MikuDuino.h" + +#define LED D7 + +void setup() +{ + pinMode (LED, OUTPUT) ; +} + +void loop() +{ + digitalWrite (LED, HIGH) ; + delay(500); + digitalWrite (LED, LOW) ; + delay(500); +} diff --git a/test/gpio_lib.c b/test/gpio_lib.c deleted file mode 100755 index 8f7a33c..0000000 --- a/test/gpio_lib.c +++ /dev/null @@ -1,174 +0,0 @@ -/* - * 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; -unsigned int SUNXI_PIO_LM_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; - - addr_start = SW_PORTL_IO_BASE & PageMask; - addr_offset = SW_PORTL_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_LM_BASE = (unsigned int)gpio_map; - SUNXI_PIO_LM_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; - if(bank>10) - pio = &((struct sunxi_gpio_reg *)SUNXI_PIO_LM_BASE)->gpio_bank[bank-11]; - else - 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; - if(bank>10) - pio = &((struct sunxi_gpio_reg *)SUNXI_PIO_LM_BASE)->gpio_bank[bank-11]; - else - 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 deleted file mode 100755 index 975ace2..0000000 --- a/test/gpio_lib.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef _GPIO_LIB_H_ -#define _GPIO_LIB_H_ - -#define SW_PORTC_IO_BASE 0x01c20800 -#define SW_PORTL_IO_BASE 0x01f02c00 - -#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[9]; - 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) - -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; -extern unsigned int SUNXI_PIO_LM_BASE; -#endif diff --git a/test/hello.c b/test/hello.c deleted file mode 100755 index fc7687d..0000000 --- a/test/hello.c +++ /dev/null @@ -1,6 +0,0 @@ -#include -int main() -{ - printf("Hello MikuQ.com!\n"); - return 0; -} diff --git a/test/hello2.c b/test/hello2.c deleted file mode 100755 index 3a80785..0000000 --- a/test/hello2.c +++ /dev/null @@ -1,7 +0,0 @@ -#include "MikuPi.h" - -int main() -{ - sayhello(); - return 0; -} diff --git a/test/hello3.c b/test/hello3.c deleted file mode 100755 index 005d04e..0000000 --- a/test/hello3.c +++ /dev/null @@ -1,15 +0,0 @@ -#include "MikuDuino.h" - -int i=0; - -void setup() -{ - sayhello(); -} - -void loop() -{ - delay(5000); - printf("%d\n",++i); -} - diff --git a/test/hello4.c b/test/hello4.c deleted file mode 100755 index 9135a27..0000000 --- a/test/hello4.c +++ /dev/null @@ -1,98 +0,0 @@ -#include -#include -#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 ; - -static int wPinToGpioM2p [32] = -{ - 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; git add . git commit -a -m "gpio test" git push origin master - -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[32]] - -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; - } - - 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(); - - return 0; -} \ No newline at end of file diff --git a/test/hello4.exe b/test/hello4.exe deleted file mode 100755 index 665d0a2..0000000 Binary files a/test/hello4.exe and /dev/null differ