0

UART on SAM4SxPLAINED

Hello,

I have a program which (almost) works !

The characters are transmitted but they are not still the ones hoped. Who can correct?

Thank you for everything.

Greetings.

Voici le code :

#include "sam.h"

// Fréquence d'horloge
#define MCLK CHIP_FREQ_MAINCK_RC_4MHZ

// Configure UART
#define BAUD    115200
#define PARITY  UART_MR_PAR_NO
//#define PARITY UART_MR_PAR_EVEN

// Définitions pour PA9 comme RX_UART0 et PA10 comme TX_UART0 (Périphérique A).
#define RXTX_PIO PIOA
#define RX_PIN PIO_PA9
#define TX_PIN PIO_PA10
#define UART UART0
#define UART_PID ID_UART0
#define RXTX_IRQn PIOA_IRQn
#define UART_IRQn UART0_IRQn

//prototype pour les fonctions. On ne veut pas faire un .h pour ça !
void hardware_init( void ) ;

void hardware_init( void )
{
    //pour utiliser RX_UART et TX_UART.
    PMC->PMC_PCER0 = (1 << UART_PID) ;
    RXTX_PIO->PIO_IDR |= RX_PIN | TX_PIN ;
    // Sélectionne le périphérique A
    RXTX_PIO->PIO_ABCDSR[0] &= ~(RX_PIN | TX_PIN) ;
    RXTX_PIO->PIO_ABCDSR[1] &= ~(RX_PIN | TX_PIN) ;
    RXTX_PIO->PIO_PDR       = (RX_PIN | TX_PIN) ;
    //Configure UART
    UART->UART_BRGR= MCLK / (BAUD * 16) ;
    UART->UART_CR = UART_CR_TXEN | UART_CR_RXEN ;
    UART->UART_IER = UART_IER_RXRDY ;
    // Active les interruptions sur UART;
    NVIC_EnableIRQ( UART_IRQn ) ;
    // Configure Systick pour un déclenchement chaque milliseconde
    SysTick_Config( SystemCoreClock/1000UL ) ;
    NVIC_EnableIRQ( SysTick_IRQn ) ;
}

// Variable globale pour compter les tics
static uint32_t ul_tickcount=0 ;

// Variable globale pour UART_Handler
static int32_t tr ;
static uint32_t j = 0 ;

static char chaine[] = "Bonjour UART0 " ;
static uint8_t nb_car = 14 ; //nombre de caractères de chaine

void SysTick_Handler( void )
{
    ul_tickcount++ ;
    // envoie un caractère toutes les secondes (ie 1000ms)
    if ( ul_tickcount == 1000)
    {
        if (j == nb_car)
        {
            j=0;
        }
        while(!(UART->UART_SR & UART_SR_TXRDY)) ;
        UART->UART_THR = chaine[j] ;
        j++ ;
        ul_tickcount = 0 ;
    }
}

void UART0_Handler( void )
{
     if (UART->UART_SR & UART_SR_RXRDY)
     {
          tr = UART->UART_RHR ;
          while(!(UART->UART_SR & UART_SR_TXRDY));
          UART->UART_THR = tr - 1 ;
     }
}

int main (void)
{
     hardware_init() ;
     while ( 1 )
     {
     }
}
Joss
  • 1
  • Did you sync the baud rates correctly with the output terminal? – πάντα ῥεῖ Dec 23 '14 at 09:43
  • yes. I use "windows terminal" in Atmel Studio 6.2 or Tera Term with the same result. – Joss Dec 23 '14 at 19:03
  • You define `BAUD` and `PARITY`, but I can't spot where you're actually using it to init the UART. – πάντα ῥεῖ Dec 23 '14 at 19:05
  • UART->UART_BRGR= MCLK / (BAUD * 16) ; and, if I add UART->UART_MR = PARITY ; it goes even more badly – Joss Dec 23 '14 at 19:32
  • Boîjoõr UAÒT0 Bonjoõr UAÒT0 Boîjoõr UAÒÔ0 Boîjour UART0 Boîjour UAÒT0 Bonjour UART0 Boîjour UART0 is a sample of output. One good for many false. – Joss Dec 23 '14 at 19:44
  • _@Joss_ I don't think this comment is useful. Please [improve your question](http://stackoverflow.com/posts/27617738/edit) with additional information. – πάντα ῥεῖ Dec 23 '14 at 19:47
  • The maximal speed of transmission is 19200 bauds. If I try 38400 or more, it does not work. I do not understand why and shall wish an explanation. Thank you. – Joss Dec 24 '14 at 09:03

0 Answers0