Forum: Mikrocontroller und Digitale Elektronik UART -Code für ARM7 (LPC vom Philips)


von Jan (Gast)


Lesenswert?

HI
ich muss jetzt einen UART bei einem ARM7 (LPC von Philips ´)
programmieren und bevor ich das RAd neu erfinde habe ich etwas im Netz
nach Beispiel Code gesucht den cih verwenden kann.
Da ich aber nichts gefunden habe meine Frage ob das hier schon mal
jemand gemacht hat und den COde hier posten könnte?
Oder aber was auch hilfreich ist einem guten Tip oder eine Anleitung.
Gruß
Jan

von Jan (Gast)


Lesenswert?

Hab doch noch was gefunden bei Hitex.co.uk:
lpc-ARM-book_srn.pdf
dadran werde ich mich jetzt erstmal orientieren

von Ppp M. (sanic)


Lesenswert?

Erstmal:

http://www.mikrocontroller.net/articles/ARM-elf-GCC-Tutorial#UART

Dann noch mein eigener Code (wird noch verbessert):
uart.c:
1
#include <arch/philips/lpc2114.h>
2
#include "config.h" // Config-File with System Defines
3
#include <string.h>
4
#include <stdlib.h>
5
#include "uart.h"
6
7
char uart0_rx_buffer;
8
char uart0_buf_len=0;
9
10
void uart_init(int baud, char num){
11
  unsigned int UART_DIVISOR=0;
12
  if(num==0){
13
    UART0_IER = 0x00;             // disable all interrupts
14
    UART0_IIR = 0x00;             // clear interrupt ID register
15
    UART0_LSR = 0x00;             // clear line status register
16
    //Setting the UART Divisor Latch
17
    UART0_LCR = (1<<7);
18
    UART_DIVISOR = (((FOSC*PLL_M/VPBDIV_VAL) / ((baud) * 16.0)) + 0.5);
19
//Calculate UART Divisor Latch
20
    UART0_DLL = (char)UART_DIVISOR; // Setting lower Byte of the Latch
21
Divisor
22
    UART0_DLM = (char)(UART_DIVISOR >> 8); // Setting upper Byte of the
23
Latch Divisor
24
    
25
    UART0_LCR = (3<<0); // 8bit Char Length
26
    UART0_FCR = (1<<0) | (2<<6); // FIFO Enable (Bit0 = 1) + Trigger
27
level 2 = 8 Characters (at Bit6 = 2 )
28
  
29
    UART0_IER = (1<<0); // Receive Data Available Interrupt enablen
30
  }else{
31
    UART1_IER = 0x00;             // disable all interrupts
32
    UART1_IIR = 0x00;             // clear interrupt ID register
33
    UART1_LSR = 0x00;             // clear line status register
34
    //Setting the UART Divisor Latch
35
    UART1_LCR = (1<<7);
36
    UART_DIVISOR = (((FOSC*PLL_M/VPBDIV_VAL) / ((baud) * 16.0)) + 0.5);
37
//Calculate UART Divisor Latch
38
    UART1_DLL = (char)UART_DIVISOR; // Setting lower Byte of the Latch
39
Divisor
40
    UART1_DLM = (char)(UART_DIVISOR >> 8); // Setting upper Byte of the
41
Latch Divisor
42
    
43
    UART1_LCR = (3<<0); // 8bit Char Length
44
    UART1_FCR = (1<<0) | (2<<6); // FIFO Enable (Bit0 = 1) + Trigger
45
level 2 = 8 Characters (at Bit6 = 2 )
46
  
47
    UART1_IER = (1<<0); // Receive Data Available Interrupt enablen
48
  }
49
}
50
void uart_putc(char c,char num){
51
  char ULSR_THRE;
52
  ULSR_THRE=(1 << 5);
53
  while (!(UART0_LSR & ULSR_THRE))          // wait for TX buffer to
54
empty
55
    continue;                           // also either WDOG() or
56
swap()
57
  
58
  UART0_THR = c;  // put char to Transmit Holding Register
59
}
60
61
void uart_puts(char *s,char num){
62
  while(*s){
63
    uart_putc(*s,num);
64
  s++;
65
  }
66
}
67
68
int uart_getcW(int num){
69
  char ULSR_RDR;
70
    ULSR_RDR=(1 << 0);
71
  while ( !(UART0_LSR & ULSR_RDR) ); // wait for character 
72
  return UART0_RBR;                // return character
73
}

uart.h
1
#ifndef UART_H_
2
#define UART_H_
3
#include <arch/philips/lpc2114.h>
4
#include "config.h" // Config-File with System Defines
5
#include <stdlib.h>
6
7
#define UIIR_MASK 7; // 00000111
8
9
char uart0_rx_buffer;
10
char uart0_buf_len;
11
12
/* Init UART */
13
void uart_init(int baud, char num);
14
15
/* Put Char to UART */
16
void uart_putc(char c,char num);
17
18
/* Put String to UART */
19
void uart_puts(char *s,char num);
20
21
/* Get Character from UART(num) with Wait
22
The function waits for an appearing character in the recieve register
23
and will continue then
24
*/
25
int uart_getcW(int num);
26
#endif

Es sind einige schwachsinnige Includes drin und außerdem gibt´s diese
"num" Variable.
Damit wollte ich später mehrere Uarts mit einer Bibliothek abdecken.
Ist aber erstmal ein Anfang für dich.

Grüße,
Patrick

von jan (Gast)


Lesenswert?

Herzlichen Dank für das Beispiel
Jan

von Lupin (Gast)


Lesenswert?

in deinem code fehlt aber noch die Aktivierung im pin connect block
(PINSEL0 register) :P

von Ppp M. (sanic)


Lesenswert?

Stimmt, das mach ich im Main Part :)

Grüße

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.