Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .mxproject

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion AZURE_RTOS/App/app_azure_rtos_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extern "C" {

#define TX_APP_MEM_POOL_SIZE 46080

#define NX_APP_MEM_POOL_SIZE 10240
#define NX_APP_MEM_POOL_SIZE 1024

/* USER CODE BEGIN EC */

Expand Down
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,7 @@ target_link_libraries(${CMAKE_PROJECT_NAME}
stm32cubemx

# Add user defined libraries
)
)

# NER add nanopb
add_subdirectory(Drivers/Odyssey-Definitions/nanopb)
31 changes: 19 additions & 12 deletions Cerberus-2.0.ioc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ GPDMA1.SRCDATAWIDTH_GPDMACH5=DMA_SRC_DATAWIDTH_HALFWORD
GPIO.groupedBy=Group By Peripherals
I2C2.IPParameters=Timing
I2C2.Timing=0x60606293
IWDG.IPParameters=Prescaler
IWDG.Prescaler=IWDG_PRESCALER_16
KeepUserPlacement=false
LPUART1.BaudRate=115200
LPUART1.IPParameters=BaudRate
Expand Down Expand Up @@ -175,15 +177,16 @@ Mcu.Pin115=VP_GPDMA1_VS_GPDMACH5
Mcu.Pin116=VP_ICACHE_VS_ICACHE
Mcu.Pin117=VP_IWDG_VS_IWDG
Mcu.Pin118=VP_NETXDUO_VS_NXOoCore
Mcu.Pin119=VP_NETXDUO_VS_NetworkOoInterface
Mcu.Pin119=VP_NETXDUO_VS_AddonsOoMQTT
Mcu.Pin12=PF4
Mcu.Pin120=VP_NETXDUO_VS_EthernetOoPhyOoInterface
Mcu.Pin121=VP_PWR_VS_SECSignals
Mcu.Pin122=VP_PWR_VS_LPOM
Mcu.Pin123=VP_SYS_VS_tim1
Mcu.Pin124=VP_THREADX_VS_RTOSJjThreadXJjCoreJjDefault
Mcu.Pin125=VP_BOOTPATH_VS_BOOTPATH
Mcu.Pin126=VP_MEMORYMAP_VS_MEMORYMAP
Mcu.Pin120=VP_NETXDUO_VS_NetworkOoInterface
Mcu.Pin121=VP_NETXDUO_VS_EthernetOoPhyOoInterface
Mcu.Pin122=VP_PWR_VS_SECSignals
Mcu.Pin123=VP_PWR_VS_LPOM
Mcu.Pin124=VP_SYS_VS_tim1
Mcu.Pin125=VP_THREADX_VS_RTOSJjThreadXJjCoreJjDefault
Mcu.Pin126=VP_BOOTPATH_VS_BOOTPATH
Mcu.Pin127=VP_MEMORYMAP_VS_MEMORYMAP
Mcu.Pin13=PF5
Mcu.Pin14=PF6
Mcu.Pin15=PF7
Expand Down Expand Up @@ -279,17 +282,18 @@ Mcu.Pin96=PD7
Mcu.Pin97=PG9
Mcu.Pin98=PG10
Mcu.Pin99=PG11
Mcu.PinsNb=127
Mcu.PinsNb=128
Mcu.ThirdPartyNb=0
Mcu.UserConstants=
Mcu.UserName=STM32H563ZITx
MxCube.Version=6.15.0
MxDb.Version=DB.6.0.150
NETXDUO.ETH_ON=1
NETXDUO.IPParameters=ETH_ON,NX_APP_MEM_POOL_SIZE,NX_MAX_MULTICAST_GROUPS,NX_ENABLE_VLAN
NETXDUO.NX_APP_MEM_POOL_SIZE=10240
NETXDUO.NX_ENABLE_VLAN=false
NETXDUO.IPParameters=ETH_ON,NX_ENABLE_TCP_KEEPALIVE,NX_TCP_IMMEDIATE_ACK,NX_PACKET_HEADER_PAD,NX_MAX_MULTICAST_GROUPS
NETXDUO.NX_ENABLE_TCP_KEEPALIVE=false
NETXDUO.NX_MAX_MULTICAST_GROUPS=256
NETXDUO.NX_PACKET_HEADER_PAD=false
NETXDUO.NX_TCP_IMMEDIATE_ACK=false
NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false
NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false
NVIC.ETH_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true\:true
Expand Down Expand Up @@ -475,6 +479,7 @@ PC9.GPIO_Label=MUX_SEL4
PC9.Locked=true
PC9.Signal=GPIO_Output
PCC.Checker=false
PCC.Display=Plot\: All Steps
PCC.Line=STM32H563/H573
PCC.MCU=STM32H563ZITx
PCC.PartNumber=STM32H563ZITx
Expand Down Expand Up @@ -893,6 +898,8 @@ VP_IWDG_VS_IWDG.Mode=IWDG_Activate
VP_IWDG_VS_IWDG.Signal=IWDG_VS_IWDG
VP_MEMORYMAP_VS_MEMORYMAP.Mode=CurAppReg
VP_MEMORYMAP_VS_MEMORYMAP.Signal=MEMORYMAP_VS_MEMORYMAP
VP_NETXDUO_VS_AddonsOoMQTT.Mode=Addons_MQTT
VP_NETXDUO_VS_AddonsOoMQTT.Signal=NETXDUO_VS_AddonsOoMQTT
VP_NETXDUO_VS_EthernetOoPhyOoInterface.Mode=Custom_Phy_Interface
VP_NETXDUO_VS_EthernetOoPhyOoInterface.Signal=NETXDUO_VS_EthernetOoPhyOoInterface
VP_NETXDUO_VS_NXOoCore.Mode=NX_Core
Expand Down
25 changes: 19 additions & 6 deletions Core/Inc/u_ethernet.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
#pragma once
#include "serverdata.pb.h"
#include "tx_api.h"
#include <stdint.h>

#include "nx_stm32_eth_driver.h"
#include "u_nx_ethernet.h"
#include "main.h"
#include "u_queues.h"
#define ETH_MAX_TOPIC_SIZE 100

/* API */
int ethernet1_init(void);
void ethernet_inbox(ethernet_message_t *message);
/**
* Initialize ethernet
*/
UINT ethernet1_init(void);
/**
* Send a protobuf message over MQTT
*/
UINT ethernet1_mqtt_send(char* topic, uint8_t topic_size, char* unit, uint8_t unit_size, float* values, uint8_t values_len, uint64_t time_us);

typedef struct {
uint8_t type;
char topic[ETH_MAX_TOPIC_SIZE];
uint8_t topic_size;
serverdata_v2_ServerData msg;
} eth_mqtt_queue_message_t;
7 changes: 3 additions & 4 deletions Core/Inc/u_queues.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

/*
* Basically just a wrapper for ThreadX stuff. Lets you create/configure queues.
*
*
* Author: Blake Jackson
*/

/* Queue List */
extern queue_t eth_incoming; // Incoming Ethernet Queue
extern queue_t eth_outgoing; // Outgoing Ethernet Queue
extern queue_t eth_manager; // Ethernet manager Queue
extern queue_t can_incoming; // Incoming CAN Queue
extern queue_t can_outgoing; // Outgoing CAN Queue
extern queue_t faults; // Faults Queue
Expand All @@ -24,4 +23,4 @@ extern queue_t state_transition_queue; // State Transition Queue
/* API */
uint8_t queues_init(TX_BYTE_POOL *byte_pool); // Initializes all queues. Called from app_threadx.c

#endif /* u_queues.h */
#endif /* u_queues.h */
7 changes: 3 additions & 4 deletions Core/Inc/u_threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/*
* Basically just a wrapper for ThreadX stuff. Lets you create/configure threads.
*
*
* Author: Blake Jackson
*/

Expand All @@ -17,8 +17,7 @@ uint8_t threads_init(TX_BYTE_POOL *byte_pool);

/* Thread Functions */
void vDefault(ULONG thread_input);
void vEthernetIncoming(ULONG thread_input);
void vEthernetOutgoing(ULONG thread_input);
void vEthernet1Manager(ULONG thread_input);
void vCANIncoming(ULONG thread_input);
void vCANOutgoing(ULONG thread_input);
void vFaults(ULONG thread_input);
Expand All @@ -34,4 +33,4 @@ void vTest(ULONG thread_input);
void vRTDS(ULONG thread_input);


#endif /* u_threads.h */
#endif /* u_threads.h */
2 changes: 1 addition & 1 deletion Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ static void MX_IWDG_Init(void)

/* USER CODE END IWDG_Init 1 */
hiwdg.Instance = IWDG;
hiwdg.Init.Prescaler = IWDG_PRESCALER_4;
hiwdg.Init.Prescaler = IWDG_PRESCALER_16;
hiwdg.Init.Window = 4095;
hiwdg.Init.Reload = 4095;
hiwdg.Init.EWI = 0;
Expand Down
110 changes: 87 additions & 23 deletions Core/Src/u_ethernet.c
Original file line number Diff line number Diff line change
@@ -1,43 +1,107 @@
#include "u_ethernet.h"
#include "nx_api.h"
#include "pb.h"
#include "pb_encode.h"
#include "serverdata.pb.h"
#include "tx_api.h"
#include "u_queues.h"
#include "nx_stm32_eth_driver.h"
#include "u_nx_ethernet.h"
#include "u_nx_debug.h"
#include "main.h"
#include "u_tx_debug.h"
#include "u_tx_general.h"
#include "u_tx_queues.h"

#define ETH_TYPE_SEND 0
#define ETH_TYPE_RECV 1

/* Callback for when a ethernet message is recieved. */
void _ethernet_recieve(ethernet_message_t message) {
void _ethernet_recieve(eth_mqtt_queue_message_t message) {
/* Send the message to the incoming ethernet queue. */
int status = queue_send(&eth_incoming, &message, TX_NO_WAIT);
int status = queue_send(&eth_manager, &message, TX_NO_WAIT);
if(status != U_SUCCESS) {
PRINTLN_ERROR("Failed to send message to the incoming ethernet queue (Status: %d).", status);
return;
}
}

UINT ethernet1_mqtt_send(char* topic, uint8_t topic_size, char* unit, uint8_t unit_size, float* values, uint8_t values_len, uint64_t time_us) {
serverdata_v2_ServerData msg = serverdata_v2_ServerData_init_zero;
// if (unit_size > sizeof(msg.unit)) {
// return U_ERROR;
// }
if (topic_size > ETH_MAX_TOPIC_SIZE) {
return U_ERROR;
}
// if (values_len > (sizeof(msg.values) / sizeof(float))) {
// return U_ERROR;
// }
eth_mqtt_queue_message_t sendable;
msg.time_us = time_us;
// COPIES
*(msg.unit) = *unit;
*(msg.values) = *values;
msg.values_count = values_len;
sendable.type = ETH_TYPE_SEND;
sendable.msg = msg;
memcpy(sendable.topic, topic, topic_size);
sendable.topic_size = topic_size;
return queue_send(&eth_manager, &sendable, TX_NO_WAIT);
}

/* Initializes ethernet. */
int ethernet1_init(void) {
UINT ethernet1_init(void) {
/* PHY_RESET Pin has to be set HIGH for the PHY to function. */
HAL_GPIO_WritePin(PHY_RESET_GPIO_Port, PHY_RESET_Pin, GPIO_PIN_SET);

/* Init the ethernet. */
return ethernet_init(VCU, nx_stm32_eth_driver, _ethernet_recieve);
}

/* Processes received ethernet messages. */
void ethernet_inbox(ethernet_message_t *message) {
switch(message->message_id) {
case 5:
PRINTLN_INFO("Incoming Ethernet message: %s", message->data);
break;
case 0x02:
// do thing
break;
case 0x03:
// etc
break;
default:
uint8_t buff = 0;
buff = message->data[0];
PRINTLN_ERROR("Unknown Ethernet Message Recieved (Message ID: %d).", message->message_id);

PRINTLN_INFO("sender_id=%d, recipient_id=%d, message_id=%d, data_length=%d, data=%d", message->sender_id, message->recipient_id, message->message_id, message->data_length, message->data[0]);

break;

void vEthernet1Manager(ULONG thread_input) {

// all calls to the ethernet driver in this thread only to prevent race conditons
UINT status = ethernet1_init();
if(status != NX_SUCCESS) {
PRINTLN_ERROR("Failed to call ethernet1_init() (Status: %d/%s).", status, nx_status_toString(status));
}
PRINTLN_INFO("Successfully initialized ethernet 1!");

eth_mqtt_queue_message_t msg;
unsigned char buffer[serverdata_v2_ServerData_size];
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));

while(1) {

/* Send outgoing messages, recieve incoming messages */
while(queue_receive(&eth_manager, &msg, TX_WAIT_FOREVER) == U_SUCCESS) {
if (msg.type == ETH_TYPE_SEND) {
status = pb_encode(&stream, serverdata_v2_ServerData_fields, &msg.msg);
if (!status) {
PRINTLN_ERROR("Failed to serialize protobuf message: %s", PB_GET_ERROR(&stream));
continue;
}
status = ethernet_mqtt_publish(msg.topic, msg.topic_size, (char*)buffer, stream.bytes_written);
stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
if(status != U_SUCCESS) {
PRINTLN_WARNING("Failed to send Ethernet message after removing from outgoing queue: %ud", status);
if (status == 0x10002) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nit but can we make a var for 0x10002. assuming that is mqtt disconnection return status

do {
tx_thread_sleep(MS_TO_TICKS(1000));
status = ethernet_mqtt_reconnect();
PRINTLN_WARNING("ATTEMPTING MQTT RECONNECTION: Status %d", status);
} while (status != NX_SUCCESS);
PRINTLN_WARNING("MQTT RECONNECTION SUCCESS");
}
continue;
} else {
PRINTLN_INFO("Sent ethernet message!");
}
}
}

/* No sleep. Thread timing is controlled completely by the queue timeout. */
}
}
20 changes: 7 additions & 13 deletions Core/Src/u_queues.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
#include "u_tx_debug.h"
#include "u_faults.h"
#include "u_statemachine.h"
#include "u_ethernet.h"
#include <stdio.h>

/* Incoming Ethernet Queue */
queue_t eth_incoming = {
.name = "Incoming Ethernet Queue", /* Name of the queue. */
.message_size = sizeof(ethernet_message_t), /* Size of each queue message, in bytes. */
.capacity = 10 /* Number of messages the queue can hold. */
};

/* Outgoing Ethernet Queue */
queue_t eth_outgoing = {
/* Outgoing Activity Queue */
queue_t eth_manager = {
.name = "Outgoing Ethernet Queue", /* Name of the queue. */
.message_size = sizeof(ethernet_message_t), /* Size of each queue message, in bytes. */
.message_size = sizeof(eth_mqtt_queue_message_t), /* Size of each queue message, in bytes. */
.capacity = 10 /* Number of messages the queue can hold. */
};

Expand Down Expand Up @@ -47,19 +42,18 @@ queue_t state_transition_queue = {
.capacity = 10 /* Number of messages the queue can hold. */
};

/* Initializes all ThreadX queues.
/* Initializes all ThreadX queues.
* Calls to _create_queue() should go in here
*/
uint8_t queues_init(TX_BYTE_POOL *byte_pool) {

/* Create Queues */
CATCH_ERROR(create_queue(byte_pool, &eth_incoming), U_SUCCESS); // Create Incoming Ethernet Queue
CATCH_ERROR(create_queue(byte_pool, &eth_outgoing), U_SUCCESS); // Create Outgoing Ethernet Queue
CATCH_ERROR(create_queue(byte_pool, &eth_manager), U_SUCCESS); // Create Ethernet Manager Queue
CATCH_ERROR(create_queue(byte_pool, &can_incoming), U_SUCCESS); // Create Incoming CAN Queue
CATCH_ERROR(create_queue(byte_pool, &can_outgoing), U_SUCCESS); // Create Outgoing CAN Queue
CATCH_ERROR(create_queue(byte_pool, &faults), U_SUCCESS); // Create Faults Queue
CATCH_ERROR(create_queue(byte_pool, &state_transition_queue), U_SUCCESS); // Create state transition queue.

PRINTLN_INFO("Ran queues_init().");
return U_SUCCESS;
}
}
Loading
Loading