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
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 #include "board.h"
00102
00103 #include <stdbool.h>
00104 #include <stdio.h>
00105 #include <string.h>
00106
00107
00108
00109
00110
00111
00112 #define BUFFER_SIZE 1000
00113
00114
00115 #define BAUDRATE_RS485 256000
00116
00117
00118 #define USART USART0
00119 #define ID_USART ID_USART0
00120
00121
00122
00123
00124
00125 static sXdmad dmad;
00126
00127 static uint32_t usartDmaRxChannel;
00128 static uint32_t usartDmaTxChannel;
00129
00130
00131 #define PIN_USART0_CTS_IOR {PIO_PB2C_CTS0, PIOB, ID_PIOB, PIO_OUTPUT_0, PIO_DEFAULT}
00132
00133
00134 const Pin pins[] = { PIN_USART0_RXD, PIN_USART0_TXD, PIN_USART0_CTS_IOR, PIN_USART0_RTS };
00135
00136
00137 char palette[BUFFER_SIZE]=
00138 "**************************************************************************\n\r\
00139 * This application gives an example of how to use USART in RS485 mode.\n\r\
00140 * The USART features the RS485 mode to enable line driver control.\n\r\
00141 * While operating in RS485 mode, the USART behaves as though in asynchronous \n\r\
00142 * or synchronous mode and configuration of all the parameters is possible \n\r\
00143 * \n\r\
00144 * The difference is that the RTS pin is driven high when the transmitter\n\r\
00145 * is operating. The behavior of the RTS pin is controlled by the TXEMPTY bit.\n\r\
00146 * \n\r\
00147 **************************************************************************\n\r\
00148 ";
00149
00150
00151 COMPILER_ALIGNED(8) static char Buffer[BUFFER_SIZE];
00152
00153
00154 COMPILER_ALIGNED(8) static char pRecvBufferUSART[BUFFER_SIZE];
00155
00156
00157 static volatile uint8_t recvDone = 0;
00158
00159
00160 static volatile uint8_t transDone = 0;
00161
00162
00163
00164
00165
00166
00167
00168
00169 static void _ConfigureUsart( void )
00170 {
00171 uint32_t mode = US_MR_USART_MODE_RS485 | US_MR_USCLKS_MCK
00172 | US_MR_CHRL_8_BIT | US_MR_PAR_NO | US_MR_NBSTOP_1_BIT
00173 | US_MR_CHMODE_NORMAL;
00174
00175
00176 PMC_EnablePeripheral( ID_USART ) ;
00177
00178
00179 USART_Configure( USART, mode, BAUDRATE_RS485, BOARD_MCK ) ;
00180
00181
00182 USART_SetTransmitterEnabled( USART, 1 ) ;
00183 USART_SetReceiverEnabled( USART, 1 ) ;
00184 }
00185
00186
00187
00188
00189 void XDMAC_Handler(void)
00190 {
00191 XDMAD_Handler(&dmad);
00192 }
00193
00194
00195
00196
00197 static void _DmaRxCallback( uint32_t channel, void* pArg )
00198 {
00199 channel = channel;
00200 pArg = pArg;
00201 recvDone = 1;
00202 }
00203
00204
00205
00206
00207 static void _DmaTxCallback( uint32_t channel, void* pArg )
00208 {
00209 channel = channel;
00210 pArg = pArg;
00211 transDone = 1;
00212 }
00213
00214
00215
00216
00217 static void _DmaUsartTx( void )
00218 {
00219 sXdmadCfg xdmadCfg;
00220 xdmadCfg.mbr_ubc = BUFFER_SIZE;
00221 xdmadCfg.mbr_sa = (uint32_t)Buffer;
00222 xdmadCfg.mbr_da = (uint32_t)&USART->US_THR;
00223 xdmadCfg.mbr_cfg = XDMAC_CC_TYPE_PER_TRAN |
00224 XDMAC_CC_MBSIZE_SINGLE |
00225 XDMAC_CC_DSYNC_MEM2PER |
00226 XDMAC_CC_CSIZE_CHK_1 |
00227 XDMAC_CC_DWIDTH_BYTE |
00228 XDMAC_CC_SIF_AHB_IF0 |
00229 XDMAC_CC_DIF_AHB_IF1 |
00230 XDMAC_CC_SAM_INCREMENTED_AM |
00231 XDMAC_CC_DAM_FIXED_AM |
00232 XDMAC_CC_PERID(XDMAIF_Get_ChannelNumber( ID_USART, XDMAD_TRANSFER_TX ));
00233
00234 xdmadCfg.mbr_bc = 0;
00235 xdmadCfg.mbr_ds = 0;
00236 xdmadCfg.mbr_sus = 0;
00237 xdmadCfg.mbr_dus = 0;
00238 XDMAD_ConfigureTransfer( &dmad, usartDmaTxChannel, &xdmadCfg, 0, 0,
00239 XDMAC_CIE_BIE |
00240 XDMAC_CIE_DIE |
00241 XDMAC_CIE_FIE |
00242 XDMAC_CIE_RBIE |
00243 XDMAC_CIE_WBIE |
00244 XDMAC_CIE_ROIE);
00245 XDMAD_StartTransfer( &dmad, usartDmaTxChannel);
00246 }
00247
00248
00249
00250
00251 static void _DmaUsartRx( void )
00252 {
00253 uint32_t status;
00254
00255 status = USART->US_CSR;
00256 if ((status & US_CSR_RXRDY) == US_CSR_RXRDY )
00257 {
00258 status = USART->US_RHR;
00259 }
00260 sXdmadCfg xdmadCfg;
00261 xdmadCfg.mbr_ubc = BUFFER_SIZE;
00262 xdmadCfg.mbr_sa = (uint32_t)&USART->US_RHR;
00263 xdmadCfg.mbr_da = (uint32_t)pRecvBufferUSART;
00264 xdmadCfg.mbr_cfg = XDMAC_CC_TYPE_PER_TRAN |
00265 XDMAC_CC_MBSIZE_SINGLE |
00266 XDMAC_CC_DSYNC_PER2MEM |
00267 XDMAC_CC_CSIZE_CHK_1 |
00268 XDMAC_CC_DWIDTH_BYTE |
00269 XDMAC_CC_SIF_AHB_IF1 |
00270 XDMAC_CC_DIF_AHB_IF0 |
00271 XDMAC_CC_SAM_FIXED_AM |
00272 XDMAC_CC_DAM_INCREMENTED_AM |
00273 XDMAC_CC_PERID(XDMAIF_Get_ChannelNumber( ID_USART, XDMAD_TRANSFER_RX ));
00274
00275 xdmadCfg.mbr_bc = 0;
00276 xdmadCfg.mbr_ds = 0;
00277 xdmadCfg.mbr_sus = 0;
00278 xdmadCfg.mbr_dus = 0;
00279 XDMAD_ConfigureTransfer( &dmad, usartDmaRxChannel, &xdmadCfg, 0, 0,
00280 XDMAC_CIE_BIE |
00281 XDMAC_CIE_DIE |
00282 XDMAC_CIE_FIE |
00283 XDMAC_CIE_RBIE |
00284 XDMAC_CIE_WBIE |
00285 XDMAC_CIE_ROIE);
00286 XDMAD_StartTransfer( &dmad, usartDmaRxChannel);
00287 }
00288
00289
00290
00291
00292
00293 static void _ConfigureDma( void )
00294 {
00295
00296 XDMAD_Initialize( &dmad, 0 );
00297
00298
00299 usartDmaTxChannel = XDMAD_AllocateChannel( &dmad, XDMAD_TRANSFER_MEMORY, ID_USART);
00300 usartDmaRxChannel = XDMAD_AllocateChannel( &dmad, ID_USART, XDMAD_TRANSFER_MEMORY);
00301 if ( usartDmaTxChannel == XDMAD_ALLOC_FAILED || usartDmaRxChannel == XDMAD_ALLOC_FAILED )
00302 {
00303 printf("XDMA channel allocat failed!\n\r");
00304 while(1);
00305 }
00306
00307
00308 XDMAD_SetCallback(&dmad, usartDmaRxChannel,(XdmadTransferCallback)_DmaRxCallback, 0);
00309
00310 XDMAD_SetCallback(&dmad, usartDmaTxChannel,(XdmadTransferCallback)_DmaTxCallback, 0);
00311 XDMAD_PrepareChannel( &dmad, usartDmaRxChannel );
00312 XDMAD_PrepareChannel( &dmad, usartDmaTxChannel);
00313
00314 }
00315
00316
00317
00318
00319 static void _DisplayMenu( void )
00320 {
00321 printf("Menu :\n\r");
00322 printf("------\n\r");
00323 printf(" - t: Transmit pattern to RS485\n\r");
00324 printf(" - r: Receive data from RS485 \n\r");
00325 printf(" - m: Display menu \n\r");
00326 }
00327
00328
00329
00330
00331
00332 static void _DumpInfo( char *buf, uint32_t size )
00333 {
00334 uint32_t i = 0 ;
00335
00336 while ( (i < size) && (buf[i] != 0) )
00337 {
00338 printf( "%c", buf[i++] ) ;
00339 }
00340 }
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 extern int main( void )
00351 {
00352 uint8_t ucKey;
00353
00354
00355 WDT_Disable( WDT ) ;
00356
00357 SCB_EnableICache();
00358 SCB_EnableDCache();
00359
00360
00361 printf( "-- USART RS485 Example %s --\n\r", SOFTPACK_VERSION ) ;
00362 printf( "-- %s\n\r", BOARD_NAME ) ;
00363 printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00364
00365
00366 PIO_Configure( pins, PIO_LISTSIZE( pins ) ) ;
00367
00368
00369 _DisplayMenu() ;
00370
00371
00372 _ConfigureDma();
00373
00374 memcpy(Buffer, palette, BUFFER_SIZE);
00375 SCB_CleanDCache();
00376
00377
00378 _ConfigureUsart();
00379
00380 NVIC_EnableIRQ(XDMAC_IRQn);
00381
00382 while ( 1 )
00383 {
00384 ucKey = DBG_GetChar() ;
00385 switch ( ucKey )
00386 {
00387 case 't': case 'T':
00388 {
00389 printf("-I- RS485 Transmitting ... \n\r");
00390 _DmaUsartTx();
00391 while(!transDone);
00392 printf("-I- RS485 Transmitting completed \n\r");
00393 transDone = 0;
00394 break;
00395 }
00396 case 'r': case 'R':
00397 {
00398 printf("-I- RS485 receiving ... \n\r");
00399 SCB_CleanInvalidateDCache();
00400 recvDone = 0;
00401 _DmaUsartRx();
00402 while(!recvDone);
00403
00404 _DumpInfo(pRecvBufferUSART, BUFFER_SIZE);
00405 printf("-I- RS485 Receiving completed \n\r");
00406 memset(pRecvBufferUSART, 0, sizeof(pRecvBufferUSART));
00407 break;
00408 }
00409 case 'm': case 'M':
00410 {
00411 _DisplayMenu() ;
00412 break;
00413 }
00414 }
00415 }
00416 }
00417