3

I am trying to use D0/D1 as normal IO pins but DigitalWrite does not work as on an Arduino UNO.

I know the SAMD21G has SERCOM but I can't seem to find an example of not using D0/D1 as an UART port.

EDIT ** Code I have tried:

#define MYMASK 0x00000C00
void setup() {
  REG_PORT_DIRSET0 = MYMASK; // Direction set to OUTPUT
  REG_PORT_OUTSET0 = MYMASK; // set state of pin(s) to TRUE (HIGH)
  delay(1000);
  REG_PORT_OUTCLR0 = MYMASK; // set state of pin(s) to FALSE (LOW)  
}

void loop() {
}
PhillyNJ
  • 1,128
  • 3
  • 10
  • 20
Bertus Kruger
  • 223
  • 1
  • 8

1 Answers1

2

You can do this by simply calling the right registers:

Example 1, using a MASK

D0/D1 are mapped to to PA10 & PA11 on the M0

/* The following ports where selected
    PORT_PA11
    PORT_PA10
    */  
#define MYMASK 0x00000C00

int main (void)
{
    system_init();

    REG_PORT_DIRSET0 = MYMASK; // Direction set to OUTPUT
    REG_PORT_OUTSET0 = MYMASK; // set state of pin(s) to TRUE (HIGH)
    //REG_PORT_OUTCLR0 = MYMASK; // set state of pin(s) to FALSE (LOW)

    while(1){       

    }   
}

Here is another simple example:

/* The following ports where selected
    PORT_PA11
    PORT_PA10
    */  
#define MYMASK 0x00000C00
#define LED1 PORT_PA10
#define LED2 PORT_PA11
int main (void)
{
    system_init();

    REG_PORT_DIRSET0 = LED1 | LED2; // Direction set to OUTPUT
    REG_PORT_OUTSET0 = LED1 | LED2; // set state of pin(s) to TRUE (HIGH)
    //REG_PORT_OUTCLR0 = LED1 | LED2; // set state of pin(s) to FALSE (LOW)
    while(1){   

    }   
}

In the Arduino IDE you can do something like:

#define MYMASK 0x00000C00
void setup() {
  REG_PORT_DIRSET0 = MYMASK; // Direction set to OUTPUT 
}

void loop() {
   REG_PORT_OUTSET0 = MYMASK; // set state of pin(s) to TRUE (HIGH)
   delay(1000);
   REG_PORT_OUTCLR0 = MYMASK; // set state of pin(s) to FALSE (LOW)  
   delay(1000);
}
PhillyNJ
  • 1,128
  • 3
  • 10
  • 20
  • Thanks for the quick reply... I tried the the above trough Arduino but without any luck. I wonder if Arduino overwrites the registers in the background. – Bertus Kruger Jun 26 '17 at 07:19
  • Can't tell unless you post your code. – PhillyNJ Jun 26 '17 at 10:42
  • 1
    Have added the Arduino Code... – Bertus Kruger Jun 27 '17 at 08:58
  • @BertusKruger - I just tried the code on my Arduino M0 which has D0/D1 mapped to PA11 & PA12. You have set the ports to low before the loop. See my revised code. – PhillyNJ Jun 27 '17 at 11:57
  • edit - D0/D1 are mapped to PA10 & PA11 – PhillyNJ Jun 27 '17 at 21:06
  • Thanks @PhillyNJ . I can confirm that that worked for me. How did you derive the mask and where can I read more about the registers? I would like to do the same with the SPI and I2C pins... – Bertus Kruger Jun 30 '17 at 18:46
  • @BertusKruger HTH - please don't forget to mark as answered :) - You can read all about the GPIO registers in the Datasheet or my tutorial which I posted here https://community.atmel.com/forum/samd21samr21-xplained-pro-gpio-tutorial for peer review. Let me know how it goes. – PhillyNJ Jun 30 '17 at 19:00
  • Thanks. I figured out the mask but could not see any mention of REG_PORT_DIRSET0 in the datasheet. I now noticed the DS talks about DIRSET. Still need to find the link... I am building a SAMD21 board from scratch and is now busy on a test jig testing each pin... :) – Bertus Kruger Jun 30 '17 at 19:09
  • Thanks for the tutorial link. Haha I found my answer... :) https://github.com/arduino/ArduinoCore-samd/blob/master/bootloaders/sofia/Bootloader_D21_Sofia_V2.1/src/ASF/sam0/utils/cmsis/samd21/include/instance/port.h – Bertus Kruger Jun 30 '17 at 19:11