00001 /* 00002 00003 Copyright (c) 2010, Embedded Adventures, www.embeddedadventures.com 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions are met: 00008 00009 - Redistributions of source code must retain the above copyright notice, 00010 this list of conditions and the following disclaimer. 00011 00012 - Redistributions in binary form must reproduce the above copyright 00013 notice, this list of conditions and the following disclaimer in the 00014 documentation and/or other materials provided with the distribution. 00015 00016 - Neither the name of Embedded Adventures nor the names of its contributors 00017 may be used to endorse or promote products derived from this software 00018 without specific prior written permission. 00019 00020 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00024 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 00030 THE POSSIBILITY OF SUCH DAMAGE. 00031 00032 Contact us at admin@embeddedadventures.com 00033 00034 */ 00035 00036 00037 #include "spi_hw.h" 00038 #include "pic_serial.h" 00039 00040 void spi_hw_setup_io() { 00041 00042 make_output(PORTC, 5); 00043 make_input(PORTC, 4); 00044 00045 #ifdef SPI_HW_MASTER_MODE 00046 make_output(PORTC, 3); 00047 clear_bit(sspcon1, 3); 00048 clear_bit(sspcon1, 2); 00049 #ifdef SPI_HW_MASTER_CLOCK_TMR2_DIV_2 00050 set_bit(sspcon1, 1); 00051 set_bit(sspcon1, 0); 00052 #endif 00053 #ifdef SPI_HW_MASTER_CLOCK_FOSC_DIV_64 00054 set_bit (sspcon1, 1); 00055 clear_bit(sspcon1, 0); 00056 #endif 00057 #ifdef SPI_HW_MASTER_CLOCK_FOSC_DIV_16 00058 clear_bit(sspcon1, 1); 00059 set_bit (sspcon1, 0); 00060 #endif 00061 #ifdef SPI_HW_MASTER_CLOCK_FOSC_DIV_4 00062 clear_bit(sspcon1, 1); 00063 clear_bit(sspcon1, 0); 00064 #endif 00065 #else 00066 // Slave mode 00067 make_input(PORTC, 3); // sck 00068 clear_bit(sspcon1, 3); 00069 set_bit (sspcon1, 2); 00070 clear_bit(sspcon1, 1); 00071 #ifdef SPI_HW_USE_SS 00072 make_input(PORTA, 5); // SS 00073 clear_bit(sspcon1, 0); 00074 #else 00075 set_bit(sspcon1, 0); 00076 #endif 00077 #endif 00078 00079 } 00080 00081 00082 void spi_hw_init() { 00083 // for 0,0 mode 00084 clear_bit(sspcon1, CKP); 00085 00086 set_bit(sspstat, CKE); 00087 set_bit(sspstat, SMP); // from tim 00088 set_bit(sspcon1, SSPEN); // enable mssp serial port 00089 } 00090 00091 void spi_hw_transmit(uns8 data) { 00092 00093 clear_bit(pir1, SSPIF); 00094 // Try and send, if we have something already sending, try again 00095 do { 00096 clear_bit(sspcon1, WCOL); 00097 sspbuf = data; 00098 } while (test_bit(sspcon1, WCOL)); 00099 00100 // Wait here while we transmit 00101 // Note that we will hang here if we don't have SPI setup properly 00102 // or we're running under the simulator 00103 while (!test_bit(pir1, SSPIF)) {} 00104 } 00105 00106 uns8 spi_hw_receive() { 00107 00108 spi_hw_transmit(0x00); // dummy transmit to get something back 00109 return sspbuf; 00110 00111 } 00112