lpcmanager

Merge.

2017-03-24, bmakuc
dde474d32698
Merge.
--- a/LPCBus/MC9_Devices_decl.c Fri Mar 24 09:00:08 2017 +0100
+++ b/LPCBus/MC9_Devices_decl.c Fri Mar 24 12:32:20 2017 +0100
@@ -220,13 +220,14 @@
#define LOCKED 1
#define FULL 2
-#define MAX_UART_DEVICES 68
+#define MAX_UART_DEVICES 32
#define UART_BUFSIZE_SHORT 26 /* UART bus read & write buffer size - short buffer for LPC-2 */
#define UART_BUFSIZE_LONG 48 /* UART bus read & write buffer size - long buffer for LHC-2 */
-#define UART_RETRY_NUM 10
+#define UART_RETRY_NUM 0
-char uartBufSize = UART_BUFSIZE_SHORT; /* Smarteh uart bus: buffer size */
+char uartBufSize = UART_BUFSIZE_SHORT; /* Smarteh uart bus: buffer size */
unsigned long uartBaudrate = 19200; /* Smarteh uart bus: baudrate */
+struct timeval uartTimeout = {0,40000}; /* Smarteh uart bus: timeout */
/* Tables containing information about connected devices on UART port
(initialized by Composer) */
@@ -274,42 +275,64 @@
{
static unsigned char i=0;
char commStat;
+ uint64_t actTime;
+ static uint64_t lastCommTime=0;
while (rt_task_sleep_until(TM_INFINITE) == -EINTR){
- if(!rt_mutex_acquire(&UART_WriteMutex, TM_INFINITE )){
- if(uartDevWriteBuf_plc_state == FULL){
- uartDevWriteBuf_t *uartDevWriteBuf_tmp;
- uartDevWriteBuf_tmp = uartDevWriteBuf_plc;
- uartDevWriteBuf_plc = uartDevWriteBuf_drv;
- uartDevWriteBuf_drv = uartDevWriteBuf_tmp;
- uartDevWriteBuf_plc_state = EMPTY;
- }
- rt_mutex_release(&UART_WriteMutex);
- }
-
- // 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)
{
+
+ // Timers for UART port modules
+ if(uartPortTim[i].status != TIM_DISABLED)
+ {
+ actTime = (uint64_t)rt_timer_read();
+ uartPortTim[i].actValue = actTime - uartPortTim[i].oldTime;
+
+ if((uartPortTim[i].actValue < uartPortTim[i].toValue)
+ || (actTime - lastCommTime < 50000000))
+ uartPortTim[i].status = TIM_EN_RUNNING;
+ else {
+ uartPortTim[i].status = TIM_EN_EXPIRED;
+ uartPortTim[i].oldTime = actTime;
+ lastCommTime = actTime;
+ }
+ }
+
// Check timer status
if((uartPortTim[i].status == TIM_DISABLED)
|| (uartPortTim[i].status == TIM_EN_EXPIRED))
{
+ if(!rt_mutex_acquire(&UART_WriteMutex, TM_INFINITE )){
+ if(uartDevWriteBuf_plc_state == FULL){
+ uartDevWriteBuf_t *uartDevWriteBuf_tmp;
+ uartDevWriteBuf_tmp = uartDevWriteBuf_plc;
+ uartDevWriteBuf_plc = uartDevWriteBuf_drv;
+ uartDevWriteBuf_drv = uartDevWriteBuf_tmp;
+ uartDevWriteBuf_plc_state = EMPTY;
+ }
+ rt_mutex_release(&UART_WriteMutex);
+ }
+
// Communicate with device
commStat = (*uartPortDevices[i])(
&uartDevReadBuf[i][0],
&uartDevWriteBuf[i][0],
uartDev[i][1]);
+ 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);
+ }
+
// Check communication status:
if(commStat == TRUE)
{
@@ -343,19 +366,6 @@
}
}
else i=0;
-
- 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);
- }
}
}
@@ -463,8 +473,8 @@
FD_ZERO(&set); /* clear the set */
FD_SET(UART_fd, &set); /* add our file descriptor to the set */
- timeout.tv_sec = 0;
- timeout.tv_usec = 20000;
+ timeout.tv_sec = uartTimeout.tv_sec;
+ timeout.tv_usec = uartTimeout.tv_usec;
while(count < uartBufSize){
rv = select(UART_fd + 1, &set, NULL, NULL, &timeout);
@@ -472,7 +482,7 @@
printf("RS485 select error\n");
else if(rv == 0)
{
- //printf("timeout\n");
+ //printf("timeout\n");
break;
}
else {
@@ -508,6 +518,9 @@
{
unsigned char i=0;
+ uartTimeout.tv_sec = 0;
+ uartTimeout.tv_usec = 20000; /* 20 ms timeout */
+
for(i=0;i<MAX_UART_DEVICES;i++)
{
switch(uartDev[i][1])
@@ -597,6 +610,9 @@
{
unsigned char i=0;
+ uartTimeout.tv_sec = 0;
+ uartTimeout.tv_usec = 40000; /* 40 ms timeout */
+
for(i=0;i<MAX_UART_DEVICES;i++)
{
switch(uartDev[i][1])