~aleteoryx/muditaos

ref: fa24dcc8293533a27df76838cb8077ab09ea14da muditaos/module-bsp/bsp/bluetooth/Bluetooth.hpp -rw-r--r-- 3.5 KiB
fa24dcc8 — SP2FET [EGD-5483] Updated BT harness tests 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#pragma once

#include <cstdint>      // uint32
#include <sys/types.h>  // ssize_t
#include <cstdarg>
#include <FreeRTOS.h>
#include <thread.hpp>
#include <board.h>

#if defined(TARGET_RT1051)
#include "board/rt1051/common/fsl_drivers/fsl_lpuart_edma.h"
#include "drivers/dmamux/DriverDMAMux.hpp"
#include "drivers/dma/DriverDMA.hpp"
#endif

/// c++ low level driver overlay

namespace bsp {

    class BTdev {
        public:
            enum Error {
                Success,
                ErrorUndefined,
                ErrorTimeout,
                ErrorBSP,
            };
            enum LogLvl {
                LogNone,
                LogError,
                LogWarning,
                LogDebug,
            };
            typedef int(*LogFoo)(const char*,va_list args);
        private:
            LogFoo flog;
        public:
            LogLvl loglvl;
            bool is_open;
            static const unsigned int default_timeout_ms = 1000;
            static const unsigned int default_buff_size = 1024;
            static const unsigned int default_baudrate = 115200;

            BTdev();
            virtual ~BTdev();

            // general
            virtual void open() = 0;    // enable device -> irq enable
            virtual void close() = 0;   // disable device -> irq disable
            virtual BTdev::Error read(uint8_t *buf, size_t nbytes) = 0;
            void log(LogLvl lvl,const char* val, ...);
            // uart specific
    };

    class BluetoothCommon : public BTdev
    {
        public:
            static const ssize_t baudrate =115200;
            static const ssize_t off_threshold =16;
            static const ssize_t on_threshold =32;

            BluetoothCommon();
            virtual ~BluetoothCommon();
            // uart specific Common part
            virtual void open() override;
            virtual void close() override;
            virtual BTdev::Error read(uint8_t *buf, size_t nbytes) override;
            virtual BTdev::Error write(const uint8_t *buf, size_t nbytes);
            virtual ssize_t write_blocking(const uint8_t *buf, ssize_t nbytes);
            Error set_baudrate(uint32_t bd);
            Error set_reset(bool on);
            void sleep_ms(ssize_t ms);
            void set_irq(bool enable);

        private:
            void configure_uart_io();
            void configure_lpuart();
            void configure_cts_irq();
#if defined(TARGET_RT1051)
            std::shared_ptr<drivers::DriverDMAMux> dmamux;
            std::shared_ptr<drivers::DriverDMA> dma;
            std::unique_ptr<drivers::DriverDMAHandle> uartRxDmaHandle;
            std::unique_ptr<drivers::DriverDMAHandle> uartTxDmaHandle;
            static AT_NONCACHEABLE_SECTION_INIT(lpuart_edma_handle_t uartDmaHandle);
            static void uartDmaCallback(LPUART_Type *base, lpuart_edma_handle_t *handle, status_t status, void *userData);
#endif
    };

    /// definitions needed by BT stack

    class BlueKitchen : public BluetoothCommon {
        public:
            BlueKitchen();
            virtual ~BlueKitchen();
            static BlueKitchen *getInstance();

            virtual BTdev::Error read(uint8_t *buf, size_t nbytes) override;
            virtual BTdev::Error write(const uint8_t *buf, size_t size) override;
            uint32_t read_len = 0;
            uint8_t* read_buff;

            void set_flowcontrol(int on);

            void (*read_ready_cb)(void);
            void (*write_done_cb)(void);
            /// to be able to trigger events on thread
            xQueueHandle qHandle = nullptr;
    };
};