void bbio_spi_sniff(t_hydra_console *con) { uint8_t cs_state, data, rx_data[2]; mode_config_proto_t* proto = &con->mode->proto; bsp_status_t status; proto->dev_mode = DEV_SPI_SLAVE; status = bsp_spi_init(proto->dev_num, proto); status = bsp_spi_init(proto->dev_num+1, proto); if(status == BSP_OK) { cprint(con, "\x01", 1); } else { cprint(con, "\x00", 1); proto->dev_mode = DEV_SPI_MASTER; status = bsp_spi_init(proto->dev_num, proto); status = bsp_spi_deinit(proto->dev_num+1); return; } cs_state = 1; while(!USER_BUTTON || chnReadTimeout(con->sdu, &data, 1,1)) { if (cs_state == 0 && bsp_spi_get_cs(proto->dev_num)) { cprint(con, "]", 1); cs_state = 1; } else if (cs_state == 1 && !(bsp_spi_get_cs(proto->dev_num))) { cprint(con, "[", 1); cs_state = 0; } if(bsp_spi_rxne(proto->dev_num)){ bsp_spi_read_u8(proto->dev_num, &rx_data[0], 1); if(bsp_spi_rxne(proto->dev_num+1)){ bsp_spi_read_u8(proto->dev_num+1, &rx_data[1], 1); } else { rx_data[1] = 0; } cprintf(con, "\\%c%c", rx_data[0], rx_data[1]); } } proto->dev_mode = DEV_SPI_MASTER; status = bsp_spi_init(proto->dev_num, proto); status = bsp_spi_deinit(proto->dev_num+1); }
static int init(t_hydra_console *con, t_tokenline_parsed *p) { mode_config_proto_t* proto = &con->mode->proto; int tokens_used; init_proto_default(con); /* Process cmdline arguments, skipping "spi". */ tokens_used = 1 + exec(con, p, 1); bsp_spi_init(proto->dev_num, proto); show_params(con); return tokens_used; }
static int exec(t_hydra_console *con, t_tokenline_parsed *p, int token_pos) { mode_config_proto_t* proto = &con->mode->proto; float arg_float; int arg_int, t, i; bsp_status_t bsp_status; for (t = token_pos; p->tokens[t]; t++) { switch (p->tokens[t]) { case T_SHOW: t += show(con, p); break; case T_DEVICE: /* Integer parameter. */ t += 2; memcpy(&arg_int, p->buf + p->tokens[t], sizeof(int)); if (arg_int < 1 || arg_int > 2) { cprintf(con, "SPI device must be 1 or 2.\r\n"); return t; } init_proto_default(con); proto->dev_num = arg_int - 1; bsp_status = bsp_spi_init(proto->dev_num, proto); if( bsp_status != BSP_OK) { cprintf(con, str_bsp_init_err, bsp_status); return t; } tl_set_prompt(con->tl, (char *)con->mode->exec->get_prompt(con)); cprintf(con, "Note: SPI parameters have been reset to default values.\r\n"); break; case T_PULL: switch (p->tokens[++t]) { case T_UP: proto->dev_gpio_pull = MODE_CONFIG_DEV_GPIO_PULLUP; break; case T_DOWN: proto->dev_gpio_pull = MODE_CONFIG_DEV_GPIO_PULLDOWN; break; case T_FLOATING: proto->dev_gpio_pull = MODE_CONFIG_DEV_GPIO_NOPULL; break; } bsp_status = bsp_spi_init(proto->dev_num, proto); if( bsp_status != BSP_OK) { cprintf(con, str_bsp_init_err, bsp_status); return t; } break; case T_MODE: if (p->tokens[++t] == T_MASTER) proto->dev_mode = DEV_SPI_MASTER; else proto->dev_mode = DEV_SPI_SLAVE; bsp_status = bsp_spi_init(proto->dev_num, proto); if( bsp_status != BSP_OK) { cprintf(con, str_bsp_init_err, bsp_status); return t; } break; case T_FREQUENCY: t += 2; memcpy(&arg_float, p->buf + p->tokens[t], sizeof(float)); for (i = 0; i < SPEED_NB; i++) { if (arg_float == speeds[proto->dev_num][i]) { proto->dev_speed = i; break; } } if (i == 8) { cprintf(con, "Invalid frequency.\r\n"); return t; } bsp_status = bsp_spi_init(proto->dev_num, proto); if( bsp_status != BSP_OK) { cprintf(con, str_bsp_init_err, bsp_status); return t; } break; case T_POLARITY: t += 2; memcpy(&arg_int, p->buf + p->tokens[t], sizeof(int)); if (arg_int < 0 || arg_int > 1) { cprintf(con, "Polarity device must be 0 or 1.\r\n"); return t; } proto->dev_polarity = arg_int; bsp_status = bsp_spi_init(proto->dev_num, proto); if( bsp_status != BSP_OK) { cprintf(con, str_bsp_init_err, bsp_status); return t; } break; case T_PHASE: t += 2; memcpy(&arg_int, p->buf + p->tokens[t], sizeof(int)); if (arg_int < 0 || arg_int > 1) { cprintf(con, "Phase device must be 0 or 1.\r\n"); return t; } proto->dev_phase = arg_int; bsp_spi_init(proto->dev_num, proto); break; case T_MSB_FIRST: proto->dev_bit_lsb_msb = DEV_SPI_FIRSTBIT_MSB; bsp_status = bsp_spi_init(proto->dev_num, proto); if( bsp_status != BSP_OK) { cprintf(con, str_bsp_init_err, bsp_status); return t; } break; case T_LSB_FIRST: proto->dev_bit_lsb_msb = DEV_SPI_FIRSTBIT_LSB; bsp_status = bsp_spi_init(proto->dev_num, proto); if( bsp_status != BSP_OK) { cprintf(con, str_bsp_init_err, bsp_status); return t; } break; default: return t - token_pos; } } return t - token_pos; }
void bbio_mode_spi(t_hydra_console *con) { uint8_t bbio_subcommand; uint16_t to_rx, to_tx, i; uint8_t *tx_data = (uint8_t *)g_sbuf; uint8_t *rx_data = (uint8_t *)g_sbuf+4096; uint8_t data; bsp_status_t status; mode_config_proto_t* proto = &con->mode->proto; bbio_spi_init_proto_default(con); bsp_spi_init(proto->dev_num, proto); while (!USER_BUTTON) { if(chSequentialStreamRead(con->sdu, &bbio_subcommand, 1) == 1) { switch(bbio_subcommand) { case BBIO_RESET: bsp_spi_deinit(proto->dev_num); return; case BBIO_SPI_CS_LOW: bsp_spi_select(proto->dev_num); cprint(con, "\x01", 1); break; case BBIO_SPI_CS_HIGH: bsp_spi_unselect(proto->dev_num); cprint(con, "\x01", 1); break; case BBIO_SPI_SNIFF_ALL: case BBIO_SPI_SNIFF_CS_LOW: case BBIO_SPI_SNIFF_CS_HIGH: bbio_spi_sniff(con); break; case BBIO_SPI_WRITE_READ: case BBIO_SPI_WRITE_READ_NCS: chSequentialStreamRead(con->sdu, rx_data, 4); to_tx = (rx_data[0] << 8) + rx_data[1]; to_rx = (rx_data[2] << 8) + rx_data[3]; if ((to_tx > 4096) || (to_rx > 4096)) { cprint(con, "\x00", 1); break; } chSequentialStreamRead(con->sdu, tx_data, to_tx); if(bbio_subcommand == BBIO_SPI_WRITE_READ) { bsp_spi_select(proto->dev_num); } bsp_spi_write_u8(proto->dev_num, tx_data, to_tx); i=0; while(i<to_rx) { if((to_rx-i) >= 255) { bsp_spi_read_u8(proto->dev_num, rx_data+i, 255); } else { bsp_spi_read_u8(proto->dev_num, rx_data+i, to_rx-i); } i+=255; } if(bbio_subcommand == BBIO_SPI_WRITE_READ) { bsp_spi_unselect(proto->dev_num); } i=0; cprint(con, "\x01", 1); while(i < to_rx) { cprintf(con, "%c", rx_data[i]); i++; } break; default: if ((bbio_subcommand & BBIO_SPI_BULK_TRANSFER) == BBIO_SPI_BULK_TRANSFER) { // data contains the number of bytes to // write data = (bbio_subcommand & 0b1111) + 1; chSequentialStreamRead(con->sdu, tx_data, data); bsp_spi_write_read_u8(proto->dev_num, tx_data, rx_data, data); cprint(con, "\x01", 1); i=0; while(i < data) { cprintf(con, "%c", rx_data[i]); i++; } } else if ((bbio_subcommand & BBIO_SPI_SET_SPEED) == BBIO_SPI_SET_SPEED) { proto->dev_speed = bbio_subcommand & 0b111; status = bsp_spi_init(proto->dev_num, proto); if(status == BSP_OK) { cprint(con, "\x01", 1); } else { cprint(con, "\x00", 1); } } else if ((bbio_subcommand & BBIO_SPI_CONFIG) == BBIO_SPI_CONFIG) { proto->dev_polarity = (bbio_subcommand & 0b100)?1:0; proto->dev_phase = (bbio_subcommand & 0b10)?1:0; status = bsp_spi_init(proto->dev_num, proto); if(status == BSP_OK) { cprint(con, "\x01", 1); } else { cprint(con, "\x00", 1); } } else if ((bbio_subcommand & BBIO_SPI_CONFIG_PERIPH) == BBIO_SPI_CONFIG_PERIPH) { cprint(con, "\x01", 1); } } } } }
static bool init_gpio(t_hydra_console *con) { /* PA7 as Input connected to TRF7970A MOD Pin */ // palSetPadMode(GPIOA, 7, PAL_MODE_INPUT); /* Configure NFC/TRF7970A in SPI mode with Chip Select */ /* TRF7970A IO0 (To set to "0" for SPI) */ palClearPad(GPIOA, 3); palSetPadMode(GPIOA, 3, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1); /* TRF7970A IO1 (To set to "1" for SPI) */ palSetPad(GPIOA, 2); palSetPadMode(GPIOA, 2, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1); /* TRF7970A IO2 (To set to "1" for SPI) */ palSetPad(GPIOC, 0); palSetPadMode(GPIOC, 0, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1); /* * Initializes the SPI driver 1. The SPI1 signals are routed as follows: * Shall be configured as SPI Slave for TRF7970A NFC data sampling on MOD pin. * NSS. (Not used use Software). * PA5 - SCK.(AF5) => Connected to TRF7970A SYS_CLK pin * PA6 - MISO.(AF5) (Not Used) * PA7 - MOSI.(AF5) => Connected to TRF7970A MOD pin */ /* spiStart() is done in sniffer see sniffer.c */ /* SCK. */ palSetPadMode(GPIOA, 5, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_MID1); /* MISO. Not used/Not connected */ palSetPadMode(GPIOA, 6, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_MID1); /* MOSI. connected to TRF7970A MOD Pin */ palSetPadMode(GPIOA, 7, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_MID1); /* * Initializes the SPI driver 2. The SPI2 signals are routed as follow: * PC1 - NSS. * PB10 - SCK. * PC2 - MISO. * PC3 - MOSI. * Used for communication with TRF7970A in SPI mode with NSS. */ mode_con1.proto.dev_gpio_pull = MODE_CONFIG_DEV_GPIO_NOPULL; mode_con1.proto.dev_speed = 5; /* 5 250 000 Hz */ mode_con1.proto.dev_phase = 1; mode_con1.proto.dev_polarity = 0; mode_con1.proto.dev_bit_lsb_msb = DEV_SPI_FIRSTBIT_MSB; mode_con1.proto.dev_mode = DEV_SPI_MASTER; bsp_spi_init(BSP_DEV_SPI2, &mode_con1.proto); /* NSS - ChipSelect. */ palSetPad(GPIOC, 1); palSetPadMode(GPIOC, 1, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1); /* SCK. */ palSetPadMode(GPIOB, 10, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_MID1); /* MISO. */ palSetPadMode(GPIOC, 2, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_MID1); /* MOSI. */ palSetPadMode(GPIOC, 3, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_MID1); /* Enable TRF7970A EN=1 (EN2 is already equal to GND) */ palClearPad(GPIOB, 11); palSetPadMode(GPIOB, 11, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1); McuDelayMillisecond(2); palSetPad(GPIOB, 11); /* After setting EN=1 wait at least 21ms */ McuDelayMillisecond(21); if (!hydranfc_test_shield()) { if(con != NULL) cprintf(con, "HydraNFC not found.\r\n"); return FALSE; } /* Configure K1/2/3/4 Buttons as Input */ palSetPadMode(GPIOB, 7, PAL_MODE_INPUT); palSetPadMode(GPIOB, 6, PAL_MODE_INPUT); palSetPadMode(GPIOB, 8, PAL_MODE_INPUT); palSetPadMode(GPIOB, 9, PAL_MODE_INPUT); /* Configure D2/3/4/5 LEDs as Output */ D2_OFF; D3_OFF; D4_OFF; D5_OFF; palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1); palSetPadMode(GPIOB, 3, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1); palSetPadMode(GPIOB, 4, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1); palSetPadMode(GPIOB, 5, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1); /* Activates the EXT driver 1. */ if(con != NULL) extStart(&EXTD1, &extcfg); return TRUE; }
void BSP_DRV_Init() { #ifdef CONFIG_MODULE_VIC s32 ret = 0; #endif /***********************基础模块初始化***************************/ #ifdef CONFIG_BALONG_CCLK hi6930_clock_init(); #endif #ifdef CONFIG_CCORE_PM bsp_dpm_init(); #endif #ifdef K3_TIMER_FEATURE k3_timer_init(); #endif adp_timer_init(); timer_dpm_init(); if(0 != BSP_UDI_Init()) logMsg("BSP_UDI_Init fail\n",0,0,0,0,0,0); bsp_ipc_init(); bsp_icc_init(); #ifdef CONFIG_K3V3_CLK_CRG /*CONFIG_K3V3_CLK_CRG*/ gps_refclk_icc_read_cb_init(); #endif /* Cshell init if magic number is set to PRT_FLAG_EN_MAGIC_M */ #ifdef CONFIG_CSHELL if(0 != cshell_init()) { logMsg("cshell init fail\n",0,0,0,0,0,0); } #endif #ifdef CONFIG_NVIM if(0 != bsp_nvm_init()) logMsg("nv init fail\n",0,0,0,0,0,0); #endif /* axi monitor监控初始化 */ (void)bsp_amon_init(); /*此初始化必须放置在MSP/OAM/PS初始化之前,请不要随意改动顺序*/ tcxo_init_configure(); if(0 != bsp_rfile_init()) logMsg("rfile init fail\n",0,0,0,0,0,0); /* version inits */ bsp_productinfo_init(); hwspinlock_init(); bsp_hkadc_init(); bsp_version_init(); bsp_lowpower_mntn_init(); #ifdef CONFIG_MODULE_VIC ret = bsp_vic_init(); if(ret != OK) { logMsg("bsp_vic_init error\n", 0, 0, 0, 0, 0, 0); } #endif (void)bsp_softtimer_init(); #ifdef CONFIG_BALONG_EDMA if(0 != bsp_edma_init()) { logMsg("edma init fail \n",0,0,0,0,0,0); } #endif /*C core init ipc module*/ if(0 != socp_init()) logMsg("socp init fail\n",0,0,0,0,0,0); if(0 != bsp_om_server_init()) logMsg("om init fail\n",0,0,0,0,0,0); if(0 != bsp_dual_modem_init()) logMsg("dual modem uart init fail\n",0,0,0,0,0,0); /***********************外设模块初始化***************************/ bsp_dsp_init(); #ifdef CONFIG_BBP_INT bbp_int_init();/*此处需要放在dsp初始化之后,放在pastar/abb之前*/ #endif bsp_spi_init(); bsp_pmu_init(); regulator_init(); #if defined(CONFIG_PMIC_HI6559) if(bsp_pa_rf_init()) /* 依赖于regulator_init */ { logMsg("bsp_pa_rf_init fail\n",0,0,0,0,0,0); } #endif /*init mipi*/ #ifdef CONFIG_MIPI bsp_mipi_init(); #endif #ifdef CONFIG_TUNER bsp_tuner_init(); #endif #ifdef CONFIG_PASTAR /*此函数的位置不可以向后移动,为pastar上电后,提供足够的稳定时间*/ pmu_hi6561_init_phase1(); #endif if(0 != hi6930_wdt_init()) logMsg("wdt init fail\n",0,0,0,0,0,0); #ifdef CONFIG_CCORE_I2C if(0!=bsp_i2c_initial()) logMsg("i2c init fail\n",0,0,0,0,0,0); #endif if(0 != bsp_gpio_init()) logMsg("gpio init fail\n",0,0,0,0,0,0); #ifdef CONFIG_EFUSE if(0 != efuse_init()) { logMsg("efuse init fail \n",0,0,0,0,0,0); } #endif #ifdef CONFIG_LEDS_CCORE if(0 != bsp_led_init()) { logMsg("led init fail\n",0,0,0,0,0,0); } #endif /***********************通信支撑模块初始化***************************/ #ifdef CONFIG_CIPHER if(0 != cipher_init()) { logMsg("cipher init fail \n",0,0,0,0,0,0); } if(0 != bsp_acc_init()) { logMsg("acc init fail \n",0,0,0,0,0,0); } #endif #ifdef CONFIG_IPF if(0 != ipf_init()) logMsg("ipf init fail\n",0,0,0,0,0,0); #endif #ifdef CONFIG_MODULE_BUSSTRESS ipf_ul_stress_test_start(10); #endif #ifdef FEATURE_TLPHY_MAILBOX bsp_mailbox_init(); #endif mailbox_init(); #ifdef CONFIG_ANTEN if(0 != bsp_anten_init()) logMsg("anten init fail.\n",0,0,0,0,0,0); #endif bsp_sci_cfg_init(); bsp_abb_init(); bsp_on_off_init(); cpufreq_init(); /*初始化醒来的时间戳*/ update_awake_time_stamp(); #ifdef CONFIG_CCORE_BALONG_PM balong_pm_init(); #endif #ifdef CONFIG_AUDIO audio_init(); #endif #ifdef CONFIG_BALONG_MODEM_RESET bsp_reset_init(); #endif (void)bsp_rf_rse_init(); #ifdef CONFIG_PASTAR /*勿动!此处需要放置在该函数最后,确保pastar上电后稳定后,进行初始化配置*/ pmu_hi6561_init_phase2(); #endif (void)bsp_antn_sw_init(); }