Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 #include "board.h"
00092
00093 #include <stdbool.h>
00094 #include <stdio.h>
00095 #include <stdlib.h>
00096 #include <string.h>
00097
00098
00099
00100
00101
00102
00103 static const Pin spi_pins[] = {
00104 PIN_SPI_MISO,
00105 PIN_SPI_MOSI,
00106 PIN_SPI_SPCK,
00107 PIN_SPI_NPCS3
00108 };
00109
00110
00111
00112
00113
00114
00115 volatile uint32_t dwTimeStamp = 0;
00116
00117
00118 static uint32_t spiClock = 500000;
00119
00120
00121 static Spid SpiDma;
00122 static SpidCmd SpiCommand;
00123 static sXdmad Dma;
00124
00125 #define SPI0_CS0 0
00126 #define SPI0_CS1 1
00127 #define SPI0_CS2 2
00128 #define SPI0_CS3 3
00129
00130
00131 static const uint32_t clockConfigurations[3] = { 500000, 1000000, 5000000};
00132
00133
00134
00135
00136 COMPILER_ALIGNED(32) uint8_t pTxBuffer[] = "This is SPI LoopBack Test Buffer";
00137 COMPILER_ALIGNED(32) uint8_t pRxBuffer[30];
00138
00139
00140
00141
00142
00143
00144 void SPI0_Handler(void)
00145 {
00146 printf("%c", (char) SPI_Read(SPI0));
00147 }
00148
00149
00150
00151
00152
00153
00154 void XDMAC_Handler(void)
00155 {
00156 XDMAD_Handler(&Dma);
00157 }
00158
00159
00160
00161
00162
00163 static void SetClockConfiguration(uint8_t configuration)
00164 {
00165 spiClock = clockConfigurations[configuration];
00166 printf("Setting SPI master clock #%u ... \n\r",
00167 (unsigned int)clockConfigurations[configuration]);
00168 }
00169
00170
00171
00172
00173 static void SpiLoopBack(void)
00174 {
00175 uint8_t i;
00176
00177 printf( "\n\r-I- Configure SPI master\n\r" );
00178 SPI_Configure(SPI0, ID_SPI0, (SPI_MR_MSTR | SPI_MR_MODFDIS
00179 | SPI_MR_LLB | SPI_PCS( SPI0_CS3 )));
00180 SPI_ConfigureNPCS( SPI0,
00181 SPI0_CS3,
00182 SPI_DLYBCT( 1000, BOARD_MCK ) |
00183 SPI_DLYBS(1000, BOARD_MCK) |
00184 SPI_SCBR( spiClock, BOARD_MCK) );
00185
00186
00187 NVIC_ClearPendingIRQ(SPI0_IRQn);
00188 NVIC_SetPriority(SPI0_IRQn ,1);
00189 NVIC_EnableIRQ(SPI0_IRQn);
00190
00191 SPI_EnableIt(SPI0, SPI_IER_RDRF);
00192 SPI_Enable(SPI0);
00193
00194 for (i = 0; ;i++) {
00195 SPI_Write(SPI0, SPI0_CS3 , (uint16_t)pTxBuffer[i]);
00196 if (pTxBuffer[i] =='\0')
00197 break;
00198 }
00199 if (SPI_IsFinished(SPI0)) {
00200 SPI_Disable(SPI0);
00201 }
00202 }
00203
00204
00205
00206
00207 static void SpiLoopBackDma(void)
00208 {
00209 printf( "\n\r-I- Configure SPI master\n\r" );
00210 Dma.pXdmacs = XDMAC;
00211
00212 SpiCommand.TxSize = 30;
00213 SpiCommand.pTxBuff = (uint8_t *)pTxBuffer;
00214 SpiCommand.RxSize= 30;
00215 SpiCommand.pRxBuff = (uint8_t *)pRxBuffer;
00216 SpiCommand.spiCs = SPI0_CS3;
00217
00218
00219 SPID_Configure(&SpiDma, SPI0, ID_SPI0, (SPI_MR_MSTR | SPI_MR_MODFDIS
00220 | SPI_MR_LLB | SPI_PCS( SPI0_CS3 )), &Dma);
00221 SPI_ConfigureNPCS(SPI0,
00222 SPI0_CS3,
00223 SPI_DLYBCT( 1000, BOARD_MCK ) |
00224 SPI_DLYBS(1000, BOARD_MCK) |
00225 SPI_SCBR( spiClock, BOARD_MCK));
00226
00227 SPI_Enable(SPI0);
00228 SPID_SendCommand(&SpiDma, &SpiCommand);
00229 }
00230
00231
00232
00233
00234
00235 static void DisplayMenu( void )
00236 {
00237 uint32_t i;
00238
00239 printf("\n\rMenu :\n\r");
00240 printf("------\n\r");
00241
00242 for (i = 0; i < 3; i++) {
00243 printf(" %u: Set SPCK = %7u Hz\n\r",
00244 (unsigned int)i, (unsigned int)clockConfigurations[i]);
00245 }
00246 printf(" s: Perform SPI transfer start\n\r");
00247 printf(" d: Perform SPI DMA Transfer (first 30 bytes of Tx buffer)\n\r");
00248 printf(" h: Display menu \n\r\n\r");
00249 }
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 extern int main(void)
00260 {
00261 uint8_t ucKey;
00262
00263
00264 WDT_Disable(WDT);
00265
00266
00267 printf("\n\r-- SPI Example %s --\n\r", SOFTPACK_VERSION);
00268 printf("-- %s\n\r", BOARD_NAME);
00269 printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__, COMPILER_NAME);
00270
00271
00272 SCB_EnableICache();
00273 SCB_EnableDCache();
00274
00275 PIO_Configure(spi_pins, PIO_LISTSIZE(spi_pins));
00276
00277 DisplayMenu();
00278
00279 while (1) {
00280 ucKey = DBG_GetChar();
00281
00282 switch (ucKey) {
00283 case 'h':
00284 DisplayMenu();
00285 break;
00286 case 's':
00287 SpiLoopBack();
00288 break;
00289
00290 case 'd':
00291 SpiLoopBackDma();
00292 default:
00293
00294 if ((ucKey >= '0') && (ucKey <= ('0' + 2))) {
00295 SetClockConfiguration(ucKey - '0');
00296 }
00297 break;
00298 }
00299 }
00300 }
00301