Beispiel #1
0
/*---------------------------------------------------------------------------*/
uint32_t
pic32_clock_calculate_brg(uint32_t mul, uint32_t desired)
{
    uint32_t fp = pic32_clock_get_peripheral_clock();

    uint32_t brg[3];
    uint32_t obtained[3];
    int32_t err[3];

    uint32_t min;

    brg[0] = fp / (mul * desired);     // +1
    brg[1] = fp / (mul * desired) - 1; //  0
    brg[2] = fp / (mul * desired) - 2; // -1

    obtained[0] = fp / (mul * (brg[0] + 1));
    obtained[1] = fp / (mul * (brg[1] + 1));
    obtained[2] = fp / (mul * (brg[2] + 1));

    err[0] = obtained[0] - desired;
    err[1] = obtained[1] - desired;
    err[2] = obtained[2] - desired;

    err[0] = err[0] < 0 ? -err[0] : err[0];
    err[1] = err[1] < 0 ? -err[1] : err[1];
    err[2] = err[2] < 0 ? -err[2] : err[2];

    min = 0;
    min = err[1] < err[min] ? 1 : min;
    min = err[2] < err[min] ? 2 : min;

    return brg[min];
}
Beispiel #2
0
int uart_get_real_baudrate(uint32_t *baudrate)
{
#ifndef __USE_UART_PORT3__
    fprintf(stderr, "uart: __USE_UART_PORT3__ not defined\n");
    return -1;
#else
    if(baudrate == NULL)
    {
        fprintf(stderr, "uart: Cannot set baudrate using null pointer.\n");
        return -1;
    }

    /* From section 21. UART:
     *
     * If BRGH = 0:
     *                    Fpb
     * Baud rate = -----------------
     *              16 * (U3BRG + 1)
     *
     * If BRGH = 1:
     *                    Fpb
     * Baud rate = -----------------
     *              4 * (U3BRG + 1)
     */
    uint32_t x = (U3MODE & _U3MODE_BRGH_MASK) ? 4 : 16;
    *baudrate = pic32_clock_get_peripheral_clock() / (x * (U3BRG + 1));

    return 0;
#endif
}
int main(int argc, char **argv)
{
	int32_t r;

	process_init(); // run before any function that starts a process
	pic32_init();
	watchdog_init();
	leds_init();
	leds_progress_init();
	buzzer_init();

	clock_init();
	rtimer_init();
	ctimer_init();

	leds_on(LEDS_ALL);

        /* Serial line init part 2/3: set up the UART port. */
	uart_console_init(UART_BAUDRATE);

//	usb_serial_init();
//	usb_serial_set_input(serial_line_input_byte);

        /* Serial line init part 3/3: start the OS process. */
	serial_line_init();

	asm volatile("ei");  // enable interrupts

	PRINTF("CPU Clock: %uMhz\n",
	       pic32_clock_get_system_clock() / 1000000);
	PRINTF("Peripheral Clock: %uMhz\n",
	       pic32_clock_get_peripheral_clock() / 1000000);

	random_init(4321);
	process_start(&etimer_process, NULL);
	process_start(&sensors_process, NULL);
	SENSORS_ACTIVATE(button_sensor);

	/* Starting autostarting process */
	print_processes(autostart_processes);
	autostart_start(autostart_processes);

	leds_off(LEDS_ALL);
	watchdog_start();
	PRINTF("Starting the main scheduler loop\n");

	/*
	 * This is the scheduler loop.
	 */
	while (1) {

		do {
			/* Reset watchdog. */
			watchdog_periodic();
			r = process_run();
		} while (r > 0);

#if LPM_MODE > LPM_MODE_NONE
		watchdog_stop();
		/* low-power mode start */
		asm volatile("wait");
		/* low-power mode end */
		watchdog_start();
#endif // LPM_MODE
	}

	return 0;
}