]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blobdiff - rpp-test-sw/commands/cmd_motor_example.c
Update library
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_motor_example.c
index 0a8b0a7b7e23e286bd8127251839db8a8e80b7a5..1c6a9e2c24e778feac0608542395cbbd37d7c011 100644 (file)
@@ -6,26 +6,18 @@
  * Authors:
  *     - Michal Horn
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * This document contains proprietary information belonging to Czech
+ * Technical University in Prague. Passing on and copying of this
+ * document, and communication of its contents is not permitted
+ * without prior written authorization.
  *
  * File : cmd_pin.c
  *
  * Abstract:
  *             Example commands for motor controlling over FlexRay. The first board has connected a control panel
- *     with two buttons and variable resistor, the second board has a DC motor connected to the H-bridge.
- *     Both boards are connected by the FlexRay bus and user can drive the motor by the control panel
- *     (speed, direction and enabling).
+ *      with two buttons and variable resistor, the second board has a DC motor connected to the H-bridge.
+ *      Both boards are connected by the FlexRay bus and user can drive the motor by the control panel
+ *      (speed, direction and enabling).
  */
 
 #include "cmd_motor_example.h"
 #ifndef DOCGEN
 
 #include "rpp/rpp.h"
-#include "hal/hal.h"
-#include "drv/drv.h"
+#include "drv/digital_io_def.h"
 #include "cmdproc.h"
 
 #define ADC_MAX_VALUE 0x0983
 #define RX_ERROR_MAX 100
-#define ENABLE_DEBOUNCE_TIME   2
-#define DIRECTION_DEBOUNCE_TIME        2
+#define ENABLE_DEBOUNCE_TIME    2
+#define DIRECTION_DEBOUNCE_TIME 2
 
 /**
  * This structure contains global FlexRay configuration.
@@ -50,7 +41,7 @@
 static Fr_TMS570LS_ClusterConfigType Fr_cluster_config = {
        .gColdStartAttempts = 0x2,
        .gListenNoise = 0xF,
-       .gMacroPerCycle = 0x15E0,       // (cycle period, 5.6us)
+       .gMacroPerCycle = 0x15E0,   // (cycle period, 5.6us)
        .gMaxWithoutClockCorrectionFatal = 0xF,
        .gMaxWithoutClockCorrectionPassive = 0xF,
        .gNetworkManagementVectorLength = 12,
@@ -65,7 +56,7 @@ static Fr_TMS570LS_ClusterConfigType Fr_cluster_config = {
        .gdMinislot = 0x4,
        .gdMinislotActionPointOffset = 0x2,
        .gdNIT = 0xAE3,
-       .gdSampleClockPeriod = 0,               // 10mbit/sec
+       .gdSampleClockPeriod = 0,       // 10mbit/sec
        .gdStaticSlot = 0x56,
        .gdTSSTransmitter = 0xA,
        .gdWakeupSymbolRxIdle = 18,
@@ -98,7 +89,7 @@ static Fr_TMS570LS_NodeConfigType Fr_node_control_config = {
        .pMicroPerCycle = 0x36B00,
        .pRateCorrectionOut = 0xCD,
        .pOffsetCorrectionOut = 0x151,
-       .pSamplesPerMicrotick = 0,              // 10 mbit/sec
+       .pSamplesPerMicrotick = 0,      // 10 mbit/sec
        .pSingleSlotEnabled = TRUE,
        .pWakeupChannel = FR_CHANNEL_A,
        .pWakeupPattern = 2,
@@ -114,32 +105,32 @@ static Fr_TMS570LS_NodeConfigType Fr_node_control_config = {
  * and configures the H-bridge.
  */
 static Fr_TMS570LS_NodeConfigType Fr_node_motor_config = {
-               .pAllowHaltDueToClock = 0,
-               .pAllowPassiveToActive = 0xF,
-               .pChannels = FR_CHANNEL_AB,
-               .pClusterDriftDamping = 0x1,
-               .pDelayCompensationA = 0x3,
-               .pDelayCompensationB = 0x3,
-               .pExternOffsetCorrection = 0,
-               .pExternRateCorrection = 0,
-               .pKeySlotUsedForStartup = TRUE,
-               .pKeySlotUsedForSync = TRUE,
-               .pLatestTx = 0x10D,
-               .pMacroInitialOffsetA = 0x6,
-               .pMacroInitialOffsetB = 0x6,
-               .pMicroInitialOffsetA = 0x18,
-               .pMicroInitialOffsetB = 0x18,
-               .pMicroPerCycle = 0x36B00,
-               .pRateCorrectionOut = 0xCD,
-               .pOffsetCorrectionOut = 0x151,
-               .pSamplesPerMicrotick = 0,              // 10 mbit/sec
-               .pSingleSlotEnabled = TRUE,
-               .pWakeupChannel = FR_CHANNEL_A,
-               .pWakeupPattern = 2,
-               .pdAcceptedStartupRange = 0x81,
-               .pdListenTimeout = 0x36DA2,
-               .pdMaxDrift = 0x151,
-               .pDecodingCorrection = 0x33
+       .pAllowHaltDueToClock = 0,
+       .pAllowPassiveToActive = 0xF,
+       .pChannels = FR_CHANNEL_AB,
+       .pClusterDriftDamping = 0x1,
+       .pDelayCompensationA = 0x3,
+       .pDelayCompensationB = 0x3,
+       .pExternOffsetCorrection = 0,
+       .pExternRateCorrection = 0,
+       .pKeySlotUsedForStartup = TRUE,
+       .pKeySlotUsedForSync = TRUE,
+       .pLatestTx = 0x10D,
+       .pMacroInitialOffsetA = 0x6,
+       .pMacroInitialOffsetB = 0x6,
+       .pMicroInitialOffsetA = 0x18,
+       .pMicroInitialOffsetB = 0x18,
+       .pMicroPerCycle = 0x36B00,
+       .pRateCorrectionOut = 0xCD,
+       .pOffsetCorrectionOut = 0x151,
+       .pSamplesPerMicrotick = 0,          // 10 mbit/sec
+       .pSingleSlotEnabled = TRUE,
+       .pWakeupChannel = FR_CHANNEL_A,
+       .pWakeupPattern = 2,
+       .pdAcceptedStartupRange = 0x81,
+       .pdListenTimeout = 0x36DA2,
+       .pdMaxDrift = 0x151,
+       .pDecodingCorrection = 0x33
 };
 
 /**
@@ -172,30 +163,30 @@ static Fr_TMS570LS_MsgRAMConfig Fr_node_motor_msgRAM_config = {
  * transfer the control data.
  */
 static Fr_TMS570LS_BufferConfigType Fr_node_control_static_buffers_config[] = {
-               {
-                       .channel = FR_CHANNEL_AB,
-                       .cycleCounterFiltering = 0,
-                       .isTx = TRUE,
-                       .maxPayload = 2,
-                       .msgBufferInterrupt = TRUE,
-                       .payloadPreambleIndicatorTr = FALSE,
-                       .rejectNullFrames = FALSE,
-                       .rejectStaticSegment = FALSE,
-                       .singleTransmit = FALSE,
-                       .slotId = 1
-               },
-               {
-                       .channel = FR_CHANNEL_AB,
-                       .cycleCounterFiltering = 0,
-                       .isTx = TRUE,
-                       .maxPayload = 2,
-                       .msgBufferInterrupt = TRUE,
-                       .payloadPreambleIndicatorTr = FALSE,
-                       .rejectNullFrames = FALSE,
-                       .rejectStaticSegment = FALSE,
-                       .singleTransmit = FALSE,
-                       .slotId = 3
-               }
+       {
+               .channel = FR_CHANNEL_AB,
+               .cycleCounterFiltering = 0,
+               .isTx = TRUE,
+               .maxPayload = 2,
+               .msgBufferInterrupt = TRUE,
+               .payloadPreambleIndicatorTr = FALSE,
+               .rejectNullFrames = FALSE,
+               .rejectStaticSegment = FALSE,
+               .singleTransmit = FALSE,
+               .slotId = 1
+       },
+       {
+               .channel = FR_CHANNEL_AB,
+               .cycleCounterFiltering = 0,
+               .isTx = TRUE,
+               .maxPayload = 2,
+               .msgBufferInterrupt = TRUE,
+               .payloadPreambleIndicatorTr = FALSE,
+               .rejectNullFrames = FALSE,
+               .rejectStaticSegment = FALSE,
+               .singleTransmit = FALSE,
+               .slotId = 3
+       }
 };
 
 /**
@@ -256,20 +247,21 @@ static Fr_ConfigType Fr_config_node_motor = {
 };
 
 /**
- *     Loads data into TX buffers for FlexRay node sending the control data.
+ *  Loads data into TX buffers for FlexRay node sending the control data.
  *
- *     After data are copied into TX buffer, the TX request is enabled, which
- *     means that message will be transmitted as soon as the frame occures in the
- *     communication cycle.
+ *  After data are copied into TX buffer, the TX request is enabled, which
+ *  means that message will be transmitted as soon as the frame occures in the
+ *  communication cycle.
  *
- *     @param  enable  enable flag to be send
- *     @param  direction       direction flag to be send
- *     @param  duty    duty cycle value to be send
+ *  @param     enable  enable flag to be send
+ *  @param  direction  direction flag to be send
+ *  @param     duty    duty cycle value to be send
  *
- *     @return SUCCESS when data were succesfully sent
- *             FAILURE when some error occured.
+ *  @return SUCCESS when data were succesfully sent
+ *          FAILURE when some error occured.
  */
-int8_t transmit_control_data(int enable, int direction, int duty) {
+int8_t transmit_control_data(int enable, int direction, int duty)
+{
        uint8_t data[3];
 
        // Write payload for buffer associated with frame 1
@@ -291,22 +283,21 @@ int8_t transmit_control_data(int enable, int direction, int duty) {
  *                     1 if message no message was received
  *                     2 if receiving failed
  */
-int receive_control_data(int* enable, int* direction, int* duty) {
+int receive_control_data(int *enable, int *direction, int *duty)
+{
        uint8_t data[3];
        Fr_RxLPduStatusType status;
        uint8_t size;
 
-       if (rpp_fr_receive_lpdu(0, 3, data, &status, &size) == FAILURE) {
+       if (rpp_fr_receive_lpdu(0, 3, data, &status, &size) == FAILURE)
                return 2;
-       }
-       if (status == FR_NOT_RECEIVED) {
+       if (status == FR_NOT_RECEIVED)
                return 1;
-       }
        else {
-       *duty = data[0];
-       *direction = data[1];
-       *enable = data[2];
-       return 0;
+               *duty = data[0];
+               *direction = data[1];
+               *enable = data[2];
+               return 0;
        }
 }
 
@@ -326,37 +317,36 @@ int receive_control_data(int* enable, int* direction, int* duty) {
  * @param[in]  param   Parameters of command
  * @return     0 when OK or error code
  */
-int cmd_do_control(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
-    port_desc_t* adc_desc;
-    uint32_t adc_values[PORT_ADC_CHANNEL_NUM*2];
-    int ret;
-    int duty = 0;
-    int button = 0;
-    int enable = 0;
-    int direction = 1;
-    int enable_cnt = 0;                        // Debounce counter for enable button
-    int dir_cnt = 0;                   // Debounce counter for direction button
-    uint32_t error;
-
-    adc_desc = hal_port_get_dsc(PORT_NAME_ADC, -1);
-
-    if (rpp_din_setup(0, FALSE, TRUE, FALSE) == FAILURE) {     // For the red button, which is switch to battery
+int cmd_do_control(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+       uint32_t adc_num_values = rpp_port_get_pin_cnt(DIO_PORT_NAME_ADC);
+       uint32_t adc_values[adc_num_values*2];
+       int ret;
+       int duty = 0;
+       int button = 0;
+       int enable = 0;
+       int direction = 1;
+       int enable_cnt = 0;         // Debounce counter for enable button
+       int dir_cnt = 0;            // Debounce counter for direction button
+       uint32_t error;
+
+       if (rpp_din_setup(0, FALSE, TRUE, FALSE) == FAILURE) {  // For the red button, which is switch to battery
                rpp_sci_printf("Din 0 setup failed.\n");
                return -CMDERR_BADCFG;
-    }
-    if (rpp_din_setup(1, FALSE, TRUE, FALSE) == FAILURE) {     // For the black button, which is switch to ground
+       }
+       if (rpp_din_setup(1, FALSE, TRUE, FALSE) == FAILURE) {  // For the black button, which is switch to ground
                rpp_sci_printf("Din 1 setup failed.\n");
                return -CMDERR_BADCFG;
-    }
-    if (rpp_din_update() == FAILURE) {
+       }
+       if (rpp_din_update() == FAILURE) {
                rpp_sci_printf("Din update failed.\n");
                return -CMDERR_BADCFG;
-    }
+       }
 
-    if (rpp_fr_init_driver(&Fr_config_node_control, &error) == FAILURE) {
+       if (rpp_fr_init_driver(&Fr_config_node_control, &error) == FAILURE) {
                rpp_sci_printf("Fray driver initialization failed: %#x.\n", error);
                return -CMDERR_BADCFG;
-    }
+       }
        if (rpp_fr_init_controller(0, &error) == FAILURE) {
                rpp_sci_printf("Fray control node initialization failed: %#x.\n", error);
                return -CMDERR_BADCFG;
@@ -375,56 +365,48 @@ int cmd_do_control(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
 
        rpp_sci_printf("Connected.\r\n");
 
-       while(1) {
+       while (1) {
                vTaskDelay(10/portTICK_RATE_MS);
-           ret = adc_desc->port_getfnc_ptr(adc_desc->config, PORT_ADC_CHANNEL_NUM, adc_values);
-           if (ret < 0) {
-               rpp_sci_printf("ADC read failed!\n");
-               break;
-           }
-           duty = (100*adc_values[0])/ADC_MAX_VALUE;
-           if (rpp_din_update() == FAILURE) {
+               ret = rpp_port_get_val(DIO_PORT_NAME_ADC, adc_num_values, adc_values);
+               if (ret < 0) {
+                       rpp_sci_printf("ADC read failed!\n");
+                       break;
+               }
+               duty = (100*adc_values[0])/ADC_MAX_VALUE;
+               if (rpp_din_update() == FAILURE) {
                        rpp_sci_printf("Din update failed.\n");
                        break;
-           }
-           button = !rpp_din_get(0);
-           if (button == 1) {
-               if (enable_cnt++ == ENABLE_DEBOUNCE_TIME) {
-                       enable = !enable;
-               }
-               else {
-                       enable_cnt++;
-               }
-           }
-           else {
-               enable_cnt = 0;
-           }
-           button = rpp_din_get(1);
-           if (button == 1) {
-               if (dir_cnt++ == DIRECTION_DEBOUNCE_TIME) {
-                       direction = !direction;
-               }
-               else {
-                       dir_cnt++;
-               }
-           }
-           else {
-               dir_cnt = 0;
-           }
-
-           if (transmit_control_data(enable, direction, duty) == FAILURE) {
-               rpp_sci_printf("Data transmission failed!\n");
-               break;
-           }
-           rpp_sci_printf("Enable: %d, Direction: %s, Duty: %d%%\r", enable, (direction == 1) ? "L" : "R", duty);
+               }
+               button = !rpp_din_get(0);
+               if (button == 1) {
+                       if (enable_cnt++ == ENABLE_DEBOUNCE_TIME)
+                               enable = !enable;
+                       else
+                               enable_cnt++;
+               }
+               else
+                       enable_cnt = 0;
+               button = rpp_din_get(1);
+               if (button == 1) {
+                       if (dir_cnt++ == DIRECTION_DEBOUNCE_TIME)
+                               direction = !direction;
+                       else
+                               dir_cnt++;
+               }
+               else
+                       dir_cnt = 0;
+
+               if (transmit_control_data(enable, direction, duty) == FAILURE) {
+                       rpp_sci_printf("Data transmission failed!\n");
+                       break;
+               }
+               rpp_sci_printf("Enable: %d, Direction: %s, Duty: %d%%\r", enable, (direction == 1) ? "L" : "R", duty);
        }
 
-       if (rpp_fr_halt_communication(0) == FAILURE) {
+       if (rpp_fr_halt_communication(0) == FAILURE)
                rpp_sci_printf("FlexRay HALT command failed, please reset the board to stop transmission.\n");
-       }
-       else {
+       else
                rpp_sci_printf("FlexRay halted, reset the board to make FlexRay usable again.\r\n");
-       }
        return 0;
 }
 
@@ -440,8 +422,9 @@ int cmd_do_control(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
  * @param[in]  param   Parameters of command
  * @return     0 when OK or error code
  */
-int cmd_do_motor(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
-       double hbr_period = 50; // us
+int cmd_do_motor(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+       double hbr_period = 50; // us
        double control = 0.0;
        int hbr_duty = 0;
        int direction = 1;
@@ -452,15 +435,15 @@ int cmd_do_motor(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
 
        if (rpp_hbr_enable(hbr_period) == FAILURE ||
                rpp_hbr_control(0) == FAILURE
-       ) {
+               ) {
                rpp_sci_printf("H-bridge initialization failed.\n");
                return -CMDERR_BADCFG;
        }
 
-    if (rpp_fr_init_driver(&Fr_config_node_motor, &error) == FAILURE) {
+       if (rpp_fr_init_driver(&Fr_config_node_motor, &error) == FAILURE) {
                rpp_sci_printf("Fray driver initialization failed: %#x.\n", error);
                return -CMDERR_BADCFG;
-    }
+       }
        if (rpp_fr_init_controller(0, &error) == FAILURE) {
                rpp_sci_printf("Fray motor node initialization failed: %#x.\n", error);
                return -CMDERR_BADCFG;
@@ -481,81 +464,77 @@ int cmd_do_motor(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
 
        while (1) {
                ret = receive_control_data(&enable, &direction, &hbr_duty);
-               if (ret == 2) { // receiving failed
+               if (ret == 2) { // receiving failed
                        rx_error++;
                        if (rx_error > RX_ERROR_MAX) {
                                rpp_sci_printf("Maximum RX errors reached!\n");
                                break;
                        }
                }
-               else if (ret == 0) {    // message received
+               else if (ret == 0) {    // message received
                        control = hbr_duty/((double)100) * ((direction == 1) ? -1 : 1) * enable;
-                   rpp_sci_printf("Enable: %d, Direction: %s, Duty: %d%%\r", enable, (direction == 1) ? "L" : "R", hbr_duty);
+                       rpp_sci_printf("Enable: %d, Direction: %s, Duty: %d%%\r", enable, (direction == 1) ? "L" : "R", hbr_duty);
                        if (rpp_hbr_control(control) == FAILURE) {
                                rpp_sci_printf("H-bridge control failed!\n");
                                break;
                        }
                }
        }
-       if (rpp_hbr_disable() == FAILURE) {
+       if (rpp_hbr_disable() == FAILURE)
                rpp_sci_printf("H-bridge disabling failed!\n");
-       }
-       else {
+       else
                rpp_sci_printf("H-bridge disabled.\n");
-       }
-       if (rpp_fr_halt_communication(0) == FAILURE) {
+       if (rpp_fr_halt_communication(0) == FAILURE)
                rpp_sci_printf("FlexRay HALT command failed, please reset the board to stop transmission.\n");
-       }
-       else {
+       else
                rpp_sci_printf("FlexRay halted, reset the board to make FlexRay usable again.\r\n");
-       }
        return 0;
 }
 
-#endif /* DOCGEN */
+#endif  /* DOCGEN */
 
 /** Command descriptor for control */
-cmd_des_t const cmd_des_control={
-               0, 0,
-               "demomotctrl","Run motor control demo - reads input and sends it",
-               "### Command syntax ###\n"
-               "\n"
-               "    demomotctrl\n"
-               "\n"
-               "### Description ###\n"
-               "\n"
-               "This command creates a FlexRay node and starts to read buttons\n"
-               "(connected to DIN0 and DIN1) and a potentiometer (ADC1) from a control\n"
-               "panel. The read data are sent via FlexRay to the second node, created\n"
-               "by running demomotdrive command.\n"
-               "\n"
-               "The purpose of this pair of commands is to demonstrate functionality\n"
-               "of the FlexRay, ADC, DIN and HBR peripherals.\n",
-               CMD_HANDLER(cmd_do_control), (void *)&cmd_list_motor_example
-       };
+cmd_des_t const cmd_des_control = {
+       0, 0,
+       "demomotctrl","Run motor control demo - reads input and sends it",
+       "### Command syntax ###\n"
+       "\n"
+       "    demomotctrl\n"
+       "\n"
+       "### Description ###\n"
+       "\n"
+       "This command creates a FlexRay node and starts to read buttons\n"
+       "(connected to DIN0 and DIN1) and a potentiometer (ADC1) from a control\n"
+       "panel. The read data are sent via FlexRay to the second node, created\n"
+       "by running demomotdrive command.\n"
+       "\n"
+       "The purpose of this pair of commands is to demonstrate functionality\n"
+       "of the FlexRay, ADC, DIN and HBR peripherals.\n",
+       CMD_HANDLER(cmd_do_control), (void *)&cmd_list_motor_example
+};
 
 /** Command descriptor for motor */
-cmd_des_t const cmd_des_motor={
-               0, 0,
-               "demomotdrive","Run motor control demo - drives the DC motor",
-               "### Command syntax ###\n"
-               "\n"
-               "    demomotdrive\n"
-               "\n"
-               "### Description ###\n"
-               "\n"
-               "This command creates a FlexRay node and starts to receive the data\n"
-               "from another node created by command demomotctrl. The received data\n"
-               "are applied to HBR to control the DC motor.\n"
-               "\n"
-               "The purpose of this pair of commands is to demonstrate functionality\n"
-               "of the FlexRay, ADC, DIN and HBR peripherals.\n",
-               CMD_HANDLER(cmd_do_motor), (void *)&cmd_list_motor_example
-       };
+cmd_des_t const cmd_des_motor = {
+       0, 0,
+       "demomotdrive","Run motor control demo - drives the DC motor",
+       "### Command syntax ###\n"
+       "\n"
+       "    demomotdrive\n"
+       "\n"
+       "### Description ###\n"
+       "\n"
+       "This command creates a FlexRay node and starts to receive the data\n"
+       "from another node created by command demomotctrl. The received data\n"
+       "are applied to HBR to control the DC motor.\n"
+       "\n"
+       "The purpose of this pair of commands is to demonstrate functionality\n"
+       "of the FlexRay, ADC, DIN and HBR peripherals.\n",
+       CMD_HANDLER(cmd_do_motor), (void *)&cmd_list_motor_example
+};
 
 /** List of commands for example, defined as external */
-cmd_des_t const *cmd_list_motor_example[]={
-  &cmd_des_control,
-  &cmd_des_motor,
-  NULL
+cmd_des_t const *cmd_list_motor_example[] = {
+       &cmd_des_control,
+       &cmd_des_motor,
+       NULL
 };