Mirror with some correction
Dependencies: mbed FastIO FastPWM USBDevice
TinyDigitalIn/TinyDigitalIn.h@116:7a67265d7c19, 2021-10-01 (annotated)
- Committer:
- arnoz
- Date:
- Fri Oct 01 08:19:46 2021 +0000
- Revision:
- 116:7a67265d7c19
- Parent:
- 82:4f6209cb5c33
- Correct information regarding your last merge
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mjr | 73:4e8ce0b18915 | 1 | #include "mbed.h" |
mjr | 73:4e8ce0b18915 | 2 | |
mjr | 48:058ace2aed1d | 3 | // TinyDigitalIn - a simpler verison of DigitalIn that takes less |
mjr | 48:058ace2aed1d | 4 | // memory. |
mjr | 48:058ace2aed1d | 5 | // |
mjr | 48:058ace2aed1d | 6 | // This version uses the same mbed library infrastructure as the |
mjr | 73:4e8ce0b18915 | 7 | // regular DigitalIn, but we save a little memory by storing only |
mjr | 48:058ace2aed1d | 8 | // the minimum set of fields needed to read the pin. The mbed |
mjr | 48:058ace2aed1d | 9 | // DigitalIn has a larger memory footprint because it stores the |
mjr | 48:058ace2aed1d | 10 | // full set of machine register pointers for the pin, most of |
mjr | 48:058ace2aed1d | 11 | // which aren't needed for an input-only pin. |
mjr | 48:058ace2aed1d | 12 | |
mjr | 48:058ace2aed1d | 13 | class TinyDigitalIn |
mjr | 48:058ace2aed1d | 14 | { |
mjr | 48:058ace2aed1d | 15 | public: |
mjr | 73:4e8ce0b18915 | 16 | TinyDigitalIn(PinName pin) { assignPin(pin); } |
mjr | 73:4e8ce0b18915 | 17 | TinyDigitalIn() { assignPin(NC); } |
mjr | 73:4e8ce0b18915 | 18 | |
mjr | 73:4e8ce0b18915 | 19 | void assignPin(PinName pin) |
mjr | 48:058ace2aed1d | 20 | { |
mjr | 48:058ace2aed1d | 21 | if (pin != NC) |
mjr | 48:058ace2aed1d | 22 | { |
mjr | 48:058ace2aed1d | 23 | // initialize the pin as a GPIO Digital In port |
mjr | 48:058ace2aed1d | 24 | gpio_t gpio; |
mjr | 48:058ace2aed1d | 25 | gpio_init_in(&gpio, pin); |
mjr | 48:058ace2aed1d | 26 | |
mjr | 48:058ace2aed1d | 27 | // get the register input port and mask |
mjr | 48:058ace2aed1d | 28 | pdir = gpio.reg_in; |
mjr | 73:4e8ce0b18915 | 29 | uint32_t mask = gpio.mask; |
mjr | 73:4e8ce0b18915 | 30 | |
mjr | 73:4e8ce0b18915 | 31 | // Figure the bit shift: find how many right shifts it takes |
mjr | 73:4e8ce0b18915 | 32 | // to shift the mask bit into the 0th bit position. This lets |
mjr | 73:4e8ce0b18915 | 33 | // us pull out the result value in read() as a 0 or 1 by shifting |
mjr | 73:4e8ce0b18915 | 34 | // the register by this same amount and masking it against 0x01. |
mjr | 73:4e8ce0b18915 | 35 | // The right shift is slightly more efficient than a conditional |
mjr | 73:4e8ce0b18915 | 36 | // to convert a bit in the middle of the register to a canonical |
mjr | 73:4e8ce0b18915 | 37 | // 0 or 1 result, and we have to do the mask anyway to pull out |
mjr | 73:4e8ce0b18915 | 38 | // the one bit, so this makes the overall read slightly faster. |
mjr | 73:4e8ce0b18915 | 39 | for (shift = 0 ; |
mjr | 73:4e8ce0b18915 | 40 | mask != 0 && (mask & 0x00000001) == 0 ; |
mjr | 73:4e8ce0b18915 | 41 | mask >>= 1, shift++) ; |
mjr | 73:4e8ce0b18915 | 42 | } |
mjr | 73:4e8ce0b18915 | 43 | else |
mjr | 73:4e8ce0b18915 | 44 | { |
mjr | 73:4e8ce0b18915 | 45 | // not connected - point to a dummy port that always reads as 0 |
mjr | 73:4e8ce0b18915 | 46 | pdir = (volatile uint32_t *)&pdir_nc; |
mjr | 73:4e8ce0b18915 | 47 | shift = 0; |
mjr | 73:4e8ce0b18915 | 48 | } |
mjr | 48:058ace2aed1d | 49 | } |
mjr | 48:058ace2aed1d | 50 | |
mjr | 73:4e8ce0b18915 | 51 | inline int read() { return (*pdir >> shift) & 0x00000001; } |
mjr | 48:058ace2aed1d | 52 | inline operator int() { return read(); } |
mjr | 48:058ace2aed1d | 53 | |
mjr | 77:0b96f6867312 | 54 | protected: |
mjr | 73:4e8ce0b18915 | 55 | volatile uint32_t *pdir; // pointer to GPIO register for this port |
mjr | 73:4e8ce0b18915 | 56 | uint8_t shift; // number of bits to shift register value to get our port bit |
mjr | 73:4e8ce0b18915 | 57 | |
mjr | 73:4e8ce0b18915 | 58 | // pointer to dummy location for NC ports - reads as all 1 bits, |
mjr | 73:4e8ce0b18915 | 59 | // as though it were wired to a pull-up port that's not connected |
mjr | 73:4e8ce0b18915 | 60 | // to anything external |
mjr | 73:4e8ce0b18915 | 61 | static const uint32_t pdir_nc; |
mjr | 73:4e8ce0b18915 | 62 | } __attribute__((packed)); |