flinklib
flinklib: flink C library for Linux
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dio.c
Go to the documentation of this file.
1 /*******************************************************************
2  * _________ _____ _____ ____ _____ ___ ____ *
3  * |_ ___ | |_ _| |_ _| |_ \|_ _| |_ ||_ _| *
4  * | |_ \_| | | | | | \ | | | |_/ / *
5  * | _| | | _ | | | |\ \| | | __'. *
6  * _| |_ _| |__/ | _| |_ _| |_\ |_ _| | \ \_ *
7  * |_____| |________| |_____| |_____|\____| |____||____| *
8  * *
9  *******************************************************************
10  * *
11  * flink userspace library, subdevice function "digital I/O" *
12  * *
13  *******************************************************************/
14 
24 #include "flinklib.h"
25 #include "types.h"
26 #include "error.h"
27 #include "log.h"
28 
29 #include <stdint.h>
30 
38 int flink_dio_set_direction(flink_subdev* subdev, uint32_t channel, uint8_t output) {
39  uint32_t offset;
40  uint8_t bit;
41 
42  dbg_print("Setting digital I/O direction for channel %d on subdevice %d\n", channel, subdev->id);
43 
44  offset = HEADER_SIZE + SUBHEADER_SIZE + channel / (REGISTER_WITH * 8);
45  bit = channel % (REGISTER_WITH * 8);
46 
47  dbg_print(" --> calculated offset is 0x%x\n", offset);
48  dbg_print(" --> calculated bit is %u\n", bit);
49 
50  if(flink_write_bit(subdev, offset, bit, &output)) {
51  libc_error();
52  return EXIT_ERROR;
53  }
54  return EXIT_SUCCESS;
55 }
56 
64 int flink_dio_set_value(flink_subdev* subdev, uint32_t channel, uint8_t value) {
65  uint32_t offset;
66  uint8_t bit;
67  uint8_t val = (value != 0);
68 
69  dbg_print("Setting digital output value to %u...\n", val);
70 
71  offset = HEADER_SIZE + SUBHEADER_SIZE + subdev->nof_channels / (REGISTER_WITH * 8) + channel / (REGISTER_WITH * 8);
72  if(subdev->nof_channels % (REGISTER_WITH * 8) != 0) offset += 4;
73  bit = channel % (REGISTER_WITH * 8);
74 
75  dbg_print(" --> calculated offset is 0x%x\n", offset);
76  dbg_print(" --> calculated bit is %u\n", bit);
77 
78  if(flink_write_bit(subdev, offset, bit, &val)) {
79  libc_error();
80  return EXIT_ERROR;
81  }
82  return EXIT_SUCCESS;
83 }
84 
92 int flink_dio_get_value(flink_subdev* subdev, uint32_t channel, uint8_t* value) {
93  uint32_t offset;
94  uint8_t bit;
95 
96  dbg_print("Reading digital input value from channel %d on subdevice %d\n", channel, subdev->id);
97 
98  offset = HEADER_SIZE + SUBHEADER_SIZE + subdev->nof_channels / (REGISTER_WITH * 8) + channel / (REGISTER_WITH * 8);
99  if(subdev->nof_channels % (REGISTER_WITH * 8) != 0) offset += 4;
100  bit = channel % (REGISTER_WITH * 8);
101 
102  dbg_print("[DEBUG] --> calculated offset is 0x%x\n", offset);
103  dbg_print("[DEBUG] --> calculated bit is %u\n", bit);
104 
105  if(flink_read_bit(subdev, offset, bit, value)) {
106  libc_error();
107  return EXIT_ERROR;
108  }
109  return EXIT_SUCCESS;
110 }
int flink_dio_get_value(flink_subdev *subdev, uint32_t channel, uint8_t *value)
Reads an input channel.
Definition: dio.c:92
Data structures for flink devices and subdevices.
int flink_dio_set_value(flink_subdev *subdev, uint32_t channel, uint8_t value)
Sets a output channel to a value.
Definition: dio.c:64
int flink_dio_set_direction(flink_subdev *subdev, uint32_t channel, uint8_t output)
Configures a channel as input or output.
Definition: dio.c:38
#define EXIT_ERROR
Definition: flinklib.h:118
Error handling.
#define REGISTER_WITH
Definition: flinklib.h:48
#define HEADER_SIZE
Definition: flinklib.h:49
int flink_write_bit(flink_subdev *subdev, uint32_t offset, uint8_t bit, void *wdata)
Write a single bit to a flink subdevice.
Definition: lowlevel.c:189
#define EXIT_SUCCESS
Definition: flinklib.h:117
int flink_read_bit(flink_subdev *subdev, uint32_t offset, uint8_t bit, void *rdata)
Read a single bit from a flink subdevice.
Definition: lowlevel.c:151
#define SUBHEADER_SIZE
Definition: flinklib.h:50
void libc_error(void)
Definition: error.c:56
Debug utilities.
#define dbg_print(fmt,...)
Definition: log.h:29