Juraj Ďuďák 2 роки тому
батько
коміт
80d063752f
4 змінених файлів з 47 додано та 38 видалено
  1. 4 3
      Inc/nbus_app.h
  2. 14 22
      Inc/nbus_types.h
  3. 25 11
      Src/nbus_app.c
  4. 4 2
      Src/nbus_slave_broadcast.c

+ 4 - 3
Inc/nbus_app.h

@@ -18,16 +18,17 @@
 #endif
 
 #include "nbus_cmd.h"
-//#include "app_bridge.h"
-//#include "nbus_memory.h"
 #include "one_wire.h"
 #include "memory_ec20.h"
 
-void nbus_init(Peripheral_typeDef *periph, nBusAppInterface_t *interface);
+
+void nbus_init(Peripheral_typeDef *periph, nBusAppInterface_t *interface, nBusPlatformInterface_t *hw);
 void nbus_init_app(void *hw_interface, void *hw_config);
 void nbus_stack(void);
 void nbus_init_memory_driver(nBus_MemoryDriver *memDriver, uint16_t capacity);
 
 
+void nbus_cb_UART_RX();
+
 #endif
 

+ 14 - 22
Inc/nbus_types.h

@@ -42,23 +42,13 @@ typedef enum {
     DATA_ERROR,
 } DataReadyTypedef;
 
-/**
- * @brief Definícia GPIO pinu na platforma STM32.
- */
-typedef struct {
-    /** GPIO port */
-    GPIO_TypeDef* port;
-    /** číslo pinu na zvolenom porte */
-    uint16_t pin;
-} McuPin_typeDef;
-
 
 /**
  * @brief Definícia periférií pre protokol uBUS.
  */
 typedef struct {
     /** @brief Handler pre rozhranie UART */
-    UART_HandleTypeDef* huart;
+    //UART_HandleTypeDef* huart;
 
     /**
     * @brief Časovač pre detekciu timeoutu na UART.
@@ -87,22 +77,12 @@ typedef struct {
     /**
      * @brief Definícia pinu pre signalizačnú LED diódu
      */
-    McuPin_typeDef* led;
+   // McuPin_typeDef* led;
 
 #if MODULE_MASTER == 1
     RTC_HandleTypeDef *rtc;
 #endif
 
-#if USE_SW_485 == 1
-    /**
-     * @brief Definícia pinu riadenie smeru komunikácie RS485.
-     *
-     * Táto vlastnosť je aktívna len v prípade ak má makro USE_SW_485 hodnotu 1
-     */
-    McuPin_typeDef* rs485De;
-#else
-    void* rs485De;
-#endif
 
 } Peripheral_typeDef;
 
@@ -161,6 +141,16 @@ typedef enum {
 } nBusMeasurementActive_t;
 
 
+typedef struct{
+	void (*uart_receive)(uint8_t *, int);
+	void (*uart_transmit)(uint8_t *, int );
+	void (*uart_abort_receive)();
+	void (*led_on)(void);
+	void (*led_off)(void);
+	void (*led_toggle)(void);
+
+}nBusPlatformInterface_t;
+
 
 /**
 * @brief Základná štruktúra protokolu nBUS.
@@ -231,6 +221,8 @@ typedef struct {
     volatile uint8_t flag_UartTxCplt_DMA;
 #endif
 
+    nBusPlatformInterface_t *hw_platform;
+
 } nBus_TypeDef;
 
 

+ 25 - 11
Src/nbus_app.c

@@ -40,28 +40,35 @@ static uint8_t crc8x_fast(void const *mem, uint16_t len) {
 
 
 inline static void receiveOneByte(){
-	HAL_UART_AbortReceive_IT(nBus.periph->huart);
-	HAL_UART_Receive_IT(nBus.periph->huart, nBus.rx_buffer, 1);
+	//HAL_UART_AbortReceive_IT(nBus.periph->huart);
+	nBus.hw_platform->uart_abort_receive();
+	//HAL_UART_Receive_IT(nBus.periph->huart, nBus.rx_buffer, 1);
+	nBus.hw_platform->uart_receive(nBus.rx_buffer, 1);
 	nBus.uart_state = UART_RX_1B;
 	nBus.rx_length = 1;
 }
 
 inline static void receiveNBytes(uint8_t n){
 	nBus.rx_length = n;
-	HAL_UART_Receive_IT(nBus.periph->huart, nBus.rx_buffer, nBus.rx_length);
+	//HAL_UART_Receive_IT(nBus.periph->huart, nBus.rx_buffer, nBus.rx_length);
+	nBus.hw_platform->uart_receive(nBus.rx_buffer, nBus.rx_length);
 	nBus.uart_state = UART_RX_PAYLOAD;
 }
 
 inline static void send_response(){
 	if(nBus.send_response == SEND_RESPONSE) {
 		nBus.tx_buffer[0] -= 1;		// prvý bajt sa nepočíta
+		nBus.hw_platform->uart_transmit(nBus.tx_buffer, nBus.tx_length);
+		/*
 #if USE_USART_DMA_TX == 1
 		HAL_UART_Transmit_DMA(nBus.periph->huart, nBus.tx_buffer, nBus.tx_length);
 #else
 		HAL_UART_Transmit(nBus.periph->huart, nBus.tx_buffer, nBus.tx_length, 10);
 #endif
+*/
 	}
-	HAL_GPIO_WritePin(nBus.periph->led->port, nBus.periph->led->pin, GPIO_PIN_RESET);
+	//HAL_GPIO_WritePin(nBus.periph->led->port, nBus.periph->led->pin, GPIO_PIN_RESET);
+	nBus.hw_platform->led_off();
 }
 
 #if MODULE_MASTER == 1
@@ -164,14 +171,16 @@ static void process_request(){
 /* -------------------------------------------------------- */
 /* ----------------------- CALLBACKS----------------------- */
 /* -------------------------------------------------------- */
-static void nbus_cb_UART_RX(UART_HandleTypeDef *huart){
+//static void nbus_cb_UART_RX(UART_HandleTypeDef *huart){
+void nbus_cb_UART_RX(){
 	if(nBus.uart_state == UART_RX_PAYLOAD) {
 		nBus.uart_state = UART_RECEIVED;
 		HAL_TIM_Base_Stop_IT(nBus.periph->uart_timer);
 	}
 
 	if(nBus.uart_state == UART_RX_1B) {
-		HAL_GPIO_WritePin(nBus.periph->led->port, nBus.periph->led->pin, GPIO_PIN_SET);
+		//HAL_GPIO_WritePin(nBus.periph->led->port, nBus.periph->led->pin, GPIO_PIN_SET);
+		nBus.hw_platform->led_on();
 		if(nBus.rx_buffer[0] > 0) {
 			receiveNBytes(nBus.rx_buffer[0]);
 
@@ -187,14 +196,17 @@ static void nbus_cb_UART_RX(UART_HandleTypeDef *huart){
 }
 
 static void nbus_cb_TIM_periodElapsed_base(TIM_HandleTypeDef *htim) {
-	HAL_GPIO_TogglePin(nBus.periph->led->port, nBus.periph->led->pin);
+	//HAL_GPIO_TogglePin(nBus.periph->led->port, nBus.periph->led->pin);
+	nBus.hw_platform->led_toggle();
 }
 
 static void nbus_cb_TIM_periodElapsed(TIM_HandleTypeDef *htim) {
 	if(nBus.uart_state == UART_RX_PAYLOAD) {
-		HAL_GPIO_WritePin(nBus.periph->led->port, nBus.periph->led->pin, GPIO_PIN_RESET);
+		//HAL_GPIO_WritePin(nBus.periph->led->port, nBus.periph->led->pin, GPIO_PIN_RESET);
+		nBus.hw_platform->led_off();
 		HAL_TIM_Base_Stop_IT(nBus.periph->uart_timer);
-		HAL_UART_AbortReceive_IT(nBus.periph->huart);
+		//HAL_UART_AbortReceive_IT(nBus.periph->huart);
+		nBus.hw_platform->uart_abort_receive();
 		receiveOneByte();
 	}
 }
@@ -205,12 +217,14 @@ static void nbus_cb_TIM_periodElapsed(TIM_HandleTypeDef *htim) {
 /* ------------------ PUBLIC FUNCTIONS-------------------- */
 /* -------------------------------------------------------- */
 
-void nbus_init(Peripheral_typeDef *periph, nBusAppInterface_t *interface){
+void nbus_init(Peripheral_typeDef *periph, nBusAppInterface_t *interface, nBusPlatformInterface_t *hw){
   nBus.periph = periph;
+  nBus.hw_platform = hw;
 
   HAL_TIM_RegisterCallback(nBus.periph->uart_timer, HAL_TIM_PERIOD_ELAPSED_CB_ID, nbus_cb_TIM_periodElapsed);
   HAL_TIM_RegisterCallback(nBus.periph->measure_timer, HAL_TIM_PERIOD_ELAPSED_CB_ID, nbus_cb_TIM_periodElapsed_base);
-  HAL_UART_RegisterCallback(nBus.periph->huart, HAL_UART_RX_COMPLETE_CB_ID, nbus_cb_UART_RX);
+  // TODO vymysiet ako to urobit
+  //HAL_UART_RegisterCallback(nBus.periph->huart, HAL_UART_RX_COMPLETE_CB_ID, nbus_cb_UART_RX);
 
   nBus.rx_length = 0;
   nBus.data_timebase = 0;

+ 4 - 2
Src/nbus_slave_broadcast.c

@@ -22,7 +22,8 @@ void nbus_slave_broadcast(nBus_TypeDef *nbus, nBusCommandType_t request_type){
 		{
 			nbus->measure_active = MEASURE_RUNNING;
 			nbus->interface->start();
-			HAL_GPIO_WritePin(nbus->periph->led->port, nbus->periph->led->pin, GPIO_PIN_SET);
+			//HAL_GPIO_WritePin(nbus->periph->led->port, nbus->periph->led->pin, GPIO_PIN_SET);
+			nbus->hw_platform->led_on();
 		}
 		break;
 
@@ -30,7 +31,8 @@ void nbus_slave_broadcast(nBus_TypeDef *nbus, nBusCommandType_t request_type){
 		{
 			nbus->measure_active = MEASURE_STOPPED;
 			nbus->interface->stop();
-			HAL_GPIO_WritePin(nbus->periph->led->port, nbus->periph->led->pin, GPIO_PIN_RESET);
+			//HAL_GPIO_WritePin(nbus->periph->led->port, nbus->periph->led->pin, GPIO_PIN_RESET);
+			nbus->hw_platform->led_off();
 		}
 		break;