Skip to content

Commit

Permalink
pico: Implement GET_TEMP telecommand to return temperatura data
Browse files Browse the repository at this point in the history
This commit implements a telecommand for temperature retrieval.  It
uses the port number (11) and temperature data type (float) defined in
MCS.

temp.c is modified to use zephyr's sensor API (fetch and get) for
temperature retrieval.  The Fetch and Get API is selected because the
Zephyr kernel of this satellite is version 3.7 and the newer Read and
Decode API is still unstable.

Signed-off-by: Riho Natsushima <nasshi7224@gmail.com>
  • Loading branch information
Nari-19 authored and yashi committed Nov 25, 2024
1 parent e054f74 commit d540c88
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
2 changes: 2 additions & 0 deletions pico/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ CONFIG_SERIAL=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_POSIX_API=y
CONFIG_LOG=y
CONFIG_SENSOR=y
CONFIG_LM75=y
15 changes: 15 additions & 0 deletions pico/src/pico_csp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(csp, CONFIG_MAIN_LOG_LEVEL);

#include "temp.h"

#define ROUTER_STACK_SIZE (256U)
#define SERVER_STACK_SIZE (1024U)
#define ROUTER_PRIO (0U)
Expand All @@ -24,6 +26,8 @@ LOG_MODULE_REGISTER(csp, CONFIG_MAIN_LOG_LEVEL);
#define PICO_UART0_PARITY (0U)
#define PICO_CSP_ADDR (26U)

#define PORT_T (11U) /* for get temperature */

extern csp_conf_t csp_conf;

static void server(void);
Expand Down Expand Up @@ -82,6 +86,17 @@ static void server(void)
csp_packet_t *packet;
while ((packet = csp_read(conn, 50)) != NULL) {
switch (csp_conn_dport(conn)) {
case PORT_T:
float temp;
int result = get_temp(&temp);
if (result < 0) {
LOG_ERR("Failed to get temperature");
} else {
LOG_DBG("Temperature: %.2f℃", (double)temp);
memcpy(packet->data, &temp, sizeof(temp));
packet->length = sizeof(temp);
csp_send(conn, packet);
}
default:
csp_service_handler(packet);
break;
Expand Down
42 changes: 33 additions & 9 deletions pico/src/temp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,51 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include "temp.h"

#include <zephyr/drivers/i2c.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/sensor.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(temp, LOG_LEVEL_DBG);

#define DEV_ADDR (0x4e)
#define START_ADDR (0x00)
#define RESOLUTION (0.0625f)
#define BIT_SHIFT (4)

int get_temp(const struct device *dev, float *temp)
{
int ret;
uint8_t tmp[2];
int get_temp(float *temp) {

const struct device *const temp_sensor = DEVICE_DT_GET_ANY(lm75);
struct sensor_value temp_val;
int r;
int rc;

if (!device_is_ready(temp_sensor)) {
LOG_ERR("Temperature sensor device not ready");
return -ENODEV;
}

if (temp == NULL) {
return -EINVAL;
}

ret = i2c_burst_read(dev, DEV_ADDR, START_ADDR, tmp, sizeof(tmp));
if (ret == 0) {
tmp[1] = tmp[1] >> BIT_SHIFT;
*temp = (int8_t)tmp[0] + (float)tmp[1] * RESOLUTION;
rc = sensor_sample_fetch(temp_sensor);
if ( rc < 0 ) {
LOG_ERR("Failed to fetch sensor data, error: %d", rc);
return rc;
}

r = sensor_channel_get(temp_sensor, SENSOR_CHAN_AMBIENT_TEMP, &temp_val);
if ( r < 0 ) {
LOG_ERR("Failed to get sensor data, error: %d", r);
return r;
}

return ret;
*temp = (float)(temp_val.val1 + (float)(temp_val.val2) /1000000.0f);
LOG_DBG("Temperature: %d.%06d°C", temp_val.val1, temp_val.val2);

return 0;
}
4 changes: 1 addition & 3 deletions pico/src/temp.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

#pragma once

#include <zephyr/device.h>

int get_temp(const struct device *dev, float *temp);
int get_temp(float *temp);
10 changes: 10 additions & 0 deletions pico/uart1.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@
};
};
};

&i2c0 {
status = "okay";
clock-frequency = <100000>;

lm75@4c{
compatible = "lm75";
reg = <0x4C>;
};
};

0 comments on commit d540c88

Please sign in to comment.