aboutsummaryrefslogtreecommitdiffstats
path: root/testhal/STM32/STM32L4xx/USB_CDC/main.c
blob: 79e4e8687bb8b3ba2e5b235f2c8767c44a1dd7da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
    ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include <stdio.h>
#include <string.h>

#include "ch.h"
#include "hal.h"

#include "shell.h"
#include "chprintf.h"

#include "usbcfg.h"

/*===========================================================================*/
/* Command line related.                                                     */
/*===========================================================================*/

#define SHELL_WA_SIZE   THD_WORKING_AREA_SIZE(2048)

/* Can be measured using dd if=/dev/xxxx of=/dev/null bs=512 count=10000.*/
static void cmd_write(BaseSequentialStream *chp, int argc, char *argv[]) {
  static uint8_t buf[] =
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";

  (void)argv;
  if (argc > 0) {
    chprintf(chp, "Usage: write\r\n");
    return;
  }

  while (chnGetTimeout((BaseChannel *)chp, TIME_IMMEDIATE) == Q_TIMEOUT) {
#if 1
    /* Writing in channel mode.*/
    chnWrite(&SDU1, buf, sizeof buf - 1);
#else
    /* Writing in buffer mode.*/
    (void) obqGetEmptyBufferTimeout(&SDU1.obqueue, TIME_INFINITE);
    memcpy(SDU1.obqueue.ptr, buf, SERIAL_USB_BUFFERS_SIZE);
    obqPostFullBuffer(&SDU1.obqueue, SERIAL_USB_BUFFERS_SIZE);
#endif
  }
  chprintf(chp, "\r\n\nstopped\r\n");
}

static const ShellCommand commands[] = {
  {"write", cmd_write},
  {NULL, NULL}
};

static const ShellConfig shell_cfg1 = {
  (BaseSequentialStream *)&SDU1,
  commands
};

/*===========================================================================*/
/* Generic code.                                                             */
/*===========================================================================*/

/*
 * LED blinker thread, times are in milliseconds.
 */
static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg) {

  (void)arg;
  chRegSetThreadName("blinker");
  while (true) {
    systime_t time;

    time = serusbcfg.usbp->state == USB_ACTIVE ? 250 : 500;
    palClearLine(LINE_LED_GREEN);
    chThdSleepMilliseconds(time);
    palSetLine(LINE_LED_GREEN);
    chThdSleepMilliseconds(time);
  }
}

/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * Initializes a serial-over-USB CDC driver.
   */
  sduObjectInit(&SDU1);
  sduStart(&SDU1, &serusbcfg);

  /*
   * Activates the USB driver and then the USB bus pull-up on D+.
   * Note, a delay is inserted in order to not have to disconnect the cable
   * after a reset.
   */
  usbDisconnectBus(serusbcfg.usbp);
  chThdSleepMilliseconds(1500);
  usbStart(serusbcfg.usbp, &usbcfg);
  usbConnectBus(serusbcfg.usbp);

  /*
   * Shell manager initialization.
   */
  shellInit();

  /*
   * Creates the blinker thread.
   */
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

  /*
   * Normal main() thread activity, spawning shells.
   */
  while (true) {
    if (SDU1.config->usbp->state == USB_ACTIVE) {
      thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
                                              "shell", NORMALPRIO + 1,
                                              shellThread, (void *)&shell_cfg1);
      chThdWait(shelltp);               /* Waiting termination.             */
    }
    chThdSleepMilliseconds(1000);
  }
}
">, struct xen_processor_power *power); static long cpu_frequency_change_helper(void *data) { return cpu_frequency_change(this_cpu(freq)); } static long cpu_down_helper(void *data) { int cpu = (unsigned long)data; return cpu_down(cpu); } ret_t do_platform_op(XEN_GUEST_HANDLE(xen_platform_op_t) u_xenpf_op) { ret_t ret = 0; struct xen_platform_op curop, *op = &curop; if ( !IS_PRIV(current->domain) ) return -EPERM; if ( copy_from_guest(op, u_xenpf_op, 1) ) return -EFAULT; if ( op->interface_version != XENPF_INTERFACE_VERSION ) return -EACCES; spin_lock(&xenpf_lock); switch ( op->cmd ) { case XENPF_settime: { ret = xsm_xen_settime(); if ( ret ) break; do_settime(op->u.settime.secs, op->u.settime.nsecs, op->u.settime.system_time); ret = 0; } break; case XENPF_add_memtype: { ret = xsm_memtype(op->cmd); if ( ret ) break; ret = mtrr_add_page( op->u.add_memtype.mfn, op->u.add_memtype.nr_mfns, op->u.add_memtype.type, 1); if ( ret >= 0 ) { op->u.add_memtype.handle = 0; op->u.add_memtype.reg = ret; ret = copy_to_guest(u_xenpf_op, op, 1) ? -EFAULT : 0; if ( ret != 0 ) mtrr_del_page(ret, 0, 0); } } break; case XENPF_del_memtype: { ret = xsm_memtype(op->cmd); if ( ret ) break; if (op->u.del_memtype.handle == 0 /* mtrr/main.c otherwise does a lookup */ && (int)op->u.del_memtype.reg >= 0) { ret = mtrr_del_page(op->u.del_memtype.reg, 0, 0); if ( ret > 0 ) ret = 0; } else ret = -EINVAL; } break; case XENPF_read_memtype: { unsigned long mfn, nr_mfns; mtrr_type type; ret = xsm_memtype(op->cmd); if ( ret ) break; ret = -EINVAL; if ( op->u.read_memtype.reg < num_var_ranges ) { mtrr_if->get(op->u.read_memtype.reg, &mfn, &nr_mfns, &type); op->u.read_memtype.mfn = mfn; op->u.read_memtype.nr_mfns = nr_mfns; op->u.read_memtype.type = type; ret = copy_to_guest(u_xenpf_op, op, 1) ? -EFAULT : 0; } } break; case XENPF_microcode_update: { XEN_GUEST_HANDLE(const_void) data; ret = xsm_microcode(); if ( ret ) break; guest_from_compat_handle(data, op->u.microcode.data); ret = microcode_update(data, op->u.microcode.length); } break; case XENPF_platform_quirk: { extern int opt_noirqbalance; int quirk_id = op->u.platform_quirk.quirk_id; ret = xsm_platform_quirk(quirk_id); if ( ret ) break; switch ( quirk_id ) { case QUIRK_NOIRQBALANCING: printk("Platform quirk -- Disabling IRQ balancing/affinity.\n"); opt_noirqbalance = 1; setup_ioapic_dest(); break; case QUIRK_IOAPIC_BAD_REGSEL: case QUIRK_IOAPIC_GOOD_REGSEL: #ifndef sis_apic_bug sis_apic_bug = (quirk_id == QUIRK_IOAPIC_BAD_REGSEL); dprintk(XENLOG_INFO, "Domain 0 says that IO-APIC REGSEL is %s\n", sis_apic_bug ? "bad" : "good"); #else BUG_ON(sis_apic_bug != (quirk_id == QUIRK_IOAPIC_BAD_REGSEL)); #endif break; default: ret = -EINVAL; break; } } break; case XENPF_firmware_info: ret = xsm_firmware_info(); if ( ret ) break; switch ( op->u.firmware_info.type ) { case XEN_FW_DISK_INFO: { const struct edd_info *info; u16 length; ret = -ESRCH; if ( op->u.firmware_info.index >= bootsym(boot_edd_info_nr) ) break; info = bootsym(boot_edd_info) + op->u.firmware_info.index; /* Transfer the EDD info block. */ ret = -EFAULT; if ( copy_from_compat(&length, op->u.firmware_info.u. disk_info.edd_params, 1) ) break; if ( length > info->edd_device_params.length ) length = info->edd_device_params.length; if ( copy_to_compat(op->u.firmware_info.u.disk_info.edd_params, (u8 *)&info->edd_device_params, length) ) break; if ( copy_to_compat(op->u.firmware_info.u.disk_info.edd_params, &length, 1) ) break; /* Transfer miscellaneous other information values. */ #define C(x) op->u.firmware_info.u.disk_info.x = info->x C(device); C(version); C(interface_support); C(legacy_max_cylinder); C(legacy_max_head); C(legacy_sectors_per_track); #undef C ret = (copy_field_to_guest(u_xenpf_op, op, u.firmware_info.u.disk_info) ? -EFAULT : 0); break; } case XEN_FW_DISK_MBR_SIGNATURE: { const struct mbr_signature *sig; ret = -ESRCH; if ( op->u.firmware_info.index >= bootsym(boot_mbr_signature_nr) ) break; sig = bootsym(boot_mbr_signature) + op->u.firmware_info.index; op->u.firmware_info.u.disk_mbr_signature.device = sig->device; op->u.firmware_info.u.disk_mbr_signature.mbr_signature = sig->signature; ret = (copy_field_to_guest(u_xenpf_op, op, u.firmware_info.u.disk_mbr_signature) ? -EFAULT : 0); break; } case XEN_FW_VBEDDC_INFO: ret = -ESRCH; if ( op->u.firmware_info.index != 0 ) break; if ( *(u32 *)bootsym(boot_edid_info) == 0x13131313 ) break; op->u.firmware_info.u.vbeddc_info.capabilities = bootsym(boot_edid_caps); op->u.firmware_info.u.vbeddc_info.edid_transfer_time = bootsym(boot_edid_caps) >> 8; ret = 0; if ( copy_field_to_guest(u_xenpf_op, op, u.firmware_info. u.vbeddc_info.capabilities) || copy_field_to_guest(u_xenpf_op, op, u.firmware_info. u.vbeddc_info.edid_transfer_time) || copy_to_compat(op->u.firmware_info.u.vbeddc_info.edid, bootsym(boot_edid_info), 128) ) ret = -EFAULT; break; default: ret = -EINVAL; break; } break; case XENPF_enter_acpi_sleep: ret = xsm_acpi_sleep(); if ( ret ) break; ret = acpi_enter_sleep(&op->u.enter_acpi_sleep); break; case XENPF_change_freq: ret = xsm_change_freq(); if ( ret ) break; ret = -ENOSYS; if ( cpufreq_controller != FREQCTL_dom0_kernel ) break; ret = -EINVAL; if ( op->u.change_freq.flags || !cpu_online(op->u.change_freq.cpu) ) break; per_cpu(freq, op->u.change_freq.cpu) = op->u.change_freq.freq; ret = continue_hypercall_on_cpu(op->u.change_freq.cpu, cpu_frequency_change_helper, NULL); break; case XENPF_getidletime: { uint32_t cpu; uint64_t idletime, now = NOW(); struct xenctl_cpumap ctlmap; cpumask_t cpumap; XEN_GUEST_HANDLE(uint8) cpumap_bitmap; XEN_GUEST_HANDLE(uint64) idletimes; ret = xsm_getidletime(); if ( ret ) break; ret = -ENOSYS; if ( cpufreq_controller != FREQCTL_dom0_kernel ) break; ctlmap.nr_cpus = op->u.getidletime.cpumap_nr_cpus; guest_from_compat_handle(cpumap_bitmap, op->u.getidletime.cpumap_bitmap); ctlmap.bitmap.p = cpumap_bitmap.p; /* handle -> handle_64 conversion */ xenctl_cpumap_to_cpumask(&cpumap, &ctlmap); guest_from_compat_handle(idletimes, op->u.getidletime.idletime); for_each_cpu_mask ( cpu, cpumap ) { if ( idle_vcpu[cpu] == NULL ) cpu_clear(cpu, cpumap); idletime = get_cpu_idle_time(cpu); ret = -EFAULT; if ( copy_to_guest_offset(idletimes, cpu, &idletime, 1) ) goto out; } op->u.getidletime.now = now; cpumask_to_xenctl_cpumap(&ctlmap, &cpumap); ret = copy_to_guest(u_xenpf_op, op, 1) ? -EFAULT : 0; } break; case XENPF_set_processor_pminfo: switch ( op->u.set_pminfo.type ) { case XEN_PM_PX: if ( !(xen_processor_pmbits & XEN_PROCESSOR_PM_PX) ) { ret = -ENOSYS; break; } ret = set_px_pminfo(op->u.set_pminfo.id, &op->u.set_pminfo.u.perf); break; case XEN_PM_CX: if ( !(xen_processor_pmbits & XEN_PROCESSOR_PM_CX) ) { ret = -ENOSYS; break; } ret = set_cx_pminfo(op->u.set_pminfo.id, &op->u.set_pminfo.u.power); break; case XEN_PM_TX: if ( !(xen_processor_pmbits & XEN_PROCESSOR_PM_TX) ) { ret = -ENOSYS; break; } ret = -EINVAL; break; default: ret = -EINVAL; break; } break; case XENPF_get_cpuinfo: { struct xenpf_pcpuinfo *g_info; g_info = &op->u.pcpu_info; spin_lock(&cpu_add_remove_lock); if ( (g_info->xen_cpuid >= NR_CPUS) || (g_info->xen_cpuid < 0) || !cpu_present(g_info->xen_cpuid) ) { g_info->flags |= XEN_PCPU_FLAGS_INVALID; } else { g_info->apic_id = x86_cpu_to_apicid[g_info->xen_cpuid]; g_info->acpi_id = acpi_get_processor_id(g_info->xen_cpuid); ASSERT(g_info->apic_id != BAD_APICID); if (cpu_online(g_info->xen_cpuid)) g_info->flags |= XEN_PCPU_FLAGS_ONLINE; } g_info->max_present = last_cpu(cpu_present_map); spin_unlock(&cpu_add_remove_lock); ret = copy_to_guest(u_xenpf_op, op, 1) ? -EFAULT : 0; } break; case XENPF_cpu_online: { int cpu; cpu = op->u.cpu_ol.cpuid; if (!cpu_present(cpu)) { ret = -EINVAL; break; } else if (cpu_online(cpu)) { ret = 0; break; } ret = cpu_up(cpu); break; } case XENPF_cpu_offline: { int cpu; cpu = op->u.cpu_ol.cpuid; if (!cpu_present(cpu)) { ret = -EINVAL; break; } else if (!cpu_online(cpu)) { ret = 0; break; } ret = continue_hypercall_on_cpu( 0, cpu_down_helper, (void *)(unsigned long)cpu); break; } break; case XENPF_cpu_hotadd: ret = cpu_add(op->u.cpu_add.apic_id, op->u.cpu_add.acpi_id, op->u.cpu_add.pxm); break; case XENPF_mem_hotadd: ret = memory_add(op->u.mem_add.spfn, op->u.mem_add.epfn, op->u.mem_add.pxm); break; default: ret = -ENOSYS; break; } out: spin_unlock(&xenpf_lock); return ret; } /* * Local variables: * mode: C * c-set-style: "BSD" * c-basic-offset: 4 * tab-width: 4 * indent-tabs-mode: nil * End: */