forked from stm32duino/Arduino_Core_STM32
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSubGhz.h
118 lines (95 loc) · 4.11 KB
/
SubGhz.h
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
110
111
112
113
114
115
116
117
118
/*
*******************************************************************************
* Copyright (c) 2022, STMicroelectronics
* All rights reserved.
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
*******************************************************************************
*/
#ifndef __SUBGHZ_H
#define __SUBGHZ_H
#include <functional>
#include <stdint.h>
#include <SPI.h>
#if !defined(SUBGHZSPI_BASE)
#error "This board does not seem to have a SubGhz radio module, cannot use this library."
#endif
extern "C" void SUBGHZ_Radio_IRQHandler();
/**
* Singleton class to manage the SubGHZ ISR and internal signals
*/
class SubGhzClass {
public:
using callback_function_t = std::function<void(void)>;
// Attach the passed handler to be called (from ISR context) when
// the radio IRQ triggers, then clear any pending IRQs and enable
// it.
//
// Note that the radio IRQ is *level* triggered, so the callback
// should either disable directly process the IRQ and clear the IRQ
// flags in the radio (so the radio module clears its DIO lines
// before the ISR returns), or the callback should disable the IRQ
// to prevent it from being triggered over and over again.
void attachInterrupt(callback_function_t callback);
// Detach the interrupt handler, disabling the IRQ too.
void detachInterrupt();
// Return whether an interrupt callback is currently attached
// (regardless of whether it is enabled or not).
bool hasInterrupt();
// Enable the interrupt again if it was previously disabled using
// disableInterrupt(). This does *not* clear any pending interrupts,
// so if the IRQ was triggered while it was disabled, this will
// cause the callback (previously registered with attachInterrupt)
// to fire directly.
void enableInterrupt();
// Temporarily disable interrupt processing by disabling the IRQ in
// the NVIC.
void disableInterrupt();
// Clear the interrupt pending flag (to suppress an IRQs that
// happened while the interrupt was disabled).
void clearPendingInterrupt();
// Returns whether the interrupt is currently pending (only really
// useful when the interrupt is disabled, or while some higher
// priority interrupt is currently running).
bool isInterruptPending();
// Update the NSS signal that is internally connected to the radio
// SPI block.
// This signal is active-low, so passing LOW activates the block,
// HIGH deactivates it.
void setNssActive(bool value);
// Read back the value written to NSS
bool isNssActive();
// Update the RESET signal that is internally connected to the
// radio. Pass true to put the module in reset, or false to enable
// the module.
void setResetActive(bool value);
// Read back the value written to RESET
bool isResetActive();
// Return the state of busy signal exported by the radio module.
// Note that this uses the RFBUSYS flag that returns the radio busy
// signal directly, not the RFBUSYMS flag that also returns an extra
// busy period after every SPI transaction.
bool isBusy();
// The SPI object that can be used to talk to the radio. Works
// like regular SPI objects, except that writeNSS() above must be
// used to control the (internal) NSS "pin".
SUBGHZSPIClass SPI;
// SPI settings to use for the radio. This uses the maximum speed
// supported by the radio, which should always work (no chance of
// bad wiring that requires reducing the speed).
// This value should be passed to `SubGhz.SPI.beginTransaction()`.
const SPISettings spi_settings = {16000000, MSBFIRST, SPI_MODE0};
protected:
// To access handleIrq()
friend void SUBGHZ_Radio_IRQHandler();
void handleIrq();
private:
callback_function_t callback;
};
// Singleton instance, no other instances should be created
extern SubGhzClass SubGhz;
#endif // __SUBGHZ_H