@@ -97,23 +97,34 @@ void HardwarePWM::DebugOutput(Stream& logger)
97
97
// returns true ONLY when (1) no PWM channel has a pin, and (2) the owner token is nullptr
98
98
bool HardwarePWM::takeOwnership (uintptr_t token)
99
99
{
100
+ bool notInIsr = !isInISR ();
100
101
if (token == 0 ) {
101
- LOG_LV1 (" HwPWM" , " zero / nullptr is not a valid ownership token (attempted use in takeOwnership)" );
102
+ if (notInIsr) {
103
+ LOG_LV1 (" HwPWM" , " zero / nullptr is not a valid ownership token (attempted use in takeOwnership)" );
104
+ }
102
105
return false ; // cannot take ownership with nullptr
103
106
}
104
107
if (token == this ->_owner_token ) {
105
- LOG_LV1 (" HwPWM" , " failing to acquire ownership because already owned by requesting token (cannot take ownership twice)" );
108
+ if (notInIsr) {
109
+ LOG_LV1 (" HwPWM" , " failing to acquire ownership because already owned by requesting token (cannot take ownership twice)" );
110
+ }
106
111
}
107
112
if (this ->_owner_token != 0 ) {
108
- LOG_LV3 (" HwPWM" , " failing to acquire ownership because already owned by other token" );
113
+ if (notInIsr) {
114
+ LOG_LV3 (" HwPWM" , " failing to acquire ownership because already owned by other token" );
115
+ }
109
116
return false ;
110
117
}
111
118
if (this ->usedChannelCount () != 0 ) {
112
- LOG_LV3 (" HwPWM" , " failing to acquire ownership because at least one channel connected" );
119
+ if (notInIsr) {
120
+ LOG_LV3 (" HwPWM" , " failing to acquire ownership because at least one channel connected" );
121
+ }
113
122
return false ;
114
123
}
115
124
if (this ->enabled ()) {
116
- LOG_LV3 (" HwPWM" , " failing to acquire ownership because peripheral is already enabled" );
125
+ if (notInIsr) {
126
+ LOG_LV3 (" HwPWM" , " failing to acquire ownership because peripheral is already enabled" );
127
+ }
117
128
return false ;
118
129
}
119
130
// TODO: warn, but do not fail, if taking ownership with IRQs already enabled
@@ -126,20 +137,29 @@ bool HardwarePWM::takeOwnership(uintptr_t token)
126
137
// returns true ONLY when (1) no PWM channel has a pin attached, and (2) the owner token matches
127
138
bool HardwarePWM::releaseOwnership (uintptr_t token)
128
139
{
140
+ bool notInIsr = !isInISR ();
129
141
if (token == 0 ) {
130
- LOG_LV1 (" HwPWM" , " zero / nullptr is not a valid ownership token (attempted use in releaseOwnership)" );
142
+ if (notInIsr) {
143
+ LOG_LV1 (" HwPWM" , " zero / nullptr is not a valid ownership token (attempted use in releaseOwnership)" );
144
+ }
131
145
return false ;
132
146
}
133
147
if (!this ->isOwner (token)) {
134
- LOG_LV1 (" HwPWM" , " attempt to release ownership when not the current owner" );
148
+ if (notInIsr) {
149
+ LOG_LV1 (" HwPWM" , " attempt to release ownership when not the current owner" );
150
+ }
135
151
return false ;
136
152
}
137
153
if (this ->usedChannelCount () != 0 ) {
138
- LOG_LV1 (" HwPWM" , " attempt to release ownership when at least on channel is still connected" );
154
+ if (notInIsr) {
155
+ LOG_LV1 (" HwPWM" , " attempt to release ownership when at least on channel is still connected" );
156
+ }
139
157
return false ;
140
158
}
141
159
if (this ->enabled ()) {
142
- LOG_LV1 (" HwPWM" , " attempt to release ownership when PWM peripheral is still enabled" );
160
+ if (notInIsr) {
161
+ LOG_LV1 (" HwPWM" , " attempt to release ownership when PWM peripheral is still enabled" );
162
+ }
143
163
return false ; // if it's enabled, do not allow ownership to be released, even with no pins in use
144
164
}
145
165
// TODO: warn, but do not fail, if releasing ownership with IRQs enabled
@@ -153,16 +173,6 @@ bool HardwarePWM::releaseOwnership(uintptr_t token)
153
173
}
154
174
return result;
155
175
}
156
- bool HardwarePWM::releaseOwnershipFromISR (uintptr_t token) {
157
- // Do not do any logging if called from ISR ...
158
- if (token == 0 ) return false ; // cannot release ownership with nullptr
159
- if (!this ->isOwner (token) ) return false ; // don't even look at peripheral
160
- if ( this ->usedChannelCount () != 0 ) return false ; // fail if any channels still have pins
161
- if ( this ->enabled () ) return false ; // if it's enabled, do not allow ownership to be released, even with no pins in use
162
- // use gcc built-in intrinsic to ensure atomicity
163
- // See https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
164
- return __sync_bool_compare_and_swap (&(this ->_owner_token ), token, 0 );
165
- }
166
176
167
177
HardwarePWM::HardwarePWM (NRF_PWM_Type* pwm) :
168
178
_pwm(pwm)
0 commit comments