Untitled

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
3.4 kB
2
Indexable
Never
/*
 * flash.c
 *
 *  Created on: Jul 22, 2021
 *      Author: David
 */

#include "main.h"
#include "flash.h"
#define CTRL_DIS  0xF8
#define A_1_DIS   0x08
#define E_DIS     0x10
#define G_DIS     0x20
//#define W_DIS     0x8

#define A_1_EN    0xF0 //TODO corregir
#define E_EN      0xE8
#define G_EN      0xD8
//#define W_EN      0x70
#define delay()   asm("nop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\nnop")



volatile uint16_t data;
volatile uint16_t count;
volatile uint32_t address;

volatile uint8_t diskStatus;
volatile uint8_t bufReady;
volatile uint8_t readPos;
volatile uint8_t writePos;
volatile uint8_t buf[bufNum][bufSz];
volatile uint8_t *readPtr=buf[0];
volatile uint8_t *writePtr=buf[0];

static uint8_t status=0;

void setDataDir(bool input){
  GPIOA->MODER = input ? (GPIOA->MODER & 0xFFFF0000) : (GPIOA->MODER & 0xFFFF0000) | 0x5555;
}

void pulse(GPIO_TypeDef  *GPIOx, uint32_t pin){
  //delay();
  GPIOx->BSRR=pin;
  delay();
  GPIOx->BSRR=pin<<16;
}

uint8_t read(void){
  setDataDir(1);
  delay();
  OE_DATA_GPIO_Port->BSRR = OE_DATA_Pin<<16;
  pulse(LE_DATA_GPIO_Port, LE_DATA_Pin);
  uint8_t data = GPIOA->IDR & 0xFF;
  OE_DATA_GPIO_Port->BSRR = OE_DATA_Pin;
  delay();
  setDataDir(0);
  return data;
}

void flash_init(void){
  setDataDir(0);
  // All to 1 ( Disable state)
  LE_ADR0_GPIO_Port->BSRR=LE_ADR0_Pin<<16;
  LE_ADR1_GPIO_Port->BSRR=LE_ADR1_Pin<<16;
  LE_ADR2_GPIO_Port->BSRR=LE_ADR2_Pin<<16;
  LE_DATA_GPIO_Port->BSRR=LE_DATA_Pin<<16;
  OE_DATA_GPIO_Port->BSRR=OE_DATA_Pin;
  delay();
  GPIOA->ODR &= ~(0xFF);
  pulse(LE_ADR0_GPIO_Port, LE_ADR0_Pin);
  pulse(LE_ADR1_GPIO_Port, LE_ADR1_Pin);
  GPIOA->ODR |= CTRL_DIS;
  pulse(LE_ADR2_GPIO_Port, LE_ADR2_Pin);
}

void fillBuffer(void){
  count=0;
  readPtr = buf[0];
  while(count<bufSz){

    // Load address with A-1=0
    GPIOA->ODR = address & 0xFF;
    pulse(LE_ADR0_GPIO_Port, LE_ADR0_Pin);
    GPIOA->ODR = (address>>8) & 0xFF;
    pulse(LE_ADR1_GPIO_Port, LE_ADR1_Pin);
    GPIOA->ODR = ((address>>16) & 0xFF) | A_1_EN;
    pulse(LE_ADR2_GPIO_Port, LE_ADR2_Pin);

    // Enable CE
    GPIOA->ODR = ((address>>16) & 0xFF) | (A_1_EN & E_EN);
    pulse(LE_ADR2_GPIO_Port, LE_ADR2_Pin);

    // Enable G
    GPIOA->ODR = ((address>>16) & 0xFF) | (A_1_EN & E_EN & G_EN);
    pulse(LE_ADR2_GPIO_Port, LE_ADR2_Pin);

    // Read lower byte
    *readPtr++=read();

    // Deselect G
    GPIOA->ODR = ((address>>16) & 0xFF) | (A_1_EN & E_EN);
    pulse(LE_ADR2_GPIO_Port, LE_ADR2_Pin);

    // Deselect E
    GPIOA->ODR = ((address>>16) & 0xFF) | (A_1_EN);
    pulse(LE_ADR2_GPIO_Port, LE_ADR2_Pin);

    // Disable A-1
    GPIOA->ODR = ((address>>16) & 0xFF) | CTRL_DIS;
    pulse(LE_ADR2_GPIO_Port, LE_ADR2_Pin);

    // Enable E
    GPIOA->ODR = ((address>>16) & 0xFF) | E_EN;
    pulse(LE_ADR2_GPIO_Port, LE_ADR2_Pin);

    // Enable G
    GPIOA->ODR = ((address>>16) & 0xFF) | (E_EN & G_EN);
    pulse(LE_ADR2_GPIO_Port, LE_ADR2_Pin);

    // Read higher byte
    *readPtr++=read();

    // Deselect G
    GPIOA->ODR = ((address>>16) & 0xFF) | E_EN;
    pulse(LE_ADR2_GPIO_Port, LE_ADR2_Pin);

    // Deselect E
    GPIOA->ODR = ((address>>16) & 0xFF) | CTRL_DIS;
    pulse(LE_ADR2_GPIO_Port, LE_ADR2_Pin);

    address++;
    count+=2;
  }
}