#include "at32f403a_407.h" //#include "at32f403a_407_board.h" //#include "at32f403a_407_clock.h" #include "FreeRTOS.h" #include "task.h" #include "mb.h" #include "port.h" #include "mbport.h" #include #define USART_INVALID_PORT ( 0xFF ) void vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable ) { if (xRxEnable) { gpio_bits_reset(GPIOD, GPIO_PINS_10); usart_interrupt_enable(USART3, USART_RDBF_INT, TRUE); } else { gpio_bits_set(GPIOD, GPIO_PINS_10); usart_interrupt_enable(USART3, USART_RDBF_INT, FALSE); } if (xTxEnable) { gpio_bits_set(GPIOD, GPIO_PINS_10); usart_interrupt_enable(USART3, USART_TDC_INT, TRUE); } else { gpio_bits_reset(GPIOD, GPIO_PINS_10); usart_interrupt_enable(USART3, USART_TDC_INT, FALSE); } } BOOL xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity, unsigned int stop_bit ) { gpio_init_type gpio_init_struct; usart_parity_selection_type parity; usart_data_bit_num_type data_bits; usart_stop_bit_num_type stop_bits; crm_periph_clock_enable(CRM_USART3_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE); gpio_pin_remap_config(USART3_GMUX_0011, TRUE); gpio_default_para_init(&gpio_init_struct); // configure the uart tx pin gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; gpio_init_struct.gpio_mode = GPIO_MODE_MUX; gpio_init_struct.gpio_pins = GPIO_PINS_8; gpio_init_struct.gpio_pull = GPIO_PULL_NONE; gpio_init(GPIOD, &gpio_init_struct); // configure the uart rx pin gpio_init_struct.gpio_mode = GPIO_MODE_INPUT; gpio_init_struct.gpio_pins = GPIO_PINS_9; gpio_init_struct.gpio_pull = GPIO_PULL_UP; gpio_init(GPIOD, &gpio_init_struct); // configure the uart rx pin gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT; gpio_init_struct.gpio_pins = GPIO_PINS_10; gpio_init_struct.gpio_pull = GPIO_PULL_UP; gpio_init(GPIOD, &gpio_init_struct); gpio_bits_reset(GPIOD, GPIO_PINS_10); //gpio_bits_set(GPIOD, GPIO_PINS_10); switch (eParity) { case MB_PAR_NONE : parity = USART_PARITY_NONE; data_bits = USART_DATA_8BITS; break; case MB_PAR_ODD : parity = USART_PARITY_ODD; data_bits = USART_DATA_9BITS; break; case MB_PAR_EVEN : parity = USART_PARITY_EVEN; data_bits = USART_DATA_9BITS; break; default : return FALSE; } switch (stop_bit) { case 1 : stop_bits = USART_STOP_1_BIT; break; case 3 : stop_bits = USART_STOP_2_BIT; break; default : return FALSE; } // configure uart param usart_init(USART3, ulBaudRate, data_bits, stop_bits); usart_parity_selection_config(USART3, parity); usart_flag_clear(USART3, USART_RDBF_FLAG); usart_interrupt_enable(USART3, USART_RDBF_INT, TRUE); usart_receiver_enable(USART3, TRUE); usart_transmitter_enable(USART3, TRUE); usart_enable(USART3, TRUE); nvic_priority_group_config(NVIC_PRIORITY_GROUP_4); nvic_irq_enable(USART3_IRQn, 5, 0); //usart_interrupt_enable(USART3, USART_TDC_INT, TRUE); //usart_interrupt_enable(USART3, USART_RDBF_INT, TRUE); return TRUE; #if 0 GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; __HAL_RCC_USART4_FORCE_RESET(); __HAL_RCC_USART4_RELEASE_RESET(); huart.Init.BaudRate = ulBaudRate; huart.Init.Mode = UART_MODE_TX_RX; huart.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart.Init.OverSampling = UART_OVERSAMPLING_16; huart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart.Init.ClockPrescaler = UART_PRESCALER_DIV1; huart.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; switch (ucPORT) { case 4 : __HAL_RCC_USART4_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; GPIO_InitStruct.Alternate = GPIO_AF4_USART4; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); huart.Instance = USART4; HAL_NVIC_SetPriority(USART3_4_IRQn, 5, 0); break; default : return FALSE; } switch (eParity) { case MB_PAR_NONE : huart.Init.Parity = UART_PARITY_NONE; huart.Init.WordLength = UART_WORDLENGTH_8B; break; case MB_PAR_ODD : huart.Init.Parity = UART_PARITY_ODD; huart.Init.WordLength = UART_WORDLENGTH_9B; break; case MB_PAR_EVEN : huart.Init.Parity = UART_PARITY_EVEN; huart.Init.WordLength = UART_WORDLENGTH_9B; break; default : return FALSE; } switch (stop_bit) { case 1 : huart.Init.StopBits = UART_STOPBITS_1; break; case 3 : huart.Init.StopBits = UART_STOPBITS_2; break; default : return FALSE; } HAL_UART_Init(&huart); HAL_NVIC_EnableIRQ(USART3_4_IRQn); return TRUE; #endif } // void vMBPortSerialClose( void ) { crm_periph_clock_enable(CRM_USART3_PERIPH_CLOCK, FALSE); } // BOOL xMBPortSerialPutByte( CHAR ucByte ) { //while(usart_flag_get(USART3, USART_TDBE_FLAG) == RESET); USART3->dt = ((uint16_t)ucByte & 0x01FF); //while(usart_flag_get(USART3, USART_TDC_FLAG) == RESET); return TRUE; #if 0 USART3->dt = ((uint16_t)ucByte & 0x01FF); return TRUE; #endif } // BOOL xMBPortSerialGetByte( CHAR * pucByte ) { *pucByte = USART3->dt; return TRUE; } // void USART3_IRQHandler(void) { if (usart_flag_get(USART3, USART_RDBF_FLAG) != RESET) { pxMBFrameCBByteReceived(); } if (usart_flag_get(USART3, USART_TDC_FLAG) != RESET) { vMBPortSetWithinException(TRUE); pxMBFrameCBTransmitterEmpty(); vMBPortSetWithinException(FALSE); } } // void vMB_USART_IRQHandler(void) { #if 0 if ((__HAL_UART_GET_IT(&huart, UART_IT_RXNE) != RESET) && (__HAL_UART_GET_IT_SOURCE(&huart, UART_IT_RXNE) != RESET)) { pxMBFrameCBByteReceived(); __HAL_UART_SEND_REQ(&huart, UART_RXDATA_FLUSH_REQUEST); } if ((__HAL_UART_GET_IT(&huart, UART_IT_TXE) != RESET) &&(__HAL_UART_GET_IT_SOURCE(&huart, UART_IT_TXE) != RESET)) { pxMBFrameCBTransmitterEmpty(); HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); } if (__HAL_UART_GET_IT(&huart, UART_IT_ERR) != RESET) __HAL_UART_CLEAR_IT(&huart, UART_CLEAR_OREF); #endif }