Skip to content

Commit

Permalink
pico: Add Address Definition
Browse files Browse the repository at this point in the history
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 <nasshi7224@gmail.com>

pico: Fix 'Add i2c0 Settings'

Fixed Linelint because Linelint is an error.

Signed-off-by: Riho Natsushima <nasshi7224@gmail.com>
  • Loading branch information
Nari-19 committed Nov 15, 2024
1 parent d4c64e6 commit e975776
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 12 deletions.
13 changes: 13 additions & 0 deletions pico/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#include "pico_csp.h"
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/sensor.h>

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

const struct device *const temp_sensor = DEVICE_DT_GET_ANY(lm75);

int main(void)
{
Expand All @@ -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) {
Expand Down
17 changes: 16 additions & 1 deletion pico/src/pico_csp.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

#include <stdlib.h>
#include <zephyr/kernel.h>
#include <csp/csp.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <csp/drivers/usart.h>
#include "pico_csp.h"

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(csp, CONFIG_MAIN_LOG_LEVEL);
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions pico/src/pico_csp.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@

#pragma once

#include <csp/csp.h>

#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);
49 changes: 39 additions & 10 deletions pico/src/temp.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,55 @@
*/

#include <zephyr/drivers/i2c.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <csp/csp.h>
#include <csp/drivers/usart.h>
#include "temp.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)
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);

}
2 changes: 1 addition & 1 deletion pico/src/temp.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

#include <zephyr/device.h>

int get_temp(const struct device *dev, float *temp);
void get_temp(csp_conn_t *conn);

0 comments on commit e975776

Please sign in to comment.