Add debug prints on error conditions to the core

parent 96e31c64
......@@ -35,11 +35,13 @@ bool SerialUART::setRX(pin_size_t rx) {
constexpr uint32_t valid[2] = { __bitset({1, 13, 17, 29}) /* UART0 */,
__bitset({5, 9, 21, 25}) /* UART1 */};
if (_running) {
DEBUGCORE("ERROR: SerialUART setRX while running\n");
return false;
} else if ((1 << rx) & valid[uart_get_index(_uart)]) {
_rx = rx;
return true;
} else {
DEBUGCORE("ERROR: SerialUART setRX illegal pin (%d)\n", rx);
return false;
}
}
......@@ -48,11 +50,13 @@ bool SerialUART::setTX(pin_size_t tx) {
constexpr uint32_t valid[2] = { __bitset({0, 12, 16, 28}) /* UART0 */,
__bitset({4, 8, 20, 24}) /* UART1 */};
if (_running) {
DEBUGCORE("ERROR: SerialUART setTX while running\n");
return false;
} else if ((1 << tx) & valid[uart_get_index(_uart)]) {
_tx = tx;
return true;
} else {
DEBUGCORE("ERROR: SerialUART setTX illegal pin (%d)\n", tx);
return false;
}
}
......
......@@ -30,12 +30,15 @@ typedef struct {
} Tone;
#include "tone.pio.h"
PIOProgram _tonePgm(&tone_program);
static PIOProgram _tonePgm(&tone_program);
static std::map<pin_size_t, Tone *> _toneMap;
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) {
noTone(pin);
return;
......@@ -50,13 +53,13 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
if (entry != _toneMap.end()) {
noTone(pin);
}
DEBUGCORE("TONE: phaseUS=%d, phaseCnt=%d\n", us, phases);
auto newTone = new Tone();
newTone->pin = pin;
pinMode(pin, OUTPUT);
int 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
delete newTone;
return;
......@@ -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_put_blocking(newTone->pio, newTone->sm, phases);
DEBUGCORE("TONE: Began on pio=%p, sm=%d\n", newTone->pio, newTone->sm);
_toneMap.insert({pin, newTone});
}
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);
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_unclaim(entry->second->pio, entry->second->sm);
_toneMap.erase(entry);
......
......@@ -16,33 +16,25 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
extern "C" {
#include "stdlib.h"
#include "stdint.h"
}
#include "stdlib.h"
#include "stdint.h"
void randomSeed( uint32_t dwSeed )
{
if ( dwSeed != 0 )
{
void randomSeed( uint32_t dwSeed ) {
if ( dwSeed != 0 ) {
srand( dwSeed ) ;
}
}
long random( long howbig )
{
if ( howbig == 0 )
{
long random( long howbig ) {
if ( howbig == 0 ) {
return 0 ;
}
return rand() % howbig;
}
long random( long howsmall, long howbig )
{
if (howsmall >= howbig)
{
long random( long howsmall, long howbig ) {
if (howsmall >= howbig) {
return howsmall;
}
......
......@@ -21,8 +21,7 @@
#include <pico.h>
#include <pico/time.h>
extern "C" void delay( unsigned long ms )
{
extern "C" void delay( unsigned long ms ) {
if (!ms) {
return;
}
......@@ -30,8 +29,7 @@ extern "C" void delay( unsigned long ms )
sleep_ms(ms);
}
extern "C" void delayMicroseconds( unsigned int usec )
{
extern "C" void delayMicroseconds( unsigned int usec ) {
if (!usec) {
return;
}
......
......@@ -29,14 +29,17 @@ static int32_t analogScale = 255;
static uint32_t analogMap = 0;
static uint16_t analogFreq = 1000;
static bool pwmInitted = false;
static bool adcInitted = false;
extern "C" void analogWriteFreq(uint32_t freq) {
if (freq == analogFreq) {
return;
}
if (freq < 100) {
DEBUGCORE("ERROR: analogWriteFreq too low (%d)\n", freq);
analogFreq = 100;
} else if (freq > 60000) {
DEBUGCORE("ERROR: analogWriteFreq too high (%d)\n", freq);
analogFreq = 60000;
} else {
analogFreq = freq;
......@@ -51,16 +54,24 @@ extern "C" void analogWriteRange(uint32_t range) {
if ((range >= 15) && (range <= 65535)) {
analogScale = range;
pwmInitted = false;
}
} else {
DEBUGCORE("ERROR: analogWriteRange out of range (%d)\n", range);
}
}
extern "C" void analogWriteResolution(int res) {
if ((res >= 4) && (res <= 16)) {
analogWriteRange((1 << res) - 1);
} else {
DEBUGCORE("ERROR: analogWriteResolution out of range (%d)\n", res);
}
}
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) {
pwm_config c = pwm_get_default_config();
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) {
pwm_set_gpio_level(pin, val);
}
static bool adcInitted = false;
extern "C" int analogRead(pin_size_t pinNumber) {
if ((pinNumber < A0) || (pinNumber > A3)) {
extern "C" int analogRead(pin_size_t pin) {
if ((pin < A0) || (pin > A3)) {
DEBUGCORE("ERROR: Illegal analogRead pin (%d)\n", pin);
return 0;
}
if (!adcInitted) {
adc_init();
}
adc_gpio_init(pinNumber);
adc_select_input(pinNumber - A0);
adc_gpio_init(pin);
adc_select_input(pin - A0);
return adc_read();
}
......
......@@ -46,13 +46,23 @@ extern "C" void pinMode( pin_size_t ulPin, PinMode ulMode ) {
gpio_set_dir(ulPin, true);
break;
default:
DEBUGCORE("ERROR: Illegal pinMode mode (%d)\n", ulMode);
// Error
return;
}
if ((ulPin < 0) || (ulPin > 29)) {
DEBUGCORE("ERROR: Illegal pin in pinMode (%d)\n", ulPin);
return;
}
_pm[ulPin] = ulMode;
}
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 (ulVal == LOW) {
gpio_set_dir(ulPin, false);
......@@ -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;
}
......@@ -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 abort = start + timeout;
if ((pin < 0) || (pin > 29)) {
DEBUGCORE("ERROR: Illegal pin in pulseIn (%d)\n", pin);
return 0;
}
// Wait for deassert, if needed
while ((!!gpio_get(pin) != !state) && (time_us_64() < abort) );
if (time_us_64() >= abort) return 0;
......
......@@ -24,6 +24,14 @@
extern "C" uint8_t shiftIn(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder) {
uint8_t value = 0;
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) {
digitalWrite(clockPin, HIGH);
if (bitOrder == LSBFIRST)
......@@ -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) {
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++) {
if (bitOrder == LSBFIRST)
digitalWrite(dataPin, !!(val & (1 << i)));
......
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