mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
189 lines
5.4 KiB
C
189 lines
5.4 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include "delay.h"
|
|
#include "air32f10x.h"
|
|
#include "air_rcc.h"
|
|
|
|
#define PRINTF_LOG printf
|
|
#include "pikaScript.h"
|
|
USART_TypeDef* USART_TEST = USART1;
|
|
|
|
void UART_Configuration(uint32_t bound);
|
|
void DMA_RecvConfiguration(void);
|
|
void DMA_SendConfiguration(void);
|
|
#define BUFFSIZE 8
|
|
uint8_t Buff[BUFFSIZE];
|
|
void RCC_ClkConfiguration(void);
|
|
|
|
int main(void)
|
|
{
|
|
RCC_ClocksTypeDef clocks;
|
|
RCC_ClkConfiguration(); //????
|
|
|
|
Delay_Init();
|
|
UART_Configuration(115200);
|
|
RCC_GetClocksFreq(&clocks);
|
|
|
|
PRINTF_LOG("\n");
|
|
PRINTF_LOG("SYSCLK: %3.1fMhz, HCLK: %3.1fMhz, PCLK1: %3.1fMhz, PCLK2: %3.1fMhz, ADCCLK: %3.1fMhz\n", \
|
|
(float)clocks.SYSCLK_Frequency/1000000, (float)clocks.HCLK_Frequency/1000000, \
|
|
(float)clocks.PCLK1_Frequency/1000000, (float)clocks.PCLK2_Frequency / 1000000, (float)clocks.ADCCLK_Frequency / 1000000);
|
|
|
|
PRINTF_LOG("Hardware init ok!\n");
|
|
PikaObj* pikaMain = pikaScriptInit();
|
|
pikaScriptShell(pikaMain);
|
|
while(1)
|
|
{
|
|
}
|
|
}
|
|
|
|
void RCC_ClkConfiguration(void)
|
|
{
|
|
RCC_DeInit(); //??RCC???
|
|
|
|
RCC_HSEConfig(RCC_HSE_ON); //??HSE
|
|
while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET)
|
|
; //??HSE??
|
|
|
|
RCC_PLLCmd(DISABLE); //??PLL
|
|
AIR_RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_27, 1); //??PLL,8*27=216MHz
|
|
|
|
RCC_PLLCmd(ENABLE); //??PLL
|
|
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
|
|
; //??PLL??
|
|
|
|
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //??PLL??????
|
|
|
|
RCC_HCLKConfig(RCC_SYSCLK_Div1); //??AHB??
|
|
RCC_PCLK1Config(RCC_HCLK_Div2); //??APB1??
|
|
RCC_PCLK2Config(RCC_HCLK_Div1); //??APB2??
|
|
|
|
RCC_LSICmd(ENABLE); //????????
|
|
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
|
|
; //??LSI??
|
|
RCC_HSICmd(ENABLE); //????????
|
|
while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET)
|
|
; //??HSI??
|
|
}
|
|
|
|
void UART_Configuration(uint32_t bound)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
USART_InitTypeDef USART_InitStructure;
|
|
NVIC_InitTypeDef NVIC_InitStructe;
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
USART_InitStructure.USART_BaudRate = bound;
|
|
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
|
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
|
USART_InitStructure.USART_Parity = USART_Parity_No;
|
|
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
|
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
|
|
|
USART_Init(USART_TEST, &USART_InitStructure);
|
|
|
|
NVIC_InitStructe.NVIC_IRQChannel = USART1_IRQn;
|
|
NVIC_InitStructe.NVIC_IRQChannelCmd = ENABLE;
|
|
NVIC_InitStructe.NVIC_IRQChannelPreemptionPriority = 1;
|
|
NVIC_InitStructe.NVIC_IRQChannelSubPriority = 0;
|
|
NVIC_Init(&NVIC_InitStructe);
|
|
|
|
USART_ITConfig(USART_TEST, USART_IT_RXNE, ENABLE);
|
|
USART_Cmd(USART_TEST, ENABLE);
|
|
}
|
|
|
|
void DMA_RecvConfiguration(void)
|
|
{
|
|
DMA_InitTypeDef DMA_InitStructure;
|
|
|
|
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
|
|
|
|
//USART1_RX DMA Config
|
|
DMA_DeInit(DMA1_Channel5);
|
|
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t )&USART1->DR;
|
|
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t )Buff;
|
|
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
|
|
DMA_InitStructure.DMA_BufferSize = BUFFSIZE;
|
|
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
|
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
|
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
|
|
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
|
|
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
|
|
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
|
|
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
|
|
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
|
|
|
|
DMA_Cmd(DMA1_Channel5, ENABLE);
|
|
}
|
|
|
|
void DMA_SendConfiguration(void)
|
|
{
|
|
DMA_InitTypeDef DMA_InitStructure;
|
|
|
|
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
|
|
|
|
//USART1_TX DMA Config
|
|
DMA_DeInit(DMA1_Channel4);
|
|
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t )&USART1->DR;
|
|
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t )Buff;
|
|
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
|
|
DMA_InitStructure.DMA_BufferSize = BUFFSIZE;
|
|
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
|
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
|
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
|
|
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
|
|
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
|
|
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
|
|
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
|
|
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
|
|
|
|
DMA_Cmd(DMA1_Channel4, ENABLE);
|
|
}
|
|
|
|
int SER_PutChar (int ch)
|
|
{
|
|
while(!USART_GetFlagStatus(USART_TEST,USART_FLAG_TC));
|
|
USART_SendData(USART_TEST, (uint8_t) ch);
|
|
|
|
return ch;
|
|
}
|
|
|
|
int fputc(int c, FILE *f)
|
|
{
|
|
/* Place your implementation of fputc here */
|
|
/* e.g. write a character to the USART */
|
|
if (c == '\n')
|
|
{
|
|
SER_PutChar('\r');
|
|
}
|
|
return (SER_PutChar(c));
|
|
}
|
|
|
|
volatile uint8_t g_ch;
|
|
void USART1_IRQHandler(void)
|
|
{
|
|
if(USART_GetITStatus(USART1,USART_IT_RXNE))
|
|
{
|
|
g_ch = USART_ReceiveData(USART1);
|
|
}
|
|
}
|
|
|
|
char __platform_getchar(void) {
|
|
while(g_ch==0);
|
|
uint8_t ch = g_ch;
|
|
g_ch = 0;
|
|
return ch;
|
|
}
|