Пример #1
0
static int
partool_init(int mode, int argc, char *argv[])
{
    char *partition = argv[2];
    int err;
    
    err = part_init(partition);
    if (err<0) {
        return err;
    }
    
    blk = part_block_create(
            mode, 
            partool_size, 
            partool_read, 
            partool_write,
#ifdef __BOOT__
            partition,
            NULL,
#else
            partool_tmpfile, 
            partool_humfile, 
#endif
            0);
    if (NULL==blk) {
        return -EINVAL;
    }

    return 0;
}
Пример #2
0
int __sata_initialize(void)
{
	int rc;
	int i;

	for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) {
		memset(&sata_dev_desc[i], 0, sizeof(struct blk_desc));
		sata_dev_desc[i].if_type = IF_TYPE_SATA;
		sata_dev_desc[i].devnum = i;
		sata_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
		sata_dev_desc[i].type = DEV_TYPE_HARDDISK;
		sata_dev_desc[i].lba = 0;
		sata_dev_desc[i].blksz = 512;
		sata_dev_desc[i].log2blksz = LOG2(sata_dev_desc[i].blksz);
#ifndef CONFIG_BLK
		sata_dev_desc[i].block_read = sata_bread;
		sata_dev_desc[i].block_write = sata_bwrite;
#endif
		rc = init_sata(i);
		if (!rc) {
			rc = scan_sata(i);
			if (!rc && sata_dev_desc[i].lba > 0 &&
			    sata_dev_desc[i].blksz > 0)
				part_init(&sata_dev_desc[i]);
		}
	}

	return rc;
}
Пример #3
0
/*============================================================================*/
static void bldr_pre_process(void)
{
    /* enter preloader safe mode */
    platform_safe_mode(1, 5000);

    /* essential hardware initialization. e.g. timer, pll, uart... */
    platform_pre_init();

    print("\n%s Build Time: %s\n", MOD, BUILD_TIME);    

    g_boot_mode = NORMAL_BOOT;

    /* hardware initialization */
    platform_init();

#if CFG_UART_TOOL_HANDSHAKE
    /* init uart handshake for sending 'ready' to tool and receiving handshake
     * pattern from tool in the background and we'll see the pattern later.
     * this can reduce the handshake time.
     */
    uart_handshake_init();
#endif   

    part_init();
    part_dump();

    /* init security library */
    sec_lib_init();
}
Пример #4
0
int blk_prepare_device(struct udevice *dev)
{
    struct blk_desc *desc = dev_get_uclass_platdata(dev);

    part_init(desc);

    return 0;
}
Пример #5
0
int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
			  struct blk_desc **dev_desc)
{
	char *ep;
	char *dup_str = NULL;
	const char *dev_str, *hwpart_str;
	int dev, hwpart;

	hwpart_str = strchr(dev_hwpart_str, '.');
	if (hwpart_str) {
		dup_str = strdup(dev_hwpart_str);
		dup_str[hwpart_str - dev_hwpart_str] = 0;
		dev_str = dup_str;
		hwpart_str++;
	} else {
		dev_str = dev_hwpart_str;
		hwpart = 0;
	}

	dev = simple_strtoul(dev_str, &ep, 16);
	if (*ep) {
		printf("** Bad device specification %s %s **\n",
		       ifname, dev_str);
		dev = -1;
		goto cleanup;
	}

	if (hwpart_str) {
		hwpart = simple_strtoul(hwpart_str, &ep, 16);
		if (*ep) {
			printf("** Bad HW partition specification %s %s **\n",
			    ifname, hwpart_str);
			dev = -1;
			goto cleanup;
		}
	}

	*dev_desc = get_dev_hwpart(ifname, dev, hwpart);
	if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
		printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
		dev = -1;
		goto cleanup;
	}

#ifdef HAVE_BLOCK_DEVICE
	/*
	 * Updates the partition table for the specified hw partition.
	 * Does not need to be done for hwpart 0 since it is default and
	 * already loaded.
	 */
	if(hwpart != 0)
		part_init(*dev_desc);
#endif

cleanup:
	free(dup_str);
	return dev;
}
Пример #6
0
static void null_leave(struct state *st, struct state *next, int id)
{
    part_init();
    shad_init();
    ball_init();
    item_init();
    geom_init();
    gui_init();
    hud_init();
}
Пример #7
0
int wm_core_init(void)
{
	int ret = 0;
	static char init_done;

	if (init_done)
		return WM_SUCCESS;

	wmstdio_init(UART0_ID, 0);

	ret = cli_init();
	if (ret != WM_SUCCESS) {
		init_e("Cli init failed.");
		goto out;
	}

	ret = wmtime_init();
	if (ret != WM_SUCCESS) {
		init_e("Wmtime init failed.");
		goto out;
	}

	ret = pm_init();
	if (ret != WM_SUCCESS) {
		init_e("Power manager init failed.");
		goto out;
	}

	ret = healthmon_init();
	if (ret != WM_SUCCESS) {
		init_e("Healthmon init failed.");
		goto out;
	}

	boot_report_flags();

	wmlog("boot", "Reset Cause Register: 0x%x",
	      boot_reset_cause());
	if (boot_reset_cause() & (1<<5))
		wmlog("boot", " - Watchdog reset bit is set");

	/* Read partition table layout from flash */
	part_init();
out:
	if (ret == WM_SUCCESS)
		init_done = 1;
	return ret;
}
Пример #8
0
static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
{
	int ret;
	struct udevice *bdev;
	struct blk_desc bd;
	struct blk_desc *bdesc;
	char str[10];

	/*
	 * detect the scsi driver to get information about its geometry (block
	 * size, number of blocks) and other parameters (ids, type, ...)
	 */
	scsi_init_dev_desc_priv(&bd);
	if (scsi_detect_dev(dev, id, lun, &bd))
		return -ENODEV;

	/*
	* Create only one block device and do detection
	* to make sure that there won't be a lot of
	* block devices created
	*/
	snprintf(str, sizeof(str), "id%dlun%d", id, lun);
	ret = blk_create_devicef(dev, "scsi_blk", str, IF_TYPE_SCSI, -1,
			bd.blksz, bd.blksz * bd.lba, &bdev);
	if (ret) {
		debug("Can't create device\n");
		return ret;
	}

	bdesc = dev_get_uclass_platdata(bdev);
	bdesc->target = id;
	bdesc->lun = lun;
	bdesc->removable = bd.removable;
	bdesc->type = bd.type;
	memcpy(&bdesc->vendor, &bd.vendor, sizeof(bd.vendor));
	memcpy(&bdesc->product, &bd.product, sizeof(bd.product));
	memcpy(&bdesc->revision, &bd.revision,	sizeof(bd.revision));
	part_init(bdesc);

	if (verbose) {
		printf("  Device %d: ", 0);
		dev_print(bdesc);
	}
	return 0;
}
Пример #9
0
static void bldr_pre_process(void)
{

    #if CFG_USB_AUTO_DETECT
    platform_usbdl_flag_check();
    #endif

    #if CFG_EMERGENCY_DL_SUPPORT
    platform_safe_mode(1, CFG_EMERGENCY_DL_TIMEOUT_MS);
    #endif

    /* essential hardware initialization. e.g. timer, pll, uart... */
    platform_pre_init();

    print("\n%s Build Time: %s\n", MOD, BUILD_TIME);
//    config_DCC_Calin();
//    dump_dcc_regs();
//    disable_FBB_SW();

    g_boot_mode = NORMAL_BOOT;

    /* hardware initialization */
    platform_init();

#if CFG_UART_TOOL_HANDSHAKE
				/* init uart handshake for sending 'ready' to tool and receiving handshake
				 * pattern from tool in the background and we'll see the pattern later.
				 * this can reduce the handshake time.
				 */
	//#ifdef DUMMY_AP //FIXME
				uart_handshake_init();
	//#endif
#endif
    BOOTING_TIME_PROFILING_LOG("UART handshake init");
    log_buf_ctrl(1); /* switch log buffer to dram */

    part_init();
    part_dump();
	BOOTING_TIME_PROFILING_LOG("Part Init");

    /* init security library */
    sec_lib_init();
	BOOTING_TIME_PROFILING_LOG("Sec lib init");
}
Пример #10
0
int _start(int argc, char* argv[])
{
    printf("Block Device Manager (BDM) v%d.%d\n", MAJOR_VER, MINOR_VER);

    if (RegisterLibraryEntries(&_exp_bdm) != 0) {
        M_PRINTF("ERROR: Already registered!\n");
        return MODULE_NO_RESIDENT_END;
    }

    // initialize the block device manager
    if (bdm_init() < 0) {
        M_PRINTF("ERROR: BDM init failed!\n");
        return MODULE_NO_RESIDENT_END;
    }

    // initialize the partition driver
    part_init();

    // return resident
    return MODULE_RESIDENT_END;
}
Пример #11
0
int host_dev_bind(int dev, char *filename)
{
	struct host_block_dev *host_dev = find_host_device(dev);

	if (!host_dev)
		return -1;
	if (host_dev->blk_dev.priv) {
		os_close(host_dev->fd);
		host_dev->blk_dev.priv = NULL;
	}
	if (host_dev->filename)
		free(host_dev->filename);
	if (filename && *filename) {
		host_dev->filename = strdup(filename);
	} else {
		host_dev->filename = NULL;
		return 0;
	}

	host_dev->fd = os_open(host_dev->filename, OS_O_RDWR);
	if (host_dev->fd == -1) {
		printf("Failed to access host backing file '%s'\n",
		       host_dev->filename);
		return 1;
	}

	struct blk_desc *blk_dev = &host_dev->blk_dev;
	blk_dev->if_type = IF_TYPE_HOST;
	blk_dev->priv = host_dev;
	blk_dev->blksz = 512;
	blk_dev->lba = os_lseek(host_dev->fd, 0, OS_SEEK_END) / blk_dev->blksz;
	blk_dev->block_read = host_block_read;
	blk_dev->block_write = host_block_write;
	blk_dev->devnum = dev;
	blk_dev->part_type = PART_TYPE_UNKNOWN;
	part_init(blk_dev);

	return 0;
}
Пример #12
0
int wm_wlan_init()
{
	static char init_done;
	struct partition_entry *p;
	short history = 0;
	struct partition_entry *f1, *f2;

	if (init_done)
		return WM_SUCCESS;

	int ret = part_init();
	if (ret != WM_SUCCESS) {
		init_e("wm_wlan_init: could not read partition table");
		return ret;
	}
	f1 = part_get_layout_by_id(FC_COMP_WLAN_FW, &history);
	f2 = part_get_layout_by_id(FC_COMP_WLAN_FW, &history);

	if (f1 && f2)
		p = part_get_active_partition(f1, f2);
	else if (!f1 && f2)
		p = f2;
	else if (!f2 && f1)
		p = f1;
	else
		return -WLAN_ERROR_FW_NOT_DETECTED;

	flash_desc_t fl;
	part_to_flash_desc(p, &fl);

	/* Initialize wlan */
	ret = wlan_init(&fl);
	if (ret != WM_SUCCESS)
		return ret;

	init_done = 1;
	return WM_SUCCESS;
}
Пример #13
0
static void modules_init()
{
    int ret;
    struct partition_entry *p;
    flash_desc_t fl;

    /*
     * Initialize wmstdio prints
     */
    ret = wmstdio_init(UART0_ID, 115200);
    if (ret != WM_SUCCESS) {
        appln_critical_error_handler((void *) -WM_FAIL);
    }

    /* Initialize time subsystem.
     *
     * Initializes time to 1/1/1970 epoch 0.
     */
    ret = wmtime_init();
    if (ret != WM_SUCCESS) {
        wmprintf("Error: wmtime_init failed");
        appln_critical_error_handler((void *) -WM_FAIL);
    }

    /*
     * Initialize CLI Commands
     */
    ret = cli_init();
    if (ret != WM_SUCCESS) {
        LOG_ERROR("Error: cli_init failed");
        appln_critical_error_handler((void *) -WM_FAIL);
    }

    /* Initialize the partition module */
    ret = part_init();
    if (ret != 0) {
        LOG_ERROR("Failed to initialize partition\r\n");
        appln_critical_error_handler((void *) -WM_FAIL);
    }
    p = part_get_layout_by_id(FC_COMP_PSM, NULL);
    if (!p) {
        LOG_ERROR("Error: no psm partition found");
        appln_critical_error_handler((void *) -WM_FAIL);
    }
    part_to_flash_desc(p, &fl);

#if defined CONFIG_CPU_MC200 && defined CONFIG_WiFi_8801
    //check psm _format
    psm_format_check(&fl);
#endif
    /* Initilize psm module */
    ret = app_psm_init();
    if (ret != 0) {
        LOG_ERROR("Failed to initialize psm module\r\n");
        appln_critical_error_handler((void *) -WM_FAIL);
    }

    wmprintf("\n\r");
    wmprintf("_|      _|  _|_|_|  _|_|_|    _|_|  \n\r");
    wmprintf("_|_|  _|_|    _|      _|    _|    _|\n\r");
    wmprintf("_|  _|  _|    _|      _|    _|    _|\n\r");
    wmprintf("_|      _|    _|      _|    _|    _|\n\r");
    wmprintf("_|      _|  _|_|_|  _|_|_|    _|_|  \n\r");
    print_versions();

    read_provision_status();

#ifndef RELEASE
    /* Initilize cli for psm module */
    ret = psm_cli_init();
    if (ret != 0) {
        LOG_ERROR("Failed to register psm-cli commands\r\n");
        appln_critical_error_handler((void *) -WM_FAIL);
    }
#endif

    ret = gpio_drv_init();
    if (ret != WM_SUCCESS) {
        LOG_ERROR("Error: gpio_drv_init failed");
        appln_critical_error_handler((void *) -WM_FAIL);
    }

    ret = aes_drv_init();
    if (ret != WM_SUCCESS) {
        LOG_ERROR("Error: aes drv init failed");
        appln_critical_error_handler((void *) -WM_FAIL);
    }

    ret = factory_cli_init();
    if (ret != 0) {
        LOG_ERROR("Error: factory_cli_init failed");
        appln_critical_error_handler((void *) -WM_FAIL);
    }

    ret = miio_chip_rpc_init();
    if (ret != 0) {
        LOG_ERROR("Error: miio_chip_rpc_cli_init failed");
        appln_critical_error_handler((void *) -WM_FAIL);
    }

#ifndef RELEASE
    ret = appln_cli_init();
    if (ret != 0) {
        LOG_ERROR("Error: appln init failed");
        appln_critical_error_handler((void *) -WM_FAIL);
    }
#endif

    /* init add on interface */
    init_addon_interface();

    ret = ota_init();
    if (ret != 0) {
        LOG_ERROR("Error: ota init failed");
        appln_critical_error_handler((void *) -WM_FAIL);
    }

#ifdef MIIO_COMMANDS
    ret = mcmd_create(UART1_ID);
    if (ret < 0) {
        LOG_ERROR("Error: miio command init failed(%d)\r\n", ret);
        appln_critical_error_handler((void *) -WM_FAIL);
    }
#endif

    /*
	* Initialize Power Management Subsystem
	*/
	ret = pm_init();
	if (ret != WM_SUCCESS) {
		LOG_ERROR("Error: pm_init failed");
		appln_critical_error_handler((void *) -WM_FAIL);
	}

}
Пример #14
0
/*============================================================================*/
static void bldr_pre_process(void)
{
    #ifdef PL_PROFILING
    u32 profiling_time;
    profiling_time = 0;
    #endif

#if defined(CFG_USB_AUTO_DETECT)
	platform_usbdl_flag_check();
#endif

    /* enter preloader safe mode */
#if CFG_EMERGENCY_DL_SUPPORT
    platform_safe_mode(1, CFG_EMERGENCY_DL_TIMEOUT_MS);
#endif

    /* essential hardware initialization. e.g. timer, pll, uart... */
    platform_pre_init();

    #ifdef PL_PROFILING
    printf("#T#total_preplf_init=%d\n", get_timer(0));
    #endif
    print("\n%s Build Time: %s\n", MOD, BUILD_TIME);

    g_boot_mode = NORMAL_BOOT;

    #ifdef PL_PROFILING
    profiling_time = get_timer(0);
    #endif
    /* hardware initialization */
    platform_init();

    #ifdef PL_PROFILING
    printf("#T#total_plf_init=%d\n", get_timer(profiling_time));
    #endif
#if CFG_UART_TOOL_HANDSHAKE && (!defined(CFG_MEM_PRESERVED_MODE))
    #ifdef PL_PROFILING
    profiling_time = get_timer(0);
    #endif
    /* init uart handshake for sending 'ready' to tool and receiving handshake
     * pattern from tool in the background and we'll see the pattern later.
     * this can reduce the handshake time.
     */
    uart_handshake_init();

    #ifdef PL_PROFILING
    printf("#T#UART_hdshk=%d\n", get_timer(profiling_time));
    #endif
#endif  //#if CFG_UART_TOOL_HANDSHAKE && (!defined(CFG_MEM_PRESERVED_MODE))

    #ifdef PL_PROFILING
    profiling_time = get_timer(0);
    #endif
    part_init();
    part_dump();

    #ifdef PL_PROFILING
    printf("#T#part_init+dump=%d\n", get_timer(profiling_time));
    #endif

    #ifdef PL_PROFILING
    profiling_time = get_timer(0);
    #endif
    /* init security library */
    sec_lib_init();

    #ifdef PL_PROFILING
    printf("#T#sec_lib_init=%d\n", get_timer(profiling_time));
    #endif
}
Пример #15
0
int main(void)
{

  /* USER CODE BEGIN 1 */

	// create pointer to MX_SPI_Init() function in order to use it in utility functions
   MX_SPI1_Init_Pointer = &MX_SPI1_Init;

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  //MX_SPI1_Init();
  MX_TIM16_Init();
  MX_TIM17_Init();
  MX_USART1_UART_Init();
  MX_USART2_UART_Init();
  MX_WWDG_Init();

  /* USER CODE BEGIN 2 */
	// delete MX_SPI1_Init(); up there after regenerating code with mx cube

	//=================================================================================
	/// some basic setups
	//=================================================================================

	// input is up
	// output is down
	uart_queue_initialize(&uart_input_queue);
	uart_queue_initialize(&uart_output_queue);
	uart_queue_initialize(&uart_command_queue);

	// load setup_data from flash to struct: config_data_flash_struct setup_data;
	get_from_Flash();

	LED_init();

	if(setup_data.initied_0xAA!=0xAA)
	{
		reset_joint();
	}

	part_init();

	/// FIND DIRECTION = WAIT FOR FIRST 'R'
	DIRECTION_SET = 0;

	PROGRAMM_SENSOR = 0;
	SET_FLASH = 0;

	UP_STATE = IDLE;
	DOWN_STATE = IDLE;

	// init some huart stuff
	huart_DOWN = &huart1;
	huart_UP = &huart2;

	uint16_t si = 0;
	data_mode = 0;

	HAL_StatusTypeDef status;

	// create empty messages
	Line empty_command_line;
	empty_command_line.text[0] = empty;
	for(si=1; si < DOWN_MESSAGE_LENGTH-1; si++)
	{
		empty_command_line.text[si] = si;
	}

	empty_command_line.text[DOWN_MESSAGE_LENGTH-1] = ComputeCRCN(empty_command_line.text, DOWN_MESSAGE_LENGTH-1);
	empty_command_line.length = DOWN_MESSAGE_LENGTH;

	Line empty_up_line;
	empty_up_line.text[0] = empty;
	for(si=1; si < UP_MESSAGE_LENGTH-1; si++)
	{
		empty_up_line.text[si] = si;
	}

	empty_up_line.text[UP_MESSAGE_LENGTH-1] = ComputeCRCN(empty_up_line.text, UP_MESSAGE_LENGTH-1);
	empty_up_line.length = UP_MESSAGE_LENGTH;

	// start timer
	HAL_TIM_Base_Start(&htim16);
	HAL_TIM_Base_Start(&htim17);

	// send some initial init messages
	uart_queue_push_line(&uart_input_queue, &init_message_line);

	if(is_splitter(setup_data.type))
	{
		for(si = 0; si < 8; si++)
		{
			if(setup_data.splitter_outputs[si]==1)
			{
				uart_queue_push_line(&uart_input_queue, &splitter_message_lines[si]);
				skc[si] = 1;
			}
		}
	}

	int redled_timeout_timer = __HAL_TIM_GetCounter(&htim16);

	int init_timout_timer = __HAL_TIM_GetCounter(&htim16);

	interrupt_up_timeout_time = __HAL_TIM_GetCounter(&htim16);
	interrupt_down_timeout_time = __HAL_TIM_GetCounter(&htim16);

	int new_sensor_timer = __HAL_TIM_GetCounter(&htim16);

	int up_timeout_timer = __HAL_TIM_GetCounter(&htim16);

	int kids_timer[] = {-1,-1,-1,-1,-1,-1,-1,-1};

	transmittedUP=0;
	receivedUP=0;
	transmittedDOWN=0;
	receivedDOWN=0;

	uint8_t TIMEDOUT = 0;

	update_sensor_messages();

	HAL_HalfDuplex_EnableTransmitterReceiver(huart_DOWN);
	HAL_HalfDuplex_EnableTransmitterReceiver(huart_UP);
	HAL_UART_Receive_DMA(huart_UP, up_buffer, DOWN_MESSAGE_LENGTH + 1);
	HAL_UART_Receive_DMA(huart_DOWN, down_buffer, DOWN_MESSAGE_LENGTH + 1);

	led_set(1, 1, 0);

	//while(1);

	//if(is_splitter(setup_data.type))
	HAL_WWDG_Start(&hwwdg);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

	get_up_time = __HAL_TIM_GetCounter(&htim16);

	uint8_t WDGO = 0;

	uint8_t WDSET = 0;

  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

//	if((WD_UP && WD_DOWN) || DIRECTION_SET==0)
//	{
//		WD_UP = 0;
//		WD_DOWN = 0;
//		// for up and dwn we have own flags
//
//		HAL_WWDG_Refresh(&hwwdg, 127);
//	}

	if(DIRECTION_SET && WDSET==0)
	{
		HAL_WWDG_Start(&hwwdg);
		WDSET = 1;
		up_timeout_timer = __HAL_TIM_GetCounter(&htim16);
	}

	if(WDGO==0 || is_splitter(setup_data.type)==0)
	{
		HAL_WWDG_Refresh(&hwwdg, 127);
	}


	// if there was no poll from above a certain time = reset!
	if(__HAL_TIM_GetCounter(&htim16) - interrupt_up_timeout_time > 250)
	{
		WDGO = 1;

		get_up(1);

		interrupt_up_timeout_time = __HAL_TIM_GetCounter(&htim16);

		UP_STATE = IDLE;
	}


	// if there was no interrupt call for data from below
	if(__HAL_TIM_GetCounter(&htim16) - interrupt_down_timeout_time > 250)
	{
		WDGO = 1;

		HAL_UART_DMAStop(huart_DOWN);
		HAL_UART_DMAResume(huart_DOWN);

		if(!DIRECTION_SET)
		{
			HAL_HalfDuplex_EnableTransmitterReceiver(huart_DOWN);
			HAL_UART_Receive_DMA(huart_DOWN, down_buffer, DOWN_MESSAGE_LENGTH + 1);
		}

		interrupt_down_timeout_time = __HAL_TIM_GetCounter(&htim16);

		DOWN_STATE = IDLE;
	}

	  // wait for directions to be set
	if(!DIRECTION_SET)
	{
		led_set(8, 1, 8);
		continue;
	}

	led_set(8, 0, 8);

	if(__HAL_TIM_GetCounter(&htim16) - get_up_time > 20 && receivedUP == 0)
	{
		get_up(0);
	}

	// red blinking led
	if(__HAL_TIM_GetCounter(&htim16) - redled_timeout_timer > 250)
	{
		HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
		redled_timeout_timer = __HAL_TIM_GetCounter(&htim16);
	}

	if(__HAL_TIM_GetCounter(&htim16) - up_timeout_timer > 500)
	{
		WDGO = 1;
	}

	// sent init messages
	if(__HAL_TIM_GetCounter(&htim16) - init_timout_timer > 1000)
	{
		uart_queue_replace_push_line(&uart_input_queue, &init_message_line, setup_data.id);

		if(is_splitter(setup_data.type))
		{
			for(si = 0; si < 8; si++)
			{
				if(setup_data.splitter_outputs[si]==1)
				{
					uart_queue_push_line(&uart_input_queue, &splitter_message_lines[si]);
				}
			}
		}

		init_timout_timer = __HAL_TIM_GetCounter(&htim16);
	}

	if(SET_FLASH && UP_STATE == IDLE && DOWN_STATE == IDLE)
	{
		HAL_HalfDuplex_EnableTransmitter(huart_UP);

		set_in_Flash(&setup_data);
		SET_FLASH = 0;

		get_up(1);
	}

	if(PROGRAMM_SENSOR && UP_STATE == IDLE && DOWN_STATE == IDLE)
	{
		HAL_HalfDuplex_EnableTransmitter(huart_UP);

		if(setup_data.hardware == 2)
			init_2D(hspi1, GPIOA, GPIO_PIN_3);
		else
			init_2D(hspi1, GPIOB, GPIO_PIN_1);

		PROGRAMM_SENSOR = 0;

		get_up(1);
	}

	/// UP_STATE_MACHINE
	switch(UP_STATE)
	{
		case IDLE:
			if(SET_FLASH || PROGRAMM_SENSOR)
			{
				UP_STATE = IDLE;
				break;
			}

			if(receivedUP)
			{
				receivedUP = 0;

				if(up_buffer[0] == poll_receive /*&& __HAL_TIM_GetCounter(&htim16) - receivedUPtime < 5*/)
				{
					if(UP_RESEND_MESSAGE==0)
					{
						//update_sensor_messages();

						uint8_t n_up_messages = 0;

						while(uart_queue_is_empty(&uart_input_queue)==0 && (n_up_messages < UP_MESSAGES_MAX))
						{
							uart_queue_pop_line(&uart_input_queue, &send_up_line);
							memcpy(&poll_up_buffer[n_up_messages*(UP_MESSAGE_LENGTH)], send_up_line.text, UP_MESSAGE_LENGTH);

							n_up_messages++;
						}

						while(n_up_messages < UP_MESSAGES_MAX)
						{
							memcpy(&poll_up_buffer[n_up_messages*(UP_MESSAGE_LENGTH)], empty_up_line.text, UP_MESSAGE_LENGTH);

							n_up_messages++;
						}

						poll_up_size = n_up_messages*UP_MESSAGE_LENGTH;
					}

					UP_RESEND_MESSAGE = 0;
					UP_STATE = SENDING;

					HAL_HalfDuplex_EnableTransmitter(huart_UP);
					HAL_UART_Transmit_DMA(huart_UP, poll_up_buffer, poll_up_size);
					int something_else = __HAL_TIM_GetCounter(&htim16);

					if(up_buffer[1]!=empty)
					{
						memcpy(command_in_line.text, &up_buffer[1], DOWN_MESSAGE_LENGTH);
						command_in_line.length = DOWN_MESSAGE_LENGTH;

						uint8_t checksum = ComputeCRCN(command_in_line.text, DOWN_MESSAGE_LENGTH-1);

						if(checksum == command_in_line.text[DOWN_MESSAGE_LENGTH-1])
						{
							if(command_in_line.text[0] == poll_direct || getIDfromMessage(command_in_line.text)==setup_data.id)
							{
								uart_queue_push_line(&uart_command_queue, &command_in_line);

								if((command_in_line.text[0] == poll_send) && (setup_data.type==NODE_JOINT_TUT_TU))
									uart_queue_push_line(&uart_output_queue, &command_in_line);
							}
							else if(getIDfromMessage(command_in_line.text)==BROADCAST_ID)
							{
								uart_queue_push_line(&uart_command_queue, &command_in_line);
								uart_queue_push_line(&uart_output_queue, &command_in_line);
							}
							else
							{
								uart_queue_push_line(&uart_output_queue, &command_in_line);
							}
						}
					}

					if(is_splitter(setup_data.type))
					{
						/*__disable_irq();
						delayUS(10000);
						__enable_irq();*/
					}
					else
					{
						update_sensor_messages();
					}

					up_timeout_time = __HAL_TIM_GetCounter(&htim16);
				}
				else
				{
					//HAL_UART_DMAStop(huart_UP);
					//HAL_UART_DMAResume(huart_UP);

					// HAL_UART_Receive(huart_UP, &up_buffer[1], DOWN_MESSAGE_LENGTH, 2);

					get_up(1);
				}
			}
			break;

		case SENDING:
			if(transmittedUP)
			{
				UP_STATE = END;
				transmittedUP = 0;
			}
			else if(__HAL_TIM_GetCounter(&htim16) - up_timeout_time > 10)
			{
				HAL_UART_DMAStop(huart_UP);
				HAL_UART_DMAResume(huart_UP);
				UP_STATE = END;
			}
			break;

		case END:
			WD_UP = 1;
			up_timeout_timer = __HAL_TIM_GetCounter(&htim16);

			// work on commands

			UP_STATE = IDLE;
			get_up(1);
			break;
	}

	uint8_t s=0;

	switch(DOWN_STATE)
	{
		case DELAY:
			if(is_splitter(setup_data.type) || (__HAL_TIM_GetCounter(&htim16) - down_timeout_time > 7) /* && TIMEDOUT==0 */)
				DOWN_STATE = IDLE;

			/*else if (__HAL_TIM_GetCounter(&htim16) - down_timeout_time > 5)
				DOWN_STATE = IDLE;*/
			break;

		case IDLE:
			// do nothing
			if(SET_FLASH || PROGRAMM_SENSOR)
			{
				DOWN_STATE = IDLE;
				break;
			}


			if(is_splitter(setup_data.type))
			{
				if(splitter_kids_counter[i_p_splitter] > 10)
				{
					splitter_kids_counter[i_p_splitter] = 9;
					kids_timer[i_p_splitter] = __HAL_TIM_GetCounter(&htim16) + 200;
				}

				if(kids_timer[i_p_splitter]!=-1 && kids_timer[i_p_splitter] > __HAL_TIM_GetCounter(&htim16))
				{
					interrupt_down_timeout_time = __HAL_TIM_GetCounter(&htim16);
					DOWN_STATE = END;
					break;
				}

				if(skc[i_p_splitter] == 0)
				{
					send_down_line = send_down_line_now;
				}
				else
				{
					send_down_line = empty_command_line;
				}
			}


			if(skc[0]+skc[1]+skc[2]+skc[3]+skc[4]+skc[5]+skc[6]+skc[7] >= n_splitter)
			{
				if(uart_queue_is_empty(&uart_output_queue)==0)
				{
					uart_queue_pop_line(&uart_output_queue, &send_down_line_now);
					send_down_line = send_down_line_now;
					for(s=0; s<n_splitter; s++)
						skc[s] = 0;
				}
				else
				{
					send_down_line = empty_command_line;
					for(s=0; s<n_splitter; s++)
						skc[s] = 1;
				}
			}

			//send_down_line = empty_command_line;

			if(is_splitter(setup_data.type))
			{
				MPX_UART_Open(i_p_splitter);
			}

			DOWN_STATE = RECEIVING;

			//__disable_irq();
			HAL_HalfDuplex_EnableTransmitter(huart_DOWN);
			status = HAL_UART_Transmit(huart_DOWN, "R", 1, 3);

			status = HAL_UART_Transmit(huart_DOWN, send_down_line.text, DOWN_MESSAGE_LENGTH, 5);
			//__enable_irq();

			// DMA Receive for 3
			HAL_HalfDuplex_EnableTransmitterReceiver(huart_DOWN);
			HAL_UART_Receive_DMA(huart_DOWN, poll_down_buffer, UP_MESSAGE_LENGTH*UP_MESSAGES_MAX);

			down_timeout_time = __HAL_TIM_GetCounter(&htim16);
			break;

		case RECEIVING:
			if(receivedDOWN)
			{
				receivedDOWN = 0;
				splitter_kids_counter[i_p_splitter] = 0;
				skc[i_p_splitter] = 1;
				kids_timer[i_p_splitter] = -1;

				// no more transmitting checksum, only calculating
				uint8_t checksum = ComputeCRCN(&poll_down_buffer[poll_down_size-UP_MESSAGE_LENGTH], UP_MESSAGE_LENGTH-1);

				uint8_t i = 0;

				for(i=0; i<UP_MESSAGES_MAX; i++)
				{
					// check checksum
					if(1 /*checksum == poll_down_buffer[i*UP_MESSAGE_LENGTH-1]*/)
					{
						Line sensor_message_line;
						memcpy(sensor_message_line.text, &poll_down_buffer[i*UP_MESSAGE_LENGTH], UP_MESSAGE_LENGTH);
						sensor_message_line.length = UP_MESSAGE_LENGTH;

						if(sensor_message_line.text[0]==empty)
							break;

						// delete messages from T joint but the answer_data, there update angles[3]
						if(getIDfromMessage(sensor_message_line.text)==setup_data.id && setup_data.type == NODE_JOINT_TUT_TU && data_mode!=2)
						{
							if(sensor_message_line.text[0] == answer_data)
								angles[3] = ((sensor_message_line.text[15] << 8) & 0xFF00) + sensor_message_line.text[16];
						}
						else // for the other kinds (splitter, init, data from other) replace them if already in queue, ack messages always add to queue
						{
							// if it's from own child, add own id
							if(sensor_message_line.text[5]==0)
							{
								sensor_message_line.text[5] = (setup_data.id >> 24) & 0xFF;
								sensor_message_line.text[6] =  (setup_data.id >> 16) & 0xFF;
								sensor_message_line.text[7] = (setup_data.id >> 8) & 0xFF;
								sensor_message_line.text[8] = setup_data.id & 0xFF;

								// add splitter branch number
								if(is_splitter(setup_data.type))
									sensor_message_line.text[5] |= ((i_p_splitter << 4) & 0xF0);
							}

							UpdateCRCLine(&sensor_message_line);

							if(sensor_message_line.text[0] == answer_ack || sensor_message_line.text[0] == splitter)
								uart_queue_push_line(&uart_input_queue, &sensor_message_line);
							else
								uart_queue_replace_push_line(&uart_input_queue, &sensor_message_line, getIDfromMessage(sensor_message_line.text));
						}
					}
				}

				DOWN_STATE = END;
				TIMEDOUT = 0;
			}
			else if(__HAL_TIM_GetCounter(&htim16) - down_timeout_time > 13)