Add debug prints on error conditions to the core

parent 96e31c64
...@@ -35,11 +35,13 @@ bool SerialUART::setRX(pin_size_t rx) { ...@@ -35,11 +35,13 @@ bool SerialUART::setRX(pin_size_t rx) {
constexpr uint32_t valid[2] = { __bitset({1, 13, 17, 29}) /* UART0 */, constexpr uint32_t valid[2] = { __bitset({1, 13, 17, 29}) /* UART0 */,
__bitset({5, 9, 21, 25}) /* UART1 */}; __bitset({5, 9, 21, 25}) /* UART1 */};
if (_running) { if (_running) {
DEBUGCORE("ERROR: SerialUART setRX while running\n");
return false; return false;
} else if ((1 << rx) & valid[uart_get_index(_uart)]) { } else if ((1 << rx) & valid[uart_get_index(_uart)]) {
_rx = rx; _rx = rx;
return true; return true;
} else { } else {
DEBUGCORE("ERROR: SerialUART setRX illegal pin (%d)\n", rx);
return false; return false;
} }
} }
...@@ -48,11 +50,13 @@ bool SerialUART::setTX(pin_size_t tx) { ...@@ -48,11 +50,13 @@ bool SerialUART::setTX(pin_size_t tx) {
constexpr uint32_t valid[2] = { __bitset({0, 12, 16, 28}) /* UART0 */, constexpr uint32_t valid[2] = { __bitset({0, 12, 16, 28}) /* UART0 */,
__bitset({4, 8, 20, 24}) /* UART1 */}; __bitset({4, 8, 20, 24}) /* UART1 */};
if (_running) { if (_running) {
DEBUGCORE("ERROR: SerialUART setTX while running\n");
return false; return false;
} else if ((1 << tx) & valid[uart_get_index(_uart)]) { } else if ((1 << tx) & valid[uart_get_index(_uart)]) {
_tx = tx; _tx = tx;
return true; return true;
} else { } else {
DEBUGCORE("ERROR: SerialUART setTX illegal pin (%d)\n", tx);
return false; return false;
} }
} }
......
...@@ -30,12 +30,15 @@ typedef struct { ...@@ -30,12 +30,15 @@ typedef struct {
} Tone; } Tone;
#include "tone.pio.h" #include "tone.pio.h"
PIOProgram _tonePgm(&tone_program); static PIOProgram _tonePgm(&tone_program);
static std::map<pin_size_t, Tone *> _toneMap; static std::map<pin_size_t, Tone *> _toneMap;
void tone(uint8_t pin, unsigned int frequency, unsigned long duration) { void tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
DEBUGCORE("TONE: tone(%d, %d, %d)\n", pin, frequency, duration); if ((pin < 0) || (pin > 29)) {
DEBUGCORE("ERROR: Illegal pin in tone (%d)\n", pin);
return;
}
if (!frequency) { if (!frequency) {
noTone(pin); noTone(pin);
return; return;
...@@ -50,13 +53,13 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) { ...@@ -50,13 +53,13 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
if (entry != _toneMap.end()) { if (entry != _toneMap.end()) {
noTone(pin); noTone(pin);
} }
DEBUGCORE("TONE: phaseUS=%d, phaseCnt=%d\n", us, phases);
auto newTone = new Tone(); auto newTone = new Tone();
newTone->pin = pin; newTone->pin = pin;
pinMode(pin, OUTPUT); pinMode(pin, OUTPUT);
int off; int off;
if (!_tonePgm.prepare(&newTone->pio, &newTone->sm, &off)) { if (!_tonePgm.prepare(&newTone->pio, &newTone->sm, &off)) {
DEBUGCORE("TONE: Unable to start, out of PIO resources\n"); DEBUGCORE("ERROR: tone unable to start, out of PIO resources\n");
// ERROR, no free slots // ERROR, no free slots
delete newTone; delete newTone;
return; return;
...@@ -69,15 +72,16 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) { ...@@ -69,15 +72,16 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
pio_sm_set_enabled(newTone->pio, newTone->sm, true); pio_sm_set_enabled(newTone->pio, newTone->sm, true);
pio_sm_put_blocking(newTone->pio, newTone->sm, phases); pio_sm_put_blocking(newTone->pio, newTone->sm, phases);
DEBUGCORE("TONE: Began on pio=%p, sm=%d\n", newTone->pio, newTone->sm);
_toneMap.insert({pin, newTone}); _toneMap.insert({pin, newTone});
} }
void noTone(uint8_t pin) { void noTone(uint8_t pin) {
DEBUGCORE("NOTONE: noTone(%d)\n", pin); if ((pin < 0) || (pin > 29)) {
DEBUGCORE("ERROR: Illegal pin in tone (%d)\n", pin);
return;
}
auto entry = _toneMap.find(pin); auto entry = _toneMap.find(pin);
if (entry != _toneMap.end()) { if (entry != _toneMap.end()) {
DEBUGCORE("NOTONE: Disabling PIO tone generator pio=%p, sm=%d\n", entry->second->pio, entry->second->sm);
pio_sm_set_enabled(entry->second->pio, entry->second->sm, false); pio_sm_set_enabled(entry->second->pio, entry->second->sm, false);
pio_sm_unclaim(entry->second->pio, entry->second->sm); pio_sm_unclaim(entry->second->pio, entry->second->sm);
_toneMap.erase(entry); _toneMap.erase(entry);
......
...@@ -16,33 +16,25 @@ ...@@ -16,33 +16,25 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
extern "C" { #include "stdlib.h"
#include "stdlib.h" #include "stdint.h"
#include "stdint.h"
}
void randomSeed( uint32_t dwSeed ) void randomSeed( uint32_t dwSeed ) {
{ if ( dwSeed != 0 ) {
if ( dwSeed != 0 )
{
srand( dwSeed ) ; srand( dwSeed ) ;
} }
} }
long random( long howbig ) long random( long howbig ) {
{ if ( howbig == 0 ) {
if ( howbig == 0 )
{
return 0 ; return 0 ;
} }
return rand() % howbig; return rand() % howbig;
} }
long random( long howsmall, long howbig ) long random( long howsmall, long howbig ) {
{ if (howsmall >= howbig) {
if (howsmall >= howbig)
{
return howsmall; return howsmall;
} }
......
...@@ -21,8 +21,7 @@ ...@@ -21,8 +21,7 @@
#include <pico.h> #include <pico.h>
#include <pico/time.h> #include <pico/time.h>
extern "C" void delay( unsigned long ms ) extern "C" void delay( unsigned long ms ) {
{
if (!ms) { if (!ms) {
return; return;
} }
...@@ -30,8 +29,7 @@ extern "C" void delay( unsigned long ms ) ...@@ -30,8 +29,7 @@ extern "C" void delay( unsigned long ms )
sleep_ms(ms); sleep_ms(ms);
} }
extern "C" void delayMicroseconds( unsigned int usec ) extern "C" void delayMicroseconds( unsigned int usec ) {
{
if (!usec) { if (!usec) {
return; return;
} }
......
...@@ -29,14 +29,17 @@ static int32_t analogScale = 255; ...@@ -29,14 +29,17 @@ static int32_t analogScale = 255;
static uint32_t analogMap = 0; static uint32_t analogMap = 0;
static uint16_t analogFreq = 1000; static uint16_t analogFreq = 1000;
static bool pwmInitted = false; static bool pwmInitted = false;
static bool adcInitted = false;
extern "C" void analogWriteFreq(uint32_t freq) { extern "C" void analogWriteFreq(uint32_t freq) {
if (freq == analogFreq) { if (freq == analogFreq) {
return; return;
} }
if (freq < 100) { if (freq < 100) {
DEBUGCORE("ERROR: analogWriteFreq too low (%d)\n", freq);
analogFreq = 100; analogFreq = 100;
} else if (freq > 60000) { } else if (freq > 60000) {
DEBUGCORE("ERROR: analogWriteFreq too high (%d)\n", freq);
analogFreq = 60000; analogFreq = 60000;
} else { } else {
analogFreq = freq; analogFreq = freq;
...@@ -51,16 +54,24 @@ extern "C" void analogWriteRange(uint32_t range) { ...@@ -51,16 +54,24 @@ extern "C" void analogWriteRange(uint32_t range) {
if ((range >= 15) && (range <= 65535)) { if ((range >= 15) && (range <= 65535)) {
analogScale = range; analogScale = range;
pwmInitted = false; pwmInitted = false;
} } else {
DEBUGCORE("ERROR: analogWriteRange out of range (%d)\n", range);
}
} }
extern "C" void analogWriteResolution(int res) { extern "C" void analogWriteResolution(int res) {
if ((res >= 4) && (res <= 16)) { if ((res >= 4) && (res <= 16)) {
analogWriteRange((1 << res) - 1); analogWriteRange((1 << res) - 1);
} else {
DEBUGCORE("ERROR: analogWriteResolution out of range (%d)\n", res);
} }
} }
extern "C" void analogWrite(pin_size_t pin, int val) { extern "C" void analogWrite(pin_size_t pin, int val) {
if ((pin < 0) || (pin > 29)) {
DEBUGCORE("ERROR: Illegal analogWrite pin (%d)\n", pin);
return;
}
if (!pwmInitted) { if (!pwmInitted) {
pwm_config c = pwm_get_default_config(); pwm_config c = pwm_get_default_config();
pwm_config_set_clkdiv( &c, clock_get_hz(clk_sys) / (float) (analogScale * analogFreq) ); pwm_config_set_clkdiv( &c, clock_get_hz(clk_sys) / (float) (analogScale * analogFreq) );
...@@ -81,16 +92,16 @@ extern "C" void analogWrite(pin_size_t pin, int val) { ...@@ -81,16 +92,16 @@ extern "C" void analogWrite(pin_size_t pin, int val) {
pwm_set_gpio_level(pin, val); pwm_set_gpio_level(pin, val);
} }
static bool adcInitted = false; extern "C" int analogRead(pin_size_t pin) {
extern "C" int analogRead(pin_size_t pinNumber) { if ((pin < A0) || (pin > A3)) {
if ((pinNumber < A0) || (pinNumber > A3)) { DEBUGCORE("ERROR: Illegal analogRead pin (%d)\n", pin);
return 0; return 0;
} }
if (!adcInitted) { if (!adcInitted) {
adc_init(); adc_init();
} }
adc_gpio_init(pinNumber); adc_gpio_init(pin);
adc_select_input(pinNumber - A0); adc_select_input(pin - A0);
return adc_read(); return adc_read();
} }
......
...@@ -46,13 +46,23 @@ extern "C" void pinMode( pin_size_t ulPin, PinMode ulMode ) { ...@@ -46,13 +46,23 @@ extern "C" void pinMode( pin_size_t ulPin, PinMode ulMode ) {
gpio_set_dir(ulPin, true); gpio_set_dir(ulPin, true);
break; break;
default: default:
DEBUGCORE("ERROR: Illegal pinMode mode (%d)\n", ulMode);
// Error // Error
return; return;
} }
if ((ulPin < 0) || (ulPin > 29)) {
DEBUGCORE("ERROR: Illegal pin in pinMode (%d)\n", ulPin);
return;
}
_pm[ulPin] = ulMode; _pm[ulPin] = ulMode;
} }
extern "C" void digitalWrite( pin_size_t ulPin, PinStatus ulVal ) { extern "C" void digitalWrite( pin_size_t ulPin, PinStatus ulVal ) {
if ((ulPin < 0) || (ulPin > 29)) {
DEBUGCORE("ERROR: Illegal pin in pinMode (%d)\n", ulPin);
return;
}
if (_pm[ulPin] == INPUT_PULLDOWN) { if (_pm[ulPin] == INPUT_PULLDOWN) {
if (ulVal == LOW) { if (ulVal == LOW) {
gpio_set_dir(ulPin, false); gpio_set_dir(ulPin, false);
...@@ -70,8 +80,11 @@ extern "C" void digitalWrite( pin_size_t ulPin, PinStatus ulVal ) { ...@@ -70,8 +80,11 @@ extern "C" void digitalWrite( pin_size_t ulPin, PinStatus ulVal ) {
} }
} }
extern "C" PinStatus digitalRead( pin_size_t ulPin ) extern "C" PinStatus digitalRead( pin_size_t ulPin ) {
{ if ((ulPin < 0) || (ulPin > 29)) {
DEBUGCORE("ERROR: Illegal pin in digitalRead (%d)\n", ulPin);
return LOW;
}
return gpio_get(ulPin) ? HIGH : LOW; return gpio_get(ulPin) ? HIGH : LOW;
} }
...@@ -27,6 +27,11 @@ extern "C" unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeo ...@@ -27,6 +27,11 @@ extern "C" unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeo
uint64_t start = time_us_64(); uint64_t start = time_us_64();
uint64_t abort = start + timeout; uint64_t abort = start + timeout;
if ((pin < 0) || (pin > 29)) {
DEBUGCORE("ERROR: Illegal pin in pulseIn (%d)\n", pin);
return 0;
}
// Wait for deassert, if needed // Wait for deassert, if needed
while ((!!gpio_get(pin) != !state) && (time_us_64() < abort) ); while ((!!gpio_get(pin) != !state) && (time_us_64() < abort) );
if (time_us_64() >= abort) return 0; if (time_us_64() >= abort) return 0;
......
...@@ -24,6 +24,14 @@ ...@@ -24,6 +24,14 @@
extern "C" uint8_t shiftIn(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder) { extern "C" uint8_t shiftIn(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder) {
uint8_t value = 0; uint8_t value = 0;
uint8_t i; uint8_t i;
if ((dataPin < 0) || (dataPin > 29)) {
DEBUGCORE("ERROR: Illegal dataPin in shiftIn (%d)\n", dataPin);
return 0;
}
if ((clockPin < 0) || (clockPin > 29)) {
DEBUGCORE("ERROR: Illegal clockPin in shiftIn (%d)\n", clockPin);
return 0;
}
for (i = 0; i < 8; ++i) { for (i = 0; i < 8; ++i) {
digitalWrite(clockPin, HIGH); digitalWrite(clockPin, HIGH);
if (bitOrder == LSBFIRST) if (bitOrder == LSBFIRST)
...@@ -37,6 +45,14 @@ extern "C" uint8_t shiftIn(pin_size_t dataPin, pin_size_t clockPin, BitOrder bit ...@@ -37,6 +45,14 @@ extern "C" uint8_t shiftIn(pin_size_t dataPin, pin_size_t clockPin, BitOrder bit
extern "C" void shiftOut(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder, uint8_t val) { extern "C" void shiftOut(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder, uint8_t val) {
uint8_t i; uint8_t i;
if ((dataPin < 0) || (dataPin > 29)) {
DEBUGCORE("ERROR: Illegal dataPin in shiftOut (%d)\n", dataPin);
return;
}
if ((clockPin < 0) || (clockPin > 29)) {
DEBUGCORE("ERROR: Illegal clockPin in shiftOut (%d)\n", clockPin);
return;
}
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
if (bitOrder == LSBFIRST) if (bitOrder == LSBFIRST)
digitalWrite(dataPin, !!(val & (1 << i))); digitalWrite(dataPin, !!(val & (1 << i)));
......
BOOTSEL KEYWORD1 BOOTSEL KEYWORD1
readAnalogTemp KEYWORD1
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment