int main() { int address; int number_of_slaves; sys_start(); i2c_module_init(); i2c_init(&i2c, &i2c_0_dev, I2C_BAUDRATE_100KBPS, -1); i2c_start(&i2c); std_printf(FSTR("Scanning the i2c bus for slaves...\r\n" "\r\n")); number_of_slaves = 0; for (address = 0; address < 128; address++) { if (i2c_scan(&i2c, address) == 1) { std_printf(FSTR("Found slave with address 0x%x.\r\n"), address); number_of_slaves++; } } std_printf(FSTR("\r\n" "Scan complete. Found %d slaves.\r\n"), number_of_slaves); return (0); }
static int enumerate(struct usb_host_device_t *device_p) { struct usb_descriptor_interface_t *int_desc_p; std_printf(FSTR("Enumerating a MASS_STORAGE device.\r\n")); int_desc_p = usb_desc_get_interface(device_p->descriptors.buf, device_p->descriptors.size, 1, 0); if (int_desc_p == NULL) { return (-1); } if (usb_host_device_set_configuration(device_p, 1) != 0) { return (-1); } get_max_lun(device_p); scsi_get_inquiry(device_p); if (scsi_test_unit_ready(device_p) != 0) { std_printf(FSTR("device is not ready\r\n")); } return (0); }
static int storage_init(fat16_read_t *read_p, fat16_write_t *write_p, void **arg_pp) { std_printf(FSTR("SD storage.\r\n")); std_printf(FSTR("spi bitrate = %lu kbps\r\n"), 2 * 16 * SAMPLES_PER_SOCOND / 1024); /* Initialize SPI for the SD card. */ spi_init(&spi, &spi_device[0], &pin_d53_dev, SPI_MODE_MASTER, SPI_SPEED_2MBPS, 0, 1); sd_init(&sd, &spi); if (sd_start(&sd) != 0) { return (-1); } std_printf(FSTR("sd card started\r\n")); *read_p = (fat16_read_t)sd_read_block; *write_p = (fat16_write_t)sd_write_block; *arg_pp = &sd; return (0); }
static int get_max_lun(struct usb_host_device_t *device_p) { struct usb_setup_t setup; uint8_t buf[1]; std_printf(FSTR("get number of LUN:s (logical units) from mass storage device\r\n")); /* Initiate the setup datastructure. */ setup.request_type = (REQUEST_TYPE_DATA_DIRECTION_DEVICE_TO_HOST | REQUEST_TYPE_TYPE_CLASS | REQUEST_TYPE_RECIPIENT_INTERFACE); setup.request = REQUEST_GET_MAX_LUN; setup.u.base.value = 0; setup.u.base.index = 0; setup.length = 1; if (usb_host_device_control_transfer(device_p, &setup, buf, sizeof(buf)) != sizeof(buf)) { return (-1); } std_printf(FSTR("LUN max = %d\r\n"), buf[0]); /* Save the max LUN number. */ //device_p->max_lun = 0; return (0); }
static int scsi_get_inquiry(struct usb_host_device_t *device_p) { struct cbw_t cbw; struct csw_t csw; struct scsi_cdb_inquiry_t *cdb_p; struct scsi_cdb_inquiry_data_t inquiry_data; std_printf(FSTR("get inquiry\r\n")); device_p->max_packet_size = 512; /* Initiate the Command Block Wrapper. */ memset(&cbw, 0, sizeof(cbw)); cbw.signature = CBW_SIGNATURE; cbw.tag = (uint32_t)device_p; cbw.data_transfer_length = sizeof(inquiry_data); cbw.flags = 0x80; cbw.lun = 0; cbw.command_block_length = sizeof(*cdb_p); cdb_p = (struct scsi_cdb_inquiry_t *)cbw.command_block; cdb_p->operation_code = SCSI_CDB_OPERATION_CODE_INQUIRY; cdb_p->allocation_length = sizeof(inquiry_data); /* Write the Command Block Wrapper to the device. */ if (usb_host_device_write(device_p, 2, &cbw, sizeof(cbw)) != sizeof(cbw)) { std_printf(FSTR("failed to write the command block wrapper.\r\n")); return (-1); } /* Read the data from the device. */ if (usb_host_device_read(device_p, 1, &inquiry_data, sizeof(inquiry_data)) != sizeof(inquiry_data)) { std_printf(FSTR("Failed to read data.\r\n")); return (-1); } /* Read the Comand Status Wrapper from the device. */ if (usb_host_device_read(device_p, 1, &csw, sizeof(csw)) != sizeof(csw)) { return (-1); } if (csw.status != CSW_STATUS_PASSED) { std_printf(FSTR("status = %d\r\n"), csw.status); return (-1); } std_printf(FSTR("version = %d\r\n"), inquiry_data.version); return (csw.data_residue); }
/** * Print a message after a script has been executed. */ static void print_exit_message(int res, const char *prefix_p) { if (res == 1) { std_printf(FSTR("%s exited normally.\r\n\r\n"), prefix_p); } else if (res & PYEXEC_FORCED_EXIT) { std_printf(FSTR("%s forced exit.\r\n\r\n"), prefix_p); } else { std_printf(FSTR("%s exited abnormally.\r\n\r\n"), prefix_p); } }
ssize_t usb_host_class_mass_storage_device_read( struct usb_host_device_t *device_p, void *buf_p, size_t address, size_t size) { struct cbw_t cbw; struct csw_t csw; struct scsi_cdb_read_t *cdb_p; device_p->max_packet_size = 512; /* Initiate the Command Block Wrapper. */ memset(&cbw, 0, sizeof(cbw)); cbw.signature = CBW_SIGNATURE; cbw.tag = (uint32_t)device_p; cbw.data_transfer_length = size; cbw.flags = 0x80; cbw.lun = 0; cbw.command_block_length = sizeof(*cdb_p); cdb_p = (struct scsi_cdb_read_t *)cbw.command_block; cdb_p->operation_code = SCSI_CDB_OPERATION_CODE_READ_10; cdb_p->logical_block_address = htonl(address / 512); cdb_p->transfer_length = htons(size / 512); /* Write the Command Block Wrapper to the device. */ if (usb_host_device_write(device_p, 2, &cbw, sizeof(cbw)) != sizeof(cbw)) { std_printf(FSTR("failed to write the command block wrapper.\r\n")); return (-1); } /* Read the dat from the device. */ if (usb_host_device_read(device_p, 1, buf_p, size) != size) { std_printf(FSTR("Failed to read data.\r\n")); return (-1); } /* Read the Comand Status Wrapper from the device. */ if (usb_host_device_read(device_p, 1, &csw, sizeof(csw)) != sizeof(csw)) { return (-1); } if (csw.status != CSW_STATUS_PASSED) { std_printf(FSTR("status = %d\r\n"), csw.status); return (-1); } return (size - csw.data_residue); }
static int test_sine_440_hz(struct harness_t *harness_p) { #if !defined(SKIP_TEST_SINE_440_HZ) int i; float sample; int samples_per_second = (membersof(dac_gen_sine) * 440); struct dac_driver_t dac; int total_number_of_samples; BTASSERT(dac_init(&dac, &dac_0_dev, &pin_0_dev, NULL, samples_per_second) == 0); /* Samples are in the range -1.0 to 1.0. Convert them to the range 0 to AMPLITUDE_MAX. */ for (i = 0; i < membersof(samples); i++) { sample = dac_gen_sine[i % membersof(dac_gen_sine)]; samples[i] = AMPLITUDE_MAX * ((sample + 1.0) / 2.0); } std_printf(FSTR("Converting %d samples.\r\n"), (int)membersof(samples)); BTASSERT(dac_convert(&dac, (uint32_t *)samples, membersof(samples) / 2) == 0); /* Converting the signal on the DAC0-pin for 5 seconds. */ total_number_of_samples = (5 * samples_per_second); std_printf(FSTR("Converting %d samples.\r\n"), total_number_of_samples); for (i = 0; i < total_number_of_samples; i += membersof(samples)) { BTASSERT(dac_async_convert(&dac, (uint32_t *)samples, membersof(samples) / 2) == 0); std_printf(FSTR("Samples left = %d\r\n"), total_number_of_samples - i); } std_printf(FSTR("Waiting for last samples to be converted\r\n")); BTASSERT(dac_async_wait(&dac) == 0); return (0); #else (void)dac_gen_sine; return (1); #endif }
int test_exti(struct harness_t *harness_p) { int i; struct exti_driver_t exti; struct pin_driver_t pin; pin_init(&pin, &pin_d4_dev, PIN_OUTPUT); pin_write(&pin, 1); BTASSERT(exti_init(&exti, &exti_d3_dev, EXTI_TRIGGER_FALLING_EDGE, isr, NULL) == 0); BTASSERT(exti_start(&exti) == 0); for (i = 0; i < 10; i++) { pin_write(&pin, 0); time_busy_wait_us(10000); pin_write(&pin, 1); time_busy_wait_us(10000); } std_printf(FSTR("flag = %d\r\n"), (int)flag); BTASSERT(flag == 10); return (0); }
static int cmd_set_bits_per_sample_cb(int argc, const char *argv[], void *out_p, void *in_p, void *arg_p, void *call_arg_p) { long bits_per_sample; if (argc != 2) { std_fprintf(out_p, FSTR("Usage: %s <number of bits>\r\n"), argv[0]); return (-EINVAL); } if (std_strtol(argv[1], &bits_per_sample) != 0) { std_fprintf(out_p, FSTR("Usage: %s <number of bits>\r\n"), argv[0]); return (-EINVAL); } if ((bits_per_sample < 0) || (bits_per_sample > 12)) { std_printf(FSTR("The number of bits per sample bust be " "an interger from 0 to 12.\r\n")); return (-EINVAL); } return (music_player_set_bits_per_sample(&music_player, bits_per_sample)); }
int test_get_temp(struct harness_t *harness_p) { struct owi_driver_t owi; struct ds18b20_driver_t ds; struct owi_device_t devices[4]; char buf[24]; int number_of_sensors; BTASSERT(owi_init(&owi, &pin_d7_dev, devices, membersof(devices)) == 0); BTASSERT(ds18b20_init(&ds, &owi) == 0); time_busy_wait_us(50000); number_of_sensors = owi_search(&owi); std_printf(FSTR("number_of_sensors = %d\r\n"), number_of_sensors); BTASSERT(number_of_sensors == 2); strcpy(buf, "drivers/ds18b20/list"); BTASSERT(fs_call(buf, NULL, sys_get_stdout(), NULL) == 0); time_busy_wait_us(50000); return (0); }
static int test_read_performance(struct harness_t *harness_p) { int i, block, res; struct time_t start, stop, diff; float seconds; unsigned long bytes_per_seconds; int number_of_blocks = 32; /* Write reference data to given block and verify that it was written. */ for (i = 0; i < membersof(buf); i++) { buf[i] = (i & 0xff); } time_get(&start); /* Write to and read from the first five blocks. */ for (block = 0; block < number_of_blocks; block++) { BTASSERT((res = sd_read_block(&sd, buf, block)) == SD_BLOCK_SIZE, ", res = %d\r\n", res); } time_get(&stop); time_diff(&diff, &stop, &start); seconds = (diff.seconds + diff.nanoseconds / 1000000000.0f); bytes_per_seconds = ((SD_BLOCK_SIZE * number_of_blocks) / seconds); std_printf(FSTR("Read 32 blocks of %d bytes in %f s (%lu bytes/s).\r\n"), SD_BLOCK_SIZE, seconds, bytes_per_seconds); return (0); }
int main() { sys_start(); uart_soft_init(&uart, &pin_d4_dev, &pin_d3_dev, &exti_d3_dev, 9600, qinbuf, sizeof(qinbuf)); fs_command_init(&cmd_at, CSTR("/at"), cmd_at_cb, NULL); fs_command_register(&cmd_at); std_printf(FSTR("Welcome to HC-0X configuration tool!\r\n" "\r\n" "SETUP: Connect pin34 to VCC so the device enters AT mode.\r\n" "\r\n" "Type 'at' to start communicating with the device.\r\n")); shell_init(&shell, sys_get_stdin(), sys_get_stdout(), NULL, NULL, NULL, NULL); shell_main(&shell); return (0); }
static int test_duty_cycles(struct harness_t *harness_p) { int percentage; long duty_cycle; BTASSERT(pwm_soft_start(&pwm_soft[0]) == 0); BTASSERT(pwm_soft_start(&pwm_soft[1]) == 0); thrd_sleep_ms(100); /* Test various duty cycles. */ for (percentage = 0; percentage <= 100; percentage += 10) { duty_cycle = pwm_soft_duty_cycle(percentage); BTASSERT(pwm_soft_set_duty_cycle(&pwm_soft[0], duty_cycle) == 0); BTASSERT(pwm_soft_set_duty_cycle(&pwm_soft[1], duty_cycle) == 0); std_printf(FSTR("Duty cycle: %ld\r\n"), duty_cycle); thrd_sleep_ms(100); } BTASSERT(pwm_soft_stop(&pwm_soft[0]) == 0); BTASSERT(pwm_soft_stop(&pwm_soft[1]) == 0); return (0); }
static int test_init(struct harness_t *harness_p) { struct thrd_t *thrd_p; int port = REMOTE_HOST_PORT; char remote_host_ip[] = STRINGIFY(REMOTE_HOST_IP); struct inet_addr_t remote_host_address; self_p = thrd_self(); std_printf(FSTR("Connecting to '%s:%d'.\r\n"), remote_host_ip, port); BTASSERT(inet_aton(remote_host_ip, &remote_host_address.ip) == 0); remote_host_address.port = port; BTASSERT(socket_open_tcp(&server_sock) == 0); BTASSERT(socket_connect(&server_sock, &remote_host_address) == 0); BTASSERT(mqtt_client_init(&client, "mqtt_client", NULL, &server_sock, &server_sock, on_publish, NULL) == 0); thrd_p = thrd_spawn(mqtt_client_main, &client, 0, stack, sizeof(stack)); thrd_set_log_mask(thrd_p, LOG_UPTO(DEBUG)); return (0); }
/** * The entry function for a Pumbaa application. */ int main() { int stack_dummy; int res; /* Start the system. */ sys_start(); std_printf(sys_get_info()); std_printf(FSTR("\r\n")); /* Initialize the thread module. */ #if MICROPY_PY_THREAD == 1 module_thread_init(); #endif stack_top_p = (char*)&stack_dummy; mp_stack_set_limit(40000 * (BYTES_PER_WORD / 4)); gc_init(heap, heap + sizeof(heap)); mp_init(); /* Initialize the keyboard interrupt object. */ MP_STATE_VM(keyboard_interrupt_obj) = mp_obj_new_exception(&mp_type_KeyboardInterrupt); /* Initialize sys.path and sys.argv. */ mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0); mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0); /* 1. Execute the file main.py. */ std_printf(FSTR("Executing file 'main.py'.\r\n")); res = pyexec_file("main.py"); print_exit_message(res, "File 'main.py'"); /* 2. Execute the frozen module main.py. */ std_printf(FSTR("Executing frozen module 'main.py'.\r\n")); res = pyexec_frozen_module("main.py"); print_exit_message(res, "Frozen module 'main.py'"); #if CONFIG_PUMBAA_MAIN_FRIENDLY_REPL == 1 /* 3. Execute the interactive shell. */ res = pyexec_friendly_repl(); print_exit_message(res, "Interactive shell"); #endif return (res != 1); }
static ssize_t write_block(void *arg_p, uint32_t dst_block, const void *src_p) { std_printf(FSTR("write block not supported.\r\n")); return (-1); }
static void print_header() { int i; std_printf(FSTR("+")); for (i = 0; i < membersof(adc); i++) { std_printf(FSTR("--------+")); } std_printf(FSTR("\r\n" "|")); for (i = 0; i < membersof(adc); i++) { std_printf(FSTR(" %6s |"), analog_pins[i].name_p); } std_printf(FSTR("\r\n" "+")); for (i = 0; i < membersof(adc); i++) { std_printf(FSTR("--------+")); } std_printf(FSTR("\r\n")); }
void __assert_func(const char *file, int line, const char *func, const char *expr) { std_printf(FSTR("assert:%s:%d:%s: %s\n"), file, line, func, expr); nlr_raise(mp_obj_new_exception_msg(&mp_type_AssertionError, "C-level assert")); }
static int test_get_set(struct harness_t *harness) { struct time_t time1; struct time_t time2; /* Start by getting the current time. */ BTASSERT(time_get(&time1) == 0); std_printf(FSTR("time1: seconds = %lu, nanoseconds = %lu\r\n"), time1.seconds, time1.nanoseconds); /* Wait a while and verify that the time has changed. */ thrd_sleep_us(100000); BTASSERT(time_get(&time2) == 0); std_printf(FSTR("time2: seconds = %lu, nanoseconds = %lu\r\n"), time2.seconds, time2.nanoseconds); BTASSERT((time2.seconds > time1.seconds) || (time2.nanoseconds > time1.nanoseconds)); /* Set the time to a high value. */ time1.seconds = 100; time1.nanoseconds = 350000000; BTASSERT(time_set(&time1) == 0); /* Get the new time. */ BTASSERT(time_get(&time1) == 0); std_printf(FSTR("time1: seconds = %lu, nanoseconds = %lu\r\n"), time1.seconds, time1.nanoseconds); /* Wait a while and verify that the time has changed. */ thrd_sleep_us(100000); BTASSERT(time_get(&time2) == 0); std_printf(FSTR("time2: seconds = %lu, nanoseconds = %lu\r\n"), time2.seconds, time2.nanoseconds); BTASSERT((time2.seconds > time1.seconds) || (time2.nanoseconds > time1.nanoseconds)); return (0); }
static int scsi_test_unit_ready(struct usb_host_device_t *device_p) { struct cbw_t cbw; struct csw_t csw; struct scsi_cdb_test_unit_ready_t *cdb_p; std_printf(FSTR("test unit ready\r\n")); device_p->max_packet_size = 512; /* Initiate the Command Block Wrapper. */ memset(&cbw, 0, sizeof(cbw)); cbw.signature = CBW_SIGNATURE; cbw.tag = (uint32_t)device_p; cbw.data_transfer_length = 0; cbw.flags = 0x80; cbw.lun = 0; cbw.command_block_length = sizeof(*cdb_p); cdb_p = (struct scsi_cdb_test_unit_ready_t *)cbw.command_block; cdb_p->operation_code = SCSI_CDB_OPERATION_CODE_TEST_UNIT_READY; /* Write the Command Block Wrapper to the device. */ if (usb_host_device_write(device_p, 2, &cbw, sizeof(cbw)) != sizeof(cbw)) { std_printf(FSTR("failed to write the command block wrapper.\r\n")); return (-1); } /* Read the Comand Status Wrapper from the device. */ if (usb_host_device_read(device_p, 1, &csw, sizeof(csw)) != sizeof(csw)) { return (-1); } if (csw.status != CSW_STATUS_PASSED) { std_printf(FSTR("status = %d\r\n"), csw.status); return (-1); } return (csw.data_residue); }
static int storage_init(fat16_read_t *read_p, fat16_write_t *write_p, void **arg_pp) { struct usb_host_device_t *device_p; union usb_message_t message; std_printf(FSTR("USB storage.\r\n")); /* Initialize the USB host driver. */ usb_host_init(&usb, &usb_device[0], host_devices, membersof(host_devices)); usb_host_class_mass_storage_init(&mass_storage, &usb, mass_storage_devices, membersof(mass_storage_devices)); usb_host_class_mass_storage_start(&mass_storage); /* Start the USB driver. */ usb_host_start(&usb); chan_read(&usb.control, &message, sizeof(message)); std_printf(FSTR("The USB control thread read a message of type %d\r\n"), message.header.type); if (message.header.type != USB_MESSAGE_TYPE_ADD) { std_printf(FSTR("bad message type %d\r\n"), message.header.type); return (-1); } device_p = usb_host_device_open(&usb, message.add.device); *read_p = read_block; *write_p = write_block; *arg_pp = device_p; return (0); }
/** * The builtin help() function. * * help([object]) */ static mp_obj_t builtin_help(size_t n_args, const mp_obj_t *args_p) { if (n_args == 0) { std_printf(help_text_p); } else { pyhelp_print_obj(args_p[0]); } return mp_const_none; }
static int test_scan(struct harness_t *harness_p) { int number_of_slaves_found; int address; number_of_slaves_found = 0; for (address = 0; address < 128; address++) { if (i2c_scan(&i2c, address) == 1) { std_printf(OSTR("Found slave with address 0x%02x.\r\n"), address); number_of_slaves_found++; } } std_printf(OSTR("Number of slaves found: %d\r\n"), number_of_slaves_found); return (0); }
int battery_get_stored_energy_level(struct battery_t *battery_p) { int energy_level; energy_level = battery_stub_energy_level[0]; std_printf(FSTR("battery_stub: energy_level = %d\r\n"), energy_level); return (energy_level); }
int main() { int i, j; uint16_t samples[membersof(analog_pins)]; sys_start(); adc_module_init(); for (i = 0; i < membersof(adc); i++) { adc_init(&adc[i], analog_pins[i].adc_dev_p, analog_pins[i].pin_dev_p, ADC_REFERENCE_VCC, 1); } i = 0; while (1) { if ((i % 10) == 0) { print_header(); } std_printf(FSTR("|")); for (j = 0; j < membersof(adc); j++) { samples[j] = 0xffff; adc_convert(&adc[j], &samples[j], 1); } for (j = 0; j < membersof(adc); j++) { std_printf(FSTR(" %6d |"), (int)samples[j]); } std_printf(FSTR("\r\n")); thrd_sleep_ms(500); i++; } return (0); }
int main() { int res, attempt; char remote_host_ip[] = STRINGIFY(REMOTE_HOST_IP); struct inet_ip_addr_t remote_host_ip_address; struct time_t round_trip_time, timeout; sys_start(); if (inet_aton(remote_host_ip, &remote_host_ip_address) != 0) { std_printf(FSTR("Bad ip address '%s'.\r\n"), remote_host_ip); return (-1); } timeout.seconds = 3; timeout.nanoseconds = 0; attempt = 1; /* Ping the remote host once every second. */ while (1) { res = ping_host_by_ip_address(&remote_host_ip_address, &timeout, &round_trip_time); if (res == 0) { std_printf(FSTR("Successfully pinged '%s' (#%d).\r\n"), remote_host_ip, attempt); } else { std_printf(FSTR("Failed to ping '%s' (#%d).\r\n"), remote_host_ip, attempt); } attempt++; thrd_sleep(1); } return (0); }
static int handle_event_play(struct music_player_t *self_p) { const char *path_p; switch (self_p->state) { case STATE_PAUSED: self_p->state = STATE_PLAYING; break; case STATE_STOPPED: if ((path_p = self_p->cb.current_song_path_p(self_p->cb.arg_p)) != NULL) { fat16_file_open(self_p->fat16_p, &self_p->file, path_p, O_READ); strcpy(self_p->path, path_p); self_p->state = STATE_PLAYING; } break; case STATE_IDLE: if ((path_p = self_p->cb.current_song_path_p(self_p->cb.arg_p)) != NULL) { if (fat16_file_open(self_p->fat16_p, &self_p->file, path_p, O_READ) == 0) { strcpy(self_p->path, path_p); self_p->state = STATE_PLAYING; } else { std_printf(FSTR("Failed to open %s\r\n"), path_p); } } break; default: break; } if (self_p->state == STATE_PLAYING) { std_printf(FSTR("Playing | %s\r\n"), self_p->path); } return (0); }
static int test_list_devices(struct harness_t *harness_p) { BTASSERT(usb_host_init(&usb, &usb_device[0], host_devices, membersof(host_devices)) == 0); BTASSERT(usb_host_start(&usb) == 0); std_printf(FSTR("ADDRESS CLASS VENDOR PRODUCT\r\n")); BTASSERT(usb_host_stop(&usb) == 0); return (0); }
int main() { sys_start(); /* Configure and start the the WiFi module as a Station and SoftAP. */ esp_wifi_set_op_mode(esp_wifi_op_mode_station_softap_t); if (esp_wifi_softap_init("Simba", NULL) != 0) { std_printf(FSTR("Failed to configure the Soft AP.\r\n")); } if (esp_wifi_station_init("Qvist2", "maxierik", NULL) != 0) { std_printf(FSTR("Failed to configure the Station.\r\n")); } while (1) { esp_wifi_print(sys_get_stdout()); thrd_sleep(2); } return (0); }