Update platform drivers
src/spi.cpp@8:70fc373a5f46, 2020-02-26 (annotated)
- Committer:
- mahphalke
- Date:
- Wed Feb 26 06:09:13 2020 +0000
- Revision:
- 8:70fc373a5f46
Structured platform drivers similar to github version by splitting functionality from single file into multiple modules.
Who changed what in which revision?
User | Revision | Line number | New 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 | #define SEND_BYTE 8 |
mahphalke | 8:70fc373a5f46 | 29 | |
mahphalke | 8:70fc373a5f46 | 30 | /******************************************************************************/ |
mahphalke | 8:70fc373a5f46 | 31 | /********************** Variables and User defined data types *****************/ |
mahphalke | 8:70fc373a5f46 | 32 | /******************************************************************************/ |
mahphalke | 8:70fc373a5f46 | 33 | |
mahphalke | 8:70fc373a5f46 | 34 | /******************************************************************************/ |
mahphalke | 8:70fc373a5f46 | 35 | /************************ Functions Declarations ******************************/ |
mahphalke | 8:70fc373a5f46 | 36 | /******************************************************************************/ |
mahphalke | 8:70fc373a5f46 | 37 | |
mahphalke | 8:70fc373a5f46 | 38 | /******************************************************************************/ |
mahphalke | 8:70fc373a5f46 | 39 | /************************ Functions Definitions *******************************/ |
mahphalke | 8:70fc373a5f46 | 40 | /******************************************************************************/ |
mahphalke | 8:70fc373a5f46 | 41 | |
mahphalke | 8:70fc373a5f46 | 42 | /** |
mahphalke | 8:70fc373a5f46 | 43 | * @brief Initialize the SPI communication peripheral. |
mahphalke | 8:70fc373a5f46 | 44 | * @param desc - The SPI descriptor. |
mahphalke | 8:70fc373a5f46 | 45 | * @param init_param - The structure that contains the SPI parameters. |
mahphalke | 8:70fc373a5f46 | 46 | * @return SUCCESS in case of success, FAILURE otherwise. |
mahphalke | 8:70fc373a5f46 | 47 | */ |
mahphalke | 8:70fc373a5f46 | 48 | int32_t spi_init_noos(struct spi_desc **desc, |
mahphalke | 8:70fc373a5f46 | 49 | const struct spi_init_param *param) |
mahphalke | 8:70fc373a5f46 | 50 | { |
mahphalke | 8:70fc373a5f46 | 51 | mbed::SPI *spi; // pointer to new spi instance |
mahphalke | 8:70fc373a5f46 | 52 | DigitalOut *ss; // pointer to new SS instance |
mahphalke | 8:70fc373a5f46 | 53 | mbed_spi_desc *mbed_desc; // Pointer to mbed spi descriptor |
mahphalke | 8:70fc373a5f46 | 54 | |
mahphalke | 8:70fc373a5f46 | 55 | if (desc) { |
mahphalke | 8:70fc373a5f46 | 56 | // Create the spi description object for the device |
mahphalke | 8:70fc373a5f46 | 57 | spi_desc * new_desc = (spi_desc *)malloc(sizeof(spi_desc)); |
mahphalke | 8:70fc373a5f46 | 58 | if (new_desc == NULL) { |
mahphalke | 8:70fc373a5f46 | 59 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 60 | } |
mahphalke | 8:70fc373a5f46 | 61 | |
mahphalke | 8:70fc373a5f46 | 62 | new_desc->chip_select = param->chip_select; |
mahphalke | 8:70fc373a5f46 | 63 | new_desc->mode = param->mode; |
mahphalke | 8:70fc373a5f46 | 64 | new_desc->max_speed_hz = param->max_speed_hz; |
mahphalke | 8:70fc373a5f46 | 65 | |
mahphalke | 8:70fc373a5f46 | 66 | // Configure and instantiate SPI protocol |
mahphalke | 8:70fc373a5f46 | 67 | spi = new SPI( |
mahphalke | 8:70fc373a5f46 | 68 | (PinName)(((mbed_spi_init_param *)param->extra)->spi_mosi_pin), |
mahphalke | 8:70fc373a5f46 | 69 | (PinName)(((mbed_spi_init_param *)param->extra)->spi_miso_pin), |
mahphalke | 8:70fc373a5f46 | 70 | (PinName)(((mbed_spi_init_param *)param->extra)->spi_clk_pin)); |
mahphalke | 8:70fc373a5f46 | 71 | |
mahphalke | 8:70fc373a5f46 | 72 | if (spi == NULL) { |
mahphalke | 8:70fc373a5f46 | 73 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 74 | } |
mahphalke | 8:70fc373a5f46 | 75 | |
mahphalke | 8:70fc373a5f46 | 76 | // Configure and instantiate slave select pin |
mahphalke | 8:70fc373a5f46 | 77 | ss = new DigitalOut((PinName)(new_desc->chip_select)); |
mahphalke | 8:70fc373a5f46 | 78 | if (ss == NULL) { |
mahphalke | 8:70fc373a5f46 | 79 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 80 | } |
mahphalke | 8:70fc373a5f46 | 81 | |
mahphalke | 8:70fc373a5f46 | 82 | // Create the SPI extra descriptor object to store new SPI instances |
mahphalke | 8:70fc373a5f46 | 83 | mbed_desc = (mbed_spi_desc *)malloc(sizeof(mbed_spi_desc)); |
mahphalke | 8:70fc373a5f46 | 84 | if (mbed_desc == NULL) { |
mahphalke | 8:70fc373a5f46 | 85 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 86 | } |
mahphalke | 8:70fc373a5f46 | 87 | |
mahphalke | 8:70fc373a5f46 | 88 | mbed_desc->spi_port = (SPI *)spi; |
mahphalke | 8:70fc373a5f46 | 89 | mbed_desc->slave_select = (DigitalOut *)ss; |
mahphalke | 8:70fc373a5f46 | 90 | new_desc->extra = (mbed_spi_desc *)mbed_desc; |
mahphalke | 8:70fc373a5f46 | 91 | |
mahphalke | 8:70fc373a5f46 | 92 | *desc = new_desc; |
mahphalke | 8:70fc373a5f46 | 93 | |
mahphalke | 8:70fc373a5f46 | 94 | /** |
mahphalke | 8:70fc373a5f46 | 95 | NOTE: Actual frequency of SPI clk will be somewhat device |
mahphalke | 8:70fc373a5f46 | 96 | dependent, relating to clock-settings, prescalars etc. If absolute |
mahphalke | 8:70fc373a5f46 | 97 | SPI frequency is required, consult your device documentation. |
mahphalke | 8:70fc373a5f46 | 98 | **/ |
mahphalke | 8:70fc373a5f46 | 99 | spi->frequency(param->max_speed_hz); |
mahphalke | 8:70fc373a5f46 | 100 | spi->format(SEND_BYTE, param->mode); // Stick to byte-multiples |
mahphalke | 8:70fc373a5f46 | 101 | spi->set_default_write_value(0x00); // code to write when reading back |
mahphalke | 8:70fc373a5f46 | 102 | ss->write(GPIO_HIGH); // set SS high |
mahphalke | 8:70fc373a5f46 | 103 | |
mahphalke | 8:70fc373a5f46 | 104 | return SUCCESS; |
mahphalke | 8:70fc373a5f46 | 105 | } |
mahphalke | 8:70fc373a5f46 | 106 | |
mahphalke | 8:70fc373a5f46 | 107 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 108 | } |
mahphalke | 8:70fc373a5f46 | 109 | |
mahphalke | 8:70fc373a5f46 | 110 | |
mahphalke | 8:70fc373a5f46 | 111 | /** |
mahphalke | 8:70fc373a5f46 | 112 | * @brief Free the resources allocated by spi_init(). |
mahphalke | 8:70fc373a5f46 | 113 | * @param desc - The SPI descriptor. |
mahphalke | 8:70fc373a5f46 | 114 | * @return SUCCESS in case of success, FAILURE otherwise. |
mahphalke | 8:70fc373a5f46 | 115 | */ |
mahphalke | 8:70fc373a5f46 | 116 | int32_t spi_remove(struct spi_desc *desc) |
mahphalke | 8:70fc373a5f46 | 117 | { |
mahphalke | 8:70fc373a5f46 | 118 | if (desc) { |
mahphalke | 8:70fc373a5f46 | 119 | // Free the SPI port object |
mahphalke | 8:70fc373a5f46 | 120 | if ((SPI *)(((mbed_spi_desc *)(desc->extra))->spi_port)) { |
mahphalke | 8:70fc373a5f46 | 121 | delete((SPI *)(((mbed_spi_desc *)(desc->extra))->spi_port)); |
mahphalke | 8:70fc373a5f46 | 122 | } |
mahphalke | 8:70fc373a5f46 | 123 | |
mahphalke | 8:70fc373a5f46 | 124 | // Free the SS port object |
mahphalke | 8:70fc373a5f46 | 125 | if ((DigitalOut *)(((mbed_spi_desc *)(desc->extra))->slave_select)) { |
mahphalke | 8:70fc373a5f46 | 126 | delete((DigitalOut *)(((mbed_spi_desc *)(desc->extra))->slave_select)); |
mahphalke | 8:70fc373a5f46 | 127 | } |
mahphalke | 8:70fc373a5f46 | 128 | |
mahphalke | 8:70fc373a5f46 | 129 | // Free the SPI extra descriptor object |
mahphalke | 8:70fc373a5f46 | 130 | if ((mbed_spi_desc *)(desc->extra)) { |
mahphalke | 8:70fc373a5f46 | 131 | free((mbed_spi_desc *)(desc->extra)); |
mahphalke | 8:70fc373a5f46 | 132 | } |
mahphalke | 8:70fc373a5f46 | 133 | |
mahphalke | 8:70fc373a5f46 | 134 | // Free the SPI descriptor object |
mahphalke | 8:70fc373a5f46 | 135 | free(desc); |
mahphalke | 8:70fc373a5f46 | 136 | |
mahphalke | 8:70fc373a5f46 | 137 | return SUCCESS; |
mahphalke | 8:70fc373a5f46 | 138 | } |
mahphalke | 8:70fc373a5f46 | 139 | |
mahphalke | 8:70fc373a5f46 | 140 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 141 | } |
mahphalke | 8:70fc373a5f46 | 142 | |
mahphalke | 8:70fc373a5f46 | 143 | |
mahphalke | 8:70fc373a5f46 | 144 | /** |
mahphalke | 8:70fc373a5f46 | 145 | * @brief Write and read data to/from SPI. |
mahphalke | 8:70fc373a5f46 | 146 | * @param desc - The SPI descriptor. |
mahphalke | 8:70fc373a5f46 | 147 | * @param data - The buffer with the transmitted/received data. |
mahphalke | 8:70fc373a5f46 | 148 | * @param bytes_number - Number of bytes to write/read. |
mahphalke | 8:70fc373a5f46 | 149 | * @return SUCCESS in case of success, FAILURE otherwise. |
mahphalke | 8:70fc373a5f46 | 150 | */ |
mahphalke | 8:70fc373a5f46 | 151 | int32_t spi_write_and_read(struct spi_desc *desc, |
mahphalke | 8:70fc373a5f46 | 152 | uint8_t *data, |
mahphalke | 8:70fc373a5f46 | 153 | uint16_t bytes_number) |
mahphalke | 8:70fc373a5f46 | 154 | { |
mahphalke | 8:70fc373a5f46 | 155 | mbed::SPI *spi; // pointer to new spi instance |
mahphalke | 8:70fc373a5f46 | 156 | DigitalOut *ss; // pointer to new SS instance |
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); |
mahphalke | 8:70fc373a5f46 | 161 | |
mahphalke | 8:70fc373a5f46 | 162 | ss->write(GPIO_LOW); |
mahphalke | 8:70fc373a5f46 | 163 | |
mahphalke | 8:70fc373a5f46 | 164 | for (size_t byte = 0 ; byte < bytes_number ; byte++) { |
mahphalke | 8:70fc373a5f46 | 165 | data[byte] = spi->write(data[byte]); |
mahphalke | 8:70fc373a5f46 | 166 | } |
mahphalke | 8:70fc373a5f46 | 167 | |
mahphalke | 8:70fc373a5f46 | 168 | ss->write(GPIO_HIGH); |
mahphalke | 8:70fc373a5f46 | 169 | |
mahphalke | 8:70fc373a5f46 | 170 | return SUCCESS; |
mahphalke | 8:70fc373a5f46 | 171 | } |
mahphalke | 8:70fc373a5f46 | 172 | |
mahphalke | 8:70fc373a5f46 | 173 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 174 | } |