Skip to content
Merged

v6.1 #343

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/RISCV-32-arduino/example4.s
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ lightUp:

loop:
#read LDR
la t0, ledPin
lw a0, 0(t0)
addi sp, sp, -4
sw ra,0(sp)
jal ra, analogRead #analogRead(lightSensorPin);
Expand Down
24 changes: 21 additions & 3 deletions src/core/capi/arduino_functions.mts
Original file line number Diff line number Diff line change
Expand Up @@ -715,11 +715,18 @@ export function cr_attachInterrupt() {
// esp32vect.value[Number(interr_pos)]![2] = mode;
//TODO: Graphic retroalimentation
// const gpiopin = "GPIO" + esp32vect.value[Number(interr_pos)]![0];
const gpiopin = "GPIO" + "6"
let gpiopin = "GPIO?"; // Valor por defecto

coreEvents.emit("arduino-get-pin-from-slot", {
position: Number(interr_pos),
callback: (pin: string) => {
gpiopin = "GPIO" + pin; // El suscriptor nos devuelve el pin real
}
});
coreEvents.emit("arduino-terminal-write", {
text: `attachInterrupt(${interr_pos}, 0x${interr_isr.toString(16)}, ${mode}) `,
});
coreEvents.emit("arduino-pin-interrupt", { pin: gpiopin });
coreEvents.emit("arduino-pin-interrupt", { pin: gpiopin, mode: mode, isr: interr_isr, position: interr_pos });
}
export function cr_detachInterrupt() {
//TODO: Revise
Expand Down Expand Up @@ -757,10 +764,15 @@ export function cr_digitalPinToInterrupt() {
}
var pin = BigInt.asUintN(32, readRegister(ret1.indexComp, ret1.indexElem));
//Find clean slot in the interrupt vector table
let pos = -1;
// const pos = esp32vect.value.findIndex(
// (slot: bigint[]) => slot[1] === 0n && slot[2] === 0n,
// );
const pos: number = 0; // Simulamos que siempre se asigna a la posición 0 para simplificar
coreEvents.emit("arduino-find-vector-slot", {
callback: (index: number) => {
pos = index;
}
});

// Si no encuentra ninguna posición libre, findIndex devuelve -1
if (pos === -1) {
Expand All @@ -772,6 +784,12 @@ export function cr_digitalPinToInterrupt() {
);
}
// esp32vect.value[pos] = [BigInt(pin), 0n, 0n]; // Mark the slot as used with the position
coreEvents.emit("arduino-pin-interrupt", {
position: BigInt(pos),
pin: pin.toString(),
mode: 0n,
isr: 0n,
});
writeRegister(BigInt(pos), ret1.indexComp, ret1.indexElem);
coreEvents.emit("arduino-terminal-write", {
text: `digitalPinToInterrupt(${pin})`,
Expand Down
20 changes: 20 additions & 0 deletions src/core/events.mts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ export type CoreEvents = {
"arduino-pin-interrupt":ArduinoPinInterruptEvent;
/** Emitted when a pin is detached from an interrupt */
"arduino-pin-detach-interrupt": ArduinoPinDetachInterruptEvent;
/** Emitted when the simulator requests to find a free slot in the interrupt vector table */
"arduino-find-vector-slot": ArduinoFindSlotEvent;
/** Emitted when the simulator requests to get the pin assigned to an interrupt vector slot */
"arduino-get-pin-from-slot": ArduinoGetPinFromSlotEvent;
};
/**
* Emitted when the simulator sends text to the Arduino Terminal
Expand Down Expand Up @@ -167,6 +171,9 @@ export interface ArduinoPinMode{
export interface ArduinoPinInterruptEvent {
/** The pin number */
pin: string;
isr: bigint;
mode: bigint;
position: bigint;
}
/**
* Emitted when a pin is detached from an interrupt
Expand All @@ -187,6 +194,19 @@ export interface ArduinoPinRead {
callback: (value: number) => void;
}

/**
* Event is emitted when searching a interrupt vector slot
*/ export interface ArduinoFindSlotEvent {
/** Callback to return the found slot index */
callback: (index: number) => void;
}

export interface ArduinoGetPinFromSlotEvent {
position: number;
callback: (pin: string) => void;
}



/**
* Global event emitter for CREATOR core events
Expand Down
11 changes: 11 additions & 0 deletions src/web/arduino/pinstates.mts
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,15 @@ export function switchBoard(boardKey: string) {
coreEvents.on("arduino-pin-read", (event: ArduinoPinRead) => {
const value = pinStates.value[event.pin] ?? 0;
event.callback(value);
});
coreEvents.on("arduino-find-vector-slot", (event) => {
const indexEncontrado = esp32vect.value.findIndex(
(slot: bigint[]) => slot[1] === 0n && slot[2] === 0n
);
event.callback(indexEncontrado);
});

coreEvents.on("arduino-get-pin-from-slot", (event) => {
const pinGuardado = esp32vect.value[event.position]?.[0];
event.callback(pinGuardado?.toString() ?? "unknown");
});
5 changes: 5 additions & 0 deletions src/web/components/simulator/ArduinoTerminal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ export default {

coreEvents.on("arduino-pin-interrupt", pinName => {
this.interrupt[pinName.pin] = true;
esp32vect.value[pinName.position] = [
BigInt(pinName.pin.replace(/\D/g, "")),
pinName.isr,
pinName.mode,
];
});
coreEvents.on("arduino-pin-detach-interrupt", pinName => {
delete this.interrupt[pinName.pin];
Expand Down
Loading