What are the names of interrupt service routines for the msp430 series of microcontrollers? - interrupt

Is there a comprehensive list of names of interrupt service routines (ISR), specifically for msp430F5438A? Any help would be appreciated, thanks.

You can find the list of #defines along with descriptive comments in msp430/include/msp430f5438a (for msp430f5438a specifically, other MCU models have their own header files).
Copy-pasting from there:
/************************************************************
* Interrupt Vectors (offset from 0xFF80)
************************************************************/
#define RTC_VECTOR (0x0052) /* 0xFFD2 RTC */
#define PORT2_VECTOR (0x0054) /* 0xFFD4 Port 2 */
#define USCI_B3_VECTOR (0x0056) /* 0xFFD6 USCI B3 Receive/Transmit */
#define USCI_A3_VECTOR (0x0058) /* 0xFFD8 USCI A3 Receive/Transmit */
#define USCI_B1_VECTOR (0x005A) /* 0xFFDA USCI B1 Receive/Transmit */
#define USCI_A1_VECTOR (0x005C) /* 0xFFDC USCI A1 Receive/Transmit */
#define PORT1_VECTOR (0x005E) /* 0xFFDE Port 1 */
#define TIMER1_A1_VECTOR (0x0060) /* 0xFFE0 Timer1_A3 CC1-2, TA1 */
#define TIMER1_A0_VECTOR (0x0062) /* 0xFFE2 Timer1_A3 CC0 */
#define DMA_VECTOR (0x0064) /* 0xFFE4 DMA */
#define USCI_B2_VECTOR (0x0066) /* 0xFFE6 USCI B2 Receive/Transmit */
#define USCI_A2_VECTOR (0x0068) /* 0xFFE8 USCI A2 Receive/Transmit */
#define TIMER0_A1_VECTOR (0x006A) /* 0xFFEA Timer0_A5 CC1-4, TA */
#define TIMER0_A0_VECTOR (0x006C) /* 0xFFEC Timer0_A5 CC0 */
#define ADC12_VECTOR (0x006E) /* 0xFFEE ADC */
#define USCI_B0_VECTOR (0x0070) /* 0xFFF0 USCI B0 Receive/Transmit */
#define USCI_A0_VECTOR (0x0072) /* 0xFFF2 USCI A0 Receive/Transmit */
#define WDT_VECTOR (0x0074) /* 0xFFF4 Watchdog Timer */
#define TIMER0_B1_VECTOR (0x0076) /* 0xFFF6 Timer0_B7 CC1-6, TB */
#define TIMER0_B0_VECTOR (0x0078) /* 0xFFF8 Timer0_B7 CC0 */
#define UNMI_VECTOR (0x007A) /* 0xFFFA User Non-maskable */
#define SYSNMI_VECTOR (0x007C) /* 0xFFFC System Non-maskable */
#define RESET_VECTOR (0x007E) /* 0xFFFE Reset [Highest Priority] */

A function is connected to an interrupt vector with #pragma vector=nr or __attribute__((interrupt(nr))), depending on the compiler used.
The name of the function does not matter, only that vector number.
You can name the function after the hardware module (see kfx's answer for an example list), but it might make more sense to name it after the actual function you have assigned to that part of the hardware (e.g., red_button_gpio_interrupt() instead of port1_interrupt()).

Related

PLL Init function in STM32F429 to configure max frequency (180Mhz)

I want to configure PLL in STM32F429 to its max frequency (180Mhz) without using STMCube-generated configurations. I am using my own register definitions like this
#define RCC_CFGR (*((volatile uint32 *)0x40023808))
and my own SET_BIT()/CLEAR_BIT() macros
My questions are:
1- Is this procedure correct?
2- How can I check if it is working?
3- May the MCU can not handle this speed (reading/writing in registers)?
#define PLL_M 16
#define PLL_N 360
#define PLL_P 2
#define PLL_Q 7
void PLL_init(void)
{
/* System Init */
/* HSI ON */
SET_BIT(RCC_CR, HSION);
/* Reset CFGR register */
RCC_CFGR = 0x00000000 ;
/* Reset PLLCFGR register */
RCC_PLLCFGR = 0x24003010;
/* Reset HSEON, CSSON and PLLON bits */
RCC_CR &= (uint32_t)0xFEF6FFFF;
/************* SetSysClock ************/
RCC_PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16)| (PLL_Q << 24);
/* PLL clock Source HSI or HSE */
CLEAR_BIT(RCC_PLLCFGR, PLLSRC);
/* APB1 PWR Enable*/
SET_BIT(RCC_APB1ENR, PWREN);
/* Select regulator voltage output Scale 1 mode, System frequency up to 180 MHz */
SET_BIT(PWR_CR, VOS0);
SET_BIT(PWR_CR, VOS1);
/* AHB div 1 */
CLEAR_BIT(RCC_CFGR,HPRE3);
CLEAR_BIT(RCC_CFGR,HPRE0);
/* APB2 Div = 2*/
CLEAR_BIT(RCC_CFGR,PPRE20);
CLEAR_BIT(RCC_CFGR,PPRE21);
SET_BIT(RCC_CFGR, PPRE23);
/* APB 1 Div = 8 */
CLEAR_BIT(RCC_CFGR, PPRE10);
SET_BIT(RCC_CFGR, PPRE11);
SET_BIT(RCC_CFGR, PPRE12);
/* SET PLL ON */
SET_BIT(RCC_CR, PLLON);
/* Check PLL is ready */
while(BIT_IS_CLEAR(RCC_CR,PLLRDY));
/* Enable the Over-drive to extend the clock frequency to 180 Mhz */
SET_BIT(PWR_CR, ODEN);
while(BIT_IS_CLEAR(PWR_CSR,ODRDY));
SET_BIT(PWR_CR, ODSWEN);
while(BIT_IS_CLEAR(PWR_CSR,ODSWRDY));
SET_BIT(FLASH_ACR, PRFTEN);
SET_BIT(FLASH_ACR, ICEN);
SET_BIT(FLASH_ACR, DCEN);
FLASH_ACR |= FLASH_ACR_LATENCY_5WS;
/* Select the main PLL as system clock source */
CLEAR_BIT(RCC_CFGR, SW0);
SET_BIT(RCC_CFGR, SW1);
/* Wait till the main PLL is used as system clock source */
while(!BIT_IS_CLEAR(RCC_CFGR,SWS0) && !BIT_IS_SET(RCC_CFGR,SWS1) ); /* Loop till is Set */
}
I did exactly this thing on my STM32F746 - set 216MHz with registers. It looks very similar to mine. Not observing any obvious things. You wait for all ready flags and all. Overdrive and overdrive switching are there, flash wait states are there.
How to check if it's working - timer is an easy idea. Timer that is clocked by a system clock with some prescaler and that outputs PWM to some pin. Given the configuration of the timer is set by you, you should know what output frequency to expect. You can set timer parameters in such a way that you expect timer-interrupt-toggle an LED every 1s.
The MCU should handle it OK if all prescalers for AHB/APB are within limits, as well as Flash latency is set correctly. Which seems to be the case.
If you want to compare the logic with what I do with STM32F746 (which looks almost identical in terms of how you do it), you can check it in this rcc.c file of mine. It definitely works, I tested it.

Redefinition of z_stream_s, gz_header_s, gzfile_s in zlib.h for objective c after upgrade to xcode13

After upgrade to xcode13.4, redefinition of struct z_stream_s, gz_header_s, gzfile_s in zlib.h.
I added to guard to avoid the redefinition in the starting and ending
#ifndef ZLIB_H
#define ZLIB_H
------ coding
#endif
Code where the error occurring
typedef struct z_stream_s {
z_const Bytef *next_in; /* next input byte */
uInt avail_in; /* number of bytes available at next_in */
uLong total_in; /* total number of input bytes read so far */
Bytef *next_out; /* next output byte will go here */
uInt avail_out; /* remaining free space at next_out */
uLong total_out; /* total number of bytes output so far */
z_const char *msg; /* last error message, NULL if no error */
struct internal_state FAR *state; /* not visible by applications */
alloc_func zalloc; /* used to allocate the internal state */
free_func zfree; /* used to free the internal state */
voidpf opaque; /* private data object passed to zalloc and zfree */
int data_type; /* best guess about the data type: binary or text
for deflate, or the decoding state for inflate */
uLong adler; /* Adler-32 or CRC-32 value of the uncompressed data */
uLong reserved; /* reserved for future use */
} z_stream;
typedef struct z_stream_s FAR *z_streamp;
typedef struct gz_header_s {
int text; /* true if compressed data believed to be text */
uLong time; /* modification time */
int xflags; /* extra flags (not used when writing a gzip file) */
int os; /* operating system */
Bytef *extra; /* pointer to extra field or Z_NULL if none */
uInt extra_len; /* extra field length (valid if extra != Z_NULL) */
uInt extra_max; /* space at extra (only when reading header) */
Bytef *name; /* pointer to zero-terminated file name or Z_NULL */
uInt name_max; /* space at name (only when reading header) */
Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */
uInt comm_max; /* space at comment (only when reading header) */
int hcrc; /* true if there was or will be a header crc */
int done; /* true when done reading gzip header (not used
when writing a gzip file) */
} gz_header;
struct gzFile_s {
unsigned have;
unsigned char *next;
z_off64_t pos;
};
I replaced the latest zlib.h also but the same redefinition error occurring again.
These files are used to generate the qrcode
redefintion issue occurring in all these files
When i click the error occur it goes to the line where struct z_stream_s is defined
typedef struct z_stream_s {
In that error shows that it has been already defined in the file unzip.c
in the unzip.c when i look into that zlib.h has been include in the both unzip.h and unzip.c
in unzip.h
#ifndef _unz_H
#define _unz_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef _ZLIB_H
#include "zlib.h"
#endif
#ifndef _ZLIBIOAPI_H
#include "ioapi.h"
#endif
in unzip.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zlib.h"
#include "unzip.h"
#ifdef STDC
# include <stddef.h>
# include <string.h>
# include <stdlib.h>
#endif
#ifdef NO_ERRNO_H
extern int errno;
#else
# include <errno.h>
#endif
These files are used to generate the QRCODE
and the redefinition error continues on file by file zip.c, zip.h, pngpriv.h, pngstruct.h like that it goes on
Note:The same project is working fine in Xcode 12.4, but in latest Xcode 13 Its shows the redefinition error
Please help me to resolve this issue.
Thanks in advance.

STM32 Nucleo EEPROM Emulator ee_init issue

I am working on trying to get the EEPROM Emulator from stm32 working. I have followed the example given for a stm32 l47x board however I am still running into issues. When I call EE_init I end up running into a stack overflow. I am not too familiar with this emulator and am using the default configurations from the example.
This is how I am initializing everything.
EE_Status ee_status = EE_OK;
/* Enable and set FLASH Interrupt priority */
/* FLASH interrupt is used for the purpose of pages clean up under interrupt */
HAL_NVIC_SetPriority(FLASH_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(FLASH_IRQn);
HAL_FLASH_Unlock();
if(__HAL_PWR_GET_FLAG(PWR_FLAG_SB) == RESET)
{
/* Blink LED_OK (Green) twice at startup */
LEDInterface_toggleColor(GREEN);
HAL_Delay(100);
LEDInterface_toggleColor(NONE);
HAL_Delay(100);
LEDInterface_toggleColor(GREEN);
HAL_Delay(100);
LEDInterface_toggleColor(NONE);
ee_status = EE_Init(EE_FORCED_ERASE);
if(ee_status != EE_OK)
{
while(1);
}
This is the eeprom_emul_conf.h settings which I also have not changed
/* Configuration of eeprom emulation in flash, can be custom */
#if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
#define START_PAGE_ADDRESS 0x08100000U /*!< Start address of the 1st page in flash, for EEPROM emulation */
#else
#define START_PAGE_ADDRESS 0x08080000U /*!< Start address of the 1st page in flash, for EEPROM emulation */
#endif
#define CYCLES_NUMBER 1U /*!< Number of 10Kcycles requested, minimum 1 for 10Kcycles (default),
for instance 10 to reach 100Kcycles. This factor will increase
pages number */
#define GUARD_PAGES_NUMBER 2U /*!< Number of guard pages avoiding frequent transfers (must be multiple of 2): 0,2,4.. */
/* Configuration of crc calculation for eeprom emulation in flash */
#define CRC_POLYNOMIAL_LENGTH LL_CRC_POLYLENGTH_16B /* CRC polynomial lenght 16 bits */
#define CRC_POLYNOMIAL_VALUE 0x8005U /* Polynomial to use for CRC calculation *
/
I am running into the osal_hooks.c file where I am getting stuck in this while loop
#if defined(DOXYGEN)
void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName )
#else
OSAL_WEAK_FN(void, vApplicationStackOverflowHook)( TaskHandle_t xTask, char *pcTaskName )
#endif
{
volatile char * name = pcTaskName;
(void)name;
while (1)
{
;
}
}
Im sure I need to change where I allocate the memory but what is the best way to go about this. Thank you

GPIO commands for Blinking LED problem in simplisity studio

I have the following commands
GPIO_PinOutClear(LED_PORT_E,15)
GPIO_PinModeSet(LED_PORT_A,15,gpioModePushPull,0)
GPIO_PinOutSet(LED_PORT_E,15)
I know that I should put ON value delay OFF value in a loop in order to make the LEDs blink constantly. I have tried to implement it, but it's not working.
Where did I go wrong?
#include <stdint.h>
#include <stdbool.h>
#include "em_device.h"
#include "em_chip.h"
#include "em_cmu.h"
#include "em_emu.h"
#include "bsp.h"
#include "bsp_trace.h"
#define LED_PORT_E gpioPortE
#define LED_PIN_E 15
#define LED_PORT_A gpioPortA
#define LED_PIN_A 15
volatile uint32_t msTicks; /* counts 1ms timeTicks */
void Delay(uint32_t dlyTicks);
/***************************************************************************//**
* #brief SysTick_Handler
* Interrupt Service Routine for system tick counter
******************************************************************************/
void SysTick_Handler(void)
{
msTicks++; /* increment counter necessary in Delay()*/
}
/***************************************************************************//**
* #brief Delays number of msTick Systicks (typically 1 ms)
* #param dlyTicks Number of ticks to delay
******************************************************************************/
void Delay(uint32_t dlyTicks)
{
uint32_t curTicks;
curTicks = msTicks;
while ((msTicks - curTicks) < dlyTicks) ;
}
/***************************************************************************//**
* #brief Main function
******************************************************************************/
int main(void)
{
/* Chip errata */
CHIP_Init();
CMU_ClockEnable(cmuClock_GPIO,true);
/* If first word of user data page is non-zero, enable Energy Profiler trace */
BSP_TraceProfilerSetup();
/* Setup SysTick Timer for 1 msec interrupts */
if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000)) {
while (1) ;
}
/* Initialize LED driver */
BSP_LedsInit();
BSP_LedSet(0);
GPIO_PinModeSet(LED_PORT_A,15,gpioModePushPull,0);
Delay(1000);
GPIO_PinModeSet(LED_PORT_E,15,gpioModePushPull,0);
while (1) {
GPIO_PinOutClear(LED_PORT_E,15);
GPIO_PinOutSet(LED_PORT_E,15);
}
}
Give delay in while loop .
while (1) {
GPIO_PinOutClear(LED_PORT_E,15);
Delay(1000);
GPIO_PinOutSet(LED_PORT_E,15);
Delay(1000);
}
If configuration of pins is ok then LEDS start blinking.

How to read from and write to EEPROM suing SPI communication

I am using PIC32MX350F128L Microcontroller to read from and write to EEPROM(SST26VF032B) using SPI communication. SPI communication in this program is working, I have checked it by sending JEDEC code which is provided in the SST26VF032B datasheet. So when i send 0x9F i am getting 3 bytes of data as mentioned in the datasheet. When i run now i am sending a string of data to a specific address of eeprom and getting 0xff in return. I am erasing the eeprom before writing into it. So i think i am getting 0xff after erasing the eeprom. The writing, reading operations are not working. If i send a string of value or a BYTE i am getting 0xff in return. So can u guys please suggest me where i am going wrong. I am using UART for debugging purpose to read values recieved through spi communication. The complete code is below, i am using MPLAB X.
Best Regards
Sandesh
#include <xc.h>
#include <stdio.h>
#include <plib.h>
#include <p32xxxx.h>
/* Configuration Bits */
#pragma config FSRSSEL = PRIORITY_7 // Shadow Register Set Priority Select (SRS Priority 7)
#pragma config PMDL1WAY = ON // Peripheral Module Disable Configuration (Allow only one reconfiguration)
#pragma config IOL1WAY = ON // Peripheral Pin Select Configuration (Allow only one reconfiguration)
// DEVCFG2
#pragma config FPLLIDIV = DIV_2 // PLL Input Divider (2x Divider)
#pragma config FPLLMUL = MUL_20 // PLL Multiplier (20x Multiplier)
#pragma config FPLLODIV = DIV_1 // System PLL Output Clock Divider (PLL Divide by 1)
// DEVCFG1
#pragma config FNOSC = PRIPLL // Oscillator Selection Bits (Primary Osc (XT,HS,EC))
#pragma config FSOSCEN = ON // Secondary Oscillator Enable (Enabled)
#pragma config IESO = ON // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = HS // Primary Oscillator Configuration (XT osc mode)
#pragma config OSCIOFNC = ON // CLKO Output Signal Active on the OSCO Pin (Enabled)
#pragma config FPBDIV = DIV_1 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/8)
#pragma config FCKSM = CSECME // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window Size is 25%)
// DEVCFG0
#pragma config DEBUG = OFF // Background Debugger Enable (Debugger is Disabled)
#pragma config JTAGEN = OFF // JTAG Enable (JTAG Disabled)
#pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2)
#pragma config PWP = OFF // Program Flash Write Protect (Disable)
#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF // Code Protect (Protection Disabled)
/* MACRO DEFINITIONS */
/* Defining the Slave Select Pin */
#define SS LATDbits.LATD9
/* Defining the System Clock Frequency */
#define SYSCLK 40000000
/* Macro to get array size in bytes
* note that array size can't be found after passing pointer to a function */
#define LEN(x) (sizeof(x) / sizeof(x[0]))
/* SST26VF032B EEPROM instructions */
/* Write Enable */
#define WREN 0x06
/* Write Disable */
#define WRDI 0x04
/* Initialize Start of Write Sequence */
#define WRITE 0x02
/* Initialize Start of Read Sequence */
#define READ 0x03
/* Erase all sectors of Memory */
#define CE 0xc7
/* Read STATUS Register */
#define RDSR 0x05
/* Function Prototypes */
/* UART bit configuration */
void Bitconfig_uart(void);
/* SPI Initialization */
void SPI1_Init(void);
/* UART Initialization */
void Init_uart(void);
/* Send a Character Byte through UART */
void UART5PutChar(char Ch);
/* Function to Read and Write SPI1 buffer */
int SPI1_transfer( int b);
/* Function to check the Status of SPI */
void waitBusy();
/* Function to erase the contents in EEPROM */
void eraseEEPROM();
/* Function to Read data from EEPROM */
void readEEPROM( int address, char* loadArray, int loadArray_size);
/* Function to Write to EEPROM */
void writeEEPROM( int address, char* storeArray, int storeArray_size);
/* Global Variables Declaration */
/* Declare variables to check the functionality of EEPROM */
int i,j = 0;
char st = 0x9F;
char rec;
int x,y,z;
/*******************************************************************************
* Function Name: main()
********************************************************************************
* Summary:
* Initializes SPI
* Erase EEPROM
* Writes to EEPROM
* Read from EEPROM
*
* Parameters:
* None.
*
* Return:
* None.
*
*******************************************************************************/
int main()
{
int i;
/* Clock Setting */
SYSTEMConfigPerformance(SYSCLK);
/* UART bit configuration */
Bitconfig_uart();
/* Set the Controller OScillator Register bits */
//OSCCON = 0x00002200;
/* Initialize a String to Write to EEPROM and an array to Read back contents */
char writeData[] = "123456789ABCDEF";
/* Array to read 35 bytes of data */
char readData[15];
/* SPI Initialization */
SPI1_Init();
/* UART Initialization */
Init_uart();
/* Erase contents of EEPROM */
eraseEEPROM();
/* Write contents of writeData array to address 180 */
writeEEPROM( 0x1000, writeData, LEN(writeData));
/*
JEDEC Code (working) getting output as per datasheet (0x9F = 159)
SS=0;
SPI1_transfer(159);
x=SPI1_transfer(0);
UART5PutChar(x);
y=SPI1_transfer(0);
UART5PutChar(y);
z=SPI1_transfer(0);
UART5PutChar(z);
*/
while(1)
{
/* Read contents of EEPROM into readData array
* start at address 180 and read up to 180+length(readData) */
readEEPROM( 0x1000, readData, LEN(readData) );
}
} /* END main() */
/*******************************************************************************
* Function Name: SPI1_Init()
********************************************************************************
* Summary:
* SPI1 Initialization
*
* Parameters:
* None.
*
* Return:
* None.
*
*******************************************************************************/
void SPI1_Init(void)
{
/* Configure Peripheral Pin Select (PPS) for the SPI1 module
* Note: SS will be toggled manually in code
* SCK is hardwired to pin 55 */
/* Output Pin Selection */
RPE5R = 8;
SDI1R = 3;
/* RB4 (Slave Select 1) : output */
TRISDbits.TRISD9 = 0;
/* SPI configuration */
/* SPI1CON Register Configuration
* MSTEN: Master Mode Enable bit = 1 (Master)
* CKP (clock polarity control) = 0
* CKE (clock edge control) = 1
* ON: SPI Peripheral On bit
* 8-bit, Master Mode */
SPI1CON = 0x8120;
/* SPI1BRG Register Configuration */
SPI1BRG = 0x4D;
//REFOCONbits.ON = 1;
// REFOCONbits.DIVSWEN = 1;
}
/*******************************************************************************
* Function Name: SPI1_transfer()
********************************************************************************
* Summary:
* Write to and Read from SPI1 buffer
*
* Parameters:
* char b - Writes a Character to Buffer
*
* Return:
* Char - Returns the Character Read from EEPROM
*
*******************************************************************************/
int SPI1_transfer( int b)
{
/* write to buffer for TX */
SPI1BUF = b;
/* wait transfer complete */
while(!SPI1STATbits.SPIRBF);
/* read the received value */
return SPI1BUF;
} /* END SPI1_transfer() */
/*******************************************************************************
* Function Name: waitBusy()
********************************************************************************
* Summary:
* Checks if EEPROM is ready to be modified and waits if not ready
*
* Parameters:
* None.
*
* Return:
* None.
*
*******************************************************************************/
void waitBusy()
{
char status = 0;
do{
/* Select EEPROM */
SS = 0;
/* Read EEPROM status register */
SPI1_transfer(RDSR);
/* send dummy byte to receive incoming data */
status = SPI1_transfer(0);
/* Release EEPROM */
SS = 1;
}
/* write-in-progress while status<0> set to '1' */
while( status & 0x01);
} /* END waitBusy() */
/*******************************************************************************
* Function Name: readEEPROM()
********************************************************************************
* Summary:
* Reads data from EEPROM
*
* Parameters:
* Inputs: address - EEPROM address
* loadArray - array to load EEPROM data to
* loadArray_size - number of bytes of EEPROM data to load into array
*
* Return:
* None.
*
*******************************************************************************/
void readEEPROM( int address, char* loadArray, int loadArray_size)
{
int i;
/* Wait until EEPROM is not busy */
waitBusy();
/* Select EEPROM */
SS = 0;
/* Initiate Read */
SPI1_transfer( READ);
/* Address must be 16-bits but we're transferring it in two 8-bit sessions */
SPI1_transfer( address >> 16);
SPI1_transfer( address >> 8);
SPI1_transfer( address);
/* Request and store loadArray_size number of bytes into loadArray */
for( i=0 ; i<loadArray_size ; i++)
{
/* send dummy byte to read 1 byte */
loadArray[i] = SPI1_transfer( 0x00);
}
/* Release EEPROM */
SS = 1;
/* UART Test */
for(i=0;i<35;i++)
{
UART5PutChar(loadArray[i]);
for(j=0;j<20000;j++)
{}
}
} /* END readEEPROM() */
/*******************************************************************************
* Function Name: writeEEPROM()
********************************************************************************
* Summary:
* Write data to EEPROM
*
* Parameters:
* Inputs: address - EEPROM address
* storeArray - array of which contents are stored in EEPROM
* storeArray_size - number of bytes in array to store into EEPROM
*
* Return:
* None.
*
*******************************************************************************/
void writeEEPROM( int address, char* storeArray, int storeArray_size)
{
int i;
/* Wait until EEPROM is not busy */
waitBusy();
/* Select EEPROM */
SS = 0;
/* Send WRITE_ENABLE command */
SPI1_transfer( WREN);
/* Release EEPROM */
SS = 1;
/* Select EEPROM again after WREN cmd */
SS = 0;
/* Initiate Write */
SPI1_transfer( WRITE);
SPI1_transfer( address >> 16 );
SPI1_transfer( address >> 8 );
SPI1_transfer( address );
/* write 1 byte at a time from array */
/* MSB at lowest address (0 - first letter in string) */
for( i=0 ; i<storeArray_size; i++)
{
/* Initiate Write */
SPI1_transfer( WRITE);
SPI1_transfer( (address+i) >> 16 );
SPI1_transfer( (address+i) >> 8 );
SPI1_transfer( address+i );
SPI1_transfer( storeArray[i]);
}
/* Release EEPROM */
SS = 1;
} /* END writeEEPROM() */
/*******************************************************************************
* Function Name: eraseEEPROM()
********************************************************************************
* Summary:
* Erase entire contents of EEPROM
*
* Parameters:
* None
*
* Return:
* None.
*
*******************************************************************************/
void eraseEEPROM()
{
/* Wait until EEPROM is not busy */
waitBusy();
/* Select EEPROM */
SS = 0;
/* Send WRITE_ENABLE command */
SPI1_transfer( WREN);
/* Release EEPROM */
SS = 1;
/* Select EEPROM again after WREN cmd */
SS = 0;
/* send CHIP_ERASE command */
SPI1_transfer( CE);
/* Release EEPROM */
SS = 1;
} /* END eraseEEPROM() */
/*******************************************************************************
* Function Name: Init_uart()
********************************************g************************************
* Summary:
* Initialize UART4
*
* Parameters:
* None
*
* Return:
* None.
*
*******************************************************************************/
void Init_uart()
{
/* Enable UART */
U5MODEbits.ON = 1 ;
/* set baud rate(9600) */
U5BRG = 521;
/* Set U4STA Register for Enabling tx and rx */
U5STA=0x9400;
}
/*******************************************************************************
* Function Name: UART4PutChar(unsigned char Ch)
********************************************************************************
* Summary:
* Send data from controller to putty GUI
*
* Parameters:
* input
* unsigned char Ch - To Send a byte of data over UART
*
* Return:
* None.
*
*******************************************************************************/
void UART5PutChar(char Ch)
{
while(U5STAbits.UTXBF == 1);
U5TXREG=Ch;
}
/*******************************************************************************
* Function Name: Bitconfig_uart()
********************************************************************************
* Summary:
* UART Pin Configuration
*
* Parameters:
* None
*
* Return:
* None.
*
*******************************************************************************/
void Bitconfig_uart(void)
{
/* UART4 Initialization */
// OSCCON=0x00002200;
/* Set pins as digital */
ANSELBbits.ANSB2 = 0;
ANSELBbits.ANSB0 = 0;
/* Set UART Tx pin as Output */
TRISBbits.TRISB0 = 0; //in controler tx
TRISBbits.TRISB2 = 1; // in controller RX
/* Peripheral Pin select for UART4 */
U5RXR=0x07;
RPB0R=0x04;
}
I faced the same problem for 3 long days until I found that there's a 18bytes long register called Block Protection Register BPR.
You need to set its bits to 0, according to the memory area you want to write.
So I read the BPR (send command 0x72 followed by 18 byte read) and I found it was not at zero everywhere in my case.
Reading on page 41 of the datasheet you can see that after a power on the BPR register is set to 5555 FFFFFFFF FFFFFFFF, so it protects from write the whole memory.
So for testing purpose I tried to clears it completely, and there's a specific command (0x98) for that purpose, allowing you to write anywhere in the whole memory.
But be sure to write enable the memory (command 0x06) before sending the clear BPR command (command 0x98).
At this point if you read the BPR (command 0x72) you will read 00 at every of its 10 bytes. (And this means the whole memory is now writeable)
In this state the write now finally works for me.
(I sent WriteEnable - SectorErase - SectorRead - WriteEnable - SectorWrite - SectorRead and it now works!)
Hope it helps, the datasheet is very chaotic about that.
P.S.
Somewhere the datasheet says that BPR is 18 bytes long, it is wrong, the BPR is only 10 bytes long