QSPI chip select pin control and configuration in Zephyr RTOS question - spi

I am trying to use QSPI bus to access a serial NOR flash in Zephyr. The CS(chip select) pin stays high all the time. It should be active low when the flash chip is selected. Just wondering if the QSPI CS is working in Zephyr or I need to configure the CS pin as GPIO and control it by my software.
Anyone had CS pin of QSPI working in Zephyr ?? I am using rNF52480 from Nordic semiconductor.
Thanks,
JC

I think you need to configure it yourself:
...
#include <drivers/gpio.h>
#include <drivers/spi.h>
struct spi_cs_control spi_cs = {
/* PA4 as CS pin */
.gpio_dev = DEVICE_DT_GET(DT_NODELABEL(gpioa)),
.gpio_pin = 4,
.gpio_dt_flags = GPIO_ACTIVE_LOW,
/* delay in microseconds to wait before starting the transmission and before releasing the CS line */
.delay = 10,
};
#define SPI_CS (&spi_cs)
struct spi_config spi_cfg = {
.frequency = 350000,
.operation = SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(8) | SPI_LINES_QUAD | SPI_LOCK_ON,
.cs = SPI_CS,
};
void spi_init()
{
spi = device_get_binding("SPI_1");
....
}

Related

How to enable UART for STM32 Nucleo F429ZI?

I am trying to enable UART for STM32 Nucleo - F429ZI.
I have gone through user manual and it says by default USART 3 can be configured for virtual com port purpose. Here is my code. I can see that USART 3 has pins D8 and D9. Here is my code. What am I doing wrong here, I don't see prints on my com port.
Data sheet refered - https://www.st.com/resource/en/data_brief/nucleo-f429zi.pdf
https://www.st.com/resource/en/user_manual/dm00244518-stm32-nucleo-144-boards-stmicroelectronics.pdf
//test print msg
char usr_msg[256];
//Ends
void printmsg(char *msg)
{
for(uint32_t i = 0; i < strlen(msg); i++)
{
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) != SET);
USART_SendData(USART3, msg[i]);
}
}
static void prvSetupUart(void)
{
GPIO_InitTypeDef gpio_uart_pins;
USART_InitTypeDef uart3_init;
//GPIO D is connected to AHB1 bus.
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
// Enable the UART 3 peripheral clock -- connected to APB1 bus.
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
// zero the local variable
memset(&gpio_uart_pins,0,sizeof(gpio_uart_pins));
memset(&uart3_init,0,sizeof(uart3_init));
// GPIO port D - pin 8 like TX
// GPIO port D - ping 9 like RX
gpio_uart_pins.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
gpio_uart_pins.GPIO_Mode = GPIO_Mode_AF; //AF - Alternate function i.e. TX and RX.
gpio_uart_pins.GPIO_PuPd = GPIO_PuPd_UP; //Pull up so that we see some default voltage.
GPIO_Init(GPIOD, &gpio_uart_pins);
uart3_init.USART_BaudRate = 115200;
uart3_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
uart3_init.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
uart3_init.USART_Parity = USART_Parity_No;
uart3_init.USART_StopBits = USART_StopBits_1;
uart3_init.USART_WordLength = USART_WordLength_8b;
USART_Init(USART3,&uart3_init);
//Enable USART 3 peripheral.
USART_Cmd(USART3, ENABLE);
}
int main(void)
{
//Step number 1 - DeInit the RCC - Reset Control Clock so that we don't use Pre-scaler or PLL.
// Basically, we want to use lower speed.
RCC_DeInit();
// update the System Core Clock
//Step number 2 -- Update the clock to use the default clock.
SystemCoreClockUpdate();
prvSetupUart();
sprintf(usr_msg, "This is Hello World\r\\n");
printmsg(usr_msg);
for(;;);
}
you forgot to set what AF mode to be used (there are possible 16 AF modes for every pin). At the moment AF registers are set to the 0 and it is wrong. You need to make it 7.
How can it be archived using the SPL library (depreciated and not supported anymore) I do not know.
Here is the register version
#define GPIO_AFRL_AFRL0_Msk (GPIO_AFRL_AFRL0_0 | GPIO_AFRL_AFRL0_1 | GPIO_AFRL_AFRL0_2 | GPIO_AFRL_AFRL0_3)
void GPIO_SetAF(GPIO_TypeDef *gpio, unsigned pin, unsigned AF)
{
volatile uint32_t *AFreg = &gpio -> AFR[pin >= 8];
if(AF <= 15)
{
if(pin > 7) pin -= 8;
*AFreg &= ~(GPIO_AFRL_AFRL0_Msk << (4 * pin));
*AFreg |= (AF << (4 * pin));
}
}

STM32F4 Timing Problem with systick after switching IDE (coocox to TrueStudio)

I am working on a weird problem: As a part of my project, I migrated a firmware from CooCox to TrueStudio. Both, CooCox and TrueStudio automatically create some standard files while creating a project for a specific Microcontroller. The Microcontroller used here is the STM32F407VGT6. I am using ms - delay and s - delay which are derived from the µs - Delay function I will show you.
*edit2: I should mention, that the original project is a pure C project. I am trying to make the Project a C++/C project in TrueStudio.
What I will try now is to migrate the firmware into a TrueStudio pure C project and see if the problem still exists.
I will inform you about the results
**Results: The problem is actually gone now in the pure C Project, but I would really like to implement classes etc using C++. Any ideas how to solve this?
**
*
The initializing systick code is (HCLK Frequency = 168MHz).
*edit1: the HCLK Frequency equals the SYSCLK *
void systick_init(void){
RCC_ClocksTypeDef RCC_Clocks;
Systick_Delay=0;
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config((RCC_Clocks.HCLK_Frequency / 1000000) - 1);
}
The function for the 1µs Delay looks like this:
void delay_us(volatile uint32_t delay)
{
Systick_Delay = delay;
while(Systick_Delay != 0);
}
The Systick Handler contains the following Code:
void SysTick_Handler(void)
{
// Tick für Delay
if(Systick_Delay != 0x00)
{
Systick_Delay--;
}
}
When I create a .hex file to flash the µC with using Coocox, the timing function works (with some minor accuracy mistakes that don't bother me).
When I create the .hex file with TrueStudio, the delays have massive inaccuracys. For example, a delay of 500ms becomes a delay of roughly 2s.
Since the Code is written dependant on the actual HCLK_Frequency, I can't understand the mistake and in my understanding, even if the HCLK should differ, the 1µs Delay should still take about 1µs.
My next step will be comparing the automatically created system files, but maybe anyone has a different approach / another idea?
*edit 3: I normally include my systick - header with the command ' extern "C" '. So my systick source file is a .c file. When I rename the file to systick.cpp, and I include the header without 'extern "C"', the delay function does not work at all. Maybe, that helps with the solution?
*
You are either running off a different clock or have different PLL settings. Looks like your clock speed is 1/4 of what you had before.
The basic startup code provided does not always set the maximum speed for the board. Have a look in some of the examples in the stm32cube.zip code. You will find some System Clock Configuration code for your board which will select the correct clock and pll settings. (this will be in your code somewhere as well).
Look in main.c under stm32cubef4/projects/STM32F4-Discovery\Demonstrations\src.
You will find the following code which sets up the clock:
/**
* #brief System Clock Configuration
* The system Clock is configured as follow :
* System Clock source = PLL (HSE)
* SYSCLK(Hz) = 168000000
* HCLK(Hz) = 168000000
* AHB Prescaler = 1
* APB1 Prescaler = 4
* APB2 Prescaler = 2
* HSE Frequency(Hz) = 8000000
* PLL_M = 8
* PLL_N = 336
* PLL_P = 2
* PLL_Q = 7
* VDD(V) = 3.3
* Main regulator output voltage = Scale1 mode
* Flash Latency(WS) = 5
* #param None
* #retval None
*/
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
/* Enable Power Control clock */
__HAL_RCC_PWR_CLK_ENABLE();
/* The voltage scaling allows optimizing the power consumption when the device is
clocked below the maximum system frequency, to update the voltage scaling value
regarding system frequency refer to product datasheet. */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
/* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported */
if (HAL_GetREVID() == 0x1001)
{
/* Enable the Flash prefetch */
__HAL_FLASH_PREFETCH_BUFFER_ENABLE();
}
}
I found the solution now!
For anyone having similar problems:
You need to declare the SysTick_Handler function as
extern "C" void SysTick_Handler(void)
{
//Tick für Delay
if(Systick_Delay != 0x00)
{
Systick_Delay--;
}
}
Now it is working like it is supposed to do.

PIC32MX795F512H UART communication to RS232

I am currently working for the first time with a PIC microcontroller. In the code I specified exactly which PIC, compiler, etc I am using. Maybe this is of help.
I am trying to set up UART communication on the PIC32 and send a hex code like 0x41 for example to a terminal on my computer through RS232. To convert the signal from the PIC UART to RS232 levels I am using a MAX232EPE.
At the moment I am running into the problem that when I send 0x41 for example from the PIC32 to the terminal or vice versa, the received data doesn't match. I think this being caused by a mistake in my baud rate settings, but I am not sure. Could someone please look over my code and see if someone can see a problem? Did I forget to define something? Did I define something wrong? Did I mis calculate the baud rate?
P.S. I know the data being received doesn't match the data send because i checked in the "watches" in debug mode in mplab and when I echo the data send from the terminal to the PIC32 back to the terminal it doesn't match either.
The Delay and interrupt code can be ignored, they are working as expected, so I really believe the problem has to do with the initial setting of the PIC/buad rate.
I hope this is clear enough, any help is very much appreciated
Thanks,
See code below
/*
The configuration below and in void UART1_Init should set up the UART correctly.
I want to achieve a buadrate of 9600. My external Crystal is 8MHz. So:
FPLLIDIV=2, FPLLMUL=20, FPLLODIV=1, FPBDIV=2, FNOSC=PRIPLL, BRGH = 0, and U1BRG = 259.
This should give me the desired baudrate of 9600.
- ((8MHz / 2) * 20)/2) = 40MHz PBclk.
- U1BRG = (PBclk/(16*Buad rate))-1 so 259
- 16*Buad rate because BRGH = 0
PIC32MX795F512H
MPLAB X IDE V3.26
XC32 Compiler
PICKit3
*/
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <plib.h>
// Give useful names to pins
#define LED1_TRIS TRISDbits.TRISD6
#define LED1 LATDbits.LATD6
#define UART1TX_TRIS TRISDbits.TRISD3
#define UART1RX_TRIS TRISDbits.TRISD2
#define FOSC 8000000 // Crystal frequency = 8 MHz
#define SYS_FREQ (80000000UL) // SYSCLK is 80 MHz
#define GetSystemClock() (FOSC) // For delay
#pragma config FPLLIDIV=DIV_2 // PLL Input Divider Value (Divide by 2)
#pragma config FPLLMUL=MUL_20 // Phase-Locked Loop (PPL) muiltiplier, multiplier of 20
#pragma config FPLLODIV=DIV_1 // Postscaler for PLL, output divided by 1
#pragma config FPBDIV=DIV_2 // 2 = PBCLK is SYSCLK divided by 2 (80MHz/2 = 40MHz)
#pragma config FWDTEN=OFF // Watch Dog Timer (WDT) is not enabled. It can be enabled by software
#pragma config CP=OFF // Code-Protect, 1 = OFF/Disabled
#pragma config BWP=OFF // Boot Flash Write-protect, 1 = OFF/Disabled
#pragma config POSCMOD=XT // Primary oscillator configuration, HS = HS Oscillator mode selection
#pragma config FNOSC=PRIPLL // Oscillator selection, PRIPLL = Primary Oscillator with PLL module
#pragma config OSCIOFNC=OFF // CLKO output disabled
#pragma config FSOSCEN=OFF // Disable secondary Oscillator
int UART_RX_Count; // Counter variable for the UART1 receiver interrupt
int UART_TX_Count; // Counter varible for the UART1 transmitted interrupt
unsigned char RD_SER_NUM; // Variable to store command to read serial number
unsigned char UART_RX_OUTPUT; // Variable to store the UART output
unsigned char i;
void UART1_Init(void){
// UART1 initialization
U1MODEbits.ON = 1; // UART1 is enabled
U1MODEbits.SIDL = 0; // Continue operation in idle mode
U1MODEbits.IREN = 0; // Disable IrDA (IrDA Encoder and Decoder Enable bit)
U1MODEbits.RTSMD = 1; // !U1RTS! pin is in Simplex mode, 0 = !U1RTS! pin is in Flow Control mode
U1MODEbits.UEN = 0; // UxTX and UxRX pins are enabled and used; UxCTS and UxRTS/UxBCLK pins are controlled by corresponding bits in the PORTx register
U1MODEbits.WAKE = 1; // Enable Wake-up on Start bit Detect During Sleep Mode bit
U1MODEbits.LPBACK = 0; // UARTx Loopback Mode Select bit, 0 = disabled, loopback = UxTX output is internally connected to the UxRX input
U1MODEbits.PDSEL = 2; // Parity and Data Selection bits, 10 = 8-bit data, odd parity
U1MODEbits.STSEL = 0; // Stop Selection bit, 0 = 1 stop bit
U1MODEbits.BRGH = 0; // High Baud Rate Enable bit, 0 = Standard Speed mode 16x baud clock enabled
U1MODEbits.RXINV = 1; // Receive Polarity Inversion bit, 1 = UxRX Idle state is 0
U1STAbits.URXEN = 1; // 1 = UART1 receiver is enabled. U1RX pin is controlled by UARTx (if ON = 1)
U1STAbits.UTXEN = 1; // 1 = UART1 transmitter is enabled. U1TX pin is controlled by UARTx (if ON = 1)
U1STAbits.UTXINV = 1; // Transmit Polarity Inversion bit, 1 = UxTX Idle state is 0
U1STAbits.ADM_EN = 0; // 0 = Automatic Address Detect mode is disabled
U1BRG = 259; // Baud Rate Divisor bits (0-15 bits), set baud rate, 9600 # 40 MHz PBclk
__builtin_disable_interrupts(); // Tell CPU to stop paying attention to interrupts
INTCONbits.MVEC = 1; // Multi Vector interrupts
U1STAbits.URXISEL = 0; // 0x = Interrupt flag bit is set when a character is received
U1STAbits.UTXISEL = 1; // 01 = Interrupt flag bit is set when all characters have been transmitted
IPC6bits.U1IP = 5; // Set UART1 priority 5 of 7
IPC6bits.U1IS = 0; // Set UART1 sub priority to 0
IFS0bits.U1RXIF = 0; // Clear UART1 RX interrupt flag
IFS0bits.U1TXIF = 0; // Clear UART1 TX interrupt flag
IEC0bits.U1RXIE = 1; // Enable UART1 RX ISR
__builtin_enable_interrupts(); // Tell CPU to start paying attention to interrupts again
UART_RX_Count = 0; // Set initial UART1 received interrupts count to 0
UART_TX_Count = 0; // Set initial UART1 transmit interrupts count to 0
}
void __ISR(_UART_1_VECTOR, IPL5SRS) UART1_INT(void){
if(INTGetFlag(INT_U1RX)){ // Check if UART1 RX interrupt was triggered
LED1 = ~LED1; // Toggle LED1
UART_RX_Count++; // Add 1 to UART1 RX interrupt occurrence counter
// UART_RX_OUTPUT = U1RXREG; // Read UART1 RX buffer/register
U1TXREG = U1RXREG; // Transmit the received data back
// U1STAbits.OERR = 0; // Clear UART1 buffer overflow
IFS0bits.U1RXIF = 0; // Clear UART1 RX interrupt flag
}else{
if(INTGetFlag(INT_U1TX)){ // Check if UART1 TX interrupt was triggered
UART_TX_Count++; // Add 1 to UART1 TX interrupt occurrence counter
IEC0bits.U1TXIE = 0; // Disable UART1 TX ISR
IFS0bits.U1TXIF = 0; // Clear UART1 TX interrupt flag
}
}
}
// DelayMs creates a delay of given milliseconds using the Core Timer
void DelayMs(WORD delay){
unsigned int int_status;
while( delay-- ){
int_status = INTDisableInterrupts();
OpenCoreTimer(GetSystemClock() / 200);
INTRestoreInterrupts(int_status);
mCTClearIntFlag();
while( !mCTGetIntFlag() );
}
mCTClearIntFlag();
}
int main(){
UART1_Init(); // Call the initializations function of UART1
LED1_TRIS = 0; // Set the LED1 as an output
UART1TX_TRIS = 0; // Set UART1 TX pin as output
UART1RX_TRIS = 1; // Set UART1 RX pin as input
LED1 = 0; // Turn off LED1
while(1){
// DelayMs(1000);
// IEC0bits.U1TXIE = 1; // Enable UART1 TX ISR
// U1TXREG = 0x41; // Send command to U1TXREG
}
return 0;
}
As You are using PIC32MX795F512H, You can use the MPLAB Harmony framework tool for creating your project. So that you need not to play on bit level and stuck in minor error or most probabaly typo error.
Its convinient to generate drivers and all framework properly.
Thanks and regards
Ravi
Using Harmony will actually help you avoid simple errors like the one you have encountered. Especially for the clocks you even have auto calculating functions decreasing your implementation time significantly.

PIC32 UART transmission trouble, no data being transmitted

I am trying to use UART1 on PIC32 to transmit and receive data but there is nothing coming out from that port.
I am using the PIC32 Ethernet kit with 8Mhz crystal.
For the programming in C I am using MPLAB IDE v2.15 and XC32 v 1.32
I don't have scope available right now so I was trying to test with multimeter which is very limited. When I connect the meter to UART1 TX pin and can see that once the initU1 routine is executed the pin goes from 0V to 3.3V which seems to be working but when the bits are supposed to be transmitted I don't see anything on the LCD (LK204-25), I also had the multimeter connected and don't see any change in voltage, it also does not show any frequency. The baud rate is set to 9600.
The code I am using is shown below, please let me know what should be changed to get it to work.
/*
* File: main.c
* Author: Acer
*
* Created on August 27, 2014, 11:57 PM
*/
#include <stdio.h>
#include <stdlib.h>
//#include <p32xxxx.h>
#include <proc/p32mx795f512l.h>
#include <plib.h>
/*
*
*/
#define CTS1 _RD14
#define RTS1 _RD15
#define TRTS1 TRISDbits.TRISD15
#define BRATE1 368 //2083 //9600 #80MHz, BREGH=1
#define U_ENABLE1 0x8008 //BREGH=1, 1 stop, no parity
#define U_TX1 0x0400 //Enable TX, Clear all flags
#define CTS2 _RF12
#define RTS2 _RF13
#define TRTS2 TRISFbits.TRISF13
#define BRATE2 2082 //9600 #80MHz, BREGH=1
#define U_ENABLE2 0x8008 //BREGH=1, 1 stop, no parity
#define U_TX2 0x0400 //Enable TX, Clear all flags
char Port1Buffer[100], Port2Buffer[100];
int i1=0, i2=0;
int nextRun1=0, nextRun2=0;
void __attribute__ ((interrupt(ipl1),vector(27)))
U1RXInterruptHandler(void)//UART 1 RX interrupt routine
{
if(mU1RXGetIntFlag())
{
// Clear the RX interrupt Flag
mU1RXClearIntFlag();
// Echo what we just received
//putcUART1(ReadUART1());
Port1Buffer[i1]=ReadUART1();
i1++;
if(i1==100){
i1=0;
nextRun1=1;
}
// Toggle LED to indicate UART activity
mPORTDToggleBits(BIT_0);
}
// We don't care about TX interrupt
if ( mU1TXGetIntFlag() )
{
mU1TXClearIntFlag();
}
}
void __attribute__ ((interrupt(ipl2),vector(41)))
U2RXInterruptHandler(void)//UART 2 RX interrupt routine
{
if(mU2RXGetIntFlag())
{
// Clear the RX interrupt Flag
mU2RXClearIntFlag();
// Echo what we just received
//putcUART2(ReadUART2());
Port2Buffer[i2]=ReadUART2();
i2++;
if(i2==100){
i2=0;
nextRun2=1;
}
// Toggle LED to indicate UART activity
mPORTDToggleBits(BIT_0);
}
// We don't care about TX interrupt
if ( mU2TXGetIntFlag() )
{
mU2TXClearIntFlag();
}
}
void initU1(void)
{
U1BRG = BRATE1; //Initialize baud rate generator
U1MODE = U_ENABLE1; //Initialize UART module
U1STA = U_TX1; //Enable Transmitter
TRTS1 = 0; //Make RTS an output pin
RTS1 = 1; //Set RTS default status (not ready)
//ConfigIntUART1(UART_INT_PR1|UART_RX_INT_EN);
}
void initU2(void)
{
U2BRG = BRATE2; //Initialize baud rate generator
U2MODE = U_ENABLE2; //Initialize UART module
U2STA = U_TX2; //Enable Transmitter
TRTS2 = 0; //Make RTS an output pin
RTS2 = 1; //Set RTS default status (not ready)
ConfigIntUART2(UART_INT_PR2|UART_RX_INT_EN);
}
void putU1(char c)
{
// while(CTS1);
while(U1STAbits.UTXBF);
U1TXREG = c;
}
void putU2(char c)
{
while(CTS2);
while(U2STAbits.UTXBF);
U2TXREG = c;
}
void DelayRoutine(int delint){
int delcount=0;
while(delcount < delint){
delcount++;
}
}
void main(void) {
DelayRoutine(0xFFFF);
// mU1SetIntPriority(1);
// mU2SetIntPriority(2);
INTEnableSystemMultiVectoredInt();
//mU1IntEnable( 1);
//mU2IntEnable( 1);
int y1=0, y2=0;
initU1();
initU2();
// putU1(0x41);
putU1(0xFE);
putU1(0x58);
while(1)
{
if(y1<i1 || nextRun1==1){
putU1(Port1Buffer[y1]);
y1++;
if(y1==100){
y1=0;
nextRun1=0;
}
}
/* if(y2<i2 || nextRun2==1){
putU1(Port2Buffer[y2]);
y2++;
if(y2==100){
y2=0;
nextRun2=0;
}
}
*/ }
//U2CTS=1;
//return (EXIT_SUCCESS);
}
void init_serial_port( U32 ulWantedBaud )
{
unsigned short usBRG;
/* Configure the UART and interrupts. */
usBRG = (unsigned short)(( (float)40000000/ ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);
OpenUART2( UART_EN, UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR, usBRG );
ConfigIntUART2( ( configKERNEL_INTERRUPT_PRIORITY + 1 ) | UART_INT_SUB_PR0 | UART_RX_INT_EN );
U2STAbits.UTXISEL = 0x00;
}
Just replace my "2" with a 1 and that is my exact init sequence for my UART on a PIC32MX745F512L
ulWantedBaud is my desired baud rate (usually 115200, but sometimes 9600 depending on my settings)
I can't post my interrupt handler b/c it is way too involved, but use the serial port API's they are much less complex. Also, I would suggest a simple Logic Analyzer like this one (Used on the TTL side of course).
Side Note: If you use the API's they take care of the TRIS and LAT for you.
Edit: Adding Pragmas
#pragma config DEBUG = OFF // Background Debugger disabled
#pragma config FPLLMUL = MUL_20 // PLL Multiplier: Multiply by 20
#pragma config FPLLIDIV = DIV_2 // PLL Input Divider: Divide by 2
#pragma config FPLLODIV = DIV_1 // PLL Output Divider: Divide by 1
#pragma config FWDTEN = OFF // WD timer: OFF
#pragma config POSCMOD = HS // Primary Oscillator Mode: High Speed xtal
#pragma config FNOSC = PRIPLL // Oscillator Selection: Primary oscillator w/ PLL
#pragma config FPBDIV = DIV_1 // Peripheral Bus Clock: Divide by 1 //Note: Application FPBDIV = DIV_2
#pragma config BWP = OFF // Boot write protect: OFF
#pragma config ICESEL = ICS_PGx2 // ICE pins configured on PGx2, Boot write protect OFF.
#pragma config WDTPS = PS1 // Watchdog Timer Postscale
#pragma config CP = OFF
#pragma config PWP = OFF
#pragma config UPLLEN = ON
#pragma config UPLLIDIV = DIV_2 // USB PLL Input Divider
#pragma config FSRSSEL = PRIORITY_7
#pragma config FMIIEN = OFF
#pragma config FVBUSONIO = OFF // VBUSON is controlled by the port
#pragma config FETHIO = ON // external PHY in RMII/alternate configuration
#pragma config FUSBIDIO = OFF // USB ID controlled by PORT function

cannot Enable SPI Peripheral of AT91SAM7S256

I am writing a code to interface micro SD card with AT91SAM7S ARM core, while debugging I had found that everything is right but the module cannot running as the enable bit is not set however i did it multi-times.
void SPI_Configure(AT91S_SPI *spi,
unsigned int id,
unsigned int configuration)
{
// Disable the interrupt first
AT91C_BASE_AIC->AIC_IDCR = 1 << id;
// Clear interrupt
AT91C_BASE_AIC->AIC_ICCR = 1 << id;
AT91C_BASE_PMC->PMC_PCER = 1 << id;
// Execute a software reset of the SPI twice
spi->SPI_CR = AT91C_SPI_SWRST ;
spi->SPI_CR = AT91C_SPI_SWRST | AT91C_SPI_SPIEN;
spi->SPI_MR = configuration;
// Disables the transmitter PDC transfer requests.
spi->SPI_PTCR = AT91C_PDC_RXTDIS | AT91C_PDC_TXTDIS;
spi->SPI_CR |= AT91C_SPI_SPIEN;
}
any one has any idea that may help me to solve this problem
thanks at all