chore: First tests
This commit is contained in:
@@ -69,8 +69,8 @@ int incphub_send_msg(incphub_cli_t *cli, uint8_t *buffer, size_t length, incphub
|
||||
return INCPHUB_EINVAL;
|
||||
|
||||
// Unwind buffer pointer
|
||||
ntl_packet_t *ntl_packet = (ntl_packet_t*)(buffer - sizeof(ntl_header_t));
|
||||
nil_packet_t *nil_packet = (nil_packet_t*)(ntl_packet - sizeof(ntl_header_t));
|
||||
ntl_packet_t *ntl_packet = (ntl_packet_t*)((uint8_t*)buffer - sizeof(ntl_header_t));
|
||||
nil_packet_t *nil_packet = (nil_packet_t*)((uint8_t*)ntl_packet - sizeof(ntl_header_t));
|
||||
|
||||
// Populate NTL header
|
||||
ntl_build_header(&ntl_packet->header, dst.port, cli->port, length);
|
||||
|
||||
@@ -206,7 +206,7 @@ int dequeue_message_ingest(uint8_t **buffer_ptr, TickType_t max_delay)
|
||||
uint8_t *buffer;
|
||||
|
||||
// Try to dequeue message
|
||||
if (xQueueReceive(incphub_ingest_queue, &buffer, max_delay))
|
||||
if (xQueueReceive(incphub_ingest_queue, &buffer, max_delay) == pdFALSE)
|
||||
{
|
||||
return INCPHUB_EAGAIN;
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ void incphub_task_main(void *params)
|
||||
{
|
||||
// Dequeue message
|
||||
uint8_t *buffer;
|
||||
dequeue_message_ingest(&buffer, portMAX_DELAY);
|
||||
err = dequeue_message_ingest(&buffer, portMAX_DELAY);
|
||||
|
||||
// Get NIL packet
|
||||
nil_packet_t *nil = extract_nil_packet(buffer, INCPHUB_MSG_BUFFER_SIZE);
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include "task.h"
|
||||
|
||||
#include "tasks/heartbeat_task.h"
|
||||
#include "tasks/sensor_task.h"
|
||||
#include "tasks/requester_task.h"
|
||||
#include "incphub/incphub_task.h"
|
||||
|
||||
int my_main()
|
||||
@@ -13,6 +15,8 @@ int my_main()
|
||||
|
||||
// Create tasks
|
||||
xTaskCreateStatic(heartbeat_task_main, "heartbeat", HEARTBEAT_TASK_STACK_DEPTH, NULL, HEARTBEAT_TASK_PRIORITY, _heartbeat_task_stack, &_heartbeat_task);
|
||||
xTaskCreateStatic(sensor_task_main, "sensor", SENSOR_TASK_STACK_DEPTH, NULL, SENSOR_TASK_PRIORITY, _sensor_task_stack, &_sensor_task);
|
||||
xTaskCreateStatic(requester_task_main, "requester", REQUESTER_TASK_STACK_DEPTH, NULL, REQUESTER_TASK_PRIORITY, _requester_task_stack, &_requester_task);
|
||||
xTaskCreateStatic(incphub_task_main, "incphub", INCPHUB_TASK_STACK_DEPTH, NULL, INCPHUB_TASK_PRIORITY, _incphub_task_stack, &_incphub_task);
|
||||
|
||||
// Run
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
target_sources(${EXECUTABLE} PRIVATE
|
||||
tasks.c
|
||||
heartbeat_task.c
|
||||
sensor_task.c
|
||||
requester_task.c
|
||||
)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef _HEARTBEAT_TASK_H_
|
||||
#define _HEARTBEAT_TASK_H_
|
||||
#ifndef HEARTBEAT_TASK_H_
|
||||
#define HEARTBEAT_TASK_H_
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
|
||||
70
app/src/tasks/requester_task.c
Normal file
70
app/src/tasks/requester_task.c
Normal file
@@ -0,0 +1,70 @@
|
||||
#include "requester_task.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include "stm32f4xx_hal_uart.h"
|
||||
#include "usart.h"
|
||||
|
||||
#include "incphub/incphub_client.h"
|
||||
|
||||
|
||||
|
||||
StackType_t _requester_task_stack[REQUESTER_TASK_STACK_DEPTH];
|
||||
StaticTask_t _requester_task;
|
||||
|
||||
|
||||
#define RANDOM_COUNT 4
|
||||
uint32_t random_values[RANDOM_COUNT] = { 411, 69, 420, 0xdeadbeef };
|
||||
|
||||
void requester_task_main(void *params)
|
||||
{
|
||||
(void)params;
|
||||
|
||||
TickType_t last_wake = xTaskGetTickCount();
|
||||
int random_index = 0;
|
||||
|
||||
uint8_t *buffer;
|
||||
size_t length;
|
||||
incphub_addr_t source;
|
||||
|
||||
// Init incphub client
|
||||
incphub_cli_t *cli;
|
||||
incphub_init_client(&cli, 70);
|
||||
|
||||
while (1)
|
||||
{
|
||||
// Avoid spamming the sensor task
|
||||
xTaskDelayUntil(&last_wake, 50);
|
||||
|
||||
// Generate request
|
||||
uint32_t request = random_values[random_index++];
|
||||
random_index %= RANDOM_COUNT;
|
||||
|
||||
// Get buffer
|
||||
incphub_get_msg_buffer(cli, &buffer, sizeof(uint32_t));
|
||||
|
||||
// Populate buffer
|
||||
*(uint32_t*)buffer = request;
|
||||
|
||||
// Send to sendor task
|
||||
incphub_addr_t destination = { .addr = NIL_ADDR_LOCALHOST, .port = 69 };
|
||||
incphub_send_msg(cli, buffer, sizeof(uint32_t), destination, portMAX_DELAY);
|
||||
|
||||
// Await reply (assumes no other task will send to this one)
|
||||
incphub_await_msg(cli, &buffer, &length, &source, portMAX_DELAY);
|
||||
|
||||
// Ignore messages of different size
|
||||
if (length != sizeof(uint32_t))
|
||||
continue;
|
||||
|
||||
// Read reply message
|
||||
uint32_t num = *(uint32_t*)buffer;
|
||||
|
||||
// Profit (send to UART to ensure it worked)
|
||||
char strbuf[32];
|
||||
sprintf(strbuf, "%u\r\n", num);
|
||||
HAL_UART_Transmit(&huart2, strbuf, strlen(strbuf), 9999);
|
||||
}
|
||||
}
|
||||
16
app/src/tasks/requester_task.h
Normal file
16
app/src/tasks/requester_task.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef REQUSTER_TASK_H_
|
||||
#define REQUSTER_TASK_H_
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "FreeRTOSConfig.h"
|
||||
|
||||
#define REQUESTER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
|
||||
#define REQUESTER_TASK_PRIORITY (configMAX_PRIORITIES - 1)
|
||||
|
||||
extern StackType_t _requester_task_stack[REQUESTER_TASK_STACK_DEPTH];
|
||||
extern StaticTask_t _requester_task;
|
||||
|
||||
void requester_task_main(void *params);
|
||||
|
||||
#endif
|
||||
55
app/src/tasks/sensor_task.c
Normal file
55
app/src/tasks/sensor_task.c
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "sensor_task.h"
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include "stm32f4xx_hal_gpio.h"
|
||||
#include "gpio.h"
|
||||
|
||||
#include "incphub/incphub_client.h"
|
||||
|
||||
|
||||
|
||||
StackType_t _sensor_task_stack[SENSOR_TASK_STACK_DEPTH];
|
||||
StaticTask_t _sensor_task;
|
||||
|
||||
|
||||
|
||||
void sensor_task_main(void *params)
|
||||
{
|
||||
(void)params;
|
||||
|
||||
uint8_t *buffer;
|
||||
size_t length;
|
||||
incphub_addr_t source;
|
||||
|
||||
// Init incphub client
|
||||
incphub_cli_t *cli;
|
||||
incphub_init_client(&cli, 69);
|
||||
|
||||
while (1)
|
||||
{
|
||||
// Get a message (assume it is a uint32_t)
|
||||
incphub_await_msg(cli, &buffer, &length, &source, portMAX_DELAY);
|
||||
|
||||
// Ignore messages of different size
|
||||
if (length != sizeof(uint32_t))
|
||||
continue;
|
||||
|
||||
// Read incoming message
|
||||
uint32_t num = *(uint32_t*)buffer;
|
||||
|
||||
// Release buffer once read
|
||||
incphub_release_msg_buffer(cli, buffer);
|
||||
|
||||
// Process message
|
||||
num++;
|
||||
|
||||
// Request buffer for reply
|
||||
incphub_get_msg_buffer(cli, &buffer, sizeof(uint32_t));
|
||||
|
||||
// Prepare reply message
|
||||
*(uint32_t*)buffer = num;
|
||||
|
||||
// Send reply
|
||||
incphub_send_msg(cli, buffer, sizeof(uint32_t), source, portMAX_DELAY);
|
||||
}
|
||||
}
|
||||
16
app/src/tasks/sensor_task.h
Normal file
16
app/src/tasks/sensor_task.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef SENSOR_TASK_H_
|
||||
#define SENSOR_TASK_H_
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "FreeRTOSConfig.h"
|
||||
|
||||
#define SENSOR_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
|
||||
#define SENSOR_TASK_PRIORITY (configMAX_PRIORITIES - 1)
|
||||
|
||||
extern StackType_t _sensor_task_stack[SENSOR_TASK_STACK_DEPTH];
|
||||
extern StaticTask_t _sensor_task;
|
||||
|
||||
void sensor_task_main(void *params);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user