|
| 1 | +/**************************************************************************** |
| 2 | + * apps/examples/spislv_test/spislv_test.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +/**************************************************************************** |
| 22 | + * Included Files |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <nuttx/config.h> |
| 26 | +#include <stdio.h> |
| 27 | +#include <fcntl.h> |
| 28 | +#include <unistd.h> |
| 29 | +#include <errno.h> |
| 30 | +#include <string.h> |
| 31 | +#include <stdlib.h> |
| 32 | + |
| 33 | +#define SOURCE_FILE "/dev/spislv2" |
| 34 | +#define BUFFER_SIZE 256 |
| 35 | + |
| 36 | +/**************************************************************************** |
| 37 | + * Public Functions |
| 38 | + ****************************************************************************/ |
| 39 | + |
| 40 | +/**************************************************************************** |
| 41 | + * spislv_test |
| 42 | + ****************************************************************************/ |
| 43 | + |
| 44 | +int main(int argc, FAR char *argv[]) |
| 45 | +{ |
| 46 | + int fd; |
| 47 | + char buffer[BUFFER_SIZE]; |
| 48 | + ssize_t bytes_read; |
| 49 | + ssize_t i; |
| 50 | + |
| 51 | + printf("Slave started!!\n"); |
| 52 | + fd = open(SOURCE_FILE, O_RDWR); |
| 53 | + |
| 54 | + if (fd < 0) |
| 55 | + { |
| 56 | + printf("Failed to open %s: %s\n", SOURCE_FILE, strerror(errno)); |
| 57 | + return 0; |
| 58 | + } |
| 59 | + |
| 60 | + while (1) |
| 61 | + { |
| 62 | + /* Read the number from the source file */ |
| 63 | + |
| 64 | + printf("Slave: Reading from %s\n", SOURCE_FILE); |
| 65 | + bytes_read = read(fd, buffer, BUFFER_SIZE - 1); |
| 66 | + |
| 67 | + if (bytes_read < 0) |
| 68 | + { |
| 69 | + printf("Failed to read from %s: %s\n", |
| 70 | + SOURCE_FILE, strerror(errno)); |
| 71 | + close(fd); |
| 72 | + return 0; |
| 73 | + } |
| 74 | + else if (bytes_read > 0) |
| 75 | + { |
| 76 | + buffer[bytes_read] = '\0'; |
| 77 | + |
| 78 | + /* Print buffer in hexadecimal format */ |
| 79 | + |
| 80 | + printf("Slave: Read value in hex: "); |
| 81 | + for (i = 0; i < bytes_read; ++i) |
| 82 | + { |
| 83 | + printf("%02x ", (unsigned char)buffer[i]); |
| 84 | + } |
| 85 | + |
| 86 | + printf("\n"); |
| 87 | + |
| 88 | + /* Write the same value back */ |
| 89 | + |
| 90 | + printf("Slave: Writing %d bytes back to %s\n", |
| 91 | + bytes_read, SOURCE_FILE); |
| 92 | + ssize_t bytes_written = write(fd, buffer, bytes_read); |
| 93 | + if (bytes_written < 0) |
| 94 | + { |
| 95 | + printf("Failed to write to %s: %s\n", |
| 96 | + SOURCE_FILE, strerror(errno)); |
| 97 | + close(fd); |
| 98 | + return 0; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments