pikapython/package/STM32F1/STM32F1_GPIO.c

172 lines
4.8 KiB
C
Raw Normal View History

2021-12-05 22:13:32 +08:00
#include <stdint.h>
#include "BaseObj.h"
#include "STM32F1_common.h"
#include "dataStrs.h"
2021-12-11 20:12:20 +08:00
2021-12-05 22:13:32 +08:00
void STM32F1_GPIO_platformDisable(PikaObj* self) {
char* pin = obj_getStr(self, "pin");
char* mode = obj_getStr(self, "mode");
2021-12-11 20:12:20 +08:00
GPIO_TypeDef* gpioPort = getGpioPort(pin);
2021-12-05 22:13:32 +08:00
if (NULL == gpioPort) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio port.");
}
2021-12-11 20:12:20 +08:00
uint16_t gpioPin = getGpioPin(pin);
2021-12-05 22:13:32 +08:00
if (0 == gpioPin) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio pin.");
}
2021-12-11 20:12:20 +08:00
HAL_GPIO_DeInit(gpioPort, gpioPin);
2021-12-05 22:13:32 +08:00
}
void STM32F1_GPIO_platformEnable(PikaObj* self) {
char* pin = obj_getStr(self, "pin");
char* mode = obj_getStr(self, "mode");
2021-12-11 20:12:20 +08:00
if (0 != enableClk(pin)) {
2021-12-05 22:13:32 +08:00
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio port.");
return;
}
2021-12-11 20:12:20 +08:00
GPIO_TypeDef* gpioPort = getGpioPort(pin);
2021-12-05 22:13:32 +08:00
if (NULL == gpioPort) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio port.");
}
2021-12-11 20:12:20 +08:00
uint16_t gpioPin = getGpioPin(pin);
2021-12-05 22:13:32 +08:00
if (0 == gpioPin) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio pin.");
}
/*Configure GPIO pin Output Level */
2021-12-11 20:12:20 +08:00
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_RESET);
2021-12-05 22:13:32 +08:00
uint32_t pinMode = getPinMode(mode);
uint32_t gpioPull = GPIO_NOPULL;
2021-12-11 20:12:20 +08:00
char *pull = obj_getStr(self, "pull");
if(strEqu(pull, "up")){
2021-12-05 22:13:32 +08:00
gpioPull = GPIO_PULLUP;
2021-12-11 20:12:20 +08:00
}else if(strEqu(pull, "down")){
2021-12-05 22:13:32 +08:00
gpioPull = GPIO_PULLDOWN;
}
2021-12-11 20:12:20 +08:00
2021-12-05 22:13:32 +08:00
GPIO_InitTypeDef GPIO_InitStruct = {0};
/*Configure GPIO*/
GPIO_InitStruct.Pin = gpioPin;
GPIO_InitStruct.Mode = pinMode;
GPIO_InitStruct.Pull = gpioPull;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(gpioPort, &GPIO_InitStruct);
}
void STM32F1_GPIO_platformLow(PikaObj* self) {
char* pin = obj_getStr(self, "pin");
2021-12-11 20:12:20 +08:00
GPIO_TypeDef* gpioPort = getGpioPort(pin);
2021-12-05 22:13:32 +08:00
if (NULL == gpioPort) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio port.");
}
2021-12-11 20:12:20 +08:00
uint16_t gpioPin = getGpioPin(pin);
2021-12-05 22:13:32 +08:00
if (0 == gpioPin) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio pin.");
}
2021-12-11 20:12:20 +08:00
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_RESET);
2021-12-05 22:13:32 +08:00
}
void STM32F1_GPIO_platformHigh(PikaObj* self) {
char* pin = obj_getStr(self, "pin");
2021-12-11 20:12:20 +08:00
GPIO_TypeDef* gpioPort = getGpioPort(pin);
2021-12-05 22:13:32 +08:00
if (NULL == gpioPort) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio port.");
}
2021-12-11 20:12:20 +08:00
uint16_t gpioPin = getGpioPin(pin);
2021-12-05 22:13:32 +08:00
if (0 == gpioPin) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio pin.");
}
2021-12-11 20:12:20 +08:00
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_SET);
2021-12-05 22:13:32 +08:00
}
void STM32F1_GPIO_platformSetMode(PikaObj* self) {
char* pin = obj_getStr(self, "pin");
2021-12-11 20:12:20 +08:00
char *mode = obj_getStr(self, "mode");
if (0 != enableClk(pin)) {
2021-12-05 22:13:32 +08:00
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio port.");
return;
}
2021-12-11 20:12:20 +08:00
GPIO_TypeDef* gpioPort = getGpioPort(pin);
2021-12-05 22:13:32 +08:00
if (NULL == gpioPort) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio port.");
}
2021-12-11 20:12:20 +08:00
uint16_t gpioPin = getGpioPin(pin);
2021-12-05 22:13:32 +08:00
if (0 == gpioPin) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio pin.");
}
/*Configure GPIO pin Output Level */
2021-12-11 20:12:20 +08:00
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_RESET);
2021-12-05 22:13:32 +08:00
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;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(gpioPort, &GPIO_InitStruct);
}
2021-12-11 20:12:20 +08:00
void STM32F1_GPIO_platformRead(PikaObj *self){
2021-12-05 22:13:32 +08:00
char* pin = obj_getStr(self, "pin");
2021-12-11 20:12:20 +08:00
GPIO_TypeDef* gpioPort = getGpioPort(pin);
2021-12-05 22:13:32 +08:00
if (NULL == gpioPort) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio port.");
}
2021-12-11 20:12:20 +08:00
uint16_t gpioPin = getGpioPin(pin);
2021-12-05 22:13:32 +08:00
if (0 == gpioPin) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio pin.");
}
2021-12-11 20:12:20 +08:00
obj_setInt(self, "readBuff", HAL_GPIO_ReadPin(gpioPort,gpioPin));
2021-12-05 22:13:32 +08:00
}
2021-12-11 20:12:20 +08:00
int STM32F1_lowLevel_readPin(PikaObj *self, char * pin){
GPIO_TypeDef* gpioPort = getGpioPort(pin);
2021-12-05 22:13:32 +08:00
if (NULL == gpioPort) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio port.");
}
2021-12-11 20:12:20 +08:00
uint16_t gpioPin = getGpioPin(pin);
2021-12-05 22:13:32 +08:00
if (0 == gpioPin) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio pin.");
}
2021-12-11 20:12:20 +08:00
return HAL_GPIO_ReadPin(gpioPort,gpioPin);
2021-12-12 00:30:04 +08:00
}