mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
add stm32F4 package
This commit is contained in:
parent
91242415fe
commit
5f992314b5
98
package/STM32F4/STM32F4.py
Normal file
98
package/STM32F4/STM32F4.py
Normal file
@ -0,0 +1,98 @@
|
||||
from PikaObj import *
|
||||
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():
|
||||
pass
|
||||
|
||||
# override
|
||||
def platformRead():
|
||||
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():
|
||||
pass
|
||||
|
||||
# override
|
||||
def platformRead():
|
||||
pass
|
||||
|
||||
|
||||
class UART(PikaStdDevice.UART):
|
||||
# override
|
||||
def platformEnable():
|
||||
pass
|
||||
|
||||
# override
|
||||
def platformWrite():
|
||||
pass
|
||||
|
||||
# override
|
||||
def platformRead():
|
||||
pass
|
||||
|
||||
|
||||
class PWM(PikaStdDevice.PWM):
|
||||
# override
|
||||
def platformEnable():
|
||||
pass
|
||||
|
||||
# override
|
||||
def platformSetFrequency():
|
||||
pass
|
||||
|
||||
# override
|
||||
def platformSetDuty():
|
||||
pass
|
||||
|
||||
|
||||
class IIC(PikaStdDevice.IIC):
|
||||
SCL = GPIO()
|
||||
SDA = GPIO()
|
||||
# override
|
||||
|
||||
def platformEnable():
|
||||
pass
|
||||
|
||||
# override
|
||||
def platformWrite():
|
||||
pass
|
||||
|
||||
# override
|
||||
def platformRead():
|
||||
pass
|
||||
|
||||
|
||||
# class lowLevel(TinyObj):
|
||||
# def readPin(pin: str) -> int:
|
||||
# pass
|
142
package/STM32F4/STM32F4_ADC.c
Normal file
142
package/STM32F4/STM32F4_ADC.c
Normal file
@ -0,0 +1,142 @@
|
||||
#include <stdint.h>
|
||||
#include "BaseObj.h"
|
||||
#include "STM32F4_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 = 1;
|
||||
HAL_ADC_ConfigChannel(hadc, &ADC_ChanConf);
|
||||
HAL_ADC_Start(hadc);
|
||||
HAL_ADC_PollForConversion(hadc, 10);
|
||||
return (uint16_t)HAL_ADC_GetValue(hadc);
|
||||
}
|
||||
|
||||
void STM32F4_ADC_platformEnable(PikaObj* self) {
|
||||
char *pin = obj_getStr(self, "pin");
|
||||
if (!strIsStartWith(pin, "PA")) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match adc pin.");
|
||||
return;
|
||||
}
|
||||
|
||||
/* MSP Init */
|
||||
|
||||
__HAL_RCC_ADC1_CLK_ENABLE();
|
||||
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;
|
||||
pika_hadc1.Init.ScanConvMode = 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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void STM32F4_ADC_platformRead(PikaObj* self) {
|
||||
char *pin = obj_getStr(self, "pin");
|
||||
obj_setFloat(self, "val", 3.3f * Get_Adc(&pika_hadc1, getChannel(pin)) / 4096.0f);
|
||||
}
|
171
package/STM32F4/STM32F4_GPIO.c
Normal file
171
package/STM32F4/STM32F4_GPIO.c
Normal file
@ -0,0 +1,171 @@
|
||||
#include <stdint.h>
|
||||
#include "BaseObj.h"
|
||||
#include "STM32F4_common.h"
|
||||
#include "dataStrs.h"
|
||||
|
||||
|
||||
void STM32F4_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 STM32F4_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);
|
||||
|
||||
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;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(gpioPort, &GPIO_InitStruct);
|
||||
}
|
||||
void STM32F4_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 STM32F4_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 STM32F4_GPIO_platformSetMode(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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void STM32F4_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.");
|
||||
}
|
||||
obj_setInt(self, "readBuff", HAL_GPIO_ReadPin(gpioPort,gpioPin));
|
||||
}
|
||||
|
||||
int STM32F4_lowLevel_readPin(PikaObj *self, char * 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);
|
||||
}
|
256
package/STM32F4/STM32F4_IIC.c
Normal file
256
package/STM32F4/STM32F4_IIC.c
Normal file
@ -0,0 +1,256 @@
|
||||
#include <stdint.h>
|
||||
#include "BaseObj.h"
|
||||
#include "STM32F4_common.h"
|
||||
#include "dataStrs.h"
|
||||
|
||||
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;
|
||||
|
||||
#define delay_rate 1
|
||||
|
||||
void SDA_OUT(pika_IIC_info* iic) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
/*Configure GPIO*/
|
||||
GPIO_InitStruct.Pin = iic->SDA_GPIO_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(iic->SDA_GPIO, &GPIO_InitStruct);
|
||||
}
|
||||
|
||||
void SDA_IN(pika_IIC_info* iic) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
/*Configure GPIO*/
|
||||
GPIO_InitStruct.Pin = iic->SDA_GPIO_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(iic->SDA_GPIO, &GPIO_InitStruct);
|
||||
}
|
||||
|
||||
void IIC_SDA_high(pika_IIC_info* iic) {
|
||||
HAL_GPIO_WritePin(iic->SDA_GPIO, iic->SDA_GPIO_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
void IIC_SDA_low(pika_IIC_info* iic) {
|
||||
HAL_GPIO_WritePin(iic->SDA_GPIO, iic->SDA_GPIO_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
|
||||
void IIC_SCL_high(pika_IIC_info* iic) {
|
||||
HAL_GPIO_WritePin(iic->SCL_GPIO, iic->SCL_GPIO_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
void IIC_SCL_low(pika_IIC_info* iic) {
|
||||
HAL_GPIO_WritePin(iic->SCL_GPIO, iic->SCL_GPIO_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
|
||||
uint8_t READ_SDA(pika_IIC_info* iic) {
|
||||
return HAL_GPIO_ReadPin(iic->SDA_GPIO, iic->SDA_GPIO_Pin);
|
||||
}
|
||||
|
||||
void WRITE_SDA(pika_IIC_info* iic, uint8_t data) {
|
||||
HAL_GPIO_WritePin(iic->SDA_GPIO, iic->SDA_GPIO_Pin, (GPIO_PinState) data);
|
||||
}
|
||||
|
||||
void IIC_Start(pika_IIC_info* iic) {
|
||||
SDA_OUT(iic);
|
||||
IIC_SDA_high(iic);
|
||||
IIC_SCL_high(iic);
|
||||
delay_us(delay_rate * 4);
|
||||
IIC_SDA_low(iic);
|
||||
delay_us(delay_rate * 4);
|
||||
IIC_SCL_low(iic);
|
||||
}
|
||||
|
||||
void IIC_Stop(pika_IIC_info* iic) {
|
||||
SDA_OUT(iic);
|
||||
IIC_SCL_low(iic);
|
||||
IIC_SDA_low(iic);
|
||||
delay_us(delay_rate * 4);
|
||||
IIC_SCL_high(iic);
|
||||
IIC_SDA_high(iic);
|
||||
delay_us(delay_rate * 4);
|
||||
}
|
||||
|
||||
uint8_t IIC_Wait_Ack(pika_IIC_info* iic) {
|
||||
uint8_t ucErrTime = 0;
|
||||
SDA_IN(iic);
|
||||
|
||||
IIC_SDA_high(iic);
|
||||
delay_us(delay_rate * 1);
|
||||
IIC_SCL_high(iic);
|
||||
delay_us(delay_rate * 1);
|
||||
while (READ_SDA(iic)) {
|
||||
ucErrTime++;
|
||||
if (ucErrTime > 250) {
|
||||
IIC_Stop(iic);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
IIC_SCL_low(iic);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void IIC_Ack(pika_IIC_info* iic) {
|
||||
IIC_SCL_low(iic);
|
||||
SDA_OUT(iic);
|
||||
|
||||
IIC_SDA_low(iic);
|
||||
delay_us(delay_rate * 2);
|
||||
IIC_SCL_high(iic);
|
||||
delay_us(delay_rate * 2);
|
||||
IIC_SCL_low(iic);
|
||||
}
|
||||
|
||||
void IIC_NAck(pika_IIC_info* iic) {
|
||||
IIC_SCL_low(iic);
|
||||
SDA_OUT(iic);
|
||||
|
||||
IIC_SDA_high(iic);
|
||||
delay_us(delay_rate * 2);
|
||||
IIC_SCL_high(iic);
|
||||
delay_us(delay_rate * 2);
|
||||
IIC_SCL_low(iic);
|
||||
}
|
||||
|
||||
void IIC_Send_Byte(pika_IIC_info* iic, uint8_t txd) {
|
||||
uint8_t t;
|
||||
SDA_OUT(iic);
|
||||
|
||||
IIC_SCL_low(iic);
|
||||
for (t = 0; t < 8; t++) {
|
||||
WRITE_SDA(iic, (txd & 0x80) >> 7);
|
||||
txd <<= 1;
|
||||
delay_us(delay_rate * 2);
|
||||
IIC_SCL_high(iic);
|
||||
delay_us(delay_rate * 2);
|
||||
IIC_SCL_low(iic);
|
||||
delay_us(delay_rate * 2);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t IIC_Read_Byte(pika_IIC_info* iic, unsigned char ack) {
|
||||
unsigned char i, receive = 0;
|
||||
SDA_IN(iic);
|
||||
|
||||
for (i = 0; i < 8; i++) {
|
||||
IIC_SCL_low(iic);
|
||||
delay_us(delay_rate * 2);
|
||||
IIC_SCL_high(iic);
|
||||
receive <<= 1;
|
||||
if (READ_SDA(iic))
|
||||
receive++;
|
||||
delay_us(delay_rate * 1);
|
||||
}
|
||||
if (!ack)
|
||||
IIC_NAck(iic);
|
||||
else
|
||||
IIC_Ack(iic);
|
||||
return receive;
|
||||
}
|
||||
|
||||
uint8_t MPU_Write_Len(pika_IIC_info* iic,
|
||||
uint8_t addr,
|
||||
uint8_t reg,
|
||||
uint8_t len,
|
||||
uint8_t* buf) {
|
||||
uint8_t i;
|
||||
IIC_Start(iic);
|
||||
IIC_Send_Byte(iic, (addr << 1) | 0);
|
||||
if (IIC_Wait_Ack(iic)) {
|
||||
IIC_Stop(iic);
|
||||
return 1;
|
||||
}
|
||||
IIC_Send_Byte(iic, reg);
|
||||
IIC_Wait_Ack(iic);
|
||||
for (i = 0; i < len; i++) {
|
||||
IIC_Send_Byte(iic, buf[i]);
|
||||
if (IIC_Wait_Ack(iic)) {
|
||||
IIC_Stop(iic);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
IIC_Stop(iic);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t MPU_Read_Len(pika_IIC_info* iic,
|
||||
uint8_t addr,
|
||||
uint8_t reg,
|
||||
uint8_t len,
|
||||
uint8_t* buf) {
|
||||
IIC_Start(iic);
|
||||
IIC_Send_Byte(iic, (addr << 1) | 0);
|
||||
if (IIC_Wait_Ack(iic)) {
|
||||
IIC_Stop(iic);
|
||||
return 1;
|
||||
}
|
||||
IIC_Send_Byte(iic, reg);
|
||||
IIC_Wait_Ack(iic);
|
||||
IIC_Start(iic);
|
||||
IIC_Send_Byte(iic, (addr << 1) | 1);
|
||||
IIC_Wait_Ack(iic);
|
||||
while (len) {
|
||||
if (len == 1)
|
||||
*buf = IIC_Read_Byte(iic, 0);
|
||||
else
|
||||
*buf = IIC_Read_Byte(iic, 1);
|
||||
len--;
|
||||
buf++;
|
||||
}
|
||||
IIC_Stop(iic);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void STM32F4_IIC_platformEnable(PikaObj* self) {
|
||||
obj_run(self, "SCL.init()");
|
||||
obj_run(self, "SDA.init()");
|
||||
obj_run(self, "SCL.setPin(SCLpin)");
|
||||
obj_run(self, "SDA.setPin(SDApin)");
|
||||
obj_run(self, "SCL.setMode('out')");
|
||||
obj_run(self, "SDA.setMode('out')");
|
||||
obj_run(self, "SCL.enable()");
|
||||
obj_run(self, "SDA.enable()");
|
||||
obj_run(self, "SCL.low()");
|
||||
obj_run(self, "SDA.low()");
|
||||
char* SCLpin = obj_getStr(self, "SCLpin");
|
||||
char* SDApin = obj_getStr(self, "SDApin");
|
||||
uint8_t deviceAddr = obj_getInt(self, "deviceAddr");
|
||||
|
||||
pika_IIC_info* iic = obj_getPtr(self, "iic");
|
||||
if (NULL == iic) {
|
||||
iic = pikaMalloc(sizeof(pika_IIC_info));
|
||||
obj_setPtr(self, "iic", iic);
|
||||
}
|
||||
iic->SDA_GPIO = getGpioPort(SDApin);
|
||||
iic->SDA_GPIO_Pin = getGpioPin(SDApin);
|
||||
iic->SCL_GPIO = getGpioPort(SCLpin);
|
||||
iic->SCL_GPIO_Pin = getGpioPin(SCLpin);
|
||||
iic->deviceAddr = deviceAddr;
|
||||
SDA_OUT(iic);
|
||||
}
|
||||
|
||||
void STM32F4_IIC_platformRead(PikaObj* self) {
|
||||
int addr = obj_getInt(self, "addr");
|
||||
int length = obj_getInt(self, "length");
|
||||
pika_IIC_info* iic = obj_getPtr(self, "iic");
|
||||
|
||||
MPU_Read_Len(iic, iic->deviceAddr, addr, length, (uint8_t*)iic->readBuff);
|
||||
obj_setStr(self, "readData", (char*)iic->readBuff);
|
||||
}
|
||||
|
||||
void STM32F4_IIC_platformWrite(PikaObj* self) {
|
||||
int addr = obj_getInt(self, "addr");
|
||||
char* data = obj_getStr(self, "data");
|
||||
pika_IIC_info* iic = obj_getPtr(self, "iic");
|
||||
|
||||
MPU_Write_Len(iic, iic->deviceAddr, addr, strGetSize(data), (uint8_t*)data);
|
||||
}
|
321
package/STM32F4/STM32F4_PWM.c
Normal file
321
package/STM32F4/STM32F4_PWM.c
Normal file
@ -0,0 +1,321 @@
|
||||
#include <stdint.h>
|
||||
#include "BaseObj.h"
|
||||
#include "STM32F4_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;
|
||||
}
|
||||
|
||||
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;
|
||||
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 STM32F4_PWM_platformEnable(PikaObj* self) {
|
||||
float duty = obj_getFloat(self, "duty");
|
||||
int freq = obj_getInt(self, "freq");
|
||||
char *pin = obj_getStr(self, "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);
|
||||
pika_tim->Init.Prescaler = 72 - 1;
|
||||
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;
|
||||
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 (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 STM32F4_PWM_platformSetDuty(PikaObj* self) {
|
||||
float duty = obj_getFloat(self, "duty");
|
||||
char *pin = obj_getStr(self, "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 STM32F4_PWM_platformSetFrequency(PikaObj* self) {
|
||||
int freq = obj_getInt(self, "freq");
|
||||
char *pin = obj_getStr(self, "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));
|
||||
}
|
||||
}
|
13
package/STM32F4/STM32F4_Time.c
Normal file
13
package/STM32F4/STM32F4_Time.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdint.h>
|
||||
#include "BaseObj.h"
|
||||
|
||||
#include "STM32F4_common.h"
|
||||
|
||||
void STM32F4_Time_sleep_ms(PikaObj* self, int ms) {
|
||||
HAL_Delay(ms);
|
||||
}
|
||||
void STM32F4_Time_sleep_s(PikaObj* self, int s) {
|
||||
for (int i = 0; i < s; i++) {
|
||||
HAL_Delay(1000);
|
||||
}
|
||||
}
|
338
package/STM32F4/STM32F4_UART.c
Normal file
338
package/STM32F4/STM32F4_UART.c
Normal file
@ -0,0 +1,338 @@
|
||||
#include <stdint.h>
|
||||
#include "BaseObj.h"
|
||||
#include "STM32F4_common.h"
|
||||
#include "dataStrs.h"
|
||||
|
||||
extern PikaObj* pikaMain;
|
||||
|
||||
#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
|
||||
|
||||
/* support by booter */
|
||||
extern UART_HandleTypeDef huart1;
|
||||
|
||||
static pika_uart_t* getPikaUart(uint8_t id) {
|
||||
#ifdef UART1_EXIST
|
||||
if (1 == id) {
|
||||
return &pika_uart1;
|
||||
}
|
||||
#endif
|
||||
#ifdef UART2_EXIST
|
||||
if (2 == id) {
|
||||
return &pika_uart2;
|
||||
}
|
||||
#endif
|
||||
#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 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 (HAL_UART_Init(huart) != HAL_OK) {
|
||||
errCode = 1;
|
||||
goto exit;
|
||||
}
|
||||
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
|
||||
*/
|
||||
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);
|
||||
/* 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
|
||||
*/
|
||||
|
||||
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);
|
||||
/* 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
|
||||
*/
|
||||
|
||||
/**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);
|
||||
|
||||
/* USART3 interrupt Init */
|
||||
HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(USART3_IRQn);
|
||||
}
|
||||
#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
|
||||
void USART3_IRQHandler(void) {
|
||||
HAL_UART_IRQHandler(&pika_uart3.huart);
|
||||
}
|
||||
#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 STM32F4_UART_platformEnable(PikaObj* self) {
|
||||
int id = obj_getInt(self, "id");
|
||||
int baudRate = obj_getInt(self, "baudRate");
|
||||
setUartObj(id, self);
|
||||
/* uart 1 is inited by hardward */
|
||||
if (1 == id) {
|
||||
return;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
void STM32F4_UART_platformRead(PikaObj* self) {
|
||||
int id = obj_getInt(self, "id");
|
||||
int length = obj_getInt(self, "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);
|
||||
args_deinit(buffs);
|
||||
obj_setStr(self,"readData", readBuff);
|
||||
}
|
||||
|
||||
void STM32F4_UART_platformWrite(PikaObj* self) {
|
||||
char *data = obj_getStr(self, "data");
|
||||
int id = obj_getInt(self, "id");
|
||||
HAL_UART_Transmit(getUartHandle(id), (uint8_t*)data, strGetSize(data), 100);
|
||||
}
|
||||
|
||||
void STM32F4_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);
|
||||
}
|
||||
|
||||
char pikaShell[RX_BUFF_LENGTH] = {0};
|
||||
uint8_t pikaShellRxOk = 0;
|
||||
|
||||
/* 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)) {
|
||||
}
|
||||
/* 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);
|
||||
}
|
||||
|
168
package/STM32F4/STM32F4_common.c
Normal file
168
package/STM32F4/STM32F4_common.c
Normal file
@ -0,0 +1,168 @@
|
||||
#include "STM32F4_common.h"
|
||||
#include "dataStrs.h"
|
||||
|
||||
void __platformDisableIrqHandle(){
|
||||
__disable_irq();
|
||||
}
|
||||
void __platformEnableIrqHandle(){
|
||||
__enable_irq();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
86
package/STM32F4/STM32F4_common.h
Normal file
86
package/STM32F4/STM32F4_common.h
Normal file
@ -0,0 +1,86 @@
|
||||
#ifndef __STM32__COMMON__H
|
||||
#define __STM32__COMMON__H
|
||||
#include "PikaObj.h"
|
||||
#include <stdint.h>
|
||||
|
||||
/* support std lib for stm32 */
|
||||
#define delay_ms HAL_Delay
|
||||
typedef uint16_t u16;
|
||||
typedef uint8_t u8;
|
||||
typedef uint32_t u32;
|
||||
|
||||
#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
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
/* default hardware config */
|
||||
#define UART1_EXIST
|
||||
#define UART2_EXIST
|
||||
#define UART3_EXIST
|
||||
|
||||
#define TIM1_EXIST
|
||||
#define TIM2_EXIST
|
||||
#define TIM3_EXIST
|
||||
#define TIM4_EXIST
|
||||
|
||||
/* configuration for STM32F411xE */
|
||||
#ifdef STM32F411xE
|
||||
#undef UART3_EXIST
|
||||
#endif
|
||||
|
||||
#define RX_BUFF_LENGTH 64
|
||||
|
||||
#define FLASH_SCRIPT_START_ADDR (FLASH_BASE + ((FLASH_PAGE_NB - 2) * FLASH_PAGE_SIZE))
|
||||
#define FLASH_SCRIPT_END_ADDR (FLASH_BASE + FLASH_SIZE - 1)
|
||||
|
||||
#define FLASH_PIKA_ASM_START_ADDR FLASH_SCRIPT_START_ADDR
|
||||
#define FLASH_PIKA_ASM_END_ADDR FLASH_SCRIPT_END_ADDR
|
||||
|
||||
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);
|
||||
void STM32_Code_flashHandler(void);
|
||||
void HARDWARE_PRINTF_Init(void);
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user