Loading...
Searching...
No Matches
canpub.c
1/**************************************************************************************************
2 * @file uartcomm.c *
3 * @brief Implements the FreeRTOS tasks and Interrupt Service Routine (ISR) *
4 * responsible for managing uart communication. *
5 * *
6 * *
7 * @{ *
8 **************************************************************************************************/
9
10#include "AustralisConfig.h"
11#include "canpub.h"
12
13#include "stm32f439xx.h"
14
15#include "FreeRTOS.h"
16#include "event_groups.h"
17#include "message_buffer.h"
18#include "queue.h"
19
20#include "_topic.h"
21#include "can.h"
22#include "rcc.h"
23#include "gpiopin.h"
24
25// Create publication topic for UART data
26//
27// NOTE:
28// This topic is exposed for reader
29// comments in the public header.
30CREATE_TOPIC(can, 10, CAN_MSG_LENGTH)
31Topic *canTopic = (Topic *)&can;
32
33static TaskHandle_t vCanTransmitHandle;
34static TaskHandle_t vCanReceiveHandle;
35
36// LoRa transceiver device
37//
38// TODO:
39// Add deviceReady flag to driver API to indicate
40// when a device struct is initialised and populated
41static CAN_t *peripheral;
42void CAN_setPeripheral(CAN_t *peripheral_) {
43 peripheral = peripheral_;
44}
45
46void __attribute__((constructor)) init() {
47 RCC_START_PERIPHERAL(APB1, CAN1);
48 // CANGPIO_config();
49 // CAN_Peripheral_config();
50
51 // static CAN_t c;
52 // c = CAN_init(CAN1, NULL);
53 // CAN_setPeripheral(&c);
54}
55
56/* ============================================================================================== */
62void vCanTransmit(void *argument) {
63 const TickType_t blockTime = portMAX_DELAY;
64 CAN_Data txData;
65
66 vCanTransmitHandle = xTaskGetCurrentTaskHandle();
67
68 for (;;) {
69 // Don't operate unless transceiver is ready
70 if (peripheral == NULL)
71 continue;
72
73 // Wait to receive message to transmit
74 // BaseType_t result = WAIT_COMMENT(
75 // can.public.commentInbox, // Read from LoRa topic comment queue
76 // (void *)&txData, // Store data in binary array
77 // portMAX_DELAY // Block forever until comment is available
78 //);
79
80 // if (result == pdTRUE) {
81 // Transmit data if successfully retrieved from queue
82
83 TickType_t xLastWakeTime = xTaskGetTickCount();
84 vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(150));
85
86 txData.id = 0x603;
87 txData.length = 8;
88 txData.data[0] = 25;
89 txData.data[1] = 59;
90 CAN_transmit(peripheral, &txData);
91 //}
92 }
93}
94
95/* ============================================================================================== */
101void vCanReceive(void *argument) {
102 const TickType_t blockTime = portMAX_DELAY;
103 CAN_Data rxData;
104
105 vCanReceiveHandle = xTaskGetCurrentTaskHandle();
106
107 GPIOpin_t indicator = GPIOpin_init(GPIOA, GPIO_PIN1, NULL);
108
109 for (;;) {
110 // Don't operate unless transceiver is ready
111 if (peripheral == NULL)
112 continue;
113
114 // Read byte from UART Rx buffer
115 xTaskNotifyWait(0, 0, NULL, portMAX_DELAY);
116
117 indicator.toggle(&indicator);
118
119 CAN_receive(peripheral, &rxData);
120
121 peripheral->interface->IER |= CAN_IER_FMPIE0;
122
123 // Publish packet data to topic
124 // Topic_publish((PrivateTopic *)canTopic, (uint8_t *)&rxData);
125 }
126}
127
128/* ============================================================================================== */
134void pubCanInterrupt() {
135 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
136
137 // Exit if peripheral is not ready
138 if (peripheral == NULL)
139 goto CAN_NOT_READY;
140
141 xTaskNotifyFromISR(vCanReceiveHandle, 0, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
142 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
143
144 peripheral->interface->IER &= ~CAN_IER_FMPIE0;
145
146CAN_NOT_READY:
147 return;
148}
149
#define CREATE_TOPIC(topic, commentInboxSize, messageSize)
Macro to define and initialize a topic instance.
Definition _topic.h:60
void(* toggle)(struct GPIOpin *)
Definition gpiopin.h:157
GPIOpin_t GPIOpin_init(GPIO_TypeDef *, GPIO_Pin, GPIO_Config *)
Initialiser for a GPIO peripheral pin interface.
Definition gpiopin.c:28
@ GPIO_PIN1
Pin 1.
Definition gpiopin.h:51
Struct definition for a GPIO pin.
Definition gpiopin.h:151
Definition can.h:22
Definition can.h:28
Public representation of a Topic.
Definition topic.h:71