STM32F207 bare metal SPI RXNE and TXE not working? - arm

I need to communicate with an eeprom chip(25lc1024) via SPI.
I can get the following code to work but have to resort to delays isntead of the flags alone.
Otherwise I can see on the scope that it is not waiting for all the bits to be shifted out and proceeds.
Any ideas why the flags are not stopping it?
void Init_SPI(void){
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; // enable SPI clock
GPIOA->AFR[0] |= SET_AFL_A_FOR_SPI1;
GPIOB->AFR[0] |= SET_AFL_B_FOR_SPI1;
SPI1_NSS = DESELECT_CHIP; // set the SS pin high
// initialize the SPI configuration register
SPI1->CR1 = SPI_CR1_MSTR // SPI master mode
| SPI_CR1_BR_1; // 010 bit rate prescale /8 (60MHz/8 = 7.5MHz)
SPI1->CR2 = SPI_CR2_SSOE;
SPI1->CR1 |= SPI_CR1_SPE; // enable SPI
}
unsigned int eeprom_read(unsigned long address, unsigned int chars_to_read)
{
char temp;
unsigned int result = 0;
SPI1_NSS = SELECT_CHIP; // set the SS pin low
SPI1->DR = READ_FROM_SPI; // send data out SPI
while( !(SPI1->SR & SPI_SR_TXE) ); // wait until transmit buffer empty
SPI1->DR = (address & 0xFF0000) >> ADJ16; // send data out SPI
while( !(SPI1->SR & SPI_SR_TXE) ); // wait until transmit buffer empty
SPI1->DR = (address & 0x00FF00) >> ADJ8; // send data out SPI
while( !(SPI1->SR & SPI_SR_TXE) ); // wait until transmit buffer empty
SPI1->DR = (address & 0x0000FF); // send data out SPI
while( !(SPI1->SR & SPI_SR_TXE) ); // wait until transmit buffer empty
delay();
temp = SPI1->DR;
SPI1->DR = DUMMY_8BIT; // send dummy 8 bits to generate clock
chars_to_read--;
while( !(SPI1->SR & SPI_SR_RXNE) ); // wait until receive buffer is not empty
//USART2->DR = SPI1->DR;
result = SPI1->DR;
if(chars_to_read != 0){
while((SPI1->SR & SPI_SR_RXNE) ); // wait until receive buffer is empty
delay();
SPI1->DR = DUMMY_8BIT; // send dummy 8 bits to generate clock
//delay();
while( !(SPI1->SR & SPI_SR_TXE) ); // wait until transmit buffer is empty
while( !(SPI1->SR & SPI_SR_RXNE) ); // wait until receive buffer is not empty
temp = SPI1->DR;
result = (result*256) + temp;
chars_to_read--;
}
delay();
SPI1_NSS = DESELECT_CHIP; // set the SS pin high
return result;
}
static void delay(void)
{
unsigned int j = FALSE;
for (j = 0; j < 25; j++) {
}
}

Related

Havig trouble with SPI in PIC24FJ256GB412

I am trying to configure a PIC24FJ256GB412 to use the SPI interface with an ADC module(ADS114S08). And now, I can view data by oscilloscope(by measuring the SDI pin). But the SPI1BUFL didn't receive correct data(always received 0x01). Below is the relevant code that I am using to configure the SPI.
uint8_t DATA_S[1000]; // SPI data buffer for Receiving
uint8_t DATA1; // SPI data buffer for Receiving
uint16_t DATA_counter=0;
void System_Initial(void)
{
//Setup IO
TRISDbits.TRISD0 = 0; //SCLK
TRISDbits.TRISD5 = 0; //ADC RESET
TRISDbits.TRISD10 = 0; //ADC START
TRISDbits.TRISD11 = 1; //ADC DRDY
TRISFbits.TRISF3 = 1; //SDI
TRISFbits.TRISF4 = 0; //SDO
ANSELDbits.ANSELD0 = 0; //Digital pins
ANSELDbits.ANSELD5 = 0;
ANSELDbits.ANSELD10 = 0;
ANSELDbits.ANSELD11 = 0;
ANSELFbits.ANSELF3 = 0;
ANSELFbits.ANSELF4 = 0;
//Setup SPI
//MCU operates in 16MHz, ADC internal clk 4MHz
SPI1BRGL = 1; //Baud rate = 4MHz
SPI1CON1L = 0x8020; //8bit mode //SCLK enable //Master Mode
SPI1CON1H = 0x2000; //AUDEN=0
SPI1CON2L = 0x0007; //8bit data
//Setup PPS
__builtin_write_OSCCONL(OSCCON & 0xBF);
_RP11R = _RPOUT_SCK1OUT; // RP11 -> SCK1.
RPINR20bits.SDI1R = 16; // RP16 -> SDI1.
_RP10R = _RPOUT_SDO1; // RP10 -> SDO1.
__builtin_write_OSCCONL(OSCCON | 0x40);
}
//SPI1 - Set ADS114S08
void SPI1_SetADC()
{
//set PGA
//Input MUX Register, address = 02h
//Gain setting Register, address = 03h
SPI1BUFL = 0x42; //WREG at 02h
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x01; //Two bytes
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x01; //02h //AINP = AIN0, AINN = AIN1
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x0B; //03h //PGA enabled, Gain = 011 =8
while( SPI1STATLbits.SPIBUSY==1);
//set Mode
//Data Rate Register, address = 04h
//Reference Control Register, address = 05h
SPI1BUFL = 0x44; //WREG at 04h
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x01; //Two bytes
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x14; //04h //Continuous conversion mode //0100 : 20 SPS
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x30; //05h
while( SPI1STATLbits.SPIBUSY==1);
//set Excitation Current Sources
//Excitation Current Register1(IDACMAG), address = 06h
//Excitation Current Register2(IDACMUX), address = 07h
SPI1BUFL = 0x46; //WREG at 06h
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x01; //Two bytes
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x05; //06h //IDAC = 0101 = 500uA
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0xF0; //07h //use IDAC1 => AIN0
while( SPI1STATLbits.SPIBUSY==1);
}
//SPI1 - RREG
void SPI1_RREG()
{
SPI1BUFL = 0x20; //send RREG Command //read 00h
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x00; //1 byte
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x0000; //send DUMMY data
while( SPI1STATLbits.SPIBUSY==1); //Wait Data
uint8_t register1= SPI1BUFL; //get data
DATA1 = register1;
DATA_S[DATA_counter%1000] = register1;
}
int main(void)
{
System_Initial();
LATDbits.LATD10 = 0; //ADC START=0
LATDbits.LATD5 = 0; //ADC RESET=0 //Enter RESET Status
uint32_t loop = 0;
loop = 4*4; //MCU 16Mhz, ADC internal clk 4Mhz
while (loop--); //wait 4clk => ADS114S08 tw(RSL)
LATDbits.LATD5 = 1; //ADC RESET low -> high //Enter Standby Mode
loop=4096*4;
while (loop--); //wait 4096clk => ADS114S08 td(RSSC)
SPI1_SetADC(); //set ADC Configuration
LATDbits.LATD10 = 1; //START high -> Start Conversion
//main while
while(1)
{
DATA_counter++;
SPI1_RREG();
}
}
bold
italic
As mentioned above, I can see data on SDI pin by oscilloscope.
enter image description here
And another data test.
enter image description here
By the waveform that we can see on the oscilloscope. The ADC module probably works the right way.
But... Let's see the MPLAB IDE side.
enter image description here
I can't get the right data(SPI1BUF = 0x0001). I have tried changing baudrate lower. But the result is no different. Is there a parameter I'm missing? Thanks for your help.
------------------------------------------0530-----------------------------------------------
Hi Craig, I have tried the code.
SPI1BUFL = 0x20; //send RREG Command //read 00h
while(SPI1STATLbits.SPITBF); // wait until the transmit buffer is empty
SPI1BUFL = 0x00; //set 1 byte
while(SPI1STATLbits.SPITBF); // wait until the transmit buffer is empty
uint8_t register1=0;
SPI1BUFL = 0x00; //send DUMMY data
while(!SPI1STATLbits.SPIRBF); //hang until data is received
register1 = SPI1BUFL; //get data
The results were no difference. And I recorded it.
Oscilloscope:
enter image description here
IDE:
enter image description here
enter image description here
The SPI should clear SPI1BUFL every time as you transmit data.
So I try the code like this.
uint8_t SPI_EXCH(uint8_t data)
{
SPI1BUFL = data; // write to buffer for TX
while(!SPI1STATLbits.SPIRBF); // wait for transfer to complete
return SPI1BUFL; // read the received value
}
And every time you try to do an SPI exchange, use the function above.

Unreliable PEC on stm32 smbus

I've got a problem with PEC on STM32 SMBUS, which i use to read data from a MLX90614 IR thermometer. When i start the device, either the PECERR flag is set and continues to be set, for all transmissions, even though the data from the IR thermometer seems to be correct. Or the PECERR is never set and all the data from the IR thermometer is still correct.
When i study the data on my oscilloscope i can see no difference between the signals when the PECERR is set or not. As stated earlier data seems to be good either way.
I could of course just ignore the PECERR flag, but i'd lige to be able to filter out any eventual garbled transmissions. Anyone have an idea what i am doing wrong here?
void I2C_SMBUS_Initialize(I2C_TypeDef *h) {
h->CR2 &= ~I2C_CR2_FREQ; // Clear frequency part of register
h->CR2 |= 0x8; // Clock speed in Mhz
h->OAR1 = 0x4000;
h->CCR = 0;
h->CCR &= ~I2C_CCR_DUTY;
h->CCR |= 0x190;
h->TRISE &= ~I2C_TRISE_TRISE; // Clear TRISE bits
h->TRISE |= 0x9; // Set TRISE
h->CR1 |= I2C_CR1_ENPEC; // Enable packet error check
h->CR1 |= I2C_CR1_SMBTYPE; // SMBUS host
h->CR1 |= I2C_CR1_SMBUS; // SMBUS Mode
h->CR1 |= I2C_CR1_PE; // Start i2c
}
uint16_t I2C_SMBUS_ReadWord(I2C_TypeDef* h, uint8_t deviceAddress, uint8_t command) {
static const uint16_t ERROR_CODE = 0x3BFF;
//static const uint8_t deviceAddress = 0x5A;
static const uint8_t timeout = 100;
uint16_t temp = 0;
h->CR1 &= ~I2C_CR1_POS;
// Generate start bit
sendStartBit(h);
// Wait for start bit set
if (!waitFlag((&h->SR1), I2C_SR1_SB, BIT_SET, timeout)) {
DEBUG_PUTS("Timeout while waiting for start bit set");
return ERROR_CODE;
}
// Address byte. 7 bit. Shifted one lefet
sendAddress(h, deviceAddress, I2C_WRITE);
// Wait for address bit set
if (!waitFlag((&h->SR1), I2C_SR1_ADDR, BIT_SET, timeout)) {
DEBUG_PUTS("Timeout while waiting for address bit set");
return ERROR_CODE;
}
// Clear ADDR bit
clearAddressFlag(h);
sendData(h, command);
// wait for tx buffer empty
if (!waitFlag((&h->SR1), I2C_SR1_TXE, BIT_SET, timeout)) {
DEBUG_PUTS("Timeout while waiting for buffer empty");
return ERROR_CODE;
}
uint8_t length = 3;
uint8_t tmpBuffer[length+1];
memset(tmpBuffer, 0x00, 4);
// Enable automatic ACK generation
enableAutomaticACK(h);
// Generate start bit
sendStartBit(h);
// Wait for start bit set
if (!waitFlag((&h->SR1), I2C_SR1_SB, BIT_SET, timeout)) {
DEBUG_PUTS("Timeout while waiting for repeted start bit set");
return ERROR_CODE;
}
// Send the read command to the slave address
sendAddress(h, deviceAddress, I2C_READ);
// Wait for address bit set
if (!waitFlag((&h->SR1), I2C_SR1_ADDR, BIT_SET, timeout)) {
DEBUG_PUTS("Timeout while waiting for address bit set");
return ERROR_CODE;
}
// Clear ADDR bit by reading status register
clearAddressFlag(h);
// Now we must read the data from the slave
if (length > 2) {
// Receive the first n-2 bytes
for (uint8_t i=0; i < length-2; i++) {
// Wait for Byte Transfer ready
if (!waitFlag((&h->SR1), I2C_SR1_BTF, BIT_RESET, timeout)) {
DEBUG_PUTS("Timeout while waiting for Byte Transfer ready");
return ERROR_CODE;
}
tmpBuffer[i] = h->DR;
// Wait for Byte Transfer Finished
if (!waitFlag((&h->SR1), I2C_SR1_BTF, BIT_SET, timeout)) {
DEBUG_PUTS("Timeout while waiting for Byte Transfer Finished");
return ERROR_CODE;
}
}
// Wait for Byte Transfer ready
if (!waitFlag((&h->SR1), I2C_SR1_BTF, BIT_RESET, timeout)) {
DEBUG_PUTS("Timeout while waiting for Byte Transfer ready");
return ERROR_CODE;
}
// Disable automatic ACK generation
disableAutomaticACK(h);
// Read the second last byte
tmpBuffer[length-1] = h->DR;
// Send stop bit
sendStopBit(h);
// Enable packet error check
h->CR1 |= I2C_CR1_PEC;
// Read the last byte
tmpBuffer[length] = h->DR;
temp = tmpBuffer[3]*256 + tmpBuffer[2];
uint8_t pec = h->SR2 &= I2C_SR2_PEC_Msk;
if ((h->SR1 & I2C_SR1_PECERR) != 0) {
puts("PEC ERROR");
}
}
return temp;
}
Found the bug. PECERR must be cleared by software. The first transmission after power on occasionally fails.
if ((h->SR1 & I2C_SR1_PECERR) != 0) {
DEBUG_PUTS("PEC ERROR");
h->SR1 &= ~I2C_SR1_PECERR;
return ERROR_CODE;
}

AVR 9 bit Usart not working (MPCM)

So there is Master and Slave Attiny2313. The Master sends the 9 bits of data(the 9th bit TXB8 is set to 1), but the slave doesn't detect the 9th bit(RXB8 is still 0).
I think if the TXB8 bit is set in the Master the RXB8 bit on the Slave should be set automatically, or not? (In the code i check if RXB8 in UCSRB is set to 1.
And it isn't, that is the problem)
void USART_Init(void)
{
/* Set baud rate */
UBRRH = (unsigned char)(BAUDRATE>>8);
UBRRL = (unsigned char)BAUDRATE;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 9data, 1stop bit */
UCSRC = (7<<UCSZ0);
}
void USART_Transmit(unsigned int data)
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) );
/* Copy 9th bit to TXB8 */
UCSRB &= ~(1<<TXB8);
if ( data & 0x0100 )
UCSRB |= (1<<TXB8);
/* Put data into buffer, sends the data */
UDR = data;
//Slave Receive Code
gned int USART_Receive( void )
{
unsigned char status, resh, resl;
/* Wait for data to be received */
while ( !(UCSRA & (1<<RXC)) );
/* Get status and 9th bit, then data from buffer */
status = UCSRA;
resh = UCSRB;
resl = UDR;
return resh; ///test
/* If error, return -1 */
if ( status & ((1<<FE)|(1<<DOR)|(1<<UPE)) )
return -1;
/* Filter the 9th bit, then return */
resh = (resh >> 1) & 0x01;
return ((resh << 8) | resl);
}
I found what was the problem, the USART was initialised like this:
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 9data, 1stop bit */
UCSRC = (7<<UCSZ0);
But the UCSZ2 bit is in the USCRB register and not in UCSRC, so the right code will be:
UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<UCSZ2);
UCSRC = (1<<UCSZ0)|(1<<UCSZ1);

No response from UART

I'm able to receive with the following code, but unfortunately, nothing is sent back. What am I doing wrong?
#include <pic18f25k80.h>
#include "config.h"
#include <usart.h>
int i = 0;
unsigned char MessageBuffer[200];
void main() {
OSCCONbits.IRCF = 0b110; // 8MHz
TRISB6 = 0; // TX set as output
TRISB7 = 0; // RX set as output
// Clear TX interrupt
// Set RX interrupt
// 8-bit Asynch. mode
// BRGH = 1 = high baud mode
// 51 = ((8MHz/baud)/16)-1 with baud = 9600
Open2USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE
& USART_EIGHT_BIT & USART_BRGH_HIGH, 51 );
RC2IF = 0; // reset RX2 flag
RC2IP = 0; // not high priority
RC2IE = 1; // Eneble RX2 interrupt
INTCONbits.PEIE = 1; // enable peripheral interrupts
INTCONbits.GIE = 1; // enable interrupts
RCSTA2bits.SPEN = 1; // enable USART
while(1){
}
}
void interrupt ISR () {
if(PIR3bits.RC2IF == 1) {
if(i<200) { // buffer size
MessageBuffer[i] = Read2USART(); // read byte from RX reg
if (MessageBuffer[i] == 0x0D) { // check for return key
puts2USART(MessageBuffer);
for(;i>0;i--)
MessageBuffer[i] = 0x00; // clear array
i=0;
return;
}
i++;
RC2IF = 0; // clear RX flag
} else {
puts2USART(MessageBuffer);
for(;i>0;i--)
MessageBuffer[i] = 0x00; // clear array
i = 0;
return;
}
}
}
I'm transmitting the 0x41 hex code, I checked with the scope and see that is is being received. And according to the code I have, an echo of the received data should be sent back. When I check the TX pin, nothing is happening.
Add USART_CONT_RX to Open2USART to enable continuous receive.
Also, it's a good idea to do the minimum necessary in the interrupt service routine. Consider something like:
void interrupt ISR () {
char data;
if(PIR3bits.RC2IF == 1) {
data = Read2USART(); // always read byte from RX reg (clears RC2IF)
if(i<200) { // buffer size
MessageBuffer[i] = data; // read byte from RX reg
i++;
}
else{
// flag buffer full error
}
}
}
and doing the rest of what you are doing in the while(1) loop.

AVR Butterfly UART - can't receive data

I am using the UART of Atmega169/AVR Butterfly for transmission to another board, baudrate 56700, no parity, 1 stopbit, no flow control. The oscillator is running at 7,3768Mhz (checked). I can transmit data successfully (checked with the other board and PC/HyperTerminal), but not receive any data - when running the debugger the configuration bits are all set correctly, but RXC is false constantly - I also checked if I can send data to myself (connected TXD to RXD and grounded), but without success. (Tried with ISR as well as polling)
Below are the relevant parts of the code, I hope you can deal with it - PORTB is used as output for testing with the oscilloscope (I know I could just use one pin, but there is nothing else on PORTB right now):
int main(void){
OSCCAL_Calibrate(); // calibrate the internal oscillator
int UBRR_VAL = ((F_CPU)/(BAUD*16)-1);
UART_Init(UBRR_VAL);
DDRB |= 0xFF;
PORTB = 0;
testCharSend();
while(1);
return 0;
}
void testCharSend()
{
char i = 'x';
while(1){
Uart_Tx(i);
}
}
void UART_Init(unsigned int baudrate)
{
// Set baud rate
UBRRH = (unsigned char)(baudrate>>8);
UBRRL = (unsigned char)baudrate;
UCSRA = 0;
// Enable receiver and transmitter
UCSRB = (1<<RXEN)|(1<<TXEN);
// Async. mode, 8bit, No parity, 1 stop bit (like CC2540)
UCSRC = (0<<UMSEL)|(0<<UPM0)|(0<<USBS)|(3<<UCSZ0)|(0<<UCPOL);
// enable receive interrupt
UCSRB |= (1<<RXCIE);
// flush UART
UART_Flush();
}
void UART_Flush( void )
{
unsigned char dummy;
while ( UCSRA & (1<<RXC) ) dummy = UDR;
}
void Uart_Tx(char data)
{
while (!(UCSRA & (1<<UDRE)));
UDR = data;
}
ISR (USART0_RX_vect)
{
PORTB ^= 0xFF;
char c = UDR;
}
OK, I tested the connections with an oscilloscope, the RXD line on the board was broken, switched the board and now it's working, so the code above is valid!

Resources