0

我正在使用带有 MPLABX/MCC 的 PIC18f46k42 微控制器 XC8 2.3。我的目标是使用单个 SPI 硬件外设与使用各种 GPIO 引脚作为芯片选择的多个设备进行接口。我有一个 LCD 和一个 SD 卡,它们都需要与 MCU 通信(obv 不是同时)。我的问题是当我尝试更改 SPI 硬件配置寄存器以在设备之间切换时。所以我尝试减少问题并得出了这个结论:

如果我只使用一种 spi 配置来驱动屏幕,它就可以工作。但是,如果我尝试关闭该 spi 连接,然后重新打开同一个 spi 连接,则屏幕不起作用。我相信问题出在 mcc 生成的 SPI1_Open() 上。

spi1.h:

#ifndef SPI1_MASTER_H
#define SPI1_MASTER_H

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

/* SPI interfaces */
typedef enum { 
    SPI1_DEFAULT
} spi1_modes_t;

void SPI1_Initialize(void);
bool SPI1_Open(spi1_modes_t spi1UniqueConfiguration);
void SPI1_Close(void);
uint8_t SPI1_ExchangeByte(uint8_t data);
void SPI1_ExchangeBlock(void *block, size_t blockSize);
void SPI1_WriteBlock(void *block, size_t blockSize);
void SPI1_ReadBlock(void *block, size_t blockSize);
void SPI1_WriteByte(uint8_t byte);
uint8_t SPI1_ReadByte(void);

#endif //SPI1_H

spi1.c:

#include "spi1.h"
#include <xc.h>

typedef struct { 
    uint8_t con0; 
    uint8_t con1; 
    uint8_t con2; 
    uint8_t baud; 
    uint8_t operation;
} spi1_configuration_t;


//con0 == SPIxCON0, con1 == SPIxCON1, con2 == SPIxCON2, baud == SPIxBAUD, operation == Master/Slave
static const spi1_configuration_t spi1_configuration[] = {   
    { 0x3, 0x60, 0x2, 0x3, 0 }
};

void SPI1_Initialize(void)
{
    //EN disabled; LSBF MSb first; MST bus master; BMODE every byte; 
    SPI1CON0 = 0x03;
    //SMP Middle; CKE Active to idle; CKP Idle:High, Active:Low; FST disabled; SSP active high; SDIP active high; SDOP active high; 
    SPI1CON1 = 0x60;
    //SSET disabled; TXR required for a transfer; RXR data is not stored in the FIFO; 
    SPI1CON2 = 0x02;
    //CLKSEL FOSC; 
    SPI1CLK = 0x00;
    //BAUD 3; 
    SPI1BAUD = 0x03;
    TRISCbits.TRISC1 = 0;
}

bool SPI1_Open(spi1_modes_t spi1UniqueConfiguration)
{
    if(!SPI1CON0bits.EN)
    {
        SPI1CON0 = spi1_configuration[spi1UniqueConfiguration].con0;
        SPI1CON1 = spi1_configuration[spi1UniqueConfiguration].con1;
        SPI1CON2 = spi1_configuration[spi1UniqueConfiguration].con2 | (_SPI1CON2_SPI1RXR_MASK | _SPI1CON2_SPI1TXR_MASK);
        SPI1CLK  = 0x00;
        SPI1BAUD = spi1_configuration[spi1UniqueConfiguration].baud;        
        TRISCbits.TRISC1 = spi1_configuration[spi1UniqueConfiguration].operation;
        SPI1CON0bits.EN = 1;
        return true;
    }
    return false;
}

void SPI1_Close(void)
{
    SPI1CON0bits.EN = 0;
}

uint8_t SPI1_ExchangeByte(uint8_t data)
{
    SPI1TCNTL = 1;
    SPI1TXB = data;
    while(!PIR2bits.SPI1RXIF);
    return SPI1RXB;
}

void SPI1_ExchangeBlock(void *block, size_t blockSize)
{
    uint8_t *data = block;
    while(blockSize--)
    {
        SPI1TCNTL = 1;
        SPI1TXB = *data;
        while(!PIR2bits.SPI1RXIF);
        *data++ = SPI1RXB;
    }
}

// Half Duplex SPI Functions
void SPI1_WriteBlock(void *block, size_t blockSize)
{
    uint8_t *data = block;
    while(blockSize--)
    {
        SPI1_ExchangeByte(*data++);
    }
}

void SPI1_ReadBlock(void *block, size_t blockSize)
{
    uint8_t *data = block;
    while(blockSize--)
    {
        *data++ = SPI1_ExchangeByte(0);
    }
}

void SPI1_WriteByte(uint8_t byte)
{
    SPI1TXB = byte;
}

uint8_t SPI1_ReadByte(void)
{
    return SPI1RXB;
}

主.c:

#include "mcc_generated_files/mcc.h"
#include "ST7735.h"

#define led0 PORTAbits.RA7
#define led1 PORTAbits.RA6
#define led2 PORTAbits.RA5
#define led3 PORTAbits.RA4

void main(void)
{
    // Initialize the device
    SYSTEM_Initialize();
    
    led0 = SPI1_Open(SPI1_DEFAULT);
    led0 = SPI1CON0bits.EN;
    
    LCD_RESET = 1;
    delay_us(500);
    LCD_RESET = 0;
    delay_us(500);
    LCD_RESET = 1;
    delay_us(500);
    
    __delay_ms(1000);
    
    ST7735_initR();
    ST7735_fillScreen(ST7735_BLACK);

    while (1)
    {
        // Add your application code
    }
}

我也可以包括 ST7735.h/ST7735.c 中的内容,但我确信它们很好,因为当 spi1 配置正常时它们工作得很好。不良行为仅在我尝试切换 spi 配置时才开始,我必须这样做才能不可避免地在多个设备之间切换。在上述代码中,SPI 默认为 EN=0。我可以包含 SPI 默认为 EN=1 的版本,并使用 SPI1_Close() 将其关闭。但是两者的行为相同,并且此版本稍微简单一些。我非常感谢您的任何意见。

4

1 回答 1

0

我自己解决了这个问题。

这是有问题的功能:

bool SPI1_Open(spi1_modes_t spi1UniqueConfiguration)
{
    if(!SPI1CON0bits.EN)
    {
        SPI1CON0 = spi1_configuration[spi1UniqueConfiguration].con0;
        SPI1CON1 = spi1_configuration[spi1UniqueConfiguration].con1;
        SPI1CON2 = spi1_configuration[spi1UniqueConfiguration].con2 | (_SPI1CON2_SPI1RXR_MASK | _SPI1CON2_SPI1TXR_MASK);
        SPI1CLK  = 0x00;
        SPI1BAUD = spi1_configuration[spi1UniqueConfiguration].baud;        
        TRISCbits.TRISC1 = spi1_configuration[spi1UniqueConfiguration].operation;
        SPI1CON0bits.EN = 1;
        return true;
    }
    return false;
}

该行:

SPI1CON2 = spi1_configuration[spi1UniqueConfiguration].con2 | (_SPI1CON2_SPI1RXR_MASK | _SPI1CON2_SPI1TXR_MASK);

应该只是:

SPI1CON2 = spi1_configuration[spi1UniqueConfiguration].con2;

这在查看初始化函数时变得很明显,该函数在默认状态启用时工作(即 SPI1CON0 = 0x83):

void SPI1_Initialize(void)
{
    //EN disabled; LSBF MSb first; MST bus master; BMODE every byte; 
    SPI1CON0 = 0x03;
    //SMP Middle; CKE Active to idle; CKP Idle:High, Active:Low; FST disabled; SSP active high; SDIP active high; SDOP active high; 
    SPI1CON1 = 0x60;
    //SSET disabled; TXR required for a transfer; RXR data is not stored in the FIFO; 
    SPI1CON2 = 0x02;
    //CLKSEL FOSC; 
    SPI1CLK = 0x00;
    //BAUD 3; 
    SPI1BAUD = 0x03;
    TRISCbits.TRISC1 = 0;
} 

这个正常工作的函数不会用 (_SPI1CON2_SPI1RXR_MASK | _SPI1CON2_SPI1TXR_MASK) “或” SPI1CON2。

测试时,它的行为正确。归功于 Oleg Mazurov,他的评论让我重新检查了 mcc 生成的代码。虽然 mcc 生成的代码以前从未让我失望过,但如果您使用 mcc 做任何足够先进的事情,我建议在假设它正常工作之前仔细检查它。

于 2020-10-25T20:51:59.133 回答