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