123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868 |
- // Platform-dependent functions and includes
- #include "platform.h"
- #include "common.h"
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "llimits.h"
- #include "gpio.h"
- #include "user_interface.h"
- #include "driver/gpio16.h"
- #include "driver/i2c_master.h"
- #include "driver/spi.h"
- #include "driver/uart.h"
- #include "driver/sigma_delta.h"
- #ifdef GPIO_INTERRUPT_ENABLE
- static task_handle_t gpio_task_handle;
- #ifdef GPIO_INTERRUPT_HOOK_ENABLE
- struct gpio_hook_entry {
- platform_hook_function func;
- uint32_t bits;
- };
- struct gpio_hook {
- struct gpio_hook_entry *entry;
- uint32_t all_bits;
- uint32_t count;
- };
- static struct gpio_hook platform_gpio_hook;
- #endif
- #endif
- int platform_init()
- {
- // Setup the various forward and reverse mappings for the pins
- get_pin_map();
- cmn_platform_init();
- // All done
- return PLATFORM_OK;
- }
- #ifdef __ESP2866__
- // ****************************************************************************
- // KEY_LED functions
- uint8_t platform_key_led( uint8_t level){
- uint8_t temp;
- gpio16_output_set(1); // set to high first, for reading key low level
- gpio16_input_conf();
- temp = gpio16_input_get();
- gpio16_output_conf();
- gpio16_output_set(level);
- return temp;
- }
- #endif
- // ****************************************************************************
- // GPIO functions
- /*
- * Set GPIO mode to output. Optionally in RAM helper because interrupts are dsabled
- */
- static void NO_INTR_CODE set_gpio_no_interrupt(uint8 pin, uint8_t push_pull) {
- unsigned pnum = pin_num[pin];
- ETS_GPIO_INTR_DISABLE();
- #ifdef GPIO_INTERRUPT_ENABLE
- pin_int_type[pin] = GPIO_PIN_INTR_DISABLE;
- #endif
- PIN_FUNC_SELECT(pin_mux[pin], pin_func[pin]);
- //disable interrupt
- gpio_pin_intr_state_set(GPIO_ID_PIN(pnum), GPIO_PIN_INTR_DISABLE);
- //clear interrupt status
- GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(pnum));
- // configure push-pull vs open-drain
- if (push_pull) {
- GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(pnum)),
- GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(pnum))) &
- (~ GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE))); //disable open drain;
- } else {
- GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(pnum)),
- GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(pnum))) |
- GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)); //enable open drain;
- }
- ETS_GPIO_INTR_ENABLE();
- }
- /*
- * Set GPIO mode to interrupt. Optionally RAM helper because interrupts are dsabled
- */
- #ifdef GPIO_INTERRUPT_ENABLE
- static void NO_INTR_CODE set_gpio_interrupt(uint8 pin) {
- ETS_GPIO_INTR_DISABLE();
- PIN_FUNC_SELECT(pin_mux[pin], pin_func[pin]);
- GPIO_DIS_OUTPUT(pin_num[pin]);
- gpio_register_set(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin])),
- GPIO_PIN_INT_TYPE_SET(GPIO_PIN_INTR_DISABLE)
- | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_DISABLE)
- | GPIO_PIN_SOURCE_SET(GPIO_AS_PIN_SOURCE));
- ETS_GPIO_INTR_ENABLE();
- }
- #endif
- int platform_gpio_mode( unsigned pin, unsigned mode, unsigned pull )
- {
- NODE_DBG("Function platform_gpio_mode() is called. pin_mux:%d, func:%d\n", pin_mux[pin], pin_func[pin]);
- if (pin >= NUM_GPIO)
- return -1;
- #ifdef __ESP8266__
- if(pin == 0){
- if(mode==PLATFORM_GPIO_INPUT)
- gpio16_input_conf();
- else
- gpio16_output_conf();
- return 1;
- }
- #endif
- #ifdef LUA_USE_MODULES_PWM
- platform_pwm_close(pin); // closed from pwm module, if it is used in pwm
- #endif
- if (pull == PLATFORM_GPIO_PULLUP) {
- PIN_PULLUP_EN(pin_mux[pin]);
- } else {
- PIN_PULLUP_DIS(pin_mux[pin]);
- }
- switch(mode){
- case PLATFORM_GPIO_INPUT:
- GPIO_DIS_OUTPUT(pin_num[pin]);
- /* run on */
- case PLATFORM_GPIO_OUTPUT:
- set_gpio_no_interrupt(pin, TRUE);
- break;
- case PLATFORM_GPIO_OPENDRAIN:
- set_gpio_no_interrupt(pin, FALSE);
- break;
- #ifdef GPIO_INTERRUPT_ENABLE
- case PLATFORM_GPIO_INT:
- set_gpio_interrupt(pin);
- break;
- #endif
- default:
- break;
- }
- return 1;
- }
- int platform_gpio_write( unsigned pin, unsigned level )
- {
- // NODE_DBG("Function platform_gpio_write() is called. pin:%d, level:%d\n",GPIO_ID_PIN(pin_num[pin]),level);
- if (pin >= NUM_GPIO)
- return -1;
- #ifdef __ESP8266__
- if(pin == 0){
- gpio16_output_conf();
- gpio16_output_set(level);
- return 1;
- }
- #endif
- GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), level);
- }
- int platform_gpio_read( unsigned pin )
- {
- // NODE_DBG("Function platform_gpio_read() is called. pin:%d\n",GPIO_ID_PIN(pin_num[pin]));
- if (pin >= NUM_GPIO)
- return -1;
- #ifdef __ESP8266__
- if(pin == 0){
- // gpio16_input_conf();
- return 0x1 & gpio16_input_get();
- }
- #endif
- // GPIO_DIS_OUTPUT(pin_num[pin]);
- return 0x1 & GPIO_INPUT_GET(GPIO_ID_PIN(pin_num[pin]));
- }
- #ifdef GPIO_INTERRUPT_ENABLE
- static void ICACHE_RAM_ATTR platform_gpio_intr_dispatcher (void *dummy){
- uint32 j=0;
- uint32 gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);
- UNUSED(dummy);
- #ifdef GPIO_INTERRUPT_HOOK_ENABLE
- if (gpio_status & platform_gpio_hook.all_bits) {
- for (j = 0; j < platform_gpio_hook.count; j++) {
- if (gpio_status & platform_gpio_hook.entry[j].bits)
- gpio_status = (platform_gpio_hook.entry[j].func)(gpio_status);
- }
- }
- #endif
- /*
- * gpio_status is a bit map where bit 0 is set if unmapped gpio pin 0 (pin3) has
- * triggered the ISR. bit 1 if unmapped gpio pin 1 (pin10=U0TXD), etc. Since this
- * is the ISR, it makes sense to optimize this by doing a fast scan of the status
- * and reverse mapping any set bits.
- */
- for (j = 0; gpio_status>0; j++, gpio_status >>= 1) {
- if (gpio_status&1) {
- int i = pin_num_inv[j];
- if (pin_int_type[i]) {
- //disable interrupt
- gpio_pin_intr_state_set(GPIO_ID_PIN(j), GPIO_PIN_INTR_DISABLE);
- //clear interrupt status
- GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(j));
- uint32 level = 0x1 & GPIO_INPUT_GET(GPIO_ID_PIN(j));
- task_post_high (gpio_task_handle, (i<<1) + level);
- // We re-enable the interrupt when we execute the callback
- }
- }
- }
- }
- void platform_gpio_init( task_handle_t gpio_task )
- {
- gpio_task_handle = gpio_task;
- ETS_GPIO_INTR_ATTACH(platform_gpio_intr_dispatcher, NULL);
- }
- #ifdef GPIO_INTERRUPT_HOOK_ENABLE
- /*
- * Register an ISR hook to be called from the GPIO ISR for a given GPIO bitmask.
- * This routine is only called a few times so has been optimised for size and
- * the unregister is a special case when the bits are 0.
- *
- * Each hook function can only be registered once. If it is re-registered
- * then the hooked bits are just updated to the new value.
- */
- int platform_gpio_register_intr_hook(uint32_t bits, platform_hook_function hook)
- {
- struct gpio_hook nh, oh = platform_gpio_hook;
- int i, j;
- if (!hook) {
- // Cannot register or unregister null hook
- return 0;
- }
- int delete_slot = -1;
- // If hook already registered, just update the bits
- for (i=0; i<oh.count; i++) {
- if (hook == oh.entry[i].func) {
- if (!bits) {
- // Unregister if move to zero bits
- delete_slot = i;
- break;
- }
- if (bits & (oh.all_bits & ~oh.entry[i].bits)) {
- // Attempt to hook an already hooked bit
- return 0;
- }
- // Update the hooked bits (in the right order)
- uint32_t old_bits = oh.entry[i].bits;
- *(volatile uint32_t *) &oh.entry[i].bits = bits;
- *(volatile uint32_t *) &oh.all_bits = (oh.all_bits & ~old_bits) | bits;
- return 1;
- }
- }
- // This must be the register new hook / delete old hook
-
- if (delete_slot < 0) {
- if (bits & oh.all_bits) {
- return 0; // Attempt to hook already hooked bits
- }
- nh.count = oh.count + 1; // register a new hook
- } else {
- nh.count = oh.count - 1; // unregister an old hook
- }
- // These return NULL if the count = 0 so only error check if > 0)
- nh.entry = malloc( nh.count * sizeof(*(nh.entry)) );
- if (nh.count && !(nh.entry)) {
- return 0; // Allocation failure
- }
- for (i=0, j=0; i<oh.count; i++) {
- // Don't copy if this is the entry to delete
- if (i != delete_slot) {
- nh.entry[j++] = oh.entry[i];
- }
- }
- if (delete_slot < 0) { // for a register add the hook to the tail and set the all bits
- nh.entry[j].bits = bits;
- nh.entry[j].func = hook;
- nh.all_bits = oh.all_bits | bits;
- } else { // for an unregister clear the matching all bits
- nh.all_bits = oh.all_bits & (~oh.entry[delete_slot].bits);
- }
- ETS_GPIO_INTR_DISABLE();
- // This is a structure copy, so interrupts need to be disabled
- platform_gpio_hook = nh;
- ETS_GPIO_INTR_ENABLE();
- free(oh.entry);
- return 1;
- }
- #endif // GPIO_INTERRUPT_HOOK_ENABLE
- /*
- * Initialise GPIO interrupt mode. Optionally in RAM because interrupts are dsabled
- */
- void NO_INTR_CODE platform_gpio_intr_init( unsigned pin, GPIO_INT_TYPE type )
- {
- if (platform_gpio_exists(pin)) {
- ETS_GPIO_INTR_DISABLE();
- //clear interrupt status
- GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(pin_num[pin]));
- pin_int_type[pin] = type;
- //enable interrupt
- gpio_pin_intr_state_set(GPIO_ID_PIN(pin_num[pin]), type);
- ETS_GPIO_INTR_ENABLE();
- }
- }
- #endif
- // ****************************************************************************
- // UART
- // TODO: Support timeouts.
- uint32_t platform_uart_setup( unsigned id, uint32_t baud, int databits, int parity, int stopbits )
- {
- UART_ConfigTypeDef cfg;
- switch( baud )
- {
- case BIT_RATE_300:
- case BIT_RATE_600:
- case BIT_RATE_1200:
- case BIT_RATE_2400:
- case BIT_RATE_4800:
- case BIT_RATE_9600:
- case BIT_RATE_19200:
- case BIT_RATE_38400:
- case BIT_RATE_57600:
- case BIT_RATE_74880:
- case BIT_RATE_115200:
- case BIT_RATE_230400:
- case BIT_RATE_460800:
- case BIT_RATE_921600:
- case BIT_RATE_1843200:
- case BIT_RATE_3686400:
- cfg.baud_rate = baud;
- break;
- default:
- cfg.baud_rate = BIT_RATE_9600;
- break;
- }
- switch( databits )
- {
- case 5:
- cfg.data_bits = UART_WordLength_5b;
- break;
- case 6:
- cfg.data_bits = UART_WordLength_6b;
- break;
- case 7:
- cfg.data_bits = UART_WordLength_7b;
- break;
- case 8:
- default:
- cfg.data_bits = UART_WordLength_8b;
- break;
- }
- switch (stopbits)
- {
- case PLATFORM_UART_STOPBITS_1_5:
- cfg.stop_bits = USART_StopBits_1_5;
- break;
- case PLATFORM_UART_STOPBITS_2:
- cfg.stop_bits = USART_StopBits_2;
- break;
- default:
- cfg.stop_bits = USART_StopBits_1;
- break;
- }
- switch (parity)
- {
- case PLATFORM_UART_PARITY_EVEN:
- cfg.parity = USART_Parity_Even;
- break;
- case PLATFORM_UART_PARITY_ODD:
- cfg.parity = USART_Parity_Odd;
- break;
- default:
- cfg.parity = USART_Parity_None;
- break;
- }
- UART_ParamConfig (id, &cfg);
- return baud;
- }
- // if set=1, then alternate serial output pins are used. (15=rx, 13=tx)
- void platform_uart_alt( int set )
- {
- uart0_alt( set );
- return;
- }
- // Send: version with and without mux
- void platform_uart_send( unsigned id, u8 data )
- {
- uart_tx_one_char(id, data);
- }
- // ****************************************************************************
- // PWMs
- static uint16_t pwms_duty[NUM_PWM] = {0};
- void platform_pwm_init()
- {
- int i;
- for(i=0;i<NUM_PWM;i++){
- pwms_duty[i] = DUTY(0);
- }
- pwm_init(500, NULL);
- // NODE_DBG("Function pwms_init() is called.\n");
- }
- // Return the PWM clock
- // NOTE: Can't find a function to query for the period set for the timer,
- // therefore using the struct.
- // This may require adjustment if driver libraries are updated.
- uint32_t platform_pwm_get_clock( unsigned pin )
- {
- // NODE_DBG("Function platform_pwm_get_clock() is called.\n");
- if( pin >= NUM_PWM)
- return 0;
- #ifdef __ESP8266__ // FIXME
- if(!pwm_exist(pin))
- return 0;
- return (uint32_t)pwm_get_freq(pin);
- #else
- return 0;
- #endif
- }
- // Set the PWM clock
- uint32_t platform_pwm_set_clock( unsigned pin, uint32_t clock )
- {
- #ifdef __ESP8266__ // FIXME
- // NODE_DBG("Function platform_pwm_set_clock() is called.\n");
- if( pin >= NUM_PWM)
- return 0;
- if(!pwm_exist(pin))
- return 0;
- pwm_set_freq((uint16_t)clock, pin);
- pwm_start();
- return (uint32_t)pwm_get_freq( pin );
- #else
- return 0;
- #endif
- }
- uint32_t platform_pwm_get_duty( unsigned pin )
- {
- #ifdef __ESP8266__ // FIXME
- // NODE_DBG("Function platform_pwm_get_duty() is called.\n");
- if( pin < NUM_PWM){
- if(!pwm_exist(pin))
- return 0;
- // return NORMAL_DUTY(pwm_get_duty(pin));
- return pwms_duty[pin];
- }
- #endif
- return 0;
- }
- // Set the PWM duty
- uint32_t platform_pwm_set_duty( unsigned pin, uint32_t duty )
- {
- #ifdef __ESP8266__ // FIXME
- // NODE_DBG("Function platform_pwm_set_duty() is called.\n");
- if ( pin < NUM_PWM)
- {
- if(!pwm_exist(pin))
- return 0;
- pwm_set_duty(DUTY(duty), pin);
- } else {
- return 0;
- }
- pwm_start();
- pwms_duty[pin] = NORMAL_DUTY(pwm_get_duty(pin));
- #endif
- return pwms_duty[pin];
- }
- uint32_t platform_pwm_setup( unsigned pin, uint32_t frequency, unsigned duty )
- {
- uint32_t clock;
- #ifdef __ESP8266__ // FIXME
- if ( pin < NUM_PWM)
- {
- platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); // disable gpio interrupt first
- if(!pwm_add(pin))
- return 0;
- // pwm_set_duty(DUTY(duty), pin);
- pwm_set_duty(0, pin);
- pwms_duty[pin] = duty;
- pwm_set_freq((uint16_t)frequency, pin);
- } else {
- return 0;
- }
- clock = platform_pwm_get_clock( pin );
- if (!pwm_start()) {
- return 0;
- }
- #endif
- return clock;
- }
- void platform_pwm_close( unsigned pin )
- {
- // NODE_DBG("Function platform_pwm_stop() is called.\n");
- if ( pin < NUM_PWM)
- {
- #ifdef __ESP8266__ // FIXME
- pwm_delete(pin);
- #endif
- pwm_start();
- }
- }
- bool platform_pwm_start( unsigned pin )
- {
- #ifdef __ESP8266__ // FIXME
- // NODE_DBG("Function platform_pwm_start() is called.\n");
- if ( pin < NUM_PWM)
- {
- if(!pwm_exist(pin))
- return FALSE;
- pwm_set_duty(DUTY(pwms_duty[pin]), pin);
- return pwm_start();
- }
- #endif
- return FALSE;
- }
- void platform_pwm_stop( unsigned pin )
- {
- #ifdef __ESP8266__ // FIXME
- // NODE_DBG("Function platform_pwm_stop() is called.\n");
- if ( pin < NUM_PWM)
- {
- if(!pwm_exist(pin))
- return;
- pwm_set_duty(0, pin);
- pwm_start();
- }
- #endif
- }
- // *****************************************************************************
- // Sigma-Delta platform interface
- uint8_t platform_sigma_delta_setup( uint8_t pin )
- {
- #ifdef __ESP8266__ // FIXME
- if (pin < 1 || pin > NUM_GPIO)
- return 0;
- sigma_delta_setup();
- // set GPIO output mode for this pin
- platform_gpio_mode( pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT );
- platform_gpio_write( pin, PLATFORM_GPIO_LOW );
- // enable sigma-delta on this pin
- GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin])),
- (GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin]))) &(~GPIO_PIN_SOURCE_MASK)) |
- GPIO_PIN_SOURCE_SET( SIGMA_AS_PIN_SOURCE ));
- #endif
- return 1;
- }
- uint8_t platform_sigma_delta_close( uint8_t pin )
- {
- #ifdef __ESP8266__ // FIXME
- if (pin < 1 || pin > NUM_GPIO)
- return 0;
- sigma_delta_stop();
- // set GPIO input mode for this pin
- platform_gpio_mode( pin, PLATFORM_GPIO_INPUT, PLATFORM_GPIO_PULLUP );
- // CONNECT GPIO TO PIN PAD
- GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin])),
- (GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin]))) &(~GPIO_PIN_SOURCE_MASK)) |
- GPIO_PIN_SOURCE_SET( GPIO_AS_PIN_SOURCE ));
- #endif
- return 1;
- }
- void platform_sigma_delta_set_pwmduty( uint8_t duty )
- {
- #ifdef __ESP8266__ // FIXME
- uint8_t target = 0, prescale = 0;
- target = duty > 128 ? 256 - duty : duty;
- prescale = target == 0 ? 0 : target-1;
- //freq = 80000 (khz) /256 /duty_target * (prescale+1)
- sigma_delta_set_prescale_target( prescale, duty );
- #endif
- }
- void platform_sigma_delta_set_prescale( uint8_t prescale )
- {
- #ifdef __ESP8266__ // FIXME
- sigma_delta_set_prescale_target( prescale, -1 );
- #endif
- }
- void platform_sigma_delta_set_target( uint8_t target )
- {
- #ifdef __ESP8266__ // FIXME
- sigma_delta_set_prescale_target( -1, target );
- #endif
- }
- // *****************************************************************************
- // I2C platform interface
- uint32_t platform_i2c_setup( unsigned id, uint8_t sda, uint8_t scl, uint32_t speed ){
- if (sda >= NUM_GPIO || scl >= NUM_GPIO)
- return 0;
- // platform_pwm_close(sda);
- // platform_pwm_close(scl);
- // disable gpio interrupt first
- platform_gpio_mode(sda, PLATFORM_GPIO_INPUT, PLATFORM_GPIO_PULLUP); // inside this func call platform_pwm_close
- platform_gpio_mode(scl, PLATFORM_GPIO_INPUT, PLATFORM_GPIO_PULLUP); // disable gpio interrupt first
- i2c_master_gpio_init(sda, scl);
- return PLATFORM_I2C_SPEED_SLOW;
- }
- void platform_i2c_send_start( unsigned id ){
- i2c_master_start();
- }
- void platform_i2c_send_stop( unsigned id ){
- i2c_master_stop();
- }
- int platform_i2c_send_address( unsigned id, uint16_t address, int direction ){
- // Convert enum codes to R/w bit value.
- // If TX == 0 and RX == 1, this test will be removed by the compiler
- if ( ! ( PLATFORM_I2C_DIRECTION_TRANSMITTER == 0 &&
- PLATFORM_I2C_DIRECTION_RECEIVER == 1 ) ) {
- direction = ( direction == PLATFORM_I2C_DIRECTION_TRANSMITTER ) ? 0 : 1;
- }
- i2c_master_writeByte( (uint8_t) ((address << 1) | direction ));
- // Low-level returns nack (0=acked); we return ack (1=acked).
- return ! i2c_master_getAck();
- }
- int platform_i2c_send_byte( unsigned id, uint8_t data ){
- i2c_master_writeByte(data);
- // Low-level returns nack (0=acked); we return ack (1=acked).
- return ! i2c_master_getAck();
- }
- int platform_i2c_recv_byte( unsigned id, int ack ){
- uint8_t r = i2c_master_readByte();
- i2c_master_setAck( !ack );
- return r;
- }
- // *****************************************************************************
- // SPI platform interface
- uint32_t platform_spi_setup( uint8_t id, int mode, unsigned cpol, unsigned cpha, uint32_t clock_div)
- {
- #ifdef __ESP8266__ // FIXME
- spi_master_init( id, cpol, cpha, clock_div );
- #endif
- return 1;
- }
- int platform_spi_send( uint8_t id, uint8_t bitlen, spi_data_type data )
- {
- if (bitlen > 32)
- return PLATFORM_ERR;
- #ifdef __ESP8266__ // FIXME
- spi_mast_transaction( id, 0, 0, bitlen, data, 0, 0, 0 );
- #endif
- return PLATFORM_OK;
- }
- spi_data_type platform_spi_send_recv( uint8_t id, uint8_t bitlen, spi_data_type data )
- {
- if (bitlen > 32)
- return 0;
- #ifdef __ESP8266__ // FIXME
- spi_mast_set_mosi( id, 0, bitlen, data );
- spi_mast_transaction( id, 0, 0, 0, 0, bitlen, 0, -1 );
- return spi_mast_get_miso( id, 0, bitlen );
- #else
- return 0;
- #endif
- }
- int platform_spi_set_mosi( uint8_t id, uint16_t offset, uint8_t bitlen, spi_data_type data )
- {
- if (offset + bitlen > 512)
- return PLATFORM_ERR;
- #ifdef __ESP8266__ // FIXME
- spi_mast_set_mosi( id, offset, bitlen, data );
- #endif
- return PLATFORM_OK;
- }
- spi_data_type platform_spi_get_miso( uint8_t id, uint16_t offset, uint8_t bitlen )
- {
- if (offset + bitlen > 512)
- return 0;
- #ifdef __ESP8266__ // FIXME
- return spi_mast_get_miso( id, offset, bitlen );
- #else
- return 0;
- #endif
- }
- int platform_spi_transaction( uint8_t id, uint8_t cmd_bitlen, spi_data_type cmd_data,
- uint8_t addr_bitlen, spi_data_type addr_data,
- uint16_t mosi_bitlen, uint8_t dummy_bitlen, int16_t miso_bitlen )
- {
- if ((cmd_bitlen > 16) ||
- (addr_bitlen > 32) ||
- (mosi_bitlen > 512) ||
- (dummy_bitlen > 256) ||
- (miso_bitlen > 512))
- return PLATFORM_ERR;
- #ifdef __ESP8266__ // FIXME
- spi_mast_transaction( id, cmd_bitlen, cmd_data, addr_bitlen, addr_data, mosi_bitlen, dummy_bitlen, miso_bitlen );
- #endif
- return PLATFORM_OK;
- }
- // ****************************************************************************
- // Flash access functions
- /*
- * Assumptions:
- * > toaddr is INTERNAL_FLASH_WRITE_UNIT_SIZE aligned
- * > size is a multiple of INTERNAL_FLASH_WRITE_UNIT_SIZE
- */
- uint32_t platform_s_flash_write( const void *from, uint32_t toaddr, uint32_t size )
- {
- SpiFlashOpResult r;
- const uint32_t blkmask = INTERNAL_FLASH_WRITE_UNIT_SIZE - 1;
- uint32_t *apbuf = NULL;
- uint32_t fromaddr = (uint32_t)from;
- if( (fromaddr & blkmask ) || (fromaddr >= INTERNAL_FLASH_MAPPED_ADDRESS)) {
- apbuf = (uint32_t *)malloc(size);
- if(!apbuf)
- return 0;
- memcpy(apbuf, from, size);
- }
- system_soft_wdt_feed ();
- r = flash_write(toaddr, apbuf?(uint32 *)apbuf:(uint32 *)from, size);
- if(apbuf)
- free(apbuf);
- if(SPI_FLASH_RESULT_OK == r)
- return size;
- else{
- NODE_ERR( "ERROR in flash_write: r=%d at %08X\n", ( int )r, ( unsigned )toaddr);
- return 0;
- }
- }
- /*
- * Assumptions:
- * > fromaddr is INTERNAL_FLASH_READ_UNIT_SIZE aligned
- * > size is a multiple of INTERNAL_FLASH_READ_UNIT_SIZE
- */
- uint32_t platform_s_flash_read( void *to, uint32_t fromaddr, uint32_t size )
- {
- if (size==0)
- return 0;
- SpiFlashOpResult r;
- system_soft_wdt_feed ();
- const uint32_t blkmask = (INTERNAL_FLASH_READ_UNIT_SIZE - 1);
- if( ((uint32_t)to) & blkmask )
- {
- uint32_t size2=size-INTERNAL_FLASH_READ_UNIT_SIZE;
- uint32* to2=(uint32*)((((uint32_t)to)&(~blkmask))+INTERNAL_FLASH_READ_UNIT_SIZE);
- r = flash_read(fromaddr, to2, size2);
- if(SPI_FLASH_RESULT_OK == r)
- {
- memmove(to,to2,size2);
- char back[ INTERNAL_FLASH_READ_UNIT_SIZE ] __attribute__ ((aligned(INTERNAL_FLASH_READ_UNIT_SIZE)));
- r=flash_read(fromaddr+size2,(uint32*)back,INTERNAL_FLASH_READ_UNIT_SIZE);
- os_memcpy((uint8_t*)to+size2,back,INTERNAL_FLASH_READ_UNIT_SIZE);
- }
- }
- else
- r = flash_read(fromaddr, (uint32 *)to, size);
- if(SPI_FLASH_RESULT_OK == r)
- return size;
- else{
- NODE_ERR( "ERROR in flash_read: r=%d at %08X\n", ( int )r, ( unsigned )fromaddr);
- return 0;
- }
- }
- int platform_flash_erase_sector( uint32_t sector_id )
- {
- system_soft_wdt_feed ();
- return flash_erase( sector_id ) == SPI_FLASH_RESULT_OK ? PLATFORM_OK : PLATFORM_ERR;
- }
- uint32_t platform_flash_mapped2phys (uint32_t mapped_addr)
- {
- #ifdef __ESP8266__
- uint32_t cache_ctrl = READ_PERI_REG(CACHE_FLASH_CTRL_REG);
- if (!(cache_ctrl & CACHE_FLASH_ACTIVE))
- return -1;
- bool b0 = (cache_ctrl & CACHE_FLASH_MAPPED0) ? 1 : 0;
- bool b1 = (cache_ctrl & CACHE_FLASH_MAPPED1) ? 1 : 0;
- uint32_t meg = (b1 << 1) | b0;
- return mapped_addr - INTERNAL_FLASH_MAPPED_ADDRESS + meg * 0x100000;
- #else
- // FIXME
- return mapped_addr - INTERNAL_FLASH_MAPPED_ADDRESS;
- #endif
- }
|