]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blobdiff - rpp/src/drv/din.c
Merge port and gpio definitions into one file in the DRV layer
[pes-rpp/rpp-lib.git] / rpp / src / drv / din.c
index baa8c14f28d8aef18a402c0365fce530ebb018b9..ccdafb23548e3248503fe90a8fc5ef4d06f9d760 100644 (file)
@@ -1,20 +1,24 @@
-/*
- * din.c
+/* Copyright (C) 2012-2013 Czech Technical University in Prague
  *
- *  Created on: 17.12.2012
- *      Author: Michal Horn
+ * Authors:
+ *     - Michal Horn <hornmich@fel.cvut.cz>
+ *     - Martin Koubek <martin.koubek@porsche-engineering.com>
  *
- *  Refactored by: Martin Koubek
- *     martin.koubek@porsche-engineering.com
- *     18.9.2013
+ * 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.
  *
- *  This file is written for 33972 Multiple Switch
- *  http://www.freescale.com/files/analog/doc/data_sheet/MC33972.pdf
+ * File : din.c
+
+ * Abstract:
+ *        This file is written for 33972 Multiple Switch
+ *        http://www.freescale.com/files/analog/doc/data_sheet/MC33972.pdf
  *
- *  This file contains functions to control DIN
- *  Voltage on each pin can be set
- *  switch to ground or to battery on programable pins can be set
- *  interrupts on each pins can be disabled and enabled
+ *        This file contains functions to control DIN
+ *        Voltage on each pin can be set
+ *        switch to ground or to battery on programable pins can be set
+ *        interrupts on each pins can be disabled and enabled
  */
 
 
@@ -34,19 +38,6 @@ static uint32_t din_spi_resp = 0;
 /** Store commands in shadow registers */
 static uint16_t shadow_reg_list[DIN_NUM_SPI_CMD];
 
-
-const static uint32_t dsc_pin_map[8U] = {
-    PIN_DSC_DIN8,
-    PIN_DSC_DIN9,
-    PIN_DSC_DIN10,
-    PIN_DSC_DIN11,
-    PIN_DSC_DIN12,
-    PIN_DSC_DIN13,
-    PIN_DSC_DIN14,
-    PIN_DSC_DIN15
-};
-
-
 /******************************************************************************
 *   Function Prototypes
 ******************************************************************************/
@@ -87,14 +78,13 @@ static uint32_t enum2cmd(const enum SpiCmdTable index)
 /* ---------------- */
 
 uint16_t din_set_reg(
-               enum SpiCmdTable spi_cmd_index, uint16_t clear_mask, uint16_t xor_mask)
+       enum SpiCmdTable spi_cmd_index, uint16_t clear_mask, uint16_t xor_mask)
 {
-       if (spi_cmd_index >= DIN_NUM_SPI_CMD) {
+       if (spi_cmd_index >= DIN_NUM_SPI_CMD)
                return 0;
-       }
 
        shadow_reg_list[spi_cmd_index] =
-                       shadow_reg_list[spi_cmd_index] & ~clear_mask ^ xor_mask;
+               shadow_reg_list[spi_cmd_index] & ~clear_mask ^ xor_mask;
 
        uint32_t din_spi_cmd = enum2cmd(spi_cmd_index) | shadow_reg_list[spi_cmd_index];
 
@@ -104,63 +94,69 @@ uint16_t din_set_reg(
 int8_t drv_din_ref(uint16_t ref_a, uint16_t ref_b)
 {
 
-    uint16_t cmd;
+       uint16_t cmd;
 
-    // Get descriptor
-    uint32_t commands[2];
-    port_desc_t* desc = hal_port_get_dsc(PORT_NAME_DACDREF, -1);
+       // Get descriptor
+       uint32_t commands[2];
+       dio_port_desc_t *desc = dio_port_get_dsc(DIO_PORT_NAME_DACDREF, -1);
 
-    // Send command for DAC A
-    cmd = DACA_INIT_VAL | (ref_a & 0x0FFF);
+       // Send command for DAC A
+       cmd = DACA_INIT_VAL | (ref_a & 0x0FFF);
 
-    commands[0] = (cmd & 0xFF00) >> 8;
-    commands[1] = (cmd & 0xFF);
-    desc->port_setfnc_ptr(desc->config, desc->numValues, commands);
+       commands[0] = (cmd & 0xFF00) >> 8;
+       commands[1] = (cmd & 0xFF);
+       desc->port_setfnc_ptr(desc->config, desc->numValues, commands);
 
-    // Send command for DAC B
-    cmd = DACB_INIT_VAL | (ref_b & 0x0FFF);
+       // Send command for DAC B
+       cmd = DACB_INIT_VAL | (ref_b & 0x0FFF);
 
-    commands[0] = (cmd & 0xFF00) >> 8;
-    commands[1] = (cmd & 0xFF);
-    desc->port_setfnc_ptr(desc->config, desc->numValues, commands);
+       commands[0] = (cmd & 0xFF00) >> 8;
+       commands[1] = (cmd & 0xFF);
+       desc->port_setfnc_ptr(desc->config, desc->numValues, commands);
 
-    // Fixme: check SPI return value.
-    return SUCCESS;
+       // Fixme: check SPI return value.
+       return SUCCESS;
 }
 
 
-int8_t drv_din_get_varthr(uint8_t pin) {
+int8_t drv_din_get_varthr(uint8_t pin)
+{
+
+       // Check range
+       if ((pin < 8) || (pin > 15))
+               return FAILURE;
 
-    // Check range
-    if((pin < 8) || (pin > 15)) {
-        return FAILURE;
-    }
+       dio_port_desc_t* port = dio_port_get_dsc(DIO_PORT_NAME_DINMCU, -1);
+       if (pin-8 >= port->numValues)
+               return FAILURE;
 
-    return hal_gpio_pin_get_value(dsc_pin_map[pin - 8]);
+       return dio_gpio_pin_get_value(port->config[pin - 8]);
 }
 
 uint16_t din_get_val_word()
 {
-    // How it should be.
-    //uint16_t sp = ((din_spi_resp >> 14) & 0x00FF);
-    //uint16_t sg = ((din_spi_resp << 8 ) & 0xFF00);
-
-    // How it actually is.
-    // Ignore datasheet, this is the actual response from the SPI driver:
-    // [xxxx xxxx][SG7-SG0][SP1 SP0 yy yyyy][zz SP7-SP2]
-    //  x: Unknown.
-    //  y: Maybe SG13-SG8, but untested.
-    //  z: Maybe therm flag and int flag.
-    // For SP: First get SP7-SP2 right, then add SP1 and SP0
-    uint16_t sp = ((din_spi_resp << 2) & 0x00FF) | ((din_spi_resp >> 14) & 0x3);
-    uint16_t sg = ((din_spi_resp >> 8) & 0xFF00);
-    uint16_t word = sg | sp;
-    return word;
+       // How it should be.
+       //uint16_t sp = ((din_spi_resp >> 14) & 0x00FF);
+       //uint16_t sg = ((din_spi_resp << 8 ) & 0xFF00);
+
+       // How it actually is.
+       // Ignore datasheet, this is the actual response from the SPI driver:
+       // [xxxx xxxx][SG7-SG0][SP1 SP0 yy yyyy][zz SP7-SP2]
+       //  x: Unknown.
+       //  y: Maybe SG13-SG8, but untested.
+       //  z: Maybe therm flag and int flag.
+       // For SP: First get SP7-SP2 right, then add SP1 and SP0
+       uint16_t sp = ((din_spi_resp << 2) & 0x00FF) | ((din_spi_resp >> 14) & 0x3);
+       uint16_t sg = ((din_spi_resp >> 8) & 0xFF00);
+       uint16_t word = sg | sp;
+
+       return word;
 }
 
 
-int din_spi_response() {
-    return din_spi_resp;
+int din_spi_response()
+{
+       return din_spi_resp;
 }
 
 
@@ -173,16 +169,17 @@ int din_spi_response() {
  * store spi response
  * return spi response
  */
-int din_spi_transfer_mst(const uint32_t din_spi_cmd) {
-    port_desc_t* desc;
-
-    desc = hal_port_get_dsc(PORT_NAME_DINSPI, -1);
-    uint32_t commands[3];
-    commands[0] = (din_spi_cmd & 0xFF0000) >> 16;      // command
-    commands[1] = (din_spi_cmd & 0xFF00) >> 8;         // 1.st B of data
-    commands[2] = (din_spi_cmd & 0xFF);                                // 2.nd B of data
-
-    din_spi_resp = desc->port_setfnc_ptr(desc->config, desc->numValues, \
-               commands);
-    return din_spi_resp;
+int din_spi_transfer_mst(const uint32_t din_spi_cmd)
+{
+       dio_port_desc_t *desc;
+
+       desc = dio_port_get_dsc(DIO_PORT_NAME_DINSPI, -1);
+       uint32_t commands[3];
+       commands[0] = (din_spi_cmd & 0xFF0000) >> 16;   // command
+       commands[1] = (din_spi_cmd & 0xFF00) >> 8;      // 1.st B of data
+       commands[2] = (din_spi_cmd & 0xFF);             // 2.nd B of data
+
+       din_spi_resp = desc->port_setfnc_ptr(desc->config, desc->numValues,     \
+                                                                                commands);
+       return din_spi_resp;
 }