Commit 82f69827 authored by lcgamboa's avatar lcgamboa

new: Add support to PWM outputs for ESP32C3 and STM32 boards

parent 0e883416
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -1141,7 +1141,7 @@ void bsim_qemu::MReset(int flags) { ...@@ -1141,7 +1141,7 @@ void bsim_qemu::MReset(int flags) {
bitbang_uart_rst(&master_uart[0]); bitbang_uart_rst(&master_uart[0]);
bitbang_uart_rst(&master_uart[1]); bitbang_uart_rst(&master_uart[1]);
bitbang_uart_rst(&master_uart[2]); bitbang_uart_rst(&master_uart[2]);
bitbang_pwm_rst(&ledc); bitbang_pwm_rst(&pwm_out);
} }
const picpin* bsim_qemu::MGetPinsValues(void) { const picpin* bsim_qemu::MGetPinsValues(void) {
...@@ -1204,10 +1204,10 @@ void bsim_qemu::MStep(void) { ...@@ -1204,10 +1204,10 @@ void bsim_qemu::MStep(void) {
} }
} }
} }
for (int i = 0; i < ledc.chanels; i++) { for (int i = 0; i < pwm_out.chanels; i++) {
if (ledc.pins[i]) { if (pwm_out.pins[i]) {
pins[ledc.pins[i] - 1].dir = PD_OUT; pins[pwm_out.pins[i] - 1].dir = PD_OUT;
pins[ledc.pins[i] - 1].value = ledc.out[i]; pins[pwm_out.pins[i] - 1].value = pwm_out.out[i];
} }
} }
} }
......
...@@ -72,7 +72,7 @@ public: ...@@ -72,7 +72,7 @@ public:
bitbang_i2c_t master_i2c[2]; bitbang_i2c_t master_i2c[2];
bitbang_spi_t master_spi[2]; bitbang_spi_t master_spi[2];
bitbang_uart_t master_uart[3]; bitbang_uart_t master_uart[3];
bitbang_pwm_t ledc; bitbang_pwm_t pwm_out;
void IoLockAccess(void) override; void IoLockAccess(void) override;
void IoUnlockAccess(void) override; void IoUnlockAccess(void) override;
int GetUARTRX(const int uart_num) override; int GetUARTRX(const int uart_num) override;
......
...@@ -57,8 +57,6 @@ cpart_leds::cpart_leds(const unsigned x, const unsigned y, const char* name, con ...@@ -57,8 +57,6 @@ cpart_leds::cpart_leds(const unsigned x, const unsigned y, const char* name, con
OWidth = Width; OWidth = Width;
OHeight = Height; OHeight = Height;
ChangeSize(8);
input_pins[0] = 0; input_pins[0] = 0;
input_pins[1] = 0; input_pins[1] = 0;
input_pins[2] = 0; input_pins[2] = 0;
...@@ -68,6 +66,8 @@ cpart_leds::cpart_leds(const unsigned x, const unsigned y, const char* name, con ...@@ -68,6 +66,8 @@ cpart_leds::cpart_leds(const unsigned x, const unsigned y, const char* name, con
input_pins[6] = 0; input_pins[6] = 0;
input_pins[7] = 0; input_pins[7] = 0;
ChangeSize(8);
colors[0] = 0; colors[0] = 0;
colors[1] = 0; colors[1] = 0;
colors[2] = 0; colors[2] = 0;
...@@ -235,9 +235,11 @@ void cpart_leds::ReadPreferences(lxString value) { ...@@ -235,9 +235,11 @@ void cpart_leds::ReadPreferences(lxString value) {
void cpart_leds::RegisterRemoteControl(void) { void cpart_leds::RegisterRemoteControl(void) {
const picpin* ppins = SpareParts.GetPinsValues(); const picpin* ppins = SpareParts.GetPinsValues();
for (int i = 0; i > 8; i++) { for (int i = 0; i < 8; i++) {
if (input_pins[i]) { if (input_pins[i]) {
output_ids[O_L1 + i]->status = (void*)&ppins[input_pins[i] - 1].oavalue; output_ids[O_L1 + i]->status = (void*)&ppins[input_pins[i] - 1].oavalue;
} else {
output_ids[O_L1 + i]->status = NULL;
} }
} }
} }
...@@ -337,6 +339,7 @@ void cpart_leds::ChangeSize(const unsigned int sz) { ...@@ -337,6 +339,7 @@ void cpart_leds::ChangeSize(const unsigned int sz) {
outputc = Size * 2; outputc = Size * 2;
LoadImage(); LoadImage();
} }
RegisterRemoteControl();
} }
void cpart_leds::LoadImage(void) { void cpart_leds::LoadImage(void) {
......
...@@ -93,6 +93,9 @@ void CPWindow5::_EvOnCreate(CControl* control) { ...@@ -93,6 +93,9 @@ void CPWindow5::_EvOnCreate(CControl* control) {
? (lxT("PICSimLab[") + itoa(PICSimLab.GetInstanceNumber()) + lxT("] - ")) ? (lxT("PICSimLab[") + itoa(PICSimLab.GetInstanceNumber()) + lxT("] - "))
: (lxT("PICSimLab - "))) + : (lxT("PICSimLab - "))) +
"Spare parts"); "Spare parts");
msleep(BASETIMER);
PICSimLab.GetBoard()->Reset();
} }
void CPWindow5::draw1_EvMouseButtonPress(CControl* control, uint button, uint x, uint y, uint state) { void CPWindow5::draw1_EvMouseButtonPress(CControl* control, uint button, uint x, uint y, uint state) {
......
#if defined(ARDUINO_ARCH_AVR)
#define ADCMAX 1023
#define ANALOGIN A0
#define LED1 3
#define PBUTTON 2
#define LED2 4
#else
#endif
#if defined(ARDUINO_ARCH_STM32)
#define ADCMAX 1023
#define ANALOGIN PA0
#define LED1 PB1
#define PBUTTON PB3
#define LED2 PB4
#endif
#if defined(ARDUINO_ARCH_ESP32)
#if defined (CONFIG_IDF_TARGET_ESP32C3)
#define ADCMAX 4095
#define ANALOGIN 0
#define LED1 1
#define PBUTTON 3
#define LED2 4
#else
#define ADCMAX 4095
#define ANALOGIN 4
#define LED1 3
#define PBUTTON 2
#define LED2 5
#endif
#endif
static int bstate = 0;
void isr(void) {
bstate = digitalRead(PBUTTON);
}
void setup() {
Serial.begin(115200);
pinMode(ANALOGIN, INPUT);
pinMode(PBUTTON , INPUT_PULLUP);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
attachInterrupt(digitalPinToInterrupt(PBUTTON), isr, CHANGE);
}
unsigned int value;
unsigned char value2;
char buffer[50];
void loop() {
value = analogRead(ANALOGIN);
value2 = map(value, 0 , ADCMAX, 0, 255 );
analogWrite(LED1, value2);
digitalWrite(LED2, bstate);
sprintf(buffer, "AD=%04d PWM=%03d BT=%d", value, value2, bstate);
Serial.println(buffer);
delay(250);
}
/* ########################################################################
PICsimLab - PIC laboratory simulator
########################################################################
Copyright (c) : 2020-2023 Luis Claudio Gamboa Lopes
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
For e-mail suggestions : lcgamboa@yahoo.com
######################################################################## */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "tests.h"
static int in_out_test(const char* tname, const char* fname, const int start_delay_sec = 1,
const unsigned long inter_delay_us = 1000000L) {
int pwm;
int state;
printf("test %s\n", tname);
if (!test_load(fname)) {
return 0;
}
sleep(start_delay_sec);
for (int i = 0; i < 3; i++) {
if (!test_send_rcmd("set part[02].in[00] 1")) {
printf("Error send rcmd \n");
test_end();
return 0;
}
usleep(inter_delay_us);
if (!test_send_rcmd("get part[01].out[03]")) {
printf("Error send rcmd \n");
test_end();
return 0;
}
sscanf(test_get_cmd_resp() + 22, "%i", &state);
if (state < 50) {
printf("Failed in Button Test \n");
test_end();
return 0;
}
if (!test_send_rcmd("set part[02].in[00] 0")) {
printf("Error send rcmd \n");
test_end();
return 0;
}
usleep(inter_delay_us);
if (!test_send_rcmd("get part[01].out[03]")) {
printf("Error send rcmd \n");
test_end();
return 0;
}
sscanf(test_get_cmd_resp() + 22, "%i", &state);
if (state > 150) {
printf("Failed in Button Test \n");
test_end();
return 0;
}
}
for (int i = 0; i < 3; i++) {
if (!test_send_rcmd("set part[00].in[00] 0")) {
printf("Error send rcmd \n");
test_end();
return 0;
}
usleep(inter_delay_us);
if (!test_send_rcmd("get part[01].out[01]")) {
printf("Error send rcmd \n");
test_end();
return 0;
}
sscanf(test_get_cmd_resp() + 22, "%i", &pwm);
if (pwm > 5) {
printf("Failed in PWM Test Set 0 Get %i\n", pwm);
test_end();
return 0;
}
if (!test_send_rcmd("set part[00].in[00] 100")) {
printf("Error send rcmd \n");
test_end();
return 0;
}
usleep(inter_delay_us);
if (!test_send_rcmd("get part[01].out[01]")) {
printf("Error send rcmd \n");
test_end();
return 0;
}
sscanf(test_get_cmd_resp() + 22, "%i", &pwm);
if ((pwm < 95) || (pwm > 105)) {
printf("Failed in PWM Test Set 100 Get %i\n", pwm);
test_end();
return 0;
}
if (!test_send_rcmd("set part[00].in[00] 200")) {
printf("Error send rcmd \n");
test_end();
return 0;
}
usleep(inter_delay_us);
if (!test_send_rcmd("get part[01].out[01]")) {
printf("Error send rcmd \n");
test_end();
return 0;
}
sscanf(test_get_cmd_resp() + 22, "%i", &pwm);
if (pwm < 195) {
printf("Failed in PWM Test Set 200 Get %i\n", pwm);
test_end();
return 0;
}
}
return test_end();
}
static int test_IN_OUT_ESP32(void* arg) {
return in_out_test("SPI ESP32", "in_out/in_out_esp32.pzw", 5);
}
register_test("IN/OUT ESP32", test_IN_OUT_ESP32, NULL);
static int test_IN_OUT_ESP32C3(void* arg) {
return in_out_test("SPI ESP32C3", "in_out/in_out_esp32c3.pzw", 1, 2000000L);
}
register_test("IN/OUT ESP32C3", test_IN_OUT_ESP32C3, NULL);
static int test_IN_OUT_AVR(void* arg) {
return in_out_test("SPI AVR", "in_out/in_out_uno.pzw");
}
register_test("IN/OUT AVR", test_IN_OUT_AVR, NULL);
static int test_IN_OUT_STM32(void* arg) {
return in_out_test("SPI STM32", "in_out/in_out_stm32.pzw", 1, 2000000L);
}
register_test("IN/OUT STM32", test_IN_OUT_STM32, NULL);
/*
static int test_IN_OUT_PIC18F(void* arg) {
return in_out_test("SPI PIC18F", "in_out/in_out_p18.pzw");
}
register_test("IN/OUT PIC18F", test_IN_OUT_PIC18F, NULL);
*/
\ No newline at end of file
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