static msg_t Th2(void *p) { (void)p; chRegSetThreadName("Th2"); while (TRUE) { /////DEVICE 2/////////// if(palReadPad(GPIO1_PORT, GPIO1_PAD) != PAL_HIGH) { palWritePad(GPIO22_PORT,GPIO22_PAD,PAL_HIGH); chSemWait(&mySemaphore); chprintf((BaseSequentialStream *)&SD1, "D2ON\r\n"); chSemSignal(&mySemaphore); }else{ palWritePad(GPIO22_PORT,GPIO22_PAD,PAL_LOW); chSemWait(&mySemaphore); chprintf((BaseSequentialStream *)&SD1, "D2OFF\r\n"); chSemSignal(&mySemaphore); } chThdSleepMilliseconds(1000); } return 0; }
static void test_002_002_execute(void) { /* The function chSemWait() is invoked, after return the counter and the returned message are tested. The semaphore is signaled by another thread.*/ test_set_step(1); { msg_t msg; msg = chSemWait(&gsem1); test_assert_lock(chSemGetCounterI(&gsem1) == 0, "wrong counter value"); test_assert(MSG_OK == msg, "wrong returned message"); } /* The function chSemWait() is invoked, after return the counter and the returned message are tested. The semaphore is reset by another thread.*/ test_set_step(2); { msg_t msg; msg = chSemWait(&gsem2); test_assert_lock(chSemGetCounterI(&gsem2) == 0, "wrong counter value"); test_assert(MSG_RESET == msg, "wrong returned message"); } }
static void rt_test_005_001_execute(void) { /* [5.1.1] The function chSemWait() is invoked, after return the counter and the returned message are tested.*/ test_set_step(1); { msg_t msg; msg = chSemWait(&sem1); test_assert_lock(chSemGetCounterI(&sem1) == 0, "wrong counter value"); test_assert(MSG_OK == msg, "wrong returned message"); } /* [5.1.2] The function chSemSignal() is invoked, after return the counter is tested.*/ test_set_step(2); { chSemSignal(&sem1); test_assert_lock(chSemGetCounterI(&sem1) == 1, "wrong counter value"); } /* [5.1.3] The function chSemReset() is invoked, after return the counter is tested.*/ test_set_step(3); { chSemReset(&sem1, 2); test_assert_lock(chSemGetCounterI(&sem1) == 2, "wrong counter value"); } }
bool_t gfxSemWait(gfxSem *psem, delaytime_t ms) { #if CH_KERNEL_MAJOR == 2 switch(ms) { case TIME_IMMEDIATE: return chSemWaitTimeout(&psem->sem, TIME_IMMEDIATE) != RDY_TIMEOUT; case TIME_INFINITE: chSemWait(&psem->sem); return TRUE; default: return chSemWaitTimeout(&psem->sem, MS2ST(ms)) != RDY_TIMEOUT; } #elif CH_KERNEL_MAJOR == 3 switch(ms) { case TIME_IMMEDIATE: return chSemWaitTimeout(&psem->sem, TIME_IMMEDIATE) != MSG_TIMEOUT; case TIME_INFINITE: chSemWait(&psem->sem); return TRUE; default: return chSemWaitTimeout(&psem->sem, MS2ST(ms)) != MSG_TIMEOUT; } #endif }
static msg_t thread3(void *p) { (void)p; chSemWait(&sem1); chSemSignal(&sem1); return 0; }
bool_t gfxSemWait(gfxSem *psem, delaytime_t ms) { if (ms == TIME_INFINITE) { chSemWait(&psem->sem); return TRUE; } return chSemWaitTimeout(&psem->sem, MS2ST(ms)) != RDY_TIMEOUT; }
static msg_t thread3(void *p) { (void)p; while (!chThdShouldTerminate()) chSemWait(&sem1); return 0; }
static msg_t Thread1(void *arg) { while (TRUE) { chSemWait(&sem); digitalWrite(LED_PIN, LOW); } return 0; }
msg_t Light::moduleThread(void* arg) { (void) arg; bool noRecall = true; LedData* state; while (!chThdShouldTerminate()) { chSemWait(&_sem); while (TRUE) { noRecall = true; for (uint8_t i = 0; i < N_LEDS; ++i) { if (!data[i].isEmpty()) { state = data[i].getHead(); switch (state->state) { case FADE: state->current.setRGB(state->startColor.getR() + state->diff.getR() * state->steps / state->totalSteps, state->startColor.getG() + state->diff.getG() * state->steps / state->totalSteps, state->startColor.getB() + state->diff.getB() * state->steps / state->totalSteps ); leds[i].shine(state->current); state->steps++; if (state->steps == state->totalSteps) { leds[i].shine(state->endColor); data[i].pop(); if (!data[i].isEmpty()) noRecall = false; } else noRecall = false; break; case SHINE: break; case INACTIVE: break; } } } waitMs(_threadDelay); if (noRecall) break; } } return (msg_t)0; }
static void _lun_object_deinit(USBHMassStorageLUNDriver *lunp) { osalDbgCheck(lunp != NULL); chSemWait(&lunp->sem); lunp->msdp = NULL; lunp->next = NULL; memset(&lunp->info, 0, sizeof(lunp->info)); lunp->state = BLK_STOP; chSemSignal(&lunp->sem); }
/** * @brief Gains exclusive access to the ADC peripheral. * @details This function tries to gain ownership to the ADC bus, if the bus * is already being used then the invoking thread is queued. * @pre In order to use this function the option * @p ADC_USE_MUTUAL_EXCLUSION must be enabled. * * @param[in] adcp pointer to the @p ADCDriver object * * @api */ void adcAcquireBus(ADCDriver *adcp) { chDbgCheck(adcp != NULL, "adcAcquireBus"); #if CH_USE_MUTEXES chMtxLock(&adcp->mutex); #elif CH_USE_SEMAPHORES chSemWait(&adcp->semaphore); #endif }
/** * @brief Gains exclusive access to the SPI bus. * @details This function tries to gain ownership to the SPI bus, if the bus * is already being used then the invoking thread is queued. * @pre In order to use this function the option @p SPI_USE_MUTUAL_EXCLUSION * must be enabled. * * @param[in] spip pointer to the @p SPIDriver object * * @api */ void spiAcquireBus(SPIDriver *spip) { chDbgCheck(spip != NULL, "spiAcquireBus"); #if CH_USE_MUTEXES chMtxLock(&spip->mutex); #elif CH_USE_SEMAPHORES chSemWait(&spip->semaphore); #endif }
/** * @brief Gains exclusive access to the I2C bus. * @details This function tries to gain ownership to the SPI bus, if the bus * is already being used then the invoking thread is queued. * @pre In order to use this function the option @p I2C_USE_MUTUAL_EXCLUSION * must be enabled. * * @param[in] i2cp pointer to the @p I2CDriver object * * @api */ void i2cAcquireBus(I2CDriver *i2cp) { chDbgCheck(i2cp != NULL, "i2cAcquireBus"); #if CH_USE_MUTEXES chMtxLock(&i2cp->mutex); #elif CH_USE_SEMAPHORES chSemWait(&i2cp->semaphore); #endif }
/** * @brief Gains exclusive access to the DAC bus. * @details This function tries to gain ownership to the DAC bus, if the bus * is already being used then the invoking thread is queued. * @pre In order to use this function the option @p DAC_USE_MUTUAL_EXCLUSION * must be enabled. * * @param[in] dacp pointer to the @p DACDriver object * * @api */ void dacAcquireBus(DACDriver *dacp) { chDbgCheck(dacp != NULL, "dacAcquireBus"); #if CH_USE_MUTEXES chMtxLock(&dacp->mutex); #elif CH_USE_SEMAPHORES chSemWait(&dacp->semaphore); #endif }
static msg_t Thread2(void *arg) { while (i<3) { chSemWait(&sem); // acquires the semaphore after it is released by thread 1 // ----------------------- This gives the time to acquire the semaphore ----------------------- digitalWrite(LED_PIN, LOW); i++; } return 0; }
static THD_FUNCTION(mp45dt02ProcessingThd, arg) { (void)arg; chRegSetThreadName(__FUNCTION__); while (chThdShouldTerminateX() == false) { chSemWait(&mp45dt02ProcessingSem); if (chThdShouldTerminateX() == true) { break; } if (mp45dt02I2sData.number != MP45DT02_I2S_SAMPLE_SIZE_2B) { PRINT_CRITICAL("Unexpected number of samples provided. %d not %d.", mp45dt02I2sData.number, MP45DT02_I2S_SAMPLE_SIZE_2B); } /**********************************************************************/ /* Convert I2S data to a useful format */ /**********************************************************************/ expand(mp45dt02ExpandedBuffer, &mp45dt02I2sData.buffer[mp45dt02I2sData.offset]); /**********************************************************************/ /* Filtering */ /**********************************************************************/ arm_fir_decimate_f32(&cmsisDsp.decimateInstance, mp45dt02ExpandedBuffer, mp45dt02DecimatedBuffer, MP45DT02_EXPANDED_BUFFER_SIZE); /**********************************************************************/ /* Notify of new data */ /**********************************************************************/ initConfig.fullbufferCb(mp45dt02DecimatedBuffer, MP45DT02_DECIMATED_BUFFER_SIZE); if (mp45dt02I2sData.guard != MEMORY_GUARD) { PRINT_CRITICAL("Overflow detected.",0); } if (cmsisDsp.guard != MEMORY_GUARD) { PRINT_CRITICAL("Overflow detected.",0); } } }
static msg_t Thread1(void *arg) { while (!chThdShouldTerminate()) { // Wait for signal from thread 2. chSemWait(&sem); // Turn LED off. digitalWrite(LED_PIN, LOW); } return 0; }
void Lcd_t::PrintfInverted(const uint8_t x, const uint8_t y, const char *S, ...) { msg_t msg = chSemWait(&semLcd); if(msg == RDY_OK) { GotoCharXY(x, y); va_list args; va_start(args, S); kl_vsprintf(FLcdPutCharInverted, 16, S, args); va_end(args); chSemSignal(&semLcd); } }
//----------------------------------------------------------------------------- static void return_write_buffer_idx_after_writing(int8_t idx) { chSemWait(&write_buffer_semaphore); // @TODO: optional? Do we really need to memset it? we ARE going to be // overwriting it memset(&write_buffers[idx],0,sizeof(write_buffers[idx])); // if the current idx is -1, then we can assume it's good to go write_buffers[idx].current_idx = -1; chSemSignal(&write_buffer_semaphore); }
//----------------------------------------------------------------------------- // new file scenario / etc. static void flush_write_buffers(void) { chSemWait(&write_buffer_semaphore); memset(&write_buffers, 0, sizeof(write_buffers)); for ( int8_t idx = 0 ; idx < BUFFER_COUNT ; idx++ ) write_buffers[idx].current_idx = -1; flush_counters(); chSemSignal(&write_buffer_semaphore); }
static void bmk11_execute(void) { uint32_t n = 0; test_wait_tick(); test_start_timer(1000); do { chSemWait(&sem1); chSemSignal(&sem1); chSemWait(&sem1); chSemSignal(&sem1); chSemWait(&sem1); chSemSignal(&sem1); chSemWait(&sem1); chSemSignal(&sem1); n++; #if defined(SIMULATOR) ChkIntSources(); #endif } while (!test_timer_done); test_print("--- Score : "); test_printn(n * 4); test_println(" wait+signal/S"); }
/** * TX handler */ static void handle_uart_tx(struct uart_periph *p) { // check if more data to send struct SerialInit *init_struct = (struct SerialInit*)(p->init_struct); chSemWait (init_struct->tx_sem); while (p->tx_insert_idx != p->tx_extract_idx) { uint8_t data = p->tx_buf[p->tx_extract_idx]; p->tx_running = true; sdWrite((SerialDriver *)p->reg_addr, &data, sizeof(data)); p->tx_running = false; // TODO send by block (be careful with circular buffer) chMtxLock(init_struct->tx_mtx); p->tx_extract_idx++; p->tx_extract_idx %= UART_TX_BUFFER_SIZE; chMtxUnlock(init_struct->tx_mtx); } }
void uart_printf_i(const char *format, ...) { va_list ap; va_start (ap, format); chSemWait(&uart_sem); /* * Build string to send to the buffer. */ vsnprintf(uart_print_buf, PRINTF_BUF_SIZE, format, ap); /* * Print stuff UART */ uartStartSendI(&UARTD3, strlen(uart_print_buf), uart_print_buf); }
bool usbhmsdLUNDisconnect(USBHMassStorageLUNDriver *lunp) { osalDbgCheck(lunp != NULL); chSemWait(&lunp->sem); osalDbgAssert((lunp->state == BLK_READY) || (lunp->state == BLK_ACTIVE), "invalid state"); if (lunp->state == BLK_ACTIVE) { chSemSignal(&lunp->sem); return HAL_SUCCESS; } lunp->state = BLK_DISCONNECTING; //TODO: complete: sync, etc. lunp->state = BLK_ACTIVE; chSemSignal(&lunp->sem); return HAL_SUCCESS; }
bool usbhmsdLUNWrite(USBHMassStorageLUNDriver *lunp, uint32_t startblk, const uint8_t *buffer, uint32_t n) { osalDbgCheck(lunp != NULL); bool ret = HAL_FAILED; uint16_t blocks; msd_result_t res; uint32_t actual_len; chSemWait(&lunp->sem); if (lunp->state != BLK_READY) { chSemSignal(&lunp->sem); return ret; } lunp->state = BLK_WRITING; while (n) { if (n > 0xffff) { blocks = 0xffff; } else { blocks = (uint16_t)n; } res = scsi_write10(lunp, startblk, blocks, buffer, &actual_len); if (res == MSD_RESULT_DISCONNECTED) { goto exit; } else if (res == MSD_RESULT_TRANSPORT_ERROR) { //retry? goto exit; } else if (res == MSD_RESULT_FAILED) { //retry? goto exit; } n -= blocks; startblk += blocks; buffer += blocks * lunp->info.blk_size; } ret = HAL_SUCCESS; exit: lunp->state = BLK_READY; chSemSignal(&lunp->sem); return ret; }
//----------------------------------------------------------------------------- static int8_t new_write_buffer_idx_to_write(void) { int8_t buffer_idx = -1; chSemWait(&write_buffer_semaphore); for ( int8_t idx = 0 ; idx < BUFFER_COUNT ; idx++ ) { // we know it's full and ready to be written out when current_idx is // at the end if ( write_buffers[idx].current_idx == BUFFER_SIZE ) { buffer_idx = idx; break; } } chSemSignal(&write_buffer_semaphore); return buffer_idx; }
//----------------------------------------------------------------------------- // when the logger thread needs a new buffer, we call this - it's semaphore // protected static int8_t new_write_buffer_idx_to_fill(void) { int8_t buffer_idx = -1; chSemWait(&write_buffer_semaphore); for ( int8_t idx = 0 ; idx < BUFFER_COUNT ; idx++ ) { // we know it's empty and ready to be filled when current_idx == -1 if ( write_buffers[idx].current_idx == -1 ) { // ok, we're going to return this one, initialise it ready! write_buffers[idx].current_idx = 0; buffer_idx = idx; break; } } chSemSignal(&write_buffer_semaphore); // if -1, then no buffers are free... return buffer_idx; }
void gadcLowSpeedGet(uint32_t physdev, adcsample_t *buffer) { struct lsdev *p; BSEMAPHORE_DECL(mysem, TRUE); /* Start the Low Speed Timer */ chMtxLock(&gadcmutex); if (!gtimerIsActive(&LowSpeedGTimer)) gtimerStart(&LowSpeedGTimer, LowSpeedGTimerCallback, NULL, TRUE, TIME_INFINITE); chMtxUnlock(); while(1) { /* Wait for an available slot */ chSemWait(&gadcsem); /* Find a slot */ chMtxLock(&gadcmutex); for(p = ls; p < &ls[GADC_MAX_LOWSPEED_DEVICES]; p++) { if (!(p->flags & GADC_FLG_ISACTIVE)) { p->lld.physdev = physdev; p->lld.buffer = buffer; p->fn = BSemSignalCallback; p->param = &mysem; p->flags = GADC_FLG_ISACTIVE; chMtxUnlock(); StartADC(FALSE); chBSemWait(&mysem); return; } } chMtxUnlock(); /** * We should never get here - the count semaphore must be wrong. * Decrement it and try again. */ } }
static msg_t vexAudioTask( void *arg ) { uint32_t tmpCounter; (void)arg; chRegSetThreadName("audio"); chEvtInit(&sound_done); while(!chThdShouldTerminate()) { if(VSL_Counter > 0) { tmpCounter = VSL_Counter; VSL_Counter = 0; // wait for semaphore timeout or other thread reseting the semaphore chSemWaitTimeout( &vslSem, tmpCounter ); chSysLock(); if( chEvtIsListeningI(&sound_done) ) chEvtBroadcastI(&sound_done); chSysUnlock(); if( VSL_Counter == 0 ) { if( !vexAudioPlayNextChipTone() ) VSL_Deinit(); } } else chSemWait( &vslSem ); } return (msg_t)0; }
static gdisp_lld_msg_t *gdispAllocMsg(gdisp_msgaction_t action) { gdisp_lld_msg_t *p; while(1) { /* To be sure, to be sure */ /* Wait for a slot */ chSemWait(&gdispMsgsSem); /* Find the slot */ chMtxLock(&gdispMsgsMutex); for(p=gdispMsgs; p < &gdispMsgs[GDISP_QUEUE_SIZE]; p++) { if (p->action == GDISP_LLD_MSG_NOP) { /* Allocate it */ p->action = action; chMtxUnlock(); return p; } } chMtxUnlock(); /* Oops - none found, try again */ chSemSignal(&gdispMsgsSem); } }