support uart3 and uart4 as console

This commit is contained in:
Lyon 2023-04-09 17:09:39 +08:00
parent 1e725a1a11
commit 7c0ee24f88
4 changed files with 177 additions and 133 deletions

View File

@ -49,44 +49,10 @@ PikaObj *pikaMain;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
volatile static char rx_char = 0;
char UART1_RxBuff[RX_BUFF_LENGTH] = {0};
uint16_t UART1_RXBuff_offset = 0;
__attribute__((weak)) void __PIKA_USART1_IRQHandler(char rx_char) {}
void USART1_IRQHandler(void){
if (LL_USART_IsActiveFlag_RXNE(USART1)) {
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] = rx_char;
UART1_RXBuff_offset++;
if ('\n' == rx_char) {
/* handle python script download */
if (STM32_Code_reciveHandler(UART1_RxBuff, UART1_RXBuff_offset)) {
goto line_exit;
}
line_exit:
UART1_RXBuff_offset = 0;
memset(UART1_RxBuff, 0, sizeof(UART1_RxBuff));
return;
}
}
}
volatile static char g_rx_char = 0;
char CONSOLE_RxBuff[RX_BUFF_LENGTH] = {0};
uint16_t CONSOLE_RxBuff_offset = 0;
/* support pikaScript Shell */
char __platform_getchar(){
char res = 0;
while(rx_char == 0){
};
res = rx_char;
rx_char = 0;
return res;
}
int64_t pika_platform_getTick(void){
@ -105,6 +71,147 @@ void pika_platform_sleep_s(uint32_t s){
extern PikaObj *__pikaMain;
// ѡ<><D1A1>ʹ<EFBFBD>õĴ<C3B5><C4B4><EFBFBD>
#define USING_CONSOLE_USART1 1
// #define USING_CONSOLE_USART3 1
// #define USING_CONSOLE_USART4 1
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD>Ĵ<EFBFBD><C4B4>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>κ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>򱨴<EFBFBD>
#if !USING_CONSOLE_USART1 && !USING_CONSOLE_USART3 && !USING_CONSOLE_USART4
#error "Please define a console USART (1, 3, or 4) to use by setting the USING_CONSOLE_USARTX to 1."
#endif
#if (USING_CONSOLE_USART1)
#define USART_CONSOLE USART1
#define USART_CONSOLE_IRQn USART1_IRQn
#define USART_CONSOLE_GPIO GPIOA
#define USART_CONSOLE_PIN_TX LL_GPIO_PIN_9
#define USART_CONSOLE_PIN_RX LL_GPIO_PIN_10
#define USART_CONSOLE_AF LL_GPIO_AF_1
#define USART_CONSOLE_CLK_ENABLE() LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1)
#define USART_CONSOLE_GPIO_CLK_ENABLE() LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA)
#define USART_CONSOLE_IRQHandler USART1_IRQHandler
#elif (USING_CONSOLE_USART3)
#define USART_CONSOLE USART3
#define USART_CONSOLE_IRQn USART3_4_IRQn
#define USART_CONSOLE_GPIO GPIOD
#define USART_CONSOLE_PIN_TX LL_GPIO_PIN_8
#define USART_CONSOLE_PIN_RX LL_GPIO_PIN_9
#define USART_CONSOLE_AF LL_GPIO_AF_0
#define USART_CONSOLE_CLK_ENABLE() LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART3)
#define USART_CONSOLE_GPIO_CLK_ENABLE() LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOD)
#define USART_CONSOLE_IRQHandler USART3_4_IRQHandler
#elif (USING_CONSOLE_USART4)
#define USART_CONSOLE USART4
#define USART_CONSOLE_IRQn USART3_4_IRQn
#define USART_CONSOLE_GPIO GPIOC
#define USART_CONSOLE_PIN_TX LL_GPIO_PIN_10
#define USART_CONSOLE_PIN_RX LL_GPIO_PIN_11
#define USART_CONSOLE_AF LL_GPIO_AF_0
#define USART_CONSOLE_CLK_ENABLE() LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART4)
#define USART_CONSOLE_GPIO_CLK_ENABLE() LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOC)
#define USART_CONSOLE_IRQHandler USART3_4_IRQHandler
#endif
__attribute__((weak)) void __PIKA_USART_IRQHandler(USART_TypeDef* USARTx, char rx_char) {};
void USART_CONSOLE_IRQHandler(void) {
if (LL_USART_IsActiveFlag_RXNE(USART_CONSOLE)) {
g_rx_char = LL_USART_ReceiveData8(USART_CONSOLE);
__PIKA_USART_IRQHandler(USART_CONSOLE, g_rx_char);
/* clear buff when overflow */
if (CONSOLE_RxBuff_offset >= RX_BUFF_LENGTH) {
CONSOLE_RxBuff_offset = 0;
memset(CONSOLE_RxBuff, 0, sizeof(CONSOLE_RxBuff));
}
/* recive char */
CONSOLE_RxBuff[CONSOLE_RxBuff_offset] = g_rx_char;
CONSOLE_RxBuff_offset++;
if ('\n' == g_rx_char || '\r' == g_rx_char) {
/* handle python script download */
if (STM32_Code_reciveHandler(CONSOLE_RxBuff, CONSOLE_RxBuff_offset)) {
goto line_exit;
}
line_exit:
CONSOLE_RxBuff_offset = 0;
memset(CONSOLE_RxBuff, 0, sizeof(CONSOLE_RxBuff));
return;
}
}
}
/* support printf */
void HARDWARE_PRINTF_Init(void) {
LL_USART_InitTypeDef USART_InitStruct = {0};
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
// Enable peripheral and GPIO clocks
USART_CONSOLE_CLK_ENABLE();
USART_CONSOLE_GPIO_CLK_ENABLE();
// Configure USART GPIO
GPIO_InitStruct.Pin = USART_CONSOLE_PIN_TX;
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 = USART_CONSOLE_AF;
LL_GPIO_Init(USART_CONSOLE_GPIO, &GPIO_InitStruct);
GPIO_InitStruct.Pin = USART_CONSOLE_PIN_RX;
LL_GPIO_Init(USART_CONSOLE_GPIO, &GPIO_InitStruct);
// USART interrupt Init
NVIC_SetPriority(USART_CONSOLE_IRQn, 0);
NVIC_EnableIRQ(USART_CONSOLE_IRQn);
USART_InitStruct.PrescalerValue = LL_USART_PRESCALER_DIV1;
USART_InitStruct.BaudRate = 115200;
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(USART_CONSOLE, &USART_InitStruct);
LL_USART_SetTXFIFOThreshold(USART_CONSOLE, LL_USART_FIFOTHRESHOLD_1_8);
LL_USART_SetRXFIFOThreshold(USART_CONSOLE, LL_USART_FIFOTHRESHOLD_1_8);
LL_USART_DisableFIFO(USART_CONSOLE);
LL_USART_ConfigAsyncMode(USART_CONSOLE);
LL_USART_Enable(USART_CONSOLE);
// Polling USART initialisation
while ((!(LL_USART_IsActiveFlag_TEACK(USART_CONSOLE))) ||
(!(LL_USART_IsActiveFlag_REACK(USART_CONSOLE)))) {
}
// Enable interrupt
LL_USART_EnableIT_RXNE(USART_CONSOLE);
LL_USART_EnableIT_PE(USART_CONSOLE);
}
int fputc(int ch, FILE* f) {
LL_USART_TransmitData8(USART_CONSOLE, ch);
while (LL_USART_IsActiveFlag_TC(USART_CONSOLE) != 1)
;
return ch;
}
/* support pikaScript Shell */
char __platform_getchar(){
char res = 0;
while(g_rx_char == 0){
};
res = g_rx_char;
g_rx_char = 0;
return res;
}
int main(void){
HAL_Init();
SystemClock_Config();

View File

@ -12,69 +12,6 @@ void __platform_enable_irq_handle(){
}
/* support printf */
void HARDWARE_PRINTF_Init(void) {
LL_USART_InitTypeDef USART_InitStruct = {0};
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
/* Peripheral clock enable */
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
GPIO_InitStruct.Pin = LL_GPIO_PIN_9;
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_10;
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);
/* USART1 interrupt Init */
NVIC_SetPriority(USART1_IRQn, 0);
NVIC_EnableIRQ(USART1_IRQn);
USART_InitStruct.PrescalerValue = LL_USART_PRESCALER_DIV1;
USART_InitStruct.BaudRate = 115200;
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(USART1, &USART_InitStruct);
LL_USART_SetTXFIFOThreshold(USART1, LL_USART_FIFOTHRESHOLD_1_8);
LL_USART_SetRXFIFOThreshold(USART1, LL_USART_FIFOTHRESHOLD_1_8);
LL_USART_DisableFIFO(USART1);
LL_USART_ConfigAsyncMode(USART1);
LL_USART_Enable(USART1);
/* Polling USART1 initialisation */
while ((!(LL_USART_IsActiveFlag_TEACK(USART1))) ||
(!(LL_USART_IsActiveFlag_REACK(USART1)))) {
}
/* open interrupt */
LL_USART_EnableIT_RXNE(USART1);
LL_USART_EnableIT_PE(USART1);
}
int fputc(int ch, FILE* f) {
LL_USART_TransmitData8(USART1, ch);
while (LL_USART_IsActiveFlag_TC(USART1) != 1)
;
return ch;
}
/* support delay_us */

View File

@ -340,7 +340,7 @@
<MiscControls></MiscControls>
<Define>USE_FULL_LL_DRIVER, USE_HAL_DRIVER,STM32G070xx</Define>
<Undefine></Undefine>
<IncludePath>..\Booter;../Drivers/STM32G0xx_HAL_Driver/Inc;../Drivers/STM32G0xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32G0xx/Include;../Drivers/CMSIS/Include;..\Drivers\CMSIS\DSP\Include;..\pikascript\pikascript-lib/PikaStdDevice;..\pikascript\pikascript-lib/STM32G0;..\pikascript\pikascript-lib/PikaPiZero;..\pikascript\pikascript-lib/PikaStdLib;..\pikascript\pikascript-api;..\pikascript\pikascript-core</IncludePath>
<IncludePath>..\Booter;../Drivers/STM32G0xx_HAL_Driver/Inc;../Drivers/STM32G0xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32G0xx/Include;../Drivers/CMSIS/Include;..\Drivers\CMSIS\DSP\Include;..\pikascript\pikascript-lib/PikaStdDevice;..\pikascript\pikascript-lib/time;..\pikascript\pikascript-lib/STM32G0;..\pikascript\pikascript-lib/PikaPiZero;..\pikascript\pikascript-lib/PikaStdLib;..\pikascript\pikascript-api;..\pikascript\pikascript-core</IncludePath>
</VariousControls>
</Cads>
<Aads>
@ -714,11 +714,41 @@
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib/PikaStdDevice/PikaStdDevice_DAC.c</FilePath>
</File>
<File>
<FileName>_time.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib/time/_time.c</FilePath>
</File>
<File>
<FileName>STM32G0_common.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib/STM32G0/STM32G0_common.c</FilePath>
</File>
<File>
<FileName>pika_hal_STM32G0_GPIO.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib/STM32G0/pika_hal_STM32G0_GPIO.c</FilePath>
</File>
<File>
<FileName>pika_hal_STM32G0_PWM.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib/STM32G0/pika_hal_STM32G0_PWM.c</FilePath>
</File>
<File>
<FileName>pika_hal_STM32G0_ADC.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib/STM32G0/pika_hal_STM32G0_ADC.c</FilePath>
</File>
<File>
<FileName>pika_hal_STM32G0_UART.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib/STM32G0/pika_hal_STM32G0_UART.c</FilePath>
</File>
<File>
<FileName>pika_hal_STM32G0_SPI.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib/STM32G0/pika_hal_STM32G0_SPI.c</FilePath>
</File>
<File>
<FileName>PikaPiZero_RGB.c</FileName>
<FileType>1</FileType>
@ -814,36 +844,6 @@
<FileType>4</FileType>
<FilePath>..\pikascript\pikascript-lib/PikaPiZero/RGB_ASM.lib</FilePath>
</File>
<File>
<FileName>_time.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib\time\_time.c</FilePath>
</File>
<File>
<FileName>pika_hal_STM32G0_ADC.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib\STM32G0\pika_hal_STM32G0_ADC.c</FilePath>
</File>
<File>
<FileName>pika_hal_STM32G0_GPIO.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib\STM32G0\pika_hal_STM32G0_GPIO.c</FilePath>
</File>
<File>
<FileName>pika_hal_STM32G0_SPI.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib\STM32G0\pika_hal_STM32G0_SPI.c</FilePath>
</File>
<File>
<FileName>pika_hal_STM32G0_PWM.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib\STM32G0\pika_hal_STM32G0_PWM.c</FilePath>
</File>
<File>
<FileName>pika_hal_STM32G0_UART.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib\STM32G0\pika_hal_STM32G0_UART.c</FilePath>
</File>
</Files>
</Group>
<Group>

View File

@ -1,6 +1,6 @@
pikascript-core==v1.12.0
PikaStdLib==v1.12.0
PikaStdDevice==v2.3.0
STM32G0==v2.0.0
STM32G0==v2.1.0
PikaPiZero==v1.4.0
time==v0.1.0
time==v0.1.0