/**
* Copyright (c) 2014 - 2018, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/** @file
* @defgroup nrf_adc_example main.c
* @{
* @ingroup nrf_adc_example
* @brief ADC Example Application main file.
*
* This file contains the source code for a sample application using ADC.
*
* @image html example_board_setup_a.jpg "Use board setup A for this example."
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "nrf.h"
#include "nrf_drv_saadc.h"
#include "nrf_drv_ppi.h"
#include "nrf_drv_timer.h"
#include "boards.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "app_util_platform.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_drv_twi.h"
#include "config.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
//--------------
//------------------------
#define NB_OF_BLOCKS 8192 //per block pages
#define NB_OF_PAGES 1048576
#define MAIN_AREA_SIZE 8192
#define SPARE_AREA_SIZE 128
#define Flash_RD_1_CMND 0x00
#define Flash_RD_2_CMND 0x30
#define Flash_RD_Id_CMND 0x90
#define Flash_RST_CMND 0xFF
#define Flash_PageProg_1_CMND 0x80
#define Flash_PageProg_2_CMND 0x10
#define Flash_RdSts_CMND 0x70
//Define WORM Connection
//------------Control I/O---------------
#define RB 10
#define RE 9
#define CE 8
#define CLE 7
#define ALE 6
#define WE 5
#define WP 4
//-----------I/O---------------
#define M1 12
#define M2 24
#define M3 23
#define M4 22
#define M5 12
#define M6 14
#define M7 15
#define M8 16
uint8_t markerCode;
uint8_t deviceCode;
uint8_t Thirdcycle;
uint8_t Forthcycle;
uint8_t Fifthcycle;
uint8_t pageSize;
uint8_t blockSize;
uint8_t spareSize;
uint8_t idData;
uint32_t page_num; // Total page number
uint16_t page_Location; // Location of Data in Page
uint8_t Temp_data[4096];
// Twi Slave Code
nrf_drv_twis_t instance = NRF_DRV_TWIS_INSTANCE(0);
void twis_event_handler(nrf_drv_twis_evt_t const * const p_event)
{
switch(p_event->type)
{
case TWIS_EVT_READ_REQ:
if(p_event->data.buf_req)
{
// Function size_t get_buffer_to_send(char const * * const rptr)
// needs to be implemented
const char * rptr;
size_t size = get_buffer_to_send(&rptr);
nrf_drv_twis_tx_prepare(&m_slave_inst, rptr, size);
}
break;
case TWIS_EVT_READ_DONE:
break;
case TWIS_EVT_WRITE_REQ:
if(p_event->data.buf_req)
{
// Function size_t get_buffer_to_write(char * * const wptr)
// needs to be implemented
char *wptr;
size_t size = get_buffer_to_write(&wptr);
nrf_drv_twis_rx_prepare(&m_slave_inst, wptr, size);
}
break;
case TWIS_EVT_WRITE_DONE:
// Function void mark_buffer_ready(size_t amount)
// needs to be implemented
mark_buffer_ready(p_event->data.rx_amount);
break;
default:
break;
}
}
typedef struct SNandInfo {
unsigned int pageSize;
unsigned int blockSize;
unsigned int spareSize;
unsigned int busWidth;
} nandInfo, *pnandInfo;
// Configure Data port as input
void config_dataport_in()
{
nrf_gpio_cfg_input(M1,NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_input(M2,NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_input(M3,NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_input(M4,NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_input(M5,NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_input(M6,NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_input(M7,NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_input(M8,NRF_GPIO_PIN_NOPULL);
}
// Configure Data port as output
void config_dataport_out()
{
nrf_gpio_cfg_output(M1);
nrf_gpio_cfg_output(M2);
nrf_gpio_cfg_output(M3);
nrf_gpio_cfg_output(M4);
nrf_gpio_cfg_output(M5);
nrf_gpio_cfg_output(M6);
nrf_gpio_cfg_output(M7);
nrf_gpio_cfg_output(M8);
}
// Send Data to Port
void data_out(uint8_t data)
{
char i,bit[8];
for(i=0;i<8;i++)
{
bit[i] =((data>>i)&0x01);
}
// SET data to port
nrf_gpio_pin_write(M1,bit[0]);
nrf_gpio_pin_write(M2,bit[1]);
nrf_gpio_pin_write(M3,bit[2]);
nrf_gpio_pin_write(M4,bit[3]);
nrf_gpio_pin_write(M5,bit[4]);
nrf_gpio_pin_write(M6,bit[5]);
nrf_gpio_pin_write(M7,bit[6]);
nrf_gpio_pin_write(M8,bit[7]);
}
uint8_t data_in(){
int data[8],sum,i;
data[0]=nrf_gpio_pin_read(M1);
data[1]=nrf_gpio_pin_read(M2);
data[2]=nrf_gpio_pin_read(M3);
data[3]=nrf_gpio_pin_read(M4);
data[4]=nrf_gpio_pin_read(M5);
data[5]=nrf_gpio_pin_read(M6);
data[6]=nrf_gpio_pin_read(M7);
data[7]=nrf_gpio_pin_read(M8);
for(i =7;i>=0;i--)
{
sum+=data[7];
}
return sum;
}
void hold(char a)
{
char i;
for(i=0;i<a;i++)
{
nrf_delay_us(1);
}
}
void mem_con() {
/*RB = P10;
RE = P09;
CE = P08;
CLE =P07;
ALE =P06;
WE = P05;
WP = P04;*/
}
void mem_data() {
/*M1 = P12;
M2 = P24;
M3 = P23;
M4 = P22;
M5 = P12;
M6 = P14;
M7 = P15;
M8 = P16;*/
}
//void flash_writeCommand(uint8_t cmndData);
//void flash_writeAddress(uint8_t addData);
void flash_inpDataStream(uint8_t inpData); //page datasheet33
void flash_Write(); // page: 21, 37,48
void flash_readStatus();
void flash_ReadId(pnandInfo pNandInfo);
void flash_Reset();
unsigned short flash_creatBadBlockTable();
uint8_t flash_nandRead();
static inline void flash_CE_Hi() {/*Set PIN Low*/ nrf_gpio_pin_set(CE);}
static inline void flash_CE_Lo() {/*Set PIN High*/ nrf_gpio_pin_clear(CE);}
static inline void flash_CLE_Hi() {/*Set PIN Low*/ nrf_gpio_pin_set(CLE);}
static inline void flash_CLE_Lo() {/*Set PIN Low*/ nrf_gpio_pin_clear(CLE);}
static inline void flash_WE_Hi() {/*Set PIN Low*/ nrf_gpio_pin_set(WE);}
static inline void flash_WE_Lo() {/*Set PIN Low*/ nrf_gpio_pin_clear(WE);}
static inline void flash_ALE_Hi() {/*Set PIN Low*/ nrf_gpio_pin_set(ALE);}
static inline void flash_ALE_Lo() {/*Set PIN Low*/ nrf_gpio_pin_clear(ALE);}
static inline void flash_RE_Hi() {/*Set PIN Low*/ nrf_gpio_pin_set(RE);}
static inline void flash_RE_Lo() {/*Set PIN Low*/ nrf_gpio_pin_clear(RE);}
static inline _Bool flash_RBread() {}
// ---------------Custom Functions----------------//
// creat low pulse at we pin
void pulse_we()
{
flash_ALE_Lo();
hold(10); // 10us delay
flash_ALE_Hi();
}
void pulse_re()
{
flash_RE_Lo();
hold(10); // 10us delay
flash_RE_Hi();
}
uint8_t flash_Read()
{
uint8_t ioRead;
pulse_re();
ioRead=data_in();
return ioRead;
}
void flash_writeCommand(uint8_t cmdData) //page datasheet32
{
flash_CLE_Hi();
flash_CE_Lo();
flash_ALE_Lo();
data_out(cmdData);
pulse_we();
flash_CE_Hi();
flash_CLE_Lo();
flash_ALE_Hi();
}
void flash_writeAddress(uint8_t addData)
{
flash_CLE_Lo();
flash_CE_Lo();
flash_ALE_Hi();
//Address_1----Col.Add1
data_out(addData);
pulse_we();
flash_ALE_Lo();
}
/**
* DataSheet Page 44
Read Chip ID Function
*/
void flash_ReadId(pnandInfo pNandInfo)
{
flash_writeCommand(Flash_RD_Id_CMND);
flash_writeAddress(0x00);
//if possible 10 ns Delay
hold(5);
config_dataport_in();
markerCode = flash_Read();
deviceCode = flash_Read();
Thirdcycle = flash_Read();
Forthcycle = flash_Read();
Fifthcycle = flash_Read();
// markerCode =
// deviceCode
/*pNandInfo->pageSize = 1024 << (idData & 0x03);
pNandInfo->blockSize = (64 * 1024) << ((idData & 0x30) >> 4);;
pNandInfo->spareSize = 128;
pNandInfo->busWidth = 8;*/
}
void flash_write(uint8_t data)
{
data_out(data);
pulse_we();
}
// page write function
void page_write(uint32_t row, uint16_t col)
{
unsigned char bytes[4],Cbytes[2];
uint16_t i;
config_dataport_out();
flash_writeCommand(0x80);
Cbytes[0] = (col >> 8) & 0xFF;
Cbytes[1] = col & 0xFF;
flash_writeAddress(Cbytes[0]);
flash_writeAddress(Cbytes[1]);
bytes[0] = (row >> 24) & 0xFF;
bytes[1] = (row >> 16) & 0xFF;
bytes[2] = (row >> 8) & 0xFF;
bytes[3] = row & 0xFF;
flash_writeAddress(bytes[1]);
flash_writeAddress(bytes[2]);
flash_writeAddress(bytes[3]);
for(i=0;i<4096;i++)
{
// data write
//flash_write(); // place variable for data
}
flash_writeCommand(0x10);
// wait for busy pin
flash_writeCommand(0x70);
// read port for write status
}
////-------------------------------
uint16_t i;
/**
* @brief Function for main application entry.
*/
int main(void)
{
uint32_t err_code = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(err_code);
NRF_LOG_DEFAULT_BACKENDS_INIT();
ret_code_t ret_code = nrf_pwr_mgmt_init();
APP_ERROR_CHECK(ret_code);
// Twi init
// Initialization
bool waitingRx = false;
nrf_drv_twis_init(&instance, NULL, NULL);
nrf_drv_twis_enable(&instance);
NRF_LOG_INFO("SAADC HAL simple example started.");
for(i=0;i<4096;i++)
{
Temp_data[i]=0xff;
}
while (1)
{
nrf_pwr_mgmt_run();
for(i=0;i<4096;i++)
{
NRF_LOG_INFO("Memory Data %i:-->%d",i,Temp_data[i]);
NRF_LOG_FLUSH();
nrf_delay_ms(100);
}
}
}
/** @} */
