embkernel
 All Classes Functions Variables Typedefs Groups Pages
DrvUart.hpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
5 
6 #ifndef DRVUART_HPP_
7 #define DRVUART_HPP_
8 
9 #include "DrvCfg.hpp"
10 #include "DrvTypes.hpp"
11 #include "RtosInclude.hpp"
12 #include "RtosCfg.hpp"
13 #include "LibStreamOut.hpp"
14 #include "LibStreamIn.hpp"
15 #include "LibStringFormat.hpp"
16 
17 class DrvUart: public LibStreamIn, public LibStreamOut {
18  friend class RtosInterrupt;
19 public:
20  class IntCallbacks {
21  friend class DrvUart;
22  protected:
23  virtual void onRxInt(uint8_t value)=0;
24  virtual void onRxParityErrorInt()=0;
25  virtual bool onTxCompletedInt(uint8_t* value)=0;
26  };
27 private:
28  int mIndex;
29  int mBaudrate;
30 
31  static DrvUart* sInstances[];
32 
33  USART_TypeDef* mUart;
34 
35  RtosBuffer mBufferRx;
36  RtosBuffer mBufferTx;
37 
38  IntCallbacks* mIntCallbacks;
39 
40  inline void onInterrupt();
41 public:
42  DrvUart(size_t rxBufferSize, size_t txBufferSize);
43  ~DrvUart();
44  void init(DrvTypes::UART port, int baudrate = 9600, DrvTypes::UART_PARITY parity = DrvTypes::DRV_UART_PARITY_NONE, DrvTypes::UART_STOP_BITS stopBits =
45  DrvTypes::DRV_UART_STOP_BITS_1);
46  void setBaudrate(int baudrate);
47  void setParity(DrvTypes::UART_PARITY parity);
48  void setStopBits(DrvTypes::UART_STOP_BITS stopBits);
49 
50  inline void setIntCallbacks(IntCallbacks* intCallbacks) {
52  mIntCallbacks = intCallbacks;
54  }
55  inline void enableRx() {
56  mUart->CR1 |= USART_CR1_RE;
57  }
58  inline void disableRx() {
59  mUart->CR1 &= ~USART_CR1_RE;
60  }
61  inline void enableTxCompletedInterrupt() {
62  mUart->CR1 |= USART_CR1_TCIE;
63  }
64  inline void disableTxCompletedInterrupt() {
65  mUart->CR1 &= ~USART_CR1_TCIE;
66  }
67  inline int getBaudrate() {
68  return mBaudrate;
69  }
70 
71  virtual int read(void* buffer, int len, Rtos::TICK timeout = Rtos::TICK_INFINITE);
72  virtual int write(const void* buffer, int len, Rtos::TICK timeout = Rtos::TICK_INFINITE);
73 };
74 
75 #endif /* DRVUART_HPP_ */