/** * @brief Mail Producer Thread. * @param argument: Not used * @retval None */ static void MailQueueProducer(const void *argument) { Amail_TypeDef *pTMail; for(;;) { pTMail = osMailAlloc(mailId, osWaitForever); /* Allocate memory */ pTMail->var1 = ProducerValue1; /* Set the mail content */ pTMail->var2 = ProducerValue2; pTMail->var3 = ProducerValue3; if(osMailPut(mailId, pTMail) != osOK) /* Send Mail */ { /* Toggle LED2 to indicate error */ BSP_LED_Toggle(LED2); } else { /* Increment the variables we are going to post next time round. The consumer will expect the numbers to follow in numerical order. */ ++ProducerValue1; ProducerValue2 += 2; ProducerValue3 += 3; /* Toggle LED2 to indicate a correct number received */ BSP_LED_Toggle(LED2); osDelay(250); } } }
// NOTE: callback from ISR -- cannot wait static void key_event_handler(uint8_t ri, uint8_t ci, bool state) { KeyEvent* e = (KeyEvent*)osMailAlloc(key_events, 0); if (!e) return; e->keycode = mapping[ri][ci]; e->state = state; osMailPut(key_events, e); }
/** * Quick method to send a package to input mailbox * @param type type of package * @param value value in package */ void MAIL_send_input(MAIL_package_type type, float value) { MAIL_package_typedef *package; package = osMailAlloc(input_mailbox, osWaitForever); package->value = value; package->type = type; osMailPut(input_mailbox, package); }
/// Allocate a memory block from a mail and set memory block to zero /// \param[in] queue_id mail queue ID obtained with \ref osMailCreate. /// \param[in] millisec timeout value or 0 in case of no time-out /// \return pointer to memory block that can shall filled with mail or NULL in case error. /// \note MUST REMAIN UNCHANGED: \b osMailCAlloc shall be consistent in every CMSIS-RTOS. void *osMailCAlloc (osMailQId queue_id, uint32_t millisec) { // uint32_t i; void *p = osMailAlloc(queue_id, millisec); if (p) { os_memset(p, 0, queue_id->queue_def->item_sz); } return p; }
void send_thread (void const *argument) { uint32_t i = 0; while (true) { i++; // fake data update mail_t *mail = (mail_t*)osMailAlloc(mail_box, osWaitForever); mail->voltage = (i * 0.1) * 33; mail->current = (i * 0.1) * 11; mail->counter = i; osMailPut(mail_box, mail); osDelay(1000); } }
/** * @brief Allocate a memory block from a mail and set memory block to zero * @param queue_id mail queue ID obtained with \ref osMailCreate. * @param millisec timeout value or 0 in case of no time-out. * @retval pointer to memory block that can be filled with mail or NULL in case error. * @note MUST REMAIN UNCHANGED: \b osMailCAlloc shall be consistent in every CMSIS-RTOS. */ void *osMailCAlloc (osMailQId queue_id, uint32_t millisec) { uint32_t i; void *p = osMailAlloc(queue_id, millisec); if (p) { for (i = 0; i < sizeof(queue_id->queue_def->item_sz); i++) { ((uint8_t *)p)[i] = 0; } } return p; }
void Thread_MailQueue1(void const *argument) { MAILQUEUE_OBJ_t *pMail = 0; while(1) { ; // Insert thread code here... pMail = osMailAlloc(qid_MailQueue, osWaitForever); // Allocate memory if(pMail) { pMail->Buf[0] = 0xff; // Set the mail content pMail->Idx = 0; osMailPut(qid_MailQueue, pMail); // Send Mail } osThreadYield(); // suspend thread } }
void GetTemperature(void const *argument) { T_MEAS *mptr; Chip_ADS1248_Init(); Chip_ADS1248_SelfOffsetCal(CHIP_U1); Chip_ADS1248_SelfOffsetCal(CHIP_U3); while(1) { osSignalWait(ADS_TEMP_GET, osWaitForever); mptr=osMailAlloc(mail, osWaitForever); mptr->rtd1 = Chip_ADS1248_GetTemperature(CHIP_U1, RTD_1); mptr->rtd2 = Chip_ADS1248_GetTemperature(CHIP_U1, RTD_2); mptr->rtd3 = Chip_ADS1248_GetTemperature(CHIP_U3, RTD_3); mptr->rtd4 = Chip_ADS1248_GetTemperature(CHIP_U3, RTD_4); osMailPut(mail, mptr); osThreadYield(); } }
/* StartDataProcessTask function */ void StartDataProcessTask(void const * argument) { /* USER CODE BEGIN StartDataProcessTask */ SYS_DEBUG_TRACK(); SensorData_t* pSensorData; ControlData_t* pControlData; /* Infinite loop */ for(;;) { SYS_DEBUG("one loop!\n"); /* -----| 1.从邮箱接收传感器数据 |---------------------*/ osEvent event = osMailGet( gSenorDataMail, osWaitForever); pSensorData = (SensorData_t *)event.value.p; /* -----| 2.申请输出数据邮箱 |-------------------------- */ pControlData = (ControlData_t *) osMailAlloc(gControlDataMail, osWaitForever); /* -----| 3.处理数据,处理结果存放于输出缓冲区 |--------*/ DataProcess_DoProcess( pSensorData, pControlData ); /* -----| 4.发送处理结果(缓冲区)到输出邮箱 | -----------*/ osMailPut( gControlDataMail , pControlData ) ; /* -----| 5.释放超声波数据缓冲区 |------------------------*/ osMailFree( gSenorDataMail, pSensorData ); // osDelay(1); } /* USER CODE END StartDataProcessTask */ }
/* StartDataParseTask function */ void StartDataParseTask(void const * argument) { /* USER CODE BEGIN StartDataParseTask */ uint8_t tmp; SYS_DEBUG_TRACK(); /* Infinite loop */ for(;;) { SYS_DEBUG("one loop!\n"); /*------------------------接收并解析数据包---------------------------*/ /* 1. 开始接收数据帧*/ /*读取帧头1——head1 */ xSerialGetChar( ReceiveDataQueueHandle, &tmp, osWaitForever ); /*判断是否为帧头1——head1*/ if( tmp == SENSOR_PROTOCOL_PACK_HEADER0 ) { /*读取帧头2——head2*/ xSerialGetChar( ReceiveDataQueueHandle, &tmp, osWaitForever ); /*如果读到的还是帧头1,则重新读帧头2;防止重复的帧头1干扰*/ if( tmp == SENSOR_PROTOCOL_PACK_HEADER0 ) { /* 重新读取帧头2 */ xSerialGetChar( ReceiveDataQueueHandle, &tmp, osWaitForever ); } /* 验证帧头2——head2*/ if( tmp == SENSOR_PROTOCOL_PACK_HEADER1 ) { /* 读取数据段长度 len */ uint8_t len; xSerialGetChar( ReceiveDataQueueHandle, &len, osWaitForever ); if( len == SENSOR_PROTOCOL_PACK_MAX_DATA_LEN ) { /* 从邮箱申请空间,用来保存读取结果 */ SensorData_t* pSensorData; pSensorData = (SensorData_t *) osMailAlloc( gSenorDataMail, osWaitForever); /* 读取数据段内容 */ uint8_t* ptr = (uint8_t*)pSensorData ; uint8_t i; for( i = 0; i < len; i++ ) { /* 循环接收整个数据段 */ xSerialGetChar( ReceiveDataQueueHandle, ptr++, osWaitForever ); } /* 读取校验字节 */ xSerialGetChar( ReceiveDataQueueHandle, &tmp , osWaitForever ); if( tmp == doSumCheck( (uint8_t*)pSensorData, len ) ) { /* 如果校验通过,则发送缓冲区到队列 */ if( osOK != osMailPut( gSenorDataMail, pSensorData) ) { ; //TODO: Add something here } }else { /* 释放内存空间 */ osMailFree( gSenorDataMail, pSensorData ); } } } } // osDelay(1); } /* USER CODE END StartDataParseTask */ }
//Mailbox thread behaviour void Thread_MAIL_CONTROLLER(void const *argument) { osEvent event; MAIL_package_typedef *package_recieved; MAIL_package_typedef *package_to_send; while (1) { event = osMailGet(input_mailbox, osWaitForever); if (event.status == osEventMail) { package_recieved = (MAIL_package_typedef *) event.value.p; if (package_recieved->type == MAIL_TEMP) { //Update temp //Synthetic alarm simulator //if (test < 150) package_recieved->value += 20; //test = (test +1) % 300; temperature = package_recieved->value; if (temperature >= ALARM_THRESHOLD && !alarm_on) { package_to_send = osMailAlloc(led_mailbox, osWaitForever); package_to_send->type = MAIL_ALARM; package_to_send->value = 100.0f; osMailPut(led_mailbox, package_to_send); alarm_on = 1; } else if(temperature < ALARM_THRESHOLD && alarm_on) { package_to_send = osMailAlloc(led_mailbox, osWaitForever); package_to_send->type = MAIL_ALARM; package_to_send->value = 0.0f; osMailPut(led_mailbox, package_to_send); alarm_on = 0; } } else if (package_recieved->type == MAIL_ANGLE) { //Update angle angle = package_recieved->value; } else if (package_recieved->type == MAIL_KEY) { //Update key event key_event = (int)package_recieved->value; printf("%i\n", key_event); if (key_event == KP_STAR) { //Star pressed //Change which type of event we send to led if (curent_display_type == MAIL_TEMP) { curent_display_type = MAIL_ANGLE; //Prepare package to send current angle value to led right away at last if of function package_recieved->type = curent_display_type; package_recieved->value = angle; } else if (curent_display_type == MAIL_ANGLE) { curent_display_type = MAIL_TEMP; //Prepare package to send current temp value to led right away at last if of function package_recieved->type = curent_display_type; package_recieved->value = temperature; } } else if (key_event == KP_POUND && curent_display_type != MAIL_TEMP) { //Pound pressed if (!target_set) { target_set = 1; //Send pound event to led to confirm current target value package_to_send = osMailAlloc(led_mailbox, osWaitForever); package_to_send->value = key_event; package_to_send->type = MAIL_KEY; osMailPut(led_mailbox, package_to_send); } } else { //Digit pressed if (!target_set) { //Send key event to led to change current digit package_to_send = osMailAlloc(led_mailbox, osWaitForever); package_to_send->value = key_event; package_to_send->type = MAIL_KEY; osMailPut(led_mailbox, package_to_send); } } } //Send new data to LED if of current type if (package_recieved->type == curent_display_type){ package_to_send = osMailAlloc(led_mailbox, osWaitForever); package_to_send->value = package_recieved->value; package_to_send->type = package_recieved->type; osMailPut(led_mailbox, package_to_send); } osMailFree(input_mailbox, package_recieved); } } }