]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blobdiff - rpp/src/hal/hal.c
Merge all GPIO and Digital IO functions into the hal.c and hal.h file
[pes-rpp/rpp-lib.git] / rpp / src / hal / hal.c
similarity index 63%
rename from rpp/src/hal/gpio.c
rename to rpp/src/hal/hal.c
index 4633ff13e8e679c84c3f959d55f2fc13b69e9703..cbe891366f680b907650805e998399843f28f9da 100644 (file)
@@ -1,28 +1,22 @@
-/* Copyright (C) 2012-2013 Czech Technical University in Prague
+/*
+ * hal.c
  *
- * Authors:
- *     - Michal Horn <hornmich@fel.cvut.cz>
- *
- * 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 : gpio.c
- *
- * Abstract:
- *         This file contains gpio pin configuration functions.
- *      Some additional function for setting and getting pin values, getting
- *      descriptors etc. are defined in header file as inline functions.
- *
- *      Each pin is defined by its descriptor defined in hal_gpio_platform_def. The descriptor can be obtained
- *      by hal_gpio_get_pin_dsc by giving a pin name as an argument.
+ *  Created on: 16.7.2015
+ *      Author: michal
  */
 
 #include "hal/hal.h"
+#include "drv/spi.h"
 
 extern gioPORT_t *port_id_map[MAX_PORT_CNT];
 extern pin_map_element_t pin_map[MAX_PIN_CNT];
+extern port_def_t port_definition[PORT_CNT];
+
+#define SPI_PORT_BUF_LEN 4
+/** Buffer for spi command to be sent */
+uint8_t spi_port_buf_tx[SPI_PORT_BUF_LEN];
+/** Buffer for spi response */
+uint8_t spi_port_buf_rx[SPI_PORT_BUF_LEN];
 
 uint8_t hal_gpio_get_pin_cnt() {
        return MAX_PIN_CNT;
@@ -151,3 +145,72 @@ uint32_t hal_gpio_pin_conf(uint32_t pin_dsc)
 {
        return hal_gpio_pin_conf_set(pin_dsc, pin_dsc);
 }
+
+
+uint32_t hal_gio_port_get_val(uint32_t *config, uint32_t num_val, uint32_t *values)
+{
+       uint32_t i;
+
+       for (i = 0; i < num_val; i++) {
+               values[i] = hal_gpio_pin_get_value(config[i]);
+       }
+       return 0;
+}
+
+uint32_t hal_gio_port_set_val(uint32_t *config, uint32_t num_val, const uint32_t *values)
+{
+       uint32_t i;
+
+       for (i = 0; i < num_val; i++) {
+               hal_gpio_pin_set_value(config[i], (values[i/8] >> i%8) & 0x1);
+       }
+       return 0;
+}
+
+const port_def_t *hal_port_get_definitions()
+{
+       return (const port_def_t *)port_definition;
+}
+
+port_desc_t *hal_port_get_dsc(const char *port_name, int len)
+{
+       uint32_t i;
+       const char *port_name_ptr;
+       char port_name_term[32];
+
+       if (len != -1) {    // port name not terminated by '\0'
+               strncpy(port_name_term, port_name, len);
+               port_name_term[len] = '\0';
+               port_name_ptr = port_name_term;
+       }
+       else port_name_ptr = port_name;
+
+       for (i = 0; i < PORT_CNT; i++) {
+               if (strcmp(port_name_ptr, port_definition[i].name) == 0)
+                       return port_definition[i].desc;
+       }
+       return NULL;
+}
+
+uint32_t hal_spi_port_transfer_command(uint32_t *config, uint32_t num_bytes, const uint32_t *commands)
+{
+       spi_drv_t *ifc;
+       int i;
+       uint32_t ret;
+
+       for (i = 0; i < num_bytes; i++)
+               spi_port_buf_tx[i] = commands[i];
+
+       ifc = spi_find_drv(NULL, config[0]);
+       if (ifc == NULL)
+               return 0;
+
+       if (!(ifc->flags & SPI_IFC_ON))
+               return 0;
+
+       spi_transfer(ifc, config[1], num_bytes, spi_port_buf_tx, spi_port_buf_rx);
+       ret = 0;
+       for (i = 0; i < num_bytes; i++)
+               ret |= spi_port_buf_rx[i] << i*8;
+       return ret;
+}