add package

This commit is contained in:
graduateDesign 2021-10-01 20:00:15 +08:00
parent 67d45c44a1
commit 2a87ed7595
24 changed files with 2225 additions and 3 deletions

View File

@ -0,0 +1,21 @@
from PikaObj import *
import PikaStdLib
import STM32
class RGB(PikaStdLib.SysObj):
pin = STM32.GPIO()
def init():
pass
def enable():
pass
def red():
pass
def green():
pass
def blue():
pass
def white():
pass
def flow():
pass

View File

@ -0,0 +1,95 @@
#include "PikaPiZero_RGB.h"
#include <stdint.h>
#include "BaseObj.h"
#include "RGB_ASM.h"
#include "STM32_common.h"
#include "dataStrs.h"
void RGB_reset() {
GPIOB->BRR = GPIO_PIN_12; // reset
delay_us(50);
}
#define RED 0x000100
#define GREEN 0x010000
#define BLUE 0x000001
#define WHITE 0x010101
#define CUTDOWN 0x000000
void RGB_setVoid() {
__asm("nop");
}
void PikaPiZero_RGB_enable(PikaObj* self) {
obj_run(self, "pin.init()");
obj_run(self, "pin.setPin('PB12')");
obj_run(self, "pin.setMode('out')");
obj_run(self, "pin.enable()");
}
void PikaPiZero_RGB_init(PikaObj* self) {}
void PikaPiZero_RGB_red(PikaObj* self) {
RGB_set(RED);
RGB_set(RED);
RGB_set(RED);
RGB_set(RED);
}
void PikaPiZero_RGB_blue(PikaObj* self) {
RGB_set(BLUE);
RGB_set(BLUE);
RGB_set(BLUE);
RGB_set(BLUE);
}
void PikaPiZero_RGB_green(PikaObj* self) {
RGB_set(GREEN);
RGB_set(GREEN);
RGB_set(GREEN);
RGB_set(GREEN);
}
void PikaPiZero_RGB_white(PikaObj* self) {
RGB_set(WHITE);
RGB_set(WHITE);
RGB_set(WHITE);
RGB_set(WHITE);
}
void PikaPiZero_RGB_flow(PikaObj* self) {
if (!obj_isArgExist(self, "flowState")) {
obj_setInt(self, "flowState", 0);
}
int flowState = obj_getInt(self, "flowState");
if (0 == flowState) {
RGB_set(RED);
RGB_set(BLUE);
RGB_set(GREEN);
RGB_set(WHITE);
goto exit;
}
if (1 == flowState) {
RGB_set(BLUE);
RGB_set(GREEN);
RGB_set(WHITE);
RGB_set(RED);
goto exit;
}
if (2 == flowState) {
RGB_set(GREEN);
RGB_set(WHITE);
RGB_set(RED);
RGB_set(BLUE);
goto exit;
}
if (3 == flowState) {
RGB_set(WHITE);
RGB_set(RED);
RGB_set(BLUE);
RGB_set(GREEN);
goto exit;
}
exit:
obj_setInt(self, "flowState", (flowState + 1) % 4);
}

View File

@ -0,0 +1,38 @@
#include "RGB_ASM.h"
#include "STM32_common.h"
static void RGB_setUp() {
GPIOB->BSRR = GPIO_PIN_12; // set
int i;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
GPIOB->BRR = GPIO_PIN_12; // reset
}
static void RGB_setDown() {
GPIOB->BSRR = GPIO_PIN_12; // set
int i;
i++;
i++;
i++;
GPIOB->BRR = GPIO_PIN_12;
}
void RGB_set(uint32_t G8R8B8) {
int i;
uint8_t byte = 0;
for (i = 23; i >= 0; i--) {
byte = ((G8R8B8 >> i) & 0x01);
if (byte) {
RGB_setUp();
} else {
RGB_setDown();
}
}
}

View File

@ -0,0 +1,3 @@
#include <stdint.h>
void RGB_set(uint32_t G8R8B8);

Binary file not shown.

View File

@ -0,0 +1,80 @@
#include "BaseObj.h"
#include "PikaStdDevice_GPIO.h"
void PikaStdDevice_GPIO_init(PikaObj* self) {
obj_setInt(self, "isEnable", 0);
obj_setStr(self, "pin", "PA0");
obj_setStr(self, "mode", "out");
obj_setInt(self, "isOn", 0);
}
void PikaStdDevice_GPIO_disable(PikaObj* self) {
obj_setInt(self, "isEnable", 0);
obj_run(self, "platformDisable()");
}
void PikaStdDevice_GPIO_enable(PikaObj* self) {
obj_setInt(self, "isEnable", 1);
obj_run(self, "platformEnable()");
}
char* PikaStdDevice_GPIO_getMode(PikaObj* self) {
return obj_getStr(self, "mode");
}
char* PikaStdDevice_GPIO_getPin(PikaObj* self) {
return obj_getStr(self, "pin");
}
void PikaStdDevice_GPIO_low(PikaObj* self) {
obj_setInt(self, "isOn", 0);
obj_run(self, "platformLow()");
}
void PikaStdDevice_GPIO_high(PikaObj* self) {
obj_setInt(self, "isOn", 1);
obj_run(self, "platformHigh()");
}
void PikaStdDevice_GPIO_setMode(PikaObj* self, char* mode) {
obj_setStr(self, "mode", mode);
obj_run(self, "platformSetMode(mode)");
}
void PikaStdDevice_GPIO_setPin(PikaObj* self, char* pinName) {
obj_setStr(self, "pin", pinName);
}
void PikaStdDevice_GPIO_platformDisable(PikaObj* self) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}
void PikaStdDevice_GPIO_platformEnable(PikaObj* self) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}
void PikaStdDevice_GPIO_platformLow(PikaObj* self) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}
void PikaStdDevice_GPIO_platformHigh(PikaObj* self) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}
void PikaStdDevice_GPIO_platformSetMode(PikaObj* self, char* mode) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}
void PikaStdDevice_GPIO_platformOff(PikaObj* self) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}
void PikaStdDevice_GPIO_platformOn(PikaObj* self) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}

View File

@ -0,0 +1,149 @@
from PikaObj import *
class GPIO(TinyObj):
def init():
pass
def setPin(pinName: str):
pass
def getPin() -> str:
pass
def setMode(mode: str):
pass
def getMode() -> str:
pass
def enable():
pass
def disable():
pass
def high():
pass
def low():
pass
# 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(mode: str):
pass
class Time(TinyObj):
# need override
def sleep_s(s: int):
pass
# need override
def sleep_ms(ms: int):
pass
class ADC(TinyObj):
def init():
pass
def setPin(pin: str):
pass
def enable():
pass
def read() -> float:
pass
# need override
def platformEnable(pin: str):
pass
# need override
def platformRead(pin: str) -> float:
pass
class UART(TinyObj):
def init():
pass
def setBaudRate(baudRate: int):
pass
def setId(id: int):
pass
def enable():
pass
def write(data: str):
pass
def read(length: int) -> str:
pass
# need override
def platformEnable(id: int, baudRate: int):
pass
# need override
def platformWrite(id: int, data: str):
pass
# need override
def platformRead(id: int, length: int) -> str:
pass
class PWM(TinyObj):
def init():
pass
def setPin(pin: str):
pass
def setFrequency(freq: int):
pass
def setDuty(duty: float):
pass
def enable():
pass
def getFrequency() -> int:
pass
def getDuty() -> float:
pass
# need override
def platformEnable(pin: str, freq: int, duty: float):
pass
# need override
def platformSetFrequency(pin: str, freq: int):
pass
# need override
def platformSetDuty(pin: str, duty: float):
pass

View File

@ -0,0 +1,30 @@
#include "PikaStdDevice_ADC.h"
#include "BaseObj.h"
void PikaStdDevice_ADC_enable(PikaObj* self) {
obj_run(self, "platformEnable(pin)");
}
void PikaStdDevice_ADC_init(PikaObj* self) {
obj_setStr(self, "pin", "PA0");
}
float PikaStdDevice_ADC_read(PikaObj* self) {
obj_run(self, "val = platformRead(pin)");
return obj_getFloat(self, "val");
}
void PikaStdDevice_ADC_setPin(PikaObj* self, char* pin) {
obj_setStr(self, "pin", pin);
}
void PikaStdDevice_ADC_platformEnable(PikaObj* self, char* pin) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}
float PikaStdDevice_ADC_platformRead(PikaObj* self, char* pin) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
return -1;
}

View File

@ -0,0 +1,52 @@
#include "PikaStdDevice_PWM.h"
#include "BaseObj.h"
void PikaStdDevice_PWM_init(PikaObj* self) {
obj_setStr(self, "pin", "PA8");
obj_setInt(self, "freq", 1000);
obj_setFloat(self, "duty", 0.5f);
}
void PikaStdDevice_PWM_setPin(PikaObj* self, char* pin) {
obj_setStr(self, "pin", pin);
}
void PikaStdDevice_PWM_setFrequency(PikaObj* self, int freq) {
obj_setInt(self, "freq", freq);
obj_run(self, "platformSetFrequency(pin, freq)");
}
void PikaStdDevice_PWM_setDuty(PikaObj* self, float duty) {
obj_setFloat(self, "duty", duty);
obj_run(self, "platformSetDuty(pin, duty)");
}
void PikaStdDevice_PWM_enable(PikaObj* self) {
obj_run(self, "platformEnable(pin, freq, duty)");
}
float PikaStdDevice_PWM_getDuty(PikaObj* self) {
return obj_getFloat(self, "duty");
}
int PikaStdDevice_PWM_getFrequency(PikaObj* self) {
return obj_getInt(self, "freq");
}
void PikaStdDevice_PWM_platformEnable(PikaObj* self,
float dute,
int freq,
char* pin) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}
void PikaStdDevice_PWM_platformSetDuty(PikaObj* self, float duty, char* pin) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}
void PikaStdDevice_PWM_platformSetFrequency(PikaObj* self,
int freq,
char* pin) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}

View File

@ -0,0 +1,11 @@
#include "PikaStdDevice_Time.h"
#include "BaseObj.h"
void PikaStdDevice_Time_sleep_ms(PikaObj* self, int ms) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}
void PikaStdDevice_Time_sleep_s(PikaObj* self, int s) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}

View File

@ -0,0 +1,40 @@
#include "PikaStdDevice_UART.h"
#include "BaseObj.h"
void PikaStdDevice_UART_enable(PikaObj* self) {
obj_run(self, "platformEnable(id, baudRate)");
}
void PikaStdDevice_UART_init(PikaObj* self) {
obj_setInt(self, "baudRate", 115200);
obj_setInt(self, "id", 1);
obj_setStr(self, "readBuff", "");
}
char* PikaStdDevice_UART_read(PikaObj* self, int length) {
obj_setInt(self, "length", length);
obj_run(self, "readData = platformRead(id, length)");
return obj_getStr(self, "readData");
}
void PikaStdDevice_UART_setBaudRate(PikaObj* self, int baudRate) {
obj_setInt(self, "baudRate", baudRate);
}
void PikaStdDevice_UART_setId(PikaObj* self, int id) {
obj_setInt(self, "id", id);
}
void PikaStdDevice_UART_write(PikaObj* self, char* data) {
obj_setStr(self, "writeData", data);
obj_run(self, "platformWrite(id, writeData)");
}
void PikaStdDevice_UART_platformEnable(PikaObj* self, int baudRate, int id) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}
char* PikaStdDevice_UART_platformRead(PikaObj* self, int id, int length) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
return NULL;
}
void PikaStdDevice_UART_platformWrite(PikaObj* self, char* data, int id) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}

View File

@ -0,0 +1,27 @@
from PikaObj import *
class MemChecker(BaseObj):
def max():
pass
def now():
pass
def resetMax():
pass
class SysObj(BaseObj):
def type(argPath: str):
pass
def ls(objPath: str):
pass
def remove(argPath: str):
pass
def new(objPath: str, classPath: str):
pass

View File

@ -0,0 +1,14 @@
#include "BaseObj.h"
#include "dataStrs.h"
void PikaStdLib_MemChecker_max(PikaObj* self) {
obj_sysPrintf(self, "%0.2f kB", pikaMemMax() / 1024.0);
}
void PikaStdLib_MemChecker_now(PikaObj* self) {
obj_sysPrintf(self, "%0.2f kB", pikaMemNow() / 1024.0);
}
void PikaStdLib_MemChecker_resetMax(PikaObj* self) {
pikaMemMaxReset();
}

View File

@ -0,0 +1,98 @@
#include "BaseObj.h"
#include "dataStrs.h"
static int32_t __foreach_listEachArg(Arg* argEach, Args* handleArgs) {
Args* buffs = handleArgs;
if (NULL == handleArgs) {
/* error: not handleArgs input */
return 1;
}
char* argName = strsCopy(buffs, arg_getName(argEach));
if (strIsStartWith(argName, "[")) {
/* skip */
return 0;
}
char* stringOut = args_getStr(handleArgs, "stringOut");
if (NULL == stringOut) {
// stringOut no found
return 1;
}
stringOut = strsAppend(buffs, stringOut, argName);
stringOut = strsAppend(buffs, stringOut, " ");
args_setStr(handleArgs, "stringOut", stringOut);
return 0;
}
void PikaStdLib_SysObj_ls(PikaObj* self, char* objPath) {
obj_setErrorCode(self, 0);
Args* args = New_args(NULL);
args_setStr(args, "stringOut", "");
obj_setSysOut(self, "");
if (NULL == objPath) {
/* no input obj path, use current obj */
args_foreach(self->attributeList, __foreach_listEachArg, args);
obj_setSysOut(self, args_getStr(args, "stringOut"));
goto exit;
}
PikaObj* obj = obj_getObj(self, objPath, 0);
if (NULL == obj) {
/* do not find obj */
obj_setSysOut(self, "[error] list: object no found.");
obj_setErrorCode(self, 1);
goto exit;
}
/* list args */
args_foreach(obj->attributeList, __foreach_listEachArg, args);
obj_setSysOut(self, args_getStr(args, "stringOut"));
exit:
args_deinit(args);
}
void PikaStdLib_SysObj_new(PikaObj* self, char* classPath, char* objPath) {
int32_t res = obj_newObj(self, objPath, classPath);
if (1 == res) {
obj_setSysOut(self, "[error] new: class not found .");
obj_setErrorCode(self, 1);
return;
}
}
void PikaStdLib_SysObj_remove(PikaObj* self, char* argPath) {
obj_setErrorCode(self, 0);
int32_t res = obj_removeArg(self, argPath);
if (1 == res) {
obj_setSysOut(self, "[error] del: object no found.");
obj_setErrorCode(self, 1);
return;
}
if (2 == res) {
obj_setSysOut(self, "[error] del: arg not match.");
obj_setErrorCode(self, 2);
return;
}
}
void PikaStdLib_SysObj_type(PikaObj* self, char* argPath) {
if (NULL == argPath) {
/* no input obj path, use current obj */
PikaObj* objHost = obj_getContext(self);
Arg* objArg = obj_getArg(objHost, obj_getStr(self, "_n"));
if (NULL == objArg) {
obj_setSysOut(self, "[error] type: arg no found.");
obj_setErrorCode(self, 1);
return;
}
obj_setSysOut(self, arg_getType(objArg));
return;
}
Arg* arg = obj_getArg(self, argPath);
if (NULL == arg) {
obj_setSysOut(self, "[error] type: arg no found.");
obj_setErrorCode(self, 1);
return;
}
obj_setSysOut(self, arg_getType(arg));
}

71
package/STM32/STM32.py Normal file
View File

@ -0,0 +1,71 @@
from typing import overload
import PikaStdDevice
class GPIO(PikaStdDevice.GPIO):
# override
def platformHigh():
pass
# override
def platformLow():
pass
# override
def platformEnable():
pass
# override
def platformDisable():
pass
# override
def platformSetMode(mode: str):
pass
class Time(PikaStdDevice.Time):
# override
def sleep_s(s: int):
pass
# override
def sleep_ms(ms: int):
pass
class ADC(PikaStdDevice.ADC):
# override
def platformEnable(pin: str):
pass
# override
def platformRead(pin: str) -> float:
pass
class UART(PikaStdDevice.UART):
# override
def platformEnable(id: int, baudRate: int):
pass
# override
def platformWrite(id: int, data: str):
pass
# override
def platformRead(id: int, length: int) -> str:
pass
class PWM(PikaStdDevice.PWM):
# override
def platformEnable(pin: str, freq: int, duty: float):
pass
# override
def platformSetFrequency(pin: str, freq: int):
pass
# override
def platformSetDuty(pin: str, duty: float):
pass

167
package/STM32/STM32_ADC.c Normal file
View File

@ -0,0 +1,167 @@
#include "STM32_ADC.h"
#include <stdint.h>
#include "BaseObj.h"
#include "STM32_common.h"
#include "dataStrs.h"
ADC_HandleTypeDef pika_hadc1 = {0};
uint16_t Get_Adc(ADC_HandleTypeDef* hadc, uint32_t ch) {
ADC_ChannelConfTypeDef ADC_ChanConf;
ADC_ChanConf.Channel = ch;
ADC_ChanConf.Rank = ADC_REGULAR_RANK_1;
#if (defined STM32G070xx) || (defined STM32G030xx)
ADC_ChanConf.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
#endif
#ifdef STM32F103xB
ADC_ChanConf.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
#endif
HAL_ADC_ConfigChannel(hadc, &ADC_ChanConf);
HAL_ADC_Start(hadc);
HAL_ADC_PollForConversion(hadc, 10);
return (uint16_t)HAL_ADC_GetValue(hadc);
}
void STM32_ADC_platformEnable(PikaObj* self, char* pin) {
if (!strIsStartWith(pin, "PA")) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match adc pin.");
return;
}
/* MSP Init */
#if (defined STM32G070xx) || (defined STM32G030xx)
__HAL_RCC_ADC_CLK_ENABLE();
#endif
#ifdef STM32F103xB
__HAL_RCC_ADC1_CLK_ENABLE();
#endif
if (0 != enableClk(pin)) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio port.");
return;
}
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = getGpioPin(pin);
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(getGpioPort(pin), &GPIO_InitStruct);
/* init ADC */
pika_hadc1.Instance = ADC1;
#if (defined STM32G070xx) || (defined STM32G030xx)
pika_hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
pika_hadc1.Init.Resolution = ADC_RESOLUTION_12B;
pika_hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
pika_hadc1.Init.LowPowerAutoWait = DISABLE;
pika_hadc1.Init.LowPowerAutoPowerOff = DISABLE;
#endif
pika_hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
pika_hadc1.Init.ContinuousConvMode = DISABLE;
pika_hadc1.Init.DiscontinuousConvMode = DISABLE;
pika_hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
pika_hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
pika_hadc1.Init.NbrOfConversion = 1;
#if (defined STM32G070xx) || (defined STM32G030xx)
pika_hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
pika_hadc1.Init.DMAContinuousRequests = DISABLE;
pika_hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
pika_hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_1CYCLE_5;
pika_hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_1CYCLE_5;
pika_hadc1.Init.OversamplingMode = DISABLE;
pika_hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
#endif
HAL_StatusTypeDef state = HAL_ADC_Init(&pika_hadc1);
if (state != HAL_OK) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] adc init faild.");
return;
}
/* Run the ADC calibration */
if (HAL_ADCEx_Calibration_Start(&pika_hadc1) != HAL_OK) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] adc calibratie faild.");
return;
}
}
uint32_t getChannel(char* pin) {
Args* buffs = New_strBuff();
uint32_t channel = 0;
pin = strsCopy(buffs, pin + 2);
if (strEqu(pin, "0")) {
channel = ADC_CHANNEL_0;
goto exit;
}
if (strEqu(pin, "1")) {
channel = ADC_CHANNEL_1;
goto exit;
}
if (strEqu(pin, "2")) {
channel = ADC_CHANNEL_2;
goto exit;
}
if (strEqu(pin, "3")) {
channel = ADC_CHANNEL_3;
goto exit;
}
if (strEqu(pin, "4")) {
channel = ADC_CHANNEL_4;
goto exit;
}
if (strEqu(pin, "5")) {
channel = ADC_CHANNEL_5;
goto exit;
}
if (strEqu(pin, "6")) {
channel = ADC_CHANNEL_6;
goto exit;
}
if (strEqu(pin, "7")) {
channel = ADC_CHANNEL_7;
goto exit;
}
if (strEqu(pin, "8")) {
channel = ADC_CHANNEL_8;
goto exit;
}
if (strEqu(pin, "9")) {
channel = ADC_CHANNEL_9;
goto exit;
}
if (strEqu(pin, "10")) {
channel = ADC_CHANNEL_10;
goto exit;
}
if (strEqu(pin, "11")) {
channel = ADC_CHANNEL_11;
goto exit;
}
if (strEqu(pin, "12")) {
channel = ADC_CHANNEL_12;
goto exit;
}
if (strEqu(pin, "13")) {
channel = ADC_CHANNEL_13;
goto exit;
}
if (strEqu(pin, "14")) {
channel = ADC_CHANNEL_14;
goto exit;
}
if (strEqu(pin, "15")) {
channel = ADC_CHANNEL_15;
goto exit;
}
exit:
args_deinit(buffs);
return channel;
}
float STM32_ADC_platformRead(PikaObj* self, char* pin) {
return 3.3f * Get_Adc(&pika_hadc1, getChannel(pin)) / 4096.0f;
}

137
package/STM32/STM32_Code.c Normal file
View File

@ -0,0 +1,137 @@
#include <stdint.h>
#include "BaseObj.h"
#include "STM32_common.h"
#include "dataStrs.h"
#include <stdlib.h>
CodeHeap codeHeap;
void STM32_Code_Init() {
codeHeap.size = 0;
codeHeap.content = pikaMalloc(codeHeap.size + 1);
codeHeap.ena = 0;
}
uint8_t STM32_Code_reciveHandler(char* data, uint32_t rxSize) {
char buff[RX_BUFF_LENGTH] = {0};
if (0 == codeHeap.ena) {
char* strLine = strGetLastLine(buff, data);
if (strIsStartWith(strLine, "import ")) {
codeHeap.reciveTime = uwTick;
codeHeap.ena = 1;
data = strLine;
rxSize = strGetSize(strLine);
}
}
if (1 == codeHeap.ena) {
codeHeap.reciveTime = uwTick;
codeHeap.oldSize = codeHeap.size;
codeHeap.size += rxSize;
codeHeap.content = realloc(codeHeap.content , codeHeap.size + 1);
memcpy(codeHeap.content + codeHeap.oldSize, data, rxSize);
codeHeap.content[codeHeap.size] = 0;
/* reciving code */
return 1;
}
/* not work */
return 0;
}
uint32_t GetPage(uint32_t Addr) {
return (Addr - FLASH_BASE) / FLASH_PAGE_SIZE;
}
void STM32_Code_flashHandler() {
if (!codeHeap.ena){
/* recive not activate */
return;
}
if ( uwTick - codeHeap.reciveTime < 200 ){
/* still reciving */
return;
}
/* transmite is finished */
uint32_t FirstPage = 0, NbOfPages = 0;
uint32_t PageError = 0;
__IO uint32_t data32 = 0, MemoryProgramStatus = 0;
static FLASH_EraseInitTypeDef EraseInitStruct = {0};
printf("==============[Programer]==============\r\n");
printf("[info]: Recived byte: %d\r\n", codeHeap.size);
printf("[info]: Programing... \r\n");
HAL_FLASH_Unlock();
/* Get the 1st page to erase */
FirstPage = GetPage(FLASH_CODE_START_ADDR);
/* Get the number of pages to erase from 1st page */
NbOfPages = GetPage(FLASH_USER_END_ADDR) - FirstPage + 1;
/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Page = FirstPage;
EraseInitStruct.NbPages = NbOfPages;
printf("[info]: Erasing flash... \r\n");
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK) {
printf("[error]: Erase faild! \r\n");
while (1) {
}
}
printf("[ OK ]: Erase flash ok! \r\n");
printf("[info]: Writing flash... \r\n");
uint32_t baseAddress = FLASH_CODE_START_ADDR;
uint32_t writeAddress = 0;
uint64_t writeData64 = 0;
while (writeAddress < codeHeap.size + 1) {
writeData64 = 0;
for (int i = 7; i >= 0; i--) {
char ch = codeHeap.content[writeAddress + i];
writeData64 = writeData64 << 8;
writeData64 += ch;
}
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,
baseAddress + writeAddress,
writeData64) == HAL_OK) {
writeAddress = writeAddress + 8;
} else {
printf("[error]: Write flash faild. \r\n");
while (1) {
}
}
}
HAL_FLASH_Lock();
printf("[ OK ]: Write flash ok! \r\n");
baseAddress = FLASH_CODE_START_ADDR;
MemoryProgramStatus = 0x0;
printf("[info]: Checking flash... \r\n");
char* codeInFlash = (char*)baseAddress;
printf("\r\n");
printf("----[code in flash]-----\r\n");
printf("%s", codeInFlash);
printf("----[code in flash]-----\r\n");
printf("\r\n");
if (!strEqu(codeInFlash, codeHeap.content)) {
printf("[error]: Check flash faild.\r\n");
printf("\r\n");
printf("\r\n\r\n");
printf("---------[code in heap]----------\r\n");
printf("\r\n");
printf("%s", codeHeap.content);
printf("\r\n\r\n");
printf("---------[code in heap]----------\r\n");
while (1) {
}
}
printf("[ OK ]: Checking flash ok! \r\n");
printf("[ OK ]: Programing ok! \r\n");
printf("[info]: Restarting... \r\n");
printf("==============[Programer]==============\r\n");
printf("\r\n");
HAL_NVIC_SystemReset();
}

142
package/STM32/STM32_GPIO.c Normal file
View File

@ -0,0 +1,142 @@
#include "STM32_GPIO.h"
#include <stdint.h>
#include "BaseObj.h"
#include "STM32_common.h"
#include "dataStrs.h"
void STM32_GPIO_platformDisable(PikaObj* self) {
char* pin = obj_getStr(self, "pin");
char* mode = obj_getStr(self, "mode");
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.");
}
HAL_GPIO_DeInit(gpioPort, gpioPin);
}
void STM32_GPIO_platformEnable(PikaObj* self) {
char* pin = obj_getStr(self, "pin");
char* mode = obj_getStr(self, "mode");
if (0 != enableClk(pin)) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio port.");
return;
}
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.");
}
/*Configure GPIO pin Output Level */
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*/
GPIO_InitStruct.Pin = gpioPin;
GPIO_InitStruct.Mode = pinMode;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(gpioPort, &GPIO_InitStruct);
}
void STM32_GPIO_platformLow(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.");
}
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_RESET);
}
void STM32_GPIO_platformHigh(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.");
}
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_SET);
}
void STM32_GPIO_platformSetMode(PikaObj* self, char* mode) {
char* pin = obj_getStr(self, "pin");
if (0 != enableClk(pin)) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] not match gpio port.");
return;
}
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.");
}
/*Configure GPIO pin Output Level */
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*/
GPIO_InitStruct.Pin = gpioPin;
GPIO_InitStruct.Mode = pinMode;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(gpioPort, &GPIO_InitStruct);
}

366
package/STM32/STM32_PWM.c Normal file
View File

@ -0,0 +1,366 @@
#include "STM32_PWM.h"
#include <stdint.h>
#include "BaseObj.h"
#include "STM32_common.h"
#include "dataStrs.h"
#ifdef TIM1_EXIST
TIM_HandleTypeDef pika_tim1;
#endif
#ifdef TIM2_EXIST
TIM_HandleTypeDef pika_tim2;
#endif
#ifdef TIM3_EXIST
TIM_HandleTypeDef pika_tim3;
#endif
#ifdef TIM4_EXIST
TIM_HandleTypeDef pika_tim4;
#endif
#ifdef TIM14_EXIST
TIM_HandleTypeDef pika_tim14;
#endif
#ifdef TIM16_EXIST
TIM_HandleTypeDef pika_tim16;
#endif
#ifdef TIM17_EXIST
TIM_HandleTypeDef pika_tim17;
#endif
static TIM_HandleTypeDef* getTimHandle(char* pin) {
#ifdef TIM1_EXIST
if (strEqu("PA8", pin) || strEqu("PA9", pin) || strEqu("PA10", pin) ||
strEqu("PA11", pin)) {
return &pika_tim1;
}
#endif
#ifdef TIM2_EXIST
if (strEqu("PA0", pin) || strEqu("PA1", pin) || strEqu("PA2", pin) ||
strEqu("PA3", pin)) {
return &pika_tim2;
}
#endif
#ifdef TIM3_EXIST
if (strEqu("PA6", pin) || strEqu("PA7", pin) || strEqu("PB0", pin) ||
strEqu("PB1", pin)) {
return &pika_tim3;
}
#endif
#ifdef TIM4_EXIST
if (strEqu("PB6", pin) || strEqu("PB7", pin) || strEqu("PB8", pin) ||
strEqu("PB9", pin)) {
return &pika_tim3;
}
#endif
#ifdef TIM14_EXIST
if (strEqu("PA4", pin)) {
return &pika_tim14;
}
#endif
#ifdef TIM16_EXIST
if (strEqu("PD0", pin)) {
return &pika_tim16;
}
#endif
#ifdef TIM17_EXIST
if (strEqu("PD1", pin)) {
return &pika_tim17;
}
#endif
return NULL;
}
static TIM_TypeDef* getTimInstance(char* pin) {
#ifdef TIM1_EXIST
if (strEqu("PA8", pin) || strEqu("PA9", pin) || strEqu("PA10", pin) ||
strEqu("PA11", pin)) {
return TIM1;
}
#endif
#ifdef TIM2_EXIST
if (strEqu("PA0", pin) || strEqu("PA1", pin) || strEqu("PA2", pin) ||
strEqu("PA3", pin)) {
return TIM2;
}
#endif
#ifdef TIM3_EXIST
if (strEqu("PA6", pin) || strEqu("PA7", pin) || strEqu("PB0", pin) ||
strEqu("PB1", pin)) {
return TIM3;
}
#endif
#ifdef TIM4_EXIST
if (strEqu("PB6", pin) || strEqu("PB7", pin) || strEqu("PB8", pin) ||
strEqu("PB9", pin)) {
return TIM4;
}
#endif
#ifdef TIM14_EXIST
if (strEqu("PA4", pin)) {
return TIM14;
}
#endif
#ifdef TIM16_EXIST
if (strEqu("PD0", pin)) {
return TIM16;
}
#endif
#ifdef TIM17_EXIST_EXIST
if (strEqu("PD1", pin)) {
return TIM17;
}
#endif
return NULL;
}
#if (defined STM32G030xx) || (defined STM32G070xx)
static uint32_t getGPIO_AlternateForTim(TIM_TypeDef* timInstance) {
#ifdef TIM1_EXIST
if (TIM1 == timInstance) {
return GPIO_AF2_TIM1;
}
#endif
#ifdef TIM3_EXIST
if (TIM3 == timInstance) {
return GPIO_AF1_TIM3;
}
#endif
#ifdef TIM14_EXIST
if (TIM14 == timInstance) {
return GPIO_AF4_TIM14;
}
#endif
#ifdef TIM16_EXIST
if (TIM16 == timInstance) {
return GPIO_AF2_TIM16;
}
#endif
#ifdef TIM17_EXIST
if (TIM17 == timInstance) {
return GPIO_AF2_TIM17;
}
#endif
return 0;
}
#endif
static void PWM_TimClockEnable(TIM_TypeDef* timInstance) {
#ifdef TIM1_EXIST
if (TIM1 == timInstance) {
__HAL_RCC_TIM1_CLK_ENABLE();
return;
}
#endif
#ifdef TIM2_EXIST
if (TIM2 == timInstance) {
__HAL_RCC_TIM2_CLK_ENABLE();
return;
}
#endif
#ifdef TIM3_EXIST
if (TIM3 == timInstance) {
__HAL_RCC_TIM3_CLK_ENABLE();
return;
}
#endif
#ifdef TIM4_EXIST
if (TIM4 == timInstance) {
__HAL_RCC_TIM4_CLK_ENABLE();
return;
}
#endif
#ifdef TIM14_EXIST
if (TIM14 == timInstance) {
__HAL_RCC_TIM14_CLK_ENABLE();
return;
}
#endif
#ifdef TIM16_EXIST
if (TIM16 == timInstance) {
__HAL_RCC_TIM16_CLK_ENABLE();
return;
}
#endif
#ifdef TIM17_EXIST
if (TIM17 == timInstance) {
__HAL_RCC_TIM17_CLK_ENABLE();
return;
}
#endif
}
uint8_t PWM_MspInit(char* pin) {
TIM_TypeDef* timInstance = getTimInstance(pin);
if (NULL == timInstance) {
/* this Pin do not match any PWM generator */
return 1;
}
enableClk(pin);
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = getGpioPin(pin);
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
#if (defined STM32G030xx) || (defined STM32G070xx)
GPIO_InitStruct.Alternate = getGPIO_AlternateForTim(timInstance);
#endif
HAL_GPIO_Init(getGpioPort(pin), &GPIO_InitStruct);
PWM_TimClockEnable(timInstance);
return 0;
}
uint32_t getTimChennel(char* pin) {
if (strEqu("PA8", pin) || strEqu("PA0", pin) || strEqu("PA6", pin) ||
strEqu("PB6", pin) || strEqu("PA4", pin) || strEqu("PD0", pin) ||
strEqu("PD1", pin)) {
return TIM_CHANNEL_1;
}
if (strEqu("PA9", pin) || strEqu("PA1", pin) || strEqu("PB7", pin) ||
strEqu("PA7", pin)) {
return TIM_CHANNEL_2;
}
if (strEqu("PA10", pin) || strEqu("PA2", pin) || strEqu("PB8", pin) ||
strEqu("PB0", pin)) {
return TIM_CHANNEL_3;
}
if (strEqu("PA11", pin) || strEqu("PA3", pin) || strEqu("PB9", pin) ||
strEqu("PB1", pin)) {
return TIM_CHANNEL_4;
}
/* Chennel not match */
return 99999;
}
void STM32_PWM_platformEnable(PikaObj* self, float duty, int freq, char* pin) {
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
if (0 != PWM_MspInit(pin)) {
obj_setSysOut(self, "[error]: init PWM port faild.");
obj_setErrorCode(self, 1);
return;
}
TIM_HandleTypeDef* pika_tim = getTimHandle(pin);
if (NULL == pika_tim) {
obj_setSysOut(self, "[error]: can not found PWM hardware.");
obj_setErrorCode(self, 1);
return;
}
pika_tim->Instance = getTimInstance(pin);
#if (defined STM32G030xx) || (defined STM32G070xx)
pika_tim->Init.Prescaler = 64 - 1;
#endif
#if (defined STM32F103xB)
pika_tim->Init.Prescaler = 72 - 1;
#endif
pika_tim->Init.CounterMode = TIM_COUNTERMODE_UP;
/* calculate period */
pika_tim->Init.Period = (uint32_t)((float)(1000 * 1000) / (float)freq) - 1;
pika_tim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
pika_tim->Init.RepetitionCounter = 0;
pika_tim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(pika_tim) != HAL_OK) {
obj_setSysOut(self, "[error]: init PWM faild.");
obj_setErrorCode(self, 1);
return;
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(pika_tim, &sClockSourceConfig) != HAL_OK) {
obj_setSysOut(self, "[error]: init PWM faild.");
obj_setErrorCode(self, 1);
return;
}
if (HAL_TIM_PWM_Init(pika_tim) != HAL_OK) {
obj_setSysOut(self, "[error]: init PWM faild.");
obj_setErrorCode(self, 1);
return;
}
if (HAL_TIM_OC_Init(pika_tim) != HAL_OK) {
obj_setSysOut(self, "[error]: init PWM faild.");
obj_setErrorCode(self, 1);
return;
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
#if (defined STM32G030xx) || (defined STM32G070xx)
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
#endif
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(pika_tim, &sMasterConfig) !=
HAL_OK) {
obj_setSysOut(self, "[error]: init PWM faild.");
obj_setErrorCode(self, 1);
return;
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
/* calculate pulse by duty and freq */
sConfigOC.Pulse = (uint32_t)(pika_tim->Init.Period * duty);
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_PWM_ConfigChannel(pika_tim, &sConfigOC, getTimChennel(pin)) !=
HAL_OK) {
obj_setSysOut(self, "[error]: init PWM faild.");
obj_setErrorCode(self, 1);
return;
}
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
#if (defined STM32G030xx) || (defined STM32G070xx)
sBreakDeadTimeConfig.BreakFilter = 0;
sBreakDeadTimeConfig.BreakAFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
sBreakDeadTimeConfig.Break2Filter = 0;
sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
#endif
if (HAL_TIMEx_ConfigBreakDeadTime(pika_tim, &sBreakDeadTimeConfig) !=
HAL_OK) {
obj_setSysOut(self, "[error]: init PWM faild.");
obj_setErrorCode(self, 1);
return;
}
HAL_TIM_PWM_Start(pika_tim, getTimChennel(pin));
}
void STM32_PWM_platformSetDuty(PikaObj* self, float duty, char* pin) {
TIM_HandleTypeDef* pika_tim = getTimHandle(pin);
if (NULL == pika_tim) {
obj_setSysOut(self, "[error]: can not found PWM hardware.");
obj_setErrorCode(self, 1);
return;
}
/* update duty in run time */
if (NULL != pika_tim->Instance) {
__HAL_TIM_SET_COMPARE(pika_tim, getTimChennel(pin),
(uint32_t)(pika_tim->Init.Period * duty));
}
}
void STM32_PWM_platformSetFrequency(PikaObj* self, int freq, char* pin) {
TIM_HandleTypeDef* pika_tim = getTimHandle(pin);
if (NULL == pika_tim) {
obj_setSysOut(self, "[error]: can not found PWM hardware.");
obj_setErrorCode(self, 1);
return;
}
/* update frequency in run time */
if (NULL != pika_tim->Instance) {
__HAL_TIM_SET_AUTORELOAD(
pika_tim, (uint32_t)((float)(1000 * 1000) / (float)freq) - 1);
float duty = obj_getFloat(self, "duty");
__HAL_TIM_SET_COMPARE(pika_tim, getTimChennel(pin),
(uint32_t)(pika_tim->Init.Period * duty));
}
}

View File

@ -0,0 +1,14 @@
#include "STM32_Time.h"
#include <stdint.h>
#include "BaseObj.h"
#include "STM32_common.h"
void STM32_Time_sleep_ms(PikaObj* self, int ms) {
HAL_Delay(ms);
}
void STM32_Time_sleep_s(PikaObj* self, int s) {
for (int i = 0; i < s; i++) {
HAL_Delay(1000);
}
}

434
package/STM32/STM32_UART.c Normal file
View File

@ -0,0 +1,434 @@
#include "STM32_UART.h"
#include <stdint.h>
#include "BaseObj.h"
#include "STM32_common.h"
#include "dataStrs.h"
#ifdef UART1_EXIST
pika_uart_t pika_uart1;
#endif
#ifdef UART2_EXIST
pika_uart_t pika_uart2;
#endif
#ifdef UART3_EXIST
pika_uart_t pika_uart3;
#endif
#ifdef UART4_EXIST
pika_uart_t pika_uart4;
#endif
static pika_uart_t* getPikaUart(uint8_t id) {
if (1 == id) {
return &pika_uart1;
}
if (2 == id) {
return &pika_uart2;
}
#ifdef UART3_EXIST
if (3 == id) {
return &pika_uart3;
}
#endif
#ifdef UART4_EXIST
if (4 == id) {
return &pika_uart4;
}
#endif
return NULL;
}
static void setUartObj(uint8_t id, PikaObj* obj) {
pika_uart_t* pika_uart = getPikaUart(id);
pika_uart->obj = obj;
}
static PikaObj* getUartObj(uint8_t id) {
pika_uart_t* pika_uart = getPikaUart(id);
if (NULL == pika_uart) {
return NULL;
}
return pika_uart->obj;
}
static USART_TypeDef* getUartInstance(uint8_t id) {
#ifdef UART1_EXIST
if (1 == id) {
return USART1;
}
#endif
#ifdef UART2_EXIST
if (2 == id) {
return USART2;
}
#endif
#ifdef UART3_EXIST
if (3 == id) {
return USART3;
}
#endif
#ifdef UART4_EXIST
if (4 == id) {
return USART4;
}
#endif
return NULL;
}
static uint8_t getUartId(UART_HandleTypeDef* huart) {
#ifdef UART1_EXIST
if (huart == &pika_uart1.huart) {
return 1;
}
#endif
#ifdef UART2_EXIST
if (huart == &pika_uart2.huart) {
return 2;
}
#endif
#ifdef UART3_EXIST
if (huart == &pika_uart3.huart) {
return 3;
}
#endif
#ifdef UART4_EXIST
if (huart == &pika_uart4.huart) {
return 4;
}
#endif
return 0;
}
static UART_HandleTypeDef* getUartHandle(uint8_t id) {
pika_uart_t* pika_uart = getPikaUart(id);
if (NULL == pika_uart) {
return NULL;
}
return &(pika_uart->huart);
}
static char* getUartRxBuff(uint8_t id) {
pika_uart_t* pika_uart = getPikaUart(id);
if (NULL == pika_uart) {
return NULL;
}
return pika_uart->rxBuff;
}
static uint8_t USART_UART_Init(uint32_t baudRate, uint8_t id) {
uint8_t errCode = 0;
UART_HandleTypeDef* huart = getUartHandle(id);
huart->Instance = getUartInstance(id);
if (NULL == huart->Instance) {
errCode = 5;
goto exit;
}
huart->Init.BaudRate = baudRate;
huart->Init.WordLength = UART_WORDLENGTH_8B;
huart->Init.StopBits = UART_STOPBITS_1;
huart->Init.Parity = UART_PARITY_NONE;
huart->Init.Mode = UART_MODE_TX_RX;
huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart->Init.OverSampling = UART_OVERSAMPLING_16;
#if (defined STM32G070xx) || (defined STM32G030xx)
huart->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart->Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart->AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
#endif
if (HAL_UART_Init(huart) != HAL_OK) {
errCode = 1;
goto exit;
}
#if (defined STM32G070xx) || (defined STM32G030xx)
if (HAL_UARTEx_SetTxFifoThreshold(huart, UART_TXFIFO_THRESHOLD_1_8) !=
HAL_OK) {
errCode = 2;
goto exit;
}
if (HAL_UARTEx_SetRxFifoThreshold(huart, UART_RXFIFO_THRESHOLD_1_8) !=
HAL_OK) {
errCode = 3;
goto exit;
}
if (HAL_UARTEx_DisableFifoMode(huart) != HAL_OK) {
errCode = 4;
goto exit;
}
#endif
exit:
return errCode;
}
static void UART_MspInit(UART_HandleTypeDef* uartHandle) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
#ifdef UART1_EXIST
if (uartHandle->Instance == USART1) {
/* USART1 clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
#if (defined STM32G070xx) || (defined STM32G030xx)
GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
#endif
#ifdef STM32F103xB
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
#endif
/* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
}
#endif
#ifdef UART2_EXIST
if (uartHandle->Instance == USART2) {
/* USART2 clock enable */
__HAL_RCC_USART2_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART2 GPIO Configuration
PA2 ------> USART2_TX
PA3 ------> USART2_RX
*/
#if (defined STM32G070xx) || (defined STM32G030xx)
GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
#endif
#ifdef STM32F103xB
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
#endif
/* USART2 interrupt Init */
HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART2_IRQn);
}
#endif
#ifdef UART3_EXIST
if (uartHandle->Instance == USART3) {
/* USART3 clock enable */
__HAL_RCC_USART3_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**USART3 GPIO Configuration
PA5 ------> USART3_TX
PB0 ------> USART3_RX
*/
#if (defined STM32G070xx)
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF4_USART3;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF4_USART3;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
#endif
#ifdef STM32F103xB
/**USART3 GPIO Configuration
PB10 ------> USART3_TX
PB11 ------> USART3_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
#endif
/* USART3 interrupt Init */
#if (defined STM32G070xx) || (defined STM32G030xx)
HAL_NVIC_SetPriority(USART3_4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART3_4_IRQn);
#endif
#ifdef STM32F103xB
HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART3_IRQn);
#endif
}
#endif
#ifdef UART4_EXIST
#if (defined STM32G070xx)
if (uartHandle->Instance == USART4) {
/* USART4 clock enable */
__HAL_RCC_USART4_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART4 GPIO Configuration
PA0 ------> USART4_TX
PA1 ------> USART4_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF4_USART4;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USART4 interrupt Init */
HAL_NVIC_SetPriority(USART3_4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART3_4_IRQn);
}
#endif
#endif
}
/* Msp handle interrupt */
#ifdef UART1_EXIST
void USART1_IRQHandler(void) {
HAL_UART_IRQHandler(&pika_uart1.huart);
}
#endif
#ifdef UART2_EXIST
void USART2_IRQHandler(void) {
HAL_UART_IRQHandler(&pika_uart2.huart);
}
#endif
#ifdef UART3_EXIST
#ifdef STM32F103xB
void USART3_IRQHandler(void) {
HAL_UART_IRQHandler(&pika_uart3.huart);
}
#endif
#endif
#if (defined UART3_EXIST) && (defined UART4_EXIST)
#if defined STM32G070xx
void USART3_4_IRQHandler(void) {
HAL_UART_IRQHandler(&pika_uart3.huart);
HAL_UART_IRQHandler(&pika_uart4.huart);
}
#endif
#endif
void STM32_UART_platformEnable(PikaObj* self, int baudRate, int id) {
STM32_Code_Init();
setUartObj(id, self);
UART_HandleTypeDef* huart = getUartHandle(id);
huart->Instance = getUartInstance(id);
UART_MspInit(huart);
int errCode = USART_UART_Init(baudRate, id);
if (0 != errCode) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] uart init faild.");
return;
}
HAL_UART_Receive_IT(getUartHandle(id), (uint8_t*)getUartRxBuff(id), 1);
}
char* STM32_UART_platformRead(PikaObj* self, int id, int length) {
Args* buffs = New_strBuff();
char* readBuff = NULL;
pika_uart_t* pika_uart = getPikaUart(id);
if (length >= pika_uart->rxBuffOffset) {
/* not enough str */
length = pika_uart->rxBuffOffset;
}
readBuff = args_getBuff(buffs, length);
memcpy(readBuff, pika_uart->rxBuff, length);
obj_setStr(self, "readBuff", readBuff);
readBuff = obj_getStr(self, "readBuff");
/* update rxBuff */
memcpy(pika_uart->rxBuff, pika_uart->rxBuff + length,
pika_uart->rxBuffOffset - length);
pika_uart->rxBuffOffset -= length;
pika_uart->rxBuff[pika_uart->rxBuffOffset] = 0;
UART_Start_Receive_IT(
&pika_uart->huart,
(uint8_t*)(pika_uart->rxBuff + pika_uart->rxBuffOffset), 1);
exit:
args_deinit(buffs);
return readBuff;
}
void STM32_UART_platformWrite(PikaObj* self, char* data, int id) {
HAL_UART_Transmit(getUartHandle(id), (uint8_t*)data, strGetSize(data), 100);
}
void STM32_UART_clearRxBuff(pika_uart_t* pika_uart) {
pika_uart->rxBuffOffset = 0;
pika_uart->rxBuff[pika_uart->rxBuffOffset] = 0;
UART_Start_Receive_IT(
&pika_uart->huart,
(uint8_t*)(pika_uart->rxBuff + pika_uart->rxBuffOffset), 1);
}
/* Recive Interrupt Handler */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef* huart) {
uint8_t id = getUartId(huart);
pika_uart_t* pika_uart = getPikaUart(id);
char inputChar = pika_uart->rxBuff[pika_uart->rxBuffOffset];
if ((id == 1) && ('\n' == inputChar)) {
uint8_t res = STM32_Code_reciveHandler(pika_uart->rxBuff,
pika_uart->rxBuffOffset + 1);
/* handler is working */
if (0 != res) {
STM32_UART_clearRxBuff(pika_uart);
return;
}
}
/* avoid recive buff overflow */
if (pika_uart->rxBuffOffset + 2 > RX_BUFF_LENGTH) {
memmove(pika_uart->rxBuff, pika_uart->rxBuff + 1, RX_BUFF_LENGTH);
UART_Start_Receive_IT(
huart, (uint8_t*)(pika_uart->rxBuff + pika_uart->rxBuffOffset), 1);
return;
}
/* recive next char */
pika_uart->rxBuffOffset++;
pika_uart->rxBuff[pika_uart->rxBuffOffset] = 0;
UART_Start_Receive_IT(
huart, (uint8_t*)(pika_uart->rxBuff + pika_uart->rxBuffOffset), 1);
}
/* support prinf */
int fputc(int ch, FILE* f) {
HAL_UART_Transmit(&pika_uart1.huart, (uint8_t*)&ch, 1, 0xffff);
return ch;
}
/* support scanf */
int fgetc(FILE* f) {
uint8_t ch = 0;
HAL_UART_Receive(&pika_uart1.huart, &ch, 1, 0xffff);
return ch;
}

View File

@ -0,0 +1,161 @@
#include "STM32_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) {
}
}
}
void delay_us(uint32_t udelay) {
uint32_t startval, tickn, delays, wait;
startval = SysTick->VAL;
tickn = HAL_GetTick();
delays = udelay * 64; // delay 1us when delays = 64
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* getGpioPort(char* pin) {
if (strIsStartWith(pin, "PA")) {
return GPIOA;
}
if (strIsStartWith(pin, "PB")) {
return GPIOB;
}
if (strIsStartWith(pin, "PC")) {
return GPIOC;
}
if (strIsStartWith(pin, "PD")) {
return GPIOD;
}
return NULL;
}
uint16_t getGpioPin(char* pin) {
Args* buffs = New_strBuff();
uint16_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;
}
exit:
args_deinit(buffs);
return gpioPin;
}
uint32_t getPinMode(char* mode) {
if (strEqu(mode, "out")) {
return GPIO_MODE_OUTPUT_PP;
}
if (strEqu(mode, "in")) {
return GPIO_MODE_INPUT;
}
return NULL;
}
uint8_t enableClk(char* pin) {
if (strIsStartWith(pin, "PA")) {
__HAL_RCC_GPIOA_CLK_ENABLE();
return 0;
}
if (strIsStartWith(pin, "PB")) {
__HAL_RCC_GPIOB_CLK_ENABLE();
return 0;
}
if (strIsStartWith(pin, "PC")) {
__HAL_RCC_GPIOC_CLK_ENABLE();
return 0;
}
if (strIsStartWith(pin, "PD")) {
__HAL_RCC_GPIOD_CLK_ENABLE();
return 0;
}
return 1;
}

View File

@ -0,0 +1,75 @@
#ifndef __STM32__COMMON__H
#define __STM32__COMMON__H
#include "PikaObj.h"
#ifdef STM32G070xx
#include "stm32g0xx_hal.h"
#define UART1_EXIST
#define UART2_EXIST
#define UART3_EXIST
#define UART4_EXIST
#endif
#ifdef STM32F103xB
#include "stm32f1xx_hal.h"
#define UART1_EXIST
#define UART2_EXIST
#define UART3_EXIST
#define TIM1_EXIST
#define TIM2_EXIST
#define TIM3_EXIST
#define TIM4_EXIST
#endif
#ifdef STM32G030xx
#include "stm32g0xx_hal.h"
#define UART1_EXIST
#define UART2_EXIST
#define TIM1_EXIST
#define TIM3_EXIST
#define TIM14_EXIST
#define TIM16_EXIST
#define TIM17_EXIST
#endif
#define RX_BUFF_LENGTH 64
#define FLASH_CODE_START_ADDR \
(FLASH_BASE + \
((FLASH_PAGE_NB - 1) * FLASH_PAGE_SIZE)) /* Start @ of user Flash area */
#define FLASH_USER_END_ADDR \
(FLASH_BASE + FLASH_SIZE - 1) /* End @ of user Flash area */
uint32_t GetPage(uint32_t Addr);
#define DATA_64 ((uint64_t)0x1234567812345678)
#define DATA_32 ((uint32_t)0x12345678)
typedef struct {
UART_HandleTypeDef huart;
uint8_t id;
char rxBuff[RX_BUFF_LENGTH];
uint16_t rxBuffOffset;
PikaObj* obj;
} pika_uart_t;
typedef struct _CodeHeap{
char *content;
uint32_t size;
uint8_t ena;
uint32_t reciveTime;
uint32_t oldSize;
}CodeHeap;
GPIO_TypeDef* getGpioPort(char* pin);
uint16_t getGpioPin(char* pin);
uint32_t getPinMode(char* mode);
uint8_t enableClk(char* pin);
void delay_us(uint32_t delay);
void delay_unit(uint32_t delay);
void STM32_UART_clearRxBuff(pika_uart_t* pika_uart);
uint8_t STM32_Code_reciveHandler(char *data, uint32_t rxSize);
void STM32_Code_Init();
void STM32_Code_flashHandler();
#endif

View File

@ -1,3 +0,0 @@
[[package]]
name = "STM32"