Debian Codegraph @

microhope/microhope-doc/prog/pmdk_digital.c

raw
1 /* digital.c -- routines for manipulating the I/O pins.
2
3 Copyright (C) 2008 Ajith Kumar, Inter-University Accelerator Centre,
4 New Delhi and Pramode C.E, GnuVision.com.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10 */
11
12 #include <avr/io.h>
13
14 struct port_pin {
15 volatile uint8_t* port;
16 volatile uint8_t* input_reg;
17 volatile uint8_t* dir_reg;
18 uint8_t pin;
19 };
20
21 static struct port_pin pmap[] = {{&PORTB, &PINB, &DDRB,0}, {&PORTD, &PIND, &DDRD, 0},
22 {&PORTC, &PINC, &DDRC, 0}};
23 /*
24 * The following functions act on pin numbers 0 ... 20 on the
25 * Phoenix-MDK board. These pins are mapped to the
26 * corresponding ports by the `translate' function.
27 */
28
29 /*
30 * Pins belonging to Ports B,C and D are marked with serial numbers ranging
31 * from 0 to 20 on the PCB. This routine translates those numbers into port
32 * address and the actual pin number. Used for implementing simpler functions.
33 */
34
35 static struct port_pin* translate(uint8_t pin)
36 {
37 if (pin <= 4) { // PB0 to PB4 are labelled from 0 to 4
38 pmap[0].pin = pin;
39 return pmap;
40 } else if (pin <= 12) { // PD0 to PD7 are labelled from 5 to 12
41 pmap[1].pin = pin - 5;
42 return pmap + 1;
43 } else if (pin <= 20) { // PC0 to PC7 are labelled from 13 to 20
44 pmap[2].pin = pin - 13;
45 return pmap + 2;
46 } else {
47 // Invalid pin number will select the LSB of port B.
48 return pmap;
49 }
50 }
51
52 /*
53 * Set direction of pin to `input' or `output'
54 */
55
56 void make_pin_input(uint8_t pin)
57 {
58 struct port_pin *t = translate(pin);
59 *(t->dir_reg) &= ~(1 << t->pin);
60 }
61
62 void make_pin_output(uint8_t pin)
63 {
64 struct port_pin *t = translate(pin);
65 *(t->dir_reg) |= (1 << t->pin);
66 }
67
68 /*
69 * For a PIN configured as input, setting/clearing the corresponding bit
70 * of PORTX will enable/disable the corresponding pullup resistor.
71 */
72
73 void enable_pullup(uint8_t pin)
74 {
75 struct port_pin *t = translate(pin);
76 if(!(((*(t->dir_reg)) >> t->pin) & 1))
77 *(t->port) |= (1 << t->pin);
78 }
79
80 void disable_pullup(uint8_t pin)
81 {
82 struct port_pin *t = translate(pin);
83 if(!(((*(t->dir_reg)) >> t->pin) & 1))
84 *(t->port) &= ~(1 << t->pin);
85 }
86
87 void set_high(uint8_t pin)
88 {
89 struct port_pin *t = translate(pin);
90 *(t->port) |= (1 << t->pin);
91 }
92
93 void set_low(uint8_t pin)
94 {
95 struct port_pin *t = translate(pin);
96 *(t->port) &= ~(1 << t->pin);
97 }
98
99 uint8_t read_pin(uint8_t pin)
100 {
101 struct port_pin *t = translate(pin);
102 return ((*(t->input_reg)) >> (t->pin)) & 1;
103 }
104