/******************************************************************************* * 文件名 : USART1WriteDataToBuffer * 描述 : 检查发送缓冲区的大小,若空间足够,将待发送的数据放入到发送缓冲 区中去,并且启动发送 * 输入 : buffer待发送的数据的指针,count待发送的数据的数量 * 输出 : 无 * 返回 : 若正确放入到发送缓冲区中去了,就返回0x00 ,否则返回0x01 *******************************************************************************/ u8 USART1WriteDataToBuffer(u8 *buffer,u8 count) { u8 i=count; u8 err; /*此处可以加入信号灯或者关闭中断*/ if(count==0)return 0x01; USART1StopSendISR(); /*计算放入count个数据需要多少个内存块*/ if(count%USART1_SEND_MAX_Q)count=count/USART1_SEND_MAX_Q+1; else count=count/USART1_SEND_MAX_Q; /*需要count个数据块*/ /*如果内存不足,直接返回*/ if(USART1SendQFree<count){USART1StartSendISR();return ERR_NO_SPACE;} //首先申请内存块,USART1SendQBoxHost在下一个内存申请后才加一 USART1SendTCB[USART1SendQBoxHost].Index=(u8 *)OSMemGet(OSQUSART1Index,&err); if(USART1SendQBoxHost>=USART1_SEND_MAX_BOX)USART1SendQBoxHost=0; count=0; while(i!='\0') //while(i>0) { *(USART1SendTCB[USART1SendQBoxHost].Index+count)=*buffer; count++; if(count>=USART1_SEND_MAX_Q) { USART1SendTCB[USART1SendQBoxHost].Num=USART1_SEND_MAX_Q; //需要一个新的内存块存放接下来的数据,所以更新USART1SendQBoxHost if(++USART1SendQBoxHost>=USART1_SEND_MAX_BOX)USART1SendQBoxHost=0; //需要一个新的内存块存放接下来的数据 USART1SendTCB[USART1SendQBoxHost].Index=(u8 *)OSMemGet(OSQUSART1Index,&err); //空的发送任务块减一 USART1SendQFree--; count=0; } buffer++; i--; } //此处是尚未整块存完的数据,它们也要存放在一个新的内存块里 if(count!=0) { USART1SendTCB[USART1SendQBoxHost].Num=count; USART1SendQFree--; if(++USART1SendQBoxHost>=USART1_SEND_MAX_BOX)USART1SendQBoxHost=0; } //如果是第一次,就启动发送,如果是已经启动就没有这个必要了 if(USART1RunningFlag==0) { #if DMA_MODE USART1DMAConfig(USART1SendTCB[USART1SendQBoxTail].Index,USART1SendTCB[USART1SendQBoxTail].Num); #else USART1SendUpdate(); #endif USART1RunningFlag=1; } /*此处可以开启信号灯或者打开中断*/ USART1StartSendISR(); return 0x00; }
void task_parallet(void *pdata) { INT8U err; pblock = (parallet_msg_t *)OSMemGet(g_hParalletRxMem, &err); if (err != OS_NO_ERR) { while(1) { p_err("%s err", __FUNCTION__); OSTimeDlyHMSM(0,0,1,0); } } else { memset(&pblock->u8Buf, 0, sizeof(pblock->u8Buf)); pblock->len = 0; pblock->fst = 0; pblock->end = 0; } //while (1) // OSTimeDly(1); while (1) { //OSSemPend(sem_udp_rec_flag,0,&err); if (pblock->fst != 0) { pblock->end = os_time_get(); if (pblock->end - pblock->fst > 1000) { OSQPost(hParalletMsgQueue, (void *)pblock); pblock = (parallet_msg_t *)OSMemGet(g_hParalletRxMem, &err); if (OS_NO_ERR == err) { memset(&pblock->u8Buf, 0, sizeof(pblock->u8Buf)); pblock->len = 0; pblock->fst = 0; pblock->end = 0; } } } OSTimeDly(1); } }
// Creates an empty mailbox. signed char _sys_mbox_new(sys_mbox_t *mbox, int size) { // prarmeter "size" can be ignored in your implementation. u8_t ucErr; PQ_DESCR pQDesc; *mbox = SYS_MBOX_NULL; pQDesc = OSMemGet( pQueueMem, &ucErr ); LWIP_ASSERT("OSMemGet ", ucErr == OS_ERR_NONE ); if( ucErr == OS_ERR_NONE ) { if(size > MAX_QUEUE_ENTRIES ) // 邮箱最多容纳MAX_QUEUE_ENTRIES消息数目 size = MAX_QUEUE_ENTRIES; pQDesc->pQ = OSQCreate( &(pQDesc->pvQEntries[0]), size ); LWIP_ASSERT( "OSQCreate ", pQDesc->pQ != NULL ); if( pQDesc->pQ != NULL ) { *mbox = pQDesc; return ERR_OK; } else { ucErr = OSMemPut( pQueueMem, pQDesc ); return ERR_MEM; } } else return ERR_MEM; }
void OSTaskCreateHook (OS_TCB *ptcb) { #if OS_CPU_FPU_EN > 0 INT8U err; void *pblk; #endif #if OS_CPU_FPU_EN > 0 if (ptcb->OSTCBOpt & OS_TASK_OPT_SAVE_FP) /* See if task needs FP support */ { pblk = OSMemGet(OSFPPartPtr, &err); /* Yes, Get storage for VFP registers */ if (pblk != (void *)0) /* Did we get a memory block? */ { ptcb->OSTCBExtPtr = pblk; /* Yes, Link to task's TCB */ OS_CPU_FP_Save(pblk); /* Save the VFP registers in block */ } } #endif #if OS_APP_HOOKS_EN > 0 App_TaskCreateHook(ptcb); #else (void)ptcb; /* Prevent compiler warning */ #endif }
/*FUNCTION********************************************************************** * * Function Name : OSA_MsgQPut * Description : This function is used to put a message to a message queue. * Return kStatus_OSA_Success if the message is put successfully, otherwise * return kStatus_OSA_Error. * *END**************************************************************************/ osa_status_t OSA_MsgQPut(msg_queue_handler_t handler, void* pMessage) { INT8U err; void *localMsg; int32_t *from_ptr, *to_ptr; uint32_t msg_size = handler->size; /* Get a memory to save the message. */ localMsg = OSMemGet(handler->pMem, &err); if (!localMsg) { return kStatus_OSA_Error; } /* Copy msg to localMsg */ from_ptr = (int32_t*)pMessage; to_ptr = (int32_t*)localMsg; while (msg_size--) { *to_ptr++ = *from_ptr++; } if (OS_ERR_NONE != OSQPost(handler->pQueue, localMsg)) { OSMemPut(handler->pMem, localMsg); return kStatus_OSA_Error; } return kStatus_OSA_Success; }
/* Creates an empty mailbox */ sys_mbox_t sys_mbox_new(int size) { /* prarmeter "size" can be ignored in your implementation */ u8_t ucErr; PQ_DESCR pQDesc; pQDesc = OSMemGet( pQueueMem, &ucErr ); LWIP_ASSERT("OSMemGet ", ucErr == OS_ERR_NONE ); if( ucErr == OS_ERR_NONE ) { if( size > MAX_QUEUE_ENTRIES ) /* 邮箱最多容纳MAX_QUEUE_ENTRIES消息数目 */ { size = MAX_QUEUE_ENTRIES; } pQDesc->pQ = OSQCreate( &(pQDesc->pvQEntries[0]), size ); LWIP_ASSERT( "OSQCreate ", pQDesc->pQ != NULL ); if( pQDesc->pQ != NULL ) { return pQDesc; } else { ucErr = OSMemPut( pQueueMem, pQDesc ); return SYS_MBOX_NULL; } } else { return SYS_MBOX_NULL; } }
void TaskSensores(void *pdata) { #if OS_CRITICAL_METHOD == 3 OS_CPU_SR cpu_sr; #endif struct AdcMsg *m; INT16U value = 0; INT16U ValorTeclado = 0; INT8U err; for(;;) { m = (struct AdcMsg *) OSMemGet(dMemory,&err); if(err == OS_NO_ERR){ OSSemPend(STeclado,0,&err); ValorTeclado = NumeroSensores; OSSemPost(STeclado); m->adc0 = 0; m->adc1 = 0; m->adc2 = 0; switch(ValorTeclado){ case 3: Delay10TCYx(100); SetChanADC(ADC_CH2); ConvertADC(); while( BusyADC() ); value = ReadADC(); m->adc2 = Temp(value); case 2: Delay10TCYx(100); SetChanADC(ADC_CH1); ConvertADC(); while( BusyADC() ); value = ReadADC(); m->adc1 = Temp(value); case 1: Delay10TCYx(100); SetChanADC(ADC_CH0); ConvertADC(); while( BusyADC() ); value = ReadADC(); m->adc0 = Temp(value); default: break; } err = OSQPost(QueueADC0,m); if(err == OS_Q_FULL){ OSMemPut(dMemory,m); OSSemPost(STaskTxSerial); } }else{ OSSemPost(STaskTxSerial); } //OSSemPost(STask2); OSTimeDly(1); } }
void OSPrintf(const char *fmt,...)//post massage to UartQ { char *pUartBuf; INT8U err; va_list ap; pUartBuf=OSMemGet(pUartMem,&err);//Reqire a Mem for Msg va_start(ap,fmt); vsprintf(pUartBuf,fmt,ap);//Msg formate va_end(ap); //Uart_SendString(string); OSQPost(pUart_Q,pUartBuf);//Post Msg }
void gd_msg_malloc(gd_msg_t **msg) { INT8U err = 0; *msg = (gd_msg_t *)OSMemGet(gd_system.gd_msg_PartitionPtr, &err); if((*msg) == NULL) { while(1) { OSTimeDlyHMSM(0, 0, 0, 100); } } }
void OSPrintf(const char *fmt,...)//post massage to UartQ { INT8U *pUartBuf; INT8U err; va_list ap; pUartBuf=OSMemGet(pUartMem,&err);// Reqire a Mem for Msg va_start(ap,fmt); vsprintf(pUartBuf,fmt,ap); // Msg formate,处理打印信息 va_end(ap); OSQPost(pUart_Q,pUartBuf); // Post Msg,将打印信息post给pUart_Q }
void OS_CPU_FP_Init (void) { INT8U err; #if OS_TASK_STAT_EN && OS_TASK_CREATE_EXT_EN OS_TCB *ptcb; void *pblk; #endif OSFPPartPtr = OSMemCreate(&OSFPPart[0][0], OS_NTASKS_FP, OS_FP_STORAGE_SIZE, &err); #if OS_TASK_STAT_EN && OS_TASK_CREATE_EXT_EN /* CHANGE 'OPTIONS' for OS_TaskStat() */ ptcb = OSTCBPrioTbl[OS_TASK_STAT_PRIO]; ptcb->OSTCBOpt |= OS_TASK_OPT_SAVE_FP; /* Allow floating-point support for Statistic task */ pblk = OSMemGet(OSFPPartPtr, &err); /* Get storage for VFP registers */ if (pblk != (void *)0) { /* Did we get a memory block? */ ptcb->OSTCBExtPtr = pblk; /* Yes, Link to task's TCB */ OS_CPU_FP_Save(pblk); /* Save the VFP registers in block */ } #endif }
/*FUNCTION********************************************************************** * * Function Name : OSA_MsgQPut * Description : This function is used to put a message to a message queue. * Return kStatus_OSA_Success if the message is put successfully, otherwise * return kStatus_OSA_Error. * *END**************************************************************************/ osa_status_t OSA_MsgQPut(msg_queue_handler_t handler, void* pMessage) { OS_ERR err; void *localMsg; int32_t *from_ptr, *to_ptr; uint32_t msg_size = handler->size; /* In this case, the message is saved into internal memory */ localMsg = OSMemGet(&(handler->mem), &err); if (!localMsg) { return kStatus_OSA_Error; } /* Copy msg to localMsg. */ from_ptr = (int32_t*)pMessage; to_ptr = (int32_t*)localMsg; while (msg_size--) { *to_ptr++ = *from_ptr++; } OSQPost(&(handler->queue), localMsg, handler->size*sizeof(int32_t), OS_OPT_POST_FIFO, &err); if (OS_ERR_NONE != err) { OSMemPut(&(handler->mem), localMsg, &err); return kStatus_OSA_Error; } return kStatus_OSA_Success; }
/******************************************************************************* * Function Name : psr_mgnt_cmd() * Description : mgnt命令的统一处理进程, * Input : pt_prop分别针对额外属性数据体 * Output : None * Return : *******************************************************************************/ STATUS psr_mgnt_cmd(u8 mgnt_cmd, u16 pipe_id, void * pt_prop, ARRAY_HANDLE return_buffer, AUTOHEAL_LEVEL autoheal_level) { u32 expiring_sticks; PIPE_REF pipe_ref; STATUS result; ARRAY_HANDLE mpdu; u8 mpdu_len; ARRAY_HANDLE ptw; result = FAIL; /* check validity of pipe_id */ pipe_ref = inquire_pipe_mnpl_db(pipe_id); if(!pipe_ref) { #ifdef DEBUG_MGNT my_printf("PSR ERR: NO PIPE INFO.\r\n"); #endif if(return_buffer) { return_buffer[0] = NO_PIPE_INFO; } return FAIL; } mpdu = OSMemGet(MINOR); if(!mpdu) { if(return_buffer) { return_buffer[0] = MEM_OUT; } return result; } switch(mgnt_cmd) { case(EXP_MGNT_PING): { expiring_sticks = psr_ping_sticks(pipe_ref); mpdu[0] = (0x80 | EXP_MGNT_PING); mpdu_len = 1; break; } case(EXP_MGNT_DIAG): { DLL_SEND_HANDLE pds; pds = (DLL_SEND_HANDLE)(pt_prop); expiring_sticks = psr_diag_sticks(pipe_ref,pds); ptw = mpdu; *(ptw++) = (0x80 | EXP_MGNT_DIAG); mem_cpy(ptw,pds->target_uid_handle,6); ptw += 6; *(ptw++) = pds->xmode; *(ptw++) = pds->rmode; *(ptw++) = (pds->prop & BIT_DLL_SEND_PROP_SCAN)?1:0; mpdu_len = ptw-mpdu; break; } case(EXP_MGNT_EBC_BROADCAST): { EBC_BROADCAST_HANDLE ebc; ebc = (EBC_BROADCAST_HANDLE)(pt_prop); /* limit ebc window */ switch(ebc->rmode & 0x03) { case(0x00): { if(ebc->window > CFG_EBC_MAX_BPSK_WINDOW) { ebc->window = CFG_EBC_MAX_BPSK_WINDOW; } break; } case(0x01): { if(ebc->window > CFG_EBC_MAX_DS15_WINDOW) { ebc->window = CFG_EBC_MAX_DS15_WINDOW; } break; } case(0x02): { if(ebc->window > CFG_EBC_MAX_DS63_WINDOW) { ebc->window = CFG_EBC_MAX_DS63_WINDOW; } } } expiring_sticks = psr_broadcast_sticks(pipe_ref,ebc); ptw = mpdu; *ptw++ = (0x80 | EXP_MGNT_EBC_BROADCAST); *ptw++ = ebc->bid; *ptw++ = ebc->xmode; *ptw++ = ebc->rmode; *ptw++ = ebc->scan; *ptw++ = ebc->window; *ptw++ = ebc->mask; mpdu_len = 7; /*** todo: add other conditions ***/ if(ebc->mask & BIT_NBF_MASK_UID) { mem_cpy(ptw,ebc->uid,6); ptw += 6; mpdu_len += 6; } if(ebc->mask & BIT_NBF_MASK_METERID) { mem_cpy(ptw,ebc->mid,6); ptw += 6; mpdu_len += 6; } if(ebc->mask & BIT_NBF_MASK_BUILDID) { *ptw++ = ebc->build_id; mpdu_len++; } break; } case(EXP_MGNT_EBC_IDENTIFY): { EBC_IDENTIFY_HANDLE ebc; ebc = (EBC_IDENTIFY_HANDLE)(pt_prop); expiring_sticks = psr_identify_sticks(pipe_ref,ebc); ptw = mpdu; *ptw++ = (0x80 | EXP_MGNT_EBC_IDENTIFY); *ptw++ = ebc->bid; *ptw++ = ebc->num_bsrf; *ptw++ = ebc->max_identify_try; mpdu_len = 4; break; } case(EXP_MGNT_EBC_ACQUIRE): { EBC_ACQUIRE_HANDLE ebc; ebc = (EBC_ACQUIRE_HANDLE)(pt_prop); expiring_sticks = psr_acquire_sticks(pipe_ref,ebc); ptw = mpdu; *ptw++ = (0x80 | EXP_MGNT_EBC_ACQUIRE); *ptw++ = ebc->bid; *ptw++ = ebc->node_index; *ptw++ = ebc->node_num; mpdu_len = 4; break; } case(EXP_MGNT_SET_BUILD_ID): { expiring_sticks = psr_transaction_sticks(pipe_ref,2,9,0); ptw = mpdu; *ptw++ = (0x80 | EXP_MGNT_SET_BUILD_ID); *ptw++ = *(u8 *)(pt_prop); mpdu_len = 2; break; } default: { #ifdef DEBUG_MANIPULATION my_printf("error mgnt cmd %bX\r\n",mgnt_cmd); #endif OSMemPut(MINOR,mpdu); return result; } } // 2013-07-18 autoheal_level 作为参数传入 // 一般地: ping命令的autoheal_level为0, ebc各个命令由于是组网过程中使用, autoheal_level为1(允许重刷pipe, 允许重试) // diag命令要看情况, 在组网过程中使用level1, 在诊断和修复过程中使用level1等等, 这样能保证代码的重用和最大的灵活性 result = psr_transaction(pipe_id, mpdu, mpdu_len, expiring_sticks, return_buffer, autoheal_level); OSMemPut(MINOR,mpdu); if(!result) { #ifdef DEBUG_MANIPULATION my_printf("mgnt psr comm fail\r\n"); #endif return FAIL; } if(result && ((*return_buffer & 0xF0) == 0xF0)) //2013-07-18 status为okay仅表示通信成功, return_buffer首字节为0xFX即表示mgnt层功能失败 { #ifdef DEBUG_MANIPULATION my_printf("mgnt cmd return fail\r\n"); #endif return FAIL; } else { return OKAY; } }
static void tcp_ip_Thread_Method (void *p_arg) { NET_SOCK_ID sock; NET_SOCK_ADDR_IP server_sock_addr_ip; NET_SOCK_ADDR_LEN server_sock_addr_ip_size; NET_SOCK_ADDR_IP client_sock_addr_ip; NET_SOCK_ADDR_LEN client_sock_addr_ip_size; NET_SOCK_RTN_CODE rx_size, tx_size; CPU_BOOLEAN attempt_rx; NET_ERR err; dispatchMsg* dmPtr; INT8U continueInt = 1; sock = NetSock_Open( NET_SOCK_ADDR_FAMILY_IP_V4, NET_SOCK_TYPE_DATAGRAM, NET_SOCK_PROTOCOL_UDP, &err); if (err != NET_SOCK_ERR_NONE) { err_printf("NetSock Open failed\n"); } server_sock_addr_ip_size = sizeof(server_sock_addr_ip); Mem_Clr((void *)&server_sock_addr_ip, (CPU_SIZE_T) server_sock_addr_ip_size); server_sock_addr_ip.Family = NET_SOCK_ADDR_FAMILY_IP_V4; server_sock_addr_ip.Addr = NET_UTIL_HOST_TO_NET_32(NET_SOCK_ADDR_IP_WILD_CARD); server_sock_addr_ip.Port = NET_UTIL_HOST_TO_NET_16(UDP_SERVER_PORT); #warning: " tobe debugged in method tcp_ip_Thread_Method ip.addr in server_sock_add_ip" NetSock_Bind((NET_SOCK_ID ) sock, (NET_SOCK_ADDR *)&server_sock_addr_ip, (NET_SOCK_ADDR_LEN) NET_SOCK_ADDR_SIZE, (NET_ERR *)&err); if (err != NET_SOCK_ERR_NONE) { NetSock_Close(sock, &err); err_printf("Net sock Bind failed\n"); } while (continueInt) { do { client_sock_addr_ip_size = sizeof(client_sock_addr_ip); rx_size = NetSock_RxDataFrom((NET_SOCK_ID ) sock, (void *) tcp_ip_RecvBuffer, (CPU_INT16S ) sizeof(tcp_ip_RecvBuffer), (CPU_INT16S ) NET_SOCK_FLAG_NONE, (NET_SOCK_ADDR *)&client_sock_addr_ip, (NET_SOCK_ADDR_LEN *)&client_sock_addr_ip_size, (void *) 0, (CPU_INT08U ) 0, (CPU_INT08U *) 0, (NET_ERR *)&err); switch (err) { case NET_SOCK_ERR_NONE: attempt_rx = DEF_NO; break; case NET_SOCK_ERR_RX_Q_EMPTY: case NET_OS_ERR_LOCK: attempt_rx = DEF_YES; break; default: attempt_rx = DEF_NO; break; } } while (attempt_rx == DEF_YES); info_printf("Net received %i bytes : %X\n",rx_size,tcp_ip_RecvBuffer); dmPtr = OSMemGet(dispatchMsgMem,&err); if (err != OS_NO_ERR) { err_printf("error get memory in method tcp_ip_Thread_Method, err = %d\n ",err); } else { dmPtr->dispatchData = (INT32U) tcp_ip_RecvBuffer; err = OSQPost(dispatchMsgQ,dmPtr); if (err != OS_NO_ERR) { err_printf("error OSQPost in method tcp_ip_Thread_Method, err = %d\n ",err); } } snprintf((char*)&tcp_ip_SendBuffer,sizeof(tcp_ip_SendBuffer)," msg received"); tx_size = NetSock_TxDataTo((NET_SOCK_ID ) sock, (void *) tcp_ip_SendBuffer, (CPU_INT16S ) strlen((char*) tcp_ip_SendBuffer)+ 1, (CPU_INT16S ) NET_SOCK_FLAG_NONE, (NET_SOCK_ADDR *)&client_sock_addr_ip, (NET_SOCK_ADDR_LEN) client_sock_addr_ip_size, (NET_ERR *)&err); info_printf("net sent (ret error code %i) %i bytes : &s\n",err,tx_size, tcp_ip_SendBuffer); } NetSock_Close(sock, &err); if (err != NET_SOCK_ERR_NONE) { err_printf("Net Sock Close error\n"); } }
/********************************************************************************************************* ** Function name: zyMsgPost ** Descriptions: 发送消息 ** input parameters: ulMsg: zyMsgCreate返回值 ** pucMsgSend: 要发送的消息 ** ucMod: 发送模式,一般为0 ** output parameters: none ** Returned value: ZY_OK: 成功 ** 负数: 错误,绝对值参考zy_if.h ** Created by: Chenmingji ** Created Date: 2009-07-23 **-------------------------------------------------------------------------------------------------------- ** Modified by: ** Modified date: *********************************************************************************************************/ INT32S zyMsgPost (unsigned long ulMsg, INT8U *pucMsgSend, INT8U ucMod) { __ZY_IF_MSG *pimThis; /* 消息通道信息 */ INT8U ucErr; /* 错误代码 */ void *pvTmp; pimThis = (__ZY_IF_MSG *)ulMsg; /* * 参数检查 */ if (ulMsg == 0) { return -ZY_PARAMETER_ERR; } #if OS_ARG_CHK_EN == 0 if (pimThis->poeQ == NULL) { return -ZY_PARAMETER_ERR; } if (pimThis->pomMsg == NULL) { return -ZY_PARAMETER_ERR; } #endif /* OS_ARG_CHK_EN */ /* * 申请缓冲区 */ pvTmp = OSMemGet(pimThis->pomMsg, &ucErr); switch (ucErr) { case OS_MEM_INVALID_PMEM: return -ZY_NO_FIND_OBJECT; case OS_MEM_NO_FREE_BLKS: return -ZY_NO_MEMORY; case OS_NO_ERR: break; default: return -ZY_NOT_OK; } /* * 发送消息 */ memcpy(pvTmp, pucMsgSend, (size_t)(pimThis->ulMsgSize)); /* 拷贝数据 */ ucErr = OSQPostOpt(pimThis->poeQ, pvTmp, ucMod); /* 保存消息存储位置 */ if (ucErr == OS_NO_ERR) { return ZY_OK; } OSMemPut(pimThis->pomMsg, pvTmp); /* 归还缓冲区 */ switch (ucErr) { case OS_ERR_PEVENT_NULL: return -ZY_PARAMETER_ERR; case OS_ERR_EVENT_TYPE: return -ZY_NO_FIND_OBJECT; case OS_Q_FULL: return -ZY_NO_MEMORY; default: return -ZY_NOT_OK; } }