Project

General

Profile

Revision 1461

Added by Brad Neuman over 14 years ago

updated all the library code to have sensible _init behavior.
Almost all of the library components have a global variable which gets set after init and the functions inside will fail with an error code if init has not been called. Also, the init functions themselves check this variable and will bail out without doing any damage if that init has already been called

View differences:

i2c.c
43 43
#include "i2c.h"
44 44
#include "ring_buffer.h"
45 45

  
46
//NOTE: this is here just to provide colony error codes, it can be safely removed
47
// for use in another project
48
#include <dragonfly_lib.h>
49

  
46 50
/**
47 51
 * @defgroup i2c I2C 
48 52
 *
......
78 82
 */
79 83
#define I2C_BIT_RATE_DIVIDER 0x0C
80 84

  
85
unsigned char i2c_initd=0;
86

  
81 87
static int start_flag;
82 88

  
83 89
static fun_mrecv_t master_recv_function;
......
105 111
 * @return 0 for success, nonzero for failure
106 112
 **/
107 113
int i2c_init(char addr, fun_mrecv_t master_recv, fun_srecv_t slave_recv, fun_send_t slave_send) {
114

  
115
    if(i2c_initd)
116
      return ERROR_INIT_ALREADY_INITD;
117

  
118

  
108 119
    master_recv_function = master_recv;
109 120
    slave_recv_function = slave_recv;
110 121
    slave_send_function = slave_send;
......
122 133
     * global messages to be accepted */
123 134
    TWAR = (addr << 1) | 1;
124 135
  
136
    i2c_initd = 1;
125 137
    return 0;
126 138
}
127 139

  
......
141 153
int i2c_send(char dest, char *data, unsigned int bytes) {
142 154
    int i;
143 155

  
156
    if(!i2c_initd)
157
      return ERROR_LIBRARY_NOT_INITD;
158

  
159

  
144 160
    /* adding data to be sent to ring buffers is not atomic,
145 161
     * so disable interrupts */
146 162
    cli();
......
180 196
 * @return 0 for success, nonzero for failure
181 197
 **/ 
182 198
int i2c_request(char dest) {
199
    if(!i2c_initd)
200
      return ERROR_LIBRARY_NOT_INITD;
201

  
183 202
    if(RING_BUFFER_FULL(i2c_write_buff))
184 203
        return -1;
185 204
  

Also available in: Unified diff