Skip to content

Commit 80c15d8

Browse files
committed
Add support for Silicon Labs hardware
1 parent 800f26f commit 80c15d8

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

util/OneWire_direct_gpio.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,89 @@ void directWriteHigh(IO_REG_TYPE mask)
501501
#define DIRECT_MODE_OUTPUT(base, pin) pinMode(pin,OUTPUT)
502502
#warning "OneWire. RP2040 in Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite."
503503

504+
#elif defined(ARDUINO_SILABS)
505+
506+
#define IO_REG_TYPE uint32_t
507+
#define IO_REG_BASE_ATTR
508+
#define IO_REG_MASK_ATTR
509+
510+
uint8_t pin_to_silabs_pin(uint8_t pin) {
511+
// The provided pin is PinName - remove the offset from it
512+
if(pin >= PIN_NAME_MIN && pin < PIN_NAME_MAX) {
513+
return (pin - PIN_NAME_MIN);
514+
}
515+
// The provided pin is an Arduino pin - convert it to PinName then remove the offset
516+
uint8_t pin_mapped = (uint8_t)pinToPinName(pin);
517+
return (pin_mapped - PIN_NAME_MIN);
518+
}
519+
520+
static inline __attribute__((always_inline))
521+
uint32_t* pin_to_basereg(uint8_t pin) {
522+
pin = pin_to_silabs_pin(pin);
523+
// Place the pin number into the 'basereg' pointer as a value
524+
return (uint32_t*)(uint32_t)pin;
525+
}
526+
527+
static inline __attribute__((always_inline))
528+
uint32_t pin_to_bitmask(uint8_t pin) {
529+
pin = pin_to_silabs_pin(pin);
530+
return 1 << (pin % 16);
531+
}
532+
533+
static inline __attribute__((always_inline))
534+
uint32_t direct_read(volatile uint32_t* base, uint32_t mask) {
535+
(void)mask;
536+
const uint32_t port = (uint32_t)base / 16;
537+
const uint32_t pin = (uint32_t)base % 16;
538+
return BUS_RegBitRead(&GPIO->P[port].DIN, pin);
539+
}
540+
541+
static inline __attribute__((always_inline))
542+
void direct_write_low(volatile uint32_t* base, uint32_t mask) {
543+
const uint32_t port = (uint32_t)base / 16;
544+
BUS_RegMaskedClear(&GPIO->P[port].DOUT, mask);
545+
}
546+
547+
static inline __attribute__((always_inline))
548+
void direct_write_high(volatile uint32_t* base, uint32_t mask) {
549+
const uint32_t port = (uint32_t)base / 16;
550+
BUS_RegMaskedSet(&GPIO->P[port].DOUT, mask);
551+
}
552+
553+
static inline __attribute__((always_inline))
554+
void direct_mode_input(volatile uint32_t* base, uint32_t mask) {
555+
(void)mask;
556+
const uint32_t port = (uint32_t)base / 16;
557+
const uint32_t pin = (uint32_t)base % 16;
558+
// The MODEL register controls pins 0-7 and MODEH controls pins 8-15
559+
if (pin < 8) {
560+
BUS_RegMaskedWrite(&(GPIO->P[port].MODEL), 0xFu << (pin * 4), (uint32_t)gpioModeInput << (pin * 4));
561+
} else {
562+
BUS_RegMaskedWrite(&(GPIO->P[port].MODEH), 0xFu << ((pin - 8) * 4), (uint32_t)gpioModeInput << ((pin - 8) * 4));
563+
}
564+
}
565+
566+
static inline __attribute__((always_inline))
567+
void direct_mode_output(volatile uint32_t* base, uint32_t mask) {
568+
(void)mask;
569+
const uint32_t port = (uint32_t)base / 16;
570+
const uint32_t pin = (uint32_t)base % 16;
571+
// The MODEL register controls pins 0-7 and MODEH controls pins 8-15
572+
if (pin < 8) {
573+
BUS_RegMaskedWrite(&(GPIO->P[port].MODEL), 0xFu << (pin * 4), (uint32_t)gpioModePushPull << (pin * 4));
574+
} else {
575+
BUS_RegMaskedWrite(&(GPIO->P[port].MODEH), 0xFu << ((pin - 8) * 4), (uint32_t)gpioModePushPull << ((pin - 8) * 4));
576+
}
577+
}
578+
579+
#define PIN_TO_BASEREG(pin) pin_to_basereg(pin)
580+
#define PIN_TO_BITMASK(pin) pin_to_bitmask(pin)
581+
#define DIRECT_READ(base, mask) direct_read(base, mask)
582+
#define DIRECT_WRITE_LOW(base, mask) direct_write_low(base, mask)
583+
#define DIRECT_WRITE_HIGH(base, mask) direct_write_high(base, mask)
584+
#define DIRECT_MODE_INPUT(base, mask) direct_mode_input(base, mask)
585+
#define DIRECT_MODE_OUTPUT(base, mask) direct_mode_output(base, mask)
586+
504587
#else
505588
#define PIN_TO_BASEREG(pin) (0)
506589
#define PIN_TO_BITMASK(pin) (pin)

util/OneWire_direct_regtype.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
#elif defined(__riscv)
5353
#define IO_REG_TYPE uint32_t
5454

55+
#elif defined(ARDUINO_SILABS)
56+
#define IO_REG_TYPE uint32_t
57+
5558
#else
5659
#define IO_REG_TYPE unsigned int
5760

0 commit comments

Comments
 (0)