|
@@ -1,6 +1,5 @@
|
|
|
#include "at32f403a_407.h"
|
|
|
-//#include "at32f403a_407_board.h"
|
|
|
-//#include "at32f403a_407_clock.h"
|
|
|
+#include "at32f403a_407_board.h"
|
|
|
#include "FreeRTOS.h"
|
|
|
#include "task.h"
|
|
|
#include "mb.h"
|
|
@@ -8,27 +7,35 @@
|
|
|
#include "mbport.h"
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
+
|
|
|
+#define MB_UART USART3
|
|
|
#define USART_INVALID_PORT ( 0xFF )
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
void vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
|
|
|
{
|
|
|
+ char foo = 0;
|
|
|
+
|
|
|
if (xRxEnable) {
|
|
|
gpio_bits_reset(GPIOD, GPIO_PINS_10);
|
|
|
- usart_interrupt_enable(USART3, USART_RDBF_INT, TRUE);
|
|
|
+ usart_interrupt_enable(MB_UART, USART_RDBF_INT, TRUE);
|
|
|
}
|
|
|
else {
|
|
|
- gpio_bits_set(GPIOD, GPIO_PINS_10);
|
|
|
- usart_interrupt_enable(USART3, USART_RDBF_INT, FALSE);
|
|
|
+ //gpio_bits_set(GPIOD, GPIO_PINS_10);
|
|
|
+ //foo = usart_data_receive(MB_UART);
|
|
|
+ usart_interrupt_enable(MB_UART, USART_RDBF_INT, FALSE);
|
|
|
}
|
|
|
if (xTxEnable) {
|
|
|
+ //foo = usart_data_receive(MB_UART);
|
|
|
gpio_bits_set(GPIOD, GPIO_PINS_10);
|
|
|
- usart_interrupt_enable(USART3, USART_TDC_INT, TRUE);
|
|
|
+ vTaskDelay(100);
|
|
|
+ usart_interrupt_enable(MB_UART, USART_TDBE_INT, TRUE);
|
|
|
}
|
|
|
else {
|
|
|
- gpio_bits_reset(GPIOD, GPIO_PINS_10);
|
|
|
- usart_interrupt_enable(USART3, USART_TDC_INT, FALSE);
|
|
|
+ //gpio_bits_reset(GPIOD, GPIO_PINS_10);
|
|
|
+ usart_interrupt_enable(MB_UART, USART_TDBE_INT, FALSE);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -36,6 +43,7 @@ void vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
|
|
|
BOOL xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
|
|
|
eMBParity eParity, unsigned int stop_bit )
|
|
|
{
|
|
|
+#if 1
|
|
|
gpio_init_type gpio_init_struct;
|
|
|
usart_parity_selection_type parity;
|
|
|
usart_data_bit_num_type data_bits;
|
|
@@ -48,21 +56,15 @@ BOOL xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
|
|
|
|
|
|
gpio_default_para_init(&gpio_init_struct);
|
|
|
|
|
|
- // configure the uart tx pin
|
|
|
+ // configure 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_pins = GPIO_PINS_8 | GPIO_PINS_9;
|
|
|
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
|
|
|
+ // configure DE 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;
|
|
@@ -107,77 +109,54 @@ BOOL xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
-
|
|
|
+ usart_parity_selection_config(MB_UART, parity);
|
|
|
+ usart_init(MB_UART, ulBaudRate, data_bits, stop_bits);
|
|
|
+ usart_transmitter_enable(MB_UART, TRUE);
|
|
|
+ usart_receiver_enable(MB_UART, TRUE);
|
|
|
+ usart_enable(MB_UART, TRUE);
|
|
|
nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
|
|
|
+ NVIC_ClearPendingIRQ(USART3_IRQn);
|
|
|
nvic_irq_enable(USART3_IRQn, 5, 0);
|
|
|
+ //nvic_irq_enable(USART3_IRQn, 0, 5);
|
|
|
|
|
|
//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;
|
|
|
+#endif
|
|
|
+#if 0
|
|
|
+ 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;
|
|
|
|
|
|
- __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;
|
|
|
+ crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE);
|
|
|
+ crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
|
|
|
|
|
|
- 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;
|
|
|
- }
|
|
|
+ gpio_default_para_init(&gpio_init_struct);
|
|
|
|
|
|
- switch (eParity)
|
|
|
+ // configure 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_10 | GPIO_PINS_9;
|
|
|
+ gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
|
|
+ gpio_init(GPIOA, &gpio_init_struct);
|
|
|
+
|
|
|
+ switch (eParity)
|
|
|
{
|
|
|
case MB_PAR_NONE :
|
|
|
- huart.Init.Parity = UART_PARITY_NONE;
|
|
|
- huart.Init.WordLength = UART_WORDLENGTH_8B;
|
|
|
+ parity = USART_PARITY_NONE;
|
|
|
+ data_bits = USART_DATA_8BITS;
|
|
|
break;
|
|
|
case MB_PAR_ODD :
|
|
|
- huart.Init.Parity = UART_PARITY_ODD;
|
|
|
- huart.Init.WordLength = UART_WORDLENGTH_9B;
|
|
|
+ parity = USART_PARITY_ODD;
|
|
|
+ data_bits = USART_DATA_9BITS;
|
|
|
break;
|
|
|
case MB_PAR_EVEN :
|
|
|
- huart.Init.Parity = UART_PARITY_EVEN;
|
|
|
- huart.Init.WordLength = UART_WORDLENGTH_9B;
|
|
|
+ parity = USART_PARITY_EVEN;
|
|
|
+ data_bits = USART_DATA_9BITS;
|
|
|
break;
|
|
|
-
|
|
|
default :
|
|
|
return FALSE;
|
|
|
}
|
|
@@ -185,21 +164,28 @@ BOOL xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
|
|
|
switch (stop_bit)
|
|
|
{
|
|
|
case 1 :
|
|
|
- huart.Init.StopBits = UART_STOPBITS_1;
|
|
|
+ stop_bits = USART_STOP_1_BIT;
|
|
|
break;
|
|
|
|
|
|
case 3 :
|
|
|
- huart.Init.StopBits = UART_STOPBITS_2;
|
|
|
+ stop_bits = USART_STOP_2_BIT;
|
|
|
break;
|
|
|
|
|
|
default :
|
|
|
return FALSE;
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- HAL_UART_Init(&huart);
|
|
|
- HAL_NVIC_EnableIRQ(USART3_4_IRQn);
|
|
|
-
|
|
|
+
|
|
|
+ // configure uart param
|
|
|
+ usart_parity_selection_config(MB_UART, parity);
|
|
|
+ usart_init(MB_UART, ulBaudRate, data_bits, stop_bits);
|
|
|
+ usart_transmitter_enable(MB_UART, TRUE);
|
|
|
+ usart_receiver_enable(MB_UART, TRUE);
|
|
|
+ usart_enable(MB_UART, TRUE);
|
|
|
+ nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
|
|
|
+ NVIC_ClearPendingIRQ(USART1_IRQn);
|
|
|
+ nvic_irq_enable(USART1_IRQn, 5, 0);
|
|
|
+
|
|
|
return TRUE;
|
|
|
#endif
|
|
|
}
|
|
@@ -213,38 +199,42 @@ void vMBPortSerialClose( void )
|
|
|
//
|
|
|
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);
|
|
|
+ //usart_data_transmit(PRINT_UART, ucByte);
|
|
|
+ usart_data_transmit(MB_UART, ucByte);
|
|
|
return TRUE;
|
|
|
-#endif
|
|
|
}
|
|
|
|
|
|
//
|
|
|
BOOL xMBPortSerialGetByte( CHAR * pucByte )
|
|
|
{
|
|
|
- *pucByte = USART3->dt;
|
|
|
+ *pucByte = usart_data_receive(MB_UART);
|
|
|
return TRUE;
|
|
|
}
|
|
|
|
|
|
-//
|
|
|
+
|
|
|
+#if 1
|
|
|
void USART3_IRQHandler(void)
|
|
|
-{
|
|
|
- if (usart_flag_get(USART3, USART_RDBF_FLAG) != RESET) {
|
|
|
- pxMBFrameCBByteReceived();
|
|
|
+//void USART1_IRQHandler(void)
|
|
|
+{
|
|
|
+ if (MB_UART->ctrl1_bit.rdbfien == SET)
|
|
|
+ {
|
|
|
+ if (usart_flag_get(MB_UART, USART_RDBF_FLAG) == SET)
|
|
|
+ {
|
|
|
+ pxMBFrameCBByteReceived();
|
|
|
+ }
|
|
|
}
|
|
|
- if (usart_flag_get(USART3, USART_TDC_FLAG) != RESET) {
|
|
|
- vMBPortSetWithinException(TRUE);
|
|
|
- pxMBFrameCBTransmitterEmpty();
|
|
|
- vMBPortSetWithinException(FALSE);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
+
|
|
|
+ if (MB_UART->ctrl1_bit.tdbeien == SET)
|
|
|
+ {
|
|
|
+ if (usart_flag_get(MB_UART, USART_TDBE_FLAG) == SET)
|
|
|
+ {
|
|
|
+ vMBPortSetWithinException(TRUE);
|
|
|
+ pxMBFrameCBTransmitterEmpty();
|
|
|
+ vMBPortSetWithinException(FALSE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+#endif
|
|
|
|
|
|
//
|
|
|
void vMB_USART_IRQHandler(void)
|