lpcmanager

Parents 8d978a4cc32a
Children 1217faa3dd27
Smarteh uart bus: added support for short buffer (26B) and 19200 baudrate.
--- 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)
{
int fd;
struct termios attr;
speed_t baud = B115200; /* baud rate */
+ switch(baudrate)
+ {
+ case 19200:
+ baud = B19200;
+ break;
+ case 115200:
+ baud = B115200;
+ break;
+ default:
+ baud = B115200;
+ }
+
if ((fd = open(devicename, O_RDWR)) == -1) {
perror("openserial(): open()");
return 0;
@@ -208,11 +220,13 @@
#define LOCKED 1
#define FULL 2
-#define MAX_UART_DEVICES 68
-/* UART bus read and write buffer size */
-#define UART_BUFSIZE 48
+#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;
- for(i=0;i<45;i++)
- {
- checksum1Temp=buffer[i];
- 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
- }
- 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 &
+* bufLen-TAIL_LEN+1
******************************************************************************/
-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;
- for(i=0;i<45;i++)
- {
- checksum1Temp = buffer[i];
- 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
- }
- checksum2 = checksum2^buffer[i];
- }
- 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;
+ 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)
+ checksum1++;
+ 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)
+ checksum1++;
+ 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]))
+ return TRUE;
+ else
+ return FALSE;
+}
+
static int UART_fd;
-
/*************************************************************************//**
* Support for UART modules
* @param [in] readBuf Pointer to read buffer (for previously polled UART device!)
@@ -413,32 +435,32 @@
int rv;
RTIME now;
int count = 0;
- char tmp[UART_BUFSIZE];
+ char tmp[uartBufSize];
/* Prepare transmit buffer */
- memcpy(tmp+1,writeBuf,UART_BUFSIZE-TAIL_LEN-1);
+ memcpy(tmp+1,writeBuf,uartBufSize-TAIL_LEN-1);
tmp[0] = 'S';
- tmp[45] = address;
- Checksum((unsigned char*)tmp+1);
+ tmp[uartBufSize-TAIL_LEN] = address;
+ Checksum((unsigned char*)tmp+1, uartBufSize);
tcflush(UART_fd, TCIOFLUSH);
now = rt_timer_read();
- if(write(UART_fd, tmp, UART_BUFSIZE) != UART_BUFSIZE){
+ if(write(UART_fd, tmp, uartBufSize) != uartBufSize){
goto UARTDevfail;
}
/* Sleep until transmission completes + 0.5ms safety */
while(rt_task_sleep_until(
now + rt_timer_ns2ticks(
- 1000000000LL * UART_BUFSIZE * 10 / 115200
+ 1000000000LL * uartBufSize * 10 / uartBaudrate
+ 500000)) == -EINTR);
/* Turn to listen mode*/
RecieveMode();
- 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 @@
break;
}
else {
- int rr = read(UART_fd, tmp + count, UART_BUFSIZE - count);
+ int rr = read(UART_fd, tmp + count, uartBufSize - count);
if(rr > 0){
count += rr;
}else{
@@ -464,23 +486,16 @@
}
}
-// {
-// 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();
/* Copy received buffer */
- if(count == UART_BUFSIZE){
- memcpy(readBuf,tmp+1,UART_BUFSIZE-TAIL_LEN-1);
- return(TRUE);
- }
+ if(count == uartBufSize){
+ if(ChecksumValid((unsigned char*)tmp+1, uartBufSize)){
+ memcpy(readBuf,tmp+1,uartBufSize-TAIL_LEN-1);
+ return(TRUE);
+ }
+ }
UARTDevfail:
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));
%(init_code)s
+// 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")))
return err;
@@ -33,7 +36,7 @@
return 1;
}
-UART_fd = openserial(serialdev);
+UART_fd = openserial(serialdev, uartBaudrate);
if (!UART_fd) {
fprintf(stderr, "Error while initializing %%s.\n", serialdev);
return 1;