|
|
/*------------------------------------------------------------------------------
|
|
|
-- This file is a part of the libuc, microcontroler library
|
|
|
-- Copyright (C) 2011, Alexis Jeandet
|
|
|
--
|
|
|
-- This program is free software; you can redistribute it and/or modify
|
|
|
-- it under the terms of the GNU General Public License as published by
|
|
|
-- the Free Software Foundation; either version 3 of the License, or
|
|
|
-- (at your option) any later version.
|
|
|
--
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
-- GNU General Public License for more details.
|
|
|
--
|
|
|
-- You should have received a copy of the GNU General Public License
|
|
|
-- along with this program; if not, write to the Free Software
|
|
|
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
-------------------------------------------------------------------------------
|
|
|
-- Author : Alexis Jeandet
|
|
|
-- Mail : alexis.jeandet@gmail.com
|
|
|
-------------------------------------------------------------------------------*/
|
|
|
#include "core.h"
|
|
|
#include "uart.h"
|
|
|
|
|
|
|
|
|
void uartputc(uartDev* dev,char c) {
|
|
|
while (!((dev->LineStat & (1<<5))));
|
|
|
dev->RWreg = c;
|
|
|
}
|
|
|
|
|
|
char uartgetc(uartDev* dev) {
|
|
|
while (!((dev->LineStat & 1)));
|
|
|
return (char)dev->RWreg;
|
|
|
}
|
|
|
|
|
|
|
|
|
void uartputs(uartDev* dev,char *s) {
|
|
|
while (*s) uartputc(dev,*s++);
|
|
|
}
|
|
|
|
|
|
|
|
|
void uartgets(uartDev* dev,char *s) {
|
|
|
while (*s && (*s!=0xd)) *s++ = uartgetc(dev);
|
|
|
}
|
|
|
|
|
|
void uartoff(int count)
|
|
|
{
|
|
|
switch(count)
|
|
|
{
|
|
|
case 0:
|
|
|
PCONP &= ~( 1 << 3 );
|
|
|
break;
|
|
|
case 1:
|
|
|
PCONP &= ~( 1 << 4 );
|
|
|
break;
|
|
|
case 2:
|
|
|
PCONP &= ~( 1 << 24 );
|
|
|
break;
|
|
|
case 3:
|
|
|
PCONP &= ~( 1 << 25 );
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void uarton(int count)
|
|
|
{
|
|
|
switch(count)
|
|
|
{
|
|
|
case 0:
|
|
|
PCONP |= ( 1 << 3 );
|
|
|
break;
|
|
|
case 1:
|
|
|
PCONP |= ( 1 << 4 );
|
|
|
break;
|
|
|
case 2:
|
|
|
PCONP |= ( 1 << 24 );
|
|
|
break;
|
|
|
case 3:
|
|
|
PCONP |= ( 1 << 25 );
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void uartsetup(uartDev* dev,unsigned int baudRate,unsigned char WordLength,unsigned char StopBitCnt,unsigned char Parity)
|
|
|
{
|
|
|
if(WordLength>9)WordLength=8;
|
|
|
if((StopBitCnt>2)||(StopBitCnt==0))StopBitCnt=1;
|
|
|
if(Parity>7)Parity=0;
|
|
|
dev->LineCtrl = (WordLength-5) + ((StopBitCnt-1)<<2) + (Parity<<3);
|
|
|
uartsetbaudrate(dev,baudRate);
|
|
|
}
|
|
|
|
|
|
unsigned char uartgetpclkfactor(uartDev* dev)
|
|
|
{
|
|
|
unsigned int clksel=0;
|
|
|
const char clkselDec[]={4,1,2,8};
|
|
|
switch((int)dev)
|
|
|
{
|
|
|
case (int)UART0_BASE_ADDR:
|
|
|
clksel = (PCLKSEL0>>6) & 3;
|
|
|
break;
|
|
|
case (int)UART1_BASE_ADDR:
|
|
|
clksel = (PCLKSEL0>>8) & 3;
|
|
|
break;
|
|
|
case (int)UART2_BASE_ADDR:
|
|
|
clksel = (PCLKSEL1>>16) & 3;
|
|
|
break;
|
|
|
case (int)UART3_BASE_ADDR:
|
|
|
clksel = (PCLKSEL1>>18) & 3;
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
return clkselDec[clksel];
|
|
|
}
|
|
|
|
|
|
void uartsetpclkfactor(uartDev* dev,unsigned char pclkfactor)
|
|
|
{
|
|
|
const char clkselDec[]={1,1,2,2,0,0,0,0,3};
|
|
|
unsigned int clksel=0;
|
|
|
switch((int)dev)
|
|
|
{
|
|
|
case (int)UART0_BASE_ADDR:
|
|
|
PCLKSEL0 |= clkselDec[pclkfactor]<<6;
|
|
|
PCLKSEL0 &= clkselDec[pclkfactor]<<6;
|
|
|
break;
|
|
|
case (int)UART1_BASE_ADDR:
|
|
|
PCLKSEL0 |= clkselDec[pclkfactor]<<8;
|
|
|
PCLKSEL0 &= clkselDec[pclkfactor]<<8;
|
|
|
break;
|
|
|
case (int)UART2_BASE_ADDR:
|
|
|
PCLKSEL1 |= clkselDec[pclkfactor]<<16;
|
|
|
PCLKSEL1 &= clkselDec[pclkfactor]<<16;
|
|
|
break;
|
|
|
case (int)UART3_BASE_ADDR:
|
|
|
PCLKSEL1 |= clkselDec[pclkfactor]<<18;
|
|
|
PCLKSEL1 &= clkselDec[pclkfactor]<<18;
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void uartsetbaudrate(uartDev* dev,unsigned int baudRate)
|
|
|
{
|
|
|
unsigned int pclk = 0;
|
|
|
unsigned int clksel=0;
|
|
|
unsigned int cpuclk=0;
|
|
|
if(dev==0)return;
|
|
|
cpuclk = coregetCpuFreq();
|
|
|
pclk = cpuclk / uartgetpclkfactor(dev);
|
|
|
while((baudRate*16)>pclk)
|
|
|
{
|
|
|
unsigned char pclkfact= uartgetpclkfactor(dev);
|
|
|
if(pclkfact==1)return;
|
|
|
uartsetpclkfactor(dev,pclkfact/2);
|
|
|
pclk = cpuclk / uartgetpclkfactor(dev);
|
|
|
}
|
|
|
dev->LineCtrl |= 128;
|
|
|
dev->RWreg = pclk/(baudRate*16);
|
|
|
dev->LineCtrl &= ~(128);
|
|
|
}
|
|
|
|
|
|
uartDev* uartopen(int count){
|
|
|
uartDev* dev;
|
|
|
switch(count)
|
|
|
{
|
|
|
case 0:
|
|
|
dev = (uartDev*)((unsigned long)UART0_BASE_ADDR);
|
|
|
break;
|
|
|
case 1:
|
|
|
dev = (uartDev*)((unsigned long)UART1_BASE_ADDR);
|
|
|
break;
|
|
|
case 2:
|
|
|
dev = (uartDev*)((unsigned long)UART2_BASE_ADDR);
|
|
|
break;
|
|
|
case 3:
|
|
|
dev = (uartDev*)((unsigned long)UART3_BASE_ADDR);
|
|
|
break;
|
|
|
default:
|
|
|
dev = (uartDev*)0;
|
|
|
break;
|
|
|
}
|
|
|
return dev;
|
|
|
}
|
|
|
|