diff --git a/Arduino.h b/Arduino.h new file mode 100644 index 0000000..99cd368 --- /dev/null +++ b/Arduino.h @@ -0,0 +1,112 @@ +/* + Arduino.h - Main include file for the Arduino SDK + Copyright (c) 2005-2013 Arduino Team. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Modified 20 Aug 2014 by MediaTek Inc. + +*/ + +#ifndef Arduino_h +#define Arduino_h + +#include +#include +#include +#include + +// some libraries and sketches depend on this +// AVR stuff, assuming Arduino.h or WProgram.h +// automatically includes it... +//#include + + +#include "vmdcl.h" + +#ifdef __cplusplus +extern "C"{ +#endif // __cplusplus + +#include "wiring_constants.h" + +void yield(void); + +/* sketch */ +extern void setup( void ) ; +extern void loop( void ) ; + + +extern boolean changePinType(uint32_t ulPin, uint32_t ulPinType, VM_DCL_HANDLE* handle); +extern void spiPinsRest(void); +extern void setPinHandle(uint32_t ulPin, VM_DCL_HANDLE handle); + +typedef enum _EExt_Interrupts +{ + EXTERNAL_INT_0=0, + EXTERNAL_INT_1=1, + EXTERNAL_NUM_INTERRUPTS +} EExt_Interrupts ; + + +typedef void (*voidFuncPtr)( void ) ; + +#define PIO_MAX_NUM 19 + +typedef enum _EPioType +{ + PIO_DIGITAL, + PIO_ANALOG, + PIO_EINT, + PIO_PWM, + PIO_SPI, + PIO_UART, + PIO_I2C, + PIO_SD, + PIO_END +} EPioType ; + +/* Types used for the tables below */ +typedef struct _PinDescription +{ + VM_DCL_HANDLE ulHandle; + uint32_t ulGpioId ; + EPioType ulPinType ; + uint32_t ulPupd; +} PinDescription ; + +/* Pins table to be instanciated into variant.cpp */ +extern PinDescription g_APinDescription[] ; + +#ifdef __cplusplus +} // extern "C" + + +#endif // __cplusplus + +// Include board variant +#include "variant.h" + + + +#define USB_PID_LEONARDO 0x0034 +#define USB_PID_MICRO 0x0035 +#define USB_PID_DUE 0x003E + +boolean noStopInterrupts(void); +extern void spiPinsRest(void); +extern void setPinHandle(uint32_t ulPin, VM_DCL_HANDLE handle); + +#endif // Arduino_h diff --git a/Makefile b/Makefile index 634359c..98631e3 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ # GCC_BIN = ../LinkIt-1.1-1.60/hardware/tools/gcc-arm-none-eabi-4.8.3-2014q1/bin/ PROJECT = lua -OBJECTS = main.o syscalls_mtk.o console.o shell.o laudiolib.o lgsmlib.o timer.o +OBJECTS = main.o syscalls_mtk.o console.o shell.o laudiolib.o lgsmlib.o timer.o variant.o wiring_digital.o gpio.o OBJECTS += lua/linenoise.o lua/lapi.o lua/lcode.o lua/ldebug.o lua/ldo.o lua/ldump.o lua/lfunc.o lua/lgc.o lua/llex.o lua/lmem.o \ lua/lobject.o lua/lopcodes.o lua/lparser.o lua/lstate.o lua/lstring.o lua/ltable.o lua/ltm.o \ lua/lundump.o lua/lvm.o lua/lzio.o lua/lrotable.o \ diff --git a/gpio.c b/gpio.c new file mode 100644 index 0000000..a84d1c8 --- /dev/null +++ b/gpio.c @@ -0,0 +1,80 @@ + +#include "Arduino.h" +#include "wiring_digital.h" + +#include "lua.h" +#include "lauxlib.h" + + +int gpio_mode(lua_State *L) +{ + int pin = luaL_checkinteger(L, 1); + int mode = luaL_checkinteger(L, 2); + + pinMode(pin, mode); + + return 0; +} + +int gpio_read(lua_State *L) +{ + int pin = luaL_checkinteger(L, 1); + + lua_pushnumber(L, digitalRead(pin)); + + return 1; +} + +int gpio_write(lua_State *L) +{ + int pin = luaL_checkinteger(L, 1); + int value = luaL_checkinteger(L, 2); + + digitalWrite(pin, value); + + return 0; +} + + +#undef MIN_OPT_LEVEL +#define MIN_OPT_LEVEL 0 +#include "lrodefs.h" + +#define MOD_REG_NUMBER(L, name, value) lua_pushnumber(L, value); \ + lua_setfield(L, -2, name); + +const LUA_REG_TYPE gpio_map[] = +{ + {LSTRKEY("mode"), LFUNCVAL(gpio_mode)}, + {LSTRKEY("read"), LFUNCVAL(gpio_read)}, + {LSTRKEY("write"), LFUNCVAL(gpio_write)}, +#if LUA_OPTIMIZE_MEMORY > 0 + { LSTRKEY( "OUTPUT" ), LNUMVAL( OUTPUT ) }, + { LSTRKEY( "INPUT" ), LNUMVAL( INPUT ) }, + { LSTRKEY( "HIGH" ), LNUMVAL( HIGH ) }, + { LSTRKEY( "LOW" ), LNUMVAL( LOW ) }, + { LSTRKEY( "INPUT_PULLUP" ), LNUMVAL( INPUT_PULLUP ) }, +#endif + {LNILKEY, LNILVAL} +}; + +LUALIB_API int luaopen_gpio(lua_State *L) +{ + lua_register(L, "pinMode", gpio_mode); + lua_register(L, "digitalRead", gpio_read); + lua_register(L, "digitalWrite", gpio_write); + +#if LUA_OPTIMIZE_MEMORY > 0 + return 0; +#else // #if LUA_OPTIMIZE_MEMORY > 0 + + luaL_register(L, "gpio", gpio_map); + // Add constants + MOD_REG_NUMBER( L, "OUTPUT", OUTPUT ); + MOD_REG_NUMBER( L, "INPUT", INPUT ); + MOD_REG_NUMBER( L, "HIGH", HIGH ); + MOD_REG_NUMBER( L, "LOW", LOW ); + MOD_REG_NUMBER( L, "INPUT_PULLUP", INPUT_PULLUP ); + return 1; +#endif // #if LUA_OPTIMIZE_MEMORY > 0 +} diff --git a/main.c b/main.c index 181644a..353d87e 100644 --- a/main.c +++ b/main.c @@ -22,6 +22,7 @@ lua_State *L = NULL; extern int luaopen_audio(lua_State *L); extern int luaopen_gsm(lua_State *L); extern int luaopen_timer(lua_State *L); +extern int luaopen_gpio(lua_State *L); static int msleep_c(lua_State *L) { @@ -41,6 +42,7 @@ void setup_lua() luaopen_audio(L); luaopen_gsm(L); luaopen_timer(L); + luaopen_gpio(L); lua_register(L, "msleep", msleep_c); diff --git a/variant.c b/variant.c new file mode 100644 index 0000000..cec41b7 --- /dev/null +++ b/variant.c @@ -0,0 +1,252 @@ +/* + Copyright (c) 2014 MediaTek Inc. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License.. + + This library 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 Lesser General Public License for more details. +*/ + + +#include "variant.h" +#include "vmdcl.h" +#include "vmdcl_gpio.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Pins descriptions + */ +PinDescription g_APinDescription[]= +{ + { VM_DCL_HANDLE_INVALID, 10, PIO_END, 0 }, // 0 + { VM_DCL_HANDLE_INVALID, 11, PIO_END, 0 }, // 1 + { VM_DCL_HANDLE_INVALID, 46, PIO_END, 1 }, // 2 + { VM_DCL_HANDLE_INVALID, 13, PIO_END, 1 }, // 3 +#ifdef __LINKIT_V1__ + { VM_DCL_HANDLE_INVALID, 47, PIO_END, 1 }, // 4 + { VM_DCL_HANDLE_INVALID, 49, PIO_END, 1 }, // 5 + { VM_DCL_HANDLE_INVALID, 45, PIO_END, 1 }, // 6 +#else + { VM_DCL_HANDLE_INVALID, 40, PIO_END, 1 }, // 4 + { VM_DCL_HANDLE_INVALID, 3, PIO_END, 1 }, // 5 + { VM_DCL_HANDLE_INVALID, 25, PIO_END, 1 }, // 6 +#endif + { VM_DCL_HANDLE_INVALID, 50, PIO_END, 1 }, // 7 + { VM_DCL_HANDLE_INVALID, 48, PIO_END, 0 }, // 8 + { VM_DCL_HANDLE_INVALID, 19, PIO_END, 1 }, // 9 + { VM_DCL_HANDLE_INVALID, 26, PIO_END, 1 }, // 10 + { VM_DCL_HANDLE_INVALID, 28, PIO_END, 1 }, // 11 + { VM_DCL_HANDLE_INVALID, 29, PIO_END, 1 }, // 12 + { VM_DCL_HANDLE_INVALID, 27, PIO_END, 1 }, // 13 + { VM_DCL_HANDLE_INVALID, 0, PIO_END, 0 }, // 14 + { VM_DCL_HANDLE_INVALID, 1, PIO_END, 0 }, // 15 + { VM_DCL_HANDLE_INVALID, 2, PIO_END, 0 }, // 16 +#ifdef __LINKIT_V1__ + { VM_DCL_HANDLE_INVALID, 3, PIO_END, 0 }, // 17 +#else + { VM_DCL_HANDLE_INVALID, -1, PIO_END, 0 }, // 17 +#endif + { VM_DCL_HANDLE_INVALID, 44, PIO_END, 0 }, // 18 + { VM_DCL_HANDLE_INVALID, 43, PIO_END, 0 } // 19 +} ; + +#ifdef __cplusplus +} +#endif + +boolean changePinType(uint32_t ulPin, uint32_t ulPinType, VM_DCL_HANDLE* handle) +{ + VM_DCL_HANDLE gpio_handle; + + if (ulPin > PIO_MAX_NUM) + { + return false; + } + + + + if(g_APinDescription[ulPin].ulPinType == ulPinType && g_APinDescription[ulPin].ulHandle != VM_DCL_HANDLE_INVALID) + { + *handle = g_APinDescription[ulPin].ulHandle; + return true; + } + + *handle = VM_DCL_HANDLE_INVALID; + + if(ulPinType == PIO_DIGITAL) + { + if(g_APinDescription[ulPin].ulHandle != VM_DCL_HANDLE_INVALID) + { + vm_dcl_close(g_APinDescription[ulPin].ulHandle); + } + gpio_handle = vm_dcl_open(VM_DCL_GPIO,g_APinDescription[ulPin].ulGpioId); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_MODE_0,NULL); + vm_dcl_close(gpio_handle); + } + else if(ulPinType == PIO_ANALOG) + { + if(g_APinDescription[ulPin].ulHandle != VM_DCL_HANDLE_INVALID) + { + vm_dcl_close(g_APinDescription[ulPin].ulHandle); + } + gpio_handle = vm_dcl_open(VM_DCL_GPIO,g_APinDescription[ulPin].ulGpioId); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_MODE_2,NULL); + vm_dcl_close(gpio_handle); + } + else if(ulPinType == PIO_EINT) + { + if(g_APinDescription[ulPin].ulHandle != VM_DCL_HANDLE_INVALID) + { + vm_dcl_close(g_APinDescription[ulPin].ulHandle); + } + gpio_handle = vm_dcl_open(VM_DCL_GPIO,g_APinDescription[ulPin].ulGpioId); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_MODE_2,NULL); + vm_dcl_close(gpio_handle); + } + else if(ulPinType == PIO_PWM) + { + if(g_APinDescription[ulPin].ulHandle != VM_DCL_HANDLE_INVALID) + { + vm_dcl_close(g_APinDescription[ulPin].ulHandle); + } + gpio_handle = vm_dcl_open(VM_DCL_GPIO,g_APinDescription[ulPin].ulGpioId); + + if(ulPin == 3) + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_MODE_3,NULL); + else + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_MODE_2,NULL); + + vm_dcl_close(gpio_handle); + } + else if(ulPinType == PIO_SPI) + { + for(int i = 11; i<14; i++) + { + if(g_APinDescription[i].ulHandle != VM_DCL_HANDLE_INVALID) + { + vm_dcl_close(g_APinDescription[i].ulHandle); + } + gpio_handle = vm_dcl_open(VM_DCL_GPIO,g_APinDescription[i].ulGpioId); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_MODE_4,NULL); + vm_dcl_close(gpio_handle); + g_APinDescription[i].ulHandle = VM_DCL_HANDLE_INVALID; + + } + } + else if(ulPinType == PIO_UART) + { + for(int i = 0; i<2; i++) + { + if(g_APinDescription[i].ulHandle != VM_DCL_HANDLE_INVALID) + { + vm_dcl_close(g_APinDescription[i].ulHandle); + } + gpio_handle = vm_dcl_open(VM_DCL_GPIO,g_APinDescription[i].ulGpioId); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_MODE_1,NULL); + vm_dcl_close(gpio_handle); + g_APinDescription[i].ulHandle = VM_DCL_HANDLE_INVALID; + + } + } + else if(ulPinType == PIO_I2C) + { + for(int i = 18; i<20; i++) + { + if(g_APinDescription[i].ulHandle != VM_DCL_HANDLE_INVALID) + { + vm_dcl_close(g_APinDescription[i].ulHandle); + } + gpio_handle = vm_dcl_open(VM_DCL_GPIO,g_APinDescription[i].ulGpioId); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_MODE_1,NULL); + vm_dcl_close(gpio_handle); + g_APinDescription[i].ulHandle = VM_DCL_HANDLE_INVALID; + + } + } + else if(ulPinType == PIO_SD) + { + for(int i = 11; i<14; i++) + { + if(g_APinDescription[i].ulHandle != VM_DCL_HANDLE_INVALID) + { + vm_dcl_close(g_APinDescription[i].ulHandle); + } + gpio_handle = vm_dcl_open(VM_DCL_GPIO,g_APinDescription[i].ulGpioId); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_MODE_8,NULL); + vm_dcl_close(gpio_handle); + g_APinDescription[i].ulHandle = VM_DCL_HANDLE_INVALID; + + } + } + else + { + return false; + } + + g_APinDescription[ulPin].ulPinType = (EPioType)ulPinType; + + return true; +} + +void spiPinsRest(void) +{ + int i; + VM_DCL_HANDLE gpio_handle; + + for(int i = 11; i<14; i++) + { + gpio_handle = vm_dcl_open(VM_DCL_GPIO, g_APinDescription[i].ulGpioId); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_MODE_0,NULL); + vm_dcl_close(gpio_handle); + g_APinDescription[i].ulHandle = VM_DCL_HANDLE_INVALID; + g_APinDescription[i].ulPinType = PIO_DIGITAL; + } +} + +void setPinHandle(uint32_t ulPin, VM_DCL_HANDLE handle) +{ + g_APinDescription[ulPin].ulHandle = handle; +} + + +#ifdef __cplusplus +extern "C" { +#endif + +void init( void ) +{ + int i; + VM_DCL_HANDLE gpio_handle; + + for(i=2;i<14;i++) + { + gpio_handle = vm_dcl_open(VM_DCL_GPIO,g_APinDescription[i].ulGpioId); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_MODE_0,NULL); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_DIR_OUT, NULL); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_WRITE_LOW, NULL); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_DIR_IN, NULL); + vm_dcl_close(gpio_handle); + } + for(i=18;i<20;i++) + { + gpio_handle = vm_dcl_open(VM_DCL_GPIO,g_APinDescription[i].ulGpioId); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_MODE_0,NULL); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_DIR_OUT, NULL); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_WRITE_LOW, NULL); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_DIR_IN, NULL); + vm_dcl_close(gpio_handle); + } +} + +#ifdef __cplusplus +} +#endif + diff --git a/variant.h b/variant.h new file mode 100644 index 0000000..631d008 --- /dev/null +++ b/variant.h @@ -0,0 +1,101 @@ +/* + Copyright (c) 2011 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Modified 20 Aug 2014 by MediaTek Inc. + +*/ + +#ifndef _VARIANT_ARDUINO_DUE_X_ +#define _VARIANT_ARDUINO_DUE_X_ + +/*---------------------------------------------------------------------------- + * Definitions + *----------------------------------------------------------------------------*/ + +/** Frequency of the board main oscillator */ +#define VARIANT_MAINOSC 12000000 + +/** Master clock frequency */ +#define VARIANT_MCK 84000000 + +//#define __LINKIT_V1__ + +/*---------------------------------------------------------------------------- + * Headers + *----------------------------------------------------------------------------*/ + +#include "Arduino.h" + + + +static const uint8_t A0 = 14;/*analog input pin A0*/ +static const uint8_t A1 = 15;/*analog input pin A1*/ +static const uint8_t A2 = 16;/*analog input pin A2*/ + +#ifdef __LINKIT_V1__ +static const uint8_t A3 = 17;/*analog input pin A3*/ +#endif + + +/*---------------------------------------------------------------------------- + * Pins + *----------------------------------------------------------------------------*/ +/* + * SPI Interfaces + */ +#define SPI_INTERFACES_COUNT 1 + +#define SPI_INTERFACE SPI0 +#define SPI_INTERFACE_ID ID_SPI0 +#define SPI_CHANNELS_NUM 4 +#define PIN_SPI_SS0 (10) +#define PIN_SPI_SS1 (10) +#define PIN_SPI_SS2 (10) +#define PIN_SPI_SS3 (10) +#define PIN_SPI_MOSI (11) +#define PIN_SPI_MISO (12) +#define PIN_SPI_SCK (13) +#define BOARD_SPI_SS0 (10) +#define BOARD_SPI_SS1 (10) +#define BOARD_SPI_SS2 (10) +#define BOARD_SPI_SS3 PIN_SPI_SS3 +#define BOARD_SPI_DEFAULT_SS BOARD_SPI_SS3 + +#define BOARD_PIN_TO_SPI_PIN(x) \ + (x==BOARD_SPI_SS0 ? PIN_SPI_SS0 : \ + (x==BOARD_SPI_SS1 ? PIN_SPI_SS1 : \ + (x==BOARD_SPI_SS2 ? PIN_SPI_SS2 : PIN_SPI_SS3 ))) +#define BOARD_PIN_TO_SPI_CHANNEL(x) \ + (x==BOARD_SPI_SS0 ? 0 : \ + (x==BOARD_SPI_SS1 ? 1 : \ + (x==BOARD_SPI_SS2 ? 2 : 3))) + +static const uint8_t SS = BOARD_SPI_SS0; +static const uint8_t SS1 = BOARD_SPI_SS1; +static const uint8_t SS2 = BOARD_SPI_SS2; +static const uint8_t SS3 = BOARD_SPI_SS3; +static const uint8_t MOSI = PIN_SPI_MOSI; +static const uint8_t MISO = PIN_SPI_MISO; +static const uint8_t SCK = PIN_SPI_SCK; + +/*---------------------------------------------------------------------------- + * Arduino objects - C++ only + *----------------------------------------------------------------------------*/ + + +#endif /* _VARIANT_ARDUINO_DUE_X_ */ + diff --git a/wiring_constants.h b/wiring_constants.h new file mode 100644 index 0000000..782498f --- /dev/null +++ b/wiring_constants.h @@ -0,0 +1,263 @@ +/* + Copyright (c) 2011 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Modified 20 Aug 2014 by MediaTek Inc. +*/ + +#ifndef _WIRING_CONSTANTS_ +#define _WIRING_CONSTANTS_ + +#ifdef __cplusplus +extern "C"{ +#endif // __cplusplus + +/* DOM-NOT_FOR_SDK-BEGIN */ +#define HIGH 0x1 +#define LOW 0x0 + +#define INPUT 0x0 +#define OUTPUT 0x1 +#define INPUT_PULLUP 0x2 + +#define true 0x1 +#define false 0x0 + +#define PI 3.1415926535897932384626433832795 +#define HALF_PI 1.5707963267948966192313216916398 +#define TWO_PI 6.283185307179586476925286766559 +#define DEG_TO_RAD 0.017453292519943295769236907684886 //弧度转角度 +#define RAD_TO_DEG 57.295779513082320876798154814105 //角度转弧度 +#define EULER 2.718281828459045235360287471352 + +#define SERIAL 0x0 +#define DISPLAY 0x1 + +enum BitOrder { + LSBFIRST = 0, + MSBFIRST = 1 +}; + +// LOW 0 +// HIGH 1 +#define CHANGE 2 +#define FALLING 3 +#define RISING 4 + +#define DEFAULT 1 +#define EXTERNAL 0 + +// undefine stdlib's abs if encountered +#ifdef abs +#undef abs +#endif // abs +/* DOM-NOT_FOR_SDK-END */ + +#ifndef min +/***************************************************************************** + * + * FUNCTION + * min + * DESCRIPTION + * Calculates the minimum of two numbers. + * PARAMETERS + * x: the first number, any data type + * y: the second number, any data type + * RETURNS + * The smaller of the two numbers. + * RETURN VALUES + * depend on input value + *****************************************************************************/ +#define min(a,b) ((a)<(b)?(a):(b)) +#endif // min + +#ifndef max +/***************************************************************************** + * + * FUNCTION + * max + * DESCRIPTION + * Calculates the maximum of two numbers. + * PARAMETERS + * x: the first number, any data type + * y: the second number, any data type + * RETURNS + * The larger of the two numbers. + * RETURN VALUES + * depend on input value + *****************************************************************************/ +#define max(a,b) ((a)>(b)?(a):(b)) +#endif // max + +/***************************************************************************** + * + * FUNCTION + * abs + * DESCRIPTION + * Computes the absolute value of a number. + * PARAMETERS + * x: the number + * RETURNS + * x: if x is greater than or equal to 0. + * -x: if x is less than 0. + * RETURN VALUES + * depend on input value + *****************************************************************************/ +#define abs(x) ((x)>0?(x):-(x)) + +/***************************************************************************** + * + * FUNCTION + * constrain + * DESCRIPTION + * Constrains a number to be within a range. + * PARAMETERS + * x: the number to constrain, all data types + * a: the lower end of the range, all data types + * b: the upper end of the range, all data types + * RETURNS + * x: if x is between a and b + * a: if x is less than a + * b: if x is greater than b + * RETURN VALUES + * depend on input value + *****************************************************************************/ +#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) + +/* DOM-NOT_FOR_SDK-BEGIN */ +#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5)) //四舍五入 +#define radians(deg) ((deg)*DEG_TO_RAD) // 角度转弧度 +#define degrees(rad) ((rad)*RAD_TO_DEG) //弧度转角度 +#define sq(x) ((x)*(x)) //平方 +/* DOM-NOT_FOR_SDK-END */ + +/***************************************************************************** + * FUNCTION + * lowByte + * DESCRIPTION + * Extracts the high-order (leftmost) byte of a word (or the second lowest byte of a larger data type). + * PARAMETERS + * x: a value of any type + * RETURNS + * byte + * RETURN VALUES + * depend on input value + *****************************************************************************/ +#define lowByte(w) ((uint8_t) ((w) & 0xff)) + +/***************************************************************************** + * FUNCTION + * highByte + * DESCRIPTION + * Extracts the high-order (leftmost) byte of a word (or the second lowest byte of a larger data type). + * PARAMETERS + * x: a value of any type + * RETURNS + * byte + * RETURN VALUES + * depend on input value + *****************************************************************************/ +#define highByte(w) ((uint8_t) ((w) >> 8)) + +/***************************************************************************** + * FUNCTION + * bitRead + * DESCRIPTION + * Reads a bit of a number. + * PARAMETERS + * x: the number from which to read + * n: which bit to read, starting at 0 for the least-significant (rightmost) bit + * RETURNS + * the value of the bit (0 or 1). + * RETURN VALUES + * depend on input value + *****************************************************************************/ +#define bitRead(value, bit) (((value) >> (bit)) & 0x01) + +/***************************************************************************** + * FUNCTION + * bitSet + * DESCRIPTION + * Sets (writes a 1 to) a bit of a numeric variable. + * PARAMETERS + * x: the numeric variable whose bit to set + * n: which bit to set, starting at 0 for the least-significant (rightmost) bit + * RETURNS + * none + *****************************************************************************/ +#define bitSet(value, bit) ((value) |= (1UL << (bit))) + +/***************************************************************************** + * FUNCTION + * bitClear + * DESCRIPTION + * Clears (writes a 0 to) a bit of a numeric variable. + * PARAMETERS + * x: the numeric variable whose bit to clear + * n: which bit to clear, starting at 0 for the least-significant (rightmost) bit + * RETURNS + * none + *****************************************************************************/ +#define bitClear(value, bit) ((value) &= ~(1UL << (bit))) + +/***************************************************************************** + * FUNCTION + * bitWrite + * DESCRIPTION + * Writes a bit of a numeric variable. + * PARAMETERS + * x: the numeric variable to which to write + * n: which bit of the number to write, starting at 0 for the least-significant (rightmost) bit + * b: the value to write to the bit (0 or 1) + * RETURNS + * the value of the bit + * RETURN VALUES + * depend on input value + *****************************************************************************/ +#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit)) + +/* DOM-NOT_FOR_SDK-BEGIN */ +typedef unsigned int word; +/* DOM-NOT_FOR_SDK-END */ + +/***************************************************************************** + * FUNCTION + * bit + * DESCRIPTION + * Computes the value of the specified bit (bit 0 is 1, bit 1 is 2, bit 2 is 4, etc.). + * PARAMETERS + * b: the bit whose value to compute + * RETURNS + * the value of the bit + * RETURN VALUES + * depend on input value + *****************************************************************************/ +#define bit(b) (1UL << (b)) + +#define _BV(bit) (1 << (bit)) + +/* DOM-NOT_FOR_SDK-BEGIN */ +// TODO: to be checked +typedef uint8_t boolean ; +typedef uint8_t byte ; +/* DOM-NOT_FOR_SDK-END */ + + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus + +#endif /* _WIRING_CONSTANTS_ */ diff --git a/wiring_digital.c b/wiring_digital.c new file mode 100644 index 0000000..e610cb6 --- /dev/null +++ b/wiring_digital.c @@ -0,0 +1,148 @@ +/* + Copyright (c) 2011 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "Arduino.h" +#include "vmdcl.h" +#include "vmdcl_gpio.h" +#include "vmlog.h" + +#ifdef __cplusplus + extern "C" { +#endif + +extern void pinMode( uint32_t ulPin, uint32_t ulMode ) +{ + VM_DCL_HANDLE gpio_handle; + vm_gpio_ctrl_set_pupd_r0_r1_t pupd; + //vm_log_info("pinMode(): pin = %d , pin_internal = %d, mode = %d", ulPin, g_APinDescription[ulPin].ulGpioId, ulMode); + + if(!changePinType(ulPin, PIO_DIGITAL, &gpio_handle)) + return; + + if(gpio_handle == VM_DCL_HANDLE_INVALID) + gpio_handle = vm_dcl_open(VM_DCL_GPIO, g_APinDescription[ulPin].ulGpioId); + + // set PIN direction + switch ( ulMode ) + { + case INPUT: + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_DIR_IN, NULL); + + if( g_APinDescription[ulPin].ulPupd) + { + pupd.fgSetPupd = 0; + pupd.fgSetR0 = 1; + pupd.fgSetR1 = 0; + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_PUPD_R0_R1, &pupd); + } + else + { + vm_dcl_control(gpio_handle,VM_GPIO_CMD_ENABLE_PULL, NULL); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_PULL_LOW, NULL); + } + break ; + + case INPUT_PULLUP: + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_DIR_IN, NULL); + + if( g_APinDescription[ulPin].ulPupd) + { + pupd.fgSetPupd = 1; + pupd.fgSetR0 = 1; + pupd.fgSetR1 = 0; + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_PUPD_R0_R1, &pupd); + } + else + { + vm_dcl_control(gpio_handle,VM_GPIO_CMD_ENABLE_PULL, NULL); + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_PULL_HIGH, NULL); + } + break ; + + case OUTPUT: + vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_DIR_OUT, NULL); + break ; + + default: + break ; + } + + g_APinDescription[ulPin].ulHandle = gpio_handle; + +} + +extern void digitalWrite( uint32_t ulPin, uint32_t ulVal ) +{ + //vm_log_info("digitalWrite(): pin = %d , value = %d", ulPin, ulVal); + + if (ulPin > PIO_MAX_NUM ) + { + return; + } + + // write PIN + switch (ulVal) + { + case HIGH: + vm_dcl_control(g_APinDescription[ulPin].ulHandle,VM_GPIO_CMD_WRITE_HIGH, NULL); + break; + + case LOW: + vm_dcl_control(g_APinDescription[ulPin].ulHandle,VM_GPIO_CMD_WRITE_LOW, NULL); + break; + + default: + break; + } + +} + +extern int digitalRead( uint32_t ulPin ) +{ + vm_gpio_ctrl_read_t data; + + //vm_log_info("digitalRead(): pin = %d", ulPin); + + if (ulPin > PIO_MAX_NUM ) + { + //vm_log_info("digitalRead(): pin mapping failed, return LOW as default."); + return LOW; + } + + vm_dcl_control(g_APinDescription[ulPin].ulHandle, VM_GPIO_CMD_READ,(void *)&data); + + if ( data.u1IOData == VM_GPIO_IO_LOW ) + { + //vm_log_info("digitalRead(): result = LOW"); + return LOW ; + } + else if ( data.u1IOData == VM_GPIO_IO_HIGH ) + { + //vm_log_info("digitalRead(): result = HIGH"); + return HIGH ; + } + else + { + //vm_log_info("digitalRead(): read failed, return LOW as default."); + return LOW ; + } +} + +#ifdef __cplusplus +} +#endif diff --git a/wiring_digital.h b/wiring_digital.h new file mode 100644 index 0000000..2af30e5 --- /dev/null +++ b/wiring_digital.h @@ -0,0 +1,133 @@ +/* + * digital I/O for Arduino Due + * Copyright (c) 2011 Cristian Maglie . + * All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Modified 20 Aug 2014 by MediaTek Inc. + */ +#ifndef _WIRING_DIGITAL_ +#define _WIRING_DIGITAL_ + +#include + +#ifdef __cplusplus + extern "C" { +#endif + +//DESCRIPTION +// Sets up modes for assigned pins. +// +// There are 16 pins on LinkIt ONE used as digital I/O, they are D0 ~ D13 +// and D18/D19, particularly D18/D19 are shared with Wire/I2C. +// +// +// +// LinkIt One supports 3 pin modes: +// +// * INPUT mode is used for reading voltage (HIGH or LOW) or sensor. In INPUT mode when +// the circuit is at high impedance, similar to serially connecting a big resistance in +// the circuit, the pin can read accurate voltage value. However, this pin may not have +// enough voltage to activate peripheral devices, e.g. LED. +// * INPUT_PULLUP mode is similar to INPUT mode, used for reading voltage of sensor. However +// in this mode, the pin is often at high voltage when the sensor is disabled and turns to +// low voltage when the sensor is enabled, which is opposite to its behavior in INPUT mode. +// * OUTPUT mode is used for controlling peripheral devices. In OUTPUT mode when the circuit +// is at low impedance, the pin has enough voltage to activate or control other devices but +// cannot read the voltage of sensor. +// +//EXAMPLE +// +// #define LED 13 +// void setup() +// { +// pinMode(LED, OUTPUT); +// } +// void loop() +// { +// digitalWrite(LED, HIGH); +// delay(3000); +// digitalWrite(LED, LOW); +// delay(3000); +// } +// +extern void pinMode( + uint32_t dwPin, // [IN] Pin number that needs to be set with a mode + uint32_t dwMode // [IN] Mode of pin (INPUT, INPUT_PULLUP or OUTPUT) + ) ; + +//DESCRIPTION +// Sets assigned pins to high voltage (3.3V) or low voltage (0V). +// +// There are 16 pins on LinkIt ONE used as digital I/O, they are D0 ~ D13 and D18/D19, particularly D18/D19 are shared with Wire/I2C. +//EXAMPLE +// +// #define LED 13 +// void setup() +// { +// pinMode(LED, OUTPUT); +// } +// void loop() +// { +// digitalWrite(LED, HIGH); +// delay(3000); +// digitalWrite(LED, LOW); +// delay(3000); +// } +// +extern void digitalWrite( + uint32_t dwPin, // [IN] Pin number that needs to be set with a value + uint32_t dwVal // [IN] HIGH or LOW + ) ; + +//DESCRIPTION +// Reads voltage of assigned pin, HIGH or LOW. +// +// There are 16 pins on LinkIt ONE used as digital I/O, they are D0 ~ D13 and D18/D19, particularly D18/D19 are shared with Wire/I2C. +//RETURNS +// HIGH or LOW +//EXAMPLE +// +// #define LED 13 +// #define BUTTON 8 +// void setup() +// { +// pinMode(LED, OUTPUT); +// pinMode(BUTTON, INPUT); +// } +// void loop() +// { +// int n= digitalRead(BUTTON); +// if (n == HIGH) +// { +// digitalWrite(LED, LOW); +// } +// else +// { +// digitalWrite(LED, HIGH); +// } +// delay(2000); +// } +// +extern int digitalRead( + uint32_t ulPin // [IN] Pin number that needs to read voltage + ) ; + +#ifdef __cplusplus +} +#endif + +#endif /* _WIRING_DIGITAL_ */