--- a/LPCBus/MC9_Devices_decl.c Tue Jun 07 12:32:12 2016 +0200
+++ b/LPCBus/MC9_Devices_decl.c Tue Jun 14 13:06:03 2016 +0200
@@ -162,12 +162,24 @@
/*--------------------------- Serial Port handling ---------------------------*/
-int openserial(char *devicename)
+int openserial(char *devicename, unsigned long baudrate) speed_t baud = B115200; /* baud rate */
if ((fd = open(devicename, O_RDWR)) == -1) {
perror("openserial(): open()");
@@ -208,11 +220,13 @@
-#define MAX_UART_DEVICES 68
-/* UART bus read and write buffer size */
+#define MAX_UART_DEVICES 68 +#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 10
+char uartBufSize = UART_BUFSIZE_LONG; /* Smarteh uart bus: buffer size */ +unsigned long uartBaudrate = 115200; /* Smarteh uart bus: baudrate */ /* Tables containing information about connected devices on UART port
(initialized by Composer) */
@@ -220,7 +234,7 @@
unsigned char uartDevNum = MAX_UART_DEVICES;
/* Buffers for reading data from UART port devices */
-typedef char uartDevReadBuf_t[MAX_UART_DEVICES][UART_BUFSIZE];
+typedef char uartDevReadBuf_t[MAX_UART_DEVICES][UART_BUFSIZE_LONG]; uartDevReadBuf_t uartDevReadBufA;
uartDevReadBuf_t uartDevReadBufB;
uartDevReadBuf_t *uartDevReadBuf_drv;
@@ -229,7 +243,7 @@
uartDevReadBuf_t uartDevReadBuf;
/* Buffers for writing data to UART port devices */
-typedef char uartDevWriteBuf_t[MAX_UART_DEVICES][UART_BUFSIZE];
+typedef char uartDevWriteBuf_t[MAX_UART_DEVICES][UART_BUFSIZE_LONG]; uartDevWriteBuf_t uartDevWriteBufA;
uartDevWriteBuf_t uartDevWriteBufB;
uartDevWriteBuf_t *uartDevWriteBuf_drv;
@@ -345,60 +359,68 @@
-/**************************************************************************//**
-* Calculate checksum of a buffer
-* @param [in] buffer Pointer to buffer
-* @return Checksum values on buffer locations 23 & 24
-******************************************************************************/
-void Checksum(unsigned char *buffer)
- unsigned char i=0, j=0;
- unsigned char checksum1=0, checksum2=0;
- unsigned char checksum1Temp=0;
- checksum1Temp=buffer[i];
- 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
- buffer[45] = checksum2; // Write number of '1' to buffer
- buffer[46] = checksum1; // Write XOR to buffer
+#define TAIL_LEN 3 /* Length of data tail in bytes */ /**************************************************************************//**
-* Check if checksum values of received buffer are valid
-* @param [in] buffer Pointer to buffer
-* @return TRUE if valid, otherwise FALSE
+* Calculate checksum of a buffer +* @param [in] buffer Pointer to buffer +* @param [in] bufLen Buffer length +* @return Checksum values on buffer locations bufLen-TAIL_LEN & ******************************************************************************/
-unsigned char ChecksumValid(unsigned char *buffer)
+void Checksum(unsigned char *buffer, unsigned char bufLen) - unsigned char i=0, j=0;
- unsigned char checksum1=0, checksum2=0;
- unsigned char checksum1Temp=0;
- checksum1Temp = buffer[i];
- 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
- checksum2 = checksum2^buffer[i];
- if((checksum2==buffer[45]) && (checksum1==buffer[46])) // Check if computed checksums are the same as those in buffer (=> no error)
+ unsigned char i=0, j=0; + unsigned char checksum1=0, checksum2=0; + unsigned char checksum1Temp=0; + for(i=0;i<bufLen-TAIL_LEN;i++) + checksum1Temp=buffer[i]; + 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 */ + buffer[bufLen-TAIL_LEN] = checksum2; /* Write number of '1' to buffer */ + buffer[bufLen-TAIL_LEN+1] = checksum1; /* Write XOR to buffer */ -#define TAIL_LEN 3 /* Length of data tail in bytes */
+/**************************************************************************//** +* Check if checksum values of received buffer are valid +* @param [in] buffer Pointer to buffer +* @param [in] bufLen Buffer length +* @return TRUE if valid, otherwise FALSE +******************************************************************************/ +unsigned char ChecksumValid(unsigned char *buffer, unsigned char bufLen) + unsigned char i=0, j=0; + unsigned char checksum1=0, checksum2=0; + unsigned char checksum1Temp=0; + for(i=0;i<bufLen-TAIL_LEN;i++) + checksum1Temp = buffer[i]; + 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 */ + checksum2 = checksum2 ^ buffer[i]; + /* Check if computed checksums are the same as those in buffer (=> no error) */ + if((checksum2==buffer[bufLen-TAIL_LEN]) && (checksum1==buffer[bufLen-TAIL_LEN+1]))
/*************************************************************************//**
* Support for UART modules
* @param [in] readBuf Pointer to read buffer (for previously polled UART device!)
@@ -413,32 +435,32 @@
- char tmp[UART_BUFSIZE];
/* Prepare transmit buffer */
- memcpy(tmp+1,writeBuf,UART_BUFSIZE-TAIL_LEN-1);
+ memcpy(tmp+1,writeBuf,uartBufSize-TAIL_LEN-1);
- Checksum((unsigned char*)tmp+1);
+ tmp[uartBufSize-TAIL_LEN] = address; + Checksum((unsigned char*)tmp+1, uartBufSize); tcflush(UART_fd, TCIOFLUSH);
- if(write(UART_fd, tmp, UART_BUFSIZE) != UART_BUFSIZE){
+ if(write(UART_fd, tmp, uartBufSize) != uartBufSize){ /* Sleep until transmission completes + 0.5ms safety */
while(rt_task_sleep_until(
- 1000000000LL * UART_BUFSIZE * 10 / 115200
+ 1000000000LL * uartBufSize * 10 / uartBaudrate - while(count < UART_BUFSIZE){
+ while(count < uartBufSize){ FD_ZERO(&set); /* clear the set */
FD_SET(UART_fd, &set); /* add our file descriptor to the set */
@@ -455,7 +477,7 @@
- int rr = read(UART_fd, tmp + count, UART_BUFSIZE - count);
+ int rr = read(UART_fd, tmp + count, uartBufSize - count); @@ -464,23 +486,16 @@
-// for (i=0; i<UART_BUFSIZE; i++){
-// printf("|%%c:%%d", tmp[i], tmp[i]);
/* Turn to transmit mode*/
/* Copy received buffer */
- if(count == UART_BUFSIZE){
- memcpy(readBuf,tmp+1,UART_BUFSIZE-TAIL_LEN-1);
+ if(count == uartBufSize){ + if(ChecksumValid((unsigned char*)tmp+1, uartBufSize)){ + memcpy(readBuf,tmp+1,uartBufSize-TAIL_LEN-1); tcflush(UART_fd, TCIOFLUSH);
--- a/LPCBus/MC9_Devices_init.c Tue Jun 07 12:32:12 2016 +0200
+++ b/LPCBus/MC9_Devices_init.c Tue Jun 14 13:06:03 2016 +0200
@@ -20,6 +20,9 @@
bzero(&uartCommErrCntBuf, sizeof(uartCommErrCntBuf));
+// TODO XXX remove next 2 lines when initialization is done from Composer: +uartBufSize = UART_BUFSIZE_LONG; // UART_BUFSIZE_SHORT +uartBaudrate = 115200; // 19200 if((err = rt_mutex_create (&UART_WriteMutex, "UART_WriteMutex")))
@@ -33,7 +36,7 @@
-UART_fd = openserial(serialdev);
+UART_fd = openserial(serialdev, uartBaudrate); fprintf(stderr, "Error while initializing %%s.\n", serialdev);