Commit 25f80168 authored by Benjamin Vedder's avatar Benjamin Vedder

Added another interface file to make the code easier to follow

parent 6d406af0
......@@ -120,7 +120,8 @@ CSRC = $(STARTUPSRC) \
buffer.c \
packet.c \
crc.c \
comm_uart.c
comm_uart.c \
bldc_interface_uart.c
# C++ sources that can be compiled in ARM or THUMB mode depending on the global
# setting.
......
/*
Copyright 2015 Benjamin Vedder benjamin@vedder.se
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* bldc_interface_uart.c
*
* Created on: 9 okt 2015
* Author: benjamin
*/
#include "bldc_interface_uart.h"
#include "ch.h"
#include "hal.h"
#include "bldc_interface.h"
// Settings
#define PACKET_HANDLER 0
// Private functions
static void process_packet(unsigned char *data, unsigned int len);
static void send_packet_bldc_interface(unsigned char *data, unsigned int len);
/**
* Initialize the UART BLDC interface and provide a function to be used for
* sending packets.
*
* @param func
* Function provided for sending packets.
*/
void bldc_interface_uart_init(void(*func)(unsigned char *data, unsigned int len)) {
// Initialize packet handler
packet_init(func, process_packet, PACKET_HANDLER);
// Initialize the bldc interface and provide a send function
bldc_interface_init(send_packet_bldc_interface);
}
/**
* Process one byte received on the UART. Once a full packet is received the
* corresponding callback will be called by bldc_interface.
*
* @param b
* The byte received on the UART to process.
*/
void bldc_interface_uart_process_byte(unsigned char b) {
packet_process_byte(b, PACKET_HANDLER);
}
/**
* Call this function at around 1 khz to reset the state of the packet
* interface after a timeout in case data is lost.
*/
void bldc_interface_uart_run_timer(void) {
packet_timerfunc();
}
/**
* Callback for the packet handled for when a whole packet is received,
* assembled and checked.
*
* @param data
* Data array pointer
* @param len
* Data array length
*/
static void process_packet(unsigned char *data, unsigned int len) {
// Let bldc_interface process the packet.
bldc_interface_process_packet(data, len);
}
/**
* Callback that bldc_interface uses to send packets.
*
* @param data
* Data array pointer
* @param len
* Data array length
*/
static void send_packet_bldc_interface(unsigned char *data, unsigned int len) {
// Pass the packet to the packet handler to add checksum, length, start and stop bytes.
packet_send_packet(data, len, PACKET_HANDLER);
}
/*
Copyright 2015 Benjamin Vedder benjamin@vedder.se
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* bldc_interface_uart.h
*
* Created on: 9 okt 2015
* Author: benjamin
*/
#ifndef BLDC_INTERFACE_UART_H_
#define BLDC_INTERFACE_UART_H_
// Includes
#include "packet.h" // For the MAX_PACKET_LEN define
// Functions
void bldc_interface_uart_init(void(*func)(unsigned char *data, unsigned int len));
void bldc_interface_uart_process_byte(unsigned char b);
void bldc_interface_uart_run_timer(void);
#endif /* BLDC_INTERFACE_UART_H_ */
......@@ -25,15 +25,11 @@
#include "comm_uart.h"
#include "ch.h"
#include "hal.h"
#include "packet.h"
#include "bldc_interface.h"
#include "bldc_interface_uart.h"
#include <string.h>
// Settings
#define PACKET_HANDLER 0
#define SERIAL_RX_BUFFER_SIZE 1024
#define UART_BAUDRATE 115200
#define UART_DEV UARTD3
#define UART_GPIO_AF 7
......@@ -41,13 +37,14 @@
#define UART_TX_PIN 10
#define UART_RX_PORT GPIOB
#define UART_RX_PIN 11
#define SERIAL_RX_BUFFER_SIZE 1024
// Private functions
static void process_packet(unsigned char *data, unsigned int len);
static void send_packet_bldc_interface(unsigned char *data, unsigned int len);
static void send_packet(unsigned char *data, unsigned int len);
// Threads
static THD_FUNCTION(timer_thread, arg);
static THD_WORKING_AREA(timer_thread_wa, 512);
static THD_FUNCTION(packet_process_thread, arg);
static THD_WORKING_AREA(packet_process_thread_wa, 4096);
static thread_t *process_tp;
......@@ -144,7 +141,7 @@ static THD_FUNCTION(packet_process_thread, arg) {
*/
while (serial_rx_read_pos != serial_rx_write_pos) {
packet_process_byte(serial_rx_buffer[serial_rx_read_pos++], PACKET_HANDLER);
bldc_interface_uart_process_byte(serial_rx_buffer[serial_rx_read_pos++]);
if (serial_rx_read_pos == SERIAL_RX_BUFFER_SIZE) {
serial_rx_read_pos = 0;
......@@ -153,20 +150,6 @@ static THD_FUNCTION(packet_process_thread, arg) {
}
}
/**
* Callback for the packet handled for when a whole packet is received,
* assembled and checked.
*
* @param data
* Data array pointer
* @param len
* Data array length
*/
static void process_packet(unsigned char *data, unsigned int len) {
// Let bldc_interface process the packet.
bldc_interface_process_packet(data, len);
}
/**
* Callback that the packet handler uses to send an assembled packet.
*
......@@ -195,22 +178,21 @@ static void send_packet(unsigned char *data, unsigned int len) {
}
/**
* Callback that bldc_interface uses to send packets.
*
* @param data
* Data array pointer
* @param len
* Data array length
* This thread is only for calling the timer function once
* per millisecond. Can also be implementer using interrupts
* if no RTOS is available.
*/
static void send_packet_bldc_interface(unsigned char *data, unsigned int len) {
// Pass the packet to the packet handler to add checksum, length, start and stop bytes.
packet_send_packet(data, len, PACKET_HANDLER);
static THD_FUNCTION(timer_thread, arg) {
(void)arg;
chRegSetThreadName("packet timer");
for(;;) {
bldc_interface_uart_run_timer();
chThdSleepMilliseconds(1);
}
}
void comm_uart_init(void) {
// Initialize packet handler
packet_init(send_packet, process_packet, PACKET_HANDLER);
// Initialize UART
uartStart(&UART_DEV, &uart_cfg);
palSetPadMode(UART_TX_PORT, UART_TX_PIN, PAL_MODE_ALTERNATE(UART_GPIO_AF) |
......@@ -221,9 +203,13 @@ void comm_uart_init(void) {
PAL_STM32_PUDR_PULLUP);
// Initialize the bldc interface and provide a send function
bldc_interface_init(send_packet_bldc_interface);
bldc_interface_uart_init(send_packet);
// Start processing thread
chThdCreateStatic(packet_process_thread_wa, sizeof(packet_process_thread_wa),
NORMALPRIO, packet_process_thread, NULL);
// Start timer thread
chThdCreateStatic(timer_thread_wa, sizeof(timer_thread_wa),
NORMALPRIO, timer_thread, NULL);
}
......@@ -30,7 +30,7 @@
// Settings
#define PACKET_RX_TIMEOUT 2
#define PACKET_HANDLERS 1
#define PACKET_MAX_PL_LEN 1024
#define PACKET_MAX_PL_LEN 512
// Functions
void packet_init(void (*s_func)(unsigned char *data, unsigned int len),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment