FTVOID ft9xx_spi_init (FTU8 spi_type, FTU32 spi_div) { /* limitation check */ if (spi_type == 0 || spi_type == 3 || spi_type > 4) { FTPRINT("\nSPI type error"); return; } else if (spi_type == 4 && spi_div < 8) { FTPRINT("\nQSPI div too small"); return; } /* receive dummy byte */ if (spi_type != 1) { spi_dummy = 2; } else { spi_dummy = 1; } /* basic SPI init */ sys_enable(sys_device_spi_master); /* in order to use the SPI in multiple way (not only for EVE) do not use FT900 integrated CS (pad_spim_ss0), do the CS action by GPIO in other place */ gpio_function(FT9XX_SPI_SCK, pad_spim_sck); gpio_function(FT9XX_SPI_MOSI, pad_spim_mosi); gpio_function(FT9XX_SPI_MISO, pad_spim_miso); gpio_dir(FT9XX_SPI_SCK, pad_dir_output); gpio_dir(FT9XX_SPI_MOSI, pad_dir_output); gpio_dir(FT9XX_SPI_MISO, pad_dir_input); /* QSPI init */ if (spi_type == 4) { gpio_function(FT9XX_SPI_MOSI_2, pad_spim_io2); gpio_function(FT9XX_SPI_MOSI_3, pad_spim_io3); gpio_dir(FT9XX_SPI_MOSI_2, pad_dir_output); gpio_dir(FT9XX_SPI_MOSI_3, pad_dir_output); } /* enable SPI: host, mode0, speed */ spi_init(SPIM, spi_dir_master, spi_mode_0, spi_div); /* DSPI, QSPI setting */ if (spi_type != 1) { spi_option(SPIM,spi_option_fifo_size,64); spi_option(SPIM,spi_option_fifo,1); spi_option(SPIM,spi_option_fifo_receive_trigger,1); } /* spi bus width */ spi_option(SPIM,spi_option_bus_width,spi_type); /* set all output pins HIGH */ if (spi_type == 4) { gpio_write(FT9XX_SPI_MOSI_2, 1); gpio_write(FT9XX_SPI_MOSI_3, 1); } gpio_write(FT9XX_SPI_SCK, 1); gpio_write(FT9XX_SPI_MOSI, 1); }
FTVOID ft9xx_init (FTVOID) { interrupt_enable_globally(); /* set and pull HIGH the PD pin of EVE */ gpio_function(FT9XX_PD, pad_gpio43); gpio_dir(FT9XX_PD, pad_dir_output); gpio_write(FT9XX_PD, 1); /* connect a LED in this GPIO to see * if the MCU run here */ gpio_function(FT9XX_TST, pad_gpio54); gpio_dir(FT9XX_TST, pad_dir_output); gpio_write(FT9XX_TST, 1); /* set all the possible functional GPIO to HIGH for better initial status*/ gpio_function(FT9XX_ILI9488_CS, pad_gpio33); gpio_function(FT9XX_SPI_CS_P, pad_gpio28); gpio_function(FT9XX_ILI9488_DCX, pad_gpio34); gpio_dir(FT9XX_ILI9488_CS, pad_dir_output); gpio_dir(FT9XX_SPI_CS_P, pad_dir_output); gpio_dir(FT9XX_ILI9488_DCX, pad_dir_output); gpio_write(FT9XX_ILI9488_CS, 1); gpio_write(FT9XX_SPI_CS_P, 1); gpio_write(FT9XX_ILI9488_DCX, 1); ft9xx_int_timer(); }
FTVOID ft9xx_int_print (char *p) { sys_enable(sys_device_uart0); gpio_function(FT9XX_UART0_TX, pad_uart0_txd); /* UART0 TXD */ gpio_function(FT9XX_UART0_RX, pad_uart0_rxd); /* UART0 RXD */ uart_open(UART0, /* Device */ FT9XX_PRESCALER, /* Prescaler = 1 */ UART_DIVIDER_9600_BAUD, uart_data_bits_8, /* No. Data Bits */ uart_parity_none, /* Parity */ uart_stop_bits_1); /* No. Stop Bits */ FTPRINT("\n"); FTPRINT(p); }
// python function value = gpio_function(gpio) static PyObject *py_gpio_function(PyObject *self, PyObject *args) { unsigned int gpio; int f; PyObject *func; if (!PyArg_ParseTuple(args, "i", &gpio)) return NULL; if (setup_error) { PyErr_SetString(PyExc_RuntimeError, "Module not imported correctly!"); return NULL; } if (!module_setup && (init_module() != SETUP_OK)) return NULL; f = gpio_function(gpio); switch (f) { case 0 : f = INPUT; break; case 1 : f = OUTPUT; break; } func = Py_BuildValue("i", f); return func; }
// python function value = gpio_function(gpio) static PyObject* py_gpio_function(PyObject *self, PyObject *args) { int gpio, channel, f; PyObject *func; if (!PyArg_ParseTuple(args, "i", &channel)) return NULL; if ((gpio = channel_to_gpio(channel)) < 0) return NULL; f = gpio_function(gpio); switch (f) { case 0 : f = INPUT; break; case 1 : f = OUTPUT; break; } func = Py_BuildValue("i", f); return func; }
// python function setup(channel, direction, pull_up_down=PUD_OFF, initial=None) static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwargs) { unsigned int gpio; int channel, direction; int pud = PUD_OFF + PY_PUD_CONST_OFFSET; int initial = -1; static char *kwlist[] = {"channel", "direction", "pull_up_down", "initial", NULL}; int func; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|ii", kwlist, &channel, &direction, &pud, &initial)) return NULL; // check module has been imported cleanly if (setup_error) { PyErr_SetString(PyExc_RuntimeError, "Module not imported correctly!"); return NULL; } // run init_module if module not set up if (!module_setup && (init_module() != SETUP_OK)) return NULL; if (get_gpio_number(channel, &gpio)) return NULL; if (direction != INPUT && direction != OUTPUT) { PyErr_SetString(PyExc_ValueError, "An invalid direction was passed to setup()"); return NULL; } if (direction == OUTPUT) pud = PUD_OFF + PY_PUD_CONST_OFFSET; pud -= PY_PUD_CONST_OFFSET; if (pud != PUD_OFF && pud != PUD_DOWN && pud != PUD_UP) { PyErr_SetString(PyExc_ValueError, "Invalid value for pull_up_down - should be either PUD_OFF, PUD_UP or PUD_DOWN"); return NULL; } func = gpio_function(gpio); if (gpio_warnings && // warnings enabled and ((func != 0 && func != 1) || // (already one of the alt functions or (gpio_direction[gpio] == -1 && func == 1))) // already an output not set from this program) { PyErr_WarnEx(NULL, "This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.", 1); } if (direction == OUTPUT && (initial == LOW || initial == HIGH)) { output_gpio(gpio, initial); } setup_gpio(gpio, direction, pud); gpio_direction[gpio] = direction; Py_RETURN_NONE; }
app_status_t ICACHE_FLASH_ATTR lamp_create(char * id, cJSON * config, app_lamp_t **lamp) { cJSON * jtmp = NULL; app_lamp_t *app = NULL; app_status_t result = APP_STATUS_ERR; char *ctmp = NULL; DBG_MSG("id: %s, config: %p\r\n", id, config); if ((id == NULL) || (config == NULL)) return APP_STATUS_INVALID_PARAM; app = os_malloc(sizeof(app_lamp_t)); DBG_MSG("app: %p\r\n", app); if (app == NULL) goto done; os_bzero(app, 0, sizeof(app_lamp_t)); app->id = id; jtmp = cJSON_GetObjectItem(config, ALAMP_CFG_URL_STR); DBG_MSG("jtmp: %p\r\n", jtmp); if (jtmp == NULL) goto err1; ctmp = (char *)os_malloc(os_strlen(jtmp->valuestring) + 1); os_memset(ctmp, 0, os_strlen(jtmp->valuestring) + 1); os_memcpy(ctmp, jtmp->valuestring, os_strlen(jtmp->valuestring)); app->url = ctmp; DBG_MSG("app->url: %s\r\n", app->url); jtmp = cJSON_GetObjectItem(config, ALAMP_CFG_PIN_STR); DBG_MSG("jtmp: %p\r\n", jtmp); if (jtmp == NULL) goto err1; app->pin = jtmp->valueint; DBG_MSG("app->pin: %d\r\n", app->pin); app->type = APP_LAMP; app->recv = lamp_recv; app->online = lamp_online; app->offline = lamp_offline; // Init GPIO pin gpio_function(app->pin); GPIO_OUTPUT_SET(GPIO_ID_PIN(app->pin), 0); // If everything ok, goto done result = APP_STATUS_OK; *lamp = app; goto done; err1: DBG_MSG("Error occured with status %d\r\n", result); if ((app != NULL) && (app->id != NULL)) os_free(app->id); if ((app != NULL) && (app->url != NULL)) os_free(app->url); if (app != NULL) os_free(app); done: return result; }
FTVOID ft9xx_sdc_init (FTVOID) { #define SDC_TRY_RUN 10 #define SDC_TRY_WAIT 10 FTU8 i = 0; sys_enable(sys_device_sd_card); sdhost_init(); gpio_function(FT9XX_SD_CLK, pad_sd_clk); gpio_pull(FT9XX_SD_CLK, pad_pull_none); gpio_function(FT9XX_SD_CMD, pad_sd_cmd); gpio_pull(FT9XX_SD_CMD, pad_pull_pullup); gpio_function(FT9XX_SD_DAT3, pad_sd_data3); gpio_pull(FT9XX_SD_DAT3, pad_pull_pullup); gpio_function(FT9XX_SD_DAT2, pad_sd_data2); gpio_pull(FT9XX_SD_DAT2, pad_pull_pullup); gpio_function(FT9XX_SD_DAT1, pad_sd_data1); gpio_pull(FT9XX_SD_DAT1, pad_pull_pullup); gpio_function(FT9XX_SD_DAT0, pad_sd_data0); gpio_pull(FT9XX_SD_DAT0, pad_pull_pullup); gpio_function(FT9XX_SD_CD, pad_sd_cd); gpio_pull(FT9XX_SD_CD, pad_pull_pullup); gpio_function(FT9XX_SD_WP, pad_sd_wp); gpio_pull(FT9XX_SD_WP, pad_pull_pullup); while ((SDC_TRY_RUN != ++i) && (sdhost_card_detect() != SDHOST_CARD_INSERTED)) { delayms(SDC_TRY_WAIT); FTPRINT("."); } if(i < SDC_TRY_RUN) { if (f_mount(&FT9xxFatFs, "", 0) != FR_OK) { FTPRINT("\nMount Disk Fail"); } else { /* this lib seems not set the fs_type even it successful mounted */ if (FT9xxFatFs.fs_type) { FTPRINT("\nMount Disk Success"); } else { FTPRINT("\nDisk Found"); } } } else { FTPRINT("\nNo SD Card"); } return; }
void initUART() { *AUX_ENABLES = 1; // enable Mini UART *AUX_MU_IER_REG = 0; // disable interrupts *AUX_MU_CNTL_REG = 0; // disable transmit/receive *AUX_MU_LCR_REG = 3; // set 8 bit mode (8N1) *AUX_MU_MCR_REG = 0; // set RTS line high *AUX_MU_IER_REG = 0; // leave interrupts disabled *AUX_MU_IIR_REG = 0xc6; // enable and clear both tx/rx FIFOs *AUX_MU_BAUD_REG = ((250000000/115200)/8)-1; // 250Mhz clock, 115200 baud gpio_function(14, GPIO_ALT5); gpio_function(15, GPIO_ALT5); // remove pullups on tx/rx lines *gppud = 0; delayms(1); *gppudclk0 = (1 << 14) | (1 << 15); delayms(1); *gppudclk0 = 0; *AUX_MU_CNTL_REG = 3; // enable transmit/receive }
// python function setup(channel, direction, pull_up_down=PUD_OFF, initial=None) static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwargs) { unsigned int gpio; int channel, direction; int pud = PUD_OFF; int initial = -1; static char *kwlist[] = {"channel", "direction", "pull_up_down", "initial", NULL}; int func; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|ii", kwlist, &channel, &direction, &pud, &initial)) return NULL; if (get_gpio_number(channel, &gpio)) return NULL; if (direction != INPUT && direction != OUTPUT) { PyErr_SetString(InvalidDirectionException, "An invalid direction was passed to setup()"); return NULL; } if (direction == OUTPUT) pud = PUD_OFF; if (pud != PUD_OFF && pud != PUD_DOWN && pud != PUD_UP) { PyErr_SetString(InvalidPullException, "Invalid value for pull_up_down - should be either PUD_OFF, PUD_UP or PUD_DOWN"); return NULL; } func = gpio_function(gpio); if (gpio_warnings && // warnings enabled and ((func != 0 && func != 1) || // (already one of the alt functions or (gpio_direction[gpio] == -1 && func == 1))) // already an output not set from this program) { PyErr_WarnEx(NULL, "This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.", 1); } // printf("Setup GPIO %d direction %d pud %d\n", gpio, direction, pud); if (direction == OUTPUT && (initial == LOW || initial == HIGH)) { // printf("Writing intial value %d\n",initial); output_gpio(gpio, initial); } setup_gpio(gpio, direction, pud); gpio_direction[gpio] = direction; Py_INCREF(Py_None); return Py_None; }
// python function value = gpio_function(channel) static PyObject *py_gpio_function(PyObject *self, PyObject *args) { unsigned int gpio; int channel; int f; PyObject *func; if (!PyArg_ParseTuple(args, "i", &channel)) return NULL; if (get_gpio_number(channel, &gpio)) return NULL; if (mmap_gpio_mem()) return NULL; f = gpio_function(gpio); switch (f) { case 0 : f = INPUT; break; case 1 : f = OUTPUT; break; // ALT 0 case 4 : switch (gpio) { case 0 : case 1 : case 2 : case 3 : f = I2C; break; case 7 : case 8 : case 9 : case 10 : case 11 : f = SPI; break; case 12 : case 13 : f = PWM; break; case 14 : case 15 : f = SERIAL; break; case 28 : case 29 : f = I2C; break; default : f = MODE_UNKNOWN; break; } break; // ALT 5 case 2 : if (gpio == 18 || gpio == 19) f = PWM; else f = MODE_UNKNOWN; break; // ALT 4 case 3 : switch (gpio) { case 16 : case 17 : case 18 : case 19 : case 20 : case 21 : f = SPI; break; default : f = MODE_UNKNOWN; break; } break; default : f = MODE_UNKNOWN; break; } func = Py_BuildValue("i", f); return func; }
// python function value = gpio_function(channel) static PyObject *py_gpio_function(PyObject *self, PyObject *args) { unsigned int gpio; unsigned int sys_gpio; int channel; int f,v; PyObject *func; if (!PyArg_ParseTuple(args, "i", &channel)) return NULL; if (get_gpio_number(channel, &gpio, &sys_gpio)) return NULL; v = get_lmk_revision(); f = gpio_function(gpio); debug("### %s: f=%d, gpio=%d ###\n",__func__, f, gpio); switch (f) { case 0 : f = INPUT; break; case 1 : f = OUTPUT; break; // ALT 0 case 4 :if(v == BANANAPRO){ //a20 switch (gpio) { case 257 : case 256 : case 53 : case 52 : f = I2C; break; case 270 : case 266 : case 269 : case 268 : case 267 : f = SPI; break; case 276 : case 45 : f = PWM; break; case 228 : case 229 : f = SERIAL; break; //case 28 : //case 29 : f = I2C; break; default : f = MODE_UNKNOWN; break; } }else if(v == LEMAKER_GUITAR) { //add for guitar switch (gpio) { case 131 : //GPIOE3 case 130 : f = I2C; break;//GPIOE2 case 87 : //GPIOC23 case 88 : //GPIOC24 case 89 : //GPIOC25 case 86 : f = SPI; break;//GPIOC22 case 40 : f = PWM; break;//GPIOB8 case 91 : //GPIOC27 case 90 : f = SERIAL; break;//GPIOD18 default : f = MODE_UNKNOWN; break; } } break; // ALT 5 case 2 : if (gpio == 259 || gpio == 39) f = PWM; else f = MODE_UNKNOWN; break; // ALT 4 case 3 : switch (gpio) { case 38 : case 275 : case 259 : case 39 : case 44 : case 40 : f = SPI; break; default : f = MODE_UNKNOWN; break; } break; default : f = MODE_UNKNOWN; break; } func = Py_BuildValue("i", f); return func; }