--- 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;
- uartPortTim[i].status = TIM_EN_EXPIRED;
- if(uartPortDevices[i] != NULL)
- if((uartPortTim[i].status == TIM_DISABLED)
- || (uartPortTim[i].status == TIM_EN_EXPIRED))
- // Communicate with device
- commStat = (*uartPortDevices[i])(
- &uartDevWriteBuf[i][0],
+ // 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; + uartPortTim[i].status = TIM_EN_EXPIRED; + // Communicate only with initialised UART devices + if(uartPortDevices[i] != NULL) + if((uartPortTim[i].status == TIM_DISABLED) + || (uartPortTim[i].status == TIM_EN_EXPIRED)) + // Communicate with device + commStat = (*uartPortDevices[i])( + &uartDevWriteBuf[i][0], - // Check communication status:
- uartCommStatusBuf[i][0] = TRUE;
- uartCommErrCntBuf[i][0] = 0;
- if(uartCommErrCntBuf[i][0] < UART_RETRY_NUM)
- uartCommErrCntBuf[i][0]++;
- uartCommStatusBuf[i][0] = FALSE;
+ // Check communication status: + uartCommStatusBuf[i][0] = TRUE; + uartCommErrCntBuf[i][0] = 0; + if(uartCommErrCntBuf[i][0] < UART_RETRY_NUM) + uartCommErrCntBuf[i][0]++; + 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;
+ // Procede with next UART device only after + // communication with the current one is done + else /* Go back to the first UART device */ + 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(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)
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 @@
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)
- 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) @@ -462,14 +464,14 @@
-// for (i=0; i<UART_BUFSIZE; i++){
-// printf("|%%c:%%d", tmp[i], tmp[i]);
+// for (i=0; i<UART_BUFSIZE; i++){ +// printf("|%%c:%%d", tmp[i], tmp[i]); /* Turn to transmit mode*/
@@ -497,8 +499,8 @@
@@ -581,7 +583,7 @@
uartPortDevices[i] = &UARTDevice;
- uartPortTim[i].toValue = msTOns(200); /* 200ms */
+ uartPortTim[i].toValue = msTOns(200); /* 200ms */ uartPortTim[i].status = TIM_EN_RUNNING;
@@ -589,7 +591,7 @@
uartPortDevices[i] = &UARTDevice;
- uartPortTim[i].toValue = msTOns(600); /* 600ms */
+ uartPortTim[i].toValue = msTOns(600); /* 600ms */ uartPortTim[i].status = TIM_EN_RUNNING;
case(170): /* ID1, ID2, ID3 */
@@ -597,7 +599,7 @@
uartPortDevices[i] = &UARTDevice;
- uartPortTim[i].toValue = msTOns(400); /* 400ms */
+ uartPortTim[i].toValue = msTOns(400); /* 400ms */ uartPortTim[i].status = TIM_EN_RUNNING;
@@ -617,13 +619,14 @@
uartPortDevices[i] = &UARTDevice;
- uartPortTim[i].toValue = msTOns(400); /* 400ms */
+ uartPortTim[i].toValue = msTOns(400); /* 400ms */ uartPortTim[i].status = TIM_EN_RUNNING;
uartPortDevices[i] = NULL; /* "Empty" or unknown module */
uartPortTim[i].status = TIM_DISABLED;
+ uartDevNum--; /* Substract unused devices from MAX_UART_DEVICES */