This commit is contained in:
MikuQ.com 2016-06-02 23:15:45 +08:00
parent 1801a9b2b8
commit 9ef8b45408
13 changed files with 247 additions and 363 deletions

View File

@ -2,6 +2,7 @@
int main()
{
mikuPiSetup();
setup();
while(1)
loop();

View File

@ -4,6 +4,35 @@
#include <stdio.h>
#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();

157
MikuPi.c
View File

@ -2,21 +2,73 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#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);
}

View File

@ -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

BIN
examples/blink Executable file

Binary file not shown.

27
examples/blink.c Executable file
View File

@ -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);
}

View File

@ -1,174 +0,0 @@
/*
* gpio_lib.c
*
* Copyright 2013 Stefan Mavrodiev <support@olimex.com>
*
* 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 <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <pthread.h>
#include <unistd.h>
#include <sched.h>
#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);
}

View File

@ -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

View File

@ -1,6 +0,0 @@
#include <stdio.h>
int main()
{
printf("Hello MikuQ.com!\n");
return 0;
}

View File

@ -1,7 +0,0 @@
#include "MikuPi.h"
int main()
{
sayhello();
return 0;
}

View File

@ -1,15 +0,0 @@
#include "MikuDuino.h"
int i=0;
void setup()
{
sayhello();
}
void loop()
{
delay(5000);
printf("%d\n",++i);
}

View File

@ -1,98 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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;
}

Binary file not shown.