Commit 2b43c48b authored by ryukoposting's avatar ryukoposting

add UNO implementation

parent 3e4f3f2b
......@@ -18,10 +18,60 @@
// email: epg@tfwno.gf
#if defined(PCGPIO_TARGET_ARCHITECTURE_ARDUINO_UNO)
#pragma message("found implementation for UNO")
#include "../pcgpio_mcu.h"
// due pin numbers: all digital pins same as what's printed on the board
// analog pins: 14 + n
SIGNAL (TIMER0_COMA_vect){
// digital IO
for(int i = 0; i < 20; i++) {
uint8_t setting = pin_modes[i] & 0xF0;
switch(setting) {
case 0:
if((pin_modes[i] & 0x0C) == 0x0C) { // both edge capture
uint8_t read = digitalRead(i);
if(read != pin_last_scan[i]) {
PCGPIO_SendDigitalInputRead(i, read);
}
pin_last_scan[i] = read;
} else if(pin_modes[i] & 0x04) { // rising edge capture
uint8_t read = digitalRead(i);
if(read > pin_last_scan[i]) {
PCGPIO_SendDigitalInputRead(i, read);
}
pin_last_scan[i] = read;
} else if(pin_modes[i] & 0x08) { // falling edge capture
uint8_t read = digitalRead(i);
if(read < pin_last_scan[i]) {
PCGPIO_SendDigitalInputRead(i, read);
}
pin_last_scan[i] = read;
}
break;
// nothing else currently needed for this,
// more stuff may be here at some point in the future
}
}
}
enum PCGPIO_Status PCGPIO_Start() {
analogWriteResolution(16);
Serial.begin(115200);
state = 0;
receiver_method = {};
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
for(int i = 0; i < 62; i++) {
// special case state used for debugging erroneous captures
pin_last_scan[i] = 0xF0;
}
return PCGPIO_STATUS_OK;
}
enum PCGPIO_Status PCGPIO_TransmitCallback(struct PCGPIO_Callback *callback) {
......@@ -66,4 +116,94 @@ enum PCGPIO_Status PCGPIO_TransmitCallback(struct PCGPIO_Callback *callback) {
return PCGPIO_STATUS_OK;
}
enum PCGPIO_Status PCGPIO_ProcessMethod(struct PCGPIO_Method *method) {
struct PCGPIO_Callback callback;
switch(method->method_type) {
case PCGPIO_METHODTYPE_MCUIDENTITY: {
callback.callback_type = PCGPIO_CALLBACKTYPE_MCUIDENTITY;
struct PCGPIO_CALLBACK_MCUIdentity mcu_identity = {
.mcu_identifier = PCGPIO_GLOBAL_MCU_ID
};
callback.callback.mcu_identity = mcu_identity;
PCGPIO_TransmitCallback(&callback);
}
break;
case PCGPIO_METHODTYPE_IOCONFIG:
if((method->method.io_pin_config.pin) < 54) {
if((method->method.io_pin_config.conf & 0xF0) == 0) { // is an input
// pullup/pulldown configuration
switch((method->method.io_pin_config.conf) & 0x03) {
case 0: // no pullups or pulldowns
pinMode(method->method.io_pin_config.pin, INPUT);
pin_modes[method->method.io_pin_config.pin] = method->method.io_pin_config.conf;
break;
case 1: // pulldown
/* not available on DUE */
break;
case 2: // pullup
pinMode(method->method.io_pin_config.pin, INPUT_PULLUP);
pin_modes[method->method.io_pin_config.pin] = method->method.io_pin_config.conf;
break;
}
// input capture on DUE is handled in interrupt
if((method->method.io_pin_config.conf) & 0x0C) {
pin_last_scan[method->method.io_pin_config.pin] = digitalRead(method->method.io_pin_config.pin);
}
} else if((method->method.io_pin_config.conf & 0xF0) == 0x10) { // is an output
if((method->method.io_pin_config.conf == 0x17) || (method->method.io_pin_config.conf == 0x10)) {
// DUE doesn't implement open drain outputs, and PWM config is
// the same as normal outputs
pinMode(method->method.io_pin_config.pin, OUTPUT);
pin_modes[method->method.io_pin_config.pin] = method->method.io_pin_config.conf;
}
}
} else {
}
break;
case PCGPIO_METHODTYPE_WRITEDIGITAL:
if(method->method.write_digital.pin < 14) {
if(pin_modes[(method->method.write_digital.pin)] == 0x17) {
// is a PWM pin
analogWrite(method->method.write_digital.pin, method->method.write_digital.value);
} else {
digitalWrite(method->method.write_digital.pin, method->method.write_digital.value);
}
}
break;
case PCGPIO_METHODTYPE_WRITEANALOG:
break;
case PCGPIO_METHODTYPE_READDIGITAL:
if(method->method.read_digital.pin < 14) {
callback.callback_type = PCGPIO_CALLBACKTYPE_READDIGITAL;
struct PCGPIO_CALLBACK_ReadDigital read_digital = {
.pin = (method->method.read_digital.pin),
.value = digitalRead(method->method.read_digital.pin)
};
callback.callback.read_digital = read_digital;
PCGPIO_TransmitCallback(&callback);
}
break;
case PCGPIO_METHODTYPE_READANALOG:
if((method->method.read_digital.pin >= 14) && (method->method.read_digital.pin < 20)) {
callback.callback_type = PCGPIO_CALLBACKTYPE_READANALOG;
struct PCGPIO_CALLBACK_ReadAnalog read_analog = {
.pin = (method->method.read_digital.pin),
.value = analogRead(method->method.read_digital.pin)
};
callback.callback.read_analog = read_analog;
PCGPIO_TransmitCallback(&callback);
}
break;
case PCGPIO_METHODTYPE_GETPINCONFIG:
break;
default:
return PCGPIO_STATUS_INVALID_METHOD;
}
return PCGPIO_STATUS_OK;
}
#endif
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