N25Q128.c
136 lines
| 4.1 KiB
| text/x-c
|
CLexer
r53 | /*------------------------------------------------------------------------------ | |||
-- This file is a part of the libuc, microcontroler library | ||||
-- Copyright (C) 2013, Alexis Jeandet | ||||
-- | ||||
-- This program is free software; you can redistribute it and/or modify | ||||
-- it under the terms of the GNU General Public License as published by | ||||
-- the Free Software Foundation; either version 3 of the License, or | ||||
-- (at your option) any later version. | ||||
-- | ||||
-- This program is distributed in the hope that it will be useful, | ||||
-- but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
-- GNU General Public License for more details. | ||||
-- | ||||
-- You should have received a copy of the GNU General Public License | ||||
-- along with this program; if not, write to the Free Software | ||||
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||||
------------------------------------------------------------------------------- | ||||
-- Author : Alexis Jeandet | ||||
|
r64 | -- Mail : alexis.jeandet@member.fsf.org | ||
r53 | -------------------------------------------------------------------------------*/ | |||
#include <N25Q128.h> | ||||
#include <core.h> | ||||
#include <stdio.h> | ||||
void eepromN25Q128open(eepromN25Q128Dev *dev, spi_t spidev, void (*select)(int), void (*writeprotect)(int), void (*holdreset)(int)) | ||||
{ | ||||
dev->spidev = spidev; | ||||
dev->holdreset = holdreset; | ||||
dev->select = select; | ||||
dev->writeprotect = writeprotect; | ||||
} | ||||
void eepromN25Q128pagewrite(eepromN25Q128Dev* dev,uint32_t address,unsigned char* page) | ||||
{ | ||||
int cmdcnt=1,datacnt=8; | ||||
} | ||||
void eepromN25Q128pageread(eepromN25Q128Dev* dev,uint32_t address,unsigned char* page) | ||||
{ | ||||
int cmdcnt=1,datacnt=8; | ||||
} | ||||
void eepromN25Q128bytewrite(eepromN25Q128Dev* dev,uint32_t address,unsigned char data) | ||||
{ | ||||
int cmdcnt=1,datacnt=1; | ||||
} | ||||
unsigned char eepromN25Q128byteread(eepromN25Q128Dev* dev,uint32_t address) | ||||
{ | ||||
int cmdcnt=1,datacnt=1; | ||||
unsigned char data = 0; | ||||
return data; | ||||
} | ||||
void eepromN25Q128readn(eepromN25Q128Dev* dev,uint32_t address,unsigned char* data, unsigned int count) | ||||
{ | ||||
char baddress[3]; | ||||
baddress[0]=(char)0xFF&((address)>>16); | ||||
baddress[1]=(char)0xFF&((address)>>8); | ||||
baddress[2]=(char)0xFF&((address)); | ||||
printf("about to read %d bytes\n\r",count); | ||||
if(dev->select!=NULL)dev->select(0); | ||||
spiputw(dev->spidev,N25Q128_READ); | ||||
spiputnc(dev->spidev,baddress,3); | ||||
spigetnc(dev->spidev,data,count); | ||||
if(dev->select!=NULL)dev->select(1); | ||||
} | ||||
void eepromN25Q128writen(eepromN25Q128Dev* dev,uint32_t address,unsigned char* data, unsigned int count) | ||||
{ | ||||
char baddress[3]; | ||||
if(dev->select!=NULL)dev->select(0); | ||||
for(int i=0;i<(count/N25Q128_PAGE_SZ);i++) | ||||
{ | ||||
printf("about to send %d bytes\n\r",N25Q128_PAGE_SZ); | ||||
int index =i*N25Q128_PAGE_SZ; | ||||
baddress[0]=(char)0xFF&((address+index)>>16); | ||||
baddress[1]=(char)0xFF&((address+index)>>8); | ||||
baddress[2]=(char)0xFF&((address+index)); | ||||
if(dev->select!=NULL)dev->select(0); | ||||
spiputw(dev->spidev,N25Q128_PP); | ||||
spiputnc(dev->spidev,baddress,3); | ||||
spiputnc(dev->spidev,data+(index),count); | ||||
if(dev->select!=NULL) | ||||
{ | ||||
dev->select(1); | ||||
} | ||||
else { | ||||
while (!spitransactionfinished(dev->spidev)); | ||||
} | ||||
} | ||||
int mod = count%N25Q128_PAGE_SZ; | ||||
if(mod) | ||||
{ | ||||
baddress[0]=(char)0xFF&((address+(count-mod))>>16); | ||||
baddress[1]=(char)0xFF&((address+(count-mod))>>8); | ||||
baddress[2]=(char)0xFF&((address+(count-mod))); | ||||
printf("about to send %d bytes\n\r",mod); | ||||
if(dev->select!=NULL)dev->select(0); | ||||
spiputw(dev->spidev,N25Q128_PP); | ||||
spiputnc(dev->spidev,baddress,3); | ||||
spiputnc(dev->spidev,data+(count-mod),mod); | ||||
if(dev->select!=NULL) | ||||
{ | ||||
dev->select(1); | ||||
} | ||||
else { | ||||
while (!spitransactionfinished(dev->spidev)); | ||||
} | ||||
} | ||||
} | ||||
void eepromN25Q128enablewrite(eepromN25Q128Dev *dev) | ||||
{ | ||||
if(dev->select!=NULL)dev->select(0); | ||||
spiputw(dev->spidev,N25Q128_WREN); | ||||
if(dev->select!=NULL)dev->select(1); | ||||
} | ||||