cembeddedatsam3x

Configuring ATSAM3x8e port to output


I am having an issue configuring my SAM3x8e port to output, when I check the pin with a multimeter I get 1.5V so the pin 0 of Port A just isn't turning on. I believe I configured the port pins to correctly out as outputs but I do not know, I don't have a debugger on me to look at what's happening internally.

#include "sam.h"

uint32_t right_tick = 0;
uint32_t left_tick = 0;
uint32_t LED_status = 1;


void InitializeSystemTimer(void)
{
    const uint32_t tickcount = 1000000;
    // set value countdown restarts to
    SysTick->LOAD = tickcount;
    // set interrupt priority
    NVIC_SetPriority(SysTick_IRQn, 3);
    // restart timer
    SysTick->VAL = 0;
    // set system tick counter ClockSource/8, creates system tick exception when hits 0, enable system tick count,
    SysTick->CTRL = SysTick_CTRL_TICKINT_Msk|SysTick_CTRL_ENABLE_Msk;
}

void InitializeIOPorts(void)
{
    // enable Peripheral Clock on Port A
    PMC->PMC_PCER0 = ID_PIOA;
    
    // configure output pins
    
    // claim Port A control of pin 0, 1, 2
    PIOA->PIO_PER = PIO_PER_P0|PIO_PER_P1|PIO_PER_P2;
    // enable Port A output on pins
    PIOA->PIO_WPMR = PIO_WPMR_WPKEY_Msk;
    // set Port A pin 0, 1, 2 to output
    PIOA->PIO_PER = PIO_PER_P0|PIO_PER_P1|PIO_PER_P2;
    // set Port A pin 0, 1, 2 to be accessed directly by setting Output Write Enable Register
    PIOA->PIO_OWER = PIO_PER_P0|PIO_PER_P1|PIO_PER_P2;
    // set Port A pin 0, 1, 2 to high
    PIOA->PIO_ODSR = PIO_PER_P0|PIO_PER_P1|PIO_PER_P2;

    // configure input pins 
    
    // claim Port A control of pin 3, 4
    PIOA->PIO_PER = PIO_PER_P3|PIO_PER_P4;
    // set pull up resistors on pin 3, 4
    PIOA->PIO_PUER = PIO_PER_P3|PIO_PER_P4;
    // set interrupt on pin 3, 4
    PIOA->PIO_IER = PIO_PER_P3|PIO_PER_P4;
    // enable input change interrupt on Pin 3, 4 by setting mask
    PIOA->PIO_IMR = PIO_PER_P3|PIO_PER_P4;
    
    // set priority and enable interrupt for port A
    NVIC_SetPriority(PIOA_IRQn, 3);
    NVIC_EnableIRQ(PIOA_IRQn);
    
}

void InitializeUart(void)
{
    PMC->PMC_PCER0 = ID_UART;
    // baud rate is 84Mhz/(16*45)
    UART->UART_BRGR = uint32_t(45);
    UART->UART_CR = UART_CR_TXEN|UART_CR_RXEN;
    
    NVIC_SetPriority(UART_IRQn,2);
    NVIC_EnableIRQ(UART_IRQn);
}


void SysTick_Handler(void)
{
    if (LED_status == 0)
    {
        PIOA->PIO_SODR = PIO_PER_P0;
        LED_status = 1;
    }
    else
    {
        PIOA->PIO_CODR = PIO_PER_P0;
        LED_status = 0;
    }
    
}

void PIOA_Handler(void)
{
    uint32_t PORTA_interrupt_status = 0;
    PORTA_interrupt_status = PIOA->PIO_ISR;
    
    if ((PORTA_interrupt_status&PIO_PER_P3) == 1)
    {
        left_tick = left_tick+1;  
    }
    if ((PORTA_interrupt_status&PIO_PER_P4) == 1)
    {
        right_tick = right_tick + 1;
    }
}


void UART_Handler(void)
{
    
}


int main(void)
{
    /* Initialize the SAM system */
    SystemInit();
    InitializeSystemTimer();
    InitializeIOPorts();

    /* Replace with your application code */
    while (1) 
    {
    }
}

Solution

  • You should double check an example given in the Atmel SAM3X8E Datasheet in the section 31.6 I/O Lines Programming Example. Have a look at example provided:

    and the following configuration:

    Register Value to be Written
    PIO_PER  0x0000FFFF
    PIO_PDR  0xFFFF0000
    PIO_OER  0x000000FF
    PIO_ODR  0xFFFFFF00
    PIO_IFER 0x00000F00
    PIO_IFDR 0xFFFFF0FF
    PIO_SODR 0x00000000
    PIO_CODR 0x0FFFFFFF
    PIO_IER  0x0F000F00
    PIO_IDR  0xF0FFF0FF
    PIO_MDER 0x0000000F
    PIO_MDDR 0xFFFFFFF0
    PIO_PUDR 0xF0F000F0
    PIO_PUER 0x0F0FFF0F
    PIO_ABSR 0x00F00000
    PIO_OWER 0x0000000F
    PIO_OWDR 0x0FFFFFF0
    

    We need the second character from the end (as far as pins 4 to 7 are used in this example) to determine wheather the register should be programmed or not. For instance, "PIO_PER 0x0000FFFF" means the register should be programmed. And, in contrast, "PIO_PDR 0xFFFF0000" means the register should be left untouched. Thus, the following configuration could is obtained:

    PIOA->PIO_PER  = PIO_PER_P0 | PIO_PER_P1 | PIO_PER_P2;
    PIOA->PIO_OER  = PIO_PER_P0 | PIO_PER_P1 | PIO_PER_P2;
    PIOA->PIO_IFDR = PIO_PER_P0 | PIO_PER_P1 | PIO_PER_P2;
    PIOA->PIO_CODR = PIO_PER_P0 | PIO_PER_P1 | PIO_PER_P2;
    PIOA->PIO_IDR  = PIO_PER_P0 | PIO_PER_P1 | PIO_PER_P2;
    PIOA->PIO_MDDR = PIO_PER_P0 | PIO_PER_P1 | PIO_PER_P2;
    PIOA->PIO_PUDR = PIO_PER_P0 | PIO_PER_P1 | PIO_PER_P2;
    PIOA->PIO_OWDR = PIO_PER_P0 | PIO_PER_P1 | PIO_PER_P2;
    

    I have no SAM3X8E device to check this configuration, but the above considerations may help to find a solution to your problem.