Skip to content

Instantly share code, notes, and snippets.

@alg0trader
Last active December 14, 2015 03:39
Show Gist options
  • Save alg0trader/5022270 to your computer and use it in GitHub Desktop.
Save alg0trader/5022270 to your computer and use it in GitHub Desktop.
/*******************************************************************************
*
* Author: Austin Schaller
* Module: uart.c
* Description: Library functions that permit utilization of LPC17xx
* UART0 peripheral.
* Documentation: "UART - TLD"
*
*******************************************************************************/
#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif
#include <cr_section_macros.h>
#include <NXP/crp.h>
/*
* Variable to store CRP value in. Will be placed automatically
* by the linker when "Enable Code Read Protect selected.
* See crp.h header for more information.
*/
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;
#include <stdio.h>
#include "uart.h"
#include "timer.h"
int main(void)
{
set_u0pclk(UART0_PCLK); // UART_PCLK = 120 MHz
init_uart0(BAUD_RATE); // Baud Rate of 57600
printf("Hello World!");
return 0;
}
/*******************************************************************************
*
* Author: Austin Schaller
* Module: uart.c
* Description: Library functions that permit utilization of LPC17xx
* UART0 peripheral.
* Documentation: "UART - TLD"
*
*******************************************************************************/
/** Include Files *************************************************************/
#include "LPC17xx.h"
#include "system_LPC17xx.h"
#include "uart.h"
/** Defines *******************************************************************/
#define IER_RBR 0x01
#define IER_THRE 0x02
#define IER_RLS 0x04
#define IIR_PEND 0x01
#define IIR_RLS 0x03
#define IIR_RDA 0x02
#define IIR_CTI 0x06
#define IIR_THRE 0x01
#define LSR_RDR 0x01
#define LSR_OE 0x02
#define LSR_PE 0x04
#define LSR_FE 0x08
#define LSR_BI 0x10
#define LSR_THRE 0x20
#define LSR_TEMT 0x40
#define LSR_RXFE 0x80
static uint32_t get_u0pclk(void)
{
uint32_t u0pclk_div = (LPC_SC->PCLKSEL0 >> 8) & 0x03;
switch(u0pclk_div)
{
case 0x00:
return SystemCoreClock/4.0;
case 0x01:
return SystemCoreClock;
case 0x02:
return SystemCoreClock/2.0;
case 0x03:
return SystemCoreClock/8.0;
}
return (SystemCoreClock/u0pclk_div);
}
static void set_u0pclk(uint8_t bit_val)
{
uint32_t regVal;
regVal = LPC_SC->PCLKSEL0;
regVal |= ((bit_val << 6) & 0xFF); // UART0
}
void UART0_SendChar(char c)
{
while((LPC_UART0->LSR & LSR_THRE) == 0); // Wait until TX is empty
LPC_UART0->THR = c;
}
char UART0_GetChar(void)
{
char c;
while((LPC_UART0->LSR & LSR_RDR) == 0); // Nothing received so just block
c = LPC_UART0->RBR; // Read Rx buffer register
return c;
}
void UART0_print(char *p_string)
{
int i = 0;
while(p_string[i] != '\0')
{
UART0_SendChar(p_string[i]);
i++;
}
}
void init_uart0(uint32_t baud_rate)
{
uint64_t Fdiv;
LPC_SC->PCONP |= (1 << 3); // Power UART0
set_u0pclk(UART0_PCLK);
// Set PINSEL0 so that P0.2 = TXD0, P0.3 = RXD0
LPC_PINCON->PINSEL0 &= ~0xF0;
LPC_PINCON->PINSEL0 |= ((1 << 4) | (1 << 6));
LPC_UART0->LCR = 0x83; // 8 bits, no Parity, 1 Stop bit, DLAB=1
Fdiv = (get_u0pclk()/16)/baud_rate;
LPC_UART0->DLM = Fdiv / 256;
LPC_UART0->DLL = Fdiv % 256;
LPC_UART0->LCR = 0x03; // 8 bits, no Parity, 1 Stop bit DLAB = 0
LPC_UART0->FCR = 0x07; // Enable and reset TX and RX FIFO
}
/*******************************************************************************
*
* Author: Austin Schaller
* Module: uart.h
* Description: UART0 library for the LPC17xx ARM microprocessor.
* Documentation: "UART - TLD"
*
*******************************************************************************/
#ifndef UART_H
#define UART_H
/** Defines *******************************************************************/
#define UART0_PCLK 0x01 // Equal to CCLK
#define BAUD_RATE 57600
/** Functions *****************************************************************/
static void set_u0pclk(uint8_t bit_val);
void UART0_SendChar(char c);
char UART0_GetChar(void);
void UART0_print(char *p_string);
void init_uart0(uint32_t baud_rate);
#endif // UART_H
/*******************************************************************************
*
* Author: Austin Schaller
* Module: uart_io.c
* Description: Configures UART0 peripheral to utilize "printf".
* Documentation: "UART - TLD"
*
*******************************************************************************/
/** Include Files *************************************************************/
#include <stdio.h>
#include <stdint.h>
#include "uart.h"
#if (__REDLIB_INTERFACE_VERSION__ >= 20000)
// We are using new Redlib_v2 semihosting interface
#define WRITEFUNC __sys_write
#else
// We are using original Redlib semihosting interface
#define WRITEFUNC __write
#endif
int WRITEFUNC(int iFileHandle, char *p_string, int iLen)
{
uint32_t i;
for (i = 0; i < iLen; i++)
UART0_SendChar(p_string[i]); // print each character
return iLen;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment