123

Dependencies:   mbed

Fork of LG by igor Apu

DeviceServicePort.c

Committer:
Kovalev_D
Date:
2016-10-19
Revision:
197:7a05523bf588
Parent:
182:2bd8ec44998f

File content as of revision 197:7a05523bf588:

#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 InitServicePortDefaultSettings(void) {
  device.service.port.settings.baud = 921600;
}

void InitServicePortState(void){ 
  device.service.port.state.baud = device.service.port.settings.baud;
}

void DeviceStartServicePort(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.controller.uart[0].state.LCR = 0x83;
  LPC_UART0->LCR  = device.controller.uart[0].state.LCR;
  
  Fdiv = ((pclk  / 16) / device.service.port.state.baud) + 1;
  
  device.controller.uart[0].state.DLM = Fdiv / 256;
  LPC_UART0->DLM = device.controller.uart[0].state.DLM;
  device.controller.uart[0].state.DLL = Fdiv % 256;
  LPC_UART0->DLL = device.controller.uart[0].state.DLL; 

  device.controller.uart[0].state.LCR = 0x03;
  LPC_UART0->LCR  = device.controller.uart[0].state.LCR;
  device.controller.uart[0].state.FCR = 0x07;
  LPC_UART0->FCR  = device.controller.uart[0].state.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(void) 
{ 
  uint16_t i = 0;
  while (ConInPnt != ConInCur){
    device.service.buffer[i] = InCon[ConInPnt];
    ConInPnt++; ConInPnt = ConInPnt & 0x3ff;
    i++;
  }
  device.service.buffer[i] = 0;
  return i;
}

void WriteConcole(void) {
  uint16_t i = 0;
  while (device.service.buffer[i] != 0){
    OutCon[ConOutPnt] = device.service.buffer[i];
    ConOutPnt++; ConOutPnt = ConOutPnt & 0x3ff; 
    i++;
  }
}