Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cproject
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.option.optimization.level.2032300559" name="Optimization level" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.option.optimization.level" useByScannerDiscovery="false"/>
</tool>
<tool id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.1428656477" name="MCU GCC Linker" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker">
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.option.script.227955233" name="Linker Script (-T)" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.option.script" value="${workspace_loc:/${ProjName}/STM32F407VGTX_FLASH.ld}" valueType="string"/>
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.option.script.227955233" name="Linker Script (-T)" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.option.script" value="${workspace_loc:/${ProjName}/STM32F407VGTX_FLASH_custom.ld}" valueType="string"/>
<inputType id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.input.296230319" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.input">
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
Expand Down
2 changes: 1 addition & 1 deletion Core/Inc/stm32f4xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
#define USE_HAL_SMBUS_REGISTER_CALLBACKS 0U /* SMBUS register callback disabled */
#define USE_HAL_SPI_REGISTER_CALLBACKS 0U /* SPI register callback disabled */
#define USE_HAL_TIM_REGISTER_CALLBACKS 0U /* TIM register callback disabled */
#define USE_HAL_UART_REGISTER_CALLBACKS 0U /* UART register callback disabled */
#define USE_HAL_UART_REGISTER_CALLBACKS 1U /* UART register callback enabled */
#define USE_HAL_USART_REGISTER_CALLBACKS 0U /* USART register callback disabled */
#define USE_HAL_WWDG_REGISTER_CALLBACKS 0U /* WWDG register callback disabled */

Expand Down
2 changes: 1 addition & 1 deletion Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ UART_HandleTypeDef huart3;
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
.name = "defaultTask",
.stack_size = 128 * 4,
.stack_size = 512 * 4,
.priority = (osPriority_t) osPriorityNormal,
};
/* USER CODE BEGIN PV */
Expand Down
35 changes: 34 additions & 1 deletion Core/Src/stm32f4xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
extern TIM_HandleTypeDef htim7;

/* USER CODE BEGIN EV */

extern UART_HandleTypeDef huart3;
extern DMA_HandleTypeDef hdma_usart3_rx;
extern DMA_HandleTypeDef hdma_usart3_tx;
/* USER CODE END EV */

/******************************************************************************/
Expand Down Expand Up @@ -189,5 +191,36 @@ void OTG_FS_IRQHandler(void)
}

/* USER CODE BEGIN 1 */
void USART3_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart3);
}

/**
* @brief This function handles DMA1 stream1 global interrupt.
*/
void DMA1_Stream1_IRQHandler(void)
{
/* USER CODE BEGIN DMA1_Stream1_IRQn 0 */

/* USER CODE END DMA1_Stream1_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_usart3_rx);
/* USER CODE BEGIN DMA1_Stream1_IRQn 1 */

/* USER CODE END DMA1_Stream1_IRQn 1 */
}

/**
* @brief This function handles DMA1 stream3 global interrupt.
*/
void DMA1_Stream3_IRQHandler(void)
{
/* USER CODE BEGIN DMA1_Stream3_IRQn 0 */

/* USER CODE END DMA1_Stream3_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_usart3_tx);
/* USER CODE BEGIN DMA1_Stream3_IRQn 1 */

/* USER CODE END DMA1_Stream3_IRQn 1 */
}
/* USER CODE END 1 */
104 changes: 104 additions & 0 deletions KPI_Rover/Communication/ProtocolHandler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "UARTTransport.h"

#include <string.h>
#include <stdint.h>
#include "cmsis_os2.h"

#define SEND_BUFFER_SIZE 64
#define RECV_BUFFER_SIZE 32

static uint8_t sendBuffer[SEND_BUFFER_SIZE];
static uint8_t recvBuffer[RECV_BUFFER_SIZE];
static uint8_t recvSize;

static void (*dispatch_table[256])(void);

static osThreadAttr_t ta = {
.stack_size = 64 * 4
};

static void dispatch_unsupported(void)
{
sendBuffer[0] = 0xFF;
UARTTransport_send(sendBuffer, 1);
}

static void dispatch_01(void)
{
sendBuffer[0] = 0x01;
sendBuffer[1] = 0x01;
UARTTransport_send(sendBuffer, 2);
}

static void dispatch_02(void)
{
sendBuffer[0] = 0x02;

switch (recvBuffer[1]) {
case 0:
sendBuffer[1] = 0x0;
break;
case 1:
sendBuffer[1] = 0x0;
break;
case 2:
sendBuffer[1] = 0x0;
break;
case 3:
sendBuffer[1] = 0x0;
break;
default:
sendBuffer[1] = 0x01;
break;
}

UARTTransport_send(sendBuffer, 2);
}

static void dispatch_03(void)
{
sendBuffer[0] = 0x03;
sendBuffer[1] = 0x00;
UARTTransport_send(sendBuffer, 2);
}

static void dispatch_05(void)
{
sendBuffer[0] = 0x05;
memset(sendBuffer + 1, 0xBB, 16);
UARTTransport_send(sendBuffer, 17);
}

static void dispatch_06(void)
{
sendBuffer[0] = 0x06;
memset(sendBuffer + 1, 0xCC, 52);
UARTTransport_send(sendBuffer, 53);
}

void ProtocolHandler_processTask(void *d)
{
(void) d;

UARTTransport_init();

for ( ; ; ) {
UARTTransport_receive(recvBuffer, &recvSize);
dispatch_table[recvBuffer[0]]();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add if to check range.

}
}

void ProtocolHandler_init(void)
{
for (uint32_t i = 0; i < 256; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this

dispatch_table[i] = dispatch_unsupported;
}

dispatch_table[1] = dispatch_01;
dispatch_table[2] = dispatch_02;
dispatch_table[3] = dispatch_03;
dispatch_table[5] = dispatch_05;
dispatch_table[6] = dispatch_06;

(void) osThreadNew(ProtocolHandler_processTask, NULL, &ta);
}
8 changes: 8 additions & 0 deletions KPI_Rover/Communication/ProtocolHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef __PROTOCOL_HANDLER_H
#define __PROTOCOL_HANDLER_H


void ProtocolHandler_init(void);


#endif /* __PROTOCOL_HANDLER_H */
78 changes: 78 additions & 0 deletions KPI_Rover/Communication/UARTTransport.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <string.h>

#include "UARTTransport.h"
#include "cmsis_os2.h"

#include "drvUart.h"

#define FLAG_TRANSMIT_FINISH 0x1

static osMessageQueueId_t sendQ;
static osMessageQueueId_t recvQ;

static osEventFlagsId_t ef;

static osThreadAttr_t ta = {
.stack_size = 64 * 4
};

void UARTTransport_sendTask(void *d)
{
(void) d;

static uint8_t sendMessageBuffer[UART_TRANSPORT_SEND_BUFFER_SIZE];

for ( ; ; ) {
if (osMessageQueueGet(sendQ, sendMessageBuffer, NULL, osWaitForever))
continue;

drvUart_send(sendMessageBuffer);

osEventFlagsWait(ef, FLAG_TRANSMIT_FINISH, 0, osWaitForever);
}
}

void UARTTransport_onRxCplt(const uint8_t * const buffer)
{
(void) osMessageQueuePut(recvQ, buffer, 0, 0);
}

void UARTTransport_onTxCplt(void)
{
(void) osEventFlagsSet(ef, FLAG_TRANSMIT_FINISH);
}

void UARTTransport_init(void)
{
(void) drvUart_on_rx_cplt(UARTTransport_onRxCplt);
(void) drvUart_on_tx_cplt(UARTTransport_onTxCplt);

(void) drvUart_start();

ef = osEventFlagsNew(NULL);

sendQ = osMessageQueueNew(16, UART_TRANSPORT_SEND_BUFFER_SIZE, NULL);
recvQ = osMessageQueueNew(16, UART_TRANSPORT_RECV_BUFFER_SIZE, NULL);

(void) osThreadNew(UARTTransport_sendTask, NULL, &ta);
}

void UARTTransport_receive(uint8_t * const buf, uint8_t * const size)
{
static uint8_t recvBuffer[UART_TRANSPORT_RECV_BUFFER_SIZE];

(void) osMessageQueueGet(recvQ, recvBuffer, NULL, osWaitForever);

*size = recvBuffer[0] - 1;
memcpy(buf, recvBuffer + 1, *size);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here also I prefer control size. What if recvBuffer[0] == 0?

}

void UARTTransport_send(const uint8_t * const buf, const uint8_t size)
{
static uint8_t sendEncodingBuffer[UART_TRANSPORT_SEND_BUFFER_SIZE];

sendEncodingBuffer[0] = size + 1;
memcpy(sendEncodingBuffer + 1, buf, size);

(void) osMessageQueuePut(sendQ, sendEncodingBuffer, 0, osWaitForever);
}
13 changes: 13 additions & 0 deletions KPI_Rover/Communication/UARTTransport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef __UART_TRANSPORT_H
#define __UART_TRANSPORT_H

#include <stdint.h>

#define UART_TRANSPORT_RECV_BUFFER_SIZE 32
#define UART_TRANSPORT_SEND_BUFFER_SIZE 64

void UARTTransport_init(void);
void UARTTransport_receive(uint8_t * const buf, uint8_t * const size);
void UARTTransport_send(const uint8_t * const buf, const uint8_t size);

#endif /* __UART_TRANSPORT_H */
60 changes: 60 additions & 0 deletions KPI_Rover/Communication/crc16.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "crc16.h"

static uint16_t crc_table[UINT8_MAX + 1];

uint16_t crc16( const unsigned char *buf, unsigned int len )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signature mismatch
crc16.h: uint16_t crc16(uint8_t const *data, size_t size);

{
uint16_t crc = 0xFFFF;
char i = 0;

while(len--)
{
crc ^= (*buf++);

for(i = 0; i < 8; i++)
{
if( crc & 1 )
{
crc >>= 1;
crc ^= 0xA001;
}
else
{
crc >>= 1;
}
}
}

return crc;
}

uint16_t crc16_tmp1(uint8_t const * data, size_t size)
{
uint16_t crc = 0xFFFF;
while (size--) {
/* XOR-in next input byte into MSB of crc, that's our new intermediate dividend */
uint8_t pos = (uint8_t)( (crc >> 8) ^ *data++); /* equal: ((crc ^ (b << 8)) >> 8) */
/* Shift out the MSB used for division per lookuptable and XOR with the remainder */
crc = (uint16_t)((crc << 8) ^ (uint16_t)(crc_table[pos]));
}
return crc;
}

void crc16_fillTable()
{
const uint16_t generator = 0x8005;
for (int dividend = 0; dividend < UINT8_MAX + 1; ++dividend) {
uint16_t current_byte = dividend << 8;

for (uint8_t bit = 0; bit < 8; ++bit) {
if ((current_byte & 0x8000) != 0) {
current_byte <<= 1;
current_byte ^= generator;
}
else {
current_byte <<= 1;
}
}
crc_table[dividend] = current_byte;
}
}
10 changes: 10 additions & 0 deletions KPI_Rover/Communication/crc16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef COMMUNICATION_CRC16_H_
#define COMMUNICATION_CRC16_H_

#include <stdint.h>
#include <stddef.h>

uint16_t crc16(uint8_t const *data, size_t size);
void crc16_fillTable(void);

#endif /* COMMUNICATION_CRC16_H_ */
Loading