This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF52 Interfacing sensor with serial through UART

I have a sensor with serial protocol of 7 bits data, even parity, 2 stop bits. I wanted to use uart library to interface with this sensor. But I couldn't find the settings to change data bits, even/odd parity or number of stop bits. Is it possible to do so with uart library? Or I have to write functions by myself to read bit by bit? Thanks

  • Hi RK, I kept the nrf_delay structure and added some checking functions in start bit waiting while loop. Now I can get out of the loop if anything happens and keep the device running.

    The issue now is every now and then I see faulty numbers. The sensor output consists of 9 or 10 digits, each digit is an ASCII character with 1 start bit, 7 data bits, 1 parity bit and 2 stop bits. The faulty number usually only has one digit wrong, e.g. I have seen -8.04745, -0.84745, -0.04705, -0.0474 for reading -0.04745. Is it because of interrupts of softdevice? And the reason it only affects one digit is that there is wait period between frames anyway?

    I collected some data, usually there are 5-15 wrong numbers in every 1000 samples. I'm considering just discarding those wrong numbers. Is there any callback function of softdevice interrupt in which I can flag and discard the most recent reading?

  • can't really say what it's likely to be at this point. Bitbanging the UART just isn't going to be that reliable, unfortunately. I don't understand your last question, you're reading the data, you can determine whether or not to send it or discard it, that has nothing to do with the softdevice.

    I looked to see if there were any cheap SPI/I2C --> UART bridge chips, because honestly you'd be better off doing that, however they were all more expensive than I would have expected.

    Your last option might be to go find the timeslot api which gives you a slot of time you get FULL control of everything. Use that to do your reading at a much higher baudrate (if your data providing chip supports that). Or find a different chip to interface with which uses a supported UART format or a different transport.

  • I mean if there is certain type of softdevice interrupt taking long time, I can discard data when it happens. I don't see wrong numbers a lot, I assume it doesn't happen quite often. If this is not possible, I cannot tell whether the reading is real measurement or contains wrong bit because of UART transmission.

    I have never used timeslot API, it looks like it's only good for period of 100~125 us? My data transmission takes ~20 ms though.

    You mentioned using timer interrupt earlier.

    1. I remember app timer has resolution of about 30 us. How could I get a timer with higher resolution?
    2. Another issue I could anticipate is the wait time between stop bits and the next start bit is not consistent. That means I can't keep the timer on I have to start the timer when start bit is detected and stop it after I read 7 data bits. Is it possible?
    3. Do I change timer priority in app_timer.c?
  • The Application timer (app_timer) use RTC, if you want higher resolution you need to use a timer peripheral. You can read more about the differences in this answer. The SDK have timer example, that show how to use the peripheral using the driver.

Related