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:

spi.c
8 8
#include <avr/interrupt.h>
9 9
#include "spi.h"
10 10

  
11
unsigned char spi_initd=0;
11 12

  
12 13
static volatile char spi_bytes; /* number of bytes to read */
13 14
static spi_fun_recv_t spi_recv_func; /* byte handler */
......
18 19
* 
19 20
* @param recv_func The function to be called for each byte of data received.
20 21
* @param recv_complete_func  The function to be called at the end of a complete transmission.
22
*
23
* @return 0 if init succesfull, an error code otherwise
21 24
*/
22
void spi_init (spi_fun_recv_t recv_func, spi_fun_recv_complete_t recv_complete_func)
25
int spi_init (spi_fun_recv_t recv_func, spi_fun_recv_complete_t recv_complete_func)
23 26
{
27
    if(spi_initd) {
28
      return ERROR_INIT_ALREADY_INITD;
29
    }
30

  
24 31
    /*  Enable Interrupt, Enable SPI Module, MSB First, Master Mode, Clock div = 64 */
25 32
    SPCR = _BV(SPE) | _BV(SPIE) /*| _BV(DORD)*/ | _BV(MSTR) | _BV(SPR1) | _BV(SPR0);
26 33
    SPSR = _BV(SPI2X); 
......
37 44
    spi_recv_complete_func = recv_complete_func;
38 45
    spi_bytes = 0;
39 46
    //usb_puts("\tspi.c Debug: SPI INITIALIZED\n");
47

  
48
    spi_initd=1;
49
    return 0;
40 50
}
41 51

  
42 52
/** 
......
44 54
* 
45 55
* @param bytes The number of bytes to be transferred.
46 56
**/
47
void spi_transfer(char bytes)
57
int spi_transfer(char bytes)
48 58
{
59
    if(!spi_initd)
60
      return ERROR_LIBRARY_NOT_INITD;
61

  
49 62
    spi_bytes = bytes;
50 63
    PORTB &= ~SS; /* Set SS low to initiate transmission */
51 64
    SPDR = 0xff; /* Initiate data transmision */
65

  
66
    return 0;
52 67
}
53 68

  
54 69
ISR(SIG_SPI) 

Also available in: Unified diff