mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
use LL for stm32 lib excipt RCC and FLASH
This commit is contained in:
parent
f8f5903f70
commit
e29a77527f
@ -28,18 +28,5 @@ class RGB(PikaStdLib.SysObj):
|
||||
pass
|
||||
|
||||
|
||||
class OLED(TinyObj):
|
||||
def init():
|
||||
pass
|
||||
|
||||
def drawPoint(x: int, y: int):
|
||||
pass
|
||||
|
||||
def clear():
|
||||
pass
|
||||
|
||||
def refresh():
|
||||
pass
|
||||
|
||||
class Point(TinyObj):
|
||||
pass
|
||||
|
@ -4,9 +4,10 @@
|
||||
|
||||
#define oled_x_size 128
|
||||
#define oled_y_size 64
|
||||
#define x_size 16
|
||||
#define x_size 8
|
||||
#define y_size 8
|
||||
#define y_size_byte y_size/8
|
||||
|
||||
u8 OLED_GRAM[x_size][y_size_byte];
|
||||
|
||||
void OLED_ColorTurn(u8 i) {
|
||||
|
@ -30,11 +30,11 @@ void STM32_ADC_platformEnable(PikaObj* self) {
|
||||
return;
|
||||
}
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Pin = GPIO_get_pin(pin);
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIO_get_Group(pin), &GPIO_InitStruct);
|
||||
LL_GPIO_Init(GPIO_get_Group(pin), &GPIO_InitStruct);
|
||||
|
||||
/* init ADC */
|
||||
LL_ADC_InitTypeDef ADC_InitStruct = {0};
|
||||
@ -75,9 +75,9 @@ void STM32_ADC_platformEnable(PikaObj* self) {
|
||||
LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_0);
|
||||
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_0,
|
||||
LL_ADC_SAMPLINGTIME_COMMON_1);
|
||||
// LL_ADC_StartCalibration(ADC1);
|
||||
// while (LL_ADC_IsCalibrationOnGoing(ADC1) != 0) {
|
||||
// };
|
||||
// LL_ADC_StartCalibration(ADC1);
|
||||
// while (LL_ADC_IsCalibrationOnGoing(ADC1) != 0) {
|
||||
// };
|
||||
LL_ADC_Enable(ADC1);
|
||||
while (LL_ADC_IsActiveFlag_ADRDY(ADC1) == 0) {
|
||||
};
|
||||
|
@ -4,7 +4,6 @@
|
||||
#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");
|
||||
@ -23,7 +22,7 @@ void STM32_GPIO_platformDisable(PikaObj* self) {
|
||||
obj_setSysOut(self, "[error] not match gpio pin.");
|
||||
}
|
||||
|
||||
HAL_GPIO_DeInit(gpioPort, gpioPin);
|
||||
LL_GPIO_DeInit(gpioPort);
|
||||
}
|
||||
|
||||
void STM32_GPIO_platformEnable(PikaObj* self) {
|
||||
@ -51,25 +50,25 @@ void STM32_GPIO_platformEnable(PikaObj* self) {
|
||||
}
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_RESET);
|
||||
LL_GPIO_ResetOutputPin(gpioPort, gpioPin);
|
||||
|
||||
uint32_t pinMode = getPinMode(mode);
|
||||
|
||||
uint32_t gpioPull = GPIO_NOPULL;
|
||||
char *pull = obj_getStr(self, "pull");
|
||||
if(strEqu(pull, "up")){
|
||||
char* pull = obj_getStr(self, "pull");
|
||||
if (strEqu(pull, "up")) {
|
||||
gpioPull = GPIO_PULLUP;
|
||||
}else if(strEqu(pull, "down")){
|
||||
} else if (strEqu(pull, "down")) {
|
||||
gpioPull = GPIO_PULLDOWN;
|
||||
}
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
LL_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);
|
||||
LL_GPIO_Init(gpioPort, &GPIO_InitStruct);
|
||||
}
|
||||
void STM32_GPIO_platformLow(PikaObj* self) {
|
||||
char* pin = obj_getStr(self, "pin");
|
||||
@ -86,7 +85,7 @@ void STM32_GPIO_platformLow(PikaObj* self) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio pin.");
|
||||
}
|
||||
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_RESET);
|
||||
LL_GPIO_ResetOutputPin(gpioPort, gpioPin);
|
||||
}
|
||||
void STM32_GPIO_platformHigh(PikaObj* self) {
|
||||
char* pin = obj_getStr(self, "pin");
|
||||
@ -103,11 +102,12 @@ void STM32_GPIO_platformHigh(PikaObj* self) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio pin.");
|
||||
}
|
||||
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_SET);
|
||||
LL_GPIO_SetOutputPin(gpioPort, gpioPin);
|
||||
}
|
||||
|
||||
void STM32_GPIO_platformSetMode(PikaObj* self) {
|
||||
char* pin = obj_getStr(self, "pin");
|
||||
char *mode = obj_getStr(self, "mode");
|
||||
char* mode = obj_getStr(self, "mode");
|
||||
if (0 != GPIO_enable_clock(pin)) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio port.");
|
||||
@ -129,20 +129,20 @@ void STM32_GPIO_platformSetMode(PikaObj* self) {
|
||||
}
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_RESET);
|
||||
LL_GPIO_ResetOutputPin(gpioPort, gpioPin);
|
||||
|
||||
uint32_t pinMode = getPinMode(mode);
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
LL_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);
|
||||
LL_GPIO_Init(gpioPort, &GPIO_InitStruct);
|
||||
}
|
||||
|
||||
void STM32_GPIO_platformRead(PikaObj *self){
|
||||
void STM32_GPIO_platformRead(PikaObj* self) {
|
||||
char* pin = obj_getStr(self, "pin");
|
||||
GPIO_TypeDef* gpioPort = GPIO_get_Group(pin);
|
||||
if (NULL == gpioPort) {
|
||||
@ -154,10 +154,10 @@ void STM32_GPIO_platformRead(PikaObj *self){
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio pin.");
|
||||
}
|
||||
obj_setInt(self, "readBuff", HAL_GPIO_ReadPin(gpioPort,gpioPin));
|
||||
obj_setInt(self, "readBuff", LL_GPIO_IsInputPinSet(gpioPort, gpioPin));
|
||||
}
|
||||
|
||||
int STM32_lowLevel_readPin(PikaObj *self, char * pin){
|
||||
int STM32_lowLevel_readPin(PikaObj* self, char* pin) {
|
||||
GPIO_TypeDef* gpioPort = GPIO_get_Group(pin);
|
||||
if (NULL == gpioPort) {
|
||||
obj_setErrorCode(self, 1);
|
||||
@ -168,5 +168,5 @@ int STM32_lowLevel_readPin(PikaObj *self, char * pin){
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio pin.");
|
||||
}
|
||||
return HAL_GPIO_ReadPin(gpioPort,gpioPin);
|
||||
return LL_GPIO_IsInputPinSet(gpioPort, gpioPin);
|
||||
}
|
@ -8,47 +8,51 @@
|
||||
#define IIC_DELAY_RATE 1
|
||||
|
||||
void SDA_OUT(pika_IIC_info* iic) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
LL_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);
|
||||
LL_GPIO_Init(iic->SDA_GPIO, &GPIO_InitStruct);
|
||||
}
|
||||
|
||||
void SDA_IN(pika_IIC_info* iic) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
LL_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);
|
||||
LL_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);
|
||||
LL_GPIO_SetOutputPin(iic->SDA_GPIO, iic->SDA_GPIO_Pin);
|
||||
}
|
||||
|
||||
void IIC_SDA_low(pika_IIC_info* iic) {
|
||||
HAL_GPIO_WritePin(iic->SDA_GPIO, iic->SDA_GPIO_Pin, GPIO_PIN_RESET);
|
||||
LL_GPIO_ResetOutputPin(iic->SDA_GPIO, iic->SDA_GPIO_Pin);
|
||||
}
|
||||
|
||||
void IIC_SCL_high(pika_IIC_info* iic) {
|
||||
HAL_GPIO_WritePin(iic->SCL_GPIO, iic->SCL_GPIO_Pin, GPIO_PIN_SET);
|
||||
LL_GPIO_SetOutputPin(iic->SCL_GPIO, iic->SCL_GPIO_Pin);
|
||||
}
|
||||
|
||||
void IIC_SCL_low(pika_IIC_info* iic) {
|
||||
HAL_GPIO_WritePin(iic->SCL_GPIO, iic->SCL_GPIO_Pin, GPIO_PIN_RESET);
|
||||
LL_GPIO_ResetOutputPin(iic->SCL_GPIO, iic->SCL_GPIO_Pin);
|
||||
}
|
||||
|
||||
uint8_t READ_SDA(pika_IIC_info* iic) {
|
||||
return HAL_GPIO_ReadPin(iic->SDA_GPIO, iic->SDA_GPIO_Pin);
|
||||
return LL_GPIO_IsInputPinSet(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, data);
|
||||
if (0 == data) {
|
||||
IIC_SDA_low(iic);
|
||||
return;
|
||||
}
|
||||
IIC_SDA_high(iic);
|
||||
}
|
||||
|
||||
void IIC_Start(pika_IIC_info* iic) {
|
||||
|
@ -96,13 +96,13 @@ uint8_t PWM_GPIO_init(char* pin) {
|
||||
return 1;
|
||||
}
|
||||
GPIO_enable_clock(pin);
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Pin = GPIO_get_pin(pin);
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = TIM_get_GPIO_alternate(TIMx);
|
||||
HAL_GPIO_Init(GPIO_get_Group(pin), &GPIO_InitStruct);
|
||||
LL_GPIO_Init(GPIO_get_Group(pin), &GPIO_InitStruct);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -4,26 +4,40 @@
|
||||
#include "STM32_common.h"
|
||||
#include "dataStrs.h"
|
||||
|
||||
extern PikaObj* pikaMain;
|
||||
char* usart1_rx_buff = NULL;
|
||||
char* usart2_rx_buff = NULL;
|
||||
uint8_t usart1_rx_offset = 0;
|
||||
uint8_t usart2_rx_offset = 0;
|
||||
|
||||
static pika_uart_t* getPikaUart(uint8_t id) {
|
||||
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;
|
||||
static char* UART_get_rx_buff(USART_TypeDef* USARTx) {
|
||||
if (USARTx == USART1) {
|
||||
/* new buff if rx_buff is NULL */
|
||||
if (NULL == usart1_rx_buff) {
|
||||
usart1_rx_buff = pikaMalloc(RX_BUFF_LENGTH);
|
||||
}
|
||||
return pika_uart->obj;
|
||||
return usart1_rx_buff;
|
||||
}
|
||||
if (USARTx == USART2) {
|
||||
/* new buff if rx_buff is NULL */
|
||||
if (NULL == usart2_rx_buff) {
|
||||
usart2_rx_buff = pikaMalloc(RX_BUFF_LENGTH);
|
||||
}
|
||||
return usart2_rx_buff;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static USART_TypeDef* getUartInstance(uint8_t id) {
|
||||
static uint8_t* UART_get_rx_offset(USART_TypeDef* USARTx) {
|
||||
if (USARTx == USART1) {
|
||||
return &usart1_rx_offset;
|
||||
}
|
||||
if (USARTx == USART2) {
|
||||
return &usart2_rx_offset;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static USART_TypeDef* UART_get_instance(uint8_t id) {
|
||||
if (1 == id) {
|
||||
return USART1;
|
||||
}
|
||||
@ -33,130 +47,125 @@ static USART_TypeDef* getUartInstance(uint8_t id) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static uint8_t getUartId(UART_HandleTypeDef* huart) {
|
||||
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) {
|
||||
static uint8_t UART_hardware_init(uint32_t baudRate, uint8_t id) {
|
||||
uint8_t errCode = 0;
|
||||
USART_TypeDef* USARTx = getUartInstance(id);
|
||||
USART_TypeDef* USARTx = UART_get_instance(id);
|
||||
if (NULL == USARTx) {
|
||||
errCode = 5;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
exit:
|
||||
return errCode;
|
||||
}
|
||||
|
||||
static void UART_GPIO_init(USART_TypeDef* UARTx) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if (UARTx == 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_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);
|
||||
/* USART1 interrupt Init */
|
||||
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(USART1_IRQn);
|
||||
if (USART1 == USARTx) {
|
||||
/* USART1 is inited by pika mcu sopport package in
|
||||
* stm32g030_pika_msp.c*/
|
||||
errCode = 0;
|
||||
goto exit;
|
||||
}
|
||||
if (UARTx == USART2) {
|
||||
/* USART2 clock enable */
|
||||
__HAL_RCC_USART2_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
/* init USART2 */
|
||||
if (USART2 == USARTx) {
|
||||
LL_USART_InitTypeDef USART_InitStruct = {0};
|
||||
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
/* Peripheral clock enable */
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART2);
|
||||
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
|
||||
/**USART2 GPIO Configuration
|
||||
PA2 ------> USART2_TX
|
||||
PA3 ------> USART2_RX
|
||||
*/
|
||||
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);
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_2;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
|
||||
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_3;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
|
||||
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
/* USART2 interrupt Init */
|
||||
NVIC_SetPriority(USART2_IRQn, 0);
|
||||
NVIC_EnableIRQ(USART2_IRQn);
|
||||
USART_InitStruct.PrescalerValue = LL_USART_PRESCALER_DIV1;
|
||||
USART_InitStruct.BaudRate = baudRate;
|
||||
USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
|
||||
USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
|
||||
USART_InitStruct.Parity = LL_USART_PARITY_NONE;
|
||||
USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
|
||||
USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
|
||||
USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
|
||||
LL_USART_Init(USART2, &USART_InitStruct);
|
||||
LL_USART_ConfigAsyncMode(USART2);
|
||||
LL_USART_Enable(USART2);
|
||||
while ((!(LL_USART_IsActiveFlag_TEACK(USART2))) ||
|
||||
(!(LL_USART_IsActiveFlag_REACK(USART2)))) {
|
||||
}
|
||||
/* open interrupt */
|
||||
LL_USART_EnableIT_RXNE(USART2);
|
||||
LL_USART_EnableIT_PE(USART2);
|
||||
}
|
||||
exit:
|
||||
return errCode;
|
||||
}
|
||||
|
||||
void __callback_UART_recive(USART_TypeDef* USARTx, char rx_char) {
|
||||
uint8_t* rx_offset = UART_get_rx_offset(USARTx);
|
||||
char* rx_buff = UART_get_rx_buff(USARTx);
|
||||
rx_buff[*rx_offset] = rx_char;
|
||||
/* avoid recive buff overflow */
|
||||
if (*rx_offset + 2 > RX_BUFF_LENGTH) {
|
||||
memmove(rx_buff, rx_buff + 1, RX_BUFF_LENGTH);
|
||||
return;
|
||||
}
|
||||
|
||||
/* recive next char */
|
||||
(*rx_offset)++;
|
||||
rx_buff[*rx_offset] = 0;
|
||||
}
|
||||
|
||||
/* Msp handle interrupt */
|
||||
#ifdef UART1_EXIST
|
||||
void __PIKA_USART1_IRQHandler(void) {}
|
||||
#endif
|
||||
#ifdef UART2_EXIST
|
||||
void USART2_IRQHandler(void) {
|
||||
// HAL_UART_IRQHandler(&pika_uart2.huart);
|
||||
void __PIKA_USART1_IRQHandler(char rx_char) {
|
||||
__callback_UART_recive(USART1, rx_char);
|
||||
}
|
||||
void USART2_IRQHandler() {
|
||||
uint8_t rx_char = LL_USART_ReceiveData8(USART2);
|
||||
__callback_UART_recive(USART2, rx_char);
|
||||
}
|
||||
#endif
|
||||
|
||||
void STM32_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;
|
||||
}
|
||||
USART_TypeDef* UARTx = getUartInstance(id);
|
||||
UART_GPIO_init(UARTx);
|
||||
int errCode = USART_UART_Init(baudRate, id);
|
||||
USART_TypeDef* USARTx = UART_get_instance(id);
|
||||
int errCode = UART_hardware_init(baudRate, id);
|
||||
if (0 != errCode) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] uart init faild.");
|
||||
obj_setSysOut(self, "[error] USART init faild.");
|
||||
return;
|
||||
}
|
||||
HAL_UART_Receive_IT(getUartHandle(id), (uint8_t*)getUartRxBuff(id), 1);
|
||||
}
|
||||
|
||||
void STM32_UART_platformRead(PikaObj* self) {
|
||||
int id = obj_getInt(self, "id");
|
||||
USART_TypeDef* USARTx = UART_get_instance(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) {
|
||||
uint8_t* rx_offset = UART_get_rx_offset(USARTx);
|
||||
char* rx_buff = UART_get_rx_buff(USARTx);
|
||||
if (length >= *rx_offset) {
|
||||
/* not enough str */
|
||||
length = pika_uart->rxBuffOffset;
|
||||
length = *rx_offset;
|
||||
}
|
||||
readBuff = args_getBuff(buffs, length);
|
||||
memcpy(readBuff, pika_uart->rxBuff, length);
|
||||
char* readBuff = args_getBuff(buffs, length);
|
||||
memcpy(readBuff, rx_buff, 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);
|
||||
memcpy(rx_buff, rx_buff + length, *rx_offset - length);
|
||||
*rx_offset -= length;
|
||||
rx_buff[*rx_offset] = 0;
|
||||
exit:
|
||||
args_deinit(buffs);
|
||||
obj_setStr(self, "readData", readBuff);
|
||||
@ -165,52 +174,13 @@ exit:
|
||||
void STM32_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 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);
|
||||
USART_TypeDef* USARTx = UART_get_instance(id);
|
||||
for (int i = 0; i < strGetSize(data); i++) {
|
||||
LL_USART_TransmitData8(USARTx, data[i]);
|
||||
while (LL_USART_IsActiveFlag_TC(USARTx) != 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)) {
|
||||
#ifdef Code_ENABLE
|
||||
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;
|
||||
}
|
||||
/* run as shell */
|
||||
memset(pikaShell, 0, RX_BUFF_LENGTH);
|
||||
strGetLastLine(pikaShell, pika_uart->rxBuff);
|
||||
pikaShellRxOk = 1;
|
||||
#endif
|
||||
}
|
||||
/* 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);
|
||||
}
|
||||
|
@ -72,14 +72,6 @@
|
||||
#define Code_ENABLE
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
UART_HandleTypeDef huart;
|
||||
uint8_t id;
|
||||
char rxBuff[RX_BUFF_LENGTH];
|
||||
uint16_t rxBuffOffset;
|
||||
PikaObj* obj;
|
||||
} pika_uart_t;
|
||||
|
||||
typedef struct pika_IIC_info_t {
|
||||
GPIO_TypeDef* SCL_GPIO;
|
||||
GPIO_TypeDef* SDA_GPIO;
|
||||
@ -97,6 +89,5 @@ uint32_t getPinMode(char* mode);
|
||||
uint8_t GPIO_enable_clock(char* pin);
|
||||
void delay_us(uint32_t delay);
|
||||
void delay_unit(uint32_t delay);
|
||||
void STM32_UART_clearRxBuff(pika_uart_t* pika_uart);
|
||||
|
||||
#endif
|
@ -59,7 +59,6 @@ void HARDWARE_PRINTF_Init(void) {
|
||||
}
|
||||
|
||||
int fputc(int ch, FILE* f) {
|
||||
char ch_ = ch;
|
||||
LL_USART_TransmitData8(USART1, ch);
|
||||
while (LL_USART_IsActiveFlag_TC(USART1) != 1)
|
||||
;
|
||||
|
@ -3,13 +3,13 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include "pikaObj.h"
|
||||
typedef struct _CodeHeap{
|
||||
char *content;
|
||||
typedef struct _CodeHeap {
|
||||
char* content;
|
||||
uint32_t size;
|
||||
uint8_t ena;
|
||||
uint32_t reciveTime;
|
||||
uint32_t oldSize;
|
||||
}CodeHeap;
|
||||
} CodeHeap;
|
||||
|
||||
/* support std lib for stm32 */
|
||||
#define delay_ms HAL_Delay
|
||||
@ -41,7 +41,8 @@ typedef struct _CodeHeap{
|
||||
void HARDWARE_PRINTF_Init(void);
|
||||
|
||||
/* support write asm to flash */
|
||||
#define FLASH_SCRIPT_START_ADDR (FLASH_BASE + ((FLASH_PAGE_NB - 2) * FLASH_PAGE_SIZE))
|
||||
#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
|
||||
@ -49,10 +50,10 @@ void HARDWARE_PRINTF_Init(void);
|
||||
#define RX_BUFF_LENGTH 32
|
||||
|
||||
/* support download python script by uart1 */
|
||||
uint8_t STM32_Code_reciveHandler(char *data, uint32_t rxSize);
|
||||
uint8_t STM32_Code_reciveHandler(char* data, uint32_t rxSize);
|
||||
void STM32_Code_Init();
|
||||
void STM32_Code_flashHandler();
|
||||
|
||||
/* handler for usart1 */
|
||||
__attribute__((weak)) void __PIKA_USART1_IRQHandler(void);
|
||||
void __PIKA_USART1_IRQHandler(char rx_char);
|
||||
#endif
|
@ -26,21 +26,21 @@ void SysTick_Handler(void) {
|
||||
STM32_Code_flashHandler();
|
||||
}
|
||||
|
||||
__attribute__((weak)) void __PIKA_USART1_IRQHandler(void){
|
||||
}
|
||||
__attribute__((weak)) void __PIKA_USART1_IRQHandler(char rx_char) {}
|
||||
|
||||
void USART1_IRQHandler(void) {
|
||||
if (LL_USART_IsActiveFlag_RXNE(USART1)) {
|
||||
uint8_t inputChar = LL_USART_ReceiveData8(USART1);
|
||||
uint8_t rx_char = LL_USART_ReceiveData8(USART1);
|
||||
__PIKA_USART1_IRQHandler(rx_char);
|
||||
/* clear buff when overflow */
|
||||
if (UART1_RXBuff_offset >= RX_BUFF_LENGTH) {
|
||||
UART1_RXBuff_offset = 0;
|
||||
memset(UART1_RxBuff, 0, sizeof(UART1_RxBuff));
|
||||
}
|
||||
/* recive char */
|
||||
UART1_RxBuff[UART1_RXBuff_offset] = inputChar;
|
||||
UART1_RxBuff[UART1_RXBuff_offset] = rx_char;
|
||||
UART1_RXBuff_offset++;
|
||||
if ('\n' == inputChar) {
|
||||
if ('\n' == rx_char) {
|
||||
/* handle python script download */
|
||||
if (STM32_Code_reciveHandler(UART1_RxBuff, UART1_RXBuff_offset)) {
|
||||
goto line_exit;
|
||||
|
Loading…
x
Reference in New Issue
Block a user