Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 0:d488d550469e
- Child:
- 1:1e577b8befeb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun May 11 07:39:13 2014 +0000 @@ -0,0 +1,121 @@ +#include "mbed.h" + +DigitalInOut PDIO(p30); +DigitalOut CSn(p21); +Serial pc(USBTX, USBRX); + +int read_temperature(I2C i2c, int addr, float *temperature) +{ + char data[5]; + int status = i2c.read(addr, data, 5); + *temperature = data[4]*0.667-75; + return status; +} + +void OTP_setup() +{ + DigitalOut CLK(p27); + PDIO.output(); + PDIO = 1; + CLK = 0; + CSn = 0; + wait_ms(1); + CSn = 1; + wait_us(30); + CSn = 0; + wait_us(30); + CLK = 1; + wait_us(30); + CLK = 0; + wait_us(30); +} + +void OTP_exit() +{ + DigitalOut CLK(p27); + CLK = 0; + CSn = 0; + PDIO = 1; + wait_us(30); + CLK = 1; + wait_us(30); + CLK = 0; + wait_us(30); + CLK = 1; + wait_us(30); + CSn = 1; + wait_us(30); + +} + +void OTP_read(char *data, int length) +{ + DigitalOut CLK(p27); + CLK = 1; + PDIO = 0; + wait_us(30); + CSn = 1; + wait_us(30); + CLK = 0; + wait_us(30); + + //generate two CLK pulses + for(int i=0; i<2; i++) + { + CLK = 1; + wait_us(30); + CLK = 0; + wait_us(15); + + //switch PDIO to input on first faling edge of CLK + if(i==0) + PDIO.input(); + wait_us(15); + } + + uint32_t factory_section = 0; + uint8_t I2C_A = 0; //I2C address <5:1> + uint8_t CCW = 0; //change increasing/decreasing code with encoder movement + uint16_t Z = 0; //zero position + + //read all parameters + read_bits(&factory_section, 29); + read_bits(&I2C_A, 5); + read_bits(&CCW, 1); + read_bits(&Z, 12); +} + +int scan_NSE5310() +{ + CSn = 0; + I2C i2c(p28, p27); + int device_count = 0; + for(int addr=0x80; addr<0xFE; addr+=2) + { + float temperature; + //pc.printf("addr:0x%02X\n", addr); + if(read_temperature(i2c, addr, &temperature)==0) + { + pc.printf("Found device: 0x%02X, Temperature: %2.1fC\n", addr, temperature); + device_count++; + } + } + return device_count; +} + +int main() { + pc.baud(115200); + pc.printf("Scanning for NSE5310 devices...\n"); + int device_count = scan_NSE5310(); + //pc.printf("device_count=%d\n", device_count); + if(device_count==0) + pc.printf("No device found. Completed.\n"); + else if(device_count>1) + pc.printf("Found more than one device. Completed.\n"); + else if(device_count==1) + { + pc.printf("Entering programming mode...\n"); + OTP_setup(); + } + while(1); +}