Merge branch 'feature/drive' into 'develop'

Feature/drive



See merge request !8
This commit is contained in:
tkl 2016-08-18 12:19:08 +00:00
commit 859adc5d35
18 changed files with 438 additions and 51 deletions

View File

@ -23,16 +23,22 @@
<tool id="org.eclipse.cdt.build.core.settings.holder.libs.583579175" name="holder for library settings" superClass="org.eclipse.cdt.build.core.settings.holder.libs"/>
<tool id="org.eclipse.cdt.build.core.settings.holder.804428440" name="Assembly" superClass="org.eclipse.cdt.build.core.settings.holder">
<option id="org.eclipse.cdt.build.core.settings.holder.incpaths.1612628392" name="Include Paths" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths" valueType="includePath">
<listOptionValue builtIn="false" value="/opt/arm-2011.09/arm-none-eabi/include"/>
<listOptionValue builtIn="false" value="/opt/arm-2011.09/lib/gcc/arm-none-eabi/4.6.1/include"/>
</option>
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.1218410972" languageId="org.eclipse.cdt.core.assembly" languageName="Assembly" sourceContentType="org.eclipse.cdt.core.asmSource" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
</tool>
<tool id="org.eclipse.cdt.build.core.settings.holder.1838895569" name="GNU C++" superClass="org.eclipse.cdt.build.core.settings.holder">
<option id="org.eclipse.cdt.build.core.settings.holder.incpaths.1874409566" name="Include Paths" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths" valueType="includePath">
<listOptionValue builtIn="false" value="/opt/arm-2011.09/arm-none-eabi/include"/>
<listOptionValue builtIn="false" value="/opt/arm-2011.09/lib/gcc/arm-none-eabi/4.6.1/include"/>
</option>
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.258587679" languageId="org.eclipse.cdt.core.g++" languageName="GNU C++" sourceContentType="org.eclipse.cdt.core.cxxSource,org.eclipse.cdt.core.cxxHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
</tool>
<tool id="org.eclipse.cdt.build.core.settings.holder.433020597" name="GNU C" superClass="org.eclipse.cdt.build.core.settings.holder">
<option id="org.eclipse.cdt.build.core.settings.holder.incpaths.7772132" name="Include Paths" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths" valueType="includePath">
<listOptionValue builtIn="false" value="/opt/arm-2011.09/arm-none-eabi/include"/>
<listOptionValue builtIn="false" value="/opt/arm-2011.09/lib/gcc/arm-none-eabi/4.6.1/include"/>
</option>
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.761231156" languageId="org.eclipse.cdt.core.gcc" languageName="GNU C" sourceContentType="org.eclipse.cdt.core.cSource,org.eclipse.cdt.core.cHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
</tool>
@ -64,16 +70,22 @@
<tool id="org.eclipse.cdt.build.core.settings.holder.libs.232481989" name="holder for library settings" superClass="org.eclipse.cdt.build.core.settings.holder.libs"/>
<tool id="org.eclipse.cdt.build.core.settings.holder.562391475" name="Assembly" superClass="org.eclipse.cdt.build.core.settings.holder">
<option id="org.eclipse.cdt.build.core.settings.holder.incpaths.1491856129" name="Include Paths" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths" valueType="includePath">
<listOptionValue builtIn="false" value="/opt/arm-2011.09/arm-none-eabi/include"/>
<listOptionValue builtIn="false" value="/opt/arm-2011.09/lib/gcc/arm-none-eabi/4.6.1/include"/>
</option>
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.275129533" languageId="org.eclipse.cdt.core.assembly" languageName="Assembly" sourceContentType="org.eclipse.cdt.core.asmSource" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
</tool>
<tool id="org.eclipse.cdt.build.core.settings.holder.193886409" name="GNU C++" superClass="org.eclipse.cdt.build.core.settings.holder">
<option id="org.eclipse.cdt.build.core.settings.holder.incpaths.715382618" name="Include Paths" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths" valueType="includePath">
<listOptionValue builtIn="false" value="/opt/arm-2011.09/arm-none-eabi/include"/>
<listOptionValue builtIn="false" value="/opt/arm-2011.09/lib/gcc/arm-none-eabi/4.6.1/include"/>
</option>
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.1629243941" languageId="org.eclipse.cdt.core.g++" languageName="GNU C++" sourceContentType="org.eclipse.cdt.core.cxxSource,org.eclipse.cdt.core.cxxHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
</tool>
<tool id="org.eclipse.cdt.build.core.settings.holder.1854601715" name="GNU C" superClass="org.eclipse.cdt.build.core.settings.holder">
<option id="org.eclipse.cdt.build.core.settings.holder.incpaths.1688578924" name="Include Paths" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths" valueType="includePath">
<listOptionValue builtIn="false" value="/opt/arm-2011.09/arm-none-eabi/include"/>
<listOptionValue builtIn="false" value="/opt/arm-2011.09/lib/gcc/arm-none-eabi/4.6.1/include"/>
</option>
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.683508981" languageId="org.eclipse.cdt.core.gcc" languageName="GNU C" sourceContentType="org.eclipse.cdt.core.cSource,org.eclipse.cdt.core.cHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
</tool>

View File

@ -1,4 +1,4 @@
CHECK_FOLDER += source/application
SUB_FOLDER += source/application
INCLUDES += source/application
INCLUDES += source/application/include
DOC_SRC += source/application

View File

@ -0,0 +1,125 @@
/*
* drive_ctrl.c
*
* Created on: Aug 10, 2016
* Author: tkl
*/
#include <stdlib.h>
#include "driver.h"
#include "board_devices.h"
#include "engines.h"
#include "drive_ctrl.h"
struct drive_ctrl {
struct engine_ctrl *left_forward;
struct engine_ctrl *left_backward;
struct engine_ctrl *right_forward;
struct engine_ctrl *right_backward;
};
struct drive_ctrl_object {
unsigned int current_speed;
};
static struct drive_ctrl drive_ctrl = {
.left_forward = &left_forward,
.left_backward = &left_backward,
.right_forward = &right_forward,
.right_backward = &right_backward,
};
static struct drive_ctrl_object drive_ctrl_object = {
.current_speed = 50, /* % */
};
int drive_ctrl_init(void)
{
unsigned int duty = 0;
/* open enable pins */
open(drive_ctrl.left_forward->enable);
write(drive_ctrl.left_forward->enable, "0", 1);
open(drive_ctrl.left_backward->enable);
write(drive_ctrl.left_backward->enable, "0", 1);
open(drive_ctrl.right_forward->enable);
write(drive_ctrl.right_forward->enable, "0", 1);
open(drive_ctrl.right_backward->enable);
write(drive_ctrl.right_backward->enable, "0", 1);
/* open pwm's */
open(drive_ctrl.left_backward->pwm);
ioctl(drive_ctrl.left_backward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty);
open(drive_ctrl.left_forward->pwm);
ioctl(drive_ctrl.left_forward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty);
open(drive_ctrl.right_backward->pwm);
ioctl(drive_ctrl.right_backward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty);
open(drive_ctrl.right_forward->pwm);
ioctl(drive_ctrl.right_forward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty);
/* enable enable pins */
write(drive_ctrl.left_forward->enable, "1", 1);
write(drive_ctrl.left_backward->enable, "1", 1);
write(drive_ctrl.right_forward->enable, "1", 1);
write(drive_ctrl.right_backward->enable, "1", 1);
return 0;
}
static unsigned int calc_speed_front(int speed_percent)
{
unsigned int speed_front = 0;
if(speed_percent > 0) {
if(speed_percent > 100)
speed_front = 100;
else
speed_front = (unsigned int) speed_percent;
}
return speed_front;
}
static unsigned int calc_speed_back(int speed_percent)
{
unsigned int speed_back = 0;
if(speed_percent < 0) {
if(speed_percent < -100)
speed_back = 100;
else
speed_back = (unsigned int) abs(speed_percent);
}
return speed_back;
}
int drive_ctrl_set_speed_left(int speed_percent)
{
unsigned int front_speed = calc_speed_front(speed_percent);
unsigned int back_speed = calc_speed_back(speed_percent);
int ret = ioctl(drive_ctrl.left_backward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&back_speed);
ret |= ioctl(drive_ctrl.left_forward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&front_speed);
return ret;
}
int drive_ctrl_set_speed_right(int speed_percent)
{
unsigned int front_speed = calc_speed_front(speed_percent);
unsigned int back_speed = calc_speed_back(speed_percent);
int ret = ioctl(drive_ctrl.right_backward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&back_speed);
ret |= ioctl(drive_ctrl.right_forward->pwm, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&front_speed);
return ret;
}
int drive_ctrl_set_speed_value(int speed_percent)
{
drive_ctrl_object.current_speed = speed_percent;
return 0;
}
int drive_ctrl_get_speed_value(void)
{
return drive_ctrl_object.current_speed;
}

View File

@ -0,0 +1,19 @@
/*
* drive_ctrl.h
*
* Created on: Aug 10, 2016
* Author: tkl
*/
#ifndef SOURCE_APPLICATION_INCLUDE_DRIVE_CTRL_H_
#define SOURCE_APPLICATION_INCLUDE_DRIVE_CTRL_H_
int drive_ctrl_init(void);
int drive_ctrl_set_speed_left(int speed_percent);
int drive_ctrl_set_speed_right(int speed_percent);
int drive_ctrl_set_speed_value(int speed_percent);
int drive_ctrl_get_speed_value(void);
#endif /* SOURCE_APPLICATION_INCLUDE_DRIVE_CTRL_H_ */

View File

@ -0,0 +1,36 @@
/*
* engines.h
*
* Created on: Aug 10, 2016
* Author: tkl
*/
#ifndef SOURCE_APPLICATION_INCLUDE_ENGINES_H_
#define SOURCE_APPLICATION_INCLUDE_ENGINES_H_
struct engine_ctrl {
const struct driver *pwm;
const struct driver *enable;
};
static struct engine_ctrl right_forward = {
.pwm = &pwm_2,
.enable = &gpio_c0,
};
static struct engine_ctrl right_backward = {
.pwm = &pwm_1,
.enable = &gpio_c1,
};
static struct engine_ctrl left_forward = {
.pwm = &pwm_4,
.enable = &gpio_c2,
};
static struct engine_ctrl left_backward = {
.pwm = &pwm_3,
.enable = &gpio_c3,
};
#endif /* SOURCE_APPLICATION_INCLUDE_ENGINES_H_ */

View File

@ -0,0 +1,13 @@
/*
* shell_commands.h
*
* Created on: Aug 10, 2016
* Author: tkl
*/
#ifndef SOURCE_APPLICATION_SHELL_COMMANDS_H_
#define SOURCE_APPLICATION_SHELL_COMMANDS_H_
int shell_commands_init(void);
#endif /* SOURCE_APPLICATION_SHELL_COMMANDS_H_ */

View File

@ -1,55 +1,21 @@
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "driver.h"
#include "board_devices.h"
#include "stack.h"
#include "queue.h"
#include "kernel.h"
#include "driver.h"
#include "list.h"
#include "shell.h"
#include "shell_commands.h"
#include "drive_ctrl.h"
#define STACK_SIZE 256
stack_t tc_1_stack[STACK_SIZE];
struct thread_context tc_1;
void task1(void *arg)
{
char rd = '0';
open(&led_4);
write(&led_4, &rd, 1);
while(1) {
sleep_ms(1000);
read(&led_4, &rd, 1);
if(rd == '0')
rd = '1';
else
rd = '0';
write(&led_4, &rd, 1);
write(&uart_1, "Driver test\r\n", 13);
}
}
char rx_buf[80];
void task2(void *arg)
{
while(1) {
int i = read(&uart_1, rx_buf, 80);
if(i > 0) {
write(&uart_1, rx_buf, i);
}
}
}
stack_t tc_2_stack[STACK_SIZE];
struct thread_context tc_2;
int main(void)
{
open(&uart_1);
thread_create(&tc_1, tc_1_stack, STACK_SIZE, task1, NULL, THREAD_PRIO_LOW);
thread_create(&tc_2, tc_2_stack, STACK_SIZE, task2, NULL, THREAD_PRIO_LOW);
drive_ctrl_init();
shell_init(&uart_1);
shell_commands_init();
schedule_start();

View File

@ -0,0 +1,152 @@
/*
* shell_commands.c
*
* Created on: Aug 10, 2016
* Author: tkl
*/
#include <stddef.h>
#include <stdlib.h>
#include "list.h"
#include "driver.h"
#include "shell.h"
#include "drive_ctrl.h"
static void *drive_turn_right_cb(const char *param);
static void *drive_turn_left_cb(const char *param);
static void *drive_boost_cb(const char *param);
static void *drive_retard_cb(const char *param);
static void *drive_halt_cb(const char *param);
static void *drive_forward_cb(const char *param);
static void *drive_backward_cb(const char *param);
static struct command drive_forward = {
.command = "f",
.description = "Start driving forward with preset speed.",
.command_callback = drive_forward_cb,
};
static struct command drive_backward = {
.command = "b",
.description = "Start driving backward with preset speed.",
.command_callback = drive_backward_cb,
};
static struct command drive_halt = {
.command = "h",
.description = "Stop driving.",
.command_callback = drive_halt_cb,
};
static struct command drive_boost = {
.command = "+",
.description = "Boost speed.",
.command_callback = drive_boost_cb,
};
static struct command drive_retard = {
.command = "-",
.description = "Retard speed",
.command_callback = drive_retard_cb,
};
static struct command drive_turn_left = {
.command = "l",
.description = "Turn left.",
.command_callback = drive_turn_left_cb,
};
static struct command drive_turn_right = {
.command = "r",
.description = "Turn right",
.command_callback = drive_turn_right_cb,
};
int shell_commands_init(void)
{
shell_add_command(&drive_forward);
shell_add_command(&drive_backward);
shell_add_command(&drive_boost);
shell_add_command(&drive_retard);
shell_add_command(&drive_halt);
shell_add_command(&drive_turn_left);
shell_add_command(&drive_turn_right);
return 0;
}
static void *drive_turn_right_cb(const char *param)
{
int speed = drive_ctrl_get_speed_value();
drive_ctrl_set_speed_left(abs(speed));
if(speed > 0)
speed *= -1;
drive_ctrl_set_speed_right(speed);
return NULL;
}
static void *drive_turn_left_cb(const char *param)
{
int speed = drive_ctrl_get_speed_value();
drive_ctrl_set_speed_right(abs(speed));
if(speed > 0)
speed *= -1;
drive_ctrl_set_speed_left(speed);
return NULL;
}
static void *drive_boost_cb(const char *param)
{
int speed = drive_ctrl_get_speed_value();
speed += 10;
drive_ctrl_set_speed_value(speed);
drive_ctrl_set_speed_left(speed);
drive_ctrl_set_speed_right(speed);
return NULL;
}
static void *drive_retard_cb(const char *param)
{
int speed = drive_ctrl_get_speed_value();
speed -= 10;
drive_ctrl_set_speed_value(speed);
drive_ctrl_set_speed_left(speed);
drive_ctrl_set_speed_right(speed);
return NULL;
}
static void *drive_halt_cb(const char *param)
{
drive_ctrl_set_speed_left(0);
drive_ctrl_set_speed_right(0);
return NULL;
}
static void *drive_forward_cb(const char *param)
{
int speed = drive_ctrl_get_speed_value();
if(speed < 0)
speed = abs(speed);
drive_ctrl_set_speed_value(speed);
drive_ctrl_set_speed_left(speed);
drive_ctrl_set_speed_right(speed);
return NULL;
}
static void *drive_backward_cb(const char *param)
{
int speed = drive_ctrl_get_speed_value();
if(speed > 0)
speed *= -1;
drive_ctrl_set_speed_value(speed);
drive_ctrl_set_speed_left(speed);
drive_ctrl_set_speed_right(speed);
return NULL;
}

View File

@ -3,10 +3,14 @@
#ifndef BOARD_DEVICES_H
#define BOARD_DEVICES_H
extern const struct driver pwm_4;
extern const struct driver pwm_3;
extern const struct driver pwm_2;
extern const struct driver pwm_1;
extern const struct driver uart_1;
extern const struct driver led_3;
extern const struct driver led_4;
extern const struct driver led_5;
extern const struct driver led_6;
extern const struct driver gpio_c0;
extern const struct driver gpio_c1;
extern const struct driver gpio_c2;
extern const struct driver gpio_c3;
#endif /* BOARD_DEVICES_H */

View File

@ -8,10 +8,13 @@
#ifndef SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_DRIVER_H_
#define SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_DRIVER_H_
#define IOCTL_PWM_SET_DUTY_CYCLE 0
enum driver_type {
DRIVER_TYPE_ADC,
DRIVER_TYPE_GPIO,
DRIVER_TYPE_I2C,
DRIVER_TYPE_PWM,
DRIVER_TYPE_RTC,
DRIVER_TYPE_SPI,
DRIVER_TYPE_UART
@ -26,5 +29,6 @@ int open(const struct driver *driver);
int close(const struct driver *driver);
int read(const struct driver *driver, char *buffer, int len);
int write(const struct driver *driver, const char *buffer, int len);
int ioctl(const struct driver *driver, unsigned int cmd, const void *data);
#endif /* SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_DRIVER_H_ */

View File

@ -0,0 +1,25 @@
/*
* list.h
*
* Created on: Jul 27, 2016
* Author: tkl
*/
#ifndef SOURCE_FIRMWARE_KERNEL_LIST_H_
#define SOURCE_FIRMWARE_KERNEL_LIST_H_
struct list_node {
struct list_node *next;
unsigned int data;
};
struct list {
struct list_node *front;
struct list_node *rear;
};
int list_init(struct list *head);
int list_add(struct list *head, struct list_node *node);
int list_get_len(struct list *head);
#endif /* SOURCE_FIRMWARE_KERNEL_LIST_H_ */

View File

@ -0,0 +1,23 @@
/*
* shell.h
*
* Created on: Aug 1, 2016
* Author: tkl
*/
#ifndef SOURCE_FIRMWARE_KERNEL_INTERFACE_SHELL_H_
#define SOURCE_FIRMWARE_KERNEL_INTERFACE_SHELL_H_
typedef void *(*command_callback)(const char*);
struct command {
const char *command;
const char *description;
const command_callback command_callback;
struct list_node item;
};
int shell_init(const struct driver *shell_device);
int shell_add_command(struct command *command);
#endif /* SOURCE_FIRMWARE_KERNEL_INTERFACE_SHELL_H_ */

View File

@ -0,0 +1 @@
libkosmos-arm-stm32f4-discovery-0.0.5-dbg.a

View File

@ -3,10 +3,14 @@
#ifndef BOARD_DEVICES_H
#define BOARD_DEVICES_H
extern const struct driver pwm_4;
extern const struct driver pwm_3;
extern const struct driver pwm_2;
extern const struct driver pwm_1;
extern const struct driver uart_1;
extern const struct driver led_3;
extern const struct driver led_4;
extern const struct driver led_5;
extern const struct driver led_6;
extern const struct driver gpio_c0;
extern const struct driver gpio_c1;
extern const struct driver gpio_c2;
extern const struct driver gpio_c3;
#endif /* BOARD_DEVICES_H */

View File

@ -8,10 +8,13 @@
#ifndef SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_DRIVER_H_
#define SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_DRIVER_H_
#define IOCTL_PWM_SET_DUTY_CYCLE 0
enum driver_type {
DRIVER_TYPE_ADC,
DRIVER_TYPE_GPIO,
DRIVER_TYPE_I2C,
DRIVER_TYPE_PWM,
DRIVER_TYPE_RTC,
DRIVER_TYPE_SPI,
DRIVER_TYPE_UART
@ -26,5 +29,6 @@ int open(const struct driver *driver);
int close(const struct driver *driver);
int read(const struct driver *driver, char *buffer, int len);
int write(const struct driver *driver, const char *buffer, int len);
int ioctl(const struct driver *driver, unsigned int cmd, const void *data);
#endif /* SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_DRIVER_H_ */

View File

@ -0,0 +1 @@
libkosmos-arm-stm32f4-discovery-0.0.5.a