/* * 统计 task init * 被其他 task 调用 * Q: 怎么统计任务 * A: 当空闲任务 idle_ctr 越大时那么显然 cpu_usage 利用率越小 * 这里关键以什么为基准? 来计算利用率. */ void stat_init(void) { #if CRITICAL_METHOD == 3 OS_CPU_SR cpu_sr = sigset_mask; #endif // 延迟是为了与时钟节拍同步 // 这是恰好时 task_stat 运行 time_dly(2); ENTER_CRITICAL(); idle_ctr = 0; EXIT_CRITICAL(); time_dly(TICKS_PER_SEC); ENTER_CRITICAL(); idle_ctr_max = idle_ctr; stat_rdy = TRUE; EXIT_CRITICAL(); }
void local_flush_tlb_all(void) { unsigned long flags; unsigned long old_ctx; int entry; ENTER_CRITICAL(flags); /* Save old context and create impossible VPN2 value */ old_ctx = read_c0_entryhi(); write_c0_entrylo0(0); write_c0_entrylo1(0); entry = read_c0_wired(); /* Blast 'em all away. */ while (entry < current_cpu_data.tlbsize) { /* Make sure all entries differ. */ write_c0_entryhi(UNIQUE_ENTRYHI(entry)); write_c0_index(entry); mtc0_tlbw_hazard(); tlb_write_indexed(); entry++; } tlbw_use_hazard(); write_c0_entryhi(old_ctx); FLUSH_ITLB; EXIT_CRITICAL(flags); }
int32_t RingBuf_Write(ring_buffer_t* pRB, const uint8_t* data, uint32_t dataBytes) { uint32_t writeToEnd, bytesToCopy; INIT_CRITICAL(); ENTER_CRITICAL(); /* Calculate the maximum amount we can copy */ writeToEnd = pRB->size - pRB->wNdx; bytesToCopy = MIN(dataBytes, pRB->size - pRB->cnt); if (bytesToCopy != 0) { /* Copy as much as we can until we fall off the end of the buffer */ memcpy(&pRB->pBuf[pRB->wNdx], data, MIN(bytesToCopy, writeToEnd)); /* Check if we have more to copy to the front of the buffer */ if (writeToEnd < bytesToCopy) { memcpy(pRB->pBuf, data + writeToEnd, bytesToCopy - writeToEnd); } /* Update the wNdx */ pRB->wNdx = (pRB->wNdx + bytesToCopy) % pRB->size; pRB->cnt += dataBytes; } LEAVE_CRITICAL(); return bytesToCopy; }
/** * \brief Create and start a timer * \param[in] timeout_ms - the time timeout in ms * \param[in] periodic - non-zero for a periodic timer, 0 for single shot * \param[in] callback - timer callback function * \param[in] arg - callback function private data * \return non-negative integer - the timer handle - on success, -1 otherwise */ int timer_create(uint16_t timeout_ms, int periodic, void (*callback)(void *), void *arg) { int handle = -1; size_t i; /* Find a free timer */ for (i = 0; i < MAX_TIMERS; i++) { if (_timer[i].callback == NULL) { break; } } /* Make sure a valid timer is found */ if (i < MAX_TIMERS) { SR_ALLOC(); ENTER_CRITICAL(); /* Set up the timer */ if (periodic != 0) { _timer[i].periodic = (timeout_ms < 100) ? 1 : (timeout_ms / TIMER_RESOLUTION_MS); } else { _timer[i].periodic = 0; } _timer[i].callback = callback; _timer[i].arg = arg; _timer[i].expiry = _timer_tick + _timer[i].periodic; EXIT_CRITICAL(); handle = i; } return handle; }
void _clear_sem(INT16U sem) /***************************************************************************** * Function : See module specification (.h-file). *****************************************************************************/ { ENTER_CRITICAL(); semaphores &= (~sem); EXIT_CRITICAL(); }
void _signal_sem(INT16U sem) /***************************************************************************** * Function : See module specification (.h-file). *****************************************************************************/ { ENTER_CRITICAL(); semaphores |= sem; EXIT_CRITICAL(); }
/************************************************************************************** ** 函数名称 :watchdog_process() ** 函数功能 :定时喂狗 ** 入口参数 :无 ** 出口参数 : **************************************************************************************/ void watchdog_process(void) { if(dog_cnt > DOG_TIME){ dog_cnt = 0; ENTER_CRITICAL(); //喂狗 watchdog_weigou(); EXIT_CRITICAL(); } }
esp_err_t spi_set_mode(spi_host_t host, spi_mode_t *mode) { SPI_CHECK(host < SPI_NUM_MAX, "host num error", ESP_ERR_INVALID_ARG); SPI_CHECK(spi_object[host], "spi has not been initialized yet", ESP_FAIL); SPI_CHECK(mode, "parameter pointer is empty", ESP_ERR_INVALID_ARG); spi_object[host]->mode = *mode; ENTER_CRITICAL(); // Disable flash operation mode SPI[host]->user.flash_mode = false; if (SPI_MASTER_MODE == *mode) { // Set to Master mode SPI[host]->pin.slave_mode = false; SPI[host]->slave.slave_mode = false; // Master uses the entire hardware buffer to improve transmission speed SPI[host]->user.usr_mosi_highpart = false; SPI[host]->user.usr_miso_highpart = false; SPI[host]->user.usr_mosi = true; // Create hardware cs in advance SPI[host]->user.cs_setup = true; // Hysteresis to keep hardware cs SPI[host]->user.cs_hold = true; SPI[host]->user.duplex = true; SPI[host]->user.ck_i_edge = true; SPI[host]->ctrl2.mosi_delay_num = 0; SPI[host]->ctrl2.miso_delay_num = 1; } else { // Set to Slave mode SPI[host]->pin.slave_mode = true; SPI[host]->slave.slave_mode = true; SPI[host]->user.usr_miso_highpart = true; // MOSI signals are delayed by APB_CLK(80MHz) mosi_delay_num cycles SPI[host]->ctrl2.mosi_delay_num = 2; SPI[host]->ctrl2.miso_delay_num = 0; SPI[host]->slave.wr_rd_sta_en = 1; SPI[host]->slave1.status_bitlen = 31; SPI[host]->slave1.status_readback = 0; // Put the slave's miso on the highpart, so you can only send 256bits // In Slave mode miso, mosi length is the same SPI[host]->slave1.buf_bitlen = 255; SPI[host]->cmd.usr = 1; } SPI[host]->user.fwrite_dual = false; SPI[host]->user.fwrite_quad = false; SPI[host]->user.fwrite_dio = false; SPI[host]->user.fwrite_qio = false; SPI[host]->ctrl.fread_dual = false; SPI[host]->ctrl.fread_quad = false; SPI[host]->ctrl.fread_dio = false; SPI[host]->ctrl.fread_qio = false; SPI[host]->ctrl.fastrd_mode = true; EXIT_CRITICAL(); return ESP_OK; }
int32_t RingBuf_Free(ring_buffer_t* pRB, uint32_t bytesToFree) { INIT_CRITICAL(); ENTER_CRITICAL(); pRB->rNdx = (pRB->rNdx + bytesToFree) % pRB->size; pRB->cnt -= bytesToFree; LEAVE_CRITICAL(); return bytesToFree; }
INT8U systick_get(void) { INT8U return_value = 0; ENTER_CRITICAL(); return_value = tick; EXIT_CRITICAL(); return return_value; }
esp_err_t spi_set_clk_div(spi_host_t host, spi_clk_div_t *clk_div) { SPI_CHECK(host < SPI_NUM_MAX, "host num error", ESP_ERR_INVALID_ARG); SPI_CHECK(spi_object[host], "spi has not been initialized yet", ESP_FAIL); SPI_CHECK(clk_div, "parameter pointer is empty", ESP_ERR_INVALID_ARG); ENTER_CRITICAL(); if (SPI_MASTER_MODE == spi_object[host]->mode) { if (SPI_80MHz_DIV == *clk_div) { switch (host) { case CSPI_HOST: { SET_PERI_REG_MASK(PERIPHS_IO_MUX_CONF_U, SPI0_CLK_EQU_SYS_CLK); } break; case HSPI_HOST: { SET_PERI_REG_MASK(PERIPHS_IO_MUX_CONF_U, SPI1_CLK_EQU_SYS_CLK); } break; } SPI[host]->clock.clk_equ_sysclk = true; } else { // Configure the IO_MUX clock (required, otherwise the clock output will be confusing) switch (host) { case CSPI_HOST: { CLEAR_PERI_REG_MASK(PERIPHS_IO_MUX_CONF_U, SPI0_CLK_EQU_SYS_CLK); } break; case HSPI_HOST: { CLEAR_PERI_REG_MASK(PERIPHS_IO_MUX_CONF_U, SPI1_CLK_EQU_SYS_CLK); } break; } // FRE(SCLK) = clk_equ_sysclk ? 80MHz : APB_CLK(80MHz) / clkdiv_pre / clkcnt SPI[host]->clock.clk_equ_sysclk = false; SPI[host]->clock.clkdiv_pre = 0; SPI[host]->clock.clkcnt_n = *clk_div - 1; // In the master mode clkcnt_h = floor((clkcnt_n+1)/2-1). In the slave mode it must be 0 SPI[host]->clock.clkcnt_h = *clk_div / 2 - 1; // In the master mode clkcnt_l = clkcnt_n. In the slave mode it must be 0 SPI[host]->clock.clkcnt_l = *clk_div - 1; } } else { // Slave mode must be set to 0 SPI[host]->clock.val = 0; } EXIT_CRITICAL(); return ESP_OK; }
/* * FUNCTION * IMGPROC_Open * * DESCRIPTION * Set owner of the IMGPROC. While one owner open the IMGPROC, no one * is allowed to use IMGPROC untile the owern close it. * * CALLS * * PARAMETERS * * RETURNS * None * * GLOBALS AFFECTED * imgproc_dcb.owner */ kal_int32 API IMGPROC_Open(MMDI_SCENERIO_ID owner) { #if (defined(DRV_IDP_6219_SERIES)) ENTER_CRITICAL(); ASSERT(imgproc_dcb.owner == SCENARIO_UNKNOW_ID); imgproc_dcb.owner = owner; EXIT_CRITICAL(); DRVPDN_Disable(DRVPDN_CON3,DRVPDN_CON3_IMGPROC,PDN_IMGPROC); #endif return NO_ERROR; }
/******************************************************************************* 函 数 名: USART1_IRQHandler 功能说明: 串口中断处理 参 数: 无 返 回 值: 无 *******************************************************************************/ void USART2_IRQHandler(void) { if(USART2->SR&(1<<5))//接收到数据 { CPU_SR_ALLOC(); ENTER_CRITICAL(); fifo_putc(&phy_rcvUsartfifo, USART2->DR); EXIT_CRITICAL(); } USART2->SR &=~(1<<5); }
/* * ticks */ void time_set(u32 ticks) { #if CRITICAL_METHOD == 3 OS_CPU_SR cpu_sr = sigset_mask; #endif ENTER_CRITICAL(); jiffies = ticks; EXIT_CRITICAL(); }
bool EventBuffer::put(Event* event) { ENTER_CRITICAL(); m_tailPos = (m_tailPos + 1) % EVENT_BUFFER_SIZE; EXIT_CRITICAL(); if (m_tailPos == m_headPos) { return false; } ENTER_CRITICAL(); m_buffer[m_tailPos] = event; EXIT_CRITICAL(); return true; }
void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end) { struct mm_struct *mm = vma->vm_mm; int cpu = smp_processor_id(); if (cpu_context(cpu, mm) != 0) { unsigned long size, flags; unsigned long config6_flags; ENTER_CRITICAL(flags); disable_pgwalker(config6_flags); size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT; size = (size + 1) >> 1; if (size <= current_cpu_data.tlbsize/2) { int oldpid = read_c0_entryhi(); int newpid = cpu_asid(cpu, mm); start &= (PAGE_MASK << 1); end += ((PAGE_SIZE << 1) - 1); end &= (PAGE_MASK << 1); while (start < end) { int idx; write_c0_entryhi(start | newpid); start += (PAGE_SIZE << 1); mtc0_tlbw_hazard(); tlb_probe(); tlb_probe_hazard(); idx = read_c0_index(); write_c0_entrylo0(0); write_c0_entrylo1(0); if (idx < 0) continue; /* Make sure all entries differ. */ #ifndef CONFIG_NLM_VMIPS write_c0_entryhi(UNIQUE_ENTRYHI(idx)); #else __write_64bit_c0_register($10, 0, (UNIQUE_VMIPS_ENTRYHI(idx))); #endif mtc0_tlbw_hazard(); tlb_write_indexed(); } tlbw_use_hazard(); write_c0_entryhi(oldpid); } else { drop_mmu_context(mm, cpu); } FLUSH_ITLB; enable_pgwalker(config6_flags); EXIT_CRITICAL(flags); }
uint8_t _test_sem(uint16_t sem) /***************************************************************************** * Function : See module specification (.h-file). *****************************************************************************/ { uint8_t return_value = 0; ENTER_CRITICAL(); return_value = ((semaphores & sem) != 0); EXIT_CRITICAL(); return return_value; }
INT8U _test_sem(INT16U sem) /***************************************************************************** * Function : See module specification (.h-file). *****************************************************************************/ { INT8U return_value = 0; ENTER_CRITICAL(); return_value = ((semaphores & sem) != 0); EXIT_CRITICAL(); return return_value; }
static void task_stat(void) { #if CRITICAL_METHOD == 3 OS_CPU_SR cpu_sr = sigset_mask; #endif s32 run; s8 usage; while(FALSE == stat_rdy) { time_dly(2 * TICKS_PER_SEC); } while(1) { ENTER_CRITICAL(); //idle_ctr_run = idle_ctr; run = idle_ctr; idle_ctr = 0; EXIT_CRITICAL(); //printf("idle_ctr_max: %d\n", idle_ctr_max); if (idle_ctr_max > 0) { usage = (s8)(100 - 100 * run / idle_ctr_max); //printf("usage: %d, run: %d\n", usage, run); if (usage > 100) { cpu_usage = 100; } else if (usage < 0) { cpu_usage = 0; } else { cpu_usage = usage; } } else { cpu_usage = 0; } time_dly(TICKS_PER_SEC); } }
static esp_err_t spi_slave_trans(spi_host_t host, spi_trans_t trans) { SPI_CHECK(trans.bits.cmd >= 3 && trans.bits.cmd <= 16, "spi cmd must be longer than 3 bits and shorter than 16 bits", ESP_ERR_INVALID_ARG); SPI_CHECK(trans.bits.addr >= 1 && trans.bits.addr <= 32, "spi addr must be longer than 1 bits and shorter than 32 bits", ESP_ERR_INVALID_ARG); SPI_CHECK(trans.bits.miso <= 256, "spi miso must be shorter than 256 bits", ESP_ERR_INVALID_ARG); SPI_CHECK(trans.bits.mosi <= 256, "spi mosi must be shorter than 256 bits", ESP_ERR_INVALID_ARG); int x, y; ENTER_CRITICAL(); // Set cmd length and receive cmd SPI[host]->user2.usr_command_bitlen = trans.bits.cmd - 1; if (trans.cmd) { *trans.cmd = SPI[host]->user2.usr_command_value; } // Set addr length and transfer addr SPI[host]->slave1.wr_addr_bitlen = trans.bits.addr - 1; SPI[host]->slave1.rd_addr_bitlen = trans.bits.addr - 1; if (trans.addr) { *trans.addr = SPI[host]->addr; } // Set the length of the miso and transfer the miso if (trans.bits.miso && trans.miso) { for (x = 0; x < trans.bits.miso; x += 32) { y = x / 32; SPI[host]->data_buf[y + 8] = trans.miso[y]; } } // Call the event callback function to send a transfer start event if (spi_object[host]->event_cb) { spi_object[host]->event_cb(SPI_TRANS_START_EVENT, NULL); } // Receive mosi data if (trans.bits.mosi && trans.mosi) { for (x = 0; x < trans.bits.mosi; x += 32) { y = x / 32; trans.mosi[y] = SPI[host]->data_buf[y]; } } EXIT_CRITICAL(); return ESP_OK; }
int32_t RingBuf_Write1Byte(ring_buffer_t* pRB, const uint8_t *pcData) { uint32_t ret = 0; INIT_CRITICAL(); ENTER_CRITICAL(); if (pRB->cnt < pRB->size) { pRB->pBuf[pRB->wNdx] = pcData[0]; pRB->wNdx = (pRB->wNdx + 1) % pRB->size; pRB->cnt++; ret = 1; } LEAVE_CRITICAL(); return ret; }
int check_Restore_function(void) { ENTER_CRITICAL(); if(Restorechecking) { if(MS_TIMER - RESET_Button_pressed_start_time > RESET_BUTTON_PRESSED_TIME_DEFAULT){ Restorechecking = 0; EXIT_CRITICAL(); return 1; } } EXIT_CRITICAL(); return 0; }
int check_WPS_function(void) { ENTER_CRITICAL(); if(WPSchecking) { if(MS_TIMER - WPS_Button_pressed_start_time > WPS_BUTTON_PRESSED_TIME_WPS){ WPSchecking = 0; EXIT_CRITICAL(); return 1; } } EXIT_CRITICAL(); return 0; }
int32_t RingBuf_Read1Byte(ring_buffer_t* pRB, uint8_t *pData) { uint32_t ret = 0; INIT_CRITICAL(); ENTER_CRITICAL(); if (pRB->cnt != 0) { pData[0] = pRB->pBuf[pRB->rNdx]; pRB->rNdx = (pRB->rNdx + 1) % pRB->size; pRB->cnt--; ret = 1; } LEAVE_CRITICAL(); return ret; }
esp_err_t spi_slave_set_status(spi_host_t host, uint32_t *status) { SPI_CHECK(host < SPI_NUM_MAX, "host num error", ESP_ERR_INVALID_ARG); SPI_CHECK(spi_object[host], "spi has not been initialized yet", ESP_FAIL); SPI_CHECK(SPI_SLAVE_MODE == spi_object[host]->mode, "this function must used by spi slave mode", ESP_FAIL); SPI_CHECK(status, "parameter pointer is empty", ESP_ERR_INVALID_ARG); ENTER_CRITICAL(); SPI[host]->rd_status.val = *status; EXIT_CRITICAL(); return ESP_OK; }
u32 time_get(void) { #if CRITICAL_METHOD == 3 OS_CPU_SR cpu_sr = sigset_mask; #endif u32 ticks; ENTER_CRITICAL(); ticks = jiffies; EXIT_CRITICAL(); return (ticks); }
void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end) { struct mm_struct *mm = vma->vm_mm; int cpu = smp_processor_id(); if (cpu_context(cpu, mm) != 0) { unsigned long size, flags; int huge = is_vm_hugetlb_page(vma); ENTER_CRITICAL(flags); if (huge) { start = round_down(start, HPAGE_SIZE); end = round_up(end, HPAGE_SIZE); size = (end - start) >> HPAGE_SHIFT; } else {
/* * 取消 延时 * 同时也会取消因 semaphore or event 等设置的延时 */ u8 time_dly_resume(u8 prio) { #if CRITICAL_METHOD == 3 OS_CPU_SR cpu_sr = sigset_mask; #endif struct tcb *ptcb; u8 err = NO_ERR; err = general_check_prio(prio); if (NO_ERR != err) { goto fail; } ENTER_CRITICAL(); ptcb = tcb_prio_tbl[prio]; // check ptcb->delay = 0; // 如果有 semaphore event flag 挂起 if (TASK_STAT_RDY != (ptcb->stat & TASK_STAT_PEND_ANY)) { ptcb->stat &= ~TASK_STAT_PEND_ANY; ptcb->pend_to = TRUE; } else { ptcb->pend_to = FALSE; } if (TASK_STAT_RDY == (ptcb->stat & TASK_STAT_SUSPEND)) { tcb_enter_rdy(prio); EXIT_CRITICAL(); schedule(); } else { EXIT_CRITICAL(); } fail: return err; }
void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end) { struct mm_struct *mm = vma->vm_mm; int cpu = smp_processor_id(); if (cpu_context(cpu, mm) != 0) { unsigned long flags; int size; ENTER_CRITICAL(flags); size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT; size = (size + 1) >> 1; local_irq_save(flags); if (size <= current_cpu_data.tlbsize/2) { int oldpid = read_c0_entryhi(); int newpid = cpu_asid(cpu, mm); start &= (PAGE_MASK << 1); end += ((PAGE_SIZE << 1) - 1); end &= (PAGE_MASK << 1); while (start < end) { int idx; write_c0_entryhi(start | newpid); start += (PAGE_SIZE << 1); mtc0_tlbw_hazard(); tlb_probe(); tlb_probe_hazard(); idx = read_c0_index(); write_c0_entrylo0(0); write_c0_entrylo1(0); if (idx < 0) continue; /* Make sure all entries differ. */ write_c0_entryhi(UNIQUE_ENTRYHI(idx)); mtc0_tlbw_hazard(); tlb_write_indexed(); } tlbw_use_hazard(); write_c0_entryhi(oldpid); } else { drop_mmu_context(mm, cpu); } EXIT_CRITICAL(flags); }
/** * \brief Delete a timer * \param[in] handle - the timer handle to delete * \return 0 if the handle is valid, -1 otherwise */ int timer_delete(int handle) { int status = -1; if (handle < MAX_TIMERS) { SR_ALLOC(); ENTER_CRITICAL(); /* Clear the callback to delete the timer */ _timer[handle].callback = NULL; EXIT_CRITICAL(); status = 0; } return status; }