diff --git a/pico/prj.conf b/pico/prj.conf index 8c6d581..3d1d35b 100644 --- a/pico/prj.conf +++ b/pico/prj.conf @@ -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 diff --git a/pico/src/pico_csp.c b/pico/src/pico_csp.c index 584786b..0a3b792 100644 --- a/pico/src/pico_csp.c +++ b/pico/src/pico_csp.c @@ -12,6 +12,8 @@ #include 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) @@ -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); @@ -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; diff --git a/pico/src/temp.c b/pico/src/temp.c index 873a084..07dc395 100644 --- a/pico/src/temp.c +++ b/pico/src/temp.c @@ -4,27 +4,51 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include "temp.h" + #include +#include +#include +#include + +#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) -{ - 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; } diff --git a/pico/src/temp.h b/pico/src/temp.h index d86f037..324c5c4 100644 --- a/pico/src/temp.h +++ b/pico/src/temp.h @@ -6,6 +6,4 @@ #pragma once -#include - -int get_temp(const struct device *dev, float *temp); +int get_temp(float *temp); diff --git a/pico/uart1.overlay b/pico/uart1.overlay index 26b500c..64ee67f 100644 --- a/pico/uart1.overlay +++ b/pico/uart1.overlay @@ -24,3 +24,13 @@ }; }; }; + +&i2c0 { + status = "okay"; + clock-frequency = <100000>; + + lm75@4c{ + compatible = "lm75"; + reg = <0x4C>; + }; +};