diff --git a/megaavr/cores/megatinycore/Arduino.h b/megaavr/cores/megatinycore/Arduino.h index f1ee551e..d1a91be5 100644 --- a/megaavr/cores/megatinycore/Arduino.h +++ b/megaavr/cores/megatinycore/Arduino.h @@ -40,7 +40,7 @@ extern "C"{ // internal from VREF /* Values shifted to avoid clashing with ADC REFSEL defines - Will shift back in analog_reference function + Will shift back in analog_reference function */ #define INTERNAL0V55 (VREF_ADC0REFSEL_0V55_gc >> VREF_ADC0REFSEL_gp) #define INTERNAL1V1 (VREF_ADC0REFSEL_1V1_gc >> VREF_ADC0REFSEL_gp) @@ -77,7 +77,7 @@ extern "C"{ #endif /* Allows performing a correction on the CPU value using the signature row - values indicating oscillator error provided from the device manufacturer */ + values indicating oscillator error provided from the device manufacturer */ #define PERFORM_SIGROW_CORRECTION_F_CPU 0 uint16_t clockCyclesPerMicrosecondComp(uint32_t clk); diff --git a/megaavr/cores/megatinycore/UART.cpp b/megaavr/cores/megatinycore/UART.cpp index dd600683..44c1eb5f 100644 --- a/megaavr/cores/megatinycore/UART.cpp +++ b/megaavr/cores/megatinycore/UART.cpp @@ -145,8 +145,8 @@ bool UartClass::pins(uint8_t tx, uint8_t rx) for (_pin_set = 0; _pin_set < SERIAL_PIN_SETS; ++_pin_set) { if (tx == _hw_set[_pin_set].tx_pin && rx == _hw_set[_pin_set].rx_pin) { // We are good, this set of pins is supported - return true; - } + return true; + } } _pin_set = 0; // Default to standard return false; diff --git a/megaavr/cores/megatinycore/UART.h b/megaavr/cores/megatinycore/UART.h index a1147d60..5d0e545c 100644 --- a/megaavr/cores/megatinycore/UART.h +++ b/megaavr/cores/megatinycore/UART.h @@ -128,9 +128,9 @@ class UartClass : public HardwareSerial volatile USART_t * const _hwserial_module; struct UartPinSet { - uint8_t const rx_pin; - uint8_t const tx_pin; - uint8_t const mux; + uint8_t const rx_pin; + uint8_t const tx_pin; + uint8_t const mux; } _hw_set[SERIAL_PIN_SETS]; uint8_t _pin_set; @@ -176,7 +176,7 @@ class UartClass : public HardwareSerial // Interrupt handlers - Not intended to be called externally inline void _rx_complete_irq(void); void _tx_data_empty_irq(void); - + private: void _poll_tx_data_empty(void); }; diff --git a/megaavr/cores/megatinycore/UART_private.h b/megaavr/cores/megatinycore/UART_private.h index 5e8e1f13..8350cd69 100644 --- a/megaavr/cores/megatinycore/UART_private.h +++ b/megaavr/cores/megatinycore/UART_private.h @@ -23,7 +23,7 @@ #include "wiring_private.h" -// this next line disables the entire UART.cpp, +// this next line disables the entire UART.cpp, // this is so I can support Attiny series and any other chip without a uart #if defined(HAVE_HWSERIAL0) || defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3) @@ -40,7 +40,7 @@ UartClass::UartClass( volatile uint8_t uart_mux_swap) : _hwserial_module(hwserial_module), _hw_set { { hwserial_rx_pin, hwserial_tx_pin, uart_mux }, - { hwserial_rx_pin_swap, hwserial_tx_pin_swap, uart_mux_swap } }, + { hwserial_rx_pin_swap, hwserial_tx_pin_swap, uart_mux_swap } }, _pin_set(0), _written(false), _rx_buffer_head(0), _rx_buffer_tail(0), diff --git a/megaavr/cores/megatinycore/WMath.cpp b/megaavr/cores/megatinycore/WMath.cpp index f4ce2b75..34b46ce6 100644 --- a/megaavr/cores/megatinycore/WMath.cpp +++ b/megaavr/cores/megatinycore/WMath.cpp @@ -4,7 +4,7 @@ Part of the Wiring project - http://wiring.org.co Copyright (c) 2004-06 Hernando Barragan Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/ - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either diff --git a/megaavr/cores/megatinycore/api/HardwareI2C.h b/megaavr/cores/megatinycore/api/HardwareI2C.h index 25bf40e5..4ffbbc31 100644 --- a/megaavr/cores/megatinycore/api/HardwareI2C.h +++ b/megaavr/cores/megatinycore/api/HardwareI2C.h @@ -29,7 +29,7 @@ class HardwareI2C : public Stream virtual void end() = 0; virtual void setClock(uint32_t freq) = 0; - + virtual void beginTransmission(uint8_t address) = 0; virtual uint8_t endTransmission(bool stopBit) = 0; virtual uint8_t endTransmission(void) = 0; diff --git a/megaavr/cores/megatinycore/api/IPAddress.h b/megaavr/cores/megatinycore/api/IPAddress.h index af33cf84..29dbe8ff 100644 --- a/megaavr/cores/megatinycore/api/IPAddress.h +++ b/megaavr/cores/megatinycore/api/IPAddress.h @@ -28,8 +28,8 @@ class IPAddress : public Printable { private: union { - uint8_t bytes[4]; // IPv4 address - uint32_t dword; + uint8_t bytes[4]; // IPv4 address + uint32_t dword; } _address; // Access the raw byte array containing the address. Because this returns a pointer diff --git a/megaavr/cores/megatinycore/api/PluggableUSB.cpp b/megaavr/cores/megatinycore/api/PluggableUSB.cpp index f0c45f01..6904ab59 100644 --- a/megaavr/cores/megatinycore/api/PluggableUSB.cpp +++ b/megaavr/cores/megatinycore/api/PluggableUSB.cpp @@ -22,78 +22,78 @@ int PluggableUSB_::getInterface(uint8_t* interfaceCount) { - int sent = 0; - PluggableUSBModule* node; - for (node = rootNode; node; node = node->next) { - int res = node->getInterface(interfaceCount); - if (res < 0) - return -1; - sent += res; - } - return sent; + int sent = 0; + PluggableUSBModule* node; + for (node = rootNode; node; node = node->next) { + int res = node->getInterface(interfaceCount); + if (res < 0) + return -1; + sent += res; + } + return sent; } int PluggableUSB_::getDescriptor(USBSetup& setup) { - PluggableUSBModule* node; - for (node = rootNode; node; node = node->next) { - int ret = node->getDescriptor(setup); - // ret!=0 -> request has been processed - if (ret) - return ret; - } - return 0; + PluggableUSBModule* node; + for (node = rootNode; node; node = node->next) { + int ret = node->getDescriptor(setup); + // ret!=0 -> request has been processed + if (ret) + return ret; + } + return 0; } void PluggableUSB_::getShortName(char *iSerialNum) { - PluggableUSBModule* node; - for (node = rootNode; node; node = node->next) { - iSerialNum += node->getShortName(iSerialNum); - } - *iSerialNum = 0; + PluggableUSBModule* node; + for (node = rootNode; node; node = node->next) { + iSerialNum += node->getShortName(iSerialNum); + } + *iSerialNum = 0; } bool PluggableUSB_::setup(USBSetup& setup) { - PluggableUSBModule* node; - for (node = rootNode; node; node = node->next) { - if (node->setup(setup)) { - return true; - } - } - return false; + PluggableUSBModule* node; + for (node = rootNode; node; node = node->next) { + if (node->setup(setup)) { + return true; + } + } + return false; } bool PluggableUSB_::plug(PluggableUSBModule *node) { - if ((lastEp + node->numEndpoints) > totalEP) { - return false; - } + if ((lastEp + node->numEndpoints) > totalEP) { + return false; + } - if (!rootNode) { - rootNode = node; - } else { - PluggableUSBModule *current = rootNode; - while (current->next) { - current = current->next; - } - current->next = node; - } + if (!rootNode) { + rootNode = node; + } else { + PluggableUSBModule *current = rootNode; + while (current->next) { + current = current->next; + } + current->next = node; + } - node->pluggedInterface = lastIf; - node->pluggedEndpoint = lastEp; - lastIf += node->numInterfaces; - for (uint8_t i = 0; i < node->numEndpoints; i++) { - *(unsigned int*)(epBuffer(lastEp)) = node->endpointType[i]; - lastEp++; - } - return true; - // restart USB layer??? + node->pluggedInterface = lastIf; + node->pluggedEndpoint = lastEp; + lastIf += node->numInterfaces; + for (uint8_t i = 0; i < node->numEndpoints; i++) { + *(unsigned int*)(epBuffer(lastEp)) = node->endpointType[i]; + lastEp++; + } + return true; + // restart USB layer??? } PluggableUSB_& PluggableUSB() { - static PluggableUSB_ obj; - return obj; -} \ No newline at end of file + static PluggableUSB_ obj; + return obj; +} diff --git a/megaavr/cores/megatinycore/api/Stream.h b/megaavr/cores/megatinycore/api/Stream.h index 7dbe2fde..ec98f2c6 100644 --- a/megaavr/cores/megatinycore/api/Stream.h +++ b/megaavr/cores/megatinycore/api/Stream.h @@ -66,7 +66,7 @@ class Stream : public Print void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second unsigned long getTimeout(void) { return _timeout; } - + bool find(char *target); // reads data from the stream until the target string is found bool find(uint8_t *target) { return find ((char *)target); } // returns true if target string is found, false if timed out (see setTimeout) diff --git a/megaavr/cores/megatinycore/api/USBAPI.h b/megaavr/cores/megatinycore/api/USBAPI.h index 8743ab4b..5e01cb3e 100644 --- a/megaavr/cores/megatinycore/api/USBAPI.h +++ b/megaavr/cores/megatinycore/api/USBAPI.h @@ -36,11 +36,11 @@ typedef struct __attribute__((packed)) uint8_t transferDirection : 1; }; }; - uint8_t bRequest; - uint8_t wValueL; - uint8_t wValueH; - uint16_t wIndex; - uint16_t wLength; + uint8_t bRequest; + uint8_t wValueL; + uint8_t wValueH; + uint16_t wIndex; + uint16_t wLength; } USBSetup; //================================================================================ @@ -50,11 +50,11 @@ int USB_SendControl(uint8_t flags, const void* d, int len); int USB_RecvControl(void* d, int len); int USB_RecvControlLong(void* d, int len); -uint8_t USB_Available(uint8_t ep); +uint8_t USB_Available(uint8_t ep); uint8_t USB_SendSpace(uint8_t ep); -int USB_Send(uint8_t ep, const void* data, int len); // blocking -int USB_Recv(uint8_t ep, void* data, int len); // non-blocking -int USB_Recv(uint8_t ep); // non-blocking +int USB_Send(uint8_t ep, const void* data, int len); // blocking +int USB_Recv(uint8_t ep, void* data, int len); // non-blocking +int USB_Recv(uint8_t ep); // non-blocking void USB_Flush(uint8_t ep); -#endif \ No newline at end of file +#endif diff --git a/megaavr/cores/megatinycore/api/Udp.h b/megaavr/cores/megatinycore/api/Udp.h index 90581653..8a1842fe 100644 --- a/megaavr/cores/megatinycore/api/Udp.h +++ b/megaavr/cores/megatinycore/api/Udp.h @@ -4,7 +4,7 @@ * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these) * 1) UDP does not guarantee the order in which assembled UDP packets are received. This * might not happen often in practice, but in larger network topologies, a UDP - * packet can be received out of sequence. + * packet can be received out of sequence. * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being * aware of it. Again, this may not be a concern in practice on small local networks. * For more information, see http://www.cafeaulait.org/course/week12/35.html @@ -17,10 +17,10 @@ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -40,11 +40,11 @@ class UDP : public Stream { public: - virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use + virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use virtual void stop() =0; // Finish with the UDP socket // Sending UDP packets - + // Start building up a packet to send to the remote host specific in ip and port // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port virtual int beginPacket(IPAddress ip, uint16_t port) =0; @@ -74,7 +74,7 @@ class UDP : public Stream { virtual int read(char* buffer, size_t len) =0; // Return the next byte from the current packet without moving on to the next byte virtual int peek() =0; - virtual void flush() =0; // Finish reading the current packet + virtual void flush() =0; // Finish reading the current packet // Return the IP address of the host who sent the current incoming packet virtual IPAddress remoteIP() =0; diff --git a/megaavr/cores/megatinycore/api/Udp.h.orig b/megaavr/cores/megatinycore/api/Udp.h.orig index b6950024..b54d4cf0 100644 --- a/megaavr/cores/megatinycore/api/Udp.h.orig +++ b/megaavr/cores/megatinycore/api/Udp.h.orig @@ -4,7 +4,7 @@ * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these) * 1) UDP does not guarantee the order in which assembled UDP packets are received. This * might not happen often in practice, but in larger network topologies, a UDP - * packet can be received out of sequence. + * packet can be received out of sequence. * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being * aware of it. Again, this may not be a concern in practice on small local networks. * For more information, see http://www.cafeaulait.org/course/week12/35.html @@ -17,10 +17,10 @@ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -42,11 +42,11 @@ namespace arduino { class UDP : public Stream { public: - virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use + virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use virtual void stop() =0; // Finish with the UDP socket // Sending UDP packets - + // Start building up a packet to send to the remote host specific in ip and port // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port virtual int beginPacket(IPAddress ip, uint16_t port) =0; @@ -76,7 +76,7 @@ public: virtual int read(char* buffer, size_t len) =0; // Return the next byte from the current packet without moving on to the next byte virtual int peek() =0; - virtual void flush() =0; // Finish reading the current packet + virtual void flush() =0; // Finish reading the current packet // Return the IP address of the host who sent the current incoming packet virtual IPAddress remoteIP() =0; @@ -86,4 +86,4 @@ protected: uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; }; -} \ No newline at end of file +} diff --git a/megaavr/cores/megatinycore/api/WCharacter.h b/megaavr/cores/megatinycore/api/WCharacter.h index 4ce89ee8..1a2dff4d 100644 --- a/megaavr/cores/megatinycore/api/WCharacter.h +++ b/megaavr/cores/megatinycore/api/WCharacter.h @@ -1,17 +1,17 @@ /* WCharacter.h - Character utility functions for Wiring & Arduino Copyright (c) 2010 Hernando Barragan. All right reserved. - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. - + This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. - + You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA @@ -41,15 +41,15 @@ inline int toLowerCase(int c) __attribute__((always_inline)); inline int toUpperCase(int c)__attribute__((always_inline)); -// Checks for an alphanumeric character. +// Checks for an alphanumeric character. // It is equivalent to (isalpha(c) || isdigit(c)). -inline bool isAlphaNumeric(int c) +inline bool isAlphaNumeric(int c) { return ( isalnum(c) == 0 ? false : true); } -// Checks for an alphabetic character. +// Checks for an alphabetic character. // It is equivalent to (isupper(c) || islower(c)). inline bool isAlpha(int c) { @@ -57,7 +57,7 @@ inline bool isAlpha(int c) } -// Checks whether c is a 7-bit unsigned char value +// Checks whether c is a 7-bit unsigned char value // that fits into the ASCII character set. inline bool isAscii(int c) { @@ -107,7 +107,7 @@ inline bool isPrintable(int c) } -// Checks for any printable character which is not a space +// Checks for any printable character which is not a space // or an alphanumeric character. inline bool isPunct(int c) { @@ -115,8 +115,8 @@ inline bool isPunct(int c) } -// Checks for white-space characters. For the avr-libc library, -// these are: space, formfeed ('\f'), newline ('\n'), carriage +// Checks for white-space characters. For the avr-libc library, +// these are: space, formfeed ('\f'), newline ('\n'), carriage // return ('\r'), horizontal tab ('\t'), and vertical tab ('\v'). inline bool isSpace(int c) { @@ -131,7 +131,7 @@ inline bool isUpperCase(int c) } -// Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7 +// Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7 // 8 9 a b c d e f A B C D E F. inline bool isHexadecimalDigit(int c) { @@ -139,7 +139,7 @@ inline bool isHexadecimalDigit(int c) } -// Converts c to a 7-bit unsigned char value that fits into the +// Converts c to a 7-bit unsigned char value that fits into the // ASCII character set, by clearing the high-order bits. inline int toAscii(int c) { @@ -148,8 +148,8 @@ inline int toAscii(int c) // Warning: -// Many people will be unhappy if you use this function. -// This function will convert accented letters into random +// Many people will be unhappy if you use this function. +// This function will convert accented letters into random // characters. // Converts the letter c to lower case, if possible. @@ -165,4 +165,4 @@ inline int toUpperCase(int c) return toupper (c); } -#endif \ No newline at end of file +#endif diff --git a/megaavr/cores/megatinycore/hooks.c b/megaavr/cores/megatinycore/hooks.c index 641eabc7..a61b5ef5 100644 --- a/megaavr/cores/megatinycore/hooks.c +++ b/megaavr/cores/megatinycore/hooks.c @@ -26,6 +26,6 @@ * real cooperative scheduler. */ static void __empty() { - // Empty + // Empty } void yield(void) __attribute__ ((weak, alias("__empty"))); diff --git a/megaavr/cores/megatinycore/main.cpp b/megaavr/cores/megatinycore/main.cpp index 2462a6ae..f9f07098 100644 --- a/megaavr/cores/megatinycore/main.cpp +++ b/megaavr/cores/megatinycore/main.cpp @@ -32,23 +32,23 @@ void setupUSB() { } int main(void) { - init(); + init(); - initVariant(); + initVariant(); #if defined(USBCON) - USBDevice.attach(); + USBDevice.attach(); #endif - setup(); + setup(); - for (;;) { - loop(); + for (;;) { + loop(); #ifndef NOSERIALEVENT - if (serialEventRun) serialEventRun(); + if (serialEventRun) serialEventRun(); #endif - } + } - return 0; + return 0; } diff --git a/megaavr/cores/megatinycore/timers.h b/megaavr/cores/megatinycore/timers.h index b05fa6cf..983f36b8 100644 --- a/megaavr/cores/megatinycore/timers.h +++ b/megaavr/cores/megatinycore/timers.h @@ -14,21 +14,21 @@ #define TIME_TRACKING_TIMER_PERIOD ((F_CPU/2000)-1) #endif #elif defined(MILLIS_USE_TIMERD0) - #define TIME_TRACKING_TIMER_PERIOD 0x1FD + #define TIME_TRACKING_TIMER_PERIOD 0x1FD #if (F_CPU==1000000UL) - #define TIME_TRACKING_TIMER_DIVIDER 64 /* Clock divider for TCD0 */ + #define TIME_TRACKING_TIMER_DIVIDER 64 /* Clock divider for TCD0 */ #else #define TIME_TRACKING_TIMER_DIVIDER 32 /* Clock divider for TCD0 */ #endif #else //Otherwise TCA0 - #define TIME_TRACKING_TIMER_PERIOD 0xFE + #define TIME_TRACKING_TIMER_PERIOD 0xFE #if (F_CPU==5000000UL)||(F_CPU==4000000UL) #define TIME_TRACKING_TIMER_DIVIDER 16 #elif (F_CPU==1000000UL) #define TIME_TRACKING_TIMER_DIVIDER 8 #else - #define TIME_TRACKING_TIMER_DIVIDER 64 /* Clock divider for TCA0 */ + #define TIME_TRACKING_TIMER_DIVIDER 64 /* Clock divider for TCA0 */ #endif #endif @@ -39,13 +39,13 @@ #define TIMERD0_PRESCALER 0x10 #endif -#define TIME_TRACKING_TICKS_PER_OVF (TIME_TRACKING_TIMER_PERIOD + 1) -#define TIME_TRACKING_CYCLES_PER_OVF (TIME_TRACKING_TICKS_PER_OVF * TIME_TRACKING_TIMER_DIVIDER) +#define TIME_TRACKING_TICKS_PER_OVF (TIME_TRACKING_TIMER_PERIOD + 1) +#define TIME_TRACKING_CYCLES_PER_OVF (TIME_TRACKING_TICKS_PER_OVF * TIME_TRACKING_TIMER_DIVIDER) //For a type B timer as millis, these #defines aren't needed! -#define PWM_TIMER_PERIOD 0xFE /* For frequency */ -#define PWM_TIMER_COMPARE 0x80 /* For duty cycle */ +#define PWM_TIMER_PERIOD 0xFE /* For frequency */ +#define PWM_TIMER_COMPARE 0x80 /* For duty cycle */ #if defined(MILLIS_USE_TIMERA0) #define MILLIS_TIMER TIMERA0 diff --git a/megaavr/cores/megatinycore/wiring_analog.c b/megaavr/cores/megatinycore/wiring_analog.c index be6b9b89..c389e847 100644 --- a/megaavr/cores/megatinycore/wiring_analog.c +++ b/megaavr/cores/megatinycore/wiring_analog.c @@ -28,69 +28,69 @@ void analogReference(uint8_t mode) { - switch (mode) - { - #if defined(EXTERNAL) - case EXTERNAL: - #elif defined(EXTERNAL_EXPERIMENTAL) - case EXTERNAL_EXPERIMENTAL: - #endif - case VDD: - ADC0.CTRLC = (ADC0.CTRLC & ~(ADC_REFSEL_gm)) | mode | ADC_SAMPCAP_bm; //per datasheet, recommended SAMPCAP=1 at ref > 1v - we don't *KNOW* the external reference will be >1v, but it's probably more likely... - // VREF.CTRLA does not need to be reconfigured, as the voltage references only supply their specified voltage when requested to do so by the ADC. - break; - case INTERNAL0V55: - VREF.CTRLA = VREF.CTRLA & ~(VREF_ADC0REFSEL_gm); //These bits are all 0 for 0.55v reference, so no need to do the mode << VREF_ADC0REFSEL_gp here; - ADC0.CTRLC = (ADC0.CTRLC & ~(ADC_REFSEL_gm | ADC_SAMPCAP_bm)) | INTERNAL; //per datasheet, recommended SAMPCAP=0 at ref < 1v - break; - case INTERNAL1V1: - case INTERNAL2V5: - case INTERNAL4V34: - case INTERNAL1V5: - VREF.CTRLA = (VREF.CTRLA & ~(VREF_ADC0REFSEL_gm)) | (mode << VREF_ADC0REFSEL_gp); - ADC0.CTRLC = (ADC0.CTRLC & ~(ADC_REFSEL_gm)) | INTERNAL | ADC_SAMPCAP_bm; //per datasheet, recommended SAMPCAP=1 at ref > 1v - break; - default: - ADC0.CTRLC = (ADC0.CTRLC & ~(ADC_REFSEL_gm)) | VDD | ADC_SAMPCAP_bm; //per datasheet, recommended SAMPCAP=1 at ref > 1v - we don't *KNOW* the external reference will be >1v, but it's probably more likely... - } + switch (mode) + { + #if defined(EXTERNAL) + case EXTERNAL: + #elif defined(EXTERNAL_EXPERIMENTAL) + case EXTERNAL_EXPERIMENTAL: + #endif + case VDD: + ADC0.CTRLC = (ADC0.CTRLC & ~(ADC_REFSEL_gm)) | mode | ADC_SAMPCAP_bm; //per datasheet, recommended SAMPCAP=1 at ref > 1v - we don't *KNOW* the external reference will be >1v, but it's probably more likely... + // VREF.CTRLA does not need to be reconfigured, as the voltage references only supply their specified voltage when requested to do so by the ADC. + break; + case INTERNAL0V55: + VREF.CTRLA = VREF.CTRLA & ~(VREF_ADC0REFSEL_gm); //These bits are all 0 for 0.55v reference, so no need to do the mode << VREF_ADC0REFSEL_gp here; + ADC0.CTRLC = (ADC0.CTRLC & ~(ADC_REFSEL_gm | ADC_SAMPCAP_bm)) | INTERNAL; //per datasheet, recommended SAMPCAP=0 at ref < 1v + break; + case INTERNAL1V1: + case INTERNAL2V5: + case INTERNAL4V34: + case INTERNAL1V5: + VREF.CTRLA = (VREF.CTRLA & ~(VREF_ADC0REFSEL_gm)) | (mode << VREF_ADC0REFSEL_gp); + ADC0.CTRLC = (ADC0.CTRLC & ~(ADC_REFSEL_gm)) | INTERNAL | ADC_SAMPCAP_bm; //per datasheet, recommended SAMPCAP=1 at ref > 1v + break; + default: + ADC0.CTRLC = (ADC0.CTRLC & ~(ADC_REFSEL_gm)) | VDD | ADC_SAMPCAP_bm; //per datasheet, recommended SAMPCAP=1 at ref > 1v - we don't *KNOW* the external reference will be >1v, but it's probably more likely... + } } #ifdef DAC0 void DACReference(uint8_t mode) { - if (mode < 5){ - VREF.CTRLA = mode|(VREF.CTRLA&~7); - } + if (mode < 5){ + VREF.CTRLA = mode|(VREF.CTRLA&~7); + } } #endif int analogRead(uint8_t pin) { - #ifdef ADC_DAC0 - if (pin!=ADC_DAC0 && pin != ADC_INTREF && pin != ADC_TEMPERATURE) - #else - if (pin != ADC_INTREF && pin != ADC_TEMPERATURE) - #endif - { - pin = digitalPinToAnalogInput(pin); - if(pin == NOT_A_PIN) return -1; - } + #ifdef ADC_DAC0 + if (pin!=ADC_DAC0 && pin != ADC_INTREF && pin != ADC_TEMPERATURE) + #else + if (pin != ADC_INTREF && pin != ADC_TEMPERATURE) + #endif + { + pin = digitalPinToAnalogInput(pin); + if(pin == NOT_A_PIN) return -1; + } #if defined(ADC0) - /* Reference should be already set up */ - /* Select channel */ - ADC0.MUXPOS = (pin << ADC_MUXPOS_gp); + /* Reference should be already set up */ + /* Select channel */ + ADC0.MUXPOS = (pin << ADC_MUXPOS_gp); - /* Start conversion */ - ADC0.COMMAND = ADC_STCONV_bm; + /* Start conversion */ + ADC0.COMMAND = ADC_STCONV_bm; - /* Wait for result ready */ - while(!(ADC0.INTFLAGS & ADC_RESRDY_bm)); + /* Wait for result ready */ + while(!(ADC0.INTFLAGS & ADC_RESRDY_bm)); - /* Combine two bytes */ - return ADC0.RES; + /* Combine two bytes */ + return ADC0.RES; -#else /* No ADC, return 0 */ - return 0; +#else /* No ADC, return 0 */ + return 0; #endif } @@ -99,13 +99,13 @@ int analogRead(uint8_t pin) //like the pinswap functions, if the user passes bogus values, we set it to the default and return false. bool analogReadResolution(uint8_t res) { - if (res==8) { - ADC0.CTRLA|=ADC_RESSEL_bm; - return true; - } - //if argument wasn't 8, we'll be putting it to default value either way - ADC0.CTRLA&=~ADC_RESSEL_bm; - return (res==10); //but only return true if the value passed was the valid option, 10. + if (res==8) { + ADC0.CTRLA|=ADC_RESSEL_bm; + return true; + } + //if argument wasn't 8, we'll be putting it to default value either way + ADC0.CTRLA&=~ADC_RESSEL_bm; + return (res==10); //but only return true if the value passed was the valid option, 10. } @@ -115,118 +115,118 @@ bool analogReadResolution(uint8_t res) { // to digital output. void analogWrite(uint8_t pin, int val) { - uint8_t bit_pos = digitalPinToBitPosition(pin); - if(bit_pos == NOT_A_PIN) return; - // We need to make sure the PWM output is enabled for those pins - // that support it, as we turn it off when digitally reading or - // writing with them. Also, make sure the pin is in output mode - // for consistently with Wiring, which doesn't require a pinMode - // call for the analog output pins. - pinMode(pin, OUTPUT); - - //if(val < 1){ /* if zero or negative drive digital low */ - - // digitalWrite(pin, LOW); - - //} else if(val > 255){ /* if max or greater drive digital high */ - - // digitalWrite(pin, HIGH); - - //} else { /* handle pwm to generate analog value */ - /* Get timer */ - uint8_t digital_pin_timer = digitalPinToTimer(pin); - uint8_t* timer_cmp_out; - - //TCB_t *timer_B; - /* Find out Port and Pin to correctly handle port mux, and timer. */ - switch (digital_pin_timer) { //use only low nybble which defines which timer it is - - case TIMERA0: - if(val <= 0){ /* if zero or negative drive digital low */ - digitalWrite(pin, LOW); - } else if(val >= 255){ /* if max or greater drive digital high */ - digitalWrite(pin, HIGH); - } else { - /* Calculate correct compare buffer register */ - #ifdef __AVR_ATtinyxy2__ - if (bit_pos==7) bit_pos=0; //on the xy2, WO0 is on PA7 - #endif - if (bit_pos>2) { - bit_pos-=3; - timer_cmp_out = ((uint8_t*) (&TCA0.SPLIT.HCMP0)) + (bit_pos<<1); - (*timer_cmp_out) = (val); - TCA0.SPLIT.CTRLB |= (1 << (TCA_SPLIT_HCMP0EN_bp + bit_pos)); - } else { - timer_cmp_out = ((uint8_t*) (&TCA0.SPLIT.LCMP0)) + (bit_pos<<1); - (*timer_cmp_out) = (val); - TCA0.SPLIT.CTRLB |= (1 << (TCA_SPLIT_LCMP0EN_bp + bit_pos)); - } - } - break; - /* None of these parts have a Timer B that gives us PWM on a pin we don't already have it on. - case TIMERB0: - case TIMERB1: - case TIMERB2: - case TIMERB3: - - - // Get pointer to timer, TIMERB0 order definition in Arduino.h - //assert (((TIMERB0 - TIMERB3) == 2)); - timer_B = ((TCB_t *)&TCB0 + (digital_pin_timer - (TIMERB0&0x07)); - - // set duty cycle - timer_B->CCMPH = val; - - ///Enable Timer Output - timer_B->CTRLB |= (TCB_CCMPEN_bm); - - break; - */ - #if defined(DAC0) - case DACOUT: - DAC0.DATA=val; - DAC0.CTRLA=0x41; //OUTEN=1, ENABLE=1 - break; - #endif - #if (defined(TCD0) && defined(USE_TIMERD0_PWM)) - case TIMERD0: - if(val < 1){ /* if zero or negative drive digital low */ - digitalWrite(pin, LOW); - } else if(val > 254){ /* if max or greater drive digital high */ - digitalWrite(pin, HIGH); - } else { - if (bit_pos) { - TCD0.CMPBSET=(255-val)<<1; - } else { - TCD0.CMPASET=(255-val)<<1; - } - if (!(TCD0.FAULTCTRL & (1<<(6+bit_pos)))) { //bitpos will be 0 or 1 for TIMERD pins - //if not active, we need to activate it, which produces a glitch in the PWM - TCD0.CTRLA=TIMERD0_PRESCALER;//stop the timer - while(!(TCD0.STATUS&0x01)) {;} // wait until it's actually stopped - uint8_t sreg=SREG; - cli(); - _PROTECTED_WRITE(TCD0.FAULTCTRL,TCD0.FAULTCTRL|(1<<(6+bit_pos))); - SREG=sreg; - TCD0.CTRLA=TIMERD0_PRESCALER|1; //reenable it - } else { - while(!(TCD0.STATUS&0x02)) {;} //if previous sync in progress, wait for it to finish. - TCD0.CTRLE=0x02; //Synchronize - } - } - break; - - #endif - - /* If non timer pin, or unknown timer definition. */ - /* do a digital write */ - case NOT_ON_TIMER: - default: - if (val < 128) { - digitalWrite(pin, LOW); - } else { - digitalWrite(pin, HIGH); - } - break; - } + uint8_t bit_pos = digitalPinToBitPosition(pin); + if(bit_pos == NOT_A_PIN) return; + // We need to make sure the PWM output is enabled for those pins + // that support it, as we turn it off when digitally reading or + // writing with them. Also, make sure the pin is in output mode + // for consistently with Wiring, which doesn't require a pinMode + // call for the analog output pins. + pinMode(pin, OUTPUT); + + //if(val < 1){ /* if zero or negative drive digital low */ + + // digitalWrite(pin, LOW); + + //} else if(val > 255){ /* if max or greater drive digital high */ + + // digitalWrite(pin, HIGH); + + //} else { /* handle pwm to generate analog value */ + /* Get timer */ + uint8_t digital_pin_timer = digitalPinToTimer(pin); + uint8_t* timer_cmp_out; + + //TCB_t *timer_B; + /* Find out Port and Pin to correctly handle port mux, and timer. */ + switch (digital_pin_timer) { //use only low nybble which defines which timer it is + + case TIMERA0: + if(val <= 0){ /* if zero or negative drive digital low */ + digitalWrite(pin, LOW); + } else if(val >= 255){ /* if max or greater drive digital high */ + digitalWrite(pin, HIGH); + } else { + /* Calculate correct compare buffer register */ + #ifdef __AVR_ATtinyxy2__ + if (bit_pos==7) bit_pos=0; //on the xy2, WO0 is on PA7 + #endif + if (bit_pos>2) { + bit_pos-=3; + timer_cmp_out = ((uint8_t*) (&TCA0.SPLIT.HCMP0)) + (bit_pos<<1); + (*timer_cmp_out) = (val); + TCA0.SPLIT.CTRLB |= (1 << (TCA_SPLIT_HCMP0EN_bp + bit_pos)); + } else { + timer_cmp_out = ((uint8_t*) (&TCA0.SPLIT.LCMP0)) + (bit_pos<<1); + (*timer_cmp_out) = (val); + TCA0.SPLIT.CTRLB |= (1 << (TCA_SPLIT_LCMP0EN_bp + bit_pos)); + } + } + break; + /* None of these parts have a Timer B that gives us PWM on a pin we don't already have it on. + case TIMERB0: + case TIMERB1: + case TIMERB2: + case TIMERB3: + + + // Get pointer to timer, TIMERB0 order definition in Arduino.h + //assert (((TIMERB0 - TIMERB3) == 2)); + timer_B = ((TCB_t *)&TCB0 + (digital_pin_timer - (TIMERB0&0x07)); + + // set duty cycle + timer_B->CCMPH = val; + + ///Enable Timer Output + timer_B->CTRLB |= (TCB_CCMPEN_bm); + + break; + */ + #if defined(DAC0) + case DACOUT: + DAC0.DATA=val; + DAC0.CTRLA=0x41; //OUTEN=1, ENABLE=1 + break; + #endif + #if (defined(TCD0) && defined(USE_TIMERD0_PWM)) + case TIMERD0: + if(val < 1){ /* if zero or negative drive digital low */ + digitalWrite(pin, LOW); + } else if(val > 254){ /* if max or greater drive digital high */ + digitalWrite(pin, HIGH); + } else { + if (bit_pos) { + TCD0.CMPBSET=(255-val)<<1; + } else { + TCD0.CMPASET=(255-val)<<1; + } + if (!(TCD0.FAULTCTRL & (1<<(6+bit_pos)))) { //bitpos will be 0 or 1 for TIMERD pins + //if not active, we need to activate it, which produces a glitch in the PWM + TCD0.CTRLA=TIMERD0_PRESCALER;//stop the timer + while(!(TCD0.STATUS&0x01)) {;} // wait until it's actually stopped + uint8_t sreg=SREG; + cli(); + _PROTECTED_WRITE(TCD0.FAULTCTRL,TCD0.FAULTCTRL|(1<<(6+bit_pos))); + SREG=sreg; + TCD0.CTRLA=TIMERD0_PRESCALER|1; //reenable it + } else { + while(!(TCD0.STATUS&0x02)) {;} //if previous sync in progress, wait for it to finish. + TCD0.CTRLE=0x02; //Synchronize + } + } + break; + + #endif + + /* If non timer pin, or unknown timer definition. */ + /* do a digital write */ + case NOT_ON_TIMER: + default: + if (val < 128) { + digitalWrite(pin, LOW); + } else { + digitalWrite(pin, HIGH); + } + break; + } } diff --git a/megaavr/cores/megatinycore/wiring_digital.c b/megaavr/cores/megatinycore/wiring_digital.c index 55619782..f4e50782 100644 --- a/megaavr/cores/megatinycore/wiring_digital.c +++ b/megaavr/cores/megatinycore/wiring_digital.c @@ -29,46 +29,46 @@ void pinMode(uint8_t pin, uint8_t mode) { - uint8_t bit_mask = digitalPinToBitMask(pin); + uint8_t bit_mask = digitalPinToBitMask(pin); - if ((bit_mask == NOT_A_PIN) || (mode > INPUT_PULLUP)) return; + if ((bit_mask == NOT_A_PIN) || (mode > INPUT_PULLUP)) return; - PORT_t* port = digitalPinToPortStruct(pin); - if(port == NULL) return; + PORT_t* port = digitalPinToPortStruct(pin); + if(port == NULL) return; - if(mode == OUTPUT){ + if(mode == OUTPUT){ - /* Configure direction as output */ - port->DIRSET = bit_mask; + /* Configure direction as output */ + port->DIRSET = bit_mask; - } else { /* mode == INPUT or INPUT_PULLUP */ + } else { /* mode == INPUT or INPUT_PULLUP */ - uint8_t bit_pos = digitalPinToBitPosition(pin); - /* Calculate where pin control register is */ - volatile uint8_t* pin_ctrl_reg = getPINnCTRLregister(port, bit_pos); + uint8_t bit_pos = digitalPinToBitPosition(pin); + /* Calculate where pin control register is */ + volatile uint8_t* pin_ctrl_reg = getPINnCTRLregister(port, bit_pos); - /* Save state */ - uint8_t status = SREG; - cli(); + /* Save state */ + uint8_t status = SREG; + cli(); - /* Configure direction as input */ - port->DIRCLR = bit_mask; + /* Configure direction as input */ + port->DIRCLR = bit_mask; - /* Configure pull-up resistor */ - if(mode == INPUT_PULLUP){ + /* Configure pull-up resistor */ + if(mode == INPUT_PULLUP){ - /* Enable pull-up */ - *pin_ctrl_reg |= PORT_PULLUPEN_bm; + /* Enable pull-up */ + *pin_ctrl_reg |= PORT_PULLUPEN_bm; - } else { /* mode == INPUT (no pullup) */ + } else { /* mode == INPUT (no pullup) */ - /* Disable pull-up */ - *pin_ctrl_reg &= ~(PORT_PULLUPEN_bm); - } + /* Disable pull-up */ + *pin_ctrl_reg &= ~(PORT_PULLUPEN_bm); + } - /* Restore state */ - SREG = status; - } + /* Restore state */ + SREG = status; + } } // Forcing this inline keeps the callers from having to push their own stuff @@ -87,144 +87,144 @@ void pinMode(uint8_t pin, uint8_t mode) //static inline void turnOffPWM(uint8_t timer) static void turnOffPWM(uint8_t pin) { - /* Actually turn off compare channel, not the timer */ - - /* Get pin's timer */ - uint8_t timer = digitalPinToTimer(pin); - if(timer == NOT_ON_TIMER) return; - - uint8_t bit_pos = digitalPinToBitPosition(pin); - //TCB_t *timerB; - - switch (timer) { - - /* TCA0 */ - case TIMERA0: - /* Bit position will give output channel */ - #ifdef __AVR_ATtinyxy2__ - if (bit_pos==7) bit_pos=0; //on the xy2, WO0 is on PA7 - #endif - if (bit_pos>2) bit_pos++; //there's a blank bit in the middle - /* Disable corresponding channel */ - TCA0.SPLIT.CTRLB &= ~(1 << (TCA_SPLIT_LCMP0EN_bp + bit_pos)); - break; - /* we don't need the type b timers as this core does not use them for PWM - //TCB - only one output - case TIMERB0: - case TIMERB1: - case TIMERB2: - case TIMERB3: - - timerB = (TCB_t *)&TCB0 + (timer - TIMERB0); - - //Disable TCB compare channel - timerB->CTRLB &= ~(TCB_CCMPEN_bm); - - break; - */ - #if defined(DAC0) - case DACOUT: - DAC0.CTRLA=0x00; - break; - #endif + /* Actually turn off compare channel, not the timer */ + + /* Get pin's timer */ + uint8_t timer = digitalPinToTimer(pin); + if(timer == NOT_ON_TIMER) return; + + uint8_t bit_pos = digitalPinToBitPosition(pin); + //TCB_t *timerB; + + switch (timer) { + + /* TCA0 */ + case TIMERA0: + /* Bit position will give output channel */ + #ifdef __AVR_ATtinyxy2__ + if (bit_pos==7) bit_pos=0; //on the xy2, WO0 is on PA7 + #endif + if (bit_pos>2) bit_pos++; //there's a blank bit in the middle + /* Disable corresponding channel */ + TCA0.SPLIT.CTRLB &= ~(1 << (TCA_SPLIT_LCMP0EN_bp + bit_pos)); + break; + /* we don't need the type b timers as this core does not use them for PWM + //TCB - only one output + case TIMERB0: + case TIMERB1: + case TIMERB2: + case TIMERB3: + + timerB = (TCB_t *)&TCB0 + (timer - TIMERB0); + + //Disable TCB compare channel + timerB->CTRLB &= ~(TCB_CCMPEN_bm); + + break; + */ + #if defined(DAC0) + case DACOUT: + DAC0.CTRLA=0x00; + break; + #endif #if (defined(TCD0) && defined(USE_TIMERD0_PWM)) - case TIMERD0: - // rigmarole that produces a glitch in the PWM - TCD0.CTRLA=0x10;//stop the timer - while(!(TCD0.STATUS&0x01)) {;}// wait until it's actually stopped - _PROTECTED_WRITE(TCD0.FAULTCTRL,TCD0.FAULTCTRL & ~(1<<(6+bit_pos))); - TCD0.CTRLA=0x11; //reenable it - break; - #endif - default: - break; - } + case TIMERD0: + // rigmarole that produces a glitch in the PWM + TCD0.CTRLA=0x10;//stop the timer + while(!(TCD0.STATUS&0x01)) {;}// wait until it's actually stopped + _PROTECTED_WRITE(TCD0.FAULTCTRL,TCD0.FAULTCTRL & ~(1<<(6+bit_pos))); + TCD0.CTRLA=0x11; //reenable it + break; + #endif + default: + break; + } } void digitalWrite(uint8_t pin, uint8_t val) { - /* Get bit mask for pin */ - uint8_t bit_mask = digitalPinToBitMask(pin); - if(bit_mask == NOT_A_PIN) return; + /* Get bit mask for pin */ + uint8_t bit_mask = digitalPinToBitMask(pin); + if(bit_mask == NOT_A_PIN) return; - /* Turn off PWM if applicable */ + /* Turn off PWM if applicable */ - // If the pin that support PWM output, we need to turn it off - // before doing a digital write. - turnOffPWM(pin); + // If the pin that support PWM output, we need to turn it off + // before doing a digital write. + turnOffPWM(pin); - /* Assuming the direction is already output !! */ + /* Assuming the direction is already output !! */ - /* Get port */ - PORT_t *port = digitalPinToPortStruct(pin); + /* Get port */ + PORT_t *port = digitalPinToPortStruct(pin); - /* Output direction */ - if(port->DIR & bit_mask){ + /* Output direction */ + if(port->DIR & bit_mask){ - /* Set output to value */ - if (val == LOW) { /* If LOW */ - port->OUTCLR = bit_mask; + /* Set output to value */ + if (val == LOW) { /* If LOW */ + port->OUTCLR = bit_mask; - } else if (val == CHANGE) { /* If TOGGLE */ - port->OUTTGL = bit_mask; - /* If HIGH OR > TOGGLE */ - } else { - port->OUTSET = bit_mask; - } + } else if (val == CHANGE) { /* If TOGGLE */ + port->OUTTGL = bit_mask; + /* If HIGH OR > TOGGLE */ + } else { + port->OUTSET = bit_mask; + } - /* Input direction */ - } else { - /* Old implementation has side effect when pin set as input - - pull up is enabled if this function is called. - Should we purposely implement this side effect? - */ + /* Input direction */ + } else { + /* Old implementation has side effect when pin set as input - + pull up is enabled if this function is called. + Should we purposely implement this side effect? + */ - /* Get bit position for getting pin ctrl reg */ - uint8_t bit_pos = digitalPinToBitPosition(pin); + /* Get bit position for getting pin ctrl reg */ + uint8_t bit_pos = digitalPinToBitPosition(pin); - /* Calculate where pin control register is */ - volatile uint8_t* pin_ctrl_reg = getPINnCTRLregister(port, bit_pos); + /* Calculate where pin control register is */ + volatile uint8_t* pin_ctrl_reg = getPINnCTRLregister(port, bit_pos); - /* Save system status and disable interrupts */ - uint8_t status = SREG; - cli(); + /* Save system status and disable interrupts */ + uint8_t status = SREG; + cli(); - if(val == LOW){ - /* Disable pullup */ - *pin_ctrl_reg &= ~PORT_PULLUPEN_bm; + if(val == LOW){ + /* Disable pullup */ + *pin_ctrl_reg &= ~PORT_PULLUPEN_bm; - } else { - /* Enable pull-up */ - *pin_ctrl_reg |= PORT_PULLUPEN_bm; - } + } else { + /* Enable pull-up */ + *pin_ctrl_reg |= PORT_PULLUPEN_bm; + } - /* Restore system status */ - SREG = status; - } + /* Restore system status */ + SREG = status; + } } int8_t digitalRead(uint8_t pin) { - /* Get bit mask and check valid pin */ - uint8_t bit_mask = digitalPinToBitMask(pin); - if(bit_mask == NOT_A_PIN) return -1; - - // If the pin that support PWM output, we need to turn it off - // before getting a digital reading. - // turnOffPWM(pin); - // And why do we "need" to do that? Do we have some obsessive - // need to make digitalRead slower than it needs to be?! - // or keep people from seeing their own PWM? - // digitalRead() should not change the output of the pin! - - /* Get port and check valid port */ - PORT_t *port = digitalPinToPortStruct(pin); - - /* Read pin value from PORTx.IN register */ - if(port->IN & bit_mask){ - return HIGH; - } else { - return LOW; - } + /* Get bit mask and check valid pin */ + uint8_t bit_mask = digitalPinToBitMask(pin); + if(bit_mask == NOT_A_PIN) return -1; + + // If the pin that support PWM output, we need to turn it off + // before getting a digital reading. + // turnOffPWM(pin); + // And why do we "need" to do that? Do we have some obsessive + // need to make digitalRead slower than it needs to be?! + // or keep people from seeing their own PWM? + // digitalRead() should not change the output of the pin! + + /* Get port and check valid port */ + PORT_t *port = digitalPinToPortStruct(pin); + + /* Read pin value from PORTx.IN register */ + if(port->IN & bit_mask){ + return HIGH; + } else { + return LOW; + } } diff --git a/megaavr/cores/megatinycore/wiring_shift.c b/megaavr/cores/megatinycore/wiring_shift.c index 848c5e91..f4c41559 100644 --- a/megaavr/cores/megatinycore/wiring_shift.c +++ b/megaavr/cores/megatinycore/wiring_shift.c @@ -27,56 +27,56 @@ Note that faster implementations are possible (MUCH faster, such that they might cause compatibility problems because they're too fast) See #197 uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) { - uint8_t value = 0; - uint8_t i; + uint8_t value = 0; + uint8_t i; - for (i = 0; i < 8; ++i) { - digitalWrite(clockPin, HIGH); - if (bitOrder == LSBFIRST) - value |= digitalRead(dataPin) << i; - else - value |= digitalRead(dataPin) << (7 - i); - digitalWrite(clockPin, LOW); - } - return value; + for (i = 0; i < 8; ++i) { + digitalWrite(clockPin, HIGH); + if (bitOrder == LSBFIRST) + value |= digitalRead(dataPin) << i; + else + value |= digitalRead(dataPin) << (7 - i); + digitalWrite(clockPin, LOW); + } + return value; } void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) { - uint8_t i; + uint8_t i; - for (i = 0; i < 8; i++) { - if (bitOrder == LSBFIRST) - digitalWrite(dataPin, !!(val & (1 << i))); - else - digitalWrite(dataPin, !!(val & (1 << (7 - i)))); + for (i = 0; i < 8; i++) { + if (bitOrder == LSBFIRST) + digitalWrite(dataPin, !!(val & (1 << i))); + else + digitalWrite(dataPin, !!(val & (1 << (7 - i)))); - digitalWrite(clockPin, HIGH); - digitalWrite(clockPin, LOW); - } + digitalWrite(clockPin, HIGH); + digitalWrite(clockPin, LOW); + } } */ uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) { - uint8_t val = 0; - for (uint8_t i = 0; i != 8; i++) { - digitalWrite(clockPin, HIGH); - if (bitOrder == LSBFIRST) - val = (val>>1) | (digitalRead(dataPin)<<7); - else - val = (val<<1) | digitalRead(dataPin); - digitalWrite(clockPin, LOW); - } - return val; + uint8_t val = 0; + for (uint8_t i = 0; i != 8; i++) { + digitalWrite(clockPin, HIGH); + if (bitOrder == LSBFIRST) + val = (val>>1) | (digitalRead(dataPin)<<7); + else + val = (val<<1) | digitalRead(dataPin); + digitalWrite(clockPin, LOW); + } + return val; } void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) { - for (uint8_t i = 0; i != 8; i++) { - if (bitOrder == LSBFIRST) - digitalWrite(dataPin, val & 0x01), val >>= 1; - else - digitalWrite(dataPin, !!(val & 0x80)), val <<= 1; + for (uint8_t i = 0; i != 8; i++) { + if (bitOrder == LSBFIRST) + digitalWrite(dataPin, val & 0x01), val >>= 1; + else + digitalWrite(dataPin, !!(val & 0x80)), val <<= 1; - digitalWrite(clockPin, HIGH); - digitalWrite(clockPin, LOW); - } + digitalWrite(clockPin, HIGH); + digitalWrite(clockPin, LOW); + } } diff --git a/megaavr/libraries/SD/src/README.txt b/megaavr/libraries/SD/src/README.txt index 495ea4c7..fedccdde 100644 --- a/megaavr/libraries/SD/src/README.txt +++ b/megaavr/libraries/SD/src/README.txt @@ -1,4 +1,3 @@ - ** SD - a slightly more friendly wrapper for sdfatlib ** This library aims to expose a subset of SD card functionality in the