You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.5 KiB
78 lines
2.5 KiB
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "main.h"
|
|
#include "usart.h"
|
|
#include "tim.h"
|
|
|
|
#define READ_CMD 0x80
|
|
#define SPI_TIMEOUT_VALUE 1000
|
|
#define SPI_CS_1() HAL_GPIO_WritePin(SPI_SEL_GPIO_Port, SPI_SEL_Pin, GPIO_PIN_SET) /* */
|
|
#define SPI_CS_0() HAL_GPIO_WritePin(SPI_SEL_GPIO_Port, SPI_SEL_Pin, GPIO_PIN_RESET) /* */
|
|
#define SPI_SCK_1() HAL_GPIO_WritePin(CLK_GPIO_Port, CLK_Pin, GPIO_PIN_SET) /* SCK = 1 */
|
|
#define SPI_SCK_0() HAL_GPIO_WritePin(CLK_GPIO_Port, CLK_Pin, GPIO_PIN_RESET) /* SCK = 0 */
|
|
#define SPI_MOSI_1() HAL_GPIO_WritePin(MOSI_GPIO_Port, MOSI_Pin, GPIO_PIN_SET) /* MOSI = 1 */
|
|
#define SPI_MOSI_0() HAL_GPIO_WritePin(MOSI_GPIO_Port, MOSI_Pin, GPIO_PIN_RESET) /* MOSI = 0 */
|
|
#define SPI_READ_MISO() HAL_GPIO_ReadPin(MISO_GPIO_Port, MISO_Pin) /* 读MISO口线状态 */
|
|
|
|
#define Dummy_Byte 0xFF // 读取时MISO发送的数据,可以为任意数据
|
|
|
|
#define spi_delay_us 5
|
|
#define spi_table_type_size 50
|
|
|
|
typedef enum
|
|
{
|
|
SPI_SEL_TYPE = 0,
|
|
SPI_SCK_TYPE,
|
|
SPI_MOSI_TYPE,
|
|
SPI_MISO_TYPE,
|
|
SPI_TYPE_NUMBER,
|
|
} SPI_TYPE;
|
|
|
|
uint8_t table[SPI_TYPE_NUMBER][spi_table_type_size] = {
|
|
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}, // sel
|
|
{1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // sck
|
|
{1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}, // mosi
|
|
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // miso
|
|
};
|
|
|
|
void sys_delay_us(uint32_t us)
|
|
{
|
|
uint16_t counter = 0;
|
|
__HAL_TIM_SET_COUNTER(&htim7, 0);
|
|
HAL_TIM_Base_Start(&htim7);
|
|
while (counter < us)
|
|
{
|
|
counter = __HAL_TIM_GET_COUNTER(&htim7);
|
|
}
|
|
HAL_TIM_Base_Stop(&htim7);
|
|
}
|
|
|
|
void dotable()
|
|
{
|
|
for (size_t i = 0; i < spi_table_type_size; i++)
|
|
{
|
|
|
|
table[0][i] ? SPI_CS_1() : SPI_CS_0();
|
|
table[1][i] ? SPI_SCK_1() : SPI_SCK_0();
|
|
table[2][i] ? SPI_MOSI_1() : SPI_MOSI_0();
|
|
sys_delay_us(spi_delay_us);
|
|
}
|
|
}
|
|
|
|
void user_main()
|
|
{
|
|
uint8_t temp;
|
|
|
|
SPI_CS_1(); // 片选
|
|
SPI_SCK_1(); // 时钟空闲高电平
|
|
SPI_MOSI_1(); // 数据线空闲高电平
|
|
|
|
while (1)
|
|
{
|
|
dotable();
|
|
// HAL_UART_Transmit(&huart1, &temp, 1, 100);
|
|
HAL_Delay(10);
|
|
}
|
|
}
|