this is testing

Committer:
pmallick
Date:
Thu Jan 14 18:54:16 2021 +0530
Revision:
0:3afcd581558d
this is testing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmallick 0:3afcd581558d 1 /***************************************************************************//**
pmallick 0:3afcd581558d 2 * @file iio_transport.c
pmallick 0:3afcd581558d 3 * @brief Implementation of iio transport layer interface for AD7606
pmallick 0:3afcd581558d 4 ********************************************************************************
pmallick 0:3afcd581558d 5 * Copyright (c) 2020 Analog Devices, Inc.
pmallick 0:3afcd581558d 6 *
pmallick 0:3afcd581558d 7 * This software is proprietary to Analog Devices, Inc. and its licensors.
pmallick 0:3afcd581558d 8 * By using this software you agree to the terms of the associated
pmallick 0:3afcd581558d 9 * Analog Devices Software License Agreement.
pmallick 0:3afcd581558d 10 *******************************************************************************/
pmallick 0:3afcd581558d 11
pmallick 0:3afcd581558d 12 /******************************************************************************/
pmallick 0:3afcd581558d 13 /***************************** Include Files **********************************/
pmallick 0:3afcd581558d 14 /******************************************************************************/
pmallick 0:3afcd581558d 15
pmallick 0:3afcd581558d 16 #include "iio_transport.h"
pmallick 0:3afcd581558d 17
pmallick 0:3afcd581558d 18 /******************************************************************************/
pmallick 0:3afcd581558d 19 /********************** Macros and Constants Definition ***********************/
pmallick 0:3afcd581558d 20 /******************************************************************************/
pmallick 0:3afcd581558d 21
pmallick 0:3afcd581558d 22 /* Max buffer length to hold IIO client data/command */
pmallick 0:3afcd581558d 23 #define IIO_CMD_DATA_BUFF_SIZE (100)
pmallick 0:3afcd581558d 24
pmallick 0:3afcd581558d 25 /******************************************************************************/
pmallick 0:3afcd581558d 26 /*************************** Types Declarations *******************************/
pmallick 0:3afcd581558d 27 /******************************************************************************/
pmallick 0:3afcd581558d 28
pmallick 0:3afcd581558d 29 /* UART descriptor to read/write UART peripheral data */
pmallick 0:3afcd581558d 30 static struct uart_desc *uart_desc = NULL;
pmallick 0:3afcd581558d 31
pmallick 0:3afcd581558d 32 /* IIO command buffer */
pmallick 0:3afcd581558d 33 static volatile char iio_cmd_buffer[IIO_CMD_DATA_BUFF_SIZE];
pmallick 0:3afcd581558d 34
pmallick 0:3afcd581558d 35 /* IIO data buffer */
pmallick 0:3afcd581558d 36 static volatile char iio_data_buffer[IIO_CMD_DATA_BUFF_SIZE];
pmallick 0:3afcd581558d 37
pmallick 0:3afcd581558d 38 /* New IIO command detect flag */
pmallick 0:3afcd581558d 39 static volatile bool new_iio_cmd_detected = false;
pmallick 0:3afcd581558d 40
pmallick 0:3afcd581558d 41 /* Data buffer index */
pmallick 0:3afcd581558d 42 volatile static uint8_t data_buff_indx = 0;
pmallick 0:3afcd581558d 43
pmallick 0:3afcd581558d 44 /* Command buffer index */
pmallick 0:3afcd581558d 45 volatile static uint8_t cmd_buff_indx = 0;
pmallick 0:3afcd581558d 46
pmallick 0:3afcd581558d 47 /******************************************************************************/
pmallick 0:3afcd581558d 48 /************************ Functions Declarations ******************************/
pmallick 0:3afcd581558d 49 /******************************************************************************/
pmallick 0:3afcd581558d 50
pmallick 0:3afcd581558d 51 /******************************************************************************/
pmallick 0:3afcd581558d 52 /************************ Functions Definitions *******************************/
pmallick 0:3afcd581558d 53 /******************************************************************************/
pmallick 0:3afcd581558d 54
pmallick 0:3afcd581558d 55 /**
pmallick 0:3afcd581558d 56 * @brief IIO Wrapper to initialize the UART peripheral
pmallick 0:3afcd581558d 57 * @param desc- Pointer UART descriptor structure
pmallick 0:3afcd581558d 58 * @param param- Pointer to UART init param structure
pmallick 0:3afcd581558d 59 * @return uart init status (SUCCESS/FAILURE)
pmallick 0:3afcd581558d 60 */
pmallick 0:3afcd581558d 61 int32_t iio_uart_init(struct uart_desc **desc, struct uart_init_param *param)
pmallick 0:3afcd581558d 62 {
pmallick 0:3afcd581558d 63 if (uart_init(desc, param) == SUCCESS) {
pmallick 0:3afcd581558d 64 /* Save the descriptor for data read/write operation */
pmallick 0:3afcd581558d 65 uart_desc = *desc;
pmallick 0:3afcd581558d 66
pmallick 0:3afcd581558d 67 return SUCCESS;
pmallick 0:3afcd581558d 68 } else {
pmallick 0:3afcd581558d 69 return FAILURE;
pmallick 0:3afcd581558d 70 }
pmallick 0:3afcd581558d 71 }
pmallick 0:3afcd581558d 72
pmallick 0:3afcd581558d 73
pmallick 0:3afcd581558d 74 /**
pmallick 0:3afcd581558d 75 * @brief IIO Wrapper to wrire data to UART peripheral
pmallick 0:3afcd581558d 76 * @param buf- Pointer to buffer containing data
pmallick 0:3afcd581558d 77 * @param len- Number of bytes to write
pmallick 0:3afcd581558d 78 * @return bytes len in case of success, 0 otherwise
pmallick 0:3afcd581558d 79 */
pmallick 0:3afcd581558d 80 ssize_t iio_uart_write(const char *buf, size_t len)
pmallick 0:3afcd581558d 81 {
pmallick 0:3afcd581558d 82 if (uart_write(uart_desc, (uint8_t *)buf, len) == SUCCESS) {
pmallick 0:3afcd581558d 83 return len;
pmallick 0:3afcd581558d 84 } else {
pmallick 0:3afcd581558d 85 return 0;
pmallick 0:3afcd581558d 86 }
pmallick 0:3afcd581558d 87 }
pmallick 0:3afcd581558d 88
pmallick 0:3afcd581558d 89
pmallick 0:3afcd581558d 90 /**
pmallick 0:3afcd581558d 91 * @brief IIO Wrapper to read iio data/command from UART peripheral
pmallick 0:3afcd581558d 92 * @param buf- Pointer to buffer containing data
pmallick 0:3afcd581558d 93 * @param len- Number of bytes to read
pmallick 0:3afcd581558d 94 * @return bytes len in case of success, 0 otherwise
pmallick 0:3afcd581558d 95 */
pmallick 0:3afcd581558d 96 ssize_t iio_uart_read(char *buf, size_t len)
pmallick 0:3afcd581558d 97 {
pmallick 0:3afcd581558d 98 size_t i = 0;
pmallick 0:3afcd581558d 99
pmallick 0:3afcd581558d 100 if (len > 1) {
pmallick 0:3afcd581558d 101 do {
pmallick 0:3afcd581558d 102 /* Wait until new data of expected length is received
pmallick 0:3afcd581558d 103 * from UART Rx interrupt event */
pmallick 0:3afcd581558d 104 } while (data_buff_indx < len);
pmallick 0:3afcd581558d 105
pmallick 0:3afcd581558d 106 /* Get the data into local buffer (entire data) */
pmallick 0:3afcd581558d 107 while (i < len) {
pmallick 0:3afcd581558d 108 buf[i] = iio_data_buffer[i];
pmallick 0:3afcd581558d 109 i++;
pmallick 0:3afcd581558d 110 }
pmallick 0:3afcd581558d 111 data_buff_indx = 0;
pmallick 0:3afcd581558d 112 } else {
pmallick 0:3afcd581558d 113 /* Get the iio command into local buffer (1 character at a time) */
pmallick 0:3afcd581558d 114 buf[i] = iio_cmd_buffer[cmd_buff_indx];
pmallick 0:3afcd581558d 115
pmallick 0:3afcd581558d 116 /* Reset buffer index after reading whole command */
pmallick 0:3afcd581558d 117 if (iio_cmd_buffer[cmd_buff_indx] == '\n') {
pmallick 0:3afcd581558d 118 cmd_buff_indx = 0;
pmallick 0:3afcd581558d 119 } else {
pmallick 0:3afcd581558d 120 cmd_buff_indx++;
pmallick 0:3afcd581558d 121 }
pmallick 0:3afcd581558d 122 }
pmallick 0:3afcd581558d 123
pmallick 0:3afcd581558d 124 return len;
pmallick 0:3afcd581558d 125 }
pmallick 0:3afcd581558d 126
pmallick 0:3afcd581558d 127
pmallick 0:3afcd581558d 128 /*!
pmallick 0:3afcd581558d 129 * @brief Callback function to receive IIO command
pmallick 0:3afcd581558d 130 * @return None
pmallick 0:3afcd581558d 131 * @details This function is an UART Rx ISR registered by IIO application
pmallick 0:3afcd581558d 132 * to invoke upon receipt og new character over UART link.
pmallick 0:3afcd581558d 133 */
pmallick 0:3afcd581558d 134 void iio_uart_rx_callback(void)
pmallick 0:3afcd581558d 135 {
pmallick 0:3afcd581558d 136 uint8_t rx_char;
pmallick 0:3afcd581558d 137
pmallick 0:3afcd581558d 138 if (uart_read(uart_desc, &rx_char, 1) == SUCCESS) {
pmallick 0:3afcd581558d 139
pmallick 0:3afcd581558d 140 iio_data_buffer[data_buff_indx++] = rx_char;
pmallick 0:3afcd581558d 141
pmallick 0:3afcd581558d 142 if (rx_char == '\n') {
pmallick 0:3afcd581558d 143 /* The iio command is identified with new line character */
pmallick 0:3afcd581558d 144 memcpy(iio_cmd_buffer, iio_data_buffer, data_buff_indx);
pmallick 0:3afcd581558d 145 data_buff_indx = 0;
pmallick 0:3afcd581558d 146 new_iio_cmd_detected = true;
pmallick 0:3afcd581558d 147 }
pmallick 0:3afcd581558d 148 }
pmallick 0:3afcd581558d 149 }
pmallick 0:3afcd581558d 150
pmallick 0:3afcd581558d 151
pmallick 0:3afcd581558d 152 /*!
pmallick 0:3afcd581558d 153 * @brief Function to check for newer IIO command
pmallick 0:3afcd581558d 154 * @return IIO command status (true/false)
pmallick 0:3afcd581558d 155 */
pmallick 0:3afcd581558d 156 bool is_new_iio_command_detected(void)
pmallick 0:3afcd581558d 157 {
pmallick 0:3afcd581558d 158 if (new_iio_cmd_detected) {
pmallick 0:3afcd581558d 159 new_iio_cmd_detected = false;
pmallick 0:3afcd581558d 160 return true;
pmallick 0:3afcd581558d 161 } else {
pmallick 0:3afcd581558d 162 return false;
pmallick 0:3afcd581558d 163 }
pmallick 0:3afcd581558d 164 }
pmallick 0:3afcd581558d 165
pmallick 0:3afcd581558d 166
pmallick 0:3afcd581558d 167 /*!
pmallick 0:3afcd581558d 168 * @brief Function to check for expected IIO command
pmallick 0:3afcd581558d 169 * @param cmd_str[in] - Expected IIO command string
pmallick 0:3afcd581558d 170 * @param len[in] - Length of IIO command
pmallick 0:3afcd581558d 171 * @return IIO command status (true/false)
pmallick 0:3afcd581558d 172 */
pmallick 0:3afcd581558d 173 bool check_iio_cmd(const char *cmd_str, uint8_t len)
pmallick 0:3afcd581558d 174 {
pmallick 0:3afcd581558d 175 int ret;
pmallick 0:3afcd581558d 176
pmallick 0:3afcd581558d 177 ret = strncmp(cmd_str, iio_cmd_buffer, len);
pmallick 0:3afcd581558d 178 if (ret == 0) {
pmallick 0:3afcd581558d 179 /* String matches, return true */
pmallick 0:3afcd581558d 180 return true;
pmallick 0:3afcd581558d 181 }
pmallick 0:3afcd581558d 182
pmallick 0:3afcd581558d 183 return false;
pmallick 0:3afcd581558d 184 }