WiP: refactor serial ports
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
|
||||
#include "opentx.h"
|
||||
#include "intmodule_serial_driver.h"
|
||||
#include "stm32_usart_driver.h"
|
||||
|
||||
ModuleFifo intmoduleFifo;
|
||||
#if !defined(INTMODULE_DMA_STREAM)
|
||||
@@ -36,32 +37,30 @@ struct etx_serial_driver {
|
||||
static etx_serial_driver intmodule_driver = { nullptr, nullptr };
|
||||
|
||||
// TODO: move this somewhere else
|
||||
static void intmoduleFifoReceive(uint8_t data)
|
||||
void intmoduleFifoReceive(uint8_t data)
|
||||
{
|
||||
intmoduleFifo.push(data);
|
||||
}
|
||||
|
||||
static void intmoduleFifoError()
|
||||
void intmoduleFifoError()
|
||||
{
|
||||
intmoduleFifo.errors++;
|
||||
}
|
||||
|
||||
etx_serial_init::etx_serial_init():
|
||||
baudrate(0),
|
||||
parity(USART_Parity_No),
|
||||
stop_bits(USART_StopBits_1),
|
||||
word_length(USART_WordLength_8b),
|
||||
rx_enable(false),
|
||||
// TODO: this should not be needed
|
||||
on_receive(intmoduleFifoReceive),
|
||||
on_error(intmoduleFifoError)
|
||||
{
|
||||
}
|
||||
// etx_serial_init::etx_serial_init():
|
||||
// baudrate(0),
|
||||
// parity(USART_Parity_No),
|
||||
// stop_bits(USART_StopBits_1),
|
||||
// word_length(USART_WordLength_8b),
|
||||
// rx_enable(false),
|
||||
// // TODO: this should not be needed
|
||||
// on_receive(intmoduleFifoReceive),
|
||||
// on_error(intmoduleFifoError)
|
||||
// {
|
||||
// }
|
||||
|
||||
void intmoduleStop()
|
||||
{
|
||||
GPIO_ResetBits(INTMODULE_PWR_GPIO, INTMODULE_PWR_GPIO_PIN);
|
||||
|
||||
#if defined(INTMODULE_DMA_STREAM)
|
||||
INTMODULE_DMA_STREAM->CR &= ~DMA_SxCR_EN; // Disable DMA
|
||||
#endif
|
||||
@@ -83,17 +82,29 @@ void intmoduleStop()
|
||||
intmodule_driver.on_error = nullptr;
|
||||
}
|
||||
|
||||
static const etx_serial_init pxx1SerialInit = {
|
||||
.baudrate = INTMODULE_PXX1_SERIAL_BAUDRATE,
|
||||
.parity = ETX_Parity_None,
|
||||
.stop_bits = ETX_StopBits_One,
|
||||
.word_length = ETX_WordLength_8,
|
||||
.rx_enable = false,
|
||||
.on_receive = intmoduleFifoReceive,
|
||||
.on_error = intmoduleFifoError,
|
||||
};
|
||||
|
||||
void intmodulePxx1SerialStart()
|
||||
{
|
||||
etx_serial_init params;
|
||||
params.baudrate = INTMODULE_PXX1_SERIAL_BAUDRATE;
|
||||
intmoduleSerialStart(¶ms);
|
||||
IntmoduleSerialDriver.init(&pxx1SerialInit);
|
||||
}
|
||||
|
||||
void intmoduleSerialStart(const etx_serial_init* params)
|
||||
{
|
||||
if (!params) return;
|
||||
|
||||
|
||||
// TODO: sanity check parameters
|
||||
// - the UART seems to block when initialised with baudrate = 0
|
||||
|
||||
// TODO: remove this!!!
|
||||
INTERNAL_MODULE_ON();
|
||||
|
||||
// init callbacks
|
||||
@@ -130,16 +141,7 @@ void intmoduleSerialStart(const etx_serial_init* params)
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(INTMODULE_GPIO, &GPIO_InitStructure);
|
||||
|
||||
USART_DeInit(INTMODULE_USART);
|
||||
USART_InitTypeDef USART_InitStructure;
|
||||
USART_InitStructure.USART_BaudRate = baudrate;
|
||||
USART_InitStructure.USART_Parity = params->parity;
|
||||
USART_InitStructure.USART_StopBits = params->stop_bits;
|
||||
USART_InitStructure.USART_WordLength = params->word_length;
|
||||
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
||||
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
|
||||
USART_Init(INTMODULE_USART, &USART_InitStructure);
|
||||
USART_Cmd(INTMODULE_USART, ENABLE);
|
||||
stm32_usart_init(INTMODULE_USART, params);
|
||||
|
||||
if (params->rx_enable) {
|
||||
USART_ITConfig(INTMODULE_USART, USART_IT_RXNE, ENABLE);
|
||||
@@ -224,6 +226,8 @@ void intmoduleWaitForTxCompleted()
|
||||
{
|
||||
#if !defined(SIMU)
|
||||
#if defined(INTMODULE_DMA_STREAM)
|
||||
// TODO: check if everything is properly initialised, this seems to block when
|
||||
// the port has been initialised with a zero baudrate
|
||||
if (DMA_GetCmdStatus(INTMODULE_DMA_STREAM) == ENABLE) {
|
||||
while(DMA_GetFlagStatus(INTMODULE_DMA_STREAM, INTMODULE_DMA_FLAG_TC) == RESET);
|
||||
DMA_ClearFlag(INTMODULE_DMA_STREAM, INTMODULE_DMA_FLAG_TC);
|
||||
@@ -233,3 +237,11 @@ void intmoduleWaitForTxCompleted()
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
etx_hal_serial_driver_t IntmoduleSerialDriver = {
|
||||
.init = intmoduleSerialStart,
|
||||
.deinit = intmoduleStop,
|
||||
.sendByte = intmoduleSendByte,
|
||||
.sendBuffer = intmoduleSendBuffer,
|
||||
.waitForTxCompleted = intmoduleWaitForTxCompleted,
|
||||
};
|
||||
|
||||
@@ -22,22 +22,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "hal/serial_driver.h"
|
||||
|
||||
struct etx_serial_init {
|
||||
uint32_t baudrate;
|
||||
uint16_t parity;
|
||||
uint16_t stop_bits;
|
||||
uint16_t word_length;
|
||||
bool rx_enable;
|
||||
void (*on_receive)(uint8_t data);
|
||||
void (*on_error)();
|
||||
// Callbacks using intmoduleFifo
|
||||
void intmoduleFifoReceive(uint8_t data);
|
||||
void intmoduleFifoError();
|
||||
|
||||
etx_serial_init();
|
||||
};
|
||||
|
||||
void intmoduleSerialStart(const etx_serial_init* params);
|
||||
void intmoduleSerialStop();
|
||||
|
||||
void intmoduleSendByte(uint8_t byte);
|
||||
void intmoduleSendBuffer(const uint8_t* data, uint8_t size);
|
||||
void intmoduleWaitForTxCompleted();
|
||||
extern etx_hal_serial_driver_t IntmoduleSerialDriver;
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "stm32_usart_driver.h"
|
||||
|
||||
void stm32_usart_init(USART_TypeDef *USARTx, const etx_serial_init* params)
|
||||
{
|
||||
USART_DeInit(USARTx);
|
||||
|
||||
USART_InitTypeDef USART_InitStructure;
|
||||
|
||||
USART_InitStructure.USART_BaudRate = params->baudrate;
|
||||
|
||||
uint16_t parity = USART_Parity_No;
|
||||
switch(params->parity){
|
||||
case ETX_Parity_None:
|
||||
parity = USART_Parity_No;
|
||||
break;
|
||||
case ETX_Parity_Even:
|
||||
parity = USART_Parity_Even;
|
||||
break;
|
||||
case ETX_Parity_Odd:
|
||||
parity = USART_Parity_Odd;
|
||||
break;
|
||||
}
|
||||
USART_InitStructure.USART_Parity = parity;
|
||||
|
||||
uint16_t stop_bits = USART_StopBits_1;
|
||||
switch(params->stop_bits) {
|
||||
case ETX_StopBits_One:
|
||||
stop_bits = USART_StopBits_1;
|
||||
break;
|
||||
case ETX_StopBits_OneAndHalf:
|
||||
stop_bits = USART_StopBits_1_5;
|
||||
break;
|
||||
case ETX_StopBits_Two:
|
||||
stop_bits = USART_StopBits_2;
|
||||
break;
|
||||
}
|
||||
USART_InitStructure.USART_StopBits = stop_bits;
|
||||
|
||||
if (params->word_length == ETX_WordLength_9) {
|
||||
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
|
||||
} else {
|
||||
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
||||
}
|
||||
|
||||
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
||||
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
|
||||
USART_Init(USARTx, &USART_InitStructure);
|
||||
USART_Cmd(USARTx, ENABLE);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "hal/serial_driver.h"
|
||||
|
||||
#if defined(STM32F4)
|
||||
// #include "stm32f4xx.h"
|
||||
#include "stm32f4xx_usart.h"
|
||||
#else
|
||||
// #include "stm32f2xx.h"
|
||||
#include "stm32f2xx_usart.h"
|
||||
#endif
|
||||
|
||||
void stm32_usart_init(USART_TypeDef *USARTx, const etx_serial_init* params);
|
||||
@@ -37,6 +37,10 @@ enum usbMode {
|
||||
#endif
|
||||
};
|
||||
|
||||
// Control line state bits
|
||||
#define CTRL_LINE_STATE_DTR (1 << 0)
|
||||
#define CTRL_LINE_STATE_RTS (1 << 1)
|
||||
|
||||
int usbPlugged();
|
||||
void usbInit();
|
||||
void usbStart();
|
||||
|
||||
Reference in New Issue
Block a user