]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/hal/port_spi.c
21fbe63f9b5f328a20196874815d12828b952ad1
[pes-rpp/rpp-lib.git] / rpp / src / hal / port_spi.c
1 /* Copyright (C) 2012-2013, 2015 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Michal Horn <hornmich@fel.cvut.cz>
5  *
6  * This document contains proprietary information belonging to Czech
7  * Technical University in Prague. Passing on and copying of this
8  * document, and communication of its contents is not permitted
9  * without prior written authorization.
10  *
11  * File : port_spi.c
12  *
13  * Abstract:
14  *          This file contains getter and setter functions for SPI port type.
15  */
16
17 //#include "hal_port_spi.h"
18 // Cannot include upper layer
19 //#include "drv_spi.h"
20 //#include "cmdproc.h"
21 #include "hal/spi.h"
22
23 #define PORT_BUF 4
24 /** Buffer for spi command to be sent */
25 uint8_t spi_port_buf_tx[PORT_BUF];
26 /** Buffer for spi response */
27 uint8_t spi_port_buf_rx[PORT_BUF];
28
29
30 /**
31  * Transfer command through the spi
32  * @param[in] config    Address of the SPI
33  * @param[in] num_bytes Number of bytes to be trasfered
34  * @param[in] commands  SPI command to be sent
35  * @return spi response
36  */
37 uint32_t hal_spi_port_transfer_command(uint32_t *config, uint32_t num_bytes, const uint32_t *commands)
38 {
39         spi_drv_t *ifc;
40         int i;
41         uint32_t ret;
42
43         for (i = 0; i < num_bytes; i++)
44                 spi_port_buf_tx[i] = commands[i];
45
46         ifc = spi_find_drv(NULL, config[0]);
47         if (ifc == NULL)
48                 return 0;
49
50         if (!(ifc->flags & SPI_IFC_ON))
51                 return 0;
52
53         spi_transfer(ifc, config[1], num_bytes, spi_port_buf_tx, spi_port_buf_rx);
54         ret = 0;
55         for (i = 0; i < num_bytes; i++)
56                 ret |= spi_port_buf_rx[i] << i*8;
57         return ret;
58 }