flinklib
flinklib: flink C library for Linux
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)) * REGISTER_WITH;
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;
72  offset += ((subdev->nof_channels - 1) / (REGISTER_WITH * 8) + 1) * REGISTER_WITH;
73  offset += (channel / (REGISTER_WITH * 8)) * REGISTER_WITH;
74  bit = channel % (REGISTER_WITH * 8);
75 
76  dbg_print(" --> calculated offset is 0x%x\n", offset);
77  dbg_print(" --> calculated bit is %u\n", bit);
78 
79  if(flink_write_bit(subdev, offset, bit, &val)) {
80  libc_error();
81  return EXIT_ERROR;
82  }
83  return EXIT_SUCCESS;
84 }
85 
93 int flink_dio_get_value(flink_subdev* subdev, uint32_t channel, uint8_t* value) {
94  uint32_t offset;
95  uint8_t bit;
96 
97  dbg_print("Reading digital input value from channel %d on subdevice %d\n", channel, subdev->id);
98 
99  offset = HEADER_SIZE + SUBHEADER_SIZE;
100  offset += ((subdev->nof_channels - 1) / (REGISTER_WITH * 8) + 1) * REGISTER_WITH;
101  offset += (channel / (REGISTER_WITH * 8)) * REGISTER_WITH;
102  bit = channel % (REGISTER_WITH * 8);
103 
104  dbg_print("[DEBUG] --> calculated offset is 0x%x\n", offset);
105  dbg_print("[DEBUG] --> calculated bit is %u\n", bit);
106 
107  if(flink_read_bit(subdev, offset, bit, value)) {
108  libc_error();
109  return EXIT_ERROR;
110  }
111  return EXIT_SUCCESS;
112 }
int flink_dio_get_value(flink_subdev *subdev, uint32_t channel, uint8_t *value)
Reads an input channel.
Definition: dio.c:93
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:129
Error handling.
#define REGISTER_WITH
Definition: flinklib.h:50
#define HEADER_SIZE
Definition: flinklib.h:51
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:187
#define EXIT_SUCCESS
Definition: flinklib.h:128
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:149
#define SUBHEADER_SIZE
Definition: flinklib.h:52
void libc_error(void)
Definition: error.c:56
Debug utilities.
#define dbg_print(fmt,...)
Definition: log.h:29