Firstly, apologies if this is the wrong forum for (possible) bug reporting.
On to the business: I've been playing with the shell implementation in ChibiOS, just extending on some example code. I have implemented a shell function which takes arguments, namely a pwm function which takes an argument which is duty cycle. So in Chibi shell we could issue a command "pwm 45" to set the duty cycle of a pwm channel(s) (hardcoded) to 45%. To accomplish this, I am using the argc and argv arguments provided with the shell implementation.
Now, in the example program below, if the cmd_pwm is "invoked" by typing "pwm 47" at the shell, I expect argv[0] to be "pwm" and argv[1] to be "47" as this is how it works in Linux (POSIX systems?). However, in ChibiOS shell, it seems argv[0] is "47". I don't know if this has been intentionally done, but I thought I would point it out.
See example program below - cmd_pwm is the part of interest.
Code: Select all
/*
ChibiOS/RT - Copyright (C) 2006-2013 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 <stdlib.h>
#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#include "shell.h"
#include "usbcfg.h"
/* Virtual serial port over USB.*/
SerialUSBDriver SDU1;
/*===========================================================================*/
/* PWM related. */
/*===========================================================================*/
static const PWMConfig pwmcfg = {
1000000, /* 1MHz PWM clock frequency. */
100, /* PWM period is 100 cycles. */
NULL,
{
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_ACTIVE_HIGH, NULL}
},
/* HW dependent part.*/
0,
0
};
/*===========================================================================*/
/* Command line related. */
/*===========================================================================*/
#define SHELL_WA_SIZE THD_WA_SIZE(2048)
#define TEST_WA_SIZE THD_WA_SIZE(256)
static void cmd_mem(BaseSequentialStream *chp, int argc, char *argv[]) {
size_t n, size;
(void)argv;
if (argc > 0) {
chprintf(chp, "Usage: mem\r\n");
return;
}
n = chHeapStatus(NULL, &size);
chprintf(chp, "core free memory : %u bytes\r\n", chCoreStatus());
chprintf(chp, "heap fragments : %u\r\n", n);
chprintf(chp, "heap free total : %u bytes\r\n", size);
}
static void cmd_threads(BaseSequentialStream *chp, int argc, char *argv[]) {
static const char *states[] = {THD_STATE_NAMES};
Thread *tp;
(void)argv;
if (argc > 0) {
chprintf(chp, "Usage: threads\r\n");
return;
}
chprintf(chp, " addr stack prio refs state time\r\n");
tp = chRegFirstThread();
do {
chprintf(chp, "%.8lx %.8lx %4lu %4lu %9s %lu\r\n",
(uint32_t)tp, (uint32_t)tp->p_ctx.r13,
(uint32_t)tp->p_prio, (uint32_t)(tp->p_refs - 1),
states[tp->p_state], (uint32_t)tp->p_time);
tp = chRegNextThread(tp);
} while (tp != NULL);
}
static void cmd_greenled (BaseSequentialStream *chp, int argc, char *argv[]) {
static uint8_t state = 0;
(void)argv;
(void)argc;
chprintf(chp, "Green Led Command Selected\r\n");
if (!state) {
chprintf(chp, "Turning Green LED ON\r\n");
palSetPad(GPIOD, GPIOD_LED4);
state = 1;
} else {
chprintf(chp, "Turning Green LED OFF\r\n");
palClearPad(GPIOD, GPIOD_LED4);
state = 0;
}
}
static void cmd_orangeled (BaseSequentialStream *chp, int argc, char *argv[]) {
static uint8_t state = 0;
(void)argv;
(void)argc;
chprintf(chp, "Orange Led Command Selected\r\n");
if (!state) {
chprintf(chp, "Turning Orange LED ON\r\n");
palSetPad(GPIOD, GPIOD_LED3);
state = 1;
} else {
chprintf(chp, "Turning Orange LED OFF\r\n");
palClearPad(GPIOD, GPIOD_LED3);
state = 0;
}
}
static void cmd_pwm(BaseSequentialStream *chp, int argc, char *argv[]) {
int dt = 50;
chprintf(chp, "Enabling PWM\r\n");
if (!argc) {
pwmEnableChannel(&PWMD2, 2, dt);
pwmEnableChannel(&PWMD2, 3, dt);
chprintf(chp, "Duty Cycle set to 50%\r\n");
} else if (argc > 1) {
chprintf(chp, "Wrong number of arguments\r\n");
return;
} else {
chprintf(chp, "Figuring out duty cycle...\r\n");
/* Expecting... */
/* argv[0] = "pwm" */
/* argv[1] = "dt" */
dt = atoi(argv[0]);
/* dt = atoi(argv[1]); -- This does NOT work as in Linux, UNIX, systems */
chprintf(chp, "The DT passed is %d\r\n", dt);
return;
}
}
static const ShellCommand commands[] = {
{"mem", cmd_mem},
{"threads", cmd_threads},
{"greenled", cmd_greenled},
{"orangeled", cmd_orangeled},
{"pwm", cmd_pwm},
{NULL, NULL}
};
static const ShellConfig shell_cfg1 = {
(BaseSequentialStream *)&SDU1,
commands
};
/*===========================================================================*/
/* Initialization and main thread. */
/*===========================================================================*/
/*
* Application entry point.
*/
int main(void) {
Thread *shelltp = NULL;
/*
* 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();
/*
* Shell manager initialization.
*/
shellInit();
/*
* 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(1000);
usbStart(serusbcfg.usbp, &usbcfg);
usbConnectBus(serusbcfg.usbp);
/* Setup pins for PWM Output TIM2 CH3 and CH4 (PB10 and PB11) */
pwmStart(&PWMD2, &pwmcfg);
palSetPadMode(GPIOB, 10, PAL_MODE_ALTERNATE(1));
palSetPadMode(GPIOB, 11, PAL_MODE_ALTERNATE(1));
/*
* Normal main() thread activity, in this demo it just performs
* a shell respawn upon its termination.
*/
while (TRUE) {
if (!shelltp) {
if (SDU1.config->usbp->state == USB_ACTIVE) {
/* Spawns a new shell.*/
shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO);
}
}
else {
/* If the previous shell exited.*/
if (chThdTerminated(shelltp)) {
/* Recovers memory of the previous shell.*/
chThdRelease(shelltp);
shelltp = NULL;
}
}
chThdSleepMilliseconds(500);
}
}