Library
Fork of mbed by
DigitalInOut.h@0:a8fa94490a0a, 2012-09-22 (annotated)
- Committer:
- gokmenascioglu
- Date:
- Sat Sep 22 10:32:07 2012 +0000
- Revision:
- 0:a8fa94490a0a
alican library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gokmenascioglu | 0:a8fa94490a0a | 1 | /* mbed Microcontroller Library - DigitalInOut |
gokmenascioglu | 0:a8fa94490a0a | 2 | * Copyright (c) 2006-2011 ARM Limited. All rights reserved. |
gokmenascioglu | 0:a8fa94490a0a | 3 | */ |
gokmenascioglu | 0:a8fa94490a0a | 4 | |
gokmenascioglu | 0:a8fa94490a0a | 5 | #ifndef MBED_DIGITALINOUT_H |
gokmenascioglu | 0:a8fa94490a0a | 6 | #define MBED_DIGITALINOUT_H |
gokmenascioglu | 0:a8fa94490a0a | 7 | |
gokmenascioglu | 0:a8fa94490a0a | 8 | #include "platform.h" |
gokmenascioglu | 0:a8fa94490a0a | 9 | #include "PinNames.h" |
gokmenascioglu | 0:a8fa94490a0a | 10 | #include "PeripheralNames.h" |
gokmenascioglu | 0:a8fa94490a0a | 11 | #include "Base.h" |
gokmenascioglu | 0:a8fa94490a0a | 12 | |
gokmenascioglu | 0:a8fa94490a0a | 13 | namespace mbed { |
gokmenascioglu | 0:a8fa94490a0a | 14 | |
gokmenascioglu | 0:a8fa94490a0a | 15 | /* Class: DigitalInOut |
gokmenascioglu | 0:a8fa94490a0a | 16 | * A digital input/output, used for setting or reading a bi-directional pin |
gokmenascioglu | 0:a8fa94490a0a | 17 | */ |
gokmenascioglu | 0:a8fa94490a0a | 18 | class DigitalInOut : public Base { |
gokmenascioglu | 0:a8fa94490a0a | 19 | |
gokmenascioglu | 0:a8fa94490a0a | 20 | public: |
gokmenascioglu | 0:a8fa94490a0a | 21 | |
gokmenascioglu | 0:a8fa94490a0a | 22 | /* Constructor: DigitalInOut |
gokmenascioglu | 0:a8fa94490a0a | 23 | * Create a DigitalInOut connected to the specified pin |
gokmenascioglu | 0:a8fa94490a0a | 24 | * |
gokmenascioglu | 0:a8fa94490a0a | 25 | * Variables: |
gokmenascioglu | 0:a8fa94490a0a | 26 | * pin - DigitalInOut pin to connect to |
gokmenascioglu | 0:a8fa94490a0a | 27 | */ |
gokmenascioglu | 0:a8fa94490a0a | 28 | DigitalInOut(PinName pin, const char* name = NULL); |
gokmenascioglu | 0:a8fa94490a0a | 29 | |
gokmenascioglu | 0:a8fa94490a0a | 30 | /* Function: write |
gokmenascioglu | 0:a8fa94490a0a | 31 | * Set the output, specified as 0 or 1 (int) |
gokmenascioglu | 0:a8fa94490a0a | 32 | * |
gokmenascioglu | 0:a8fa94490a0a | 33 | * Variables: |
gokmenascioglu | 0:a8fa94490a0a | 34 | * value - An integer specifying the pin output value, |
gokmenascioglu | 0:a8fa94490a0a | 35 | * 0 for logical 0 and 1 (or any other non-zero value) for logical 1 |
gokmenascioglu | 0:a8fa94490a0a | 36 | */ |
gokmenascioglu | 0:a8fa94490a0a | 37 | void write(int value) { |
gokmenascioglu | 0:a8fa94490a0a | 38 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
gokmenascioglu | 0:a8fa94490a0a | 39 | |
gokmenascioglu | 0:a8fa94490a0a | 40 | if(value) { |
gokmenascioglu | 0:a8fa94490a0a | 41 | _gpio->FIOSET = _mask; |
gokmenascioglu | 0:a8fa94490a0a | 42 | } else { |
gokmenascioglu | 0:a8fa94490a0a | 43 | _gpio->FIOCLR = _mask; |
gokmenascioglu | 0:a8fa94490a0a | 44 | } |
gokmenascioglu | 0:a8fa94490a0a | 45 | |
gokmenascioglu | 0:a8fa94490a0a | 46 | #elif defined(TARGET_LPC11U24) |
gokmenascioglu | 0:a8fa94490a0a | 47 | |
gokmenascioglu | 0:a8fa94490a0a | 48 | if(value) { |
gokmenascioglu | 0:a8fa94490a0a | 49 | LPC_GPIO->SET[_index] = _mask; |
gokmenascioglu | 0:a8fa94490a0a | 50 | } else { |
gokmenascioglu | 0:a8fa94490a0a | 51 | LPC_GPIO->CLR[_index] = _mask; |
gokmenascioglu | 0:a8fa94490a0a | 52 | } |
gokmenascioglu | 0:a8fa94490a0a | 53 | #endif |
gokmenascioglu | 0:a8fa94490a0a | 54 | } |
gokmenascioglu | 0:a8fa94490a0a | 55 | |
gokmenascioglu | 0:a8fa94490a0a | 56 | /* Function: read |
gokmenascioglu | 0:a8fa94490a0a | 57 | * Return the output setting, represented as 0 or 1 (int) |
gokmenascioglu | 0:a8fa94490a0a | 58 | * |
gokmenascioglu | 0:a8fa94490a0a | 59 | * Variables: |
gokmenascioglu | 0:a8fa94490a0a | 60 | * returns - An integer representing the output setting of the pin if it is an output, |
gokmenascioglu | 0:a8fa94490a0a | 61 | * or read the input if set as an input |
gokmenascioglu | 0:a8fa94490a0a | 62 | */ |
gokmenascioglu | 0:a8fa94490a0a | 63 | int read() { |
gokmenascioglu | 0:a8fa94490a0a | 64 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
gokmenascioglu | 0:a8fa94490a0a | 65 | |
gokmenascioglu | 0:a8fa94490a0a | 66 | return ((_gpio->FIOPIN & _mask) ? 1 : 0); |
gokmenascioglu | 0:a8fa94490a0a | 67 | #elif defined(TARGET_LPC11U24) |
gokmenascioglu | 0:a8fa94490a0a | 68 | return ((LPC_GPIO->PIN[_index] & _mask) ? 1 : 0); |
gokmenascioglu | 0:a8fa94490a0a | 69 | #endif |
gokmenascioglu | 0:a8fa94490a0a | 70 | } |
gokmenascioglu | 0:a8fa94490a0a | 71 | |
gokmenascioglu | 0:a8fa94490a0a | 72 | |
gokmenascioglu | 0:a8fa94490a0a | 73 | /* Function: output |
gokmenascioglu | 0:a8fa94490a0a | 74 | * Set as an output |
gokmenascioglu | 0:a8fa94490a0a | 75 | */ |
gokmenascioglu | 0:a8fa94490a0a | 76 | void output(); |
gokmenascioglu | 0:a8fa94490a0a | 77 | |
gokmenascioglu | 0:a8fa94490a0a | 78 | /* Function: input |
gokmenascioglu | 0:a8fa94490a0a | 79 | * Set as an input |
gokmenascioglu | 0:a8fa94490a0a | 80 | */ |
gokmenascioglu | 0:a8fa94490a0a | 81 | void input(); |
gokmenascioglu | 0:a8fa94490a0a | 82 | |
gokmenascioglu | 0:a8fa94490a0a | 83 | /* Function: mode |
gokmenascioglu | 0:a8fa94490a0a | 84 | * Set the input pin mode |
gokmenascioglu | 0:a8fa94490a0a | 85 | * |
gokmenascioglu | 0:a8fa94490a0a | 86 | * Variables: |
gokmenascioglu | 0:a8fa94490a0a | 87 | * mode - PullUp, PullDown, PullNone, OpenDrain |
gokmenascioglu | 0:a8fa94490a0a | 88 | */ |
gokmenascioglu | 0:a8fa94490a0a | 89 | void mode(PinMode pull); |
gokmenascioglu | 0:a8fa94490a0a | 90 | |
gokmenascioglu | 0:a8fa94490a0a | 91 | #ifdef MBED_OPERATORS |
gokmenascioglu | 0:a8fa94490a0a | 92 | /* Function: operator= |
gokmenascioglu | 0:a8fa94490a0a | 93 | * A shorthand for <write> |
gokmenascioglu | 0:a8fa94490a0a | 94 | */ |
gokmenascioglu | 0:a8fa94490a0a | 95 | DigitalInOut& operator= (int value) { |
gokmenascioglu | 0:a8fa94490a0a | 96 | write(value); |
gokmenascioglu | 0:a8fa94490a0a | 97 | return *this; |
gokmenascioglu | 0:a8fa94490a0a | 98 | } |
gokmenascioglu | 0:a8fa94490a0a | 99 | |
gokmenascioglu | 0:a8fa94490a0a | 100 | DigitalInOut& operator= (DigitalInOut& rhs) { |
gokmenascioglu | 0:a8fa94490a0a | 101 | write(rhs.read()); |
gokmenascioglu | 0:a8fa94490a0a | 102 | return *this; |
gokmenascioglu | 0:a8fa94490a0a | 103 | } |
gokmenascioglu | 0:a8fa94490a0a | 104 | |
gokmenascioglu | 0:a8fa94490a0a | 105 | /* Function: operator int() |
gokmenascioglu | 0:a8fa94490a0a | 106 | * A shorthand for <read> |
gokmenascioglu | 0:a8fa94490a0a | 107 | */ |
gokmenascioglu | 0:a8fa94490a0a | 108 | operator int() { |
gokmenascioglu | 0:a8fa94490a0a | 109 | return read(); |
gokmenascioglu | 0:a8fa94490a0a | 110 | } |
gokmenascioglu | 0:a8fa94490a0a | 111 | #endif |
gokmenascioglu | 0:a8fa94490a0a | 112 | |
gokmenascioglu | 0:a8fa94490a0a | 113 | #ifdef MBED_RPC |
gokmenascioglu | 0:a8fa94490a0a | 114 | virtual const struct rpc_method *get_rpc_methods(); |
gokmenascioglu | 0:a8fa94490a0a | 115 | static struct rpc_class *get_rpc_class(); |
gokmenascioglu | 0:a8fa94490a0a | 116 | #endif |
gokmenascioglu | 0:a8fa94490a0a | 117 | |
gokmenascioglu | 0:a8fa94490a0a | 118 | protected: |
gokmenascioglu | 0:a8fa94490a0a | 119 | |
gokmenascioglu | 0:a8fa94490a0a | 120 | PinName _pin; |
gokmenascioglu | 0:a8fa94490a0a | 121 | |
gokmenascioglu | 0:a8fa94490a0a | 122 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
gokmenascioglu | 0:a8fa94490a0a | 123 | LPC_GPIO_TypeDef *_gpio; |
gokmenascioglu | 0:a8fa94490a0a | 124 | #elif defined(TARGET_LPC11U24) |
gokmenascioglu | 0:a8fa94490a0a | 125 | int _index; |
gokmenascioglu | 0:a8fa94490a0a | 126 | #endif |
gokmenascioglu | 0:a8fa94490a0a | 127 | |
gokmenascioglu | 0:a8fa94490a0a | 128 | uint32_t _mask; |
gokmenascioglu | 0:a8fa94490a0a | 129 | |
gokmenascioglu | 0:a8fa94490a0a | 130 | }; |
gokmenascioglu | 0:a8fa94490a0a | 131 | |
gokmenascioglu | 0:a8fa94490a0a | 132 | } // namespace mbed |
gokmenascioglu | 0:a8fa94490a0a | 133 | |
gokmenascioglu | 0:a8fa94490a0a | 134 | #endif |