Add pinMode,digitalWrite,digitalRead

Add millis,micros,delay

Signed-off-by: Bigbits <btx000@qq.com>
This commit is contained in:
Bigbits
2019-03-17 20:36:14 +08:00
parent 1a6cf1e0dc
commit a657f43573
11 changed files with 361 additions and 11 deletions

View File

@@ -15,8 +15,6 @@ m1.build.variant=standard
m1.menu.clksrc=400MHz FPGA Clock
m1.menu.clksrc.fpga.build.f_cpu=400000000L
#m1.build.mcu=rv64gc
#m1.build.mabi=ilp64
m1.build.mcmodel=medany
#"The 'core' file directory for this board, in ./cores

View File

@@ -33,14 +33,18 @@
//#include <avr/interrupt.h>
#include "binary.h"
//#include "pins_arduino.h"
#include "pins_arduino.h"
#include "fpioa.h"
#include "gpiohs.h"
#include "gpio.h"
#include "gpio_common.h"
#ifdef __cplusplus
extern "C"{
#endif // __cplusplus
//#include "wiring.h"
//#include "wiring_constants.h"
#include "wiring.h"
#include "wiring_constants.h"
#define SystemCoreClock F_CPU
@@ -77,7 +81,7 @@ void noTone(uint8_t _pin);
// Include board variant
//#include "variant.h"
//#include "wiring_digital.h"
#include "wiring_digital.h"
//#include "wiring_analog.h"
//#include "wiring_shift.h"
//#include "WInterrupts.h"

View File

@@ -1,7 +1,7 @@
#define ARDUINO_MAIN
#include "Arduino.h"
//#include "encoding.h"
#include "encoding.h"
#define cmb() __asm__ __volatile__ ("" ::: "memory")
@@ -12,8 +12,8 @@
*/
int main( void )
{
//init();
pll_init();
fpioa_init();
setup();
do {

20
cores/arduino/wiring.c Normal file
View File

@@ -0,0 +1,20 @@
#include "Arduino.h"
#include "sysctl.h"
#include "sleep.h"
uint64_t millis(){
return sysctl_get_time_us()/1000;
}
uint64_t micros(){
return sysctl_get_time_us();
}
void delay(uint64_t dwMs){
msleep(dwMs);
return;
}
void pll_init(){
sysctl_pll_set_freq(SYSCTL_PLL0, 800000000UL);
sysctl_pll_set_freq(SYSCTL_PLL1, 300000000UL);
sysctl_pll_set_freq(SYSCTL_PLL2, 45158400UL);
return;
}

32
cores/arduino/wiring.h Normal file
View File

@@ -0,0 +1,32 @@
#ifndef _WIRING_
#define _WIRING_
#include "sysctl.h"
#include "sleep.h"
/**
* \brief Returns the number of milliseconds since the board began running the current program.
*
* \return Number of milliseconds since the program started (uint64_t)
*/
extern uint64_t millis(void);
/**
* \brief Returns the number of microseconds since the board began running the current program.
*
* \note There are 1,000 microseconds in a millisecond and 1,000,000 microseconds in a second.
*/
extern uint64_t micros(void);
/**
* \brief Pauses the program for the amount of time (in miliseconds) specified as parameter.
* (There are 1000 milliseconds in a second.)
*
* \param dwMs the number of milliseconds to pause (uint64_t)
*/
extern void delay(uint64_t dwMs);
void pll_init(void);
#endif

View File

@@ -0,0 +1,105 @@
/*
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
*/
#ifndef _WIRING_CONSTANTS_
#define _WIRING_CONSTANTS_
#include "encoding.h"
#include <stdint.h>
#ifdef __cplusplus
extern "C"{
#endif // __cplusplus
#define HIGH 0x1
#define LOW 0x0
#define INPUT 0x0
#define OUTPUT 0x3
#define INPUT_PULLUP 0x2
#define INPUT_PULLDOWN 0X1
#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
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif // min
#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#endif // max
#define abs(x) ((x)>0?(x):-(x))
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
#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))
#define interrupts() set_csr(mstatus, MSTATUS_MIE)
#define noInterrupts() clear_csr(mstatus, MSTATUS_MIE)
#define lowByte(w) ((uint8_t) ((w) & 0xff))
#define highByte(w) ((uint8_t) ((w) >> 8))
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
typedef unsigned int word;
#define bit(b) (1UL << (b))
typedef bool boolean ;
typedef uint8_t byte ;
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#endif /* _WIRING_CONSTANTS_ */

View File

@@ -0,0 +1,33 @@
#include "Arduino.h"
#include <stdint.h>
#include "fpioa.h"
#include "gpiohs.h"
#include "gpio.h"
#include "wiring_constants.h"
#include "wiring_digital.h"
#ifdef __cplusplus
extern "C"{
#endif // __cplusplus
void pinMode(uint8_t dwPin, uint8_t dwMode){ //48个fpio 对应32个gpiohs
fpioa_set_function(dwPin, FUNC_GPIOHS0 + dwPin);
gpiohs_set_drive_mode(dwPin, (gpio_drive_mode_t)dwMode);
return ;
}
void digitalWrite(uint8_t dwPin, uint8_t dwVal){
gpiohs_set_pin(dwPin, (gpio_pin_value_t)dwVal);
return ;
}
int digitalRead(uint8_t ulPin){
return (int)gpiohs_get_pin(ulPin);
}
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

View File

@@ -0,0 +1,59 @@
#ifndef _WIRING_DIGITAL_
#define _WIRING_DIGITAL_
#include "Arduino.h"
#include <stdint.h>
#ifdef __cplusplus
extern "C"{
#endif // __cplusplus
#define PortValue_t uint32_t
#define PortAddr_t uint32_t
#define PortRegister_t volatile uint32_t *
#define digitalPinToPort(pin) ((PortAddr_t) GPIO_CTRL_ADDR)
#define digitalPinToPortIn(pin) ((PortAddr_t) GPIO_CTRL_ADDR)
#define digitalPinToBitMask(pin) ((PortValue_t) (1<<variant_pin_map[pin].bit_pos))
#define portOutputRegister(port) ((PortRegister_t) (GPIO_CTRL_ADDR + GPIO_OUTPUT_VAL))
#define portInputRegister(port) ((PortRegister_t) (GPIO_CTRL_ADDR + GPIO_INPUT_VAL))
/**
* \brief Configures the specified pin to behave either as an input or an output. See the description of digital pins for details.
*
* \param ulPin The number of the pin whose mode you wish to set
* \param ulMode Either INPUT or OUTPUT
*/
extern void pinMode( uint8_t dwPin, uint8_t dwMode ) ;
/**
* \brief Write a HIGH or a LOW value to a digital pin.
*
* If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the
* corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.
*
* \note Digital pin PIN_LED is harder to use as a digital input than the other digital pins because it has an LED
* and resistor attached to it that's soldered to the board on most boards. If you enable its internal 20k pull-up
* resistor, it will hang at around 1.7 V instead of the expected 5V because the onboard LED and series resistor
* pull the voltage level down, meaning it always returns LOW. If you must use pin PIN_LED as a digital input, use an
* external pull down resistor.
*
* \param dwPin the pin number
* \param dwVal HIGH or LOW
*/
extern void digitalWrite( uint8_t dwPin, uint8_t dwVal ) ;
/**
* \brief Reads the value from a specified digital pin, either HIGH or LOW.
*
* \param ulPin The number of the digital pin you want to read (int)
*
* \return HIGH or LOW
*/
extern int digitalRead( uint8_t ulPin ) ;
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#endif /* _WIRING_DIGITAL_ */

26
keywords.txt Normal file
View File

@@ -0,0 +1,26 @@
#######################################
# Syntax Coloring Map For ExampleLibrary
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
#######################################
# Methods and Functions (KEYWORD2)
#######################################
#######################################
# Instances (KEYWORD2)
#######################################
#######################################
# Constants (LITERAL1)
#######################################
PIN_LED_GREEN LITERAL1
PIN_LED_BLUE LITERAL1
PIN_LED_RED LITERAL1
PIN_LED LITERAL1

View File

@@ -1,3 +1,7 @@
// API compatibility
#include "variant.h"
#ifdef SIPEED_M1_BOARD
#include "sipeedm1.h"
#endif

View File

@@ -0,0 +1,69 @@
#ifndef _VARIANT_SIPEED_M1_DOCK
#define _VARIANT_SIPEED_M1_DOCK
#include <stdint.h>
#define RISCV
#include "platform.h"
#include "Arduino.h"
/* BOARD PIN DEFINE */
/* LEDs */
#define PIN_LED_GREEN 13
#define PIN_LED_BLUE 12
#define PIN_LED_RED 14
#define PIN_LED 13
#define LED_BUILTIN 13
/* KEY */
#define KEY0 16
/* UART */
#define RX0 4
#define TX0 5
/* MIC ARRAY */
#define MIC_BCK 18
#define MIC_WS 19
#define MIC_DAT3 20
#define MIC_DAT2 21
#define MIC_DAT1 22
#define MIC_DAT0 23
#define MIC_LED_DAT 24
/* SPI0 */
#define SPI0_CS1 25
#define SPI0_MISO 26
#define SPI0_SCLK 27
#define SPI0_MOSI 28
#define SPI0_CS0 29
/* I2S */
#define MIC0_WS 30
#define MIC0_DATA 31
#define MIC0_BCK 32
#define I2S_WS 33
#define I2S_DA 34
#define I2S_BCK 35
/* LCD */
#define LCD_CS 36
#define LCD_RST 37
#define LCD_DC 38
#define LCD_WR 39
static const uint8_t RX = RX0;
static const uint8_t TX = TX0;
static const uint8_t SDA = 10;
static const uint8_t SCL = 9;
static const uint8_t SS = SPI0_CS0 ;
static const uint8_t MOSI = SPI0_MOSI;
static const uint8_t MISO = SPI0_MISO;
static const uint8_t SCK = SPI0_SCLK;
#define VARIANT_NUM_GPIOHS (32)
#define VARIANT_NUM_GPIO ( 8)
#define VARIANT_NUM_PWM ( 3)
#define VARIANT_NUM_I2C ( 3)
#define VARIANT_NUM_SPI ( 3)
#define VARIANT_NUM_UART ( 3)
#endif