/**************************************************************************** * * NAME: vUtils_Init * * DESCRIPTION: * Initialises UART * * PARAMETERS: Name RW Usage * * RETURNS: * void, never returns * ****************************************************************************/ PUBLIC void vUtils_Init(void) { vAHI_UartEnable(UTILS_UART); vAHI_UartReset(UTILS_UART, TRUE, TRUE); vAHI_UartReset(UTILS_UART, FALSE, FALSE); vAHI_UartSetRTSCTS(UTILS_UART, FALSE); vAHI_UartSetClockDivisor(UTILS_UART, UTILS_UART_BAUD_RATE); }
void uart0_init(unsigned long br) { vAHI_UartSetRTSCTS(E_AHI_UART_0, false); vAHI_UartEnable(E_AHI_UART_0); vAHI_UartReset(E_AHI_UART_0, true, true); uart0_set_br(br); vAHI_UartSetRTSCTS(E_AHI_UART_0, false); vAHI_Uart0RegisterCallback(irq); vAHI_UartSetInterrupt(E_AHI_UART_0, false, /* modem status */ false, /* rx line error status */ false, /* tx fifo empty */ true, /* rx data there */ E_AHI_UART_FIFO_LEVEL_1); vAHI_UartSetRTSCTS(E_AHI_UART_0, false); vAHI_UartReset(E_AHI_UART_0, false, false); }
void AppColdStart(void) { /* initialize unaligned access handler */ UNALIGNED_ACCESS = UNALIGNED_ACCESS_HANDLER; /* initialize uart to 8N1 at 115200 baud, that gives a throughput og * ~14.4 kb/s, maxmimum packet rate on Ieee802.15.4 is 248 packets/sec at * 127 bytes payload + header, which allows to return about 50bytes on the * uart per packet. */ vAHI_UartEnable(UART); vAHI_UartReset(UART, true, true); vAHI_UartReset(UART, false, false); vAHI_UartSetControl(UART, E_AHI_UART_EVEN_PARITY, E_AHI_UART_PARITY_DISABLE, E_AHI_UART_WORD_LEN_8, E_AHI_UART_1_STOP_BIT, E_AHI_UART_RTS_HIGH); vAHI_UartSetBaudrate(UART, BAUD); vAHI_UartSetRTSCTS(UART, false); vAHI_DioSetDirection(CTS, RTS); vAHI_DioSetOutput(0x00, RTS); /* run the main loop, wait for channel number, then start reception * and packet delivery. Ack by sending "okay\n". */ while (1) { static int started = MAC_ENUM_NO_DATA; static char input[10]; static uint8_t i=0; /* write one rxd packet to uart */ if (started==MAC_ENUM_SUCCESS) { MAC_DcfmIndHdr_s *ind; if (rxq_peektype() == MCPS) { ind = rxq_peek(); if (ind->u8Type == MAC_MCPS_IND_DATA) { MAC_McpsDcfmInd_s *s = ind; uart_write(UART, (char*) &s->uParam.sIndData, sizeof(s->uParam.sIndData)); } } rxq_dequeue(); } /* read from uart into buf */ while (i<sizeof(input) && DATAREADY(UART)) { input[i] = u8AHI_UartReadData(UART); if ((i+1)==sizeof(input)) { /* buffer overrun, discard input */ memset(input, '\0', i); i = 0; } else if (input[i]=='\n' || input[i]=='\r') { /* read as channel number */ int channel; input[9] = '\0'; /* terminate string */ channel = atoi(input); /* convert string to num */ started = start_sniffer(channel); if (started != MAC_ENUM_SUCCESS) printf("not started, error code: 0x%x, see MAC_Enum_e\r\n", started); else printf("started on channel %d\r\n", channel); memset(input, '\0', i); i = 0; } else i++; } } }
/**************************************************************************** * * NAME: vUART_Init * * DESCRIPTION: * * PARAMETERS: Name RW Usage * None. * * RETURNS: * None. * * NOTES: * None. ****************************************************************************/ PUBLIC bool_t vUART_Init(uint8 u8Uart, /**< Uart to open */ uint32 u32BaudRate, /**< Baud rate */ bool_t bEvenParity, /**< Even parity */ bool_t bEnableParity, /**< Enable parity */ uint8 u8WordLength, /**< Word length, one of:\n E_AHI_UART_WORD_LEN_5\n E_AHI_UART_WORD_LEN_6\n E_AHI_UART_WORD_LEN_7\n E_AHI_UART_WORD_LEN_8 */ bool_t bOneStopBit) /**< One stop bit */ { uint16 u16Divisor; /* Baud rate divisor */ uint32 u32Remainder; /* Baud rate remainder */ /* Valid uart ? */ if (u8Uart < 2) { /* Calculate divisor for baud rate = 16MHz / (16 x baud rate) */ u16Divisor = (uint16)(16000000UL / (16UL * u32BaudRate)); /* Correct for rounding errors */ u32Remainder = (uint32)(16000000UL % (16UL * u32BaudRate)); if (u32Remainder >= ((16UL * u32BaudRate) / 2)) u16Divisor += 1; /* Start with port unopened */ Uart_Opened[u8Uart]= FALSE; vAHI_UartSetRTSCTS(u8Uart, FALSE); /* Enable UART 0 */ vAHI_UartEnable(u8Uart); vAHI_UartReset(u8Uart, TRUE, TRUE); vAHI_UartReset(u8Uart, FALSE, FALSE); Uart_Opened[u8Uart]= TRUE; #if !UART_JENOS { /* Register function that will handle UART interrupts */ if (u8Uart == E_AHI_UART_0) vAHI_Uart0RegisterCallback(UART_vInterrupt); if (u8Uart == E_AHI_UART_1) vAHI_Uart1RegisterCallback(UART_vInterrupt); } #endif /* Set baud rate */ vAHI_UartSetBaudDivisor(u8Uart, u16Divisor); /* Set control */ vAHI_UartSetControl(u8Uart, bEvenParity, bEnableParity, u8WordLength, bOneStopBit, FALSE); /* Set UART interrupts */ vAHI_UartSetInterrupt(u8Uart, FALSE, /* ModemStatus */ FALSE, /* RxLineStatus */ TRUE, /* TxFifoEmpty */ TRUE, /* RxData */ E_AHI_UART_FIFO_LEVEL_1); return TRUE; } return FALSE; }
/**************************************************************************** * * NAME: vAppMain * * DESCRIPTION: * Entry point for application from a cold start. * * RETURNS: * void * ****************************************************************************/ PUBLIC void vAppMain(void) { #if JENNIC_CHIP_FAMILY == JN516x /* Wait until FALSE i.e. on XTAL - otherwise uart data will be at wrong speed */ while (bAHI_GetClkSource() == TRUE); /* Now we are running on the XTAL, optimise the flash memory wait states */ vAHI_OptimiseWaitStates(); #endif /* * Don't use RTS/CTS pins on UART0 as they are used for buttons * */ vAHI_UartSetRTSCTS(E_AHI_UART_0, FALSE); /* * Initialize the debug diagnostics module to use UART0 at 115K Baud; * Do not use UART 1 if LEDs are used, as it shares DIO with the LEDS * */ DBG_vUartInit(DBG_E_UART_0, DBG_E_UART_BAUD_RATE_115200); DBG_vPrintf(TRACE_START, "\nAPP Start: Switch Power Up"); /* * Initialise the stack overflow exception to trigger if the end of the * stack is reached. See the linker command file to adjust the allocated * stack size. */ vAHI_SetStackOverflow(TRUE, (uint32)&_stack_low_water_mark); /* * Catch resets due to watchdog timer expiry. Comment out to harden code. */ if (bAHI_WatchdogResetEvent()) { DBG_vPrintf(TRACE_START, "\nAPP Start: Watchdog timer has reset device!"); DBG_vDumpStack(); #if HALT_ON_EXCEPTION vAHI_WatchdogStop(); while (1); #endif } /* initialise ROM based software modules */ #ifndef JENNIC_MAC_MiniMacShim u32AppApiInit(NULL, NULL, NULL, NULL, NULL, NULL); #endif /* Define HIGH_POWER_ENABLE to enable high power module */ #ifdef HIGH_POWER_ENABLE vAHI_HighPowerModuleEnable(TRUE, TRUE); #endif /* start the RTOS */ OS_vStart(vInitialiseApp, vUnclaimedInterrupt, vOSError); DBG_vPrintf(TRACE_START, "OS started\n"); /* idle task commences here */ while (TRUE) { /* Re-load the watch-dog timer. Execution must return through the idle * task before the CPU is suspended by the power manager. This ensures * that at least one task / ISR has executed with in the watchdog period * otherwise the system will be reset. */ DBG_vPrintf(TRACE_START, "#"); vAHI_WatchdogRestart(); /* * suspends CPU operation when the system is idle or puts the device to * sleep if there are no activities in progress */ PWRM_vManagePower(); DBG_vPrintf(TRACE_START, "?"); } }
/**************************************************************************** * * NAME: vAppMain * * DESCRIPTION: * Entry point for application from a cold start. * * RETURNS: * Never returns. * ****************************************************************************/ PUBLIC void vAppMain(void) { #ifdef DBG_ENABLE dbg_vPatchInit(); #endif os_vPatchInit(); /* Debug */ DBG_vUartInit(DBG_E_UART_1, DBG_E_UART_BAUD_RATE_115200); vAHI_UartSetRTSCTS(DBG_E_UART_1, FALSE); DBG_vPrintf(RTR_TRACE, "\n%d RTR < vAppMain()", NODE_sData.u32Timer); /* Initialise and turn on LEDs */ vLedInitRfd(); vLedControl(0, TRUE); vLedControl(1, TRUE); /* Initialise buttons */ vButtonInitRfd(); /* Watchdog ? */ #if NODE_FUNC_WATCHDOG { DBG_vPrintf(RTR_TRACE, " Stack low water mark = %08x", &stack_low_water_mark); *(volatile uint32 *)0x020010e0 = ((uint32)&stack_low_water_mark) | 0x8000000; if ((*(volatile uint32 *)0x02000004) & 0x80 ) { DBG_vPrintf(RTR_TRACE, " Watchdog timer has reset device!"); vAHI_WatchdogStop(); while(1); } } #else { /* Don't use watchdog */ vAHI_WatchdogStop(); } #endif /* Initialise application API */ u32AppApiInit(NULL, NULL, NULL, NULL, NULL, NULL); /* Start OS */ OS_vStart(RTR_vInit, RTR_vUncInt); /* Turn off LEDs */ vLedControl(0, FALSE); vLedControl(1, FALSE); /* Idle task commences on exit from OS start call */ while (TRUE) { /* Watchdog ? */ #if NODE_FUNC_WATCHDOG { /* Restart watchdog */ vAHI_WatchdogRestart(); } #endif /* Manage power */ PWRM_vManagePower(); } }
/**************************************************************************** * * NAME: Device_vInit * * DESCRIPTION: * Entry point for application * * RETURNS: * void, never returns * ****************************************************************************/ PUBLIC void Device_vInit(bool_t bWarmStart) { bool_t bFactoryReset; /* Initialise stack and hardware interfaces */ v6LP_InitHardware(); /* Cold start ? */ if (FALSE == bWarmStart) { /* Initialise exception handler */ Exception_vInit(); /* Initialise all DIO as outputs and drive low */ vAHI_DioSetDirection(0, 0xFFFFFFFE); vAHI_DioSetOutput(0, 0xFFFFFFFE); } /* Debug ? */ #ifdef DBG_ENABLE { /* Initialise debugging */ DBG_vUartInit(DEBUG_UART, DEBUG_BAUD_RATE); /* Disable the debug port flow control lines to turn off LED2 */ vAHI_UartSetRTSCTS(DEBUG_UART, FALSE); } #endif /* 6x chip family ? */ #ifdef JENNIC_CHIP_FAMILY_JN516x /* Wait for clock to stablise */ while(bAHI_Clock32MHzStable() == FALSE); #endif /* 42J01 chip ? */ #ifdef JENNIC_CHIP_JN5142J01 /* Wait for clock to stablise */ while(bAHI_Clock32MHzStable() == FALSE); #endif /* Debug */ DBG_vPrintf(TRUE, " "); DBG_vPrintf(TRUE, "\n\nDEVICE DIO"); DBG_vPrintf(DEBUG_DEVICE_FUNC, "\nDevice_vInit(%d)", bWarmStart); /* Node initialisation */ Node_vInit(bWarmStart); /* Cold start ? */ if (FALSE == bWarmStart) { /* 4x chip family ? */ #ifdef JENNIC_CHIP_FAMILY_JN514x { /* Check for factory reset using flags from flash */ bFactoryReset = Node_bTestFactoryResetFlash(); } #else { /* Check for factory reset using flags from EEPROM */ bFactoryReset = Node_bTestFactoryResetEeprom(); } #endif /* Reset the tick queue */ u8TickQueue = 0; /* Initialise PDM and MIB data */ Device_vPdmInit(); /* Apply factory reset if required */ if (bFactoryReset) Device_vReset(TRUE); /* Initialise JIP */ (void) Device_eJipInit(); } #ifdef MK_BLD_NODE_TYPE_END_DEVICE else { /* Debug */ DBG_vPrintf(DEBUG_DEVICE_FUNC, "\ni6LP_ResumeStack()"); /* Resume 6LoWPAN */ i6LP_ResumeStack(); } #endif /* Now initialised */ bInitialised = TRUE; /* Enter main loop */ Device_vMain(); /* Allow sleeping ? */ #ifdef MK_BLD_NODE_TYPE_END_DEVICE { /* Go to sleep if we exit main loop */ Device_vSleep(); } #endif }