]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blobdiff - rpp/src/drv/hbridge.c
hbr: Check and simplify watchdog code
[pes-rpp/rpp-lib.git] / rpp / src / drv / hbridge.c
index f0d19aa8f367fbe17e9fd34766a3337254ddca77..08b4efcfbf8c4878ce48385b4a953e9bcf1b0cf2 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2013 Czech Technical University in Prague
+/* Copyright (C) 2013, 2015, 2016 Czech Technical University in Prague
  *
  * Authors:
  *     - Michal Horn
@@ -22,6 +22,8 @@
 // PWM is available for HBR control.
 #include "drv/drv.h"
 #include <os/semphr.h>
+#include "drv/spi_tms570.h"
+#include "drv/gio_tab.h"
 
 //Flag variable if pwm was initialized and is ready to start.
 static boolean_t pwm_initialized = FALSE;
@@ -31,16 +33,13 @@ static boolean_t wdg_start = FALSE;
 
 // Prepared command to be send on SPI.
 // Default value is watchdog reset command.
-uint16_t hbr_spi_wdg_tx = 0x03DB;
-
-// Shadow variable of hbr_spi_wdg_tx
-uint16_t hbr_spi_wdg_tx_shd = 0x03DB;
+static const uint8_t hbr_spi_wdg_tx[2] = { 0x03, 0xDB };
 
 // Response from SPI.
-uint16_t hbr_spi_wdg_rx = 0;
+static uint8_t hbr_spi_wdg_rx[2] = {0};
 
 // Shadow variable of hbr_spi_wdg_shd
-uint16_t hbr_spi_wdg_rx_shd = 0;
+static uint8_t hbr_spi_wdg_rx_shd[2] = {0};
 
 // SPI communication result code (one of SPI_MSG_*)
 int hbr_spi_code = 0;
@@ -66,23 +65,22 @@ static xSemaphoreHandle wdg_sync;
  * @return  always zero
  */
 int drv_hbr_spi_wdg_callback(struct spi_drv *ifc, int code,
-                                                        struct spi_msg_head *msg)
+                                                        struct spi_msg *msg)
 {
        hbr_spi_code = code;
        if (code == SPI_MSG_FINISHED) {
-               hbr_spi_wdg_rx = hbr_spi_wdg_rx_shd;
-               hbr_spi_wdg_tx_shd = hbr_spi_wdg_tx;
+               memcpy(hbr_spi_wdg_rx, hbr_spi_wdg_rx_shd, sizeof(hbr_spi_wdg_rx));
        }
        return 0;
 }
 
 // SPI message format definition for watchdog reset command
-spi_msg_head_t hbr_spi_wdg = {
+spi_msg_t hbr_spi_wdg = {
        .flags = 0,
-       .addr = 0,
-       .rq_len = 2,
-       .tx_buf = (uint8_t*)&hbr_spi_wdg_tx_shd,
-       .rx_buf = (uint8_t*)&hbr_spi_wdg_rx_shd,
+       .dev = SPIDEV_L99H01,
+       .rq_len = sizeof(hbr_spi_wdg_tx),
+       .tx_buf = hbr_spi_wdg_tx,
+       .rx_buf = hbr_spi_wdg_rx_shd,
        .callback = drv_hbr_spi_wdg_callback,
        .private = 1
 };
@@ -98,15 +96,6 @@ spi_msg_head_t hbr_spi_wdg = {
  */
 void drv_hbr_wdg_task(void *p)
 {
-       spi_drv_t *ifc;
-
-       ifc = spi_find_drv(NULL, 4);
-
-       if (ifc == NULL) {
-               wdg_start = FALSE;
-               vTaskDelete(NULL);
-       }
-
        portTickType xLastWakeTime;
        xLastWakeTime = xTaskGetTickCount();
 
@@ -118,7 +107,7 @@ void drv_hbr_wdg_task(void *p)
                        wdg_start = FALSE;
                }
                else
-                       spi_msg_rq_ins(ifc, &hbr_spi_wdg);
+                       spi_msg_rq_ins(&hbr_spi_wdg);
                xSemaphoreGive(wdg_sync);
        }
 }
@@ -145,7 +134,7 @@ int8_t drv_hbr_wdg_start()
                if (wdg_sync == NULL )
                        return -RPP_ENOMEM;
                if (xTaskCreate(drv_hbr_wdg_task,
-                                               (const signed char*)"hbr_wdg_task",
+                                               "hbr_wdg_task",
                                                1024, NULL, 1, &wdg_handle) != pdPASS) {
                        wdg_start = FALSE;
                        return -RPP_ENOMEM;
@@ -276,7 +265,7 @@ double drv_hbr_pwm_get_period()
  */
 void drv_hbr_set_dir(int direction)
 {
-       hal_gpio_pin_set_value(PIN_DSC_HBRDIR, direction);
+       gio_tab_set(PIN_HBRDIR, !!direction);
 }
 
 /**
@@ -286,7 +275,7 @@ void drv_hbr_set_dir(int direction)
  */
 int drv_hbr_get_dir()
 {
-       return hal_gpio_pin_get_value(PIN_DSC_HBRDIR);
+       return gio_tab_get(PIN_HBRDIR) ? 1 : 0;
 }
 
 /**
@@ -296,7 +285,7 @@ int drv_hbr_get_dir()
  */
 void drv_hbr_set_en(int value)
 {
-       hal_gpio_pin_set_value(PIN_DSC_HBREN, value);
+       gio_tab_set(PIN_HBREN, !!value);
 }
 
 /**
@@ -306,5 +295,5 @@ void drv_hbr_set_en(int value)
  */
 int drv_hbr_get_en()
 {
-       return hal_gpio_pin_get_value(PIN_DSC_HBREN);
+       return gio_tab_get(PIN_HBREN) ? 1 : 0;
 }