Skip to content

Commit b51c722

Browse files
committed
[VirtIO] Copy files from example project
1 parent 791cab4 commit b51c722

File tree

9 files changed

+1214
-0
lines changed

9 files changed

+1214
-0
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/**
2+
******************************************************************************
3+
* @file mbox_ipcc.c
4+
* @author MCD Application Team
5+
* @brief This file provides code for the configuration
6+
* of the mailbox_ipcc_if.c MiddleWare.
7+
******************************************************************************
8+
* @attention
9+
*
10+
* <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
11+
* All rights reserved.</center></h2>
12+
*
13+
* This software component is licensed by ST under BSD 3-Clause license,
14+
* the "License"; You may not use this file except in compliance with the
15+
* License. You may obtain a copy of the License at:
16+
* opensource.org/licenses/BSD-3-Clause
17+
*
18+
******************************************************************************
19+
*/
20+
#ifdef VIRTIOCON
21+
22+
/*
23+
* Channel direction and usage:
24+
*
25+
* ======== <-- new msg ---=============--------<------ =======
26+
* || || || CHANNEL 1 || || ||
27+
* || A7 || ------->-------=============--- buf free--> || M4 ||
28+
* || || || ||
29+
* ||master|| <-- buf free---=============--------<------ ||slave||
30+
* || || || CHANNEL 2 || || ||
31+
* ======== ------->-------=============----new msg --> =======
32+
*/
33+
34+
/* Includes ------------------------------------------------------------------*/
35+
#include "openamp/open_amp.h"
36+
#include "stm32_def.h"
37+
#include "openamp_conf.h"
38+
39+
/* Within 'USER CODE' section, code will be kept by default at each generation */
40+
/* USER CODE BEGIN 0 */
41+
42+
/* USER CODE END 0 */
43+
44+
/* Private define ------------------------------------------------------------*/
45+
#define MASTER_CPU_ID 0
46+
#define REMOTE_CPU_ID 1
47+
#define IPCC_CPU_A7 MASTER_CPU_ID
48+
#define IPCC_CPU_M4 REMOTE_CPU_ID
49+
50+
#define RX_NO_MSG 0
51+
#define RX_NEW_MSG 1
52+
#define RX_BUF_FREE 2
53+
54+
/* Private variables ---------------------------------------------------------*/
55+
extern IPCC_HandleTypeDef hipcc;
56+
int msg_received_ch1 = RX_NO_MSG;
57+
int msg_received_ch2 = RX_NO_MSG;
58+
uint32_t vring0_id = 0; /* used for channel 1 */
59+
uint32_t vring1_id = 1; /* used for channel 2 */
60+
61+
62+
63+
64+
IPCC_HandleTypeDef hipcc;
65+
66+
67+
68+
69+
/* Private function prototypes -----------------------------------------------*/
70+
void IPCC_channel1_callback(IPCC_HandleTypeDef *hipcc, uint32_t ChannelIndex, IPCC_CHANNELDirTypeDef ChannelDir);
71+
void IPCC_channel2_callback(IPCC_HandleTypeDef *hipcc, uint32_t ChannelIndex, IPCC_CHANNELDirTypeDef ChannelDir);
72+
73+
74+
/**
75+
* @brief Initialize MAILBOX with IPCC peripheral
76+
* @param None
77+
* @retval : Operation result
78+
*/
79+
int MAILBOX_Init(void)
80+
{
81+
__HAL_RCC_IPCC_CLK_ENABLE();
82+
HAL_NVIC_SetPriority(IPCC_RX1_IRQn, IPCC_IRQ_PRIO, IPCC_IRQ_SUBPRIO);
83+
HAL_NVIC_EnableIRQ(IPCC_RX1_IRQn);
84+
hipcc.Instance = IPCC;
85+
if (HAL_IPCC_Init(&hipcc) != HAL_OK) {
86+
Error_Handler();
87+
}
88+
89+
if (HAL_IPCC_ActivateNotification(&hipcc, IPCC_CHANNEL_1, IPCC_CHANNEL_DIR_RX,
90+
IPCC_channel1_callback) != HAL_OK) {
91+
OPENAMP_log_err("%s: ch_1 RX fail\n", __func__);
92+
return -1;
93+
}
94+
95+
if (HAL_IPCC_ActivateNotification(&hipcc, IPCC_CHANNEL_2, IPCC_CHANNEL_DIR_RX,
96+
IPCC_channel2_callback) != HAL_OK) {
97+
OPENAMP_log_err("%s: ch_2 RX fail\n", __func__);
98+
return -1;
99+
}
100+
101+
return 0;
102+
}
103+
104+
/**
105+
* @brief Initialize MAILBOX with IPCC peripheral
106+
* @param virtio device
107+
* @retval : Operation result
108+
*/
109+
int MAILBOX_Poll(struct virtio_device *vdev)
110+
{
111+
/* If we got an interrupt, ask for the corresponding virtqueue processing */
112+
113+
if (msg_received_ch1 == RX_BUF_FREE) {
114+
OPENAMP_log_dbg("Running virt0 (ch_1 buf free)\r\n");
115+
rproc_virtio_notified(vdev, VRING0_ID);
116+
msg_received_ch1 = RX_NO_MSG;
117+
return 0;
118+
}
119+
120+
if (msg_received_ch2 == RX_NEW_MSG) {
121+
OPENAMP_log_dbg("Running virt1 (ch_2 new msg)\r\n");
122+
rproc_virtio_notified(vdev, VRING1_ID);
123+
msg_received_ch2 = RX_NO_MSG;
124+
125+
/* The OpenAMP framework does not notify for free buf: do it here */
126+
rproc_virtio_notified(NULL, VRING1_ID);
127+
return 0;
128+
}
129+
130+
return -1;
131+
}
132+
133+
134+
/**
135+
* @brief Callback function called by OpenAMP MW to notify message processing
136+
* @param VRING id
137+
* @retval Operation result
138+
*/
139+
int MAILBOX_Notify(void *priv, uint32_t id)
140+
{
141+
uint32_t channel;
142+
(void)priv;
143+
144+
/* Called after virtqueue processing: time to inform the remote */
145+
if (id == VRING0_ID) {
146+
channel = IPCC_CHANNEL_1;
147+
OPENAMP_log_dbg("Send msg on ch_1\r\n");
148+
} else if (id == VRING1_ID) {
149+
/* Note: the OpenAMP framework never notifies this */
150+
channel = IPCC_CHANNEL_2;
151+
OPENAMP_log_dbg("Send 'buff free' on ch_2\r\n");
152+
} else {
153+
OPENAMP_log_err("invalid vring (%d)\r\n", (int)id);
154+
return -1;
155+
}
156+
157+
/* Check that the channel is free (otherwise wait until it is) */
158+
if (HAL_IPCC_GetChannelStatus(&hipcc, channel, IPCC_CHANNEL_DIR_TX) == IPCC_CHANNEL_STATUS_OCCUPIED) {
159+
OPENAMP_log_dbg("Waiting for channel to be freed\r\n");
160+
while (HAL_IPCC_GetChannelStatus(&hipcc, channel, IPCC_CHANNEL_DIR_TX) == IPCC_CHANNEL_STATUS_OCCUPIED)
161+
;
162+
}
163+
164+
/* Inform A7 (either new message, or buf free) */
165+
HAL_IPCC_NotifyCPU(&hipcc, channel, IPCC_CHANNEL_DIR_TX);
166+
167+
return 0;
168+
}
169+
170+
/* Private function ---------------------------------------------------------*/
171+
/* Callback from IPCC Interrupt Handler: Master Processor informs that there are some free buffers */
172+
void IPCC_channel1_callback(IPCC_HandleTypeDef *hipcc,
173+
uint32_t ChannelIndex, IPCC_CHANNELDirTypeDef ChannelDir)
174+
{
175+
if (msg_received_ch1 != RX_NO_MSG) {
176+
OPENAMP_log_dbg("IPCC_channel1_callback: previous IRQ not treated (status = %d)\r\n", msg_received_ch1);
177+
}
178+
179+
msg_received_ch1 = RX_BUF_FREE;
180+
181+
/* Inform A7 that we have received the 'buff free' msg */
182+
OPENAMP_log_dbg("Ack 'buff free' message on ch1\r\n");
183+
HAL_IPCC_NotifyCPU(hipcc, ChannelIndex, IPCC_CHANNEL_DIR_RX);
184+
}
185+
186+
/* Callback from IPCC Interrupt Handler: new message received from Master Processor */
187+
void IPCC_channel2_callback(IPCC_HandleTypeDef *hipcc,
188+
uint32_t ChannelIndex, IPCC_CHANNELDirTypeDef ChannelDir)
189+
{
190+
if (msg_received_ch2 != RX_NO_MSG) {
191+
OPENAMP_log_dbg("IPCC_channel2_callback: previous IRQ not treated (status = %d)\r\n", msg_received_ch2);
192+
}
193+
194+
msg_received_ch2 = RX_NEW_MSG;
195+
196+
/* Inform A7 that we have received the new msg */
197+
OPENAMP_log_dbg("Ack new message on ch2\r\n");
198+
HAL_IPCC_NotifyCPU(hipcc, ChannelIndex, IPCC_CHANNEL_DIR_RX);
199+
}
200+
201+
/**
202+
* @brief This function handles IPCC RX1 occupied interrupt.
203+
*/
204+
void IPCC_RX1_IRQHandler(void)
205+
{
206+
/* USER CODE BEGIN IPCC_RX1_IRQn 0 */
207+
log_dbg("%s: IT RX1\r\n", __func__);
208+
/* USER CODE END IPCC_RX1_IRQn 0 */
209+
HAL_IPCC_RX_IRQHandler(&hipcc);
210+
/* USER CODE BEGIN IPCC_RX1_IRQn 1 */
211+
212+
/* USER CODE END IPCC_RX1_IRQn 1 */
213+
}
214+
215+
#endif /* VIRTIOCON */
216+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
******************************************************************************
3+
* @file mbox_ipcc.h
4+
* @author MCD Application Team
5+
* @brief Header for mbox_ipcc.c module
6+
******************************************************************************
7+
* @attention
8+
*
9+
* <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
10+
* All rights reserved.</center></h2>
11+
*
12+
* This software component is licensed by ST under BSD 3-Clause license,
13+
* the "License"; You may not use this file except in compliance with the
14+
* License. You may obtain a copy of the License at:
15+
* opensource.org/licenses/BSD-3-Clause
16+
*
17+
******************************************************************************
18+
*/
19+
20+
#ifndef MBOX_IPCC_H_
21+
#define MBOX_IPCC_H_
22+
23+
#ifdef VIRTIOCON
24+
25+
/* Interrupt priority */
26+
#ifndef IPCC_IRQ_PRIO
27+
#define IPCC_IRQ_PRIO 1
28+
#endif
29+
#ifndef IPCC_IRQ_SUBPRIO
30+
#define IPCC_IRQ_SUBPRIO 0
31+
#endif
32+
33+
/* USER CODE BEGIN firstSection */
34+
/* can be used to modify / undefine following code or add new definitions */
35+
/* USER CODE END firstSection */
36+
37+
/* Includes ------------------------------------------------------------------*/
38+
/* Exported types ------------------------------------------------------------*/
39+
/* Exported constants --------------------------------------------------------*/
40+
/* Exported functions ------------------------------------------------------- */
41+
int MAILBOX_Init(void);
42+
int MAILBOX_Notify(void *priv, uint32_t id);
43+
int MAILBOX_Poll(struct virtio_device *vdev);
44+
45+
/* USER CODE BEGIN lastSection */
46+
/* can be used to modify / undefine previous code or add new definitions */
47+
/* USER CODE END lastSection */
48+
49+
#endif /* VIRTIOCON */
50+
#endif /* MBOX_IPCC_H_ */

0 commit comments

Comments
 (0)