From e9757768574d45b7fc799a397419103efc969850 Mon Sep 17 00:00:00 2001 From: Riho Natsushima Date: Mon, 11 Nov 2024 14:54:09 +0900 Subject: [PATCH] pico: Add Address Definition Defined CSP port number for temperature data acquisition service. Defined addresses in CAN and UART communication. pico: Fix Switch Statement Fixed to return temperature when a request comes in on a specific port. pico: Fix for temperature acquisition Fixed for temperature acquisition. pico: Fix Program Fixed to take data from temperature sensor and send it through CSP. Signed-off-by: Riho Natsushima pico: Fix 'Add i2c0 Settings' Fixed Linelint because Linelint is an error. Signed-off-by: Riho Natsushima --- pico/src/main.c | 13 ++++++++++++ pico/src/pico_csp.c | 17 +++++++++++++++- pico/src/pico_csp.h | 9 +++++++++ pico/src/temp.c | 49 ++++++++++++++++++++++++++++++++++++--------- pico/src/temp.h | 2 +- 5 files changed, 78 insertions(+), 12 deletions(-) diff --git a/pico/src/main.c b/pico/src/main.c index c5195b2..e5d0250 100644 --- a/pico/src/main.c +++ b/pico/src/main.c @@ -7,6 +7,14 @@ #include #include #include "pico_csp.h" +#include +#include +#include + +#include +LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG); + +const struct device *const temp_sensor = DEVICE_DT_GET_ANY(lm75); int main(void) { @@ -22,6 +30,11 @@ int main(void) return 0; } + if (!device_is_ready(temp_sensor)) { + LOG_ERR("Temperature sensor not found or not ready!"); + return 0; + } + csp_server_start(); while (true) { diff --git a/pico/src/pico_csp.c b/pico/src/pico_csp.c index 584786b..97cd2d2 100644 --- a/pico/src/pico_csp.c +++ b/pico/src/pico_csp.c @@ -6,8 +6,10 @@ #include #include -#include +#include +#include #include +#include "pico_csp.h" #include LOG_MODULE_REGISTER(csp, CONFIG_MAIN_LOG_LEVEL); @@ -27,6 +29,8 @@ LOG_MODULE_REGISTER(csp, CONFIG_MAIN_LOG_LEVEL); extern csp_conf_t csp_conf; static void server(void); +/* get sensor device */ +extern const struct device *const temp_sensor; static void *router_task(void *param) { @@ -74,6 +78,12 @@ static void server(void) csp_listen(&sock, 10); + if (!device_is_ready(temp_sensor)) { + LOG_ERR("Temperature sensor not ready"); + return; + } else { + LOG_INF("Temperature sensor ready"); + } while (true) { if ((conn = csp_accept(&sock, 10000)) == NULL) { continue; @@ -82,6 +92,11 @@ static void server(void) csp_packet_t *packet; while ((packet = csp_read(conn, 50)) != NULL) { switch (csp_conn_dport(conn)) { + case PORT_T: + get_temp(conn); + csp_buffer_free(packet); + break; + default: csp_service_handler(packet); break; diff --git a/pico/src/pico_csp.h b/pico/src/pico_csp.h index 27c03e4..7d153ff 100644 --- a/pico/src/pico_csp.h +++ b/pico/src/pico_csp.h @@ -6,4 +6,13 @@ #pragma once +#include + +#define PORT_T (11U) /* for get temperature */ + +#define MAIN_OBC_CAN_ADDR (1U) +#define RPI_ZERO_CAN_ADDR (2U) +#define RPI_ZERO_UART_ADDR (3U) +#define RPI_PICO_UART_ADDR (4U) + int csp_server_start(void); diff --git a/pico/src/temp.c b/pico/src/temp.c index 873a084..d738f14 100644 --- a/pico/src/temp.c +++ b/pico/src/temp.c @@ -5,26 +5,55 @@ */ #include +#include +#include +#include +#include +#include "temp.h" + +#include +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) +extern const struct device *const temp_sensor; + +void get_temp(csp_conn_t *conn) { - int ret; - uint8_t tmp[2]; + struct sensor_value temp_val; + int rc = sensor_sample_fetch(temp_sensor); + + if (rc < 0) { + LOG_ERR("Failed to fetch sensor data, error: %d", rc); + return; + } else { + LOG_INF("Sensor data fetch successful"); + } - if (temp == NULL) { - return -EINVAL; + /* get temp_data */ + rc = sensor_channel_get(temp_sensor, SENSOR_CHAN_AMBIENT_TEMP, &temp_val); + if (rc < 0) { + LOG_ERR("Failed to get sensor data, error: %d", rc); + return; + } else { + LOG_INF("Sensor data get successful"); } - 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; + /* creating a packet to send temp_data */ + csp_packet_t *send_packet = csp_buffer_get(0); + if (send_packet == NULL) { + LOG_ERR("Failed to get CSP buffer"); + return; } - return ret; + /* Convert temperature data to the appropriate format*/ + float temp = (float)(temp_val.val1 + (float)(temp_val.val2)/1000000.0f); + + memcpy(send_packet->data, &temp, sizeof(temp)); + send_packet->length = sizeof(temp); + csp_send(conn, send_packet); + } diff --git a/pico/src/temp.h b/pico/src/temp.h index d86f037..fbb36c0 100644 --- a/pico/src/temp.h +++ b/pico/src/temp.h @@ -8,4 +8,4 @@ #include -int get_temp(const struct device *dev, float *temp); +void get_temp(csp_conn_t *conn);