Forum: Mikrocontroller und Digitale Elektronik SAM4S Xplained UART


von Leo K. (leodennis)


Lesenswert?

Hi,

ich habe einen SAM4s Xplained und will UART1 benutzen, kann aber kein 
Beispiel und sonst auch keine Hilfe finden. Mein eigener Versuch ist 
auch fehlgeschlagen, aber ich bin mir sicher, dass es nur an einer 
kleinigkeit liegt.

Hier ist mein Code:
conf_uart.h
1
#include "asf.h" //uart.h etc. included here
2
#include <sam4s_xplained.h>
3
4
#define UART_SERIAL_BAUDRATE        9600
5
#define UART_SERIAL_CHANNEL_MODE   UART_MR_CHMODE_AUTOMATIC //UART_MR_CHMODE_NORMAL
6
#define UART_SERIAL_MCK            240000000UL //CHIP_FREQ_CPU_MAX (tried both!)
7
#define UART_SERIAL_MODE         UART_MR_PAR_NO
8
9
void uart_custom_init(void);



conf_uart.c
1
#include "conf_uart.h"
2
3
uint8_t received_byte;
4
5
void uart_custom_init(void) {
6
    sysclk_init();
7
    
8
    const sam_uart_opt_t uart_console_settings = {
9
       UART_SERIAL_BAUDRATE,
10
       UART_SERIAL_CHANNEL_MODE,
11
       UART_SERIAL_MCK,
12
       UART_SERIAL_MODE
13
    };
14
    
15
    uart_init(UART1,&uart_console_settings);      //Init UART1
16
    
17
    uart_enable_rx(UART1);                     //Enable RX (receiving)
18
    uart_enable_tx(UART1);                     //Enable TX (transmitting)
19
    uart_enable(UART1);                     //Enable UART1
20
    
21
    uart_enable_interrupt(UART1,UART_IER_RXRDY);   //Interrupt reading ready
22
    NVIC_EnableIRQ(UART1_IRQn);
23
    
24
}
25
26
void UART1_Handler() {
27
   uint32_t dw_status = uart_get_status(UART1);
28
   
29
   if(dw_status & UART_SR_RXRDY) {
30
      uint8_t received_byte;
31
      uart_read(UART1, &received_byte);
32
      uart_write(UART1, received_byte);
33
   }
34
}

Ich habe verschiedene Dinge versucht, z.B. habe ich automatic echo 
eingestellt. Aber Ich empfange einfach nichts beim Computer oder beim 
Microprozessor...
(Mit nem AVR hab ichs schon geschaft, muss aber umsteigen...)

Hoffe ihr Könnt mir helfen!

Lg
leodennis

von Leo K. (leodennis)


Lesenswert?

Hi,

ich habs durch Hilfe von einem anderem forum geschafft
Here ist mein Code:

conf_uart.h
1
#include "asf.h" //uart.h etc. included here
2
3
#define UART_SERIAL_BAUDRATE        9600
4
#define UART_SERIAL_CHANNEL_MODE  UART_MR_CHMODE_NORMAL
5
#define UART_SERIAL_MODE      UART_MR_PAR_NO
6
7
/* =============== UART1 =============== */ //(UART0 is defined but not UART1)
8
#define PINS_UART1          (PIO_PB2A_URXD1 | PIO_PB3A_UTXD1)
9
#define PINS_UART1_FLAGS    (PIO_PERIPH_A | PIO_DEFAULT)
10
#define PINS_UART1_MASK     (PIO_PB2A_URXD1 | PIO_PB3A_UTXD1)
11
#define PINS_UART1_PIO      PIOB
12
#define PINS_UART1_ID       ID_PIOB
13
#define PINS_UART1_TYPE     PIO_PERIPH_A
14
#define PINS_UART1_ATTR     PIO_DEFAULT
15
16
void uart_custom_init(void);
17
void sendViaUart(uint8_t data);

conf_uart.cpp
1
#include "conf_uart.h"
2
3
uint8_t received_byte;
4
5
void uart_custom_init(void) {
6
   sysclk_init();
7
   
8
    // set the pins to use the uart peripheral
9
   pio_configure(PINS_UART1_PIO, PINS_UART1_TYPE, PINS_UART1_MASK, PINS_UART1_ATTR);
10
11
   //enable the uart peripherial clock
12
   pmc_enable_periph_clk(ID_UART1);
13
   
14
   const sam_uart_opt_t uart1_settings =
15
    { sysclk_get_cpu_hz(), UART_SERIAL_BAUDRATE, UART_SERIAL_MODE };
16
   
17
   uart_init(UART1,&uart1_settings);    //Init UART1
18
   uart_enable_rx(UART1);          //Enable RX (receiving)
19
   uart_enable_tx(UART1);          //Enable TX (transmitting)
20
   uart_enable(UART1);          //Enable UART1
21
   
22
   uart_enable_interrupt(UART1,UART_IER_RXRDY);  //Interrupt reading ready
23
   NVIC_EnableIRQ(UART1_IRQn);
24
   
25
}
26
27
void sendViaUart(uint8_t data) {
28
  while (!(UART1->UART_SR & UART_SR_TXRDY));
29
  uart_write(UART1, data);
30
}
31
32
void UART1_Handler() {
33
  uint32_t dw_status = uart_get_status(UART1);
34
  
35
  if(dw_status & UART_SR_RXRDY) {
36
    uint8_t received_byte;
37
    uart_read(UART1, &received_byte);
38
    sendViaUart(received_byte);
39
  }
40
}

lg
Leo

- closed -

von Leo K. (leodennis)


Lesenswert?

Edit (sonst kommts zu problemen!):

Das
1
uint8_t received_byte;

nach dem
1
#include "conf_uart.h"

muss weg!!!!!!!!!!!!!!!!!!!!!

1
#include "conf_uart.h"
2
uint8_t received_byte; //löschen!

und das kann auch weg weils im uart_init gemacht wird
1
uart_enable_rx(UART1);          //Enable RX (receiving)
2
uart_enable_tx(UART1);          //Enable TX (transmitting)
3
uart_enable(UART1);          //Enable UART1

von Nabil (Gast)


Lesenswert?

ich habe diese code für SAM4S Xplained pro geschrieben.... aber 
funktioniert nicht... habt ihr eine Idee

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.