summaryrefslogtreecommitdiff
path: root/examples/imx7_colibri_m4/demo_apps/rpmsg/gpio_freertos/main.c
blob: ee5db19f16e191b22c3cf981f7eede16b0070fd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * Copyright (c) 2015, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include "board.h"
#include "gpio_pins.h"
#include "gpio_imx.h"
#include "gpio_ctrl.h"
#include "debug_console_imx.h"
#include "rpmsg/rpmsg_rtos.h"
#include "FreeRTOS.h"
#include "task.h"
#include "gpt.h"
#include "semphr.h"
#include "string.h"
#include "mu_imx.h"
#include "rdc_semaphore.h"

////////////////////////////////////////////////////////////////////////////////
// Definitions
////////////////////////////////////////////////////////////////////////////////
#define APP_TASK_STACK_SIZE 256

/*
 * APP decided interrupt priority
 */
#define APP_MU_IRQ_PRIORITY 3


/******************************************************************************
 *
 * Function Name: ToggleTask
 * Comments: this task is used to turn toggle on/off LED.
 *
 ******************************************************************************/
static void StrEchoTask(void *pvParameters)
{
    bool key1PressCount = 0;
    bool key1PressCountOld = 0;
    bool key2PressCount = 0;
    bool key2PressCountOld = 0;
    bool sendNewData = 0;

    int result;
    struct remote_device *rdev = NULL;
    struct rpmsg_channel *app_chnl = NULL;
    char str_buffer[256] ;
    int len;
    void *rx_buf;
    void *tx_buf;
    unsigned long size;
    unsigned long src;

    /* Print the initial banner */
    PRINTF("\r\nRPMSG String Echo FreeRTOS RTOS API Demo...\r\n");

    /* RPMSG Init as REMOTE */
    PRINTF("RPMSG Init as Remote\r\n");
    result = rpmsg_rtos_init(0 /*REMOTE_CPU_ID*/, &rdev, RPMSG_MASTER, &app_chnl);
    assert(result == 0);
    PRINTF("Name service handshake is done, M4 has setup a rpmsg channel [%d ---> %d]\r\n", app_chnl->src, app_chnl->dst);

    /* Initialize enviroments and led status */
    key1PressCount = GPIO_Ctrl_GetKey(BOARD_GPIO_SWITCH1_CONFIG);
    key1PressCountOld = key1PressCount;
    GPIO_Ctrl_WriteLed(BOARD_GPIO_LED1_CONFIG, BOARD_GPIO_LED1_RDC_PDAP, key1PressCount);

    key2PressCount = GPIO_Ctrl_GetKey(BOARD_GPIO_SWITCH2_CONFIG);
    key2PressCountOld = key2PressCount;
    GPIO_Ctrl_WriteLed(BOARD_GPIO_LED2_CONFIG, BOARD_GPIO_LED2_RDC_PDAP, key2PressCount);

    sendNewData = 1;
    PRINTF("================== RPMSG DONE===================\n\r");

    while(true)
    {
        /* wait for user change button leve */
        key1PressCount = GPIO_Ctrl_GetKey(BOARD_GPIO_SWITCH1_CONFIG);
        key2PressCount = GPIO_Ctrl_GetKey(BOARD_GPIO_SWITCH2_CONFIG);

        /* Get RPMsg rx buffer with message */
        result = rpmsg_rtos_recv_nocopy(app_chnl->rp_ept, &rx_buf, &len, &src, 0);

        /* Copy string from RPMsg rx buffer */
        assert(len < sizeof(str_buffer));
        memcpy(str_buffer, rx_buf, len);
        str_buffer[len] = 0; /* End string by '\0' */


        /*Parse MSG*/
        char * pch1;
        char * pch2;
        pch1 = strstr (str_buffer, "Led");
        if(pch1)
        {
            pch1 = pch1 + 3;
            pch2 = pch1 + 2;
            /*What LED 1 or 2*/
            if(*pch1 == '1')
            {
                /* What Value 1 or 0*/
                if(*pch2 == '1')
                {
                    GPIO_Ctrl_WriteLed(BOARD_GPIO_LED1_CONFIG, BOARD_GPIO_LED1_RDC_PDAP, 1);
                }
                else
                {
                    GPIO_Ctrl_WriteLed(BOARD_GPIO_LED1_CONFIG, BOARD_GPIO_LED1_RDC_PDAP, 0);
                }
            }
            if(*pch1 == '2')
            {
                /* What Value 1 or 0*/
                if(*pch2 == '1')
                {
                    GPIO_Ctrl_WriteLed(BOARD_GPIO_LED2_CONFIG, BOARD_GPIO_LED2_RDC_PDAP, 1);
                }
                else
                {
                    GPIO_Ctrl_WriteLed(BOARD_GPIO_LED2_CONFIG, BOARD_GPIO_LED2_RDC_PDAP, 0);
                }
            }
        }

        /* free rx buffer */
        result = rpmsg_rtos_recv_nocopy_free(app_chnl->rp_ept, rx_buf);

        /* If button change, than print and update LED */
        if(key1PressCount != key1PressCountOld)
        {
            key1PressCountOld = key1PressCount;
            /* update led status */
            GPIO_Ctrl_WriteLed(BOARD_GPIO_LED1_CONFIG, BOARD_GPIO_LED1_RDC_PDAP, key1PressCount);
            sendNewData = 1;
        }
        /* If button change, than print and update LED */
        if(key2PressCount != key2PressCountOld)
        {
            /* update led status */
            key2PressCountOld = key2PressCount;
            GPIO_Ctrl_WriteLed(BOARD_GPIO_LED2_CONFIG, BOARD_GPIO_LED2_RDC_PDAP, key2PressCount);
            sendNewData = 1;
        }

        /* Switch changed*/
        if(sendNewData == 1)
        {
            sendNewData = 0;
            memset(str_buffer, 0, sizeof(str_buffer));

            PRINTF("================== key1PressCount = %d ==================\n\r", key1PressCount);
            PRINTF("================== key2PressCount = %d ==================\n\r", key2PressCount);

            /* Create message */
            len = snprintf(str_buffer, sizeof(str_buffer),"KEY1=%d; KEY2=%d\n",
                    key1PressCount, key2PressCount);

            /* Release tx buffer */
            tx_buf = rpmsg_rtos_alloc_tx_buffer(app_chnl->rp_ept, &size);
            if(tx_buf == NULL)
            {
                PRINTF("\r\n ERROR = rpmsg_rtos_alloc_tx_buffer\r\n");
                continue;
            }

            /* Copy string to RPMsg tx buffer */
            memcpy(tx_buf, str_buffer, len);

            /* Send message... */
            result = rpmsg_rtos_send_nocopy(app_chnl->rp_ept, tx_buf, len, app_chnl->dst);
            if(result != 0)
            {
                PRINTF("\r\n ERROR = rpmsg_rtos_send_nocopy\r\n");
            }
            assert(result == 0);
        }
    }

}
/*
 * MU Interrrupt ISR
 */
void BOARD_MU_HANDLER(void)
{
    /*
     * calls into rpmsg_handler provided by middleware
     */
    rpmsg_handler();
}


/******************************************************************************
 *
 * Function Name: main
 * Comments: Hello World Example with GPIO.
 *   This example include:
 *   Configure BUTTON1 as GPIO functionality
 *     and check the button's state(pressed or released). According to the Button
 *     status, copy it to the LED
 *
 ******************************************************************************/
int main(void)
{
    /* hardware initialiize, include RDC, IOMUX, Uart debug initialize */
    hardware_init();
    PRINTF("\n\r\n\r\n\r");
    PRINTF("=======================================================\n\r");
    PRINTF("============ GPIO FreeRtos and MCC Example ============\n\r");
    PRINTF("========== Play with Led and Button and MCC ===========\n\r");
    PRINTF("=======================================================\n\r");


    /* GPIO module initialize, configure "LED" as output and button as interrupt mode. */
    GPIO_Ctrl_Init();

    /*
     * Prepare for the MU Interrupt
     *  MU must be initialized before rpmsg init is called
     */
    MU_Init(BOARD_MU_BASE_ADDR);
    NVIC_SetPriority(BOARD_MU_IRQ_NUM, APP_MU_IRQ_PRIORITY);
    NVIC_EnableIRQ(BOARD_MU_IRQ_NUM);

    /* Create a demo task. */
    xTaskCreate(StrEchoTask, "String Echo Task", APP_TASK_STACK_SIZE,
                NULL, tskIDLE_PRIORITY+1, NULL);

    /* Start FreeRTOS scheduler. */
    vTaskStartScheduler();

    /* Should never reach this point. */
    while (true);
}
/*******************************************************************************
 * EOF
 ******************************************************************************/