Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
include/Lib17_DIO.h@0:127224866798, 2011-04-11 (annotated)
- Committer:
- AjK
- Date:
- Mon Apr 11 15:05:27 2011 +0000
- Revision:
- 0:127224866798
- Child:
- 1:2f99edb5545c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AjK | 0:127224866798 | 1 | /**************************************************************************** |
AjK | 0:127224866798 | 2 | * Product: LIB17 Open Source Library |
AjK | 0:127224866798 | 3 | * |
AjK | 0:127224866798 | 4 | * Steller Technologies Limited |
AjK | 0:127224866798 | 5 | * ---------------------------- |
AjK | 0:127224866798 | 6 | * |
AjK | 0:127224866798 | 7 | * Copyright (C) 2002-2011 Steller Technologies Limited. All rights reserved. |
AjK | 0:127224866798 | 8 | * |
AjK | 0:127224866798 | 9 | * This software may be distributed and modified under the terms of the GNU |
AjK | 0:127224866798 | 10 | * General Public License version 2 (GPL) as published by the Free Software |
AjK | 0:127224866798 | 11 | * Foundation and appearing in the file GPL.TXT included in the packaging of |
AjK | 0:127224866798 | 12 | * this file. Please note that GPL Section 2[b] requires that all works based |
AjK | 0:127224866798 | 13 | * on this software must also be made publicly available under the terms of |
AjK | 0:127224866798 | 14 | * the GPL ("Copyleft"). |
AjK | 0:127224866798 | 15 | * |
AjK | 0:127224866798 | 16 | * Alternatively, this software may be distributed and modified under the |
AjK | 0:127224866798 | 17 | * terms of Steller Technologies Limited commercial licenses, which expressly |
AjK | 0:127224866798 | 18 | * supersede the GPL and are specifically designed for licensees interested in |
AjK | 0:127224866798 | 19 | * retaining the proprietary status of their code. |
AjK | 0:127224866798 | 20 | * |
AjK | 0:127224866798 | 21 | ***************************************************************************/ |
AjK | 0:127224866798 | 22 | |
AjK | 0:127224866798 | 23 | #ifndef AJK_Lib17_DIO_H |
AjK | 0:127224866798 | 24 | #define AJK_Lib17_DIO_H |
AjK | 0:127224866798 | 25 | |
AjK | 0:127224866798 | 26 | #include "Lib17_Mbed.h" |
AjK | 0:127224866798 | 27 | #include "Lib17_IOmacros.h" |
AjK | 0:127224866798 | 28 | |
AjK | 0:127224866798 | 29 | namespace AjK { |
AjK | 0:127224866798 | 30 | |
AjK | 0:127224866798 | 31 | class Lib17_DIO { |
AjK | 0:127224866798 | 32 | public: |
AjK | 0:127224866798 | 33 | enum Direction { |
AjK | 0:127224866798 | 34 | Out = 0 |
AjK | 0:127224866798 | 35 | , In |
AjK | 0:127224866798 | 36 | }; |
AjK | 0:127224866798 | 37 | |
AjK | 0:127224866798 | 38 | protected: |
AjK | 0:127224866798 | 39 | PinName pin; |
AjK | 0:127224866798 | 40 | uint32_t mask; |
AjK | 0:127224866798 | 41 | uint32_t fiodir; |
AjK | 0:127224866798 | 42 | uint32_t fiomask; |
AjK | 0:127224866798 | 43 | uint32_t fiopin; |
AjK | 0:127224866798 | 44 | uint32_t fioset; |
AjK | 0:127224866798 | 45 | uint32_t fioclr; |
AjK | 0:127224866798 | 46 | |
AjK | 0:127224866798 | 47 | inline void setpin(PinName p) { pin = p; } |
AjK | 0:127224866798 | 48 | inline void setmask(PinName p) { mask = (uint32_t)(1UL << ((uint32_t)p & 0x1F)); } |
AjK | 0:127224866798 | 49 | inline void setDir(PinName p) { fiodir = (uint32_t)(p & ~0x1F) + 0x00; } |
AjK | 0:127224866798 | 50 | inline void setMask(PinName p) { fiomask = (uint32_t)(p & ~0x1F) + 0x10; } |
AjK | 0:127224866798 | 51 | inline void setPin(PinName p) { fiopin = (uint32_t)(p & ~0x1F) + 0x14; } |
AjK | 0:127224866798 | 52 | inline void setSet(PinName p) { fioset = (uint32_t)(p & ~0x1F) + 0x18; } |
AjK | 0:127224866798 | 53 | inline void setClr(PinName p) { fioclr = (uint32_t)(p & ~0x1F) + 0x1C; } |
AjK | 0:127224866798 | 54 | |
AjK | 0:127224866798 | 55 | inline void pinUp() { *((volatile uint32_t *)fioset) = mask; } |
AjK | 0:127224866798 | 56 | inline void pinDn() { *((volatile uint32_t *)fioclr) = mask; } |
AjK | 0:127224866798 | 57 | inline uint32_t pinIs() { return *((volatile uint32_t *)(fiopin)) & mask; } |
AjK | 0:127224866798 | 58 | |
AjK | 0:127224866798 | 59 | public: |
AjK | 0:127224866798 | 60 | |
AjK | 0:127224866798 | 61 | Lib17_DIO(PinName p, Direction d = Out, PinMode m = PullDown) { init(p, d, m); }; |
AjK | 0:127224866798 | 62 | |
AjK | 0:127224866798 | 63 | void write(int i) { if (i!=0) { pinUp(); } else { pinDn(); } } |
AjK | 0:127224866798 | 64 | int read(void) { return pinIs() ? 1 : 0; }; |
AjK | 0:127224866798 | 65 | void output(void) { *((volatile uint32_t *)fiodir) |= mask; } |
AjK | 0:127224866798 | 66 | void input(void) { *((volatile uint32_t *)fiodir) &= ~mask; } |
AjK | 0:127224866798 | 67 | |
AjK | 0:127224866798 | 68 | PinName getPin(void) { return pin; } |
AjK | 0:127224866798 | 69 | int getDirection(void) { return *((volatile uint32_t *)fiomask) & mask ? 1 : 0; } |
AjK | 0:127224866798 | 70 | |
AjK | 0:127224866798 | 71 | operator int() { return read(); } |
AjK | 0:127224866798 | 72 | Lib17_DIO& operator= (int value) { write(value); return *this; } |
AjK | 0:127224866798 | 73 | Lib17_DIO& operator= (Lib17_DIO& rhs) { write(rhs.read()); return *this; } |
AjK | 0:127224866798 | 74 | |
AjK | 0:127224866798 | 75 | uint32_t getMask(void) { return mask; } |
AjK | 0:127224866798 | 76 | uint32_t getFiodir(void) { return fiodir; } |
AjK | 0:127224866798 | 77 | uint32_t getFiomask(void) { return fiomask; } |
AjK | 0:127224866798 | 78 | uint32_t getFiopin(void) { return fiopin; } |
AjK | 0:127224866798 | 79 | uint32_t getFioset(void) { return fioset; } |
AjK | 0:127224866798 | 80 | uint32_t getFioclr(void) { return fioclr; } |
AjK | 0:127224866798 | 81 | |
AjK | 0:127224866798 | 82 | |
AjK | 0:127224866798 | 83 | protected: |
AjK | 0:127224866798 | 84 | void init(PinName p, Direction d, PinMode m) |
AjK | 0:127224866798 | 85 | { |
AjK | 0:127224866798 | 86 | // We rely on the fact that by default the LPC1768 |
AjK | 0:127224866798 | 87 | // sets all pins to be GPIO. The user will change |
AjK | 0:127224866798 | 88 | // that if they need to. So we don't bother trying |
AjK | 0:127224866798 | 89 | // to setup PINSELx |
AjK | 0:127224866798 | 90 | |
AjK | 0:127224866798 | 91 | // psel(); // Not used, see above. |
AjK | 0:127224866798 | 92 | |
AjK | 0:127224866798 | 93 | setpin(p); |
AjK | 0:127224866798 | 94 | setmask(p); |
AjK | 0:127224866798 | 95 | setDir(p); |
AjK | 0:127224866798 | 96 | setMask(p); |
AjK | 0:127224866798 | 97 | setPin(p); |
AjK | 0:127224866798 | 98 | setSet(p); |
AjK | 0:127224866798 | 99 | setClr(p); |
AjK | 0:127224866798 | 100 | |
AjK | 0:127224866798 | 101 | if (d == Out) output(); |
AjK | 0:127224866798 | 102 | else mode( m ); |
AjK | 0:127224866798 | 103 | } |
AjK | 0:127224866798 | 104 | |
AjK | 0:127224866798 | 105 | protected: |
AjK | 0:127224866798 | 106 | void psel(void) |
AjK | 0:127224866798 | 107 | { |
AjK | 0:127224866798 | 108 | uint32_t ppsel, pumask; |
AjK | 0:127224866798 | 109 | |
AjK | 0:127224866798 | 110 | if (pin >= P0_0 && pin <= P0_15) ppsel = (uint32_t)(&LPC_PINCON->PINSEL0); |
AjK | 0:127224866798 | 111 | else if (pin >= P0_16 && pin <= P0_31) ppsel = (uint32_t)(&LPC_PINCON->PINSEL1); |
AjK | 0:127224866798 | 112 | else if (pin >= P1_0 && pin <= P1_15) ppsel = (uint32_t)(&LPC_PINCON->PINSEL2); |
AjK | 0:127224866798 | 113 | else if (pin >= P1_16 && pin <= P1_31) ppsel = (uint32_t)(&LPC_PINCON->PINSEL3); |
AjK | 0:127224866798 | 114 | else if (pin >= P2_0 && pin <= P2_15) ppsel = (uint32_t)(&LPC_PINCON->PINSEL4); |
AjK | 0:127224866798 | 115 | else if (pin >= P3_16 && pin <= P3_31) ppsel = (uint32_t)(&LPC_PINCON->PINSEL7); |
AjK | 0:127224866798 | 116 | else if (pin >= P4_16 && pin <= P4_31) ppsel = (uint32_t)(&LPC_PINCON->PINSEL9); |
AjK | 0:127224866798 | 117 | else return; |
AjK | 0:127224866798 | 118 | |
AjK | 0:127224866798 | 119 | pumask = ~(3UL << ((pin & 0x1F)>>1)); |
AjK | 0:127224866798 | 120 | *((volatile uint32_t *)ppsel) &= pumask; |
AjK | 0:127224866798 | 121 | } |
AjK | 0:127224866798 | 122 | |
AjK | 0:127224866798 | 123 | public: |
AjK | 0:127224866798 | 124 | void mode(PinMode m) |
AjK | 0:127224866798 | 125 | { |
AjK | 0:127224866798 | 126 | uint32_t ppmod, pumask; |
AjK | 0:127224866798 | 127 | |
AjK | 0:127224866798 | 128 | if (m == OpenDrain) { |
AjK | 0:127224866798 | 129 | openDrain(1); |
AjK | 0:127224866798 | 130 | } |
AjK | 0:127224866798 | 131 | else { |
AjK | 0:127224866798 | 132 | if (pin >= P0_0 && pin <= P0_15) { |
AjK | 0:127224866798 | 133 | ppmod = (uint32_t)(&LPC_PINCON->PINMODE0); |
AjK | 0:127224866798 | 134 | pumask = ((m & 0x3) << ( ((pin & 0x1F)-0)*2) ); |
AjK | 0:127224866798 | 135 | } |
AjK | 0:127224866798 | 136 | else if (pin >= P0_16 && pin <= P0_31) { |
AjK | 0:127224866798 | 137 | ppmod = (uint32_t)(&LPC_PINCON->PINMODE1); |
AjK | 0:127224866798 | 138 | pumask = ((m & 0x3) << ( ((pin & 0x1F)-16)*2) ); |
AjK | 0:127224866798 | 139 | } |
AjK | 0:127224866798 | 140 | else if (pin >= P1_0 && pin <= P1_15) { |
AjK | 0:127224866798 | 141 | ppmod = (uint32_t)(&LPC_PINCON->PINMODE2); |
AjK | 0:127224866798 | 142 | pumask = ((m & 0x3) << ( ((pin & 0x1F)-0)*2) ); |
AjK | 0:127224866798 | 143 | } |
AjK | 0:127224866798 | 144 | else if (pin >= P1_16 && pin <= P1_31) { |
AjK | 0:127224866798 | 145 | ppmod = (uint32_t)(&LPC_PINCON->PINMODE3); |
AjK | 0:127224866798 | 146 | pumask = ((m & 0x3) << ( ((pin & 0x1F)-16)*2) ); |
AjK | 0:127224866798 | 147 | } |
AjK | 0:127224866798 | 148 | else if (pin >= P2_0 && pin <= P2_15) { |
AjK | 0:127224866798 | 149 | ppmod = (uint32_t)(&LPC_PINCON->PINMODE4); |
AjK | 0:127224866798 | 150 | pumask = ((m & 0x3) << ( ((pin & 0x1F)-0)*2) ); |
AjK | 0:127224866798 | 151 | } |
AjK | 0:127224866798 | 152 | else if (pin >= P3_16 && pin <= P3_31) { |
AjK | 0:127224866798 | 153 | ppmod = (uint32_t)(&LPC_PINCON->PINMODE7); |
AjK | 0:127224866798 | 154 | pumask = ((m & 0x3) << ( ((pin & 0x1F)-16)*2) ); |
AjK | 0:127224866798 | 155 | } |
AjK | 0:127224866798 | 156 | else if (pin >= P4_16 && pin <= P4_31) { |
AjK | 0:127224866798 | 157 | ppmod = (uint32_t)(&LPC_PINCON->PINMODE9); |
AjK | 0:127224866798 | 158 | pumask = ((m & 0x3) << ( ((pin & 0x1F)-16)*2) ); |
AjK | 0:127224866798 | 159 | } |
AjK | 0:127224866798 | 160 | else return; |
AjK | 0:127224866798 | 161 | |
AjK | 0:127224866798 | 162 | *((volatile uint32_t *)ppmod) |= pumask; |
AjK | 0:127224866798 | 163 | } |
AjK | 0:127224866798 | 164 | } |
AjK | 0:127224866798 | 165 | |
AjK | 0:127224866798 | 166 | public: |
AjK | 0:127224866798 | 167 | void openDrain(int i = 1) |
AjK | 0:127224866798 | 168 | { |
AjK | 0:127224866798 | 169 | if (pin >= P0_0 && pin <= P0_31) { if (i) LPC_PINCON->PINMODE_OD0 |= mask; else LPC_PINCON->PINMODE_OD0 &= ~mask; } |
AjK | 0:127224866798 | 170 | else if (pin >= P1_0 && pin <= P1_31) { if (i) LPC_PINCON->PINMODE_OD1 |= mask; else LPC_PINCON->PINMODE_OD1 &= ~mask; } |
AjK | 0:127224866798 | 171 | else if (pin >= P2_0 && pin <= P2_31) { if (i) LPC_PINCON->PINMODE_OD2 |= mask; else LPC_PINCON->PINMODE_OD2 &= ~mask; } |
AjK | 0:127224866798 | 172 | else if (pin >= P3_16 && pin <= P3_31) { if (i) LPC_PINCON->PINMODE_OD3 |= mask; else LPC_PINCON->PINMODE_OD3 &= ~mask; } |
AjK | 0:127224866798 | 173 | else if (pin >= P4_16 && pin <= P4_31) { if (i) LPC_PINCON->PINMODE_OD4 |= mask; else LPC_PINCON->PINMODE_OD4 &= ~mask; } |
AjK | 0:127224866798 | 174 | } |
AjK | 0:127224866798 | 175 | |
AjK | 0:127224866798 | 176 | }; |
AjK | 0:127224866798 | 177 | |
AjK | 0:127224866798 | 178 | }; /* namespace AjK ends. */ |
AjK | 0:127224866798 | 179 | |
AjK | 0:127224866798 | 180 | using namespace AjK; |
AjK | 0:127224866798 | 181 | |
AjK | 0:127224866798 | 182 | #endif /* AJK_Lib17_DIO_H */ |