51 #define LOWEST_BITRATE 2400 // Lowest possible bit rate (always used for sync and info) [b/S]
52 #define MIDLE_BITRATE 57600 // Highest bit rate allowed when adjusting clock [b/S]
53 #define HIGHEST_BITRATE 460800 // Highest possible bit rate [b/S]
55 #define MIDLE_BITRATE_DEVIATION 40 // Largest deviation able to adjust clock to [%]
56 #define MIDLE_BITRATE_INCREAMENT 1 // Amount to adjust per loop [%]
57 #define MIDLE_BITRATE_FIX 5 // Last adjustment to get in the middle of the bit [%]
450 #define BYTE_SYNC 0x00 // Synchronisation byte
451 #define BYTE_ACK 0x04 // Acknowledge byte
452 #define BYTE_NACK 0x02 // Not acknowledge byte
454 #define MESSAGE_SYS 0x00 // System message
455 #define MESSAGE_CMD 0x40 // Command message
456 #define MESSAGE_INFO 0x80 // Info message
457 #define MESSAGE_DATA 0xC0 // Data message
458 #define GET_MESSAGE_TYPE(B) (B & 0xC0) // Get message type
460 #define CMD_TYPE 0x00 // CMD command - TYPE (device type for VM reference)
461 #define CMD_MODES 0x01 // CMD command - MODES (number of supported modes 0=1)
462 #define CMD_SPEED 0x02 // CMD command - SPEED (maximun communication speed)
463 #define CMD_SELECT 0x03 // CMD command - SELECT (select mode)
464 #define CMD_WRITE 0x04 // CMD command - WRITE (write to device)
465 #define GET_CMD_COMMAND(B) (B & 0x07) // Get CMD command
467 #define GET_MODE(B) (B & 0x07) // Get mode
469 #define CONVERT_LENGTH(C) (1 << (C & 0x07))
470 #define GET_MESSAGE_LENGTH(B) (CONVERT_LENGTH(B >> 3)) // Get message length exclusive check byte
472 #define MAKE_CMD_COMMAND(C,LC) (MESSAGE_CMD + (C & 0x07) + ((LC & 0x07) << 3))
477 #define INFO_NAME 0x00 // INFO command - NAME (device name)
478 #define INFO_RAW 0x01 // INFO command - RAW (device RAW value span)
479 #define INFO_PCT 0x02 // INFO command - PCT (device PCT value span)
480 #define INFO_SI 0x03 // INFO command - SI (device SI value span)
481 #define INFO_SYMBOL 0x04 // INFO command - SYMBOL (device SI unit symbol)
482 #define INFO_FORMAT 0x80 // INFO command - FORMAT (device data sets and format)
483 #define GET_INFO_COMMAND(B) (B) // Get INFO command
487 #include <asm/types.h>
490 #define HW_ID_SUPPORT
492 #include "../../lms2012/source/lms2012.h"
493 #include "../../lms2012/source/am1808.h"
499 {
"" ,
TYPE_UNKNOWN , CONN_UNKNOWN , 0 , 1 , 1 , 4 , 0 , 0 , 0.0, 1023.0, 0.0, 100.0, 0.0, 1023.0, 10, 0,
'-',
"" },
500 {
"TERMINAL" ,
TYPE_TERMINAL , CONN_INPUT_UART , 0 , 0 , 0 , 4 , 0 , 0 , 0.0, 4095.0, 0.0, 100.0, 0.0, 1000.0, 0, 0,
'-',
"" },
513 #define DEBUG_TRACE_MODE_CHANGE
514 #define DEBUG_D_UART_ERROR
518 #define MODULE_NAME "uart_module"
519 #define DEVICE1_NAME UART_DEVICE
520 #define DEVICE2_NAME TEST_UART_DEVICE
522 static int ModuleInit(
void);
523 static void ModuleExit(
void);
525 #include <linux/kernel.h>
526 #include <linux/fs.h>
528 #include <linux/sched.h>
531 #include <linux/mm.h>
532 #include <linux/hrtimer.h>
533 #include <linux/interrupt.h>
535 #include <linux/init.h>
536 #include <linux/uaccess.h>
537 #include <linux/debugfs.h>
539 #include <linux/ioport.h>
540 #include <asm/gpio.h>
542 #include <linux/module.h>
543 #include <linux/miscdevice.h>
544 #include <asm/uaccess.h>
547 #ifndef DISABLE_PRU_UARTS
548 #include <linux/ti_omapl_pru_suart.h>
572 #define NO_OF_INPUT_PORTS INPUTS
667 #ifdef ENABLE_TEST_ON_PORT4
712 #ifdef ENABLE_TEST_ON_PORT4
758 #ifdef ENABLE_TEST_ON_PORT4
775 [
EP2] = (
INPIN*)&EP2_InputUartPin[0],
781 [
FINAL] = FINAL_TestPin,
782 [
FINALB] = FINALB_TestPin,
789 static void __iomem *GpioBase;
816 printk(
" GP%d_%-2d 0x%08X and 0x%08X or 0x%08X\n",(Pin >> 4),(Pin & 0x0F),(
u32)Reg,
MuxRegMap[Tmp].Mask,
MuxRegMap[Tmp].Mode);
822 printk(
" FUNCTION 0x%08X and 0x%08X or 0x%08X\n",(
u32)Reg,
MuxRegMap[Tmp].Mask,
MuxRegMap[Tmp].Mode);
829 printk(
"* GP%d_%-2d ********* ERROR not found *********\n",(Pin >> 4),(Pin & 0x0F));
848 memcpy(InputUartPin,pInputUartPin[
Hw],
sizeof(EP2_InputUartPin));
849 if (memcmp((
const void*)InputUartPin,(
const void*)pInputUartPin[Hw],
sizeof(EP2_InputUartPin)) != 0)
851 printk(
"%s InputUartPin tabel broken!\n",
MODULE_NAME);
854 for (Port = 0;Port <
INPUTS;Port++)
862 if (InputUartPin[Port][Pin].Pin >= 0)
864 InputUartPin[Port][Pin].
pGpio = (
struct gpio_controller *__iomem)(GpioBase + ((InputUartPin[Port][Pin].Pin >> 5) * 0x28) + 0x10);
865 InputUartPin[Port][Pin].
Mask = (1 << (InputUartPin[Port][Pin].
Pin & 0x1F));
867 SetGpio(InputUartPin[Port][Pin].Pin);
875 memcpy(TestPin,pTestPin[Hw],
sizeof(EP2_TestPin));
876 if (memcmp((
const void*)TestPin,(
const void*)pTestPin[Hw],
sizeof(EP2_TestPin)) != 0)
883 if (TestPin[Pin].Pin >= 0)
885 TestPin[Pin].
pGpio = (
struct gpio_controller *__iomem)(GpioBase + ((TestPin[Pin].Pin >> 5) * 0x28) + 0x10);
886 TestPin[Pin].
Mask = (1 << (TestPin[Pin].
Pin & 0x1F));
896 #define PUARTFloat(port,pin) {\
897 (*InputUartPin[port][pin].pGpio).dir |= InputUartPin[port][pin].Mask;\
901 #define PUARTRead(port,pin) ((*InputUartPin[port][pin].pGpio).in_data & InputUartPin[port][pin].Mask)
904 #define PUARTHigh(port,pin) {\
905 (*InputUartPin[port][pin].pGpio).set_data = InputUartPin[port][pin].Mask;\
906 (*InputUartPin[port][pin].pGpio).dir &= ~InputUartPin[port][pin].Mask;\
909 #define PUARTLow(port,pin) {\
910 (*InputUartPin[port][pin].pGpio).clr_data = InputUartPin[port][pin].Mask;\
911 (*InputUartPin[port][pin].pGpio).dir &= ~InputUartPin[port][pin].Mask;\
915 (*TestPin[TESTPIN].pGpio).set_data = TestPin[TESTPIN].Mask;\
916 (*TestPin[TESTPIN].pGpio).dir &= ~TestPin[TESTPIN].Mask;\
920 (*TestPin[TESTPIN].pGpio).clr_data = TestPin[TESTPIN].Mask;\
921 (*TestPin[TESTPIN].pGpio).dir &= ~TestPin[TESTPIN].Mask;\
927 #define UART_CLOCK1 132000000
928 #define UART_CLOCK2 150000000
943 #define UART_REVID1 10
944 #define UART_REVID2 11
945 #define UART_PWREMU_MGMT 12
948 #define UART_RECBUF_SIZE 256
951 #ifdef DEBUG_D_UART_ERROR
952 #define DEBUG_UART_WRITE
954 #ifdef DEBUG_TRACE_US
955 #define DEBUG_UART_WRITE
957 #ifdef DEBUG_TRACE_ANGLE
958 #define DEBUG_UART_WRITE
960 #ifdef DEBUG_TRACE_MODE_CHANGE
961 #define DEBUG_UART_WRITE
964 #define DEBUG_UART_WRITE
967 #define DEBUG_UART_WRITE
971 #ifdef DEBUG_UART_WRITE
973 #define UARTBUFFERSIZE 250
974 static char UartBuffer[UARTBUFFERSIZE];
980 #define LOGPOOLSIZE 100000
981 ULONG LogPointer = 0;
982 ULONG LogOutPointer = 0;
983 char LogPool[LOGPOOLSIZE];
985 void UartWrite(
char *pString)
991 LogPool[LogPointer] = *pString;
995 if (Tmp >= LOGPOOLSIZE)
999 if (Tmp != LogOutPointer)
1020 static char Uart1Name[20];
1024 static UWORD Uart1RecBufIn;
1025 static UWORD Uart1RecBufOut;
1027 static UBYTE Uart1RecMesLng;
1028 static UBYTE Uart1RecMes[UART_BUFFER_SIZE];
1029 static UBYTE Uart1RecMesIn;
1038 while (!(IntrType & 1))
1063 return (IRQ_HANDLED);
1071 if (Uart1RecBufIn != Uart1RecBufOut)
1073 *pByte = Uart1RecBuf[Uart1RecBufOut];
1086 static void Uart1Flush(
void)
1107 if (Uart1Read(&Byte))
1109 if (Uart1RecMesIn == 0)
1117 if (Uart1RecMesLng <= UART_BUFFER_SIZE)
1120 Uart1RecMes[Uart1RecMesIn] = Byte;
1127 Uart1RecMes[Uart1RecMesIn] = Byte;
1129 if (++Uart1RecMesIn >= Uart1RecMesLng)
1132 *pCmd = Uart1RecMes[0];
1135 while (Length < (Uart1RecMesLng - 2))
1137 pData[Length] = Uart1RecMes[Length + 1];
1138 *pFail ^= pData[Length];
1141 *pCheck = Uart1RecMes[Length + 1];
1173 static void Uart1Setup(
ULONG BitRate)
1180 Uart1Base[
UART_DLL] = Divisor & 0xFF;
1181 Uart1Base[
UART_DLH] = (Divisor >> 8) & 0xFF;
1186 #ifndef UART1_FAKE_INTERRUPT
1195 #ifdef UART1_FAKE_INTERRUPT
1197 #define Uart1_RECEIVE_TIMER_RESOLUTION 500 // [uS]
1199 static struct hrtimer Uart1ReceiveTimer;
1200 static ktime_t Uart1ReceiveTime;
1203 static enum hrtimer_restart Uart1ReceiveInterruptFake(
struct hrtimer *pTimer)
1205 hrtimer_forward_now(pTimer,Uart1ReceiveTime);
1218 return (HRTIMER_RESTART);
1223 static int Uart1Init(
void)
1228 IntSuccess[
Uart1] = 0;
1241 if (Uart1Base !=
NULL)
1245 printk(
" %s memory mapped from 0x%08lX\r\n",
DEVICE1_NAME,(
unsigned long)Uart1Base);
1248 #ifdef UART1_FAKE_INTERRUPT
1250 hrtimer_init(&Uart1ReceiveTimer,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
1251 Uart1ReceiveTimer.function = Uart1ReceiveInterruptFake;
1252 hrtimer_start(&Uart1ReceiveTimer,Uart1ReceiveTime,HRTIMER_MODE_REL);
1258 printk(
" %s request interrupt success for UART1\r\n",
DEVICE1_NAME);
1260 IntSuccess[
Uart1] = 1;
1264 #ifndef UART1_FAKE_INTERRUPT
1265 printk(
" %s request interrupt failed for UART1\r\n",
DEVICE1_NAME);
1280 #ifndef UART1_FAKE_INTERRUPT
1283 hrtimer_cancel(&Uart1ReceiveTimer);
1285 if (Uart1Base !=
NULL)
1287 if (IntSuccess[Uart1])
1289 free_irq(
UartIntr[Uart1],Uart1Name);
1299 static char Uart2Name[20];
1305 static UWORD Uart2RecBufIn;
1306 static UWORD Uart2RecBufOut;
1308 static UBYTE Uart2RecMesLng;
1309 static UBYTE Uart2RecMes[UART_BUFFER_SIZE];
1310 static UBYTE Uart2RecMesIn;
1319 while (!(IntrType & 1))
1344 return (IRQ_HANDLED);
1352 if (Uart2RecBufIn != Uart2RecBufOut)
1354 *pByte = Uart2RecBuf[Uart2RecBufOut];
1367 static void Uart2Flush(
void)
1389 if (Uart2Read(&Byte))
1391 if (Uart2RecMesIn == 0)
1399 if (Uart2RecMesLng <= UART_BUFFER_SIZE)
1402 Uart2RecMes[Uart2RecMesIn] = Byte;
1409 Uart2RecMes[Uart2RecMesIn] = Byte;
1411 if (++Uart2RecMesIn >= Uart2RecMesLng)
1414 *pCmd = Uart2RecMes[0];
1417 while (Length < (Uart2RecMesLng - 2))
1419 pData[Length] = Uart2RecMes[Length + 1];
1420 *pFail ^= pData[Length];
1423 *pCheck = Uart2RecMes[Length + 1];
1455 static void Uart2Setup(
ULONG BitRate)
1462 Uart2Base[
UART_DLL] = Divisor & 0xFF;
1463 Uart2Base[
UART_DLH] = (Divisor >> 8) & 0xFF;
1472 static int Uart2Init(
void)
1477 IntSuccess[
Uart2] = 0;
1490 if (Uart2Base !=
NULL)
1494 printk(
" %s memory mapped from 0x%08lX\n",
DEVICE1_NAME,(
unsigned long)Uart2Base);
1501 printk(
" %s request interrupt success for UART2\n",
DEVICE1_NAME);
1503 IntSuccess[
Uart2] = 1;
1507 printk(
" %s request interrupt failed for UART2\n",
DEVICE1_NAME);
1522 if (Uart2Base !=
NULL)
1524 if (IntSuccess[Uart2])
1526 free_irq(
UartIntr[Uart2],Uart2Name);
1532 #ifndef DISABLE_PRU_UARTS
1535 static UBYTE Uart3RecMesLng;
1536 static UBYTE Uart3RecMes[UART_BUFFER_SIZE];
1537 static UBYTE Uart3RecMesIn;
1553 static void Uart3Flush(
void)
1557 while (Uart3Read(&Byte))
1577 if (Uart3Read(&Byte))
1579 if (Uart3RecMesIn == 0)
1587 if (Uart3RecMesLng <= UART_BUFFER_SIZE)
1590 Uart3RecMes[Uart3RecMesIn] = Byte;
1597 Uart3RecMes[Uart3RecMesIn] = Byte;
1599 if (++Uart3RecMesIn >= Uart3RecMesLng)
1602 *pCmd = Uart3RecMes[0];
1605 while (Length < (Uart3RecMesLng - 2))
1607 pData[Length] = Uart3RecMes[Length + 1];
1608 *pFail ^= pData[Length];
1611 *pCheck = Uart3RecMes[Length + 1];
1642 static void Uart3Setup(
ULONG BitRate)
1648 static void Uart3PortEnable(
void)
1654 static void Uart3PortDisable(
void)
1660 static int Uart3Init(
void)
1678 static UBYTE Uart4RecMesLng;
1679 static UBYTE Uart4RecMes[UART_BUFFER_SIZE];
1680 static UBYTE Uart4RecMesIn;
1696 static void Uart4Flush(
void)
1700 while (Uart4Read(&Byte))
1720 if (Uart4Read(&Byte))
1722 if (Uart4RecMesIn == 0)
1730 if (Uart4RecMesLng <= UART_BUFFER_SIZE)
1733 Uart4RecMes[Uart4RecMesIn] = Byte;
1740 Uart4RecMes[Uart4RecMesIn] = Byte;
1742 if (++Uart4RecMesIn >= Uart4RecMesLng)
1745 *pCmd = Uart4RecMes[0];
1748 while (Length < (Uart4RecMesLng - 2))
1750 pData[Length] = Uart4RecMes[Length + 1];
1751 *pFail ^= pData[Length];
1754 *pCheck = Uart4RecMes[Length + 1];
1785 static void Uart4Setup(
ULONG BitRate)
1791 static void Uart4PortEnable(
void)
1797 static void Uart4PortDisable(
void)
1803 static int Uart4Init(
void)
1824 #define INFODATA_INIT 0x00000000L
1825 #define INFODATA_CMD_TYPE 0x00000001L
1826 #define INFODATA_CMD_MODES 0x00000002L
1827 #define INFODATA_CMD_SPEED 0x00000004L
1829 #define INFODATA_INFO_NAME 0x00000100L
1830 #define INFODATA_INFO_RAW 0x00000200L
1831 #define INFODATA_INFO_PCT 0x00000400L
1832 #define INFODATA_INFO_SI 0x00000800L
1833 #define INFODATA_INFO_SYMBOL 0x00001000L
1834 #define INFODATA_INFO_FORMAT 0x00002000L
1836 #define INFODATA_CLEAR (~(INFODATA_INFO_NAME | INFODATA_INFO_RAW | INFODATA_INFO_PCT | INFODATA_INFO_SI | INFODATA_INFO_SYMBOL | INFODATA_INFO_FORMAT))
1838 #define INFODATA_NEEDED (INFODATA_CMD_TYPE | INFODATA_CMD_MODES | INFODATA_INFO_NAME | INFODATA_INFO_FORMAT)
1926 (
ULONG)LOWEST_BITRATE,
1927 (
ULONG)LOWEST_BITRATE,
1956 #define UART_TIMER_RESOLUTION 10 // [100uS]
1958 #define UART_BREAK_TIME 1000 // [100uS]
1959 #define UART_TERMINAL_DELAY 20000 // [100uS]
1960 #define UART_CHANGE_BITRATE_DELAY 100 // [100uS]
1961 #define UART_ACK_DELAY 100 // [100uS]
1962 #define UART_SHOW_TIME 2500 // [100uS]
1964 #define UART_WATCHDOG_TIME 1000 // [100uS]
1966 #define UART_ALLOWABLE_DATA_ERRORS 6
1971 static UART UartDefault;
1972 static UART *pUart = &UartDefault;
1974 static struct hrtimer Device1Timer;
1975 static ktime_t Device1Time;
1977 static UBYTE TestMode = 0;
1979 static void UartPortDisable(
UBYTE Port)
1993 #ifndef DISABLE_PRU_UARTS
2012 static void UartPortFlush(
UBYTE Port)
2028 #ifndef DISABLE_PRU_UARTS
2046 static void UartPortEnable(
UBYTE Port)
2065 #ifndef DISABLE_PRU_UARTS
2091 Result = Uart1Write(Byte);
2097 Result = Uart2Write(Byte);
2101 #ifndef DISABLE_PRU_UARTS
2104 Result = Uart3Write(Byte);
2110 Result = Uart4Write(Byte);
2129 Result = Uart1Read(pByte);
2135 Result = Uart2Read(pByte);
2139 #ifndef DISABLE_PRU_UARTS
2142 Result = Uart3Read(pByte);
2148 Result = Uart4Read(pByte);
2157 snprintf(UartBuffer,UARTBUFFERSIZE,
">%d 0x%02X\r\n",Port,*pByte);
2158 UartWrite(UartBuffer);
2174 Result = Uart1ReadData(pCmd,pData,pCheck,pFail);
2180 Result = Uart2ReadData(pCmd,pData,pCheck,pFail);
2184 #ifndef DISABLE_PRU_UARTS
2187 Result = Uart3ReadData(pCmd,pData,pCheck,pFail);
2193 Result = Uart4ReadData(pCmd,pData,pCheck,pFail);
2204 static void UartPortSetup(
UBYTE Port,
ULONG BitRate)
2207 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d SETTING BITRATE = %lu\r\n",Port,(
unsigned long)BitRate);
2208 UartWrite(UartBuffer);
2215 Uart1Setup(BitRate);
2221 Uart2Setup(BitRate);
2225 #ifndef DISABLE_PRU_UARTS
2228 Uart3Setup(BitRate);
2234 Uart4Setup(BitRate);
2246 static enum hrtimer_restart Device1TimerInterrupt1(
struct hrtimer *pTimer)
2256 UBYTE TmpBuffer[UART_DATA_LENGTH];
2257 #ifdef DEBUG_TRACE_US
2260 #ifdef DEBUG_TRACE_ANGLE
2264 hrtimer_forward_now(pTimer,Device1Time);
2269 if ((UartPort[Port].State >
UART_ENABLE) && (!TestMode))
2278 #ifdef DEBUG_D_UART_ERROR
2279 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d BREAK\r\n",Port);
2280 UartWrite(UartBuffer);
2283 UartPortDisable(Port);
2285 UartPortEnable(Port);
2286 UartPortSetup(Port,UartPort[Port].BitRate);
2287 for (Tmp = 0;Tmp < MAX_DEVICE_MODES;Tmp++)
2289 TypeData[Port][Tmp] = TypeDefaultUart[0];
2290 Changed[Port][Tmp] = 0;
2292 #ifndef DISABLE_FAST_DATALOG_BUFFER
2293 (*pUart).Actual[Port] = 0;
2294 (*pUart).LogIn[Port] = 0;
2296 (*pUart).Status[Port] = 0;
2297 UartPortFlush(Port);
2307 if (Port != DEBUG_UART)
2317 switch (UartPort[Port].State)
2325 WriteRequest[Port] = 0;
2340 UartPortDisable(Port);
2349 UartPortEnable(Port);
2350 UartPortSetup(Port,UartPort[Port].BitRate);
2351 for (Tmp = 0;Tmp < MAX_DEVICE_MODES;Tmp++)
2353 TypeData[Port][Tmp] = TypeDefaultUart[0];
2354 Changed[Port][Tmp] = 0;
2356 #ifndef DISABLE_FAST_DATALOG_BUFFER
2357 (*pUart).Actual[Port] = 0;
2358 (*pUart).LogIn[Port] = 0;
2360 (*pUart).Status[Port] = 0;
2361 UartPortFlush(Port);
2369 if (UartPortReceive(Port,&Byte))
2371 if (UartPort[Port].InPointer < 3)
2376 if (UartPort[Port].InPointer >= 3)
2379 UartPort[Port].
Check = 0xFF;
2380 for (Tmp = 0;Tmp < 2;Tmp++)
2384 if ((UartPort[Port].Check == UartPort[Port].InBuffer[2]) && (UartPort[Port].InBuffer[0] == 0x40) && (UartPort[Port].InBuffer[1] > 0) && (UartPort[Port].InBuffer[1] <= MAX_VALID_TYPE))
2389 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d TYPE = %-3u\r\n",Port,(
UWORD)UartPort[Port].Type & 0xFF);
2390 UartWrite(UartBuffer);
2396 #ifdef DEBUG_D_UART_ERROR
2402 for (Tmp = 0;Tmp < 2;Tmp++)
2413 UartPort[Port].
BitRate = 115200;
2414 UartPortSetup(Port,UartPort[Port].BitRate);
2415 TypeData[Port][0] = TypeDefaultUart[1];
2417 Changed[Port][0] = 1;
2426 if (UartPortReceive(Port,&Byte))
2429 switch (UartPort[Port].State)
2438 UartPort[Port].
Check = 0xFF;
2439 UartPort[Port].
Cmd = Byte;
2469 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d ACK RECEIVED\r\n",Port);
2470 UartWrite(UartBuffer);
2472 if (UartPort[Port].Types == 0)
2476 UartPort[Port].
Timer = 0;
2520 if (UartPort[Port].InPointer < UartPort[Port].InLength)
2531 if (UartPort[Port].Check != Byte)
2533 #ifdef DEBUG_D_UART_ERROR
2534 snprintf(UartBuffer,UARTBUFFERSIZE,
" c %d %02X[",Port,UartPort[Port].Cmd & 0xFF);
2535 UartWrite(UartBuffer);
2536 for (Tmp = 0;Tmp < UartPort[Port].
InLength;Tmp++)
2538 snprintf(UartBuffer,UARTBUFFERSIZE,
"%02X",UartPort[Port].InBuffer[Tmp] & 0xFF);
2539 UartWrite(UartBuffer);
2541 snprintf(UartBuffer,UARTBUFFERSIZE,
"]%02X\r\n",UartPort[Port].Check & 0xFF);
2542 UartWrite(UartBuffer);
2549 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d %02X[",Port,UartPort[Port].Cmd & 0xFF);
2550 UartWrite(UartBuffer);
2551 for (Tmp = 0;Tmp < UartPort[Port].
InLength;Tmp++)
2553 snprintf(UartBuffer,UARTBUFFERSIZE,
"%02X",UartPort[Port].InBuffer[Tmp] & 0xFF);
2554 UartWrite(UartBuffer);
2556 snprintf(UartBuffer,UARTBUFFERSIZE,
"]%02X\r\n",UartPort[Port].Check & 0xFF);
2557 UartWrite(UartBuffer);
2565 if ((UartPort[Port].InBuffer[0] >= 0) && (UartPort[Port].InBuffer[0] < MAX_DEVICE_MODES))
2571 #ifdef DEBUG_D_UART_ERROR
2572 snprintf(UartBuffer,UARTBUFFERSIZE,
" ## %d MODES ALREADY GIVEN\r\n",Port);
2573 UartWrite(UartBuffer);
2581 if (UartPort[Port].InLength >= 2)
2586 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d MODES = %u VIEWS = %u\r\n",Port,(
UWORD)UartPort[Port].Types & 0xFF,(
UWORD)UartPort[Port].Views & 0xFF);
2587 UartWrite(UartBuffer);
2595 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d MODES = %u = VIEWS\r\n",Port,(
UWORD)UartPort[Port].Types & 0xFF);
2596 UartWrite(UartBuffer);
2604 #ifdef DEBUG_D_UART_ERROR
2605 snprintf(UartBuffer,UARTBUFFERSIZE,
" ## %d MODES ERROR %u\r\n",Port,(
UWORD)UartPort[Port].Types & 0xFF);
2606 UartWrite(UartBuffer);
2617 for (Tmp = 0;Tmp < UartPort[Port].
InLength;Tmp++)
2619 TmpL |= (
ULONG)UartPort[Port].InBuffer[Tmp] << (8 * Tmp);
2622 if ((TmpL >= LOWEST_BITRATE) && (TmpL <= HIGHEST_BITRATE))
2628 #ifdef DEBUG_D_UART_ERROR
2629 snprintf(UartBuffer,UARTBUFFERSIZE,
" ## %d SPEED ALREADY GIVEN\r\n",Port);
2630 UartWrite(UartBuffer);
2636 if ((UartPort[Port].BitRate != LOWEST_BITRATE) && (TmpL <= MIDLE_BITRATE))
2639 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d SPEED ADJUST\r\n",Port);
2640 UartWrite(UartBuffer);
2650 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d SPEED = %lu\r\n",Port,(
unsigned long)UartPort[Port].BitRateMax);
2651 UartWrite(UartBuffer);
2658 #ifdef DEBUG_D_UART_ERROR
2659 snprintf(UartBuffer,UARTBUFFERSIZE,
" ## %d SPEED ERROR\r\n",Port);
2660 UartWrite(UartBuffer);
2678 switch (UartPort[Port].SubState)
2682 UartPort[Port].
InfoCmd = Byte;
2691 if (UartPort[Port].InLength < 2)
2693 #ifdef DEBUG_D_UART_ERROR
2694 snprintf(UartBuffer,UARTBUFFERSIZE,
" ## %d FORMAT ERROR\r\n",Port);
2695 UartWrite(UartBuffer);
2709 if (UartPort[Port].InPointer < UartPort[Port].InLength)
2721 if (UartPort[Port].Check != Byte)
2723 #ifdef DEBUG_D_UART_ERROR
2724 snprintf(UartBuffer,UARTBUFFERSIZE,
" c %d %02X ",Port,UartPort[Port].Cmd & 0xFF);
2725 UartWrite(UartBuffer);
2726 snprintf(UartBuffer,UARTBUFFERSIZE,
"%02X[",UartPort[Port].InfoCmd & 0xFF);
2727 UartWrite(UartBuffer);
2728 for (Tmp = 0;Tmp < UartPort[Port].
InLength;Tmp++)
2730 snprintf(UartBuffer,UARTBUFFERSIZE,
"%02X",UartPort[Port].InBuffer[Tmp] & 0xFF);
2731 UartWrite(UartBuffer);
2733 snprintf(UartBuffer,UARTBUFFERSIZE,
"]%02X\r\n",UartPort[Port].Check & 0xFF);
2734 UartWrite(UartBuffer);
2741 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d %02X ",Port,UartPort[Port].Cmd & 0xFF);
2742 UartWrite(UartBuffer);
2743 snprintf(UartBuffer,UARTBUFFERSIZE,
"%02X[",UartPort[Port].InfoCmd & 0xFF);
2744 UartWrite(UartBuffer);
2745 for (Tmp = 0;Tmp < UartPort[Port].
InLength;Tmp++)
2747 snprintf(UartBuffer,UARTBUFFERSIZE,
"%02X",UartPort[Port].InBuffer[Tmp] & 0xFF);
2748 UartWrite(UartBuffer);
2750 snprintf(UartBuffer,UARTBUFFERSIZE,
"]%02X\r\n",UartPort[Port].Check & 0xFF);
2751 UartWrite(UartBuffer);
2754 Mode =
GET_MODE(UartPort[Port].Cmd);
2763 if ((UartPort[Port].InBuffer[0] >=
'A') && (UartPort[Port].InBuffer[0] <=
'z') && (strlen(UartPort[Port].InBuffer) <=
TYPE_NAME_LENGTH))
2767 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d NAME = %s\r\n",Port,UartPort[Port].Name);
2768 UartWrite(UartBuffer);
2770 TypeData[Port][Mode].
Mode = Mode;
2775 #ifdef DEBUG_D_UART_ERROR
2777 snprintf(UartBuffer,UARTBUFFERSIZE,
" f %d NAME = %s\r\n",Port,UartPort[Port].InBuffer);
2778 UartWrite(UartBuffer);
2789 for (Tmp = 0;(Tmp < UartPort[Port].
InLength) && (Tmp < 4);Tmp++)
2791 TmpL |= (
ULONG)UartPort[Port].InBuffer[Tmp] << (8 * Tmp);
2793 *((
ULONG*)&TypeData[Port][Mode].RawMin) = TmpL;
2795 for (Tmp = 0;(Tmp < (UartPort[Port].
InLength - 4)) && (Tmp < 4);Tmp++)
2797 TmpL |= (
ULONG)UartPort[Port].InBuffer[Tmp + 4] << (8 * Tmp);
2799 *((
ULONG*)&TypeData[Port][Mode].RawMax) = TmpL;
2801 if (TypeData[Port][Mode].Mode ==
GET_MODE(UartPort[Port].Cmd))
2806 #ifdef DEBUG_D_UART_ERROR
2807 snprintf(UartBuffer,UARTBUFFERSIZE,
" ## %d RAW ALREADY GIVEN\r\n",Port);
2808 UartWrite(UartBuffer);
2815 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d RAW = Min..Max\r\n",Port);
2816 UartWrite(UartBuffer);
2823 #ifdef DEBUG_D_UART_ERROR
2824 snprintf(UartBuffer,UARTBUFFERSIZE,
" f %d RAW = Min..Max\r\n",Port);
2825 UartWrite(UartBuffer);
2836 for (Tmp = 0;(Tmp < UartPort[Port].
InLength) && (Tmp < 4);Tmp++)
2838 TmpL |= (
ULONG)UartPort[Port].InBuffer[Tmp] << (8 * Tmp);
2840 *((
ULONG*)&TypeData[Port][Mode].PctMin) = TmpL;
2842 for (Tmp = 0;(Tmp < (UartPort[Port].
InLength - 4)) && (Tmp < 4);Tmp++)
2844 TmpL |= (
ULONG)UartPort[Port].InBuffer[Tmp + 4] << (8 * Tmp);
2846 *((
ULONG*)&TypeData[Port][Mode].PctMax) = TmpL;
2848 if (TypeData[Port][Mode].Mode ==
GET_MODE(UartPort[Port].Cmd))
2854 #ifdef DEBUG_D_UART_ERROR
2855 snprintf(UartBuffer,UARTBUFFERSIZE,
" ## %d PCT ALREADY GIVEN\r\n",Port);
2856 UartWrite(UartBuffer);
2863 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d PCT = Min..Max\r\n",Port);
2864 UartWrite(UartBuffer);
2871 #ifdef DEBUG_D_UART_ERROR
2872 snprintf(UartBuffer,UARTBUFFERSIZE,
" f %d PCT = Min..Max\r\n",Port);
2873 UartWrite(UartBuffer);
2884 for (Tmp = 0;(Tmp < UartPort[Port].
InLength) && (Tmp < 4);Tmp++)
2886 TmpL |= (
ULONG)UartPort[Port].InBuffer[Tmp] << (8 * Tmp);
2888 *((
ULONG*)&TypeData[Port][Mode].SiMin) = TmpL;
2890 for (Tmp = 0;(Tmp < (UartPort[Port].
InLength - 4)) && (Tmp < 4);Tmp++)
2892 TmpL |= (
ULONG)UartPort[Port].InBuffer[Tmp + 4] << (8 * Tmp);
2894 *((
ULONG*)&TypeData[Port][Mode].SiMax) = TmpL;
2896 if (TypeData[Port][Mode].Mode ==
GET_MODE(UartPort[Port].Cmd))
2902 #ifdef DEBUG_D_UART_ERROR
2903 snprintf(UartBuffer,UARTBUFFERSIZE,
" ## %d SI ALREADY GIVEN\r\n",Port);
2904 UartWrite(UartBuffer);
2911 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d SI = Min..Max\r\n",Port);
2912 UartWrite(UartBuffer);
2919 #ifdef DEBUG_D_UART_ERROR
2920 snprintf(UartBuffer,UARTBUFFERSIZE,
" f %d SI = Min..Max\r\n",Port);
2921 UartWrite(UartBuffer);
2934 #ifdef DEBUG_D_UART_ERROR
2935 snprintf(UartBuffer,UARTBUFFERSIZE,
" ## %d SYMBOL ALREADY GIVEN\r\n",Port);
2936 UartWrite(UartBuffer);
2944 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d SYMBOL = %s\r\n",Port,TypeData[Port][Mode].Symbol);
2945 UartWrite(UartBuffer);
2958 #ifdef DEBUG_D_UART_ERROR
2959 snprintf(UartBuffer,UARTBUFFERSIZE,
" ## %d FORMAT ALREADY GIVEN\r\n",Port);
2960 UartWrite(UartBuffer);
2970 if (TypeData[Port][Mode].DataSets > 0)
2973 if (UartPort[Port].Types)
2976 UartPort[Port].
Types--;
2977 if (TypeData[Port][Mode].Mode ==
GET_MODE(UartPort[Port].Cmd))
2980 if (UartPort[Port].InLength >= 4)
2989 TypeData[Port][Mode].
Type = UartPort[Port].
Type;
2990 TypeData[Port][Mode].
Connection = CONN_INPUT_UART;
2991 TypeData[Port][Mode].
Views = UartPort[Port].
Views;
2997 if (TypeData[Port][Mode].Type == 33)
3002 Changed[Port][Mode] = 1;
3004 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d FORMAT = %u * %u %u.%u\r\n",Port,TypeData[Port][Mode].DataSets,TypeData[Port][Mode].Format,TypeData[Port][Mode].Figures,TypeData[Port][Mode].Decimals);
3005 UartWrite(UartBuffer);
3010 #ifdef DEBUG_D_UART_ERROR
3011 snprintf(UartBuffer,UARTBUFFERSIZE,
" ## %d NOT ENOUGH INFO GIVEN\r\n",Port);
3012 UartWrite(UartBuffer);
3020 #ifdef DEBUG_D_UART_ERROR
3021 snprintf(UartBuffer,UARTBUFFERSIZE,
" ## %d FORMAT ERROR\r\n",Port);
3022 UartWrite(UartBuffer);
3030 #ifdef DEBUG_D_UART_ERROR
3031 snprintf(UartBuffer,UARTBUFFERSIZE,
" f %d FORMAT = %u * %u %u.%u\r\n",Port,TypeData[Port][Mode].DataSets,TypeData[Port][Mode].Format,TypeData[Port][Mode].Figures,TypeData[Port][Mode].Decimals);
3032 UartWrite(UartBuffer);
3039 #ifdef DEBUG_D_UART_ERROR
3040 snprintf(UartBuffer,UARTBUFFERSIZE,
" ## %d TYPES ERROR\r\n",Port);
3041 UartWrite(UartBuffer);
3049 #ifdef DEBUG_D_UART_ERROR
3050 snprintf(UartBuffer,UARTBUFFERSIZE,
" f %d FORMAT = %u * %u %u.%u\r\n",Port,TypeData[Port][Mode].DataSets,TypeData[Port][Mode].Format,TypeData[Port][Mode].Figures,TypeData[Port][Mode].Decimals);
3051 UartWrite(UartBuffer);
3067 if (UartPort[Port].Type == UartPortDefault.
Type)
3069 #ifdef DEBUG_D_UART_ERROR
3070 snprintf(UartBuffer,UARTBUFFERSIZE,
" ## %d TYPE ERROR\r\n",Port);
3071 UartWrite(UartBuffer);
3102 UartPort[Port].
Check ^= Byte;
3112 UartPort[Port].
InLength = UartPortReadData(Port,&UartPort[Port].Cmd,TmpBuffer,&UartPort[Port].Check,&CrcError);
3114 if (UartPort[Port].InLength)
3118 if (UartPort[Port].Initialised == 0)
3124 if (UartPort[Port].Mode ==
GET_MODE(UartPort[Port].Cmd))
3130 #ifdef DEBUG_TRACE_MODE_CHANGE
3131 snprintf(UartBuffer,UARTBUFFERSIZE,
"d_uart %d State machine: mode changed to %d\n",Port,UartPort[Port].Mode);
3132 UartWrite(UartBuffer);
3136 #ifdef DEBUG_TRACE_US
3139 if (
GET_MODE(UartPort[Port].Cmd) == 1)
3141 In = (
UWORD)TmpBuffer[0];
3142 In |= (
UWORD)TmpBuffer[1] << 8;
3148 snprintf(UartBuffer,UARTBUFFERSIZE,
" c %d %02X[",Port,UartPort[Port].Cmd & 0xFF);
3152 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d %02X[",Port,UartPort[Port].Cmd & 0xFF);
3156 for (Tmp = 0;Tmp < UartPort[Port].
InLength;Tmp++)
3158 snprintf(UartBuffer,UARTBUFFERSIZE,
"%02X",TmpBuffer[Tmp] & 0xFF);
3161 snprintf(UartBuffer,UARTBUFFERSIZE,
"]%02X\n",UartPort[Port].Check & 0xFF);
3168 #ifndef DISABLE_FAST_DATALOG_BUFFER
3171 memcpy((
void*)(*pUart).Raw[Port][(*pUart).LogIn[Port]],(
void*)TmpBuffer,UART_DATA_LENGTH);
3173 (*pUart).Actual[Port] = (*pUart).LogIn[Port];
3174 (*pUart).Repeat[Port][(*pUart).Actual[Port]] = 0;
3176 if (++((*pUart).LogIn[Port]) >= DEVICE_LOGBUF_SIZE)
3178 (*pUart).LogIn[Port] = 0;
3181 memcpy((
void*)(*pUart).Raw[Port],(
void*)TmpBuffer,UART_DATA_LENGTH);
3183 if (UartPort[Port].DataErrors)
3187 UartPort[Port].
DataOk = 1;
3197 #ifdef DEBUG_D_UART_ERROR
3198 snprintf(UartBuffer,UARTBUFFERSIZE,
" c %d %02X[",Port,UartPort[Port].Cmd & 0xFF);
3199 UartWrite(UartBuffer);
3201 for (Tmp = 0;Tmp < UartPort[Port].
InLength;Tmp++)
3203 snprintf(UartBuffer,UARTBUFFERSIZE,
"%02X",TmpBuffer[Tmp] & 0xFF);
3204 UartWrite(UartBuffer);
3206 snprintf(UartBuffer,UARTBUFFERSIZE,
"]%02X\r\n",UartPort[Port].Check & 0xFF);
3207 UartWrite(UartBuffer);
3209 #ifndef DISABLE_UART_DATA_ERROR
3212 #ifdef DEBUG_D_UART_ERROR
3213 snprintf(UartBuffer,UARTBUFFERSIZE,
" ## %d No valid data in %d messages\r\n",Port,UartPort[Port].DataErrors);
3214 UartWrite(UartBuffer);
3220 #ifdef DEBUG_TRACE_ANGLE
3223 Angle = (
UWORD)(*pUart).Raw[Port][0];
3224 Angle |= (
UWORD)(*pUart).Raw[Port][1] << 8;
3228 printk(
"Angle = %u\r",Angle);
3231 snprintf(UartBuffer,UARTBUFFERSIZE,
" c %d %02X[",Port,UartPort[Port].Cmd & 0xFF);
3235 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d %02X[",Port,UartPort[Port].Cmd & 0xFF);
3239 for (Tmp = 0;Tmp < UartPort[Port].
InLength;Tmp++)
3241 snprintf(UartBuffer,UARTBUFFERSIZE,
"%02X",TmpBuffer[Tmp] & 0xFF);
3244 snprintf(UartBuffer,UARTBUFFERSIZE,
"]%02X\r\n",UartPort[Port].Check & 0xFF);
3253 ShowTimer[Port] = 0;
3256 snprintf(UartBuffer,UARTBUFFERSIZE,
" c %d %02X[",Port,UartPort[Port].Cmd & 0xFF);
3260 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d %02X[",Port,UartPort[Port].Cmd & 0xFF);
3262 UartWrite(UartBuffer);
3264 for (Tmp = 0;Tmp < UartPort[Port].
InLength;Tmp++)
3266 snprintf(UartBuffer,UARTBUFFERSIZE,
"%02X",TmpBuffer[Tmp] & 0xFF);
3267 UartWrite(UartBuffer);
3269 snprintf(UartBuffer,UARTBUFFERSIZE,
"]%02X\r\n",UartPort[Port].Check & 0xFF);
3270 UartWrite(UartBuffer);
3275 if (UartPort[Port].ChangeMode)
3278 if (UartPort[Port].OutPointer >= UartPort[Port].OutLength)
3282 ShowTimer[Port] = 0;
3285 snprintf(UartBuffer,UARTBUFFERSIZE,
" c %d %02X[",Port,UartPort[Port].Cmd & 0xFF);
3289 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d %02X[",Port,UartPort[Port].Cmd & 0xFF);
3291 UartWrite(UartBuffer);
3293 for (Tmp = 0;Tmp < UartPort[Port].
InLength;Tmp++)
3295 snprintf(UartBuffer,UARTBUFFERSIZE,
"%02X",TmpBuffer[Tmp] & 0xFF);
3296 UartWrite(UartBuffer);
3298 snprintf(UartBuffer,UARTBUFFERSIZE,
"]%02X\r\n",UartPort[Port].Check & 0xFF);
3299 UartWrite(UartBuffer);
3301 UartPort[Port].
Cmd = UartPort[Port].
Mode;
3310 #ifdef DEBUG_TRACE_MODE_CHANGE
3311 snprintf(UartBuffer,UARTBUFFERSIZE,
" WR %d %02X[",Port,UartPort[Port].OutBuffer[0]);
3312 UartWrite(UartBuffer);
3314 for (Tmp = 1;Tmp < (UartPort[Port].
OutLength - 1);Tmp++)
3316 snprintf(UartBuffer,UARTBUFFERSIZE,
"%02X",UartPort[Port].OutBuffer[Tmp] & 0xFF);
3317 UartWrite(UartBuffer);
3319 snprintf(UartBuffer,UARTBUFFERSIZE,
"]%02X\r\n",UartPort[Port].OutBuffer[Tmp] & 0xFF);
3320 UartWrite(UartBuffer);
3328 if (UartPort[Port].OutPointer >= UartPort[Port].OutLength)
3333 if (!UartPort[Port].DataOk)
3336 #ifndef DISABLE_UART_DATA_ERROR
3339 #ifdef DEBUG_D_UART_ERROR
3341 UartWrite(UartBuffer);
3347 UartPort[Port].
DataOk = 1;
3350 UartPort[Port].
DataOk = 1;
3353 if (UartPort[Port].DataOk)
3355 UartPort[Port].
DataOk = 0;
3361 snprintf(UartBuffer,UARTBUFFERSIZE,
" WD %d %02X\r\n",Port,UartPort[Port].OutBuffer[0]);
3362 UartWrite(UartBuffer);
3368 if (WriteRequest[Port])
3371 if (UartPort[Port].OutPointer >= UartPort[Port].OutLength)
3377 while ((Tmp < UART_DATA_LENGTH) && (Tmp < (*pUart).OutputLength[Port]))
3389 while (Pointer < Tmp)
3391 if (Pointer < (*pUart).OutputLength[Port])
3393 UartPort[Port].
OutBuffer[1 + Pointer] = (*pUart).Output[Port][Pointer];
3397 UartPort[Port].
OutBuffer[1 + Pointer] = 0;
3399 Chksum ^= UartPort[Port].
OutBuffer[1 + Pointer];
3403 UartPort[Port].
OutBuffer[1 + Pointer] = Chksum;
3407 WriteRequest[Port] = 0;
3410 snprintf(UartBuffer,UARTBUFFERSIZE,
" WR %d %02X[",Port,UartPort[Port].OutBuffer[0]);
3411 UartWrite(UartBuffer);
3413 for (Tmp = 1;Tmp < (UartPort[Port].
OutLength - 1);Tmp++)
3415 snprintf(UartBuffer,UARTBUFFERSIZE,
"%02X",UartPort[Port].OutBuffer[Tmp] & 0xFF);
3416 UartWrite(UartBuffer);
3418 snprintf(UartBuffer,UARTBUFFERSIZE,
"]%02X\r\n",UartPort[Port].OutBuffer[Tmp] & 0xFF);
3419 UartWrite(UartBuffer);
3424 #ifndef DISABLE_FAST_DATALOG_BUFFER
3425 ((*pUart).Repeat[Port][(*pUart).Actual[Port]])++;
3436 UartPort[Port].
Timer = 0;
3438 #ifdef DEBUG_D_UART_ERROR
3439 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d Type %-3d has changed modes: ",Port,TypeData[Port][0].Type);
3440 UartWrite(UartBuffer);
3441 for (Mode = 0;Mode < MAX_DEVICE_MODES;Mode++)
3443 UartBuffer[Mode] = Changed[Port][Mode] +
'0';
3445 UartBuffer[Mode++] =
'\r';
3446 UartBuffer[Mode++] =
'\n';
3447 UartBuffer[Mode] = 0;
3448 UartWrite(UartBuffer);
3458 UartPort[Port].
DataOk = 1;
3460 UartPort[Port].
Mode = 0;
3462 UartPortSetup(Port,UartPort[Port].BitRate);
3487 UartPortDisable(Port);
3491 for (Tmp = 0;Tmp < MAX_DEVICE_MODES;Tmp++)
3493 TypeData[Port][Tmp].
Name[0] = 0;
3494 TypeData[Port][Tmp].
Type = 0;
3495 Changed[Port][Tmp] = 0;
3497 (*pUart).Status[Port] = 0;
3504 if (UartPort[Port].OutPointer < UartPort[Port].OutLength)
3506 if (UartPortSend(Port,UartPort[Port].OutBuffer[UartPort[Port].OutPointer]))
3515 switch (UartPort[Port].State)
3527 UartPortDisable(Port);
3537 UartPortEnable(Port);
3538 UartPortSetup(Port,UartPort[Port].BitRate);
3539 (*pUart).Status[Port] = 0;
3540 UartPortFlush(Port);
3548 if (UartPort[Port].OutPointer < UartPort[Port].OutLength)
3550 if (UartPortSend(Port,UartPort[Port].OutBuffer[UartPort[Port].OutPointer]))
3555 if (UartPortReceive(Port,&Byte))
3558 snprintf(UartBuffer,UARTBUFFERSIZE,
"[0x%02X]\r\n",Byte);
3559 UartWrite(UartBuffer);
3561 if (UartPort[Port].InPointer < UART_BUFFER_SIZE)
3575 if (UartPort[Port].OldState != UartPort[Port].State)
3580 snprintf(UartBuffer,UARTBUFFERSIZE,
" %d %s\r\n",Port,UartStateText[UartPort[Port].State]);
3584 snprintf(UartBuffer,UARTBUFFERSIZE,
"*** %d %s ***\r\n",Port,UartStateText[UartPort[Port].State]);
3586 UartWrite(UartBuffer);
3592 return (HRTIMER_RESTART);
3596 static int Device1Ioctl(
struct inode *pNode,
struct file *File,
unsigned int Request,
unsigned long Pointer)
3610 copy_from_user((
void*)&DevCon,(
void*)Pointer,
sizeof(
DEVCON));
3612 for (Port = 0;Port <
INPUTS;Port++)
3614 if (DevCon.
Connection[Port] == CONN_INPUT_UART)
3616 if (UartConfigured[Port] == 0)
3618 UartConfigured[Port] = 1;
3623 if (UartPort[Port].Initialised)
3625 if (UartPort[Port].Mode != DevCon.
Mode[Port])
3627 #ifdef DEBUG_TRACE_MODE_CHANGE
3628 snprintf(UartBuffer,UARTBUFFERSIZE,
"d_uart %d Device1Ioctl: Changing to %c\r\n",Port,DevCon.
Mode[Port] +
'0');
3629 UartWrite(UartBuffer);
3631 UartPort[Port].
Mode = DevCon.
Mode[Port];
3641 if (UartConfigured[Port])
3643 UartConfigured[Port] = 0;
3655 Port = (*pUartCtl).
Port;
3656 Mode = (*pUartCtl).Mode;
3659 if (TypeData[Port][Mode].Name[0])
3661 snprintf(UartBuffer,UARTBUFFERSIZE,
"d_uart %d Device1Ioctl: READ Type=%d Mode=%d\r\n",Port,TypeData[Port][Mode].Type,Mode);
3662 UartWrite(UartBuffer);
3665 if ((Mode < MAX_DEVICE_MODES) && (Port < INPUTS))
3667 copy_to_user((
void*)&(*pUartCtl).TypeData,(
void*)&TypeData[Port][Mode],
sizeof(
TYPES));
3668 if (Changed[Port][Mode] == 0)
3670 (*pUartCtl).TypeData.Name[0] = 0;
3672 Changed[Port][Mode] = 0;
3680 Port = (*pUartCtl).
Port;
3681 Mode = (*pUartCtl).Mode;
3684 snprintf(UartBuffer,UARTBUFFERSIZE,
"d_uart %d Device1Ioctl: NACK Type=%d Mode=%d\r\n",Port,TypeData[Port][Mode].Type,Mode);
3685 UartWrite(UartBuffer);
3687 if ((Mode < MAX_DEVICE_MODES) && (Port < INPUTS))
3689 Changed[Port][Mode] = 1;
3697 Port = (*pUartCtl).
Port;
3709 static ssize_t Device1Write(
struct file *File,
const char *
Buffer,
size_t Count,loff_t *Data)
3711 char Buf[UART_DATA_LENGTH + 1];
3715 if (Count <= (UART_DATA_LENGTH + 1))
3717 copy_from_user(Buf,Buffer,Count);
3726 (*pUart).Output[Port][Lng - 1] = Buf[Lng];
3729 (*pUart).OutputLength[Port] = Lng - 1;
3731 WriteRequest[Port] = 1;
3738 static ssize_t Device1Read(
struct file *File,
char *Buffer,
size_t Count,loff_t *Offset)
3742 #ifdef DEBUG_UART_WRITE
3743 if (LogOutPointer != LogPointer)
3745 while ((Count--) && (LogOutPointer != LogPointer))
3747 Buffer[Lng++] = LogPool[LogOutPointer];
3751 if (LogOutPointer >= LOGPOOLSIZE)
3759 UartBuffer[0] = 0x1B;
3760 UartBuffer[1] =
'[';
3761 UartBuffer[2] =
'2';
3762 UartBuffer[3] =
'J';
3763 UartBuffer[4] = 0x1B;
3764 UartBuffer[5] =
'[';
3765 UartBuffer[6] =
'H';
3767 UartWrite(UartBuffer);
3768 UartWrite(UartBuffer);
3769 snprintf(UartBuffer,UARTBUFFERSIZE,
"-----------------------------------------------------------------\r\n");
3770 UartWrite(UartBuffer);
3771 snprintf(UartBuffer,UARTBUFFERSIZE,
" UART DUMP\r\n");
3772 UartWrite(UartBuffer);
3773 snprintf(UartBuffer,UARTBUFFERSIZE,
"-----------------------------------------------------------------\r\n");
3774 UartWrite(UartBuffer);
3782 while ((Count > Tmp) && (Port < INPUTS))
3784 if (Port != (INPUTS - 1))
3786 Tmp =
snprintf(&Buffer[Lng],4,
"%2u ",(
UWORD)UartPort[Port].State);
3790 Tmp =
snprintf(&Buffer[Lng],5,
"%2u\r",(
UWORD)UartPort[Port].State);
3802 #define SHM_LENGTH (sizeof(UartDefault))
3803 #define NPAGES ((SHM_LENGTH + PAGE_SIZE - 1) / PAGE_SIZE)
3804 static void *kmalloc_ptr;
3806 static int Device1Mmap(
struct file *filp,
struct vm_area_struct *vma)
3810 ret = remap_pfn_range(vma,vma->vm_start,virt_to_phys((
void*)((
unsigned long)pUart)) >> PAGE_SHIFT,vma->vm_end-vma->vm_start,PAGE_SHARED);
3821 static const struct file_operations Device1Entries =
3823 .owner = THIS_MODULE,
3824 .read = Device1Read,
3825 .write = Device1Write,
3826 .mmap = Device1Mmap,
3827 .ioctl = Device1Ioctl,
3831 static struct miscdevice Device1 =
3842 static int Device1Init(
void)
3849 Result = misc_register(&Device1);
3857 if ((kmalloc_ptr = kmalloc((
NPAGES + 2) * PAGE_SIZE, GFP_KERNEL)) !=
NULL)
3859 pTmp = (
UWORD*)((((
unsigned long)kmalloc_ptr) + PAGE_SIZE - 1) & PAGE_MASK);
3860 for (i = 0; i <
NPAGES * PAGE_SIZE; i += PAGE_SIZE)
3862 SetPageReserved(virt_to_page(((
unsigned long)pTmp) + i));
3864 pUart = (UART*)pTmp;
3865 memset(pUart,0,
sizeof(UART));
3867 Result += Uart1Init();
3868 Result += Uart2Init();
3869 #ifndef DISABLE_PRU_UARTS
3870 Result += Uart3Init();
3871 Result += Uart4Init();
3874 for (Tmp = 0;Tmp <
INPUTS;Tmp++)
3877 UartConfigured[Tmp] = 0;
3879 if (Tmp == DEBUG_UART)
3881 UartPort[Tmp].
BitRate = 115200;
3882 UartPortSetup(Tmp,UartPort[Tmp].BitRate);
3883 TypeData[Tmp][0] = TypeDefaultUart[1];
3885 UartPortEnable(Tmp);
3888 UartWrite(UartBuffer);
3893 UartPortDisable(Tmp);
3901 hrtimer_init(&Device1Timer,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
3902 Device1Timer.function = Device1TimerInterrupt1;
3903 hrtimer_start(&Device1Timer,Device1Time,HRTIMER_MODE_REL);
3904 #ifdef DEBUG_UART_WRITE
3905 UartBuffer[0] = 0x1B;
3906 UartBuffer[1] =
'[';
3907 UartBuffer[2] =
'2';
3908 UartBuffer[3] =
'J';
3909 UartBuffer[4] = 0x1B;
3910 UartBuffer[5] =
'[';
3911 UartBuffer[6] =
'H';
3913 UartWrite(UartBuffer);
3914 snprintf(UartBuffer,UARTBUFFERSIZE,
"-----------------------------------------------------------------\r\n");
3915 UartWrite(UartBuffer);
3916 snprintf(UartBuffer,UARTBUFFERSIZE,
" UART DUMP\r\n");
3917 UartWrite(UartBuffer);
3918 snprintf(UartBuffer,UARTBUFFERSIZE,
"-----------------------------------------------------------------\r\n");
3919 UartWrite(UartBuffer);
3928 static void Device1Exit(
void)
3934 hrtimer_cancel(&Device1Timer);
3936 #ifndef DISABLE_PRU_UARTS
3944 for (Tmp = 0;Tmp <
INPUTS;Tmp++)
3948 if (Tmp == DEBUG_UART)
3950 UartPortEnable(Tmp);
3954 UartPortDisable(Tmp);
3957 if (Tmp == DEBUG_UART)
3966 pTmp = (
UWORD*)pUart;
3967 pUart = &UartDefault;
3969 for (i = 0; i <
NPAGES * PAGE_SIZE; i+= PAGE_SIZE)
3971 ClearPageReserved(virt_to_page(((
unsigned long)pTmp) + i));
3973 printk(
" %s memory page %d unmapped\n",
DEVICE1_NAME,i);
3978 misc_deregister(&Device1);
3982 for (Tmp = 0;Tmp <
INPUTS;Tmp++)
3984 if (Base[Tmp] !=
NULL)
3998 #define BUFFER_LNG 16
4000 static int Device2Ioctl(
struct inode *pNode,
struct file *File,
unsigned int Request,
unsigned long Pointer)
4007 copy_from_user((
void*)&Tstuart,(
void*)Pointer,
sizeof(
TSTUART));
4008 Port = Tstuart.
Port;
4015 for (Port = 0;Port <
INPUTS;Port++)
4028 for (Port = 0;Port <
INPUTS;Port++)
4030 UartPortDisable(Port);
4032 (*pUart).Status[Port] = 0;
4040 for (Port = 0;Port <
INPUTS;Port++)
4051 for (Port = 0;Port <
INPUTS;Port++)
4062 for (Poi = 0;(Poi < Tstuart.
Length) && (Poi < UART_BUFFER_SIZE);Poi++)
4074 for (Poi = 0;(Poi < UartPort[Port].
InPointer) && (Poi < Tstuart.
Length) && (Poi < UART_BUFFER_SIZE);Poi++)
4079 copy_to_user((
void*)Pointer,(
void*)&Tstuart,
sizeof(
TSTUART));
4090 static ssize_t Device2Write(
struct file *File,
const char *Buffer,
size_t Count,loff_t *Data)
4098 static ssize_t Device2Read(
struct file *File,
char *Buffer,
size_t Count,loff_t *Offset)
4106 static const struct file_operations Device2Entries =
4108 .owner = THIS_MODULE,
4109 .read = Device2Read,
4110 .write = Device2Write,
4111 .ioctl = Device2Ioctl
4115 static struct miscdevice Device2 =
4123 static int Device2Init(
void)
4127 Result = misc_register(&Device2);
4143 static void Device2Exit(
void)
4145 misc_deregister(&Device2);
4159 static int ModuleInit(
void)
4176 if (request_mem_region(DA8XX_GPIO_BASE,0xD8,
MODULE_NAME) >= 0)
4178 GpioBase = (
void*)ioremap(DA8XX_GPIO_BASE,0xD8);
4179 if (GpioBase !=
NULL)
4187 #ifndef DISABLE_PRU_UARTS
4201 static void ModuleExit(
void)
4210 #ifndef DISABLE_PRU_UARTS
#define PUARTHigh(port, pin)
#define PUARTLow(port, pin)
#define INFODATA_INFO_SYMBOL
#define INFODATA_CMD_TYPE
#define PUARTFloat(port, pin)
DATA8 Changed[INPUTS][MAX_DEVICE_MODES]
#define UART_ALLOWABLE_DATA_ERRORS
#define UART_DATA_READY
Data is ready.
signed char SBYTE
Basic Type used to symbolise 8 bit signed values.
int lego_pru_uart_init(int port)
SBYTE Name[TYPE_NAME_LENGTH+1]
Device name.
#define EP2
Schematics revision D.
#define INFODATA_INFO_RAW
irqreturn_t Uart2Interrupt(int irq, void *dev_id)
UBYTE InBuffer[UART_BUFFER_SIZE]
#define FINAL
Final prototype.
#define INFODATA_INFO_PCT
int lego_pru_write_bytes(int port, unsigned char *pdata, int size)
#define INFODATA_CMD_MODES
#define UART_WATCHDOG_TIME
INPIN InputUartPin[NO_OF_INPUT_PORTS][INPUT_UART_PINS]
void lego_pru_uart_exit(int port)
module_param(HwId, charp, 0)
volatile ULONG * Uart2Base
#define INFODATA_INFO_FORMAT
Port not empty but type has not been determined.
#define UART_CHANGE_BITRATE_DELAY
#define MAKE_CMD_COMMAND(C, LC)
#define NO_OF_INPUT_PORTS
#define Uart1_RECEIVE_TIMER_RESOLUTION
#define GET_INFO_COMMAND(B)
unsigned int ULONG
Basic Type used to symbolise 32 bit unsigned values.
void lego_pru_suart_exit(void)
TYPES TypeData[INPUTS][MAX_DEVICE_MODES]
TypeData.
#define GET_MESSAGE_TYPE(B)
#define UART_CLEAR_CHANGED
#define SYMBOL_LENGTH
Symbol leng th (not including zero)
DATA8 String[TST_UART_LENGTH]
UBYTE WriteRequest[INPUTS]
#define UART_NACK_MODE_INFO
int lego_pru_uart_activate(int port)
#define UART_TERMINAL_DELAY
#define FINALB
Schematics revision B and C.
unsigned long UartBaseAddr[]
UBYTE OutBuffer[UART_BUFFER_SIZE]
MODULE_SUPPORTED_DEVICE(DEVICE1_NAME)
#define GET_CMD_COMMAND(B)
#define UART_TIMER_RESOLUTION
#define UART_READ_MODE_INFO
volatile ULONG * Uart1Base
MODULE_AUTHOR("The LEGO Group")
#define INFODATA_INFO_NAME
#define PUARTRead(port, pin)
#define UART_WRITE_REQUEST
Write request.
void lego_pru_uart_deactivate(int port)
unsigned char UBYTE
Basic Type used to symbolise 8 bit unsigned values.
Port is connected to a terminal.
MODULE_DESCRIPTION(MODULE_NAME)
unsigned short UWORD
Basic Type used to symbolise 16 bit unsigned values.
#define GET_MESSAGE_LENGTH(B)
int lego_pru_read_bytes(int port, unsigned char *pdata, int size)
UWORD InvalidTime
mS from type change to valid data
UARTPORT UartPort[INPUTS]
UBYTE UartConfigured[INPUTS]
char UartStateText[UART_STATES][50]
SBYTE DATA8
VM Type for 1 byte signed value.
#define CONVERT_LENGTH(C)
#define UART_PORT_CHANGED
Input port changed.
#define INPUTS
Number of input ports in the system.
irqreturn_t Uart1Interrupt(int irq, void *dev_id)
#define PLATFORM_END
Newest supported hardware (newer versions will use this)
void lego_pru_set_baudrate(int port, unsigned int baud)
#define INFODATA_CMD_SPEED
int lego_pru_suart_init(void)
#define PLATFORM_START
Oldest supported hardware (older versions will use this)
volatile ULONG * Base[INPUTS]