8 DrvUart* DrvUart::sInstances[3];
10 DrvUart::DrvUart(
size_t rxBufferSize,
size_t txBufferSize) :
11 mBufferRx(rxBufferSize), mBufferTx(txBufferSize) {
19 void DrvUart::init(DrvTypes::UART port,
int baudrate, DrvTypes::UART_PARITY parity, DrvTypes::UART_STOP_BITS stopBits) {
23 case DrvTypes::DRV_UART1:
25 irqType = USART1_IRQn;
27 case DrvTypes::DRV_UART2:
29 irqType = USART2_IRQn;
31 case DrvTypes::DRV_UART3:
33 irqType = USART3_IRQn;
39 sInstances[port - 1] =
this;
41 mUart->CR1 = USART_CR1_TE | USART_CR1_RXNEIE;
44 setBaudrate(baudrate);
46 setStopBits(stopBits);
48 NVIC ->IP[irqType] = RTOS_INT_PRI;
49 NVIC_EnableIRQ(irqType);
50 mUart->CR1 |= USART_CR1_UE;
53 void DrvUart::setBaudrate(
int baudrate) {
54 uint16_t enableMask = mUart->CR1 & USART_CR1_UE;
55 mUart->CR1 &= ~enableMask;
56 if (mUart == USART1 ) {
57 mUart->BRR = DRV_CFG_PCLK2_FREQ / baudrate;
60 mUart->BRR = DRV_CFG_PCLK1_FREQ / baudrate;
63 mUart->CR1 |= enableMask;
66 void DrvUart::setParity(DrvTypes::UART_PARITY parity) {
67 uint16_t enableMask = mUart->CR1 & USART_CR1_UE;
68 mUart->CR1 &= ~enableMask;
70 case DrvTypes::DRV_UART_PARITY_NONE:
71 mUart->CR1 &= ~(USART_CR1_PCE | USART_CR1_M );
73 case DrvTypes::DRV_UART_PARITY_EVEN:
74 mUart->CR1 |= USART_CR1_PCE | USART_CR1_M;
75 mUart->CR1 &= ~USART_CR1_PS;
77 case DrvTypes::DRV_UART_PARITY_ODD:
78 mUart->CR1 |= USART_CR1_PCE | USART_CR1_M;
79 mUart->CR1 |= USART_CR1_PS;
82 mUart->CR1 |= enableMask;
85 void DrvUart::setStopBits(DrvTypes::UART_STOP_BITS stopBits) {
86 uint16_t enableMask = mUart->CR1 & USART_CR1_UE;
87 mUart->CR1 &= ~enableMask;
88 mUart->CR2 &= ~USART_CR2_STOP;
90 case DrvTypes::DRV_UART_STOP_BITS_1:
92 case DrvTypes::DRV_UART_STOP_BITS_1_5:
93 mUart->CR2 |= USART_CR2_STOP_1 | USART_CR2_STOP_0;
95 case DrvTypes::DRV_UART_STOP_BITS_2:
96 mUart->CR2 |= USART_CR2_STOP_1;
99 mUart->CR1 |= enableMask;
102 int DrvUart::read(
void* buffer,
int len,
Rtos::TICK timeout) {
104 uint8_t* p = (uint8_t*) buffer;
106 int count = mBufferRx.read(p, left, timeout);
118 int DrvUart::write(
const void* buffer,
int len,
Rtos::TICK timeout) {
119 len = mBufferTx.write((
const uint8_t*) buffer, len, timeout);
120 enableTxCompletedInterrupt();
124 void DrvUart::onInterrupt() {
126 while (mUart->ISR & USART_ISR_RXNE) {
127 if (mUart->ISR & USART_ISR_PE) {
128 mIntCallbacks->onRxParityErrorInt(mUart->RDR & 0xFF);
131 mIntCallbacks->onRxInt(mUart->RDR & 0xFF);
134 if (((mUart->ISR & USART_ISR_TC) && (mUart->CR1 & USART_CR1_TCIE ))) {
136 if (mIntCallbacks->onTxCompletedInt(&byte)) {
140 disableTxCompletedInterrupt();
148 while (((mUart->ISR & USART_ISR_RXNE) && (i < (
int)
sizeof(buffer)))) {
149 buffer[i++] = mUart->RDR & 0xFF;
151 mBufferRx.writeFromInt(buffer, i);
153 if (((mUart->ISR & USART_ISR_TC) && (mUart->CR1 & USART_CR1_TCIE ))) {
155 int len = mBufferTx.readFromInt(&buffer, 1);
159 if (mBufferTx.getCount() == 0) {
160 disableTxCompletedInterrupt();
164 if (mUart->ISR & USART_ISR_PE) {
165 mUart->ICR = USART_ICR_PECF;
167 if (mUart->ISR & USART_ISR_ORE) {
168 mUart->ICR = USART_ICR_ORECF;
172 extern "C" void RtosInterrupt::IRQ_USART1() {
173 DrvUart::sInstances[0]->onInterrupt();
176 extern "C" void RtosInterrupt::IRQ_USART2() {
177 DrvUart::sInstances[1]->onInterrupt();
180 extern "C" void RtosInterrupt::IRQ_USART3() {
181 DrvUart::sInstances[2]->onInterrupt();