William Marsh / Mbed OS DACandticker_sample_with_debug
Committer:
WilliamMarshQMUL
Date:
Mon Feb 04 16:11:54 2019 +0000
Revision:
3:f04c8a88a27d
Parent:
2:4857a7cbb9da
Child:
4:42b85520dca6
Allow DAC to be called from Ticker with lock-free version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WilliamMarshQMUL 0:5307f49cd305 1 // Lab 3 Example Program 2
WilliamMarshQMUL 0:5307f49cd305 2 // -----------------------
WilliamMarshQMUL 0:5307f49cd305 3 // Periodically write to the AnalogOut to create a sine wave
WilliamMarshQMUL 2:4857a7cbb9da 4 // Alternate between two fixed frequencies every 30 sec
WilliamMarshQMUL 3:f04c8a88a27d 5 //
WilliamMarshQMUL 3:f04c8a88a27d 6 // This code uses the Ticker API to create a periodic interrupt
WilliamMarshQMUL 3:f04c8a88a27d 7 // Devices used in this way must not call locks; a variant of
WilliamMarshQMUL 3:f04c8a88a27d 8 // the AnalogOut device w/o locking is created for this
WilliamMarshQMUL 0:5307f49cd305 9 //
WilliamMarshQMUL 0:5307f49cd305 10 // THIS VERSION HAS DEBUGGING CODE USING THE SERIAL PORT
WilliamMarshQMUL 0:5307f49cd305 11
WilliamMarshQMUL 1:284229960d6a 12 // Revised for mbed 5
WilliamMarshQMUL 1:284229960d6a 13
WilliamMarshQMUL 0:5307f49cd305 14 #include "mbed.h"
WilliamMarshQMUL 0:5307f49cd305 15 #include "sineTable.h"
WilliamMarshQMUL 0:5307f49cd305 16
WilliamMarshQMUL 3:f04c8a88a27d 17 // --------------------------
WilliamMarshQMUL 3:f04c8a88a27d 18 // This declaration introduces a derived version of the mbed AnalogOut
WilliamMarshQMUL 3:f04c8a88a27d 19 // class with the locking removed.
WilliamMarshQMUL 3:f04c8a88a27d 20 // You do NOT NEED TO UNDERSTAND this
WilliamMarshQMUL 3:f04c8a88a27d 21 class AnalogOut_unsafe : public AnalogOut {
WilliamMarshQMUL 3:f04c8a88a27d 22 public:
WilliamMarshQMUL 3:f04c8a88a27d 23 AnalogOut_unsafe (PinName pin) : AnalogOut (pin) {}
WilliamMarshQMUL 3:f04c8a88a27d 24 protected:
WilliamMarshQMUL 3:f04c8a88a27d 25 virtual void lock() {}
WilliamMarshQMUL 3:f04c8a88a27d 26 virtual void unlock() {}
WilliamMarshQMUL 3:f04c8a88a27d 27 };
WilliamMarshQMUL 3:f04c8a88a27d 28 // --------------------------
WilliamMarshQMUL 3:f04c8a88a27d 29
WilliamMarshQMUL 0:5307f49cd305 30 Ticker tick ; // Creates periodic interrupt
WilliamMarshQMUL 3:f04c8a88a27d 31 AnalogOut_unsafe ao(PTE30) ; // Analog output
WilliamMarshQMUL 0:5307f49cd305 32
WilliamMarshQMUL 0:5307f49cd305 33 // --- following code for debugging ---
WilliamMarshQMUL 0:5307f49cd305 34 Thread debugT ;
WilliamMarshQMUL 3:f04c8a88a27d 35 EventFlags event_flags;
WilliamMarshQMUL 0:5307f49cd305 36 Serial pc(USBTX, USBRX); // tx, rx, useful for debugging
WilliamMarshQMUL 0:5307f49cd305 37
WilliamMarshQMUL 2:4857a7cbb9da 38 // Put a simple representation of the sine wave
WilliamMarshQMUL 0:5307f49cd305 39 // to the serial output. ONLY at low frequency
WilliamMarshQMUL 0:5307f49cd305 40 void debug(int index) {
WilliamMarshQMUL 0:5307f49cd305 41 int sine4 = sine[index] >> 11 ; // get top 5 bits
WilliamMarshQMUL 0:5307f49cd305 42 pc.putc('*') ;
WilliamMarshQMUL 0:5307f49cd305 43 while (sine4--) pc.putc('*') ;
WilliamMarshQMUL 0:5307f49cd305 44 pc.putc('\n') ;
WilliamMarshQMUL 3:f04c8a88a27d 45 pc.putc('\r') ; // depends on the OS being used
WilliamMarshQMUL 0:5307f49cd305 46 }
WilliamMarshQMUL 0:5307f49cd305 47
WilliamMarshQMUL 0:5307f49cd305 48 volatile int index = 0 ; // this variable is not just for debugging!!
WilliamMarshQMUL 0:5307f49cd305 49
WilliamMarshQMUL 0:5307f49cd305 50 void debugCallback() {
WilliamMarshQMUL 0:5307f49cd305 51 while (true) {
WilliamMarshQMUL 3:f04c8a88a27d 52 event_flags.wait_any(0x1) ;
WilliamMarshQMUL 0:5307f49cd305 53 debug(index) ; // there is a race condition here
WilliamMarshQMUL 0:5307f49cd305 54 }
WilliamMarshQMUL 0:5307f49cd305 55 }
WilliamMarshQMUL 0:5307f49cd305 56 // ---- end of debugging code ---------
WilliamMarshQMUL 0:5307f49cd305 57
WilliamMarshQMUL 2:4857a7cbb9da 58 // Function called periodically
WilliamMarshQMUL 0:5307f49cd305 59 // Write new value to AnalogOut
WilliamMarshQMUL 0:5307f49cd305 60 void writeAout() {
WilliamMarshQMUL 0:5307f49cd305 61 ao.write_u16(sine[index]) ;
WilliamMarshQMUL 3:f04c8a88a27d 62 event_flags.set(0x1) ; // DEBUGGING low frequency only
WilliamMarshQMUL 0:5307f49cd305 63 index = (index + 1) % 64 ;
WilliamMarshQMUL 0:5307f49cd305 64 }
WilliamMarshQMUL 0:5307f49cd305 65
WilliamMarshQMUL 0:5307f49cd305 66 // Control the frequency of updates
WilliamMarshQMUL 0:5307f49cd305 67 // Alternative between two frequencies
WilliamMarshQMUL 0:5307f49cd305 68 int main() {
WilliamMarshQMUL 0:5307f49cd305 69 int update_us = 100000 ; // 100ms
WilliamMarshQMUL 0:5307f49cd305 70 debugT.start(&debugCallback) ;
WilliamMarshQMUL 0:5307f49cd305 71 while (true) {
WilliamMarshQMUL 0:5307f49cd305 72 pc.printf("Update at 64 x 100ms giving about 0.15Hz\n");
WilliamMarshQMUL 0:5307f49cd305 73 tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut
WilliamMarshQMUL 2:4857a7cbb9da 74 wait(30.0) ; // wait 30 sec - 30000ms
WilliamMarshQMUL 0:5307f49cd305 75 update_us = 150000 ; // 150ms
WilliamMarshQMUL 0:5307f49cd305 76 pc.printf("Update at 64 x 150ms giving about 0.1Hz\n");
WilliamMarshQMUL 0:5307f49cd305 77 tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut
WilliamMarshQMUL 2:4857a7cbb9da 78 wait(30.0) ; // wait 30 sec - 30000ms
WilliamMarshQMUL 0:5307f49cd305 79 update_us = 100000 ; // 100ms
WilliamMarshQMUL 0:5307f49cd305 80 }
WilliamMarshQMUL 0:5307f49cd305 81 }