Skip to content

Commit de9a7c8

Browse files
committed
Update to 2.1.0 and add support for PI-SPI-8AI-16B
1 parent 86fc38a commit de9a7c8

9 files changed

Lines changed: 141 additions & 2 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ install:
99

1010
clean:
1111
rm -rf bin
12-
rm -f overlays/*.dtbo
12+
rm -f overlays/*.dtbo

debian/changelog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
libwidgetlords (2.1.0) unstable; urgency=medium
2+
3+
* Add support for PI-SPI-8AI-16B
4+
5+
-- Kelsey Maes <kelsey@vpprocess.com> Mon, 18 Nov 2019 02:00:00 -0800
6+
17
libwidgetlords (2.0.0) unstable; urgency=medium
28

39
* Drop dependency on wiringpi in favor of using the character-device GPIO API.

include/pi_spi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@ void pi_spi_8di_init(uint8_t address, uint8_t type);
3535
void pi_spi_8ko_write(uint8_t data, uint8_t type);
3636
void pi_spi_8ko_write_single(uint8_t channel, uint8_t data, uint8_t type);
3737

38+
void pi_spi_8ai_16b_set_channel(uint8_t channel, uint8_t type);
39+
uint16_t pi_spi_8ai_16b_read(uint8_t type);
40+
3841
#endif

meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ source = [
1414
'src/pi_spi.c',
1515
'src/pi_spi_2ao.c',
1616
'src/pi_spi_8ai.c',
17+
'src/pi_spi_8ai_16b.c',
1718
'src/pi_spi_8di.c',
1819
'src/pi_spi_8ko.c',
1920
'src/pi_spi_din.c',
@@ -69,6 +70,7 @@ endif
6970

7071
executable('pi_spi_8ko_demo', 'src/demos/pi_spi_8ko_demo.c', dependencies: [dep])
7172
executable('pi_spi_8ai_demo', 'src/demos/pi_spi_8ai_demo.c', dependencies: [dep])
73+
executable('pi_spi_8ai_16b_demo', 'src/demos/pi_spi_8ai_16b_demo.c', dependencies: [dep])
7274
executable('pi_spi_8di_demo', 'src/demos/pi_spi_8di_demo.c', dependencies: [dep])
7375
executable('pi_spi_2ao_demo', 'src/demos/pi_spi_2ao_demo.c', dependencies: [dep])
7476

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from time import sleep
2+
from widgetlords.pi_spi import *
3+
4+
inputs = Mod8AI16B()
5+
while True:
6+
for i in range(8):
7+
inputs.set_channel(i)
8+
print(inputs.read())
9+
sleep(0.5)

python/widgetlords/pi_spi.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,15 @@ def __init__(self, optional: bool = False):
4444

4545
def write_single(self, channel: int, counts: int):
4646
widgetlords.pi_spi_2ao_write_single(channel, counts, int(self.optional))
47-
__all__.append('Mod2AO')
47+
__all__.append('Mod2AO')
48+
49+
class Mod8AI16B:
50+
def __init__(self, spare: int = 0):
51+
self.spare = int(spare) % 4
52+
53+
def set_channel(self, channel: int):
54+
widgetlords.pi_spi_8ai_16b_set_channel(channel)
55+
56+
def read(self):
57+
return widgetlords.pi_spi_8ai_16b_read(self.spare)
58+
__all__.append('Mod8AI16B')

src/demos/pi_spi_4freq_demo.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <widgetlords.h>
4+
5+
int main(void)
6+
{
7+
while(1)
8+
{
9+
printf("Channel\tFixed\tVariable\tPulse\n");
10+
for(uint8_t i = 0; i < 4; ++i)
11+
{
12+
uint32_t fixed = pi_spi_4freq_read_fixed(CE0, 0, i);
13+
uint32_t variable = pi_spi_4freq_read_variable(CE0, 0, i);
14+
uint32_t pulse = pi_spi_4freq_read_pulse(CE0, 0, i);
15+
// printf("CH%i: ")
16+
printf("%i\t%i\t%i\t%i\n", i, fixed, variable, pulse);
17+
}
18+
uint8_t di = pi_spi_4freq_read_di(CE0, 0);
19+
printf("%i\n\n", di);
20+
usleep(500000);
21+
}
22+
23+
return 0;
24+
}

src/demos/pi_spi_8ai_16b_demo.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <widgetlords.h>
4+
5+
int main(void)
6+
{
7+
while(1)
8+
{
9+
printf("\nCH 1\tCH 2\tCH 3\tCH 4\tCH 5\tCH 6\tCH 7\tCH 8\n");
10+
for(int i = 0; i < 8; ++i)
11+
{
12+
pi_spi_8ai_16b_set_channel(i, DEFAULT);
13+
printf("%i\t", pi_spi_8ai_16b_read(DEFAULT));
14+
}
15+
usleep(500000);
16+
}
17+
18+
return 0;
19+
}

src/pi_spi_8ai_16b.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <pi_spi.h>
2+
#include <spi.h>
3+
4+
#define MAX_SPI_FREQ 100000
5+
6+
uint8_t _channels[4] = { 0, 0, 0, 0 };
7+
8+
int8_t _open(uint8_t type)
9+
{
10+
if(type == DEFAULT)
11+
{
12+
spi_open(SPI_8AI, MAX_SPI_FREQ);
13+
}
14+
else if(type == SPARE1)
15+
{
16+
spi_open(SPI_SPARE1, MAX_SPI_FREQ);
17+
}
18+
else if(type == SPARE2)
19+
{
20+
spi_open(SPI_SPARE2, MAX_SPI_FREQ);
21+
}
22+
else if(type == SPARE3)
23+
{
24+
spi_open(SPI_SPARE3, MAX_SPI_FREQ);
25+
}
26+
else
27+
{
28+
return -1;
29+
}
30+
31+
return 0;
32+
}
33+
34+
// read data from previously opened SPI device
35+
uint16_t _read(uint8_t channel)
36+
{
37+
uint8_t data[2] = { channel, channel };
38+
39+
spi_transfer(data, 2);
40+
41+
return ((uint16_t)data[0] << 8) | data[1];
42+
}
43+
44+
void pi_spi_8ai_16b_set_channel(uint8_t channel, uint8_t type)
45+
{
46+
channel = channel > 7 ? 7 : channel;
47+
48+
uint8_t ret = _open(type);
49+
if(ret < 0) { return; }
50+
51+
_channels[type] = channel;
52+
53+
// read once to write channel to mux
54+
_read(channel);
55+
// read twice to ensure ADC has updated reading
56+
_read(channel);
57+
}
58+
59+
uint16_t pi_spi_8ai_16b_read(uint8_t type)
60+
{
61+
uint8_t ret = _open(type);
62+
if(ret < 0) { return 0; }
63+
64+
return _read(_channels[type]);
65+
}

0 commit comments

Comments
 (0)