Merged with dev0.2_uart1
This commit is contained in:
1 parent
87e881aea1
commit
0ea1ec5b97
7 files changed
+131
-35
No files matched your search
@@ -6,8 +6,13 @@
|
||||
#include <uart_lib.h>
|
||||
#include "mik32_hal_irq.h"
|
||||
|
||||
// объект класса HardwareSerial для использования в arduino ide
|
||||
HardwareSerial Serial;
|
||||
|
||||
// HardwareSerial class objects for use in Arduino IDE
|
||||
HardwareSerial Serial(0);
|
||||
#if SERIAL_PORT_QTY > 1
|
||||
HardwareSerial Serial1(1);
|
||||
#endif
|
||||
|
||||
void serialEvent() __attribute__((weak));
|
||||
bool Serial0_available() __attribute__((weak));
|
||||
|
||||
@@ -55,35 +60,46 @@ void HardwareSerial::begin(unsigned long baud, uint8_t config)
|
||||
reg2config |= UART_CONTROL2_STOP_1_M;
|
||||
|
||||
// turn on the receiver and transmitter, apply the parsed config
|
||||
UART_Init(UART_0, brr, reg1config, reg2config, reg3config);
|
||||
// Enable level-based interrupts for the EPIC_UART_0 line, we have only receive interrupt enabled
|
||||
HAL_EPIC_MaskLevelSet(HAL_EPIC_UART_0_MASK);
|
||||
|
||||
isInited = true;
|
||||
if (uartNum == 0)
|
||||
{
|
||||
UART_Init(UART_0, brr, reg1config, reg2config, reg3config);
|
||||
// Enable level-based interrupts for the EPIC_UART_0 line, we have only receive interrupt enabled
|
||||
HAL_EPIC_MaskLevelSet(HAL_EPIC_UART_0_MASK);
|
||||
isInited = true;
|
||||
}
|
||||
else if (uartNum == 1)
|
||||
{
|
||||
UART_Init(UART_1, brr, reg1config, reg2config, reg3config);
|
||||
// Enable level-based interrupts for the EPIC_UART_1 line, we have only receive interrupt enabled
|
||||
HAL_EPIC_MaskLevelSet(HAL_EPIC_UART_1_MASK);
|
||||
isInited = true;
|
||||
}
|
||||
}
|
||||
|
||||
void HardwareSerial::end()
|
||||
{
|
||||
if (isInited)
|
||||
{
|
||||
// wait for the data to be sent if necessary
|
||||
flush();
|
||||
|
||||
// disable clock
|
||||
__HAL_PCC_UART_0_CLK_DISABLE();
|
||||
HAL_EPIC_MaskLevelClear(HAL_EPIC_UART_0_MASK);
|
||||
// deinit uart and disable uart interrupt
|
||||
if (uartNum == 0)
|
||||
{
|
||||
// wait for the data to be sent if necessary
|
||||
UART_WaitDataTranferCompleteFlag(UART_0);
|
||||
UART_Deinit(UART_0);
|
||||
HAL_EPIC_MaskLevelClear(HAL_EPIC_UART_0_MASK);
|
||||
isInited = false;
|
||||
}
|
||||
else if (uartNum == 1)
|
||||
{
|
||||
// wait for the data to be sent if necessary
|
||||
UART_WaitDataTranferCompleteFlag(UART_1);
|
||||
UART_Deinit(UART_1);
|
||||
HAL_EPIC_MaskLevelClear(HAL_EPIC_UART_1_MASK);
|
||||
isInited = false;
|
||||
}
|
||||
|
||||
// reconfigure pins to z state
|
||||
GPIO_InitTypeDef GPIO_InitStruct;
|
||||
memset(&GPIO_InitStruct, 0, sizeof(GPIO_InitStruct));
|
||||
GPIO_InitStruct.Pin = (HAL_PinsTypeDef)(GPIO_PIN_5 | GPIO_PIN_6);
|
||||
GPIO_InitStruct.Mode = HAL_GPIO_MODE_GPIO_INPUT;
|
||||
GPIO_InitStruct.Pull = HAL_GPIO_PULL_NONE;
|
||||
HAL_GPIO_Init(GPIO_0, &GPIO_InitStruct);
|
||||
// reset buffer indices
|
||||
_rx_buffer_head = _rx_buffer_tail = 0;
|
||||
|
||||
isInited = false;
|
||||
_rx_buffer_head = _rx_buffer_tail = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,14 +117,19 @@ int HardwareSerial::availableForWrite(void)
|
||||
|
||||
void HardwareSerial::rx_complete_irq(void)
|
||||
{
|
||||
// which UART to use
|
||||
UART_TypeDef* uart = UART_0;
|
||||
if (uartNum == 1)
|
||||
uart = UART_1;
|
||||
|
||||
// find next index in buffer with upper limit
|
||||
uint8_t i = (uint8_t)(_rx_buffer_head + 1)%SERIAL_RX_BUFFER_SIZE;
|
||||
unsigned char c;
|
||||
// while there is something to receive, put the data into the buffer
|
||||
// and edit the buffer index
|
||||
while (!UART_IsRxFifoEmpty(UART_0))
|
||||
while (!UART_IsRxFifoEmpty(uart))
|
||||
{
|
||||
c = UART_ReadByte(UART_0);
|
||||
c = UART_ReadByte(uart);
|
||||
if (i != _rx_buffer_tail)
|
||||
{
|
||||
// write if there is space in the buffer
|
||||
@@ -119,9 +140,16 @@ void HardwareSerial::rx_complete_irq(void)
|
||||
}
|
||||
|
||||
// wrapper for use in С-files
|
||||
extern "C" void serial_handler_wrapper(void)
|
||||
extern "C" void serial_handler_wrapper(uint8_t uartNumInt)
|
||||
{
|
||||
if (uartNumInt == 0)
|
||||
{
|
||||
Serial.rx_complete_irq();
|
||||
}
|
||||
else if ((uartNumInt == 1) && (SERIAL_PORT_QTY > 1))
|
||||
{
|
||||
Serial1.rx_complete_irq();
|
||||
}
|
||||
}
|
||||
|
||||
int HardwareSerial::peek(void)
|
||||
@@ -151,8 +179,16 @@ size_t HardwareSerial::write(uint8_t c)
|
||||
{
|
||||
if (isInited)
|
||||
{
|
||||
UART_WriteByte(UART_0, c);
|
||||
UART_WaitTransmission(UART_0);
|
||||
if (uartNum == 0)
|
||||
{
|
||||
UART_WriteByte(UART_0, c);
|
||||
UART_WaitTransmission(UART_0);
|
||||
}
|
||||
else if (uartNum == 1)
|
||||
{
|
||||
UART_WriteByte(UART_1, c);
|
||||
UART_WaitTransmission(UART_1);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
@@ -181,6 +217,8 @@ size_t HardwareSerial::write(const uint8_t *buffer, size_t size)
|
||||
void HardwareSerial::flush()
|
||||
{
|
||||
// wait for the data transfer complete
|
||||
while((UART_0->FLAGS & UART_FLAGS_TC_M) == 0)
|
||||
;
|
||||
if (uartNum == 0)
|
||||
UART_WaitDataTranferCompleteFlag(UART_0);
|
||||
else if (uartNum == 1)
|
||||
UART_WaitDataTranferCompleteFlag(UART_1);
|
||||
}
|
||||
Reference in new issue
Block a user