//=========================== CLI handlers ==================================== // Open the LPTimer device dn_error_t cli_openHandler(const char* arg, INT32U len) { dn_error_t dnErr; int length; lptimer_app_v.overflow = 0; length = sscanf(arg, "%hhu %hhx %hhu %hhu %hu %hhu %hhu %hhu", &lptimer_app_v.openArgs.mode, &lptimer_app_v.openArgs.enabledEvents, &lptimer_app_v.openArgs.oneshot, &lptimer_app_v.openArgs.polarity, &lptimer_app_v.openArgs.compare, &lptimer_app_v.openArgs.clearOnCompare, &lptimer_app_v.openArgs.captureOnGate, &lptimer_app_v.openArgs.clearOnGate); if (length != 8) { dnm_ucli_printf("Usage: open <mode> <events> <single> <pol> <cmp> <clrCmp> <capGate> <clrGate>\r\n"); return DN_ERR_NONE; } dnm_ucli_printf("Opening lptimer"); dnErr = dn_open(DN_LPTIMER_DEV_ID, &lptimer_app_v.openArgs, sizeof(dn_lptimer_open_args_t)); if (dnErr != DN_ERR_NONE) { dnm_ucli_printf(" failed with RC=%d\r\n", dnErr); return DN_ERR_NONE; } else{ dnm_ucli_printf("\r\n"); } return DN_ERR_NONE; }
/** \brief Wrapper to open the CLI serial port. \pre You need to call this function exactly once before you can use the CLI. \pre This function is invoked by #dnm_ucli_open() if you choose to use it. \param[in] port The CLI port to open. Acceptable values are listed in #dn_cli_port_t. \param[in] baudRate The CLI's baudrate. Use #DEFAULT_BAUDRATE. \return The same error are the ones returns by the #dn_open() function for the \ref device_cli. */ dn_error_t dnm_ucli_openPort (INT8U port, INT32U baudRate) { dn_cli_open_args_t conf; conf.port = port; conf.baudRate = baudRate; dnm_ucli_v.port = port; dnm_ucli_v.baudRate = baudRate; return dn_open(DN_CLI_DEV_ID, &conf, sizeof(conf)); }
static void uartTask(void* unused) { dn_error_t dnErr; INT8U osErr; dn_uart_open_args_t uartOpenArgs; INT32U rxLen; INT32U channelMsgType; INT32S err; // create the memory block for the UART channel bridge_app_v.uartRxChannelMem = OSMemCreate( bridge_app_v.uartRxChannelMemBuf, 1, sizeof(bridge_app_v.uartRxChannelMemBuf), &osErr ); ASSERT(osErr==OS_ERR_NONE); // create an asynchronous notification channel dnErr = dn_createAsyncChannel(bridge_app_v.uartRxChannelMem, &bridge_app_v.uartRxChannel); ASSERT(dnErr==DN_ERR_NONE); // associate the channel descriptor with UART notifications dnErr = dn_registerChannel(bridge_app_v.uartRxChannel, DN_MSG_TYPE_UART_NOTIF); ASSERT(dnErr==DN_ERR_NONE); // open the UART device uartOpenArgs.rxChId = bridge_app_v.uartRxChannel; uartOpenArgs.rate = 115200u; uartOpenArgs.mode = DN_UART_MODE_M4; uartOpenArgs.ctsOutVal = 0; err = dn_open( DN_UART_DEV_ID, &uartOpenArgs, sizeof(uartOpenArgs) ); ASSERT(err>=0); while(1) { // this is a task, it executes forever // wait for UART messages dnErr = dn_readAsyncMsg( bridge_app_v.uartRxChannel, // chDesc bridge_app_v.uartRxBuffer, // msg &rxLen, // rxLen &channelMsgType, // msgType MAX_UART_PACKET_SIZE, // maxLen 0 // timeout (0==never) ); ASSERT(dnErr==DN_ERR_NONE); ASSERT(channelMsgType==DN_MSG_TYPE_UART_NOTIF); // call the callback serial_rx(bridge_app_v.uartRxBuffer,rxLen); } }
static void randomTask(void* unused) { dn_error_t dnErr; int numBytesRead; INT8U randomBuf[16]; INT8U i; // open random device dnErr = dn_open( DN_RAND_DEV_ID, // device NULL, // args 0 // argLen ); ASSERT(dnErr==DN_ERR_NONE); // infinite loop while(1) { // this call blocks the task until the specified timeout expires (in ms) OSTimeDly(1000); // read temperature value numBytesRead = dn_read( DN_RAND_DEV_ID, // device randomBuf, // buf sizeof(randomBuf) // bufSize ); ASSERT(numBytesRead==sizeof(randomBuf)); // print dnm_cli_printf("randomBuf: "); for (i=0;i<sizeof(randomBuf);i++) { dnm_cli_printf("%02x",randomBuf[i]); } dnm_cli_printf("\r\n"); } }
/** \brief A demo task to show the use of the SPI. */ static void spiTask(void* unused) { INT8U i; dn_error_t dnErr; INT8U osErr; dn_spi_open_args_t spiOpenArgs; INT8U sendStatus; INT8U pkBuf[sizeof(loc_sendtoNW_t) + APP_DATA_BUF_SIZE]; loc_sendtoNW_t* pkToSend; dn_ioctl_spi_transfer_t spiTransfer; //===== initialize the configuration file initConfigFile(); //===== initialize packet variables pkToSend = (loc_sendtoNW_t*)pkBuf; //===== initialize SPI // open the SPI device spiOpenArgs.maxTransactionLenForCPHA_1 = 0; dnErr = dn_open( DN_SPI_DEV_ID, &spiOpenArgs, sizeof(spiOpenArgs) ); if ((dnErr < DN_ERR_NONE) && (dnErr != DN_ERR_STATE)) { dnm_cli_printf("unable to open SPI device, error %d\n\r",dnErr); } // initialize spi communication parameters spiTransfer.txData = spiNetApp_vars.spiTxBuffer; spiTransfer.rxData = spiNetApp_vars.spiRxBuffer; spiTransfer.transactionLen = sizeof(spiNetApp_vars.spiTxBuffer); spiTransfer.numSamples = 1; spiTransfer.startDelay = 0; spiTransfer.clockPolarity = DN_SPI_CPOL_0; spiTransfer.clockPhase = DN_SPI_CPHA_0; spiTransfer.bitOrder = DN_SPI_MSB_FIRST; spiTransfer.slaveSelect = DN_SPI_SSn0; spiTransfer.clockDivider = DN_SPI_CLKDIV_16; //===== wait for the mote to have joined OSSemPend(spiNetApp_vars.joinedSem,0,&osErr); ASSERT(osErr == OS_ERR_NONE); while(1) { // this is a task, it executes forever //===== step 1. write over SPI // set bytes to send for (i=0;i<sizeof(spiNetApp_vars.spiTxBuffer);i++) { spiNetApp_vars.spiTxBuffer[i] = i; } // send bytes dnErr = dn_ioctl( DN_SPI_DEV_ID, DN_IOCTL_SPI_TRANSFER, &spiTransfer, sizeof(spiTransfer) ); if (dnErr < DN_ERR_NONE) { dnm_cli_printf("Unable to communicate over SPI, err=%d\r\n",dnErr); } //===== step 2. print over CLI dnm_cli_printf("SPI sent: "); for (i=0;i<sizeof(spiNetApp_vars.spiTxBuffer);i++) { dnm_cli_printf(" %02x",spiNetApp_vars.spiTxBuffer[i]); } dnm_cli_printf("\r\n"); dnm_cli_printf("SPI received:"); for (i=0;i<sizeof(spiNetApp_vars.spiRxBuffer);i++) { dnm_cli_printf(" %02x",spiNetApp_vars.spiRxBuffer[i]); } dnm_cli_printf("\r\n"); //===== step 3. send data to manager // fill in packet "header" // Note: sendto->header is filled in dnm_loc_sendtoCmd pkToSend->locSendTo.socketId = loc_getSocketId(); pkToSend->locSendTo.destAddr = DN_MGR_IPV6_MULTICAST_ADDR; // IPv6 address pkToSend->locSendTo.destPort = WKP_SPI_NET; pkToSend->locSendTo.serviceType = DN_API_SERVICE_TYPE_BW; pkToSend->locSendTo.priority = DN_API_PRIORITY_MED; pkToSend->locSendTo.packetId = 0xFFFF; // fill in the packet payload memcpy(&pkToSend->locSendTo.payload[0],&spiNetApp_vars.spiRxBuffer[0],APP_DATA_BUF_SIZE); // send the packet dnErr = dnm_loc_sendtoCmd(pkToSend, APP_DATA_BUF_SIZE, &sendStatus); ASSERT (dnErr == DN_ERR_NONE); //===== step 4. pause until next iteration // this call blocks the task until the specified timeout expires (in ms) OSTimeDly(spiNetApp_vars.period); } }
static void uartRxTask(void* unused) { dn_error_t dnErr; INT8U osErr; dn_uart_open_args_t uartOpenArgs; INT32U rxLen; INT32U msgType; INT8U i; INT32S err; // create the memory block for the UART channel uart_app_v.uartRxChannelMem = OSMemCreate( uart_app_v.uartRxChannelMemBuf, 1, sizeof(uart_app_v.uartRxChannelMemBuf), &osErr ); ASSERT(osErr==OS_ERR_NONE); // create an asynchronous notification channel dnErr = dn_createAsyncChannel(uart_app_v.uartRxChannelMem, &uart_app_v.uartRxChannel); ASSERT(dnErr==DN_ERR_NONE); // associate the channel descriptor with UART notifications dnErr = dn_registerChannel(uart_app_v.uartRxChannel, DN_MSG_TYPE_UART_NOTIF); ASSERT(dnErr==DN_ERR_NONE); // open the UART device uartOpenArgs.rxChId = uart_app_v.uartRxChannel; uartOpenArgs.eventChId = 0; uartOpenArgs.rate = 115200u; uartOpenArgs.mode = DN_UART_MODE_M4; uartOpenArgs.ctsOutVal = 0; uartOpenArgs.fNoSleep = 0; err = dn_open( DN_UART_DEV_ID, &uartOpenArgs, sizeof(uartOpenArgs) ); ASSERT(err>=0); while(1) { // this is a task, it executes forever // wait for UART messages dnErr = dn_readAsyncMsg( uart_app_v.uartRxChannel, // chDesc uart_app_v.uartRxBuffer, // msg &rxLen, // rxLen &msgType, // msgType MAX_UART_PACKET_SIZE, // maxLen 0 // timeout (0==never) ); ASSERT(dnErr==DN_ERR_NONE); ASSERT(msgType==DN_MSG_TYPE_UART_NOTIF); // print message received dnm_ucli_printf("uart RX (%d bytes)",rxLen); for (i=0;i<rxLen;i++) { dnm_ucli_printf(" %02x",uart_app_v.uartRxBuffer[i]); } dnm_ucli_printf("\r\n"); } }
/** \brief A demo task to show the use of the I2C. */ static void i2cTask(void* unused) { dn_error_t dnErr; dn_i2c_open_args_t i2cOpenArgs; INT8U i; INT8U txCounter; //===== open the I2C device // wait a bit OSTimeDly(1000); // open the I2C device i2cOpenArgs.frequency = DN_I2C_FREQ_184_KHZ; dnErr = dn_open( DN_I2C_DEV_ID, &i2cOpenArgs, sizeof(i2cOpenArgs) ); ASSERT(dnErr==DN_ERR_NONE); while(1) { // infinite loop //===== step 1. write to I2C slave // wait a bit OSTimeDly(1000); // prepare buffer for (i=0;i<I2C_PAYLOAD_LENGTH;i++) { i2c_app_v.i2cBuffer[i] = 0x80+i; } // initialize I2C communication parameters i2c_app_v.i2cTransfer.slaveAddress = I2C_SLAVE_ADDR; i2c_app_v.i2cTransfer.writeBuf = i2c_app_v.i2cBuffer; i2c_app_v.i2cTransfer.readBuf = NULL; i2c_app_v.i2cTransfer.writeLen = sizeof(i2c_app_v.i2cBuffer); i2c_app_v.i2cTransfer.readLen = 0; i2c_app_v.i2cTransfer.timeout = 0xff; // initiate transaction dnErr = dn_ioctl( DN_I2C_DEV_ID, DN_IOCTL_I2C_TRANSFER, &i2c_app_v.i2cTransfer, sizeof(i2c_app_v.i2cTransfer) ); // print if (dnErr==DN_ERR_NONE) { dnm_cli_printf("Sent to I2C slave %02x: 0x",I2C_SLAVE_ADDR); for (i=0;i<I2C_PAYLOAD_LENGTH;i++) { dnm_cli_printf("%02x",i2c_app_v.i2cBuffer[i]); } dnm_cli_printf("\r\n"); } else { dnm_cli_printf("Unable to write over I2C, err=%d\r\n",dnErr); } //===== step 2. read from I2C slave // wait a bit OSTimeDly(1000); // prepare buffer memset(i2c_app_v.i2cBuffer,0,sizeof(i2c_app_v.i2cBuffer)); // initialize I2C communication parameters i2c_app_v.i2cTransfer.slaveAddress = I2C_SLAVE_ADDR; i2c_app_v.i2cTransfer.writeBuf = NULL; i2c_app_v.i2cTransfer.readBuf = i2c_app_v.i2cBuffer; i2c_app_v.i2cTransfer.writeLen = 0; i2c_app_v.i2cTransfer.readLen = sizeof(i2c_app_v.i2cBuffer); i2c_app_v.i2cTransfer.timeout = 0xff; // initiate transaction dnErr = dn_ioctl( DN_I2C_DEV_ID, DN_IOCTL_I2C_TRANSFER, &i2c_app_v.i2cTransfer, sizeof(i2c_app_v.i2cTransfer) ); // print if (dnErr==DN_ERR_NONE) { dnm_cli_printf("Received from I2C slave %02x: 0x",I2C_SLAVE_ADDR); for (i=0;i<I2C_PAYLOAD_LENGTH;i++) { dnm_cli_printf("%02x",i2c_app_v.i2cBuffer[i]); } dnm_cli_printf("\r\n"); } else { dnm_cli_printf("Unable to read over I2C, err=%d\r\n",dnErr); } } }