123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- #include "cmt_spi3.h"
- #include "myRadio_gpio.h"
- /* ************************************************************************
- * The following need to be modified by user
- * ************************************************************************ */
- // #define cmt_spi3_csb_out() SET_GPIO_OUT(CMT_CSB_GPIO)
- // #define cmt_spi3_fcsb_out() SET_GPIO_OUT(CMT_FCSB_GPIO)
- // #define cmt_spi3_scl_out() SET_GPIO_OUT(CMT_SCL_GPIO)
- // #define cmt_spi3_sda_out() SET_GPIO_OUT(CMT_SDA_GPIO)
- // #define cmt_spi3_sda_in() SET_GPIO_IN(CMT_SDA_GPIO)
- // #define cmt_spi3_csb_1() SET_GPIO_H(CMT_CSB_GPIO)
- // #define cmt_spi3_csb_0() SET_GPIO_L(CMT_CSB_GPIO)
- // #define cmt_spi3_fcsb_1() SET_GPIO_H(CMT_FCSB_GPIO)
- // #define cmt_spi3_fcsb_0() SET_GPIO_L(CMT_FCSB_GPIO)
-
- // #define cmt_spi3_scl_1() SET_GPIO_H(CMT_SCL_GPIO)
- // #define cmt_spi3_scl_0() SET_GPIO_L(CMT_SCL_GPIO)
- // #define cmt_spi3_sda_1() SET_GPIO_H(CMT_SDA_GPIO)
- // #define cmt_spi3_sda_0() SET_GPIO_L(CMT_SDA_GPIO)
- // #define cmt_spi3_sda_read() READ_GPIO_PIN(CMT_SDA_GPIO)
- /* ************************************************************************ */
-
- // void cmt_spi3_delay(void)
- // {
- // uint32_t n = 7;
- // while(n--);
- // }
- // void cmt_spi3_delay_us(void)
- // {
- // uint16_t n = 8;
- // while(n--);
- // }
- void cmt_spi3_init(void)
- {
- // cmt_spi3_csb_1();
- // cmt_spi3_csb_out();
- // cmt_spi3_csb_1(); /* CSB has an internal pull-up resistor */
-
- // cmt_spi3_scl_0();
- // cmt_spi3_scl_out();
- // cmt_spi3_scl_0(); /* SCL has an internal pull-down resistor */
-
- // cmt_spi3_sda_1();
- // cmt_spi3_sda_out();
- // cmt_spi3_sda_1();
-
- // cmt_spi3_fcsb_1();
- // cmt_spi3_fcsb_out();
- // cmt_spi3_fcsb_1(); /* FCSB has an internal pull-up resistor */
- // cmt_spi3_delay();
- }
- // void cmt_spi3_send(uint8_t data8)
- // {
- // uint8_t i;
- // for(i=0; i<8; i++)
- // {
- // cmt_spi3_scl_0();
- // /* Send byte on the rising edge of SCL */
- // if(data8 & 0x80)
- // cmt_spi3_sda_1();
- // else
- // cmt_spi3_sda_0();
- // cmt_spi3_delay();
- // data8 <<= 1;
- // cmt_spi3_scl_1();
- // cmt_spi3_delay();
- // }
- // }
- // uint8_t cmt_spi3_recv(void)
- // {
- // uint8_t i;
- // uint8_t data8 = 0xFF;
- // // for(i=0; i<8; i++)
- // // {
- // // cmt_spi3_scl_0();
- // // cmt_spi3_delay();
- // // data8 <<= 1;
- // // cmt_spi3_scl_1();
- // // /* Read byte on the rising edge of SCL */
- // // if(cmt_spi3_sda_read())
- // // data8 |= 0x01;
- // // else
- // // data8 &= ~0x01;
- // // cmt_spi3_delay();
- // // }
- // return data8;
- // }
- void cmt_spi3_write(uint8_t addr, uint8_t dat)
- {
- cmt_spi3_sda_1();
- cmt_spi3_sda_out();
- cmt_spi3_scl_0();
- cmt_spi3_scl_out();
- cmt_spi3_scl_0();
- cmt_spi3_fcsb_1();
- // cmt_spi3_fcsb_out();
- cmt_spi3_fcsb_1();
- cmt_spi3_csb_0();
- /* > 0.5 SCL cycle */
- cmt_spi3_delay();
- cmt_spi3_delay();
- /* r/w = 0 */
- cmt_spi3_send(addr&0x7F);
- cmt_spi3_send(dat);
- cmt_spi3_scl_0();
- /* > 0.5 SCL cycle */
- cmt_spi3_delay();
- cmt_spi3_delay();
- cmt_spi3_csb_1();
-
- cmt_spi3_sda_1();
- cmt_spi3_sda_in();
-
- cmt_spi3_fcsb_1();
- }
- void cmt_spi3_read(uint8_t addr, uint8_t* p_dat)
- {
- cmt_spi3_sda_1();
- cmt_spi3_sda_out();
- cmt_spi3_scl_0();
- cmt_spi3_scl_out();
- cmt_spi3_scl_0();
- cmt_spi3_fcsb_1();
- // cmt_spi3_fcsb_out();
- cmt_spi3_fcsb_1();
- cmt_spi3_csb_0();
- /* > 0.5 SCL cycle */
- cmt_spi3_delay();
- cmt_spi3_delay();
- /* r/w = 1 */
- cmt_spi3_send(addr|0x80);
- /* Must set SDA to input before the falling edge of SCL */
- cmt_spi3_sda_in();
-
- *p_dat = cmt_spi3_recv();
- cmt_spi3_scl_0();
- /* > 0.5 SCL cycle */
- cmt_spi3_delay();
- cmt_spi3_delay();
- cmt_spi3_csb_1();
-
- cmt_spi3_sda_1();
- cmt_spi3_sda_in();
-
- cmt_spi3_fcsb_1();
- }
- void cmt_spi3_write_fifo(const uint8_t* p_buf, uint16_t len)
- {
- uint16_t i;
- cmt_spi3_fcsb_1();
- // cmt_spi3_fcsb_out();
- cmt_spi3_fcsb_1();
- cmt_spi3_csb_1();
- // cmt_spi3_csb_out();
- cmt_spi3_csb_1();
- cmt_spi3_scl_0();
- cmt_spi3_scl_out();
- cmt_spi3_scl_0();
- cmt_spi3_sda_out();
- for(i=0; i<len; i++)
- {
- cmt_spi3_fcsb_0();
- /* > 1 SCL cycle */
- cmt_spi3_delay();
- cmt_spi3_delay();
- cmt_spi3_send(p_buf[i]);
- cmt_spi3_scl_0();
- /* > 2 us */
- cmt_spi3_delay_us();
- cmt_spi3_delay_us();
- cmt_spi3_delay_us();
- cmt_spi3_fcsb_1();
- /* > 4 us */
- cmt_spi3_delay_us();
- cmt_spi3_delay_us();
- cmt_spi3_delay_us();
- cmt_spi3_delay_us();
- cmt_spi3_delay_us();
- cmt_spi3_delay_us();
- }
- cmt_spi3_sda_in();
-
- cmt_spi3_fcsb_1();
- }
- void cmt_spi3_read_fifo(uint8_t* p_buf, uint16_t len)
- {
- uint16_t i;
- cmt_spi3_fcsb_1();
- // cmt_spi3_fcsb_out();
- cmt_spi3_fcsb_1();
- cmt_spi3_csb_1();
- // cmt_spi3_csb_out();
- cmt_spi3_csb_1();
- cmt_spi3_scl_0();
- cmt_spi3_scl_out();
- cmt_spi3_scl_0();
- cmt_spi3_sda_in();
- for(i=0; i<len; i++)
- {
- cmt_spi3_fcsb_0();
- /* > 1 SCL cycle */
- cmt_spi3_delay();
- cmt_spi3_delay();
- p_buf[i] = cmt_spi3_recv();
- cmt_spi3_scl_0();
- /* > 2 us */
- cmt_spi3_delay_us();
- cmt_spi3_delay_us();
- cmt_spi3_delay_us();
- cmt_spi3_fcsb_1();
- /* > 4 us */
- cmt_spi3_delay_us();
- cmt_spi3_delay_us();
- cmt_spi3_delay_us();
- cmt_spi3_delay_us();
- cmt_spi3_delay_us();
- cmt_spi3_delay_us();
- }
- cmt_spi3_sda_in();
-
- cmt_spi3_fcsb_1();
- }
|