Author Topic: SAMD10 digital read  (Read 969 times)

0 Members and 1 Guest are viewing this topic.

Offline n4alpacaTopic starter

  • Newbie
  • Posts: 9
  • Country: us
SAMD10 digital read
« on: November 10, 2017, 02:34:02 am »
Hello, I've been struggling with getting digital read to work.

This is how the code flows
Pin initialization:
Code: [Select]
void gpio_init_pin(struct PIN p){

  uint32_t WRCONFIG = 0; // this MUST be a 32 bit number or writing to this address will have no effect
 
  //SET PIN MASK
  if(p.pinNum > 15){
    WRCONFIG += (1 << PORT_WRCONFIG_HWSEL_Pos ); // selects HWSEL for upper 16 pins
  }
    WRCONFIG += (1 << ( p.pinNum % 16 )); // base pin number subtracts 16 to fit into the pinmask[0,..,15] register
  // END SET PIN MASK

 
  if(p.mux == NOMUX){ // if there's no pinmuxing then its a standard input or output

    // set output. Pins are input by default, but writing 0 will make sure the pin an input
    PORT->Group->DIRSET.reg = (p.dir << ( p.pinNum ));
    // end set output


    // set input
    WRCONFIG += (p.in << PORT_WRCONFIG_INEN_Pos );

      // possibly power hungry method of read, otherwise we require two clock cycles to read
      // note: all pins in the byte (group of 8) will be sampled continuously.

    PORT->Group->CTRL.reg = (0xFF<<(8*(p.pinNum/8)));

    // end set input


    //set pullup/down enable
    if(p.pull != NO_PULL){
      WRCONFIG += (p.pull << PORT_WRCONFIG_PULLEN_Pos ); // if it's an output, this is a "don't care condition
      PORT->Group->OUTSET.reg = (p.pull << p.pinNum); // determines pull up or down. Normally, this is used to set the level. Don't care condition on output
    }
    //end set pullup/down enable

  }
  else{
   
    WRCONFIG += (1 << PORT_WRCONFIG_WRPMUX_Pos);
    WRCONFIG += (1 << PORT_WRCONFIG_PMUXEN_Pos);
    WRCONFIG += (1 << PORT_WRCONFIG_WRPINCFG_Pos);
   
    WRCONFIG += (p.mux << PORT_WRCONFIG_PMUX_Pos );

  }

Now, we can try to read.

Code: [Select]
int gpio_digital_read(struct PIN p){

  return (PORT->Group->IN.reg & (1<<(p.pinNum)));

}

I'm not getting any results when the pin is high or low, what have I done wrong?

An unrelated question: When writing to a register requires synchronizing clocks, do I write to the register and then wait for the sync to complete or is the sync something I must instigate?
this is what I thought I could do provided that it's the first option

Code: [Select]
  while(TC_STATUS_SYNCBUSY & (0x1u<<TC_STATUS_SYNCBUSY_Pos))// wait for sync to finish
    ;

Additionally, if anyone has a working library of functions and wants to share, that would be appreciated.
« Last Edit: November 10, 2017, 03:39:14 am by n4alpaca »
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11725
  • Country: us
    • Personal site
Re: SAMD10 digital read
« Reply #1 on: November 10, 2017, 04:17:31 am »
Use this: https://github.com/ataradov/mcu-starter-projects/blob/master/samd10/hal_gpio.h , it works, it is small, it is tested.

Don't worry about the synchronization for now.
Alex
 

Offline n4alpacaTopic starter

  • Newbie
  • Posts: 9
  • Country: us
Re: SAMD10 digital read
« Reply #2 on: November 10, 2017, 05:17:56 am »
Thanks again Alex! you really seem to fielding all of the questions around here. I was asking about synchronizing clocks so that I could write idle delays and work with interrupts (timed and un-timed) through RTC and evsys. Is synchronizing not needed for those?
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11725
  • Country: us
    • Personal site
Re: SAMD10 digital read
« Reply #3 on: November 10, 2017, 05:22:02 am »
Is synchronizing not needed for those?
Synchronizations is never required for writes. Your second write to the same register will stall the bus for a few cycles. This may matter for some applications, but if you are just learning, then it is probably not important.

Some registers need read synchronizations to get correct value.

But the general approach - don't think about the synchronization until you run into problems. This is not likely to happens for a very long time.
Alex
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf