Changeset ca97cad in mainline
- Timestamp:
- 2010-05-06T11:42:55Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ab1aa871
- Parents:
- ba95e8f
- Location:
- uspace
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/test_serial/test_serial.c
rba95e8f rca97cad 45 45 #include <devman.h> 46 46 #include <device/char.h> 47 #include <string.h> 47 48 48 49 #define NAME "test serial" … … 104 105 if (read > 0) { 105 106 buf[read] = 0; 106 printf(buf); 107 printf(buf); 108 // write data back to the device to test the opposite direction of data transfer 109 write_dev(phone, buf, read); 107 110 } else { 108 111 usleep(100000); 109 112 } 110 113 } 114 115 char *the_end = "\n---------\nTHE END\n---------\n"; 116 write_dev(phone, the_end, str_size(the_end)); 111 117 112 118 devman_hangup_phone(DEVMAN_CLIENT); -
uspace/lib/libc/generic/device/char.c
rba95e8f rca97cad 40 40 #include <stdio.h> 41 41 42 43 int read_dev(int dev_phone, void *buf, size_t len) 44 { 42 /** Read to or write from the device using its character interface. 43 * 44 * Helper function. 45 * 46 * @param dev_phone phone to the device. 47 * @param buf the buffer for the data read from or written to the device. 48 * @param len the maximum length of the data to be read or written. 49 * @param read read from the device if true, write to it otherwise. 50 * 51 * @return non-negative number of bytes actually read from or written to the device on success, 52 * negative error number otherwise. 53 * 54 */ 55 static int rw_dev(int dev_phone, void *buf, size_t len, bool read) 56 { 45 57 ipc_call_t answer; 46 58 47 59 async_serialize_start(); 48 60 49 aid_t req = async_send_1(dev_phone, DEV_IFACE_ID(CHAR_DEV_IFACE), CHAR_READ_DEV, &answer); 61 aid_t req; 62 int rc; 50 63 51 int rc = async_data_read_start(dev_phone, buf, len); 64 if (read) { 65 req = async_send_1(dev_phone, DEV_IFACE_ID(CHAR_DEV_IFACE), CHAR_READ_DEV, &answer); 66 rc = async_data_read_start(dev_phone, buf, len); 67 } else { 68 req = async_send_1(dev_phone, DEV_IFACE_ID(CHAR_DEV_IFACE), CHAR_WRITE_DEV, &answer); 69 rc = async_data_write_start(dev_phone, buf, len); 70 } 52 71 53 72 if (rc != EOK) { … … 70 89 } 71 90 72 return IPC_GET_ARG1(answer); 91 return IPC_GET_ARG1(answer); 73 92 } 74 93 94 /** Read from the device using its character interface. 95 * 96 * @param dev_phone phone to the device. 97 * @param buf the output buffer for the data read from the device. 98 * @param len the maximum length of the data to be read. 99 * 100 * @return non-negative number of bytes actually read from the device on success, negative error number otherwise. 101 */ 102 int read_dev(int dev_phone, void *buf, size_t len) 103 { 104 return rw_dev(dev_phone, buf, len, true); 105 } 106 107 /** Write to the device using its character interface. 108 * 109 * @param dev_phone phone to the device. 110 * @param buf the input buffer containg the data to be written to the device. 111 * @param len the maximum length of the data to be written. 112 * 113 * @return non-negative number of bytes actually written to the device on success, negative error number otherwise. 114 */ 75 115 int write_dev(int dev_phone, void *buf, size_t len) 76 116 { 77 // TODO 78 return 0; 117 return rw_dev(dev_phone, buf, len, false); 79 118 } 80 119 -
uspace/lib/libdrv/generic/remote_char.c
rba95e8f rca97cad 45 45 static void remote_char_write(device_t *dev, void *iface, ipc_callid_t callid, ipc_call_t *call); 46 46 47 /** Remote character interface operations. 48 */ 47 49 static remote_iface_func_ptr_t remote_char_iface_ops [] = { 48 50 &remote_char_read, … … 50 52 }; 51 53 54 /** Remote character interface structure. 55 * Interface for processing request from remote clients addressed to the character interface. 56 */ 52 57 remote_iface_t remote_char_iface = { 53 58 .method_count = sizeof(remote_char_iface_ops) / sizeof(remote_iface_func_ptr_t), … … 55 60 }; 56 61 62 /** Process the read request from the remote client. 63 * 64 * Receive the read request's parameters from the remote client and pass them to the local interface. 65 * Return the result of the operation processed by the local interface to the remote client. 66 * 67 * @param dev the device from which the data are read. 68 * @param iface the local interface structure. 69 */ 57 70 static void remote_char_read(device_t *dev, void *iface, ipc_callid_t callid, ipc_call_t *call) 58 71 { … … 86 99 } 87 100 101 // the operation was successful, return the number of data read 88 102 async_data_read_finalize(cid, buf, ret); 89 103 ipc_answer_1(callid, EOK, ret); 90 104 } 91 105 106 /** Process the write request from the remote client. 107 * 108 * Receive the write request's parameters from the remote client and pass them to the local interface. 109 * Return the result of the operation processed by the local interface to the remote client. 110 * 111 * @param dev the device to which the data are written. 112 * @param iface the local interface structure. 113 */ 92 114 static void remote_char_write(device_t *dev, void *iface, ipc_callid_t callid, ipc_call_t *call) 93 115 { 94 116 char_iface_t *char_iface = (char_iface_t *)iface; 117 ipc_callid_t cid; 118 size_t len; 119 120 if (!async_data_write_receive(&cid, &len)) { 121 // TODO handle protocol error 122 ipc_answer_0(callid, EINVAL); 123 return; 124 } 125 95 126 if (!char_iface->write) { 127 async_data_write_finalize(cid, NULL, 0); 96 128 ipc_answer_0(callid, ENOTSUP); 97 129 return; 98 130 } 99 100 size_t len;101 if (!async_data_write_receive(&callid, &len)) {102 // TODO handle protocol error103 return;104 }105 131 106 132 if (len > MAX_CHAR_RW_COUNT) { … … 110 136 char buf[MAX_CHAR_RW_COUNT]; 111 137 112 async_data_write_finalize(c allid, buf, len);138 async_data_write_finalize(cid, buf, len); 113 139 114 140 int ret = (*char_iface->write)(dev, buf, len); … … 116 142 ipc_answer_0(callid, ret); 117 143 } else { 144 // the operation was successful, return the number of data written 118 145 ipc_answer_1(callid, EOK, ret); 119 146 } -
uspace/srv/drivers/serial/serial.c
rba95e8f rca97cad 92 92 } 93 93 94 static bool serial_received(ioport8_t *port) 95 { 96 return (pio_read_8(port + 5) & 1) != 0; 97 } 98 99 static uint8_t serial_read_8(ioport8_t *port) 100 { 101 return pio_read_8(port); 102 } 103 104 static bool is_transmit_empty(ioport8_t *port) 105 { 106 return (pio_read_8(port + 5) & 0x20) != 0; 107 } 108 109 static void serial_write_8(ioport8_t *port, uint8_t c) 110 { 111 while (!is_transmit_empty(port)) 112 ; 113 114 pio_write_8(port, c); 115 } 116 94 117 static int serial_read(device_t *dev, char *buf, size_t count) 95 118 { … … 111 134 } 112 135 136 static inline void serial_putchar(serial_dev_data_t *data, uint8_t c) 137 { 138 fibril_mutex_lock(&data->mutex); 139 serial_write_8(data->port, c); 140 fibril_mutex_unlock(&data->mutex); 141 } 142 113 143 static int serial_write(device_t *dev, char *buf, size_t count) 114 144 { 115 // TODO 145 serial_dev_data_t *data = (serial_dev_data_t *)dev->driver_data; 146 147 size_t idx; 148 for (idx = 0; idx < count; idx++) { 149 serial_putchar(data, (uint8_t)buf[idx]); 150 } 151 116 152 return 0; 117 153 } … … 150 186 dev->parent_phone = 0; 151 187 } 152 }153 154 static bool serial_received(ioport8_t *port)155 {156 return (pio_read_8(port + 5) & 1) != 0;157 }158 159 static uint8_t serial_read_8(ioport8_t *port)160 {161 return pio_read_8(port);162 }163 164 static bool is_transmit_empty(ioport8_t *port)165 {166 return (pio_read_8(port + 5) & 0x20) != 0;167 }168 169 static void serial_write_8(ioport8_t *port, uint8_t c)170 {171 while (!is_transmit_empty(port))172 ;173 174 pio_write_8(port, c);175 188 } 176 189 … … 333 346 334 347 while (cont) { 348 fibril_mutex_lock(&data->mutex); 349 335 350 if (cont = serial_received(port)) { 336 351 uint8_t val = serial_read_8(port); 337 352 printf(NAME ": character %c read from %s.\n", val, dev->name); 338 353 339 fibril_mutex_lock(&data->mutex);354 340 355 if (data->client_connected) { 341 356 if (!buf_push_back(&(data->input_buffer), val)) { … … 346 361 } else { 347 362 printf(NAME ": no client is connected to %s, discarding the character which was read.\n", dev->name); 348 } 349 fibril_mutex_unlock(&data->mutex); 363 } 350 364 } 351 365 366 fibril_mutex_unlock(&data->mutex); 367 368 fibril_yield(); 352 369 } 353 370 }
Note:
See TracChangeset
for help on using the changeset viewer.