##// END OF EJS Templates
Generic lib clean target bug fixed
Generic lib clean target bug fixed

File last commit:

r16:194f637a2640 default
r17:24654bf85fa1 default
Show More
uart.c
316 lines | 6.8 KiB | text/x-c | CLexer
/*------------------------------------------------------------------------------
-- 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 <uart.h>
#include <stm32f4xx_usart.h>
#include <stm32f4xx_rcc.h>
int uartopen(int count ,uart_t* uart)
{
switch(count)
{
case 0:
uart->_dev = (void*)USART1;
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
break;
case 1:
uart->_dev = (void*)USART2;
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
break;
case 2:
uart->_dev = (void*)USART3;
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
break;
case 3:
uart->_dev = (void*)UART4;
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
break;
case 4:
uart->_dev = (void*)UART5;
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
break;
case 5:
uart->_dev = (void*)USART6;
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, DISABLE);
break;
default:
break;
}
((USART_TypeDef *)(uart->_dev))->CR3 &= ~((1<<8) + (1<<9));
return 1;
}
int uartclose(uart_t* uart)
{
switch((int)uart->_dev)
{
case (int)(void*)USART1:
uart->_dev = (void*)USART1;
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
break;
case (int)(void*)USART2:
uart->_dev = (void*)USART2;
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
break;
case (int)(void*)USART3:
uart->_dev = (void*)USART3;
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
break;
case (int)(void*)UART4:
uart->_dev = (void*)UART4;
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
break;
case (int)(void*)UART5:
uart->_dev = (void*)UART5;
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
break;
case (int)(void*)USART6:
uart->_dev = (void*)USART6;
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, ENABLE);
break;
default:
break;
}
return 1;
}
int uartsetconfig(uart_t* uart)
{
int res=1;
uartdisable(uart);
uartsetspeed(uart,uart->speed);
uartsetparity(uart,uart->cfg & UARTPARITYMASK);
uartsetdatabits(uart,uart->cfg & UARTBITSMASK);
uartsetstopbits(uart,uart->cfg & UARTSTOPBITSMASK);
uartenable(uart);
return res;
}
int uartenable(uart_t* uart)
{
((USART_TypeDef *)(uart->_dev))->CR1 |= (1<<2) + (1<<3) + (1<<13);
return 1;
}
int uartdisable(uart_t* uart)
{
((USART_TypeDef *)(uart->_dev))->CR1 &= ~((1<<2) + (1<<3) +(1<<13));
return 1;
}
int uartsetspeed(uart_t* uart,int speed)
{
uart->speed = speed;
uint32_t tmpreg = 0x00, apbclock = 0x00;
uint32_t integerdivider = 0x00;
uint32_t fractionaldivider = 0x00;
RCC_ClocksTypeDef RCC_ClocksStatus;
RCC_GetClocksFreq(&RCC_ClocksStatus);
if (((USART_TypeDef *)(uart->_dev) == USART1) || (((USART_TypeDef *)(uart->_dev) == USART6)))
{
apbclock = RCC_ClocksStatus.PCLK2_Frequency;
}
else
{
apbclock = RCC_ClocksStatus.PCLK1_Frequency;
}
if (((((USART_TypeDef *)(uart->_dev))->CR1) & USART_CR1_OVER8) != 0)
{
integerdivider = ((25 * apbclock) / (2 * (speed)));
}
else
{
integerdivider = ((25 * apbclock) / (4 * (speed)));
}
tmpreg = (integerdivider / 100) << 4;
fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
if ((((USART_TypeDef *)(uart->_dev))->CR1 & USART_CR1_OVER8) != 0)
{
tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
}
else
{
tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
}
((USART_TypeDef *)(uart->_dev))->BRR = (uint16_t)tmpreg;
return 1;
}
int uartsetparity(uart_t* uart,uartparity_t parity)
{
uart->cfg &= (UARTPARITYMASK ^ -1);
uart->cfg |= parity;
((USART_TypeDef *)(uart->_dev))->CR1 &= (((1<<9)+(1<<10)) ^ -1);
switch(parity)
{
case uartparityeven:
((USART_TypeDef *)(uart->_dev))->CR1 |= (1<<10);
break;
case uartparityodd:
((USART_TypeDef *)(uart->_dev))->CR1 |= (1<<10) + (1<<9);
break;
case uartparitynone:
break;
default :
return 0;
break;
}
return 1;
}
int uartsetdatabits(uart_t* uart,uartbits_t databits)
{
uart->cfg &= UARTBITSMASK ^-1;
uart->cfg |= databits;
((USART_TypeDef *)(uart->_dev))->CR1 &= (((1<<12)) ^ -1);
switch(databits)
{
case uart7bits:
return 0;
break;
case uart8bits:
break;
case uart9bits:
((USART_TypeDef *)(uart->_dev))->CR1 |= (1<<12);
break;
default :
return 0;
break;
}
return 1;
}
int uartsetstopbits(uart_t* uart,uartstopbits_t stopbits)
{
uart->cfg &= UARTSTOPBITSMASK ^-1;
uart->cfg |= stopbits;
((USART_TypeDef *)(uart->_dev))->CR2 &= (((1<<12)+(1<<13)) ^ -1);
switch(stopbits)
{
case uarthalfstop:
((USART_TypeDef *)(uart->_dev))->CR2 |= (1<<12);
break;
case uartonestop:
break;
case uartonehalfstop:
((USART_TypeDef *)(uart->_dev))->CR2 |= (1<<12) + (1<<13);
break;
case uarttwostop:
((USART_TypeDef *)(uart->_dev))->CR2 |= (1<<13);
break;
default :
return 0;
break;
}
return 1;
}
int uartputc(uart_t* uart,char c)
{
((USART_TypeDef *)(uart->_dev))->DR = c;
while((((USART_TypeDef *)(uart->_dev))->SR & (1<<7))!=(1<<7));
return 1;
}
char uartgetc(uart_t* uart)
{
while(!(((USART_TypeDef *)(uart->_dev))->SR & (1<<5)));
return (char)((USART_TypeDef *)(uart->_dev))->DR;
}
int uartputs(uart_t* uart,char* s)
{
while (*s) uartputc(uart,*s++);
return 1;
}
int uartgets(uart_t* uart,char* s)
{
do
{
(*s) = uartgetc(uart);
}
while(*s++);
return 1;
}
int uartputnc(uart_t* uart,char* c,int n)
{
while(n)
{
uartputc(uart,c[n]);
n--;
}
return 1;
}
int uartgetnc(uart_t* uart,char* c,int n)
{
while(n)
{
c[n]=uartgetc(uart);
n--;
}
return 1;
}
int uartavailiabledata(uart_t* uart)
{
if(!(((USART_TypeDef *)(uart->_dev))->SR & (1<<5)))
return 0;
else
return 1;
}