/*
* (RP2040 Processor) ** (RP2040 Processor) **
*
* WARNING: on WOKWI, for the Raspberry Pi Pico
* ONLY A SINGLE CORE IS SIMULATED
*
* (RP2040 Processor) ** (RP0240 Processor) **
*
*
*
* RP2040_arduino_PWM_4.ino
*
*
* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
*
*
* NOTE: -ALL- of the following functions are included in the
* Pi Pico C++ SDK, and therefore also included in Arduino
*
* gpio_get_all();
* gpio_set_mask(mask);
* gpio_clr_mask(mask);
*
* NOTE: -ALL- of the above functions are included in the
* Pi Pico C++ SDK, and therefore also included in Arduino
*
*
* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
*
*
* -----------------------------------------------------------------------------------------------------------------------
*
*
* https://arduino-pico.readthedocs.io/en/latest/analog.html
*
* Analog Outputs
* The RP2040 does not have any onboard DACs, so analog outputs are simulated
* using the standard method of using pulse width modulation (PWM) using
* the RP2040’s hardware PWM units.
* While up to 16 PWM channels can be generated, they are not independent
* and there are significant restrictions as to allowed pins in parallel.
* See the RP2040 datasheet for full details.
*
* Analog Output Restrictions
* The PWM generator source clock restricts the legal combinations
* of frequency and ranges. At a CPU frequency of 133MHz, the 16 bit
* maximum range decreases by 1 bit for every doubling of the default
* PWM frequency of 1 kHz. For example, at 1MHz only about 6 bits
* of range are possible. When you define an analogWriteFreq and
* analogWriteRange that can’t be fulfilled by the hardware, the frequency
* will be preserved but the accuracy (range) will be reduced automatically.
* Your code will still send in the range you specify, but the core itself
* will transparently map it into the allowable PWN range.
*
* void analogWriteFreq(uint32_t freq)
* Sets the master PWM frequency used (i.e. how often the PWM output cycles).
* From 100Hz to 1MHz are supported.
*
* void analogWriteRange(uint32_t range) and analogWriteResolution(int res)
* These calls set the maximum PWM value (i.e. writing this value will result
* in a PWM duty cycle of 100%)/ either explicitly (range) or as a power-of-two
* (res). A range of 16 to 65535 is supported.
*
* void analogWrite(pin_size_t pin, int val)
* Writes a PWM value to a specific pin. The PWM machine is enabled and set
* to the requested frequency and scale, and the output is generated.
* This will continue until a digitalWrite or other digital output is performed.
*
*
* -----------------------------------------------------------------------------------------------------------------------
*
*
*/
/*
* (RP2040 Processor) ** (RP2040 Processor) **
*
* WARNING: on WOKWI, for the Raspberry Pi Pico
* ONLY A SINGLE CORE IS SIMULATED
*
* (RP2040 Processor) ** (RP2040 Processor) **
*
*/
//
// WARNING: the code using the RP2040 multicore property, is about
// the "Arduino MBED" core, NOT the "earlephilhower" core
//
//
const uint8_t Gk_pin_RedLed8 = 9; // GPIO9 --> OUT PWM (to RED LED)
const uint8_t Gk_pin_LOADpwm8 = 10; // GPIO10 --> OUT PWM (to LOAD driver)
const uint8_t Gk_pin_LAMPpwm8 = 12; // GPIO12 --> OUT PWM (to LED Lamp driver module)
//
const uint32_t Gk_LED_mask32 = 1ul << Gk_pin_RedLed8;
const uint32_t Gk_LOAD_mask32 = 1ul << Gk_pin_LOADpwm8;
const uint32_t Gk_LAMP_mask32 = 1ul << Gk_pin_LAMPpwm8;
//
// Implement PWM at a given frequency (base_freq) allowing for modulation
// Frequencies are in milliHz, times are in microseconds;
//
// Start with 5KHz (EQUAL to 200 uSec.)
uint16_t Gk_pwm_FREQ16 = 5000; // EQUAL to 200 uSec.
// Note: 976Hz have a PERIOD of 200 uSec.
uint16_t Gk_PWMperiod16 = 200; // Assume 10 bit ADC values
//
// NOTE: the light emitted by the LED lamp depends on the value of -Gi_LEDon16-
uint16_t Gi_LEDon16 = 512; // half of 1024
// NOTE: the light emitted by the LED lamp depends on the value of -Gi_LEDon16-
//
/*
* (RP2040 Processor) ** (RP2040 Processor) **
*
* WARNING: on WOKWI, for the Raspberry Pi Pico
* ONLY A SINGLE CORE IS SIMULATED
*
* (RP2040 Processor) ** (RP2040 Processor) **
*
*/
// ======================================================================================
// start of functions prototypes declarations
//
uint8_t readGPIOport(uint8_t, uint32_t);
void writeGPIOport(uint32_t, bool);
//
// end of functions prototypes declarations
// ======================================================================================
// *******************************************
uint8_t readGPIOport(uint8_t gpio, uint32_t GPIOmask32) {
//
// overlay the mask over all GPIO states ('bitwise' AND operator)
uint32_t GPIOstates32 = gpio_get_all() & GPIOmask32; // Pi Pico C++ SDK
// read the state of the bit at position -gpio-
return bitRead(GPIOstates32, gpio); // returns 0 or 1
//
} // readGPIOport()
void writeGPIOport(uint32_t GPIOmask32, bool value) {
//
// Pi Pico C++ SDK
if (value) gpio_set_mask(GPIOmask32); // set to ONE the bit at position -gpio-
else gpio_clr_mask(GPIOmask32); // set to ZERO the bit at position -gpio-
//
} // writeGPIOport()
/*
* (RP2040 Processor) ** (RP2040 Processor) **
*
* WARNING: on WOKWI, for the Raspberry Pi Pico
* ONLY A SINGLE CORE IS SIMULATED
*
* (RP2040 Processor) ** (RP2040 Processor) **
*
*/
void setup() {
//
Serial1.begin(115200);
//
Serial1.println("** setup() - START");
//
pinMode(Gk_pin_RedLed8, OUTPUT); // Enable output on pin GPIO9
//
//digitalWrite(Gk_pin_RedLed8, 0);
writeGPIOport(Gk_LED_mask32, false); // GPIO9 RED LED Off
//
} // setup()
void loop() {
// as usual, Arduino run in the first core [[ core-0 ]]
//
writeGPIOport(Gk_LED_mask32, true); // LED lit
Serial1.print("\nRED LED = ");Serial1.println(readGPIOport(Gk_pin_RedLed8, Gk_LED_mask32));
delay(400);
//
writeGPIOport(Gk_LED_mask32, false); // LED off
Serial1.print("RED LED = ");Serial1.println(readGPIOport(Gk_pin_RedLed8, Gk_LED_mask32));
delay(400);
//
} // loop()
/*
* (RP2040 Processor) ** (RP2040 Processor) **
*
* WARNING: on WOKWI, for the Raspberry Pi Pico
* ONLY A SINGLE CORE IS SIMULATED
*
* (RP2040 Processor) ** (RP2040 Processor) **
*
*/