// for Arduino #ifndef _MRBC_API_H #define _MRBC_API_H #include // GPIO #define MRBC_GPIO_IN INPUT #define MRBC_GPIO_OUT OUTPUT #define MRBC_GPIO_IN_PU INPUT_PULLUP #if defined(INPUT_PULLDOWN) #define MRBC_GPIO_IN_PD INPUT_PULLDOWN #else #define MRBC_GPIO_IN_PD INPUT #endif #if defined(OUTPUT_OD) #define MRBC_GPIO_OUT_OD OUTPUT_OD #elif defined(OUTPUT_OPEN_DRAIN) #define MRBC_GPIO_OUT_OD OUTPUT_OPEN_DRAIN #else #define MRBC_GPIO_OUT_OD OUTPUT #endif #define MRBC_GPIO_IN_ANALOG INPUT #define mrbcPinMode(pin, mode) pinMode(pin,mode) #define mrbcDigitalWrite(pin, val) digitalWrite(pin,val) #define mrbcDigitalRead(pin) digitalRead(pin) // ADC #define MRBC_ADC_RANGE 4095 //1023 #define MRBC_ADC_REF 3.3 //5.0 #define mrbcAnalogChannel(p) p static inline uint8_t mrbcAnalogInit(uint8_t pin_id) { uint8_t ch = mrbcAnalogChannel(pin_id); return ch; } static inline uint16_t mrbcAnalogRead(uint8_t ch) { return analogRead(ch); } // PWM #if defined(ARDUINO_ARCH_ESP8266) #include #endif #if defined(ARDUINO_ARCH_ESP32) #include #include "esp32-hal-periman.h" #endif static inline void mrbcPwmWrite(uint8_t pin, uint32_t freq, uint16_t duty) { if(freq==0){ #if defined(ARDUINO_ARCH_ESP8266) stopWaveform(pin); #elif defined(ARDUINO_ARCH_ESP32) ledcDetach(pin); #else mrbcDigitalWrite(pin,0); mrbcPinMode(pin, MRBC_GPIO_OUT); #endif return; } #if defined(ARDUINO_ARCH_ESP8266) uint32_t timeLow=(uint32_t)(1e6/freq); uint32_t timeHigh=(uint32_t)(timeLow*duty/10000); timeLow = timeHigh>timeLow ? 0 : timeLow-timeHigh; startWaveform(pin, timeHigh, timeLow); #elif defined(ARDUINO_ARCH_ESP32) if( perimanGetPinBusType(pin) != ESP32_BUS_TYPE_LEDC ){ ledcAttach(pin, freq, 10); }else{ uint32_t current_freq = ledcReadFreq(pin); if (current_freq != freq) ledcChangeFrequency(pin, freq, 10); } ledcWrite(pin, (uint32_t) duty*1024/10000); #else if(freq==1000){ analogWrite(pin,duty*255/10000); }else{ tone(pin,freq); } #endif } // I2C #include #define NO_I2C_HANDLE static inline void mrbcI2cInit(void* handle, uint freq, int scl, int sda) { if(scl>=0 && sda>=0 && scl!=sda){ Wire.begin(sda, scl); }else{ Wire.begin(); } Wire.setClock(freq); } static inline int mrbcI2cStart(void* handle, uint8_t adrs, uint8_t rw) // rw>0 if read { if(rw){ Wire.requestFrom(adrs, rw); // rw is size }else{ Wire.beginTransmission(adrs); } return 0; } static inline void mrbcI2cStop(void* handle) { } static inline int mrbcI2cWrite(void* handle, const uint8_t *buf, uint16_t len) { Wire.write(buf,len); Wire.endTransmission(); return 0; } static inline int mrbcI2cRead(void* handle, uint8_t *buf, uint16_t len) { if (len == 0) return 0; for (uint16_t i = 0; i < len; i++) { while(Wire.available()==0){} buf[i] = Wire.read(); } return 0; } // SPI #include #define MRBC_SPI_LSB_FIRST LSBFIRST #define MRBC_SPI_MSB_FIRST MSBFIRST #define NO_SPI_HANDLE static SPISettings mySPISettings = SPISettings(1000000, MSBFIRST, SPI_MODE0); static inline void mrbcSpiInit(void* handle) { SPI.begin(); } static inline void mrbcSpiMode(void* handle, uint32_t spi_freq, uint32_t spi_first_bit, uint32_t spi_mode) { mySPISettings = SPISettings(spi_freq, spi_first_bit, spi_mode); } static inline void mrbcSpiTransfer(void* handle, uint8_t *buf, uint32_t len) { SPI.beginTransaction(mySPISettings); SPI.transfer(buf, len); SPI.endTransaction(); } // UART #define MRBC_UART_NONE 0 #define MRBC_UART_EVEN 1 #define MRBC_UART_ODD 2 typedef struct UART_HANDLE { uint8_t unit_num; decltype(Serial) *unit_serial; } UART_HANDLE; static inline void mrbcUartInit(UART_HANDLE *handle) { int unit_num = handle->unit_num; decltype(Serial) *unit_serial; unit_serial=&Serial; switch(unit_num){ case 0: unit_serial=&Serial; break; case 1: unit_serial=&Serial1; break; #if defined(Serial2) case 2: unit_serial=&Serial2; break; #endif #if defined(Serial3) case 3: unit_serial=&Serial3; break; #endif #if defined(Serial4) case 4: unit_serial=&Serial4; break; #endif } handle->unit_serial = unit_serial; } static inline void mrbcUartSetMode(UART_HANDLE *handle, uint32_t baud_rate, uint32_t datb, uint32_t stpb, uint32_t prty ) { decltype(SERIAL_8N1) myUARTSettings=SERIAL_8N1; switch(prty){ case 0: myUARTSettings=(stpb<2)? SERIAL_8N1 : SERIAL_8N2; break; case 1: myUARTSettings=(stpb<2)? SERIAL_8O1 : SERIAL_8O2; break; case 2: myUARTSettings=(stpb<2)? SERIAL_8E1 : SERIAL_8E2; break; } decltype(Serial) *unit_serial = handle->unit_serial; unit_serial->end(); unit_serial->begin(baud_rate,myUARTSettings); while (!(*unit_serial)); unit_serial->setTimeout(1000); } static inline int mrbcUartByteToWrite(UART_HANDLE *handle) { return 0; } static inline int mrbcUartWrite(UART_HANDLE *handle, const char *buf, int size) { return handle->unit_serial->write(buf,size); } static inline int mrbcUartAvailable(UART_HANDLE *handle) { return handle->unit_serial->available(); } static inline int mrbcUartRead(UART_HANDLE *handle, char *buf, int size) { size=(int) handle->unit_serial->readBytes(buf,size); buf[size] = '\0'; return size; } static inline int mrbcUartCanReadLine(UART_HANDLE *handle) { return 0; } static inline void mrbcUartFlush(UART_HANDLE *handle) { handle->unit_serial->flush(); } static inline void mrbcUartClearRx(UART_HANDLE *handle) { } static inline void mrbcUartClearTx(UART_HANDLE *handle) { } // DAC #endif