Platform drivers for Mbed.
Dependents: EVAL-CN0535-FMCZ EVAL-CN0535-FMCZ EVAL-AD568x-AD569x EVAL-AD7606 ... more
src/spi.cpp
- Committer:
- Mahesh Phalke
- Date:
- 2020-08-03
- Revision:
- 11:a2dcf0ebb5b5
- Parent:
- 8:70fc373a5f46
- Child:
- 12:d85b77f4160c
File content as of revision 11:a2dcf0ebb5b5:
/***************************************************************************//** * @file spi.cpp * @brief Implementation of SPI No-OS platform driver interfaces ******************************************************************************** * Copyright (c) 2019, 2020 Analog Devices, Inc. * * All rights reserved. * * This software is proprietary to Analog Devices, Inc. and its licensors. * By using this software you agree to the terms of the associated * Analog Devices Software License Agreement. *******************************************************************************/ /******************************************************************************/ /***************************** Include Files **********************************/ /******************************************************************************/ #include <stdio.h> #include <mbed.h> #include "platform_drivers.h" #include "spi_extra.h" /******************************************************************************/ /********************** Macros and Constants Definitions **********************/ /******************************************************************************/ /******************************************************************************/ /********************** Variables and User defined data types *****************/ /******************************************************************************/ /******************************************************************************/ /************************ Functions Declarations ******************************/ /******************************************************************************/ /******************************************************************************/ /************************ Functions Definitions *******************************/ /******************************************************************************/ /** * @brief Initialize the SPI communication peripheral. * @param desc - The SPI descriptor. * @param init_param - The structure that contains the SPI parameters. * @return SUCCESS in case of success, FAILURE otherwise. */ int32_t spi_init_noos(struct spi_desc **desc, const struct spi_init_param *param) { mbed::SPI *spi; // pointer to new spi instance DigitalOut *ss; // pointer to new SS instance mbed_spi_desc *mbed_desc; // Pointer to mbed spi descriptor if (desc) { // Create the spi description object for the device spi_desc * new_desc = (spi_desc *)malloc(sizeof(spi_desc)); if (new_desc == NULL) { return FAILURE; } new_desc->chip_select = param->chip_select; new_desc->mode = param->mode; new_desc->max_speed_hz = param->max_speed_hz; // Configure and instantiate SPI protocol spi = new SPI( (PinName)(((mbed_spi_init_param *)param->extra)->spi_mosi_pin), (PinName)(((mbed_spi_init_param *)param->extra)->spi_miso_pin), (PinName)(((mbed_spi_init_param *)param->extra)->spi_clk_pin)); if (spi == NULL) { return FAILURE; } // Configure and instantiate slave select pin ss = new DigitalOut((PinName)(new_desc->chip_select)); if (ss == NULL) { return FAILURE; } // Create the SPI extra descriptor object to store new SPI instances mbed_desc = (mbed_spi_desc *)malloc(sizeof(mbed_spi_desc)); if (mbed_desc == NULL) { return FAILURE; } mbed_desc->spi_port = (SPI *)spi; mbed_desc->slave_select = (DigitalOut *)ss; new_desc->extra = (mbed_spi_desc *)mbed_desc; *desc = new_desc; /** NOTE: Actual frequency of SPI clk will be somewhat device dependent, relating to clock-settings, prescalars etc. If absolute SPI frequency is required, consult your device documentation. **/ spi->frequency(param->max_speed_hz); spi->format(16, param->mode); // 16-bit data write/read format spi->set_default_write_value(0x00); // code to write when reading back ss->write(GPIO_HIGH); // set SS high return SUCCESS; } return FAILURE; } /** * @brief Free the resources allocated by spi_init(). * @param desc - The SPI descriptor. * @return SUCCESS in case of success, FAILURE otherwise. */ int32_t spi_remove(struct spi_desc *desc) { if (desc) { // Free the SPI port object if ((SPI *)(((mbed_spi_desc *)(desc->extra))->spi_port)) { delete((SPI *)(((mbed_spi_desc *)(desc->extra))->spi_port)); } // Free the SS port object if ((DigitalOut *)(((mbed_spi_desc *)(desc->extra))->slave_select)) { delete((DigitalOut *)(((mbed_spi_desc *)(desc->extra))->slave_select)); } // Free the SPI extra descriptor object if ((mbed_spi_desc *)(desc->extra)) { free((mbed_spi_desc *)(desc->extra)); } // Free the SPI descriptor object free(desc); return SUCCESS; } return FAILURE; } /** * @brief Write and read data to/from SPI. * @param desc - The SPI descriptor. * @param data - The buffer with the transmitted/received data. * @param bytes_number - Number of bytes to write/read. * @return SUCCESS in case of success, FAILURE otherwise. */ int32_t spi_write_and_read(struct spi_desc *desc, uint8_t *data, uint16_t bytes_number) { mbed::SPI *spi; // pointer to new spi instance mbed::DigitalOut *ss; // pointer to new SS instance uint8_t num_of_words; // Number of words in SPI frame uint16_t rw_data; // SPI read data uint8_t data_index = 0; // Data index if (desc) { spi = (SPI *)(((mbed_spi_desc *)(desc->extra))->spi_port); ss = (DigitalOut *)(((mbed_spi_desc *)(desc->extra))->slave_select); spi->format(16, desc->mode); /* Get the total number of words (16-bit) and leftover bytes (8-bit) */ num_of_words = bytes_number / 2; ss->write(GPIO_LOW); while (num_of_words) { /* Form a 16-bit data to be written */ rw_data = ((uint16_t)data[data_index + 1] | ((uint16_t)data[data_index] << 8)); /* Transmit a 16-bit data over SPI */ rw_data = (uint16_t)spi->write(rw_data); /* Extract the MSB and LSB from 16-bit read data */ data[data_index++] = (uint8_t)(rw_data >> 8); data[data_index++] = (uint8_t)rw_data; num_of_words--; } /* Send the odd/single byte */ if (bytes_number % 2) { spi->format(8, desc->mode); data[data_index] = (uint8_t)spi->write(data[data_index]); } ss->write(GPIO_HIGH); return SUCCESS; } return FAILURE; }