Beispiel #1
0
// unregister all interrupt sources
void uart_deinit_all(void) {
    for (int i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(pyb_uart_obj_all)); i++) {
        pyb_uart_obj_t *uart_obj = MP_STATE_PORT(pyb_uart_obj_all)[i];
        if (uart_obj != NULL && !uart_obj->is_static) {
            uart_deinit(uart_obj);
            MP_STATE_PORT(pyb_uart_obj_all)[i] = NULL;
        }
    }
}
Beispiel #2
0
/**
 * @brief   Low level serial driver stop.
 * @details De-initializes the UART, stops the associated clock, resets the
 *          interrupt vector.
 *
 * @param[in] sdp       pointer to a @p SerialDriver object
 */
void sd_lld_stop(SerialDriver *sdp) {

  if (sdp->state == SD_READY) {
    uart_deinit(sdp->uart);
#if USE_LPC13xx_UART0
    if (&SD1 == sdp) {
      LPC_SYSCON->SYSAHBCLKCTRL &= ~(1 << 12);
      NVICDisableVector(UART_IRQn);
      return;
    }
#endif
  }
}
Beispiel #3
0
/**
 * @brief   Low level serial driver stop.
 * @details De-initializes the UART, stops the associated clock, resets the
 *          interrupt vector.
 *
 * @param[in] sdp       pointer to a @p SerialDriver object
 *
 * @notapi
 */
void sd_lld_stop(SerialDriver *sdp) {

  if (sdp->state == SD_READY) {
    uart_deinit(sdp->uart);
#if LPC11xx_SERIAL_USE_UART0
    if (&SD1 == sdp) {
      LPC_SYSCON->UARTCLKDIV = 0;
      LPC_SYSCON->SYSAHBCLKCTRL &= ~(1 << 12);
      nvicDisableVector(UART_IRQn);
      return;
    }
#endif
  }
}
Beispiel #4
0
/**
 *   \brief This will start a sleep operation.
 *
 *   \param val Used for remembering the new menu to display after a wakeup.
*/
void
menu_run_sleep(uint8_t *val)
{
    /* Turn off LED, LCD, ADC, Timer 1, SPI */
    led_off();
    lcd_deinit();
 	key_deinit();
    PRR |= (1 << PRTIM1) | (1 << PRSPI);

    /* Tell the 1284P to turn off the radio and sleep */
	sleep_count=0;
    uart_serial_send_frame(SEND_SLEEP, 1, (uint8_t *)&sleep_count);

    /* Turn off UART when transmission is complete */
	while(!(UCSR0A & (1 << TXC0)));
    _delay_us(10000); //deinit trash clears done flag on 1284p
	uart_deinit();

    /* Go to sleep until button is pushed */
    sleep_now(0);

    /* Yawn, waking up, turn on LCD with Raven Logo */
    lcd_init();
    lcd_symbol_set(LCD_SYMBOL_RAVEN);

	/* Disable interrupts before powering everything up */
    cli();
    key_init();
    PRR &= ~((1 << PRTIM1) | (1 << PRSPI));
 	uart_init();

    /* Enable interrupts, Wake up 1284p and radio */
	sei();
    sleep_wakeup();
//	uart_init();//flush receive buffer

    /* Wait for buttons up */
    while (key_state_get() != KEY_NO_KEY)
        ;
    if (is_button()){
        get_button();
    }
}
Beispiel #5
0
/**
 * @brief   Low level serial driver stop.
 * @details De-initializes the UART, stops the associated clock, resets the
 *          interrupt vector.
 *
 * @param[in] sdp       pointer to a @p SerialDriver object
 *
 * @notapi
 */
void sd_lld_stop(SerialDriver *sdp) {

  if (sdp->state == SD_READY) {
    uart_deinit(sdp->uart);
#if USE_LPC214x_UART0
    if (&SD1 == sdp) {
      PCONP = (PCONP & PCALL) & ~PCUART0;
      VICIntEnClear = INTMASK(SOURCE_UART0);
      return;
    }
#endif
#if USE_LPC214x_UART1
    if (&SD2 == sdp) {
      PCONP = (PCONP & PCALL) & ~PCUART1;
      VICIntEnClear = INTMASK(SOURCE_UART1);
      return;
    }
#endif
  }
}
Beispiel #6
0
void main()
{
	char commands[] = {128, 131 };
	char commands2[] = { 145, 0, 10, 0, 105};
	//char commands2[] = {152, 13, 137, 1, 44, 128, 0, 156, 1, 144, 137, 0, 0, 0, 0};
	int num_of_commands=2;
	int num_of_commands2=5;	

	int fd;
	fd=uart_init(57600,"/dev/ttyUSB0");
	for(int i=0;i<num_of_commands;i++)
	{
		uart_tx(fd,commands[i]);
	}
	sleep(1);
	for(int i=0;i<num_of_commands2;i++)
	{
		uart_tx(fd,commands2[i]);
	}
        sleep(1);
	printf("stopping\n");
//stop
	char commands3 [] = {128, 131}; 
	char commands4 [] ={145, 0, 0, 0, 0};
	//unsigned char commands[] = {128,132,139,2,0,0};
	int num_of_commands3=2;
	int num_of_commands4=5;	

	for(int i=0;i<num_of_commands3;i++)
	{
		uart_tx(fd,commands3[i]);
	}
	sleep(1);
	for(int i=0;i<num_of_commands4;i++)
	{
		uart_tx(fd,commands4[i]);
	}
	uart_deinit(fd);
}
Beispiel #7
0
/**
 * @brief   Low level serial driver stop.
 * @details De-initializes the UART, stops the associated clock, resets the
 *          interrupt vector.
 *
 * @param[in] sdp       pointer to a @p SerialDriver object
 *
 * @notapi
 */
void sd_lld_stop(SerialDriver *sdp) {

  if (sdp->state == SD_READY) {
    uart_deinit(sdp->uart);
#if LPC17xx_SERIAL_USE_UART0
    if (&SD1 == sdp) {
      LPC_SC->PCONP &= ~(1 << 3);
      nvicDisableVector(UART0_IRQn);
      return;
    }
#endif

#if LPC17xx_SERIAL_USE_UART1
    if (&SD2 == sdp) {
      LPC_SC->PCONP &= ~(1 << 4);
      nvicDisableVector(UART1_IRQn);
      return;
    }
#endif

#if LPC17xx_SERIAL_USE_UART2
    if (&SD3 == sdp) {
      LPC_SC->PCONP &= ~(1 << 24);
      nvicDisableVector(UART2_IRQn);
      return;
    }
#endif

#if LPC17xx_SERIAL_USE_UART3
    if (&SD4 == sdp) {
      LPC_SC->PCONP &= ~(1 << 25);
      nvicDisableVector(UART3_IRQn);
      return;
    }
#endif
  }
}
/**
 * @brief   Low level serial driver stop.
 * @details De-initializes the UART, stops the associated clock, resets the
 *          interrupt vector.
 *
 * @param[in] sdp       pointer to a @p SerialDriver object
 *
 * @notapi
 */
void sd_lld_stop(SerialDriver *sdp)
{
  if (sdp->state == SD_READY) {
    uart_deinit(sdp->uart);
#if TIVA_SERIAL_USE_UART0
    if (&SD1 == sdp) {
      HWREG(SYSCTL_RCGCUART) &= ~(1 << 0);  /* disable UART0 module */
      nvicDisableVector(TIVA_UART0_NUMBER);
      return;
    }
#endif
#if TIVA_SERIAL_USE_UART1
    if (&SD2 == sdp) {
      HWREG(SYSCTL_RCGCUART) &= ~(1 << 1);  /* disable UART1 module */
      nvicDisableVector(TIVA_UART1_NUMBER);
      return;
    }
#endif
#if TIVA_SERIAL_USE_UART2
    if (&SD3 == sdp) {
      HWREG(SYSCTL_RCGCUART) &= ~(1 << 2);  /* disable UART2 module */
      nvicDisableVector(TIVA_UART2_NUMBER);
      return;
    }
#endif
#if TIVA_SERIAL_USE_UART3
    if (&SD4 == sdp) {
      HWREG(SYSCTL_RCGCUART) &= ~(1 << 3);  /* disable UART3 module */
      nvicDisableVector(TIVA_UART3_NUMBER);
      return;
    }
#endif
#if TIVA_SERIAL_USE_UART4
    if (&SD5 == sdp) {
      HWREG(SYSCTL_RCGCUART) &= ~(1 << 4);  /* disable UART4 module */
      nvicDisableVector(TIVA_UART4_NUMBER);
      return;
    }
#endif
#if TIVA_SERIAL_USE_UART5
    if (&SD6 == sdp) {
      HWREG(SYSCTL_RCGCUART) &= ~(1 << 5);  /* disable UART5 module */
      nvicDisableVector(TIVA_UART5_NUMBER);
      return;
    }
#endif
#if TIVA_SERIAL_USE_UART6
    if (&SD7 == sdp) {
      HWREG(SYSCTL_RCGCUART) &= ~(1 << 6);  /* disable UART6 module */
      nvicDisableVector(TIVA_UART6_NUMBER);
      return;
    }
#endif
#if TIVA_SERIAL_USE_UART7
    if (&SD8 == sdp) {
      HWREG(SYSCTL_RCGCUART) &= ~(1 << 7);  /* disable UART7 module */
      nvicDisableVector(TIVA_UART7_NUMBER);
      return;
    }
#endif
  }
}
Beispiel #9
0
/// \method deinit()
/// Turn off the UART bus.
STATIC mp_obj_t pyb_uart_deinit(mp_obj_t self_in) {
    pyb_uart_obj_t *self = self_in;
    uart_deinit(self);
    return mp_const_none;
}
Beispiel #10
0
int main(void) {
    // TODO disable JTAG

    // Stack limit should be less than real stack size, so we have a chance
    // to recover from limit hit.  (Limit is measured in bytes.)
    mp_stack_set_limit((char*)&_ram_end - (char*)&_heap_end - 1024);

    /* STM32F4xx HAL library initialization:
         - Configure the Flash prefetch, instruction and Data caches
         - Configure the Systick to generate an interrupt each 1 msec
         - Set NVIC Group Priority to 4
         - Global MSP (MCU Support Package) initialization
       */
    HAL_Init();

    // set the system clock to be HSE
    SystemClock_Config();

    // enable GPIO clocks
    __GPIOA_CLK_ENABLE();
    __GPIOB_CLK_ENABLE();
    __GPIOC_CLK_ENABLE();
    __GPIOD_CLK_ENABLE();

    // enable the CCM RAM
    __CCMDATARAMEN_CLK_ENABLE();

#if 0
#if defined(NETDUINO_PLUS_2)
    {
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

#if MICROPY_HW_HAS_SDCARD
        // Turn on the power enable for the sdcard (PB1)
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
        GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_SET);
#endif

        // Turn on the power for the 5V on the expansion header (PB2)
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
        GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_SET);
    }
#endif
#endif

    // basic sub-system init
    pendsv_init();
    timer_tim3_init();
    led_init();
#if MICROPY_HW_HAS_SWITCH
    switch_init0();
#endif

    int first_soft_reset = true;

soft_reset:

    // check if user switch held to select the reset mode
    led_state(1, 0);
    led_state(2, 1);
    led_state(3, 0);
    led_state(4, 0);
    uint reset_mode = 1;

#if MICROPY_HW_HAS_SWITCH
    if (switch_get()) {
        for (uint i = 0; i < 3000; i++) {
            if (!switch_get()) {
                break;
            }
            HAL_Delay(20);
            if (i % 30 == 29) {
                if (++reset_mode > 3) {
                    reset_mode = 1;
                }
                led_state(2, reset_mode & 1);
                led_state(3, reset_mode & 2);
                led_state(4, reset_mode & 4);
            }
        }
        // flash the selected reset mode
        for (uint i = 0; i < 6; i++) {
            led_state(2, 0);
            led_state(3, 0);
            led_state(4, 0);
            HAL_Delay(50);
            led_state(2, reset_mode & 1);
            led_state(3, reset_mode & 2);
            led_state(4, reset_mode & 4);
            HAL_Delay(50);
        }
        HAL_Delay(400);
    }
#endif

#if MICROPY_HW_ENABLE_RTC
    if (first_soft_reset) {
        rtc_init();
    }
#endif

    // more sub-system init
#if MICROPY_HW_HAS_SDCARD
    if (first_soft_reset) {
        sdcard_init();
    }
#endif
    if (first_soft_reset) {
        storage_init();
    }

    // GC init
    gc_init(&_heap_start, &_heap_end);

    // Micro Python init
    mp_init();
    mp_obj_list_init(mp_sys_path, 0);
    mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
    mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash));
    mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash_slash_lib));
    mp_obj_list_init(mp_sys_argv, 0);

    // Change #if 0 to #if 1 if you want REPL on UART_6 (or another uart)
    // as well as on USB VCP
#if 0
    {
        mp_obj_t args[2] = {
            MP_OBJ_NEW_SMALL_INT(PYB_UART_6),
            MP_OBJ_NEW_SMALL_INT(115200),
        };
        pyb_stdio_uart = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, MP_ARRAY_SIZE(args), 0, args);
    }
#else
    pyb_stdio_uart = NULL;
#endif

    // Initialise low-level sub-systems.  Here we need to very basic things like
    // zeroing out memory and resetting any of the sub-systems.  Following this
    // we can run Python scripts (eg boot.py), but anything that is configurable
    // by boot.py must be set after boot.py is run.

    readline_init0();
    pin_init0();
    extint_init0();
    timer_init0();
    uart_init0();

#if MICROPY_HW_ENABLE_RNG
    rng_init0();
#endif

    i2c_init0();
    spi_init0();
    pyb_usb_init0();

    // Initialise the local flash filesystem.
    // Create it if needed, and mount in on /flash.
    {
        // try to mount the flash
        FRESULT res = f_mount(&fatfs0, "/flash", 1);
        if (reset_mode == 3 || res == FR_NO_FILESYSTEM) {
            // no filesystem, or asked to reset it, so create a fresh one

            // LED on to indicate creation of LFS
            led_state(PYB_LED_R2, 1);
            uint32_t start_tick = HAL_GetTick();

            res = f_mkfs("/flash", 0, 0);
            if (res == FR_OK) {
                // success creating fresh LFS
            } else {
                __fatal_error("could not create LFS");
            }

            // set label
            f_setlabel("/flash/pybflash");

            // create empty main.py
            FIL fp;
            f_open(&fp, "/flash/main.py", FA_WRITE | FA_CREATE_ALWAYS);
            UINT n;
            f_write(&fp, fresh_main_py, sizeof(fresh_main_py) - 1 /* don't count null terminator */, &n);
            // TODO check we could write n bytes
            f_close(&fp);

            // create .inf driver file
            f_open(&fp, "/flash/pybcdc.inf", FA_WRITE | FA_CREATE_ALWAYS);
            f_write(&fp, fresh_pybcdc_inf, sizeof(fresh_pybcdc_inf) - 1 /* don't count null terminator */, &n);
            f_close(&fp);

            // create readme file
            f_open(&fp, "/flash/README.txt", FA_WRITE | FA_CREATE_ALWAYS);
            f_write(&fp, fresh_readme_txt, sizeof(fresh_readme_txt) - 1 /* don't count null terminator */, &n);
            f_close(&fp);

            // keep LED on for at least 200ms
            sys_tick_wait_at_least(start_tick, 200);
            led_state(PYB_LED_R2, 0);
        } else if (res == FR_OK) {
            // mount sucessful
        } else {
            __fatal_error("could not access LFS");
        }
    }

    // The current directory is used as the boot up directory.
    // It is set to the internal flash filesystem by default.
    f_chdrive("/flash");

    // Make sure we have a /flash/boot.py.  Create it if needed.
    {
        FILINFO fno;
#if _USE_LFN
        fno.lfname = NULL;
        fno.lfsize = 0;
#endif
        FRESULT res = f_stat("/flash/boot.py", &fno);
        if (res == FR_OK) {
            if (fno.fattrib & AM_DIR) {
                // exists as a directory
                // TODO handle this case
                // see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation
            } else {
                // exists as a file, good!
            }
        } else {
            // doesn't exist, create fresh file

            // LED on to indicate creation of boot.py
            led_state(PYB_LED_R2, 1);
            uint32_t start_tick = HAL_GetTick();

            FIL fp;
            f_open(&fp, "/flash/boot.py", FA_WRITE | FA_CREATE_ALWAYS);
            UINT n;
            f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n);
            // TODO check we could write n bytes
            f_close(&fp);

            // keep LED on for at least 200ms
            sys_tick_wait_at_least(start_tick, 200);
            led_state(PYB_LED_R2, 0);
        }
    }

#if defined(USE_DEVICE_MODE)
    usb_storage_medium_t usb_medium = USB_STORAGE_MEDIUM_FLASH;
#endif

#if MICROPY_HW_HAS_SDCARD
    // if an SD card is present then mount it on /sd/
    if (sdcard_is_present()) {
        FRESULT res = f_mount(&fatfs1, "/sd", 1);
        if (res != FR_OK) {
            printf("[SD] could not mount SD card\n");
        } else {
            // use SD card as current directory
            f_chdrive("/sd");

            // TODO these should go before the /flash entries in the path
            mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd));
            mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd_slash_lib));

            if (first_soft_reset) {
                // use SD card as medium for the USB MSD
#if defined(USE_DEVICE_MODE)
                usb_medium = USB_STORAGE_MEDIUM_SDCARD;
#endif
            }
        }
    }
#endif

    // reset config variables; they should be set by boot.py
    pyb_config_main = MP_OBJ_NULL;
    pyb_config_usb_mode = MP_OBJ_NULL;

    // run boot.py, if it exists
    // TODO perhaps have pyb.reboot([bootpy]) function to soft-reboot and execute custom boot.py
    if (reset_mode == 1) {
        const char *boot_py = "boot.py";
        FRESULT res = f_stat(boot_py, NULL);
        if (res == FR_OK) {
            int ret = pyexec_file(boot_py);
            if (ret & PYEXEC_FORCED_EXIT) {
                goto soft_reset_exit;
            }
            if (!ret) {
                flash_error(4);
            }
        }
    }

    // turn boot-up LEDs off
    led_state(2, 0);
    led_state(3, 0);
    led_state(4, 0);

    // Now we initialise sub-systems that need configuration from boot.py,
    // or whose initialisation can be safely deferred until after running
    // boot.py.

#if defined(USE_HOST_MODE)
    // USB host
    pyb_usb_host_init();
#elif defined(USE_DEVICE_MODE)
    // USB device
    usb_device_mode_t usb_mode = USB_DEVICE_MODE_CDC_MSC;
    // if we are not in reset_mode==1, this config variable will always be NULL
    if (pyb_config_usb_mode != MP_OBJ_NULL) {
        if (strcmp(mp_obj_str_get_str(pyb_config_usb_mode), "CDC+HID") == 0) {
            usb_mode = USB_DEVICE_MODE_CDC_HID;
        }
    }
    pyb_usb_dev_init(usb_mode, usb_medium);
#endif

#if MICROPY_HW_HAS_MMA7660
    // MMA accel: init and reset
    accel_init();
#endif

#if MICROPY_HW_ENABLE_SERVO
    // servo
    servo_init();
#endif

#if MICROPY_HW_ENABLE_DAC
    // DAC
    dac_init();
#endif

    mod_network_init();

    // At this point everything is fully configured and initialised.

    // Run the main script from the current directory.
    if (reset_mode == 1 && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
        const char *main_py;
        if (pyb_config_main == MP_OBJ_NULL) {
            main_py = "main.py";
        } else {
            main_py = mp_obj_str_get_str(pyb_config_main);
        }
        FRESULT res = f_stat(main_py, NULL);
        if (res == FR_OK) {
            int ret = pyexec_file(main_py);
            if (ret & PYEXEC_FORCED_EXIT) {
                goto soft_reset_exit;
            }
            if (!ret) {
                flash_error(3);
            }
        }
    }

    // Main script is finished, so now go into REPL mode.
    // The REPL mode can change, or it can request a soft reset.
    for (;;) {
        if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
            if (pyexec_raw_repl() != 0) {
                break;
            }
        } else {
            if (pyexec_friendly_repl() != 0) {
                break;
            }
        }
    }

soft_reset_exit:

    // soft reset

    printf("PYB: sync filesystems\n");
    storage_flush();

    printf("PYB: soft reboot\n");
    timer_deinit();
    uart_deinit();

    first_soft_reset = false;
    goto soft_reset;
}