//master send
void RS485_9bitModeMaster()
{
volatile int32_t i32;
uint8_t g_u8SendDataGroup1[10] = {0};
uint8_t g_u8SendDataGroup2[10] = {0};
uint8_t g_u8SendDataGroup3[10] = {0};
uint8_t g_u8SendDataGroup4[10] = {0};
printf("\n\n");
printf("+-----------------------------------------------------------+\n");
printf("| RS485 9-bit Master Test |\n");
printf("+-----------------------------------------------------------+\n");
printf("| The function will send different address with 10 data |\n");
printf("| bytes to test RS485 9-bit mode. Please connect TX/RX to |\n");
printf("| another board and wait its ready to receive. |\n");
printf("| Press any key to start... |\n");
printf("+-----------------------------------------------------------+\n\n");
GetChar();
/* Set RS485-Master as AUD mode */
/* Enable AUD mode to HW control RTS pin automatically */
/* You also can use GPIO to control RTS pin for replacing AUD mode*/
UART_SelectRS485Mode(UART1, UART_ALT_CSR_RS485_AUD_Msk, 0);
/* Set RTS pin active level as high level active */
UART1->MCR &= ~UART_MCR_LEV_RTS_Msk;
UART1->MCR |= UART_RTS_IS_HIGH_LEV_ACTIVE;
/* Set TX delay time */
UART1->TOR = 0x2000;
/* Prepare Data to transmit*/
for(i32 = 0; i32 < 10; i32++)
{
g_u8SendDataGroup1[i32] = i32;
g_u8SendDataGroup2[i32] = i32 + 10;
g_u8SendDataGroup3[i32] = i32 + 20;
g_u8SendDataGroup4[i32] = i32 + 30;
}
/* Send different address and data for test */
printf("Send Address %x and data 0~9\n", MATCH_ADDRSS1);
RS485_SendAddressByte(MATCH_ADDRSS1);
RS485_SendDataByte(g_u8SendDataGroup1, 10);
printf("Send Address %x and data 10~19\n", UNMATCH_ADDRSS1);
RS485_SendAddressByte(UNMATCH_ADDRSS1);
RS485_SendDataByte(g_u8SendDataGroup2, 10);
printf("Send Address %x and data 20~29\n", MATCH_ADDRSS2);
RS485_SendAddressByte(MATCH_ADDRSS2);
RS485_SendDataByte(g_u8SendDataGroup3, 10);
printf("Send Address %x and data 30~39\n", UNMATCH_ADDRSS2);
RS485_SendAddressByte(UNMATCH_ADDRSS2);
RS485_SendDataByte(g_u8SendDataGroup4, 10);
printf("Transfer Done\n");
}
//slave
void RS485_9bitModeSlave()
{
/* Set Data Format, only need parity enable whatever parity ODD/EVEN */
UART_SetLine_Config(UART1, 0, UART_WORD_LEN_8, UART_PARITY_EVEN, UART_STOP_BIT_1);
/* Set RTS pin active level as high level active */
UART1->MCR &= ~UART_MCR_LEV_RTS_Msk;
UART1->MCR |= UART_RTS_IS_HIGH_LEV_ACTIVE;
#if(IS_USE_RS485NMM == 1)
printf("+-----------------------------------------------------------+\n");
printf("| Normal Multidrop Operation Mode |\n");
printf("+-----------------------------------------------------------+\n");
printf("| The function is used to test 9-bit slave mode. |\n");
printf("| Only Address %x and %x,data can receive |\n", MATCH_ADDRSS1, MATCH_ADDRSS2);
printf("+-----------------------------------------------------------+\n");
/* Set RX_DIS enable before set RS485-NMM mode */
UART1->FCR |= UART_FCR_RX_DIS_Msk;
/* Set RS485-NMM Mode */
UART_SelectRS485Mode(UART1, UART_ALT_CSR_RS485_NMM_Msk | UART_ALT_CSR_RS485_AUD_Msk, 0);
/* Set RS485 address detection enable */
UART1->ALT_CSR |= UART_ALT_CSR_RS485_ADD_EN_Msk;
#else
printf("Auto Address Match Operation Mode\n");
printf("+-----------------------------------------------------------+\n");
printf("| The function is used to test 9-bit slave mode. |\n");
printf("| Auto Address Match Operation Mode |\n");
printf("+-----------------------------------------------------------+\n");
printf("|Only Address %x,data can receive |\n", MATCH_ADDRSS1);
printf("+-----------------------------------------------------------+\n");
/* Set RS485-AAD Mode and address match is 0xC0 */
UART_SelectRS485Mode(UART1, UART_ALT_CSR_RS485_AAD_Msk | UART_ALT_CSR_RS485_AUD_Msk, MATCH_ADDRSS1);
/* Set RS485 address detection enable */
UART1->ALT_CSR |= UART_ALT_CSR_RS485_ADD_EN_Msk;
#endif
/* Enable RDA\RLS\Time-out Interrupt */
UART_ENABLE_INT(UART1, (UART_IER_RDA_IEN_Msk | UART_IER_RLS_IEN_Msk | UART_IER_RTO_IEN_Msk));
/* Enable UART1 interrupt */
NVIC_EnableIRQ(UART1_IRQn);
printf("Ready to receive data...(Press any key to stop test)\n");
GetChar();
/* Flush Rx FIFO */
UART1->FCR |= UART_FCR_RFR_Msk;
/* Disable RDA/RLS/RTO interrupt */
UART_DISABLE_INT(UART1, (UART_IER_RDA_IEN_Msk | UART_IER_RLS_IEN_Msk | UART_IER_RTO_IEN_Msk));
/* Set UART Function */
UART_Open(UART1, 115200);
printf("\nEnd test\n");
}
/*---------------------------------------------------------------------------------------------------------*/
/* ISR to handle UART Channel 1 interrupt event */
/*---------------------------------------------------------------------------------------------------------*/
void UART1_IRQHandler(void)
{
RS485_HANDLE();
}
void RS485_HANDLE()
{
volatile uint32_t addr = 0;
volatile uint32_t regRX = 0xFF;
volatile uint32_t u32IntSts = UART1->ISR;;
if(UART_GET_INT_FLAG(UART1, UART_ISR_RLS_INT_Msk) && UART_GET_INT_FLAG(UART1, UART_ISR_RDA_INT_Msk)) /* RLS INT & RDA INT */
{
//if(UART1->FSR & UART_FSR_RS485_ADD_DETF_Msk) /* ADD_IF, RS485 mode */
if(UART_RS485_GET_ADDR_FLAG(UART1))
{
//addr = UART1->DATA;
addr = UART_READ(UART1);
UART_RS485_CLEAR_ADDR_FLAG(UART1); /* clear ADD_IF flag */
printf("\nAddr=0x%x,Get:", addr);
#if (IS_USE_RS485NMM ==1) //RS485_NMM
/* if address match, enable RX to receive data, otherwise to disable RX. */
/* In NMM mode,user can decide multi-address filter. In AAD mode,only one address can set */
if((addr == MATCH_ADDRSS1) || (addr == MATCH_ADDRSS2))
{
UART1->FCR &= ~ UART_FCR_RX_DIS_Msk; /* Enable RS485 RX */
}
else
{
printf("\n");
UART1->FCR |= UART_FCR_RX_DIS_Msk; /* Disable RS485 RX */
UART1->FCR |= UART_FCR_RFR_Msk; /* Clear data from RX FIFO */
}
#endif
}
}
else if(UART_GET_INT_FLAG(UART1, UART_ISR_RDA_INT_Msk) || UART_GET_INT_FLAG(UART1, UART_ISR_TOUT_INT_Msk)) /* Rx Ready or Time-out INT*/
{
/* Handle received data */
printf("%d,", UART1->RBR);
}
else if(UART_GET_INT_FLAG(UART1, UART_ISR_BUF_ERR_INT_Msk)) /* Buffer Error INT */
{
printf("\nBuffer Error...\n");
UART_ClearIntFlag(UART1, UART_ISR_BUF_ERR_INT_Msk);
}
} |