Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PAC195X #51

Merged
merged 25 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
207fced
Collector proof-of-concept works and manufacturer ID can be read succ…
linguini1 May 22, 2024
c03d0a6
Added product ID and revision ID reads.
linguini1 May 22, 2024
a7bf57d
Added all the registers/commands from the datasheet.
linguini1 May 22, 2024
b7298cc
Implement the read byte protocol and update current functions to make…
linguini1 May 22, 2024
3488e92
Implement the write byte protocol from the datasheet.
linguini1 May 22, 2024
99edac6
Implemented block read protocol and fixed bug where REVISION_ID regis…
linguini1 May 23, 2024
5ba7442
Implemented block write protocol. Untested.
linguini1 May 23, 2024
dcc2c59
Implemented all refresh commands.
linguini1 May 23, 2024
03fa8cf
Add 1ms sleep to software reset in collector.
linguini1 May 23, 2024
a4eb96d
Implemented sampling mode setter and fixed bug in write byte function.
linguini1 May 23, 2024
1cc5111
Implement reading VSENSE. The 152-2 model only has VSENSE1 and VSENSE…
linguini1 May 23, 2024
dc90ea8
Implemented functions to read VBUS and VSENSE channels. It seems that…
linguini1 May 23, 2024
2c2e74c
Implemented power and average value reading, but power measurements d…
linguini1 May 23, 2024
19d3f8a
Added function to get accumulator for the channel. Attributing weird …
linguini1 May 24, 2024
07e4a26
Fix byte ordering problem when block reading for 2 byte values. Will …
linguini1 May 24, 2024
5ac27a7
Wrote function to calculate voltage on Vsense line
linguini1 May 24, 2024
e8599d4
Implemented current calculation. Maybe getting correct values? Pretty…
linguini1 May 24, 2024
8a4b513
Try to calculate current with R_SENSE of 18mOhms but still no luck.
linguini1 May 24, 2024
3cc20e9
Current may be incorrect because there is no current draw from USB po…
linguini1 May 24, 2024
a3a8fcc
Merge branch 'main' into pac195x
linguini1 Jun 2, 2024
f1a57ce
Increment the number of sensors after current sensor is added.
linguini1 Jun 2, 2024
3f7bb11
Add docstring to define.
linguini1 Jun 2, 2024
2f92614
Remove unneeded statics.
linguini1 Jun 2, 2024
5a1f994
Add power calculation.
linguini1 Jun 2, 2024
cc1bf2e
Implemented measurements that have IDs because they are localize meas…
linguini1 Jun 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/collectors/collectors.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
static const clctr_entry_t COLLECTORS[] = {
{.name = "SHT41", .collector = sht41_collector}, {.name = "SYSCLOCK", .collector = sysclock_collector},
{.name = "MS5611", .collector = ms5611_collector}, {.name = "LSM6DSO32", .collector = lsm6dso32_collector},
{.name = "MAXM10S", .collector = m10spg_collector},
{.name = "MAXM10S", .collector = m10spg_collector}, {.name = "PAC1952-2", .collector = pac1952_2_collector},
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/collectors/collectors.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ void *ms5611_collector(void *args);
void *sht41_collector(void *args);
void *lsm6dso32_collector(void *args);
void *m10spg_collector(void *args);
void *pac1952_2_collector(void *args);

#endif // _COLLECTORS_H_
85 changes: 85 additions & 0 deletions src/collectors/pac195x_clctr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "collectors.h"
#include "drivers/pac195x/pac195x.h"
#include <stdio.h>

/** Macro to early return errors. */
#define return_err(err) return (void *)((uint64_t)errno)

/** The RSENSE value connected to the PAC1952-2 in milliohms. */
#define RSENSE 18

typedef struct {
uint8_t type;
uint8_t id;
int16_t voltage;
} voltage_msg_t;

void *pac1952_2_collector(void *args) {

/* Open message queue. */
mqd_t sensor_q = mq_open(SENSOR_QUEUE, O_WRONLY);
if (sensor_q == -1) {
fprintf(stderr, "PAC195X collector could not open message queue '%s': '%s' \n", SENSOR_QUEUE, strerror(errno));
return_err(err);
}

SensorLocation loc = {
.addr = {.addr = clctr_args(args)->addr, .fmt = I2C_ADDRFMT_7BIT},
.bus = clctr_args(args)->bus,
};

int err = pac195x_set_sample_mode(&loc, SAMPLE_1024_SPS_AD);
if (err != EOK) {
fprintf(stderr, "Failed to set sampling mode on PAC195X: %s\n", strerror(err));
return_err(err);
}

err = pac195x_toggle_channel(&loc, CHANNEL1 | CHANNEL2, true);
if (err != EOK) {
fprintf(stderr, "Failed to enable all channels on PAC195X: %s\n", strerror(err));
return_err(err);
}

err = pac195x_refresh(&loc); // Refresh after all configuration to force changes into effect
usleep(1000); // 1ms after refresh until accumulator data can be read again.
if (err != EOK) {
fprintf(stderr, "Failed to refresh PAC195X: %s\n", strerror(err));
return_err(err);
}

uint16_t vbus[2];
voltage_msg_t msg;

for (;;) {

for (int i = 0; i < 2; i++) {
err = pac195x_get_vbusn(&loc, i + 1, &vbus[i]);
if (err != EOK) {
fprintf(stderr, "PAC195X could not read VBUS_%d: %s\n", i - 1, strerror(err));
break;
}
}

// Calculate voltage on SENSE 1
msg.type = TAG_VOLTAGE;
msg.id = 1;
msg.voltage = pac195x_calc_bus_voltage(32, vbus[0], false);
if (mq_send(sensor_q, (char *)&msg, sizeof(msg), 0) == -1) {
fprintf(stderr, "Could not send voltage measurement: %s\n", strerror(errno));
}

// Calculate voltage on SENSE 2
msg.type = TAG_VOLTAGE;
msg.id = 2;
msg.voltage = pac195x_calc_bus_voltage(32, vbus[1], false);
if (mq_send(sensor_q, (char *)&msg, sizeof(msg), 0) == -1) {
fprintf(stderr, "Could not send voltage measurement: %s\n", strerror(errno));
}

// Get new measurements
pac195x_refresh_v(&loc);
usleep(1000);
}

return_err(EOK);
}
Loading
Loading