|
| 1 | +/* mbed Microcontroller Library |
| 2 | + ******************************************************************************* |
| 3 | + * Copyright (c) 2020, STMicroelectronics |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 10 | + * this list of conditions and the following disclaimer. |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| 15 | + * may be used to endorse or promote products derived from this software |
| 16 | + * without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 22 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + ******************************************************************************* |
| 29 | + */ |
| 30 | +#include "mbed_assert.h" |
| 31 | +#include "mbed_error.h" |
| 32 | +#include "spi_api.h" |
| 33 | + |
| 34 | +#if DEVICE_SPI |
| 35 | + |
| 36 | +#include "cmsis.h" |
| 37 | +#include "pinmap.h" |
| 38 | +#include "PeripheralPins.h" |
| 39 | + |
| 40 | + |
| 41 | +#if DEVICE_SPI_ASYNCH |
| 42 | +#define SPI_S(obj) (( struct spi_s *)(&(obj->spi))) |
| 43 | +#else |
| 44 | +#define SPI_S(obj) (( struct spi_s *)(obj)) |
| 45 | +#endif |
| 46 | + |
| 47 | +/* |
| 48 | + * Only the frequency is managed in the family specific part |
| 49 | + * the rest of SPI management is common to all STM32 families |
| 50 | + */ |
| 51 | +int spi_get_clock_freq(spi_t *obj) |
| 52 | +{ |
| 53 | + struct spi_s *spiobj = SPI_S(obj); |
| 54 | + int spi_hz = 0; |
| 55 | + |
| 56 | + /* Get source clock depending on SPI instance */ |
| 57 | + switch ((int)spiobj->spi) { |
| 58 | +#if defined SPI1_BASE |
| 59 | + case SPI_1: |
| 60 | + /* SPI_1. Source CLK is PCKL2 */ |
| 61 | + spi_hz = HAL_RCC_GetPCLK2Freq(); |
| 62 | + break; |
| 63 | +#endif |
| 64 | +#if defined SPI2_BASE |
| 65 | + case SPI_2: |
| 66 | + /* SPI_2 and SPI_3. Source CLK is PCKL1 */ |
| 67 | + spi_hz = HAL_RCC_GetPCLK1Freq(); |
| 68 | + break; |
| 69 | +#endif |
| 70 | +#if defined SPI3_BASE |
| 71 | + case SPI_3: |
| 72 | + /* SPI_2 and SPI_3. Source CLK is PCKL1 */ |
| 73 | + spi_hz = HAL_RCC_GetPCLK1Freq(); |
| 74 | + break; |
| 75 | +#endif |
| 76 | +#if defined SPI4_BASE |
| 77 | + case SPI_4: |
| 78 | + /* SPI_4. Source CLK is PCKL2 */ |
| 79 | + spi_hz = HAL_RCC_GetPCLK2Freq(); |
| 80 | + break; |
| 81 | +#endif |
| 82 | + default: |
| 83 | + error("CLK: SPI instance not set"); |
| 84 | + break; |
| 85 | + } |
| 86 | + return spi_hz; |
| 87 | +} |
| 88 | + |
| 89 | +#endif |
0 commit comments