diff --git a/demo/stm32g030c8/Core/Src/stm32g0xx_it.c b/demo/stm32g030c8/Core/Src/stm32g0xx_it.c index 586f4f0cf..3527b07ed 100644 --- a/demo/stm32g030c8/Core/Src/stm32g0xx_it.c +++ b/demo/stm32g030c8/Core/Src/stm32g0xx_it.c @@ -23,7 +23,7 @@ #include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ -#include "STM32_UART.h" +#include "STM32_common.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -59,24 +59,7 @@ /* External variables --------------------------------------------------------*/ /* USER CODE BEGIN EV */ -extern struct CodeHeap { - char* content; - uint32_t size; - uint8_t ena; - uint32_t reciveTime; -} codeHeap; -#define FLASH_USER_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 */ -static uint32_t GetPage(uint32_t Addr) { - return (Addr - FLASH_BASE) / FLASH_PAGE_SIZE; - ; -} -#define DATA_64 ((uint64_t)0x1234567812345678) -#define DATA_32 ((uint32_t)0x12345678) /* USER CODE END EV */ @@ -142,78 +125,7 @@ void SysTick_Handler(void) { /* USER CODE END SysTick_IRQn 0 */ HAL_IncTick(); /* USER CODE BEGIN SysTick_IRQn 1 */ - if (codeHeap.ena) { - /* transmite is finished */ - if (uwTick - codeHeap.reciveTime > 200) { - uint32_t FirstPage = 0, NbOfPages = 0; - uint32_t Address = 0, 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]: Updating flash... \r\n"); - HAL_FLASH_Unlock(); - /* Get the 1st page to erase */ - FirstPage = GetPage(FLASH_USER_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) { - } - } - Address = FLASH_USER_START_ADDR; - - while (Address < FLASH_USER_END_ADDR) { - if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, - DATA_64) == HAL_OK) { - Address = Address + 8; - } else { - printf("[error]: Write flash faild. \r\n"); - while (1) { - } - } - } - HAL_FLASH_Lock(); - printf("[info]: Write flash ok! \r\n"); - - Address = FLASH_USER_START_ADDR; - MemoryProgramStatus = 0x0; - - printf("[info]: Checking flash... \r\n"); - - while (Address < FLASH_USER_END_ADDR) { - data32 = *(__IO uint32_t*)Address; - if (data32 != DATA_32) { - MemoryProgramStatus++; - } - Address = Address + 4; - } - - /*Check if there is an issue to program data*/ - if (MemoryProgramStatus != 0) { - printf("memory state: %d\r\n", MemoryProgramStatus); - printf("[error]: Check flash faild.\r\n"); - while (1) { - } - } - - printf("[OK]: Programing ok! \r\n"); - printf("[info]: Restarting... \r\n"); - printf("==============[Programer]==============\r\n"); - printf("\r\n"); - HAL_NVIC_SystemReset(); - } - } + STM32_Code_flashHandler(); /* USER CODE END SysTick_IRQn 1 */ } diff --git a/demo/stm32g030c8/MDK-ARM/stm32g030c8.uvprojx b/demo/stm32g030c8/MDK-ARM/stm32g030c8.uvprojx index bb7ac12ec..e7b24ecdd 100644 --- a/demo/stm32g030c8/MDK-ARM/stm32g030c8.uvprojx +++ b/demo/stm32g030c8/MDK-ARM/stm32g030c8.uvprojx @@ -16,7 +16,7 @@ STM32G030C8Tx STMicroelectronics - Keil.STM32G0xx_DFP.1.3.0 + Keil.STM32G0xx_DFP.1.2.0 http://www.keil.com/pack/ IRAM(0x20000000-0x20001FFF) IROM(0x8000000-0x800FFFF) CLOCK(8000000) CPUTYPE("Cortex-M0+") TZ @@ -800,6 +800,11 @@ 4 ..\pikascript\pikascript-lib\PikaPiZero\RGB_ASM.lib + + STM32_Code.c + 1 + ..\pikascript\pikascript-lib\STM32\STM32_Code.c + diff --git a/demo/stm32g030c8/pikascript/main_rgb.py b/demo/stm32g030c8/pikascript/main_rgb.py new file mode 100644 index 000000000..75f04779b --- /dev/null +++ b/demo/stm32g030c8/pikascript/main_rgb.py @@ -0,0 +1,19 @@ +import STM32 +import PikaPiZero +import PikaStdLib + +uart.init() +uart.setId(1) +uart.setBaudRate(9600) +uart.enable() + +print('initing rgb...') +rgb.init() +rgb.enable() +print('init rgb ok!') + +print('mem max:') +mem.max() +while True: + time.sleep_ms(50) + rgb.flow() diff --git a/demo/stm32g030c8/pikascript/pikascript-lib/STM32/STM32_Code.c b/demo/stm32g030c8/pikascript/pikascript-lib/STM32/STM32_Code.c new file mode 100644 index 000000000..a03b326a4 --- /dev/null +++ b/demo/stm32g030c8/pikascript/pikascript-lib/STM32/STM32_Code.c @@ -0,0 +1,129 @@ +#include +#include "BaseObj.h" +#include "STM32_common.h" +#include "dataStrs.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(data); + } + } + if (1 == codeHeap.ena) { + codeHeap.reciveTime = uwTick; + codeHeap.oldContent = codeHeap.content; + codeHeap.oldSize = codeHeap.size; + codeHeap.size += rxSize; + codeHeap.content = pikaMalloc(codeHeap.size + 1); + memcpy(codeHeap.content, codeHeap.oldContent, codeHeap.oldSize); + memcpy(codeHeap.content + codeHeap.oldSize, data, rxSize); + pikaFree(codeHeap.oldContent, codeHeap.oldSize + 1); + codeHeap.content[codeHeap.size] = 0; + /* reciving code */ + return 1; + } + /* not work */ + return 0; +} + +void STM32_Code_flashHandler() { + if (codeHeap.ena) { + /* transmite is finished */ + if (uwTick - codeHeap.reciveTime > 200) { + uint32_t FirstPage = 0, NbOfPages = 0; + uint32_t PageError = 0; + __IO uint32_t data32 = 0, MemoryProgramStatus = 0; + uint64_t writeData64 = 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_USER_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_USER_START_ADDR; + uint32_t writeAddress = 0; + while (writeAddress < codeHeap.size) { + for (int i = 7; i >= 0; i--) { + writeData64 = writeData64 << 8; + writeData64 += codeHeap.content[writeAddress + i]; + } + 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_USER_START_ADDR; + MemoryProgramStatus = 0x0; + + printf(" [info]: Checking flash... \r\n"); + char* codeInFlash = (char*)baseAddress; + printf("---------[code in flash]----------\r\n"); + printf("\r\n"); + printf("%s", codeInFlash); + printf("\r\n\r\n"); + printf("---------[code in flash]----------\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("==============[Programer]==============\r\n"); + + printf("[info]: Restarting... \r\n"); + printf("\r\n"); + HAL_NVIC_SystemReset(); + } + } +} \ No newline at end of file diff --git a/demo/stm32g030c8/pikascript/pikascript-lib/STM32/STM32_UART.c b/demo/stm32g030c8/pikascript/pikascript-lib/STM32/STM32_UART.c index dc2638275..41116bb90 100644 --- a/demo/stm32g030c8/pikascript/pikascript-lib/STM32/STM32_UART.c +++ b/demo/stm32g030c8/pikascript/pikascript-lib/STM32/STM32_UART.c @@ -4,23 +4,6 @@ #include "STM32_common.h" #include "dataStrs.h" -#define RX_BUFF_LENGTH 64 - -struct CodeHeap{ - char *content; - uint32_t size; - uint8_t ena; - uint32_t reciveTime; -}codeHeap; - -typedef struct { - UART_HandleTypeDef huart; - uint8_t id; - char rxBuff[RX_BUFF_LENGTH]; - uint16_t rxBuffOffset; - PikaObj* obj; -} pika_uart_t; - #ifdef UART1_EXIST pika_uart_t pika_uart1; #endif @@ -354,10 +337,7 @@ void USART3_4_IRQHandler(void) { #endif void STM32_UART_platformEnable(PikaObj* self, int baudRate, int id) { - codeHeap.size = 0; - codeHeap.content = pikaMalloc(codeHeap.size + 1); - codeHeap.ena = 0; - + STM32_Code_Init(); setUartObj(id, self); UART_HandleTypeDef* huart = getUartHandle(id); huart->Instance = getUartInstance(id); @@ -388,8 +368,8 @@ char* STM32_UART_platformRead(PikaObj* self, int id, int length) { memcpy(pika_uart->rxBuff, pika_uart->rxBuff + length, pika_uart->rxBuffOffset - length); pika_uart->rxBuffOffset -= length; - pika_uart->rxBuff[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); @@ -402,68 +382,38 @@ 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) { - char *oldContent; - uint32_t oldSize; uint8_t id = getUartId(huart); pika_uart_t* pika_uart = getPikaUart(id); char inputChar = pika_uart->rxBuff[pika_uart->rxBuffOffset]; - - if(id == 1){ - - if( '\n' == inputChar){ - if( 1 == codeHeap.ena){ - codeHeap.reciveTime = uwTick; - oldContent = codeHeap.content; - oldSize = codeHeap.size; - uint16_t rxSize = pika_uart->rxBuffOffset + 1; - codeHeap.size += rxSize; - codeHeap.content = pikaMalloc(codeHeap.size + 1); - memcpy(codeHeap.content, oldContent, oldSize); - memcpy(codeHeap.content + oldSize, pika_uart->rxBuff, rxSize); - pikaFree(oldContent, oldSize + 1); - codeHeap.content[codeHeap.size] = 0; - - 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); - goto exit; - } - if( 0 == codeHeap.ena ){ - char buff[RX_BUFF_LENGTH] = {0}; - char *strLine = strGetLastLine(buff, pika_uart->rxBuff); - if( strIsStartWith(strLine, "import ") ){ - codeHeap.reciveTime = uwTick; - codeHeap.ena = 1; - - 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); - goto exit; - } - } + + 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 */ + /* recive next char, avoid recive buff overflow */ if (pika_uart->rxBuffOffset + 2 < RX_BUFF_LENGTH) { pika_uart->rxBuffOffset++; pika_uart->rxBuff[pika_uart->rxBuffOffset] = 0; } + UART_Start_Receive_IT( huart, (uint8_t*)(pika_uart->rxBuff + pika_uart->rxBuffOffset), 1); - - - goto exit; -exit: - return; } /* support prinf */ diff --git a/demo/stm32g030c8/pikascript/pikascript-lib/STM32/STM32_common.h b/demo/stm32g030c8/pikascript/pikascript-lib/STM32/STM32_common.h index 9a6d312f9..d1b249ce6 100644 --- a/demo/stm32g030c8/pikascript/pikascript-lib/STM32/STM32_common.h +++ b/demo/stm32g030c8/pikascript/pikascript-lib/STM32/STM32_common.h @@ -1,5 +1,6 @@ #ifndef __STM32__COMMON__H #define __STM32__COMMON__H +#include "PikaObj.h" #ifdef STM32G070xx #include "stm32g0xx_hal.h" @@ -33,11 +34,46 @@ #define TIM17_EXIST #endif +#define RX_BUFF_LENGTH 64 + +#define FLASH_USER_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 */ +static uint32_t GetPage(uint32_t Addr) { + return (Addr - FLASH_BASE) / FLASH_PAGE_SIZE; + ; +} +#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; + + char *oldContent; + 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 \ No newline at end of file