[RVB2601 Creative Application Development] GPIO Simulation SPI Serial Port
[Copy link]
SPI is a high-speed, full-duplex, synchronous communication bus, and only occupies four wires on the chip pins, saving the chip pins and saving space on the PCB layout, providing convenience. It is mainly used in EEPROM, FLASH, real-time clock, AD converter, and between digital signal processors and digital signal decoders.
When CPHA=0, it means that data is sampled at the first clock edge. When CPOL=1, it means that it is at a high level when idle, and it changes from a high level to a low level, and the first clock edge (falling edge) is sampled. When CPOL=0, it means that it is at a low level when idle, and it changes from a low level to a high level, and the first clock edge (rising edge) is sampled.
When CPHA=1, it means that data is sampled at the second clock edge. When CPOL=1, it means that it is at a high level when idle, and it changes from a high level to a low level and then to a high level, and the second clock edge (rising edge) is sampled. When CPOL=0, it means that it is at a low level when idle, and it changes from a low level to a high level and then to a low level, and the second clock edge (falling edge) is sampled.
Programming:
#ifndef __SOFTSPI_H_
#define __SOFTSPI_H_
//#include "stdint.h"
#include <stdio.h>
#include <string.h>
#include "drv/gpio_pin.h"
#include <drv/pin.h>
#ifndef LSBFIRST
#define LSBFIRST 0
#endif
#ifndef MSBFIRST
#define MSBFIRST 1
#endif
#define SOFTSPI_MODE0 0x00
#define SOFTSPI_MODE1 0x04
#define SOFTSPI_MODE2 0x08
#define SOFTSPI_MODE3 0x0C
#define SOFTSPI_MISO PA7
#define SOFTSPI_MOSI PA25
# define SOFTSPI_SCK PA4
void wait(uint32_t del);
void spi_pinmux_init();
void setBitOrder(uint8_t);
void setDataMode(uint8_t);
uint8_t transfer(uint8_t);
uint16_t transfer16(uint16_t data);
uint16_t read_temp(uint16_t data);
#endif
Testing Procedure:
#include "soft_spi.h"
spi_pinmux_init();
uint32_t t0;
while (1)
{
t0 = transfer16(0xffff);
printf("transfer16 test:%d\n\r",t0);
printf("temprature:%d \n\r",read_temp(0x7fff));
}
|