From 30b3aa4dd13f5c80e05cac7af89101ca6211d3a8 Mon Sep 17 00:00:00 2001
From: Hamza <h.tamim@fastttube.de>
Date: Fri, 24 May 2024 15:21:29 +0200
Subject: [PATCH] changed AMS_CAN.h -> can.h

---
 Core/Inc/AMS_CAN.h       | 29 ----------------
 Core/Inc/AMS_HighLevel.h |  2 +-
 Core/Inc/TMP1075.h       |  2 +-
 Core/Inc/can.h           | 16 +++++++++
 Core/Src/AMS_CAN.c       | 73 ----------------------------------------
 Core/Src/AMS_HighLevel.c |  2 +-
 Core/Src/can.c           | 27 +++++++++++++++
 Core/Src/main.c          |  6 ++--
 8 files changed, 49 insertions(+), 108 deletions(-)
 delete mode 100644 Core/Inc/AMS_CAN.h
 create mode 100644 Core/Inc/can.h
 delete mode 100644 Core/Src/AMS_CAN.c
 create mode 100644 Core/Src/can.c

diff --git a/Core/Inc/AMS_CAN.h b/Core/Inc/AMS_CAN.h
deleted file mode 100644
index d814d38..0000000
--- a/Core/Inc/AMS_CAN.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * AMS_CAN.h
- *
- *  Created on: Mar 19, 2022
- *      Author: jasper
- */
-
-#ifndef INC_AMS_CAN_H_
-#define INC_AMS_CAN_H_
-
-#include "main.h"
-
-#include "stm32f3xx_hal.h"
-#include "stm32f3xx_hal_can.h"
-#include "stm32f3xx_hal_def.h"
-
-#include <stdint.h>
-
-void ams_can_init(CAN_HandleTypeDef* hcan);
-
-void ams_can_handle_ams_msg(CAN_RxHeaderTypeDef* header, uint8_t* data);
-
-void ams_can_send_status();
-
-HAL_StatusTypeDef ams_can_wait_for_free_mailboxes(CAN_HandleTypeDef* handle,
-                                                  int num_mailboxes,
-                                                  uint32_t timeout);
-
-#endif /* INC_AMS_CAN_H_ */
diff --git a/Core/Inc/AMS_HighLevel.h b/Core/Inc/AMS_HighLevel.h
index 78a8609..870007a 100644
--- a/Core/Inc/AMS_HighLevel.h
+++ b/Core/Inc/AMS_HighLevel.h
@@ -11,7 +11,7 @@
 #include "ADBMS_Abstraction.h"
 #include "ADBMS_CMD_MAKROS.h"
 #include "ADBMS_LL_Driver.h"
-#include "AMS_CAN.h"
+#include "can.h"
 
 typedef enum {
   AMSDEACTIVE,
diff --git a/Core/Inc/TMP1075.h b/Core/Inc/TMP1075.h
index 9c7dd5c..1e9e680 100644
--- a/Core/Inc/TMP1075.h
+++ b/Core/Inc/TMP1075.h
@@ -1,7 +1,7 @@
 #ifndef INC_TMP1075_H_
 #define INC_TMP1075_H_
 
-#include "AMS_CAN.h"
+#include "can.h"
 #include "common_defs.h"
 #include "stm32f3xx_hal.h"
 #include "stm32f3xx_hal_def.h"
diff --git a/Core/Inc/can.h b/Core/Inc/can.h
new file mode 100644
index 0000000..4b2b471
--- /dev/null
+++ b/Core/Inc/can.h
@@ -0,0 +1,16 @@
+
+#include "stm32f3xx_hal.h"
+#include "stm32f3xx_hal_can.h"
+#include "stm32f3xx_hal_def.h"
+#include "state_machine.h"
+#include <stdint.h>
+
+#define CAN_ID_IN   0x501
+#define CAN_ID_OUT  0x502
+
+void can_init(CAN_HandleTypeDef* hcan);
+
+void can_handle_send_status();
+
+void can_handle_recieve_command(CAN_RxHeaderTypeDef* header, uint8_t* data);
+
diff --git a/Core/Src/AMS_CAN.c b/Core/Src/AMS_CAN.c
deleted file mode 100644
index 4ff7b40..0000000
--- a/Core/Src/AMS_CAN.c
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * AMS_CAN.c
- *
- *  Created on: Mar 19, 2022
- *      Author: jasper
- */
-
-#include "AMS_CAN.h"
-
-#include "ADBMS_Abstraction.h"
-
-#include "AMS_HighLevel.h"
-#include "TMP1075.h"
-#include "common_defs.h"
-#include "eeprom.h"
-#include "errors.h"
-#include "main.h"
-#include "stm32f3xx.h"
-#include "stm32f3xx_hal.h"
-#include "stm32f3xx_hal_can.h"
-
-#include "can-halal.h"
-#include "stm32f3xx_hal_gpio.h"
-
-#include <stdint.h>
-#include <string.h>
-
-#define CAN_ID_MV_BMS 0x501
-
-void ams_can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); }
-
-#define ITER_COUNT 10
-static uint8_t count = 0;
-static bool isOn = false;
-
-void ams_can_send_status() {
-  if (count == ITER_COUNT) {
-    HAL_GPIO_WritePin(STATUS_LED_G_GPIO_Port, STATUS_LED_G_Pin, isOn ? GPIO_PIN_SET : GPIO_PIN_RESET);
-
-    count = 0;
-    isOn = !isOn;
-  } else {
-    count++;
-  }
-
-  static uint8_t data[8];
-  int error = error_data.error_sources != 0;
-  data[0] = eeprom_config.id | (error << 7);
-  data[1] = stateofcharge;
-  uint8_t* ptr = &data[2];
-  uint16_t min_volt = 0xFFFF;
-  uint16_t max_volt = 0;
-  for (size_t i = 0; i < numberofCells; i++) {
-    if (module.cellVoltages[i] < min_volt) {
-      min_volt = module.cellVoltages[i];
-    }
-    if (module.cellVoltages[i] > max_volt) {
-      max_volt = module.cellVoltages[i];
-    }
-  }
-  ptr = ftcan_marshal_unsigned(ptr, min_volt, 2);
-  ptr = ftcan_marshal_unsigned(ptr, max_volt, 2);
-  int16_t max_temp = -0x8000;
-  for (size_t i = 0; i < N_TEMP_SENSORS; i++) {
-    if (tmp1075_temps[i] > max_temp &&
-        (tmp1075_failed_sensors & (1 << i)) == 0) {
-      max_temp = tmp1075_temps[i];
-    }
-  }
-  ftcan_marshal_unsigned(ptr, max_temp, 2);
-  uint16_t id = CAN_ID_MV_BMS | eeprom_config.id;
-  ftcan_transmit(id, data, sizeof(data));
-}
diff --git a/Core/Src/AMS_HighLevel.c b/Core/Src/AMS_HighLevel.c
index 8008693..2eeb096 100644
--- a/Core/Src/AMS_HighLevel.c
+++ b/Core/Src/AMS_HighLevel.c
@@ -8,7 +8,7 @@
 #include "AMS_HighLevel.h"
 #include "ADBMS_Abstraction.h"
 #include "ADBMS_LL_Driver.h"
-#include "AMS_CAN.h"
+#include "can.h"
 #include "TMP1075.h"
 #include "can-halal.h"
 #include "errors.h"
diff --git a/Core/Src/can.c b/Core/Src/can.c
new file mode 100644
index 0000000..0aaf81b
--- /dev/null
+++ b/Core/Src/can.c
@@ -0,0 +1,27 @@
+/*
+ *          can.c
+ *  Created on: Mai 23, 2024
+ *      Author: Hamza
+ */
+
+#include "can.h"
+
+#include "AMS_HighLevel.h"
+#include "TMP1075.h"
+#include "can-halal.h"
+#include <stdint.h>
+
+void can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); }
+
+/*
+This function sends the status of the mvbms, the battery and of powerground.
+once every 1s in states: INACTIVE, PRECHARGE, DISCHARGE, CHARGING, ERROR.
+once every 0.5s in states: READY, ACTIVE.
+with format of:
+
+*/
+void can_handle_send_status() {
+  static uint8_t data[8];
+  data[0] =  state. (sm_get_state_code() << 5);       //save 5 bit since codes are from 0-6
+  //ftcan_transmit(id, data, sizeof(data));
+}
diff --git a/Core/Src/main.c b/Core/Src/main.c
index 5891d50..9a5a862 100644
--- a/Core/Src/main.c
+++ b/Core/Src/main.c
@@ -23,7 +23,7 @@
 /* USER CODE BEGIN Includes */
 #include "ADBMS_Abstraction.h"
 #include "ADBMS_CMD_MAKROS.h"
-#include "AMS_CAN.h"
+#include "can.h"
 #include "AMS_HighLevel.h"
 #include "TMP1075.h"
 #include "eeprom.h"
@@ -119,7 +119,7 @@ int main(void)
   /* USER CODE BEGIN 2 */
   tmp1075_init(&hi2c1);
   AMS_Init(&hspi1);
-  ams_can_init(&hcan);
+  can_init(&hcan);
 
   uint32_t target_time = HAL_GetTick() + 10000;
   /* USER CODE END 2 */
@@ -133,7 +133,7 @@ int main(void)
     /* USER CODE BEGIN 3 */
     AMS_Loop();
 
-    ams_can_send_status();
+    can_handle_send_status();
   }
   /* USER CODE END 3 */
 }