123

Dependencies:   mbed

Fork of LG by igor Apu

Revision:
149:abbf7663d27d
Child:
156:e68ee0bcdcda
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DeviceServicePort.c	Tue May 03 05:12:26 2016 +0000
@@ -0,0 +1,93 @@
+#include "Device.h"
+extern Device device;
+extern unsigned int SystemCoreClock1;
+
+extern char InCon[1024];
+extern char OutCon[1024];
+
+extern unsigned int  ConInPnt;
+extern unsigned int  ConInCur;
+extern unsigned int  ConOutPnt;
+extern unsigned int  ConOutCur;
+
+void InitServicePortWithDefaults(void)
+{
+  device.service.port.transmitter.baudRate = 921600;
+}
+
+void InitServicePort(void){ 
+  uint32_t Fdiv;
+  uint32_t pclkdiv, pclk;
+
+  LPC_PINCON->PINSEL0 |= (1 << 4);             /* Pin P0.2 used as TXD0 (Com0) */
+  LPC_PINCON->PINSEL0 |= (1 << 6);             /* Pin P0.3 used as RXD0 (Com0) */
+    
+  /* By default, the PCLKSELx value is zero, thus, the PCLK for all the peripherals is 1/4 of the SystemFrequency. */
+  /* Bit 6,7 are for UART0 */
+  pclkdiv = (LPC_SC->PCLKSEL0 >> 6) & 0x03;
+  switch ( pclkdiv ) {
+      case 0x00:
+      default:
+        pclk = SystemCoreClock1/4;
+        break;
+      case 0x01:
+        pclk = SystemCoreClock1;
+        break; 
+      case 0x02:
+        pclk = SystemCoreClock1/2;
+        break; 
+      case 0x03:
+        pclk = SystemCoreClock1/8;
+        break;
+  }
+  
+  device.service.port.LCR = 0x83;
+  LPC_UART0->LCR  = device.service.port.LCR;
+  
+  Fdiv = ((pclk  / 16) / device.service.port.transmitter.baudRate) + 1;
+  
+  device.service.port.DLM = Fdiv / 256;
+  LPC_UART0->DLM = device.service.port.DLM;
+  device.service.port.DLL = Fdiv % 256;
+  LPC_UART0->DLL = device.service.port.DLL; 
+
+  device.service.port.LCR = 0x03;
+  LPC_UART0->LCR  = device.service.port.LCR;
+  device.service.port.FCR = 0x07;
+  LPC_UART0->FCR  = device.service.port.FCR;
+}
+
+void Concole(void)
+{
+  if (ConOutPnt != ConOutCur)
+    if (LPC_UART0->LSR & 0x20){
+      LPC_UART0->THR = OutCon[ConOutCur];
+      ConOutCur++;
+      ConOutCur = ConOutCur & 0x3ff;
+    }
+  if (LPC_UART0->LSR & 0x01){
+    InCon[ConInCur] = (LPC_UART0->RBR);
+    ConInCur++;
+    ConInCur = ConInCur & 0x3ff;
+ }
+} 
+
+int ReadConcole(char *s) 
+{ 
+  int i = 0;
+  while (ConInPnt != ConInCur){
+    (*s++) = InCon[ConInPnt];
+    ConInPnt++; ConInPnt = ConInPnt & 0x3ff;
+    i++;
+  }
+  (*s++) = 0;
+  return i;
+}
+
+void WriteConcole(char *s)
+{
+  while (*s != 0){
+    OutCon[ConOutPnt] = (*s++);
+    ConOutPnt++; ConOutPnt = ConOutPnt & 0x3ff; 
+  }
+}
\ No newline at end of file