radioserver.c
来自「simulink real-time workshop for dragon12」· C语言 代码 · 共 1,395 行 · 第 1/3 页
C
1,395 行
/* store the clientFlags */
clientFlags = cFlags;
/* initialise the SPI */
SPI_Init(baudRate);
for(i=0; i<CLIENT_COUNT; i++) {
commStates[i].id = i;
/* init the transmit ring buffers */
RB_INIT(&commStates[i].outRB, commStates[i].outBuf);
/* init the receive ring buffers */
RB_INIT(&commStates[i].inRB, commStates[i].inBuf);
/* lastTID is 0x00 */
commStates[i].lastTID = 0x00;
/* notify clients that the server has been reset */
commStates[i].reset = HEADER_RESET_MASK;
commStates[i].failureCount = 0;
}
/* Select the first flagged client as the current client, and
simultaneously check for the no client flag set error.
If a client's flag is not set, it will not be polled. */
if(clientFlags & CLIENT_FLAG_0) {
cc = &commStates[0];
} else if(clientFlags & CLIENT_FLAG_1) {
cc = &commStates[1];
} else if(clientFlags & CLIENT_FLAG_2) {
cc = &commStates[2];
} else if(clientFlags & CLIENT_FLAG_3) {
cc = &commStates[3];
} else if(clientFlags & CLIENT_FLAG_4) {
cc = &commStates[4];
} else {
// no clients are active, return error
return INIT_ERROR_NO_CLIENTS_SET;
}
/* set the first four bytes of the receive address of pipe 1
as serverAddress */
rx1_address_array[1] = serverAddressHeader & 0xff;
rx1_address_array[2] = (serverAddressHeader >> 8) & 0xff;
rx1_address_array[3] = (serverAddressHeader >> 16) & 0xff;
rx1_address_array[4] = (serverAddressHeader >> 24) & 0xff;
/* now append the tail of the client 0 address */
rx1_address_array[0] = CLIENT_TAIL_0;
/* set the first four of the client transmit bytes */
tx_address_array[1] = clientAddressHeader & 0xff;
tx_address_array[2] = (clientAddressHeader >> 8) & 0xff;
tx_address_array[3] = (clientAddressHeader >> 16) & 0xff;
tx_address_array[4] = (clientAddressHeader >> 24) & 0xff;
/* seems to be required? May be an artifact of original buggy SPI code... */
SPI_OutChar(NOP);
/*
bit EN_AA Register
7:6 00 Reserved : Must be zero
5 1 : Enable auto-ACK on data pipe 5
4 1 : Enable auto-ACK on data pipe 4
3 1 : Enable auto-ACK on data pipe 3
2 1 : Enable auto-ACK on data pipe 2
1 1 : Enable auto-ACK on data pipe 1
0 1 : Enable auto-ACK on data pipe 0
*/
SPI_OutChar2(W_EN_AA, 0x3f);
/*
bit EN_RXADDR Register
7:6 0 Reserved : Must be zero
5 X ERX_P5 : Client 4 enable
4 X ERX_P4 : Client 3 enable
3 X ERX_P3 : Client 2 enable
2 X ERX_P2 : Client 1 enable
1 X ERX_P1 : Client 0 enable
0 1 ERX_P0 : Reserved for ACKs
*/
SPI_OutChar2(W_EN_RXADDR, clientFlags | 0x01);
/*
bit SETUP_AW Register
7:2 0 Reserved : Must be zero
1:0 11 AW : RX/TA Address field width = 5
*/
SPI_OutChar2(W_SETUP_AW, 0x03);
/*
bit SETUP_RETR Register
7:4 01 ARD (Auto re-transmit delay)
3:1 15 ARC (Auto re-transmit count)
*/
SPI_OutChar2(W_SETUP_RETR, 0x0f);
/*
Set the RF_CH, which RF channel to receive/transmit on
*/
SPI_OutChar2(W_RF_CH, (rfChannel & RF_CH_MASK));
/*
bit RF_SETUP Register
7:5 0 Reserved
4 0 PLL_LOCK : Use Default
3 X RF_DR : RF baud rate, depends on RF_BAUD_RATE
2:1 11 RF_PWR : 0dBm output power in TX mode (max)
0 1 LNA_HCURR : Setup LNA gain
*/
#ifdef SLOW_RF_BAUD_RATE
SPI_OutChar2(W_RF_SETUP, 0x07);
#else // FAST_RF_BAUD_RATE
SPI_OutChar2(W_RF_SETUP, 0x0f);
#endif
/*
RX_ADDR_P1 Register
Receive address for client 0.
*/
SPI_OutChar3(W_RX_ADDR_P1, rx1_address_array, ADDRESS_LENGTH);
/*
Perform a simple read to make sure that memory is being
correctly written.
*/
SPI_Read(R_RX_ADDR_P1, test, ADDRESS_LENGTH);
/*
Will return NO_POWER if the read RX1 address does not match
the address written, meaning that there's no power to the
module.
*/
if(test[0] != rx1_address_array[0]
|| test[1] != rx1_address_array[1]
|| test[2] != rx1_address_array[2]
|| test[3] != rx1_address_array[3]
|| test[4] != rx1_address_array[4]) {
return INIT_ERROR_NO_POWER;
}
/*
RX_ADDR_P2 Register
LSB of receive address for client 1.
*/
SPI_OutChar2(W_RX_ADDR_P2, CLIENT_TAIL_1);
/*
RX_ADDR_P3 Register
LSB of receive address for client 2.
*/
SPI_OutChar2(W_RX_ADDR_P3, CLIENT_TAIL_2);
/*
RX_ADDR_P4 Register
LSB of receive address for client 3.
*/
SPI_OutChar2(W_RX_ADDR_P4, CLIENT_TAIL_3);
/*
RX_ADDR_P5 Register
LSB of receive address for client 4.
*/
SPI_OutChar2(W_RX_ADDR_P5, CLIENT_TAIL_4);
/*
RW_PW_0 Register
Set the width of data pipe 0 (Reserved for ACKs)
*/
SPI_OutChar2(W_RX_PW_0, 0);
/*
RW_PW_1 Register
Set the width of the RX payload on pipe 1
*/
SPI_OutChar2(W_RX_PW_1, PAYLOAD_LEN);
/*
RW_PW_2 Register
Set the width of the RX payload on pipe 2
*/
SPI_OutChar2(W_RX_PW_2, PAYLOAD_LEN);
/*
RW_PW_3 Register
Set the width of the RX payload on pipe 3
*/
SPI_OutChar2(W_RX_PW_3, PAYLOAD_LEN);
/*
RW_PW_4 Register
Set the width of the RX payload on pipe 4
*/
SPI_OutChar2(W_RX_PW_4, PAYLOAD_LEN);
/*
RW_PW_5 Register
Set the width of the RX payload on pipe 5
*/
SPI_OutChar2(W_RX_PW_5, PAYLOAD_LEN);
/* clear all previous interrupts */
SPI_OutChar2(W_STATUS, 0x70);
/* flush buffers */
SPI_OutChar(FLUSH_TX);
SPI_OutChar(FLUSH_RX);
/* start polling clients */
RadioServer_Transmit(UPDATE_ADDRESS);
setState(STATE_WAIT_FOR_EOT);
/* start the ticker */
RadioServer_RestartTick();
return 0;
}
/*
Diagnostic to check for carriers (jammed signal). Infinite loop.
Cycle through all 128 channels. If Carrier is detected on a channel,
increment its corresponding element in the carrier_detect array.
See radioServer.h for more info.
*/
void RadioServer_CheckCarrier(void) {
unsigned char i,j,k,cd;
unsigned short carrier_detect[128];
while(1) {
for(i = 127; i != 0; i++) {
CE = 0;
/*
bit CONFIG Register
7 0 Reserved
6 1 MASK_RX_DR : Don't reflect RX_DR on IRQ pin
5 1 MASK_TX_DS : Don't reflect TX_DS on IRQ pin
4 1 MASK_MAX_RT : Don't reflect MAX_RT on IRQ pin
3 1 EN_CRC : Enable CRC
2 1 CRCO : 2 byte CRC
1 1 PWR_UP : Power Up
0 1 PRIM_RX : PRX
Enter receive mode
*/
SPI_OutChar2(W_CONFIG, 0x7f);
/* select a new RF channel */
SPI_OutChar2(W_RF_CH, i);
CE = 1;
for(j=255; j!=0; j--) {
for(k=255; k!=0; k--) {
/* Specification seems to imply 258us delay is required here.
This will delay for quite a bit longer than that. */
}
}
/* read the one byte long CD register */
SPI_Read(R_CD, &cd, 1);
/* if carrier is detected, increment carrier_detect[i] */
if(cd) {
/* protect against overflow */
if(carrier_detect[i] != 0xffff) {
carrier_detect[i]++;
}
}
}
/*
Each time all channels have been tested, output the results to date.
The longer this is left to run, a better average of the results should
be obtained.
*/
#if (DEBUG2SCI0 > 0)
for(i = 127; i != 0; i--) {
SCI0_OutUDec(i);
SCI0_OutString(", ");
SCI0_OutUDec(carrier_detect[i]);
SCI0_OutChar(CR);
SCI0_OutChar(LF);
}
#endif /* DEBUG2SCI0 */
}
}
void RadioServer_AddClient(unsigned char client) {
switch(client) {
case 0:
clientFlags |= CLIENT_FLAG_0;
break;
case 1:
clientFlags |= CLIENT_FLAG_1;
break;
case 2:
clientFlags |= CLIENT_FLAG_2;
break;
case 3:
clientFlags |= CLIENT_FLAG_3;
break;
case 4:
clientFlags |= CLIENT_FLAG_4;
break;
}
commStates[client].failureCount = 0;
}
void RadioServer_RemoveClient(unsigned char client) {
switch(client) {
case 0:
clientFlags &= ~CLIENT_FLAG_0;
break;
case 1:
clientFlags &= ~CLIENT_FLAG_1;
break;
case 2:
clientFlags &= ~CLIENT_FLAG_2;
break;
case 3:
clientFlags &= ~CLIENT_FLAG_3;
break;
case 4:
clientFlags &= ~CLIENT_FLAG_4;
break;
}
commStates[client].failureCount = 0;
}
unsigned char RadioServer_OutChar(unsigned char client,
unsigned char val) {
return RB_TX_PUSH(&commStates[client].outRB, val);
}
void RadioServer_OutCharArray(unsigned char client,
unsigned char *pt,
unsigned char len) {
unsigned char i;
for(i=0; i<len; i++) {
while(RadioServer_OutChar(client, pt[i]));
}
}
void RadioServer_OutString(unsigned char client,
unsigned char *pt) {
while(*pt) {
while(RadioServer_OutChar(client, *pt++));
while(RadioServer_OutChar(client, 0x00));
}
}
unsigned char RadioServer_IsOutFull(unsigned char client) {
return RB_TX_FULL(&commStates[client].outRB);
}
unsigned char RadioServer_IsOutEmpty(unsigned char client) {
return RB_EMPTY(&commStates[client].outRB);
}
unsigned char RadioServer_IsInFull(unsigned char client) {
return RB_FULL(&commStates[client].inRB);
}
unsigned char RadioServer_IsInEmpty(unsigned char client) {
return RB_EMPTY(&commStates[client].inRB);
}
unsigned char RadioServer_HasElements(unsigned char client) {
return RB_ELEMENTS(&commStates[client].inRB);
}
void RadioServer_CleanIn(unsigned char client) {
RB_CLEAN(&commStates[client].inRB);
}
void RadioServer_CleanOut(unsigned char client) {
RB_CLEAN(&commStates[client].outRB);
}
unsigned char RadioServer_InChar(unsigned char client) {
unsigned char toReturn;
while(RB_POP(&commStates[client].inRB, &toReturn));
return toReturn;
}
void RadioServer_InCharArray(unsigned char client,
unsigned char *charArray,
unsigned char len) {
unsigned char i;
for(i = 0; i<len; i++) {
while(RB_POP(&commStates[client].inRB, &charArray[i]));
}
}
unsigned short RadioServer_GetFailureCount(unsigned char client) {
return commStates[client].failureCount;
}
void RadioServer_OutStatusClient(unsigned char client) {
#if (DEBUG2SCI0 > 0)
SCI0_OutString("Client ");
SCI0_OutUDec(client);
SCI0_OutString(", Status: ");
switch(client) {
case 0:
if(clientFlags & CLIENT_FLAG_0) {
SCI0_OutString("Active");
} else {
SCI0_OutString("Inactive");
}
break;
case 1:
if(clientFlags & CLIENT_FLAG_1) {
SCI0_OutString("Active");
} else {
SCI0_OutString("Inactive");
}
break;
case 2:
if(clientFlags & CLIENT_FLAG_2) {
SCI0_OutString("Active");
} else {
SCI0_OutString("Inactive");
}
break;
case 3:
if(clientFlags & CLIENT_FLAG_3) {
SCI0_OutString("Active");
} else {
SCI0_OutString("Inactive");
}
break;
case 4:
if(clientFlags & CLIENT_FLAG_4) {
SCI0_OutString("Active");
} else {
SCI0_OutString("Inactive");
}
break;
}
SCI0_OutString(", Failure Count: ");
SCI0_OutUDec(commStates[client].failureCount);
#endif /* DEBUG2SCI0 */
}
#endif /* HAS_RFCOMMS */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?