Guest User

Untitled

a guest
Jun 27th, 2019
198
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define CS_HIGH         PORTB |= (1 << 2)
  2. #define CS_LOW          PORTB &= ~(1 << 2)
  3.  
  4. uint8_t SPI_SendByte(uint8_t byte);
  5. void AT45DB161_Init();
  6. void AT45DB161_Read_ID(uint8_t *MID, uint8_t *DID);
  7. uint8_t AT45DB161_Read_Status();
  8. void AT45DB161_Read_Data(uint16_t page, uint16_t addr, uint32_t length, uint8_t *out);
  9. void AT45DB161_PageProgram(uint16_t page, uint8_t *dat, uint16_t length);
  10.  
  11. uint8_t SPI_SendByte(uint8_t byte){
  12.     SPDR = byte;
  13.     while (!(SPSR & (1 << SPIF)));
  14.     return SPDR;
  15. }
  16.  
  17. void AT45DB161_Init(){
  18.      DDRB |= (1 << 3) | (1 << 5) | (1 << 2);
  19.      SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR0);
  20.      CS_HIGH;
  21. }
  22.  
  23. void AT45DB161_Read_ID(uint8_t *MID, uint8_t *DID){
  24.     CS_LOW;
  25.     SPI_SendByte(0x9F);
  26.     *MID = SPI_SendByte(0x00);
  27.     *DID = SPI_SendByte(0x00);
  28.     SPI_SendByte(0x00);
  29.     SPI_SendByte(0x00);
  30.     SPI_SendByte(0x00);
  31.     SPI_SendByte(0x00);
  32.     CS_HIGH;
  33. }
  34.  
  35. uint8_t AT45DB161_Read_Status(){
  36.     uint8_t temp;
  37.     CS_LOW;
  38.     SPI_SendByte(0xD7);
  39.     temp = SPI_SendByte(0x00);
  40.     CS_HIGH;
  41.     return temp;
  42. }
  43.  
  44.  
  45. void AT45DB161_Read_Data(uint16_t page, uint16_t addr, uint32_t length, uint8_t *out){
  46.     uint32_t i;
  47.     uint8_t temp;
  48.     do {
  49.         temp = AT45DB161_Read_Status();
  50.     } while (!(temp & 0x80));
  51.     if (temp & 0x01){   //512
  52.         i = ((page << 9) | (addr & 0x1FF));
  53.     } else {            //528
  54.         i = ((page << 10) | (addr & 0x3FF));
  55.     }
  56.     CS_LOW;
  57.     SPI_SendByte(0x0B);
  58.     SPI_SendByte(i >> 16);
  59.     SPI_SendByte(i >> 8);
  60.     SPI_SendByte(i);
  61.     SPI_SendByte(0x00);
  62.     for (i = 0; i < length; i++){
  63.         out[i] = SPI_SendByte(0xFF);
  64.     }
  65.     CS_HIGH;
  66. }
  67.  
  68. void AT45DB161_PageProgram(uint16_t page, uint8_t *dat, uint16_t length){
  69.     uint16_t i;
  70.     uint8_t temp;
  71.     temp = AT45DB161_Read_Status();
  72.     CS_LOW;
  73.     SPI_SendByte(0x84);
  74.     SPI_SendByte(0x00);
  75.     SPI_SendByte(0x00);
  76.     SPI_SendByte(0x00);
  77.     for (i = 0; i < length; i++){
  78.         SPI_SendByte(dat[i]);
  79.     }
  80.     CS_HIGH;
  81.     CS_LOW;
  82.     SPI_SendByte(0x83);
  83.     if (temp & 0x01){   //512
  84.         SPI_SendByte((uint8_t)(page >> 7));
  85.         SPI_SendByte((uint8_t)((page & 0x7F) << 1));
  86.         SPI_SendByte(0x00);
  87.     } else {            //528
  88.         SPI_SendByte((uint8_t)(page >> 6));
  89.         SPI_SendByte((uint8_t)((page & 0x3F) << 2));
  90.         SPI_SendByte(0x00);
  91.     }
  92.     CS_HIGH;
  93.     while (!(AT45DB161_Read_Status() & 0x80));
  94. }
RAW Paste Data