Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix XBoard stability issues #116

Merged
merged 4 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions fw/Core/Hitcon/Service/XBoardService.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ void XBoardService::Routine(void*) {
HAL_UART_Transmit_IT(_huart, &_tx_buffer[_tx_buffer_head], 1);
_tx_buffer_head = (_tx_buffer_head + 1) % kTxBufferSize;
}

if (_huart->RxState == HAL_UART_STATE_BUSY_RX) {
// We're receiving properly.
_rx_stopped_count = 0;
} else {
_rx_stopped_count++;
if (_rx_stopped_count > 10) {
TriggerRx();
_rx_stopped_count = 0;
}
}
}

void XBoardService::TriggerRx() { HAL_UART_Receive_IT(_huart, &rx_byte_, 1); }
Expand All @@ -91,3 +102,19 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef* huart) {
void HAL_UART_RxCpltCallback(UART_HandleTypeDef* huart) {
g_xboard_service.NotifyRxFinish();
}

void HAL_UART_ErrorCallback(UART_HandleTypeDef* huart) {
uint32_t osrv = g_xboard_service._huart->Instance->SR;
g_xboard_service._huart->Instance->SR = 0x00;
uint32_t srv = g_xboard_service._huart->Instance->SR;
uint32_t drv = g_xboard_service._huart->Instance->DR;
srv = g_xboard_service._huart->Instance->SR;
g_xboard_service.sr_accu |= osrv & (~srv);
g_xboard_service.sr_clear++;
}

void HAL_UART_AbortCpltCallback(UART_HandleTypeDef* huart) { my_assert(false); }

void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef* huart) {
my_assert(false);
}
10 changes: 8 additions & 2 deletions fw/Core/Hitcon/Service/XBoardService.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ class XBoardService {
// 48 * 3 = 144, 3 packets
static constexpr size_t kTxBufferSize = 160;

UART_HandleTypeDef* _huart = &huart2;

uint32_t sr_accu = 0;
uint32_t sr_clear = 0;

private:
void Routine(void*);

void TriggerRx();

void OnRxWrapper(void* arg2);

UART_HandleTypeDef* _huart = &huart2;

callback_t _on_rx_callback;
void* _on_rx_callback_arg1;
bool _tx_busy = false;
Expand All @@ -55,6 +58,9 @@ class XBoardService {
int _tx_buffer_head = 0;
// Next byte from the upper layer.
int _tx_buffer_tail = 0;

// This is a huge hack. rx might get stopped so we want to restart it.
int _rx_stopped_count = 0;
};

extern XBoardService g_xboard_service;
Expand Down
2 changes: 1 addition & 1 deletion fw/Core/Src/usart.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void MX_USART2_UART_Init(void)

/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.BaudRate = 28800;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
Expand Down
3 changes: 2 additions & 1 deletion fw/fw.ioc
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,8 @@ TIM4.IPParameters=Prescaler,Period,Channel-PWM Generation2 No Output,Pulse-PWM G
TIM4.Period=10-1
TIM4.Prescaler=12000-1
TIM4.Pulse-PWM\ Generation2\ No\ Output=5
USART2.IPParameters=VirtualMode
USART2.BaudRate=28800
USART2.IPParameters=VirtualMode,BaudRate
USART2.VirtualMode=VM_ASYNC
USB_DEVICE.CLASS_NAME_FS=CUSTOM_HID
USB_DEVICE.IPParameters=VirtualMode,VirtualModeFS,CLASS_NAME_FS,USBD_CUSTOM_HID_REPORT_DESC_SIZE,USBD_CUSTOMHID_OUTREPORT_BUF_SIZE
Expand Down
Loading