示例#1
0
void print_evt(ble_evt_t *p_ble_evt){
    
    uint32_t evt = p_ble_evt->header.evt_id;

    if( (evt >= BLE_GAP_EVT_BASE)  &&  (evt <= (BLE_GAP_EVT_LAST)) )
    {
        gap_event_decode(p_ble_evt);
        //SEGGER_RTT_printf(0, "Evt: 0x%#02x, %s", evt, msgs_gap_events[p_ble_evt->header.evt_id - BLE_GAP_EVT_BASE]);
    }
    else if( (evt >= BLE_GATTS_EVT_BASE)  &&  (evt <= (BLE_GATTS_EVT_LAST)) )
    {
        if(evt == BLE_GATTS_EVT_WRITE)
            SEGGER_RTT_printf(0, "GATTS: %s, handle: 0x%#04x, data: 0x%#02x\n", "BLE_GATTS_EVT_WRITE", p_ble_evt->evt.gatts_evt.params.write.handle, *p_ble_evt->evt.gatts_evt.params.write.data);
        else        
            SEGGER_RTT_printf(0, "GATTS: %s", msgs_gatts_events[p_ble_evt->header.evt_id - BLE_GATTS_EVT_BASE]);
    } 
    else if((evt >= BLE_GATTC_EVT_BASE) && (evt <= BLE_GATTC_EVT_LAST))
    {
        SEGGER_RTT_printf(0, "GATTC: %s", msgs_gattc_events[p_ble_evt->header.evt_id - BLE_GATTC_EVT_BASE]);
    }
    else if((evt >= BLE_EVT_BASE) && (evt <= BLE_EVT_LAST))
    {
        SEGGER_RTT_printf(0, "COMMON: %s", msgs_common_events[p_ble_evt->header.evt_id - BLE_EVT_BASE]);
    }
    else
        SEGGER_RTT_printf(0, "Undefined evt: 0x%#02x\n", evt);
}
/**
 * \brief I think of main as providing initialization and BLE event handling.  One of the event handlers is used by the LBL's service and is defined
 *  in the service_init() function.  service_init() calls into the LBL's BLE service's initialization code which identifies the UUIDs of the LBL service.
 *  \note Call service_init BEFORE advertising_init()

 *  The BLE event dispatcher (ble_evt_dispatch()) calls the LBL's BLE event dispatcher.  Communications with a client is through the client read/writing to characteristics.
 *  \note I assume the power_manage() function does a "good job" interacting with the SoftDevice stack to minimize the amount of power used up by the LBL.  I don't know how to fine tune
 *  power management at this time.  I  use examples from the nRF51 SDK as a crutch, assuming the code for most of main functionality is "cookie cutter" across examples and the Nordic engineers that
 *  write the examples realizes example code would be copy/pasted into our stuff.
 * @return while main is defined as an int, nothing is really returned.
 */
int main(void)
{
  //call flash_init() before initializing service.. the ble_lbl_service uses flash to access pH4 and 7 calibration info.... (wow - too many dependencies!)
  //initialize pstorage() - the way i'll read/write from flash.  POR is to use flash to store the calibration info for pH 4 and pH 7..
  //Note in the S110 Softdevice documentation for pstorage, there is a note:
  //  For implementation of interface included in the example, SoftDevice should be enabled and scheduler (if used) should be initialized prior to initializing this module.
//  ladybug_flash_init();
  //a good part of this is "cookie cutter" from the nRF51 SDK...my-o-my there is a lot of code for BLE (within SoftDevice) and once SoftDevice is initialized - unfortunately - there is no longer source code debugging.
  ble_stack_init();
  // The app timers rely on the BLE stack being initialized.  This means app timer initialization must happen after BLE initialization.
  timers_init();
  // (pstorage api access to) flash and the app timer used within the read/write flash functions require BLE and timers init first.
  ladybug_flash_init();
  // The device name is needed as a GAP parameter.  This is the first time a flash action (flash read) happens which means BLE and app timer init must happen first.
  char *p_deviceName;
  ladybug_get_device_name(&p_deviceName);
  SEGGER_RTT_printf(0,"Device name: %s \n",p_deviceName);
  SEGGER_RTT_printf(0,"String length: %d\n",strlen(p_deviceName));
  gap_params_init(p_deviceName);
  //call service_init() before calling advertising_init()...service_init() calls into the LBL's BLE initialization code (where the LBL peripheral service and characteristics are defined)
  service_init();
  advertising_init();
  conn_params_init();
  sec_params_init();
  advertising_start();
  // Enter main loop
  for (;;)
    {
      //Lazy write of values stored in flash
      //writing can get messed up if it is done inline with other BLE/sensing activity, and there is no rush.
      storeCalibrationValues_t *p_storeCalibrationValues;
      if (true == ladybug_there_are_calibration_values_to_write(&p_storeCalibrationValues)){
	  SEGGER_RTT_printf(0,"Writing calibration values to flash.  Number of bytes: %d\n",sizeof(storeCalibrationValues_t));
	  ladybug_flash_write(calibrationValues,(uint8_t *)p_storeCalibrationValues,sizeof(storeCalibrationValues_t),did_flash_write);
      }
      storePlantInfo_t *p_storePlantInfo;
      if (true == ladybug_there_are_plantInfo_values_to_write(&p_storePlantInfo)){
	  SEGGER_RTT_WriteString(0,"...Writing plantInfo values to flash\n");
	  ladybug_flash_write(plantInfo,(uint8_t *)p_storePlantInfo,sizeof(storePlantInfo_t),did_flash_write);
      }
      char *p_deviceName;
      if (true == ladybug_the_device_name_has_been_updated(&p_deviceName)){
	  SEGGER_RTT_WriteString(0,"Writing device name to flash\n");
	  ladybug_flash_write(deviceName,(uint8_t *)p_deviceName,DEVNAME_MAX_LEN,did_flash_write);
      }
      power_manage();
    }

}
示例#3
0
static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
{        SEGGER_RTT_WriteString(0, "received data \n");
        newData = true;
        memset(txt_data,0,sizeof(txt_data));
    for (uint32_t i = 0; i < length; i++)
    {
                //m_tx_data[i] = p_data[i];
                txt_data[i] = p_data[i];
                SEGGER_RTT_printf(0,"%c", p_data[i]);
        while(app_uart_put(p_data[i]) != NRF_SUCCESS);
    }
      SEGGER_RTT_printf(0, "to send: %s\n", txt_data);
        SEGGER_RTT_WriteString(0, "\n");
    while(app_uart_put('\n') != NRF_SUCCESS);
        
}
示例#4
0
//------------------------------------------------------
uint8_t read_batt()
{ uint32_t temp[3];
	//HAL_ADC_Start_IT(&hadc1);
	for(uint8_t i=0;i<3;i++)
	{
		HAL_ADC_Start(&hadc1);
		if (HAL_ADC_PollForConversion(&hadc1, 100) != HAL_OK)
			return 0;
		if ((HAL_ADC_GetState(&hadc1) & HAL_ADC_STATE_EOC_REG) == HAL_ADC_STATE_EOC_REG)
		{
			temp[i] = HAL_ADC_GetValue(&hadc1);
				if(temp[i]<batt_min)
					temp[i]=batt_min;
				if(temp[i]>batt_max)
					temp[i]=batt_max;
		}
		HAL_ADC_Stop(&hadc1);
	}
	temp[0]=(temp[0]+temp[1]+temp[2])/3;
	batt_status=(temp[0]-batt_min)*100/(batt_max-batt_min);
	SEGGER_RTT_printf(0,"power:%d\r\n",batt_status);			

	return 1;

}
示例#5
0
//-----------------------------------------------------
//处理接收到的数据
//return:1-收到命令正确;0-收到命令错误
void rece_dispatch()
{
	uint8_t len=buffer[0];
	uint8_t error=0;;
	if(buffer[1]!=0xfe || buffer[2]!=0x04 || (buffer[len]!=check_sum(&buffer[1],len-1)) )
	{
		//收到错误命令回复
		send_shakehand(0);
	}
	//解析命令
	switch(buffer[3])
	{
		case 0x01://确认命令
			break;
		case 0x02://初始化flash存储区
			flash_init();
			break;
		case 0x03://收到时间设置命令
			RTC_Set_datetime(&buffer[5]);
			RTC_AlarmConfig(buffer[8],buffer[9]+1);
			//SEGGER_RTT_printf(0,"set_time=%02d-%02d-%02d;%02d-%02d-%02d;\r\n",buffer[5],buffer[6],buffer[7],buffer[8],buffer[9],buffer[10]);
			break;
		case 0x04://step length
			sys_para.step_len=buffer[5];
			sys_para.height=*(uint16_t *)(&buffer[6]);
			//flash_erase_para_sector();
			break;
		case 0x05://return serial number
			send_version_info();		
			break;
		case 0x0a://模式切换
			if(buffer[5]>0x03)
				error=1;
			else
				curr_mode=buffer[5];
			break;
		case 0x06://read now  sports data
			send_sensor_data();
			break;
		case 0x09://中断批量传输
			bat_upload=0;
			break;
		default:
			send_shakehand(0);//收到异常命令S
			return;
	};
		SEGGER_RTT_printf(0,"rece comm=%x;\r\n",buffer[3]);

	if(buffer[3]!=0x01)
	{
		//回复确认收到
		if(error==0)
			send_shakehand(1);
		else
			send_shakehand(0);
	}

}
/**@brief Function for dispatching a BLE stack event to all modules with a BLE stack event handler.
 *
 * @details This function is called from the scheduler in the main loop after a BLE stack
 *          event has been received.
 *
 * @param[in] p_ble_evt  Bluetooth stack event.
 */
static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
{
  SEGGER_RTT_WriteString(0,"\n--> IN ble_evt_dispatch\n");
  SEGGER_RTT_printf(0,"connection handle: %d\n",p_ble_evt->evt.gap_evt.conn_handle);
  on_ble_evt(p_ble_evt);
  ble_conn_params_on_ble_evt(p_ble_evt);
  ladybug_BLE_on_ble_evt(&m_lbl, p_ble_evt);

}
/**
 * \callgraph
 * \brief This is a helper function to easily print out the hex value of each byte within an array of bytes
 * @param dest_bytes	the array of bytes to print out.
 * @param num_bytes	the number of bytes in the array.
 */
void display_bytes(uint8_t *dest_bytes,int num_bytes){
  SEGGER_RTT_WriteString(0,"****BYTES****\n");
  for (int i = 0; i < num_bytes; i++)
    {
      if (i > 0) SEGGER_RTT_WriteString(0,":");
      SEGGER_RTT_printf(0,"%02X",*dest_bytes);
      dest_bytes++;
    }
  SEGGER_RTT_WriteString(0,"\n");
}
/**@brief Function for error handling, which is called when an error has occurred.
 *
 * @warning This handler is an example only and does not fit a final product. You need to analyze
 *          how your product is supposed to react in case of error.
 * \note I decided to use the SEGGER_RTT printing functionality.  My understanding is the SEGGER_RTT calls can be left in code with no effect
 * when there is no terminal to output.  The con of this approach is I can't hook up a UART enabled terminal session and see what is going on without the debugger present.
 *
 * @param[in] error_code  Error code supplied to the handler.
 * @param[in] line_num    Line number where the handler is called.
 * @param[in] p_file_name Pointer to the file name.
 */
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
{
  // This call can be used for debug purposes during application development.
  // The SEGGER_RTT APIs will not cause a problem in production so I'm leaving them in.
  SEGGER_RTT_WriteString(0,"--->>>BUMMER!! In app_error_handler\n");
  SEGGER_RTT_printf(0,"error code: %d (or 0X%X if assert..base for BLE = 0x3000) Line number: %d file name: ",error_code,error_code,line_num);
  SEGGER_RTT_WriteString(0,p_file_name);
  SEGGER_RTT_WriteString(0,"\n");
  //  ble_debug_assert_handler(error_code, line_num, p_file_name);
}
/*lint -save -e14 */
__WEAK void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
{
    // On assert, the system can only recover with a reset.
//#ifndef DEBUG
//    NVIC_SystemReset();
//#else
uint32_t m_error_code;
uint32_t m_line_num;
const uint8_t * m_p_file_name;	
    // This call can be used for debug purposes during application development.
    // @note CAUTION: Activating this code will write the stack to flash on an error.
    //                This function should NOT be used in a final product.
    //                It is intended STRICTLY for development/debugging purposes.
    //                The flash write will happen EVEN if the radio is active, thus interrupting
    //                any communication.
    //                Use with care. Uncomment the line below to use.
    //ble_debug_assert_handler(error_code, line_num, p_file_name);
		static volatile uint8_t  s_file_name[128];
    static volatile uint16_t s_line_num;
    static volatile uint32_t s_error_code;

    strncpy((char *)s_file_name, (const char *)p_file_name, 127);
    s_file_name[128 - 1] = '\0';
    s_line_num                           = line_num;
    s_error_code                         = error_code;
    //UNUSED_VARIABLE(s_file_name);
    //UNUSED_VARIABLE(s_line_num);
    //UNUSED_VARIABLE(s_error_code);

    // WARNING: The PRIMASK register is set to disable ALL interrups during writing the error log.
    // 
    // Do not use __disable_irq() in normal operation.
    __disable_irq();
		SEGGER_RTT_printf(0, "File: %s, Line Number: %d, 0x%2x\n ",s_file_name,s_line_num, s_error_code);

    // The following variable helps Keil keep the call stack visible, in addition, it can be set to
    // 0 in the debugger to continue executing code after the error check.
    volatile bool loop = true;
    UNUSED_VARIABLE(loop);

    m_error_code = error_code;
    m_line_num = line_num;
    m_p_file_name = p_file_name;

    UNUSED_VARIABLE(m_error_code);
    UNUSED_VARIABLE(m_line_num);
    UNUSED_VARIABLE(m_p_file_name);
    __disable_irq();

    //while(loop);
//#endif // DEBUG
}
示例#10
0
// Function to decode IR signals and message to web if the car is hit
void ir_in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
      static uint8_t decoded = 0;
      static uint8_t offset = 0;
      static uint32_t prev = 0;

      if(offset >= 8)
          offset = 0;

      uint32_t usec = nrf_drv_timer_capture(&ir_timer, NRF_TIMER_CC_CHANNEL0); 
      if(offset) 
          SEGGER_RTT_printf(0, "%d\r\n", usec - prev);
      
      if((usec - prev) > 2000)
      {
          decoded += (1 << (offset - 1));
      }
      
      if(offset == 7) 
      {
          SEGGER_RTT_printf(0, "\r\n\n%d\n\r\n", decoded);
          new_hit_value();
          if(decoded != unique_car_ID && decoded >= 1 && decoded <= 16)
          {
              ble_lbs_on_button_change(&m_lbs, hit_counter, 0);            
          
              playNote(1516);
              nrf_delay_ms(50);
              playNote(1607);
          }

          decoded = 0;
      }
            

      prev = usec;
      offset++;
}
示例#11
0
//-------------------------------------------------
FLASH_ID Flash_Read_ID(void)
{
	FLASH_ID  Flash_id;

	FLASH_ENABLE;
	SPI2_WriteRead_Data(SPI_Flash_Read_ID);
	Flash_id.MXIC_ID 	 	 = SPI2_WriteRead_Data(DummyData);
	Flash_id.MemType_ID  	 = SPI2_WriteRead_Data(DummyData);
	Flash_id.MemDensity_ID 	 = SPI2_WriteRead_Data(DummyData);
	FLASH_DISABLE;

	SEGGER_RTT_printf(0,"rece %x;%x,%x\r\n",Flash_id.MXIC_ID,Flash_id.MemType_ID,Flash_id.MemDensity_ID);

	return 	Flash_id;
}
示例#12
0
/**@brief Function for application main entry. Does not return. */
int main(void)
{
      unsigned char reg = 0x00; //IC Identity Register
      nrf_gpio_cfg_output(30); //for the CS pin
      nrf_drv_gpiote_out_set(30);   //This should assert the CS for the SPI peripheral

		  m_transfer_completed = false;

    // Setup bsp module.
    bsp_configuration();

    nrf_drv_spi_config_t const config =
    {
        #if (SPI0_ENABLED == 1)
            .sck_pin  = SPIM0_SCK_PIN,
            .mosi_pin = SPIM0_MOSI_PIN,
            .miso_pin = SPIM0_MISO_PIN,
            .ss_pin   = SPIM0_SS_PIN,
        #elif (SPI1_ENABLED == 1)
            .sck_pin  = SPIM1_SCK_PIN,
            .mosi_pin = SPIM1_MOSI_PIN,
            .miso_pin = SPIM1_MISO_PIN,
            .ss_pin   = SPIM1_SS_PIN,
        #elif (SPI2_ENABLED == 1)
            .sck_pin  = SPIM2_SCK_PIN,
            .mosi_pin = SPIM2_MOSI_PIN,
            .miso_pin = SPIM2_MISO_PIN,
            .ss_pin   = SPIM2_SS_PIN,
        #endif
        .irq_priority = APP_IRQ_PRIORITY_LOW,
        .orc          = 0xCC,
        .frequency    = NRF_DRV_SPI_FREQ_1M,
        .mode         = NRF_DRV_SPI_MODE_0,
        .bit_order    = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST,
       .ss_pin       = NRF_DRV_SPI_PIN_NOT_USED, //added by DS - if not specified, the nrf_drv_spi_init() will set this to high, which is wrong, CS for AS3911 is active low
    };
    ret_code_t err_code = nrf_drv_spi_init(&m_spi_master, &config, spi_master_event_handler);
    APP_ERROR_CHECK(err_code);

  while(1)
	{
		  SPIWriteReg(0x18,0x27);
		   reg=SPIReadReg(0x18);
		   SEGGER_RTT_printf(0, " r value is %x\n", reg);
	}
}
示例#13
0
static void gap_event_decode(ble_evt_t *p_ble_evt){
    SEGGER_RTT_printf(0, "\nGAP Evt: %s", msgs_gap_events[p_ble_evt->header.evt_id - BLE_GAP_EVT_BASE]);
    SEGGER_RTT_printf(0, "\tConn handle: 0x%#02x\n", p_ble_evt->evt.gap_evt.conn_handle);
    switch(p_ble_evt->header.evt_id){
        case BLE_GAP_EVT_CONNECTED:
            //SEGGER_RTT_printf(0, "\tMax int: %d\n", (uint16_t)(p_ble_evt->evt.gap_evt.params.connected.conn_params.max_conn_interval * 1.25));
            SEGGER_RTT_printf(0, "\tMIN_CONN_INTERVAL: %d\n", (uint16_t)(p_ble_evt->evt.gap_evt.params.connected.conn_params.min_conn_interval * 1.25));
            SEGGER_RTT_printf(0, "\tCONN_SUP_TIMEOUT: %d\n", p_ble_evt->evt.gap_evt.params.connected.conn_params.conn_sup_timeout * 10);
            SEGGER_RTT_printf(0, "\tSLAVE_LATENCY: %d\n", p_ble_evt->evt.gap_evt.params.connected.conn_params.slave_latency);
            break;
        case BLE_GAP_EVT_CONN_PARAM_UPDATE:
            //SEGGER_RTT_printf(0, "\tMax int: %d\n", (uint16_t)(p_ble_evt->evt.gap_evt.params.conn_param_update.conn_params.max_conn_interval * 1.25));
            SEGGER_RTT_printf(0, "\tMIN_CONN_INTERVAL: %d\n", (uint16_t)(p_ble_evt->evt.gap_evt.params.conn_param_update.conn_params.min_conn_interval * 1.25));
            SEGGER_RTT_printf(0, "\tCONN_SUP_TIMEOUT: %d\n", p_ble_evt->evt.gap_evt.params.conn_param_update.conn_params.conn_sup_timeout * 10);
            SEGGER_RTT_printf(0, "\tSLAVE_LATENCY: %d\n", p_ble_evt->evt.gap_evt.params.conn_param_update.conn_params.slave_latency);
            break;
        case BLE_GAP_EVT_DISCONNECTED:
            SEGGER_RTT_printf(0, "\t%s\n", msgs_disconnect_reasons[p_ble_evt->evt.gap_evt.params.disconnected.reason - 0x12]);
            break;
        default:
            break;
    }
}
示例#14
0
//read mac address
static void get_mac_addr(uint8_t *p_mac_addr)
{
		uint32_t error_code;
		ble_gap_addr_t *p_mac_addr_t = (ble_gap_addr_t*)malloc(sizeof(ble_gap_addr_t));
		error_code = sd_ble_gap_address_get(p_mac_addr_t);
		//APP_ERROR_CHECK(error_code);
		uint8_t *d = p_mac_addr_t->addr;
		for ( uint8_t i = 6; i >0;)
		{	
			i--;
			p_mac_addr[5-i]= d[i];
		}
		free(p_mac_addr_t);
		p_mac_addr_t = NULL;

		SEGGER_RTT_printf(0,"mac=0x%x",p_mac_addr[0]);
			
}
示例#15
0
/**
  * @brief  Period elapsed callback in non blocking mode
  * @param  htim : TIM handle
  * @retval None
  */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
    __HAL_TIM_SET_COUNTER(htim, 0);

    switch (KB_state) {
    case BTN_Down:
        if ((len_KB_str == 0) || (pKB_str == NULL)) {
            HAL_TIM_Base_Stop_IT(htim);
            return;
        }
        KB_state = BTN_Up;

        char2KBID(*pKB_str);
        SEGGER_RTT_printf(0, "USB send char %c \r\n" , *pKB_str);
        pKB_str++;
        len_KB_str--;
        USBD_HID_SendReport(&USBD_Device, KB_USBBuf, HID_KB_SIZE);

        __HAL_TIM_SET_COUNTER(&TimHandle, 0);
        __HAL_TIM_SET_AUTORELOAD(&TimHandle, 200-1); // 200ms
        HAL_TIM_Base_Start_IT(&TimHandle);
        break;

    case BTN_Up:
        memset(KB_USBBuf, 0, 9);
        KB_USBBuf[0] = 1;
        USBD_HID_SendReport(&USBD_Device, KB_USBBuf, HID_KB_SIZE);
        if (len_KB_str == 0) {
            pKB_str = NULL;
            HAL_TIM_Base_Stop_IT(htim);
        } else {
            KB_state = BTN_Down;
            __HAL_TIM_SET_COUNTER(&TimHandle, 0);
            __HAL_TIM_SET_AUTORELOAD(&TimHandle, 200-1); // 200ms
            HAL_TIM_Base_Start_IT(&TimHandle);
        }
        break;
    default:
        pKB_str = NULL;
        HAL_TIM_Base_Stop_IT(htim);
        break;
    }
}
示例#16
0
/**@snippet [Handling the data received over BLE] */
static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
{	
	//Copy uint8_t array to string array then use sscanf
	char temp[length+1];
	for(int i = 0; i < length; i++)
		temp[i] = p_data[i];
	temp[length] = '\0';
	
	//Parse row number
	unsigned int num;
	sscanf(temp, "%u", &num);
	SEGGER_RTT_printf(0, "\nPlayer 2 dropped disc at column %u\n", num);
	
	//Check player status and input
	if(p1_turn)
		SEGGER_RTT_WriteString(0, "But it's not their turn yet!\n");
	else if(num < 0 || num >= WIDTH)
		SEGGER_RTT_WriteString(0, "That's not a valid column!\n");
	else 
		addToColumn(num);
}
/*********************************************************************
*
*       main
*/
void main(void) {
  int r;
  int CancelOp;

  do {
    _Cnt = 0;

    SEGGER_RTT_WriteString(0, "SEGGER Real-Time-Terminal Sample\r\n");
    SEGGER_RTT_WriteString(0, "Press <1> to continue in blocking mode (Application waits if necessary, no data lost)\r\n");
    SEGGER_RTT_WriteString(0, "Press <2> to continue in non-blocking mode (Application does not wait, data lost if fifo full)\r\n");
    do {
      r = SEGGER_RTT_WaitKey();
    } while ((r != '1') && (r != '2'));
    if (r == '1') {
      SEGGER_RTT_WriteString(0, "\r\nSelected <1>. Configuring RTT and starting...\r\n");
      SEGGER_RTT_ConfigUpBuffer(0, NULL, NULL, 0, SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL);
    } else {
      SEGGER_RTT_WriteString(0, "\r\nSelected <2>. Configuring RTT and starting...\r\n");
      SEGGER_RTT_ConfigUpBuffer(0, NULL, NULL, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP);
    }
    CancelOp = 0;
    do {
      //for (_Delay = 0; _Delay < 10000; _Delay++);
      SEGGER_RTT_printf(0, "Count: %d. Press <Space> to get back to menu.\r\n", _Cnt++);
      r = SEGGER_RTT_HasKey();
      if (r) {
        CancelOp = (SEGGER_RTT_GetKey() == ' ') ? 1 : 0;
      }
      //
      // Check if user selected to cancel the current operation
      //
      if (CancelOp) {
        SEGGER_RTT_WriteString(0, "Operation cancelled, going back to menu...\r\n");
        break;
      }
    } while (1);
    SEGGER_RTT_GetKey();
    SEGGER_RTT_WriteString(0, "\r\n");
  } while (1);
}
示例#18
0
static void prvModeAstroControlCallback(void *pvParameters)
{
    switch (state)
    {
        case MODE_ASTRO_STATE_STOP:
            xTimerStop(xModeAstroControlTimer, 0);
            break;
        case MODE_ASTRO_STATE_WAKE_SM:
            {
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
					if (((1 << motor) & motors) != 0)
					{
						vSmEnable(motor, 2);
					}
                }
            }
            state = MODE_ASTRO_STATE_BACKLASH_1;
            SEGGER_RTT_printf(0, "ModeAstro Control State Change: BACKLASH_1\n");
            break;
        case MODE_ASTRO_STATE_BACKLASH_1:
            {
                int32_t steps = direction == MODE_ASTRO_DIR_NORTH ? -1 * SM_SPR : SM_SPR;

                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
					if (((1 << motor) & motors) != 0)
					{
	                    ucSmMove(motor, steps);
					}
                }
            }
            state = MODE_ASTRO_STATE_WAIT_BACKLASH_1;
            SEGGER_RTT_printf(0, "ModeAstro Control State Change: WAIT_BACKLASH_1\n");
            break;
        case MODE_ASTRO_STATE_WAIT_BACKLASH_1:
            {
                bool all_stoped = true;
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    if (ucSmGetState(motor) != SM_STATE_STOP)
                    {
                        all_stoped = false;
                        continue;
                    }
                }
                if (all_stoped)
                {
                    state = MODE_ASTRO_STATE_BACKLASH_2;
                    SEGGER_RTT_printf(0, "ModeAstro Control State Change: BACKLASH_2\n");
                }
            }
            break;
        case MODE_ASTRO_STATE_BACKLASH_2:
            {
                int32_t steps = direction == MODE_ASTRO_DIR_NORTH ? SM_SPR : -1 * SM_SPR;

                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
					if (((1 << motor) & motors) != 0)
					{
	                    ucSmMove(motor, steps);
					}
                }
            }
            state = MODE_ASTRO_STATE_WAIT_BACKLASH_2;
            SEGGER_RTT_printf(0, "ModeAstro Control State Change: WAIT_BACKLASH_2\n");
            break;
        case MODE_ASTRO_STATE_WAIT_BACKLASH_2:
            {
                bool all_stoped = true;
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    if (ucSmGetState(motor) != SM_STATE_STOP)
                    {
                        all_stoped = false;
                        continue;
                    }
                }
                if (all_stoped)
                {
                    state = MODE_ASTRO_STATE_MOVE;
                    SEGGER_RTT_printf(0, "ModeAstro Control State Change: MOVE\n");
                }
            }
            break;
        case MODE_ASTRO_STATE_MOVE:
            {
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
					if (((1 << motor) & motors) != 0)
					{
						bSmMoveContinuousAstro(motor,
							direction == MODE_ASTRO_DIR_NORTH ? SM_CW : SM_CCW,
							gear_reduction,
							factor);
					}
				}
                
                state = MODE_ASTRO_STATE_WAIT_MOVE;
                SEGGER_RTT_printf(0, "ModeAstro Control State Change: WAIT_MOVE\n");
            }
            break;
        case MODE_ASTRO_STATE_WAIT_MOVE:
            {
                bool all_stoped = true;
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    if (ucSmGetState(motor) != SM_STATE_STOP)
                    {
                        all_stoped = false;
                        continue;
                    }
                    if (eep_params.sm[motor].power_save == 1) vSmEnable(motor, 0);
                }
                if (all_stoped)
                {
                    state = MODE_ASTRO_STATE_SLEEP_SM;
                    SEGGER_RTT_printf(0, "ModeAstro Control State Change: WAIT_PRE_TIME\n");
                }
            }
            break;
        case MODE_ASTRO_STATE_SLEEP_SM:
            {
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    if (eep_params.sm[motor].power_save == 1) vSmEnable(motor, 0);
                }
            }
            state = MODE_ASTRO_STATE_STOP;
            finished = true;
            SEGGER_RTT_printf(0, "ModeAstro Control State Change: STOP\n");
            break;
    }
}
示例#19
0
/**@brief Application main function.
 */
int main(void)
{
		/*
		beginning of Initializing services for the Nordic Board	
		
		
		*/
    uint32_t err_code;
    bool erase_bonds;
		//print start string to terminal
    uint8_t  start_string[] = START_STRING;
    printf("%s",start_string);
    // Initialize timer.
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
		nrf_drv_gpiote_init();
    uart_init();
    //buttons_leds_init(&erase_bonds);
    ble_stack_init();
        
    gap_params_init();
    services_init();
    advertising_init();
    conn_params_init();

    err_code = ble_advertising_start(BLE_ADV_MODE_FAST);

    APP_ERROR_CHECK(err_code);
		
      //More SPI Additions
         nrf_drv_spi_config_t const config =
    {
        #if (SPI0_ENABLED == 1)
            .sck_pin  = SPIM0_SCK_PIN,
            .mosi_pin = SPIM0_MOSI_PIN,
            .miso_pin = SPIM0_MISO_PIN,
            .ss_pin   = SPIM0_SS_PIN,
        #elif (SPI1_ENABLED == 1)
            .sck_pin  = SPIM1_SCK_PIN,
            .mosi_pin = SPIM1_MOSI_PIN,
            .miso_pin = SPIM1_MISO_PIN,
            .ss_pin   = SPIM1_SS_PIN,
        #elif (SPI2_ENABLED == 1)
            .sck_pin  = SPIM2_SCK_PIN,
            .mosi_pin = SPIM2_MOSI_PIN,
            .miso_pin = SPIM2_MISO_PIN,
            .ss_pin   = SPIM2_SS_PIN,
        #endif
        .irq_priority = APP_IRQ_PRIORITY_LOW,
        .orc          = 0xCC,
        .frequency    = NRF_DRV_SPI_FREQ_1M,
        .mode         = NRF_DRV_SPI_MODE_0,
        .bit_order    = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST,
    };
    ret_code_t err_code1 = nrf_drv_spi_init(&m_spi_master, &config, spi_master_event_handler);
    APP_ERROR_CHECK(err_code1);
		
		/*
		
		End of Initializing services for the Nordic Board	
		
		Beginning of CC1101 initializing.
		
		*/

    CC1101_Init();
		
		
		// Enter main loop.
		for (;;)
    {	
			//
			//for setting in receive mode
			//
			RecvDataPacket();
			SEGGER_RTT_WriteString(0,"RX data:");
			for(uint32_t i = 0; i<8;i++){
				SEGGER_RTT_printf(0,"%x",m_rx_data[i]);
			}
			SEGGER_RTT_WriteString(0,"\n");
            
			//
			//for sending data that was recieved from the BTLE event			
			//
       if(m_transfer_completed & newData)
       {
				m_transfer_completed = false;
        newData = false;
				SendDataPacket(txt_data, strlen((char *) txt_data) + 1);//Additional byte (5+1) is header byte
        nrf_delay_ms(1);
                    
       }
                
    }
}
void CC1101_Init(void){
	
	//sequence of SS pin on/off to indicate we are going to reset the system
	
	nrf_gpio_pin_clear(SPIM0_SS_PIN);
	nrf_delay_ms(1);
	nrf_gpio_pin_set(SPIM0_SS_PIN);
	nrf_delay_ms(1);
	nrf_gpio_pin_clear(SPIM0_SS_PIN);
	
	//strobe CC1101 reset
	uint8_t SRES = 0x30;
	SpiStrobe(SRES);	
	nrf_delay_ms(5);
	
	//calibrate CC1101
	CC1101_Calibrate();
	nrf_delay_ms(1);
	
}
示例#20
0
static void prvModeVideoControlCallback(void *pvParameters)
{
    if (state == MODE_VIDEO_STATE_MOVE)
    {
        for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
        {
            step_timer[motor] += 10;
        }

        interval_timer += 10;
    }

    switch (state)
    {
        case MODE_VIDEO_STATE_STOP:
            xTimerStop(xModeVideoControlTimer, 0);
            break;
        case MODE_VIDEO_STATE_WAKE_SM:
            {
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    if (eep_params.sm[motor].power_save == 0) vSmEnable(motor, 1);
                    if (eep_params.sm[motor].power_save == 2) vSmEnable(motor, 2);
                }
            }
            state = MODE_VIDEO_STATE_GOTO_START;
            SEGGER_RTT_printf(0, "ModeVideo Control State Change: GOTO_START\n");
            break;
        case MODE_VIDEO_STATE_GOTO_START:
            {
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    ucSmMove(motor, eep_params.mode_sms_positions[0].pos[motor] - lSmGetPosition(motor));
                }
            }
            state = MODE_VIDEO_STATE_WAIT_START;
            SEGGER_RTT_printf(0, "ModeVideo Control State Change: WAIT_START\n");
            break;
        case MODE_VIDEO_STATE_WAIT_START:
            if ((ucSmGetState(0) == SM_STATE_STOP) && (ucSmGetState(1) == SM_STATE_STOP) && (ucSmGetState(2) == SM_STATE_STOP))
            {
                state = MODE_VIDEO_STATE_MOVE;
                SEGGER_RTT_printf(0, "ModeVideo Control State Change: WAIT_PRE_TIME\n");
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    move_state[motor] = MODE_VIDEO_STATE_MOVE_WAIT_LEAD_IN;
                    step_timer[motor] = 0;
                }
                interval_timer = 0;
                start_to_end = true;
            }                
            break;
        case MODE_VIDEO_STATE_MOVE:
            {
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    switch (move_state[motor])
                    {
                        case MODE_VIDEO_STATE_MOVE_WAIT_LEAD_IN:
                            if (step_timer[motor] >= eep_params.mode_sms_leadin_count[motor])
                            {
                                move_state[motor] = MODE_VIDEO_STATE_MOVE_RUN;
                            }
                            break;
                        case MODE_VIDEO_STATE_MOVE_RUN:
                            {
                                // calculate available time for video movement
                                int32_t move_time = (int32_t)eep_params.mode_video_duration[motor];
                                int32_t ramp_time = (int32_t)eep_params.mode_sms_accel_count[motor] + (int32_t)eep_params.mode_sms_decel_count[motor];
                                int32_t run_time = move_time - ramp_time;
                    
                                // sanity checks
                                if (move_time < 100) move_time = 100;
                                if (run_time < 100) run_time = 100;

                                int32_t steps = 0;
                                if (start_to_end)
                                {
                                    steps = eep_params.mode_sms_positions[1].pos[motor] - lSmGetPosition(motor);
                                }
                                else
                                {
                                    steps = eep_params.mode_sms_positions[0].pos[motor] - lSmGetPosition(motor);
                                }
                    
                                if (eep_params.sm[motor].power_save == 1) vSmEnable(motor, 1);
                    
                                int32_t max_speed_steps = (int32_t)((int64_t)2000 * (int64_t)labs(steps) / (int64_t)(2 * run_time + ramp_time));
                                int32_t accel_steps = (int32_t)((int64_t)max_speed_steps * (int64_t)1000 / (int64_t)eep_params.mode_sms_accel_count[motor]);
                                int32_t decel_steps = (int32_t)((int64_t)max_speed_steps * (int64_t)1000 / (int64_t)eep_params.mode_sms_decel_count[motor]);

                                max_speed_steps = utilsMIN(max_speed_steps, eep_params.sm[motor].speed_max_steps);
                                max_speed_steps = utilsMAX(max_speed_steps, SM_SPR/16);
                                accel_steps = utilsMIN(accel_steps, eep_params.sm[motor].accel_steps);
                                accel_steps = utilsMAX(accel_steps, SM_SPR/16);
                                decel_steps = utilsMIN(decel_steps, eep_params.sm[motor].decel_steps);
                                decel_steps = utilsMAX(decel_steps, SM_SPR/16);

                                uint16_t max_speed = SM_STEPS_TO_MRAD(max_speed_steps);
                                uint16_t accel = SM_STEPS_TO_MRAD(accel_steps);
                                uint16_t decel = SM_STEPS_TO_MRAD(decel_steps);
                            
                                ucSmMoveEx(motor, steps, max_speed, accel, decel);

                                move_state[motor] = MODE_VIDEO_STATE_MOVE_WAIT_RUN;
                            }
                            break;
                        case MODE_VIDEO_STATE_MOVE_WAIT_RUN:
                            if (ucSmGetState(motor) == SM_STATE_STOP)
                            {
                                move_state[motor] = MODE_VIDEO_STATE_MOVE_WAIT_LEAD_OUT;
                                step_timer[motor] = 0;
                            }
                            break;
                        case MODE_VIDEO_STATE_MOVE_WAIT_LEAD_OUT:
                            if (step_timer[motor] >= eep_params.mode_sms_leadout_count[motor])
                            {
                                move_state[motor] = MODE_VIDEO_STATE_MOVE_DONE;
                            }
                            break;
                        case MODE_VIDEO_STATE_MOVE_DONE:
                            break;
                    }
                }
                
                bool all_done = true;
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    if (move_state[motor] != MODE_VIDEO_STATE_MOVE_DONE)
                    {
                        all_done = false;
                        continue;
                    }
                }
                if (all_done)
                {
                    state = MODE_VIDEO_STATE_WAIT_MOVE;
                    SEGGER_RTT_printf(0, "ModeVideo Control State Change: WAIT_MOVE\n");
                }
            }
            break;
        case MODE_VIDEO_STATE_WAIT_MOVE:
            {
                bool all_stoped = true;
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    if (ucSmGetState(motor) != SM_STATE_STOP)
                    {
                        all_stoped = false;
                        continue;
                    }
                    if (eep_params.sm[motor].power_save == 1) vSmEnable(motor, 0);
                }
                if (all_stoped)
                {
                    if (eep_params.mode_video_ping_pong)
                    {
                        start_to_end = !start_to_end;
                        interval_timer = 0;
                        for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                        {
                            move_state[motor] = MODE_VIDEO_STATE_MOVE_WAIT_LEAD_IN;
                            step_timer[motor] = 0;
                        }
                        state = MODE_VIDEO_STATE_MOVE;
                        SEGGER_RTT_printf(0, "ModeVideo Control State Change: MOVE\n");
                    }
                    else
                    {
                        state = MODE_VIDEO_STATE_GOTO_END;
                        SEGGER_RTT_printf(0, "ModeVideo Control State Change: GOTO_END\n");
                    }
                }
            }
            break;
        case MODE_VIDEO_STATE_GOTO_END:
            {
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    if (eep_params.sm[motor].power_save == 1) vSmEnable(motor, 1);
                    ucSmMove(motor, eep_params.mode_sms_positions[1].pos[motor] - lSmGetPosition(motor));
                }
            }
            state = MODE_VIDEO_STATE_WAIT_END;
            SEGGER_RTT_printf(0, "ModeVideo Control State Change: WAIT_END\n");
            break;
        case MODE_VIDEO_STATE_WAIT_END:
            {
                bool all_stoped = true;
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    if (ucSmGetState(motor) != SM_STATE_STOP)
                    {
                        all_stoped = false;
                        continue;
                    }
                    if (eep_params.sm[motor].power_save == 1) vSmEnable(motor, 0);
                }
                if (all_stoped)
                {
                    state = MODE_VIDEO_STATE_SLEEP_SM;
                    SEGGER_RTT_printf(0, "ModeVideo Control State Change: SLEEP_SM\n");
                }
            }
            break;
        case MODE_VIDEO_STATE_SLEEP_SM:
            {
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    if (eep_params.sm[motor].power_save == 1) vSmEnable(motor, 0);
                }
            }
            state = MODE_VIDEO_STATE_STOP;
            finished = true;
            SEGGER_RTT_printf(0, "ModeVideo Control State Change: STOP\n");
            break;
    }

    vModeVideoCalcTime();
}
示例#21
0
static void prvModeSmsControlCallback(void *pvParameters)
{
    if (state >= MODE_SMS_STATE_LOOP_BEGIN && state <= MODE_SMS_STATE_LOOP_END)
    {
        step_timer += 10;
        if (remaining_step_time > 0) remaining_step_time -= 10;
        interval_timer += 10;
    }

    switch (state)
    {
        case MODE_SMS_STATE_STOP:
            xTimerStop(xModeSmsControlTimer, 0);
            break;
        case MODE_SMS_STATE_WAKE_SM:
            {
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    if (eep_params.sm[motor].power_save == 0) vSmEnable(motor, 1);
                    if (eep_params.sm[motor].power_save == 2) vSmEnable(motor, 2);
                }
            }
            state = MODE_SMS_STATE_GOTO_START;
            SEGGER_RTT_printf(0, "ModeSms Control State Change: GOTO_START\n");
            break;
        case MODE_SMS_STATE_GOTO_START:
            {
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    ucSmMove(motor, eep_params.mode_sms_positions[0].pos[motor] - lSmGetPosition(motor));
                }
            }
            state = MODE_SMS_STATE_WAIT_START;
            SEGGER_RTT_printf(0, "ModeSms Control State Change: WAIT_START\n");
            break;
        case MODE_SMS_STATE_WAIT_START:
            if ((ucSmGetState(0) == SM_STATE_STOP) && (ucSmGetState(1) == SM_STATE_STOP) && (ucSmGetState(2) == SM_STATE_STOP))
            {
                if (!eep_params.mode_sms_use_slider_as_shutter)
                {
                    state = MODE_SMS_STATE_WAIT_PRE_TIME;
                    SEGGER_RTT_printf(0, "ModeSms Control State Change: WAIT_PRE_TIME\n");
                    step_timer = 0;
                    remaining_step_time = eep_params.mode_sms_pre_time;
                }
                else
                {
                    state = MODE_SMS_STATE_OPEN_SHUTTER;
                    SEGGER_RTT_printf(0, "ModeSms Control State Change: OPEN_SHUTTER\n");
                }
                interval_timer = 0;
            }                
            break;
        case MODE_SMS_STATE_OPEN_SHUTTER:
            {
                if (test_mode == mode_smsTEST_NONE)
                {
                    // calculate available time for mode_sms movement
                    int32_t avail_move_time = eep_params.mode_sms_interval
                        - (eep_params.mode_sms_pre_time
                            + eep_params.mode_sms_post_time
                            + eep_params.mode_sms_focus_time
                            + eep_params.mode_sms_exposure_time);
                
                    // reduce used time on ~90% and divide by 4 (for acceleration and deceleration)
                    avail_move_time = avail_move_time * 90 / 400;

                    // sanity check
                    if (avail_move_time < 100) avail_move_time = 100;

                    int32_t steps = eep_params.mode_sms_positions[0].pos[0] - lSmGetPosition(0);
                    
                    if (eep_params.sm[0].power_save == 1) vSmEnable(0, 1);
                    if (eep_params.mode_sms_optimize_accel)
                    {
                        // calculate optimal acceleration
                        uint16_t accel = SM_STEPS_TO_MRAD(labs(steps)) * 1000000UL / (avail_move_time * avail_move_time);
                        
                        accel = utilsMIN(accel, SM_STEPS_TO_MRAD(eep_params.sm[0].accel_steps));
                        accel = utilsMAX(accel, SM_STEPS_TO_MRAD(SM_SPR/8));
                        uint16_t max_speed = accel * avail_move_time / 1000;
                        
                        ucSmMoveEx(0, steps, max_speed, accel, accel);
                    }
                    else
                    {
                        ucSmMove(0, steps);
                    }
                    state = MODE_SMS_STATE_WAIT_OPEN_SHUTTER;
                    SEGGER_RTT_printf(0, "ModeSms Control State Change: WAIT_OPEN_SHUTTER\n");
                }
                else
                {
                    state = MODE_SMS_STATE_WAIT_PRE_TIME;
                    SEGGER_RTT_printf(0, "ModeSms Control State Change: WAIT_PRE_TIME\n");
                    step_timer = 0;
                    remaining_step_time = eep_params.mode_sms_pre_time;
                }
            }
            break;
        case MODE_SMS_STATE_WAIT_OPEN_SHUTTER:
            {
                if (ucSmGetState(0) == SM_STATE_STOP)
                {
                    if (eep_params.sm[0].power_save == 1) vSmEnable(0, 0);
                    state = MODE_SMS_STATE_WAIT_PRE_TIME;
                    SEGGER_RTT_printf(0, "ModeSms Control State Change: WAIT_PRE_TIME\n");
                    step_timer = 0;
                    remaining_step_time = eep_params.mode_sms_pre_time;
                }
            }
            break;
        case MODE_SMS_STATE_WAIT_PRE_TIME:
            if (step_timer >= eep_params.mode_sms_pre_time || test_mode == mode_smsTEST_EXPOSE_NOW)
            {
                state = MODE_SMS_STATE_WAIT_FOCUS_TIME;
                SEGGER_RTT_printf(0, "ModeSms Control State Change: WAIT_FOCUS_TIME\n");
                step_timer = 0;
                remaining_step_time = eep_params.mode_sms_focus_time;
                vCamFocus();
            }
            break;
        case MODE_SMS_STATE_WAIT_FOCUS_TIME:
            if (step_timer >= eep_params.mode_sms_focus_time)
            {
                current_step++;
                vCamShutter();
                
                state = MODE_SMS_STATE_WAIT_EXPOSURE_TIME;
                SEGGER_RTT_printf(0, "ModeSms Control State Change: WAIT_EXPOSURE_TIME\n");
                step_timer = 0;
                remaining_step_time = eep_params.mode_sms_exposure_time;
            }
            break;
        case MODE_SMS_STATE_WAIT_EXPOSURE_TIME:
            if (step_timer >= eep_params.mode_sms_exposure_time)
            {
                if (test_mode == mode_smsTEST_EXPOSE_NOW)
                {
                    state = MODE_SMS_STATE_STOP;
                    SEGGER_RTT_printf(0, "ModeSms Control State Change: STOP\n");
                } 
                else 
                {
                    state = MODE_SMS_STATE_WAIT_POST_TIME;
                    SEGGER_RTT_printf(0, "ModeSms Control State Change: WAIT_POST_TIME\n");
                }
                step_timer = 0;
                remaining_step_time = eep_params.mode_sms_post_time;
                vCamClear();
            }
            break;
        case MODE_SMS_STATE_WAIT_POST_TIME:
            if (step_timer >= eep_params.mode_sms_post_time)
            {
                if (current_step >= eep_params.mode_sms_count && test_mode == mode_smsTEST_NONE)
                {
                    state = MODE_SMS_STATE_GOTO_END;
                    SEGGER_RTT_printf(0, "ModeSms Control State Change: GOTO_END\n");
                }
                else
                {
                    state = MODE_SMS_STATE_MOVE;
                    SEGGER_RTT_printf(0, "ModeSms Control State Change: MOVE\n");
                }
            }
            break;
        case MODE_SMS_STATE_MOVE:
            {
                if (test_mode == mode_smsTEST_NONE)
                {
                    // calculate available time for mode_sms movement
                    int32_t avail_move_time = eep_params.mode_sms_interval 
                        - (eep_params.mode_sms_pre_time 
                            + eep_params.mode_sms_post_time 
                            + eep_params.mode_sms_focus_time 
                            + eep_params.mode_sms_exposure_time);
                
                    if (!eep_params.mode_sms_use_slider_as_shutter)
                    {
                        // reduce used time on ~90% and divide by 2 (for acceleration and deceleration)
                        avail_move_time = avail_move_time * 90 / 200;
                    }
                    else
                    {
                        // reduce used time on ~90% and divide by 4 (for acceleration and deceleration)
                        avail_move_time = avail_move_time * 90 / 400;
                    }

                    // sanity check
                    if (avail_move_time < 100) avail_move_time = 100;

                    for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                    {
                        int32_t steps = 0;

                        if (motor > 0 || !eep_params.mode_sms_use_slider_as_shutter)
                        {
                            if (current_step < eep_params.mode_sms_leadin_count[motor] + 1)
                            {
                                steps = 0;
                            }
                            else if (current_step < eep_params.mode_sms_leadin_count[motor] + eep_params.mode_sms_accel_count[motor])
                            {
                                steps = accel_steps_x1000[motor] * ((int32_t)current_step - (int32_t)eep_params.mode_sms_leadin_count[motor]) / 1000L;
                            }
                            else if (current_step < eep_params.mode_sms_count - eep_params.mode_sms_decel_count[motor] - eep_params.mode_sms_leadout_count[motor] + 1)
                            {
                                steps = run_steps[motor];
                            }
                            else if (current_step < eep_params.mode_sms_count - eep_params.mode_sms_leadout_count[motor])
                            {
                                steps = decel_steps_x1000[motor] * ((int32_t)eep_params.mode_sms_count - (int32_t)eep_params.mode_sms_leadout_count[motor] - (int32_t)current_step) / 1000L;
                            }
                            else
                            {
                                steps = 0;
                            }
                        
                            // prevent the motor to move beyond the end position
                            if (eep_params.mode_sms_positions[1].pos[motor] - eep_params.mode_sms_positions[0].pos[motor] >= 0)
                            {
                                if (lSmGetPosition(motor) + steps > eep_params.mode_sms_positions[1].pos[motor])
                                {
                                    steps = eep_params.mode_sms_positions[1].pos[motor] - lSmGetPosition(motor);
                                }
                            }
                            else
                            {
                                if (lSmGetPosition(motor) + steps < eep_params.mode_sms_positions[1].pos[motor])
                                {
                                    steps = eep_params.mode_sms_positions[1].pos[motor] - lSmGetPosition(motor);
                                }
                            }
                        }
                        else
                        {
                            steps = eep_params.mode_sms_positions[1].pos[0] - lSmGetPosition(0);
                        }                
                
                        if (eep_params.sm[motor].power_save == 1) vSmEnable(motor, 1);
                        if (eep_params.mode_sms_optimize_accel)
                        {
                            // calculate optimal acceleration
                            uint16_t accel = SM_STEPS_TO_MRAD(labs(steps)) * 1000000UL / (avail_move_time * avail_move_time);
                
                            accel = utilsMIN(accel, SM_STEPS_TO_MRAD(eep_params.sm[motor].accel_steps));
                            accel = utilsMAX(accel, SM_STEPS_TO_MRAD(SM_SPR/8));
                            uint16_t max_speed = accel * avail_move_time / 1000;
                                
                            ucSmMoveEx(motor, steps, max_speed, accel, accel);
                        }
                        else
                        {
                            ucSmMove(motor, steps);
                        }
                    }
                    state = MODE_SMS_STATE_WAIT_MOVE;
                    SEGGER_RTT_printf(0, "ModeSms Control State Change: WAIT_MOVE\n");
                }
                else
                {
                    state = MODE_SMS_STATE_WAIT_INTERVAL;
                    SEGGER_RTT_printf(0, "ModeSms Control State Change: WAIT_INTERVAL\n");
                }
            }
            break;
        case MODE_SMS_STATE_WAIT_MOVE:
            {
                bool all_stoped = true;
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    if (ucSmGetState(motor) != SM_STATE_STOP)
                    {
                        all_stoped = false;
                        continue;
                    }
                    if (eep_params.sm[motor].power_save == 1) vSmEnable(motor, 0);
                }
                if (all_stoped)
                {
                    state = MODE_SMS_STATE_WAIT_INTERVAL;
                    SEGGER_RTT_printf(0, "ModeSms Control State Change: WAIT_INTERVAL\n");
                }
            }
            break;
        case MODE_SMS_STATE_WAIT_INTERVAL:
            if (interval_timer >= eep_params.mode_sms_interval)
            {
                if (interval_timer > eep_params.mode_sms_interval)
                {
                    eep_params.mode_sms_interval = interval_timer + 50;
                }                    
                if (!eep_params.mode_sms_use_slider_as_shutter)
                {
                    state = MODE_SMS_STATE_WAIT_PRE_TIME;
                    SEGGER_RTT_printf(0, "ModeSms Control State Change: WAIT_PRE_TIME\n");
                    step_timer = 0;
                    remaining_step_time = eep_params.mode_sms_pre_time;
                }
                else
                {
                    state = MODE_SMS_STATE_OPEN_SHUTTER;
                    SEGGER_RTT_printf(0, "ModeSms Control State Change: OPEN_SHUTTER\n");
                }
                
                interval_timer = 0;
                current_loop++;
            }
            break;
        case MODE_SMS_STATE_GOTO_END:
            {
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    if (eep_params.sm[motor].power_save == 1) vSmEnable(motor, 1);
                    ucSmMove(motor, eep_params.mode_sms_positions[1].pos[motor] - lSmGetPosition(motor));
                }
            }
            state = MODE_SMS_STATE_WAIT_END;
            SEGGER_RTT_printf(0, "ModeSms Control State Change: WAIT_END\n");
            break;
        case MODE_SMS_STATE_WAIT_END:
            {
                bool all_stoped = true;
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    if (ucSmGetState(motor) != SM_STATE_STOP)
                    {
                        all_stoped = false;
                        continue;
                    }
                    if (eep_params.sm[motor].power_save == 1) vSmEnable(motor, 0);
                }
                if (all_stoped)
                {
                    state = MODE_SMS_STATE_SLEEP_SM;
                    SEGGER_RTT_printf(0, "ModeSms Control State Change: SLEEP_SM\n");
                    step_timer = 0;
                    remaining_step_time = 0;
                    interval_timer = 0;
                    current_loop++;
                }
            }
            break;
        case MODE_SMS_STATE_SLEEP_SM:
            {
                for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
                {
                    if (eep_params.sm[motor].power_save == 1) vSmEnable(motor, 0);
                }
            }
            state = MODE_SMS_STATE_STOP;
            finished = true;
            SEGGER_RTT_printf(0, "ModeSms Control State Change: STOP\n");
            break;
    }

    vModeSmsCalcTime();
}
示例#22
0
/*********************************************************************
*
*       main
*/
void main(void) {

  SEGGER_RTT_ConfigUpBuffer(0, NULL, NULL, 0, SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL);

  SEGGER_RTT_WriteString(0, "SEGGER Real-Time-Terminal Sample\r\n\r\n");
  SEGGER_RTT_WriteString(0, "###### Testing SEGGER_printf() ######\r\n");

  SEGGER_RTT_printf(0, "printf Test: %%c,         'S' : %c.\r\n", 'S');
  SEGGER_RTT_printf(0, "printf Test: %%5c,        'E' : %5c.\r\n", 'E');
  SEGGER_RTT_printf(0, "printf Test: %%-5c,       'G' : %-5c.\r\n", 'G');
  SEGGER_RTT_printf(0, "printf Test: %%5.3c,      'G' : %-5c.\r\n", 'G');
  SEGGER_RTT_printf(0, "printf Test: %%.3c,       'E' : %-5c.\r\n", 'E');
  SEGGER_RTT_printf(0, "printf Test: %%c,         'R' : %c.\r\n", 'R');

  SEGGER_RTT_printf(0, "printf Test: %%s,      \"RTT\" : %s.\r\n", "RTT");
  SEGGER_RTT_printf(0, "printf Test: %%s, \"RTT\\r\\nRocks.\" : %s.\r\n", "RTT\r\nRocks.");

  SEGGER_RTT_printf(0, "printf Test: %%u,       12345 : %u.\r\n", 12345);
  SEGGER_RTT_printf(0, "printf Test: %%+u,      12345 : %+u.\r\n", 12345);
  SEGGER_RTT_printf(0, "printf Test: %%.3u,     12345 : %.3u.\r\n", 12345);
  SEGGER_RTT_printf(0, "printf Test: %%.6u,     12345 : %.6u.\r\n", 12345);
  SEGGER_RTT_printf(0, "printf Test: %%6.3u,    12345 : %6.3u.\r\n", 12345);
  SEGGER_RTT_printf(0, "printf Test: %%8.6u,    12345 : %8.6u.\r\n", 12345);
  SEGGER_RTT_printf(0, "printf Test: %%08u,     12345 : %08u.\r\n", 12345);
  SEGGER_RTT_printf(0, "printf Test: %%08.6u,   12345 : %08.6u.\r\n", 12345);
  SEGGER_RTT_printf(0, "printf Test: %%0u,      12345 : %0u.\r\n", 12345);
  SEGGER_RTT_printf(0, "printf Test: %%-.6u,    12345 : %-.6u.\r\n", 12345);
  SEGGER_RTT_printf(0, "printf Test: %%-6.3u,   12345 : %-6.3u.\r\n", 12345);
  SEGGER_RTT_printf(0, "printf Test: %%-8.6u,   12345 : %-8.6u.\r\n", 12345);
  SEGGER_RTT_printf(0, "printf Test: %%-08u,    12345 : %-08u.\r\n", 12345);
  SEGGER_RTT_printf(0, "printf Test: %%-08.6u,  12345 : %-08.6u.\r\n", 12345);
  SEGGER_RTT_printf(0, "printf Test: %%-0u,     12345 : %-0u.\r\n", 12345);

  SEGGER_RTT_printf(0, "printf Test: %%u,      -12345 : %u.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%+u,     -12345 : %+u.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%.3u,    -12345 : %.3u.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%.6u,    -12345 : %.6u.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%6.3u,   -12345 : %6.3u.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%8.6u,   -12345 : %8.6u.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%08u,    -12345 : %08u.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%08.6u,  -12345 : %08.6u.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%0u,     -12345 : %0u.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%-.6u,   -12345 : %-.6u.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%-6.3u,  -12345 : %-6.3u.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%-8.6u,  -12345 : %-8.6u.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%-08u,   -12345 : %-08u.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%-08.6u, -12345 : %-08.6u.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%-0u,    -12345 : %-0u.\r\n", -12345);

  SEGGER_RTT_printf(0, "printf Test: %%d,      -12345 : %d.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%+d,     -12345 : %+d.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%.3d,    -12345 : %.3d.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%.6d,    -12345 : %.6d.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%6.3d,   -12345 : %6.3d.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%8.6d,   -12345 : %8.6d.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%08d,    -12345 : %08d.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%08.6d,  -12345 : %08.6d.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%0d,     -12345 : %0d.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%-.6d,   -12345 : %-.6d.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%-6.3d,  -12345 : %-6.3d.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%-8.6d,  -12345 : %-8.6d.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%-08d,   -12345 : %-08d.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%-08.6d, -12345 : %-08.6d.\r\n", -12345);
  SEGGER_RTT_printf(0, "printf Test: %%-0d,    -12345 : %-0d.\r\n", -12345);

  SEGGER_RTT_printf(0, "printf Test: %%x,      0x1234ABC : %x.\r\n", 0x1234ABC);
  SEGGER_RTT_printf(0, "printf Test: %%+x,     0x1234ABC : %+x.\r\n", 0x1234ABC);
  SEGGER_RTT_printf(0, "printf Test: %%.3x,    0x1234ABC : %.3x.\r\n", 0x1234ABC);
  SEGGER_RTT_printf(0, "printf Test: %%.6x,    0x1234ABC : %.6x.\r\n", 0x1234ABC);
  SEGGER_RTT_printf(0, "printf Test: %%6.3x,   0x1234ABC : %6.3x.\r\n", 0x1234ABC);
  SEGGER_RTT_printf(0, "printf Test: %%8.6x,   0x1234ABC : %8.6x.\r\n", 0x1234ABC);
  SEGGER_RTT_printf(0, "printf Test: %%08x,    0x1234ABC : %08x.\r\n", 0x1234ABC);
  SEGGER_RTT_printf(0, "printf Test: %%08.6x,  0x1234ABC : %08.6x.\r\n", 0x1234ABC);
  SEGGER_RTT_printf(0, "printf Test: %%0x,     0x1234ABC : %0x.\r\n", 0x1234ABC);
  SEGGER_RTT_printf(0, "printf Test: %%-.6x,   0x1234ABC : %-.6x.\r\n", 0x1234ABC);
  SEGGER_RTT_printf(0, "printf Test: %%-6.3x,  0x1234ABC : %-6.3x.\r\n", 0x1234ABC);
  SEGGER_RTT_printf(0, "printf Test: %%-8.6x,  0x1234ABC : %-8.6x.\r\n", 0x1234ABC);
  SEGGER_RTT_printf(0, "printf Test: %%-08x,   0x1234ABC : %-08x.\r\n", 0x1234ABC);
  SEGGER_RTT_printf(0, "printf Test: %%-08.6x, 0x1234ABC : %-08.6x.\r\n", 0x1234ABC);
  SEGGER_RTT_printf(0, "printf Test: %%-0x,    0x1234ABC : %-0x.\r\n", 0x1234ABC);

  SEGGER_RTT_printf(0, "printf Test: %%p,      &_Cnt      : %p.\r\n", &_Cnt);

  SEGGER_RTT_WriteString(0, "###### SEGGER_printf() Tests done. ######\r\n");
  do {
    _Cnt++;
  } while (1);
}