Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: One Wire library for SAMD21

  1. #1

    One Wire library for SAMD21

    Hello, I'm trying to make to use https://github.com/PaulStoffregen/OneWire this library with ATSAMD21G18A, I found the following definitions inside the OneWire_direct_gpio.h file.
    Code:
    #elif defined(__SAMD21G18A__)
    #define PIN_TO_BASEREG(pin)             portModeRegister(digitalPinToPort(pin))
    #define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
    #define IO_REG_TYPE uint32_t
    #define IO_REG_BASE_ATTR
    #define IO_REG_MASK_ATTR
    #define DIRECT_READ(base, mask)         (((*((base)+8)) & (mask)) ? 1 : 0)
    #define DIRECT_MODE_INPUT(base, mask)   ((*((base)+1)) = (mask))
    #define DIRECT_MODE_OUTPUT(base, mask)  ((*((base)+2)) = (mask))
    #define DIRECT_WRITE_LOW(base, mask)    ((*((base)+5)) = (mask))
    #define DIRECT_WRITE_HIGH(base, mask)   ((*((base)+6)) = (mask))
    However I can't find what portModeRegister(digitalPinToPort(pin)) and (digitalPinToBitMask(pin)) are defined as, any tips on where to look and how to use One Wire with samd21?

  2. #2
    Member
    Join Date
    Aug 2018
    Location
    Brisbane, Australia
    Posts
    77
    look in pins_arduino.h for the definitions of the macros

  3. #3
    Thanks I found them but the code is still not working. I changed this original code
    Code:
    void OneWire::begin(uint8_t pin)
    {
    	pinMode(pin, INPUT);
    	bitmask = PIN_TO_BITMASK(pin);
    	baseReg = PIN_TO_BASEREG(pin);
    #if ONEWIRE_SEARCH
    	reset_search();
    #endif
    }
    to
    Code:
    #define digitalPinToPort(P)      (&(PORT->Group[0]))
    #define digitalPinToBitMask(P)   (1 << 8)
    //#define analogInPinToBit(P)    ()
    #define portOutputRegister(port) (&(port->OUT.reg))
    #define portInputRegister(port)  (&(port->IN.reg))
    #define portModeRegister(port)   (&(port->DIR.reg))
    
    #define PIN_TO_BASEREG(pin)             portModeRegister(digitalPinToPort(pin))
    #define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
    
    
    #define IO_REG_TYPE uint32_t
    #define IO_REG_BASE_ATTR
    #define IO_REG_MASK_ATTR
    #define DIRECT_READ(base, mask)         (((*((base)+8)) & (mask)) ? 1 : 0)
    #define DIRECT_MODE_INPUT(base, mask)   ((*((base)+1)) = (mask))
    #define DIRECT_MODE_OUTPUT(base, mask)  ((*((base)+2)) = (mask))
    #define DIRECT_WRITE_LOW(base, mask)    ((*((base)+5)) = (mask))
    #define DIRECT_WRITE_HIGH(base, mask)   ((*((base)+6)) = (mask))
    #define pgm_read_byte(addr) (*(const uint8_t *)(addr))
    
    
    void OneWire::begin(uint8_t pin)
    {
    	PORT->Group[0].DIRCLR.reg = (1 << pin);									// set pin as input
    	bitmask = PIN_TO_BITMASK(pin);
    	baseReg = PIN_TO_BASEREG(pin);
    }
    So I hardcoded #define digitalPinToPort(P) (&(PORT->Group[0])) and #define digitalPinToBitMask(P) (1 << 8) to pin 8 since that's the data pin I am using, I'll try to make it more modular however I just want to get it working first.
    Code:
    #define digitalPinToPort(P)      (&(PORT->Group[g_APinDescription[P].ulPort]))
    #define digitalPinToBitMask(P)   (1 << g_APinDescription[P].ulPin)
    this were the original definitions. Did I make any mistakes?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •