Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
drv_spi.cpp
00001 /******************************************************************************* 00002 Simple SPI Transfer function 00003 00004 File Name: 00005 drv_spi.c 00006 00007 Summary: 00008 Initializes SPI 1. Transfers data over SPI. 00009 Uses SPI FIFO to speed up transfer. 00010 00011 Description: 00012 . 00013 00014 Remarks: 00015 00016 *******************************************************************************/ 00017 00018 // DOM-IGNORE-BEGIN 00019 /******************************************************************************* 00020 Copyright (c) 2016 Microchip Technology Inc. and its subsidiaries. 00021 You may use this software and any derivatives exclusively with Microchip products. 00022 00023 THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". 00024 NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, 00025 INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, 00026 AND FITNESS FOR A PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, 00027 COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION. 00028 00029 IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, 00030 INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER 00031 RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED 00032 OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, 00033 MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE 00034 WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. 00035 00036 MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE TERMS. 00037 *******************************************************************************/ 00038 // DOM-IGNORE-END 00039 00040 #include "mbed.h" 00041 #include <stdint.h> 00042 #include <stdbool.h> 00043 #include <stddef.h> 00044 #include <stdlib.h> 00045 #include "drv_spi.h" 00046 /* 00047 #include "peripheral/spi/plib_spi.h" // SPI PLIB Header 00048 #include "system_config.h" 00049 #include "system_definitions.h" 00050 */ 00051 00052 DigitalOut cs0(p13); 00053 DigitalOut cs1(p13); 00054 SPI spi0(p5, p6, p7); 00055 SPI spi1(p5, p6, p7); 00056 00057 int8_t DRV_SPI_ChipSelectAssert(uint8_t spiSlaveDeviceIndex, bool assert) 00058 { 00059 int8_t error = 0; 00060 00061 // Select Chip Select 00062 switch (spiSlaveDeviceIndex) { 00063 case DRV_CANFDSPI_INDEX_0: 00064 if (assert) cs0 = 0; 00065 else cs0 = 1; 00066 break; 00067 case DRV_CANFDSPI_INDEX_1: 00068 if (assert) cs1 = 0; 00069 else cs1 = 1; 00070 break; 00071 default: 00072 error = -1; 00073 break; 00074 } 00075 00076 return error; 00077 } 00078 00079 void DRV_SPI_Initialize() 00080 { 00081 spi0.format(8,0); /* 8 bits data tfr, mode0 */ 00082 spi0.frequency(1000000); /* 1 Mhz */ 00083 spi1.format(8,0); 00084 spi1.frequency(1000000); 00085 00086 return; 00087 } 00088 00089 int8_t DRV_SPI_TransferData(uint8_t spiSlaveDeviceIndex, uint8_t *SpiTxData, uint8_t *SpiRxData, uint16_t spiTransferSize) 00090 { 00091 int8_t error = 0; 00092 bool continueLoop; 00093 uint16_t txcounter = 0; 00094 uint16_t rxcounter = 0; 00095 uint8_t unitsTxed = 0; 00096 const uint8_t maxUnits = 16; 00097 int i; 00098 00099 // Assert CS 00100 error = DRV_SPI_ChipSelectAssert(spiSlaveDeviceIndex, true); 00101 if (error != 0) return error; 00102 00103 if (spiSlaveDeviceIndex == 0) { 00104 for (i=0; i<spiTransferSize; i++) { 00105 SpiRxData[i] = spi0.write(SpiTxData[i]); 00106 } 00107 } 00108 else if (spiSlaveDeviceIndex == 1) { 00109 for (i=0; i<spiTransferSize; i++) { 00110 SpiRxData[i] = spi1.write(SpiTxData[i]); 00111 } 00112 } 00113 00114 // De-assert CS 00115 error = DRV_SPI_ChipSelectAssert(spiSlaveDeviceIndex, false); 00116 00117 return error; 00118 } 00119
Generated on Mon Jul 25 2022 18:21:10 by
1.7.2