Commit b788050b authored by Alexey Hohlov's avatar Alexey Hohlov

Отправка сообщений в CAN шину через очередь сообщений.

parent a8191799
......@@ -7,7 +7,8 @@
static uint32_t HAL_RCC_CAN1_CLK_ENABLED = 0;
static struct can_packet_t messages[MAX_MESSAGES];
static struct can_packet_t rx_messages[MAX_MESSAGES];
static struct can_packet_t tx_messages[MAX_MESSAGES];
static CAN_TxHeaderTypeDef TxHeader[2];
static uint32_t TxMailbox;
......@@ -15,6 +16,9 @@ static uint32_t TxMailbox;
static uint32_t rx_head = 0u;
static uint32_t rx_tail = 0u;
static uint32_t tx_head = 0u;
static uint32_t tx_tail = 0u;
static inline uint32_t
next_idx(uint32_t idx)
{
......@@ -27,7 +31,7 @@ can_read_msg(struct can_packet_t **msg)
bool result = false;
if (rx_head != rx_tail) {
*msg = &messages[rx_head];
*msg = &rx_messages[rx_head];
rx_head = next_idx(rx_head);
result = true;
......@@ -36,32 +40,64 @@ can_read_msg(struct can_packet_t **msg)
return result;
}
void
can_send(struct can_packet_t *msg)
static bool
can_ready_tx(CAN_HandleTypeDef *hcan)
{
CAN_HandleTypeDef *can = &hcan1;
uint32_t tsr = READ_REG(hcan->Instance->TSR);
bool result = false;
if (msg->bus == 1) {
can = &hcan2;
if (((tsr & CAN_TSR_TME0) != 0U) || ((tsr & CAN_TSR_TME1) != 0U) ||
((tsr & CAN_TSR_TME2) != 0U)) {
result = true;
}
union {
can_msg_t *msg;
uint32_t *u32;
} id;
id.msg = &msg->msg_id;
return result;
}
void
can_work(void)
{
if (tx_head != tx_tail) {
CAN_HandleTypeDef *can = &hcan1;
struct can_packet_t *msg = &tx_messages[tx_tail % MAX_MESSAGES];
// TxHeader[msg->bus].StdId = msg->msg_id.dest_id;
TxHeader[msg->bus].ExtId = *id.u32;
TxHeader[msg->bus].DLC = msg->len;
if (msg->bus == 1) {
can = &hcan2;
}
if (HAL_CAN_AddTxMessage(can, &TxHeader[msg->bus], msg->data,
&TxMailbox) != HAL_OK) {
/* Transmission request Error */
Error_Handler();
if (can_ready_tx(can)) {
union {
can_msg_t *msg;
uint32_t *u32;
} id;
id.msg = &msg->msg_id;
// TxHeader[msg->bus].StdId = msg->msg_id.dest_id;
TxHeader[msg->bus].ExtId = *id.u32;
TxHeader[msg->bus].DLC = msg->len;
if (HAL_CAN_AddTxMessage(can, &TxHeader[msg->bus],
msg->data,
&TxMailbox) != HAL_OK) {
/* Transmission request Error */
Error_Handler();
}
tx_tail++;
}
}
}
void
can_send(struct can_packet_t *msg)
{
struct can_packet_t *add_msg = &tx_messages[tx_head % MAX_MESSAGES];
memcpy(add_msg, msg, sizeof(struct can_packet_t));
tx_head++;
}
/* CAN1 init function */
static void
MX_CAN1_Init(void)
......@@ -285,8 +321,8 @@ HAL_CAN_MspDeInit(CAN_HandleTypeDef *hcan)
}
/**
* @brief This function handles CAN1 TX interrupt.
*/
* @brief This function handles CAN1 TX interrupt.
*/
void
CAN1_TX_IRQHandler(void)
{
......@@ -294,8 +330,8 @@ CAN1_TX_IRQHandler(void)
}
/**
* @brief This function handles CAN1 RX0 interrupt.
*/
* @brief This function handles CAN1 RX0 interrupt.
*/
void
CAN1_RX0_IRQHandler(void)
{
......@@ -303,8 +339,8 @@ CAN1_RX0_IRQHandler(void)
}
/**
* @brief This function handles CAN1 RX1 interrupt.
*/
* @brief This function handles CAN1 RX1 interrupt.
*/
void
CAN1_RX1_IRQHandler(void)
{
......@@ -312,8 +348,8 @@ CAN1_RX1_IRQHandler(void)
}
/**
* @brief This function handles CAN1 SCE interrupt.
*/
* @brief This function handles CAN1 SCE interrupt.
*/
void
CAN1_SCE_IRQHandler(void)
{
......@@ -321,8 +357,8 @@ CAN1_SCE_IRQHandler(void)
}
/**
* @brief This function handles CAN2 TX interrupt.
*/
* @brief This function handles CAN2 TX interrupt.
*/
void
CAN2_TX_IRQHandler(void)
{
......@@ -330,8 +366,8 @@ CAN2_TX_IRQHandler(void)
}
/**
* @brief This function handles CAN2 RX0 interrupt.
*/
* @brief This function handles CAN2 RX0 interrupt.
*/
void
CAN2_RX0_IRQHandler(void)
{
......@@ -339,8 +375,8 @@ CAN2_RX0_IRQHandler(void)
}
/**
* @brief This function handles CAN2 RX1 interrupt.
*/
* @brief This function handles CAN2 RX1 interrupt.
*/
void
CAN2_RX1_IRQHandler(void)
{
......@@ -348,8 +384,8 @@ CAN2_RX1_IRQHandler(void)
}
/**
* @brief This function handles CAN2 SCE interrupt.
*/
* @brief This function handles CAN2 SCE interrupt.
*/
void
CAN2_SCE_IRQHandler(void)
{
......@@ -378,17 +414,17 @@ HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
/* Get RX message */
res = HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader,
messages[rx_tail].data);
rx_messages[rx_tail].data);
if (res != HAL_OK) {
/* Reception Error */
Error_Handler();
}
messages[rx_tail].magic = CAN_MSG_MAGIC;
rx_messages[rx_tail].magic = CAN_MSG_MAGIC;
if (hcan == &hcan1) {
messages[rx_tail].bus = 0u;
rx_messages[rx_tail].bus = 0u;
} else {
messages[rx_tail].bus = 1u;
rx_messages[rx_tail].bus = 1u;
}
union {
......@@ -398,9 +434,9 @@ HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
id.u32 = RxHeader.ExtId;
messages[rx_tail].msg_id = id.msg;
messages[rx_tail].len = (uint8_t)RxHeader.DLC;
make_crc(&messages[rx_tail]);
rx_messages[rx_tail].msg_id = id.msg;
rx_messages[rx_tail].len = (uint8_t)RxHeader.DLC;
make_crc(&rx_messages[rx_tail]);
rx_tail = next_idx(rx_tail);
}
......
......@@ -15,13 +15,16 @@
#define CAN_MSG_MAGIC (0xAAu)
/**
* @brief Структура пакета с сообщением
*/
struct can_packet_t {
uint8_t magic;
uint8_t bus;
can_msg_t msg_id;
uint8_t data[8];
uint8_t len;
uint8_t crc;
uint8_t magic; /**< @brief Начало пакета */
uint8_t bus; /**< @brief Номер шины, куда отправлять сообщение */
can_msg_t msg_id; /**< @brief Дескриптор команды */
uint8_t data[8]; /**< @brief Данные команды */
uint8_t len; /**< @brief Длина данных команды */
uint8_t crc; /**< @brief Контрольная сумма пакета */
};
/**
......@@ -30,11 +33,20 @@ struct can_packet_t {
void can_init(void);
/**
* @brief Отослать сообщение CAN
* @brief Добавить CAN сообщение в очередь отправки
* @param msg [in] - сообщение
*/
void can_send(struct can_packet_t *msg);
/**
* @brief Получить сообщение CAN из очереди
* @param msg [out] - указатель на полученное сообщение
* @retval true если прочитано сообщение
* @retval false если очередь входящих сообщений пуста
*/
bool can_read_msg(struct can_packet_t **msg);
/**
* @brief Функция работы CAN
*/
void can_work(void);
......@@ -16,6 +16,8 @@ extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
static uint8_t g_status = 0;
static uint8_t r_status = 0;
static uint32_t do_led = 0;
static void
loop(void)
{
......@@ -54,6 +56,34 @@ loop(void)
can_send(&msg2);
}
if (do_led) {
static uint32_t counter = 0u;
counter++;
if (counter >= 200u) {
if (g_status) {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1,
GPIO_PIN_SET);
g_status = 0;
} else {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1,
GPIO_PIN_RESET);
g_status = 1;
}
counter = 0u;
}
if (r_status > 0) {
r_status--;
} else {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
}
do_led = 0U;
}
can_work();
}
int
......@@ -98,25 +128,7 @@ SysTick_Handler(void)
HAL_IncTick();
HAL_SYSTICK_IRQHandler();
static uint32_t counter = 0u;
counter++;
if (counter >= 200u) {
if (g_status) {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
g_status = 0;
} else {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);
g_status = 1;
}
counter = 0u;
}
if (r_status > 0) {
r_status--;
} else {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
}
do_led = 1;
}
/**
......
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