Exemple #1
0
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++;
        }
    }
}
Exemple #2
0
/****************************************************************************
 *
 * 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;
}