alan broad
/
carbon_v5_arm_studio
arm studio build
src/commI2C.cpp@27:ee9c063a535b, 2020-06-10 (annotated)
- Committer:
- alan1974
- Date:
- Wed Jun 10 14:50:17 2020 +0000
- Revision:
- 27:ee9c063a535b
- Parent:
- 26:f51ff4ad7a93
- Child:
- 28:2e22e364a162
started adding gps time request
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alan1974 | 0:a91cd1b08360 | 1 | #include "mbed.h" |
alan1974 | 2:0af50f386eb2 | 2 | #include "global.h" |
alan1974 | 0:a91cd1b08360 | 3 | #include "commI2C.h" |
alan1974 | 0:a91cd1b08360 | 4 | |
alan1974 | 0:a91cd1b08360 | 5 | extern uint8_t buf_xmt[BUFFER_SIZE_I2C]; //outgoing data |
alan1974 | 0:a91cd1b08360 | 6 | extern uint8_t buf_rcv[BUFFER_SIZE_I2C]; //incoming data |
alan1974 | 0:a91cd1b08360 | 7 | I2CSlave slave(PB_9, PB_8); |
alan1974 | 0:a91cd1b08360 | 8 | DigitalInOut wake(PA_0); |
alan1974 | 0:a91cd1b08360 | 9 | |
alan1974 | 0:a91cd1b08360 | 10 | extern Serial pc; |
alan1974 | 5:abfe25f0de33 | 11 | extern bool verbose; |
alan1974 | 18:d95e1a2c4303 | 12 | //#define TEST |
alan1974 | 0:a91cd1b08360 | 13 | //============================================================================== |
alan1974 | 0:a91cd1b08360 | 14 | // i2c_proc_init |
alan1974 | 0:a91cd1b08360 | 15 | // - init i2c comm to proc |
alan1974 | 0:a91cd1b08360 | 16 | //============================================================================== |
alan1974 | 0:a91cd1b08360 | 17 | void i2c_proc_init() |
alan1974 | 0:a91cd1b08360 | 18 | { |
alan1974 | 0:a91cd1b08360 | 19 | slave.frequency(100000); // added DRT NOT NEEDED BY SLAVE????,asb, need this ???? |
alan1974 | 0:a91cd1b08360 | 20 | uint8_t slave_address = 0xA0; //PROC code uses 7bit address shifted by one so 0xA0 (xdot) => 0x50 (PROC) |
alan1974 | 17:74d60177c6b6 | 21 | slave.address(slave_address); |
alan1974 | 0:a91cd1b08360 | 22 | return; |
alan1974 | 0:a91cd1b08360 | 23 | } |
alan1974 | 0:a91cd1b08360 | 24 | //============================================================================== |
alan1974 | 0:a91cd1b08360 | 25 | // i2c_pulse_wake |
alan1974 | 0:a91cd1b08360 | 26 | // - pulse the wake signal to tell proc that xdot ready for i2c xfr |
alan1974 | 0:a91cd1b08360 | 27 | //============================================================================== |
alan1974 | 0:a91cd1b08360 | 28 | void i2c_pulse_wake(void){ |
alan1974 | 26:f51ff4ad7a93 | 29 | //wake.mode(OpenDrain); |
alan1974 | 0:a91cd1b08360 | 30 | wake.output(); |
alan1974 | 0:a91cd1b08360 | 31 | wake = 1; |
alan1974 | 18:d95e1a2c4303 | 32 | wait_ms(10); //proc uses interrupts on lora_wake, so don't need to wait,just pulse it |
alan1974 | 0:a91cd1b08360 | 33 | return; |
alan1974 | 0:a91cd1b08360 | 34 | } |
alan1974 | 0:a91cd1b08360 | 35 | //============================================================================== |
alan1974 | 0:a91cd1b08360 | 36 | // i2c_proc_comm |
alan1974 | 0:a91cd1b08360 | 37 | //- set WAKE low to notify proc that's OK to read/write i2c data |
alan1974 | 0:a91cd1b08360 | 38 | //- waits FOREVER or timeout for proc to write or read i2 data |
alan1974 | 0:a91cd1b08360 | 39 | // - returns point to read or write i2c bfr |
alan1974 | 0:a91cd1b08360 | 40 | //- returns true if xdot rcvd data into buf_rcv from proc |
alan1974 | 0:a91cd1b08360 | 41 | // false if xdot wrote data from buf_xmt to proc |
alan1974 | 0:a91cd1b08360 | 42 | //- before returning sets WAKE hi |
alan1974 | 0:a91cd1b08360 | 43 | //------------------------------------------------------------------------------ |
alan1974 | 0:a91cd1b08360 | 44 | // xdot i2c slave operation: |
alan1974 | 0:a91cd1b08360 | 45 | // - xdot does not work correctly as a i2c slave |
alan1974 | 0:a91cd1b08360 | 46 | // - if xdot does not respond to first incoming bytes from master (responseTime) |
alan1974 | 0:a91cd1b08360 | 47 | // within a defined period the xdot will hang the i2c bus until it is reset |
alan1974 | 8:a5316708e51d | 48 | // - the strategy here is sit in a very tight "while loop" polling continually for |
alan1974 | 0:a91cd1b08360 | 49 | // incoming i2c bytee within the respnseTime |
alan1974 | 8:a5316708e51d | 50 | // - time around the "while loop" must be < "responseTime" |
alan1974 | 8:a5316708e51d | 51 | // - "while loop" is measured at 1.25 usec |
alan1974 | 0:a91cd1b08360 | 52 | // - responseTimes upto 80 usec have been measured to be OK |
alan1974 | 0:a91cd1b08360 | 53 | //------------------------------------------------------------------------------ |
alan1974 | 0:a91cd1b08360 | 54 | // timeout: |
alan1974 | 0:a91cd1b08360 | 55 | // - safety mechanism to get xdot back to sleep mode if proc goes unresponsive |
alan1974 | 0:a91cd1b08360 | 56 | // - cpu clock dependant..what mhz is cpu? presently this will give 1.1sec timout before |
alan1974 | 0:a91cd1b08360 | 57 | // - 0x000fffff gives a 1.1 second timeout |
alan1974 | 0:a91cd1b08360 | 58 | // - after proc wakes xdot from sleep then proc must send an i2c omd within timout |
alan1974 | 0:a91cd1b08360 | 59 | // - once xdot pulses wake to notify proc that i2c ack msg is ready, proc must do i2c read within timeout |
alan1974 | 18:d95e1a2c4303 | 60 | // Jan2019: modified to handle large mcast code frags |
alan1974 | 18:d95e1a2c4303 | 61 | // - Input parameters: |
alan1974 | 18:d95e1a2c4303 | 62 | // buf_xmt: pointer to buffer containing I2C data for transfer to psco |
alan1974 | 18:d95e1a2c4303 | 63 | // buf_rcv: pointer to buffer continaing I2C data for transfer from psoc |
alan1974 | 0:a91cd1b08360 | 64 | //============================================================================== |
alan1974 | 18:d95e1a2c4303 | 65 | I2C_XFR_TYPE i2c_proc_comm(uint8_t *buf_xmt,uint8_t *buf_rcv,uint16_t bufSize) |
alan1974 | 0:a91cd1b08360 | 66 | { |
alan1974 | 0:a91cd1b08360 | 67 | uint32_t timeout = 0x000fffff; //1.1second timeout |
alan1974 | 0:a91cd1b08360 | 68 | |
alan1974 | 0:a91cd1b08360 | 69 | while(1){ |
alan1974 | 0:a91cd1b08360 | 70 | int i = slave.receive(); |
alan1974 | 0:a91cd1b08360 | 71 | switch (i) { |
alan1974 | 8:a5316708e51d | 72 | case I2CSlave::ReadAddressed: //xdot -> proc |
alan1974 | 26:f51ff4ad7a93 | 73 | slave.write((char *)buf_xmt,bufSize); |
alan1974 | 0:a91cd1b08360 | 74 | return I2C_WRITE; |
alan1974 | 0:a91cd1b08360 | 75 | case I2CSlave::WriteAddressed: //xdot <- proc |
alan1974 | 26:f51ff4ad7a93 | 76 | slave.read((char *)buf_rcv,bufSize); |
alan1974 | 0:a91cd1b08360 | 77 | if (verbose){ |
alan1974 | 0:a91cd1b08360 | 78 | pc.printf("\n\r Incoming buffer(hex): \n\r"); |
alan1974 | 0:a91cd1b08360 | 79 | int j; |
alan1974 | 0:a91cd1b08360 | 80 | for (j = 0; j < BUFFER_SIZE_I2C; j++){ |
alan1974 | 5:abfe25f0de33 | 81 | pc.printf("%x", buf_rcv[j]); |
alan1974 | 5:abfe25f0de33 | 82 | pc.printf(" "); |
alan1974 | 5:abfe25f0de33 | 83 | } |
alan1974 | 26:f51ff4ad7a93 | 84 | } |
alan1974 | 0:a91cd1b08360 | 85 | return I2C_READ; |
alan1974 | 0:a91cd1b08360 | 86 | default: |
alan1974 | 0:a91cd1b08360 | 87 | }; //switch |
alan1974 | 0:a91cd1b08360 | 88 | |
alan1974 | 0:a91cd1b08360 | 89 | timeout--; |
alan1974 | 9:cc23b2049639 | 90 | if(timeout == 0){ |
alan1974 | 9:cc23b2049639 | 91 | pc.printf("\r\n I2C TIMEOUT"); |
alan1974 | 9:cc23b2049639 | 92 | return I2C_WRITE; //just go to sleep |
alan1974 | 9:cc23b2049639 | 93 | } |
alan1974 | 0:a91cd1b08360 | 94 | }; //while |
alan1974 | 0:a91cd1b08360 | 95 | } |