lpcmanager

Parents 0940e81914f7
Children abeb26d6c4b0
Uart communication update: controler cycles through devicecs without skiping them. It always waits for the timer to expire.
--- a/LPCBus/MC9_Devices_decl.c Wed Jan 27 10:34:56 2016 +0100
+++ b/LPCBus/MC9_Devices_decl.c Fri Mar 11 12:53:15 2016 +0100
@@ -217,6 +217,7 @@
/* Tables containing information about connected devices on UART port
(initialized by Composer) */
unsigned char uartDev[MAX_UART_DEVICES][2];
+unsigned char uartDevNum = MAX_UART_DEVICES;
/* Buffers for reading data from UART port devices */
typedef char uartDevReadBuf_t[MAX_UART_DEVICES][UART_BUFSIZE];
@@ -272,75 +273,76 @@
rt_mutex_release(&UART_WriteMutex);
}
- /* Scan all positions for attached UART port modules */
- //for(i=0;i<MAX_UART_DEVICES;i++)
- //{
- /* Timers for right modules */
- if(uartPortTim[i].status != TIM_DISABLED)
- {
- uartPortTim[i].actValue += common_ticktime__;
- if(uartPortTim[i].actValue < uartPortTim[i].toValue)
- {
- uartPortTim[i].status = TIM_EN_RUNNING;
- }
- else
- uartPortTim[i].status = TIM_EN_EXPIRED;
- }
- if(uartPortDevices[i] != NULL)
- {
- // Check timer status
- if((uartPortTim[i].status == TIM_DISABLED)
- || (uartPortTim[i].status == TIM_EN_EXPIRED))
- {
- // Communicate with device
- commStat = (*uartPortDevices[i])(
- &uartDevReadBuf[i][0],
- &uartDevWriteBuf[i][0],
- uartDev[i][1]);
+ // Timers for UART port modules
+ if(uartPortTim[i].status != TIM_DISABLED)
+ {
+ uartPortTim[i].actValue += common_ticktime__;
+ if(uartPortTim[i].actValue < uartPortTim[i].toValue)
+ uartPortTim[i].status = TIM_EN_RUNNING;
+ else
+ uartPortTim[i].status = TIM_EN_EXPIRED;
+ }
+
+ // Communicate only with initialised UART devices
+ if(uartPortDevices[i] != NULL)
+ {
+ // Check timer status
+ if((uartPortTim[i].status == TIM_DISABLED)
+ || (uartPortTim[i].status == TIM_EN_EXPIRED))
+ {
+ // Communicate with device
+ commStat = (*uartPortDevices[i])(
+ &uartDevReadBuf[i][0],
+ &uartDevWriteBuf[i][0],
+ uartDev[i][1]);
- // Check communication status:
- if(commStat == TRUE)
- {
- uartCommStatusBuf[i][0] = TRUE;
- uartCommErrCntBuf[i][0] = 0;
- }
- else
- {
- if(uartCommErrCntBuf[i][0] < UART_RETRY_NUM)
- uartCommErrCntBuf[i][0]++;
- else
- uartCommStatusBuf[i][0] = FALSE;
- }
+ // Check communication status:
+ if(commStat == TRUE)
+ {
+ uartCommStatusBuf[i][0] = TRUE;
+ uartCommErrCntBuf[i][0] = 0;
+ }
+ else
+ {
+ if(uartCommErrCntBuf[i][0] < UART_RETRY_NUM)
+ uartCommErrCntBuf[i][0]++;
+ else
+ uartCommStatusBuf[i][0] = FALSE;
+ }
+
+ // If timer is enabled, reset it's value,
+ // otherwise keep it disabled
+ if(uartPortTim[i].status != TIM_DISABLED)
+ {
+ uartPortTim[i].actValue = 0;
+ uartPortTim[i].status = TIM_EN_RUNNING;
+ }
+ else /* Keep timer disabled */
+ uartPortTim[i].status = TIM_DISABLED;
- // If timer is enabled, reset it's value,
- // otherwise keep it disabled
- if(uartPortTim[i].status != TIM_DISABLED)
- {
- uartPortTim[i].actValue = 0;
- uartPortTim[i].status = TIM_EN_RUNNING;
- }
- else /* Keep timer disabled */
- uartPortTim[i].status = TIM_DISABLED;
- }
- i++;
- }
- else i=0;
+ // Procede with next UART device only after
+ // communication with the current one is done
+ if(i < (uartDevNum-1))
+ i++;
+ else /* Go back to the first UART device */
+ i=0;
+ }
+ }
+ else i=0;
+
+ memcpy(uartDevReadBuf_drv, &uartDevReadBuf, sizeof(uartDevReadBuf_t));
- //}
-
- memcpy(uartDevReadBuf_drv, &uartDevReadBuf, sizeof(uartDevReadBuf_t));
-
- if(!rt_mutex_acquire(&UART_ReadMutex, TM_INFINITE )){
- if(uartDevReadBuf_plc_state == EMPTY){
- uartDevReadBuf_t *uartDevReadBuf_tmp;
- uartDevReadBuf_tmp = uartDevReadBuf_plc;
- uartDevReadBuf_plc = uartDevReadBuf_drv;
- uartDevReadBuf_drv = uartDevReadBuf_tmp;
- uartDevReadBuf_plc_state = FULL;
- }
- rt_mutex_release(&UART_ReadMutex);
- }
- }
+ if(!rt_mutex_acquire(&UART_ReadMutex, TM_INFINITE )){
+ if(uartDevReadBuf_plc_state == EMPTY){
+ uartDevReadBuf_t *uartDevReadBuf_tmp;
+ uartDevReadBuf_tmp = uartDevReadBuf_plc;
+ uartDevReadBuf_plc = uartDevReadBuf_drv;
+ uartDevReadBuf_drv = uartDevReadBuf_tmp;
+ uartDevReadBuf_plc_state = FULL;
+ }
+ rt_mutex_release(&UART_ReadMutex);
+ }
+ }
}
/**************************************************************************//**
@@ -356,16 +358,16 @@
for(i=0;i<45;i++)
{
checksum1Temp=buffer[i];
- for(j=0;j<8;j++) // Compute number of '1' of whole buff.
+ for(j=0;j<8;j++) // Compute number of '1' of whole buff.
{
if((checksum1Temp & 0x01)>0)
checksum1++;
checksum1Temp = checksum1Temp >> 1;
}
- checksum2 = checksum2 ^ buffer[i]; // Compute XOR of whole buffer
+ checksum2 = checksum2 ^ buffer[i]; // Compute XOR of whole buffer
}
- buffer[45] = checksum2; // Write number of '1' to buffer
- buffer[46] = checksum1; // Write XOR to buffer
+ buffer[45] = checksum2; // Write number of '1' to buffer
+ buffer[46] = checksum1; // Write XOR to buffer
}
/**************************************************************************//**
* Check if checksum values of received buffer are valid
@@ -380,15 +382,15 @@
for(i=0;i<45;i++)
{
checksum1Temp = buffer[i];
- for(j=0;j<8;j++) // Compute number of '1' of whole buff.
+ for(j=0;j<8;j++) // Compute number of '1' of whole buff.
{
if((checksum1Temp & 0x01)>0)
checksum1++;
- checksum1Temp = checksum1Temp >> 1; // Compute XOR of whole buffer
+ checksum1Temp = checksum1Temp >> 1; // Compute XOR of whole buffer
}
checksum2 = checksum2^buffer[i];
}
- if((checksum2==buffer[45]) && (checksum1==buffer[46])) // Check if computed checksums are the same as those in buffer (=> no error)
+ if((checksum2==buffer[45]) && (checksum1==buffer[46])) // Check if computed checksums are the same as those in buffer (=> no error)
return TRUE;
else
return FALSE;
@@ -462,14 +464,14 @@
}
}
- // {
- // int i;
-// printf("tmp:\n");
-// for (i=0; i<UART_BUFSIZE; i++){
-// printf("|%%c:%%d", tmp[i], tmp[i]);
-// }
-// printf("\n");
- // }
+// {
+// int i;
+// printf("tmp:\n");
+// for (i=0; i<UART_BUFSIZE; i++){
+// printf("|%%c:%%d", tmp[i], tmp[i]);
+// }
+// printf("\n");
+// }
/* Turn to transmit mode*/
TransmitMode();
@@ -497,8 +499,8 @@
switch(uartDev[i][1])
{
case(2): /* MU1 */
- case(4):
- case(6):
+ case(4):
+ case(6):
case(8):
case(10):
case(12):
@@ -581,7 +583,7 @@
case(190):
case(192):
uartPortDevices[i] = &UARTDevice;
- uartPortTim[i].toValue = msTOns(200); /* 200ms */
+ uartPortTim[i].toValue = msTOns(200); /* 200ms */
uartPortTim[i].status = TIM_EN_RUNNING;
break;
case(146): /* CH1 */
@@ -589,7 +591,7 @@
case(150):
case(152):
uartPortDevices[i] = &UARTDevice;
- uartPortTim[i].toValue = msTOns(600); /* 600ms */
+ uartPortTim[i].toValue = msTOns(600); /* 600ms */
uartPortTim[i].status = TIM_EN_RUNNING;
break;
case(170): /* ID1, ID2, ID3 */
@@ -597,7 +599,7 @@
case(174):
case(176):
uartPortDevices[i] = &UARTDevice;
- uartPortTim[i].toValue = msTOns(400); /* 400ms */
+ uartPortTim[i].toValue = msTOns(400); /* 400ms */
uartPortTim[i].status = TIM_EN_RUNNING;
break;
case(194): /* DP1V */
@@ -617,13 +619,14 @@
case(222):
case(224):
uartPortDevices[i] = &UARTDevice;
- uartPortTim[i].toValue = msTOns(400); /* 400ms */
+ uartPortTim[i].toValue = msTOns(400); /* 400ms */
uartPortTim[i].status = TIM_EN_RUNNING;
break;
default:
uartPortDevices[i] = NULL; /* "Empty" or unknown module */
uartPortTim[i].status = TIM_DISABLED;
+ uartDevNum--; /* Substract unused devices from MAX_UART_DEVICES */
break;
}
}