add read method for gpio

This commit is contained in:
李昂 2021-10-25 20:16:08 +08:00
parent 73c7116cf2
commit 4b76b565b7
7 changed files with 104 additions and 43 deletions

View File

@ -313,7 +313,7 @@
</ArmAdsMisc>
<Cads>
<interw>1</interw>
<Optim>7</Optim>
<Optim>2</Optim>
<oTime>0</oTime>
<SplitLS>0</SplitLS>
<OneElfS>1</OneElfS>
@ -332,7 +332,7 @@
<v6LangP>3</v6LangP>
<vShortEn>1</vShortEn>
<vShortWch>1</vShortWch>
<v6Lto>1</v6Lto>
<v6Lto>0</v6Lto>
<v6WtE>0</v6WtE>
<v6Rtti>0</v6Rtti>
<VariousControls>

View File

@ -2,31 +2,40 @@ import PikaStdLib
import STM32
import PikaPiZero
uart = STM32.UART()
uart = STM32.UART()
rgb = PikaPiZero.RGB()
right = STM32.GPIO()
right.init()
right.setPin('PA0')
right.setMode('in')
right.setPull('down')
right.enable()
left= STM32.GPIO()
left.init()
left.setPin('PC13')
left.setMode('in')
left.setPull('up')
left.enable()
down= STM32.GPIO()
down.init()
down.setPin('PB6')
down.setMode('in')
down.setPull('up')
down.enable()
up= STM32.GPIO()
up.init()
up.setPin('PA15')
up.setMode('in')
up.setPull('up')
up.enable()
print('Hello PikaScript!')
mem = PikaStdLib.MemChecker()
uart.init()
uart.setId(1)
uart.setBaudRate(115200)
uart.enable()
iic = STM32.IIC()
iic.init()
iic.setPinSDA('PB0')
iic.setPinSCL('PB1')
iic.setDeviceAddr(88)
iic.enable()
print(iic.read(0, 4))
print('hello 2')
print('mem used max:')
mem.max()
i = 0
while i < 100:
i = i + 1
print(i)
while True:
if right.read() :
print('up')
print('Hello PikaScript!')

View File

@ -7,6 +7,7 @@ void PikaStdDevice_GPIO_init(PikaObj* self) {
obj_setStr(self, "pin", "PA0");
obj_setStr(self, "mode", "out");
obj_setInt(self, "isOn", 0);
obj_setStr(self, "pull", "none");
}
void PikaStdDevice_GPIO_disable(PikaObj* self) {
@ -37,6 +38,11 @@ void PikaStdDevice_GPIO_high(PikaObj* self) {
obj_run(self, "platformHigh()");
}
int PikaStdDevice_GPIO_read(PikaObj *self){
obj_run(self, "readBuff = platformRead()");
return obj_getInt(self, "readBuff");
}
void PikaStdDevice_GPIO_setMode(PikaObj* self, char* mode) {
if(strEqu(mode, "out")||strEqu(mode, "in")){
obj_setStr(self, "mode", mode);
@ -47,6 +53,15 @@ void PikaStdDevice_GPIO_setMode(PikaObj* self, char* mode) {
}
}
void PikaStdDevice_GPIO_setPull(PikaObj *self, char * pull){
if(strEqu(pull, "up")||strEqu(pull, "down")||strEqu(pull, "none")){
obj_setStr(self, "pull", pull);
}else{
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] GPIO pull should be 'up', 'down' or 'none'.");
}
}
void PikaStdDevice_GPIO_setPin(PikaObj* self, char* pinName) {
obj_setStr(self, "pin", pinName);
}
@ -84,3 +99,9 @@ void PikaStdDevice_GPIO_platformOn(PikaObj* self) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}
int PikaStdDevice_GPIO_platformRead(PikaObj *self){
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
return -1;
}

View File

@ -17,6 +17,9 @@ class GPIO(TinyObj):
def getMode() -> str:
pass
def setPull(pull:str):
pass
def enable():
pass
@ -29,6 +32,9 @@ class GPIO(TinyObj):
def low():
pass
def read()->int:
pass
# need be overrid
def platformHigh():
pass
@ -49,6 +55,10 @@ class GPIO(TinyObj):
def platformSetMode(mode: str):
pass
# need override
def platformRead()->int:
pass
class Time(TinyObj):
# need override

View File

@ -23,6 +23,10 @@ class GPIO(PikaStdDevice.GPIO):
def platformSetMode(mode: str):
pass
# override
def platformRead()->int:
pass
class Time(PikaStdDevice.Time):
# override

View File

@ -53,16 +53,20 @@ void STM32_GPIO_platformEnable(PikaObj* self) {
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_RESET);
uint32_t pinMode = getPinMode(mode);
if (NULL == pinMode) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio mode.");
}
uint32_t gpioPull = GPIO_NOPULL;
char *pull = obj_getStr(self, "pull");
if(strEqu(pull, "up")){
gpioPull = GPIO_PULLUP;
}else if(strEqu(pull, "down")){
gpioPull = GPIO_PULLDOWN;
}
GPIO_InitTypeDef GPIO_InitStruct = {0};
/*Configure GPIO*/
GPIO_InitStruct.Pin = gpioPin;
GPIO_InitStruct.Mode = pinMode;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Pull = gpioPull;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(gpioPort, &GPIO_InitStruct);
}
@ -127,10 +131,6 @@ void STM32_GPIO_platformSetMode(PikaObj* self, char* mode) {
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_RESET);
uint32_t pinMode = getPinMode(mode);
if (NULL == pinMode) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio mode.");
}
GPIO_InitTypeDef GPIO_InitStruct = {0};
/*Configure GPIO*/
@ -139,4 +139,19 @@ void STM32_GPIO_platformSetMode(PikaObj* self, char* mode) {
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(gpioPort, &GPIO_InitStruct);
}
int STM32_GPIO_platformRead(PikaObj *self){
char* pin = obj_getStr(self, "pin");
GPIO_TypeDef* gpioPort = getGpioPort(pin);
if (NULL == gpioPort) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio port.");
}
uint16_t gpioPin = getGpioPin(pin);
if (0 == gpioPin) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio pin.");
}
return HAL_GPIO_ReadPin(gpioPort,gpioPin);
}

View File

@ -97,25 +97,27 @@ int main(void) {
SCB->VTOR = FLASH_BASE | 0x2000;
__enable_irq();
pikaMain = newRootObj("pikaMain", New_PikaMain);
obj_run(pikaMain, "uart = STM32.UART()");
obj_run(pikaMain, "uart.init()");
obj_run(pikaMain, "uart.setId(1)");
obj_run(pikaMain, "uart.setBaudRate(115200)");
obj_run(pikaMain, "uart.enable()");
obj_run(pikaMain, "print('[info]: boot from flash.')");
obj_deinit(pikaMain);
char* code = (char*)FLASH_SCRIPT_START_ADDR;
uint16_t codeOffset = 0;
if (code[0] != 0xFF) {
/* boot from flash */
pikaMain = newRootObj("pikaMain", New_PikaMain);
obj_run(pikaMain, "uart = STM32.UART()");
obj_run(pikaMain, "uart.init()");
obj_run(pikaMain, "uart.setId(1)");
obj_run(pikaMain, "uart.setBaudRat e(115200)");
obj_run(pikaMain, "uart.enable()");
obj_run(pikaMain, "print('[info]: boot from flash.')");
obj_deinit(pikaMain);
pikaMain = newRootObj("pikaMain", New_PikaMain);
if(code[0] == 'i'){
printf("[info]: boot from Script.\r\n");
obj_run(pikaMain, code);
}
if(code[0] == 'B'){
printf("[info]: boot from Pika Asm.\r\n");
printf("[info]: boot from Pika Asm.\r\n");
printf("%s",code);
pikaVM_runAsm(pikaMain, code);
}
} else {