working Communication, Database, Logger (w/out CDC)
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
set(KPI_Rover_Communication_Inc
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
set(KPI_Rover_Communication_Src
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/hwUSART3.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/crc16.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/drvUart.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/UARTTransport.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/ProtocolHandler.c
|
||||
)
|
||||
|
||||
add_library(KPI_Rover_Communication STATIC)
|
||||
|
||||
target_include_directories(KPI_Rover_Communication PRIVATE
|
||||
${KPI_Rover_Communication_Inc}
|
||||
)
|
||||
|
||||
target_sources(KPI_Rover_Communication PRIVATE
|
||||
${KPI_Rover_Communication_Src}
|
||||
)
|
||||
|
||||
target_link_libraries(KPI_Rover_Communication PRIVATE
|
||||
FREERTOS_H
|
||||
HAL_H
|
||||
KPI_Rover_Database_H
|
||||
)
|
||||
|
||||
add_library(KPI_Rover_Communication_H INTERFACE)
|
||||
target_include_directories(KPI_Rover_Communication_H INTERFACE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
@@ -0,0 +1,204 @@
|
||||
#include "UARTTransport.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include "cmsis_os2.h"
|
||||
|
||||
#include "ulDatabase.h"
|
||||
|
||||
#define SEND_BUFFER_SIZE 64
|
||||
#define RECV_BUFFER_SIZE 32
|
||||
|
||||
#define LEN(x) ( sizeof(x) / sizeof((x)[0]) )
|
||||
|
||||
static uint8_t sendBuffer[SEND_BUFFER_SIZE];
|
||||
static uint8_t recvBuffer[RECV_BUFFER_SIZE];
|
||||
static uint8_t recvSize;
|
||||
|
||||
static osThreadAttr_t ta = {
|
||||
.stack_size = 256 * 4
|
||||
};
|
||||
|
||||
static int32_t switch_endianness_int32(const uint8_t * const address)
|
||||
{
|
||||
return (int32_t) ((((uint32_t)address[0]) << 24) + (((uint32_t)address[1]) << 16) + (((uint32_t)address[2]) << 8) + ((uint32_t)address[3]));
|
||||
}
|
||||
|
||||
static void dispatch_not_a_command(void) {}
|
||||
|
||||
static void dispatch_get_api_version(void)
|
||||
{
|
||||
sendBuffer[0] = 0x01;
|
||||
sendBuffer[1] = 0x01;
|
||||
UARTTransport_send(sendBuffer, 2);
|
||||
}
|
||||
|
||||
static void dispatch_set_motor_speed(void)
|
||||
{
|
||||
sendBuffer[0] = 0x02;
|
||||
sendBuffer[1] = 0x00;
|
||||
|
||||
static int32_t target_value;
|
||||
|
||||
target_value = switch_endianness_int32(&(recvBuffer[2])); // big (network) to little (host)
|
||||
target_value /= 100;
|
||||
|
||||
switch (recvBuffer[1]) {
|
||||
case 0:
|
||||
ulDatabase_setInt32(MOTOR_FL_SETPOINT, target_value);
|
||||
break;
|
||||
case 1:
|
||||
ulDatabase_setInt32(MOTOR_RL_SETPOINT, target_value);
|
||||
break;
|
||||
case 2:
|
||||
ulDatabase_setInt32(MOTOR_FR_SETPOINT, target_value);
|
||||
break;
|
||||
case 3:
|
||||
ulDatabase_setInt32(MOTOR_RR_SETPOINT, target_value);
|
||||
break;
|
||||
default:
|
||||
sendBuffer[1] = 0x01;
|
||||
break;
|
||||
}
|
||||
|
||||
UARTTransport_send(sendBuffer, 2);
|
||||
}
|
||||
|
||||
static void dispatch_set_all_motors_speed(void)
|
||||
{
|
||||
static int32_t target_value_fl, target_value_rl, target_value_fr, target_value_rr;
|
||||
|
||||
target_value_fl = switch_endianness_int32(&(recvBuffer[1]));
|
||||
target_value_rl = switch_endianness_int32(&(recvBuffer[5]));
|
||||
target_value_fr = switch_endianness_int32(&(recvBuffer[9]));
|
||||
target_value_rr = switch_endianness_int32(&(recvBuffer[13]));
|
||||
|
||||
ulDatabase_setInt32(MOTOR_FL_SETPOINT, target_value_fl / 100);
|
||||
ulDatabase_setInt32(MOTOR_RL_SETPOINT, target_value_rl / 100);
|
||||
ulDatabase_setInt32(MOTOR_FR_SETPOINT, target_value_fr / 100);
|
||||
ulDatabase_setInt32(MOTOR_RR_SETPOINT, target_value_rr / 100);
|
||||
|
||||
sendBuffer[0] = 0x03;
|
||||
sendBuffer[1] = 0x00;
|
||||
UARTTransport_send(sendBuffer, 2);
|
||||
}
|
||||
|
||||
static void dispatch_get_encoder(void)
|
||||
{
|
||||
sendBuffer[0] = 0x04;
|
||||
|
||||
static int32_t source_value;
|
||||
|
||||
switch (recvBuffer[1]) {
|
||||
case 0:
|
||||
ulDatabase_getInt32(MOTOR_FL_RPM, &source_value);
|
||||
break;
|
||||
case 1:
|
||||
ulDatabase_getInt32(MOTOR_RL_RPM, &source_value);
|
||||
break;
|
||||
case 2:
|
||||
ulDatabase_getInt32(MOTOR_FR_RPM, &source_value);
|
||||
break;
|
||||
case 3:
|
||||
ulDatabase_getInt32(MOTOR_RR_RPM, &source_value);
|
||||
break;
|
||||
default:
|
||||
// failure is not an option in the protocol
|
||||
break;
|
||||
}
|
||||
|
||||
source_value = switch_endianness_int32((uint8_t *) &source_value); // little (host) to big (network)
|
||||
memcpy(&(sendBuffer[1]), &source_value, sizeof(source_value));
|
||||
|
||||
UARTTransport_send(sendBuffer, 5);
|
||||
}
|
||||
|
||||
static void dispatch_get_all_encoders(void)
|
||||
{
|
||||
static int32_t source_value_fl, source_value_rl, source_value_fr, source_value_rr;
|
||||
|
||||
ulDatabase_getInt32(MOTOR_FL_RPM, &source_value_fl);
|
||||
ulDatabase_getInt32(MOTOR_RL_RPM, &source_value_rl);
|
||||
ulDatabase_getInt32(MOTOR_FR_RPM, &source_value_fr);
|
||||
ulDatabase_getInt32(MOTOR_RR_RPM, &source_value_rr);
|
||||
|
||||
source_value_fl = switch_endianness_int32((uint8_t *) &source_value_fl);
|
||||
source_value_rl = switch_endianness_int32((uint8_t *) &source_value_rl);
|
||||
source_value_fr = switch_endianness_int32((uint8_t *) &source_value_fr);
|
||||
source_value_rr = switch_endianness_int32((uint8_t *) &source_value_rr);
|
||||
|
||||
sendBuffer[0] = 0x05;
|
||||
memcpy(&(sendBuffer[1]), &source_value_fl, sizeof(source_value_fl));
|
||||
memcpy(&(sendBuffer[5]), &source_value_rl, sizeof(source_value_rl));
|
||||
memcpy(&(sendBuffer[9]), &source_value_fr, sizeof(source_value_fr));
|
||||
memcpy(&(sendBuffer[13]), &source_value_rr, sizeof(source_value_rr));
|
||||
|
||||
UARTTransport_send(sendBuffer, 17);
|
||||
}
|
||||
|
||||
static void dispatch_get_imu(void)
|
||||
{
|
||||
sendBuffer[0] = 0x06;
|
||||
|
||||
ulDatabase_getFloat(IMU_ACCEL_X, (float *) (sendBuffer + 1));
|
||||
ulDatabase_getFloat(IMU_ACCEL_Y, (float *) (sendBuffer + 5));
|
||||
ulDatabase_getFloat(IMU_ACCEL_Z, (float *) (sendBuffer + 9));
|
||||
|
||||
ulDatabase_getFloat(IMU_GYRO_X, (float *) (sendBuffer + 13));
|
||||
ulDatabase_getFloat(IMU_GYRO_Y, (float *) (sendBuffer + 17));
|
||||
ulDatabase_getFloat(IMU_GYRO_Z, (float *) (sendBuffer + 21));
|
||||
|
||||
ulDatabase_getFloat(IMU_MAG_X, (float *) (sendBuffer + 25));
|
||||
ulDatabase_getFloat(IMU_MAG_Y, (float *) (sendBuffer + 29));
|
||||
ulDatabase_getFloat(IMU_MAG_Z, (float *) (sendBuffer + 33));
|
||||
|
||||
ulDatabase_getFloat(IMU_QUAT_X, (float *) (sendBuffer + 37));
|
||||
ulDatabase_getFloat(IMU_QUAT_X, (float *) (sendBuffer + 41));
|
||||
ulDatabase_getFloat(IMU_QUAT_Y, (float *) (sendBuffer + 45));
|
||||
ulDatabase_getFloat(IMU_QUAT_Z, (float *) (sendBuffer + 49));
|
||||
|
||||
UARTTransport_send(sendBuffer, 53);
|
||||
}
|
||||
|
||||
static void ProtocolHandler_emergencyStop(void)
|
||||
{
|
||||
ulDatabase_setInt32(MOTOR_FL_SETPOINT, 0);
|
||||
ulDatabase_setInt32(MOTOR_RL_SETPOINT, 0);
|
||||
ulDatabase_setInt32(MOTOR_FR_SETPOINT, 0);
|
||||
ulDatabase_setInt32(MOTOR_RR_SETPOINT, 0);
|
||||
}
|
||||
|
||||
static const void (*dispatch_table[])(void) = {
|
||||
(const void (*)(void)) dispatch_not_a_command,
|
||||
(const void (*)(void)) dispatch_get_api_version,
|
||||
(const void (*)(void)) dispatch_set_motor_speed,
|
||||
(const void (*)(void)) dispatch_set_all_motors_speed,
|
||||
(const void (*)(void)) dispatch_get_encoder,
|
||||
(const void (*)(void)) dispatch_get_all_encoders,
|
||||
(const void (*)(void)) dispatch_get_imu,
|
||||
};
|
||||
|
||||
void ProtocolHandler_processTask(void *d)
|
||||
{
|
||||
(void) d;
|
||||
|
||||
UARTTransport_init();
|
||||
|
||||
for ( ; ; ) {
|
||||
if (!UARTTransport_receive(recvBuffer, &recvSize))
|
||||
{
|
||||
ProtocolHandler_emergencyStop();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (recvBuffer[0] >= LEN(dispatch_table))
|
||||
continue;
|
||||
|
||||
dispatch_table[recvBuffer[0]]();
|
||||
}
|
||||
}
|
||||
|
||||
void ProtocolHandler_init(void)
|
||||
{
|
||||
(void) osThreadNew(ProtocolHandler_processTask, NULL, &ta);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef __PROTOCOL_HANDLER_H
|
||||
#define __PROTOCOL_HANDLER_H
|
||||
|
||||
|
||||
void ProtocolHandler_init(void);
|
||||
|
||||
|
||||
#endif /* __PROTOCOL_HANDLER_H */
|
||||
@@ -0,0 +1,113 @@
|
||||
#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_set_on_rx_cplt(UARTTransport_onRxCplt);
|
||||
(void) drvUart_set_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);
|
||||
}
|
||||
|
||||
bool UARTTransport_receive(uint8_t * const buf, uint8_t * const size)
|
||||
{
|
||||
static uint8_t recvBuffer[UART_TRANSPORT_RECV_BUFFER_SIZE];
|
||||
|
||||
//for ( ; osMessageQueueGet(recvQ, recvBuffer, NULL, 50) != osOK; drvUart_restart_receiver());
|
||||
|
||||
if (osMessageQueueGet(recvQ, recvBuffer, NULL, 50) != osOK)
|
||||
{
|
||||
drvUart_restart_receiver();
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
*size = recvBuffer[0] - 1;
|
||||
memcpy(buf, recvBuffer + 1, *size);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
static osStatus_t r;
|
||||
|
||||
for ( ; ; )
|
||||
{
|
||||
r = osMessageQueueGet(recvQ, recvBuffer, NULL, 100);
|
||||
|
||||
if (r == osOK)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
drvUart_restart_receiver();
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
*size = recvBuffer[0] - 1;
|
||||
memcpy(buf, recvBuffer + 1, *size);
|
||||
|
||||
return true;
|
||||
*/
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef __UART_TRANSPORT_H
|
||||
#define __UART_TRANSPORT_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define UART_TRANSPORT_RECV_BUFFER_SIZE 32
|
||||
#define UART_TRANSPORT_SEND_BUFFER_SIZE 64
|
||||
|
||||
void UARTTransport_init(void);
|
||||
bool 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 */
|
||||
@@ -0,0 +1,27 @@
|
||||
#include "crc16.h"
|
||||
|
||||
uint16_t crc16(uint8_t const *data, size_t size)
|
||||
{
|
||||
uint16_t crc = 0xFFFF;
|
||||
char i = 0;
|
||||
|
||||
while(size--)
|
||||
{
|
||||
crc ^= (*data++);
|
||||
|
||||
for(i = 0; i < 8; i++)
|
||||
{
|
||||
if( crc & 1 )
|
||||
{
|
||||
crc >>= 1;
|
||||
crc ^= 0xA001;
|
||||
}
|
||||
else
|
||||
{
|
||||
crc >>= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef COMMUNICATION_CRC16_H_
|
||||
#define COMMUNICATION_CRC16_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
uint16_t crc16(uint8_t const *data, size_t size);
|
||||
|
||||
#endif /* COMMUNICATION_CRC16_H_ */
|
||||
@@ -0,0 +1,202 @@
|
||||
#include "main.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "hwUSART3.h"
|
||||
#include "drvUart.h"
|
||||
|
||||
#include "crc16.h"
|
||||
|
||||
extern UART_HandleTypeDef huart3;
|
||||
|
||||
static uint8_t *activeReceiveBuffer;
|
||||
static uint8_t *sideReceiveBuffer;
|
||||
|
||||
static uint8_t receiveBuffer1[DRV_UART_RECEIVE_BUFFER_SIZE];
|
||||
static uint8_t receiveBuffer2[DRV_UART_RECEIVE_BUFFER_SIZE];
|
||||
static uint8_t transmitBuffer[DRV_UART_TRANSMIT_BUFFER_SIZE];
|
||||
|
||||
static void (*on_rx_cplt)(const uint8_t * const buffer);
|
||||
static void (*on_tx_cplt)(void);
|
||||
|
||||
#if 0
|
||||
extern DMA_HandleTypeDef hdma_usart3_rx;
|
||||
extern DMA_HandleTypeDef hdma_usart3_tx;
|
||||
|
||||
static void drvUart_configureDMA(void)
|
||||
{
|
||||
/* DMA controller clock enable */
|
||||
__HAL_RCC_DMA1_CLK_ENABLE();
|
||||
|
||||
/* DMA interrupt init */
|
||||
/* DMA1_Stream1_IRQn interrupt configuration */
|
||||
HAL_NVIC_SetPriority(DMA1_Stream1_IRQn, 10, 0);
|
||||
HAL_NVIC_EnableIRQ(DMA1_Stream1_IRQn);
|
||||
/* DMA1_Stream3_IRQn interrupt configuration */
|
||||
HAL_NVIC_SetPriority(DMA1_Stream3_IRQn, 10, 0);
|
||||
HAL_NVIC_EnableIRQ(DMA1_Stream3_IRQn);
|
||||
|
||||
/* USART3 DMA Init */
|
||||
/* USART3_RX Init */
|
||||
hdma_usart3_rx.Instance = DMA1_Stream1;
|
||||
hdma_usart3_rx.Init.Channel = DMA_CHANNEL_4;
|
||||
hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
||||
hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
|
||||
hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;
|
||||
hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
||||
hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
||||
hdma_usart3_rx.Init.Mode = DMA_NORMAL;
|
||||
hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW;
|
||||
hdma_usart3_rx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
|
||||
hdma_usart3_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
|
||||
hdma_usart3_rx.Init.MemBurst = DMA_MBURST_SINGLE;
|
||||
hdma_usart3_rx.Init.PeriphBurst = DMA_PBURST_SINGLE;
|
||||
if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
__HAL_LINKDMA(&huart3, hdmarx, hdma_usart3_rx);
|
||||
|
||||
/* USART3_TX Init */
|
||||
hdma_usart3_tx.Instance = DMA1_Stream3;
|
||||
hdma_usart3_tx.Init.Channel = DMA_CHANNEL_4;
|
||||
hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
||||
hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
|
||||
hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE;
|
||||
hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
||||
hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
||||
hdma_usart3_tx.Init.Mode = DMA_NORMAL;
|
||||
hdma_usart3_tx.Init.Priority = DMA_PRIORITY_LOW;
|
||||
hdma_usart3_tx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
|
||||
hdma_usart3_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
|
||||
hdma_usart3_tx.Init.MemBurst = DMA_MBURST_SINGLE;
|
||||
hdma_usart3_tx.Init.PeriphBurst = DMA_PBURST_SINGLE;
|
||||
if (HAL_DMA_Init(&hdma_usart3_tx) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
__HAL_LINKDMA(&huart3, hdmatx, hdma_usart3_tx);
|
||||
|
||||
/* USART3 interrupt Init */
|
||||
HAL_NVIC_SetPriority(USART3_IRQn, 10, 0);
|
||||
HAL_NVIC_EnableIRQ(USART3_IRQn);
|
||||
}
|
||||
#endif
|
||||
|
||||
bool drvUart_start(void)
|
||||
{
|
||||
activeReceiveBuffer = receiveBuffer1;
|
||||
sideReceiveBuffer = receiveBuffer2;
|
||||
|
||||
transmitBuffer[0] = 0xAA;
|
||||
|
||||
//drvUart_configureDMA();
|
||||
HW_USART3_Init();
|
||||
|
||||
HAL_UARTEx_ReceiveToIdle_DMA(&huart3, activeReceiveBuffer, DRV_UART_RECEIVE_BUFFER_SIZE);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool drvUart_set_on_rx_cplt(void (*f)(const uint8_t * const buffer))
|
||||
{
|
||||
on_rx_cplt = f;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool drvUart_set_on_tx_cplt(void (*f)(void))
|
||||
{
|
||||
on_tx_cplt = f;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool drvUart_send(uint8_t * const buf)
|
||||
{
|
||||
memcpy(transmitBuffer + 1, buf, buf[0]);
|
||||
transmitBuffer[1] += 2; // add size of CRC16
|
||||
|
||||
uint16_t crc = crc16(transmitBuffer + 1, buf[0]);
|
||||
|
||||
transmitBuffer[1 + buf[0]] = (uint8_t) crc;
|
||||
transmitBuffer[1 + buf[0] + 1] = (uint8_t) (crc >> 8);
|
||||
|
||||
HAL_UART_Transmit_DMA(&huart3, transmitBuffer, buf[0] + 3);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void drvUart_restart_receiver(void)
|
||||
{
|
||||
if (huart3.RxState != HAL_UART_STATE_BUSY_RX)
|
||||
{
|
||||
// at this point callback has swapped the buffer pointers
|
||||
(void) HAL_UARTEx_ReceiveToIdle_DMA(&huart3, activeReceiveBuffer, DRV_UART_RECEIVE_BUFFER_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
|
||||
{
|
||||
// first, relaunch hardware DMA reception on the other buffer
|
||||
(void) HAL_UARTEx_ReceiveToIdle_DMA(&huart3, sideReceiveBuffer, DRV_UART_RECEIVE_BUFFER_SIZE);
|
||||
|
||||
// then update local references
|
||||
{
|
||||
// restrict symbol visibility, but use static allocation for performance
|
||||
static uint8_t *tmp;
|
||||
|
||||
tmp = sideReceiveBuffer;
|
||||
sideReceiveBuffer = activeReceiveBuffer;
|
||||
activeReceiveBuffer = tmp;
|
||||
}
|
||||
|
||||
if (on_rx_cplt == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// then process every received packet
|
||||
{
|
||||
static uint16_t offset;
|
||||
|
||||
for (offset = 0; offset < Size; offset += 3 + sideReceiveBuffer[1 + offset]) {
|
||||
// verify packet is not too short (1 byte for size, 1+ byte(s) for command and 2 bytes for CRC16)
|
||||
if (sideReceiveBuffer[1 + offset] < 4)
|
||||
{
|
||||
// drop ALL remaining packets on failure
|
||||
return;
|
||||
}
|
||||
|
||||
// verify packet length does not overflow received frame size
|
||||
if (1 + offset + sideReceiveBuffer[1 + offset] > Size)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// verify packet is not corrupted
|
||||
if (crc16(sideReceiveBuffer + 1 + offset, sideReceiveBuffer[1 + offset]))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// exclude size of CRC16
|
||||
sideReceiveBuffer[1 + offset] -= 2;
|
||||
|
||||
// then call the callback (stripping 0xAA from the data)
|
||||
on_rx_cplt(sideReceiveBuffer + 1 + offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
|
||||
{
|
||||
if (on_tx_cplt == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
on_tx_cplt();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef __DRV_UART_H
|
||||
#define __DRV_UART_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#define DRV_UART_RECEIVE_BUFFER_SIZE 256
|
||||
#define DRV_UART_TRANSMIT_BUFFER_SIZE 256
|
||||
|
||||
bool drvUart_start(void);
|
||||
bool drvUart_set_on_rx_cplt(void (*f)(const uint8_t * const buffer));
|
||||
bool drvUart_set_on_tx_cplt(void (*f)(void));
|
||||
bool drvUart_send(uint8_t *buf);
|
||||
void drvUart_restart_receiver(void);
|
||||
|
||||
|
||||
#endif /* __DRV_UART_H */
|
||||
@@ -0,0 +1,185 @@
|
||||
#include "main.h"
|
||||
#include "hwUSART3.h"
|
||||
|
||||
extern UART_HandleTypeDef huart3;
|
||||
extern DMA_HandleTypeDef hdma_usart3_rx;
|
||||
extern DMA_HandleTypeDef hdma_usart3_tx;
|
||||
|
||||
void HW_USART3_Init(void)
|
||||
{
|
||||
__HAL_RCC_DMA1_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_USART3_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
|
||||
huart3.Instance = USART3;
|
||||
huart3.Init.BaudRate = 115200;
|
||||
huart3.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
huart3.Init.StopBits = UART_STOPBITS_1;
|
||||
huart3.Init.Parity = UART_PARITY_NONE;
|
||||
huart3.Init.Mode = UART_MODE_TX_RX;
|
||||
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
|
||||
if (HAL_UART_Init(&huart3) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
#if 0
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_9;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_10;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
hdma_usart3_rx.Instance = DMA1_Stream1;
|
||||
hdma_usart3_rx.Init.Channel = DMA_CHANNEL_4;
|
||||
hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
||||
hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
|
||||
hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;
|
||||
hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
||||
hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
||||
hdma_usart3_rx.Init.Mode = DMA_NORMAL;
|
||||
hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW;
|
||||
hdma_usart3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
||||
if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
__HAL_LINKDMA(&huart3, hdmarx, hdma_usart3_rx);
|
||||
|
||||
hdma_usart3_tx.Instance = DMA1_Stream3;
|
||||
hdma_usart3_tx.Init.Channel = DMA_CHANNEL_4;
|
||||
hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
||||
hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
|
||||
hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE;
|
||||
hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
||||
hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
||||
hdma_usart3_tx.Init.Mode = DMA_NORMAL;
|
||||
hdma_usart3_tx.Init.Priority = DMA_PRIORITY_LOW;
|
||||
hdma_usart3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
||||
if (HAL_DMA_Init(&hdma_usart3_tx) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
__HAL_LINKDMA(&huart3, hdmatx, hdma_usart3_tx);
|
||||
#endif
|
||||
|
||||
HAL_NVIC_SetPriority(DMA1_Stream1_IRQn, 10, 0);
|
||||
HAL_NVIC_EnableIRQ(DMA1_Stream1_IRQn);
|
||||
|
||||
HAL_NVIC_SetPriority(DMA1_Stream3_IRQn, 10, 0);
|
||||
HAL_NVIC_EnableIRQ(DMA1_Stream3_IRQn);
|
||||
|
||||
HAL_NVIC_SetPriority(USART3_IRQn, 10, 0);
|
||||
HAL_NVIC_EnableIRQ(USART3_IRQn);
|
||||
}
|
||||
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
if(uartHandle->Instance==USART2)
|
||||
{
|
||||
__HAL_RCC_USART2_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
}
|
||||
else if(uartHandle->Instance==USART3)
|
||||
{
|
||||
__HAL_RCC_USART3_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_9;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_10;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
hdma_usart3_rx.Instance = DMA1_Stream1;
|
||||
hdma_usart3_rx.Init.Channel = DMA_CHANNEL_4;
|
||||
hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
||||
hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
|
||||
hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;
|
||||
hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
||||
hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
||||
hdma_usart3_rx.Init.Mode = DMA_NORMAL;
|
||||
hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW;
|
||||
hdma_usart3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
||||
if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
__HAL_LINKDMA(uartHandle,hdmarx,hdma_usart3_rx);
|
||||
|
||||
hdma_usart3_tx.Instance = DMA1_Stream3;
|
||||
hdma_usart3_tx.Init.Channel = DMA_CHANNEL_4;
|
||||
hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
||||
hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
|
||||
hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE;
|
||||
hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
||||
hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
||||
hdma_usart3_tx.Init.Mode = DMA_NORMAL;
|
||||
hdma_usart3_tx.Init.Priority = DMA_PRIORITY_LOW;
|
||||
hdma_usart3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
||||
if (HAL_DMA_Init(&hdma_usart3_tx) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
__HAL_LINKDMA(uartHandle,hdmatx,hdma_usart3_tx);
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
|
||||
{
|
||||
|
||||
if(uartHandle->Instance==USART2)
|
||||
{
|
||||
__HAL_RCC_USART2_CLK_DISABLE();
|
||||
|
||||
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_5|GPIO_PIN_6);
|
||||
}
|
||||
else if(uartHandle->Instance==USART3)
|
||||
{
|
||||
__HAL_RCC_USART3_CLK_DISABLE();
|
||||
|
||||
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_9);
|
||||
|
||||
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10);
|
||||
|
||||
HAL_DMA_DeInit(uartHandle->hdmarx);
|
||||
HAL_DMA_DeInit(uartHandle->hdmatx);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef __HW_USART3_H
|
||||
#define __HW_USART3_H
|
||||
|
||||
void HW_USART3_Init(void);
|
||||
|
||||
#endif /* __HW_USART3_H */
|
||||
Reference in New Issue
Block a user