Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset ca97cad in mainline


Ignore:
Timestamp:
2010-05-06T11:42:55Z (11 years ago)
Author:
Lenka Trochtova <trochtova.lenka@…>
Branches:
lfn, master
Children:
ab1aa871
Parents:
ba95e8f
Message:

writing to serial port using character interface

Location:
uspace
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/test_serial/test_serial.c

    rba95e8f rca97cad  
    4545#include <devman.h>
    4646#include <device/char.h>
     47#include <string.h>
    4748
    4849#define NAME            "test serial"
     
    104105                if (read > 0) {                 
    105106                        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);
    107110                } else {       
    108111                        usleep(100000);                 
    109112                }       
    110113        }
     114       
     115        char *the_end = "\n---------\nTHE END\n---------\n";
     116        write_dev(phone, the_end, str_size(the_end));
    111117       
    112118        devman_hangup_phone(DEVMAN_CLIENT);
  • uspace/lib/libc/generic/device/char.c

    rba95e8f rca97cad  
    4040#include <stdio.h>
    4141
    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 */
     55static int rw_dev(int dev_phone, void *buf, size_t len, bool read)
     56{
    4557        ipc_call_t answer;
    4658       
    4759        async_serialize_start();
    4860       
    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;
    5063       
    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        }
    5271       
    5372        if (rc != EOK) {
     
    7089        }
    7190       
    72         return IPC_GET_ARG1(answer);
     91        return IPC_GET_ARG1(answer);   
    7392}
    7493
     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 */
     102int 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 */
    75115int write_dev(int dev_phone, void *buf, size_t len)
    76116{
    77         // TODO
    78         return 0;
     117        return rw_dev(dev_phone, buf, len, false);
    79118}
    80119
  • uspace/lib/libdrv/generic/remote_char.c

    rba95e8f rca97cad  
    4545static void remote_char_write(device_t *dev, void *iface, ipc_callid_t callid, ipc_call_t *call);
    4646
     47/** Remote character interface operations.
     48 */
    4749static remote_iface_func_ptr_t remote_char_iface_ops [] = {
    4850        &remote_char_read,
     
    5052};
    5153 
     54/** Remote character interface structure.
     55 * Interface for processing request from remote clients addressed to the character interface.
     56 */
    5257remote_iface_t remote_char_iface = {
    5358        .method_count = sizeof(remote_char_iface_ops) / sizeof(remote_iface_func_ptr_t),
     
    5560};
    5661
     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 */
    5770static void remote_char_read(device_t *dev, void *iface, ipc_callid_t callid, ipc_call_t *call)
    5871{       
     
    8699        }
    87100       
     101        // the operation was successful, return the number of data read
    88102        async_data_read_finalize(cid, buf, ret);
    89103        ipc_answer_1(callid, EOK, ret);
    90104}
    91105
     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 */
    92114static void remote_char_write(device_t *dev, void *iface, ipc_callid_t callid, ipc_call_t *call)
    93115{
    94116        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       
    95126        if (!char_iface->write) {
     127                async_data_write_finalize(cid, NULL, 0);
    96128                ipc_answer_0(callid, ENOTSUP);
    97129                return;
    98130        }       
    99        
    100         size_t len;
    101         if (!async_data_write_receive(&callid, &len)) {
    102                 // TODO handle protocol error
    103                 return;
    104     }
    105131       
    106132        if (len > MAX_CHAR_RW_COUNT) {
     
    110136        char buf[MAX_CHAR_RW_COUNT];
    111137       
    112         async_data_write_finalize(callid, buf, len);
     138        async_data_write_finalize(cid, buf, len);
    113139       
    114140        int ret = (*char_iface->write)(dev, buf, len);
     
    116142                ipc_answer_0(callid, ret);
    117143        } else {
     144                // the operation was successful, return the number of data written
    118145                ipc_answer_1(callid, EOK, ret);
    119146        }
  • uspace/srv/drivers/serial/serial.c

    rba95e8f rca97cad  
    9292}
    9393
     94static bool serial_received(ioport8_t *port)
     95{
     96   return (pio_read_8(port + 5) & 1) != 0;
     97}
     98
     99static uint8_t serial_read_8(ioport8_t *port)
     100{
     101        return pio_read_8(port);
     102}
     103
     104static bool is_transmit_empty(ioport8_t *port)
     105{
     106   return (pio_read_8(port + 5) & 0x20) != 0;
     107}
     108
     109static 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
    94117static int serial_read(device_t *dev, char *buf, size_t count)
    95118{
     
    111134}
    112135
     136static 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
    113143static int serial_write(device_t *dev, char *buf, size_t count)
    114144{
    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       
    116152        return 0;
    117153}
     
    150186                dev->parent_phone = 0;
    151187        }       
    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);
    175188}
    176189
     
    333346       
    334347        while (cont) { 
     348                fibril_mutex_lock(&data->mutex);
     349               
    335350                if (cont = serial_received(port)) {
    336351                        uint8_t val = serial_read_8(port);
    337352                        printf(NAME ": character %c read from %s.\n", val, dev->name);
    338353                       
    339                         fibril_mutex_lock(&data->mutex);
     354                       
    340355                        if (data->client_connected) {
    341356                                if (!buf_push_back(&(data->input_buffer), val)) {
     
    346361                        } else {
    347362                                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                        }                       
    350364                }
    351365               
     366                fibril_mutex_unlock(&data->mutex);     
     367               
     368                fibril_yield();         
    352369        }       
    353370}
Note: See TracChangeset for help on using the changeset viewer.