PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

79 lines
2.4KB

  1. // ArduinoCompat/HardwareSerial.h
  2. // STM32 implementation of Arduino compatible serial class
  3. #include <RadioHead.h>
  4. #if (RH_PLATFORM == RH_PLATFORM_STM32STD)
  5. #ifndef _HardwareSerial_h
  6. #define _HardwareSerial_h
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stm32f4xx.h>
  10. #ifndef ARDUINO_RINGBUFFER_SIZE
  11. #define ARDUINO_RINGBUFFER_SIZE 64
  12. #endif
  13. class RingBuffer
  14. {
  15. public:
  16. RingBuffer();
  17. bool isEmpty();
  18. bool isFull();
  19. bool write(uint8_t ch);
  20. uint8_t read();
  21. private:
  22. uint8_t _buffer[ARDUINO_RINGBUFFER_SIZE]; // In fact we can hold up to ARDUINO_RINGBUFFER_SIZE-1 bytes
  23. uint16_t _head; // Index of next write
  24. uint16_t _tail; // Index of next read
  25. uint32_t _overruns; // Write attempted when buffer full
  26. uint32_t _underruns; // Read attempted when buffer empty
  27. };
  28. // Mostly compatible wuith Arduino HardwareSerial
  29. // Theres just enough here to support RadioHead RH_Serial
  30. class HardwareSerial
  31. {
  32. public:
  33. HardwareSerial(USART_TypeDef* usart);
  34. void begin(unsigned long baud);
  35. void end();
  36. virtual int available(void);
  37. virtual int read(void);
  38. virtual size_t write(uint8_t);
  39. inline size_t write(unsigned long n) { return write((uint8_t)n); }
  40. inline size_t write(long n) { return write((uint8_t)n); }
  41. inline size_t write(unsigned int n) { return write((uint8_t)n); }
  42. inline size_t write(int n) { return write((uint8_t)n); }
  43. // These need to be public so the IRQ handler can read and write to them:
  44. RingBuffer _rxRingBuffer;
  45. RingBuffer _txRingBuffer;
  46. private:
  47. USART_TypeDef* _usart;
  48. };
  49. // Predefined serial ports are configured so:
  50. // Serial STM32 UART RX pin Tx Pin Comments
  51. // Serial1 USART1 PA10 PA9 TX Conflicts with GREEN LED on Discovery
  52. // Serial2 USART2 PA3 PA2
  53. // Serial3 USART3 PD9 PD10
  54. // Serial4 UART4 PA1 PA0 TX conflicts with USER button on Discovery
  55. // Serial5 UART5 PD2 PC12 TX conflicts with CS43L22 SDIN on Discovery
  56. // Serial6 USART6 PC7 PC6 RX conflicts with CS43L22 MCLK on Discovery
  57. //
  58. // All ports are idle HIGH, LSB first, 8 bits, No parity, 1 stop bit
  59. extern HardwareSerial Serial1;
  60. extern HardwareSerial Serial2;
  61. extern HardwareSerial Serial3;
  62. extern HardwareSerial Serial4;
  63. extern HardwareSerial Serial5;
  64. extern HardwareSerial Serial6;
  65. #endif
  66. #endif