Skip to content

Commit

Permalink
SPI和AT45DB161D的驱动代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan committed Dec 11, 2018
0 parents commit bf6eec4
Show file tree
Hide file tree
Showing 38 changed files with 7,678 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DCCP_HCS12X_FreeRTOS_Data/
out/
Binary file added AT45DB161D-Drv.mcp
Binary file not shown.
Binary file added AT45DB161D-Drv_Data/CWSettingsWindows.stg
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added AT45DB161D-Drv_Data/Standard/ObjectCode/main.c.o
Binary file not shown.
Binary file added AT45DB161D-Drv_Data/Standard/ObjectCode/mcu.c.o
Binary file not shown.
Binary file not shown.
20 changes: 20 additions & 0 deletions C_Layout.hwl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
OPEN source 0 0 60 39
Source < attributes MARKS off
OPEN assembly 60 0 40 31
Assembly < attributes ADR on,CODE off,ABSADR on,SYMB off,TOPPC 0xF88C
OPEN procedure 0 39 60 17
Procedure < attributes VALUES on,TYPES off
OPEN register 60 31 40 25
Register < attributes FORMAT AUTO,COMPLEMENT None
OPEN memory 60 56 40 22
Memory < attributes FORMAT hex,COMPLEMENT None,WORD 1,ASC on,ADR on,ADDRESS 0x80
OPEN data 0 56 60 22
Data:1 < attributes SCOPE global,COMPLEMENT None,FORMAT Symb,MODE automatic,UPDATERATE 10,NAMEWIDTH 16
OPEN data 0 78 60 22
Data:2 < attributes SCOPE local,COMPLEMENT None,FORMAT Symb,MODE automatic,UPDATERATE 10,NAMEWIDTH 16
OPEN command 60 78 40 22
Command < attributes CACHESIZE 1000
bckcolor 50331647
font 'Courier New' 9 BLACK
AUTOSIZE on
ACTIVATE Data:2 Command Procedure Data:1 Source Register Assembly Memory
Binary file added Default.mem
Binary file not shown.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# AT45DB161D-Drv
163 changes: 163 additions & 0 deletions Sources/AT45DB161D.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#include "AT45DB161D.h"

#define AT45_TEST (1)

#if (AT45_TEST)
static uint8_t at45_status_reg = 0;
static uint8_t test_buf1[AT_PAGE_SIZE] = {0};
static uint8_t test_buf2[AT_PAGE_SIZE] = {0};
static uint8_t test_buf3[AT_PAGE_SIZE] = {0};
#endif

static SPI_PT * const port = (SPI_PT*)SPI1_BASE;

static SPI_PT const SPI_PortConfig = {
0x50,
0x00,
0x00,
0,
0,
0
};

void AT45_init(void)
{
/* buzzer */
DDRK_DDRK1 = 1;
PORTK_PK1 = 0;

/* CS */
DDRP_DDRP3 = 1;
PTP_PTP3 = 1;

/* /WP */
DDRP_DDRP4 = 1;
PTP_PTP4 = 1;

/* /RESET */
DDRP_DDRP5 = 1;
PTP_PTP5 = 1;

SPI_Init(port, (SPI_PT*const)&SPI_PortConfig);
}

void AT45_Writebyte(uint8_t data)
{
SPI_SendByte(port, data);
(void)SPI_RecvByte(port);
}
uint8_t AT45_ReadByte(void)
{
SPI_SendByte(port, 0xFF);
return SPI_RecvByte(port);
}

uint8_t AT45_ReadStatus(void)
{
AT_CS_ASSERT
AT45_Writebyte(AT_STATUS_REG_READ);
at45_status_reg = AT45_ReadByte();
AT_CS_DEASSERT
return (at45_status_reg);
}

uint8_t AT45_IsBusy(void)
{
return ((AT45_ReadStatus() & 0x80) == 0);
}

void AT45_ReadFlash(uint16_t page, uint16_t offset, uint16_t sizes, uint8_t *buf)
{
uint32_t addr = (((uint32_t)page) << 10) + offset;

while (AT45_IsBusy());

AT_CS_ASSERT
AT45_Writebyte(AT_CONTINUE_ARRAY_READ);
AT45_Writebyte((uint8_t)(addr >> 16));
AT45_Writebyte((uint8_t)(addr >> 8));
AT45_Writebyte((uint8_t)(addr >> 0));
AT45_Writebyte(0);

while (sizes)
{
*buf = AT45_ReadByte();
buf += 1;
sizes -= 1;
}
AT_CS_DEASSERT
}
void AT45_ReadBuffer1(uint16_t offset, uint16_t sizes, uint8_t *buf)
{
while (AT45_IsBusy());

AT_CS_ASSERT
AT45_Writebyte(AT_BUFFER1_READ);
AT45_Writebyte(0);
AT45_Writebyte((uint8_t)(offset >> 8));
AT45_Writebyte((uint8_t)(offset >> 0));
AT45_Writebyte(0);

while (sizes)
{
*buf = AT45_ReadByte();
buf += 1;
sizes -= 1;
}
AT_CS_DEASSERT
}
void AT45_WriteBuffer1(uint16_t offset, uint16_t sizes, uint8_t *buf)
{
while (AT45_IsBusy());

AT_CS_ASSERT
AT45_Writebyte(AT_BUFFER1_WRITE);
AT45_Writebyte(0);
AT45_Writebyte((uint8_t)(offset >> 8));
AT45_Writebyte((uint8_t)(offset >> 0));

while (sizes)
{
AT45_Writebyte(*buf);
buf += 1;
sizes -= 1;
}
AT_CS_DEASSERT
}
void AT45_Buffer1ToPage(uint16_t page)
{
while (AT45_IsBusy());

AT_CS_ASSERT
AT45_Writebyte(AT_BUFFER1_TO_PAGE_WITH_ERASE);
AT45_Writebyte((uint8_t)(page >> 6));
AT45_Writebyte((uint8_t)(page << 2));
AT45_Writebyte(0);
AT_CS_DEASSERT
}
void AT45_PageToBuffer1(uint16_t page)
{
while (AT45_IsBusy());

AT_CS_ASSERT
AT45_Writebyte(AT_PAGE_TO_BUFFER1_TRANSFER);
AT45_Writebyte((uint8_t)(page >> 6));
AT45_Writebyte((uint8_t)(page << 2));
AT45_Writebyte(0);
AT_CS_DEASSERT
}

void AT45_Test(void)
{
int i;
while (AT45_IsBusy());
for (i = 0; i < AT_PAGE_SIZE; i++)
{
test_buf1[i] = (uint8_t)i;
}
AT45_WriteBuffer1(0, AT_PAGE_SIZE, test_buf1);
AT45_Buffer1ToPage(0);
AT45_PageToBuffer1(0);
AT45_ReadBuffer1(0, AT_PAGE_SIZE, test_buf2);
AT45_ReadFlash(0, 0, AT_PAGE_SIZE, test_buf3);
}
22 changes: 22 additions & 0 deletions Sources/AT45DB161D.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef AT45DB161D_H
#define AT45DB161D_H
#include "SPI_Drv.h"

#define AT_PAGE_SIZE (528)

#define AT_CS_ASSERT {PTP_PTP3 = 0;}
#define AT_CS_DEASSERT {PTP_PTP3 = 1;}

#define AT_STATUS_REG_READ (0xD7)
#define AT_CONTINUE_ARRAY_READ (0x0B)
#define AT_BUFFER1_READ (0xD4)
#define AT_BUFFER1_WRITE (0x84)
#define AT_BUFFER1_TO_PAGE_WITH_ERASE (0x83)
#define AT_PAGE_TO_BUFFER1_TRANSFER (0x53)

void AT45_init(void);
uint8_t AT45_IsBusy(void);
void AT45_ReadFlash(uint16_t page, uint16_t offset, uint16_t sizes, uint8_t *out);
void AT45_ReadBuffer1(uint16_t offset, uint16_t sizes, uint8_t *buf);

#endif /* AT45DB161D_H */
42 changes: 42 additions & 0 deletions Sources/SPI_Drv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "SPI_Drv.h"

void SPI_Init(SPI_PT *port, SPI_PT *pConf)
{
port->CR1 = pConf->CR1;
port->CR2 = pConf->CR2;
port->BR = pConf->BR;
}

void SPI_SendByte(SPI_PT *port, uint8_t data)
{
while (!(port->SR & (0x01<<5)));
port->DRL = data;
}

void SPI_SendData(SPI_PT *port, uint8_t *data, uint16_t len)
{
uint16_t i;

for (i = 0; i < len; i++)
{
while (!(port->SR & (0x01<<5)));
port->DRL = data[i];
}
}

uint8_t SPI_RecvByte(SPI_PT *port)
{
while (!(port->SR & (0x01<<7)));
return port->DRL;
}

void SPI_RecvData(SPI_PT *port, uint8_t *data, uint16_t len)
{
uint16_t i;

for (i = 0; i < len; i++)
{
while (!(port->SR & (0x01<<7)));
data[i] = port->DRL;
}
}
41 changes: 41 additions & 0 deletions Sources/SPI_Drv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef SPI_DRV_H
#define SPI_DRV_H
#include "mc9s12xep100.h"

#define SPI0_BASE (0x00D8)
#define SPI1_BASE (0x00F0)
#define SPI2_BASE (0x00F8)

typedef unsigned char uint8_t;
typedef unsigned int uint16_t;
typedef unsigned long uint32_t;

typedef struct spi_pt SPI_PT;

typedef struct spi_pt
{
uint8_t CR1;
uint8_t CR2;
uint8_t BR;
uint8_t SR;
uint8_t DRH;
uint8_t DRL;
uint8_t RSVD1;
uint8_t RSVD2;
};

enum
{
SPI_PORT_0,
SPI_PORT_1,
SPI_PORT_2,
SPI_PORT_NUM
};

void SPI_Init(SPI_PT *port, SPI_PT *pConf);
void SPI_SendByte(SPI_PT *port, uint8_t data);
void SPI_SendData(SPI_PT *port, uint8_t *data, uint16_t len);
uint8_t SPI_RecvByte(SPI_PT *port);
void SPI_RecvData(SPI_PT *port, uint8_t *data, uint16_t len);

#endif /* SPI_DRV_H */
Loading

0 comments on commit bf6eec4

Please sign in to comment.