12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- // This file is part of parrocchetto.
- // parrocchetto is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- // parrocchetto is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- // You should have received a copy of the GNU General Public License
- // along with parrocchetto. If not, see <http://www.gnu.org/licenses/>.
- #include "spi1.h"
- #include <xc.h>
- #include <stdint.h>
- void spi1_init(void)
- {
- IFS0bits.SPI1IF = 0; // Clear the Interrupt flag
- IEC0bits.SPI1IE = 0; // Disable the interrupt
- SPI1CON1bits.DISSCK = 0; // Internal serial clock is enabled
- SPI1CON1bits.DISSDO = 0; // SDOx pin is controlled by the module
- SPI1CON1bits.MODE16 = 0; // Communication is 8 bit
- SPI1CON1bits.MSTEN = 1; // Master mode enabled
- SPI1CON1bits.SMP = 0; // Input data is sampled at the middle of data output time
- SPI1CON1bits.CKE = 0; // Serial output data changes on transition from
- // Idle clock state to active clock state
- SPI1CON1bits.CKP = 0; // Idle state for clock is a low level;
- // active state is a high level
- SPI1CON1bits.PPRE = 3; // Primary clock prescale 1:1
- SPI1CON1bits.SPRE = 6; // Secondary clock prescale 2:1
-
- // PPS
- RPINR20bits.SDI1R = 6; // SDI1: RP6
- RPOR3bits.RP7R = 9; // ~SS1: RP7
- RPOR4bits.RP8R = 7; // SDO1: RP8
- RPOR4bits.RP9R = 8; // SCK1: RP9
- spi1_end(); // Put ~SS1 high
- SPI1STATbits.SPIEN = 1;
- }
- inline uint8_t spi1_write(uint8_t x)
- {
- SPI1BUF = x;
- while(!SPI1STATbits.SPIRBF);
- //asm("nop");
- //asm("nop");
- //asm("nop");
- //asm("nop");
- return SPI1BUF;
- }
- inline void spi1_writebuf(const uint8_t* buf, uint16_t buf_size)
- {
- uint16_t i;
- for(i = 0; i < buf_size; ++i)
- spi1_write(buf[i]);
- }
- inline void spi1_readbuf(uint8_t* buf, uint16_t buf_size)
- {
- uint16_t i;
- for(i = 0; i < buf_size; ++i)
- buf[i] = spi1_read();
- }
|