|
|
|
|
@ -21,6 +21,7 @@
|
|
|
|
|
|
|
|
|
|
/* Private includes ----------------------------------------------------------*/
|
|
|
|
|
/* USER CODE BEGIN Includes */
|
|
|
|
|
#include "SEGGER_SYSVIEW.h"
|
|
|
|
|
#include "SEGGER_RTT.h"
|
|
|
|
|
#include "stm32g0b1xx.h"
|
|
|
|
|
#include "stm32g0xx_hal_cortex.h"
|
|
|
|
|
@ -62,30 +63,80 @@ static void MX_GPIO_Init(void);
|
|
|
|
|
uint8_t rxBuffer[BUFFER_SIZE];
|
|
|
|
|
uint16_t rxBufferPos = 0;
|
|
|
|
|
|
|
|
|
|
void init_UART1(){
|
|
|
|
|
void init_UART1_it(){
|
|
|
|
|
RCC->APBENR2 |= RCC_APBENR2_USART1EN;
|
|
|
|
|
RCC->IOPENR |= RCC_IOPENR_GPIOCEN;
|
|
|
|
|
|
|
|
|
|
GPIOC->MODER &= ~GPIO_MODER_MODE5; // Alternate function mode on RX pin
|
|
|
|
|
GPIOC->MODER |= GPIO_MODER_MODE5_1;
|
|
|
|
|
GPIOC->AFR[0] &= GPIO_AFRL_AFSEL5;
|
|
|
|
|
GPIOC->AFR[0] |= 1 << GPIO_AFRL_AFSEL5_Pos;
|
|
|
|
|
GPIOC->AFR[0] |= 1 << GPIO_AFRL_AFSEL5_Pos; // AF1 -> USART1 RX
|
|
|
|
|
|
|
|
|
|
USART1->BRR = 128; // 32000000÷250000
|
|
|
|
|
USART1->CR1 = USART_CR1_RE;
|
|
|
|
|
|
|
|
|
|
USART1->CR3 |= USART_CR3_EIE;
|
|
|
|
|
USART1->CR1 |= USART_CR1_RXNEIE_RXFNEIE;
|
|
|
|
|
USART1->CR3 |= USART_CR3_EIE; // Interrupt on BREAK (and other errors)
|
|
|
|
|
USART1->CR1 |= USART_CR1_RXNEIE_RXFNEIE; // RX Interrupt
|
|
|
|
|
|
|
|
|
|
USART1->CR1 |= USART_CR1_UE;
|
|
|
|
|
|
|
|
|
|
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
|
|
|
|
|
HAL_NVIC_EnableIRQ(USART1_IRQn);
|
|
|
|
|
}
|
|
|
|
|
/* USER CODE END 0 */
|
|
|
|
|
|
|
|
|
|
void init_UART1_dma(){
|
|
|
|
|
RCC->IOPENR |= RCC_IOPENR_GPIOCEN;
|
|
|
|
|
RCC->APBENR2 |= RCC_APBENR2_USART1EN;
|
|
|
|
|
RCC->AHBENR |= RCC_AHBENR_DMA1EN;
|
|
|
|
|
|
|
|
|
|
GPIOC->MODER &= ~GPIO_MODER_MODE5; // Alternate function mode on RX pin
|
|
|
|
|
GPIOC->MODER |= GPIO_MODER_MODE5_1;
|
|
|
|
|
GPIOC->AFR[0] &= GPIO_AFRL_AFSEL5;
|
|
|
|
|
GPIOC->AFR[0] |= 1 << GPIO_AFRL_AFSEL5_Pos; // AF1 -> USART1 RX
|
|
|
|
|
|
|
|
|
|
USART1->BRR = 256; // 64000000÷250000
|
|
|
|
|
USART1->CR1 = USART_CR1_RE;
|
|
|
|
|
|
|
|
|
|
USART1->CR3 |= USART_CR3_EIE; // Interrupt on BREAK (and other errors)
|
|
|
|
|
USART1->CR3 |= USART_CR3_DMAR; // DMA Receiver mode
|
|
|
|
|
|
|
|
|
|
DMA1_Channel1->CCR = (0x02 << DMA_CCR_PL_Pos) | DMA_CCR_MINC | DMA_CCR_TCIE;
|
|
|
|
|
|
|
|
|
|
DMA1_Channel1->CMAR = ( uint32_t )&rxBuffer[0];
|
|
|
|
|
DMA1_Channel1->CPAR = ( uint32_t )&(USART1->RDR);
|
|
|
|
|
DMA1_Channel1->CNDTR = 5;
|
|
|
|
|
|
|
|
|
|
DMAMUX1_Channel0->CCR &= ~( DMAMUX_CxCR_DMAREQ_ID );
|
|
|
|
|
DMAMUX1_Channel0->CCR |= ( 50 << DMAMUX_CxCR_DMAREQ_ID_Pos ); // 50 -> USART1 RX
|
|
|
|
|
|
|
|
|
|
USART1->CR1 |= USART_CR1_UE;
|
|
|
|
|
|
|
|
|
|
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
|
|
|
|
|
HAL_NVIC_EnableIRQ(USART1_IRQn);
|
|
|
|
|
|
|
|
|
|
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
|
|
|
|
|
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Reset_DMA(void) {
|
|
|
|
|
// Disable the DMA channel
|
|
|
|
|
DMA1_Channel1->CCR &= ~DMA_CCR_EN; // Disable DMA Channel 1
|
|
|
|
|
|
|
|
|
|
// Clear the transfer complete flag
|
|
|
|
|
DMA1->IFCR |= DMA_IFCR_CTCIF1; // Clear transfer complete flag for Channel 1
|
|
|
|
|
|
|
|
|
|
// Reconfigure the DMA
|
|
|
|
|
DMA1_Channel1->CMAR = (uint32_t)rxBuffer; // Set memory address to the start of the buffer
|
|
|
|
|
DMA1_Channel1->CNDTR = BUFFER_SIZE; // Set number of data items to transfer
|
|
|
|
|
|
|
|
|
|
// Enable the DMA channel again
|
|
|
|
|
DMA1_Channel1->CCR |= DMA_CCR_EN; // Enable DMA Channel 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USART1_IRQHandler(){
|
|
|
|
|
SEGGER_SYSVIEW_RecordEnterISR();
|
|
|
|
|
if(USART1->ISR & USART_ISR_RXNE_RXFNE){
|
|
|
|
|
//SEGGER_SYSVIEW_PrintfHost("RXNE");
|
|
|
|
|
//printf("%x ", USART1->RDR);
|
|
|
|
|
rxBuffer[rxBufferPos++] = USART1->RDR;
|
|
|
|
|
if(rxBufferPos >= BUFFER_SIZE){
|
|
|
|
|
@ -94,17 +145,24 @@ void USART1_IRQHandler(){
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(USART1->ISR & USART_ISR_FE){
|
|
|
|
|
//TODO read rx==0
|
|
|
|
|
USART1->ICR = USART_ICR_FECF;
|
|
|
|
|
printf("FE after %d\n",rxBufferPos);
|
|
|
|
|
rxBufferPos = 0;
|
|
|
|
|
printf("buf: ");
|
|
|
|
|
for(uint16_t i=0; i<10; i++)
|
|
|
|
|
printf("0x%02X ", rxBuffer[i]);
|
|
|
|
|
printf("\n");
|
|
|
|
|
//if(rxBufferPos != 514)
|
|
|
|
|
// printf("FE after %d\n",rxBufferPos);
|
|
|
|
|
//rxBufferPos = 0;
|
|
|
|
|
//DMA1_Channel1->CNDTR = 1;
|
|
|
|
|
SEGGER_SYSVIEW_PrintfHost("FE");
|
|
|
|
|
Reset_DMA();
|
|
|
|
|
//DMA1_Channel1->CCR &= ~( DMA_CCR_EN );
|
|
|
|
|
//DMA1_Channel1->CNDTR = 512;
|
|
|
|
|
//DMA1_Channel1->CMAR = ( uint32_t )&rxBuffer[0];
|
|
|
|
|
//DMA1_Channel1->CCR |= ( DMA_CCR_EN );
|
|
|
|
|
////DMA1_Channel1->CCR |= ( DMA_CCR_EN );
|
|
|
|
|
}
|
|
|
|
|
if(USART1->ISR & USART_ISR_ORE){
|
|
|
|
|
USART1->ICR = USART_ICR_ORECF;
|
|
|
|
|
printf("ORE\n");
|
|
|
|
|
//printf("ORE\n");
|
|
|
|
|
//SEGGER_SYSVIEW_PrintfHost("ORE");
|
|
|
|
|
}
|
|
|
|
|
if(USART1->ISR & USART_ISR_NE){
|
|
|
|
|
USART1->ICR = USART_ICR_NECF;
|
|
|
|
|
@ -114,7 +172,39 @@ void USART1_IRQHandler(){
|
|
|
|
|
USART1->ICR = USART_ICR_UDRCF;
|
|
|
|
|
printf("UDR\n");
|
|
|
|
|
}
|
|
|
|
|
SEGGER_SYSVIEW_RecordExitISR();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DMA1_Channel1_IRQHandler(){
|
|
|
|
|
SEGGER_SYSVIEW_RecordEnterISR();
|
|
|
|
|
if(DMA1->ISR & DMA_ISR_TCIF1){
|
|
|
|
|
DMA1->IFCR = DMA_IFCR_CTCIF1;
|
|
|
|
|
}
|
|
|
|
|
SEGGER_SYSVIEW_PrintfHost("DMA");
|
|
|
|
|
SEGGER_SYSVIEW_RecordExitISR();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void init_stepper(){
|
|
|
|
|
RCC->IOPENR |= RCC_IOPENR_GPIOBEN;
|
|
|
|
|
RCC->IOPENR |= RCC_IOPENR_GPIOCEN;
|
|
|
|
|
RCC->IOPENR |= RCC_IOPENR_GPIOEEN;
|
|
|
|
|
|
|
|
|
|
// Set to output (00: Input, 01: Output, 10: Alternate function, 11: Analog)
|
|
|
|
|
// PB4 Direction
|
|
|
|
|
GPIOB->MODER &= ~(0x3 << (4 * 2)); // Clear mode bits for PB4
|
|
|
|
|
GPIOB->MODER |= (0x1 << (4 * 2)); // Set mode to output for PB4
|
|
|
|
|
|
|
|
|
|
// PC11 Enable
|
|
|
|
|
GPIOC->MODER &= ~(0x3 << (11 * 2)); // Clear mode bits for PC11
|
|
|
|
|
GPIOC->MODER |= (0x1 << (11 * 2)); // Set mode to output for PC11
|
|
|
|
|
|
|
|
|
|
// PE2 Step
|
|
|
|
|
GPIOE->MODER &= ~(0x3 << (2 * 2)); // Clear mode bits for PE2
|
|
|
|
|
GPIOE->MODER |= (0x1 << (2 * 2)); // Set mode to output for PE2
|
|
|
|
|
|
|
|
|
|
GPIOC->BSRR |= GPIO_BSRR_BS11;
|
|
|
|
|
}
|
|
|
|
|
/* USER CODE END 0 */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief The application entry point.
|
|
|
|
|
@ -123,8 +213,6 @@ void USART1_IRQHandler(){
|
|
|
|
|
int main(void)
|
|
|
|
|
{
|
|
|
|
|
/* USER CODE BEGIN 1 */
|
|
|
|
|
SEGGER_RTT_ConfigUpBuffer(0, NULL, NULL, 0, SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL);
|
|
|
|
|
printf("Moin!\n");
|
|
|
|
|
/* USER CODE END 1 */
|
|
|
|
|
|
|
|
|
|
/* MCU Configuration--------------------------------------------------------*/
|
|
|
|
|
@ -140,20 +228,35 @@ int main(void)
|
|
|
|
|
SystemClock_Config();
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN SysInit */
|
|
|
|
|
SEGGER_RTT_ConfigUpBuffer(0, NULL, NULL, 0, SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL);
|
|
|
|
|
printf("Moin!\n");
|
|
|
|
|
|
|
|
|
|
SEGGER_SYSVIEW_Conf(); /* Configure and initialize SystemView */
|
|
|
|
|
|
|
|
|
|
/* USER CODE END SysInit */
|
|
|
|
|
|
|
|
|
|
/* Initialize all configured peripherals */
|
|
|
|
|
MX_GPIO_Init();
|
|
|
|
|
/* USER CODE BEGIN 2 */
|
|
|
|
|
init_UART1();
|
|
|
|
|
init_UART1_dma();
|
|
|
|
|
init_stepper();
|
|
|
|
|
/* USER CODE END 2 */
|
|
|
|
|
|
|
|
|
|
/* Infinite loop */
|
|
|
|
|
/* USER CODE BEGIN WHILE */
|
|
|
|
|
//HAL_UART_Receive_DMA(&huart1, rxBuffer, BUFFER_SIZE);
|
|
|
|
|
uint16_t cnt = 0;
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
printf("buf: ");
|
|
|
|
|
for(uint16_t i=0; i<10; i++)
|
|
|
|
|
printf("0x%02X ", rxBuffer[i]);
|
|
|
|
|
printf("\n");
|
|
|
|
|
SEGGER_SYSVIEW_OnIdle();
|
|
|
|
|
if(cnt++ >= 260-rxBuffer[1]){
|
|
|
|
|
cnt = 0;
|
|
|
|
|
GPIOE->ODR ^= GPIO_ODR_OD2;
|
|
|
|
|
}
|
|
|
|
|
/* USER CODE END WHILE */
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN 3 */
|
|
|
|
|
@ -182,7 +285,7 @@ void SystemClock_Config(void)
|
|
|
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
|
|
|
|
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
|
|
|
|
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
|
|
|
|
|
RCC_OscInitStruct.PLL.PLLN = 8;
|
|
|
|
|
RCC_OscInitStruct.PLL.PLLN = 16;
|
|
|
|
|
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
|
|
|
|
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
|
|
|
|
|
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
|
|
|
|
|
@ -199,7 +302,7 @@ void SystemClock_Config(void)
|
|
|
|
|
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
|
|
|
|
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
|
|
|
|
|
|
|
|
|
|
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
|
|
|
|
|
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
|