From 88f4e507f1665ca7ceda149aabf029f4e21e22c7 Mon Sep 17 00:00:00 2001 From: pikastech Date: Mon, 27 Dec 2021 18:30:37 +0800 Subject: [PATCH] add W801Device package --- bsp/stm32g070cb/MDK-ARM/stm32g070.uvprojx | 2 +- package/W801Device/W801Device.py | 28 ++++ package/W801Device/W801_GPIO.c | 169 +++++++++++++++++++ package/W801Device/W801_common.c | 196 ++++++++++++++++++++++ package/W801Device/W801_common.h | 70 ++++++++ 5 files changed, 464 insertions(+), 1 deletion(-) create mode 100644 package/W801Device/W801Device.py create mode 100644 package/W801Device/W801_GPIO.c create mode 100644 package/W801Device/W801_common.c create mode 100644 package/W801Device/W801_common.h diff --git a/bsp/stm32g070cb/MDK-ARM/stm32g070.uvprojx b/bsp/stm32g070cb/MDK-ARM/stm32g070.uvprojx index 4fac22b31..dc5731ab4 100644 --- a/bsp/stm32g070cb/MDK-ARM/stm32g070.uvprojx +++ b/bsp/stm32g070cb/MDK-ARM/stm32g070.uvprojx @@ -314,7 +314,7 @@ 1 - 6 + 5 0 0 1 diff --git a/package/W801Device/W801Device.py b/package/W801Device/W801Device.py new file mode 100644 index 000000000..b86b0c06c --- /dev/null +++ b/package/W801Device/W801Device.py @@ -0,0 +1,28 @@ +from PikaObj import * +import PikaStdDevice + +class GPIO(PikaStdDevice.GPIO): + # need be overrid + def platformHigh(): + pass + + # need override + def platformLow(): + pass + + # need override + def platformEnable(): + pass + + # need override + def platformDisable(): + pass + + # need override + def platformSetMode(): + pass + + # need override + def platformRead(): + pass + diff --git a/package/W801Device/W801_GPIO.c b/package/W801Device/W801_GPIO.c new file mode 100644 index 000000000..54ef43f7e --- /dev/null +++ b/package/W801Device/W801_GPIO.c @@ -0,0 +1,169 @@ +#include "wm_hal.h" +#include +#include "BaseObj.h" +#include "dataStrs.h" +#include "W801Device_GPIO.h" + +void W801Device_GPIO_platformDisable(PikaObj *self) +{ + + char* pin = obj_getStr(self, "pin"); + char* mode = obj_getStr(self, "mode"); + + GPIO_TypeDef* gpioPort = GPIO_get_Group(pin); + + if (NULL == gpioPort) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] not match gpio port."); + } + + uint32_t gpioPin = GPIO_get_pin(pin); + + if (0 == gpioPin) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] not match gpio pin."); + } + + HAL_GPIO_DeInit(gpioPort,gpioPin); +} + + +void W801Device_GPIO_platformEnable(PikaObj *self) +{ + char* pin = obj_getStr(self, "pin"); + char* mode = obj_getStr(self, "mode"); + + if (0 != GPIO_enable_clock(pin)) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] not match gpio port."); + return; + } + + GPIO_TypeDef* gpioPort = GPIO_get_Group(pin); + + if (NULL == gpioPort) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] not match gpio port."); + } + + uint32_t gpioPin = GPIO_get_pin(pin); + + if (0 == gpioPin) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] not match gpio pin."); + } + + /*Configure GPIO pin Output Level */ + + HAL_GPIO_WritePin(gpioPort,gpioPin,GPIO_PIN_RESET); + + uint32_t pinMode = getPinMode(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 = gpioPull; + HAL_GPIO_Init(gpioPort, &GPIO_InitStruct); + +} + +void W801Device_GPIO_platformHigh(PikaObj *self) +{ + char* pin = obj_getStr(self, "pin"); + GPIO_TypeDef* gpioPort = GPIO_get_Group(pin); + + if (NULL == gpioPort) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] not match gpio port."); + } + + uint32_t gpioPin = GPIO_get_pin(pin); + + if (0 == gpioPin) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] not match gpio pin."); + } + HAL_GPIO_WritePin(gpioPort, gpioPin,GPIO_PIN_SET); + +} + +void W801Device_GPIO_platformLow(PikaObj *self) +{ + char* pin = obj_getStr(self, "pin"); + GPIO_TypeDef* gpioPort = GPIO_get_Group(pin); + + if (NULL == gpioPort) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] not match gpio port."); + } + + uint32_t gpioPin = GPIO_get_pin(pin); + + if (0 == gpioPin) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] not match gpio pin."); + } + HAL_GPIO_WritePin(gpioPort, gpioPin,GPIO_PIN_RESET); +} + +void W801Device_GPIO_platformRead(PikaObj *self) +{ + char* pin = obj_getStr(self, "pin"); + GPIO_TypeDef* gpioPort = GPIO_get_Group(pin); + if (NULL == gpioPort) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] not match gpio port."); + } + uint32_t gpioPin = GPIO_get_pin(pin); + if (0 == gpioPin) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] not match gpio pin."); + } + obj_setInt(self, "readBuff", HAL_GPIO_ReadPin(gpioPort, gpioPin)); +} + +void W801Device_GPIO_platformSetMode(PikaObj *self) +{ + char* pin = obj_getStr(self, "pin"); + char* mode = obj_getStr(self, "mode"); + if (0 != GPIO_enable_clock(pin)) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] not match gpio port."); + return; + } + + GPIO_TypeDef* gpioPort = GPIO_get_Group(pin); + + if (NULL == gpioPort) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] not match gpio port."); + } + + uint32_t gpioPin = GPIO_get_pin(pin); + + if (0 == gpioPin) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] not match gpio pin."); + } + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(gpioPort, gpioPin,GPIO_PIN_RESET); + + uint32_t pinMode = getPinMode(mode); + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + /*Configure GPIO*/ + GPIO_InitStruct.Pin = gpioPin; + GPIO_InitStruct.Mode = pinMode; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(gpioPort, &GPIO_InitStruct); +} \ No newline at end of file diff --git a/package/W801Device/W801_common.c b/package/W801Device/W801_common.c new file mode 100644 index 000000000..d7c6c34e5 --- /dev/null +++ b/package/W801Device/W801_common.c @@ -0,0 +1,196 @@ +#include "W801_common.h" +#include "dataStrs.h" + +//void delay_unit(uint32_t delays) { +// /* one unit is 1/64 us */ +// uint32_t startval, tickn, wait; +// +// startval = SysTick->VAL; +// tickn = HAL_GetTick(); +// if (delays > startval) { +// while (HAL_GetTick() == tickn) { +// } +// wait = 64000 + startval - delays; +// while (wait < SysTick->VAL) { +// } +// } else { +// wait = startval - delays; +// while (wait < SysTick->VAL && HAL_GetTick() == tickn) { +// } +// } +//} + +GPIO_TypeDef* GPIO_get_Group(char* pin) { + if (strIsStartWith(pin, "PA")) { + return GPIOA; + } + if (strIsStartWith(pin, "PB")) { + return GPIOB; + } + + return NULL; +} + +uint32_t GPIO_get_pin(char* pin) { + Args* buffs = New_strBuff(); + uint32_t gpioPin = 0; + + pin = strsCopy(buffs, pin + 2); + if (strEqu(pin, "0")) { + gpioPin = GPIO_PIN_0; + goto exit; + } + if (strEqu(pin, "1")) { + gpioPin = GPIO_PIN_1; + goto exit; + } + if (strEqu(pin, "2")) { + gpioPin = GPIO_PIN_2; + goto exit; + } + if (strEqu(pin, "3")) { + gpioPin = GPIO_PIN_3; + goto exit; + } + if (strEqu(pin, "4")) { + gpioPin = GPIO_PIN_4; + goto exit; + } + if (strEqu(pin, "5")) { + gpioPin = GPIO_PIN_5; + goto exit; + } + if (strEqu(pin, "6")) { + gpioPin = GPIO_PIN_6; + goto exit; + } + if (strEqu(pin, "7")) { + gpioPin = GPIO_PIN_7; + goto exit; + } + if (strEqu(pin, "8")) { + gpioPin = GPIO_PIN_8; + goto exit; + } + if (strEqu(pin, "9")) { + gpioPin = GPIO_PIN_9; + goto exit; + } + if (strEqu(pin, "10")) { + gpioPin = GPIO_PIN_10; + goto exit; + } + if (strEqu(pin, "11")) { + gpioPin = GPIO_PIN_11; + goto exit; + } + if (strEqu(pin, "12")) { + gpioPin = GPIO_PIN_12; + goto exit; + } + if (strEqu(pin, "13")) { + gpioPin = GPIO_PIN_13; + goto exit; + } + if (strEqu(pin, "14")) { + gpioPin = GPIO_PIN_14; + goto exit; + } + if (strEqu(pin, "15")) { + gpioPin = GPIO_PIN_15; + goto exit; + } + + if (strEqu(pin, "16")) { + gpioPin = GPIO_PIN_16; + goto exit; + } + if (strEqu(pin, "17")) { + gpioPin = GPIO_PIN_17; + goto exit; + } + if (strEqu(pin, "18")) { + gpioPin = GPIO_PIN_18; + goto exit; + } + if (strEqu(pin, "19")) { + gpioPin = GPIO_PIN_19; + goto exit; + } + if (strEqu(pin, "20")) { + gpioPin = GPIO_PIN_20; + goto exit; + } + if (strEqu(pin, "21")) { + gpioPin = GPIO_PIN_21; + goto exit; + } + if (strEqu(pin, "22")) { + gpioPin = GPIO_PIN_22; + goto exit; + } + if (strEqu(pin, "23")) { + gpioPin = GPIO_PIN_23; + goto exit; + } + if (strEqu(pin, "24")) { + gpioPin = GPIO_PIN_24; + goto exit; + } + if (strEqu(pin, "25")) { + gpioPin = GPIO_PIN_25; + goto exit; + } + if (strEqu(pin, "26")) { + gpioPin = GPIO_PIN_26; + goto exit; + } + if (strEqu(pin, "27")) { + gpioPin = GPIO_PIN_27; + goto exit; + } + if (strEqu(pin, "28")) { + gpioPin = GPIO_PIN_28; + goto exit; + } + if (strEqu(pin, "29")) { + gpioPin = GPIO_PIN_29; + goto exit; + } + if (strEqu(pin, "30")) { + gpioPin = GPIO_PIN_30; + goto exit; + } + if (strEqu(pin, "31")) { + gpioPin = GPIO_PIN_31; + goto exit; + } + + +exit: + args_deinit(buffs); + return gpioPin; +} + +uint32_t getPinMode(char* mode) { + if (strEqu(mode, "out")) { + return GPIO_MODE_OUTPUT; + } + if (strEqu(mode, "in")) { + return GPIO_MODE_INPUT; + } + return NULL; +} + +uint8_t GPIO_enable_clock(char* pin) { + + if (strIsStartWith(pin, "PA")) { + __HAL_RCC_GPIO_CLK_ENABLE(); + return 0; + } + if (strIsStartWith(pin, "PB")) { + __HAL_RCC_GPIO_CLK_ENABLE(); + return 0; + } + return 1; +} diff --git a/package/W801Device/W801_common.h b/package/W801Device/W801_common.h new file mode 100644 index 000000000..5da76f69a --- /dev/null +++ b/package/W801Device/W801_common.h @@ -0,0 +1,70 @@ +#ifndef __W801__COMMON__H +#define __W801__COMMON__H +#include "PikaObj.h" +//#include "main.h" +#include "wm_hal.h" +#include + +/* support std lib for w801 */ +#define delay_ms HAL_Delay + +#undef u16 +#undef u8 +#undef u32 +#define u16 uint16_t +#define u8 uint8_t +#define u32 uint32_t + +#define GPIO_Pin_0 GPIO_PIN_0 +#define GPIO_Pin_1 GPIO_PIN_1 +#define GPIO_Pin_2 GPIO_PIN_2 +#define GPIO_Pin_3 GPIO_PIN_3 +#define GPIO_Pin_4 GPIO_PIN_4 +#define GPIO_Pin_5 GPIO_PIN_5 +#define GPIO_Pin_6 GPIO_PIN_6 +#define GPIO_Pin_7 GPIO_PIN_7 +#define GPIO_Pin_8 GPIO_PIN_8 +#define GPIO_Pin_9 GPIO_PIN_9 +#define GPIO_Pin_10 GPIO_PIN_10 +#define GPIO_Pin_11 GPIO_PIN_11 +#define GPIO_Pin_12 GPIO_PIN_12 +#define GPIO_Pin_13 GPIO_PIN_13 +#define GPIO_Pin_14 GPIO_PIN_14 +#define GPIO_Pin_15 GPIO_PIN_15 +#define GPIO_Pin_16 GPIO_PIN_16 +#define GPIO_Pin_17 GPIO_PIN_17 +#define GPIO_Pin_18 GPIO_PIN_18 +#define GPIO_Pin_19 GPIO_PIN_19 +#define GPIO_Pin_20 GPIO_PIN_20 +#define GPIO_Pin_21 GPIO_PIN_21 +#define GPIO_Pin_22 GPIO_PIN_22 +#define GPIO_Pin_23 GPIO_PIN_23 +#define GPIO_Pin_24 GPIO_PIN_24 +#define GPIO_Pin_25 GPIO_PIN_25 +#define GPIO_Pin_26 GPIO_PIN_26 +#define GPIO_Pin_27 GPIO_PIN_27 +#define GPIO_Pin_28 GPIO_PIN_28 +#define GPIO_Pin_29 GPIO_PIN_29 +#define GPIO_Pin_30 GPIO_PIN_30 +#define GPIO_Pin_31 GPIO_PIN_31 + + +typedef struct pika_IIC_info_t { + GPIO_TypeDef* SCL_GPIO; + GPIO_TypeDef* SDA_GPIO; + + uint32_t SCL_GPIO_Pin; + uint32_t SDA_GPIO_Pin; + uint8_t deviceAddr; + + uint8_t readBuff[32]; +} pika_IIC_info; + +GPIO_TypeDef* GPIO_get_Group(char* pin); +uint32_t GPIO_get_pin(char* pin); +uint32_t getPinMode(char* mode); +uint8_t GPIO_enable_clock(char* pin); +//void delay_us(uint32_t delay); +//void delay_unit(uint32_t delay); + +#endif