Platform drivers for Mbed.

Dependents:   EVAL-CN0535-FMCZ EVAL-CN0535-FMCZ EVAL-AD568x-AD569x EVAL-AD7606 ... more

Committer:
Mahesh Phalke
Date:
Mon Aug 03 17:21:20 2020 +0530
Revision:
11:a2dcf0ebb5b5
Parent:
8:70fc373a5f46
Child:
12:d85b77f4160c
Updates to make platform drivers compatible with Mbed-OS 6.0 and onwards

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mahphalke 8:70fc373a5f46 1 /***************************************************************************//**
mahphalke 8:70fc373a5f46 2 * @file spi.cpp
mahphalke 8:70fc373a5f46 3 * @brief Implementation of SPI No-OS platform driver interfaces
mahphalke 8:70fc373a5f46 4 ********************************************************************************
mahphalke 8:70fc373a5f46 5 * Copyright (c) 2019, 2020 Analog Devices, Inc.
mahphalke 8:70fc373a5f46 6 *
mahphalke 8:70fc373a5f46 7 * All rights reserved.
mahphalke 8:70fc373a5f46 8 *
mahphalke 8:70fc373a5f46 9 * This software is proprietary to Analog Devices, Inc. and its licensors.
mahphalke 8:70fc373a5f46 10 * By using this software you agree to the terms of the associated
mahphalke 8:70fc373a5f46 11 * Analog Devices Software License Agreement.
mahphalke 8:70fc373a5f46 12 *******************************************************************************/
mahphalke 8:70fc373a5f46 13
mahphalke 8:70fc373a5f46 14 /******************************************************************************/
mahphalke 8:70fc373a5f46 15 /***************************** Include Files **********************************/
mahphalke 8:70fc373a5f46 16 /******************************************************************************/
mahphalke 8:70fc373a5f46 17
mahphalke 8:70fc373a5f46 18 #include <stdio.h>
mahphalke 8:70fc373a5f46 19 #include <mbed.h>
mahphalke 8:70fc373a5f46 20
mahphalke 8:70fc373a5f46 21 #include "platform_drivers.h"
mahphalke 8:70fc373a5f46 22 #include "spi_extra.h"
mahphalke 8:70fc373a5f46 23
mahphalke 8:70fc373a5f46 24 /******************************************************************************/
mahphalke 8:70fc373a5f46 25 /********************** Macros and Constants Definitions **********************/
mahphalke 8:70fc373a5f46 26 /******************************************************************************/
mahphalke 8:70fc373a5f46 27
mahphalke 8:70fc373a5f46 28 /******************************************************************************/
mahphalke 8:70fc373a5f46 29 /********************** Variables and User defined data types *****************/
mahphalke 8:70fc373a5f46 30 /******************************************************************************/
mahphalke 8:70fc373a5f46 31
mahphalke 8:70fc373a5f46 32 /******************************************************************************/
mahphalke 8:70fc373a5f46 33 /************************ Functions Declarations ******************************/
mahphalke 8:70fc373a5f46 34 /******************************************************************************/
mahphalke 8:70fc373a5f46 35
mahphalke 8:70fc373a5f46 36 /******************************************************************************/
mahphalke 8:70fc373a5f46 37 /************************ Functions Definitions *******************************/
mahphalke 8:70fc373a5f46 38 /******************************************************************************/
mahphalke 8:70fc373a5f46 39
mahphalke 8:70fc373a5f46 40 /**
mahphalke 8:70fc373a5f46 41 * @brief Initialize the SPI communication peripheral.
mahphalke 8:70fc373a5f46 42 * @param desc - The SPI descriptor.
mahphalke 8:70fc373a5f46 43 * @param init_param - The structure that contains the SPI parameters.
mahphalke 8:70fc373a5f46 44 * @return SUCCESS in case of success, FAILURE otherwise.
mahphalke 8:70fc373a5f46 45 */
mahphalke 8:70fc373a5f46 46 int32_t spi_init_noos(struct spi_desc **desc,
mahphalke 8:70fc373a5f46 47 const struct spi_init_param *param)
mahphalke 8:70fc373a5f46 48 {
mahphalke 8:70fc373a5f46 49 mbed::SPI *spi; // pointer to new spi instance
mahphalke 8:70fc373a5f46 50 DigitalOut *ss; // pointer to new SS instance
mahphalke 8:70fc373a5f46 51 mbed_spi_desc *mbed_desc; // Pointer to mbed spi descriptor
mahphalke 8:70fc373a5f46 52
mahphalke 8:70fc373a5f46 53 if (desc) {
mahphalke 8:70fc373a5f46 54 // Create the spi description object for the device
mahphalke 8:70fc373a5f46 55 spi_desc * new_desc = (spi_desc *)malloc(sizeof(spi_desc));
mahphalke 8:70fc373a5f46 56 if (new_desc == NULL) {
mahphalke 8:70fc373a5f46 57 return FAILURE;
mahphalke 8:70fc373a5f46 58 }
mahphalke 8:70fc373a5f46 59
mahphalke 8:70fc373a5f46 60 new_desc->chip_select = param->chip_select;
mahphalke 8:70fc373a5f46 61 new_desc->mode = param->mode;
mahphalke 8:70fc373a5f46 62 new_desc->max_speed_hz = param->max_speed_hz;
mahphalke 8:70fc373a5f46 63
mahphalke 8:70fc373a5f46 64 // Configure and instantiate SPI protocol
mahphalke 8:70fc373a5f46 65 spi = new SPI(
mahphalke 8:70fc373a5f46 66 (PinName)(((mbed_spi_init_param *)param->extra)->spi_mosi_pin),
mahphalke 8:70fc373a5f46 67 (PinName)(((mbed_spi_init_param *)param->extra)->spi_miso_pin),
mahphalke 8:70fc373a5f46 68 (PinName)(((mbed_spi_init_param *)param->extra)->spi_clk_pin));
mahphalke 8:70fc373a5f46 69
mahphalke 8:70fc373a5f46 70 if (spi == NULL) {
mahphalke 8:70fc373a5f46 71 return FAILURE;
mahphalke 8:70fc373a5f46 72 }
mahphalke 8:70fc373a5f46 73
mahphalke 8:70fc373a5f46 74 // Configure and instantiate slave select pin
mahphalke 8:70fc373a5f46 75 ss = new DigitalOut((PinName)(new_desc->chip_select));
mahphalke 8:70fc373a5f46 76 if (ss == NULL) {
mahphalke 8:70fc373a5f46 77 return FAILURE;
mahphalke 8:70fc373a5f46 78 }
mahphalke 8:70fc373a5f46 79
mahphalke 8:70fc373a5f46 80 // Create the SPI extra descriptor object to store new SPI instances
mahphalke 8:70fc373a5f46 81 mbed_desc = (mbed_spi_desc *)malloc(sizeof(mbed_spi_desc));
mahphalke 8:70fc373a5f46 82 if (mbed_desc == NULL) {
mahphalke 8:70fc373a5f46 83 return FAILURE;
mahphalke 8:70fc373a5f46 84 }
mahphalke 8:70fc373a5f46 85
mahphalke 8:70fc373a5f46 86 mbed_desc->spi_port = (SPI *)spi;
mahphalke 8:70fc373a5f46 87 mbed_desc->slave_select = (DigitalOut *)ss;
mahphalke 8:70fc373a5f46 88 new_desc->extra = (mbed_spi_desc *)mbed_desc;
mahphalke 8:70fc373a5f46 89
mahphalke 8:70fc373a5f46 90 *desc = new_desc;
mahphalke 8:70fc373a5f46 91
mahphalke 8:70fc373a5f46 92 /**
mahphalke 8:70fc373a5f46 93 NOTE: Actual frequency of SPI clk will be somewhat device
mahphalke 8:70fc373a5f46 94 dependent, relating to clock-settings, prescalars etc. If absolute
mahphalke 8:70fc373a5f46 95 SPI frequency is required, consult your device documentation.
mahphalke 8:70fc373a5f46 96 **/
mahphalke 8:70fc373a5f46 97 spi->frequency(param->max_speed_hz);
Mahesh Phalke 11:a2dcf0ebb5b5 98 spi->format(16, param->mode); // 16-bit data write/read format
mahphalke 8:70fc373a5f46 99 spi->set_default_write_value(0x00); // code to write when reading back
mahphalke 8:70fc373a5f46 100 ss->write(GPIO_HIGH); // set SS high
mahphalke 8:70fc373a5f46 101 return SUCCESS;
mahphalke 8:70fc373a5f46 102 }
mahphalke 8:70fc373a5f46 103
mahphalke 8:70fc373a5f46 104 return FAILURE;
mahphalke 8:70fc373a5f46 105 }
mahphalke 8:70fc373a5f46 106
mahphalke 8:70fc373a5f46 107
mahphalke 8:70fc373a5f46 108 /**
mahphalke 8:70fc373a5f46 109 * @brief Free the resources allocated by spi_init().
mahphalke 8:70fc373a5f46 110 * @param desc - The SPI descriptor.
mahphalke 8:70fc373a5f46 111 * @return SUCCESS in case of success, FAILURE otherwise.
mahphalke 8:70fc373a5f46 112 */
mahphalke 8:70fc373a5f46 113 int32_t spi_remove(struct spi_desc *desc)
mahphalke 8:70fc373a5f46 114 {
mahphalke 8:70fc373a5f46 115 if (desc) {
mahphalke 8:70fc373a5f46 116 // Free the SPI port object
mahphalke 8:70fc373a5f46 117 if ((SPI *)(((mbed_spi_desc *)(desc->extra))->spi_port)) {
mahphalke 8:70fc373a5f46 118 delete((SPI *)(((mbed_spi_desc *)(desc->extra))->spi_port));
mahphalke 8:70fc373a5f46 119 }
mahphalke 8:70fc373a5f46 120
mahphalke 8:70fc373a5f46 121 // Free the SS port object
mahphalke 8:70fc373a5f46 122 if ((DigitalOut *)(((mbed_spi_desc *)(desc->extra))->slave_select)) {
mahphalke 8:70fc373a5f46 123 delete((DigitalOut *)(((mbed_spi_desc *)(desc->extra))->slave_select));
mahphalke 8:70fc373a5f46 124 }
mahphalke 8:70fc373a5f46 125
mahphalke 8:70fc373a5f46 126 // Free the SPI extra descriptor object
mahphalke 8:70fc373a5f46 127 if ((mbed_spi_desc *)(desc->extra)) {
mahphalke 8:70fc373a5f46 128 free((mbed_spi_desc *)(desc->extra));
mahphalke 8:70fc373a5f46 129 }
mahphalke 8:70fc373a5f46 130
mahphalke 8:70fc373a5f46 131 // Free the SPI descriptor object
mahphalke 8:70fc373a5f46 132 free(desc);
mahphalke 8:70fc373a5f46 133
mahphalke 8:70fc373a5f46 134 return SUCCESS;
mahphalke 8:70fc373a5f46 135 }
mahphalke 8:70fc373a5f46 136
mahphalke 8:70fc373a5f46 137 return FAILURE;
mahphalke 8:70fc373a5f46 138 }
mahphalke 8:70fc373a5f46 139
mahphalke 8:70fc373a5f46 140
mahphalke 8:70fc373a5f46 141 /**
mahphalke 8:70fc373a5f46 142 * @brief Write and read data to/from SPI.
mahphalke 8:70fc373a5f46 143 * @param desc - The SPI descriptor.
mahphalke 8:70fc373a5f46 144 * @param data - The buffer with the transmitted/received data.
mahphalke 8:70fc373a5f46 145 * @param bytes_number - Number of bytes to write/read.
mahphalke 8:70fc373a5f46 146 * @return SUCCESS in case of success, FAILURE otherwise.
mahphalke 8:70fc373a5f46 147 */
mahphalke 8:70fc373a5f46 148 int32_t spi_write_and_read(struct spi_desc *desc,
mahphalke 8:70fc373a5f46 149 uint8_t *data,
mahphalke 8:70fc373a5f46 150 uint16_t bytes_number)
mahphalke 8:70fc373a5f46 151 {
Mahesh Phalke 11:a2dcf0ebb5b5 152 mbed::SPI *spi; // pointer to new spi instance
Mahesh Phalke 11:a2dcf0ebb5b5 153 mbed::DigitalOut *ss; // pointer to new SS instance
Mahesh Phalke 11:a2dcf0ebb5b5 154 uint8_t num_of_words; // Number of words in SPI frame
Mahesh Phalke 11:a2dcf0ebb5b5 155 uint16_t rw_data; // SPI read data
Mahesh Phalke 11:a2dcf0ebb5b5 156 uint8_t data_index = 0; // Data index
mahphalke 8:70fc373a5f46 157
mahphalke 8:70fc373a5f46 158 if (desc) {
mahphalke 8:70fc373a5f46 159 spi = (SPI *)(((mbed_spi_desc *)(desc->extra))->spi_port);
mahphalke 8:70fc373a5f46 160 ss = (DigitalOut *)(((mbed_spi_desc *)(desc->extra))->slave_select);
Mahesh Phalke 11:a2dcf0ebb5b5 161
Mahesh Phalke 11:a2dcf0ebb5b5 162 spi->format(16, desc->mode);
Mahesh Phalke 11:a2dcf0ebb5b5 163
Mahesh Phalke 11:a2dcf0ebb5b5 164 /* Get the total number of words (16-bit) and leftover bytes (8-bit) */
Mahesh Phalke 11:a2dcf0ebb5b5 165 num_of_words = bytes_number / 2;
Mahesh Phalke 11:a2dcf0ebb5b5 166
mahphalke 8:70fc373a5f46 167 ss->write(GPIO_LOW);
Mahesh Phalke 11:a2dcf0ebb5b5 168
Mahesh Phalke 11:a2dcf0ebb5b5 169 while (num_of_words) {
Mahesh Phalke 11:a2dcf0ebb5b5 170 /* Form a 16-bit data to be written */
Mahesh Phalke 11:a2dcf0ebb5b5 171 rw_data = ((uint16_t)data[data_index + 1] | ((uint16_t)data[data_index] << 8));
Mahesh Phalke 11:a2dcf0ebb5b5 172
Mahesh Phalke 11:a2dcf0ebb5b5 173 /* Transmit a 16-bit data over SPI */
Mahesh Phalke 11:a2dcf0ebb5b5 174 rw_data = (uint16_t)spi->write(rw_data);
Mahesh Phalke 11:a2dcf0ebb5b5 175
Mahesh Phalke 11:a2dcf0ebb5b5 176 /* Extract the MSB and LSB from 16-bit read data */
Mahesh Phalke 11:a2dcf0ebb5b5 177 data[data_index++] = (uint8_t)(rw_data >> 8);
Mahesh Phalke 11:a2dcf0ebb5b5 178 data[data_index++] = (uint8_t)rw_data;
Mahesh Phalke 11:a2dcf0ebb5b5 179
Mahesh Phalke 11:a2dcf0ebb5b5 180 num_of_words--;
Mahesh Phalke 11:a2dcf0ebb5b5 181 }
Mahesh Phalke 11:a2dcf0ebb5b5 182
Mahesh Phalke 11:a2dcf0ebb5b5 183 /* Send the odd/single byte */
Mahesh Phalke 11:a2dcf0ebb5b5 184 if (bytes_number % 2) {
Mahesh Phalke 11:a2dcf0ebb5b5 185 spi->format(8, desc->mode);
Mahesh Phalke 11:a2dcf0ebb5b5 186 data[data_index] = (uint8_t)spi->write(data[data_index]);
mahphalke 8:70fc373a5f46 187 }
mahphalke 8:70fc373a5f46 188
mahphalke 8:70fc373a5f46 189 ss->write(GPIO_HIGH);
mahphalke 8:70fc373a5f46 190
mahphalke 8:70fc373a5f46 191 return SUCCESS;
mahphalke 8:70fc373a5f46 192 }
mahphalke 8:70fc373a5f46 193
mahphalke 8:70fc373a5f46 194 return FAILURE;
mahphalke 8:70fc373a5f46 195 }