Shane Lobo
/
nRF52_Pulse_Threads
Pulse consistency test using wait_us in a thread
main.cpp@0:18409537564c, 2019-03-30 (annotated)
- Committer:
- punkisnail
- Date:
- Sat Mar 30 06:37:35 2019 +0000
- Revision:
- 0:18409537564c
- Child:
- 1:4dec185d33e9
commit1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
punkisnail | 0:18409537564c | 1 | /* mbed Microcontroller Library |
punkisnail | 0:18409537564c | 2 | * Copyright (c) 2018 ARM Limited |
punkisnail | 0:18409537564c | 3 | * SPDX-License-Identifier: Apache-2.0 |
punkisnail | 0:18409537564c | 4 | */ |
punkisnail | 0:18409537564c | 5 | |
punkisnail | 0:18409537564c | 6 | #include <mbed.h> |
punkisnail | 0:18409537564c | 7 | #include "SoftPWM.h" |
punkisnail | 0:18409537564c | 8 | |
punkisnail | 0:18409537564c | 9 | #define DUTY_CYCLE 0.55f |
punkisnail | 0:18409537564c | 10 | |
punkisnail | 0:18409537564c | 11 | #define MAX_INTENSITY 70 |
punkisnail | 0:18409537564c | 12 | |
punkisnail | 0:18409537564c | 13 | SoftPWM LEDR(p2); |
punkisnail | 0:18409537564c | 14 | SoftPWM LEDG(p4); |
punkisnail | 0:18409537564c | 15 | SoftPWM LEDB(p3); |
punkisnail | 0:18409537564c | 16 | |
punkisnail | 0:18409537564c | 17 | DigitalOut VEN_3V3B(p25); |
punkisnail | 0:18409537564c | 18 | |
punkisnail | 0:18409537564c | 19 | DigitalOut BOOST_EN(p14); |
punkisnail | 0:18409537564c | 20 | PwmOut BOOST_PWM(p16); |
punkisnail | 0:18409537564c | 21 | |
punkisnail | 0:18409537564c | 22 | DigitalOut HB_CH1(p6); |
punkisnail | 0:18409537564c | 23 | DigitalOut HB_CH2(p7); |
punkisnail | 0:18409537564c | 24 | |
punkisnail | 0:18409537564c | 25 | InterruptIn SW_ONOFF(p15); |
punkisnail | 0:18409537564c | 26 | InterruptIn SW_MODE(p17); |
punkisnail | 0:18409537564c | 27 | InterruptIn SW_PLUS(p13); |
punkisnail | 0:18409537564c | 28 | InterruptIn SW_MINUS(p19); |
punkisnail | 0:18409537564c | 29 | |
punkisnail | 0:18409537564c | 30 | Ticker stimTicker; |
punkisnail | 0:18409537564c | 31 | |
punkisnail | 0:18409537564c | 32 | volatile bool flip_power = false; |
punkisnail | 0:18409537564c | 33 | volatile bool low_power = false; |
punkisnail | 0:18409537564c | 34 | |
punkisnail | 0:18409537564c | 35 | int intensity = 0; |
punkisnail | 0:18409537564c | 36 | |
punkisnail | 0:18409537564c | 37 | volatile bool stim_mode = false; |
punkisnail | 0:18409537564c | 38 | volatile bool stim_active = false; |
punkisnail | 0:18409537564c | 39 | volatile bool stim_progress = true; |
punkisnail | 0:18409537564c | 40 | short stim_count = 0; |
punkisnail | 0:18409537564c | 41 | |
punkisnail | 0:18409537564c | 42 | void flip_ONOFF() { |
punkisnail | 0:18409537564c | 43 | flip_power = true; |
punkisnail | 0:18409537564c | 44 | } |
punkisnail | 0:18409537564c | 45 | |
punkisnail | 0:18409537564c | 46 | void flip_MODE() { |
punkisnail | 0:18409537564c | 47 | if (!low_power && !stim_mode) { |
punkisnail | 0:18409537564c | 48 | stim_mode = true; |
punkisnail | 0:18409537564c | 49 | } |
punkisnail | 0:18409537564c | 50 | } |
punkisnail | 0:18409537564c | 51 | |
punkisnail | 0:18409537564c | 52 | void flip_PLUS() { |
punkisnail | 0:18409537564c | 53 | if (!low_power && !stim_mode) { |
punkisnail | 0:18409537564c | 54 | if (intensity < MAX_INTENSITY) intensity++; |
punkisnail | 0:18409537564c | 55 | } |
punkisnail | 0:18409537564c | 56 | } |
punkisnail | 0:18409537564c | 57 | |
punkisnail | 0:18409537564c | 58 | void flip_MINUS() { |
punkisnail | 0:18409537564c | 59 | if (!low_power && !stim_mode) { |
punkisnail | 0:18409537564c | 60 | if (intensity > 0) intensity--; |
punkisnail | 0:18409537564c | 61 | } |
punkisnail | 0:18409537564c | 62 | } |
punkisnail | 0:18409537564c | 63 | |
punkisnail | 0:18409537564c | 64 | void stimProgress() { |
punkisnail | 0:18409537564c | 65 | stim_progress = true; |
punkisnail | 0:18409537564c | 66 | } |
punkisnail | 0:18409537564c | 67 | |
punkisnail | 0:18409537564c | 68 | void stimulate() { |
punkisnail | 0:18409537564c | 69 | // DAC = intensity |
punkisnail | 0:18409537564c | 70 | HB_CH1 = 1; |
punkisnail | 0:18409537564c | 71 | HB_CH2 = 0; |
punkisnail | 0:18409537564c | 72 | |
punkisnail | 0:18409537564c | 73 | wait_us(100); |
punkisnail | 0:18409537564c | 74 | |
punkisnail | 0:18409537564c | 75 | HB_CH1 = 0; |
punkisnail | 0:18409537564c | 76 | HB_CH2 = 1; |
punkisnail | 0:18409537564c | 77 | |
punkisnail | 0:18409537564c | 78 | wait_us(100); |
punkisnail | 0:18409537564c | 79 | |
punkisnail | 0:18409537564c | 80 | HB_CH1 = 0; |
punkisnail | 0:18409537564c | 81 | HB_CH2 = 0; |
punkisnail | 0:18409537564c | 82 | // DAC = 0 |
punkisnail | 0:18409537564c | 83 | } |
punkisnail | 0:18409537564c | 84 | |
punkisnail | 0:18409537564c | 85 | void startStimProcess() { |
punkisnail | 0:18409537564c | 86 | stim_active = true; |
punkisnail | 0:18409537564c | 87 | stim_count = 0; |
punkisnail | 0:18409537564c | 88 | BOOST_EN = 1; |
punkisnail | 0:18409537564c | 89 | BOOST_PWM.write(DUTY_CYCLE); |
punkisnail | 0:18409537564c | 90 | stim_progress = false; |
punkisnail | 0:18409537564c | 91 | stimTicker.attach(&stimProgress, 1); |
punkisnail | 0:18409537564c | 92 | } |
punkisnail | 0:18409537564c | 93 | |
punkisnail | 0:18409537564c | 94 | void stopStimProcess() { |
punkisnail | 0:18409537564c | 95 | if (!stim_mode) return; |
punkisnail | 0:18409537564c | 96 | |
punkisnail | 0:18409537564c | 97 | stimTicker.detach(); |
punkisnail | 0:18409537564c | 98 | |
punkisnail | 0:18409537564c | 99 | stim_count = 0; |
punkisnail | 0:18409537564c | 100 | stim_active = false; |
punkisnail | 0:18409537564c | 101 | stim_mode = false; |
punkisnail | 0:18409537564c | 102 | stim_progress = false; |
punkisnail | 0:18409537564c | 103 | |
punkisnail | 0:18409537564c | 104 | HB_CH1 = 0; |
punkisnail | 0:18409537564c | 105 | HB_CH2 = 0; |
punkisnail | 0:18409537564c | 106 | |
punkisnail | 0:18409537564c | 107 | BOOST_EN = 0; |
punkisnail | 0:18409537564c | 108 | BOOST_PWM.write(0); |
punkisnail | 0:18409537564c | 109 | |
punkisnail | 0:18409537564c | 110 | // DAC = 0 |
punkisnail | 0:18409537564c | 111 | } |
punkisnail | 0:18409537564c | 112 | |
punkisnail | 0:18409537564c | 113 | int main() |
punkisnail | 0:18409537564c | 114 | { |
punkisnail | 0:18409537564c | 115 | SW_ONOFF.mode(PullUp); |
punkisnail | 0:18409537564c | 116 | SW_MODE.mode(PullUp); |
punkisnail | 0:18409537564c | 117 | SW_PLUS.mode(PullUp); |
punkisnail | 0:18409537564c | 118 | SW_MINUS.mode(PullUp); |
punkisnail | 0:18409537564c | 119 | |
punkisnail | 0:18409537564c | 120 | SW_ONOFF.fall(&flip_ONOFF); |
punkisnail | 0:18409537564c | 121 | SW_MODE.fall(&flip_MODE); |
punkisnail | 0:18409537564c | 122 | SW_PLUS.fall(&flip_PLUS); |
punkisnail | 0:18409537564c | 123 | SW_MINUS.fall(&flip_MINUS); |
punkisnail | 0:18409537564c | 124 | |
punkisnail | 0:18409537564c | 125 | HB_CH1 = 0; |
punkisnail | 0:18409537564c | 126 | HB_CH2 = 0; |
punkisnail | 0:18409537564c | 127 | BOOST_EN = 0; |
punkisnail | 0:18409537564c | 128 | VEN_3V3B = 0; |
punkisnail | 0:18409537564c | 129 | |
punkisnail | 0:18409537564c | 130 | BOOST_PWM.period_us(100); |
punkisnail | 0:18409537564c | 131 | |
punkisnail | 0:18409537564c | 132 | LEDR.period_us(500); |
punkisnail | 0:18409537564c | 133 | LEDG.period_us(500); |
punkisnail | 0:18409537564c | 134 | LEDB.period_us(500); |
punkisnail | 0:18409537564c | 135 | |
punkisnail | 0:18409537564c | 136 | while(1) |
punkisnail | 0:18409537564c | 137 | { |
punkisnail | 0:18409537564c | 138 | if (!low_power) { |
punkisnail | 0:18409537564c | 139 | SW_MODE.enable_irq(); |
punkisnail | 0:18409537564c | 140 | SW_PLUS.enable_irq(); |
punkisnail | 0:18409537564c | 141 | SW_MINUS.enable_irq(); |
punkisnail | 0:18409537564c | 142 | |
punkisnail | 0:18409537564c | 143 | VEN_3V3B = 1; |
punkisnail | 0:18409537564c | 144 | |
punkisnail | 0:18409537564c | 145 | intensity = 0; |
punkisnail | 0:18409537564c | 146 | |
punkisnail | 0:18409537564c | 147 | while (!low_power) { |
punkisnail | 0:18409537564c | 148 | if (stim_mode && !stim_active) { |
punkisnail | 0:18409537564c | 149 | startStimProcess(); |
punkisnail | 0:18409537564c | 150 | } |
punkisnail | 0:18409537564c | 151 | |
punkisnail | 0:18409537564c | 152 | if (stim_active) { |
punkisnail | 0:18409537564c | 153 | LEDR.write(0.5f); |
punkisnail | 0:18409537564c | 154 | LEDG.write(0.25f); |
punkisnail | 0:18409537564c | 155 | LEDB.write(0); |
punkisnail | 0:18409537564c | 156 | |
punkisnail | 0:18409537564c | 157 | if (stim_progress) { |
punkisnail | 0:18409537564c | 158 | stim_progress = false; |
punkisnail | 0:18409537564c | 159 | |
punkisnail | 0:18409537564c | 160 | LEDR.write(0.5f); |
punkisnail | 0:18409537564c | 161 | LEDG.write(0); |
punkisnail | 0:18409537564c | 162 | LEDB.write(0.5f); |
punkisnail | 0:18409537564c | 163 | |
punkisnail | 0:18409537564c | 164 | stimulate(); |
punkisnail | 0:18409537564c | 165 | |
punkisnail | 0:18409537564c | 166 | if (++stim_count == 4) { |
punkisnail | 0:18409537564c | 167 | stopStimProcess(); |
punkisnail | 0:18409537564c | 168 | } |
punkisnail | 0:18409537564c | 169 | |
punkisnail | 0:18409537564c | 170 | wait(0.2f); |
punkisnail | 0:18409537564c | 171 | } |
punkisnail | 0:18409537564c | 172 | } |
punkisnail | 0:18409537564c | 173 | else { |
punkisnail | 0:18409537564c | 174 | LEDR.write(0); |
punkisnail | 0:18409537564c | 175 | LEDG.write(0.5f); |
punkisnail | 0:18409537564c | 176 | LEDB.write(0); |
punkisnail | 0:18409537564c | 177 | } |
punkisnail | 0:18409537564c | 178 | |
punkisnail | 0:18409537564c | 179 | if (flip_power) { |
punkisnail | 0:18409537564c | 180 | flip_power = false; |
punkisnail | 0:18409537564c | 181 | wait(0.2f); |
punkisnail | 0:18409537564c | 182 | low_power = true; |
punkisnail | 0:18409537564c | 183 | } |
punkisnail | 0:18409537564c | 184 | |
punkisnail | 0:18409537564c | 185 | __WFE(); |
punkisnail | 0:18409537564c | 186 | __SEV(); |
punkisnail | 0:18409537564c | 187 | __WFE(); |
punkisnail | 0:18409537564c | 188 | } |
punkisnail | 0:18409537564c | 189 | } |
punkisnail | 0:18409537564c | 190 | else { |
punkisnail | 0:18409537564c | 191 | SW_ONOFF.disable_irq(); |
punkisnail | 0:18409537564c | 192 | SW_MODE.disable_irq(); |
punkisnail | 0:18409537564c | 193 | SW_PLUS.disable_irq(); |
punkisnail | 0:18409537564c | 194 | SW_MINUS.disable_irq(); |
punkisnail | 0:18409537564c | 195 | |
punkisnail | 0:18409537564c | 196 | stopStimProcess(); |
punkisnail | 0:18409537564c | 197 | |
punkisnail | 0:18409537564c | 198 | VEN_3V3B = 0; |
punkisnail | 0:18409537564c | 199 | |
punkisnail | 0:18409537564c | 200 | LEDR.write(0); |
punkisnail | 0:18409537564c | 201 | LEDG.write(0); |
punkisnail | 0:18409537564c | 202 | LEDB.write(0); |
punkisnail | 0:18409537564c | 203 | |
punkisnail | 0:18409537564c | 204 | wait(0.2f); |
punkisnail | 0:18409537564c | 205 | |
punkisnail | 0:18409537564c | 206 | SW_ONOFF.enable_irq(); |
punkisnail | 0:18409537564c | 207 | |
punkisnail | 0:18409537564c | 208 | nrf_gpio_cfg_sense_input(15, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW); |
punkisnail | 0:18409537564c | 209 | NRF_POWER->SYSTEMOFF = 1; |
punkisnail | 0:18409537564c | 210 | |
punkisnail | 0:18409537564c | 211 | if (flip_power) { |
punkisnail | 0:18409537564c | 212 | flip_power = false; |
punkisnail | 0:18409537564c | 213 | wait(0.2f); |
punkisnail | 0:18409537564c | 214 | low_power = false; |
punkisnail | 0:18409537564c | 215 | } |
punkisnail | 0:18409537564c | 216 | } |
punkisnail | 0:18409537564c | 217 | |
punkisnail | 0:18409537564c | 218 | __WFE(); |
punkisnail | 0:18409537564c | 219 | __SEV(); |
punkisnail | 0:18409537564c | 220 | __WFE(); |
punkisnail | 0:18409537564c | 221 | } |
punkisnail | 0:18409537564c | 222 | |
punkisnail | 0:18409537564c | 223 | return 0; |
punkisnail | 0:18409537564c | 224 | } |