feat: spi

This commit is contained in:
IOsetting 2021-12-31 00:59:59 +08:00
parent e35d3a28a5
commit 2f3888ae99
2 changed files with 33 additions and 3 deletions

View File

@ -43,9 +43,10 @@ typedef enum
SPI_ClockPreScaler_32or2 = 0x03,
} SPI_ClockPreScaler_t;
#define SPI_RxTxFinished() (SPSTAT & (0x01 << 7))
#define SPI_ClearInterrupt() SFR_RESET(SPSTAT, 7)
#define SPI_ClearWriteConflictInterrupt() SFR_RESET(SPSTAT, 6)
#define SPI_RxTxFinished() (SPSTAT & 0x80)
#define SPI_ClearInterrupt() SFR_SET(SPSTAT, 7)
#define SPI_ClearWriteConflictInterrupt() SFR_SET(SPSTAT, 6)
#define SPI_ClearInterrupts() (SPSTAT |= 0xC0)
#define SPI_IgnoreSlaveSelect(__STATE__) SFR_ASSIGN(SPCTL, 7, __STATE__)
#define SPI_SetEnableState(__STATE__) SFR_ASSIGN(SPCTL, 6, __STATE__)
@ -64,4 +65,6 @@ typedef enum
*/
#define SPI_SwitchPort(__ALTER_PORT__) (P_SW1 = P_SW1 & ~(0x03 << 2) | ((__ALTER_PORT__) << 2))
uint8_t SPI_TxRx(uint8_t dat);
#endif

27
src/fw_spi.c Normal file
View File

@ -0,0 +1,27 @@
// Copyright 2021 IOsetting <iosetting(at)outlook.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fw_spi.h"
#include "fw_tim.h"
#include "fw_sys.h"
uint8_t SPI_TxRx(uint8_t dat)
{
SPI_ClearInterrupts();
SPDAT = dat;
while (!SPI_RxTxFinished());
SPI_ClearInterrupts();
return SPDAT;
}