ccNOos  v0.0.0
Build Portable Microcontroller Applications!
LTC2360.c
Go to the documentation of this file.
1 /** \file LTC2360.c
2 * \brief <a href="https://www.inmechasol.org/" target="_blank">IMS</a>:
3  <a href="https://github.com/InMechaSol/ccNOos" target="_blank">ccNOos</a>,
4  Implementation for straight C
5 
6  Copyright 2021 <a href="https://www.inmechasol.org/" target="_blank">InMechaSol, Inc</a>
7 
8  Licensed under the Apache License, Version 2.0(the "License");
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11 
12  http://www.apache.org/licenses/LICENSE-2.0
13 
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19 
20 Notes:
21  (.c includes .h) - for straight C or
22  (.cpp includes .c which includes .h) - for C++ wrapped straight C
23  *Always compiled to a single compilation unit, either C or CPP, not both
24 
25 */
26 
27 #include "LTC2360.h"
28 
29 
30 // Data initialization function
32 {
33  struct LTC2360DataStruct outStruct;
34  outStruct.BandwidthMHz = 0;
35  outStruct.PowerMeasuredinBanddB = 0;
36  outStruct.current_value = 0;
37  outStruct.value_good = ui8FALSE;
38  return outStruct;
39 }
40 
41 //
42 ////#include "spi_class.h"
43 //
44 //#include <stdio.h>
45 //#include <string.h>
46 //#include <math.h>
47 //#include <unistd.h>
48 //#include <fcntl.h>
49 //#include <sys/ioctl.h>
50 //#include <cstdlib>
51 //#include <linux/types.h>
52 //#include <linux/spi/spidev.h>
53 //#include <time.h>
54 //
55 //
56 //// TODO: Collect into SPI class
57 //static int spi_fd, gpio_fd;
58 //static const char *gpio_number = "49";
59 //static const char *spi_device = "/dev/spidev1.1";
60 //static const char *gpio_device = "/sys/class/gpio/gpio49/";
61 //static const char *gpio_direction = "direction";
62 //static const char *gpio_value = "value";
63 //static uint32_t mode = 0;
64 //static uint8_t bits = 12;
65 //static uint32_t sck_speed = 100000;
66 //static uint16_t delay = 0;
67 //
68 ////LTC2360class::LTC2360class(struct LTC2360DataStruct* LTC2360DataPtrIn) : spi_class()
69 //LTC2360class::LTC2360class(struct LTC2360DataStruct* LTC2360DataPtrIn)
70 //{
71 // LTC2360DataPtr = LTC2360DataPtrIn;
72 // if (LTC2360DataPtr)
73 // {
74 // LTC2360DataPtr->value_good = false;
75 // }
76 //}
77 //
78 //void LTC2360class::Setup()
79 //{
80 // ;
81 //}
82 //
83 //void LTC2360class::InitSPI()
84 //{
85 // ///////////////////////////////
86 // ///////////////////////////////
87 // //
88 // // Set up SPI BUS if necessary
89 // // a) set up gpio chip select
90 // // b) open spi device
91 // //
92 // ///////////////////////////////
93 // ///////////////////////////////
94 //
95 // //
96 // // If "/sys/class/gpio/gpio49/value" does not exist, export gpio49
97 // if (-1 == access (gpio_device, F_OK ))
98 // {
99 // gpio_fd = open ("/sys/class/gpio/export", O_WRONLY);
100 // if (gpio_fd < 0)
101 // {
102 // //ERROR - Do something intelligent
103 // //printf ("Unable to open 'export' file\n");
104 // }
105 // write (gpio_fd, gpio_number, strlen(gpio_number));
106 // close (gpio_fd);
107 // //TODO: Do we need to pause a bit???
108 // }
109 //
110 // //
111 // // If gpio is not set to 'output', change it
112 // char *direction_file = (char *)malloc(strlen(gpio_device) + strlen(gpio_direction));
113 // strcpy (direction_file, gpio_device);
114 // strcat (direction_file, gpio_direction);
115 // gpio_fd = open (direction_file, O_RDWR);
116 // free (direction_file);
117 //
118 // char direction[4];
119 // read (gpio_fd, direction, sizeof(direction));
120 // if (memcmp("out", direction, 3))
121 // {
122 // write (gpio_fd, "out", 3);
123 // }
124 // close (gpio_fd);
125 //
126 // //
127 // // If gpio chip select is not set to 'inactive', set it to 'inactive'.
128 // char *value_file = (char *)malloc(strlen(gpio_device) + strlen(gpio_direction));
129 // strcpy (value_file, gpio_device);
130 // strcat (value_file, gpio_value);
131 // gpio_fd = open (value_file, O_RDWR);
132 // free (value_file);
133 // char value = '1';
134 // write (gpio_fd, &value, 1);
135 // close (gpio_fd);
136 //
137 //
138 // //
139 // // Open SPI device and set properties
140 // spi_fd = open(spi_device, O_RDWR);
141 // if (spi_fd < 0)
142 // {
143 // // TODO: ERROR- pabort("can't open spi device");
144 // }
145 //
146 // int ret = ioctl(spi_fd, SPI_IOC_WR_MODE32, &mode);
147 // if (ret == -1)
148 // {
149 // //TODO: ERROR- pabort("can't set spi mode");
150 // }
151 //
152 // ret = ioctl(spi_fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
153 // if (ret == -1)
154 // {
155 // //TODO: ERROR- pabort("can't set bits per word");
156 // }
157 //
158 // ret = ioctl(spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &sck_speed);
159 // if (ret == -1)
160 // {
161 // //TODO: ERROR- pabort("can't set max speed hz");
162 // }
163 // close (spi_fd);
164 //} //End set up SPI if necessary
165 //
166 //uint16_t LTC2360class::ReadValue(int num_of_averages)
167 //{
168 // int spi_fd, gpio_fd;
169 // int average = 0;
170 // uint16_t tx, rx;
171 // int ret = 0;
172 // struct timespec req;
173 //
174 // LTC2360DataPtr->value_good = false;
175 //
176 // InitSPI();
177 //
178 // // First, open necessary files
179 // // a) gpio output
180 // char *value_file = (char *)malloc(strlen(gpio_device) + strlen(gpio_direction));
181 // strcpy (value_file, gpio_device);
182 // strcat (value_file, gpio_value);
183 // gpio_fd = open (value_file, O_RDWR);
184 // free (value_file);
185 //
186 // // b) spi device
187 // spi_fd = open(spi_device, O_RDWR);
188 // if (spi_fd < 0)
189 // {
190 // // TODO: ERROR- pabort("can't open spi device");
191 // }
192 //
193 // for (int i = 0; i < num_of_averages; i++)
194 // {
195 //
196 // // Start Conversion read cycle
197 // char value = '0';
198 // write (gpio_fd, &value, 1); // Enable chip select
199 //
200 // rx = tx = 0;
201 // struct spi_ioc_transfer tr = {
202 // .tx_buf = (unsigned long)&tx,
203 // .rx_buf = (unsigned long)&rx,
204 // .len = 2,
205 // .speed_hz = sck_speed,
206 // .delay_usecs = delay,
207 // .bits_per_word = bits,
208 // };
209 // ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &tr);
210 // if (ret < 1)
211 // {
212 // //TODO: ERROR- pabort("Error sending SPI message");
213 // }
214 //
215 // average += rx;
216 //
217 // value = '1';
218 // write (gpio_fd, &value, 1); // Deselect chip select
219 //
220 // // LTC2360 conversion time = 8uS min.
221 // req.tv_nsec = 8000;
222 // req.tv_sec = 0;
223 // clock_nanosleep(CLOCK_MONOTONIC, 0, &req, NULL);
224 // }
225 //
226 //
227 // close (spi_fd);
228 // close (gpio_fd);
229 //
230 // if (LTC2360DataPtr)
231 // {
232 // LTC2360DataPtr->current_value = average / num_of_averages;
233 // LTC2360DataPtr->value_good = true;
234 // }
235 // return (rx);
236 //}
237 //
238 //
LTC2360DataStruct::BandwidthMHz
float BandwidthMHz
Definition: LTC2360.h:35
LTC2360DataStruct
Definition: LTC2360.h:34
LTC2360.h
IMS: ccNOos, Declarations for straight C and C++
ui8FALSE
#define ui8FALSE
Definition: version_config.h:190
CreateLTC2360Data
struct LTC2360DataStruct CreateLTC2360Data()
Definition: LTC2360.c:31
LTC2360DataStruct::value_good
UI_8 value_good
Definition: LTC2360.h:38
LTC2360DataStruct::current_value
I_16 current_value
Definition: LTC2360.h:37
LTC2360DataStruct::PowerMeasuredinBanddB
float PowerMeasuredinBanddB
Definition: LTC2360.h:36