diff --git a/README.md b/README.md index 1a3163b3..4d84061f 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,8 @@ Currently the following boards are supported by the library: * Balanduino * Sanguino * Black Widdow +* RedBearLab nRF51822 + * If you are using the RedBearLab nRF51822, then you must include the RedBearLab SPI library like so: ```#include ``` in your .ino file. The following boards need to be activated manually in [settings.h](settings.h): diff --git a/avrpins.h b/avrpins.h index da80349d..262bb6e2 100644 --- a/avrpins.h +++ b/avrpins.h @@ -961,6 +961,58 @@ MAKE_PIN(P78, PIOB, PIO_PB23); // Unconnected #undef MAKE_PIN +#elif defined(RBL_NRF51822) + +#define MAKE_PIN(className, pin) \ +class className { \ +public: \ + static void Set() { \ + nrf_gpio_pin_set(pin); \ + } \ + static void Clear() { \ + nrf_gpio_pin_clear(pin); \ + } \ + static void SetDirRead() { \ + nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL); \ + } \ + static void SetDirWrite() { \ + nrf_gpio_cfg_output(pin); \ + } \ + static uint8_t IsSet() { \ + return (uint8_t)nrf_gpio_pin_read(pin); \ + } \ +}; + +// See: pin_transform.c in RBL nRF51822 SDK +MAKE_PIN(P0, Pin_nRF51822_to_Arduino(D0)); +MAKE_PIN(P1, Pin_nRF51822_to_Arduino(D1)); +MAKE_PIN(P2, Pin_nRF51822_to_Arduino(D2)); +MAKE_PIN(P3, Pin_nRF51822_to_Arduino(D3)); +MAKE_PIN(P4, Pin_nRF51822_to_Arduino(D4)); +MAKE_PIN(P5, Pin_nRF51822_to_Arduino(D5)); +MAKE_PIN(P6, Pin_nRF51822_to_Arduino(D6)); +MAKE_PIN(P7, Pin_nRF51822_to_Arduino(D7)); +MAKE_PIN(P8, Pin_nRF51822_to_Arduino(D8)); +MAKE_PIN(P9, Pin_nRF51822_to_Arduino(D9)); // INT +MAKE_PIN(P10, Pin_nRF51822_to_Arduino(D10)); // SS +MAKE_PIN(P11, Pin_nRF51822_to_Arduino(D11)); +MAKE_PIN(P12, Pin_nRF51822_to_Arduino(D12)); +MAKE_PIN(P13, Pin_nRF51822_to_Arduino(D13)); +MAKE_PIN(P14, Pin_nRF51822_to_Arduino(D14)); +MAKE_PIN(P15, Pin_nRF51822_to_Arduino(D15)); +MAKE_PIN(P17, Pin_nRF51822_to_Arduino(D17)); // MISO +MAKE_PIN(P18, Pin_nRF51822_to_Arduino(D18)); // MOSI +MAKE_PIN(P16, Pin_nRF51822_to_Arduino(D16)); // CLK +MAKE_PIN(P19, Pin_nRF51822_to_Arduino(D19)); +MAKE_PIN(P20, Pin_nRF51822_to_Arduino(D20)); +MAKE_PIN(P21, Pin_nRF51822_to_Arduino(D21)); +MAKE_PIN(P22, Pin_nRF51822_to_Arduino(D22)); +MAKE_PIN(P23, Pin_nRF51822_to_Arduino(D23)); +MAKE_PIN(P24, Pin_nRF51822_to_Arduino(D24)); + +#undef MAKE_PIN + + #else #error "Please define board in avrpins.h" diff --git a/settings.h b/settings.h index e3d0ed1d..cc02f583 100644 --- a/settings.h +++ b/settings.h @@ -137,8 +137,8 @@ e-mail : support@circuitsathome.com #define USING_SPI4TEENSY3 0 #endif -#if defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__) -#include // Use the Arduino SPI library for the Arduino Due +#if (defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)) || defined(RBL_NRF51822) +#include // Use the Arduino SPI library for the Arduino Due and RedBearLab nRF51822 #endif #endif /* SETTINGS_H */ diff --git a/usbhost.h b/usbhost.h index cf26c1f0..f35bc0f4 100644 --- a/usbhost.h +++ b/usbhost.h @@ -27,6 +27,7 @@ e-mail : support@circuitsathome.com #include #endif + /* SPI initialization */ template< typename SPI_CLK, typename SPI_MOSI, typename SPI_MISO, typename SPI_SS > class SPi { public: @@ -46,6 +47,13 @@ public: SPI.begin(); SPI.setClockDivider(4); // Set speed to 84MHz/4=21MHz - the MAX3421E can handle up to 26MHz } +#elif defined(RBL_NRF51822) + static void init() { + SPI_SS::SetDirWrite(); + SPI_SS::Set(); + SPI.begin(); + // SPI.setFrequency(SPI_FREQUENCY_8M); + } #else static void init() { //uint8_t tmp; @@ -74,6 +82,8 @@ typedef SPi< Pb7, Pb5, Pb6, Pb4 > spi; typedef SPi< P13, P11, P12, P10 > spi; #elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__) typedef SPi< P76, P75, P74, P10 > spi; +#elif defined(RBL_NRF51822) +typedef SPi< P16, P18, P17, P10 > spi; #else #error "No SPI entry in usbhost.h" #endif @@ -135,7 +145,7 @@ void MAX3421e< SPI_SS, INTR >::regWr(uint8_t reg, uint8_t data) { c[0] = reg | 0x02; c[1] = data; spi4teensy3::send(c, 2); -#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__) +#elif (defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)) || defined(RBL_NRF51822) SPI.transfer(reg | 0x02); SPI.transfer(data); #else @@ -159,7 +169,7 @@ uint8_t* MAX3421e< SPI_SS, INTR >::bytesWr(uint8_t reg, uint8_t nbytes, uint8_t* spi4teensy3::send(reg | 0x02); spi4teensy3::send(data_p, nbytes); data_p += nbytes; -#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__) +#elif (defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)) || defined(RBL_NRF51822) SPI.transfer(reg | 0x02); while(nbytes) { SPI.transfer(*data_p); @@ -201,7 +211,7 @@ uint8_t MAX3421e< SPI_SS, INTR >::regRd(uint8_t reg) { spi4teensy3::send(reg); uint8_t rv = spi4teensy3::receive(); SPI_SS::Set(); -#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__) +#elif (defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)) || defined(RBL_NRF51822) SPI.transfer(reg); uint8_t rv = SPI.transfer(0); SPI_SS::Set(); @@ -227,7 +237,7 @@ uint8_t* MAX3421e< SPI_SS, INTR >::bytesRd(uint8_t reg, uint8_t nbytes, uint8_t* spi4teensy3::send(reg); spi4teensy3::receive(data_p, nbytes); data_p += nbytes; -#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__) +#elif (defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)) || defined(RBL_NRF51822) SPI.transfer(reg); while(nbytes) { *data_p++ = SPI.transfer(0);