libpic170x  0.2
Ease of use library for PIC16(L)1705/1709 chips
io_control.c
1 /*
2  Copyright 2018 Paul Konstantin Gerke
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16 
17 #include "libpic170x/io_control.h"
18 
19 #include <xc.h>
20 
21 #include <string.h>
22 
23 #if defined(PIC16F1705) || defined(PIC16LF1705)
24 const PinDef c_PIN_RA0 = {0b00000000, &RA0PPS, 0b00000001, &TRISA, &PORTA, &ANSELA, &LATA};
25 const PinDef c_PIN_RA1 = {0b00000001, &RA1PPS, 0b00000010, &TRISA, &PORTA, &ANSELA, &LATA};
26 const PinDef c_PIN_RA2 = {0b00000010, &RA2PPS, 0b00000100, &TRISA, &PORTA, &ANSELA, &LATA};
27 const PinDef c_PIN_RA3 = {0b00000011, NULL, 0b00001000, NULL, &PORTA, NULL, NULL};
28 const PinDef c_PIN_RA4 = {0b00000100, &RA4PPS, 0b00010000, &TRISA, &PORTA, &ANSELA, &LATA};
29 const PinDef c_PIN_RA5 = {0b00000101, &RA5PPS, 0b00100000, &TRISA, &PORTA, NULL, &LATA};
30 
31 const PinDef c_PIN_RC0 = {0b00010000, &RC0PPS, 0b00000001, &TRISC, &PORTC, &ANSELC, &LATC};
32 const PinDef c_PIN_RC1 = {0b00010001, &RC1PPS, 0b00000010, &TRISC, &PORTC, &ANSELC, &LATC};
33 const PinDef c_PIN_RC2 = {0b00010010, &RC2PPS, 0b00000100, &TRISC, &PORTC, &ANSELC, &LATC};
34 const PinDef c_PIN_RC3 = {0b00010011, &RC3PPS, 0b00001000, &TRISC, &PORTC, &ANSELC, &LATC};
35 const PinDef c_PIN_RC4 = {0b00010100, &RC4PPS, 0b00010000, &TRISC, &PORTC, NULL, &LATC};
36 const PinDef c_PIN_RC5 = {0b00010101, &RC5PPS, 0b00100000, &TRISC, &PORTC, NULL, &LATC};
37 
38 
39 const PinDef* PIN_RA0 = &c_PIN_RA0;
40 const PinDef* PIN_RA1 = &c_PIN_RA1;
41 const PinDef* PIN_RA2 = &c_PIN_RA2;
42 const PinDef* PIN_RA3 = &c_PIN_RA3;
43 const PinDef* PIN_RA4 = &c_PIN_RA4;
44 const PinDef* PIN_RA5 = &c_PIN_RA5;
45 
46 const PinDef* PIN_RB4 = NULL;
47 const PinDef* PIN_RB5 = NULL;
48 const PinDef* PIN_RB6 = NULL;
49 const PinDef* PIN_RB7 = NULL;
50 
51 const PinDef* PIN_RC0 = &c_PIN_RC0;
52 const PinDef* PIN_RC1 = &c_PIN_RC1;
53 const PinDef* PIN_RC2 = &c_PIN_RC2;
54 const PinDef* PIN_RC3 = &c_PIN_RC3;
55 const PinDef* PIN_RC4 = &c_PIN_RC4;
56 const PinDef* PIN_RC5 = &c_PIN_RC5;
57 const PinDef* PIN_RC6 = NULL;
58 const PinDef* PIN_RC7 = NULL;
59 #elif defined(PIC16F1709) || defined(PIC16LF1709)
60 const PinDef c_PIN_RA0 = {0b00000000, &RA0PPS, 0b00000001, &TRISA, &PORTA, &ANSELA, &LATA};
61 const PinDef c_PIN_RA1 = {0b00000001, &RA1PPS, 0b00000010, &TRISA, &PORTA, &ANSELA, &LATA};
62 const PinDef c_PIN_RA2 = {0b00000010, &RA2PPS, 0b00000100, &TRISA, &PORTA, &ANSELA, &LATA};
63 const PinDef c_PIN_RA3 = {0b00000011, NULL, 0b00001000, NULL, &PORTA, NULL, NULL};
64 const PinDef c_PIN_RA4 = {0b00000100, &RA4PPS, 0b00010000, &TRISA, &PORTA, &ANSELA, &LATA};
65 const PinDef c_PIN_RA5 = {0b00000101, &RA5PPS, 0b00100000, &TRISA, &PORTA, NULL, &LATA};
66 
67 const PinDef c_PIN_RB4 = {0b00001100, &RB4PPS, 0b00010000, &TRISB, &PORTB, &ANSELB, &LATB};
68 const PinDef c_PIN_RB5 = {0b00001101, &RB5PPS, 0b00100000, &TRISB, &PORTB, &ANSELB, &LATB};
69 const PinDef c_PIN_RB6 = {0b00001110, &RB6PPS, 0b01000000, &TRISB, &PORTB, NULL, &LATB};
70 const PinDef c_PIN_RB7 = {0b00001111, &RB7PPS, 0b10000000, &TRISB, &PORTB, NULL, &LATB};
71 
72 const PinDef c_PIN_RC0 = {0b00010000, &RC0PPS, 0b00000001, &TRISC, &PORTC, &ANSELC, &LATC};
73 const PinDef c_PIN_RC1 = {0b00010001, &RC1PPS, 0b00000010, &TRISC, &PORTC, &ANSELC, &LATC};
74 const PinDef c_PIN_RC2 = {0b00010010, &RC2PPS, 0b00000100, &TRISC, &PORTC, &ANSELC, &LATC};
75 const PinDef c_PIN_RC3 = {0b00010011, &RC3PPS, 0b00001000, &TRISC, &PORTC, &ANSELC, &LATC};
76 const PinDef c_PIN_RC4 = {0b00010100, &RC4PPS, 0b00010000, &TRISC, &PORTC, NULL, &LATC};
77 const PinDef c_PIN_RC5 = {0b00010101, &RC5PPS, 0b00100000, &TRISC, &PORTC, NULL, &LATC};
78 const PinDef c_PIN_RC6 = {0b00010110, &RC6PPS, 0b01000000, &TRISC, &PORTC, &ANSELC, &LATC};
79 const PinDef c_PIN_RC7 = {0b00010111, &RC7PPS, 0b10000000, &TRISC, &PORTC, &ANSELC, &LATC};
80 
81 
82 const PinDef* PIN_RA0 = &c_PIN_RA0;
83 const PinDef* PIN_RA1 = &c_PIN_RA1;
84 const PinDef* PIN_RA2 = &c_PIN_RA2;
85 const PinDef* PIN_RA3 = &c_PIN_RA3;
86 const PinDef* PIN_RA4 = &c_PIN_RA4;
87 const PinDef* PIN_RA5 = &c_PIN_RA5;
88 
89 const PinDef* PIN_RB4 = &c_PIN_RB4;
90 const PinDef* PIN_RB5 = &c_PIN_RB5;
91 const PinDef* PIN_RB6 = &c_PIN_RB6;
92 const PinDef* PIN_RB7 = &c_PIN_RB7;
93 
94 const PinDef* PIN_RC0 = &c_PIN_RC0;
95 const PinDef* PIN_RC1 = &c_PIN_RC1;
96 const PinDef* PIN_RC2 = &c_PIN_RC2;
97 const PinDef* PIN_RC3 = &c_PIN_RC3;
98 const PinDef* PIN_RC4 = &c_PIN_RC4;
99 const PinDef* PIN_RC5 = &c_PIN_RC5;
100 const PinDef* PIN_RC6 = &c_PIN_RC6;
101 const PinDef* PIN_RC7 = &c_PIN_RC7;
102 #else
103 #error "No valid PIC chip defined"
104 #endif
105 
106 void pin_set_pin_mode(const PinDef* def, bool output) {
107  if (!def || !def->tris_reg) {
108  return;
109  }
110 
111  if (output) {
112  *(def->tris_reg) = (uint8_t) (*(def->tris_reg) & (~def->pin_tris_bitmask));
113  } else {
114  *(def->tris_reg) = (uint8_t) (*(def->tris_reg) | def->pin_tris_bitmask);
115  }
116 }
117 
118 bool pin_get_input(const PinDef* def) {
119  if (!def || (!def->port_reg)) {
120  return 0;
121  }
122  volatile unsigned char* state_reg =
123  (def->tris_reg && ((*def->tris_reg & def->pin_tris_bitmask) == 0))
124  ? def->latch_reg : def->port_reg;
125 
126  return (*state_reg & def->pin_tris_bitmask) != 0;
127 }
128 
129 void pin_set_output(const PinDef* def, bool on) {
130  if (!def || !def->latch_reg) {
131  return;
132  }
133 
134  if (on) {
135  *(def->latch_reg) = (uint8_t) (*(def->latch_reg) | def->pin_tris_bitmask);
136  } else {
137  *(def->latch_reg) = (uint8_t) (*(def->latch_reg) & (~def->pin_tris_bitmask));
138  }
139 }
140 
141 void pin_set_input_mode(const PinDef* def, uint8_t input_mode) {
142  if (!def || !def->ansel_reg) {
143  return;
144  }
145 
146  if (input_mode == PIN_INPUT_MODE_DIGITAL) {
147  *(def->ansel_reg) = (uint8_t) (*(def->ansel_reg) & (~def->pin_tris_bitmask));
148  } else {
149  *(def->ansel_reg) = (uint8_t) (*(def->ansel_reg) | def->pin_tris_bitmask);
150  }
151 }
152 
volatile unsigned char * tris_reg
Pointer to the pin&#39;s tris register (can be NULL)
Definition: io_control.h:84
const PinDef * PIN_RA0
PIN_RA0 defintion.
GPIO input/output library allowing to write to and read from IO pins.
const PinDef * PIN_RA5
PIN_RA5 defintion.
const PinDef * PIN_RC0
PIN_RC0 defintion.
const PinDef * PIN_RB5
PIN_RB5 defintion.
volatile unsigned char * port_reg
Pointer to the pin&#39;s port register (never NULL)
Definition: io_control.h:86
void pin_set_output(const PinDef *def, bool on)
Definition: io_control.c:129
const PinDef * PIN_RA2
PIN_RA2 defintion.
#define PIN_INPUT_MODE_DIGITAL
See pin_set_input_mode())
Definition: io_control.h:63
const PinDef * PIN_RC4
PIN_RC4 defintion.
Record to store a collection of registers and masks related to a pin.
Definition: io_control.h:75
volatile unsigned char * latch_reg
Pointer to the pin&#39;s output register (can be NULL)
Definition: io_control.h:90
const PinDef * PIN_RC1
PIN_RC1 defintion.
const PinDef * PIN_RC2
PIN_RC2 defintion.
const PinDef * PIN_RB6
PIN_RB6 defintion.
const PinDef * PIN_RC6
PIN_RC6 defintion.
void pin_set_pin_mode(const PinDef *def, bool output)
Definition: io_control.c:106
bool pin_get_input(const PinDef *def)
Definition: io_control.c:118
const PinDef * PIN_RB7
PIN_RB7 defintion.
const PinDef * PIN_RA4
PIN_RA4 defintion.
const PinDef * PIN_RA1
PIN_RA1 defintion.
const PinDef * PIN_RC5
PIN_RC5 defintion.
const PinDef * PIN_RA3
PIN_RA3 defintion.
void pin_set_input_mode(const PinDef *def, uint8_t input_mode)
Definition: io_control.c:141
const PinDef * PIN_RC7
PIN_RC7 defintion.
volatile unsigned char * ansel_reg
Pointer to the pin&#39;s analog selection register (can be NULL)
Definition: io_control.h:88
uint8_t pin_tris_bitmask
Mask for the pin to use with the referenced tris_reg.
Definition: io_control.h:81
const PinDef * PIN_RB4
PIN_RB4 defintion.
const PinDef * PIN_RC3
PIN_RC3 defintion.